115 lines
4.1 KiB
C
115 lines
4.1 KiB
C
|
/* 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;
|
||
|
|
||
|
const uint32_t GPIO[] = {GPIOA,GPIOB,GPIOC,GPIOD,GPIOE,GPIOF,GPIOG};
|
||
|
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};
|
||
|
const uint32_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};
|
||
|
|
||
|
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)
|
||
|
gpio_clear(LED_PORT, LED_PIN);
|
||
|
#elif defined(MAPLE_MINI)
|
||
|
gpio_set(LED_PORT, LED_PIN);
|
||
|
#endif
|
||
|
}
|
||
|
/** switch off board LED */
|
||
|
void led_off(void)
|
||
|
{
|
||
|
#if defined(SYSTEM_BOARD) || defined(BLUE_PILL)
|
||
|
gpio_set(LED_PORT, LED_PIN);
|
||
|
#elif defined(MAPLE_MINI)
|
||
|
gpio_clear(LED_PORT, LED_PIN);
|
||
|
#endif
|
||
|
}
|
||
|
/** toggle board LED */
|
||
|
void led_toggle(void)
|
||
|
{
|
||
|
gpio_toggle(LED_PORT, LED_PIN);
|
||
|
}
|
||
|
|
||
|
void board_setup(void)
|
||
|
{
|
||
|
// setup LED
|
||
|
rcc_periph_clock_enable(LED_RCC); // enable clock for LED
|
||
|
gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); // set LED pin to 'output push-pull'
|
||
|
led_off(); // switch off LED per default
|
||
|
|
||
|
// setup button
|
||
|
#if defined(BUTTON_RCC) && defined(BUTTON_PORT) && defined(BUTTON_PIN) && defined(BUTTON_EXTI) && defined(BUTTON_IRQ)
|
||
|
rcc_periph_clock_enable(BUTTON_RCC); // enable clock for button
|
||
|
gpio_set_mode(BUTTON_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, BUTTON_PIN); // set button pin to input
|
||
|
gpio_clear(BUTTON_PORT, BUTTON_PIN); // pull down to be able to detect button push (go high)
|
||
|
rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
|
||
|
exti_select_source(BUTTON_EXTI, BUTTON_PORT); // mask external interrupt of this pin only for this port
|
||
|
exti_set_trigger(BUTTON_EXTI, EXTI_TRIGGER_RISING); // trigger when button is pressed
|
||
|
exti_enable_request(BUTTON_EXTI); // enable external interrupt
|
||
|
nvic_enable_irq(BUTTON_IRQ); // enable interrupt
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
#if defined(BUTTON_ISR) && defined(BUTTON_EXTI)
|
||
|
/** interrupt service routine called when button is pressed */
|
||
|
void BUTTON_ISR(void)
|
||
|
{
|
||
|
exti_reset_request(BUTTON_EXTI); // reset interrupt
|
||
|
button_flag = true; // perform button action
|
||
|
}
|
||
|
#endif
|