Skip to content

Instantly share code, notes, and snippets.

@cms-codes
Created February 20, 2022 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cms-codes/dd03bdef6d9b48795e229ab7e0e6ffa1 to your computer and use it in GitHub Desktop.
Save cms-codes/dd03bdef6d9b48795e229ab7e0e6ffa1 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/g0/syscfg.h>
#include <libopencm3/stm32/g0/usart.h>
#include "periph.h"
#include "systick.h"
#include "uart.h"
int main(void)
{
Periph_init();
Systick_init();
uint16_t usart_out = 0xAB;
while (1)
{
usart_set_baudrate( USART2, 115200 );
usart_set_databits( USART2, 8 );
usart_set_stopbits( USART2, USART_CR2_STOPBITS_1 );
usart_set_mode( USART2, USART_MODE_TX );
usart_enable_halfduplex( USART2 );
usart_set_parity( USART2, USART_PARITY_NONE );
usart_set_flow_control( USART2, USART_FLOWCONTROL_NONE );
usart_enable( USART2 );
Systick_delayTicks(500);
usart_send_blocking( USART2, usart_out );
gpio_toggle( PORT_LED, PIN_LED );
usart_disable( USART2 );
}
}
#include "periph.h"
/********* Periph_init *******
* Sets up clocks, pins, and peripherals.
* Inputs: none
* Outputs: none
*/
void Periph_init(void) {
rcc_init();
gpio_init();
nvic_init();
}
/********* rcc_init *******
* Enables the clock for GPIO and other peripherals.
* Inputs: none
* Outputs: none
*/
void rcc_init(void) {
rcc_set_sysclk_source(RCC_HSI);
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOC);
rcc_periph_clock_enable(RCC_DMA1);
rcc_periph_clock_enable(RCC_USART2);
}
/********* gpio_init *******
* Initializes the GPIO registers and sets up peripheral and other output pins.
* Inputs: none
* Outputs: none
*/
void gpio_init(void) {
/* USART peripheral for serial debug (via virtual COM port /tty/ACMx) */
gpio_mode_setup( PORT_USART2, GPIO_MODE_AF, GPIO_PUPD_NONE, PIN_USART2_TX | PIN_USART2_RX );
gpio_set_af( PORT_USART2, GPIO_AF1, PIN_USART2_TX | PIN_USART2_RX );
gpio_set_output_options( PORT_USART2, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, PIN_USART2_TX | PIN_USART2_RX );
/* LED for debugging purposes */
gpio_mode_setup( PORT_LED, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, PIN_LED );
gpio_set_output_options( PORT_LED, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, PIN_LED );
}
// ******* nvic_init *******
// Initializes the Nested Vector Interrupt Controller.
// Inputs: none
// Outputs: none
void nvic_init(void)
{
nvic_set_priority( NVIC_DMA1_CHANNEL2_3_IRQ, 2);
nvic_enable_irq( NVIC_DMA1_CHANNEL2_3_IRQ );
// nvic_set_priority( NVIC_USART2_IRQ, 0);
// nvic_enable_irq( NVIC_USART2_IRQ );
}
// ******* dma1_channel2_3_isr *******
// ISR function for DMA channels 2 & 3. Clears interrupt flags and signals that
// transfer has completed.
// Inputs: none
// Outputs: none
void dma1_channel2_3_isr(void)
{
uint32_t isr = DMA1_ISR;
// Channel 3 is USART2_TX
if ( isr & DMA_ISR_TCIF3 )
{
// clear interrupt flags
DMA1_IFCR |= DMA_IFCR_CGIF3;
}
}
void usart2_isr(void)
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment