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/exti.h> // external interrupt defines
32 
33 #include "global.h" // common methods
34 
35 volatile bool button_flag = false;
36 volatile bool user_input_available = false;
37 
38 static volatile uint8_t user_input_buffer[64] = {0};
39 static volatile uint8_t user_input_i = 0;
40 static volatile uint8_t user_input_used = 0;
42 static volatile uint32_t sleep_duration = 0;
44 char* b2s(uint64_t binary, uint8_t rjust)
45 {
46  static char string[64+1] = {0}; // the string representation to return
47  uint8_t bit = LENGTH(string)-1; // the index of the bit to print
48  string[bit--] = '\0'; // terminate string
49 
50  while (binary) {
51  if (binary & 1) {
52  string[bit--] = '1';
53  } else {
54  string[bit--] = '0';
55  }
56  binary >>= 1;
57  }
58 
59  while (64-bit-1<rjust && bit>0) {
60  string[bit--] = '0';
61  }
62 
63  return string;
64 }
65 
67 void led_on(void)
68 {
69 #if defined(BUSVOODOO)
70  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_PIN)); // set LED pin push-pull
71 #endif
72 #if defined(LED_ON) && LED_ON
73  gpio_set(GPIO(LED_PORT), GPIO(LED_PIN));
74 #else
75  gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN));
76 #endif
77 }
78 
80 void led_off(void)
81 {
82 #if defined(BUSVOODOO)
83  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(LED_PIN)); // set LED pin to floating to disable LEDs
84 #else
85 #if defined(LED_ON) && LED_ON
86  gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN));
87 #else
88  gpio_set(GPIO(LED_PORT), GPIO(LED_PIN));
89 #endif
90 #endif
91 }
92 
94 void led_toggle(void)
95 {
96 #if defined(BUSVOODOO)
97  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_PIN)); // set LED pin to push-pull
98 #endif
99  gpio_toggle(GPIO(LED_PORT), GPIO(LED_PIN));
100 }
101 
102 void sleep_us(uint32_t duration)
103 {
104  systick_counter_disable(); // disable SysTick to reconfigure it
105  if (!systick_set_frequency(1000000,rcc_ahb_frequency)) { // set SysTick frequency to microseconds
106  while (true); // unhandled error
107  }
108  systick_clear(); // reset SysTick
109  systick_interrupt_enable(); // enable interrupt to count duration
110  sleep_duration = duration; // save sleep duration for count down
111  systick_counter_enable(); // start counting
112  while (sleep_duration>0) { // wait for count down to complete
113  __WFI(); // go to sleep
114  }
115 }
116 
117 void sleep_ms(uint32_t duration)
118 {
119  systick_counter_disable(); // disable SysTick to reconfigure it
120  if (!systick_set_frequency(1000,rcc_ahb_frequency)) { // set SysTick frequency to milliseconds
121  while (true); // unhandled error
122  }
123  systick_clear(); // reset SysTick
124  systick_interrupt_enable(); // enable interrupt to count duration
125  sleep_duration = duration; // save sleep duration for count down
126  systick_counter_enable(); // start counting
127  while (sleep_duration>0) { // wait for count down to complete
128  __WFI(); // go to sleep
129  }
130 }
131 
134 {
135  if (sleep_duration>0) {
136  sleep_duration--; // decrement duration
137  }
138  if (0==sleep_duration) { // sleep complete
139  systick_counter_disable(); // stop systick
140  systick_interrupt_disable(); // stop interrupting
141  sleep_duration = 0; // ensure it still is at 0
142  }
143 }
144 
145 char user_input_get(void)
146 {
147  while (!user_input_available) { // wait for user input
148  __WFI(); // go to sleep
149  }
150  volatile char to_return = user_input_buffer[user_input_i]; // get the next available character
151  user_input_i = (user_input_i+1)%LENGTH(user_input_buffer); // update used buffer
152  user_input_used--; // update used buffer
153  user_input_available = (user_input_used!=0); // update available data
154  return to_return;
155 }
156 
157 void user_input_store(char c)
158 {
159  // only save data if there is space in the buffer
160  if (user_input_used>=LENGTH(user_input_buffer)) { // if buffer is full
161  user_input_i = (user_input_i+1)%LENGTH(user_input_buffer); // drop oldest data
162  user_input_used--; // update used buffer information
163  }
164  user_input_buffer[(user_input_i+user_input_used)%LENGTH(user_input_buffer)] = c; // put character in buffer
165  user_input_used++; // update used buffer
166  user_input_available = true; // update available data
167 }
168 
169 void board_setup(void)
170 {
171  // setup LED
172  rcc_periph_clock_enable(RCC_GPIO(LED_PORT)); // enable clock for LED
173 #if defined(BUSVOODOO)
174  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(LED_PIN)); // set LED pin to floating to disable LEDs
175 #else
176  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 do drive LED
177 #endif
178  led_off(); // switch off LED per default
179 
180  // setup button
181 #if defined(BUTTON_PORT) && defined(BUTTON_PIN)
182  rcc_periph_clock_enable(RCC_GPIO(BUTTON_PORT)); // enable clock for button
183  gpio_set_mode(GPIO(BUTTON_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUTTON_PIN)); // set button pin to input
184  rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
185  exti_select_source(EXTI(BUTTON_PIN), GPIO(BUTTON_PORT)); // mask external interrupt of this pin only for this port
186 #if defined(BUTTON_PRESSED) && BUTTON_PRESSED
187  gpio_clear(GPIO(BUTTON_PORT), GPIO(BUTTON_PIN)); // pull down to be able to detect button push (go high)
188  exti_set_trigger(EXTI(BUTTON_PIN), EXTI_TRIGGER_RISING); // trigger when button is pressed
189 #else
190  gpio_set(GPIO(BUTTON_PORT), GPIO(BUTTON_PIN)); // pull up to be able to detect button push (go low)
191  exti_set_trigger(EXTI(BUTTON_PIN), EXTI_TRIGGER_FALLING); // trigger when button is pressed
192 #endif
193  exti_enable_request(EXTI(BUTTON_PIN)); // enable external interrupt
194  nvic_enable_irq(NVIC_EXTI_IRQ(BUTTON_PIN)); // enable interrupt
195 #endif
196 
197  // reset user input buffer
198  user_input_available = false;
199  user_input_i = 0;
200  user_input_used = 0;
201 }
202 
203 #if defined(BUTTON_PIN)
204 
205 void EXTI_ISR(BUTTON_PIN)(void)
206 {
207  exti_reset_request(EXTI(BUTTON_PIN)); // reset interrupt
208  button_flag = true; // perform button action
209 }
210 #endif
void board_setup(void)
setup board peripherals
Definition: global.c:169
#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:80
void user_input_store(char c)
store user input
Definition: global.c:157
#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:38
void sleep_us(uint32_t duration)
go to sleep for some microseconds
Definition: global.c:102
void sys_tick_handler(void)
SysTick interrupt handler.
Definition: global.c:133
char * b2s(uint64_t binary, uint8_t rjust)
get binary representation of a number
Definition: global.c:44
#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:35
char user_input_get(void)
get user input
Definition: global.c:145
#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:39
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:117
#define LENGTH(x)
get the length of an array
Definition: global.h:26
void led_toggle(void)
toggle board LED
Definition: global.c:94
static volatile uint32_t sleep_duration
sleep duration count down (in SysTick interrupts)
Definition: global.c:42
volatile bool user_input_available
flag set when user input is available
Definition: global.c:36
void led_on(void)
switch on board LED
Definition: global.c:67
static volatile uint8_t user_input_used
how much data has been received and not red
Definition: global.c:40