[main] large code cleanup
This commit is contained in:
parent
7940c95b23
commit
e65c4d612c
178
Core/Src/main.c
178
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_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 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 */
|
/* USER CODE END PD */
|
||||||
|
|
||||||
/* Private macro -------------------------------------------------------------*/
|
/* Private macro -------------------------------------------------------------*/
|
||||||
|
@ -68,22 +79,19 @@ extern size_t display_current_frame;
|
||||||
|
|
||||||
/* USER CODE BEGIN PV */
|
/* USER CODE BEGIN PV */
|
||||||
|
|
||||||
int ((*executors[])(void)) = {
|
const int ((*executors[])(void)) = {
|
||||||
DNI_show_celsius,
|
DNI_show_celsius,
|
||||||
DNI_show_fahrenheit,
|
|
||||||
PCA9685_run_test,
|
PCA9685_run_test,
|
||||||
EEPROM_24AA02E48_run_test,
|
EEPROM_24AA02E48_run_test,
|
||||||
CS43L22_run_test,
|
CS43L22_run_test,
|
||||||
SST25VF016B_run_test,
|
SST25VF016B_run_test,
|
||||||
LIS302DL_run_test,
|
LIS302DL_run_test,
|
||||||
LIS302DL_run_test_dynamic,
|
|
||||||
MP45DT02_run_test,
|
MP45DT02_run_test,
|
||||||
LSM9DS1_test_accel,
|
LSM9DS1_test_accel,
|
||||||
LSM9DS1_test_magnet
|
LSM9DS1_test_magnet
|
||||||
};
|
};
|
||||||
|
|
||||||
void ((*cleanup_functions[])(void)) = {
|
const void ((*cleanup_functions[])(void)) = {
|
||||||
NULL,
|
|
||||||
NULL,
|
NULL,
|
||||||
PCA9685_cleanup,
|
PCA9685_cleanup,
|
||||||
NULL,
|
NULL,
|
||||||
|
@ -91,29 +99,12 @@ void ((*cleanup_functions[])(void)) = {
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
|
||||||
LSM9DS1_cleanup_accel,
|
LSM9DS1_cleanup_accel,
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
int delay_between_runs[] = {
|
|
||||||
250,
|
|
||||||
250,
|
|
||||||
-1,
|
|
||||||
-1,
|
|
||||||
-1,
|
|
||||||
-1,
|
|
||||||
-1,
|
|
||||||
200,
|
|
||||||
-1,
|
|
||||||
-1,
|
|
||||||
-1
|
|
||||||
};
|
|
||||||
|
|
||||||
size_t current_executor_id = 0;
|
size_t current_executor_id = 0;
|
||||||
|
|
||||||
size_t buttons_interrupt_enabled;
|
|
||||||
|
|
||||||
/* USER CODE END PV */
|
/* USER CODE END PV */
|
||||||
|
|
||||||
/* Private function prototypes -----------------------------------------------*/
|
/* Private function prototypes -----------------------------------------------*/
|
||||||
|
@ -131,66 +122,6 @@ static void MX_I2S2_Init(void);
|
||||||
/* Private user code ---------------------------------------------------------*/
|
/* Private user code ---------------------------------------------------------*/
|
||||||
/* USER CODE BEGIN 0 */
|
/* 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)
|
void button_init_and_test(void)
|
||||||
{
|
{
|
||||||
// letting the buttons be tested
|
// letting the buttons be tested
|
||||||
|
@ -201,11 +132,11 @@ void button_init_and_test(void)
|
||||||
size_t pressed_elements;
|
size_t pressed_elements;
|
||||||
|
|
||||||
uint32_t sw_button_locations[5][2] = {
|
uint32_t sw_button_locations[5][2] = {
|
||||||
{GPIOC, GPIO_PIN_11},
|
{(uint32_t) GPIOC, (uint32_t) GPIO_PIN_11},
|
||||||
{GPIOA, GPIO_PIN_15},
|
{(uint32_t) GPIOA, (uint32_t) GPIO_PIN_15},
|
||||||
{GPIOC, GPIO_PIN_9},
|
{(uint32_t) GPIOC, (uint32_t) GPIO_PIN_9},
|
||||||
{GPIOC, GPIO_PIN_6},
|
{(uint32_t) GPIOC, (uint32_t) GPIO_PIN_6},
|
||||||
{GPIOC, GPIO_PIN_8}
|
{(uint32_t) GPIOC, (uint32_t) GPIO_PIN_8}
|
||||||
};
|
};
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
@ -243,16 +174,16 @@ void button_init_and_test(void)
|
||||||
display_write_data_seq("Release buttons");
|
display_write_data_seq("Release buttons");
|
||||||
|
|
||||||
GPIOD->ODR = 0x1000;
|
GPIOD->ODR = 0x1000;
|
||||||
HAL_Delay(500);
|
HAL_Delay(300);
|
||||||
GPIOD->ODR = 0x2000;
|
GPIOD->ODR = 0x2000;
|
||||||
HAL_Delay(500);
|
HAL_Delay(300);
|
||||||
GPIOD->ODR = 0x4000;
|
GPIOD->ODR = 0x4000;
|
||||||
HAL_Delay(500);
|
HAL_Delay(300);
|
||||||
GPIOD->ODR = 0x8000;
|
GPIOD->ODR = 0x8000;
|
||||||
HAL_Delay(500);
|
HAL_Delay(300);
|
||||||
|
|
||||||
// waiting for control buttons to be released
|
// waiting for all buttons to be released
|
||||||
WAIT_UNTIL_CB_RELEASED;
|
WAIT_UNTIL_ALL_BUTTONS_RELEASED;
|
||||||
GPIOD->ODR = 0x0000;
|
GPIOD->ODR = 0x0000;
|
||||||
HAL_Delay(200);
|
HAL_Delay(200);
|
||||||
}
|
}
|
||||||
|
@ -294,7 +225,6 @@ int main(void)
|
||||||
MX_TIM2_Init();
|
MX_TIM2_Init();
|
||||||
MX_I2S2_Init();
|
MX_I2S2_Init();
|
||||||
/* USER CODE BEGIN 2 */
|
/* USER CODE BEGIN 2 */
|
||||||
buttons_interrupt_enabled = 0;
|
|
||||||
|
|
||||||
HAL_TIM_Base_Start(&htim2);
|
HAL_TIM_Base_Start(&htim2);
|
||||||
TIM2->CNT = 0;
|
TIM2->CNT = 0;
|
||||||
|
@ -310,9 +240,7 @@ int main(void)
|
||||||
size_t successful_tests = 0;
|
size_t successful_tests = 0;
|
||||||
size_t failed_tests = 0;
|
size_t failed_tests = 0;
|
||||||
|
|
||||||
//display_to_framebuffer();
|
for (display_current_frame = 1; display_current_frame < LEN(executors)+1; display_current_frame++)
|
||||||
|
|
||||||
for (display_current_frame = 1; display_current_frame < 12; display_current_frame++)
|
|
||||||
{
|
{
|
||||||
DISPLAY_CLEAR;
|
DISPLAY_CLEAR;
|
||||||
display_write_data_seq("Testing...");
|
display_write_data_seq("Testing...");
|
||||||
|
@ -361,20 +289,12 @@ int main(void)
|
||||||
|
|
||||||
display_to_direct();
|
display_to_direct();
|
||||||
|
|
||||||
|
/* USER CODE END 2 */
|
||||||
|
|
||||||
if (buttons_interrupt_enabled)
|
/* Infinite loop */
|
||||||
buttons_switch_to_input();
|
/* USER CODE BEGIN WHILE */
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// wait until all buttons are released
|
WAIT_UNTIL_ALL_BUTTONS_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"); }
|
|
||||||
|
|
||||||
display_load(display_current_frame);
|
display_load(display_current_frame);
|
||||||
|
|
||||||
|
@ -386,7 +306,7 @@ int main(void)
|
||||||
!(GPIOC->IDR & GPIO_PIN_8)
|
!(GPIOC->IDR & GPIO_PIN_8)
|
||||||
) {
|
) {
|
||||||
display_current_frame += 1;
|
display_current_frame += 1;
|
||||||
display_current_frame %= 12;
|
display_current_frame %= LEN(executors)+1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -398,7 +318,7 @@ int main(void)
|
||||||
display_current_frame -= 1;
|
display_current_frame -= 1;
|
||||||
|
|
||||||
if (display_current_frame == 0xFFFFFFFFU)
|
if (display_current_frame == 0xFFFFFFFFU)
|
||||||
display_current_frame = 11;
|
display_current_frame = LEN(executors);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -411,44 +331,10 @@ int main(void)
|
||||||
break;
|
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 END WHILE */
|
||||||
|
|
||||||
/* USER CODE BEGIN 3 */
|
/* USER CODE BEGIN 3 */
|
||||||
//}
|
}
|
||||||
/* USER CODE END 3 */
|
/* USER CODE END 3 */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -204,7 +204,7 @@ void EXTI0_IRQHandler(void)
|
||||||
{
|
{
|
||||||
GPIOD->ODR = 0x1000;
|
GPIOD->ODR = 0x1000;
|
||||||
|
|
||||||
switch_to_next_test();
|
//switch_to_next_test();
|
||||||
|
|
||||||
for (int i = 300000; i > 0; i--) asm("nop");
|
for (int i = 300000; i > 0; i--) asm("nop");
|
||||||
|
|
||||||
|
@ -217,7 +217,7 @@ void EXTI9_5_IRQHandler(void)
|
||||||
{
|
{
|
||||||
GPIOD->ODR = 0x1000;
|
GPIOD->ODR = 0x1000;
|
||||||
|
|
||||||
switch_to_next_test();
|
//switch_to_next_test();
|
||||||
|
|
||||||
for (int i = 300000; i > 0; i--) asm("nop");
|
for (int i = 300000; i > 0; i--) asm("nop");
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue