Move kfile interface to the io/ directory.
[bertos.git] / bertos / cpu / cortex-m3 / drv / ssi_lm3s.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 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief LM3S1968 Synchronous Serial Interface (SSI) driver.
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include "cfg/compiler.h"
39 #include "cfg/debug.h"
40
41 #include <string.h> /* memset() */
42
43 #include "ssi_lm3s.h"
44
45 /* SSI clocking informations (CPSDVSR + SCR) */
46 struct SSIClock
47 {
48         unsigned int cpsdvsr;
49         unsigned int scr;
50 };
51
52 /*
53  * Evaluate the SSI clock prescale (SSICPSR) and SSI serial clock rate (SCR).
54  */
55 INLINE struct SSIClock
56 lm3s_ssiPrescale(unsigned int bitrate)
57 {
58         struct SSIClock ret;
59
60         for (ret.cpsdvsr = 2, ret.scr = CPU_FREQ / bitrate / ret.cpsdvsr - 1;
61                         ret.scr > 255; ret.cpsdvsr += 2);
62         ASSERT(ret.cpsdvsr < 255);
63
64         return ret;
65 }
66
67 /*
68  * Initialize the SSI interface.
69  *
70  * Return 0 in case of success, a negative value otherwise.
71  */
72 int lm3s_ssiOpen(uint32_t addr, uint32_t frame, int mode,
73                         int bitrate, uint32_t data_width)
74 {
75         struct SSIClock ssi_clock;
76
77         ASSERT(addr == SSI0_BASE || addr == SSI1_BASE);
78         /* Configure the SSI operating mode */
79         switch (mode)
80         {
81                 /* SSI Slave Mode Output Disable */
82                 case SSI_MODE_SLAVE_OD:
83                         HWREG(addr + SSI_O_CR1) = SSI_CR1_SOD;
84                         break;
85                 /* SSI Slave */
86                 case SSI_MODE_SLAVE:
87                         HWREG(addr + SSI_O_CR1) = SSI_CR1_MS;
88                         break;
89                 /* SSI Master */
90                 case SSI_MODE_MASTER:
91                         HWREG(addr + SSI_O_CR1) = 0;
92                         break;
93                 default:
94                         ASSERT(0);
95                         return -1;
96         }
97         /* Configure the peripheral clock and frame format */
98         ssi_clock = lm3s_ssiPrescale(bitrate);
99         HWREG(addr + SSI_O_CPSR) = ssi_clock.cpsdvsr;
100         HWREG(addr + SSI_O_CR0) =
101                         (ssi_clock.scr << 8)            |
102                         ((frame & 3) << 6)              |
103                         (frame & SSI_CR0_FRF_M)         |
104                         (data_width - 1);
105         /* Enable the SSI interface */
106         HWREG(addr + SSI_O_CR1) |= SSI_CR1_SSE;
107
108         return 0;
109 }
110
111 /*
112  * Write data to the SSI bus.
113  *
114  * Return the number of bytes written to the bus.
115  */
116 static size_t lm3s_ssiWrite(struct KFile *fd, const void *buf, size_t size)
117 {
118         LM3SSSI *fds = LM3SSSI_CAST(fd);
119         const char *p = (const char *)buf;
120         uint32_t frame;
121         size_t count = 0;
122
123         while (count < size)
124         {
125                 frame = p[count];
126                 if (fds->flags & LM3S_SSI_NONBLOCK)
127                 {
128                         if (!lm3s_ssiWriteFrameNonBlocking(fds->addr,
129                                                                 frame))
130                                 break;
131                 }
132                 else
133                         lm3s_ssiWriteFrame(fds->addr, frame);
134                 count++;
135         }
136         return count;
137 }
138
139 /*
140  * Read data from the SSI bus.
141  *
142  * Return the number of bytes read from the bus.
143  */
144 static size_t lm3s_ssiRead(struct KFile *fd, void *buf, size_t size)
145 {
146         LM3SSSI *fds = LM3SSSI_CAST(fd);
147
148         uint8_t *p = (uint8_t *)buf;
149         uint32_t frame;
150         size_t count = 0;
151
152         while (count < size)
153         {
154                 if (fds->flags & LM3S_SSI_NONBLOCK)
155                 {
156                         if (!lm3s_ssiReadFrameNonBlocking(fds->addr, &frame))
157                                 break;
158                 }
159                 else
160                         lm3s_ssiReadFrame(fds->addr, &frame);
161                 *p++ = (uint8_t)frame;
162                 count++;
163         }
164         return count;
165 }
166
167
168 /* Wait for data in the TX FIFO being actually transmitted */
169 static int lm3s_ssiFlush(struct KFile *fd)
170 {
171         LM3SSSI *fds = LM3SSSI_CAST(fd);
172
173         while (!lm3s_ssiTxDone(fds->addr))
174                 cpu_relax();
175         return 0;
176 }
177
178 /* Disable the SSI interface */
179 static int lm3s_ssiClose(struct KFile *fd)
180 {
181         LM3SSSI *fds = LM3SSSI_CAST(fd);
182
183         lm3s_ssiFlush(fd);
184         HWREG(fds->addr + SSI_O_CR1) &= ~SSI_CR1_SSE;
185         return 0;
186 }
187
188 /**
189  * Initialize a LM3S SSI driver.
190  */
191 void lm3s_ssiInit(struct LM3SSSI *fds, uint32_t addr, uint32_t frame, int mode,
192                         int bitrate, uint32_t data_width)
193 {
194         memset(fds, 0, sizeof(*fds));
195         DB(fds->fd._type = KFT_LM3SSSI);
196
197         /* TODO: only 8-bit frame size is supported */
198         ASSERT(data_width == 8);
199
200         fds->fd.write = lm3s_ssiWrite;
201         fds->fd.read = lm3s_ssiRead;
202         fds->fd.close = lm3s_ssiClose;
203         fds->fd.flush = lm3s_ssiFlush;
204
205         fds->addr = addr;
206         lm3s_ssiOpen(addr, frame, mode, bitrate, data_width);
207 }