-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. */ |