stm32f1/global.c

352 lines
12 KiB
C

/** global definitions and methods
* @file
* @author King Kévin <kingkevin@cuvoodoo.info>
* @copyright SPDX-License-Identifier: GPL-3.0-or-later
* @date 2016-2020
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdlib.h> // general utilities
#include <string.h> // memory utilities
/* STM32 (including CM3) libraries */
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
#include <libopencm3/cm3/nvic.h> // interrupt handler
#include <libopencm3/cm3/systick.h> // SysTick library
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/exti.h> // external interrupt defines
#include <libopencm3/stm32/syscfg.h> // system definitions
#include <libopencm3/usb/dwc/otg_fs.h> // USB OTG utilities
#include "global.h" // common methods
volatile bool button_flag = false;
volatile bool user_input_available = false;
static volatile uint8_t user_input_buffer[64] = {0}; /**< ring buffer for received data */
static volatile uint8_t user_input_i = 0; /**< current position of read received data */
static volatile uint8_t user_input_used = 0; /**< how much data has been received and not red */
static volatile uint32_t sleep_duration = 0; /**< sleep duration count down (in SysTick interrupts) */
uint8_t addu8_safe(uint8_t a, uint8_t b)
{
if (a > UINT8_MAX - b) {
return UINT8_MAX;
} else {
return a + b;
}
}
uint16_t addu16_safe(uint16_t a, uint16_t b)
{
if (a > UINT16_MAX - b) {
return UINT16_MAX;
} else {
return a + b;
}
}
uint32_t addu32_safe(uint32_t a, uint32_t b)
{
if (a > UINT32_MAX - b) {
return UINT32_MAX;
} else {
return a + b;
}
}
int8_t adds8_safe(int8_t a, int8_t b)
{
if (b > 0) {
if (a > INT8_MAX - b) {
return INT8_MAX;
} else {
return a + b;
}
} else {
if (a < INT8_MIN + b) {
return INT8_MIN;
} else {
return a + b;
}
}
}
int16_t adds16_safe(int16_t a, int16_t b)
{
if (b > 0) {
if (a > INT16_MAX - b) {
return INT16_MAX;
} else {
return a + b;
}
} else {
if (a < INT16_MIN + b) {
return INT16_MIN;
} else {
return a + b;
}
}
}
int32_t adds32_safe(int32_t a, int32_t b)
{
if (b > 0) {
if (a > INT32_MAX - b) {
return INT32_MAX;
} else {
return a + b;
}
} else {
if (a < INT32_MIN + b) {
return INT32_MIN;
} else {
return a + b;
}
}
}
char* b2s(uint64_t binary, uint8_t rjust)
{
static char string[64 + 1] = {0}; // the string representation to return
uint8_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;
}
/** switch on board LED */
inline void led_on(void)
{
#if defined(LED_PIN)
#if defined(LED_ON) && LED_ON
gpio_set(GPIO_PORT(LED_PIN), GPIO_PIN(LED_PIN));
#else
gpio_clear(GPIO_PORT(LED_PIN), GPIO_PIN(LED_PIN));
#endif // LED_ON
#endif // LED_PIN
}
/** switch off board LED */
inline void led_off(void)
{
#if defined(LED_PIN)
#if defined(LED_ON) && LED_ON
gpio_clear(GPIO_PORT(LED_PIN), GPIO_PIN(LED_PIN));
#else
gpio_set(GPIO_PORT(LED_PIN), GPIO_PIN(LED_PIN));
#endif // LED_ON
#endif // LED_PIN
}
/** toggle board LED */
inline void led_toggle(void)
{
#if defined(LED_PIN)
gpio_toggle(GPIO_PORT(LED_PIN), GPIO_PIN(LED_PIN));
#endif // LED_PIN
}
void sleep_us(uint32_t duration)
{
if (duration <= 4) { // less than the setup time
for (volatile uint32_t nop = 0; nop < 5 * duration; nop++); // busy loop, approximate
return;
}
duration -= 4; // subtract setup time
systick_counter_disable(); // disable SysTick to reconfigure it
if (!systick_set_frequency(1000000, rcc_ahb_frequency)) { // set SysTick frequency to microseconds
while (true); // unhandled error
}
systick_clear(); // reset SysTick
systick_interrupt_enable(); // enable interrupt to count duration
sleep_duration = duration; // save sleep duration for count down
systick_counter_enable(); // start counting
while (sleep_duration > 0) { // wait for count down to complete
__WFI(); // go to sleep
}
}
void sleep_ms(uint32_t duration)
{
if (0 == duration) {
return;
}
systick_counter_disable(); // disable SysTick to reconfigure it
if (!systick_set_frequency(1000, rcc_ahb_frequency)) { // set SysTick frequency to milliseconds
while (true); // unhandled error
}
systick_clear(); // reset SysTick
systick_interrupt_enable(); // enable interrupt to count duration
sleep_duration = duration; // save sleep duration for count down
systick_counter_enable(); // start counting
while (sleep_duration > 0) { // wait for count down to complete
__WFI(); // go to sleep
}
}
/** SysTick interrupt handler */
void sys_tick_handler(void)
{
if (sleep_duration > 0) {
sleep_duration--; // decrement duration
}
if (0 == sleep_duration) { // sleep complete
systick_counter_disable(); // stop systick
systick_interrupt_disable(); // stop interrupting
sleep_duration = 0; // ensure it still is at 0
}
}
char user_input_get(void)
{
while (0 == user_input_used) { // wait for user input to be available (don't trust user_input_available since it user modifiable)
__WFI(); // go to sleep
}
volatile char to_return = user_input_buffer[user_input_i]; // get the next available character
user_input_i = (user_input_i + 1) % LENGTH(user_input_buffer); // update used buffer
user_input_used--; // update used buffer
user_input_available = (user_input_used != 0); // update available data
return to_return;
}
void user_input_store(char c)
{
// only save data if there is space in the buffer
if (user_input_used >= LENGTH(user_input_buffer)) { // if buffer is full
user_input_i = (user_input_i + 1) % LENGTH(user_input_buffer); // drop oldest data
user_input_used--; // update used buffer information
}
user_input_buffer[(user_input_i + user_input_used) % LENGTH(user_input_buffer)] = c; // put character in buffer
user_input_used++; // update used buffer
user_input_available = true; // update available data
}
void board_setup(void)
{
// setup main clock
#if defined(MINIF401)
rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[RCC_CLOCK_3V3_84MHZ]); // the MINIF401 uses an STM32F401 which can go up to 84 MHz, and the board has a 25 MHz crystal
#else
rcc_clock_setup_pll(&rcc_hsi_configs[RCC_CLOCK_3V3_84MHZ]); // use HSI which is present on all boards, and limit to 84MHz (supported by all STM32F4
#endif
#if defined(LED_PIN) && defined(LED_ON)
// setup LED
rcc_periph_clock_enable(GPIO_RCC(LED_PIN)); // enable clock for LED
gpio_mode_setup(GPIO_PORT(LED_PIN), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(LED_PIN)); // set LED pin as output
#if LED_ON // LED is on when sourcing
gpio_set_output_options(GPIO_PORT(LED_PIN), GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, GPIO_PIN(LED_PIN)); // set LED pin output as push-pull
#else // LED is on when sinking
gpio_set_output_options(GPIO_PORT(LED_PIN), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(LED_PIN)); // set LED pin output as open-drain
#endif
led_off(); // switch off LED per default
#endif // LED_PIN
// setup button
#if defined(BUTTON_PIN) && defined(BUTTON_PRESSED)
rcc_periph_clock_enable(GPIO_RCC(BUTTON_PIN)); // enable clock for button
exti_select_source(GPIO_EXTI(BUTTON_PIN), GPIO_PORT(BUTTON_PIN)); // mask external interrupt of this pin only for this port
#if BUTTON_PRESSED // level goes high when pressed
gpio_mode_setup(GPIO_PORT(BUTTON_PIN), GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO_PIN(BUTTON_PIN)); // set GPIO to input and pull down
exti_set_trigger(GPIO_EXTI(BUTTON_PIN), EXTI_TRIGGER_RISING); // trigger when button is pressed
#else // level goes low when pressed
gpio_mode_setup(GPIO_PORT(BUTTON_PIN), GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO_PIN(BUTTON_PIN)); // set GPIO to input and pull up
exti_set_trigger(GPIO_EXTI(BUTTON_PIN), EXTI_TRIGGER_FALLING); // trigger when button is pressed
#endif
exti_enable_request(GPIO_EXTI(BUTTON_PIN)); // enable external interrupt
nvic_enable_irq(GPIO_NVIC_EXTI_IRQ(BUTTON_PIN)); // enable interrupt
#endif
// reset user input buffer
user_input_available = false;
user_input_i = 0;
user_input_used = 0;
}
/** disconnect USB by sending a reset condition */
static void usb_disconnect(void)
{
if (OTG_FS_GUSBCFG & OTG_GUSBCFG_FDMOD) { // USB configured as device
// pull USB D+ low for a short while
OTG_FS_DCTL |= OTG_DCTL_SDIS; // disconnect DP pull-up to simulate a disconnect
// in case there is an external pull-up resistor, pull DP low
// I have no idea why, but once USB is configured, I can't use PA12/DP back as GPIO
gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12); // be sure the D+ pin can be used as GPIO output
gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, GPIO12); // use push-pull output
gpio_clear(GPIOA, GPIO12); // pull D+ low
for (volatile uint32_t i = 0; i < 0x2000; i++); // USB disconnected must be at least 10 ms long, at most 100 ms
}
}
void system_memory(void)
{
usb_disconnect(); // disconnect from USB (if necessary)
// for more details, see https://stm32f4-discovery.net/2017/04/tutorial-jump-system-memory-software-stm32/
// deinit RCC (according to STM32CubeF4 source code)
RCC_CR |= RCC_CR_HSION; // enable high speed internal clock
while (!(RCC_CR & RCC_CR_HSIRDY)); // wait until clock is ready
RCC_CR |= (0x10U << RCC_CR_HSITRIM_SHIFT); // set HSITRIM[4:0] bits to the reset value
RCC_CFGR = 0;// reset CFGR register
while (((RCC_CFGR >> RCC_CFGR_SWS_SHIFT) & RCC_CFGR_SWS_MASK) != RCC_CFGR_SWS_HSI); // wait it till clock switch is ready
RCC_CR &= ~(RCC_CR_HSEON | RCC_CR_HSEBYP | RCC_CR_CSSON); // clear HSEON, HSEBYP and CSSON bits
while (RCC_CR & RCC_CR_HSERDY); // wait till HSE is disabled
RCC_CR &= ~RCC_CR_PLLON; // Clear PLLON bit
while (RCC_CR & RCC_CR_PLLRDY); // wait till PLL is disabled
//RCC_PLLCFGR = 0x24003010; // reset PLLCFGR register to default value (value for STM32F401)
RCC_CIR &= ~(RCC_CIR_LSIRDYIE | RCC_CIR_LSERDYIE | RCC_CIR_HSIRDYIE | RCC_CIR_HSERDYIE | RCC_CIR_PLLRDYIE); // disable all interrupts
RCC_CIR |= (RCC_CIR_LSIRDYC | RCC_CIR_LSERDYC | RCC_CIR_HSIRDYC | RCC_CIR_HSERDYC | RCC_CIR_PLLRDYC | RCC_CIR_CSSC); // clear all interrupt flags
RCC_CR &= ~RCC_CSR_LSION; // clear LSION bit
RCC_CSR |= RCC_CSR_RMVF; // reset all CSR flags
// switch to system memory
RCC_APB2ENR = RCC_APB2ENR_SYSCFGEN; // enable system configure clock (all others are not required)
cm_disable_interrupts(); // disable all interrupts
SYSCFG_MEMRM = 1; // map system memory to 0x0000 0000 (this bypasses the BOOT0 pin)
const uint32_t address = 0x1FFF0000; // system memory address
__asm__ volatile ("MSR msp,%0" : :"r"(*(uint32_t*)address)); // set stack pointer to address provided in the beginning of the bootloader (loaded into a register first)
(*(void(**)(void))((uint32_t)address + 4))(); // start system memory (by jumping to the reset function which address is stored as second entry of the vector table)
// we should not reach this point
}
void dfu_bootloader(void)
{
usb_disconnect(); // disconnect from USB (if necessary)
// set DFU magic to specific RAM location
__dfu_magic[0] = 'D';
__dfu_magic[1] = 'F';
__dfu_magic[2] = 'U';
__dfu_magic[3] = '!';
scb_reset_system(); // reset system (core and peripherals)
while (true); // wait for the reset to happen
}
#if defined(BUTTON_PIN)
/** interrupt service routine called when button is pressed */
void GPIO_EXTI_ISR(BUTTON_PIN)(void)
{
exti_reset_request(GPIO_EXTI(BUTTON_PIN)); // reset interrupt
button_flag = true; // perform button action
}
#endif