11#if defined(PLATFORM_STM32F4 )
2- /*************
3- I hope this software works LOL
4- ***************/
2+
53
64#include "lf_STM32f4_support.h"
75#include "platform.h"
1513static volatile bool _lf_sleep_interrupted = false;
1614static volatile bool _lf_async_event = false;
1715
18- #define LF_MAX_SLEEP_NS USEC(UINT32_MAX)
19- #define LF_MIN_SLEEP_NS USEC(5)
20-
2116// nested critical section counter
2217static uint32_t _lf_num_nested_crit_sec = 0 ;
2318
2419// Timer upper half (for overflow)
2520static uint32_t _lf_time_us_high = 0 ;
2621
22+ #define LF_MIN_SLEEP_NS 10
23+
2724// Combine 2 32bit works to a 64 bit word (Takes from nrf52 support)
2825#define COMBINE_HI_LO (hi , lo ) ((((uint64_t)hi) << 32) | ((uint64_t)lo))
2926
@@ -34,8 +31,7 @@ void Error_Handler();
3431// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
3532// | Code for timer functions
3633// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
37-
38- // We use timer 5 for our clock (probably better than fucking with sysTick)
34+ // We use timer 5 for our clock (probably better than dealing with sysTick)
3935void _lf_initialize_clock (void ) {
4036 // Standard initializations from generated code
4137 HAL_Init ();
@@ -63,24 +59,24 @@ void _lf_initialize_clock(void) {
6359 TIM5 -> CNT = 0xFFFFFFFE ;
6460}
6561
66- /**
67- * ISR for handling timer overflow -> We increment the upper timer
68- */
62+ // ISR for handling timer overflow -> We increment the upper timer
6963void TIM5_IRQHandler (void ) {
7064 if (TIM5 -> SR & (1 << 1 )) {
7165 TIM5 -> SR &= ~(1 << 1 );
7266 _lf_time_us_high += 1 ;
7367 }
7468}
7569
70+
7671/**
7772 * Write the time since boot into time variable
7873 */
7974int _lf_clock_now (instant_t * t )
8075{
8176 // Timer is cooked
8277 if (!t ) {
83- return -1 ;
78+ return 1 ;
79+
8480 }
8581 // Get the current microseconds from TIM5
8682 uint32_t _lf_time_us_low = TIM5 -> CNT ;
@@ -101,7 +97,6 @@ int lf_sleep(interval_t sleep_duration) {
10197
10298 _lf_clock_now (& current_time );
10399 target_time = current_time + sleep_duration ;
104-
105100 // HAL_Delay only supports miliseconds. We try to use that for as long as possible
106101 // before switching to another meothd for finer tuned delay times
107102 long delaytime_ms = sleep_duration / 1000000 ;
@@ -158,8 +153,8 @@ int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_ti
158153 if (!_lf_async_event ) {
159154 return 0 ;
160155 } else {
161- LF_PRINT_DEBUG ( " *The STM32 rises from sleep* \n" ) ;
162- return -1 ;
156+ return 1 ;
157+
163158 }
164159
165160}
@@ -182,7 +177,8 @@ int lf_disable_interrupts_nested() {
182177
183178// enables the IRQ (checks if other programs still want it disabled first)
184179int lf_enable_interrupts_nested () {
185- // Somebody fucked up, LOL
180+ // Left the critical section more often than it was entered.
181+
186182 if (_lf_num_nested_crit_sec <= 0 ) {
187183 return 1 ;
188184 }
@@ -202,17 +198,14 @@ int _lf_single_threaded_notify_of_event() {
202198 return 0 ;
203199}
204200
205- int test_func (void ) {
206- return 5 ;
207- }
208-
209201// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
210- // | Other functions I found -> taken from the generated main.c
202+ // | Other functions -> taken from the generated main.c
211203// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
212204void lf_SystemClock_Config (void ) {
213205 RCC_OscInitTypeDef RCC_OscInitStruct = {0 };
214206 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0 };
215207
208+
216209 /** Configure the main internal regulator output voltage
217210 */
218211 __HAL_RCC_PWR_CLK_ENABLE ();
@@ -249,6 +242,7 @@ void Error_Handler(void) {
249242 while (1 ) {
250243 }
251244 /* USER CODE END Error_Handler_Debug */
245+
252246}
253247
254248#endif
0 commit comments