DEV_Config.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*****************************************************************************
  2. * | File : DEV_Config.c
  3. * | Author : Waveshare team
  4. * | Function : Hardware underlying interface
  5. * | Info :
  6. *----------------
  7. * | This version: V3.0
  8. * | Date : 2019-07-31
  9. * | Info :
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documnetation files (the "Software"), to deal
  13. # in the Software without restriction, including without limitation the rights
  14. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. # copies of theex Software, and to permit persons to whom the Software is
  16. # furished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. # THE SOFTWARE.
  28. #
  29. ******************************************************************************/
  30. #include "DEV_Config.h"
  31. /**
  32. * GPIO
  33. **/
  34. int EPD_RST_PIN;
  35. int EPD_DC_PIN;
  36. int EPD_CS_PIN;
  37. int EPD_BUSY_PIN;
  38. int EPD_PWR_PIN;
  39. /**
  40. * GPIO read and write
  41. **/
  42. void DEV_Digital_Write(UWORD Pin, UBYTE Value)
  43. {
  44. #ifdef RPI
  45. #ifdef USE_BCM2835_LIB
  46. bcm2835_gpio_write(Pin, Value);
  47. #elif USE_WIRINGPI_LIB
  48. digitalWrite(Pin, Value);
  49. #elif USE_DEV_LIB
  50. SYSFS_GPIO_Write(Pin, Value);
  51. #endif
  52. #endif
  53. #ifdef JETSON
  54. #ifdef USE_DEV_LIB
  55. SYSFS_GPIO_Write(Pin, Value);
  56. #elif USE_HARDWARE_LIB
  57. Debug("not support");
  58. #endif
  59. #endif
  60. }
  61. UBYTE DEV_Digital_Read(UWORD Pin)
  62. {
  63. UBYTE Read_value = 0;
  64. #ifdef RPI
  65. #ifdef USE_BCM2835_LIB
  66. Read_value = bcm2835_gpio_lev(Pin);
  67. #elif USE_WIRINGPI_LIB
  68. Read_value = digitalRead(Pin);
  69. #elif USE_DEV_LIB
  70. Read_value = SYSFS_GPIO_Read(Pin);
  71. #endif
  72. #endif
  73. #ifdef JETSON
  74. #ifdef USE_DEV_LIB
  75. Read_value = SYSFS_GPIO_Read(Pin);
  76. #elif USE_HARDWARE_LIB
  77. Debug("not support");
  78. #endif
  79. #endif
  80. return Read_value;
  81. }
  82. /**
  83. * SPI
  84. **/
  85. void DEV_SPI_WriteByte(uint8_t Value)
  86. {
  87. #ifdef RPI
  88. #ifdef USE_BCM2835_LIB
  89. bcm2835_spi_transfer(Value);
  90. #elif USE_WIRINGPI_LIB
  91. wiringPiSPIDataRW(0,&Value,1);
  92. #elif USE_DEV_LIB
  93. DEV_HARDWARE_SPI_TransferByte(Value);
  94. #endif
  95. #endif
  96. #ifdef JETSON
  97. #ifdef USE_DEV_LIB
  98. SYSFS_software_spi_transfer(Value);
  99. #elif USE_HARDWARE_LIB
  100. Debug("not support");
  101. #endif
  102. #endif
  103. }
  104. void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len)
  105. {
  106. #ifdef RPI
  107. #ifdef USE_BCM2835_LIB
  108. char rData[Len];
  109. bcm2835_spi_transfernb((char *)pData,rData,Len);
  110. #elif USE_WIRINGPI_LIB
  111. wiringPiSPIDataRW(0, pData, Len);
  112. #elif USE_DEV_LIB
  113. DEV_HARDWARE_SPI_Transfer(pData, Len);
  114. #endif
  115. #endif
  116. #ifdef JETSON
  117. #ifdef USE_DEV_LIB
  118. //JETSON nano waits for hardware SPI
  119. Debug("not support");
  120. #elif USE_HARDWARE_LIB
  121. Debug("not support");
  122. #endif
  123. #endif
  124. }
  125. /**
  126. * GPIO Mode
  127. **/
  128. void DEV_GPIO_Mode(UWORD Pin, UWORD Mode)
  129. {
  130. #ifdef RPI
  131. #ifdef USE_BCM2835_LIB
  132. if(Mode == 0 || Mode == BCM2835_GPIO_FSEL_INPT) {
  133. bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_INPT);
  134. } else {
  135. bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_OUTP);
  136. }
  137. #elif USE_WIRINGPI_LIB
  138. if(Mode == 0 || Mode == INPUT) {
  139. pinMode(Pin, INPUT);
  140. pullUpDnControl(Pin, PUD_UP);
  141. } else {
  142. pinMode(Pin, OUTPUT);
  143. // Debug (" %d OUT \r\n",Pin);
  144. }
  145. #elif USE_DEV_LIB
  146. SYSFS_GPIO_Export(Pin);
  147. if(Mode == 0 || Mode == SYSFS_GPIO_IN) {
  148. SYSFS_GPIO_Direction(Pin, SYSFS_GPIO_IN);
  149. // Debug("IN Pin = %d\r\n",Pin);
  150. } else {
  151. SYSFS_GPIO_Direction(Pin, SYSFS_GPIO_OUT);
  152. // Debug("OUT Pin = %d\r\n",Pin);
  153. }
  154. #endif
  155. #endif
  156. #ifdef JETSON
  157. #ifdef USE_DEV_LIB
  158. SYSFS_GPIO_Export(Pin);
  159. SYSFS_GPIO_Direction(Pin, Mode);
  160. #elif USE_HARDWARE_LIB
  161. Debug("not support");
  162. #endif
  163. #endif
  164. }
  165. /**
  166. * delay x ms
  167. **/
  168. void DEV_Delay_ms(UDOUBLE xms)
  169. {
  170. #ifdef RPI
  171. #ifdef USE_BCM2835_LIB
  172. bcm2835_delay(xms);
  173. #elif USE_WIRINGPI_LIB
  174. delay(xms);
  175. #elif USE_DEV_LIB
  176. UDOUBLE i;
  177. for(i=0; i < xms; i++) {
  178. usleep(1000);
  179. }
  180. #endif
  181. #endif
  182. #ifdef JETSON
  183. UDOUBLE i;
  184. for(i=0; i < xms; i++) {
  185. usleep(1000);
  186. }
  187. #endif
  188. }
  189. static int DEV_Equipment_Testing(void)
  190. {
  191. FILE *fp;
  192. char issue_str[64];
  193. fp = fopen("/etc/issue", "r");
  194. if (fp == NULL) {
  195. Debug("Unable to open /etc/issue");
  196. return -1;
  197. }
  198. if (fread(issue_str, 1, sizeof(issue_str), fp) <= 0) {
  199. Debug("Unable to read from /etc/issue");
  200. return -1;
  201. }
  202. issue_str[sizeof(issue_str)-1] = '\0';
  203. fclose(fp);
  204. printf("Current environment: ");
  205. #ifdef RPI
  206. char systems[][9] = {"Raspbian", "Debian", "NixOS"};
  207. int detected = 0;
  208. for(int i=0; i<3; i++) {
  209. if (strstr(issue_str, systems[i]) != NULL) {
  210. printf("%s\n", systems[i]);
  211. detected = 1;
  212. }
  213. }
  214. if (!detected) {
  215. printf("not recognized\n");
  216. printf("Built for Raspberry Pi, but unable to detect environment.\n");
  217. printf("Perhaps you meant to 'make JETSON' instead?\n");
  218. return -1;
  219. }
  220. #endif
  221. #ifdef JETSON
  222. char system[] = {"Ubuntu"};
  223. if (strstr(issue_str, system) != NULL) {
  224. printf("%s\n", system);
  225. } else {
  226. printf("not recognized\n");
  227. printf("Built for Jetson, but unable to detect environment.\n");
  228. printf("Perhaps you meant to 'make RPI' instead?\n");
  229. return -1;
  230. }
  231. #endif
  232. return 0;
  233. }
  234. void DEV_GPIO_Init(void)
  235. {
  236. #ifdef RPI
  237. EPD_RST_PIN = 17;
  238. EPD_DC_PIN = 25;
  239. EPD_CS_PIN = 8;
  240. EPD_PWR_PIN = 18;
  241. EPD_BUSY_PIN = 24;
  242. #elif JETSON
  243. EPD_RST_PIN = GPIO17;
  244. EPD_DC_PIN = GPIO25;
  245. EPD_CS_PIN = SPI0_CS0;
  246. EPD_PWR_PIN = GPIO18;
  247. EPD_BUSY_PIN = GPIO24;
  248. #endif
  249. DEV_GPIO_Mode(EPD_RST_PIN, 1);
  250. DEV_GPIO_Mode(EPD_DC_PIN, 1);
  251. DEV_GPIO_Mode(EPD_CS_PIN, 1);
  252. DEV_GPIO_Mode(EPD_PWR_PIN, 1);
  253. DEV_GPIO_Mode(EPD_BUSY_PIN, 0);
  254. DEV_Digital_Write(EPD_CS_PIN, 1);
  255. DEV_Digital_Write(EPD_PWR_PIN, 1);
  256. }
  257. /******************************************************************************
  258. function: Module Initialize, the library and initialize the pins, SPI protocol
  259. parameter:
  260. Info:
  261. ******************************************************************************/
  262. UBYTE DEV_Module_Init(void)
  263. {
  264. printf("/***********************************/ \r\n");
  265. if(DEV_Equipment_Testing() < 0) {
  266. return 1;
  267. }
  268. #ifdef RPI
  269. #ifdef USE_BCM2835_LIB
  270. if(!bcm2835_init()) {
  271. printf("bcm2835 init failed !!! \r\n");
  272. return 1;
  273. } else {
  274. printf("bcm2835 init success !!! \r\n");
  275. }
  276. // GPIO Config
  277. DEV_GPIO_Init();
  278. bcm2835_spi_begin(); //Start spi interface, set spi pin for the reuse function
  279. bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); //High first transmission
  280. bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); //spi mode 0
  281. bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128); //Frequency
  282. bcm2835_spi_chipSelect(BCM2835_SPI_CS0); //set CE0
  283. bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); //enable cs0
  284. #elif USE_WIRINGPI_LIB
  285. //if(wiringPiSetup() < 0)//use wiringpi Pin number table
  286. if(wiringPiSetupGpio() < 0) { //use BCM2835 Pin number table
  287. printf("set wiringPi lib failed !!! \r\n");
  288. return 1;
  289. } else {
  290. printf("set wiringPi lib success !!! \r\n");
  291. }
  292. // GPIO Config
  293. DEV_GPIO_Init();
  294. wiringPiSPISetup(0,10000000);
  295. // wiringPiSPISetupMode(0, 32000000, 0);
  296. #elif USE_DEV_LIB
  297. printf("Write and read /dev/spidev0.0 \r\n");
  298. DEV_GPIO_Init();
  299. DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
  300. DEV_HARDWARE_SPI_setSpeed(10000000);
  301. #endif
  302. #elif JETSON
  303. #ifdef USE_DEV_LIB
  304. DEV_GPIO_Init();
  305. printf("Software spi\r\n");
  306. SYSFS_software_spi_begin();
  307. SYSFS_software_spi_setBitOrder(SOFTWARE_SPI_MSBFIRST);
  308. SYSFS_software_spi_setDataMode(SOFTWARE_SPI_Mode0);
  309. SYSFS_software_spi_setClockDivider(SOFTWARE_SPI_CLOCK_DIV4);
  310. #elif USE_HARDWARE_LIB
  311. printf("Write and read /dev/spidev0.0 \r\n");
  312. DEV_GPIO_Init();
  313. DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
  314. #endif
  315. #endif
  316. printf("/***********************************/ \r\n");
  317. return 0;
  318. }
  319. /******************************************************************************
  320. function: Module exits, closes SPI and BCM2835 library
  321. parameter:
  322. Info:
  323. ******************************************************************************/
  324. void DEV_Module_Exit(void)
  325. {
  326. #ifdef RPI
  327. #ifdef USE_BCM2835_LIB
  328. DEV_Digital_Write(EPD_CS_PIN, LOW);
  329. DEV_Digital_Write(EPD_PWR_PIN, LOW);
  330. DEV_Digital_Write(EPD_DC_PIN, LOW);
  331. DEV_Digital_Write(EPD_RST_PIN, LOW);
  332. bcm2835_spi_end();
  333. bcm2835_close();
  334. #elif USE_WIRINGPI_LIB
  335. DEV_Digital_Write(EPD_CS_PIN, 0);
  336. DEV_Digital_Write(EPD_PWR_PIN, 0);
  337. DEV_Digital_Write(EPD_DC_PIN, 0);
  338. DEV_Digital_Write(EPD_RST_PIN, 0);
  339. #elif USE_DEV_LIB
  340. DEV_HARDWARE_SPI_end();
  341. DEV_Digital_Write(EPD_CS_PIN, 0);
  342. DEV_Digital_Write(EPD_PWR_PIN, 0);
  343. DEV_Digital_Write(EPD_DC_PIN, 0);
  344. DEV_Digital_Write(EPD_RST_PIN, 0);
  345. #endif
  346. #elif JETSON
  347. #ifdef USE_DEV_LIB
  348. SYSFS_GPIO_Unexport(EPD_CS_PIN);
  349. SYSFS_GPIO_Unexport(EPD_PWR_PIN;
  350. SYSFS_GPIO_Unexport(EPD_DC_PIN);
  351. SYSFS_GPIO_Unexport(EPD_RST_PIN);
  352. SYSFS_GPIO_Unexport(EPD_BUSY_PIN);
  353. #elif USE_HARDWARE_LIB
  354. Debug("not support");
  355. #endif
  356. #endif
  357. }