-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi.h
406 lines (304 loc) · 9.18 KB
/
spi.h
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* Copyright (C) 2022 (Unix69@github)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#ifndef SPI_H
#define SPI_H
#include "mmu.h"
#include "types.h"
#define SPI_CS ((volatile unsigned int*)(MMIO_BASE+0x204000))
#define SPI_FIFO ((volatile unsigned int*)(MMIO_BASE+0x204004))
#define SPI_CLK ((volatile unsigned int*)(MMIO_BASE+0x204008))
#define SPI_DLEN ((volatile unsigned int*)(MMIO_BASE+0x20400C))
#define SPI_LTOH ((volatile unsigned int*)(MMIO_BASE+0x204010))
#define SPI_DC ((volatile unsigned int*)(MMIO_BASE+0x204014))
#define SPI_IRQ_ON_RXFIFO_ENABLE 1
#define SPI_IRQ_ON_COMPLETE_ENABLE 1
#define SPI_IRQ_ON_RXFIFO_DISABLE 0
#define SPI_IRQ_ON_COMPLETE_DISABLE 0
#define SPI_LOSSI_LONG_DATA_ENABLE 1
#define SPI_LOSSI_LONG_DATA_DISABLE 0
#define SPI_DMA_ENABLE 1
#define SPI_DMA_DISABLE 0
#define SPI_CSPOL2_HIGH 1
#define SPI_CSPOL2_LOW 0
#define SPI_CSPOL1_HIGH 1
#define SPI_CSPOL1_LOW 0
#define SPI_RX_FIFO_NOT_FULL 0
#define SPI_RX_FIFO_FULL 1
#define SPI_TX_NOT_ACCEPT_DATA 0
#define SPI_TX_ACCEPT_DATA 1
#define SPI_RX_NEEDS_READING 1
#define SPI_RX_DO_NOT_NEEDS_READING 0
#define SPI_RX_FIFO_EMPTY 0
#define SPI_RX_FIFO_NOT_EMPTY 1
#define SPI_TRASNFER_IN_PROGRESS 0
#define SPI_TRASNFER_DONE 1
#define SPI_TA_POLICY 1
#define SPI_TA_HIGH 0
#define SPI_SPI_ENABLE 0
#define SPI_LOSSI_ENABLE 1
#define SPI_READ_ENABLE 1
#define SPI_WRITE_ENABLE 0
#define SPI_AD_CS_ENABLE 1
#define SPI_AD_CS_DISABLE 0
#define SPI_CSPOL_HIGH 1
#define SPI_CSPOL_LOW 0
#define SPI_CLEAR_RX 0x01b
#define SPI_CLEAR_TX 0x10b
#define SPI_CPOL_HIGH 1
#define SPI_CPOL_LOW 0
#define SPI_CPHA_MIDDLE 0
#define SPI_CPHA_BEGIN 1
#define SPI_CS_0 0
#define SPI_CS_1 1
#define SPI_CS_2 2
#define SPI_CSPOL_HIGH 1
#define SPI_CSPOL_LOW 0
#define SPI_CLK_DIV [0, 15]
#define SPI_DMA_DLEN [0, 15]
#define SPI_OUTPUT_HOLD [0, 3]
#define SPI_ENABLED 1
#define SPI_DISABLED 0
#define SPI_STOPPED 0
#define SPI_RUNNING 1
#define SPI_FREE 1
#define SPI_BUSY 0
#define SPI_NO_OWNER 0
#define SPI_NO_ID 0
#define SPI_IS_VALID_SPI_CS(SPI_CS) SPI_CS >= 0 && SPI_CS <=2
#define SPI_IS_VALID_SPI_CPHA(SPI_CPHA) SPI_CPHA >= 0 && SPI_CPHA <=1
#define SPI_IS_VALID_SPI_CPOL(SPI_CPOL) SPI_CPOL >= 0 && SPI_CPOL <=1
#define SPI_IS_VALID_SPI_CLEAR(SPI_CLEAR) SPI_CLEAR >= 0 && SPI_CLEAR <= 3
#define SPI_IS_VALID_SPI_CSPOL(SPI_CSPOL) SPI_CSPOL >= 0 && SPI_CSPOL <=1
#define SPI_IS_VALID_SPI_TA(SPI_TA) SPI_TA >= 0 && SPI_TA <=1
#define SPI_IS_VALID_SPI_DMA_ENABLE(SPI_DMA_ENABLE) SPI_DMA_ENABLE >= 0 && SPI_DMA_ENABLE <=1
#define SPI_IS_VALID_SPI_REN(SPI_REN) SPI_REN >= 0 && SPI_REN <=1
#define SPI_IS_VALID_SPI_LEN(SPI_LEN) SPI_LEN >= 0 && SPI_LEN <=1
#define SPI_IS_VALID_SPI_CSPOL0(SPI_CSPOL0) SPI_CSPOL0 >= 0 && SPI_CSPOL0 <=1
#define SPI_IS_VALID_SPI_CSPOL1(SPI_CSPOL1) SPI_CSPOL1 >= 0 && SPI_CSPOL1 <=1
#define SPI_IS_VALID_SPI_CSPOL2(SPI_CSPOL2) SPI_CSPOL2 >= 0 && SPI_CSPOL2 <=1
#define SPI_IS_VALID_SPI_LONG_ENABLE(SPI_LONG_ENABLE) SPI_LONG_ENABLE >= 0 && SPI_LONG_ENABLE <=1
#define SPI_IS_VALID_SPI_IRQ_ENABLE(SPI_IRQ_ENABLE) SPI_IRQ_ENABLE >= 0 && SPI_IRQ_ENABLE <=1
#define SPI_IS_VALID_SPI_CLK(SPI_CLK) SPI_CLK >= 0 && SPI_CLK < (2 << 16)
#define SPI_IS_VALID_SPI_DLEN(SPI_DLEN) SPI_DLEN >= 0 && SPI_DLEN < (2 << 16)
#define SPI_IS_VALID_SPI_LTOH(SPI_LTOH) SPI_LTOH >= 0 && SPI_LTOH <=15
#define SPI_IS_VALID_SPI_DC(SPI_DC) SPI_DC >= 0 && SPI_DC < (2 << 32)
typedef struct spi spi_t;
//BrainOS spi internal routines
/// @brief boot spis
void spi_boot();
/// @brief shutdown spis
void spi_shutdown();
void spi_init(register uint8_t spi_cs, register uint8_t spi_cpha, register uint8_t spi_cpol, register uint8_t spi_clear, register uint8_t spi_cspol,register uint8_t spi_ta, register uint8_t spi_dma_en, register uint8_t spi_irq_tx_d,register uint8_t spi_irq_rx_r,register uint8_t spi_adcs, register uint8_t spi_read_en, register uint8_t spi_lossi_en, register uint8_t spi_cspol0, register uint8_t spi_cspol1, register uint8_t spi_cspol2, register uint8_t spi_dma_lossi_enable, register uint8_t spi_lossi_long_en);
int spi_open( int spi_owner);
int spi_close( int spi_owner);
int spi_read( int spi_owner, uint32_t *data);
int spi_write(int spi_owner, uint32_t data);
int spi_lossi_cmd_write( int spi_owner, uint32_t cmd);
int spi_lossi_cmd_read( int spi_owner, uint32_t *cmd);
int spi_lossi_param_write( int spi_owner, uint32_t param);
int spi_lossi_param_read( int spi_owner, uint32_t *param);
//Brain OS high level API
//BrainOS spi system level API
/// @brief
/// @return
uint8_t spi_cs_cs_get();
/// @brief
/// @param spi_cs_cs
void spi_cs_cs_set(uint8_t spi_cs_cs);
/// @brief
/// @return
uint8_t spi_cs_cpol_get();
/// @brief
/// @param spi_cpol
void spi_cs_cpol_set(uint8_t spi_cpol);
/// @brief
/// @return
uint8_t spi_cs_cspol_get();
/// @brief
/// @param spi_cspol
void spi_cs_cspol_set(uint8_t spi_cspol);
/// @brief
/// @param spi_cspol0
void spi_cs_cspol0_set(uint8_t spi_cspol0);
/// @brief
/// @return
uint8_t spi_cs_cspol0_get();
/// @brief
/// @param spi_cs_cspol1
void spi_cs_cspol1_set(uint8_t spi_cs_cspol1);
/// @brief
/// @return
uint8_t spi_cs_cspol1_get();
/// @brief
/// @param spi_cs_cspol2
void spi_cs_cspol2_set(uint8_t spi_cs_cspol2);
/// @brief
/// @return
uint8_t spi_cs_cspol2_get();
/// @brief
/// @return
uint8_t spi_cs_cpha_get();
/// @brief
/// @param spi_cpha
void spi_cs_cpha_set(uint8_t spi_cpha);
/// @brief
/// @param spi_irq_on_rxr
/// @return
void spi_cs_irq_on_rxr_set(uint8_t spi_irq_on_rxr);
/// @brief
/// @return
uint8_t spi_cs_irq_on_rxr_get();
/// @brief
/// @param spi_irq_on_tx_done
/// @return
void spi_cs_irq_on_tx_done_set(uint8_t spi_irq_on_tx_done);
/// @brief
/// @return
uint8_t spi_cs_irq_on_tx_done_get();
/// @brief
/// @return
uint8_t spi_cs_long_enable_get();
/// @brief
/// @param spi_len_long
void spi_cs_len_long_set(uint8_t spi_len_long);
/// @brief
/// @param spi_dma_len
void spi_cs_dma_len_set(uint8_t spi_dma_len);
/// @brief
/// @return
uint8_t spi_cs_dma_len_get();
/// @brief
/// @return
uint8_t spi_cs_rx_reading_get();
/// @brief
/// @return
uint8_t spi_cs_rx_full_get();
/// @brief
/// @return
uint8_t spi_cs_tx_full_fifo_get();
/// @brief
/// @return
uint8_t spi_cs_rx_empty_fifo_get();
/// @brief
/// @return
uint8_t spi_cs_tdone_get();
/// @brief
/// @param spi_len
void spi_cs_len_set(uint8_t spi_len);
/// @brief
/// @return
uint8_t spi_cs_len_get();
/// @brief
/// @param spi_ren
void spi_cs_ren_set(uint8_t spi_ren);
/// @brief
/// @return
uint8_t spi_cs_ren_get();
/// @brief
/// @param spi_adcs
void spi_cs_adcs_set(uint8_t spi_adcs);
/// @brief
/// @return
uint8_t spi_cs_adcs_get();
/// @brief
/// @return
uint8_t spi_cs_dma_enable_get();
/// @brief
/// @param spi_dma_enable
void spi_cs_dma_enable_set(uint8_t spi_dma_enable);
/// @brief
/// @return
uint8_t spi_cs_ta_get();
/// @brief
/// @param spi_ta
void spi_cs_ta_set(uint8_t spi_ta);
/// @brief
/// @return
uint8_t spi_cs_clear_get();
/// @brief
/// @param spi_clear
void spi_cs_clear_set(uint8_t spi_clear);
//BrainOS spi control level API
/// @brief
/// @param spi_ltoh
void spi_ltoh_set(uint32_t spi_ltoh);
/// @brief
void spi_ltoh_reset();
/// @brief
void spi_ltoh_clear();
/// @brief
/// @return
uint32_t spi_ltoh_get();
/// @brief
/// @return
uint32_t spi_cs_get();
/// @brief
/// @param spi_cs
void spi_cs_set(uint32_t spi_cs);
/// @brief
void spi_cs_reset();
/// @brief
void spi_cs_clear();
/// @brief
/// @param spi_fifo
void spi_fifo_set(uint32_t spi_fifo);
/// @brief
void spi_fifo_reset();
/// @brief
void spi_fifo_clear();
/// @brief
/// @return
uint32_t spi_fifo_get();
/// @brief
/// @param spi_dc
void spi_dc_set(uint32_t spi_dc);
/// @brief
void spi_dc_reset();
/// @brief
void spi_dc_clear();
/// @brief
/// @return
uint32_t spi_dc_get();
/// @brief
/// @param spi_dlen
void spi_dlen_set(uint32_t spi_dlen);
/// @brief
void spi_dlen_reset();
/// @brief
void spi_dlen_clear();
/// @brief
/// @return
uint32_t spi_dlen_get();
/// @brief
/// @param spi_clk
void spi_clk_set(uint32_t spi_clk);
/// @brief
void spi_clk_reset();
/// @brief
void spi_clk_clear();
/// @brief
/// @return
uint32_t spi_clk_get();
#endif