ch56x_spi.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-30 Emuzit first version
  9. */
  10. #ifndef __CH56X_SPI_H__
  11. #define __CH56X_SPI_H__
  12. #include "soc.h"
  13. #include "ch56x_gpio.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define SPI0_BUS_NAME "spi0"
  18. #define SPI1_BUS_NAME "spi1"
  19. #ifndef SPI0_SCS_PIN
  20. #define SPI0_SCS_PIN GET_PIN(A, 12)
  21. #endif
  22. #define SPI0_SCK_PIN GET_PIN(A, 13)
  23. #define SPI0_MOSI_PIN GET_PIN(A, 14)
  24. #define SPI0_MISO_PIN GET_PIN(A, 15)
  25. #ifdef SOC_SERIES_CH569
  26. #ifndef SPI1_SCS_PIN
  27. #define SPI1_SCS_PIN GET_PIN(B, 11)
  28. #endif
  29. #define SPI1_SCK_PIN GET_PIN(B, 12)
  30. #define SPI1_MOSI_PIN GET_PIN(B, 13)
  31. #define SPI1_MISO_PIN GET_PIN(B, 14)
  32. #else
  33. #define SPI1_SCK_PIN GET_PIN(A, 0)
  34. #define SPI1_MOSI_PIN GET_PIN(A, 1)
  35. #define SPI1_MISO_PIN GET_PIN(A, 2)
  36. #endif
  37. #define SPI_FIFO_SIZE 8
  38. union _spi_ctrl_mod
  39. {
  40. uint8_t reg;
  41. struct
  42. {
  43. uint8_t mode_slave : 1; // RW, SPI master/slave (0/1) mode select
  44. uint8_t all_clear : 1; // RW, clear FIFO/count/int-flag
  45. uint8_t two_wire : 1; // RW, 2/3-wire mode (0/1), SPI slave
  46. uint8_t mst_sck_mod : 1; // RW, mode0/mode3 (0/1) for SCK idle L/H
  47. uint8_t fifo_dir : 1; // RW, FIFO direction is output/input (0/1)
  48. uint8_t sck_oe : 1; // RW, SCK pin output enable
  49. uint8_t mosi_oe : 1; // RW, MOSI pin output enable
  50. uint8_t miso_oe : 1; // RW, MISO pin output enable
  51. };
  52. struct
  53. {
  54. uint8_t stuff_0 : 3;
  55. uint8_t slv_cmd_mod : 1; // RW, 1st byte is data/cmd (0/1), SPI slave
  56. uint8_t stuff_4 : 4;
  57. };
  58. };
  59. #define RB_SPI_MODE_SLAVE 0x01
  60. #define RB_SPI_ALL_CLEAR 0x02
  61. #define RB_SPI_2WIRE_MOD 0x04
  62. #define RB_SPI_MST_SCK_MOD 0x08
  63. #define RB_SPI_SLV_CMD_MOD 0x08
  64. #define RB_SPI_FIFO_DIR 0x10
  65. #define RB_SPI_SCK_OE 0x20
  66. #define RB_SPI_MOSI_OE 0x40
  67. #define RB_SPI_MISO_OE 0x80
  68. #define MST_SCK_MOD_0 0
  69. #define MST_SCK_MOD_3 1
  70. #define SPI_FIFO_DIR_OUTPUT 0
  71. #define SPI_FIFO_DIR_INPUT 1
  72. union _spi_ctrl_cfg
  73. {
  74. uint8_t reg;
  75. struct
  76. {
  77. uint8_t dma_enable : 1; // RW, enable DMA function
  78. uint8_t resv_1 : 1;
  79. uint8_t dma_loop : 1; // RW, enable DMA loop mode (0 => single)
  80. uint8_t resv_3 : 1;
  81. uint8_t auto_if : 1; // RW, enable auto clear RB_SPI_IF_BYTE_END
  82. uint8_t bit_order : 1; // RW, data bit ordering, LSB/MSB first (0/1)
  83. uint8_t resv_6 : 2;
  84. };
  85. };
  86. #define RB_SPI_DMA_ENABLE 0x01
  87. #define RB_SPI_DMA_LOOP 0x04
  88. #define RB_SPI_AUTO_IF 0x10
  89. #define RB_SPI_BIT_ORDER 0x20
  90. #define SPI_BIT_ORDER_MSB 0
  91. #define SPI_BIT_ORDER_LSB 1
  92. union _spi_inter_en
  93. {
  94. uint8_t reg;
  95. struct
  96. {
  97. uint8_t cnt_end : 1; // RW, IE for all bytes transfered
  98. uint8_t byte_end : 1; // RW, IE for single byte transfered
  99. uint8_t fifo_hf : 1; // RW, IE for FIFO half full
  100. uint8_t dma_end : 1; // RW, IE for end of DMA
  101. uint8_t fifo_ov : 1; // RW, IE for FIFO full or empty
  102. uint8_t resv_5 : 2;
  103. uint8_t fst_byte : 1; // RW, IE for 1st byte received, SPI slave
  104. };
  105. };
  106. #define RB_SPI_IE_CNT_END 0x01
  107. #define RB_SPI_IE_BYTE_END 0x02
  108. #define RB_SPI_IE_FIFO_HF 0x04
  109. #define RB_SPI_IE_DMA_END 0x08
  110. #define RB_SPI_IE_FIFO_OV 0x10
  111. #define RB_SPI_IE_FST_BYTE 0x80
  112. union _spi_run_flag
  113. {
  114. uint8_t reg;
  115. struct
  116. {
  117. uint8_t resv_0 : 4;
  118. uint8_t slv_cmd_act : 1; // RO, SPI slave cmd received
  119. uint8_t fifo_ready : 1; // RO, SPI FIFO ready to transfer
  120. uint8_t slv_cs_load : 1; // RO, SPI slave is loading R8_SPIx_SLAVE_PRE
  121. uint8_t slv_select : 1; // RO, SPI slave CS active (selected)
  122. };
  123. };
  124. #define RB_SPI_SLV_CMD_ACT 0x10
  125. #define RB_SPI_FIFO_READY 0x20
  126. #define RB_SPI_SLV_CS_LOAD 0x40
  127. #define RB_SPI_SLV_SELECT 0x80
  128. union _spi_int_flag
  129. {
  130. uint8_t reg;
  131. struct
  132. {
  133. uint8_t cnt_end : 1; // RW1, IF for all bytes transfered
  134. uint8_t byte_end : 1; // RW1, IF for single byte transfered
  135. uint8_t fifo_hf : 1; // RW1, IF for FIFO half full
  136. uint8_t dma_end : 1; // RW1, IF for end of DMA
  137. uint8_t fifo_ov : 1; // RW1, IF for FIFO full or empty
  138. uint8_t resv_5 : 1;
  139. uint8_t free : 1; // RO, current SPI state is free
  140. uint8_t fst_byte : 1; // RW1, IF for 1st byte received, SPI slave
  141. };
  142. };
  143. #define RB_SPI_IF_CNT_END 0x01
  144. #define RB_SPI_IF_BYTE_END 0x02
  145. #define RB_SPI_IF_FIFO_HF 0x04
  146. #define RB_SPI_IF_DMA_END 0x08
  147. #define RB_SPI_IF_FIFO_OV 0x10
  148. #define RB_SPI_FREE 0x40
  149. #define RB_SPI_IF_FST_BYTE 0x80
  150. /*
  151. * 0x00 R8_SPIx_CTRL_MOD: SPI mode setting register
  152. * 0x01 R8_SPIx_CTRL_CFG: SPI configuration register
  153. * 0x02 R8_SPIx_INTER_EN: SPI interrupt enable register
  154. * 0x03 R8_SPIx_CLOCK_DIV: SPI master clock divisor, minimum 2
  155. * 0x03 R8_SPIx_SLAVE_PRE: SPI slave preset data (reset as 10h)
  156. * 0x04 R8_SPIx_BUFFER: SPI data buffer
  157. * 0x05 R8_SPIx_RUN_FLAG: SPI working state register
  158. * 0x06 R8_SPIx_INT_FLAG: SPI interrupt flags register
  159. * 0x07 R8_SPIx_FIFO_COUNT: SPI FIFO data count
  160. * 0x0c R16_SPIx_TOTAL_CNT: SPI total data length to transfer
  161. * 0x10 R8_SPIx_FIFO: SPI FIFO
  162. * 0x13 R8_SPIx_FIFO_COUNT1: SPI FIFO data count
  163. * 0x14 R32_SPIx_DMA_NOW: SPI DMA current address
  164. * 0x18 R32_SPIx_DMA_BEG: SPI DMA start address
  165. * 0x1c R32_SPIx_DMA_END: SPI DMA end address
  166. */
  167. struct spi_registers
  168. {
  169. union _spi_ctrl_mod CTRL_MOD;
  170. union _spi_ctrl_cfg CTRL_CFG;
  171. union _spi_inter_en INTER_EN;
  172. union
  173. {
  174. uint8_t CLOCK_DIV;
  175. uint8_t SLAVE_PRE;
  176. };
  177. uint8_t BUFFER;
  178. union _spi_run_flag RUN_FLAG;
  179. union _spi_int_flag INT_FLAG;
  180. uint8_t FIFO_COUNT;
  181. uint32_t resv_8;
  182. uint16_t TOTAL_COUNT;
  183. uint16_t resv_0e;
  184. uint8_t FIFO;
  185. uint8_t resv_11[2];
  186. uint8_t FIFO_COUNT1;
  187. uint32_t DMA_NOW;
  188. uint32_t DMA_BIG;
  189. uint32_t DMA_END;
  190. };
  191. CHECK_STRUCT_SIZE(struct spi_registers, 0x20);
  192. rt_err_t spi_cs_pin_assign(int spi_n, rt_base_t cs_pin);
  193. #ifdef __cplusplus
  194. }
  195. #endif
  196. #endif