Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added some better comments to ADC example #1

Open
wants to merge 2 commits into
base: stm32f3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion examples/stm32/f3/stm32f3-discovery/adc/README
Original file line number Diff line number Diff line change
Expand Up @@ -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.

8 changes: 6 additions & 2 deletions examples/stm32/f3/stm32f3-discovery/adc/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions examples/stm32/f3/stm32f3-discovery/usart/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
##
## This file is part of the libopencm3 project.
##
## Copyright (C) 2009 Uwe Hermann <[email protected]>
##
## 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 <http://www.gnu.org/licenses/>.
##

BINARY = usart

LDSCRIPT = ../stm32f3-discovery.ld

include ../../Makefile.include

29 changes: 29 additions & 0 deletions examples/stm32/f3/stm32f3-discovery/usart/README
Original file line number Diff line number Diff line change
@@ -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.

112 changes: 112 additions & 0 deletions examples/stm32/f3/stm32f3-discovery/usart/usart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2009 Uwe Hermann <[email protected]>
* Copyright (C) 2011 Stephen Caudle <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/

#include <libopencm3/stm32/f4/rcc.h>
#include <libopencm3/stm32/f4/gpio.h>
#include <libopencm3/stm32/usart.h>
#include <stdarg.h>


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;
}