add button test at the board startup, part 1

This commit is contained in:
2025-03-08 14:23:18 +02:00
parent a2c5b2fe87
commit 82a3907d8a
6 changed files with 116 additions and 36 deletions

View File

@@ -55,9 +55,10 @@ void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void EXTI0_IRQHandler(void);
/* USER CODE BEGIN EFP */
void EXTI0_IRQHandler(void);
/* USER CODE END EFP */
#ifdef __cplusplus

View File

@@ -102,9 +102,88 @@ int main(void)
MX_ADC1_Init();
/* USER CODE BEGIN 2 */
GPIOD->ODR = 0xF000;
GPIOD->ODR = 0x1000;
display_init();
GPIOD->ODR = 0xF000;
// init showcase
display_write_data_seq("Fill any bar:");
DISPLAY_SET_CURSOR(1, 5);
display_write_data_seq("[-] [-----]");
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}
};
do {
HAL_Delay(100);
pressed_elements = 0;
DISPLAY_SET_CURSOR(1, 6);
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)) {
pressed_elements |= 0x20;
display_write_data_byte('*');
} else {
display_write_data_byte('-');
}
DISPLAY_SET_CURSOR(1, 10);
for (size_t i = 0; i < 5; i++) {
pressed_elements >>= 1;
size_t input = !HAL_GPIO_ReadPin(sw_button_locations[i][0], sw_button_locations[i][1]);
if (input) {
pressed_elements |= 0x20;
display_write_data_byte('*');
} else {
display_write_data_byte('-');
}
}
} while (pressed_elements != 0x1 && pressed_elements != 0x3E);
GPIOD->ODR = 0x1000;
HAL_Delay(500);
GPIOD->ODR = 0x2000;
HAL_Delay(500);
GPIOD->ODR = 0x4000;
HAL_Delay(500);
GPIOD->ODR = 0x8000;
HAL_Delay(500);
GPIOD->ODR = 0x0000;
while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0));
HAL_Delay(500);
// switch mode for 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);
// switch mode for SW5 (alternative advancing method)
GPIO_InitTypeDef GPIO_InitStruct = {0};
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(EXTI8_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI8_IRQn);
/* USER CODE END 2 */
@@ -230,6 +309,7 @@ static void MX_GPIO_Init(void)
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12
@@ -238,9 +318,9 @@ static void MX_GPIO_Init(void)
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
/*Configure GPIO pins : PA0 PA15 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
@@ -260,9 +340,11 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
/*Configure GPIO pins : PC6 PC8 PC9 PC11 */
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */

View File

@@ -41,8 +41,6 @@
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
extern void ((*executors[])(void));
extern void *current_executor(void);
extern int current_executor_id;
/* USER CODE END PV */
@@ -200,29 +198,20 @@ void SysTick_Handler(void)
/* please refer to the startup file (startup_stm32f4xx.s). */
/******************************************************************************/
/**
* @brief This function handles EXTI line0 interrupt.
*/
/* USER CODE BEGIN 1 */
void EXTI0_IRQHandler(void)
{
/* USER CODE BEGIN EXTI0_IRQn 0 */
GPIOD->ODR = 0x1000;
current_executor_id += 1;
current_executor_id &= 1;
current_executor_id %= 2;
for (int i = 900000; i > 0; i--) asm("nop");
GPIOD->ODR = 0x0000;
/* USER CODE END EXTI0_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
/* USER CODE BEGIN EXTI0_IRQn 1 */
/* USER CODE END EXTI0_IRQn 1 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */