From e65c4d612c72f696cd108bc83f6cf170d74eda0a Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Sat, 19 Apr 2025 14:33:40 +0300 Subject: [PATCH] [main] large code cleanup --- Core/Src/main.c | 178 ++++++++-------------------------------- Core/Src/stm32f4xx_it.c | 4 +- 2 files changed, 34 insertions(+), 148 deletions(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index a23146b..73d6863 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -46,6 +46,17 @@ #define WAIT_UNTIL_CB_PRESSED while (!HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) && HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8)) #define WAIT_UNTIL_CB_RELEASED while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) || !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8)) +#define UB (GPIOA->IDR & GPIO_PIN_0) +#define SWT1 !(GPIOC->IDR & GPIO_PIN_11) +#define SWT2 !(GPIOA->IDR & GPIO_PIN_15) +#define SWT3 !(GPIOC->IDR & GPIO_PIN_9) +#define SWT4 !(GPIOC->IDR & GPIO_PIN_6) +#define SWT5 !(GPIOC->IDR & GPIO_PIN_8) + +#define WAIT_UNTIL_ALL_BUTTONS_RELEASED do {} while ( UB || SWT1 || SWT2 || SWT3 || SWT4 || SWT5 ) + +#define LEN(x) ( sizeof(x) / sizeof((x)[0]) ) + /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ @@ -68,22 +79,19 @@ extern size_t display_current_frame; /* USER CODE BEGIN PV */ -int ((*executors[])(void)) = { +const int ((*executors[])(void)) = { DNI_show_celsius, - DNI_show_fahrenheit, PCA9685_run_test, EEPROM_24AA02E48_run_test, CS43L22_run_test, SST25VF016B_run_test, LIS302DL_run_test, - LIS302DL_run_test_dynamic, MP45DT02_run_test, LSM9DS1_test_accel, LSM9DS1_test_magnet }; -void ((*cleanup_functions[])(void)) = { - NULL, +const void ((*cleanup_functions[])(void)) = { NULL, PCA9685_cleanup, NULL, @@ -91,29 +99,12 @@ void ((*cleanup_functions[])(void)) = { NULL, NULL, NULL, - NULL, LSM9DS1_cleanup_accel, NULL }; -int delay_between_runs[] = { - 250, - 250, - -1, - -1, - -1, - -1, - -1, - 200, - -1, - -1, - -1 - }; - size_t current_executor_id = 0; -size_t buttons_interrupt_enabled; - /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -131,66 +122,6 @@ static void MX_I2S2_Init(void); /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ -void switch_to_next_test(void) -{ - current_executor_id += 1; - current_executor_id %= 11; -} - -void buttons_switch_to_interrupt(void) -{ - // save current executor ID - size_t tmp = current_executor_id; - - // configure user button - GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = GPIO_PIN_0; - GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(EXTI0_IRQn); - - // configure SW5 (alternative advancing method) - GPIO_InitStruct.Pin = GPIO_PIN_8; - GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - - HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); - - // restore current executor ID - current_executor_id = tmp; - - // set interrupt mode flag - buttons_interrupt_enabled = 1; -} - -void buttons_switch_to_input(void) -{ - // configure user button - GPIO_InitTypeDef GPIO_InitStruct = {0}; - GPIO_InitStruct.Pin = GPIO_PIN_0; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - HAL_NVIC_DisableIRQ(EXTI0_IRQn); - - // configure SW5 (alternative input method) - GPIO_InitStruct.Pin = GPIO_PIN_8; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - - HAL_NVIC_DisableIRQ(EXTI9_5_IRQn); - - // clear interrupt mode flag - buttons_interrupt_enabled = 0; -} - void button_init_and_test(void) { // letting the buttons be tested @@ -201,11 +132,11 @@ void button_init_and_test(void) size_t pressed_elements; uint32_t sw_button_locations[5][2] = { - {GPIOC, GPIO_PIN_11}, - {GPIOA, GPIO_PIN_15}, - {GPIOC, GPIO_PIN_9}, - {GPIOC, GPIO_PIN_6}, - {GPIOC, GPIO_PIN_8} + {(uint32_t) GPIOC, (uint32_t) GPIO_PIN_11}, + {(uint32_t) GPIOA, (uint32_t) GPIO_PIN_15}, + {(uint32_t) GPIOC, (uint32_t) GPIO_PIN_9}, + {(uint32_t) GPIOC, (uint32_t) GPIO_PIN_6}, + {(uint32_t) GPIOC, (uint32_t) GPIO_PIN_8} }; do { @@ -243,16 +174,16 @@ void button_init_and_test(void) display_write_data_seq("Release buttons"); GPIOD->ODR = 0x1000; - HAL_Delay(500); + HAL_Delay(300); GPIOD->ODR = 0x2000; - HAL_Delay(500); + HAL_Delay(300); GPIOD->ODR = 0x4000; - HAL_Delay(500); + HAL_Delay(300); GPIOD->ODR = 0x8000; - HAL_Delay(500); + HAL_Delay(300); - // waiting for control buttons to be released - WAIT_UNTIL_CB_RELEASED; + // waiting for all buttons to be released + WAIT_UNTIL_ALL_BUTTONS_RELEASED; GPIOD->ODR = 0x0000; HAL_Delay(200); } @@ -294,7 +225,6 @@ int main(void) MX_TIM2_Init(); MX_I2S2_Init(); /* USER CODE BEGIN 2 */ - buttons_interrupt_enabled = 0; HAL_TIM_Base_Start(&htim2); TIM2->CNT = 0; @@ -310,9 +240,7 @@ int main(void) size_t successful_tests = 0; size_t failed_tests = 0; - //display_to_framebuffer(); - - for (display_current_frame = 1; display_current_frame < 12; display_current_frame++) + for (display_current_frame = 1; display_current_frame < LEN(executors)+1; display_current_frame++) { DISPLAY_CLEAR; display_write_data_seq("Testing..."); @@ -361,20 +289,12 @@ int main(void) display_to_direct(); + /* USER CODE END 2 */ - if (buttons_interrupt_enabled) - buttons_switch_to_input(); - + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ while (1) { - // wait until all buttons are released - while ( - (GPIOA->IDR & GPIO_PIN_0) || - !(GPIOC->IDR & GPIO_PIN_11) || - !(GPIOC->IDR & GPIO_PIN_8) || - !(GPIOC->IDR & GPIO_PIN_9) || - !(GPIOC->IDR & GPIO_PIN_6) || - !(GPIOA->IDR & GPIO_PIN_15) - ) { asm("nop"); } + WAIT_UNTIL_ALL_BUTTONS_RELEASED; display_load(display_current_frame); @@ -386,7 +306,7 @@ int main(void) !(GPIOC->IDR & GPIO_PIN_8) ) { display_current_frame += 1; - display_current_frame %= 12; + display_current_frame %= LEN(executors)+1; break; } @@ -398,7 +318,7 @@ int main(void) display_current_frame -= 1; if (display_current_frame == 0xFFFFFFFFU) - display_current_frame = 11; + display_current_frame = LEN(executors); break; } @@ -411,44 +331,10 @@ int main(void) break; } } - } - - /* USER CODE END 2 */ - - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - - /* - while (1) - { - if (delay_between_runs[current_executor_id] == -1 && buttons_interrupt_enabled) - buttons_switch_to_input(); - else if (delay_between_runs[current_executor_id] != -1 && !buttons_interrupt_enabled) - buttons_switch_to_interrupt(); - - executors[current_executor_id](); - - if (delay_between_runs[current_executor_id] == -1) { - WAIT_UNTIL_CB_PRESSED; - HAL_Delay(150); - GPIOD->BSRR = 0x1000; - - WAIT_UNTIL_CB_RELEASED; - HAL_Delay(150); - GPIOD->BSRR = 0x1000 << 16; - - if (cleanup_functions[current_executor_id]) - cleanup_functions[current_executor_id](); - - switch_to_next_test(); - } else { - HAL_Delay(delay_between_runs[current_executor_id]); - } - */ /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ - //} + } /* USER CODE END 3 */ } diff --git a/Core/Src/stm32f4xx_it.c b/Core/Src/stm32f4xx_it.c index 484f880..e1c3e86 100644 --- a/Core/Src/stm32f4xx_it.c +++ b/Core/Src/stm32f4xx_it.c @@ -204,7 +204,7 @@ void EXTI0_IRQHandler(void) { GPIOD->ODR = 0x1000; - switch_to_next_test(); + //switch_to_next_test(); for (int i = 300000; i > 0; i--) asm("nop"); @@ -217,7 +217,7 @@ void EXTI9_5_IRQHandler(void) { GPIOD->ODR = 0x1000; - switch_to_next_test(); + //switch_to_next_test(); for (int i = 300000; i > 0; i--) asm("nop");