Skip to content

Commit

Permalink
Merge pull request #17 from ziteh/dev-libopencm3
Browse files Browse the repository at this point in the history
Dev libopencm3
  • Loading branch information
ziteh authored Sep 28, 2022
2 parents 99a227b + 4c7127e commit de2106a
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 81 deletions.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ This repo contains some basic examples for STM32.

[LibOpenCM3](https://github.com/libopencm3/libopencm3) is an open source ARM Cortex-M microcontroller library.

| examples \ boards | Nucleo-F103RB | Nucleo-F446RE | Nucleo-F401RE | Nucleo-F302R8 | Nucleo-L432KC | Nucleo-G431KB |
| :--------------------------------------------------------------- | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: |
| [Blinking LED](./libopencm3/blink/) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| [GPIO input](./libopencm3/gpio_input/) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | |
| [EXTI button](./libopencm3/exti_button/) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | |
| [USART with printf()](./libopencm3/usart_printf/) | :heavy_check_mark: | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: | |
| [USART receive interrupt](./libopencm3/usart_receive_interrupt/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [Timer](./libopencm3/timer/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [SysTick delay](./libopencm3/systick/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [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 (Multi channel)](./libopencm3/adc_multi_channel/) | :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: | | | | |
| examples \ boards | Nucleo-F103RB | Nucleo-F446RE | Nucleo-F401RE | Nucleo-F302R8 | Nucleo-L432KC | Nucleo-G431KB |
| :----------------------------------------------------------------------- | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: |
| [Blinking LED](./libopencm3/blink/) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| [GPIO input](./libopencm3/gpio_input/) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | |
| [EXTI button](./libopencm3/exti_button/) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | |
| [USART with printf()](./libopencm3/usart_printf/) | :heavy_check_mark: | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: | |
| [USART receive interrupt](./libopencm3/usart_receive_interrupt/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [Timer](./libopencm3/timer/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [SysTick delay](./libopencm3/systick/) | :heavy_check_mark: | :heavy_check_mark: | | | | |
| [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 (Injected multi channel)](./libopencm3/adc_multi_channel_injected/) | :x: | :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: | | | | |

### STM32 HAL

Expand Down
File renamed without changes.
File renamed without changes.
156 changes: 156 additions & 0 deletions libopencm3/adc_multi_channel_injected/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* @file main.c
* @brief ADC injected multi channel 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 Ready\r\n");

while (1)
{
/* Software start ADC injected conversion. */
adc_start_conversion_injected(ADC1);

/* Wait for ADC injected end of conversion. */
while (!adc_eoc_injected(ADC1))
{
}

/* Clear ADC injected end of conversion flag. */
ADC_SR(ADC1) &= ~ADC_SR_JEOC;

/* Read ADC injected. */
uint16_t value1 = adc_read_injected(ADC1, 1);
uint16_t value2 = adc_read_injected(ADC1, 2);
uint16_t value3 = adc_read_injected(ADC1, 3);

printf("%4d, %4d, %4d\r\n", value1, value2, value3);
delay(5000000);
}

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 | GPIO_ADC_IN1_PIN | GPIO_ADC_IN4_PIN);
#else
gpio_mode_setup(GPIO_ADC_PORT,
GPIO_MODE_ANALOG,
GPIO_PUPD_NONE,
GPIO_ADC_IN0_PIN | GPIO_ADC_IN1_PIN | GPIO_ADC_IN4_PIN);
#endif

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

adc_enable_scan_mode(ADC1);
adc_set_single_conversion_mode(ADC1);
adc_disable_discontinuous_mode_regular(ADC1);
adc_disable_discontinuous_mode_injected(ADC1);

/* We want to start the injected conversion in sofrware. */
#if defined(STM32F1)
adc_enable_external_trigger_injected(ADC1,
ADC_CR2_JSWSTART);
#else
adc_enable_external_trigger_injected(ADC1,
ADC_CR2_JSWSTART,
ADC_CR2_JEXTEN_DISABLED);
#endif

adc_set_right_aligned(ADC1);
adc_set_sample_time_on_all_channels(ADC1, ADC_SIMPLE_TIME);

uint8_t channels[4];
channels[0] = 0;
channels[1] = 1;
channels[2] = 4;
adc_set_injected_sequence(ADC1, 3, 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;
}
63 changes: 63 additions & 0 deletions libopencm3/adc_multi_channel_injected/src/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @file main.h
* @brief ADC injected multi channel 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>

#define USART_BAUDRATE (9600)

#if defined(NUCLEO_F103RB)
#error "Not working, need to fix." /* TODO. */

#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 GPIO_ADC_IN1_PIN (GPIO1) /* Arduino-A1. */
#define GPIO_ADC_IN4_PIN (GPIO4) /* Arduino-A2. */

#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 GPIO_ADC_IN1_PIN (GPIO1) /* Arduino-A1. */
#define GPIO_ADC_IN4_PIN (GPIO4) /* Arduino-A2. */

#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. */
5 changes: 5 additions & 0 deletions libopencm3/adc_single_channel_regular/.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_single_channel_regular/.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_single_channel_regular/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
Loading

0 comments on commit de2106a

Please sign in to comment.