forked from tenbaht/sduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuart-ringbuffer.c
More file actions
228 lines (177 loc) · 6.15 KB
/
uart-ringbuffer.c
File metadata and controls
228 lines (177 loc) · 6.15 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
HardwareSerial.c - Hardware serial library for plainduino
Copyright (c) 2016 Michael Mayer
Plain C version of HardwareSerial.cpp of the Arduino project.
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified 23 November 2006 by David A. Mellis
Modified 28 September 2010 by Mark Sproul
Modified 14 August 2012 by Alarus
*/
#include"stm8s.h"
//#include "c-HardwareSerial.h"
#define SERIAL_BUFFER_SIZE 16
typedef struct ring_buffer
{
unsigned char buffer[SERIAL_BUFFER_SIZE];
volatile unsigned int head;
volatile unsigned int tail;
} ring_buffer;
ring_buffer rx_buffer = { { 0 }, 0, 0};
ring_buffer tx_buffer = { { 0 }, 0, 0};
void store_char(unsigned char c, ring_buffer *buffer)
//inline void store_char(unsigned char c, ring_buffer *buffer)
{
int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != buffer->tail) {
buffer->buffer[buffer->head] = c;
buffer->head = i;
}
}
volatile char transmitting=0;
// Interrupt handler ///////////////////////////////////////////////////////////
void UART1_RX_IRQHandler(void) __interrupt(ITC_IRQ_UART1_RX) /* UART1 RX */
{
unsigned char c;
c = UART1_ReceiveData8();
// check for parity error
if (!UART1_GetFlagStatus(UART1_FLAG_PE)) {
// no parity error, so store the data
store_char(c, &rx_buffer);
};
}
void UART1_TX_IRQHandler(void) __interrupt(ITC_IRQ_UART1_TX) /* UART1 TX */
{
if (tx_buffer.head == tx_buffer.tail) {
// Buffer empty, so disable interrupts
transmitting = 0;
UART1_ITConfig(UART1_IT_TXE, DISABLE);
}
else {
// There is more data in the output buffer. Send the next byte
unsigned char c = tx_buffer.buffer[tx_buffer.tail];
tx_buffer.tail = (tx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
UART1_SendData8(c);
}
}
// Public Methods //////////////////////////////////////////////////////////////
void HardwareSerial_begin(unsigned long baud)
{
//set the data bits, parity, and stop bits
UART1_Init(baud,
UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
UART1_ITConfig(UART1_IT_RXNE, ENABLE); // enable RXNE interrupt
}
void HardwareSerial_begin_config(unsigned long baud, uint8_t config)
{
UART1_StopBits_TypeDef stopbits;
UART1_Parity_TypeDef parity;
UART1_WordLength_TypeDef wordlength;
wordlength = (config & 0x10) ? UART1_WORDLENGTH_9D : UART1_WORDLENGTH_8D;
stopbits = (config & 0x03) ? UART1_STOPBITS_2 : UART1_STOPBITS_1;
parity=UART1_PARITY_NO; // default
config &= 0xc;
if (config == 0x8) parity=UART1_PARITY_EVEN;
else if (config == 0xc) parity=UART1_PARITY_ODD;
//set the data bits, parity, and stop bits
UART1_Init(baud, wordlength, stopbits, parity,
UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
UART1_ITConfig(UART1_IT_RXNE, ENABLE); // enable RXNE interrupt
}
void HardwareSerial_end(void)
{
// wait for transmission of outgoing data
while (tx_buffer.head != tx_buffer.tail)
;
UART1_DeInit();
// clear any received data
rx_buffer.head = rx_buffer.tail;
}
int HardwareSerial_available(void)
{
return (unsigned int)(SERIAL_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % SERIAL_BUFFER_SIZE;
}
int HardwareSerial_peek(void)
{
if (rx_buffer.head == rx_buffer.tail) {
return -1;
} else {
return rx_buffer.buffer[rx_buffer.tail];
}
}
int HardwareSerial_read(void)
{
// if the head isn't ahead of the tail, we don't have any characters
if (rx_buffer.head == rx_buffer.tail) {
return -1;
} else {
unsigned char c = rx_buffer.buffer[rx_buffer.tail];
rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
return c;
}
}
void HardwareSerial_flush(void)
{
// UDR is kept full while the buffer is not empty, so TXC triggers when
// EMPTY && SENT
// while (transmitting && ! (*_ucsra & _BV(TXC0)));
// while (transmitting && ! (UART1_SR & UART1_SR_TC));
while (transmitting && ! UART1_GetFlagStatus(UART1_FLAG_TC));
transmitting = 0;
}
//size_t
int HardwareSerial_write(uint8_t c)
{
int i = (tx_buffer.head + 1) % SERIAL_BUFFER_SIZE;
// If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit
// ???: return 0 here instead?
while (i == tx_buffer.tail)
;
tx_buffer.buffer[tx_buffer.head] = c;
tx_buffer.head = i;
UART1_ITConfig(UART1_IT_TXE, ENABLE); // enable TXE interrupt
transmitting = 1;
//FIXME: unklar, ob das auf dem STM8 wirklich nötig ist.
// Das TXE-Bit in UART_SR ist jedenfalls nur lesbar.
// clear the TXC bit -- "can be cleared by writing a one to its bit location"
// sbi(*_ucsra, TXC0);
return 1;
}
void send_string(char *str)
{
char c;
if (!str) return;
while ( c=*str++ ) HardwareSerial_write(c); // assignment intented
}
void main (void)
{
uint32_t i;
UART1_DeInit();
enableInterrupts();
HardwareSerial_begin(9600);
while (1) {
send_string("Hello World!\r\n");
for (i=15000; i; i--);
while(HardwareSerial_available()) {
HardwareSerial_write('.');
HardwareSerial_write(HardwareSerial_read());
};
for (i=15000; i; i--);
}
}