global.h
Go to the documentation of this file.
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#define DEBUG true
#define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
#define CAT2(x,y) x##y
#define CAT3(x,y,z) x##y##z
#define CAT4(w,x,y,z) w##x##y##z
#define COMPUTE_BUILD_YEAR \
( \
(__DATE__[ 7] - '0') * 1000 + \
(__DATE__[ 8] - '0') * 100 + \
(__DATE__[ 9] - '0') * 10 + \
(__DATE__[10] - '0') \
)
#define COMPUTE_BUILD_DAY \
( \
((__DATE__[4] >= '0') ? (__DATE__[4] - '0') * 10 : 0) + \
(__DATE__[5] - '0') \
)
#define BUILD_MONTH_IS_JAN (__DATE__[0] == 'J' && __DATE__[1] == 'a' && __DATE__[2] == 'n')
#define BUILD_MONTH_IS_FEB (__DATE__[0] == 'F')
#define BUILD_MONTH_IS_MAR (__DATE__[0] == 'M' && __DATE__[1] == 'a' && __DATE__[2] == 'r')
#define BUILD_MONTH_IS_APR (__DATE__[0] == 'A' && __DATE__[1] == 'p')
#define BUILD_MONTH_IS_MAY (__DATE__[0] == 'M' && __DATE__[1] == 'a' && __DATE__[2] == 'y')
#define BUILD_MONTH_IS_JUN (__DATE__[0] == 'J' && __DATE__[1] == 'u' && __DATE__[2] == 'n')
#define BUILD_MONTH_IS_JUL (__DATE__[0] == 'J' && __DATE__[1] == 'u' && __DATE__[2] == 'l')
#define BUILD_MONTH_IS_AUG (__DATE__[0] == 'A' && __DATE__[1] == 'u')
#define BUILD_MONTH_IS_SEP (__DATE__[0] == 'S')
#define BUILD_MONTH_IS_OCT (__DATE__[0] == 'O')
#define BUILD_MONTH_IS_NOV (__DATE__[0] == 'N')
#define BUILD_MONTH_IS_DEC (__DATE__[0] == 'D')
#define COMPUTE_BUILD_MONTH \
( \
(BUILD_MONTH_IS_JAN) ? 1 : \
(BUILD_MONTH_IS_FEB) ? 2 : \
(BUILD_MONTH_IS_MAR) ? 3 : \
(BUILD_MONTH_IS_APR) ? 4 : \
(BUILD_MONTH_IS_MAY) ? 5 : \
(BUILD_MONTH_IS_JUN) ? 6 : \
(BUILD_MONTH_IS_JUL) ? 7 : \
(BUILD_MONTH_IS_AUG) ? 8 : \
(BUILD_MONTH_IS_SEP) ? 9 : \
(BUILD_MONTH_IS_OCT) ? 10 : \
(BUILD_MONTH_IS_NOV) ? 11 : \
(BUILD_MONTH_IS_DEC) ? 12 : \
/* error default */ 99 \
)
#define BUILD_DATE_IS_BAD (__DATE__[0] == '?')
#define BUILD_YEAR ((BUILD_DATE_IS_BAD) ? 0 : COMPUTE_BUILD_YEAR)
#define BUILD_MONTH ((BUILD_DATE_IS_BAD) ? 99 : COMPUTE_BUILD_MONTH)
#define BUILD_DAY ((BUILD_DATE_IS_BAD) ? 99 : COMPUTE_BUILD_DAY)
#define GPIO(x) CAT2(GPIO,x)
#define RCC_GPIO(x) CAT2(RCC_GPIO,x)
#define TIM(x) CAT2(TIM,x)
#define RCC_TIM(x) CAT2(RCC_TIM,x)
#define NVIC_TIM_IRQ(x) CAT3(NVIC_TIM,x,_IRQ)
#define TIM_ISR(x) CAT3(tim,x,_isr)
#define TIM_CH_PORT(x,y) CAT4(GPIO_BANK_TIM,x,_CH,y)
#define TIM_CH_PIN(x,y) CAT4(GPIO_TIM,x,_CH,y)
#define RCC_TIM_CH(x,y) CAT4(RCC_TIM,x,_CH,y)
#define RCC_TIM1_CH1 RCC_GPIOA
#define RCC_TIM1_CH2 RCC_GPIOA
#define RCC_TIM1_CH3 RCC_GPIOA
#define RCC_TIM1_CH4 RCC_GPIOA
#define RCC_TIM1_CH1N RCC_GPIOB
#define RCC_TIM1_CH2N RCC_GPIOB
#define RCC_TIM1_CH3N RCC_GPIOB
#define RCC_TIM2_CH1_ETR RCC_GPIOA
#define RCC_TIM2_CH2 RCC_GPIOA
#define RCC_TIM2_CH3 RCC_GPIOA
#define RCC_TIM2_CH4 RCC_GPIOA
#define RCC_TIM3_CH1 RCC_GPIOA
#define RCC_TIM3_CH2 RCC_GPIOA
#define RCC_TIM3_CH3 RCC_GPIOB
#define RCC_TIM3_CH4 RCC_GPIOB
#define RCC_TIM4_CH1 RCC_GPIOB
#define RCC_TIM4_CH2 RCC_GPIOB
#define RCC_TIM4_CH3 RCC_GPIOB
#define RCC_TIM4_CH4 RCC_GPIOB
#define RCC_TIM5_CH1 RCC_GPIOA
#define RCC_TIM5_CH2 RCC_GPIOA
#define RCC_TIM5_CH3 RCC_GPIOA
#define RCC_TIM5_CH4 RCC_GPIOA
#define TIM_IC(x) CAT2(TIM_IC,x)
#define TIM_IC_IN_TI(x) CAT2(TIM_IC_IN_TI,x)
#define TIM_SR_CCIF(x) CAT3(TIM_SR_CC,x,IF)
#define TIM_DIER_CCIE(x) CAT3(TIM_DIER_CC,x,IE)
#define TIM_CCR(x,y) CAT2(TIM_CCR,y)(TIM(x))
#define EXTI(x) CAT2(EXTI,x)
#define NVIC_EXTI_IRQ(x) CAT3(NVIC_EXTI,x,_IRQ)
#define NVIC_EXTI5_IRQ NVIC_EXTI9_5_IRQ
#define NVIC_EXTI6_IRQ NVIC_EXTI9_5_IRQ
#define NVIC_EXTI7_IRQ NVIC_EXTI9_5_IRQ
#define NVIC_EXTI8_IRQ NVIC_EXTI9_5_IRQ
#define NVIC_EXTI9_IRQ NVIC_EXTI9_5_IRQ
#define NVIC_EXTI10_IRQ NVIC_EXTI15_10_IRQ
#define NVIC_EXTI11_IRQ NVIC_EXTI15_10_IRQ
#define NVIC_EXTI12_IRQ NVIC_EXTI15_10_IRQ
#define NVIC_EXTI13_IRQ NVIC_EXTI15_10_IRQ
#define NVIC_EXTI14_IRQ NVIC_EXTI15_10_IRQ
#define NVIC_EXTI15_IRQ NVIC_EXTI15_10_IRQ
#define EXTI_ISR(x) CAT3(exti,x,_isr)
#define exti5_isr exti9_5_isr
#define exti6_isr exti9_5_isr
#define exti7_isr exti9_5_isr
#define exti8_isr exti9_5_isr
#define exti9_isr exti9_5_isr
#define exti10_isr exti15_10_isr
#define exti11_isr exti15_10_isr
#define exti12_isr exti15_10_isr
#define exti13_isr exti15_10_isr
#define exti14_isr exti15_10_isr
#define exti15_isr exti15_10_isr
#define USART(x) CAT2(USART,x)
#define RCC_USART(x) CAT2(RCC_USART,x)
#define USART_IRQ(x) CAT3(NVIC_USART,x,_IRQ)
#define USART_ISR(x) CAT3(usart,x,_isr)
#define USART_TX_PORT(x) CAT3(GPIO_BANK_USART,x,_TX)
#define USART_RX_PORT(x) CAT3(GPIO_BANK_USART,x,_RX)
#define USART_RTS_PORT(x) CAT3(GPIO_BANK_USART,x,_RTS)
#define USART_CTS_PORT(x) CAT3(GPIO_BANK_USART,x,_CTS)
#define USART_TX_PIN(x) CAT3(GPIO_USART,x,_TX)
#define USART_RX_PIN(x) CAT3(GPIO_USART,x,_RX)
#define USART_RTS_PIN(x) CAT3(GPIO_USART,x,_RTS)
#define USART_CTS_PIN(x) CAT3(GPIO_USART,x,_CTS)
#define RCC_USART_PORT(x) CAT2(RCC_USART_PORT,x)
#define RCC_USART_PORT1 RCC_GPIOA
#define RCC_USART_PORT2 RCC_GPIOA
#define RCC_USART_PORT3 RCC_GPIOB
#define ADC12_IN_PORT(x) CAT3(ADC12_IN,x,_PORT)
#define ADC12_IN0_PORT GPIOA
#define ADC12_IN1_PORT GPIOA
#define ADC12_IN2_PORT GPIOA
#define ADC12_IN3_PORT GPIOA
#define ADC12_IN4_PORT GPIOA
#define ADC12_IN5_PORT GPIOA
#define ADC12_IN6_PORT GPIOA
#define ADC12_IN7_PORT GPIOA
#define ADC12_IN8_PORT GPIOB
#define ADC12_IN9_PORT GPIOB
#define ADC12_IN10_PORT GPIOC
#define ADC12_IN11_PORT GPIOC
#define ADC12_IN12_PORT GPIOC
#define ADC12_IN13_PORT GPIOC
#define ADC12_IN14_PORT GPIOC
#define ADC12_IN15_PORT GPIOC
#define ADC12_IN_PIN(x) CAT3(ADC12_IN,x,_PIN)
#define ADC12_IN0_PIN GPIO0
#define ADC12_IN1_PIN GPIO1
#define ADC12_IN2_PIN GPIO2
#define ADC12_IN3_PIN GPIO3
#define ADC12_IN4_PIN GPIO4
#define ADC12_IN5_PIN GPIO5
#define ADC12_IN6_PIN GPIO6
#define ADC12_IN7_PIN GPIO7
#define ADC12_IN8_PIN GPIO0
#define ADC12_IN9_PIN GPIO1
#define ADC12_IN10_PIN GPIO0
#define ADC12_IN11_PIN GPIO1
#define ADC12_IN12_PIN GPIO2
#define ADC12_IN13_PIN GPIO3
#define ADC12_IN14_PIN GPIO4
#define ADC12_IN15_PIN GPIO5
#define RCC_ADC12_IN(x) CAT2(RCC_ADC12_IN,x)
#define RCC_ADC12_IN0 RCC_GPIOA
#define RCC_ADC12_IN1 RCC_GPIOA
#define RCC_ADC12_IN2 RCC_GPIOA
#define RCC_ADC12_IN3 RCC_GPIOA
#define RCC_ADC12_IN4 RCC_GPIOA
#define RCC_ADC12_IN5 RCC_GPIOA
#define RCC_ADC12_IN6 RCC_GPIOA
#define RCC_ADC12_IN7 RCC_GPIOA
#define RCC_ADC12_IN8 RCC_GPIOB
#define RCC_ADC12_IN9 RCC_GPIOB
#define RCC_ADC12_IN10 RCC_GPIOC
#define RCC_ADC12_IN11 RCC_GPIOC
#define RCC_ADC12_IN12 RCC_GPIOC
#define RCC_ADC12_IN13 RCC_GPIOC
#define RCC_ADC12_IN14 RCC_GPIOC
#define RCC_ADC12_IN15 RCC_GPIOC
#define ADC_CHANNEL(x) CAT2(ADC_CHANNEL,x)
#define SPI(x) CAT2(SPI,x)
#define RCC_SPI(x) CAT2(RCC_SPI,x)
#define RCC_SPI_NSS_PORT(x) CAT3(RCC_SPI,x,_NSS_PORT)
#define RCC_SPI1_NSS_PORT RCC_GPIOA
#define RCC_SPI1_RE_NSS_PORT RCC_GPIOA
#define RCC_SPI2_NSS_PORT RCC_GPIOB
#define RCC_SPI_SCK_PORT(x) CAT3(RCC_SPI,x,_SCK_PORT)
#define RCC_SPI1_SCK_PORT RCC_GPIOA
#define RCC_SPI1_RE_SCK_PORT RCC_GPIOB
#define RCC_SPI2_SCK_PORT RCC_GPIOB
#define RCC_SPI_MISO_PORT(x) CAT3(RCC_SPI,x,_MISO_PORT)
#define RCC_SPI1_MISO_PORT RCC_GPIOA
#define RCC_SPI1_RE_MISO_PORT RCC_GPIOB
#define RCC_SPI2_MISO_PORT RCC_GPIOB
#define RCC_SPI_MOSI_PORT(x) CAT3(RCC_SPI,x,_MOSI_PORT)
#define RCC_SPI1_MOSI_PORT RCC_GPIOA
#define RCC_SPI1_RE_MOSI_PORT RCC_GPIOB
#define RCC_SPI2_MOSI_PORT RCC_GPIOB
#define SPI_NSS_PORT(x) CAT3(GPIO_BANK_SPI,x,_NSS)
#define SPI_SCK_PORT(x) CAT3(GPIO_BANK_SPI,x,_SCK)
#define SPI_MISO_PORT(x) CAT3(GPIO_BANK_SPI,x,_MISO)
#define SPI_MOSI_PORT(x) CAT3(GPIO_BANK_SPI,x,_MOSI)
#define SPI_NSS_PIN(x) CAT3(GPIO_SPI,x,_NSS)
#define SPI_SCK_PIN(x) CAT3(GPIO_SPI,x,_SCK)
#define SPI_MISO_PIN(x) CAT3(GPIO_SPI,x,_MISO)
#define SPI_MOSI_PIN(x) CAT3(GPIO_SPI,x,_MOSI)
#define SPI_CRC_PR(x) CAT3(SPI,x,_CRCPR)
#define SPI_CRC_TXR(x) CAT3(SPI,x,_TXCRCR)
#define SPI_CRC_RXR(x) CAT3(SPI,x,_RXCRCR)
#define DMA_SPI(x) CAT2(DMA_SPI,x)
#define DMA_SPI1 DMA1
#define DMA_SPI2 DMA1
#define DMA_SPI3 DMA2
#define RCC_DMA_SPI(x) CAT2(RCC_DMA_SPI,x)
#define RCC_DMA_SPI1 RCC_DMA1
#define RCC_DMA_SPI2 RCC_DMA1
#define RCC_DMA_SPI3 RCC_DMA2
#define DMA_CHANNEL_SPI_TX(x) CAT3(DMA_CHANNEL_SPI,x,_TX)
#define DMA_CHANNEL_SPI1_TX DMA_CHANNEL3
#define DMA_CHANNEL_SPI2_TX DMA_CHANNEL5
#define DMA_CHANNEL_SPI3_TX DMA_CHANNEL2
#define DMA_CHANNEL_SPI_RX(x) CAT3(DMA_CHANNEL_SPI,x,_RX)
#define DMA_CHANNEL_SPI1_RX DMA_CHANNEL4
#define DMA_CHANNEL_SPI2_RX DMA_CHANNEL2
#define DMA_CHANNEL_SPI3_RX DMA_CHANNEL1
#define DMA_IRQ_SPI_TX(x) CAT3(NVIC_DMA_CHANNEL_IRQ_SPI,x,_TX)
#define NVIC_DMA_CHANNEL_IRQ_SPI1_TX NVIC_DMA1_CHANNEL3_IRQ
#define NVIC_DMA_CHANNEL_IRQ_SPI2_TX NVIC_DMA1_CHANNEL5_IRQ
#define NVIC_DMA_CHANNEL_IRQ_SPI3_TX NVIC_DMA2_CHANNEL2_IRQ
#define DMA_IRQ_SPI_RX(x) CAT3(NVIC_DMA_CHANNEL_IRQ_SPI,x,_RX)
#define NVIC_DMA_CHANNEL_IRQ_SPI1_RX NVIC_DMA1_CHANNEL4_IRQ
#define NVIC_DMA_CHANNEL_IRQ_SPI2_RX NVIC_DMA1_CHANNEL2_IRQ
#define NVIC_DMA_CHANNEL_IRQ_SPI3_RX NVIC_DMA2_CHANNEL1_IRQ
#define DMA_ISR_SPI_TX(x) CAT3(DMA_CHANNEL_ISR_SPI,x,_TX)
#define DMA_CHANNEL_ISR_SPI1_TX dma1_channel3_isr
#define DMA_CHANNEL_ISR_SPI2_TX dma1_channel5_isr
#define DMA_CHANNEL_ISR_SPI3_TX dma2_channel2_isr
#define DMA_ISR_SPI_RX(x) CAT3(DMA_CHANNEL_ISR_SPI,x,_RX)
#define DMA_CHANNEL_ISR_SPI1_RX dma1_channel4_isr
#define DMA_CHANNEL_ISR_SPI2_RX dma1_channel2_isr
#define DMA_CHANNEL_ISR_SPI3_RX dma2_channel1_isr
#if defined(SYSTEM_BOARD) || defined(CORE_BOARD)
/* on system and core board LED is on pin 11/PA1 */
#define LED_PORT A
#define LED_PIN 1
#define LED_ON 0
#elif defined(BLUE_PILL)
/* on minimum system LED is on pin 2/PC13 */
#define LED_PORT C
#define LED_PIN 13
#define LED_ON 0
#elif defined (MAPLE_MINI)
/* on maple mini LED is on pin 19/PB1 */
#define LED_PORT B
#define LED_PIN 1
#define LED_ON 1
#elif defined (BUSVOODOO)
/* on BusVoodoo LED is on pin PA8 */
#define LED_PORT A
#define LED_PIN 8
#define LED_ON 1
#endif
#if defined(MAPLE_MINI)
/* on maple mini user button is on 32/PB8 */
#define BUTTON_PORT B
#define BUTTON_PIN 8
#define BUTTON_PRESSED 1
#elif defined(CORE_BOARD)
/* on core board user button is on PA8 */
#define BUTTON_PORT A
#define BUTTON_PIN 8
#define BUTTON_PRESSED 0
#endif
#if defined(MAPLE_MINI)
/* use button */
#define DFU_FORCE_PORT BUTTON_PORT
#define DFU_FORCE_PIN BUTTON_PIN
#define DFU_FORCE_VALUE 1
#elif defined(CORE_BOARD)
/* use button */
#define DFU_FORCE_PORT BUTTON_PORT
#define DFU_FORCE_PIN BUTTON_PIN
#define DFU_FORCE_VALUE 0
#elif defined(BUSVOODOO)
#if BUSVOODOO_HARDWARE_VERSION==0
/* on BusVoodoo v0 DFU input is on PC7 */
#define DFU_FORCE_PORT C
#define DFU_FORCE_PIN 7
#define DFU_FORCE_VALUE 1
#else
/* on BusVoodoo vA DFU input is on PC4 */
#define DFU_FORCE_PORT C
#define DFU_FORCE_PIN 4
#define DFU_FORCE_VALUE 1
#endif
#else
/* use the JNTRST pin as GPIO (SWJ will still be working, minus NTRST) */
#define DFU_FORCE_PORT B
#define DFU_FORCE_PIN 4
#define DFU_FORCE_VALUE 0
#endif
extern char __application_beginning;
extern char __application_end;
extern char __flash_end;
extern volatile bool button_flag;
extern volatile bool user_input_available;
char* b2s(uint64_t binary, uint8_t rjust);
void led_on(void);
void led_off(void);
void led_toggle(void);
#if defined(BUSVOODOO)
void led_blink(double period, double duty);
void led_blue(void);
void led_red(void);
#endif
void sleep_us(uint32_t duration);
void sleep_ms(uint32_t duration);
char user_input_get(void);
void user_input_store(char c);
void board_setup(void);
led_off
void led_off(void)
switch off board LED
Definition: global.c:82
__application_beginning
char __application_beginning
symbol for beginning of the application
led_on
void led_on(void)
switch on board LED
Definition: global.c:68
__flash_end
char __flash_end
symbol for end of flash
led_toggle
void led_toggle(void)
toggle board LED
Definition: global.c:97
sleep_us
void sleep_us(uint32_t duration)
go to sleep for some microseconds
Definition: global.c:144
sleep_ms
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:159
b2s
char * b2s(uint64_t binary, uint8_t rjust)
get binary representation of a number
Definition: global.c:45
user_input_get
char user_input_get(void)
get user input
Definition: global.c:187
__application_end
char __application_end
symbol for end of the application
user_input_available
volatile bool user_input_available
flag set when user input is available
Definition: global.c:37
button_flag
volatile bool button_flag
flag set when board user button has been pressed/released
Definition: global.c:36
board_setup
void board_setup(void)
setup board peripherals
Definition: global.c:211
user_input_store
void user_input_store(char c)
store user input
Definition: global.c:199