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.
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.
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/>.
15 /** global definitions and methods (code)
17 * @author King Kévin <kingkevin@cuvoodoo.info>
20 /* standard libraries */
21 #include <stdint.h> // standard integer types
22 #include <stdlib.h> // general utilities
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
31 #include "global.h" // common methods
32 #include "string.h" // memory utilities
34 volatile bool button_flag = false;
36 char* b2s(uint64_t binary, uint8_t rjust)
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
51 while (64-bit-1<rjust && bit>0) {
58 /** switch on board LED */
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));
67 /** switch off board LED */
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));
76 /** toggle board LED */
79 gpio_toggle(GPIO(LED_PORT), GPIO(LED_PIN));
82 void board_setup(void)
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
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
102 exti_enable_request(EXTI(BUTTON_PIN)); // enable external interrupt
103 nvic_enable_irq(NVIC_EXTI_IRQ(BUTTON_PIN)); // enable interrupt
107 #if defined(BUTTON_PIN)
108 /** interrupt service routine called when button is pressed */
109 void EXTI_ISR(BUTTON_PIN)(void)
111 exti_reset_request(EXTI(BUTTON_PIN)); // reset interrupt
112 button_flag = true; // perform button action