lm3s1968: generic KFile-compliant SSI bus implementation.
[bertos.git] / bertos / cpu / cortex-m3 / drv / ssi_lm3s.h
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 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief LM3S1968 Synchronous Serial Interface (SSI) driver.
34  */
35
36 #ifndef SSI_LM3S_H
37 #define SSI_LM3S_H
38
39 #include <cpu/power.h> /* cpu_relax() */
40 #include <kern/kfile.h> /* KFile */
41 #include <io/lm3s.h>
42
43 /**
44  * LM3S1968 SSI frame format
45  */
46 /*\{*/
47 #define SSI_FRF_MOTO_MODE_0     0x00000000  //< Moto fmt, polarity 0, phase 0
48 #define SSI_FRF_MOTO_MODE_1     0x00000002  //< Moto fmt, polarity 0, phase 1
49 #define SSI_FRF_MOTO_MODE_2     0x00000001  //< Moto fmt, polarity 1, phase 0
50 #define SSI_FRF_MOTO_MODE_3     0x00000003  //< Moto fmt, polarity 1, phase 1
51 #define SSI_FRF_TI              0x00000010  //< TI frame format
52 #define SSI_FRF_NMW             0x00000020  //< National MicroWire frame format
53 /*\}*/
54
55 /**
56  * LM3S1968 SSI operational mode
57  */
58 /*\{*/
59 #define SSI_MODE_MASTER         0x00000000  //< SSI master
60 #define SSI_MODE_SLAVE          0x00000001  //< SSI slave
61 #define SSI_MODE_SLAVE_OD       0x00000002  //< SSI slave with output disabled
62 /*\}*/
63
64 /* LM3S SSI handle properties */
65 enum
66 {
67         /* Non-blocking I/O */
68         LM3S_SSI_NONBLOCK = 1,
69 };
70
71 /** LM3S1968 SSI handle structure */
72 typedef struct LM3SSSI
73 {
74         /* SSI Kfile structure */
75         KFile fd;
76
77         /* Handle properties */
78         uint32_t flags;
79
80         /* SSI port address */
81         uint32_t addr;
82 } LM3SSSI;
83
84 /**
85  * ID for LM3S SSI.
86  */
87 #define KFT_LM3SSSI MAKE_ID('L', 'S', 'S', 'I')
88
89 INLINE LM3SSSI *LM3SSSI_CAST(KFile *fd)
90 {
91         ASSERT(fd->_type == KFT_LM3SSSI);
92         return (LM3SSSI *)fd;
93 }
94
95 /* KFile interface to LM3S SSI */
96 void lm3s_ssiInit(struct LM3SSSI *fds, uint32_t addr, uint32_t frame, int mode,
97                         int bitrate, uint32_t data_width);
98
99 /* Raw interface to LM3S SSI */
100 int lm3s_ssiOpen(uint32_t addr, uint32_t frame, int mode,
101                         int bitrate, uint32_t data_width);
102
103 /*
104  * Check if the SSI transmitter is busy or not
105  *
106  * This allows to determine whether the TX FIFO have been cleared by the
107  * hardware, so the transmission can be safely considered completed.
108  */
109 INLINE bool lm3s_ssiTxDone(uint32_t base)
110 {
111         return (HWREG(base + SSI_O_SR) & SSI_SR_BSY) ? true : false;
112 }
113
114 /*
115  * Check if the SSI TX FIFO is full
116  */
117 INLINE bool lm3s_ssiTxReady(uint32_t base)
118 {
119         return (HWREG(base + SSI_O_SR) & SSI_SR_TNF) ? true : false;
120 }
121
122 /*
123  * Check for data available in the RX FIFO
124  */
125 INLINE bool lm3s_ssiRxReady(uint32_t base)
126 {
127         return (HWREG(base + SSI_O_SR) & SSI_SR_RNE) ? true : false;
128 }
129
130 /*
131  * Get a frame into the SSI receive FIFO without blocking.
132  *
133  * Return the number of frames read from the RX FIFO.
134  */
135 INLINE int lm3s_ssiReadFrameNonBlocking(uint32_t base, uint32_t *val)
136 {
137         /* Check for data available in the RX FIFO */
138         if (!lm3s_ssiRxReady(base))
139                 return 0;
140         /* Read data from SSI RX FIFO */
141         *val = HWREG(base + SSI_O_DR);
142         return 1;
143 }
144
145 /*
146  * Get a frame from the SSI receive FIFO.
147  */
148 INLINE void lm3s_ssiReadFrame(uint32_t base, uint32_t *val)
149 {
150         /* Wait for data available in the RX FIFO */
151         while (!lm3s_ssiRxReady(base))
152                 cpu_relax();
153         /* Read data from SSI RX FIFO */
154         *val = HWREG(base + SSI_O_DR);
155 }
156
157 /*
158  * Put a frame into the SSI transmit FIFO without blocking.
159  *
160  * NOTE: the upper bits of the frame will be automatically discarded by the
161  * hardware according to the frame data width.
162  *
163  * Return the number of frames written to the TX FIFO.
164  */
165 INLINE int lm3s_ssiWriteFrameNonBlocking(uint32_t base, uint32_t val)
166 {
167         /* Check for available space in the TX FIFO */
168         if (!lm3s_ssiTxReady(base))
169                 return 0;
170         /* Enqueue data to the TX FIFO */
171         HWREG(base + SSI_O_DR) = val;
172         return 1;
173 }
174
175 /*
176  * Put a frame into the SSI transmit FIFO.
177  *
178  * NOTE: the upper bits of the frame will be automatically discarded by the
179  * hardware according to the frame data width.
180  */
181 INLINE void lm3s_ssiWriteFrame(uint32_t base, uint32_t val)
182 {
183         /* Wait for available space in the TX FIFO */
184         while (!lm3s_ssiTxReady(base))
185                 cpu_relax();
186         /* Enqueue data to the TX FIFO */
187         HWREG(base + SSI_O_DR) = val;
188 }
189
190 #endif /* SSI_LM3S_H */