kspi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include<linux/string.h>
  2. #include "defines.h"
  3. #include "kspi.h"
  4. #include "kfile.h"
  5. #include "ktiva.h"
  6. /////////////////////////////////////////////////////////////////////////////
  7. /////////////////////////////////////////////////////////////////////////////
  8. int kspi_write_mode(struct file *pf, unsigned char mode)
  9. {
  10. int ret;
  11. if((ret = kf_ioctl(pf, SPI_IOC_WR_MODE, (unsigned long)&mode)) < 0)
  12. {
  13. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  14. }
  15. return ret;
  16. }
  17. /////////////////////////////////////////////////////////////////////////////
  18. int kspi_read_mode(struct file *pf, unsigned char *mode)
  19. {
  20. int ret;
  21. if((ret = kf_ioctl(pf, SPI_IOC_RD_MODE, (unsigned long)mode)) < 0)
  22. {
  23. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  24. }
  25. return ret;
  26. }
  27. /////////////////////////////////////////////////////////////////////////////
  28. int kspi_write_bits_per_word(struct file *pf, unsigned char bits)
  29. {
  30. int ret;
  31. if((ret = kf_ioctl(pf, SPI_IOC_WR_BITS_PER_WORD, (unsigned long)&bits)) < 0)
  32. {
  33. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  34. }
  35. return ret;
  36. }
  37. /////////////////////////////////////////////////////////////////////////////
  38. int kspi_read_bits_per_word(struct file *pf, unsigned char *bits)
  39. {
  40. int ret;
  41. if((ret = kf_ioctl(pf, SPI_IOC_RD_BITS_PER_WORD, (unsigned long)bits)) < 0)
  42. {
  43. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  44. }
  45. return ret;
  46. }
  47. /////////////////////////////////////////////////////////////////////////////
  48. int kspi_write_max_speed_hz(struct file *pf, unsigned int speed)
  49. {
  50. int ret;
  51. if((ret = kf_ioctl(pf, SPI_IOC_WR_MAX_SPEED_HZ, (unsigned long)&speed)) < 0)
  52. {
  53. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  54. }
  55. return ret;
  56. }
  57. /////////////////////////////////////////////////////////////////////////////
  58. int kspi_read_max_speed_hz(struct file *pf, unsigned int *speed)
  59. {
  60. int ret;
  61. if((ret = kf_ioctl(pf, SPI_IOC_RD_MAX_SPEED_HZ, (unsigned long)speed)) < 0)
  62. {
  63. KALERT("%s: kf_ioctl failed: %d\n", __FUNCTION__, ret);
  64. }
  65. return ret;
  66. }
  67. /////////////////////////////////////////////////////////////////////////////
  68. /////////////////////////////////////////////////////////////////////////////
  69. /////////////////////////////////////////////////////////////////////////////
  70. int kspi_tx(struct file *pf, const void *pData, size_t nCbData)
  71. {
  72. int ret;
  73. struct spi_ioc_transfer tr =
  74. {
  75. .tx_buf = (unsigned long)pData,
  76. .rx_buf = 0,
  77. .len = nCbData,
  78. // .delay_usecs = 0,
  79. // .speed_hz = _SPI_SPEED_HZ,
  80. // .bits_per_word = _SPI_BITS_PER_WORD,
  81. };
  82. if((ret = kf_ioctl(pf, SPI_IOC_MESSAGE(1), (unsigned long)&tr)) < 0)
  83. KALERT("%s failed: %d\n", __FUNCTION__, ret);
  84. return ret;
  85. }
  86. /////////////////////////////////////////////////////////////////////////////
  87. int kspi_rx(struct file *pf, void *pData, size_t nCbData)
  88. {
  89. int ret;
  90. struct spi_ioc_transfer tr =
  91. {
  92. .tx_buf = 0,
  93. .rx_buf = (unsigned long)pData,
  94. .len = nCbData,
  95. // .delay_usecs = 0,
  96. // .speed_hz = _SPI_SPEED_HZ,
  97. // .bits_per_word = _SPI_BITS_PER_WORD,
  98. };
  99. if((ret = kf_ioctl(pf, SPI_IOC_MESSAGE(1), (unsigned long)&tr)) < 0)
  100. KALERT("%s failed: %d\n", __FUNCTION__, ret);
  101. return ret;
  102. }
  103. /////////////////////////////////////////////////////////////////////////////
  104. int kspi_tx_rx(struct file *pf, const void *pTx, void *pRx, size_t nCb)
  105. {
  106. int ret;
  107. struct spi_ioc_transfer tr =
  108. {
  109. .tx_buf = (unsigned long)pTx,
  110. .rx_buf = (unsigned long)pRx,
  111. .len = nCb,
  112. // .delay_usecs = 0,
  113. // .speed_hz = _SPI_SPEED_HZ,
  114. // .bits_per_word = _SPI_BITS_PER_WORD,
  115. };
  116. if((ret = kf_ioctl(pf, SPI_IOC_MESSAGE(1), (unsigned long)&tr)) < 0)
  117. KALERT("%s failed: %d\n", __FUNCTION__, ret);
  118. return ret;
  119. }
  120. /////////////////////////////////////////////////////////////////////////////
  121. int kspi_rx_byte(struct file *pf, unsigned char *rx)
  122. {
  123. return kspi_rx(pf, rx, 1);
  124. }
  125. /////////////////////////////////////////////////////////////////////////////
  126. int kspi_tx_byte(struct file *pf, unsigned char tx)
  127. {
  128. return kspi_tx(pf, &tx, 1);
  129. }
  130. /////////////////////////////////////////////////////////////////////////////
  131. int kspi_tx_rx_byte(struct file *pf, unsigned char tx, unsigned char *rx)
  132. {
  133. return kspi_tx_rx(pf, &tx, rx, 1);
  134. }
  135. /////////////////////////////////////////////////////////////////////////////
  136. bool KSpiInit(struct file *pf)
  137. {
  138. bool bRet = false;
  139. uint8_t mode = 0;
  140. uint8_t bits = 0;
  141. uint32_t speed = 0;
  142. do
  143. {
  144. if(kspi_write_mode(pf, SPI_MODE_3) < 0)
  145. {
  146. KALERT("%s: kspi_write_mode failed!\n", __FUNCTION__);
  147. break;
  148. }
  149. if(kspi_read_mode(pf, &mode) < 0)
  150. {
  151. KALERT("%s: kspi_read_mode failed!\n", __FUNCTION__);
  152. break;
  153. }
  154. if(mode != SPI_MODE_3)
  155. {
  156. KALERT("%s: Invalid mode!\n", __FUNCTION__);
  157. break;
  158. }
  159. /////////////////////////////////////////////////////////////////
  160. if(kspi_write_bits_per_word(pf, _SPI_BITS_PER_WORD) < 0)
  161. {
  162. KALERT("%s: kspi_write_bits_per_word failed!\n", __FUNCTION__);
  163. break;
  164. }
  165. if(kspi_read_bits_per_word(pf, &bits) < 0)
  166. {
  167. KALERT("%s: kspi_read_bits_per_word failed!\n", __FUNCTION__);
  168. break;
  169. }
  170. if(bits != _SPI_BITS_PER_WORD)
  171. {
  172. KALERT("%s: Invalid bits per word!\n", __FUNCTION__);
  173. break;
  174. }
  175. /////////////////////////////////////////////////////////////////
  176. if(kspi_write_max_speed_hz(pf, _SPI_SPEED_HZ) < 0)
  177. {
  178. KALERT("%s: kspi_write_max_speed_hz failed!\n", __FUNCTION__);
  179. break;
  180. }
  181. if(kspi_read_max_speed_hz(pf, &speed) < 0)
  182. {
  183. KALERT("%s: kspi_read_max_speed_hz failed!\n", __FUNCTION__);
  184. break;
  185. }
  186. if(speed != _SPI_SPEED_HZ)
  187. {
  188. KALERT("%s: Invalid max. speed!\n", __FUNCTION__);
  189. break;
  190. }
  191. bRet = true;
  192. }
  193. while(false);
  194. return bRet;
  195. }
  196. void KSpiExit(void)
  197. {
  198. }