Initial commit of code
[barbilliards.git] / src / global.c
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  */
15 /** global definitions and methods (code)
16  *  @file global.c
17  *  @author King Kévin <kingkevin@cuvoodoo.info>
18  *  @date 2016
19  */
20 /* standard libraries */
21 #include <stdint.h> // standard integer types
22 #include <stdlib.h> // general utilities
23
24 /* STM32 (including CM3) libraries */
25 #include <libopencm3/stm32/rcc.h> // real-time control clock library
26 #include <libopencm3/stm32/gpio.h> // general purpose input output library
27 #include <libopencm3/stm32/timer.h> // timer library
28 #include <libopencm3/cm3/nvic.h> // interrupt handler
29 #include <libopencm3/stm32/exti.h> // external interrupt defines
30
31 #include "global.h" // common methods
32 #include "string.h" // memory utilities
33
34 volatile bool button_flag = false;
35
36 char* b2s(uint64_t binary, uint8_t rjust)
37 {
38         static char string[64+1] = {0}; // the string representation to return
39         uint8_t bit = LENGTH(string)-1; // the index of the bit to print
40         string[bit--] = '\0'; // terminate string
41
42         while (binary) {
43                 if (binary & 1) {
44                         string[bit--] = '1';
45                 } else {
46                         string[bit--] = '0';
47                 }
48                 binary >>= 1;
49         }
50
51         while (64-bit-1<rjust && bit>0) {
52                 string[bit--] = '0';
53         }
54
55         return string;
56 }
57
58 /** switch on board LED */
59 void led_on(void)
60 {
61 #if defined(SYSTEM_BOARD) || defined(BLUE_PILL) || defined(CORE_BOARD)
62         gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN));
63 #elif defined(MAPLE_MINI)
64         gpio_set(GPIO(LED_PORT), GPIO(LED_PIN));
65 #endif
66 }
67 /** switch off board LED */
68 void led_off(void)
69 {
70 #if defined(SYSTEM_BOARD) || defined(BLUE_PILL) || defined(CORE_BOARD)
71         gpio_set(GPIO(LED_PORT), GPIO(LED_PIN));
72 #elif defined(MAPLE_MINI)
73         gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN));
74 #endif
75 }
76 /** toggle board LED */
77 void led_toggle(void)
78 {
79         gpio_toggle(GPIO(LED_PORT), GPIO(LED_PIN));
80 }
81
82 void board_setup(void)
83 {
84         // setup LED
85         rcc_periph_clock_enable(RCC_GPIO(LED_PORT)); // enable clock for LED
86         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'
87         led_off(); // switch off LED per default
88
89         // setup button
90 #if defined(BUTTON_PORT) && defined(BUTTON_PIN)
91         rcc_periph_clock_enable(RCC_GPIO(BUTTON_PORT)); // enable clock for button
92         gpio_set_mode(GPIO(BUTTON_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUTTON_PIN)); // set button pin to input
93         rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
94         exti_select_source(EXTI(BUTTON_PIN), GPIO(BUTTON_PORT)); // mask external interrupt of this pin only for this port
95 #if defined(MAPLE_MINI)
96         gpio_clear(GPIO(BUTTON_PORT), GPIO(BUTTON_PIN)); // pull down to be able to detect button push (go high)
97         exti_set_trigger(EXTI(BUTTON_PIN), EXTI_TRIGGER_RISING); // trigger when button is pressed
98 #elif defined(CORE_BOARD)
99         gpio_set(GPIO(BUTTON_PORT), GPIO(BUTTON_PIN)); // pull up to be able to detect button push (go low)
100         exti_set_trigger(EXTI(BUTTON_PIN), EXTI_TRIGGER_FALLING); // trigger when button is pressed
101 #endif
102         exti_enable_request(EXTI(BUTTON_PIN)); // enable external interrupt
103         nvic_enable_irq(NVIC_EXTI_IRQ(BUTTON_PIN)); // enable interrupt
104 #endif
105 }
106
107 #if defined(BUTTON_PIN)
108 /** interrupt service routine called when button is pressed */
109 void EXTI_ISR(BUTTON_PIN)(void)
110 {
111         exti_reset_request(EXTI(BUTTON_PIN)); // reset interrupt
112         button_flag = true; // perform button action
113 }
114 #endif