cmt_spi3.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "cmt_spi3.h"
  2. #include "myRadio_gpio.h"
  3. /* ************************************************************************
  4. * The following need to be modified by user
  5. * ************************************************************************ */
  6. // #define cmt_spi3_csb_out() SET_GPIO_OUT(CMT_CSB_GPIO)
  7. // #define cmt_spi3_fcsb_out() SET_GPIO_OUT(CMT_FCSB_GPIO)
  8. // #define cmt_spi3_scl_out() SET_GPIO_OUT(CMT_SCL_GPIO)
  9. // #define cmt_spi3_sda_out() SET_GPIO_OUT(CMT_SDA_GPIO)
  10. // #define cmt_spi3_sda_in() SET_GPIO_IN(CMT_SDA_GPIO)
  11. // #define cmt_spi3_csb_1() SET_GPIO_H(CMT_CSB_GPIO)
  12. // #define cmt_spi3_csb_0() SET_GPIO_L(CMT_CSB_GPIO)
  13. // #define cmt_spi3_fcsb_1() SET_GPIO_H(CMT_FCSB_GPIO)
  14. // #define cmt_spi3_fcsb_0() SET_GPIO_L(CMT_FCSB_GPIO)
  15. // #define cmt_spi3_scl_1() SET_GPIO_H(CMT_SCL_GPIO)
  16. // #define cmt_spi3_scl_0() SET_GPIO_L(CMT_SCL_GPIO)
  17. // #define cmt_spi3_sda_1() SET_GPIO_H(CMT_SDA_GPIO)
  18. // #define cmt_spi3_sda_0() SET_GPIO_L(CMT_SDA_GPIO)
  19. // #define cmt_spi3_sda_read() READ_GPIO_PIN(CMT_SDA_GPIO)
  20. /* ************************************************************************ */
  21. // void cmt_spi3_delay(void)
  22. // {
  23. // uint32_t n = 7;
  24. // while(n--);
  25. // }
  26. // void cmt_spi3_delay_us(void)
  27. // {
  28. // uint16_t n = 8;
  29. // while(n--);
  30. // }
  31. void cmt_spi3_init(void)
  32. {
  33. // cmt_spi3_csb_1();
  34. // cmt_spi3_csb_out();
  35. // cmt_spi3_csb_1(); /* CSB has an internal pull-up resistor */
  36. // cmt_spi3_scl_0();
  37. // cmt_spi3_scl_out();
  38. // cmt_spi3_scl_0(); /* SCL has an internal pull-down resistor */
  39. // cmt_spi3_sda_1();
  40. // cmt_spi3_sda_out();
  41. // cmt_spi3_sda_1();
  42. // cmt_spi3_fcsb_1();
  43. // cmt_spi3_fcsb_out();
  44. // cmt_spi3_fcsb_1(); /* FCSB has an internal pull-up resistor */
  45. // cmt_spi3_delay();
  46. }
  47. // void cmt_spi3_send(uint8_t data8)
  48. // {
  49. // uint8_t i;
  50. // for(i=0; i<8; i++)
  51. // {
  52. // cmt_spi3_scl_0();
  53. // /* Send byte on the rising edge of SCL */
  54. // if(data8 & 0x80)
  55. // cmt_spi3_sda_1();
  56. // else
  57. // cmt_spi3_sda_0();
  58. // cmt_spi3_delay();
  59. // data8 <<= 1;
  60. // cmt_spi3_scl_1();
  61. // cmt_spi3_delay();
  62. // }
  63. // }
  64. // uint8_t cmt_spi3_recv(void)
  65. // {
  66. // uint8_t i;
  67. // uint8_t data8 = 0xFF;
  68. // // for(i=0; i<8; i++)
  69. // // {
  70. // // cmt_spi3_scl_0();
  71. // // cmt_spi3_delay();
  72. // // data8 <<= 1;
  73. // // cmt_spi3_scl_1();
  74. // // /* Read byte on the rising edge of SCL */
  75. // // if(cmt_spi3_sda_read())
  76. // // data8 |= 0x01;
  77. // // else
  78. // // data8 &= ~0x01;
  79. // // cmt_spi3_delay();
  80. // // }
  81. // return data8;
  82. // }
  83. void cmt_spi3_write(uint8_t addr, uint8_t dat)
  84. {
  85. cmt_spi3_sda_1();
  86. cmt_spi3_sda_out();
  87. cmt_spi3_scl_0();
  88. cmt_spi3_scl_out();
  89. cmt_spi3_scl_0();
  90. cmt_spi3_fcsb_1();
  91. // cmt_spi3_fcsb_out();
  92. cmt_spi3_fcsb_1();
  93. cmt_spi3_csb_0();
  94. /* > 0.5 SCL cycle */
  95. cmt_spi3_delay();
  96. cmt_spi3_delay();
  97. /* r/w = 0 */
  98. cmt_spi3_send(addr&0x7F);
  99. cmt_spi3_send(dat);
  100. cmt_spi3_scl_0();
  101. /* > 0.5 SCL cycle */
  102. cmt_spi3_delay();
  103. cmt_spi3_delay();
  104. cmt_spi3_csb_1();
  105. cmt_spi3_sda_1();
  106. cmt_spi3_sda_in();
  107. cmt_spi3_fcsb_1();
  108. }
  109. void cmt_spi3_read(uint8_t addr, uint8_t* p_dat)
  110. {
  111. cmt_spi3_sda_1();
  112. cmt_spi3_sda_out();
  113. cmt_spi3_scl_0();
  114. cmt_spi3_scl_out();
  115. cmt_spi3_scl_0();
  116. cmt_spi3_fcsb_1();
  117. // cmt_spi3_fcsb_out();
  118. cmt_spi3_fcsb_1();
  119. cmt_spi3_csb_0();
  120. /* > 0.5 SCL cycle */
  121. cmt_spi3_delay();
  122. cmt_spi3_delay();
  123. /* r/w = 1 */
  124. cmt_spi3_send(addr|0x80);
  125. /* Must set SDA to input before the falling edge of SCL */
  126. cmt_spi3_sda_in();
  127. *p_dat = cmt_spi3_recv();
  128. cmt_spi3_scl_0();
  129. /* > 0.5 SCL cycle */
  130. cmt_spi3_delay();
  131. cmt_spi3_delay();
  132. cmt_spi3_csb_1();
  133. cmt_spi3_sda_1();
  134. cmt_spi3_sda_in();
  135. cmt_spi3_fcsb_1();
  136. }
  137. void cmt_spi3_write_fifo(const uint8_t* p_buf, uint16_t len)
  138. {
  139. uint16_t i;
  140. cmt_spi3_fcsb_1();
  141. // cmt_spi3_fcsb_out();
  142. cmt_spi3_fcsb_1();
  143. cmt_spi3_csb_1();
  144. // cmt_spi3_csb_out();
  145. cmt_spi3_csb_1();
  146. cmt_spi3_scl_0();
  147. cmt_spi3_scl_out();
  148. cmt_spi3_scl_0();
  149. cmt_spi3_sda_out();
  150. for(i=0; i<len; i++)
  151. {
  152. cmt_spi3_fcsb_0();
  153. /* > 1 SCL cycle */
  154. cmt_spi3_delay();
  155. cmt_spi3_delay();
  156. cmt_spi3_send(p_buf[i]);
  157. cmt_spi3_scl_0();
  158. /* > 2 us */
  159. cmt_spi3_delay_us();
  160. cmt_spi3_delay_us();
  161. cmt_spi3_delay_us();
  162. cmt_spi3_fcsb_1();
  163. /* > 4 us */
  164. cmt_spi3_delay_us();
  165. cmt_spi3_delay_us();
  166. cmt_spi3_delay_us();
  167. cmt_spi3_delay_us();
  168. cmt_spi3_delay_us();
  169. cmt_spi3_delay_us();
  170. }
  171. cmt_spi3_sda_in();
  172. cmt_spi3_fcsb_1();
  173. }
  174. void cmt_spi3_read_fifo(uint8_t* p_buf, uint16_t len)
  175. {
  176. uint16_t i;
  177. cmt_spi3_fcsb_1();
  178. // cmt_spi3_fcsb_out();
  179. cmt_spi3_fcsb_1();
  180. cmt_spi3_csb_1();
  181. // cmt_spi3_csb_out();
  182. cmt_spi3_csb_1();
  183. cmt_spi3_scl_0();
  184. cmt_spi3_scl_out();
  185. cmt_spi3_scl_0();
  186. cmt_spi3_sda_in();
  187. for(i=0; i<len; i++)
  188. {
  189. cmt_spi3_fcsb_0();
  190. /* > 1 SCL cycle */
  191. cmt_spi3_delay();
  192. cmt_spi3_delay();
  193. p_buf[i] = cmt_spi3_recv();
  194. cmt_spi3_scl_0();
  195. /* > 2 us */
  196. cmt_spi3_delay_us();
  197. cmt_spi3_delay_us();
  198. cmt_spi3_delay_us();
  199. cmt_spi3_fcsb_1();
  200. /* > 4 us */
  201. cmt_spi3_delay_us();
  202. cmt_spi3_delay_us();
  203. cmt_spi3_delay_us();
  204. cmt_spi3_delay_us();
  205. cmt_spi3_delay_us();
  206. cmt_spi3_delay_us();
  207. }
  208. cmt_spi3_sda_in();
  209. cmt_spi3_fcsb_1();
  210. }