InPlay API
hal_spi.h
1 
13 #ifndef HAL_SPI_H
14 #define HAL_SPI_H
15 
25 #include "in_mmap.h"
26 #include "in_compile.h"
27 
28 /*
29  * Defines
30  ****************************************************************************************
31  */
32 
33 #define SPI_REG_CTRL0_OFS 0x00000000UL
34 #define SPI_REG_CTRL1_OFS 0x00000004UL
35 #define SPI_REG_SSIEN_OFS 0x00000008UL
36 #define SPI_REG_SE_OFS 0x00000010UL
37 #define SPI_REG_BAUD_OFS 0x00000014UL
38 #define SPI_REG_TXFTL_OFS 0x00000018UL
39 #define SPI_REG_RXFTL_OFS 0x0000001CUL
40 #define SPI_REG_TXFL_OFS 0x00000020UL
41 #define SPI_REG_RXFL_OFS 0x00000024UL
42 #define SPI_REG_SR_OFS 0x00000028UL
43 #define SPI_REG_IMR_OFS 0x0000002CUL
44 #define SPI_REG_ISR_OFS 0x00000030UL
45 #define SPI_REG_RISR_OFS 0x00000034UL
46 #define SPI_REG_TXOIC_OFS 0x00000038UL
47 #define SPI_REG_RXOIC_OFS 0x0000003CUL
48 #define SPI_REG_RXUIC_OFS 0x00000040UL
49 #define SPI_REG_ICR_OFS 0x00000048UL
50 #define SPI_REG_ICR_OFS 0x00000048UL
51 #define SPI_REG_DMACR_OFS 0x0000004CUL
52 #define SPI_REG_DMATDL_OFS 0x00000050UL
53 #define SPI_REG_DMARDL_OFS 0x00000054UL
54 #define SPI_REG_DR_OFS 0x00000060UL
55 #define SPI_REG_RX_SAMPLE_DLY 0x000000F0UL
56 #define SPI_REG_DUAL_QUAD_CTRL_OFS 0x000000F4UL
57 
58 #define SPI_CTL0_DFS_16 0x0000000FUL
59 #define SPI_CTL0_SCPH 0x00000040UL
60 #define SPI_CTL0_SCPOL 0x00000080UL
61 #define SPI_CTL0_TMOD 0x00000300UL
62 #define SPI_CTL0_DFS 0x001F0000UL
63 #define SPI_CTL0_FRF 0x00600000UL
64 
65 #define SPI_CTL1_NDF 0x0000FFFFUL
66 
67 #define SPI_SE_EN 0x0000000FUL
68 
69 #define SPI_BAUD_SCKDV 0x0000FFFFUL
70 
71 #define SPI_TXFTL_TFT 0x0000000FUL
72 
73 #define SPI_RXFTL_RFT 0x0000000FUL
74 
75 #define SPI_TXFL_TXTFL 0x0000000FUL
76 
77 #define SPI_RXFL_RXTFL 0x0000000FUL
78 
79 #define SPI_SR_BUSY 0x00000001UL
80 #define SPI_SR_TFNF 0x00000002UL
81 #define SPI_SR_TFE 0x00000004UL
82 #define SPI_SR_RFNE 0x00000008UL
83 #define SPI_SR_RFF 0x00000010UL
84 #define SPI_SR_TXE 0x00000020UL
85 #define SPI_SR_DCOL 0x00000040UL
86 
87 #define SPI_IT_TXE 0x00000001UL
88 #define SPI_IT_TXO 0x00000002UL
89 #define SPI_IT_RXU 0x00000004UL
90 #define SPI_IT_RXO 0x00000008UL
91 #define SPI_IT_RXF 0x00000010UL
92 #define SPI_IT_MSTIM 0x00000020UL
93 #define SPI_IT_ALL (SPI_IT_TXE|SPI_IT_TXO|SPI_IT_RXU|SPI_IT_RXO|SPI_IT_RXF|SPI_IT_MSTIM)
94 
95 #define SPI_DUAL_QUAD_TRANS_TYPE 0x00000003UL
96 #define SPI_DUAL_QUAD_ADDR_LEN 0x0000003CUL
97 #define SPI_DUAL_QUAD_INS_LEN 0x00000300UL
98 #define SPI_DUAL_QUAD_WAIT_CYCLES 0x00007800UL
99 
100 /*
101  * Enumeratios
102  ****************************************************************************************
103  */
104 
106 enum spi_id {
113 };
114 
116 enum spi_fmt {
123 };
124 
126 enum spi_tmod {
135  SPI_TMOD_MAX,
136 };
137 
140  SPI_DFS_4_BITS = 0x4,
141  SPI_DFS_5_BITS,
142  SPI_DFS_6_BITS,
143  SPI_DFS_7_BITS,
144  SPI_DFS_8_BITS,
145  SPI_DFS_9_BITS,
146  SPI_DFS_10_BITS,
147  SPI_DFS_11_BITS,
148  SPI_DFS_12_BITS,
149  SPI_DFS_13_BITS,
150  SPI_DFS_14_BITS,
151  SPI_DFS_15_BITS,
152  SPI_DFS_16_BITS,
153  SPI_DFS_17_BITS,
154  SPI_DFS_18_BITS,
155  SPI_DFS_19_BITS,
156  SPI_DFS_20_BITS,
157  SPI_DFS_21_BITS,
158  SPI_DFS_22_BITS,
159  SPI_DFS_23_BITS,
160  SPI_DFS_24_BITS,
161  SPI_DFS_25_BITS,
162  SPI_DFS_26_BITS,
163  SPI_DFS_27_BITS,
164  SPI_DFS_28_BITS,
165  SPI_DFS_29_BITS,
166  SPI_DFS_30_BITS,
167  SPI_DFS_31_BITS,
168  SPI_DFS_32_BITS,
169  SPI_DFS_MAX,
170 };
171 
173 enum spi_err {
174  SPI_ERR_OK = 0,
175  SPI_ERR_INVALID_PARAM = -1,
176  SPI_ERR_INVALID_OPER = -2,
177  SPI_ERR_TX_OV = -3,
178  SPI_ERR_RX_OV = -4,
179  SPI_ERR_RX_UN = -5,
180  SPI_ERR_DMA_NOT_AVAIL = -6,
181  SPI_ERR_DMA_ERROR = -7,
182  SPI_ERR_RESET = -8,
183  SPI_ERR_ALIGN = -9,
184  SPI_ERR_TMO = -10,
185 } ;
186 
188 enum spi_dir {
189  SPI_DIR_RX = 1,
190  SPI_DIR_TX,
191 };
192 
195  SPI_CS_0 = 0x1,
196  SPI_CS_1 = 0x2,
197  SPI_CS_2 = 0x4,
198  SPI_CS_3 = 0x8,
199 };
200 
201 
204  QSPI_CMD_WIDTH_0BITS,
205  QSPI_CMD_WIDTH_4BITS,
206  QSPI_CMD_WIDTH_8BITS,
207  QSPI_CMD_WIDTH_16BITS,
208 };
209 
212  QSPI_ADDR_WIDTH_0BITS,
213  QSPI_DDR_WIDTH_4BITS,
214  QSPI_ADDR_WIDTH_8BITS,
215  QSPI_ADDR_WIDTH_12BITS,
216  QSPI_ADDR_WIDTH_16BITS,
217  QSPI_ADDR_WIDTH_20BITS,
218  QSPI_ADDR_WIDTH_24BITS,
219  QSPI_ADDR_WIDTH_28BITS,
220  QSPI_ADDR_WIDTH_32BITS,
221  QSPI_ADDR_WIDTH_36BITS,
222  QSPI_ADDR_WIDTH_40BITS,
223  QSPI_ADDR_WIDTH_44BITS,
224  QSPI_ADDR_WIDTH_48BITS,
225  QSPI_ADDR_WIDTH_52BITS,
226  QSPI_ADDR_WIDTH_56BITS,
227  QSPI_ADDR_WIDTH_60BITS,
228 };
229 
232  QSPI_TT_CMD_STD_ADDR_STD,
233  QSPI_TT_CMD_STD_ADDR_QUAD,
234  QSPI_TT_CMD_ADDR_QUAD,
235 };
236 
237 /*
238  * Inline Functions
239  ****************************************************************************************
240  */
241 
242 static INLINE void spi_ctl0_clear(uint32_t spi_base)
243 {
244  WR_WORD((spi_base + SPI_REG_CTRL0_OFS), 0);
245 }
246 
247 static INLINE void spi_ctl0(uint32_t spi_base, int fmt, int tmod, int dfs, int phase_high, int polarity_high)
248 {
249  uint32_t reg = RD_WORD(spi_base + SPI_REG_CTRL0_OFS);
250 
251  // phase
252  if (phase_high) {
253  reg |= SPI_CTL0_SCPH;
254  } else {
255  reg &= ~SPI_CTL0_SCPH;
256  }
257 
258  // polarity
259  if (polarity_high) {
260  reg |= SPI_CTL0_SCPOL;
261  } else {
262  reg &= ~SPI_CTL0_SCPOL;
263  }
264 
265  // format
266  reg &= ~SPI_CTL0_FRF;
267  reg |= ((fmt&0x3)<<21);
268 
269  // transfer mode
270  reg &= ~SPI_CTL0_TMOD;
271  reg |= (tmod << 8);
272 
273  // data frame size
274  reg &= ~SPI_CTL0_DFS;
275  dfs -= 1;
276  reg |= ((dfs&0x1F)<<16);
277 
278  WR_WORD((spi_base + SPI_REG_CTRL0_OFS), reg);
279 }
280 
281 static FORCEINLINE void spi_phase_0(uint32_t spi_base)
282 {
283  WR_WORD((spi_base + SPI_REG_CTRL0_OFS), (RD_WORD(spi_base + SPI_REG_CTRL0_OFS) & ~SPI_CTL0_SCPH));
284 }
285 
286 static FORCEINLINE void spi_phase_1(uint32_t spi_base)
287 {
288  WR_WORD((spi_base + SPI_REG_CTRL0_OFS), (RD_WORD(spi_base + SPI_REG_CTRL0_OFS) | SPI_CTL0_SCPH));
289 }
290 
291 static FORCEINLINE void spi_polarity_0(uint32_t spi_base)
292 {
293  WR_WORD((spi_base + SPI_REG_CTRL0_OFS), (RD_WORD(spi_base + SPI_REG_CTRL0_OFS) & ~SPI_CTL0_SCPOL));
294 }
295 
296 static FORCEINLINE void spi_polarity_1(uint32_t spi_base)
297 {
298  WR_WORD((spi_base + SPI_REG_CTRL0_OFS), (RD_WORD(spi_base + SPI_REG_CTRL0_OFS) | SPI_CTL0_SCPOL));
299 }
300 
301 static FORCEINLINE void spi_tmod(uint32_t spi_base, int tmod)
302 {
303  WR_WORD((spi_base + SPI_REG_CTRL0_OFS), ((RD_WORD(spi_base + SPI_REG_CTRL0_OFS) & ~SPI_CTL0_TMOD) | (tmod << 8)));
304 }
305 
306 static FORCEINLINE void spi_dfs(uint32_t spi_base, int len)
307 {
308  WR_WORD((spi_base + SPI_REG_CTRL0_OFS), ((RD_WORD(spi_base + SPI_REG_CTRL0_OFS) & ~SPI_CTL0_DFS) | (((len - 1)&0x1F)<<16)));
309 }
310 
311 static FORCEINLINE void spi_frf(uint32_t spi_base, int fmt)
312 {
313  WR_WORD((spi_base + SPI_REG_CTRL0_OFS), ((RD_WORD(spi_base + SPI_REG_CTRL0_OFS) & ~SPI_CTL0_FRF) | ((fmt&0x3)<<21)));
314 }
315 
316 static FORCEINLINE void spi_ndf(uint32_t spi_base, uint16_t ndf)
317 {
318  WR_WORD((spi_base + SPI_REG_CTRL1_OFS), ((RD_WORD(spi_base + SPI_REG_CTRL1_OFS) & ~SPI_CTL1_NDF)|ndf));
319 }
320 
321 static FORCEINLINE void spi_enable(uint32_t spi_base)
322 {
323  WR_WORD((spi_base + SPI_REG_SSIEN_OFS), (RD_WORD(spi_base + SPI_REG_SSIEN_OFS) |1UL));
324 }
325 
326 static FORCEINLINE void spi_disable(uint32_t spi_base)
327 {
328  WR_WORD((spi_base + SPI_REG_SSIEN_OFS), (RD_WORD(spi_base + SPI_REG_SSIEN_OFS) & ~1UL));
329 }
330 
331 static FORCEINLINE void spi_ser_en(uint32_t spi_base, int bit)
332 {
333  WR_WORD((spi_base + SPI_REG_SE_OFS), (RD_WORD(spi_base + SPI_REG_SE_OFS)|(1<<bit)));
334 }
335 
336 static FORCEINLINE void spi_ser_dis(uint32_t spi_base, int bit)
337 {
338  WR_WORD((spi_base + SPI_REG_SE_OFS), (RD_WORD(spi_base + SPI_REG_SE_OFS)&~(1<<bit)));
339 }
340 
341 static INLINE void spi_ser(uint32_t spi_base, int enable, int bit)
342 {
343  uint32_t reg = RD_WORD(spi_base + SPI_REG_SE_OFS);
344 
345  if (enable) {
346  reg &= ~SPI_SE_EN;
347  reg |= (1 << (bit & 0x3));
348  } else {
349  reg &= ~SPI_SE_EN;
350  }
351 
352  WR_WORD((spi_base + SPI_REG_SE_OFS), reg);
353 }
354 
355 #if CFG_FPGA
356 static INLINE void spi_fpga_cs(int ss, int low)
357 {
358  uint32_t reg = RD_WORD(FPGA_SPI_FLASH_CTRL);
359 
360  if (((reg >> 8) & 0xF) != 1) {
361  reg |= (1 << 8);
362  }
363 
364  if (low) {
365  reg &= ~(1<<((ss & 0xF) + 4));
366  } else {
367  reg |= (1<<((ss & 0xF) + 4));
368  }
369 
370  WR_WORD(FPGA_SPI_FLASH_CTRL, reg);
371 }
372 #endif
373 
374 static INLINE void spi_baud_rate(uint32_t spi_base, uint32_t clk, uint32_t spi_clk)
375 {
376  uint16_t div = clk/spi_clk;
377  if (div < 2)
378  div = 2;
379  if (div & 1)
380  div += 1;
381 
382  WR_WORD((spi_base + SPI_REG_BAUD_OFS), div);
383 }
384 
385 static FORCEINLINE void spi_txftl(uint32_t spi_base, uint8_t tl)
386 {
387  WR_WORD((spi_base + SPI_REG_TXFTL_OFS), tl);
388 }
389 
390 static INLINE void spi_rxftl(uint32_t spi_base, uint8_t tl)
391 {
392  WR_WORD((spi_base + SPI_REG_RXFTL_OFS), tl);
393 }
394 
395 static INLINE uint8_t spi_txfl(uint32_t spi_base)
396 {
397  return (RD_WORD(spi_base + SPI_REG_TXFL_OFS)/* & 0xF*/);
398 }
399 
400 static INLINE uint8_t spi_rxfl(uint32_t spi_base)
401 {
402  return (RD_WORD(spi_base + SPI_REG_RXFL_OFS) & 0xF);
403 }
404 
405 static INLINE uint32_t spi_sr(uint32_t spi_base)
406 {
407  return (RD_WORD(spi_base + SPI_REG_SR_OFS));
408 }
409 
410 static FORCEINLINE void spi_intr_mask(uint32_t spi_base, uint32_t mask)
411 {
412  WR_WORD((spi_base + SPI_REG_IMR_OFS), (RD_WORD(spi_base + SPI_REG_IMR_OFS) & ~mask));
413 }
414 
415 static FORCEINLINE void spi_intr_unmask(uint32_t spi_base, uint32_t mask)
416 {
417  WR_WORD((spi_base + SPI_REG_IMR_OFS), (RD_WORD(spi_base + SPI_REG_IMR_OFS)|mask));
418 }
419 
420 static FORCEINLINE uint32_t spi_intr_status(uint32_t spi_base)
421 {
422  return RD_WORD(spi_base + SPI_REG_ISR_OFS);
423 }
424 
425 static FORCEINLINE uint32_t spi_intr_raw_status(uint32_t spi_base)
426 {
427  return RD_WORD(spi_base + SPI_REG_RISR_OFS);
428 }
429 
430 static FORCEINLINE uint32_t spi_intr_txo_clr(uint32_t spi_base)
431 {
432  return RD_WORD(spi_base + SPI_REG_TXOIC_OFS);
433 }
434 
435 static FORCEINLINE uint32_t spi_intr_rxo_clr(uint32_t spi_base)
436 {
437  return RD_WORD(spi_base + SPI_REG_RXOIC_OFS);
438 }
439 
440 static FORCEINLINE uint32_t spi_intr_rxu_clr(uint32_t spi_base)
441 {
442  return RD_WORD(spi_base + SPI_REG_RXUIC_OFS);
443 }
444 
445 static FORCEINLINE uint32_t spi_intr_clr(uint32_t spi_base)
446 {
447  return RD_WORD(spi_base + SPI_REG_ICR_OFS);
448 }
449 
450 static FORCEINLINE void spi_txdma_enable(uint32_t spi_base)
451 {
452  WR_WORD(spi_base + SPI_REG_DMACR_OFS, (RD_WORD(spi_base + SPI_REG_DMACR_OFS)|0x2));
453 }
454 
455 static FORCEINLINE void spi_txdma_disable(uint32_t spi_base)
456 {
457  WR_WORD(spi_base + SPI_REG_DMACR_OFS, (RD_WORD(spi_base + SPI_REG_DMACR_OFS)&~0x2));
458 }
459 
460 static FORCEINLINE void spi_txdma_req_level(uint32_t spi_base, int level)
461 {
462  WR_WORD(spi_base + SPI_REG_DMATDL_OFS, (level & 0xFF));
463 }
464 
465 static FORCEINLINE void spi_rxdma_enable(uint32_t spi_base)
466 {
467  WR_WORD(spi_base + SPI_REG_DMACR_OFS, (RD_WORD(spi_base + SPI_REG_DMACR_OFS)|0x1));
468 }
469 
470 static FORCEINLINE void spi_rxdma_disable(uint32_t spi_base)
471 {
472  WR_WORD(spi_base + SPI_REG_DMACR_OFS, (RD_WORD(spi_base + SPI_REG_DMACR_OFS)&~0x1));
473 }
474 
475 static INLINE void spi_rxdma_req_level(uint32_t spi_base, int level)
476 {
477  if (level <= 0)
478  return;
479 
480  WR_WORD(spi_base + SPI_REG_DMARDL_OFS, ((level-1) & 0xFF));
481 }
482 
483 static FORCEINLINE void spi_dr_write(uint32_t spi_base, uint32_t data)
484 {
485  WR_WORD((spi_base + SPI_REG_DR_OFS), data);
486 }
487 
488 static FORCEINLINE uint32_t spi_dr_read(uint32_t spi_base)
489 {
490  return RD_WORD(spi_base + SPI_REG_DR_OFS);
491 }
492 
493 static FORCEINLINE void spi_rx_samp_dly(uint32_t spi_base, uint8_t delay)
494 {
495  WR_WORD((spi_base + SPI_REG_RX_SAMPLE_DLY), delay);
496 }
497 
498 static INLINE void qspi_spi_ctl(uint32_t spi_base, int wc, int ins_len, int adr_len, int tt)
499 {
500  uint32_t reg = RD_WORD(spi_base + SPI_REG_DUAL_QUAD_CTRL_OFS);
501 
502  reg &= ~SPI_DUAL_QUAD_WAIT_CYCLES;
503  reg |= ((wc&0xF) << 11);
504 
505  reg &= ~SPI_DUAL_QUAD_INS_LEN;
506  reg |= ((ins_len&0x3) << 8);
507 
508  reg &= ~SPI_DUAL_QUAD_ADDR_LEN;
509  reg |= ((adr_len&0xF) << 2);
510 
511  reg &= ~SPI_DUAL_QUAD_TRANS_TYPE;
512  reg |= (tt&0x3) ;
513 
514  WR_WORD((spi_base + SPI_REG_DUAL_QUAD_CTRL_OFS), reg);
515 }
516 
517 static INLINE void qspi_wait_cycle(uint32_t spi_base, int wc)
518 {
519 
520  uint32_t reg = RD_WORD(spi_base + SPI_REG_DUAL_QUAD_CTRL_OFS);
521 
522  reg &= ~SPI_DUAL_QUAD_WAIT_CYCLES;
523  reg |= ((wc&0xF) << 11);
524 
525  WR_WORD((spi_base + SPI_REG_DUAL_QUAD_CTRL_OFS), reg);
526 }
527 
528 static INLINE void qspi_cmd_width(uint32_t spi_base, int ins_len)
529 {
530  uint32_t reg = RD_WORD(spi_base + SPI_REG_DUAL_QUAD_CTRL_OFS);
531 
532  reg &= ~SPI_DUAL_QUAD_INS_LEN;
533  reg |= ((ins_len&0x3) << 8);
534 
535  WR_WORD((spi_base + SPI_REG_DUAL_QUAD_CTRL_OFS), reg);
536 }
537 
538 static INLINE void qspi_addr_width(uint32_t spi_base, int adr_len)
539 {
540  uint32_t reg = RD_WORD(spi_base + SPI_REG_DUAL_QUAD_CTRL_OFS);
541 
542  reg &= ~SPI_DUAL_QUAD_ADDR_LEN;
543  reg |= ((adr_len&0xF) << 2);
544 
545  WR_WORD((spi_base + SPI_REG_DUAL_QUAD_CTRL_OFS), reg);
546 }
547 
548 static INLINE void qspi_trans_type(uint32_t spi_base, int tt)
549 {
550  uint32_t reg = RD_WORD(spi_base + SPI_REG_DUAL_QUAD_CTRL_OFS);
551 
552  reg &= ~SPI_DUAL_QUAD_TRANS_TYPE;
553  reg |= (tt&0x3) ;
554 
555  WR_WORD((spi_base + SPI_REG_DUAL_QUAD_CTRL_OFS), reg);
556 }
557 
558 static INLINE void qspi_single_wire_mode(int single)
559 {
560 #if CFG_FPGA
561  if (single)
562  WR_WORD(FPGA_REG_FPGA_SPI_M0_CTRL, 1);
563  else
564  WR_WORD(FPGA_REG_FPGA_SPI_M0_CTRL, 0);
565 #endif
566 }
567 
568 static INLINE void qspi_xip_enable(int addr_width, int wait_cycle, int tran_type)
569 {
570  uint32_t reg = SPIFLASH_REG_SPIFLASH_MISC_CTRL_CTL_SPI_APB_MUX_SEL |
571  SPIFLASH_REG_SPIFLASH_MISC_CTRL_CTL_SPI_FLASH_ENDIAN_PRDATA_SWAP |
572  ((addr_width & SPIFLASH_REG_SPIFLASH_MISC_CTRL_CTL_SPI_FLASH_ADDR_LENGTH_MASK) << SPIFLASH_REG_SPIFLASH_MISC_CTRL_CTL_SPI_FLASH_ADDR_LENGTH_SHIFT) |
573  ((wait_cycle & SPIFLASH_REG_SPIFLASH_MISC_CTRL_CTL_SPI_WAIT_CYCLE_MASK) << SPIFLASH_REG_SPIFLASH_MISC_CTRL_CTL_SPI_WAIT_CYCLE_SHIFT) |
574  ((tran_type & SPIFLASH_REG_SPIFLASH_MISC_CTRL_CTL_SPI_TRANS_TYPE_MASK) << SPIFLASH_REG_SPIFLASH_MISC_CTRL_CTL_SPI_TRANS_TYPE_SHIFT) |
575  SPIFLASH_REG_SPIFLASH_MISC_CTRL_CTL_SPI_FLASH_ADDR_SRC;
576 
577  WR_WORD(SPIFLASH_REG_SPIFLASH_MISC_CTRL, reg);
578 }
579 
580 static INLINE void qspi_xip_disable(void)
581 {
582  WR_WORD(SPIFLASH_REG_SPIFLASH_MISC_CTRL, SPIFLASH_REG_SPIFLASH_MISC_CTRL_DEFAULT);
583 }
584 
585 static INLINE void qspi_xip_ins(uint32_t ins)
586 {
587  WR_WORD(SPIFLASH_REG_SPIFLASH_INSTRUCTION, ins);
588 }
589 
590 static INLINE void qspi_xip_ssn(int ssn)
591 {
592  WR_WORD(SPIFLASH_REG_SPIFLASH_SLV_SEL, (ssn & SPIFLASH_REG_SPIFLASH_SLV_SEL_CTL_SPI_FLASH_SLAVE_SEL_MASK));
593 }
594 static INLINE void qspi_addr_set_offset(uint32_t offset)
595 {
596  WR_WORD(SPIFLASH_REG_SPIFLASH_ADDR_OFFSET, offset);
597 }
598 static INLINE uint32_t qspi_addr_get_offset(void)
599 {
600  return RD_WORD(SPIFLASH_REG_SPIFLASH_ADDR_OFFSET);
601 }
602 static INLINE void spi_flash_icache_dec(int en)
603 {
604  if (en) {
605  WR_WORD(SPIFLASH_REG_ICACHE_DEC_MISC_CTRL, 1);
606  } else {
607  WR_WORD(SPIFLASH_REG_ICACHE_DEC_MISC_CTRL, 0);
608  }
609 }
610 
611 static INLINE void spi_reset(int id)
612 {
613  uint32_t reg = RD_WORD(GLOBAL_REG_RESET_CTRL_1);
614 
615  // Toggle 1-0-1 to reset
616  if (id == MSPI_ID) {
617  reg &= ~GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_SSI_MASTER1_SSI_RSTN_REG;
618  } else {
619  if (id == SSPI0_ID)
620  reg &= ~GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_SSI_SLAVE0_SSI_RSTN_REG;
621  else
622  reg &= ~GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_SSI_SLAVE1_SSI_RSTN_REG;
623  }
624  WR_WORD(GLOBAL_REG_RESET_CTRL_1, reg);
625  if (id == MSPI_ID) {
626  reg |= GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_SSI_MASTER1_SSI_RSTN_REG;
627  } else {
628  if (id == SSPI0_ID)
629  reg |= GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_SSI_SLAVE0_SSI_RSTN_REG;
630  else
631  reg |= GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_SSI_SLAVE1_SSI_RSTN_REG;
632  }
633  WR_WORD(GLOBAL_REG_RESET_CTRL_1, reg);
634 }
635 
636 static INLINE int qspi_is_xip_enable(void)
637 {
638  return ((RD_WORD(SPIFLASH_REG_SPIFLASH_MISC_CTRL) & SPIFLASH_REG_SPIFLASH_MISC_CTRL_CTL_SPI_APB_MUX_SEL) ? 1:0);
639 }
640 
641 
642 static INLINE void qspi_reset(void)
643 {
644  uint32_t reg = RD_WORD(GLOBAL_REG_RESET_CTRL_1);
645 
646  // Toggle 1-0-1 to reset
647  reg &= ~GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_SSI_MASTER0_SSI_RSTN_REG;
648  WR_WORD(GLOBAL_REG_RESET_CTRL_1, reg);
649  reg |= GLOBAL_REG_RESET_CTRL_1_CTL_RESET_1_D0_CPU_SSI_MASTER0_SSI_RSTN_REG;
650  WR_WORD(GLOBAL_REG_RESET_CTRL_1, reg);
651 }
652 
653 
654 /*
655  * API Functions
656  ****************************************************************************************
657  */
658 
668 //void *hal_spi_open(int id);
669 //void *hal_spi_open(int id, int speed, int phase, int polarity, int tmod, int dfs, int prio, void *arg, void (*callback)(void *arg, int status, int nbytes));
670 void *hal_spi_open(int id, int speed, int phase, int polarity, int prio);
671 //void *hal_spi_open_dma(int id, int speed, int phase, int polarity, int tmod, int prio);
672 void *hal_spi_open_dma(int id, int speed, int phase, int polarity, int prio);
673 
674 
684 int hal_spi_close(void *hdl);
685 int hal_spi_close_dma(void *hdl);
686 
697 int hal_spi_int_prio(void *hdl, int prio);
722 //int hal_spi_tx(void *hdl, int cs, int speed, int phase, int polarity, int dfs, void *buffer, uint16_t buffer_len);
723 //int hal_spi_tx(void *hdl, int cs, void *buffer, uint16_t buffer_len, uint16_t *actual_size);
724 int hal_spi_tx_non_block(void *hdl, int cs, int dfs, void *buffer, uint16_t buffer_len, void *arg, void (*callback)(void *arg, int status, uint16_t size));
725 int hal_spi_tx_block(void *hdl, int cs, int dfs, void *buffer, uint16_t buffer_len, uint32_t tmo, uint16_t *actual_size);
726 int hal_spi_tx_dma_block(void *hdl, int cs, int dfs, void *buffer, uint16_t buffer_len, uint32_t tmo, uint16_t *actual_size);
727 int hal_spi_tx_dma_non_block(void *hdl, int cs, int dfs, void *buffer, uint16_t buffer_len, void *arg, void (*callback)(void *arg, int status, uint16_t actual_size));
728 
729 
751 //int hal_spi_rx(void *hdl, int cs, int speed, int phase, int polarity, int dfs, void *buffer, uint16_t buffer_len);
752 //int hal_spi_rx(void *hdl, int cs, void *buffer, uint16_t buffer_len, uint16_t *actual_size);
753 int hal_spi_rx_non_block(void *hdl, int cs, int dfs, void *buffer, uint16_t buffer_len, void *arg, void (*callback)(void *arg, int status, uint16_t size));
754 int hal_spi_rx_block(void *hdl, int cs, int dfs, void *buffer, uint16_t buffer_len, uint32_t tmo, uint16_t *actual_size);
755 int hal_spi_rx_dma_block(void *hdl, int cs, int dfs, void *buffer, uint16_t buffer_len, uint32_t tmo, uint16_t *actual_size);
756 int hal_spi_rx_dma_non_block(void *hdl, int cs, int dfs, void *buffer, uint16_t buffer_len, void *arg, void (*callback)(void *arg, int status, uint16_t actual_size));
757 
758 
784 //int hal_spi_trx(void *hdl, int cs, int speed, int phase, int polarity, int dfs, void *wbuf, void *rbuf, uint16_t buf_len);
785 //int hal_spi_trx(void *hdl, int cs, void *wbuf, void *rbuf, uint16_t buf_len, uint16_t *actual_size);
786 int hal_spi_trx_non_block(void *hdl, int cs, int dfs, void *wbuf, void *rbuf, uint16_t buf_len, void *arg, void (*callback)(void *arg, int status, uint16_t actual_size));
787 int hal_spi_trx_block(void *hdl, int cs, int dfs, void *wbuf, void *rbuf, uint16_t buf_len, uint32_t tmo, uint16_t *actual_size);
788 int hal_spi_trx_dma_block(void *hdl, int cs, int dfs, void *wbuf, void *rbuf, uint16_t buffer_len, uint32_t tmo, uint16_t *actual_size);
789 int hal_spi_trx_dma_non_block(void *hdl, int cs, int dfs, void *wbuf, void *rbuf, uint16_t buffer_len, void *arg, void (*callback)(void *arg, int status, uint16_t actual_size));
790 
791 
816 //int hal_spi_tx_dma(void *hdl, int cs, int speed, int phase, int polarity, int dfs, void *buffer, uint16_t buffer_len);
817 int hal_spi_tx_dma(void *hdl, int cs, void *buffer, uint16_t buffer_len, uint16_t *actual_size);
818 
840 //int hal_spi_rx_dma(void *hdl, int cs, int speed, int phase, int polarity, int dfs, void *buffer, uint16_t buffer_len);
841 int hal_spi_rx_dma(void *hdl, int cs, void *buffer, uint16_t buffer_len, uint16_t *actual_size);
842 
868 //int hal_spi_trx_dma(void *hdl, int cs, int speed, int phase, int polarity, int dfs, void *wbuf, void *rbuf, uint16_t buffer_len);
869 int hal_spi_trx_dma(void *hdl, int cs, void *wbuf, void *rbuf, uint16_t buffer_len, uint16_t *actual_size);
870 
871 
898 int hal_slv_spi_tx(void *hdl, int phase, int polarity, int dfs, void *buf, uint16_t buf_len, uint16_t *wlen);
920 int hal_slv_spi_rx(void *hdl, int phase, int polarity, int dfs, void *buf, uint16_t buf_len, uint16_t *rlen);
921 
947 int hal_slv_spi_trx(void *hdl, int phase, int polarity, int dfs, void *wbuf, void *rbuf, uint16_t buf_len, uint16_t* wlen, uint16_t *rlen);
948 
959 void hal_slv_spi_stop(void *hdl);
960 
961 
972 int hal_spi_busy(void *hdl);
973 
1003 int hal_spi_trx_dma_chain_start(void *hdl, int cs, int speed, int phase, int polarity, int dfs, void *wbuf[2], void *rbuf[2], uint16_t buffer_len, void *arg, void (*callback)(void *arg, int dir, int id, int status));
1004 
1016 int hal_spi_trx_dma_chain_stop(void *hdl);
1017 
1018 
1019 int hal_spiflash_trx_dma(void *h, int cs, int speed, void *wbuf, uint16_t wbuf_len, void *rbuf, uint16_t rbuf_len, void *arg, void (*comp_cb)(void *arg, int status));
1020 
1021 
1023 
1024 #endif // HAL_SPI_H
int hal_spi_close(void *hdl)
Close the SPI driver.
qspi_addr_width
Dual or Quad SPI address width.
Definition: hal_spi.h:211
int hal_spi_trx_dma_chain_start(void *hdl, int cs, int speed, int phase, int polarity, int dfs, void *wbuf[2], void *rbuf[2], uint16_t buffer_len, void *arg, void(*callback)(void *arg, int dir, int id, int status))
Master SPI Transmit and Receive DMA block chain function (bi-direction)
spi_id
SPI ID.
Definition: hal_spi.h:106
int hal_slv_spi_rx(void *hdl, int phase, int polarity, int dfs, void *buf, uint16_t buf_len, uint16_t *rlen)
Slave SPI Receive function.
Standard.
Definition: hal_spi.h:118
int hal_spi_trx_non_block(void *hdl, int cs, int dfs, void *wbuf, void *rbuf, uint16_t buf_len, void *arg, void(*callback)(void *arg, int status, uint16_t actual_size))
Master or Slave SPI Transmit and Receive function (bi-direction)
void hal_slv_spi_stop(void *hdl)
Slave SPI stop function.
qspi_ttype
Quad SPI command and address transfer type.
Definition: hal_spi.h:231
int hal_spi_busy(void *hdl)
Get SPI busy status.
spi_err
SPI error code.
Definition: hal_spi.h:173
spi_dir
SPI DMA direction.
Definition: hal_spi.h:188
Slave SPI Id.
Definition: hal_spi.h:110
Dual format with 2 data wire.
Definition: hal_spi.h:120
spi_mstr_cs
SPI CS (master only)
Definition: hal_spi.h:194
int hal_spi_tx_dma(void *hdl, int cs, void *buffer, uint16_t buffer_len, uint16_t *actual_size)
Master or Slave SPI transmit DMA function.
Master SPI Id.
Definition: hal_spi.h:108
qspi_cmd_width
Dual or Quad SPI command width.
Definition: hal_spi.h:203
int hal_spi_rx_dma(void *hdl, int cs, void *buffer, uint16_t buffer_len, uint16_t *actual_size)
Master or Slave SPI Receive DMA function.
int hal_spi_int_prio(void *hdl, int prio)
Set SPI interrupt priority.
int hal_spi_rx_non_block(void *hdl, int cs, int dfs, void *buffer, uint16_t buffer_len, void *arg, void(*callback)(void *arg, int status, uint16_t size))
Master or Slave SPI Receive function.
Quad format with 4 data wire.
Definition: hal_spi.h:122
int hal_slv_spi_trx(void *hdl, int phase, int polarity, int dfs, void *wbuf, void *rbuf, uint16_t buf_len, uint16_t *wlen, uint16_t *rlen)
Slave SPI Transmit and Receive function (bi-direction)
Tranfer in TX mode only.
Definition: hal_spi.h:130
int hal_spi_trx_dma(void *hdl, int cs, void *wbuf, void *rbuf, uint16_t buffer_len, uint16_t *actual_size)
Master or Slave SPI Transmit and Receive DMA function (bi-direction)
Transfer in RX mode only.
Definition: hal_spi.h:132
Slave SPI Id.
Definition: hal_spi.h:112
spi_tmod
SPI transfer mode.
Definition: hal_spi.h:126
Transfer in bi-direction mode.
Definition: hal_spi.h:128
spi_dfs_bit
SPI data frame size.
Definition: hal_spi.h:139
Transfer in EEPROM.
Definition: hal_spi.h:134
spi_fmt
SPI format.
Definition: hal_spi.h:116
int hal_slv_spi_tx(void *hdl, int phase, int polarity, int dfs, void *buf, uint16_t buf_len, uint16_t *wlen)
Slave SPI transmit function.
int hal_spi_trx_dma_chain_stop(void *hdl)
Master or Slave SPI Transmit and Receive DMA block chain function (bi-direction)
int hal_spi_tx_non_block(void *hdl, int cs, int dfs, void *buffer, uint16_t buffer_len, void *arg, void(*callback)(void *arg, int status, uint16_t size))
Master or Slave SPI transmit function.
void * hal_spi_open(int id, int speed, int phase, int polarity, int prio)
Open the SPI driver.