From ae971f4bba9abe7c570fa0e70927c57fec8e6eb6 Mon Sep 17 00:00:00 2001 From: Ilgaz Er Date: Mon, 16 Jan 2023 18:24:04 +0300 Subject: [PATCH] Migrated button press interrupts to buttons driver Migrated interrupts from main to drivers --- .gitignore | 3 ++- Src/analog_sensors.c | 9 +++++++- Src/analog_sensors.h | 3 +-- Src/buttons.c | 42 +++++++++++++++++++++++++++++++++ Src/buttons.h | 1 + Src/drive.c | 14 ++++++++++- Src/drive.h | 1 + Src/main.c | 55 +++++--------------------------------------- 8 files changed, 74 insertions(+), 54 deletions(-) create mode 100644 Src/buttons.c create mode 100644 Src/buttons.h diff --git a/.gitignore b/.gitignore index 09f7c5e..25ba059 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ Debug Startup -*.launch \ No newline at end of file +*.launch +.vscode/settings.json diff --git a/Src/analog_sensors.c b/Src/analog_sensors.c index 0bc8125..50de2cd 100644 --- a/Src/analog_sensors.c +++ b/Src/analog_sensors.c @@ -3,9 +3,10 @@ #include "board/gpio.h" #include "board/iser.h" #include "board/exti.h" +#include "drive.h" #include "pins.h" -void initialize_sensors() +void init_sensors() { ADC_shared->CCR |= (0b1011 << 18); // Enable Clock for GPIO @@ -95,6 +96,12 @@ void refresh_sensors() ldr_direction = ((LDR_left - LDR_left_calib) - (LDR_right - LDR_right_calib)); } +void ADC1_2_IRQHandler() { + refresh_sensors(); + drive(); + SET(ADC1->ISR, ADC_JEOS); +} + void refresh_ldr_calib() { LDR_right_calib = ADC1->JDR3; diff --git a/Src/analog_sensors.h b/Src/analog_sensors.h index d361ac6..9a63fc5 100644 --- a/Src/analog_sensors.h +++ b/Src/analog_sensors.h @@ -1,8 +1,7 @@ #ifndef _ANALOG_SENSORS_H #define _ANALOG_SENSORS_H -void initialize_sensors(); -void refresh_sensors(); +void init_sensors(); void refresh_ldr_calib(); uint16_t get_joystick_x(); uint16_t get_joystick_y(); diff --git a/Src/buttons.c b/Src/buttons.c new file mode 100644 index 0000000..c9be623 --- /dev/null +++ b/Src/buttons.c @@ -0,0 +1,42 @@ +#include "buttons.h" + +#include "board/timer.h" +#include "board/rcc.h" +#include "board/gpio.h" +#include "board/iser.h" +#include "board/exti.h" + +#include "pins.h" + +void init_buttons() { + SET(RCC_AHB2ENR, GPIOAEN); + SET(RCC_AHB2ENR, GPIOCEN); + + SET_BITS(GPIOA->MODER, JOY_BTN * 2, INPUT_MODE, 2); + SET_BITS(GPIOA->PUPDR, JOY_BTN * 2, PULLUP, 2); + + SET_BITS(GPIOC->MODER, BLUE_BTN * 2, INPUT_MODE, 2); + SET_BITS(GPIOC->PUPDR, BLUE_BTN * 2, PULLDOWN, 2); + + EXTI->EXTISR[JOY_BTN] = 0; + SET(EXTI->IMR1, JOY_BTN); + SET(EXTI->FTSR1, JOY_BTN); + SET(ISER0, 11 + JOY_BTN); + + EXTI->EXTISR[BLUE_BTN] = 2; + SET(EXTI->IMR1, BLUE_BTN); + SET(EXTI->RTSR1, BLUE_BTN); + SET(ISER0, 11 + BLUE_BTN); +} + +//Depends on the value of JOY_BTN +void EXTI6_IRQHandler() { + joystick_button_handler(); + SET(EXTI->FPR1, JOY_BTN); +} + +//Depends on the value of BLUE_BTN +void EXTI13_IRQHandler() { + blue_button_handler(); + SET(EXTI->RPR1, BLUE_BTN); +} \ No newline at end of file diff --git a/Src/buttons.h b/Src/buttons.h new file mode 100644 index 0000000..89ea034 --- /dev/null +++ b/Src/buttons.h @@ -0,0 +1 @@ +void init_buttons(); \ No newline at end of file diff --git a/Src/drive.c b/Src/drive.c index 6e5a715..020bc94 100644 --- a/Src/drive.c +++ b/Src/drive.c @@ -109,6 +109,7 @@ void ultrasonic_stop() mode = AUTO_STOP; } } + void joystick_button_handler() { if (mode == MANUAL_STOP) @@ -126,9 +127,20 @@ void joystick_button_handler() } } +void blue_button_handler(){ + static int auto_mode = 0; + enable(); + if (auto_mode) { + auto_mode = 0; + set_mode(MANUAL); + } else { + init_mode(AUTO_WAIT); + auto_mode = 1; + } +} + void drive() { - static uint8_t disarm_manual_counter = 0; if ((get_distance() < 15) && (mode != MANUAL_OVERRIDE)) { diff --git a/Src/drive.h b/Src/drive.h index 88694df..8756281 100644 --- a/Src/drive.h +++ b/Src/drive.h @@ -20,5 +20,6 @@ void init_mode(uint8_t mode); void drive(); void driver_stop(); void joystick_button_handler(); +void blue_button_handler(); uint8_t get_mode(); #endif diff --git a/Src/main.c b/Src/main.c index 8e07fec..d2d827a 100644 --- a/Src/main.c +++ b/Src/main.c @@ -22,6 +22,8 @@ #include "board/rcc.h" #include "board/gpio.h" #include "board/iser.h" +#include "board/exti.h" + #include "utils.h" #include "motors.h" #include "analog_sensors.h" @@ -29,32 +31,11 @@ #include "drive.h" #include "pins.h" #include "indicators.h" -#include "board/adc.h" -#include "board/exti.h" #include "ultrasonic.h" +#include "buttons.h" #define BUSY_WAIT 1 -void init_extis() { - SET(RCC_AHB2ENR, GPIOAEN); - SET(RCC_AHB2ENR, GPIOCEN); - - SET_BITS(GPIOA->MODER, JOY_BTN * 2, INPUT_MODE, 2); - SET_BITS(GPIOA->PUPDR, JOY_BTN * 2, PULLUP, 2); - - SET_BITS(GPIOC->MODER, BLUE_BTN * 2, INPUT_MODE, 2); - SET_BITS(GPIOC->PUPDR, BLUE_BTN * 2, PULLDOWN, 2); - - EXTI->EXTISR[JOY_BTN] = 0; - SET(EXTI->IMR1, JOY_BTN); - SET(EXTI->FTSR1, JOY_BTN); - SET(ISER0, 11 + JOY_BTN); - - EXTI->EXTISR[BLUE_BTN] = 2; - SET(EXTI->IMR1, BLUE_BTN); - SET(EXTI->RTSR1, BLUE_BTN); - SET(ISER0, 11 + BLUE_BTN); -} void init_TIM7() { SET(RCC_APB1ENR1, TIM7EN); //TIM6x_CLK is enabled, running at 4MHz TIM7->EGR |= 1; //enable UIF to generate an interrupt @@ -71,45 +52,21 @@ void init_TIM7() { enable_interrupts(); } + void TIM7_IRQHandler(void) { SET(ADC1->CR, ADC_JADSTART); TIM7->SR = 0; //clear UIF } -void ADC1_2_IRQHandler() { - refresh_sensors(); - drive(); - SET(ADC1->ISR, ADC_JEOS); -} - -//Depends on the value of JOY_BTN -void EXTI6_IRQHandler() { - joystick_button_handler(); - SET(EXTI->FPR1, JOY_BTN); -} - -//Depends on the value of BLUE_BTN -void EXTI13_IRQHandler() { - static int auto_mode = 0; - enable(); - if (auto_mode) { - auto_mode = 0; - set_mode(MANUAL); - } else { - init_mode(AUTO_WAIT); - auto_mode = 1; - } - SET(EXTI->RPR1, BLUE_BTN); -} int main(void) { init_mode(MANUAL); init_indicators(); init_motors(); - init_extis(); init_leds(); set_led_direction(LED_STOP); - initialize_sensors(); + init_sensors(); init_ultrasonic(); + init_buttons(); if (BUSY_WAIT) { while (1) { for (int i = 0; i < 33300; i++)