Fix bug in set serial param; remove some unneeded reg set in init function.
[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
56 /**
57  * \name Overridable serial bus hooks
58  *
59  * These can be redefined in hw.h to implement
60  * special bus policies such as half-duplex, 485, etc.
61  *
62  *
63  * \code
64  *  TXBEGIN      TXCHAR      TXEND  TXOFF
65  *    |   __________|__________ |     |
66  *    |   |   |   |   |   |   | |     |
67  *    v   v   v   v   v   v   v v     v
68  * ______  __  __  __  __  __  __  ________________
69  *       \/  \/  \/  \/  \/  \/  \/
70  * ______/\__/\__/\__/\__/\__/\__/
71  *
72  * \endcode
73  *
74  * \{
75  */
76
77
78
79 /**
80  * \def CONFIG_SER_STROBE
81  *
82  * This is a debug facility that can be used to
83  * monitor SER interrupt activity on an external pin.
84  *
85  * To use strobes, redefine the macros SER_STROBE_ON,
86  * SER_STROBE_OFF and SER_STROBE_INIT and set
87  * CONFIG_SER_STROBE to 1.
88  */
89 #if !defined(CONFIG_SER_STROBE) || !CONFIG_SER_STROBE
90         #define SER_STROBE_ON    do {/*nop*/} while(0)
91         #define SER_STROBE_OFF   do {/*nop*/} while(0)
92         #define SER_STROBE_INIT  do {/*nop*/} while(0)
93 #endif
94
95
96 /* From the high-level serial driver */
97 extern struct Serial ser_handles[SER_CNT];
98
99 /* TX and RX buffers */
100 static unsigned char uart0_txbuffer[CONFIG_UART0_TXBUFSIZE];
101 static unsigned char uart0_rxbuffer[CONFIG_UART0_RXBUFSIZE];
102
103 /**
104  * Internal hardware state structure
105  *
106  * The \a sending variable is true while the transmission
107  * interrupt is retriggering itself.
108  *
109  * For the USARTs the \a sending flag is useful for taking specific
110  * actions before sending a burst of data, at the start of a trasmission
111  * but not before every char sent.
112  *
113  * For the SPI, this flag is necessary because the SPI sends and receives
114  * bytes at the same time and the SPI IRQ is unique for send/receive.
115  * The only way to start transmission is to write data in SPDR (this
116  * is done by spi_starttx()). We do this *only* if a transfer is
117  * not already started.
118  */
119 struct ArmSerial
120 {
121         struct SerialHardware hw;
122         volatile bool sending;
123 };
124
125
126 /*
127  * These are to trick GCC into *not* using absolute addressing mode
128  * when accessing ser_handles, which is very expensive.
129  *
130  * Accessing through these pointers generates much shorter
131  * (and hopefully faster) code.
132  */
133 struct Serial *ser_uart0 = &ser_handles[SER_UART0];
134
135 /**
136  * Serial 0 TX interrupt handler
137  */
138 static void serirq_tx(void)
139 {
140         SER_STROBE_ON;
141
142         struct FIFOBuffer * const txfifo = &ser_uart0->txfifo;
143
144         if (fifo_isempty(txfifo))
145         {
146                 /* Enable Tx and Rx */
147                 US0_CR = BV(US_RXEN) | BV(US_TXEN);
148         }
149         else
150         {
151                 char c = fifo_pop(txfifo);
152                 /* Send one char */
153                 US0_THR = c;
154         }
155
156         SER_STROBE_OFF;
157 }
158
159 /**
160  * Serial 0 RX complete interrupt handler.
161  */
162 static void serirq_rx(void)
163 {
164         SER_STROBE_ON;
165
166         /* Should be read before UDR */
167         ser_uart0->status |= US0_CSR & (SERRF_RXSROVERRUN | SERRF_FRAMEERROR);
168
169         char c = US0_RHR;
170         struct FIFOBuffer * const rxfifo = &ser_uart0->rxfifo;
171
172         if (fifo_isfull(rxfifo))
173                 ser_uart0->status |= SERRF_RXFIFOOVERRUN;
174         else
175         {
176                 fifo_push(rxfifo, c);
177         }
178
179         SER_STROBE_OFF;
180 }
181
182 /**
183  * Serial IRQ dispatcher.
184  */
185 static void serirq_dispatcher(void) __attribute__ ((naked));
186 static void serirq_dispatcher(void)
187 {
188         IRQ_ENTRY();
189
190         if (US0_IMR & BV(US_RXRDY))
191                 serirq_rx();
192
193         if (US0_IMR & BV(US_TXRDY))
194                 serirq_tx();
195
196         IRQ_EXIT();
197 }
198
199 /*
200  * Callbacks
201  */
202 static void uart0_init(
203         UNUSED_ARG(struct SerialHardware *, _hw),
204         UNUSED_ARG(struct Serial *, ser))
205 {
206         /* Disable all interrupt */
207         US0_IDR = 0xFFFFFFFF;
208
209         /* Set the vector. */
210         AIC_SVR(US0_ID) = serirq_dispatcher;
211         /* Initialize to edge triggered with defined priority. */
212         AIC_SMR(US0_ID) = AIC_SRCTYPE_INT_EDGE_TRIGGERED;
213         /* Enable the USART IRQ */
214         AIC_IECR = BV(US0_ID);
215
216     /* Enable UART clock. */
217         PMC_PCER = BV(US0_ID);
218
219     /* Disable GPIO on UART tx/rx pins. */
220         PIOA_PDR = BV(5) | BV(6);
221
222         /* Set serial param: mode Normal, 8bit data, 1bit stop */
223         US0_MR = US_CHMODE_NORMAL | US_CHRL_8 | US_NBSTOP_1;
224
225         /* Reset UART. */
226         US0_CR = BV(US_RSTRX) | BV(US_RSTTX);
227
228         /* Enable Tx and Rx */
229         US0_CR = BV(US_RXEN) | BV(US_TXEN);
230
231         /* Enable Tx and Rx interrupt*/
232         US0_IER = BV(US_RXRDY) | BV(US_TXRDY);
233
234
235 }
236
237 static void uart0_cleanup(UNUSED_ARG(struct SerialHardware *, _hw))
238 {
239         US0_CR = BV(US_RSTRX) | BV(US_RSTTX) | BV(US_RXDIS) | BV(US_TXDIS) | BV(US_RSTSTA);
240 }
241
242 static void uart0_enabletxirq(struct SerialHardware *_hw)
243 {
244         struct ArmSerial *hw = (struct ArmSerial *)_hw;
245
246         /*
247          * WARNING: racy code here!  The tx interrupt sets hw->sending to false
248          * when it runs with an empty fifo.  The order of statements in the
249          * if-block matters.
250          */
251         if (!hw->sending)
252         {
253                 hw->sending = true;
254                 /* Enable Tx and Rx */
255                 US0_CR = BV(US_RXEN) | BV(US_TXEN);
256         }
257 }
258
259 static void uart0_setbaudrate(UNUSED_ARG(struct SerialHardware *, _hw), unsigned long rate)
260 {
261         /* Compute baud-rate period */
262         US0_BRGR = CLOCK_FREQ / (16 * rate);
263         //DB(kprintf("uart0_setbaudrate(rate=%lu): period=%d\n", rate, period);)
264 }
265
266 static void uart0_setparity(UNUSED_ARG(struct SerialHardware *, _hw), int parity)
267 {
268         /* Set UART parity */
269         switch(parity)
270         {
271                 case SER_PARITY_NONE:
272                 {
273             /* Parity mode. */
274                         US0_MR |= US_PAR_MASK;
275                         break;
276                 }
277                 case SER_PARITY_EVEN:
278                 {
279             /* Even parity.*/
280                         US0_MR |= US_PAR_EVEN;
281                         break;
282                 }
283                 case SER_PARITY_ODD:
284                 {
285             /* Odd parity.*/
286                         US0_MR |= US_PAR_ODD;
287                         break;
288                 }
289         }
290
291 }
292
293 static bool tx_sending(struct SerialHardware* _hw)
294 {
295         struct ArmSerial *hw = (struct ArmSerial *)_hw;
296         return hw->sending;
297 }
298
299 // FIXME: move into compiler.h?  Ditch?
300 #if COMPILER_C99
301         #define C99INIT(name,val) .name = val
302 #elif defined(__GNUC__)
303         #define C99INIT(name,val) name: val
304 #else
305         #warning No designated initializers, double check your code
306         #define C99INIT(name,val) (val)
307 #endif
308
309 /*
310  * High-level interface data structures
311  */
312 static const struct SerialHardwareVT UART0_VT =
313 {
314         C99INIT(init, uart0_init),
315         C99INIT(cleanup, uart0_cleanup),
316         C99INIT(setBaudrate, uart0_setbaudrate),
317         C99INIT(setParity, uart0_setparity),
318         C99INIT(txStart, uart0_enabletxirq),
319         C99INIT(txSending, tx_sending),
320 };
321
322 static struct ArmSerial UARTDescs[SER_CNT] =
323 {
324         {
325                 C99INIT(hw, /**/) {
326                         C99INIT(table, &UART0_VT),
327                         C99INIT(txbuffer, uart0_txbuffer),
328                         C99INIT(rxbuffer, uart0_rxbuffer),
329                         C99INIT(txbuffer_size, sizeof(uart0_txbuffer)),
330                         C99INIT(rxbuffer_size, sizeof(uart0_rxbuffer)),
331                 },
332                 C99INIT(sending, false),
333         }
334 };
335
336 struct SerialHardware *ser_hw_getdesc(int unit)
337 {
338         ASSERT(unit < SER_CNT);
339         return &UARTDescs[unit].hw;
340 }