4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See devlib/README for information.
10 * \author Stefano Fedrigo <aleph@develer.com>
11 * \author Giovanni Bajo <rasky@develer.com>
13 * \brief DSP5680x CPU specific serial I/O driver
18 *#* Revision 1.8 2004/10/26 09:00:49 bernie
19 *#* Don't access serial data register twice.
21 *#* Revision 1.7 2004/10/19 08:57:15 bernie
22 *#* Bugfixes for DSP56K serial driver from scfirm.
24 *#* Revision 1.5 2004/08/25 14:12:08 rasky
25 *#* Aggiornato il comment block dei log RCS
27 *#* Revision 1.4 2004/07/30 14:27:49 rasky
28 *#* Aggiornati alcuni file DSP56k per la nuova libreria di IRQ management
30 *#* Revision 1.3 2004/06/03 11:27:09 bernie
31 *#* Add dual-license information.
33 *#* Revision 1.2 2004/05/23 18:21:53 bernie
34 *#* Trim CVS logs and cleanup header info.
42 #include <DSP56F807.h>
44 // GPIO E is shared with SPI (in DSP56807). Pins 0&1 are TXD0 and RXD0. To use
45 // the serial, we need to disable the GPIO functions on them.
46 #define REG_GPIO_SERIAL REG_GPIO_E
47 #define REG_GPIO_SERIAL_MASK 0x3
49 // Check flag consistency
50 #if (SERRF_PARITYERROR != REG_SCI_SR_PF) || \
51 (SERRF_RXSROVERRUN != REG_SCI_SR_OR) || \
52 (SERRF_FRAMEERROR != REG_SCI_SR_FE) || \
53 (SERRF_NOISEERROR != REG_SCI_SR_NF)
54 #error error flags do not match with register bits
57 static unsigned char ser0_fifo_rx[CONFIG_SER0_FIFOSIZE_RX];
58 static unsigned char ser0_fifo_tx[CONFIG_SER0_FIFOSIZE_TX];
59 static unsigned char ser1_fifo_rx[CONFIG_SER1_FIFOSIZE_RX];
60 static unsigned char ser1_fifo_tx[CONFIG_SER1_FIFOSIZE_TX];
64 struct SerialHardware hw;
65 struct Serial* serial;
66 volatile struct REG_SCI_STRUCT* regs;
71 static inline void enable_tx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
73 regs->CR |= REG_SCI_CR_TEIE | REG_SCI_CR_TIIE;
76 static inline void enable_rx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
78 regs->CR |= REG_SCI_CR_RIE;
81 static inline void disable_tx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
83 regs->CR &= ~(REG_SCI_CR_TEIE | REG_SCI_CR_TIIE);
86 static inline void disable_rx_irq_bare(volatile struct REG_SCI_STRUCT* regs)
88 regs->CR &= ~(REG_SCI_CR_RIE | REG_SCI_CR_REIE);
91 static inline void disable_tx_irq(struct SerialHardware* _hw)
93 struct SCI* hw = (struct SCI*)_hw;
94 volatile struct REG_SCI_STRUCT* regs = hw->regs;
96 disable_tx_irq_bare(regs);
99 static inline void enable_tx_irq(struct SerialHardware* _hw)
101 struct SCI* hw = (struct SCI*)_hw;
102 volatile struct REG_SCI_STRUCT* regs = hw->regs;
104 enable_tx_irq_bare(regs);
107 static inline void enable_rx_irq(struct SerialHardware* _hw)
109 struct SCI* hw = (struct SCI*)_hw;
110 volatile struct REG_SCI_STRUCT* regs = hw->regs;
112 enable_rx_irq_bare(regs);
115 static void tx_isr(const struct SCI *hw)
117 #pragma interrupt warn
118 volatile struct REG_SCI_STRUCT* regs = hw->regs;
120 if (fifo_isempty(&hw->serial->txfifo))
121 disable_tx_irq_bare(regs);
124 // Clear transmitter flags before sending data
126 regs->DR = fifo_pop(&hw->serial->txfifo);
130 static void rx_isr(const struct SCI *hw)
132 #pragma interrupt warn
133 volatile struct REG_SCI_STRUCT* regs = hw->regs;
136 hw->serial->status |= regs->SR & (SERRF_PARITYERROR |
142 * Serial IRQ can happen for two reason: data ready (RDRF) or overrun (OR)
143 * If the data is ready, we need to fetch it from the data register or
144 * the interrupt will retrigger immediatly. In case of overrun, instead,
145 * the value of the data register is meaningless.
147 if (regs->SR & REG_SCI_SR_RDRF)
149 unsigned char data = regs->DR;
151 if (fifo_isfull(&hw->serial->rxfifo))
152 hw->serial->status |= SERRF_RXFIFOOVERRUN;
154 fifo_push(&hw->serial->rxfifo, data);
157 // Writing anything to the status register clear the error bits.
161 static void init(struct SerialHardware* _hw, struct Serial* ser)
163 struct SCI* hw = (struct SCI*)_hw;
164 volatile struct REG_SCI_STRUCT* regs = hw->regs;
166 // Clear status register (IRQ/status flags)
170 // Clear data register
173 // Install the handlers and set priorities for both IRQs
174 irq_install(hw->irq_tx, (isr_t)tx_isr, hw);
175 irq_install(hw->irq_rx, (isr_t)rx_isr, hw);
176 irq_setpriority(hw->irq_tx, IRQ_PRIORITY_SCI_TX);
177 irq_setpriority(hw->irq_rx, IRQ_PRIORITY_SCI_RX);
179 // Activate the RX error interrupts, and RX/TX transmissions
180 regs->CR = REG_SCI_CR_TE | REG_SCI_CR_RE;
181 enable_rx_irq_bare(regs);
183 // Disable GPIO pins for TX and RX lines
184 REG_GPIO_SERIAL->PER |= REG_GPIO_SERIAL_MASK;
189 static void cleanup(struct SerialHardware* _hw)
195 static void setbaudrate(struct SerialHardware* _hw, unsigned long rate)
197 struct SCI* hw = (struct SCI*)_hw;
199 // SCI has an internal 16x divider on the input clock, which comes
200 // from the IPbus (see the scheme in user manual, 12.7.3). We apply
201 // it to calculate the period to store in the register.
202 hw->regs->BR = (IPBUS_FREQ + rate * 8ul) / (rate * 16ul);
205 static void setparity(struct SerialHardware* _hw, int parity)
212 static const struct SerialHardwareVT SCI_VT =
216 .setbaudrate = setbaudrate,
217 .setparity = setparity,
218 .enabletxirq = enable_tx_irq,
221 static struct SCI SCIDescs[2] =
227 .rxbuffer = ser0_fifo_rx,
228 .txbuffer = ser0_fifo_tx,
229 .rxbuffer_size = countof(ser0_fifo_rx),
230 .txbuffer_size = countof(ser0_fifo_tx),
233 .irq_rx = IRQ_SCI0_RECEIVER_FULL,
234 .irq_tx = IRQ_SCI0_TRANSMITTER_READY,
241 .rxbuffer = ser1_fifo_rx,
242 .txbuffer = ser1_fifo_tx,
243 .rxbuffer_size = countof(ser1_fifo_rx),
244 .txbuffer_size = countof(ser1_fifo_tx),
247 .irq_rx = IRQ_SCI1_RECEIVER_FULL,
248 .irq_tx = IRQ_SCI1_TRANSMITTER_READY,
253 struct SerialHardware* ser_hw_getdesc(int unit)
256 return &SCIDescs[unit].hw;