Correct the F3 examples hardfaults in rare cases, correct style

This commit is contained in:
Frantisek Burian
2014-01-01 13:41:33 +01:00
committed by Piotr Esden-Tempski
parent 3efd9f8675
commit 72c1a29779
7 changed files with 286 additions and 296 deletions

View File

@@ -21,7 +21,7 @@
*/ */
#include <libopencm3/stm32/rcc.h> #include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/f3/adc.h> #include <libopencm3/stm32/adc.h>
#include <libopencm3/stm32/usart.h> #include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/gpio.h> #include <libopencm3/stm32/gpio.h>
@@ -44,7 +44,8 @@
#define LD6 GPIOE, GPIO15 #define LD6 GPIOE, GPIO15
void adc_setup(void) { static void adc_setup(void)
{
//ADC //ADC
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_ADC12EN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_ADC12EN);
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN);
@@ -68,12 +69,13 @@ void adc_setup(void) {
/* Wait for ADC starting up. */ /* Wait for ADC starting up. */
int i; int i;
for (i = 0; i < 800000; i++) /* Wait a bit. */ for (i = 0; i < 800000; i++)
__asm__("nop"); __asm__("nop");
} }
void usart_setup(void) { static void usart_setup(void)
{
/* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN);
@@ -94,13 +96,15 @@ void usart_setup(void) {
usart_enable(USART2); usart_enable(USART2);
} }
void gpio_setup(void) static void gpio_setup(void)
{ {
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN);
gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO8| GPIO9| GPIO10| GPIO11| GPIO12| GPIO13| GPIO14| GPIO15); gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
GPIO8 | GPIO9 | GPIO10 | GPIO11 | GPIO12 | GPIO13 |
GPIO14 | GPIO15);
} }
void my_usart_print_int(uint32_t usart, int16_t value) static void my_usart_print_int(uint32_t usart, int16_t value)
{ {
int8_t i; int8_t i;
int8_t nr_digits = 0; int8_t nr_digits = 0;
@@ -128,12 +132,11 @@ void my_usart_print_int(uint32_t usart, int16_t value)
usart_send_blocking(usart, '\n'); usart_send_blocking(usart, '\n');
} }
void clock_setup(void) { static void clock_setup(void)
//rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_44MHZ]); {
rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]); rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]);
} }
extern unsigned _stack;
int main(void) int main(void)
{ {

View File

@@ -27,12 +27,12 @@
uint16_t exti_line_state; uint16_t exti_line_state;
/* Set STM32 to 64 MHz. */ /* Set STM32 to 64 MHz. */
void clock_setup(void) static void clock_setup(void)
{ {
rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]); rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]);
} }
void gpio_setup(void) static void gpio_setup(void)
{ {
/* Enable GPIOD clock. */ /* Enable GPIOD clock. */
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN);
@@ -42,7 +42,7 @@ void gpio_setup(void)
GPIO_PUPD_NONE, GPIO8 | GPIO9 | GPIO10 | GPIO11); GPIO_PUPD_NONE, GPIO8 | GPIO9 | GPIO10 | GPIO11);
} }
void button_setup(void) static void button_setup(void)
{ {
/* Enable GPIOA clock. */ /* Enable GPIOA clock. */
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN);

View File

@@ -23,7 +23,7 @@
#include <libopencm3/stm32/gpio.h> #include <libopencm3/stm32/gpio.h>
/* Set STM32 to 64 MHz. */ /* Set STM32 to 64 MHz. */
void clock_setup(void) static void clock_setup(void)
{ {
rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]); rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]);
@@ -31,7 +31,7 @@ void clock_setup(void)
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN);
} }
void gpio_setup(void) static void gpio_setup(void)
{ {
/* Set GPIO8-11 (in GPIO port E) to 'output push-pull'. */ /* Set GPIO8-11 (in GPIO port E) to 'output push-pull'. */
gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT,

View File

@@ -43,7 +43,8 @@
#define LD8 GPIOE, GPIO14 #define LD8 GPIOE, GPIO14
#define LD6 GPIOE, GPIO15 #define LD6 GPIOE, GPIO15
void i2c_setup(void) { static void i2c_setup(void)
{
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_I2C1EN); rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_I2C1EN);
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPBEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPBEN);
rcc_set_i2c_clock_hsi(I2C1); rcc_set_i2c_clock_hsi(I2C1);
@@ -56,7 +57,8 @@ void i2c_setup(void) {
//configure ANFOFF DNF[3:0] in CR1 //configure ANFOFF DNF[3:0] in CR1
i2c_enable_analog_filter(I2C1); i2c_enable_analog_filter(I2C1);
i2c_set_digital_filter(I2C1, I2C_CR1_DNF_DISABLED); i2c_set_digital_filter(I2C1, I2C_CR1_DNF_DISABLED);
//Configure PRESC[3:0] SDADEL[3:0] SCLDEL[3:0] SCLH[7:0] SCLL[7:0] in TIMINGR //Configure PRESC[3:0] SDADEL[3:0] SCLDEL[3:0] SCLH[7:0] SCLL[7:0]
// in TIMINGR
i2c_100khz_i2cclk8mhz(I2C1); i2c_100khz_i2cclk8mhz(I2C1);
//configure No-Stretch CR1 (only relevant in slave mode) //configure No-Stretch CR1 (only relevant in slave mode)
i2c_enable_stretching(I2C1); i2c_enable_stretching(I2C1);
@@ -65,7 +67,8 @@ void i2c_setup(void) {
i2c_peripheral_enable(I2C1); i2c_peripheral_enable(I2C1);
} }
void usart_setup(void) { static void usart_setup(void)
{
/* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN);
@@ -86,13 +89,15 @@ void usart_setup(void) {
usart_enable(USART2); usart_enable(USART2);
} }
void gpio_setup(void) static void gpio_setup(void)
{ {
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN);
gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO8| GPIO9| GPIO10| GPIO11| GPIO12| GPIO13| GPIO14| GPIO15); gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
GPIO8 | GPIO9 | GPIO10 | GPIO11 | GPIO12 | GPIO13 |
GPIO14 | GPIO15);
} }
void my_usart_print_int(uint32_t usart, int32_t value) static void my_usart_print_int(uint32_t usart, int32_t value)
{ {
int8_t i; int8_t i;
int8_t nr_digits = 0; int8_t nr_digits = 0;
@@ -120,8 +125,8 @@ void my_usart_print_int(uint32_t usart, int32_t value)
usart_send_blocking(usart, '\n'); usart_send_blocking(usart, '\n');
} }
void clock_setup(void) { static void clock_setup(void)
/*rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_44MHZ]);*/ {
rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]); rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]);
} }

