summaryrefslogtreecommitdiff
path: root/usart_irq/main.c
blob: 48ce18b724a5115d01917b554e1b8a8ef7eff331 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69


#include <errno.h>
#include <stdio.h>
#include <unistd.h>

#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>

#include "usart.h"


#define DELAY 1000000
#define RX_BUFFER_SIZE 1000
#define TX_BUFFER_SIZE 1000


void clock_init(void);
void gpio_init(void);
void delay(void);


USART usart3;


int main(void)
{
	// setup
	clock_init();
	gpio_init();
	uint8_t rx_buffer[RX_BUFFER_SIZE];
	uint8_t tx_buffer[TX_BUFFER_SIZE];
	usart_init(&usart3, rx_buffer, RX_BUFFER_SIZE, tx_buffer, TX_BUFFER_SIZE);

	// welcome message
	usart_writeln(&usart3, "This is a demo of USART via IRQ. Just type something!");

	// return any input
	uint8_t buff;
	while(1){
		if (usart_read_bytes(&usart3, &buff, 1)){
			usart_write_bytes(&usart3, &buff, 1);
		}
	}

	return 0;
}


void clock_init(void)
{
	rcc_periph_clock_enable(RCC_GPIOC);  // for PC13 blinking
}


void gpio_init(void)
{
	gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
	gpio_clear(GPIOC, GPIO13);
	gpio_set(GPIOC, GPIO13);
}


void delay(void)
{
	for (int i = 0; i < DELAY; i++) {
		__asm__("nop");
	}
}