Remove debug message. Define serial priority irqs.
[bertos.git] / cpu / arm / drv / ser_at91.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000 Bernardo Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief ARM UART and SPI I/O driver
35  *
36  *
37  * \version $Id: ser_amr.c 18280 2007-10-11 15:14:20Z asterix $
38  * \author Daniele Basile <asterix@develer.com>
39  */
40
41 #include <io/arm.h>
42
43 // #include "ser_at91.h"
44 #include <drv/ser.h>
45 #include <drv/ser_p.h>
46
47 #include <hw/hw_ser.h>  /* Required for bus macros overrides */
48 #include <hw/hw_cpu.h>  /* CLOCK_FREQ */
49
50 #include <mware/fifobuf.h>
51 #include <cfg/debug.h>
52
53 #include <appconfig.h>
54
55 #define SERIRQ_PRIORITY 4 ///< default priority for serial irqs.
56
57 /**
58  * \name Overridable serial bus hooks
59  *
60  * These can be redefined in hw.h to implement
61  * special bus policies such as half-duplex, 485, etc.
62  *
63  *
64  * \code
65  *  TXBEGIN      TXCHAR      TXEND  TXOFF
66  *    |   __________|__________ |     |
67  *    |   |   |   |   |   |   | |     |
68  *    v   v   v   v   v   v   v v     v
69  * ______  __  __  __  __  __  __  ________________
70  *       \/  \/  \/  \/  \/  \/  \/
71  * ______/\__/\__/\__/\__/\__/\__/
72  *
73  * \endcode
74  *
75  * \{
76  */
77
78 #ifndef SER_UART0_IRQ_INIT
79         /**
80          * Default IRQ INIT macro - invoked in uart0_init()
81          *
82          * - Disable all interrupt
83          * - Register USART0 interrupt
84          * - Enable USART0 clock.
85          */
86         #define SER_UART0_IRQ_INIT do { \
87                 US0_IDR = 0xFFFFFFFF; \
88                 /* Set the vector. */ \
89                 AIC_SVR(US0_ID) = uart0_irq_dispatcher; \
90                 /* Initialize to edge triggered with defined priority. */ \
91                 AIC_SMR(US0_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED | SERIRQ_PRIORITY; \
92                 /* Enable the USART IRQ */ \
93                 AIC_IECR = BV(US0_ID); \
94                 PMC_PCER = BV(US0_ID); \
95         } while (0)
96 #endif
97
98 #ifndef SER_UART0_BUS_TXINIT
99         /**
100          * Default TXINIT macro - invoked in uart0_init()
101          *
102          * - Disable GPIO on USART0 tx/rx pins
103          * - Reset USART0
104          * - Set serial param: mode Normal, 8bit data, 1bit stop
105          * - Enable both the receiver and the transmitter
106          * - Enable only the RX complete interrupt
107          */
108         #if CPU_ARM_AT91
109                 #define SER_UART0_BUS_TXINIT do { \
110                         PIOA_PDR = BV(5) | BV(6); \
111                         US0_CR = BV(US_RSTRX) | BV(US_RSTTX); \
112                         US0_MR = US_CHMODE_NORMAL | US_CHRL_8 | US_NBSTOP_1; \
113                         US0_CR = BV(US_RXEN) | BV(US_TXEN); \
114                         US0_IER = BV(US_RXRDY); \
115                 } while (0)
116         /*#elif  Add other ARM families here */
117         #else
118                 #error Unknown CPU
119         #endif
120
121 #endif
122
123 #ifndef SER_UART0_BUS_TXBEGIN
124         /**
125          * Invoked before starting a transmission
126          *
127          * - Enable both the receiver and the transmitter
128          * - Enable both the RX complete and TX empty interrupts
129          */
130         #define SER_UART0_BUS_TXBEGIN do { \
131                 US0_CR = BV(US_RXEN) | BV(US_TXEN); \
132                 US0_IER = BV(US_TXRDY) | BV(US_RXRDY); \
133         } while (0)
134 #endif
135
136 #ifndef SER_UART0_BUS_TXCHAR
137         /**
138          * Invoked to send one character.
139          */
140         #define SER_UART0_BUS_TXCHAR(c) do { \
141                 US0_THR = (c); \
142         } while (0)
143 #endif
144
145 #ifndef SER_UART0_BUS_TXEND
146         /**
147          * Invoked as soon as the txfifo becomes empty
148          *
149          * - Keep both the receiver and the transmitter enabled
150          * - Keep the RX complete interrupt enabled
151          * - Disable the TX empty interrupts
152          */
153         #define SER_UART0_BUS_TXEND do { \
154                 US0_CR = BV(US_RXEN) | BV(US_TXEN); \
155                 US0_IER = BV(US_RXRDY); \
156                 US0_IDR = BV(US_TXRDY); \
157         } while (0)
158 #endif
159
160 /* End USART0 macros */
161
162 #ifndef SER_UART1_IRQ_INIT
163         /** \sa SER_UART0_BUS_TXINIT */
164         #define SER_UART1_IRQ_INIT do { \
165                 US1_IDR = 0xFFFFFFFF; \
166                 /* Set the vector. */ \
167                 AIC_SVR(US1_ID) = uart1_irq_dispatcher; \
168                 /* Initialize to edge triggered with defined priority. */ \
169                 AIC_SMR(US1_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED | SERIRQ_PRIORITY; \
170                 /* Enable the USART IRQ */ \
171                 AIC_IECR = BV(US1_ID); \
172                 PMC_PCER = BV(US1_ID); \
173         } while (0)
174 #endif
175
176 #ifndef SER_UART1_BUS_TXINIT
177         /** \sa SER_UART1_BUS_TXINIT */
178         #if CPU_ARM_AT91
179                 #define SER_UART1_BUS_TXINIT do { \
180                         PIOA_PDR = BV(21) | BV(22); \
181                         US1_CR = BV(US_RSTRX) | BV(US_RSTTX); \
182                         US1_MR = US_CHMODE_NORMAL | US_CHRL_8 | US_NBSTOP_1; \
183                         US1_CR = BV(US_RXEN) | BV(US_TXEN); \
184                         US1_IER = BV(US_RXRDY); \
185                 } while (0)
186         /*#elif  Add other ARM families here */
187         #else
188                 #error Unknown CPU
189         #endif
190
191 #endif
192
193 #ifndef SER_UART1_BUS_TXBEGIN
194         /** \sa SER_UART1_BUS_TXBEGIN */
195         #define SER_UART1_BUS_TXBEGIN do { \
196                 US1_CR = BV(US_RXEN) | BV(US_TXEN); \
197                 US1_IER = BV(US_TXRDY) | BV(US_RXRDY); \
198         } while (0)
199 #endif
200
201 #ifndef SER_UART1_BUS_TXCHAR
202         /** \sa SER_UART1_BUS_TXCHAR */
203         #define SER_UART1_BUS_TXCHAR(c) do { \
204                 US1_THR = (c); \
205         } while (0)
206 #endif
207
208 #ifndef SER_UART1_BUS_TXEND
209         /** \sa SER_UART1_BUS_TXEND */
210         #define SER_UART1_BUS_TXEND do { \
211                 US1_CR = BV(US_RXEN) | BV(US_TXEN); \
212                 US1_IER = BV(US_RXRDY); \
213                 US1_IDR = BV(US_TXRDY); \
214         } while (0)
215 #endif
216
217 /**
218  * \def CONFIG_SER_STROBE
219  *
220  * This is a debug facility that can be used to
221  * monitor SER interrupt activity on an external pin.
222  *
223  * To use strobes, redefine the macros SER_STROBE_ON,
224  * SER_STROBE_OFF and SER_STROBE_INIT and set
225  * CONFIG_SER_STROBE to 1.
226  */
227 #if !defined(CONFIG_SER_STROBE) || !CONFIG_SER_STROBE
228         #define SER_STROBE_ON    do {/*nop*/} while(0)
229         #define SER_STROBE_OFF   do {/*nop*/} while(0)
230         #define SER_STROBE_INIT  do {/*nop*/} while(0)
231 #endif
232
233
234 /* From the high-level serial driver */
235 extern struct Serial ser_handles[SER_CNT];
236
237 /* TX and RX buffers */
238 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
239 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
240
241 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
242 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
243
244 /**
245  * Internal hardware state structure
246  *
247  * The \a sending variable is true while the transmission
248  * interrupt is retriggering itself.
249  *
250  * For the USARTs the \a sending flag is useful for taking specific
251  * actions before sending a burst of data, at the start of a trasmission
252  * but not before every char sent.
253  *
254  * For the SPI, this flag is necessary because the SPI sends and receives
255  * bytes at the same time and the SPI IRQ is unique for send/receive.
256  * The only way to start transmission is to write data in SPDR (this
257  * is done by spi_starttx()). We do this *only* if a transfer is
258  * not already started.
259  */
260 struct ArmSerial
261 {
262         struct SerialHardware hw;
263         volatile bool sending;
264 };
265
266
267 /*
268  * These are to trick GCC into *not* using absolute addressing mode
269  * when accessing ser_handles, which is very expensive.
270  *
271  * Accessing through these pointers generates much shorter
272  * (and hopefully faster) code.
273  */
274 struct Serial *ser_uart0 = &ser_handles[SER_UART0];
275 struct Serial *ser_uart1 = &ser_handles[SER_UART1];
276
277 /**
278  * Serial 0 TX interrupt handler
279  */
280 static void uart0_irq_tx(void)
281 {
282         SER_STROBE_ON;
283
284         struct FIFOBuffer * const txfifo = &ser_uart0->txfifo;
285
286         if (fifo_isempty(txfifo))
287         {
288                 SER_UART0_BUS_TXEND;
289         }
290         else
291         {
292                 char c = fifo_pop(txfifo);
293                 SER_UART0_BUS_TXCHAR(c);
294         }
295
296         SER_STROBE_OFF;
297 }
298
299 /**
300  * Serial 0 RX complete interrupt handler.
301  */
302 static void uart0_irq_rx(void)
303 {
304         SER_STROBE_ON;
305
306         /* Should be read before US_CRS */
307         ser_uart0->status |= US0_CSR & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
308
309         char c = US0_RHR;
310         struct FIFOBuffer * const rxfifo = &ser_uart0->rxfifo;
311
312         if (fifo_isfull(rxfifo))
313                 ser_uart0->status |= SERRF_RXFIFOOVERRUN;
314         else
315                 fifo_push(rxfifo, c);
316
317         SER_STROBE_OFF;
318 }
319
320 /**
321  * Serial IRQ dispatcher for USART0.
322  */
323 static void uart0_irq_dispatcher(void) __attribute__ ((naked));
324 static void uart0_irq_dispatcher(void)
325 {
326         IRQ_ENTRY();
327
328         if (US0_IMR & BV(US_RXRDY))
329                 uart0_irq_rx();
330
331         if (US0_IMR & BV(US_TXRDY))
332                 uart0_irq_tx();
333
334         IRQ_EXIT();
335 }
336
337 /**
338  * Serial 1 TX interrupt handler
339  */
340 static void uart1_irq_tx(void)
341 {
342         SER_STROBE_ON;
343
344         struct FIFOBuffer * const txfifo = &ser_uart1->txfifo;
345
346         if (fifo_isempty(txfifo))
347         {
348                 SER_UART1_BUS_TXEND;
349         }
350         else
351         {
352                 char c = fifo_pop(txfifo);
353                 SER_UART1_BUS_TXCHAR(c);
354         }
355
356         SER_STROBE_OFF;
357 }
358
359 /**
360  * Serial 1 RX complete interrupt handler.
361  */
362 static void uart1_irq_rx(void)
363 {
364         SER_STROBE_ON;
365
366         /* Should be read before US_CRS */
367         ser_uart1->status |= US1_CSR & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
368
369         char c = US1_RHR;
370         struct FIFOBuffer * const rxfifo = &ser_uart1->rxfifo;
371
372         if (fifo_isfull(rxfifo))
373                 ser_uart1->status |= SERRF_RXFIFOOVERRUN;
374         else
375                 fifo_push(rxfifo, c);
376
377         SER_STROBE_OFF;
378 }
379
380 /**
381  * Serial IRQ dispatcher for USART1.
382  */
383 static void uart1_irq_dispatcher(void) __attribute__ ((naked));
384 static void uart1_irq_dispatcher(void)
385 {
386         IRQ_ENTRY();
387
388         if (US1_IMR & BV(US_RXRDY))
389                 uart1_irq_rx();
390
391         if (US1_IMR & BV(US_TXRDY))
392                 uart1_irq_tx();
393
394         IRQ_EXIT();
395 }
396 /*
397  * Callbacks for USART0
398  */
399 static void uart0_init(
400         UNUSED_ARG(struct SerialHardware *, _hw),
401         UNUSED_ARG(struct Serial *, ser))
402 {
403         SER_UART0_IRQ_INIT;
404         SER_UART0_BUS_TXINIT;
405         SER_STROBE_INIT;
406 }
407
408 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
409 {
410         US0_CR = BV(US_RSTRX) | BV(US_RSTTX) | BV(US_RXDIS) | BV(US_TXDIS) | BV(US_RSTSTA);
411 }
412
413 static void uart0_enabletxirq(struct SerialHardware *_hw)
414 {
415         struct ArmSerial *hw = (struct ArmSerial *)_hw;
416
417         /*
418          * WARNING: racy code here!  The tx interrupt sets hw->sending to false
419          * when it runs with an empty fifo.  The order of statements in the
420          * if-block matters.
421          */
422         if (!hw->sending)
423         {
424                 hw->sending = true;
425                 SER_UART0_BUS_TXBEGIN;
426         }
427 }
428
429 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
430 {
431         /* Compute baud-rate period */
432         US0_BRGR = CLOCK_FREQ / (16 * rate);
433         //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
434 }
435
436 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
437 {
438         /* Set UART parity */
439         switch(parity)
440         {
441                 case SER_PARITY_NONE:
442                 {
443             /* Parity mode. */
444                         US0_MR |= US_PAR_MASK;
445                         break;
446                 }
447                 case SER_PARITY_EVEN:
448                 {
449             /* Even parity.*/
450                         US0_MR |= US_PAR_EVEN;
451                         break;
452                 }
453                 case SER_PARITY_ODD:
454                 {
455             /* Odd parity.*/
456                         US0_MR |= US_PAR_ODD;
457                         break;
458                 }
459         }
460
461 }
462 /*
463  * Callbacks for USART1
464  */
465 static void uart1_init(
466         UNUSED_ARG(struct SerialHardware *, _hw),
467         UNUSED_ARG(struct Serial *, ser))
468 {
469         SER_UART1_IRQ_INIT;
470         SER_UART1_BUS_TXINIT;
471         SER_STROBE_INIT;
472 }
473
474 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
475 {
476         US1_CR = BV(US_RSTRX) | BV(US_RSTTX) | BV(US_RXDIS) | BV(US_TXDIS) | BV(US_RSTSTA);
477 }
478
479 static void uart1_enabletxirq(struct SerialHardware *_hw)
480 {
481         struct ArmSerial *hw = (struct ArmSerial *)_hw;
482
483         /*
484          * WARNING: racy code here!  The tx interrupt sets hw->sending to false
485          * when it runs with an empty fifo.  The order of statements in the
486          * if-block matters.
487          */
488         if (!hw->sending)
489         {
490                 hw->sending = true;
491                 SER_UART1_BUS_TXBEGIN;
492         }
493 }
494
495 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
496 {
497         /* Compute baud-rate period */
498         US1_BRGR = CLOCK_FREQ / (16 * rate);
499         //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
500 }
501
502 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
503 {
504         /* Set UART parity */
505         switch(parity)
506         {
507                 case SER_PARITY_NONE:
508                 {
509             /* Parity mode. */
510                         US1_MR |= US_PAR_MASK;
511                         break;
512                 }
513                 case SER_PARITY_EVEN:
514                 {
515             /* Even parity.*/
516                         US1_MR |= US_PAR_EVEN;
517                         break;
518                 }
519                 case SER_PARITY_ODD:
520                 {
521             /* Odd parity.*/
522                         US1_MR |= US_PAR_ODD;
523                         break;
524                 }
525         }
526
527 }
528
529 static bool tx_sending(struct SerialHardware* _hw)
530 {
531         struct ArmSerial *hw = (struct ArmSerial *)_hw;
532         return hw->sending;
533 }
534
535 // FIXME: move into compiler.h?  Ditch?
536 #if COMPILER_C99
537         #define C99INIT(name,val) .name = val
538 #elif defined(__GNUC__)
539         #define C99INIT(name,val) name: val
540 #else
541         #warning No designated initializers, double check your code
542         #define C99INIT(name,val) (val)
543 #endif
544
545 /*
546  * High-level interface data structures
547  */
548 static const struct SerialHardwareVT UART0_VT =
549 {
550         C99INIT(init, uart0_init),
551         C99INIT(cleanup, uart0_cleanup),
552         C99INIT(setBaudrate, uart0_setbaudrate),
553         C99INIT(setParity, uart0_setparity),
554         C99INIT(txStart, uart0_enabletxirq),
555         C99INIT(txSending, tx_sending),
556 };
557
558 static const struct SerialHardwareVT UART1_VT =
559 {
560         C99INIT(init, uart1_init),
561         C99INIT(cleanup, uart1_cleanup),
562         C99INIT(setBaudrate, uart1_setbaudrate),
563         C99INIT(setParity, uart1_setparity),
564         C99INIT(txStart, uart1_enabletxirq),
565         C99INIT(txSending, tx_sending),
566 };
567
568 static struct ArmSerial UARTDescs[SER_CNT] =
569 {
570         {
571                 C99INIT(hw, /**/) {
572                         C99INIT(table, &UART0_VT),
573                         C99INIT(txbuffer, uart0_txbuffer),
574                         C99INIT(rxbuffer, uart0_rxbuffer),
575                         C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
576                         C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
577                 },
578                 C99INIT(sending, false),
579         },
580         {
581                 C99INIT(hw, /**/) {
582                         C99INIT(table, &UART1_VT),
583                         C99INIT(txbuffer, uart1_txbuffer),
584                         C99INIT(rxbuffer, uart1_rxbuffer),
585                         C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
586                         C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
587                 },
588                 C99INIT(sending, false),
589         }
590 };
591
592 struct SerialHardware *ser_hw_getdesc(int unit)
593 {
594         ASSERT(unit < SER_CNT);
595         return &UARTDescs[unit].hw;
596 }