Document AVR dependency.
[bertos.git] / drv / ser_avr.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
5  * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
6  * This file is part of DevLib - See devlib/README for information.
7  * -->
8  *
9  * \brief AVR UART and SPI I/O driver
10  *
11  * Rationale for project_ks hardware.
12  *
13  * The serial 0 on the board_kf board is used to communicate with the
14  * smart card, which has the TX and RX lines connected together. To
15  * allow the smart card to drive the RX line of the CPU the CPU TX has
16  * to be in a high impedance state.
17  * Whenever a transmission is done and there is nothing more to send
18  * the transmitter is turn off. The output pin is held in input with
19  * pull-up enabled, to avoid capturing noise from the nearby RX line.
20  *
21  * The line on the KBus port must keep sending data, even when
22  * there is nothing to transmit, because a burst data transfer
23  * generates noise on the audio channels.
24  * This is accomplished using the multiprocessor mode of the
25  * ATmega64/128 serial.
26  *
27  * The receiver keeps the MPCM bit always on. When useful data
28  * is trasmitted the address bit is set. The receiver hardware
29  * consider the frame as address info and receive it.
30  * When useless fill bytes are sent the address bit is cleared
31  * and the receiver will ignore them, avoiding useless triggering
32  * of RXC interrupt.
33  *
34  * \version $Id$
35  * \author Bernardo Innocenti <bernie@develer.com>
36  */
37
38 /*
39  * $Log$
40  * Revision 1.7  2004/07/18 21:54:23  bernie
41  * Add ATmega8 support.
42  *
43  * Revision 1.5  2004/06/27 15:25:40  aleph
44  * Add missing callbacks for SPI;
45  * Change UNUSED() macro to new version with two args;
46  * Use TX line filling only on the correct KBUS serial port;
47  * Fix nasty IRQ disabling bug in recv complete hander for port 1.
48  *
49  * Revision 1.4  2004/06/03 11:27:09  bernie
50  * Add dual-license information.
51  *
52  * Revision 1.3  2004/06/02 21:35:24  aleph
53  * Serial enhancements: interruptible receive handler and 8 bit serial status for AVR; remove volatile attribute to FIFOBuffer, useless for new fifobuf routens
54  *
55  * Revision 1.2  2004/05/23 18:21:53  bernie
56  * Trim CVS logs and cleanup header info.
57  *
58  */
59
60 #include "ser.h"
61 #include "ser_p.h"
62 #include "kdebug.h"
63 #include "config.h"
64 #include "hw.h"
65 #include <mware/fifobuf.h>
66
67 #include <avr/signal.h>
68
69
70 extern struct Serial ser_handles[SER_CNT];
71
72 struct AvrSerial
73 {
74         struct SerialHardware hw;
75         struct Serial* serial;
76 };
77
78
79 /* Hardware handshake */
80 #define RTS_ON
81 #define RTS_OFF
82 #define IS_CTS_ON   true
83 #define IS_CTS_OFF  false
84
85
86 /* SPI port and pin configuration */
87 #define SPI_PORT      PORTB
88 #define SPI_DDR       DDRB
89 #define SPI_SCK_BIT   PORTB1
90 #define SPI_MOSI_BIT  PORTB2
91 #define SPI_MISO_BIT  PORTB3
92
93
94 #if defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)
95         #define AVR_HAS_UART1
96 #elif defined(__AVR_ATmega8__)
97         #define UCSR0A UCSRA
98         #define UCSR0B UCSRB
99         #define UCSR0C UCSRC
100         #define UDR0   UDR
101         #define UBRR0L UBRRL
102         #define UBRR0H UBRRH
103         #define SIG_UART0_DATA SIG_UART_DATA
104         #define SIG_UART0_RECV SIG_UART_RECV
105 #elif defined(__AVR_ATmega103__)
106         /* Macro for ATmega103 compatibility */
107         #define UCSR0B UCR
108         #define UDR0   UDR
109         #define UCSR0A USR
110         #define UBRR0L UBRR
111         #define SIG_UART0_DATA SIG_UART_DATA
112         #define SIG_UART0_RECV SIG_UART_RECV
113 #else
114         #error Unknown architecture
115 #endif
116
117
118 /* Transmission fill byte */
119 #define SER_FILL_BYTE 0xAA
120
121
122 static void uart0_enabletxirq(UNUSED(struct SerialHardware *, ctx))
123 {
124 #if defined(CONFIG_SER_TXFILL) && (CONFIG_KBUS_SERIAL_PORT == 0)
125         UCSR0B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN) | BV(UCSZ2);
126 #else
127         UCSR0B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN);
128 #endif
129 }
130
131 static void uart0_init(struct SerialHardware *_hw, struct Serial *ser)
132 {
133         struct AvrSerial *hw = (struct AvrSerial *)_hw;
134         hw->serial = ser;
135
136 #if defined(ARCH_BOARD_KS) && (ARCH & ARCH_BOARD_KS)
137         /* Set TX port as input with pull-up enabled to avoid
138            noise on the remote RX when TX is disabled. */
139         cpuflags_t flags;
140         DISABLE_IRQSAVE(flags);
141         DDRE &= ~BV(PORTE1);
142         PORTE |= BV(PORTE1);
143         ENABLE_IRQRESTORE(flags);
144 #endif /* ARCH_BOARD_KS */
145
146 #if defined(CONFIG_SER_TXFILL) && (CONFIG_KBUS_SERIAL_PORT == 0)
147         /*!
148          * Set multiprocessor mode and 9 bit data frame.
149          * The receiver keep MPCM bit always on. When useful data
150          * is trasmitted the ninth bit is set and the receiver receive
151          * the frame.
152          * When useless fill bytes are sent the ninth bit is cleared
153          * and the receiver will ignore them.
154          */
155         UCSR0A = BV(MPCM);
156         UCSR0B = BV(RXCIE) | BV(RXEN) | BV(UCSZ2);
157 #else
158         UCSR0B = BV(RXCIE) | BV(RXEN);
159 #endif
160
161         RTS_ON;
162 }
163
164 static void uart0_cleanup(UNUSED(struct SerialHardware *, ctx))
165 {
166         UCSR0B = 0;
167 }
168
169 static void uart0_setbaudrate(UNUSED(struct SerialHardware *, ctx), unsigned long rate)
170 {
171         /* Compute baud-rate period */
172         uint16_t period = (((CLOCK_FREQ / 16UL) + (rate / 2)) / rate) - 1;
173         DB(kprintf("uart0_setbaudrate(rate=%ld): period=%d\n", rate, period);)
174
175 #ifndef __AVR_ATmega103__
176         UBRR0H = (period) >> 8;
177 #endif
178         UBRR0L = (period);
179 }
180
181 static void uart0_setparity(UNUSED(struct SerialHardware *, ctx), int parity)
182 {
183 #ifndef __AVR_ATmega103__
184         UCSR0C |= (parity) << UPM0;
185 #endif
186 }
187
188 #ifdef AVR_HAS_UART1
189
190 static void uart1_enabletxirq(UNUSED(struct SerialHardware *, ctx))
191 {
192 #if defined(CONFIG_SER_TXFILL) && (CONFIG_KBUS_SERIAL_PORT == 1)
193         UCSR1B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN) | BV(UCSZ2);
194 #else
195         UCSR1B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN);
196 #endif
197 }
198
199 static void uart1_init(struct SerialHardware *_hw, struct Serial *ser)
200 {
201         struct AvrSerial *hw = (struct AvrSerial *)_hw;
202         hw->serial = ser;
203
204         /* Set TX port as input with pull-up enabled to avoid
205          * noise on the remote RX when TX is disabled */
206         cpuflags_t flags;
207         DISABLE_IRQSAVE(flags);
208         DDRD &= ~BV(PORTD3);
209         PORTD |= BV(PORTD3);
210         ENABLE_IRQRESTORE(flags);
211
212 #if defined(CONFIG_SER_TXFILL) && (CONFIG_KBUS_SERIAL_PORT == 1)
213         /*! See comment in uart0_init() */
214         UCSR1A = BV(MPCM);
215         UCSR1B = BV(RXCIE) | BV(RXEN) | BV(UCSZ2);
216 #else
217         UCSR1B = BV(RXCIE) | BV(RXEN);
218 #endif
219
220         RTS_ON;
221 }
222
223 static void uart1_cleanup(UNUSED(struct SerialHardware *, ctx))
224 {
225         UCSR1B = 0;
226 }
227
228 static void uart1_setbaudrate(UNUSED(struct SerialHardware *, ctx), unsigned long rate)
229 {
230         /* Compute baud-rate period */
231         uint16_t period = (((CLOCK_FREQ / 16UL) + (rate / 2)) / rate) - 1;
232         DB(kprintf("uart1_setbaudrate(rate=%ld): period=%d\n", rate, period);)
233
234         UBRR1H = (period) >> 8;
235         UBRR1L = (period);
236 }
237
238 static void uart1_setparity(UNUSED(struct SerialHardware *, ctx), int parity)
239 {
240         // FIXME: move somewhere else
241         UCSR1C |= BV(USBS1); // 2 stop bits
242         UCSR1C |= (parity) << UPM0;
243 }
244
245 #endif // AVR_HAS_UART1
246
247
248 static void spi_init(struct SerialHardware *_hw, struct Serial *ser)
249 {
250         struct AvrSerial *hw = (struct AvrSerial *)_hw;
251         hw->serial = ser;
252
253         /*
254          * Set MOSI and SCK ports out, MISO in.
255          *
256          * The ATmega64/128 datasheet explicitly states that the input/output
257          * state of the SPI pins is not significant, as when the SPI is
258          * active the I/O port are overrided.
259          * This is *blatantly FALSE*.
260          *
261          * Moreover the MISO pin on the board_kc *must* be in high impedance
262          * state even when the SPI is off, because the line is wired together
263          * with the KBus serial RX, and the transmitter of the slave boards
264          * could not be able to drive the line.
265          */
266         SPI_DDR |= BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT);
267         SPI_DDR &= ~BV(SPI_MISO_BIT);
268         /* Enable SPI, IRQ on, Master, CPU_CLOCK/16 */
269         SPCR = BV(SPE) | BV(SPIE) | BV(MSTR) | BV(SPR0);
270 }
271
272 static void spi_cleanup(UNUSED(struct SerialHardware *, ctx))
273 {
274         SPCR = 0;
275         /* Set all pins as inputs */
276         SPI_DDR &= ~(BV(SPI_MISO_BIT) | BV(SPI_MOSI_BIT) | BV(SPI_SCK_BIT));
277 }
278
279 static void spi_setbaudrate(UNUSED(struct SerialHardware *, ctx), UNUSED(unsigned long, rate))
280 {
281         // Do nothing
282 }
283
284 static void spi_setparity(UNUSED(struct SerialHardware *, ctx), UNUSED(int, parity))
285 {
286         // Do nothing
287 }
288
289
290 #if defined(CONFIG_SER_HW_HANDSHAKE)
291
292 //! This interrupt is triggered when the CTS line goes high
293 SIGNAL(SIG_CTS)
294 {
295         // Re-enable UDR empty interrupt and TX, then disable CTS interrupt
296         UCSR0B = BV(RXCIE) | BV(UDRIE) | BV(RXEN) | BV(TXEN);
297         cbi(EIMSK, EIMSKB_CTS);
298 }
299
300 #endif // CONFIG_SER_HW_HANDSHAKE
301
302
303 /*!
304  * Serial 0 TX interrupt handler
305  */
306 SIGNAL(SIG_UART0_DATA)
307 {
308         if (fifo_isempty(&ser_handles[SER_UART0].txfifo))
309         {
310 #if defined(CONFIG_SER_TXFILL) && (CONFIG_KBUS_SERIAL_PORT == 0)
311                 /*
312                  * To avoid audio interference: always transmit useless char.
313                  * Send the byte with the ninth bit cleared, the receiver in MCPM mode
314                  * will ignore it.
315                  */
316                 UCSR0B &= ~BV(TXB8);
317                 UDR0 = SER_FILL_BYTE;
318 #else
319                 /* Disable UDR empty interrupt and transmitter */
320                 UCSR0B = BV(RXCIE) | BV(RXEN);
321 #endif
322         }
323 #if defined(CONFIG_SER_HWHANDSHAKE)
324         else if (IS_CTS_OFF)
325         {
326                 // disable rx interrupt and tx, enable CTS interrupt
327                 UCSR0B = BV(RXCIE) | BV(RXEN);
328                 sbi(EIFR, EIMSKB_CTS);
329                 sbi(EIMSK, EIMSKB_CTS);
330         }
331 #endif // CONFIG_SER_HWHANDSHAKE
332         else
333         {
334 #if defined(CONFIG_SER_TXFILL) && (CONFIG_KBUS_SERIAL_PORT == 0)
335                 /* Send with ninth bit set. Receiver in MCPM mode will receive it */
336                 UCSR0B |= BV(TXB8);
337 #endif
338                 UDR0 = fifo_pop(&ser_handles[SER_UART0].txfifo);
339         }
340 }
341
342
343 #ifdef AVR_HAS_UART1
344
345 /*!
346  * Serial 1 TX interrupt handler
347  */
348 SIGNAL(SIG_UART1_DATA)
349 {
350         if (fifo_isempty(&ser_handles[SER_UART1].txfifo))
351         {
352 #if defined(CONFIG_SER_TXFILL) && (CONFIG_KBUS_SERIAL_PORT == 1)
353                 /*
354                  * To avoid audio interference: always transmit useless char.
355                  * Send the byte with the ninth bit cleared, the receiver in MCPM mode
356                  * will ignore it.
357                  */
358                 UCSR1B &= ~BV(TXB8);
359                 UDR1 = SER_FILL_BYTE;
360 #else
361                 /* Disable UDR empty interrupt and transmitter */
362                 UCSR1B = BV(RXCIE) | BV(RXEN);
363 #endif
364         }
365 #if defined(CONFIG_SER_HWHANDSHAKE)
366         else if (IS_CTS_OFF)
367         {
368                 // disable rx interrupt and tx, enable CTS interrupt
369                 UCSR1B = BV(RXCIE) | BV(RXEN);
370                 sbi(EIFR, EIMSKB_CTS);
371                 sbi(EIMSK, EIMSKB_CTS);
372         }
373 #endif // CONFIG_SER_HWHANDSHAKE
374         else
375         {
376 #if defined(CONFIG_SER_TXFILL) && (CONFIG_KBUS_SERIAL_PORT == 1)
377                 /* Send with ninth bit set. Receiver in MCPM mode will receive it */
378                 UCSR1B |= BV(TXB8);
379 #endif
380                 UDR1 = fifo_pop(&ser_handles[SER_UART1].txfifo);
381         }
382 }
383 #endif // AVR_HAS_UART1
384
385
386 /*!
387  * Serial 0 RX complete interrupt handler.
388  *
389  * This handler is interruptible.
390  * Interrupt are reenabled as soon as recv complete interrupt is
391  * disabled. Using INTERRUPT() is troublesome when the serial
392  * is heavily loaded, because an interrupt could be retriggered
393  * when executing the handler prologue before RXCIE is disabled.
394  */
395 SIGNAL(SIG_UART0_RECV)
396 {
397         /* Disable Recv complete IRQ */
398         UCSR0B &= ~BV(RXCIE);
399         ENABLE_INTS;
400
401         /* Should be read before UDR */
402         ser_handles[SER_UART0].status |= UCSR0A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
403
404         /* To clear the RXC flag we must _always_ read the UDR even when we're
405          * not going to accept the incoming data, otherwise a new interrupt
406          * will occur once the handler terminates.
407          */
408         char c = UDR0;
409
410         if (fifo_isfull(&ser_handles[SER_UART0].rxfifo))
411                 ser_handles[SER_UART0].status |= SERRF_RXFIFOOVERRUN;
412         else
413         {
414                 fifo_push(&ser_handles[SER_UART0].rxfifo, c);
415 #if defined(CONFIG_SER_HW_HANDSHAKE)
416                 if (fifo_isfull(&ser_handles[SER_UART0].rxfifo))
417                         RTS_OFF;
418 #endif
419         }
420         /* Reenable receive complete int */
421         UCSR0B |= BV(RXCIE);
422 }
423
424
425 #ifdef AVR_HAS_UART1
426
427 /*!
428  * Serial 1 RX complete interrupt handler.
429  *
430  * This handler is interruptible.
431  * Interrupt are reenabled as soon as recv complete interrupt is
432  * disabled. Using INTERRUPT() is troublesome when the serial
433  * is heavily loaded, because an interrupt could be retriggered
434  * when executing the handler prologue before RXCIE is disabled.
435  */
436 SIGNAL(SIG_UART1_RECV)
437 {
438         /* Disable Recv complete IRQ */
439         UCSR1B &= ~BV(RXCIE);
440         ENABLE_INTS;
441
442         /* Should be read before UDR */
443         ser_handles[SER_UART1].status |= UCSR1A & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
444
445         /* To avoid an IRQ storm, we must _always_ read the UDR even when we're
446          * not going to accept the incoming data
447          */
448         char c = UDR1;
449
450         if (fifo_isfull(&ser_handles[SER_UART1].rxfifo))
451                 ser_handles[SER_UART1].status |= SERRF_RXFIFOOVERRUN;
452         else
453         {
454                 fifo_push(&ser_handles[SER_UART1].rxfifo, c);
455 #if defined(CONFIG_SER_HW_HANDSHAKE)
456                 if (fifo_isfull(&ser_handles[SER_UART1].rxfifo))
457                         RTS_OFF;
458 #endif
459         }
460         /* Reenable receive complete int */
461         UCSR1B |= BV(RXCIE);
462 }
463
464 #endif // AVR_HAS_UART1
465
466
467 /*
468  * SPI Flag: true if we are transmitting/receiving with the SPI.
469  *
470  * This kludge is necessary because the SPI sends and receives bytes
471  * at the same time and the SPI IRQ is unique for send/receive.
472  * The only way to start transmission is to write data in SPDR (this
473  * is done by spi_starttx()). We do this *only* if a transfer is
474  * not already started.
475  */
476 static volatile bool spi_sending = false;
477
478 static void spi_starttx(UNUSED(struct SerialHardware *, ctx))
479 {
480         cpuflags_t flags;
481
482         DISABLE_IRQSAVE(flags);
483
484         /* Send data only if the SPI is not already transmitting */
485         if (!spi_sending && !fifo_isempty(&ser_handles[SER_SPI].txfifo))
486         {
487                 SPDR = fifo_pop(&ser_handles[SER_SPI].txfifo);
488                 spi_sending = true;
489         }
490
491         ENABLE_IRQRESTORE(flags);
492 }
493
494 /*!
495  * SPI interrupt handler
496  */
497 SIGNAL(SIG_SPI)
498 {
499         /* Read incoming byte. */
500         if (!fifo_isfull(&ser_handles[SER_SPI].rxfifo))
501                 fifo_push(&ser_handles[SER_SPI].rxfifo, SPDR);
502         /*
503          * FIXME
504         else
505                 ser_handles[SER_SPI].status |= SERRF_RXFIFOOVERRUN;
506         */
507
508         /* Send */
509         if (!fifo_isempty(&ser_handles[SER_SPI].txfifo))
510                 SPDR = fifo_pop(&ser_handles[SER_SPI].txfifo);
511         else
512                 spi_sending = false;
513 }
514
515
516 static const struct SerialHardwareVT UART0_VT =
517 {
518         .init = uart0_init,
519         .cleanup = uart0_cleanup,
520         .setbaudrate = uart0_setbaudrate,
521         .setparity = uart0_setparity,
522         .enabletxirq = uart0_enabletxirq,
523 };
524
525 #ifdef AVR_HAS_UART1
526 static const struct SerialHardwareVT UART1_VT =
527 {
528         .init = uart1_init,
529         .cleanup = uart1_cleanup,
530         .setbaudrate = uart1_setbaudrate,
531         .setparity = uart1_setparity,
532         .enabletxirq = uart1_enabletxirq,
533 };
534 #endif // AVR_HAS_UART1
535
536 static const struct SerialHardwareVT SPI_VT =
537 {
538         .init = spi_init,
539         .cleanup = spi_cleanup,
540         .setbaudrate = spi_setbaudrate,
541         .setparity = spi_setparity,
542         .enabletxirq = spi_starttx,
543 };
544
545 static struct AvrSerial UARTDescs[SER_CNT] =
546 {
547         { .hw = { .table = &UART0_VT } },
548 #ifdef AVR_HAS_UART1
549         { .hw = { .table = &UART1_VT } },
550 #endif // AVR_HAS_UART1
551         { .hw = { .table = &SPI_VT } },
552 };
553
554 struct SerialHardware* ser_hw_getdesc(int unit)
555 {
556         ASSERT(unit < SER_CNT);
557         return &UARTDescs[unit].hw;
558 }