From 625aafd0eee0a75ee3e21cf6cfdd0a5c8a7eb5f8 Mon Sep 17 00:00:00 2001 From: Naichen Zhao Date: Fri, 10 Nov 2023 18:41:26 -0800 Subject: [PATCH] fixed typo hard coded STM32 support we dont need STM_main anymore updated files to move stuff out of core cmake squashed --- core/platform/Platform.cmake | 2 +- core/platform/lf_STM32f4_support.c | 193 ++++++++++----------- include/core/platform/lf_STM32f4_support.h | 4 - 3 files changed, 92 insertions(+), 107 deletions(-) diff --git a/core/platform/Platform.cmake b/core/platform/Platform.cmake index 10797a1c5..2d082bce6 100644 --- a/core/platform/Platform.cmake +++ b/core/platform/Platform.cmake @@ -10,7 +10,7 @@ elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows") message("Using Windows SDK version ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}") elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Zephyr") set(LF_PLATFORM_FILE lf_zephyr_support.c) -elseif(${CMAKE_SYSTEM_NAME} STREQUAL "STM32f4") +elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Stm32") set(LF_PLATFORM_FILE lf_STM32f4_support.c) elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Rp2040") message("Using pico-sdk for RP2040 target") diff --git a/core/platform/lf_STM32f4_support.c b/core/platform/lf_STM32f4_support.c index 4223244d8..77f0d9add 100644 --- a/core/platform/lf_STM32f4_support.c +++ b/core/platform/lf_STM32f4_support.c @@ -1,4 +1,4 @@ -#ifdef defined(PLATFORM_STM32F4) +#if defined(PLATFORM_STM32F4) /************* I hope this software works LOL ***************/ @@ -28,8 +28,8 @@ static uint32_t _lf_time_us_high = 0; #define COMBINE_HI_LO(hi, lo) ((((uint64_t)hi) << 32) | ((uint64_t)lo)) - - +void lf_SystemClock_Config(); +void Error_Handler(); // + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + // | Code for timer functions @@ -38,35 +38,35 @@ static uint32_t _lf_time_us_high = 0; // We use timer 5 for our clock (probably better than fucking with sysTick) void _lf_initialize_clock(void) { // Standard initializations from generated code - // HAL_Init(); - // SystemClock_Config(); + HAL_Init(); + lf_SystemClock_Config(); // Configure TIM5 as our 32-bit clock timer __HAL_RCC_TIM5_CLK_ENABLE(); // initialize counter - TIM5->CR1 = TIM_CR1_CEN; // enable counter + TIM5->CR1 = TIM_CR1_CEN; // enable counter // set prescaler to (16 - 1) = 15 // CPU runs a 16MHz so timer ticks at 1MHz // Which means period of 1 microsecond - TIM5->PSC = 15; + TIM5->PSC = 15; // Setup ISR to increment upper bits TIM5->DIER |= TIM_DIER_CC1IE; NVIC_EnableIRQ(TIM5_IRQn); /* This is to make the Prescaler actually work - * For some reason, the Prescaler only kicks in once the timer has reset once. - * Thus, the current solution is to manually ret the value to a really large + * For some reason, the Prescaler only kicks in once the timer has reset once. + * Thus, the current solution is to manually ret the value to a really large * and force it to reset once. Its really jank but its the only way I could * find to make it work - */ + */ TIM5->CNT = 0xFFFFFFFE; } /** * ISR for handling timer overflow -> We increment the upper timer */ -void TIM5_IRQHandler(void){ +void TIM5_IRQHandler(void) { if (TIM5->SR & (1 << 1)) { TIM5->SR &= ~(1 << 1); _lf_time_us_high += 1; @@ -76,16 +76,17 @@ void TIM5_IRQHandler(void){ /** * Write the time since boot into time variable */ -int _lf_clock_now(instant_t *t){ +int _lf_clock_now(instant_t *t) +{ // Timer is cooked if (!t) { return -1; } // Get the current microseconds from TIM5 - uint32_t _lf_time_us_low = TIM5->CNT; + uint32_t _lf_time_us_low = TIM5->CNT; // Combine upper and lower timers (Yoinked from lf_nrf52 support) - uint64_t now_us = COMBINE_HI_LO((_lf_time_us_high-1), _lf_time_us_low); + uint64_t now_us = COMBINE_HI_LO((_lf_time_us_high - 1), _lf_time_us_low); *t = ((instant_t)now_us) * 1000; return 0; } @@ -94,7 +95,7 @@ int _lf_clock_now(instant_t *t){ * Make the STM32 go honk shoo mimimi for set nanoseconds * I essentially stole this from the lf_nrf52 support */ -int lf_sleep(interval_t sleep_duration){ +int lf_sleep(interval_t sleep_duration) { instant_t target_time; instant_t current_time; @@ -120,51 +121,45 @@ static void lf_busy_wait_until(instant_t wakeup_time) { // I am pretty sure this function doesnt work // Ill try to fix it once i know what the fuck its supposed to do, LOL -int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_time) { - // // Get the current time and sleep time - // instant_t now; - // _lf_clock_now(&now); - // interval_t duration = wakeup_time - now; - - // // Edge case handling for super small duration - // if (duration <= 0) { - // return 0; - // } else if (duration < 10) { - // lf_busy_wait_until(wakeup_time); - // return 0; - // } - - // // Enable interrupts and prepare to wait - // _lf_async_event = false; - // instant_t curr; - // lf_enable_interrupts_nested(); - - // do { - // _lf_clock_now(&curr); - - // // Exit wither when the timer is up or there is an exception - // } while (!_lf_async_event && (now < wakeup_time)); - - // // Disable interrupts again on exit - // lf_disable_interrupts_nested(); - - // if (!_lf_async_event) { - // return 0; - // } else { - // LF_PRINT_DEBUG(" *The STM32 rises from sleep* \n"); - // return -1; - // } +/* sleep until wakeup time + But, wake up if there is an async event +*/ +int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_time) { + // Get the current time and sleep time instant_t now; - do { _lf_clock_now(&now); - } while (now < wakeup_time); - return 0; -} + interval_t duration = wakeup_time - now; + + // Edge case handling for super small duration + if (duration <= 0) { + return 0; + } else if (duration < 10) { + lf_busy_wait_until(wakeup_time); + return 0; + } + // Enable interrupts and prepare to wait + _lf_async_event = false; + lf_enable_interrupts_nested(); + do { + _lf_clock_now(&now); + // Exit when the timer is up or there is an exception + } while (!_lf_async_event && (now < wakeup_time)); + // Disable interrupts again on exit + lf_disable_interrupts_nested(); + + if (!_lf_async_event) { + return 0; + } else { + LF_PRINT_DEBUG(" *The STM32 rises from sleep* \n"); + return -1; + } + +} // + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + // | Code for enabling and disabling Interrupts @@ -199,64 +194,58 @@ int lf_enable_interrupts_nested() { return 0; } -int _lf_unthreaded_notify_of_event() { +int _lf_single_threaded_notify_of_event() { _lf_async_event = true; return 0; } -int test_func(void){ - return 5; +int test_func(void) { + return 5; } // + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + // | Other functions I found -> taken from the generated main.c // + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + +void lf_SystemClock_Config(void) { + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; + + /** Configure the main internal regulator output voltage + */ + __HAL_RCC_PWR_CLK_ENABLE(); + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3); + + /** Initializes the RCC Oscillators according to the specified parameters + * in the RCC_OscInitTypeDef structure. + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { + Error_Handler(); + } + + /** Initializes the CPU, AHB and APB buses clocks + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; -// void SystemClock_Config(void) { -// RCC_OscInitTypeDef RCC_OscInitStruct = {0}; -// RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; - -// /** Configure the main internal regulator output voltage -// */ -// __HAL_RCC_PWR_CLK_ENABLE(); -// __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3); - -// /** Initializes the RCC Oscillators according to the specified parameters -// * in the RCC_OscInitTypeDef structure. -// */ -// RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; -// RCC_OscInitStruct.HSIState = RCC_HSI_ON; -// RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; -// RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; -// if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) -// { -// Error_Handler(); -// } - -// /** Initializes the CPU, AHB and APB buses clocks -// */ -// RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; -// RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; -// RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; -// RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; -// RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - -// if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) -// { -// Error_Handler(); -// } -// } - - -// void Error_Handler(void) -// { -// /* USER CODE BEGIN Error_Handler_Debug */ -// /* User can add his own implementation to report the HAL error return state */ -// __disable_irq(); -// while (1) -// { -// } -// /* USER CODE END Error_Handler_Debug */ -// } + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { + Error_Handler(); + } +} + +void Error_Handler(void) { + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state */ + __disable_irq(); + while (1) { + } + /* USER CODE END Error_Handler_Debug */ +} #endif \ No newline at end of file diff --git a/include/core/platform/lf_STM32f4_support.h b/include/core/platform/lf_STM32f4_support.h index 7a8414576..063421d6d 100644 --- a/include/core/platform/lf_STM32f4_support.h +++ b/include/core/platform/lf_STM32f4_support.h @@ -6,12 +6,8 @@ // I have no idea what the fuck TTY is so i guess we dont support it #define NO_TTY -// #include <../../../STM_Core/Inc/main.h> -// #include <../../../Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal.h> #include -// src-gen/Main/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal.h - // Defines for formatting time in printf for pico #define PRINTF_TAG "(" PRINTF_TIME ", " PRINTF_MICROSTEP ")" #define PRINTF_TIME "%lld"