From 423dc248bc7ba7bab6f561a37e685784afd60c31 Mon Sep 17 00:00:00 2001 From: Valeriy Koval Date: Tue, 3 Feb 2015 14:43:27 +0200 Subject: [PATCH] Reorganize examples with STM32 and new frameworks --- .../stm32/stm32-cmsis-blink/platformio.ini | 24 ++ examples/stm32/stm32-cmsis-blink/src/main.c | 27 +++ .../stm32/stm32-opencm3-blink/platformio.ini | 23 ++ examples/stm32/stm32-opencm3-blink/src/main.c | 70 ++++++ examples/stm32/stm32-spl-blink/platformio.ini | 24 ++ examples/stm32/stm32-spl-blink/src/main.c | 49 +++++ .../titiva-energia-blink/platformio.ini | 23 ++ .../titiva/titiva-energia-blink/src/main.cpp | 19 ++ .../titiva-native-blink/README.rst | 0 .../titiva-native-blink/console-result.png | Bin .../titiva-native-blink/platformio.ini | 1 - .../titiva-native-blink/src/main.c | 0 .../titiva-opencm3-blink/platformio.ini | 23 ++ .../titiva/titiva-opencm3-blink/src/main.c | 206 ++++++++++++++++++ 14 files changed, 488 insertions(+), 1 deletion(-) create mode 100644 examples/stm32/stm32-cmsis-blink/platformio.ini create mode 100644 examples/stm32/stm32-cmsis-blink/src/main.c create mode 100644 examples/stm32/stm32-opencm3-blink/platformio.ini create mode 100644 examples/stm32/stm32-opencm3-blink/src/main.c create mode 100644 examples/stm32/stm32-spl-blink/platformio.ini create mode 100644 examples/stm32/stm32-spl-blink/src/main.c create mode 100644 examples/titiva/titiva-energia-blink/platformio.ini create mode 100644 examples/titiva/titiva-energia-blink/src/main.cpp rename examples/{ => titiva}/titiva-native-blink/README.rst (100%) rename examples/{ => titiva}/titiva-native-blink/console-result.png (100%) rename examples/{ => titiva}/titiva-native-blink/platformio.ini (96%) rename examples/{ => titiva}/titiva-native-blink/src/main.c (100%) create mode 100644 examples/titiva/titiva-opencm3-blink/platformio.ini create mode 100644 examples/titiva/titiva-opencm3-blink/src/main.c diff --git a/examples/stm32/stm32-cmsis-blink/platformio.ini b/examples/stm32/stm32-cmsis-blink/platformio.ini new file mode 100644 index 00000000..c5e7e5f1 --- /dev/null +++ b/examples/stm32/stm32-cmsis-blink/platformio.ini @@ -0,0 +1,24 @@ +# +# Project Configuration File +# +# A detailed documentation with the EXAMPLES is located here: +# http://docs.platformio.org/en/latest/projectconf.html +# + +# A sign `#` at the beginning of the line indicates a comment +# Comment lines are ignored. + +# Simple and base environment +# [env:mybaseenv] +# platform = %INSTALLED_PLATFORM_NAME_HERE% +# framework = +# board = +# +# Automatic targets - enable auto-uploading +# targets = upload + + +[env:stm32] +platform = stm32 +framework = cmsis +board = stm32ldiscovery diff --git a/examples/stm32/stm32-cmsis-blink/src/main.c b/examples/stm32/stm32-cmsis-blink/src/main.c new file mode 100644 index 00000000..91099736 --- /dev/null +++ b/examples/stm32/stm32-cmsis-blink/src/main.c @@ -0,0 +1,27 @@ +#include "stm32l1xx.h" + +void ms_delay(int ms) +{ + while (ms-- > 0) { + volatile int x=500; + while (x-- > 0) + __asm("nop"); + } +} + + +//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 + } +} \ No newline at end of file diff --git a/examples/stm32/stm32-opencm3-blink/platformio.ini b/examples/stm32/stm32-opencm3-blink/platformio.ini new file mode 100644 index 00000000..4453768f --- /dev/null +++ b/examples/stm32/stm32-opencm3-blink/platformio.ini @@ -0,0 +1,23 @@ +# +# Project Configuration File +# +# A detailed documentation with the EXAMPLES is located here: +# http://docs.platformio.org/en/latest/projectconf.html +# + +# A sign `#` at the beginning of the line indicates a comment +# Comment lines are ignored. + +# Simple and base environment +# [env:mybaseenv] +# platform = %INSTALLED_PLATFORM_NAME_HERE% +# framework = +# board = +# +# Automatic targets - enable auto-uploading +# targets = upload + +[env:stm32] +platform = stm32 +board = stm32ldiscovery +framework = opencm3 diff --git a/examples/stm32/stm32-opencm3-blink/src/main.c b/examples/stm32/stm32-opencm3-blink/src/main.c new file mode 100644 index 00000000..c6a832c4 --- /dev/null +++ b/examples/stm32/stm32-opencm3-blink/src/main.c @@ -0,0 +1,70 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * Copyright (C) 2012 Karl Palsson + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include + +static void gpio_setup(void) +{ + /* Enable GPIOB clock. */ + /* Manually: */ + //RCC_AHBENR |= RCC_AHBENR_GPIOBEN; + /* Using API functions: */ + rcc_periph_clock_enable(RCC_GPIOB); + + /* Set GPIO6 (in GPIO port B) to 'output push-pull'. */ + /* Using API functions: */ + gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO6); +} + +int main(void) +{ + int i; + + gpio_setup(); + + /* Blink the LED (PB6) 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 */ + for (i = 0; i < 1000000; i++) { /* Wait a bit. */ + __asm__("nop"); + } + } + + return 0; +} diff --git a/examples/stm32/stm32-spl-blink/platformio.ini b/examples/stm32/stm32-spl-blink/platformio.ini new file mode 100644 index 00000000..ef5e98fe --- /dev/null +++ b/examples/stm32/stm32-spl-blink/platformio.ini @@ -0,0 +1,24 @@ +# +# Project Configuration File +# +# A detailed documentation with the EXAMPLES is located here: +# http://docs.platformio.org/en/latest/projectconf.html +# + +# A sign `#` at the beginning of the line indicates a comment +# Comment lines are ignored. + +# Simple and base environment +# [env:mybaseenv] +# platform = %INSTALLED_PLATFORM_NAME_HERE% +# framework = +# board = +# +# Automatic targets - enable auto-uploading +# targets = upload + + +[env:stm32] +platform = stm32 +framework = cmsis,spl +board = stm32ldiscovery diff --git a/examples/stm32/stm32-spl-blink/src/main.c b/examples/stm32/stm32-spl-blink/src/main.c new file mode 100644 index 00000000..e73ae4f2 --- /dev/null +++ b/examples/stm32/stm32-spl-blink/src/main.c @@ -0,0 +1,49 @@ +#include +#include +#include + +/* timing is not guaranteed :) */ +void simple_delay(uint32_t us) +{ + /* simple delay loop */ + while (us--) { + asm volatile ("nop"); + } +} + +/* system entry point */ +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; + /* mode: output */ + gpio.GPIO_Mode = GPIO_Mode_OUT; + /* output type: push-pull */ + gpio.GPIO_OType = GPIO_OType_PP; + /* apply configuration */ + GPIO_Init(GPIOB, &gpio); + + /* main program loop */ + for (;;) { + /* set led on */ + GPIO_SetBits(GPIOB, GPIO_Pin_7); + /* delay */ + simple_delay(100000); + /* clear led */ + GPIO_ResetBits(GPIOB, GPIO_Pin_7); + /* delay */ + simple_delay(100000); + } + + /* never reached */ + return 0; +} \ No newline at end of file diff --git a/examples/titiva/titiva-energia-blink/platformio.ini b/examples/titiva/titiva-energia-blink/platformio.ini new file mode 100644 index 00000000..684d50e2 --- /dev/null +++ b/examples/titiva/titiva-energia-blink/platformio.ini @@ -0,0 +1,23 @@ +# +# Project Configuration File +# +# A detailed documentation with the EXAMPLES is located here: +# http://docs.platformio.org/en/latest/projectconf.html +# + +# A sign `#` at the beginning of the line indicates a comment +# Comment lines are ignored. + +# Simple and base environment +# [env:mybaseenv] +# platform = %INSTALLED_PLATFORM_NAME_HERE% +# framework = +# board = +# +# Automatic targets - enable auto-uploading +# targets = upload + +[env:launchpad_tm4c1230c3pm] +platform = titiva +framework = energia +board = lptm4c1230c3pm diff --git a/examples/titiva/titiva-energia-blink/src/main.cpp b/examples/titiva/titiva-energia-blink/src/main.cpp new file mode 100644 index 00000000..8c086a4b --- /dev/null +++ b/examples/titiva/titiva-energia-blink/src/main.cpp @@ -0,0 +1,19 @@ +/** + * Copyright (C) Ivan Kravets + * See LICENSE for details. + */ + +#include "Energia.h" + +void setup() +{ + pinMode(GREEN_LED, OUTPUT); // set pin as output +} + +void loop() +{ + digitalWrite(GREEN_LED, HIGH); // set the LED on + delay(1000); // wait for a second + digitalWrite(GREEN_LED, LOW); // set the LED off + delay(1000); // wait for a second +} diff --git a/examples/titiva-native-blink/README.rst b/examples/titiva/titiva-native-blink/README.rst similarity index 100% rename from examples/titiva-native-blink/README.rst rename to examples/titiva/titiva-native-blink/README.rst diff --git a/examples/titiva-native-blink/console-result.png b/examples/titiva/titiva-native-blink/console-result.png similarity index 100% rename from examples/titiva-native-blink/console-result.png rename to examples/titiva/titiva-native-blink/console-result.png diff --git a/examples/titiva-native-blink/platformio.ini b/examples/titiva/titiva-native-blink/platformio.ini similarity index 96% rename from examples/titiva-native-blink/platformio.ini rename to examples/titiva/titiva-native-blink/platformio.ini index e4c123dd..9c9493dd 100644 --- a/examples/titiva-native-blink/platformio.ini +++ b/examples/titiva/titiva-native-blink/platformio.ini @@ -21,4 +21,3 @@ platform = titiva framework = energia board = lplm4f120h5qr -targets = upload diff --git a/examples/titiva-native-blink/src/main.c b/examples/titiva/titiva-native-blink/src/main.c similarity index 100% rename from examples/titiva-native-blink/src/main.c rename to examples/titiva/titiva-native-blink/src/main.c diff --git a/examples/titiva/titiva-opencm3-blink/platformio.ini b/examples/titiva/titiva-opencm3-blink/platformio.ini new file mode 100644 index 00000000..64578480 --- /dev/null +++ b/examples/titiva/titiva-opencm3-blink/platformio.ini @@ -0,0 +1,23 @@ +# +# Project Configuration File +# +# A detailed documentation with the EXAMPLES is located here: +# http://docs.platformio.org/en/latest/projectconf.html +# + +# A sign `#` at the beginning of the line indicates a comment +# Comment lines are ignored. + +# Simple and base environment +# [env:mybaseenv] +# platform = %INSTALLED_PLATFORM_NAME_HERE% +# framework = +# board = +# +# Automatic targets - enable auto-uploading +# targets = upload + +[env:launchpad_lm4f120] +platform = titiva +framework = opencm3 +board = lplm4f120h5qr diff --git a/examples/titiva/titiva-opencm3-blink/src/main.c b/examples/titiva/titiva-opencm3-blink/src/main.c new file mode 100644 index 00000000..dfcae8a9 --- /dev/null +++ b/examples/titiva/titiva-opencm3-blink/src/main.c @@ -0,0 +1,206 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2011 Gareth McMullin + * Copyright (C) 2012-2013 Alexandru Gagniuc + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +/** + * \addtogroup Examples + * + * Flashes the Red, Green and Blue diodes on the board, in order. + * + * RED controlled by PF1 + * Green controlled by PF3 + * Blue controlled by PF2 + */ +#include +#include +#include +#include +#include + +#include +#include + +/* This is how the RGB LED is connected on the stellaris launchpad */ +#define RGB_PORT GPIOF +enum { + LED_R = GPIO1, + LED_G = GPIO3, + LED_B = GPIO2, +}; + +/* This is how the user switches are connected to GPIOF */ +enum { + USR_SW1 = GPIO4, + USR_SW2 = GPIO0, +}; + +/* The divisors we loop through when the user presses SW2 */ +enum { + PLL_DIV_80MHZ = 5, + PLL_DIV_57MHZ = 7, + PLL_DIV_40MHZ = 10, + PLL_DIV_20MHZ = 20, + PLL_DIV_16MHZ = 25, +}; + +static const uint8_t plldiv[] = { + PLL_DIV_80MHZ, + PLL_DIV_57MHZ, + PLL_DIV_40MHZ, + PLL_DIV_20MHZ, + PLL_DIV_16MHZ, + 0 +}; +/* The PLL divisor we are currently on */ +static size_t ipll = 0; +/* Are we bypassing the PLL, or not? */ +static bool bypass = false; + +/* + * Clock setup: + * Take the main crystal oscillator at 16MHz, run it through the PLL, and divide + * the 400MHz PLL clock to get a system clock of 80MHz. + */ +static void clock_setup(void) +{ + rcc_sysclk_config(OSCSRC_MOSC, XTAL_16M, PLL_DIV_80MHZ); +} + +/* + * GPIO setup: + * Enable the pins driving the RGB LED as outputs. + */ +static void gpio_setup(void) +{ + /* + * Configure GPIOF + * This port is used to control the RGB LED + */ + periph_clock_enable(RCC_GPIOF); + const uint32_t outpins = (LED_R | LED_G | LED_B); + + gpio_mode_setup(RGB_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, outpins); + gpio_set_output_config(RGB_PORT, GPIO_OTYPE_PP, GPIO_DRIVE_2MA, outpins); + + /* + * Now take care of our buttons + */ + const uint32_t btnpins = USR_SW1 | USR_SW2; + + /* + * PF0 is a locked by default. We need to unlock it before we can + * re-purpose it as a GPIO pin. + */ + gpio_unlock_commit(GPIOF, USR_SW2); + /* Configure pins as inputs, with pull-up. */ + gpio_mode_setup(GPIOF, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, btnpins); +} + +/* + * IRQ setup: + * Trigger an interrupt whenever a button is depressed. + */ +static void irq_setup(void) +{ + const uint32_t btnpins = USR_SW1 | USR_SW2; + /* Trigger interrupt on rising-edge (when button is depressed) */ + gpio_configure_trigger(GPIOF, GPIO_TRIG_EDGE_RISE, btnpins); + /* Finally, Enable interrupt */ + gpio_enable_interrupts(GPIOF, btnpins); + /* Enable the interrupt in the NVIC as well */ + nvic_enable_irq(NVIC_GPIOF_IRQ); +} + +#define FLASH_DELAY 800000 +static void delay(void) +{ + int i; + for (i = 0; i < FLASH_DELAY; i++) /* Wait a bit. */ + __asm__("nop"); +} + +int main(void) +{ + gpio_enable_ahb_aperture(); + clock_setup(); + gpio_setup(); + irq_setup(); + + /* Blink each color of the RGB LED in order. */ + while (1) { + /* + * Flash the Red diode + */ + gpio_set(RGB_PORT, LED_R); + delay(); /* Wait a bit. */ + gpio_clear(RGB_PORT, LED_R); + delay(); /* Wait a bit. */ + + /* + * Flash the Green diode + */ + gpio_set(RGB_PORT, LED_G); + delay(); /* Wait a bit. */ + gpio_clear(RGB_PORT, LED_G); + delay(); /* Wait a bit. */ + + /* + * Flash the Blue diode + */ + gpio_set(RGB_PORT, LED_B); + delay(); /* Wait a bit. */ + gpio_clear(RGB_PORT, LED_B); + delay(); /* Wait a bit. */ + } + + return 0; +} + +void gpiof_isr(void) +{ + if (gpio_is_interrupt_source(GPIOF, USR_SW1)) { + /* SW1 was just depressed */ + bypass = !bypass; + if (bypass) { + rcc_pll_bypass_enable(); + /* + * The divisor is still applied to the raw clock. + * Disable the divisor, or we'll divide the raw clock. + */ + SYSCTL_RCC &= ~SYSCTL_RCC_USESYSDIV; + } + else + { + rcc_change_pll_divisor(plldiv[ipll]); + } + /* Clear interrupt source */ + gpio_clear_interrupt_flag(GPIOF, USR_SW1); + } + + if (gpio_is_interrupt_source(GPIOF, USR_SW2)) { + /* SW2 was just depressed */ + if (!bypass) { + if (plldiv[++ipll] == 0) + ipll = 0; + rcc_change_pll_divisor(plldiv[ipll]); + } + /* Clear interrupt source */ + gpio_clear_interrupt_flag(GPIOF, USR_SW2); + } +} \ No newline at end of file