Merge pull request #78 from valeros/develop

Update STM32 examples
This commit is contained in:
Ivan Kravets
2015-02-16 15:36:48 +02:00
3 changed files with 72 additions and 68 deletions

View File

@ -1,13 +1,27 @@
#ifdef STM32L1
#include "stm32l1xx.h"
#else
#ifdef STM32F3
#include "stm32f30x.h"
#else
#include "stm32f4xx.h"
#endif
#include "stm32l1xx.h"
#define LEDPORT (GPIOB)
#define LED1 (6)
#define LED2 (7)
#define ENABLE_GPIO_CLOCK (RCC->AHBENR |= RCC_AHBENR_GPIOBEN)
#define GPIOMODER ((GPIO_MODER_MODER7_0|GPIO_MODER_MODER6_0))
#elif STM32F3
#include "stm32f30x.h"
#define LEDPORT (GPIOE)
#define LED1 (8)
#define LED2 (9)
#define ENABLE_GPIO_CLOCK (RCC->AHBENR |= RCC_AHBENR_GPIOEEN)
#define GPIOMODER ((GPIO_MODER_MODER9_0|GPIO_MODER_MODER8_0))
#elif STM32F4
#include "stm32f4xx.h"
#define LEDPORT (GPIOD)
#define LED1 (12)
#define LED2 (13)
#define ENABLE_GPIO_CLOCK (RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN)
#define GPIOMODER ((GPIO_MODER_MODER13_0|GPIO_MODER_MODER12_0))
#endif
void ms_delay(int ms)
{
while (ms-- > 0) {
@ -20,15 +34,12 @@ void ms_delay(int ms)
//Alternates blue and green LEDs quickly
int main(void)
{
RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // enable the clock to GPIOB
GPIOB->MODER |= (1 << 12); // set pin 6 to be general purpose output
GPIOB->MODER |= (1 << 14); // set pin 7 to be general purpose output
GPIOB->ODR |= (1 << 7); // Set the pin 7 high
for (;;) {
ms_delay(100);
GPIOB->ODR ^= (1 << 6); // Toggle the pin
GPIOB->ODR ^= (1 << 7); // Toggle the pin
}
ENABLE_GPIO_CLOCK; // enable the clock to GPIO
LEDPORT->MODER |= GPIOMODER; // set pins to be general purpose output
for (;;) {
ms_delay(500);
LEDPORT->ODR ^= (1<<LED1|1<<LED2); // toggle diodes
}
return 0;
}

View File

@ -21,46 +21,38 @@
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#ifdef STM32L1
#define RCCLEDPORT (RCC_GPIOB)
#define LEDPORT (GPIOB)
#define LEDPIN (GPIO6)
#elif STM32F3
#define RCCLEDPORT (RCC_GPIOE)
#define LEDPORT (GPIOE)
#define LEDPIN (GPIO8)
#elif STM32F4
#define RCCLEDPORT (RCC_GPIOD)
#define LEDPORT (GPIOD)
#define LEDPIN (GPIO12)
#endif
static void gpio_setup(void)
{
/* Enable GPIOB clock. */
/* Manually: */
//RCC_AHBENR |= RCC_AHBENR_GPIOBEN;
/* Enable GPIO clock. */
/* Using API functions: */
rcc_periph_clock_enable(RCC_GPIOB);
/* Set GPIO6 (in GPIO port B) to 'output push-pull'. */
rcc_periph_clock_enable(RCCLEDPORT);
/* Set pin to 'output push-pull'. */
/* Using API functions: */
gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO6);
gpio_mode_setup(LEDPORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LEDPIN);
}
int main(void)
{
int i;
gpio_setup();
/* Blink the LED (PB6) on the board. */
/* Blink the LED on the board. */
while (1) {
/* Manually: */
// GPIOB_BSRR = GPIO6; /* LED off */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");
// GPIOB_BRR = GPIO6; /* LED on */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");
/* Using API functions gpio_set()/gpio_clear(): */
// gpio_set(GPIOB, GPIO6); /* LED off */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");
// gpio_clear(GPIOB, GPIO6); /* LED on */
// for (i = 0; i < 1000000; i++) /* Wait a bit. */
// __asm__("nop");
/* Using API function gpio_toggle(): */
gpio_toggle(GPIOB, GPIO6); /* LED on/off */
gpio_toggle(LEDPORT, LEDPIN); /* LED on/off */
for (i = 0; i < 1000000; i++) { /* Wait a bit. */
__asm__("nop");
}

View File

@ -1,16 +1,21 @@
#ifdef STM32L1
#include <stm32l1xx_gpio.h>
#include <stm32l1xx_rcc.h>
#include <misc.h>
#else
#ifdef STM32F3
#include <stm32f30x_gpio.h>
#include <stm32f30x_rcc.h>
#else
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
#include <misc.h>
#endif
#include <stm32l1xx_gpio.h>
#include <stm32l1xx_rcc.h>
#define LEDPORT (GPIOB)
#define LEDPIN (GPIO_Pin_7)
#define ENABLE_GPIO_CLOCK (RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE))
#elif STM32F3
#include <stm32f30x_gpio.h>
#include <stm32f30x_rcc.h>
#define LEDPORT (GPIOE)
#define LEDPIN (GPIO_Pin_8)
#define ENABLE_GPIO_CLOCK (RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE))
#elif STM32F4
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
#define LEDPORT (GPIOD)
#define LEDPIN (GPIO_Pin_12)
#define ENABLE_GPIO_CLOCK (RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE))
#endif
/* timing is not guaranteed :) */
@ -27,30 +32,26 @@ int main(void)
{
/* gpio init struct */
GPIO_InitTypeDef gpio;
/* reset rcc */
RCC_DeInit();
/* enable clock to GPIOB */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/* use pin 7 */
gpio.GPIO_Pin = GPIO_Pin_7;
/* enable clock GPIO */
ENABLE_GPIO_CLOCK;
/* use LED pin */
gpio.GPIO_Pin = LEDPIN;
/* mode: output */
gpio.GPIO_Mode = GPIO_Mode_OUT;
/* output type: push-pull */
gpio.GPIO_OType = GPIO_OType_PP;
/* apply configuration */
GPIO_Init(GPIOB, &gpio);
GPIO_Init(LEDPORT, &gpio);
/* main program loop */
for (;;) {
/* set led on */
GPIO_SetBits(GPIOB, GPIO_Pin_7);
GPIO_SetBits(LEDPORT, LEDPIN);
/* delay */
simple_delay(100000);
/* clear led */
GPIO_ResetBits(GPIOB, GPIO_Pin_7);
GPIO_ResetBits(LEDPORT, LEDPIN);
/* delay */
simple_delay(100000);
}