Skip to content

Commit

Permalink
Add 'ADC interrupt' example
Browse files Browse the repository at this point in the history
  • Loading branch information
ziteh committed Sep 29, 2022
1 parent de2106a commit 6fb8fde
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ This repo contains some basic examples for STM32.
| [PWM](./libopencm3/pwm/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [IWDG](./libopencm3/iwdg/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [WWDG](./libopencm3/wwdg/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [ADC (Regular single channel)](./libopencm3/adc_single_channel_regular/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [ADC (Regular single channel)](./libopencm3/adc_single_channel_regular/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [ADC (Injected multi channel)](./libopencm3/adc_multi_channel_injected/) | :x: | :heavy_check_mark: | | | | |
| [ADC (Interrupt)](./libopencm3/adc_interrupt/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [SPI (Master mode)](./libopencm3/spi_master/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [SPI (Slave mode)](./libopencm3/spi_slave/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [I2C (LCD 1602)](./libopencm3/i2c_lcd1602/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
Expand Down
5 changes: 5 additions & 0 deletions libopencm3/adc_interrupt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions libopencm3/adc_interrupt/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
25 changes: 25 additions & 0 deletions libopencm3/adc_interrupt/platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[platformio]
default_envs = nucleo_f103rb

; Set/Override default options for each [env:XXX]
[env]
platform = ststm32
framework = libopencm3

[env:nucleo_f103rb]
board = nucleo_f103rb
build_flags = -D NUCLEO_F103RB

[env:nucleo_f446re]
board = nucleo_f446re
build_flags = -D NUCLEO_F446RE
159 changes: 159 additions & 0 deletions libopencm3/adc_interrupt/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/**
* @file main.c
* @brief ADC interrupt example for LibOpenCM3 with STM32.
* @author ZiTe ([email protected])
* @copyright MIT License
*/

#include "main.h"

int main(void)
{
rcc_setup();
adc_setup();
usart_setup();

printf("ADC Interrupt.\r\n");

/* Software start the first conversion. */
#if defined(STM32F1)
adc_start_conversion_direct(ADC1);
#else
adc_start_conversion_regular(ADC1);
#endif

/* Halt. */
while (1)
{
}
return 0;
}

static void rcc_setup(void)
{
#if defined(STM32F1)
rcc_clock_setup_in_hse_8mhz_out_72mhz();
#elif defined(STM32F4)
rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_84MHZ]);
#endif

rcc_periph_clock_enable(RCC_USART_TX_GPIO);
rcc_periph_clock_enable(RCC_USART2);
rcc_periph_clock_enable(RCC_ADC_GPIO);
rcc_periph_clock_enable(RCC_ADC1);
}

static void adc_setup(void)
{
/* Set to input analog. */
#if defined(STM32F1)
gpio_set_mode(GPIO_ADC_PORT,
GPIO_MODE_INPUT,
GPIO_CNF_INPUT_ANALOG,
GPIO_ADC_IN0_PIN);
#else
gpio_mode_setup(GPIO_ADC_PORT,
GPIO_MODE_ANALOG,
GPIO_PUPD_NONE,
GPIO_ADC_IN0_PIN);
#endif

/* Setup ADC. */
adc_power_off(ADC1);

adc_disable_scan_mode(ADC1);
adc_disable_external_trigger_regular(ADC1);
adc_set_single_conversion_mode(ADC1);
adc_set_right_aligned(ADC1);
adc_set_sample_time_on_all_channels(ADC1, ADC_SIMPLE_TIME);

/* Setup interrupt. */
adc_enable_eoc_interrupt(ADC1);
nvic_enable_irq(ADC_IRQ);

uint8_t channels[16];
channels[0] = 0;
adc_set_regular_sequence(ADC1, 1, channels);

adc_power_on(ADC1);
delay(800000); /* Wait a bit. */

#if defined(STM32F1)
adc_reset_calibration(ADC1);
adc_calibrate(ADC1);
#endif
}

static void usart_setup(void)
{
/* Set USART-Tx pin to alternate function. */
#if defined(NUCLEO_F103RB)
gpio_set_mode(GPIO_USART_TX_PORT,
GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
GPIO_USART_TX_PIN);
#else
gpio_mode_setup(GPIO_USART_TX_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_USART_TX_PIN);
gpio_set_af(GPIO_USART_TX_PORT, GPIO_USART_AF, GPIO_USART_TX_PIN);
#endif

/* Config USART params. */
usart_set_baudrate(USART2, USART_BAUDRATE);
usart_set_databits(USART2, 8);
usart_set_stopbits(USART2, USART_STOPBITS_1);
usart_set_parity(USART2, USART_PARITY_NONE);
usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
usart_set_mode(USART2, USART_MODE_TX);

usart_enable(USART2);
}

static void delay(uint32_t value)
{
for (uint32_t i = 0; i < value; i++)
{
__asm__("nop"); /* Do nothing. */
}
}

/* For printf(). */
int _write(int file, char *ptr, int len)
{
int i;

if (file == 1)
{
for (i = 0; i < len; i++)
{
usart_send_blocking(USART2, ptr[i]);
}
return i;
}

errno = EIO;
return -1;
}

/**
* @brief ADC Interrupt service routine.
*/
#if defined(STM32F1)
void adc1_2_isr(void)
#else
void adc_isr(void)
#endif
{
/* Clear regular end of conversion flag. */
ADC_SR(ADC1) &= ~ADC_SR_EOC;

uint16_t value = adc_read_regular(ADC1);
printf("%4d\r\n", value);
delay(5000000);

/* Sart a new conversion. */
#if defined(STM32F1)
adc_start_conversion_direct(ADC1);
#else
adc_start_conversion_regular(ADC1);
#endif
}
60 changes: 60 additions & 0 deletions libopencm3/adc_interrupt/src/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @file main.h
* @brief ADC interrupt example for LibOpenCM3 with STM32.
* @author ZiTe ([email protected])
* @copyright MIT License
*/

#ifndef MAIN_H
#define MAIN_H

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdio.h> /* For printf(). */
#include <errno.h> /* For printf(). */
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/adc.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/cm3/nvic.h>

#define USART_BAUDRATE (9600)

#if defined(NUCLEO_F103RB)
#define ADC_SIMPLE_TIME (ADC_SMPR_SMP_55DOT5CYC)
#define RCC_ADC_GPIO (RCC_GPIOA)
#define GPIO_ADC_PORT (GPIOA)
#define GPIO_ADC_IN0_PIN (GPIO0) /* Arduino-A0. */
#define ADC_IRQ (NVIC_ADC1_2_IRQ)

#define RCC_USART_TX_GPIO (RCC_GPIOA)
#define GPIO_USART_TX_PORT (GPIOA)
#define GPIO_USART_TX_PIN (GPIO2) /* ST-Link (Arduino-D1). */
#elif defined(NUCLEO_F446RE)
#define ADC_SIMPLE_TIME (ADC_SMPR_SMP_56CYC)
#define RCC_ADC_GPIO (RCC_GPIOA)
#define GPIO_ADC_PORT (GPIOA)
#define GPIO_ADC_IN0_PIN (GPIO0) /* Arduino-A0. */
#define ADC_IRQ (NVIC_ADC_IRQ)

#define RCC_USART_TX_GPIO (RCC_GPIOA)
#define GPIO_USART_TX_PORT (GPIOA)
#define GPIO_USART_TX_PIN (GPIO2) /* ST-Link (Arduino-D1). */
#define GPIO_USART_AF (GPIO_AF7) /* Ref: Table-11 in DS10693. */
#else
#error "STM32 board not defined."
#endif

static void rcc_setup(void);
static void usart_setup(void);
static void adc_setup(void);
static void delay(uint32_t value);

#ifdef __cplusplus
}
#endif

#endif /* MAIN_H. */

0 comments on commit 6fb8fde

Please sign in to comment.