View File

@@ -23,19 +23,12 @@
#include <libopencm3/stm32/rcc.h> #include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h> #include <libopencm3/stm32/gpio.h>
void gpio_setup(void) static void gpio_setup(void)
{ {
/* Enable GPIOE clock. */ /* Enable GPIOE clock. */
/* Manually: */
// RCC_AHB1ENR |= RCC_AHB1ENR_IOPDEN;
/* Using API functions: */
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN);
/* Set GPIO12 (in GPIO port E) to 'output push-pull'. */ /* Set GPIO12 (in GPIO port E) to 'output push-pull'. */
/* Manually: */
//GPIOE_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((8 - 8) * 4) + 2));
//GPIOE_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((8 - 8) * 4));
/* Using API functions: */
gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12); gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12);
} }
@@ -47,22 +40,6 @@ int main(void)
/* Blink the LED (PC8) on the board. */ /* Blink the LED (PC8) on the board. */
while (1) { while (1) {
/* Manually: */
// GPIOD_BSRR = GPIO12; /* LED off */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");
// GPIOD_BRR = GPIO9; /* LED on */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");
/* Using API functions gpio_set()/gpio_clear(): */
//gpio_set(GPIOE, GPIO9); /* LED off */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");
//gpio_clear(GPIOE, GPIO9); /* LED on */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");
/* Using API function gpio_toggle(): */ /* Using API function gpio_toggle(): */
gpio_toggle(GPIOE, GPIO12); /* LED on/off */ gpio_toggle(GPIOE, GPIO12); /* LED on/off */
for (i = 0; i < 2000000; i++) /* Wait a bit. */ for (i = 0; i < 2000000; i++) /* Wait a bit. */

View File

@@ -43,7 +43,8 @@
#define LD8 GPIOE, GPIO14 #define LD8 GPIOE, GPIO14
#define LD6 GPIOE, GPIO15 #define LD6 GPIOE, GPIO15
void spi_setup(void) { static void spi_setup(void)
{
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SPI1EN); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SPI1EN);
/* For spi signal pins */ /* For spi signal pins */
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN);
@@ -56,7 +57,8 @@ void spi_setup(void) {
gpio_set(GPIOE, GPIO3); gpio_set(GPIOE, GPIO3);
/* Setup GPIO pins for AF5 for SPI1 signals. */ /* Setup GPIO pins for AF5 for SPI1 signals. */
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO5 | GPIO6 | GPIO7); gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE,
GPIO5 | GPIO6 | GPIO7);
gpio_set_af(GPIOA, GPIO_AF5, GPIO5 | GPIO6 | GPIO7); gpio_set_af(GPIOA, GPIO_AF5, GPIO5 | GPIO6 | GPIO7);
//spi initialization; //spi initialization;
@@ -76,7 +78,8 @@ void spi_setup(void) {
spi_enable(SPI1); spi_enable(SPI1);
} }
void usart_setup(void) { static void usart_setup(void)
{
/* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN);
@@ -97,13 +100,15 @@ void usart_setup(void) {
usart_enable(USART2); usart_enable(USART2);
} }
void gpio_setup(void) static void gpio_setup(void)
{ {
rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPEEN);
gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO8| GPIO9| GPIO10| GPIO11| GPIO12| GPIO13| GPIO14| GPIO15); gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
GPIO8 | GPIO9 | GPIO10 | GPIO11 | GPIO12 | GPIO13 |
GPIO14 | GPIO15);
} }
void my_usart_print_int(uint32_t usart, int32_t value) static void my_usart_print_int(uint32_t usart, int32_t value)
{ {
int8_t i; int8_t i;
int8_t nr_digits = 0; int8_t nr_digits = 0;
@@ -131,8 +136,8 @@ void my_usart_print_int(uint32_t usart, int32_t value)
usart_send_blocking(usart, '\n'); usart_send_blocking(usart, '\n');
} }
void clock_setup(void) { static void clock_setup(void)
/*rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_44MHZ]);*/ {
rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]); rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]);
} }
@@ -153,9 +158,6 @@ void clock_setup(void) {
#define GYR_OUT_X_L 0x28 #define GYR_OUT_X_L 0x28
#define GYR_OUT_X_H 0x29 #define GYR_OUT_X_H 0x29
// gpio_port_write(GPIOE, (I2C_ISR(i2c) & 0xFF) << 8);
// my_usart_print_int(USART2, (I2C_ISR(i2c) & 0xFF));
int main(void) int main(void)
{ {
uint8_t temp; uint8_t temp;
@@ -168,7 +170,9 @@ int main(void)
gpio_clear(GPIOE, GPIO3); gpio_clear(GPIOE, GPIO3);
spi_send8(SPI1, GYR_CTRL_REG1); spi_send8(SPI1, GYR_CTRL_REG1);
spi_read8(SPI1); spi_read8(SPI1);
spi_send8(SPI1, GYR_CTRL_REG1_PD | GYR_CTRL_REG1_XEN | GYR_CTRL_REG1_YEN | GYR_CTRL_REG1_ZEN | (3 << GYR_CTRL_REG1_BW_SHIFT)); spi_send8(SPI1, GYR_CTRL_REG1_PD | GYR_CTRL_REG1_XEN |
GYR_CTRL_REG1_YEN | GYR_CTRL_REG1_ZEN |
(3 << GYR_CTRL_REG1_BW_SHIFT));
spi_read8(SPI1); spi_read8(SPI1);
gpio_set(GPIOE, GPIO3); gpio_set(GPIOE, GPIO3);

View File

@@ -233,7 +233,8 @@ static void cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue)
} }
void usb_setup(void) { static void usb_setup(void)
{
/* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */ /* Enable clocks for GPIO port A (for GPIO_USART2_TX) and USART2. */
rcc_usb_prescale_1(); rcc_usb_prescale_1();
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN); rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN);
@@ -242,7 +243,6 @@ void usb_setup(void) {
/* Setup GPIO pin GPIO_USART2_TX/GPIO9 on GPIO port A for transmit. */ /* Setup GPIO pin GPIO_USART2_TX/GPIO9 on GPIO port A for transmit. */
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12); gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12);
gpio_set_af(GPIOA, GPIO_AF14, GPIO11| GPIO12); gpio_set_af(GPIOA, GPIO_AF14, GPIO11| GPIO12);
} }
int main(void) int main(void)
@@ -254,7 +254,8 @@ int main(void)
rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_48MHZ]); rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_48MHZ]);
usb_setup(); usb_setup();
usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer)); usbd_dev = usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings,
3, usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(usbd_dev, cdcacm_set_config); usbd_register_set_config_callback(usbd_dev, cdcacm_set_config);
for (i = 0; i < 0x800000; i++) for (i = 0; i < 0x800000; i++)