CuVoodoo STM32F1 firmware template
global.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
20 /* standard libraries */
21 #include <stdint.h> // standard integer types
22 #include <stdlib.h> // general utilities
23 #include <string.h> // memory utilities
24 
25 /* STM32 (including CM3) libraries */
26 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
27 #include <libopencm3/cm3/nvic.h> // interrupt handler
28 #include <libopencm3/cm3/systick.h> // SysTick library
29 #include <libopencm3/stm32/rcc.h> // real-time control clock library
30 #include <libopencm3/stm32/gpio.h> // general purpose input output library
31 #include <libopencm3/stm32/timer.h> // timer library
32 #include <libopencm3/stm32/exti.h> // external interrupt defines
33 
34 #include "global.h" // common methods
35 
36 volatile bool button_flag = false;
37 volatile bool user_input_available = false;
38 
39 static volatile uint8_t user_input_buffer[64] = {0};
40 static volatile uint8_t user_input_i = 0;
41 static volatile uint8_t user_input_used = 0;
43 static volatile uint32_t sleep_duration = 0;
45 char* b2s(uint64_t binary, uint8_t rjust)
46 {
47  static char string[64+1] = {0}; // the string representation to return
48  uint8_t bit = LENGTH(string)-1; // the index of the bit to print
49  string[bit--] = '\0'; // terminate string
50 
51  while (binary) {
52  if (binary & 1) {
53  string[bit--] = '1';
54  } else {
55  string[bit--] = '0';
56  }
57  binary >>= 1;
58  }
59 
60  while (64-bit-1<rjust && bit>0) {
61  string[bit--] = '0';
62  }
63 
64  return string;
65 }
66 
68 void led_on(void)
69 {
70 #if defined(BUSVOODOO)
71  timer_disable_counter(TIM1); // disable timer for PWM
72  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'
73 #endif
74 #if defined(LED_ON) && LED_ON
75  gpio_set(GPIO(LED_PORT), GPIO(LED_PIN));
76 #else
77  gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN));
78 #endif
79 }
80 
82 void led_off(void)
83 {
84 #if defined(BUSVOODOO)
85  timer_disable_counter(TIM1); // disable timer for PWM
86  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(LED_PIN)); // set LED pin to 'output push-pull'
87 #else
88 #if defined(LED_ON) && LED_ON
89  gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN));
90 #else
91  gpio_set(GPIO(LED_PORT), GPIO(LED_PIN));
92 #endif
93 #endif
94 }
95 
97 void led_toggle(void)
98 {
99 #if defined(BUSVOODOO)
100  timer_disable_counter(TIM1); // disable timer for PWM
101  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'
102 #endif
103  gpio_toggle(GPIO(LED_PORT), GPIO(LED_PIN));
104 }
105 
106 #if defined(BUSVOODOO)
107 void led_blink(double period, double duty)
108 {
109  if (period<0.0 || period>6.0 || duty<0.0 || duty>1.0) { // input argument out of bounds
110  return; // do nothing
111  }
112  timer_disable_counter(TIM1); // disable timer for PWM before resetting it
113  if (0.0==period) { // no blinking
114  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_PIN)); // set LED pin as normal output
115  if (duty>0.5) { // LED should be on
116  gpio_set(GPIO(LED_PORT), GPIO(LED_PIN)); // switch LED on
117  } else { // LED should be off
118  gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN)); // switch LED off
119  }
120  } else {
121  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO(LED_PIN)); // set LED pin to alternate function for PWM
122  timer_set_counter(TIM1, 0); // reset counter
123  timer_set_period(TIM1, 0xffff*(period/6.0)); // set period
124  timer_set_oc_value(TIM1, TIM_OC1, 0xffff*(period/6.0)*duty); // PWM duty cycle
125  timer_enable_counter(TIM1); // enable timer to start blinking
126  }
127 }
128 
129 void led_blue(void)
130 {
131  timer_disable_counter(TIM1); // disable timer for PWM
132  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'
133  gpio_set(GPIO(LED_PORT), GPIO(LED_PIN));
134 }
135 
136 void led_red(void)
137 {
138  timer_disable_counter(TIM1); // disable timer for PWM
139  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'
140  gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN));
141 }
142 #endif
143 
144 void sleep_us(uint32_t duration)
145 {
146  systick_counter_disable(); // disable SysTick to reconfigure it
147  if (!systick_set_frequency(1000000,rcc_ahb_frequency)) { // set SysTick frequency to microseconds
148  while (true); // unhandled error
149  }
150  systick_clear(); // reset SysTick
151  systick_interrupt_enable(); // enable interrupt to count duration
152  sleep_duration = duration; // save sleep duration for count down
153  systick_counter_enable(); // start counting
154  while (sleep_duration>0) { // wait for count down to complete
155  __WFI(); // go to sleep
156  }
157 }
158 
159 void sleep_ms(uint32_t duration)
160 {
161  systick_counter_disable(); // disable SysTick to reconfigure it
162  if (!systick_set_frequency(1000,rcc_ahb_frequency)) { // set SysTick frequency to milliseconds
163  while (true); // unhandled error
164  }
165  systick_clear(); // reset SysTick
166  systick_interrupt_enable(); // enable interrupt to count duration
167  sleep_duration = duration; // save sleep duration for count down
168  systick_counter_enable(); // start counting
169  while (sleep_duration>0) { // wait for count down to complete
170 // __WFI(); // go to sleep
171  }
172 }
173 
176 {
177  if (sleep_duration>0) {
178  sleep_duration--; // decrement duration
179  }
180  if (0==sleep_duration) { // sleep complete
181  systick_counter_disable(); // stop systick
182  systick_interrupt_disable(); // stop interrupting
183  sleep_duration = 0; // ensure it still is at 0
184  }
185 }
186 
187 char user_input_get(void)
188 {
189  while (!user_input_available) { // wait for user input
190  __WFI(); // go to sleep
191  }
192  volatile char to_return = user_input_buffer[user_input_i]; // get the next available character
193  user_input_i = (user_input_i+1)%LENGTH(user_input_buffer); // update used buffer
194  user_input_used--; // update used buffer
195  user_input_available = (user_input_used!=0); // update available data
196  return to_return;
197 }
198 
199 void user_input_store(char c)
200 {
201  // only save data if there is space in the buffer
202  if (user_input_used>=LENGTH(user_input_buffer)) { // if buffer is full
203  user_input_i = (user_input_i+1)%LENGTH(user_input_buffer); // drop oldest data
204  user_input_used--; // update used buffer information
205  }
206  user_input_buffer[(user_input_i+user_input_used)%LENGTH(user_input_buffer)] = c; // put character in buffer
207  user_input_used++; // update used buffer
208  user_input_available = true; // update available data
209 }
210 
211 void board_setup(void)
212 {
213  // setup LED
214  rcc_periph_clock_enable(RCC_GPIO(LED_PORT)); // enable clock for LED
215 #if defined(BUSVOODOO)
216  // LED is connected to TIM1_CH1, allowing to used the PWM output so to display patterns
217  rcc_periph_clock_enable(RCC_TIM1); // enable clock for timer domain
218  timer_reset(TIM1); // reset timer configuration
219  timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1, TIM_CR1_DIR_UP); // configure timer to up counting mode (center aligned for more precise duty cycle control)
220  timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM1); // use PWM output compare mode
221  timer_enable_oc_output(TIM1, TIM_OC1); // enable output compare output
222  timer_enable_break_main_output(TIM1); // required to enable timer, even when no dead time is used
223  timer_set_prescaler(TIM1, 3296-1); // set prescaler to allow 3/3 seconds PWM output (72MHz/2^16/3296=0.33Hz)
224 #else
225  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'
226 #endif
227  led_off(); // switch off LED per default
228 
229  // setup button
230 #if defined(BUTTON_PORT) && defined(BUTTON_PIN)
231  rcc_periph_clock_enable(RCC_GPIO(BUTTON_PORT)); // enable clock for button
232  gpio_set_mode(GPIO(BUTTON_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUTTON_PIN)); // set button pin to input
233  rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
234  exti_select_source(EXTI(BUTTON_PIN), GPIO(BUTTON_PORT)); // mask external interrupt of this pin only for this port
235 #if defined(BUTTON_PRESSED) && BUTTON_PRESSED
236  gpio_clear(GPIO(BUTTON_PORT), GPIO(BUTTON_PIN)); // pull down to be able to detect button push (go high)
237  exti_set_trigger(EXTI(BUTTON_PIN), EXTI_TRIGGER_RISING); // trigger when button is pressed
238 #else
239  gpio_set(GPIO(BUTTON_PORT), GPIO(BUTTON_PIN)); // pull up to be able to detect button push (go low)
240  exti_set_trigger(EXTI(BUTTON_PIN), EXTI_TRIGGER_FALLING); // trigger when button is pressed
241 #endif
242  exti_enable_request(EXTI(BUTTON_PIN)); // enable external interrupt
243  nvic_enable_irq(NVIC_EXTI_IRQ(BUTTON_PIN)); // enable interrupt
244 #endif
245 
246  // reset user input buffer
247  user_input_available = false;
248  user_input_i = 0;
249  user_input_used = 0;
250 }
251 
252 #if defined(BUTTON_PIN)
253 
254 void EXTI_ISR(BUTTON_PIN)(void)
255 {
256  exti_reset_request(EXTI(BUTTON_PIN)); // reset interrupt
257  button_flag = true; // perform button action
258 }
259 #endif
void board_setup(void)
setup board peripherals
Definition: global.c:211
#define RCC_GPIO(x)
get RCC for GPIO based on GPIO identifier
Definition: global.h:105
void led_off(void)
switch off board LED
Definition: global.c:82
void user_input_store(char c)
store user input
Definition: global.c:199
#define NVIC_EXTI_IRQ(x)
get NVIC IRQ for external interrupt base on external interrupt/pin
Definition: global.h:156
global definitions and methods (API)
#define GPIO(x)
get GPIO based on GPIO identifier
Definition: global.h:103
static volatile uint8_t user_input_buffer[64]
ring buffer for received data
Definition: global.c:39
void sleep_us(uint32_t duration)
go to sleep for some microseconds
Definition: global.c:144
void sys_tick_handler(void)
SysTick interrupt handler.
Definition: global.c:175
char * b2s(uint64_t binary, uint8_t rjust)
get binary representation of a number
Definition: global.c:45
#define EXTI(x)
get external interrupt based on pin identifier
Definition: global.h:154
volatile bool button_flag
flag set when board user button has been pressed/released
Definition: global.c:36
char user_input_get(void)
get user input
Definition: global.c:187
#define EXTI_ISR(x)
get interrupt service routine for timer base on external interrupt/pin
Definition: global.h:169
static volatile uint8_t user_input_i
current position of read received data
Definition: global.c:40
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:159
#define LENGTH(x)
get the length of an array
Definition: global.h:26
void led_toggle(void)
toggle board LED
Definition: global.c:97
static volatile uint32_t sleep_duration
sleep duration count down (in SysTick interrupts)
Definition: global.c:43
volatile bool user_input_available
flag set when user input is available
Definition: global.c:37
void led_on(void)
switch on board LED
Definition: global.c:68
static volatile uint8_t user_input_used
how much data has been received and not red
Definition: global.c:41