From 65c517a06d9f1ebdf9662622d04d5bf2b1f10ee4 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Sun, 26 Jan 2025 17:56:58 -0500 Subject: [PATCH 1/4] Added torque dial stuff. Pinout is not finalized --- Core/Inc/button.h | 2 +- Core/Inc/main.h | 10 ++++-- Core/Inc/torque_dial.h | 35 +++++++++++++++++++ Core/Src/main.c | 78 ++++++++++++++++++++++++++++++++++++------ Core/Src/torque_dial.c | 46 +++++++++++++++++++++++++ 5 files changed, 156 insertions(+), 15 deletions(-) create mode 100644 Core/Inc/torque_dial.h create mode 100644 Core/Src/torque_dial.c diff --git a/Core/Inc/button.h b/Core/Inc/button.h index 142a3cd..8e3858e 100644 --- a/Core/Inc/button.h +++ b/Core/Inc/button.h @@ -27,6 +27,6 @@ void init_buttons(); #define BUTTON_1_PIN GPIO_PIN_2 // PB2 #define BUTTON_2_PIN GPIO_PIN_10 // PB10 #define BUTTON_3_PIN GPIO_PIN_11 // PB11 -#define BUTTON_4_PIN GPIO_PIN_7 // PA7 +#define BUTTON_4_PIN GPIO_PIN_4 // PA7 // (This will probably change) #define BUTTON_5_PIN GPIO_PIN_0 // PB0 #define BUTTON_6_PIN GPIO_PIN_1 // PB1 \ No newline at end of file diff --git a/Core/Inc/main.h b/Core/Inc/main.h index bd871b1..e35a8e1 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -32,6 +32,7 @@ extern "C" { /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "button.h" +#include "torque_dial.h" /* USER CODE END Includes */ /* Exported types ------------------------------------------------------------*/ @@ -60,9 +61,12 @@ void Error_Handler(void); /* USER CODE BEGIN Private defines */ extern button_t buttons[MAX_BUTTON_SIZE]; /* defined in buttons.c */ -#define STEERING_CANID_IO 0x680 -#define DEBOUNCE_TIME 8 // unit is ms -#define DEBOUNCE_ON 1 // 0 for off, 1 for on +#define BUTTON_CANID_IO 0x680 +#define DIAL_CANID_IO 0x681 +#define BUTTON_DEBOUNCE_TIME 8 // unit is ms +#define BUTTON_DEBOUNCE_ON 1 // 0 for off, 1 for on +#define DIAL_DEBOUNCE_TIME 10 // unit is ms +#define DIAL_DEBOUNCE_ON 1 // 0 for off, 1 for on /* USER CODE END Private defines */ #ifdef __cplusplus diff --git a/Core/Inc/torque_dial.h b/Core/Inc/torque_dial.h new file mode 100644 index 0000000..52360a0 --- /dev/null +++ b/Core/Inc/torque_dial.h @@ -0,0 +1,35 @@ +#include +#include +#include "main.h" +#include "can.h" + +#define SWITCH_1_PIN GPIO_PIN_5 // PB5 +#define SWITCH_2_PIN GPIO_PIN_6 // PB6 +#define SWITCH_3_PIN GPIO_PIN_7 // PB7 +#define SWITCH_4_PIN GPIO_PIN_8 // PB8 +#define SWITCH_5_PIN GPIO_PIN_9 // PB9 + +typedef enum { + SWITCH_1, + SWITCH_2, + SWITCH_3, + SWITCH_4, + SWITCH_5, + MAX_DIAL_SIZE, +} dial_switches_t; + +typedef struct dial { + uint8_t dial_current_setting; + uint32_t prev_tick; + bool actively_debouncing; +} dial_t; + +/** + * @brief Initializes torque dial + */ +void init_dial(dial_t *dial, can_t *can, can_msg_t can_msg_dial); + +/** + * @brief Switches dial setting + */ +uint8_t switch_dial_setting(uint16_t pin); \ No newline at end of file diff --git a/Core/Src/main.c b/Core/Src/main.c index 5639883..34afd64 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -85,15 +85,18 @@ int main(void) HAL_Init(); /* USER CODE BEGIN Init */ - init_buttons(); can_t *can = malloc(sizeof(can_t)); can->hcan = &hcan; + can_msg_t can_msg_button = { .len = sizeof(uint8_t), .id = BUTTON_CANID_IO }; // change this later + can_msg_t can_msg_dial = { .len = sizeof(uint8_t), .id = DIAL_CANID_IO }; // change this later - can_msg_t can_msg = { .len = sizeof(uint8_t), .id = STEERING_CANID_IO }; button_t button; GPIO_TypeDef* port = NULL; + uint8_t input_type = 0; // 0 is none, 1 is button, 2 is switch (for torque dial) + dial_t dial; + init_buttons(); /* USER CODE END Init */ /* Configure the system clock */ @@ -108,7 +111,7 @@ int main(void) MX_CAN_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ - + init_dial(&dial, can, can_msg_dial); /* USER CODE END 2 */ /* Infinite loop */ @@ -120,47 +123,77 @@ int main(void) switch (gpio_pin) { case BUTTON_1_PIN: port = GPIOB; + input_type = 1; button = buttons[BUTTON_LEFT]; break; case BUTTON_2_PIN: port = GPIOB; + input_type = 1; button = buttons[BUTTON_RIGHT]; break; case BUTTON_3_PIN: port = GPIOB; + input_type = 1; button = buttons[BUTTON_ESC]; break; case BUTTON_4_PIN: port = GPIOA; + input_type = 1; button = buttons[BUTTON_UP]; break; case BUTTON_5_PIN: port = GPIOB; + input_type = 1; button = buttons[BUTTON_DOWN]; break; case BUTTON_6_PIN: port = GPIOB; + input_type = 1; button = buttons[BUTTON_ENTER]; break; + case SWITCH_1_PIN: + port = GPIOB; + input_type = 2; + break; + case SWITCH_2_PIN: + port = GPIOB; + input_type = 2; + break; + case SWITCH_3_PIN: + port = GPIOB; + input_type = 2; + break; + case SWITCH_4_PIN: + port = GPIOB; + input_type = 2; + break; + case SWITCH_5_PIN: + port = GPIOB; + input_type = 2; + break; default: + input_type = 0; break; } + flag = 0; + } - // debounce logic + while(input_type == 1) { + // debounce logic if (!button.pressed) { button.pressed = true; button.prev_tick = HAL_GetTick(); } // wait 8ms before continuing - if ((HAL_GetTick() <= button.prev_tick + DEBOUNCE_TIME) && DEBOUNCE_ON) { + if ((HAL_GetTick() <= button.prev_tick + BUTTON_DEBOUNCE_TIME) && BUTTON_DEBOUNCE_ON) { continue; } // if the pin is still high, send CAN message if(HAL_GPIO_ReadPin(port, gpio_pin) == GPIO_PIN_SET) { - memcpy(&can_msg.data, &button.button_id, 1); - can_send_msg(can, &can_msg); + memcpy(&can_msg_button.data, &button.button_id, 1); + can_send_msg(can, &can_msg_button); printf("Button %d pressed\n", button.button_id); } else { @@ -168,13 +201,36 @@ int main(void) } button.pressed = false; - flag = 0; - } - } + input_type = 0; + } + + while(input_type == 2) { + if(!dial.actively_debouncing) { + dial.actively_debouncing = true; + dial.prev_tick = HAL_GetTick(); + } + + // wait 10ms before continuing + if ((HAL_GetTick() <= dial.prev_tick + DIAL_DEBOUNCE_TIME) && DIAL_DEBOUNCE_ON) { + continue; + } + + // if the pin is still high, send CAN message + if(HAL_GPIO_ReadPin(port, gpio_pin) == GPIO_PIN_SET) { + dial.dial_current_setting = switch_dial_setting(gpio_pin); + memcpy(&can_msg_dial.data, &dial.dial_current_setting, 1); + can_send_msg(can, &can_msg_dial); + printf("Button %d pressed\n", dial.dial_current_setting); + } + + dial.actively_debouncing = false; + input_type = 0; + } + } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ - + /* USER CODE END 3 */ } diff --git a/Core/Src/torque_dial.c b/Core/Src/torque_dial.c new file mode 100644 index 0000000..a2a52f6 --- /dev/null +++ b/Core/Src/torque_dial.c @@ -0,0 +1,46 @@ +#include "torque_dial.h" +#include + +uint16_t dial_pins[5] = {SWITCH_1_PIN, SWITCH_2_PIN, SWITCH_3_PIN, SWITCH_4_PIN, SWITCH_5_PIN}; + +uint8_t switch_dial_setting(uint16_t pin) { + uint8_t setting = -1; + switch (pin) + { + case SWITCH_1_PIN: + setting = SWITCH_1; + break; + case SWITCH_2_PIN: + setting = SWITCH_2; + break; + case SWITCH_3_PIN: + setting = SWITCH_3; + break; + case SWITCH_4_PIN: + setting = SWITCH_4; + break; + case SWITCH_5_PIN: + setting = SWITCH_5; + break; + default: + break; + } + return setting; +} + +// Init the dial's setting on startup +void init_dial(dial_t *dial, can_t *can, can_msg_t can_msg_dial) +{ + uint16_t pin = NULL; + for(int i = 0; i < 5; i++) { + if(HAL_GPIO_ReadPin(GPIOB, dial_pins[i]) == GPIO_PIN_SET) { + pin = dial_pins[i]; + break; + } + } + dial->dial_current_pin = pin; + dial->dial_current_setting = switch_dial_setting(pin); + memcpy(&can_msg_dial.data, &dial->dial_current_setting, 1); + can_send_msg(can, &can_msg_dial); + printf("Initial dial setting is %d\n", dial->dial_current_setting); +} \ No newline at end of file From 6bd89c3e96338508bc2a78fcb9c36377f7da11f2 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Sun, 26 Jan 2025 18:20:53 -0500 Subject: [PATCH 2/4] Changed pinout in STM32CubeMX for torque dial interrupts. May need to change pinout again later --- Core/Inc/button.h | 2 +- Core/Src/main.c | 6 +++--- Makefile | 4 ++-- SteeringWheel24A.ioc | 26 ++++++++++++++++++-------- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Core/Inc/button.h b/Core/Inc/button.h index 8e3858e..ea71b88 100644 --- a/Core/Inc/button.h +++ b/Core/Inc/button.h @@ -27,6 +27,6 @@ void init_buttons(); #define BUTTON_1_PIN GPIO_PIN_2 // PB2 #define BUTTON_2_PIN GPIO_PIN_10 // PB10 #define BUTTON_3_PIN GPIO_PIN_11 // PB11 -#define BUTTON_4_PIN GPIO_PIN_4 // PA7 // (This will probably change) +#define BUTTON_4_PIN GPIO_PIN_4 // PA4 // (This will probably change) #define BUTTON_5_PIN GPIO_PIN_0 // PB0 #define BUTTON_6_PIN GPIO_PIN_1 // PB1 \ No newline at end of file diff --git a/Core/Src/main.c b/Core/Src/main.c index 34afd64..0a1015b 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -355,8 +355,8 @@ static void MX_GPIO_Init(void) __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); - /*Configure GPIO pin : PA7 */ - GPIO_InitStruct.Pin = GPIO_PIN_7; + /*Configure GPIO pin : PA4 */ + GPIO_InitStruct.Pin = GPIO_PIN_4; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); @@ -373,7 +373,7 @@ static void MX_GPIO_Init(void) PB9 */ GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8 |GPIO_PIN_9; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_PULLDOWN; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); diff --git a/Makefile b/Makefile index 34dcb8d..41c8f37 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.5.0-RC5] date: [Sun Jan 26 13:13:00 EST 2025] +# File automatically-generated by tool: [projectgenerator] version: [4.5.0-RC5] date: [Sun Jan 26 18:17:55 EST 2025] ########################################################################################################################## # ------------------------------------------------ @@ -61,7 +61,7 @@ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \ Core/Src/system_stm32f1xx.c # ASM sources -# ASM sources# ASM sources# ASM sources# ASM sourcesASM_SOURCES = \ +# ASM sources# ASM sources# ASM sources# ASM sources# ASM sourcesASM_SOURCES = \ startup_stm32f103xb.s # ASM sources diff --git a/SteeringWheel24A.ioc b/SteeringWheel24A.ioc index e6b044f..376aad1 100644 --- a/SteeringWheel24A.ioc +++ b/SteeringWheel24A.ioc @@ -19,7 +19,7 @@ Mcu.IP4=USART1 Mcu.IPNb=5 Mcu.Name=STM32F103C(8-B)Tx Mcu.Package=LQFP48 -Mcu.Pin0=PA7 +Mcu.Pin0=PA4 Mcu.Pin1=PB0 Mcu.Pin10=PB5 Mcu.Pin11=PB6 @@ -59,8 +59,8 @@ PA11.Mode=CAN_Activate PA11.Signal=CAN_RX PA12.Mode=CAN_Activate PA12.Signal=CAN_TX -PA7.Locked=true -PA7.Signal=GPXTI7 +PA4.Locked=true +PA4.Signal=GPXTI4 PA9.Mode=Asynchronous PA9.Signal=USART1_TX PB0.Locked=true @@ -76,23 +76,23 @@ PB2.Signal=GPXTI2 PB5.GPIOParameters=GPIO_PuPd PB5.GPIO_PuPd=GPIO_PULLDOWN PB5.Locked=true -PB5.Signal=GPIO_Input +PB5.Signal=GPXTI5 PB6.GPIOParameters=GPIO_PuPd PB6.GPIO_PuPd=GPIO_PULLDOWN PB6.Locked=true -PB6.Signal=GPIO_Input +PB6.Signal=GPXTI6 PB7.GPIOParameters=GPIO_PuPd PB7.GPIO_PuPd=GPIO_PULLDOWN PB7.Locked=true -PB7.Signal=GPIO_Input +PB7.Signal=GPXTI7 PB8.GPIOParameters=GPIO_PuPd PB8.GPIO_PuPd=GPIO_PULLDOWN PB8.Locked=true -PB8.Signal=GPIO_Input +PB8.Signal=GPXTI8 PB9.GPIOParameters=GPIO_PuPd PB9.GPIO_PuPd=GPIO_PULLDOWN PB9.Locked=true -PB9.Signal=GPIO_Input +PB9.Signal=GPXTI9 PinOutPanel.RotationAngle=0 ProjectManager.AskForMigrate=true ProjectManager.BackupPrevious=false @@ -142,8 +142,18 @@ SH.GPXTI11.0=GPIO_EXTI11 SH.GPXTI11.ConfNb=1 SH.GPXTI2.0=GPIO_EXTI2 SH.GPXTI2.ConfNb=1 +SH.GPXTI4.0=GPIO_EXTI4 +SH.GPXTI4.ConfNb=1 +SH.GPXTI5.0=GPIO_EXTI5 +SH.GPXTI5.ConfNb=1 +SH.GPXTI6.0=GPIO_EXTI6 +SH.GPXTI6.ConfNb=1 SH.GPXTI7.0=GPIO_EXTI7 SH.GPXTI7.ConfNb=1 +SH.GPXTI8.0=GPIO_EXTI8 +SH.GPXTI8.ConfNb=1 +SH.GPXTI9.0=GPIO_EXTI9 +SH.GPXTI9.ConfNb=1 USART1.IPParameters=VirtualMode USART1.VirtualMode=VM_ASYNC VP_SYS_VS_ND.Mode=No_Debug From f2cf67d8ad2b13598e527c90aeb0d26cec1254d4 Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Sun, 26 Jan 2025 18:28:25 -0500 Subject: [PATCH 3/4] Made separate flags for button and dial --- Core/Src/main.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Core/Src/main.c b/Core/Src/main.c index 0a1015b..ed56dc1 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -33,7 +33,7 @@ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ -volatile uint8_t flag; +volatile uint8_t interrupt_flag; volatile uint16_t gpio_pin; /* USER CODE END PD */ @@ -65,7 +65,7 @@ static void MX_USART1_UART_Init(void); void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { gpio_pin = GPIO_Pin; - flag = 1; + interrupt_flag = 1; } /* USER CODE END 0 */ @@ -93,7 +93,8 @@ int main(void) button_t button; GPIO_TypeDef* port = NULL; - uint8_t input_type = 0; // 0 is none, 1 is button, 2 is switch (for torque dial) + uint8_t button_flag = 0; + uint8_t dial_flag = 0; dial_t dial; init_buttons(); @@ -117,68 +118,67 @@ int main(void) /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { - while (flag) { + while (interrupt_flag) { /* To change button mapping, change the index button is retrieving from the list */ switch (gpio_pin) { case BUTTON_1_PIN: port = GPIOB; - input_type = 1; + button_flag = 1; button = buttons[BUTTON_LEFT]; break; case BUTTON_2_PIN: port = GPIOB; - input_type = 1; + button_flag = 1; button = buttons[BUTTON_RIGHT]; break; case BUTTON_3_PIN: port = GPIOB; - input_type = 1; + button_flag = 1; button = buttons[BUTTON_ESC]; break; case BUTTON_4_PIN: port = GPIOA; - input_type = 1; + button_flag = 1; button = buttons[BUTTON_UP]; break; case BUTTON_5_PIN: port = GPIOB; - input_type = 1; + button_flag = 1; button = buttons[BUTTON_DOWN]; break; case BUTTON_6_PIN: port = GPIOB; - input_type = 1; + button_flag = 1; button = buttons[BUTTON_ENTER]; break; case SWITCH_1_PIN: port = GPIOB; - input_type = 2; + dial_flag = 1; break; case SWITCH_2_PIN: port = GPIOB; - input_type = 2; + dial_flag = 1; break; case SWITCH_3_PIN: port = GPIOB; - input_type = 2; + dial_flag = 1; break; case SWITCH_4_PIN: port = GPIOB; - input_type = 2; + dial_flag = 1; break; case SWITCH_5_PIN: port = GPIOB; - input_type = 2; + dial_flag = 1; break; default: - input_type = 0; break; } - flag = 0; + interrupt_flag = 0; } - while(input_type == 1) { + while(button_flag) { // debounce logic if (!button.pressed) { button.pressed = true; @@ -201,10 +201,10 @@ int main(void) } button.pressed = false; - input_type = 0; + button_flag = 0; } - while(input_type == 2) { + while(dial_flag) { if(!dial.actively_debouncing) { dial.actively_debouncing = true; dial.prev_tick = HAL_GetTick(); @@ -224,7 +224,7 @@ int main(void) } dial.actively_debouncing = false; - input_type = 0; + dial_flag = 0; } } /* USER CODE END WHILE */ From b7f9a14f6534e451ae7fb188ec4525d819088b5f Mon Sep 17 00:00:00 2001 From: bjackson312006 Date: Sun, 26 Jan 2025 18:32:48 -0500 Subject: [PATCH 4/4] Formatting :( --- Core/Inc/torque_dial.h | 2 +- Core/Src/torque_dial.c | 69 +++++++++++++++++++++--------------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/Core/Inc/torque_dial.h b/Core/Inc/torque_dial.h index 52360a0..24f92ad 100644 --- a/Core/Inc/torque_dial.h +++ b/Core/Inc/torque_dial.h @@ -19,7 +19,7 @@ typedef enum { } dial_switches_t; typedef struct dial { - uint8_t dial_current_setting; + uint8_t dial_current_setting; uint32_t prev_tick; bool actively_debouncing; } dial_t; diff --git a/Core/Src/torque_dial.c b/Core/Src/torque_dial.c index a2a52f6..08fbc96 100644 --- a/Core/Src/torque_dial.c +++ b/Core/Src/torque_dial.c @@ -1,46 +1,47 @@ #include "torque_dial.h" #include -uint16_t dial_pins[5] = {SWITCH_1_PIN, SWITCH_2_PIN, SWITCH_3_PIN, SWITCH_4_PIN, SWITCH_5_PIN}; +uint16_t dial_pins[5] = { SWITCH_1_PIN, SWITCH_2_PIN, SWITCH_3_PIN, + SWITCH_4_PIN, SWITCH_5_PIN }; -uint8_t switch_dial_setting(uint16_t pin) { - uint8_t setting = -1; - switch (pin) - { - case SWITCH_1_PIN: - setting = SWITCH_1; - break; - case SWITCH_2_PIN: - setting = SWITCH_2; - break; - case SWITCH_3_PIN: - setting = SWITCH_3; - break; - case SWITCH_4_PIN: - setting = SWITCH_4; - break; - case SWITCH_5_PIN: - setting = SWITCH_5; - break; - default: - break; - } - return setting; +uint8_t switch_dial_setting(uint16_t pin) +{ + uint8_t setting = -1; + switch (pin) { + case SWITCH_1_PIN: + setting = SWITCH_1; + break; + case SWITCH_2_PIN: + setting = SWITCH_2; + break; + case SWITCH_3_PIN: + setting = SWITCH_3; + break; + case SWITCH_4_PIN: + setting = SWITCH_4; + break; + case SWITCH_5_PIN: + setting = SWITCH_5; + break; + default: + break; + } + return setting; } // Init the dial's setting on startup void init_dial(dial_t *dial, can_t *can, can_msg_t can_msg_dial) { - uint16_t pin = NULL; - for(int i = 0; i < 5; i++) { - if(HAL_GPIO_ReadPin(GPIOB, dial_pins[i]) == GPIO_PIN_SET) { - pin = dial_pins[i]; - break; - } - } - dial->dial_current_pin = pin; - dial->dial_current_setting = switch_dial_setting(pin); - memcpy(&can_msg_dial.data, &dial->dial_current_setting, 1); + uint16_t pin = NULL; + for (int i = 0; i < 5; i++) { + if (HAL_GPIO_ReadPin(GPIOB, dial_pins[i]) == GPIO_PIN_SET) { + pin = dial_pins[i]; + break; + } + } + dial->dial_current_pin = pin; + dial->dial_current_setting = switch_dial_setting(pin); + memcpy(&can_msg_dial.data, &dial->dial_current_setting, 1); can_send_msg(can, &can_msg_dial); printf("Initial dial setting is %d\n", dial->dial_current_setting); } \ No newline at end of file