Uart setup

This commit is contained in:
Imbus 2025-07-16 17:33:10 +02:00
parent bdfcef687a
commit ed3f82222d
2 changed files with 58 additions and 17 deletions

42
main.c
View file

@ -1,38 +1,62 @@
#include "system.h"
#include "uart.h"
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/stm32/flash.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/usart.h>
#include <stdint.h>
#include "system.h"
#include <stdio.h>
static void gpio_setup(void) {
rcc_periph_clock_enable(RCC_GPIOC);
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
gpio_set(GPIOC, GPIO12);
/* Setup GPIO6 and 7 (in GPIO port A) for LED use. */
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
}
static void clock_setup(void) {
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
/* Enable GPIOA clock (for LED GPIOs). */
rcc_periph_clock_enable(RCC_GPIOC);
/* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_AFIO);
rcc_periph_clock_enable(RCC_USART1);
rcc_periph_clock_enable(RCC_GPIOC);
}
int main(void) {
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
// rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSI_48MHZ]);
clock_setup();
gpio_setup();
sys_tick_setup();
usart_setup();
usart_send_blocking(USART1, 'H');
usart_send_blocking(USART1, 'e');
usart_send_blocking(USART1, 'l');
usart_send_blocking(USART1, 'l');
usart_send_blocking(USART1, 'o');
usart_send_blocking(USART1, '\n');
printf("Well hello");
gpio_clear(GPIOC, GPIO13);
gpio_set(GPIOC, GPIO13);
// gpio_set(GPIOB, GPIO6); /* LED2 off */
// ticks = 0;
uint64_t init = sys_ticks_get();
while (sys_ticks_get() == init);
/* We call this handler every 1ms so 1000ms = 1s on/off. */
uint64_t ref = 0;
while (1) {
if (sys_ticks_get() > ref) {
ref += 500;
gpio_toggle(GPIOC, GPIO13);
// while (sys_ticks_get() % 1000 == 0);
}
}

33
uart.c
View file

@ -1,20 +1,37 @@
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include "uart.h"
#include <errno.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/usart.h>
#include <stdio.h>
int _write(int file, char *ptr, int len);
void usart_setup(void) {
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_USART1);
// Set PA9 (TX) to alternate function, push-pull
/* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
/* Setup UART parameters. */
usart_set_baudrate(USART1, 115200);
usart_set_databits(USART1, 8);
usart_set_stopbits(USART1, USART_STOPBITS_1);
usart_set_mode(USART1, USART_MODE_TX);
usart_set_parity(USART1, USART_PARITY_NONE);
usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
usart_set_mode(USART1, USART_MODE_TX);
/* Finally enable the USART. */
usart_enable(USART1);
}
int _write(int file, char *ptr, int len) {
int i;
if (file == 1) {
for (i = 0; i < len; i++) usart_send_blocking(USART1, ptr[i]);
return i;
}
errno = EIO;
return -1;
}