Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Torque Dial #12

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Core/Inc/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 // PA4 // (This will probably change)
#define BUTTON_5_PIN GPIO_PIN_0 // PB0
#define BUTTON_6_PIN GPIO_PIN_1 // PB1
10 changes: 7 additions & 3 deletions Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ------------------------------------------------------------*/
Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions Core/Inc/torque_dial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdint.h>
#include <stdbool.h>
#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);
90 changes: 73 additions & 17 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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 */

Expand All @@ -85,15 +85,19 @@ 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 button_flag = 0;
uint8_t dial_flag = 0;
dial_t dial;

init_buttons();
/* USER CODE END Init */

/* Configure the system clock */
Expand All @@ -108,73 +112,125 @@ 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 */
/* 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;
button_flag = 1;
button = buttons[BUTTON_LEFT];
break;
case BUTTON_2_PIN:
port = GPIOB;
button_flag = 1;
button = buttons[BUTTON_RIGHT];
break;
case BUTTON_3_PIN:
port = GPIOB;
button_flag = 1;
button = buttons[BUTTON_ESC];
break;
case BUTTON_4_PIN:
port = GPIOA;
button_flag = 1;
button = buttons[BUTTON_UP];
break;
case BUTTON_5_PIN:
port = GPIOB;
button_flag = 1;
button = buttons[BUTTON_DOWN];
break;
case BUTTON_6_PIN:
port = GPIOB;
button_flag = 1;
button = buttons[BUTTON_ENTER];
break;
case SWITCH_1_PIN:
port = GPIOB;
dial_flag = 1;
break;
case SWITCH_2_PIN:
port = GPIOB;
dial_flag = 1;
break;
case SWITCH_3_PIN:
port = GPIOB;
dial_flag = 1;
break;
case SWITCH_4_PIN:
port = GPIOB;
dial_flag = 1;
break;
case SWITCH_5_PIN:
port = GPIOB;
dial_flag = 1;
break;
default:
break;
}
interrupt_flag = 0;
}

// debounce logic
while(button_flag) {
// 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 {
printf("Failed to read the pin for button %d when doing debounce check.\n", button.button_id);
}

button.pressed = false;
flag = 0;
}
}
button_flag = 0;
}

while(dial_flag) {
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;
dial_flag = 0;
}
}
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

/* USER CODE END 3 */
}

Expand Down Expand Up @@ -299,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);
Expand All @@ -317,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);

Expand Down
47 changes: 47 additions & 0 deletions Core/Src/torque_dial.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "torque_dial.h"
#include <stdlib.h>

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);
}
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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]
##########################################################################################################################

# ------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down
26 changes: 18 additions & 8 deletions SteeringWheel24A.ioc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading