1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
#include <rtdevice.h>
#include "gtypes.h"
#include "gd_spi.h"
#include "drv_ssi.h"
#include "platform.h"
/*---------------------------------------------------------------------------*/
/* local defines */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* local data types */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* local data */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* local functions */
/*---------------------------------------------------------------------------*/
static void *gk_get_spi_dev_pri_data(struct rt_spi_device *device)
{
return device->parent.user_data;
}
static rt_err_t gk_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration)
{
struct gk_spi_slave_info *spi_slave;
struct gk_spi_controller *spi_control;
GD_SPI_OPEN_PARAMS_S openParamsP;
rt_uint32_t spi_hz;
rt_int32_t ret;
rt_uint8_t data_width = 8,polarity;
if(!device || !configuration)
{
rt_kprintf("%s,%d,param wrong!\n",__FUNCTION__,__LINE__);
return RT_ERROR;
}
spi_slave = (struct gk_spi_slave_info *)gk_get_spi_dev_pri_data(device);
spi_control = spi_slave->control;
rt_sem_take(&spi_control->xfer_lock, RT_WAITING_FOREVER);
if(spi_slave->spihandle > 0)
{
GD_SPI_Close(&spi_slave->spihandle);
spi_slave->spihandle = -1;
}
openParamsP.spi = spi_control->id;
openParamsP.slave = spi_slave->id;
openParamsP.csgpio = spi_slave->cs_pin;
openParamsP.used_irq = 1;
if (configuration->max_hz > GD_SPI_FREQ_81M)
spi_hz = GD_SPI_FREQ_81M;
else
spi_hz = configuration->max_hz;
openParamsP.baudrate = spi_hz;
/* CPOL */
if (configuration->mode & RT_SPI_CPOL && configuration->mode & RT_SPI_CPHA)
{
polarity = SPI_POLARITY_MODE3;
}
else if(configuration->mode & RT_SPI_CPOL && !configuration->mode & RT_SPI_CPHA)
{
polarity = SPI_POLARITY_MODE2;
}
else if(!configuration->mode & RT_SPI_CPOL && configuration->mode & RT_SPI_CPHA)
{
polarity = SPI_POLARITY_MODE1;
}
else
polarity = SPI_POLARITY_MODE0;
openParamsP.polarity = polarity;
ret = GD_SPI_Open(&openParamsP,&(spi_slave->spihandle));
if(ret != GD_OK)
{
rt_kprintf("GD_SPI_Open failed!\n");
rt_sem_release(&spi_control->xfer_lock);
return RT_ERROR;
}
/* data_width */
if (configuration->data_width <= 8)
{
data_width = 8;
}
else if (configuration->data_width <= 16)
{
data_width = 16;
}
ret = GD_SPI_SetDatFormat(spi_slave->spihandle, data_width);;
if(ret != GD_OK)
{
rt_kprintf("GD_SPI_SetDatFormat failed!\n");
rt_sem_release(&spi_control->xfer_lock);
return RT_ERROR;
}
rt_sem_release(&spi_control->xfer_lock);
return RT_EOK;
}
static rt_uint32_t gk_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
{
struct gk_spi_slave_info *spi_slave;
struct gk_spi_controller *spi_control;
GERR ret = GD_OK;
if(!device || !message)
{
rt_kprintf("%s,%d,param wrong!\n",__FUNCTION__,__LINE__);
return 0;
}
spi_slave = (struct gk_spi_slave_info *)gk_get_spi_dev_pri_data(device);
if(!spi_slave)
{
rt_kprintf("%s,%d,param wrong!\n",__FUNCTION__,__LINE__);
return 0;
}
spi_control = spi_slave->control;
if(spi_slave->spihandle < 0)
{
rt_kprintf("%s,%d,spi channel not configured!\n",__FUNCTION__,__LINE__);
return 0;
}
if(!message)
{
rt_kprintf("%s,%d,params wrong!\n",__FUNCTION__,__LINE__);
return 0;
}
rt_sem_take(&spi_control->xfer_lock, RT_WAITING_FOREVER);
if(message->cs_take)
GD_SPI_GetDevice(spi_slave->spihandle);
if(message->recv_buf && message->send_buf)
ret = GD_SPI_WriteThenReadBytes(spi_slave->spihandle,(rt_uint8_t *)message->send_buf,message->length,
(rt_uint8_t *)message->recv_buf,message->length);
else if(message->send_buf && !message->recv_buf)
ret = GD_SPI_WriteBytes(spi_slave->spihandle,(rt_uint8_t *)message->send_buf,message->length);
else if(!message->send_buf && message->recv_buf)
ret = GD_SPI_WriteThenReadBytes(spi_slave->spihandle,NULL,0,(rt_uint8_t *)message->recv_buf,message->length);
if(message->cs_release)
GD_SPI_ReleaseDevice(spi_slave->spihandle);
rt_sem_release(&spi_control->xfer_lock);
if(ret != GD_OK)
{
rt_kprintf("%s,%d,transfer error!\n",__FUNCTION__,__LINE__);
return 0;
}
else
{
return message->length;
}
}
static struct rt_spi_ops gk_spi_ops = {
.configure = gk_spi_configure, .xfer = gk_spi_xfer,
};
int gk_spi_probe(void *priv_data)
{
int ret,i;
char spi_dev_name[20] = {0};
char spi_bus_name[20] = {0};
char spi_lock_name[20] = {0};
struct gk_spi_controller *spi_control;
struct gk_spi_controller_data *spi_controller_data = (struct gk_spi_controller_data *)priv_data;
struct gk_spi_slave_info **control_slave;
struct gk_spi_slave_info *spi_slave,*next_slave;
if (!spi_controller_data)
{
rt_kprintf("%s,%d input param wrong!\n",__FUNCTION__,__LINE__);
return -1;
}
spi_control = (struct gk_spi_controller *)rt_malloc(sizeof(struct gk_spi_controller));
if (!spi_control)
{
rt_kprintf("%s,%d malloc failed!\n",__FUNCTION__,__LINE__);
return -1;
}
rt_memset(spi_control, 0, sizeof(struct gk_spi_controller));
spi_control->id = spi_controller_data->id;
rt_sprintf(spi_lock_name, "%s%d", "spi_lock", spi_control->id);
rt_sprintf(spi_bus_name, "%s%d", "spi_bus", spi_control->id);
rt_sem_init(&spi_control->xfer_lock, spi_lock_name, 1, RT_IPC_FLAG_FIFO);
ret = rt_spi_bus_register(&spi_control->spi_bus, spi_bus_name, &gk_spi_ops);
control_slave = &spi_control->spi_slave;
for (i = 0; i < spi_controller_data->total_slave; i++)
{
spi_slave = (struct gk_spi_slave_info *)rt_malloc(sizeof(struct gk_spi_slave_info));
if (!spi_slave)
{
rt_kprintf("%s,%d malloc failed!\n",__FUNCTION__,__LINE__);
goto exit;
}
rt_memset(spi_slave, 0, sizeof(struct gk_spi_slave_info));
spi_slave->id = i;
spi_slave->control = spi_control;
spi_slave->cs_pin = spi_controller_data->slave_cs_pin[i];
spi_slave->spihandle = -1;
rt_sprintf(spi_dev_name, "%s%d%s%d", "ssi", spi_control->id, "_", spi_slave->id);
*control_slave = spi_slave;
control_slave = &spi_slave->next;
ret = rt_spi_bus_attach_device(&spi_slave->spi_device, spi_dev_name,spi_bus_name, spi_slave);
if (ret != RT_EOK)
{
rt_kprintf("register dev to bus failed...\n");
goto exit;
}
}
spi_controller_data->control = spi_control;
GD_SPI_Init();
return RT_EOK;
exit:
spi_slave = spi_control->spi_slave;
while (spi_slave != RT_NULL)
{
next_slave = spi_slave->next;
rt_free(spi_slave);
spi_slave = next_slave;
}
rt_sem_detach(&spi_control->xfer_lock);
rt_free(spi_control);
return RT_ERROR;
}
int gk_spi_exit(void *priv_data)
{
struct gk_spi_controller *spi_control;
struct gk_spi_controller_data *plat_data;
struct gk_spi_slave_info *spi_slave;
struct gk_spi_slave_info *next_slave;
plat_data = (struct gk_spi_controller_data *)priv_data;
spi_control = plat_data->control;
spi_slave = spi_control->spi_slave;
while (spi_slave != RT_NULL)
{
next_slave = spi_slave->next;
rt_free(spi_slave);
spi_slave = next_slave;
}
rt_sem_detach(&spi_control->xfer_lock);
rt_free(spi_control);
return RT_EOK;
}
struct gk_platform_driver gk_spi_driver_ops = {
.name = "spi", .probe = gk_spi_probe, .remove = gk_spi_exit,
};
void rt_hw_spi_init(void)
{
gk_platform_driver_init(&gk_spi_driver_ops);
}
|