From 05dac677ab4ffe7eaab112547f3270a264265faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Thu, 18 Aug 2016 11:25:11 +0200 Subject: [PATCH] clean how to get address --- global.c | 37 ++++++++++++++++++----------------- global.h | 50 ++++++++++++++++++++++++++++++------------------ lib/usart_soft.c | 7 +++---- 3 files changed, 53 insertions(+), 41 deletions(-) diff --git a/global.c b/global.c index 370a4f5..2db2213 100644 --- a/global.c +++ b/global.c @@ -32,11 +32,12 @@ volatile bool button_flag = false; -const uint32_t GPIO[] = {GPIOA,GPIOB,GPIOC,GPIOD,GPIOE,GPIOF,GPIOG}; +const uint32_t GPIO_PORT[] = {GPIOA,GPIOB,GPIOC,GPIOD,GPIOE,GPIOF,GPIOG}; const uint32_t RCC_GPIO[] = {RCC_GPIOA,RCC_GPIOB,RCC_GPIOC,RCC_GPIOD,RCC_GPIOE,RCC_GPIOF,RCC_GPIOG}; const uint32_t TIM[] = {~0,TIM1,TIM2,TIM3,TIM4,TIM5,TIM6,TIM7,TIM8,TIM9,TIM10,TIM11,TIM12,TIM13,TIM14,TIM15,TIM16,TIM17}; const uint32_t RCC_TIM[] = {~0,RCC_TIM1,RCC_TIM2,RCC_TIM3,RCC_TIM4,RCC_TIM5,RCC_TIM6,RCC_TIM7,RCC_TIM8,RCC_TIM9,RCC_TIM10,RCC_TIM11,RCC_TIM12,RCC_TIM13,RCC_TIM14,RCC_TIM15,RCC_TIM16,RCC_TIM17}; -const uint32_t NVIC_TIM_IRQ[] = {~0,~0,NVIC_TIM2_IRQ,NVIC_TIM3_IRQ,NVIC_TIM4_IRQ,NVIC_TIM5_IRQ,NVIC_TIM6_IRQ,NVIC_TIM7_IRQ,~0,~0,~0,~0,~0,~0,~0,~0,~0,~0}; +const uint8_t NVIC_TIM_IRQ[] = {~0,~0,NVIC_TIM2_IRQ,NVIC_TIM3_IRQ,NVIC_TIM4_IRQ,NVIC_TIM5_IRQ,NVIC_TIM6_IRQ,NVIC_TIM7_IRQ,~0,~0,~0,~0,~0,~0,~0,~0,~0,~0}; +const uint8_t NVIC_EXTI_IRQ[] = {NVIC_EXTI0_IRQ,NVIC_EXTI1_IRQ,NVIC_EXTI2_IRQ,NVIC_EXTI3_IRQ,NVIC_EXTI4_IRQ,NVIC_EXTI9_5_IRQ,NVIC_EXTI9_5_IRQ,NVIC_EXTI9_5_IRQ,NVIC_EXTI9_5_IRQ,NVIC_EXTI9_5_IRQ,NVIC_EXTI15_10_IRQ,NVIC_EXTI15_10_IRQ,NVIC_EXTI15_10_IRQ,NVIC_EXTI15_10_IRQ,NVIC_EXTI15_10_IRQ,NVIC_EXTI15_10_IRQ}; char* b2s(uint64_t binary, uint8_t rjust) { @@ -64,18 +65,18 @@ char* b2s(uint64_t binary, uint8_t rjust) void led_on(void) { #if defined(SYSTEM_BOARD) || defined(BLUE_PILL) - gpio_clear(LED_PORT, LED_PIN); + gpio_clear(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN)); #elif defined(MAPLE_MINI) - gpio_set(LED_PORT, LED_PIN); + gpio_set(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN)); #endif } /** switch off board LED */ void led_off(void) { #if defined(SYSTEM_BOARD) || defined(BLUE_PILL) - gpio_set(LED_PORT, LED_PIN); + gpio_set(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN)); #elif defined(MAPLE_MINI) - gpio_clear(LED_PORT, LED_PIN); + gpio_clear(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN)); #endif } /** toggle board LED */ @@ -87,26 +88,26 @@ void led_toggle(void) void board_setup(void) { // setup LED - rcc_periph_clock_enable(LED_RCC); // enable clock for LED - gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull' + rcc_periph_clock_enable(RCC_GPIO[LED_PORT]); // enable clock for LED + gpio_set_mode(GPIO_PORT[LED_PORT], GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO_PIN(LED_PIN)); // set LED pin to 'output push-pull' led_off(); // switch off LED per default // setup button -#if defined(BUTTON_RCC) && defined(BUTTON_PORT) && defined(BUTTON_PIN) && defined(BUTTON_EXTI) && defined(BUTTON_IRQ) - rcc_periph_clock_enable(BUTTON_RCC); // enable clock for button - gpio_set_mode(BUTTON_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, BUTTON_PIN); // set button pin to input - gpio_clear(BUTTON_PORT, BUTTON_PIN); // pull down to be able to detect button push (go high) +#if defined(BUTTON_PORT) && defined(BUTTON_PIN) + rcc_periph_clock_enable(RCC_GPIO[BUTTON_RCC]); // enable clock for button + gpio_set_mode(GPIO_PORT[BUTTON_PORT], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_PIN(BUTTON_PIN)); // set button pin to input + gpio_clear(GPIO_PORT[BUTTON_PORT], GPIO_PIN(BUTTON_PIN)); // pull down to be able to detect button push (go high) rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt - exti_select_source(BUTTON_EXTI, BUTTON_PORT); // mask external interrupt of this pin only for this port - exti_set_trigger(BUTTON_EXTI, EXTI_TRIGGER_RISING); // trigger when button is pressed - exti_enable_request(BUTTON_EXTI); // enable external interrupt - nvic_enable_irq(BUTTON_IRQ); // enable interrupt + exti_select_source(EXTI(BUTTON_PIN), GPIO_PORT[BUTTON_PORT]); // mask external interrupt of this pin only for this port + exti_set_trigger(EXTI(BUTTON_PIN), EXTI_TRIGGER_RISING); // trigger when button is pressed + exti_enable_request(EXTI(BUTTON_PIN)); // enable external interrupt + nvic_enable_irq(NVIC_EXTI_IRQ[BUTTON_PIN]); // enable interrupt #endif } -#if defined(BUTTON_ISR) && defined(BUTTON_EXTI) +#if defined(BUTTON_PIN) /** interrupt service routine called when button is pressed */ -void BUTTON_ISR(void) +void EXTI_ISR(BUTTON_PIN)(void) { exti_reset_request(BUTTON_EXTI); // reset interrupt button_flag = true; // perform button action diff --git a/global.h b/global.h index d40d664..396432f 100644 --- a/global.h +++ b/global.h @@ -31,7 +31,7 @@ * @{ */ /** get GPIO port based on port identifier (0=A, ...) */ -extern const uint32_t GPIO[]; +extern const uint32_t GPIO_PORT[]; /** get RCC for GPIO based on GPIO identifier */ extern const uint32_t RCC_GPIO[]; /** get TIM based on TIM identifier */ @@ -39,7 +39,9 @@ extern const uint32_t TIM[]; /** get RCC for timer based on TIM */ extern const uint32_t RCC_TIM[]; /** get NVIC IRQ for timer base on TIM */ -extern const uint32_t NVIC_TIM_IRQ[]; +extern const uint8_t NVIC_TIM_IRQ[]; +/** get NVIC IRQ for external interrupt base on external interrupt/pin */ +extern const uint8_t NVIC_EXTI_IRQ[]; /** @} */ /** @defgroup reg_macro macros to get define values based on other defines values @@ -47,9 +49,26 @@ extern const uint32_t NVIC_TIM_IRQ[]; * @{ */ /** get GPIO pin based on pin identifier */ -#define GPIO(x) (1< // general purpose input output library #include // timer library #include // interrupt handler -#include // interrupt handler #include "usart_soft.h" // software USART library API #include "global.h" // common methods @@ -80,12 +79,12 @@ bool usart_soft_setup(void) // setup GPIOs for (uint8_t i = 0; i < USART_SOFT_RX_PORTS; i++) { rcc_periph_clock_enable(RCC_GPIO[usart_soft_rx_ports[i]]); // enable clock for GPIO peripheral - gpio_set_mode(GPIO[usart_soft_rx_ports[i]], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(usart_soft_rx_pins[i])); // setup GPIO pin USART receive - gpio_set(GPIO[usart_soft_rx_ports[i]], GPIO(usart_soft_rx_pins[i])); // pull up to avoid noise when not connected + gpio_set_mode(GPIO_PORT[usart_soft_rx_ports[i]], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_PIN(usart_soft_rx_pins[i])); // setup GPIO pin USART receive + gpio_set(GPIO_PORT[usart_soft_rx_ports[i]], GPIO_PIN(usart_soft_rx_pins[i])); // pull up to avoid noise when not connected } for (uint8_t i = 0; i < USART_SOFT_TX_PORTS; i++) { rcc_periph_clock_enable(RCC_GPIO[usart_soft_tx_ports[i]]); // enable clock for GPIO peripheral - gpio_set_mode(GPIO[usart_soft_tx_ports[i]], GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(usart_soft_tx_pins[i])); // setup GPIO pin USART transmit + gpio_set_mode(GPIO_PORT[usart_soft_tx_ports[i]], GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO_PIN(usart_soft_tx_pins[i])); // setup GPIO pin USART transmit } // setup timer if (USART_SOFT_RX_PORTS>0) {