26 #include <libopencmsis/core_cm3.h> 27 #include <libopencm3/cm3/nvic.h> 28 #include <libopencm3/cm3/systick.h> 29 #include <libopencm3/stm32/rcc.h> 30 #include <libopencm3/stm32/gpio.h> 31 #include <libopencm3/stm32/timer.h> 32 #include <libopencm3/stm32/exti.h> 45 char*
b2s(uint64_t binary, uint8_t rjust)
47 static char string[64+1] = {0};
48 uint8_t bit =
LENGTH(
string)-1;
60 while (64-bit-1<rjust && bit>0) {
70 #if defined(BUSVOODOO) 71 timer_disable_counter(TIM1);
72 gpio_set_mode(
GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
GPIO(LED_PIN));
74 #if defined(LED_ON) && LED_ON 75 gpio_set(
GPIO(LED_PORT),
GPIO(LED_PIN));
77 gpio_clear(
GPIO(LED_PORT),
GPIO(LED_PIN));
84 #if defined(BUSVOODOO) 85 timer_disable_counter(TIM1);
86 gpio_set_mode(
GPIO(LED_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT,
GPIO(LED_PIN));
88 #if defined(LED_ON) && LED_ON 89 gpio_clear(
GPIO(LED_PORT),
GPIO(LED_PIN));
91 gpio_set(
GPIO(LED_PORT),
GPIO(LED_PIN));
99 #if defined(BUSVOODOO) 100 timer_disable_counter(TIM1);
101 gpio_set_mode(
GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
GPIO(LED_PIN));
103 gpio_toggle(
GPIO(LED_PORT),
GPIO(LED_PIN));
106 #if defined(BUSVOODOO) 107 void led_blink(
double period,
double duty)
109 if (period<0.0 || period>6.0 || duty<0.0 || duty>1.0) {
112 timer_disable_counter(TIM1);
114 gpio_set_mode(
GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
GPIO(LED_PIN));
116 gpio_set(
GPIO(LED_PORT),
GPIO(LED_PIN));
118 gpio_clear(
GPIO(LED_PORT),
GPIO(LED_PIN));
121 gpio_set_mode(
GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
GPIO(LED_PIN));
122 timer_set_counter(TIM1, 0);
123 timer_set_period(TIM1, 0xffff*(period/6.0));
124 timer_set_oc_value(TIM1, TIM_OC1, 0xffff*(period/6.0)*duty);
125 timer_enable_counter(TIM1);
131 timer_disable_counter(TIM1);
132 gpio_set_mode(
GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
GPIO(LED_PIN));
133 gpio_set(
GPIO(LED_PORT),
GPIO(LED_PIN));
138 timer_disable_counter(TIM1);
139 gpio_set_mode(
GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
GPIO(LED_PIN));
140 gpio_clear(
GPIO(LED_PORT),
GPIO(LED_PIN));
146 systick_counter_disable();
147 if (!systick_set_frequency(1000000,rcc_ahb_frequency)) {
151 systick_interrupt_enable();
153 systick_counter_enable();
161 systick_counter_disable();
162 if (!systick_set_frequency(1000,rcc_ahb_frequency)) {
166 systick_interrupt_enable();
168 systick_counter_enable();
181 systick_counter_disable();
182 systick_interrupt_disable();
214 rcc_periph_clock_enable(
RCC_GPIO(LED_PORT));
215 #if defined(BUSVOODOO) 217 rcc_periph_clock_enable(RCC_TIM1);
219 timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1, TIM_CR1_DIR_UP);
220 timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM1);
221 timer_enable_oc_output(TIM1, TIM_OC1);
222 timer_enable_break_main_output(TIM1);
223 timer_set_prescaler(TIM1, 3296-1);
225 gpio_set_mode(
GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
GPIO(LED_PIN));
230 #if defined(BUTTON_PORT) && defined(BUTTON_PIN) 231 rcc_periph_clock_enable(
RCC_GPIO(BUTTON_PORT));
232 gpio_set_mode(
GPIO(BUTTON_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN,
GPIO(BUTTON_PIN));
233 rcc_periph_clock_enable(RCC_AFIO);
234 exti_select_source(
EXTI(BUTTON_PIN),
GPIO(BUTTON_PORT));
235 #if defined(BUTTON_PRESSED) && BUTTON_PRESSED 236 gpio_clear(
GPIO(BUTTON_PORT),
GPIO(BUTTON_PIN));
237 exti_set_trigger(
EXTI(BUTTON_PIN), EXTI_TRIGGER_RISING);
239 gpio_set(
GPIO(BUTTON_PORT),
GPIO(BUTTON_PIN));
240 exti_set_trigger(
EXTI(BUTTON_PIN), EXTI_TRIGGER_FALLING);
242 exti_enable_request(
EXTI(BUTTON_PIN));
252 #if defined(BUTTON_PIN) 256 exti_reset_request(
EXTI(BUTTON_PIN));
void board_setup(void)
setup board peripherals
#define RCC_GPIO(x)
get RCC for GPIO based on GPIO identifier
void led_off(void)
switch off board LED
void user_input_store(char c)
store user input
#define NVIC_EXTI_IRQ(x)
get NVIC IRQ for external interrupt base on external interrupt/pin
global definitions and methods (API)
#define GPIO(x)
get GPIO based on GPIO identifier
static volatile uint8_t user_input_buffer[64]
ring buffer for received data
void sleep_us(uint32_t duration)
go to sleep for some microseconds
void sys_tick_handler(void)
SysTick interrupt handler.
char * b2s(uint64_t binary, uint8_t rjust)
get binary representation of a number
#define EXTI(x)
get external interrupt based on pin identifier
volatile bool button_flag
flag set when board user button has been pressed/released
char user_input_get(void)
get user input
#define EXTI_ISR(x)
get interrupt service routine for timer base on external interrupt/pin
static volatile uint8_t user_input_i
current position of read received data
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
#define LENGTH(x)
get the length of an array
void led_toggle(void)
toggle board LED
static volatile uint32_t sleep_duration
sleep duration count down (in SysTick interrupts)
volatile bool user_input_available
flag set when user input is available
void led_on(void)
switch on board LED
static volatile uint8_t user_input_used
how much data has been received and not red