1- #ifdef defined (PLATFORM_STM32F4 )
1+ #if defined(PLATFORM_STM32F4 )
22/*************
33 I hope this software works LOL
44 ***************/
@@ -28,8 +28,8 @@ static uint32_t _lf_time_us_high = 0;
2828#define COMBINE_HI_LO (hi , lo ) ((((uint64_t)hi) << 32) | ((uint64_t)lo))
2929
3030
31-
32-
31+ void lf_SystemClock_Config ();
32+ void Error_Handler ();
3333
3434// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
3535// | Code for timer functions
@@ -38,35 +38,35 @@ static uint32_t _lf_time_us_high = 0;
3838// We use timer 5 for our clock (probably better than fucking with sysTick)
3939void _lf_initialize_clock (void ) {
4040 // Standard initializations from generated code
41- // HAL_Init();
42- // SystemClock_Config ();
41+ HAL_Init ();
42+ lf_SystemClock_Config ();
4343
4444 // Configure TIM5 as our 32-bit clock timer
4545 __HAL_RCC_TIM5_CLK_ENABLE (); // initialize counter
46- TIM5 -> CR1 = TIM_CR1_CEN ; // enable counter
46+ TIM5 -> CR1 = TIM_CR1_CEN ; // enable counter
4747
4848 // set prescaler to (16 - 1) = 15
4949 // CPU runs a 16MHz so timer ticks at 1MHz
5050 // Which means period of 1 microsecond
51- TIM5 -> PSC = 15 ;
51+ TIM5 -> PSC = 15 ;
5252
5353 // Setup ISR to increment upper bits
5454 TIM5 -> DIER |= TIM_DIER_CC1IE ;
5555 NVIC_EnableIRQ (TIM5_IRQn );
5656
5757 /* This is to make the Prescaler actually work
58- * For some reason, the Prescaler only kicks in once the timer has reset once.
59- * Thus, the current solution is to manually ret the value to a really large
58+ * For some reason, the Prescaler only kicks in once the timer has reset once.
59+ * Thus, the current solution is to manually ret the value to a really large
6060 * and force it to reset once. Its really jank but its the only way I could
6161 * find to make it work
62- */
62+ */
6363 TIM5 -> CNT = 0xFFFFFFFE ;
6464}
6565
6666/**
6767 * ISR for handling timer overflow -> We increment the upper timer
6868 */
69- void TIM5_IRQHandler (void ){
69+ void TIM5_IRQHandler (void ) {
7070 if (TIM5 -> SR & (1 << 1 )) {
7171 TIM5 -> SR &= ~(1 << 1 );
7272 _lf_time_us_high += 1 ;
@@ -76,16 +76,17 @@ void TIM5_IRQHandler(void){
7676/**
7777 * Write the time since boot into time variable
7878 */
79- int _lf_clock_now (instant_t * t ){
79+ int _lf_clock_now (instant_t * t )
80+ {
8081 // Timer is cooked
8182 if (!t ) {
8283 return -1 ;
8384 }
8485 // Get the current microseconds from TIM5
85- uint32_t _lf_time_us_low = TIM5 -> CNT ;
86+ uint32_t _lf_time_us_low = TIM5 -> CNT ;
8687
8788 // Combine upper and lower timers (Yoinked from lf_nrf52 support)
88- uint64_t now_us = COMBINE_HI_LO ((_lf_time_us_high - 1 ), _lf_time_us_low );
89+ uint64_t now_us = COMBINE_HI_LO ((_lf_time_us_high - 1 ), _lf_time_us_low );
8990 * t = ((instant_t )now_us ) * 1000 ;
9091 return 0 ;
9192}
@@ -94,7 +95,7 @@ int _lf_clock_now(instant_t *t){
9495 * Make the STM32 go honk shoo mimimi for set nanoseconds
9596 * I essentially stole this from the lf_nrf52 support
9697 */
97- int lf_sleep (interval_t sleep_duration ){
98+ int lf_sleep (interval_t sleep_duration ) {
9899 instant_t target_time ;
99100 instant_t current_time ;
100101
@@ -120,51 +121,45 @@ static void lf_busy_wait_until(instant_t wakeup_time) {
120121
121122// I am pretty sure this function doesnt work
122123// Ill try to fix it once i know what the fuck its supposed to do, LOL
123- int _lf_interruptable_sleep_until_locked (environment_t * env , instant_t wakeup_time ) {
124- // // Get the current time and sleep time
125- // instant_t now;
126- // _lf_clock_now(&now);
127- // interval_t duration = wakeup_time - now;
128-
129- // // Edge case handling for super small duration
130- // if (duration <= 0) {
131- // return 0;
132- // } else if (duration < 10) {
133- // lf_busy_wait_until(wakeup_time);
134- // return 0;
135- // }
136-
137- // // Enable interrupts and prepare to wait
138- // _lf_async_event = false;
139- // instant_t curr;
140- // lf_enable_interrupts_nested();
141-
142- // do {
143- // _lf_clock_now(&curr);
144-
145- // // Exit wither when the timer is up or there is an exception
146- // } while (!_lf_async_event && (now < wakeup_time));
147-
148- // // Disable interrupts again on exit
149- // lf_disable_interrupts_nested();
150-
151- // if (!_lf_async_event) {
152- // return 0;
153- // } else {
154- // LF_PRINT_DEBUG(" *The STM32 rises from sleep* \n");
155- // return -1;
156- // }
124+ /* sleep until wakeup time
125+ But, wake up if there is an async event
157126
127+ */
128+ int _lf_interruptable_sleep_until_locked (environment_t * env , instant_t wakeup_time ) {
129+ // Get the current time and sleep time
158130 instant_t now ;
159- do {
160131 _lf_clock_now (& now );
161- } while (now < wakeup_time );
162- return 0 ;
163- }
132+ interval_t duration = wakeup_time - now ;
133+
134+ // Edge case handling for super small duration
135+ if (duration <= 0 ) {
136+ return 0 ;
137+ } else if (duration < 10 ) {
138+ lf_busy_wait_until (wakeup_time );
139+ return 0 ;
140+ }
164141
142+ // Enable interrupts and prepare to wait
143+ _lf_async_event = false;
144+ lf_enable_interrupts_nested ();
165145
146+ do {
147+ _lf_clock_now (& now );
166148
149+ // Exit when the timer is up or there is an exception
150+ } while (!_lf_async_event && (now < wakeup_time ));
167151
152+ // Disable interrupts again on exit
153+ lf_disable_interrupts_nested ();
154+
155+ if (!_lf_async_event ) {
156+ return 0 ;
157+ } else {
158+ LF_PRINT_DEBUG (" *The STM32 rises from sleep* \n" );
159+ return -1 ;
160+ }
161+
162+ }
168163
169164// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
170165// | Code for enabling and disabling Interrupts
@@ -199,64 +194,58 @@ int lf_enable_interrupts_nested() {
199194 return 0 ;
200195}
201196
202- int _lf_unthreaded_notify_of_event () {
197+ int _lf_single_threaded_notify_of_event () {
203198 _lf_async_event = true;
204199 return 0 ;
205200}
206201
207- int test_func (void ){
208- return 5 ;
202+ int test_func (void ) {
203+ return 5 ;
209204}
210205
211206// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
212207// | Other functions I found -> taken from the generated main.c
213208// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
209+ void lf_SystemClock_Config (void ) {
210+ RCC_OscInitTypeDef RCC_OscInitStruct = {0 };
211+ RCC_ClkInitTypeDef RCC_ClkInitStruct = {0 };
212+
213+ /** Configure the main internal regulator output voltage
214+ */
215+ __HAL_RCC_PWR_CLK_ENABLE ();
216+ __HAL_PWR_VOLTAGESCALING_CONFIG (PWR_REGULATOR_VOLTAGE_SCALE3 );
217+
218+ /** Initializes the RCC Oscillators according to the specified parameters
219+ * in the RCC_OscInitTypeDef structure.
220+ */
221+ RCC_OscInitStruct .OscillatorType = RCC_OSCILLATORTYPE_HSI ;
222+ RCC_OscInitStruct .HSIState = RCC_HSI_ON ;
223+ RCC_OscInitStruct .HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT ;
224+ RCC_OscInitStruct .PLL .PLLState = RCC_PLL_NONE ;
225+ if (HAL_RCC_OscConfig (& RCC_OscInitStruct ) != HAL_OK ) {
226+ Error_Handler ();
227+ }
228+
229+ /** Initializes the CPU, AHB and APB buses clocks
230+ */
231+ RCC_ClkInitStruct .ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 ;
232+ RCC_ClkInitStruct .SYSCLKSource = RCC_SYSCLKSOURCE_HSI ;
233+ RCC_ClkInitStruct .AHBCLKDivider = RCC_SYSCLK_DIV1 ;
234+ RCC_ClkInitStruct .APB1CLKDivider = RCC_HCLK_DIV1 ;
235+ RCC_ClkInitStruct .APB2CLKDivider = RCC_HCLK_DIV1 ;
214236
215- // void SystemClock_Config(void) {
216- // RCC_OscInitTypeDef RCC_OscInitStruct = {0};
217- // RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
218-
219- // /** Configure the main internal regulator output voltage
220- // */
221- // __HAL_RCC_PWR_CLK_ENABLE();
222- // __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
223-
224- // /** Initializes the RCC Oscillators according to the specified parameters
225- // * in the RCC_OscInitTypeDef structure.
226- // */
227- // RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
228- // RCC_OscInitStruct.HSIState = RCC_HSI_ON;
229- // RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
230- // RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
231- // if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
232- // {
233- // Error_Handler();
234- // }
235-
236- // /** Initializes the CPU, AHB and APB buses clocks
237- // */
238- // RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
239- // RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
240- // RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
241- // RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
242- // RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
243-
244- // if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
245- // {
246- // Error_Handler();
247- // }
248- // }
249-
250-
251- // void Error_Handler(void)
252- // {
253- // /* USER CODE BEGIN Error_Handler_Debug */
254- // /* User can add his own implementation to report the HAL error return state */
255- // __disable_irq();
256- // while (1)
257- // {
258- // }
259- // /* USER CODE END Error_Handler_Debug */
260- // }
237+ if (HAL_RCC_ClockConfig (& RCC_ClkInitStruct , FLASH_LATENCY_0 ) != HAL_OK ) {
238+ Error_Handler ();
239+ }
240+ }
241+
242+ void Error_Handler (void ) {
243+ /* USER CODE BEGIN Error_Handler_Debug */
244+ /* User can add his own implementation to report the HAL error return state */
245+ __disable_irq ();
246+ while (1 ) {
247+ }
248+ /* USER CODE END Error_Handler_Debug */
249+ }
261250
262251#endif
0 commit comments