From 295a2bd2369007cd2c70df5abb55c822824f21c5 Mon Sep 17 00:00:00 2001 From: Roger Wolff Date: Thu, 27 Jun 2013 10:02:41 +0200 Subject: [PATCH 1/2] added some better comments to ADC example --- Makefile | 2 +- examples/stm32/f3/stm32f3-discovery/adc/README | 6 +++++- examples/stm32/f3/stm32f3-discovery/adc/adc.c | 8 ++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index af3ace9b..ce65c8e1 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ PREFIX ?= arm-none-eabi #PREFIX ?= arm-elf -TARGETS = stm32/f1 stm32/f2 stm32/f4 stm32/l1 lpc13xx lpc17xx lpc43xx lm3s lm4f efm32/efm32tg efm32/efm32g efm32/efm32lg efm32/efm32gg +TARGETS = stm32/f1 stm32/f2 stm32/f3 stm32/f4 stm32/l1 lpc13xx lpc17xx lpc43xx lm3s lm4f efm32/efm32tg efm32/efm32g efm32/efm32lg efm32/efm32gg # Be silent per default, but 'make V=1' will show all compiler calls. ifneq ($(V),1) diff --git a/examples/stm32/f3/stm32f3-discovery/adc/README b/examples/stm32/f3/stm32f3-discovery/adc/README index 224f51f4..6c2c1f79 100644 --- a/examples/stm32/f3/stm32f3-discovery/adc/README +++ b/examples/stm32/f3/stm32f3-discovery/adc/README @@ -4,6 +4,10 @@ README This is the smallest-possible example program using libopencm3. -It's intended for the ST STM32F3DISCOVERY eval board. It should read from the ADC1_IN1 (PA0) pin its voltage and print it in the LEDs. +It's intended for the ST STM32F3DISCOVERY eval board. It should read from +the ADC1_IN1 (PA0) pin its voltage and print it in decimal on UART3 +(baud rate?) and show it in the LEDs (in binary). +The code can easily be changed to show the internal temperature +sensor instead. diff --git a/examples/stm32/f3/stm32f3-discovery/adc/adc.c b/examples/stm32/f3/stm32f3-discovery/adc/adc.c index 5de126ef..5dd66199 100644 --- a/examples/stm32/f3/stm32f3-discovery/adc/adc.c +++ b/examples/stm32/f3/stm32f3-discovery/adc/adc.c @@ -59,9 +59,13 @@ void adc_setup(void) { /* We want to read the temperature sensor, so we have to enable it. */ adc_enable_temperature_sensor(); adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR1_SMP_61DOT5CYC); - uint8_t channel_array[16]; - channel_array[0]=16; // Vts (Internal temperature sensor + + uint8_t channel_array[16]; // full array, but this example only uses one entry. + + // select the channel you want to sample here: + //channel_array[0]=16; // Vts (Internal temperature sensor channel_array[0]=1; //ADC1_IN1 (PA0) + adc_set_regular_sequence(ADC1, 1, channel_array); adc_set_resolution(ADC1, ADC_CFGR_RES_12_BIT); adc_power_on(ADC1); From 749324784af7edb2b854c0ef1b8a7d82603b2f40 Mon Sep 17 00:00:00 2001 From: Roger Wolff Date: Sun, 30 Jun 2013 13:26:54 +0200 Subject: [PATCH 2/2] added uart example. --- .../stm32/f3/stm32f3-discovery/usart/Makefile | 25 ++++ .../stm32/f3/stm32f3-discovery/usart/README | 29 +++++ .../stm32/f3/stm32f3-discovery/usart/usart.c | 112 ++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 examples/stm32/f3/stm32f3-discovery/usart/Makefile create mode 100644 examples/stm32/f3/stm32f3-discovery/usart/README create mode 100644 examples/stm32/f3/stm32f3-discovery/usart/usart.c diff --git a/examples/stm32/f3/stm32f3-discovery/usart/Makefile b/examples/stm32/f3/stm32f3-discovery/usart/Makefile new file mode 100644 index 00000000..4783889b --- /dev/null +++ b/examples/stm32/f3/stm32f3-discovery/usart/Makefile @@ -0,0 +1,25 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## 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 . +## + +BINARY = usart + +LDSCRIPT = ../stm32f3-discovery.ld + +include ../../Makefile.include + diff --git a/examples/stm32/f3/stm32f3-discovery/usart/README b/examples/stm32/f3/stm32f3-discovery/usart/README new file mode 100644 index 00000000..31544bd5 --- /dev/null +++ b/examples/stm32/f3/stm32f3-discovery/usart/README @@ -0,0 +1,29 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program sends some characters on USART2 on the +ST STM32F3DISCOVERY eval board. + +The terminal settings for the receiving device/PC are 115200 8n1. + +The sending is done in a blocking way in the code. + +The program also recieves characters and prints them with <> around +the recieved characters. + +If an overrun happens "R" is printed, and the flag is cleard to reenable +reception. + +If a framing error happens "F" is printed, and the flag is cleard to reenable +reception. + +If a parity error happens "R" is printed, and the flag is cleard to reenable +reception. (this cannot currently happen as parity is disabled). + + +TODO: Figure out why +- the baud rate passed to set_baudrate is 1/4 of what is wanted. +- the RDR is called RDR here in the code and in my datasheet, but the +library code seems to want to use "DR" which I can't find anywhere. + diff --git a/examples/stm32/f3/stm32f3-discovery/usart/usart.c b/examples/stm32/f3/stm32f3-discovery/usart/usart.c new file mode 100644 index 00000000..f66596b5 --- /dev/null +++ b/examples/stm32/f3/stm32f3-discovery/usart/usart.c @@ -0,0 +1,112 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * Copyright (C) 2011 Stephen Caudle + * + * 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 +#include +#include + + +static void clock_setup(void) +{ + /* Enable GPIOD clock for LED & USARTs. */ + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); + rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); + + /* Enable clocks for USART2. */ + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN); +} + +static void usart_setup(void) +{ + /* Setup USART2 parameters. */ + + // XXX TODO: figure out why the number passed is /4 for STM32F3. + usart_set_baudrate(USART2, 115200/4); + usart_set_databits(USART2, 8); + usart_set_stopbits(USART2, USART_STOPBITS_1); + usart_set_mode(USART2, USART_MODE_TX | USART_MODE_RX); + usart_set_parity(USART2, USART_PARITY_NONE); + usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); + + USART_ICR (USART2) = USART_ICR_ORECF | USART_ICR_FECF; + + /* Finally enable the USART. */ + usart_enable(USART2); +} + +static void gpio_setup(void) +{ + /* Setup GPIO pin GPIO12 on GPIO port D for LED. */ + gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12); + + /* Setup GPIO pins for USART2 transmit. */ + gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2|GPIO3); + + /* Setup USART2 RX/TX pins as alternate function. */ + gpio_set_af(GPIOA, GPIO_AF7, GPIO2 | GPIO3); +} + + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PE12) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOE, GPIO12); /* LED on/off */ + usart_send_blocking(USART2, c + '0'); /* USART2: Send byte. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + + // The original finished the line at 80 chars. 39 gives + // a nice twirl effect and allows for reception characters + // to be printed inbetween. + if ((j++ % 39) == 0) { /* Newline after line full. */ + usart_send_blocking(USART2, '\r'); + usart_send_blocking(USART2, '\n'); + } + for (i = 0; i < 300000; i++) /* Wait a bit. */ + __asm__("NOP"); + + if ((USART_ISR(USART2) & USART_ISR_RXNE) != 0) { + usart_send_blocking (USART2, '<'); + usart_send_blocking (USART2, USART_RDR(USART2)); + usart_send_blocking (USART2, '>'); + } + if ((USART_ISR(USART2) & USART_ISR_ORE) != 0) { + USART_ICR (USART2) = USART_ICR_ORECF; + usart_send_blocking (USART2, 'R'); + } + if ((USART_ISR(USART2) & USART_ISR_PE) != 0) { + USART_ICR (USART2) = USART_ICR_PECF; + usart_send_blocking (USART2, 'P'); + } + if ((USART_ISR(USART2) & USART_ISR_FE) != 0) { + USART_ICR (USART2) = USART_ICR_FECF; + usart_send_blocking (USART2, 'F'); + } + } + + return 0; +}