clean how to get address

This commit is contained in:
King Kévin 2016-08-18 11:25:11 +02:00
parent 045f533afa
commit 05dac677ab
3 changed files with 53 additions and 41 deletions

View File

@ -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

View File

@ -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<<x)
#define GPIO_PIN(x) (1<<x)
/** get external interrupt based on pin identifier
* @note calculation true for F1 series
*/
#define EXTI(x) (1<<x)
/** get interrupt service routine for timer base on TIM */
#define TIM_ISR(x) CAT3(TIM,x,_IRQHandler)
#define TIM_ISR(x) CAT3(tim,x,_isr)
/** get interrupt service routine for timer base on external interrupt/pin */
#define EXTI_ISR(x) CAT3(exti,x,_isr)
#define exti5_isr exti9_5_isr /**< isr for line 9 to 5 for pin 5 */
#define exti6_isr exti9_5_isr /**< isr for line 9 to 5 for pin 6 */
#define exti7_isr exti9_5_isr /**< isr for line 9 to 5 for pin 7 */
#define exti8_isr exti9_5_isr /**< isr for line 9 to 5 for pin 8 */
#define exti9_isr exti9_5_isr /**< isr for line 9 to 5 for pin 9 */
#define exti10_isr exti15_10_isr /**< isr for line 15 to 10 for pin 10 */
#define exti11_isr exti15_10_isr /**< isr for line 15 to 10 for pin 11 */
#define exti12_isr exti15_10_isr /**< isr for line 15 to 10 for pin 12 */
#define exti13_isr exti15_10_isr /**< isr for line 15 to 10 for pin 13 */
#define exti14_isr exti15_10_isr /**< isr for line 15 to 10 for pin 14 */
#define exti15_isr exti15_10_isr /**< isr for line 15 to 10 for pin 15 */
/** @} */
/** @defgroup board_led board LED GPIO
@ -57,19 +76,16 @@ extern const uint32_t NVIC_TIM_IRQ[];
*/
#if defined(SYSTEM_BOARD)
/* on system board LED is on pin 11/PA1 */
#define LED_RCC RCC_GPIOA /**< GPIO peripheral clock (port A on system board) */
#define LED_PORT GPIOA /**< GPIO port (port A on system board) */
#define LED_PIN GPIO1 /**< GPIO pin (pin PA1 on system board) */
#define LED_PORT 0 /**< GPIO port (port A on system board) */
#define LED_PIN 1 /**< GPIO pin (pin PA1 on system board) */
#elif defined(BLUE_PILL)
/* on minimum system LED is on pin 2/PC13 */
#define LED_RCC RCC_GPIOC /**< GPIO peripheral clock (port C on blue pill) */
#define LED_PORT GPIOC /**< GPIO port (port C on blue pill) */
#define LED_PIN GPIO13 /**< GPIO pin (pin PC13 on system board) */
#define LED_PORT 2 /**< GPIO port (port C on blue pill) */
#define LED_PIN 13 /**< GPIO pin (pin PC13 on system board) */
#elif defined (MAPLE_MINI)
/* on maple mini LED is on pin 19/PB1 */
#define LED_RCC RCC_GPIOB /**< GPIO peripheral clock (port B on maple mini) */
#define LED_PORT GPIOB /**< GPIO port (port B on maple mini) */
#define LED_PIN GPIO1 /**< GPIO pin (pin PB1 on maple mini) */
#define LED_PORT 2 /**< GPIO port (port B on maple mini) */
#define LED_PIN 1 /**< GPIO pin (pin PB1 on maple mini) */
#endif
/** @} */
@ -78,12 +94,8 @@ extern const uint32_t NVIC_TIM_IRQ[];
*/
#if defined(MAPLE_MINI)
/* on maple mini user button is on 32/PB8 */
#define BUTTON_RCC RCC_GPIOB /**< GPIO peripheral clock */
#define BUTTON_PORT GPIOB /**< GPIO port */
#define BUTTON_PIN GPIO8 /**< GPIO pin */
#define BUTTON_EXTI EXTI8 /**< GPIO external interrupt */
#define BUTTON_IRQ NVIC_EXTI9_5_IRQ /**< GPIO line interrupt (interrupt for line 9 to 5 for pin) */
#define BUTTON_ISR exti9_5_isr /**< GPIO line interrupt service routine (isr for line 9 to 5 for pin 8) */
#define BUTTON_PORT 1 /**< GPIO port (port B on maple mini) */
#define BUTTON_PIN 8 /**< GPIO pin (pin PB8 on maple mini) */
#endif
/** @} */

View File

@ -28,7 +28,6 @@
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/timer.h> // timer library
#include <libopencm3/cm3/nvic.h> // interrupt handler
#include <libopencmsis/stm32/f1/irqhandlers.h> // 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) {