sam3n serial/spi: use correct peripheral ids and interrupt numbers; initialize correctly
[bertos.git] / bertos / cpu / cortex-m3 / drv / ser_sam3.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 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief ARM UART and SPI I/O driver
35  *
36  *
37  * \author Daniele Basile <asterix@develer.com>
38  */
39
40 #include "hw/hw_ser.h"  /* Required for bus macros overrides */
41 #include <hw/hw_cpufreq.h>  /* CPU_FREQ */
42
43 #include "cfg/cfg_ser.h"
44 #include <cfg/debug.h>
45
46
47 //#include <io/arm.h>
48 #include <io/cm3.h>
49 #include <drv/irq_cm3.h>
50
51 #include <cpu/attr.h>
52
53 #include <drv/ser.h>
54 #include <drv/ser_p.h>
55
56 #include <struct/fifobuf.h>
57
58
59 #define SERIRQ_PRIORITY 4 ///< default priority for serial irqs.
60
61 /**
62  * \name Overridable serial bus hooks
63  *
64  * These can be redefined in hw.h to implement
65  * special bus policies such as half-duplex, 485, etc.
66  *
67  *
68  * \code
69  *  TXBEGIN      TXCHAR      TXEND  TXOFF
70  *    |   __________|__________ |     |
71  *    |   |   |   |   |   |   | |     |
72  *    v   v   v   v   v   v   v v     v
73  * ______  __  __  __  __  __  __  ________________
74  *       \/  \/  \/  \/  \/  \/  \/
75  * ______/\__/\__/\__/\__/\__/\__/
76  *
77  * \endcode
78  *
79  * \{
80  */
81
82 #ifndef SER_UART0_BUS_TXINIT
83         /**
84          * Default TXINIT macro - invoked in uart0_init()
85          *
86          * - Disable GPIO on USART0 tx/rx pins
87          */
88         #if CPU_ARM_AT91 && !CPU_ARM_SAM7S_LARGE && !CPU_ARM_SAM7X
89                 #warning Check USART0 pins!
90         #endif
91         #define SER_UART0_BUS_TXINIT do { \
92                 PIOA_PDR = BV(RXD0) | BV(TXD0); \
93         } while (0)
94 #endif
95
96 #ifndef SER_UART0_BUS_TXBEGIN
97         /**
98          * Invoked before starting a transmission
99          */
100         #define SER_UART0_BUS_TXBEGIN
101 #endif
102
103 #ifndef SER_UART0_BUS_TXCHAR
104         /**
105          * Invoked to send one character.
106          */
107         #define SER_UART0_BUS_TXCHAR(c) do { \
108                 US0_THR = (c); \
109         } while (0)
110 #endif
111
112 #ifndef SER_UART0_BUS_TXEND
113         /**
114          * Invoked as soon as the txfifo becomes empty
115          */
116         #define SER_UART0_BUS_TXEND
117 #endif
118
119 /* End USART0 macros */
120
121 #if !CPU_CM3_AT91SAM3U
122
123         #ifndef SER_UART1_BUS_TXINIT
124                 /**
125                  * Default TXINIT macro - invoked in uart1_init()
126                  *
127                  * - Disable GPIO on USART1 tx/rx pins
128                  */
129                 #if CPU_ARM_AT91
130                         #if !CPU_ARM_SAM7S_LARGE && !CPU_ARM_SAM7X
131                                 #warning Check USART1 pins!
132                         #endif
133                         #define SER_UART1_BUS_TXINIT do { \
134                                 PIOA_PDR = BV(RXD1) | BV(TXD1); \
135                         } while (0)
136                 #elif CPU_CM3_AT91SAM3
137                         #define SER_UART1_BUS_TXINIT do { \
138                                 PIOB_PDR = BV(RXD1) | BV(TXD1); \
139                         } while (0)
140                 #else
141                         #error Unknown CPU
142                 #endif
143         #endif
144
145         #ifndef SER_UART1_BUS_TXBEGIN
146                 /**
147                  * Invoked before starting a transmission
148                  */
149                 #define SER_UART1_BUS_TXBEGIN
150         #endif
151
152         #ifndef SER_UART1_BUS_TXCHAR
153                 /**
154                  * Invoked to send one character.
155                  */
156                 #define SER_UART1_BUS_TXCHAR(c) do { \
157                         US1_THR = (c); \
158                 } while (0)
159         #endif
160
161         #ifndef SER_UART1_BUS_TXEND
162                 /**
163                  * Invoked as soon as the txfifo becomes empty
164                  */
165                 #define SER_UART1_BUS_TXEND
166         #endif
167
168 #endif
169
170 /**
171 * \name Overridable SPI hooks
172 *
173 * These can be redefined in hw.h to implement
174 * special bus policies such as slave select pin handling, etc.
175 *
176 * \{
177 */
178
179 #ifndef SER_SPI0_BUS_TXINIT
180         /**
181         * Default TXINIT macro - invoked in spi_init()
182         * The default is no action.
183         */
184         #if CPU_CM3_AT91SAM3
185                 #define SER_SPI0_BUS_TXINIT do { \
186                         /* Disable PIO on SPI pins */ \
187                         PIOA_PDR = BV(SPI0_SPCK) | BV(SPI0_MOSI) | BV(SPI0_MISO) | BV(30); \
188                         /* PIO is peripheral A */ \
189                         PIOA_ABCDSR1 &= ~(BV(SPI0_SPCK) | BV(SPI0_MOSI) | BV(SPI0_MISO)); \
190                         PIOA_ABCDSR2 &= ~(BV(SPI0_SPCK) | BV(SPI0_MOSI) | BV(SPI0_MISO)); \
191                         /* Peripheral B for chip select for display */ \
192                         PIOA_ABCDSR1 |= BV(30); \
193                         PIOA_ABCDSR2 &= ~BV(30); \
194                 } while (0)
195         #else
196                 #define SER_SPI0_BUS_TXINIT do { \
197                         /* Disable PIO on SPI pins */ \
198                         PIOA_PDR = BV(SPI0_SPCK) | BV(SPI0_MOSI) | BV(SPI0_MISO); \
199                 } while (0)
200         #endif
201 #endif
202
203 #ifndef SER_SPI0_BUS_TXCLOSE
204         /**
205         * Invoked after the last character has been transmitted.
206         * The default is no action.
207         */
208         #define SER_SPI0_BUS_TXCLOSE do { \
209                 /* Enable PIO on SPI pins */ \
210                 PIOA_PER = BV(SPI0_SPCK) | BV(SPI0_MOSI) | BV(SPI0_MISO); \
211         } while (0)
212 #endif
213
214 #if CPU_ARM_SAM7X
215
216         #ifndef SER_SPI1_BUS_TXINIT
217                 /**
218                 * Default TXINIT macro - invoked in spi_init()
219                 * The default is no action.
220                 */
221                 #define SER_SPI1_BUS_TXINIT do { \
222                         /* Disable PIO on SPI pins */ \
223                         PIOA_PDR = BV(SPI1_SPCK) | BV(SPI1_MOSI) | BV(SPI1_MISO); \
224                         /* SPI1 pins are on B peripheral function! */ \
225                         PIOA_BSR = BV(SPI1_SPCK) | BV(SPI1_MOSI) | BV(SPI1_MISO); \
226                 } while (0)
227         #endif
228
229         #ifndef SER_SPI1_BUS_TXCLOSE
230                 /**
231                 * Invoked after the last character has been transmitted.
232                 * The default is no action.
233                 */
234                 #define SER_SPI1_BUS_TXCLOSE do { \
235                         /* Enable PIO on SPI pins */ \
236                         PIOA_PER = BV(SPI1_SPCK) | BV(SPI1_MOSI) | BV(SPI1_MISO); \
237                 } while (0)
238         #endif
239 #endif
240 /*\}*/
241
242
243 /**
244  * \name Core dependent interrupt handling macros
245  *
246  * Atmel serial hardware is used on different CPU cores,
247  * i.e. SAM3 and SAM7.  The user interface of the serial
248  * subsystem is identical but core interrupt controllers
249  * are different.
250  *
251  * \{
252  */
253 #if CPU_ARM_AT91
254
255 INLINE void sysirq_setHandler(sysirq_t irq, sysirq_handler_t handler)
256 {
257         /* Set the vector. */
258         AIC_SVR(irq) = uart0_irq_dispatcher;
259
260         /* Initialize to level/edge sensitive with defined priority. */
261 #if CPU_ARM_SAM7X
262         if (irq == SPI0_ID || irq == SPI1_ID)
263 #else
264         if (irq == SPI0_ID)
265 #endif
266                 AIC_SMR(irq) = (AIC_SMR(irq) & ~AIC_SRCTYPE_MASK) | AIC_SRCTYPE_INT_EDGE_TRIGGERED;
267         else // USART/UART
268                 AIC_SMR(irq) = (AIC_SMR(irq) & ~AIC_SRCTYPE_MASK) | AIC_SRCTYPE_INT_LEVEL_SENSITIVE;
269
270         /* Enable IRQ */
271         AIC_IECR = BV(irq);
272 }
273
274 INLINE void sysirq_setPriority(sysirq_t irq, int prio)
275 {
276         AIC_SMR(irq) = (AIC_SMR(irq) & ~AIC_PRIOR_MASK) | SERIRQ_PRIORITY;
277 }
278
279 /** Inform hw that we have served the IRQ */
280 #define SER_INT_ACK do { \
281         AIC_EOICR = 0; \
282 } while (0)
283
284 #elif CPU_CM3_AT91SAM3
285
286 /** Inform hw that we have served the IRQ */
287 #define SER_INT_ACK do { /* nop */ } while (0)
288
289 #else
290         #error No interrupt handling macros defined for current architecture
291 #endif
292
293 /*\}*/
294
295 /* From the high-level serial driver */
296 extern struct Serial *ser_handles[SER_CNT];
297
298 /* TX and RX buffers */
299 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
300 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
301 #if !CPU_CM3_AT91SAM3U
302 static unsigned char uart1_txbuffer[CONFIG_UART1_TXBUFSIZE];
303 static unsigned char uart1_rxbuffer[CONFIG_UART1_RXBUFSIZE];
304 #endif
305 static unsigned char spi0_txbuffer[CONFIG_SPI0_TXBUFSIZE];
306 static unsigned char spi0_rxbuffer[CONFIG_SPI0_RXBUFSIZE];
307 #if CPU_ARM_SAM7X
308 static unsigned char spi1_txbuffer[CONFIG_SPI1_TXBUFSIZE];
309 static unsigned char spi1_rxbuffer[CONFIG_SPI1_RXBUFSIZE];
310 #endif
311
312 /**
313  * Internal hardware state structure
314  *
315  * The \a sending variable is true while the transmission
316  * interrupt is retriggering itself.
317  *
318  * For the USARTs the \a sending flag is useful for taking specific
319  * actions before sending a burst of data, at the start of a trasmission
320  * but not before every char sent.
321  *
322  * For the SPI, this flag is necessary because the SPI sends and receives
323  * bytes at the same time and the SPI IRQ is unique for send/receive.
324  * The only way to start transmission is to write data in SPDR (this
325  * is done by spi_starttx()). We do this *only* if a transfer is
326  * not already started.
327  */
328 struct ArmSerial
329 {
330         struct SerialHardware hw;
331         volatile bool sending;
332 };
333
334 static ISR_PROTO(uart0_irq_dispatcher);
335 #if !CPU_CM3_AT91SAM3U
336 static ISR_PROTO(uart1_irq_dispatcher);
337 #endif
338 static ISR_PROTO(spi0_irq_handler);
339 #if CPU_ARM_SAM7X
340 static ISR_PROTO(spi1_irq_handler);
341 #endif
342 /*
343  * Callbacks for USART0
344  */
345 static void uart0_init(
346         UNUSED_ARG(struct SerialHardware *, _hw),
347         UNUSED_ARG(struct Serial *, ser))
348 {
349         US0_IDR = 0xFFFFFFFF;
350         PMC_PCER = BV(US0_ID);
351
352         /*
353          * - Reset USART0
354          * - Set serial param: mode Normal, 8bit data, 1bit stop, parity none
355          * - Enable both the receiver and the transmitter
356          * - Enable only the RX complete interrupt
357          */
358         US0_CR = BV(US_RSTRX) | BV(US_RSTTX);
359         US0_MR = US_CHMODE_NORMAL | US_CHRL_8 | US_NBSTOP_1 | US_PAR_NO;
360         US0_CR = BV(US_RXEN) | BV(US_TXEN);
361         US0_IER = BV(US_RXRDY);
362
363         SER_UART0_BUS_TXINIT;
364
365         sysirq_setPriority(INT_US0, SERIRQ_PRIORITY);
366         sysirq_setHandler(INT_US0, uart0_irq_dispatcher);
367
368         SER_STROBE_INIT;
369 }
370
371 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
372 {
373         US0_CR = BV(US_RSTRX) | BV(US_RSTTX) | BV(US_RXDIS) | BV(US_TXDIS) | BV(US_RSTSTA);
374 }
375
376 static void uart0_enabletxirq(struct SerialHardware *_hw)
377 {
378         struct ArmSerial *hw = (struct ArmSerial *)_hw;
379
380         /*
381          * WARNING: racy code here!  The tx interrupt sets hw->sending to false
382          * when it runs with an empty fifo.  The order of statements in the
383          * if-block matters.
384          */
385         if (!hw->sending)
386         {
387                 hw->sending = true;
388                 /*
389                  * - Enable the transmitter
390                  * - Enable TX empty interrupt
391                  */
392                 SER_UART0_BUS_TXBEGIN;
393                 US0_IER = BV(US_TXEMPTY);
394         }
395 }
396
397 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
398 {
399         /* Compute baud-rate period */
400         US0_BRGR = CPU_FREQ / (16 * rate);
401         //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
402 }
403
404 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
405 {
406         US0_MR &= ~US_PAR_MASK;
407         /* Set UART parity */
408         switch(parity)
409         {
410                 case SER_PARITY_NONE:
411                 {
412                         /* Parity none. */
413                         US0_MR |= US_PAR_NO;
414                         break;
415                 }
416                 case SER_PARITY_EVEN:
417                 {
418                         /* Even parity. */
419                         US0_MR |= US_PAR_EVEN;
420                         break;
421                 }
422                 case SER_PARITY_ODD:
423                 {
424                         /* Odd parity. */
425                         US0_MR |= US_PAR_ODD;
426                         break;
427                 }
428                 default:
429                         ASSERT(0);
430         }
431 }
432 /*
433  * Callbacks for USART1
434  */
435 static void uart1_init(
436         UNUSED_ARG(struct SerialHardware *, _hw),
437         UNUSED_ARG(struct Serial *, ser))
438 {
439         US1_IDR = 0xFFFFFFFF;
440         PMC_PCER = BV(US1_ID);
441
442         /*
443          * - Reset USART1
444          * - Set serial param: mode Normal, 8bit data, 1bit stop, parity none
445          * - Enable both the receiver and the transmitter
446          * - Enable only the RX complete interrupt
447          */
448         US1_CR = BV(US_RSTRX) | BV(US_RSTTX);
449         US1_MR = US_CHMODE_NORMAL | US_CHRL_8 | US_NBSTOP_1 | US_PAR_NO;
450         US1_CR = BV(US_RXEN) | BV(US_TXEN);
451         US1_IER = BV(US_RXRDY);
452
453         SER_UART1_BUS_TXINIT;
454
455         sysirq_setPriority(INT_US1, SERIRQ_PRIORITY);
456         sysirq_setHandler(INT_US1, uart1_irq_dispatcher);
457
458         SER_STROBE_INIT;
459 }
460
461 static void uart1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
462 {
463         US1_CR = BV(US_RSTRX) | BV(US_RSTTX) | BV(US_RXDIS) | BV(US_TXDIS) | BV(US_RSTSTA);
464 }
465
466 static void uart1_enabletxirq(struct SerialHardware *_hw)
467 {
468         struct ArmSerial *hw = (struct ArmSerial *)_hw;
469
470         /*
471          * WARNING: racy code here!  The tx interrupt sets hw->sending to false
472          * when it runs with an empty fifo.  The order of statements in the
473          * if-block matters.
474          */
475         if (!hw->sending)
476         {
477                 hw->sending = true;
478                 /*
479                  * - Enable the transmitter
480                  * - Enable TX empty interrupt
481                  */
482                 SER_UART1_BUS_TXBEGIN;
483                 US1_IER = BV(US_TXEMPTY);
484         }
485 }
486
487 static void uart1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
488 {
489         /* Compute baud-rate period */
490         US1_BRGR = CPU_FREQ / (16 * rate);
491         //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
492 }
493
494 static void uart1_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
495 {
496         US1_MR &= ~US_PAR_MASK;
497         /* Set UART parity */
498         switch(parity)
499         {
500                 case SER_PARITY_NONE:
501                 {
502                         /* Parity none. */
503                         US1_MR |= US_PAR_NO;
504                         break;
505                 }
506                 case SER_PARITY_EVEN:
507                 {
508                         /* Even parity. */
509                         US1_MR |= US_PAR_EVEN;
510                         break;
511                 }
512                 case SER_PARITY_ODD:
513                 {
514                         /* Odd parity. */
515                         US1_MR |= US_PAR_ODD;
516                         break;
517                 }
518                 default:
519                         ASSERT(0);
520         }
521 }
522
523 /* SPI driver */
524 static void spi0_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
525 {
526         SER_SPI0_BUS_TXINIT;
527
528         /* Reset device */
529         SPI0_CR = BV(SPI_SWRST);
530
531         /*
532          * Set SPI to master mode, fixed peripheral select, chip select directly connected to a peripheral device,
533          * SPI clock set to MCK, mode fault detection disabled, loopback disable, NPCS0 active, Delay between CS = 0
534          */
535         SPI0_MR = BV(SPI_MSTR) | BV(SPI_MODFDIS); // | SPI_PCS_2;
536
537         /*
538          * Set SPI mode.
539          * At reset clock division factor is set to 0, that is
540          * *forbidden*. Set SPI clock to minimum to keep it valid.
541          */
542         SPI0_CSR0 = BV(SPI_NCPHA) | (255 << SPI_SCBR_SHIFT);
543
544         /* Disable all irqs */
545         SPI0_IDR = 0xFFFFFFFF;
546
547         //sysirq_setPriority(INT_SPI0, SERIRQ_PRIORITY);
548         sysirq_setHandler(INT_SPI0, spi0_irq_handler);
549         PMC_PCER = BV(SPI0_ID);
550
551         /* Enable interrupt on tx buffer empty */
552         SPI0_IER = BV(SPI_TXEMPTY);
553
554         /* Enable SPI */
555         SPI0_CR = BV(SPI_SPIEN);
556
557         SER_STROBE_INIT;
558 }
559
560 static void spi0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
561 {
562         /* Disable SPI */
563         SPI0_CR = BV(SPI_SPIDIS);
564
565         /* Disable all irqs */
566         SPI0_IDR = 0xFFFFFFFF;
567
568         SER_SPI0_BUS_TXCLOSE;
569 }
570
571 static void spi0_starttx(struct SerialHardware *_hw)
572 {
573         struct ArmSerial *hw = (struct ArmSerial *)_hw;
574
575         cpu_flags_t flags;
576         IRQ_SAVE_DISABLE(flags);
577
578         /* Send data only if the SPI is not already transmitting */
579         if (!hw->sending && !fifo_isempty(&ser_handles[SER_SPI0]->txfifo))
580         {
581                 hw->sending = true;
582                 SPI0_TDR = fifo_pop(&ser_handles[SER_SPI0]->txfifo);
583         }
584
585         IRQ_RESTORE(flags);
586 }
587
588 static void spi0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
589 {
590         SPI0_CSR0 &= ~SPI_SCBR;
591
592         ASSERT((uint8_t)DIV_ROUND(CPU_FREQ, rate));
593         SPI0_CSR0 |= DIV_ROUND(CPU_FREQ, rate) << SPI_SCBR_SHIFT;
594 }
595
596 #if CPU_ARM_SAM7X
597 /* SPI driver */
598 static void spi1_init(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(struct Serial *, ser))
599 {
600         SER_SPI1_BUS_TXINIT;
601
602         /* Reset device */
603         SPI1_CR = BV(SPI_SWRST);
604
605         /*
606          * Set SPI to master mode, fixed peripheral select, chip select directly connected to a peripheral device,
607          * SPI clock set to MCK, mode fault detection disabled, loopback disable, NPCS0 active, Delay between CS = 0
608          */
609         SPI1_MR = BV(SPI_MSTR) | BV(SPI_MODFDIS);
610
611         /*
612          * Set SPI mode.
613          * At reset clock division factor is set to 0, that is
614          * *forbidden*. Set SPI clock to minimum to keep it valid.
615          */
616         SPI1_CSR0 = BV(SPI_NCPHA) | (255 << SPI_SCBR_SHIFT);
617
618         /* Disable all SPI irqs */
619         SPI1_IDR = 0xFFFFFFFF;
620
621         sysirq_setPriority(INT_SPI1, SERIRQ_PRIORITY);
622         sysirq_setHandler(INT_SPI1, spi1_irq_dispatcher);
623         PMC_PCER = BV(SPI1_ID);
624
625         /* Enable interrupt on tx buffer empty */
626         SPI1_IER = BV(SPI_TXEMPTY);
627
628         /* Enable SPI */
629         SPI1_CR = BV(SPI_SPIEN);
630
631         SER_STROBE_INIT;
632 }
633
634 static void spi1_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
635 {
636         /* Disable SPI */
637         SPI1_CR = BV(SPI_SPIDIS);
638
639         /* Disable all irqs */
640         SPI1_IDR = 0xFFFFFFFF;
641
642         SER_SPI1_BUS_TXCLOSE;
643 }
644
645 static void spi1_starttx(struct SerialHardware *_hw)
646 {
647         struct ArmSerial *hw = (struct ArmSerial *)_hw;
648
649         cpu_flags_t flags;
650         IRQ_SAVE_DISABLE(flags);
651
652         /* Send data only if the SPI is not already transmitting */
653         if (!hw->sending && !fifo_isempty(&ser_handles[SER_SPI1]->txfifo))
654         {
655                 hw->sending = true;
656                 SPI1_TDR = fifo_pop(&ser_handles[SER_SPI1]->txfifo);
657         }
658
659         IRQ_RESTORE(flags);
660 }
661
662 static void spi1_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
663 {
664         SPI1_CSR0 &= ~SPI_SCBR;
665
666         ASSERT((uint8_t)DIV_ROUND(CPU_FREQ, rate));
667         SPI1_CSR0 |= DIV_ROUND(CPU_FREQ, rate) << SPI_SCBR_SHIFT;
668 }
669 #endif
670
671 static void spi_setparity(UNUSED_ARG(struct SerialHardware *, _hw), UNUSED_ARG(int, parity))
672 {
673         // nop
674 }
675
676
677 static bool tx_sending(struct SerialHardware* _hw)
678 {
679         struct ArmSerial *hw = (struct ArmSerial *)_hw;
680         return hw->sending;
681 }
682
683 // FIXME: move into compiler.h?  Ditch?
684 #if COMPILER_C99
685         #define C99INIT(name,val) .name = val
686 #elif defined(__GNUC__)
687         #define C99INIT(name,val) name: val
688 #else
689         #warning No designated initializers, double check your code
690         #define C99INIT(name,val) (val)
691 #endif
692
693 /*
694  * High-level interface data structures
695  */
696 static const struct SerialHardwareVT UART0_VT =
697 {
698         C99INIT(init, uart0_init),
699         C99INIT(cleanup, uart0_cleanup),
700         C99INIT(setBaudrate, uart0_setbaudrate),
701         C99INIT(setParity, uart0_setparity),
702         C99INIT(txStart, uart0_enabletxirq),
703         C99INIT(txSending, tx_sending),
704 };
705
706 static const struct SerialHardwareVT UART1_VT =
707 {
708         C99INIT(init, uart1_init),
709         C99INIT(cleanup, uart1_cleanup),
710         C99INIT(setBaudrate, uart1_setbaudrate),
711         C99INIT(setParity, uart1_setparity),
712         C99INIT(txStart, uart1_enabletxirq),
713         C99INIT(txSending, tx_sending),
714 };
715
716 static const struct SerialHardwareVT SPI0_VT =
717 {
718         C99INIT(init, spi0_init),
719         C99INIT(cleanup, spi0_cleanup),
720         C99INIT(setBaudrate, spi0_setbaudrate),
721         C99INIT(setParity, spi_setparity),
722         C99INIT(txStart, spi0_starttx),
723         C99INIT(txSending, tx_sending),
724 };
725 #if CPU_ARM_SAM7X
726 static const struct SerialHardwareVT SPI1_VT =
727 {
728         C99INIT(init, spi1_init),
729         C99INIT(cleanup, spi1_cleanup),
730         C99INIT(setBaudrate, spi1_setbaudrate),
731         C99INIT(setParity, spi_setparity),
732         C99INIT(txStart, spi1_starttx),
733         C99INIT(txSending, tx_sending),
734 };
735 #endif
736
737 static struct ArmSerial UARTDescs[SER_CNT] =
738 {
739         {
740                 C99INIT(hw, /**/) {
741                         C99INIT(table, &UART0_VT),
742                         C99INIT(txbuffer, uart0_txbuffer),
743                         C99INIT(rxbuffer, uart0_rxbuffer),
744                         C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
745                         C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
746                 },
747                 C99INIT(sending, false),
748         },
749         {
750                 C99INIT(hw, /**/) {
751                         C99INIT(table, &UART1_VT),
752                         C99INIT(txbuffer, uart1_txbuffer),
753                         C99INIT(rxbuffer, uart1_rxbuffer),
754                         C99INIT(txbuffer_size, sizeof(uart1_txbuffer)),
755                         C99INIT(rxbuffer_size, sizeof(uart1_rxbuffer)),
756                 },
757                 C99INIT(sending, false),
758         },
759
760         {
761                 C99INIT(hw, /**/) {
762                         C99INIT(table, &SPI0_VT),
763                         C99INIT(txbuffer, spi0_txbuffer),
764                         C99INIT(rxbuffer, spi0_rxbuffer),
765                         C99INIT(txbuffer_size, sizeof(spi0_txbuffer)),
766                         C99INIT(rxbuffer_size, sizeof(spi0_rxbuffer)),
767                 },
768                 C99INIT(sending, false),
769         },
770         #if CPU_ARM_SAM7X
771         {
772                 C99INIT(hw, /**/) {
773                         C99INIT(table, &SPI1_VT),
774                         C99INIT(txbuffer, spi1_txbuffer),
775                         C99INIT(rxbuffer, spi1_rxbuffer),
776                         C99INIT(txbuffer_size, sizeof(spi1_txbuffer)),
777                         C99INIT(rxbuffer_size, sizeof(spi1_rxbuffer)),
778                 },
779                 C99INIT(sending, false),
780         }
781
782         #endif
783 };
784
785 struct SerialHardware *ser_hw_getdesc(int unit)
786 {
787         ASSERT(unit < SER_CNT);
788         return &UARTDescs[unit].hw;
789 }
790
791 /**
792  * Serial 0 TX interrupt handler
793  */
794 INLINE void uart0_irq_tx(void)
795 {
796         SER_STROBE_ON;
797
798         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART0]->txfifo;
799
800         if (fifo_isempty(txfifo))
801         {
802                 /*
803                  * - Disable the TX empty interrupts
804                  */
805                 US0_IDR = BV(US_TXEMPTY);
806                 SER_UART0_BUS_TXEND;
807                 UARTDescs[SER_UART0].sending = false;
808         }
809         else
810         {
811                 char c = fifo_pop(txfifo);
812                 SER_UART0_BUS_TXCHAR(c);
813         }
814
815         SER_STROBE_OFF;
816 }
817
818 /**
819  * Serial 0 RX complete interrupt handler.
820  */
821 INLINE void uart0_irq_rx(void)
822 {
823         SER_STROBE_ON;
824
825         /* Should be read before US_CRS */
826         ser_handles[SER_UART0]->status |= US0_CSR & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
827         US0_CR = BV(US_RSTSTA);
828
829         char c = US0_RHR;
830         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART0]->rxfifo;
831
832         if (fifo_isfull(rxfifo))
833                 ser_handles[SER_UART0]->status |= SERRF_RXFIFOOVERRUN;
834         else
835                 fifo_push(rxfifo, c);
836
837         SER_STROBE_OFF;
838 }
839
840 /**
841  * Serial IRQ dispatcher for USART0.
842  */
843 static DECLARE_ISR(uart0_irq_dispatcher)
844 {
845         if (US0_CSR & BV(US_RXRDY))
846                 uart0_irq_rx();
847
848         if (US0_CSR & BV(US_TXEMPTY))
849                 uart0_irq_tx();
850
851         SER_INT_ACK;
852 }
853
854 /**
855  * Serial 1 TX interrupt handler
856  */
857 INLINE void uart1_irq_tx(void)
858 {
859         SER_STROBE_ON;
860
861         struct FIFOBuffer * const txfifo = &ser_handles[SER_UART1]->txfifo;
862
863         if (fifo_isempty(txfifo))
864         {
865                 /*
866                  * - Disable the TX empty interrupts
867                  */
868                 US1_IDR = BV(US_TXEMPTY);
869                 SER_UART1_BUS_TXEND;
870                 UARTDescs[SER_UART1].sending = false;
871         }
872         else
873         {
874                 char c = fifo_pop(txfifo);
875                 SER_UART1_BUS_TXCHAR(c);
876         }
877
878         SER_STROBE_OFF;
879 }
880
881 /**
882  * Serial 1 RX complete interrupt handler.
883  */
884 INLINE void uart1_irq_rx(void)
885 {
886         SER_STROBE_ON;
887
888         /* Should be read before US_CRS */
889         ser_handles[SER_UART1]->status |= US1_CSR & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
890         US1_CR = BV(US_RSTSTA);
891
892         char c = US1_RHR;
893         struct FIFOBuffer * const rxfifo = &ser_handles[SER_UART1]->rxfifo;
894
895         if (fifo_isfull(rxfifo))
896                 ser_handles[SER_UART1]->status |= SERRF_RXFIFOOVERRUN;
897         else
898                 fifo_push(rxfifo, c);
899
900         SER_STROBE_OFF;
901 }
902
903 /**
904  * Serial IRQ dispatcher for USART1.
905  */
906 static DECLARE_ISR(uart1_irq_dispatcher)
907 {
908         if (US1_CSR & BV(US_RXRDY))
909                 uart1_irq_rx();
910
911         if (US1_CSR & BV(US_TXEMPTY))
912                 uart1_irq_tx();
913
914         SER_INT_ACK;
915 }
916
917 /**
918  * SPI0 interrupt handler
919  */
920 static DECLARE_ISR(spi0_irq_handler)
921 {
922         SER_STROBE_ON;
923
924         char c = SPI0_RDR;
925         /* Read incoming byte. */
926         if (!fifo_isfull(&ser_handles[SER_SPI0]->rxfifo))
927                 fifo_push(&ser_handles[SER_SPI0]->rxfifo, c);
928         /*
929          * FIXME
930         else
931                 ser_handles[SER_SPI0]->status |= SERRF_RXFIFOOVERRUN;
932         */
933
934         /* Send */
935         if (!fifo_isempty(&ser_handles[SER_SPI0]->txfifo))
936                 SPI0_TDR = fifo_pop(&ser_handles[SER_SPI0]->txfifo);
937         else
938                 UARTDescs[SER_SPI0].sending = false;
939
940         SER_INT_ACK;
941
942         SER_STROBE_OFF;
943 }
944
945
946 #if CPU_ARM_SAM7X
947 /**
948  * SPI1 interrupt handler
949  */
950 static DECLARE_ISR(spi1_irq_handler)
951 {
952         SER_STROBE_ON;
953
954         char c = SPI1_RDR;
955         /* Read incoming byte. */
956         if (!fifo_isfull(&ser_handles[SER_SPI1]->rxfifo))
957                 fifo_push(&ser_handles[SER_SPI1]->rxfifo, c);
958         /*
959          * FIXME
960         else
961                 ser_handles[SER_SPI1]->status |= SERRF_RXFIFOOVERRUN;
962         */
963
964         /* Send */
965         if (!fifo_isempty(&ser_handles[SER_SPI1]->txfifo))
966                 SPI1_TDR = fifo_pop(&ser_handles[SER_SPI1]->txfifo);
967         else
968                 UARTDescs[SER_SPI1].sending = false;
969
970         SER_INT_ACK;
971
972         SER_STROBE_OFF;
973 }
974 #endif