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