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