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