-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialF0.c
398 lines (326 loc) · 10.7 KB
/
serialF0.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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*!
* \file serialF0.c
* \author Wim Dolman (<a href="mailto:w.e.dolman@hva.nl">w.e.dolman@hva.nl</a>)
* \date 16-04-2018
* \version 1.8
*
* \brief Serial interface voor HvA-Xmegaboard
*
* \details This serial interface doesn't use the drivers of Atmel
* The interface uses two \e non circulair buffers for sending and
* receiving the data.
* It is based om md_serial.c from J.D.Bakker.
*
* It is a serial interface for the HvA-Xmegaboard (Version 2) with a
* Xmega256a3u and Xmega32a4u for programming and the serial interface.
* You can use the standard printf, putchar, puts, scanf, getchar, ...
* functions.
*
* The baud rate is 115200
*/
#include "serialF0.h"
#include <stdio.h>
#include <stddef.h>
#include <math.h>
#include <avr/io.h>
#include <avr/interrupt.h>
static uint8_t CanRead_F0(void);
static uint8_t ReadByte_F0(void);
static uint8_t CanWrite_F0(void);
static void WriteByte_F0(uint8_t data);
/*! \brief Send a byte to UARTF0
*
* \param data byte to send
*
* \return void
*/
inline void uartF0_putc(uint8_t data)
{
WriteByte_F0(data);
}
/*! \brief Read a byte from UARTF0
*
* \return Received byte from buffer or
* UART_NO_DATA if buffer is empty
*/
inline uint16_t uartF0_getc(void)
{
uint8_t data;
if ( ! CanRead_F0() ) {
return UART_NO_DATA;
}
data = ReadByte_F0();
return (data & 0x00FF);
}
/*! \brief Send a string to UARTF0
*
* \param s a pointer to the pointer
*
* \return void
*/
void uartF0_puts(char *s)
{
char c;
while ( (c = *s++) ) {
WriteByte_F0(c);
}
}
/* \brief Send a character to UARTF0
* a static function necessary for standard stream
*
* \param c character to send
* \param stream file stream
*
* \return 0 if succeeded, else 1
*/
static int uartF0_fputc(char c, FILE *stream)
{
uint8_t timeout = 0xFF;
while ( ! CanWrite_F0() ) {
if (timeout == 0) break;
timeout--;
}
if (timeout == 0) return 1;
if (c == '\n') WriteByte_F0('\r');
WriteByte_F0(c);
return 0;
}
/* \brief Read character fromUARTF0
* a static function necessary for standard stream
*
* \param stream file stream
*
* \return Received character
*/
static int uartF0_fgetc(FILE * stream)
{
int c;
c = ReadByte_F0();
return c;
}
FILE uartF0_stdinout = FDEV_SETUP_STREAM(uartF0_fputc, uartF0_fgetc, _FDEV_SETUP_RW); //!< FILE structure for standard streams
#define BAUD_115K2 115200UL //!< Baud rate 115200
#define BAUD_57K6 57600UL //!< Baud rate 57600
#define BAUD_38K4 38000UL //!< Baud rate 38400
#define UART_DOUBLE_CLK 1 //!< Double clock speed true
#define UART_NO_DOUBLE_CLK 0 //!< Double clock speed false
/*! \brief Get a line from the serial input
*
* \param buf pointer to a buffer to store the received line
* \param len length of the buffer to store the received line
*
* \details The line has to be finished with a End-Of-Line.
* This can be \<CR\>, \<CR\>\<LF\> or \<LF\>
*
* \return Received character
*/
char *getline(char* buf, uint16_t len)
{
char *p = buf;
uint8_t c;
uint16_t timer = 0;
uint16_t i = 0;
while ( (c = getchar()) != '\n') {
if (c == '\r') {
for(timer=2000; timer>0; timer--) { // wait a short time for the next char
if ( CanRead_F0() ) break;
}
if ( timer == 0 ) break; // is CR "EOF"
if ( (c = getchar()) == '\n' ) { // is CRLF
break;
} else { // is CR
ungetc(c, stdin); // put last character back
break;
}
}
if (i++ < len) {
*p++ = c;
}
}
*p = '\0';
return buf;
}
/* \brief Calculates the baud rate value BSEL
* A static function used by init_stream
* See also code 19.13 from 'De taal C en de Xmega' 2nd edition
*
* \param f_cpu system clock (F_CPU)
* \param baud desired baud rate
* \param scale scale factor (BSCALE)
* \param clk2x clock speed double (1 for double, 0 for no double)
*
* It calculates the baud selection value BSEL from the system clock,
* the baud rate, the scale factor and a boolean for clock doubling.
*
* The formula to calculate BSEL is:
* \f{eqnarray*}{
* \mbox{BSCALE}>=0\quad &:& \quad
* \mbox{BSEL} = \frac{f_{\mbox{cpu}}}{N\ 2^{\mbox{BSCALE}}\ f_{\mbox{baud}}} - 1 \\[3pt]
* \mbox{BSCALE}<0\quad &:& \quad
* \mbox{BSEL} = \frac{1}{2^{\mbox{BSCALE}}}\
* \left( \frac{f_{\mbox{cpu}}}{N\ f_{\mbox{baud}}} - 1 \right)
* \f}
* N is a factor which is 16 with no clock doubling and 8 with clock doubling
*
* \return the calculated BSEL
*/
static uint16_t calc_bsel(uint32_t f_cpu, uint32_t baud, int8_t scale, uint8_t clk2x)
{
uint8_t factor = 16;
factor = factor >> (clk2x & 0x01);
if ( scale < 0 ) {
return round( (((double)(f_cpu)/(factor*(double)(baud))) - 1) * (1<<-(scale)) );
} else {
return round( ((double)(f_cpu)/(factor*(double)(baud))/(1<<(scale))) - 1);
}
} // calc_bsel
/* \brief Determines the scale factor BSCALE
* A static function used by init_stream
* See also code 19.12 from 'De taal C en de Xmega' 2nd edition
*
* \param f_cpu system clock (F_CPU)
* \param baud desired baud rate
* \param clk2x clock speed double (1 for double, 0 for no double)
*
* It determines the scale factor BSCALE from the system clock, the baud rate,
* and a boolean for clock doubling.
*
* \return the scale factor BSCALE
*/
static int8_t calc_bscale(uint32_t f_cpu, uint32_t baud, uint8_t clk2x)
{
int8_t bscale;
uint16_t bsel;
for (bscale = -7; bscale<8; bscale++) {
if ( (bsel = calc_bsel(f_cpu, baud, bscale, clk2x)) < 4096 ) return bscale;
}
return bscale;
} // calc_bscale
/*! \brief Initializes the serial stream for the HvA-Xmegaboard
*
* \param f_cpu clock frequency
*
* \details The only paramter is the clockfrequency. The default baud rate is 115200.
* At the moment this match best with the HvA-Xmegaboard.
* If you want to use another baud rate you can change it in this function.
*
* \return void
*/
void init_stream(uint32_t f_cpu)
{
uint16_t bsel;
int8_t bscale;
bscale = calc_bscale(f_cpu, BAUD_115K2, UART_NO_DOUBLE_CLK);
bsel = calc_bsel(f_cpu, BAUD_115K2, bscale, UART_NO_DOUBLE_CLK);
PORTF.PIN2CTRL = PORT_OPC_PULLUP_gc; // pullup on rx
PORTF.OUTSET = PIN3_bm; // tx high
PORTF.DIRSET = PIN3_bm;
PORTF.DIRCLR = PIN2_bm;
USARTF0.BAUDCTRLA = (bsel & USART_BSEL_gm);
USARTF0.BAUDCTRLB = ((bscale << USART_BSCALE_gp) & USART_BSCALE_gm) |
((bsel >> 8) & ~USART_BSCALE_gm);
USARTF0.CTRLB = USART_RXEN_bm | USART_TXEN_bm;
USARTF0.CTRLA = USART_RXCINTLVL_MED_gc |
USART_TXCINTLVL_OFF_gc | USART_DREINTLVL_OFF_gc;
PMIC.CTRL |= PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm;
stdout = stdin = &uartF0_stdinout;
} // init_stream
//@cond
// from here borrowed from J.D.Bakker md_serial.c
static volatile uint8_t tx_f0_wridx, tx_f0_rdidx, tx_f0_buf[TXBUF_DEPTH_F0];
static volatile uint8_t rx_f0_wridx, rx_f0_rdidx, rx_f0_buf[RXBUF_DEPTH_F0];
/* \brief Static function that tests if there is data in the RX buffer
*
* \return non-zero if there is data else zero
*/
static uint8_t CanRead_F0(void) {
uint8_t wridx = rx_f0_wridx, rdidx = rx_f0_rdidx;
if(wridx >= rdidx)
return wridx - rdidx;
else
return wridx - rdidx + RXBUF_DEPTH_F0;
} // CanRead_F0
/* \brief Static function that reads a byte
* This function waits until there is a byte in the buffer.
*
* \return The received byte
*/
static uint8_t ReadByte_F0(void) {
uint8_t res, curSlot, nextSlot;
curSlot = rx_f0_rdidx;
// Busy-wait for a byte to be available. Should not be necessary if the caller calls CanRead_xxx() first
while(!CanRead_F0()) ;
res = rx_f0_buf[curSlot];
nextSlot = curSlot + 1;
if(nextSlot >= RXBUF_DEPTH_F0)
nextSlot = 0;
rx_f0_rdidx = nextSlot;
return res;
} // ReadByte_F0
/* \brief Static function that tests if there is space in the TX buffer
*
* \return non-zero if there is space else zero
*/
static uint8_t CanWrite_F0(void) {
uint8_t wridx1 = tx_f0_wridx + 1, rdidx = tx_f0_rdidx;
if(wridx1 >= TXBUF_DEPTH_F0)
wridx1 -= TXBUF_DEPTH_F0;
if(rdidx >= wridx1)
return rdidx - wridx1;
else
return rdidx - wridx1 + TXBUF_DEPTH_F0;
} // CanWrite_F0
/* \brief Static function writes a byte to the TX buffer
* This function waits until there is space in the buffer.
*
* \param data byte to be written
*
* \return void
*/
static void WriteByte_F0(uint8_t data) {
uint8_t curSlot, nextSlot, savePMIC;
// Busy-wait for a byte to be available. Should not be necessary if the caller calls CanWrite_xxx() first
while(!CanWrite_F0())
USARTF0.CTRLA = USART_RXCINTLVL_MED_gc | USART_TXCINTLVL_OFF_gc | USART_DREINTLVL_LO_gc;
curSlot = tx_f0_wridx;
tx_f0_buf[curSlot] = data;
nextSlot = curSlot + 1;
if(nextSlot >= TXBUF_DEPTH_F0)
nextSlot = 0;
savePMIC = PMIC.CTRL;
PMIC.CTRL = savePMIC & ~PMIC_LOLVLEN_bm;
tx_f0_wridx = nextSlot;
USARTF0.CTRLA = USART_RXCINTLVL_MED_gc | USART_TXCINTLVL_OFF_gc | USART_DREINTLVL_LO_gc;
PMIC.CTRL = savePMIC;
} // WriteByte_F0
/* \brief ISR for receiving bytes from UARTF0.
* It puts the received byte in the RX buffer
*/
ISR(USARTF0_RXC_vect) {
uint8_t curSlot, nextSlot;
curSlot = rx_f0_wridx;
rx_f0_buf[curSlot] = USARTF0.DATA;
nextSlot = curSlot + 1;
if(nextSlot >= RXBUF_DEPTH_F0)
nextSlot = 0;
if(nextSlot != rx_f0_rdidx)
rx_f0_wridx = nextSlot;
} // ISR(USARTF0_RXC_vect)
/* \brief ISR for transmitting bytes to UARTF0.
* If there is a byte to send in the TX buffer, it will be send
*/
ISR(USARTF0_DRE_vect) {
uint8_t curSlot, nextSlot, lastSlot;
nextSlot = curSlot = tx_f0_rdidx;
lastSlot = tx_f0_wridx;
if(curSlot != lastSlot) {
USARTF0.DATA = tx_f0_buf[curSlot];
nextSlot = curSlot + 1;
if(nextSlot >= TXBUF_DEPTH_F0)
nextSlot = 0;
}
if(nextSlot == lastSlot)
USARTF0.CTRLA = USART_RXCINTLVL_MED_gc | USART_TXCINTLVL_OFF_gc | USART_DREINTLVL_OFF_gc;
tx_f0_rdidx = nextSlot;
} // ISR(USARTF0_DRE_vect)
//@endcond