Skip to content

Commit

Permalink
Migrated button press interrupts to buttons driver
Browse files Browse the repository at this point in the history
Migrated interrupts from main to drivers
  • Loading branch information
ilgazer committed Jan 16, 2023
1 parent 175b196 commit ae971f4
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Debug
Startup
*.launch
*.launch
.vscode/settings.json
9 changes: 8 additions & 1 deletion Src/analog_sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions Src/analog_sensors.h
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
42 changes: 42 additions & 0 deletions Src/buttons.c
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions Src/buttons.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void init_buttons();
14 changes: 13 additions & 1 deletion Src/drive.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void ultrasonic_stop()
mode = AUTO_STOP;
}
}

void joystick_button_handler()
{
if (mode == MANUAL_STOP)
Expand All @@ -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))
{
Expand Down
1 change: 1 addition & 0 deletions Src/drive.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
55 changes: 6 additions & 49 deletions Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,20 @@
#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"
#include "leds.h"
#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
Expand All @@ -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++)
Expand Down

0 comments on commit ae971f4

Please sign in to comment.