use macros instead of tables for less possible errors (but everything has to be defined at compile time)
This commit is contained in:
parent
af3da18de7
commit
6959c3a41f
33
global.c
33
global.c
@ -32,13 +32,6 @@
|
||||
|
||||
volatile bool button_flag = false;
|
||||
|
||||
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 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)
|
||||
{
|
||||
static char string[64+1] = {0}; // the string representation to return
|
||||
@ -65,43 +58,43 @@ char* b2s(uint64_t binary, uint8_t rjust)
|
||||
void led_on(void)
|
||||
{
|
||||
#if defined(SYSTEM_BOARD) || defined(BLUE_PILL)
|
||||
gpio_clear(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN));
|
||||
gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN));
|
||||
#elif defined(MAPLE_MINI)
|
||||
gpio_set(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN));
|
||||
gpio_set(GPIO(LED_PORT), GPIO(LED_PIN));
|
||||
#endif
|
||||
}
|
||||
/** switch off board LED */
|
||||
void led_off(void)
|
||||
{
|
||||
#if defined(SYSTEM_BOARD) || defined(BLUE_PILL)
|
||||
gpio_set(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN));
|
||||
gpio_set(GPIO(LED_PORT), GPIO(LED_PIN));
|
||||
#elif defined(MAPLE_MINI)
|
||||
gpio_clear(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN));
|
||||
gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN));
|
||||
#endif
|
||||
}
|
||||
/** toggle board LED */
|
||||
void led_toggle(void)
|
||||
{
|
||||
gpio_toggle(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN));
|
||||
gpio_toggle(GPIO(LED_PORT), GPIO(LED_PIN));
|
||||
}
|
||||
|
||||
void board_setup(void)
|
||||
{
|
||||
// setup LED
|
||||
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'
|
||||
rcc_periph_clock_enable(RCC_GPIO(LED_PORT)); // enable clock for LED
|
||||
gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_PIN)); // set LED pin to 'output push-pull'
|
||||
led_off(); // switch off LED per default
|
||||
|
||||
// setup button
|
||||
#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_GPIO(BUTTON_PORT)); // enable clock for button
|
||||
gpio_set_mode(GPIO(BUTTON_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUTTON_PIN)); // set button pin to input
|
||||
gpio_clear(GPIO(BUTTON_PORT), GPIO(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(EXTI(BUTTON_PIN), GPIO_PORT[BUTTON_PORT]); // mask external interrupt of this pin only for this port
|
||||
exti_select_source(EXTI(BUTTON_PIN), GPIO(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
|
||||
nvic_enable_irq(NVIC_EXTI_IRQ(BUTTON_PIN)); // enable interrupt
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -109,7 +102,7 @@ void board_setup(void)
|
||||
/** interrupt service routine called when button is pressed */
|
||||
void EXTI_ISR(BUTTON_PIN)(void)
|
||||
{
|
||||
exti_reset_request(BUTTON_EXTI); // reset interrupt
|
||||
exti_reset_request(EXTI(BUTTON_PIN)); // reset interrupt
|
||||
button_flag = true; // perform button action
|
||||
}
|
||||
#endif
|
||||
|
59
global.h
59
global.h
@ -26,36 +26,37 @@
|
||||
/** concatenate 3 arguments (used to defer concatenation) */
|
||||
#define CAT3(x,y,z) x##y##z
|
||||
|
||||
/** @defgroup reg_table get register address based on identifier
|
||||
* @note used when the value is not calculated
|
||||
* @{
|
||||
*/
|
||||
/** get GPIO port based on port identifier (0=A, ...) */
|
||||
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 */
|
||||
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 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
|
||||
/** @defgroup reg_macro macros to define values based on other defines values
|
||||
* @note used when the value is calculated or isn't a value
|
||||
* @{
|
||||
*/
|
||||
/** get GPIO pin based on pin identifier */
|
||||
#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 GPIO based on GPIO identifier */
|
||||
#define GPIO(x) CAT2(GPIO,x)
|
||||
/** get RCC for GPIO based on GPIO identifier */
|
||||
#define RCC_GPIO(x) CAT2(RCC_GPIO,x)
|
||||
/** get TIM based on TIM identifier */
|
||||
#define TIM(x) CAT2(TIM,x)
|
||||
/** get RCC for timer based on TIM */
|
||||
#define RCC_TIM(x) CAT2(RCC_TIM,x)
|
||||
/** get NVIC IRQ for timer base on TIM */
|
||||
#define NVIC_TIM_IRQ(x) CAT3(NVIC_TIM,x,_IRQ)
|
||||
/** get interrupt service routine for timer base on TIM */
|
||||
#define TIM_ISR(x) CAT3(tim,x,_isr)
|
||||
/** get external interrupt based on pin identifier */
|
||||
#define EXTI(x) CAT2(EXT,x)
|
||||
/** get NVIC IRQ for external interrupt base on external interrupt/pin */
|
||||
#define NVIC_EXTI_IRQ(x) CAT3(NVIC_EXTI,x,_IRQ)
|
||||
#define NVIC_EXTI5_IRQ NVIC_EXTI9_5_IRQ /**< IRQ for line 9 to 5 for pin 5 */
|
||||
#define NVIC_EXTI6_IRQ NVIC_EXTI9_5_IRQ /**< IRQ for line 9 to 5 for pin 6 */
|
||||
#define NVIC_EXTI7_IRQ NVIC_EXTI9_5_IRQ /**< IRQ for line 9 to 5 for pin 7 */
|
||||
#define NVIC_EXTI8_IRQ NVIC_EXTI9_5_IRQ /**< IRQ for line 9 to 5 for pin 8 */
|
||||
#define NVIC_EXTI9_IRQ NVIC_EXTI9_5_IRQ /**< IRQ for line 9 to 5 for pin 9 */
|
||||
#define NVIC_EXTI10_IRQ NVIC_EXTI15_10_IRQ /**< IRQ for line 15 to 10 for pin 10 */
|
||||
#define NVIC_EXTI11_IRQ NVIC_EXTI15_10_IRQ /**< IRQ for line 15 to 10 for pin 11 */
|
||||
#define NVIC_EXTI12_IRQ NVIC_EXTI15_10_IRQ /**< IRQ for line 15 to 10 for pin 12 */
|
||||
#define NVIC_EXTI13_IRQ NVIC_EXTI15_10_IRQ /**< IRQ for line 15 to 10 for pin 13 */
|
||||
#define NVIC_EXTI14_IRQ NVIC_EXTI15_10_IRQ /**< IRQ for line 15 to 10 for pin 14 */
|
||||
#define NVIC_EXTI15_IRQ NVIC_EXTI15_10_IRQ /**< IRQ for line 15 to 10 for pin 15 */
|
||||
/** 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 */
|
||||
@ -76,15 +77,15 @@ extern const uint8_t NVIC_EXTI_IRQ[];
|
||||
*/
|
||||
#if defined(SYSTEM_BOARD)
|
||||
/* on system board LED is on pin 11/PA1 */
|
||||
#define LED_PORT 0 /**< GPIO port (port A on system board) */
|
||||
#define LED_PORT A /**< 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_PORT 2 /**< GPIO port (port C on blue pill) */
|
||||
#define LED_PORT C /**< 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_PORT 1 /**< GPIO port (port B on maple mini) */
|
||||
#define LED_PORT B /**< GPIO port (port B on maple mini) */
|
||||
#define LED_PIN 1 /**< GPIO pin (pin PB1 on maple mini) */
|
||||
#endif
|
||||
/** @} */
|
||||
@ -94,7 +95,7 @@ extern const uint8_t NVIC_EXTI_IRQ[];
|
||||
*/
|
||||
#if defined(MAPLE_MINI)
|
||||
/* on maple mini user button is on 32/PB8 */
|
||||
#define BUTTON_PORT 1 /**< GPIO port (port B on maple mini) */
|
||||
#define BUTTON_PORT B /**< GPIO port (port B on maple mini) */
|
||||
#define BUTTON_PIN 8 /**< GPIO pin (pin PB8 on maple mini) */
|
||||
#endif
|
||||
/** @} */
|
||||
|
Loading…
Reference in New Issue
Block a user