Skip to content

Commit 6bf57cd

Browse files
committed
we dont need STM_main anymore
1 parent 62e2c3e commit 6bf57cd

File tree

1 file changed

+59
-70
lines changed

1 file changed

+59
-70
lines changed

core/platform/lf_STM32f4_support.c

Lines changed: 59 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -29,44 +29,42 @@ static uint32_t _lf_time_us_high = 0;
2929

3030

3131

32-
33-
3432
// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
3533
// | Code for timer functions
3634
// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
3735

3836
// We use timer 5 for our clock (probably better than fucking with sysTick)
3937
void _lf_initialize_clock(void) {
4038
// Standard initializations from generated code
41-
// HAL_Init();
42-
// SystemClock_Config();
39+
HAL_Init();
40+
SystemClock_Config();
4341

4442
// Configure TIM5 as our 32-bit clock timer
4543
__HAL_RCC_TIM5_CLK_ENABLE(); // initialize counter
46-
TIM5->CR1 = TIM_CR1_CEN; // enable counter
44+
TIM5->CR1 = TIM_CR1_CEN; // enable counter
4745

4846
// set prescaler to (16 - 1) = 15
4947
// CPU runs a 16MHz so timer ticks at 1MHz
5048
// Which means period of 1 microsecond
51-
TIM5->PSC = 15;
49+
TIM5->PSC = 15;
5250

5351
// Setup ISR to increment upper bits
5452
TIM5->DIER |= TIM_DIER_CC1IE;
5553
NVIC_EnableIRQ(TIM5_IRQn);
5654

5755
/* 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
56+
* For some reason, the Prescaler only kicks in once the timer has reset once.
57+
* Thus, the current solution is to manually ret the value to a really large
6058
* and force it to reset once. Its really jank but its the only way I could
6159
* find to make it work
62-
*/
60+
*/
6361
TIM5->CNT = 0xFFFFFFFE;
6462
}
6563

6664
/**
6765
* ISR for handling timer overflow -> We increment the upper timer
6866
*/
69-
void TIM5_IRQHandler(void){
67+
void TIM5_IRQHandler(void) {
7068
if (TIM5->SR & (1 << 1)) {
7169
TIM5->SR &= ~(1 << 1);
7270
_lf_time_us_high += 1;
@@ -76,16 +74,17 @@ void TIM5_IRQHandler(void){
7674
/**
7775
* Write the time since boot into time variable
7876
*/
79-
int _lf_clock_now(instant_t *t){
77+
int _lf_clock_now(instant_t *t)
78+
{
8079
// Timer is cooked
8180
if (!t) {
8281
return -1;
8382
}
8483
// Get the current microseconds from TIM5
85-
uint32_t _lf_time_us_low = TIM5->CNT;
84+
uint32_t _lf_time_us_low = TIM5->CNT;
8685

8786
// 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);
87+
uint64_t now_us = COMBINE_HI_LO((_lf_time_us_high - 1), _lf_time_us_low);
8988
*t = ((instant_t)now_us) * 1000;
9089
return 0;
9190
}
@@ -94,7 +93,7 @@ int _lf_clock_now(instant_t *t){
9493
* Make the STM32 go honk shoo mimimi for set nanoseconds
9594
* I essentially stole this from the lf_nrf52 support
9695
*/
97-
int lf_sleep(interval_t sleep_duration){
96+
int lf_sleep(interval_t sleep_duration) {
9897
instant_t target_time;
9998
instant_t current_time;
10099

@@ -133,12 +132,12 @@ int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_ti
133132
// lf_busy_wait_until(wakeup_time);
134133
// return 0;
135134
// }
136-
135+
137136
// // Enable interrupts and prepare to wait
138137
// _lf_async_event = false;
139138
// instant_t curr;
140139
// lf_enable_interrupts_nested();
141-
140+
142141
// do {
143142
// _lf_clock_now(&curr);
144143

@@ -149,23 +148,19 @@ int _lf_interruptable_sleep_until_locked(environment_t *env, instant_t wakeup_ti
149148
// lf_disable_interrupts_nested();
150149

151150
// if (!_lf_async_event) {
152-
// return 0;
151+
// return 0;
153152
// } else {
154153
// LF_PRINT_DEBUG(" *The STM32 rises from sleep* \n");
155154
// return -1;
156155
// }
157156

158157
instant_t now;
159158
do {
160-
_lf_clock_now(&now);
159+
_lf_clock_now(&now);
161160
} while (now < wakeup_time);
162161
return 0;
163162
}
164163

165-
166-
167-
168-
169164
// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
170165
// | Code for enabling and disabling Interrupts
171166
// + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
@@ -204,59 +199,53 @@ int _lf_unthreaded_notify_of_event() {
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 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;
236+
237+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
238+
Error_Handler();
239+
}
240+
}
214241

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-
// }
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

Comments
 (0)