2016-08-18 10:45:36 +02:00
|
|
|
/* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/** global definitions and methods (code)
|
|
|
|
* @file global.c
|
|
|
|
* @author King Kévin <kingkevin@cuvoodoo.info>
|
|
|
|
* @date 2016
|
|
|
|
*/
|
|
|
|
/* standard libraries */
|
|
|
|
#include <stdint.h> // standard integer types
|
|
|
|
#include <stdlib.h> // general utilities
|
|
|
|
|
|
|
|
/* STM32 (including CM3) libraries */
|
|
|
|
#include <libopencm3/stm32/rcc.h> // real-time control clock library
|
|
|
|
#include <libopencm3/stm32/gpio.h> // general purpose input output library
|
|
|
|
#include <libopencm3/stm32/timer.h> // timer library
|
|
|
|
#include <libopencm3/cm3/nvic.h> // interrupt handler
|
|
|
|
#include <libopencm3/stm32/exti.h> // external interrupt defines
|
|
|
|
|
|
|
|
#include "global.h" // common methods
|
|
|
|
|
|
|
|
volatile bool button_flag = false;
|
|
|
|
|
2016-08-18 11:25:11 +02:00
|
|
|
const uint32_t GPIO_PORT[] = {GPIOA,GPIOB,GPIOC,GPIOD,GPIOE,GPIOF,GPIOG};
|
2016-08-18 10:45:36 +02:00
|
|
|
const uint32_t RCC_GPIO[] = {RCC_GPIOA,RCC_GPIOB,RCC_GPIOC,RCC_GPIOD,RCC_GPIOE,RCC_GPIOF,RCC_GPIOG};
|
|
|
|
const uint32_t TIM[] = {~0,TIM1,TIM2,TIM3,TIM4,TIM5,TIM6,TIM7,TIM8,TIM9,TIM10,TIM11,TIM12,TIM13,TIM14,TIM15,TIM16,TIM17};
|
|
|
|
const uint32_t RCC_TIM[] = {~0,RCC_TIM1,RCC_TIM2,RCC_TIM3,RCC_TIM4,RCC_TIM5,RCC_TIM6,RCC_TIM7,RCC_TIM8,RCC_TIM9,RCC_TIM10,RCC_TIM11,RCC_TIM12,RCC_TIM13,RCC_TIM14,RCC_TIM15,RCC_TIM16,RCC_TIM17};
|
2016-08-18 11:25:11 +02:00
|
|
|
const uint8_t NVIC_TIM_IRQ[] = {~0,~0,NVIC_TIM2_IRQ,NVIC_TIM3_IRQ,NVIC_TIM4_IRQ,NVIC_TIM5_IRQ,NVIC_TIM6_IRQ,NVIC_TIM7_IRQ,~0,~0,~0,~0,~0,~0,~0,~0,~0,~0};
|
|
|
|
const uint8_t NVIC_EXTI_IRQ[] = {NVIC_EXTI0_IRQ,NVIC_EXTI1_IRQ,NVIC_EXTI2_IRQ,NVIC_EXTI3_IRQ,NVIC_EXTI4_IRQ,NVIC_EXTI9_5_IRQ,NVIC_EXTI9_5_IRQ,NVIC_EXTI9_5_IRQ,NVIC_EXTI9_5_IRQ,NVIC_EXTI9_5_IRQ,NVIC_EXTI15_10_IRQ,NVIC_EXTI15_10_IRQ,NVIC_EXTI15_10_IRQ,NVIC_EXTI15_10_IRQ,NVIC_EXTI15_10_IRQ,NVIC_EXTI15_10_IRQ};
|
2016-08-18 10:45:36 +02:00
|
|
|
|
|
|
|
char* b2s(uint64_t binary, uint8_t rjust)
|
|
|
|
{
|
|
|
|
static char string[64+1] = {0}; // the string representation to return
|
|
|
|
int8_t bit = LENGTH(string)-1; // the index of the bit to print
|
|
|
|
string[bit--] = 0; // terminate string
|
|
|
|
|
|
|
|
while (binary) {
|
|
|
|
if (binary & 1) {
|
|
|
|
string[bit--] = '1';
|
|
|
|
} else {
|
|
|
|
string[bit--] = '0';
|
|
|
|
}
|
|
|
|
binary >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (64-bit-1<rjust && bit>=0) {
|
|
|
|
string[bit--] = '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return &string[bit+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** switch on board LED */
|
|
|
|
void led_on(void)
|
|
|
|
{
|
|
|
|
#if defined(SYSTEM_BOARD) || defined(BLUE_PILL)
|
2016-08-18 11:25:11 +02:00
|
|
|
gpio_clear(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN));
|
2016-08-18 10:45:36 +02:00
|
|
|
#elif defined(MAPLE_MINI)
|
2016-08-18 11:25:11 +02:00
|
|
|
gpio_set(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN));
|
2016-08-18 10:45:36 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/** switch off board LED */
|
|
|
|
void led_off(void)
|
|
|
|
{
|
|
|
|
#if defined(SYSTEM_BOARD) || defined(BLUE_PILL)
|
2016-08-18 11:25:11 +02:00
|
|
|
gpio_set(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN));
|
2016-08-18 10:45:36 +02:00
|
|
|
#elif defined(MAPLE_MINI)
|
2016-08-18 11:25:11 +02:00
|
|
|
gpio_clear(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN));
|
2016-08-18 10:45:36 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/** toggle board LED */
|
|
|
|
void led_toggle(void)
|
|
|
|
{
|
2016-08-18 11:44:47 +02:00
|
|
|
gpio_toggle(GPIO_PORT[LED_PORT], GPIO_PIN(LED_PIN));
|
2016-08-18 10:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void board_setup(void)
|
|
|
|
{
|
|
|
|
// setup LED
|
2016-08-18 11:25:11 +02:00
|
|
|
rcc_periph_clock_enable(RCC_GPIO[LED_PORT]); // enable clock for LED
|
|
|
|
gpio_set_mode(GPIO_PORT[LED_PORT], GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO_PIN(LED_PIN)); // set LED pin to 'output push-pull'
|
2016-08-18 10:45:36 +02:00
|
|
|
led_off(); // switch off LED per default
|
|
|
|
|
|
|
|
// setup button
|
2016-08-18 11:25:11 +02:00
|
|
|
#if defined(BUTTON_PORT) && defined(BUTTON_PIN)
|
|
|
|
rcc_periph_clock_enable(RCC_GPIO[BUTTON_RCC]); // enable clock for button
|
|
|
|
gpio_set_mode(GPIO_PORT[BUTTON_PORT], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_PIN(BUTTON_PIN)); // set button pin to input
|
|
|
|
gpio_clear(GPIO_PORT[BUTTON_PORT], GPIO_PIN(BUTTON_PIN)); // pull down to be able to detect button push (go high)
|
2016-08-18 10:45:36 +02:00
|
|
|
rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
|
2016-08-18 11:25:11 +02:00
|
|
|
exti_select_source(EXTI(BUTTON_PIN), GPIO_PORT[BUTTON_PORT]); // mask external interrupt of this pin only for this port
|
|
|
|
exti_set_trigger(EXTI(BUTTON_PIN), EXTI_TRIGGER_RISING); // trigger when button is pressed
|
|
|
|
exti_enable_request(EXTI(BUTTON_PIN)); // enable external interrupt
|
|
|
|
nvic_enable_irq(NVIC_EXTI_IRQ[BUTTON_PIN]); // enable interrupt
|
2016-08-18 10:45:36 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-08-18 11:25:11 +02:00
|
|
|
#if defined(BUTTON_PIN)
|
2016-08-18 10:45:36 +02:00
|
|
|
/** interrupt service routine called when button is pressed */
|
2016-08-18 11:25:11 +02:00
|
|
|
void EXTI_ISR(BUTTON_PIN)(void)
|
2016-08-18 10:45:36 +02:00
|
|
|
{
|
|
|
|
exti_reset_request(BUTTON_EXTI); // reset interrupt
|
|
|
|
button_flag = true; // perform button action
|
|
|
|
}
|
|
|
|
#endif
|