dev_hardware_SPI.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*****************************************************************************
  2. * | File : dev_hardware_SPI.c
  3. * | Author : Waveshare team
  4. * | Function : Read and write /dev/SPI, hardware SPI
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2019-06-26
  9. * | Info : Basic version
  10. *
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #include "dev_hardware_SPI.h"
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <stdint.h>
  35. #include <unistd.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <getopt.h>
  39. #include <fcntl.h>
  40. #include <sys/ioctl.h>
  41. #include <linux/types.h>
  42. #include <linux/spi/spidev.h>
  43. HARDWARE_SPI hardware_SPI;
  44. static uint8_t bits = 8;
  45. #define SPI_CS_HIGH 0x04 //Chip select high
  46. #define SPI_LSB_FIRST 0x08 //LSB
  47. #define SPI_3WIRE 0x10 //3-wire mode SI and SO same line
  48. #define SPI_LOOP 0x20 //Loopback mode
  49. #define SPI_NO_CS 0x40 //A single device occupies one SPI bus, so there is no chip select
  50. #define SPI_READY 0x80 //Slave pull low to stop data transmission
  51. struct spi_ioc_transfer tr;
  52. /******************************************************************************
  53. function: SPI port initialization
  54. parameter:
  55. SPI_device : Device name
  56. Info:
  57. /dev/spidev0.0
  58. /dev/spidev0.1
  59. ******************************************************************************/
  60. void DEV_HARDWARE_SPI_begin(char *SPI_device)
  61. {
  62. //device
  63. int ret = 0;
  64. if((hardware_SPI.fd = open(SPI_device, O_RDWR )) < 0) {
  65. perror("Failed to open SPI device.\n");
  66. printf("Failed to open SPI device\r\n");
  67. DEV_HARDWARE_SPI_Debug("Failed to open SPI device\r\n");
  68. exit(1);
  69. } else {
  70. printf("open : %s\r\n", SPI_device);
  71. DEV_HARDWARE_SPI_Debug("open : %s\r\n", SPI_device);
  72. }
  73. hardware_SPI.mode = 0;
  74. ret = ioctl(hardware_SPI.fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  75. if (ret == -1) {
  76. printf("can't set bits per word\r\n");
  77. DEV_HARDWARE_SPI_Debug("can't set bits per word\r\n");
  78. }
  79. ret = ioctl(hardware_SPI.fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  80. if (ret == -1) {
  81. printf("can't get bits per word\r\n");
  82. DEV_HARDWARE_SPI_Debug("can't get bits per word\r\n");
  83. }
  84. tr.bits_per_word = bits;
  85. DEV_HARDWARE_SPI_Mode(SPI_MODE_0);
  86. DEV_HARDWARE_SPI_ChipSelect(SPI_CS_Mode_LOW);
  87. DEV_HARDWARE_SPI_SetBitOrder(SPI_BIT_ORDER_LSBFIRST);
  88. DEV_HARDWARE_SPI_setSpeed(20000000);
  89. DEV_HARDWARE_SPI_SetDataInterval(5);
  90. }
  91. void DEV_HARDWARE_SPI_beginSet(char *SPI_device, SPIMode mode, uint32_t speed)
  92. {
  93. //device
  94. int ret = 0;
  95. hardware_SPI.mode = 0;
  96. if((hardware_SPI.fd = open(SPI_device, O_RDWR )) < 0) {
  97. perror("Failed to open SPI device.\n");
  98. exit(1);
  99. } else {
  100. DEV_HARDWARE_SPI_Debug("open : %s\r\n", SPI_device);
  101. }
  102. ret = ioctl(hardware_SPI.fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
  103. if (ret == -1)
  104. DEV_HARDWARE_SPI_Debug("can't set bits per word\r\n");
  105. ret = ioctl(hardware_SPI.fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
  106. if (ret == -1)
  107. DEV_HARDWARE_SPI_Debug("can't get bits per word\r\n");
  108. DEV_HARDWARE_SPI_Mode(mode);
  109. DEV_HARDWARE_SPI_ChipSelect(SPI_CS_Mode_LOW);
  110. DEV_HARDWARE_SPI_setSpeed(speed);
  111. DEV_HARDWARE_SPI_SetDataInterval(0);
  112. }
  113. /******************************************************************************
  114. function: SPI device End
  115. parameter:
  116. Info:
  117. ******************************************************************************/
  118. void DEV_HARDWARE_SPI_end(void)
  119. {
  120. hardware_SPI.mode = 0;
  121. if (close(hardware_SPI.fd) != 0){
  122. DEV_HARDWARE_SPI_Debug("Failed to close SPI device\r\n");
  123. perror("Failed to close SPI device.\n");
  124. }
  125. }
  126. /******************************************************************************
  127. function: Set SPI speed
  128. parameter:
  129. Info: Return 1 success
  130. Return -1 failed
  131. ******************************************************************************/
  132. int DEV_HARDWARE_SPI_setSpeed(uint32_t speed)
  133. {
  134. uint32_t speed1 = hardware_SPI.speed;
  135. hardware_SPI.speed = speed;
  136. //Write speed
  137. if (ioctl(hardware_SPI.fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) == -1) {
  138. DEV_HARDWARE_SPI_Debug("can't set max speed hz\r\n");
  139. hardware_SPI.speed = speed1;//Setting failure rate unchanged
  140. return -1;
  141. }
  142. //Read the speed of just writing
  143. if (ioctl(hardware_SPI.fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) == -1) {
  144. DEV_HARDWARE_SPI_Debug("can't get max speed hz\r\n");
  145. hardware_SPI.speed = speed1;//Setting failure rate unchanged
  146. return -1;
  147. }
  148. hardware_SPI.speed = speed;
  149. tr.speed_hz = hardware_SPI.speed;
  150. return 1;
  151. }
  152. /******************************************************************************
  153. function: Set SPI Mode
  154. parameter:
  155. Info:
  156. SPIMode:
  157. SPI_MODE0
  158. SPI_MODE1
  159. SPI_MODE2
  160. SPI_MODE3
  161. Return :
  162. Return 1 success
  163. Return -1 failed
  164. ******************************************************************************/
  165. int DEV_HARDWARE_SPI_Mode(SPIMode mode)
  166. {
  167. hardware_SPI.mode &= 0xfC;//Clear low 2 digits
  168. hardware_SPI.mode |= mode;//Setting mode
  169. //Write device
  170. if (ioctl(hardware_SPI.fd, SPI_IOC_WR_MODE, &hardware_SPI.mode) == -1) {
  171. DEV_HARDWARE_SPI_Debug("can't set spi mode\r\n");
  172. return -1;
  173. }
  174. return 1;
  175. }
  176. /******************************************************************************
  177. function: Set SPI CS Enable
  178. parameter:
  179. Info:
  180. EN:
  181. DISABLE
  182. ENABLE
  183. Return :
  184. Return 1 success
  185. Return -1 failed
  186. ******************************************************************************/
  187. int DEV_HARDWARE_SPI_CSEN(SPICSEN EN)
  188. {
  189. if(EN == ENABLE){
  190. hardware_SPI.mode |= SPI_NO_CS;
  191. }else {
  192. hardware_SPI.mode &= ~SPI_NO_CS;
  193. }
  194. //Write device
  195. if (ioctl(hardware_SPI.fd, SPI_IOC_WR_MODE, &hardware_SPI.mode) == -1) {
  196. DEV_HARDWARE_SPI_Debug("can't set spi CS EN\r\n");
  197. return -1;
  198. }
  199. return 1;
  200. }
  201. /******************************************************************************
  202. function: Chip Select
  203. parameter:
  204. Info:
  205. CS_Mode:
  206. SPI_CS_Mode_LOW
  207. SPI_CS_Mode_HIGH
  208. SPI_CS_Mode_NONE
  209. Return :
  210. Return 1 success
  211. Return -1 failed
  212. ******************************************************************************/
  213. int DEV_HARDWARE_SPI_ChipSelect(SPIChipSelect CS_Mode)
  214. {
  215. if(CS_Mode == SPI_CS_Mode_HIGH){
  216. hardware_SPI.mode |= SPI_CS_HIGH;
  217. hardware_SPI.mode &= ~SPI_NO_CS;
  218. DEV_HARDWARE_SPI_Debug("CS HIGH \r\n");
  219. }else if(CS_Mode == SPI_CS_Mode_LOW){
  220. hardware_SPI.mode &= ~SPI_CS_HIGH;
  221. hardware_SPI.mode &= ~SPI_NO_CS;
  222. }else if(CS_Mode == SPI_CS_Mode_NONE){
  223. hardware_SPI.mode |= SPI_NO_CS;
  224. }
  225. if (ioctl(hardware_SPI.fd, SPI_IOC_WR_MODE, &hardware_SPI.mode) == -1) {
  226. DEV_HARDWARE_SPI_Debug("can't set spi mode\r\n");
  227. return -1;
  228. }
  229. return 1;
  230. }
  231. /******************************************************************************
  232. function: Sets the SPI bit order
  233. parameter:
  234. Info:
  235. Order:
  236. SPI_BIT_ORDER_LSBFIRST
  237. SPI_BIT_ORDER_MSBFIRST
  238. Return :
  239. Return 1 success
  240. Return -1 failed
  241. ******************************************************************************/
  242. int DEV_HARDWARE_SPI_SetBitOrder(SPIBitOrder Order)
  243. {
  244. if(Order == SPI_BIT_ORDER_LSBFIRST){
  245. hardware_SPI.mode |= SPI_LSB_FIRST;
  246. DEV_HARDWARE_SPI_Debug("SPI_LSB_FIRST\r\n");
  247. }else if(Order == SPI_BIT_ORDER_MSBFIRST){
  248. hardware_SPI.mode &= ~SPI_LSB_FIRST;
  249. DEV_HARDWARE_SPI_Debug("SPI_MSB_FIRST\r\n");
  250. }
  251. // DEV_HARDWARE_SPI_Debug("hardware_SPI.mode = 0x%02x\r\n", hardware_SPI.mode);
  252. int fd = ioctl(hardware_SPI.fd, SPI_IOC_WR_MODE, &hardware_SPI.mode);
  253. DEV_HARDWARE_SPI_Debug("fd = %d\r\n",fd);
  254. if (fd == -1) {
  255. DEV_HARDWARE_SPI_Debug("can't set spi SPI_LSB_FIRST\r\n");
  256. return -1;
  257. }
  258. return 1;
  259. }
  260. /******************************************************************************
  261. function: Sets the SPI Bus Mode
  262. parameter:
  263. Info:
  264. Order:
  265. SPI_3WIRE_Mode
  266. SPI_4WIRE_Mode
  267. Return :
  268. Return 1 success
  269. Return -1 failed
  270. ******************************************************************************/
  271. int DEV_HARDWARE_SPI_SetBusMode(BusMode mode)
  272. {
  273. if(mode == SPI_3WIRE_Mode){
  274. hardware_SPI.mode |= SPI_3WIRE;
  275. }else if(mode == SPI_4WIRE_Mode){
  276. hardware_SPI.mode &= ~SPI_3WIRE;
  277. }
  278. if (ioctl(hardware_SPI.fd, SPI_IOC_WR_MODE, &hardware_SPI.mode) == -1) {
  279. DEV_HARDWARE_SPI_Debug("can't set spi mode\r\n");
  280. return -1;
  281. }
  282. return 1;
  283. }
  284. /******************************************************************************
  285. function:
  286. Time interval after transmission of one byte during continuous transmission
  287. parameter:
  288. us : Interval time (us)
  289. Info:
  290. ******************************************************************************/
  291. void DEV_HARDWARE_SPI_SetDataInterval(uint16_t us)
  292. {
  293. hardware_SPI.delay = us;
  294. tr.delay_usecs = hardware_SPI.delay;
  295. }
  296. /******************************************************************************
  297. function: SPI port sends one byte of data
  298. parameter:
  299. buf : Sent data
  300. Info:
  301. ******************************************************************************/
  302. uint8_t DEV_HARDWARE_SPI_TransferByte(uint8_t buf)
  303. {
  304. uint8_t rbuf[1];
  305. tr.len = 1;
  306. tr.tx_buf = (unsigned long)&buf;
  307. tr.rx_buf = (unsigned long)rbuf;
  308. //ioctl Operation, transmission of data
  309. if ( ioctl(hardware_SPI.fd, SPI_IOC_MESSAGE(1), &tr) < 1 )
  310. DEV_HARDWARE_SPI_Debug("can't send spi message\r\n");
  311. return rbuf[0];
  312. }
  313. /******************************************************************************
  314. function: The SPI port reads a byte
  315. parameter:
  316. Info: Return read data
  317. ******************************************************************************/
  318. int DEV_HARDWARE_SPI_Transfer(uint8_t *buf, uint32_t len)
  319. {
  320. tr.len = len;
  321. tr.tx_buf = (unsigned long)buf;
  322. tr.rx_buf = (unsigned long)buf;
  323. //ioctl Operation, transmission of data
  324. if (ioctl(hardware_SPI.fd, SPI_IOC_MESSAGE(1), &tr) < 1 ){
  325. DEV_HARDWARE_SPI_Debug("can't send spi message\r\n");
  326. return -1;
  327. }
  328. return 1;
  329. }