forked from aspalmer/stfuboot
/
uart.c
135 lines (115 loc) · 3.12 KB
/
uart.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* This file is part of the stfuboot project.
*
* Copyright (C) 2012 Innovative Converged Devices (ICD)
*
* Author(s):
* Andrey Smirnov <andrey.smirnov@convergeddevices.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/unistd.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/f1/gpio.h>
#include "uart.h"
#define UART_BUFFER_SIZE 256
struct uart_buffer{
unsigned char start;
unsigned char end;
unsigned char watermark;
char data[UART_BUFFER_SIZE];
};
#define UART_BUF_COUNT(b) \
(b->end >= b->start) ? (b->end - b->start) : \
(UART_BUFFER_SIZE - b->start + b->end)
static int uart_buffer_push(volatile struct uart_buffer *buf, char c)
{
unsigned char count;
int ret = 0;
if (buf != NULL) {
if ((buf->end + 1) % UART_BUFFER_SIZE != buf->start) {
buf->data[buf->end] = c;
buf->end = (buf->end + 1) % UART_BUFFER_SIZE;
count = UART_BUF_COUNT(buf);
if (count > buf->watermark) {
buf->watermark = count;
}
} else {
ret = -ENOMEM;
}
} else {
ret = -EINVAL;
}
return ret;
}
static int uart_buffer_pop(volatile struct uart_buffer *buf, char *c)
{
int ret = 0;
if (buf != NULL && c != NULL) {
if (buf->end != buf->start) {
*c = buf->data[buf->start];
buf->start = (buf->start + 1) % UART_BUFFER_SIZE;
} else {
ret = -EBUSY;
}
} else {
ret = -EINVAL;
}
return ret;
}
static volatile struct uart_buffer uart_tx;
void stfub_uart_putchar(char c)
{
int n = 0;
if (c == '\n')
stfub_uart_putchar('\r');
if (usart_tx_interrupt_enabled(USART2)) {
while (uart_buffer_push(&uart_tx, c) < 0 && ++n < 1000);
} else {
usart_enable_tx_interrupt(USART2);
usart_send(USART2, (uint16_t)c);
}
}
void usart2_isr(void)
{
char c;
if ((USART_SR(USART2) & USART_SR_TXE) != 0) {
if (uart_buffer_pop(&uart_tx, &c) == 0) {
usart_send(USART2, (uint16_t)c);
} else {
usart_disable_tx_interrupt(USART2);
}
}
}
void stfub_uart_init(void)
{
memset((void *)&uart_tx,0,sizeof(uart_tx));
nvic_enable_irq(NVIC_USART2_IRQ);
nvic_set_priority(NVIC_USART2_IRQ, 3);
usart_set_baudrate(USART2, 115200);
usart_set_databits(USART2, 8);
usart_set_stopbits(USART2, USART_CR2_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);
usart_disable_tx_interrupt(USART2);
}