DEV_Config.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. #if USE_LGPIO_LIB
  32. int GPIO_Handle;
  33. int SPI_Handle;
  34. #endif
  35. /**
  36. * GPIO
  37. **/
  38. int EPD_RST_PIN;
  39. int EPD_DC_PIN;
  40. int EPD_CS_PIN;
  41. int EPD_BUSY_PIN;
  42. int EPD_PWR_PIN;
  43. int EPD_MOSI_PIN;
  44. int EPD_SCLK_PIN;
  45. /**
  46. * GPIO read and write
  47. **/
  48. void DEV_Digital_Write(UWORD Pin, UBYTE Value)
  49. {
  50. #ifdef RPI
  51. #ifdef USE_BCM2835_LIB
  52. bcm2835_gpio_write(Pin, Value);
  53. #elif USE_WIRINGPI_LIB
  54. digitalWrite(Pin, Value);
  55. #elif USE_LGPIO_LIB
  56. lgGpioWrite(GPIO_Handle, Pin, Value);
  57. #elif USE_DEV_LIB
  58. GPIOD_Write(Pin, Value);
  59. #endif
  60. #endif
  61. #ifdef JETSON
  62. #ifdef USE_DEV_LIB
  63. SYSFS_GPIO_Write(Pin, Value);
  64. #elif USE_HARDWARE_LIB
  65. Debug("not support");
  66. #endif
  67. #endif
  68. }
  69. UBYTE DEV_Digital_Read(UWORD Pin)
  70. {
  71. UBYTE Read_value = 0;
  72. #ifdef RPI
  73. #ifdef USE_BCM2835_LIB
  74. Read_value = bcm2835_gpio_lev(Pin);
  75. #elif USE_WIRINGPI_LIB
  76. Read_value = digitalRead(Pin);
  77. #elif USE_LGPIO_LIB
  78. Read_value = lgGpioRead(GPIO_Handle,Pin);
  79. #elif USE_DEV_LIB
  80. Read_value = GPIOD_Read(Pin);
  81. #endif
  82. #endif
  83. #ifdef JETSON
  84. #ifdef USE_DEV_LIB
  85. Read_value = SYSFS_GPIO_Read(Pin);
  86. #elif USE_HARDWARE_LIB
  87. Debug("not support");
  88. #endif
  89. #endif
  90. return Read_value;
  91. }
  92. /**
  93. * SPI
  94. **/
  95. void DEV_SPI_WriteByte(uint8_t Value)
  96. {
  97. #ifdef RPI
  98. #ifdef USE_BCM2835_LIB
  99. bcm2835_spi_transfer(Value);
  100. #elif USE_WIRINGPI_LIB
  101. wiringPiSPIDataRW(0,&Value,1);
  102. #elif USE_LGPIO_LIB
  103. lgSpiWrite(SPI_Handle,(char*)&Value, 1);
  104. #elif USE_DEV_LIB
  105. DEV_HARDWARE_SPI_TransferByte(Value);
  106. #endif
  107. #endif
  108. #ifdef JETSON
  109. #ifdef USE_DEV_LIB
  110. SYSFS_software_spi_transfer(Value);
  111. #elif USE_HARDWARE_LIB
  112. Debug("not support");
  113. #endif
  114. #endif
  115. }
  116. void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len)
  117. {
  118. #ifdef RPI
  119. #ifdef USE_BCM2835_LIB
  120. char rData[Len];
  121. bcm2835_spi_transfernb((char *)pData,rData,Len);
  122. #elif USE_WIRINGPI_LIB
  123. wiringPiSPIDataRW(0, pData, Len);
  124. #elif USE_LGPIO_LIB
  125. lgSpiWrite(SPI_Handle,(char*)pData, Len);
  126. #elif USE_DEV_LIB
  127. DEV_HARDWARE_SPI_Transfer(pData, Len);
  128. #endif
  129. #endif
  130. #ifdef JETSON
  131. #ifdef USE_DEV_LIB
  132. //JETSON nano waits for hardware SPI
  133. // Debug("not support");
  134. uint32_t i;
  135. for(i = 0; i<Len; i++)
  136. SYSFS_software_spi_transfer(pData[i]);
  137. #elif USE_HARDWARE_LIB
  138. Debug("not support");
  139. #endif
  140. #endif
  141. }
  142. /**
  143. * GPIO Mode
  144. **/
  145. void DEV_GPIO_Mode(UWORD Pin, UWORD Mode)
  146. {
  147. #ifdef RPI
  148. #ifdef USE_BCM2835_LIB
  149. if(Mode == 0 || Mode == BCM2835_GPIO_FSEL_INPT) {
  150. bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_INPT);
  151. } else {
  152. bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_OUTP);
  153. }
  154. #elif USE_WIRINGPI_LIB
  155. if(Mode == 0 || Mode == INPUT) {
  156. pinMode(Pin, INPUT);
  157. pullUpDnControl(Pin, PUD_UP);
  158. } else {
  159. pinMode(Pin, OUTPUT);
  160. // Debug (" %d OUT \r\n",Pin);
  161. }
  162. #elif USE_LGPIO_LIB
  163. if(Mode == 0 || Mode == LG_SET_INPUT){
  164. lgGpioClaimInput(GPIO_Handle,LFLAGS,Pin);
  165. // printf("IN Pin = %d\r\n",Pin);
  166. }else{
  167. lgGpioClaimOutput(GPIO_Handle, LFLAGS, Pin, LG_LOW);
  168. // printf("OUT Pin = %d\r\n",Pin);
  169. }
  170. #elif USE_DEV_LIB
  171. if(Mode == 0 || Mode == GPIOD_IN) {
  172. GPIOD_Direction(Pin, GPIOD_IN);
  173. // Debug("IN Pin = %d\r\n",Pin);
  174. } else {
  175. GPIOD_Direction(Pin, GPIOD_OUT);
  176. // Debug("OUT Pin = %d\r\n",Pin);
  177. }
  178. #endif
  179. #endif
  180. #ifdef JETSON
  181. #ifdef USE_DEV_LIB
  182. SYSFS_GPIO_Export(Pin);
  183. SYSFS_GPIO_Direction(Pin, Mode);
  184. #elif USE_HARDWARE_LIB
  185. Debug("not support");
  186. #endif
  187. #endif
  188. }
  189. /**
  190. * delay x ms
  191. **/
  192. void DEV_Delay_ms(UDOUBLE xms)
  193. {
  194. #ifdef RPI
  195. #ifdef USE_BCM2835_LIB
  196. bcm2835_delay(xms);
  197. #elif USE_WIRINGPI_LIB
  198. delay(xms);
  199. #elif USE_LGPIO_LIB
  200. lguSleep(xms/1000.0);
  201. #elif USE_DEV_LIB
  202. UDOUBLE i;
  203. for(i=0; i < xms; i++) {
  204. usleep(1000);
  205. }
  206. #endif
  207. #endif
  208. #ifdef JETSON
  209. UDOUBLE i;
  210. for(i=0; i < xms; i++) {
  211. usleep(1000);
  212. }
  213. #endif
  214. }
  215. static int DEV_Equipment_Testing(void)
  216. {
  217. FILE *fp;
  218. char issue_str[64];
  219. fp = fopen("/etc/issue", "r");
  220. if (fp == NULL) {
  221. Debug("Unable to open /etc/issue");
  222. return -1;
  223. }
  224. if (fread(issue_str, 1, sizeof(issue_str), fp) <= 0) {
  225. Debug("Unable to read from /etc/issue");
  226. return -1;
  227. }
  228. issue_str[sizeof(issue_str)-1] = '\0';
  229. fclose(fp);
  230. printf("Current environment: ");
  231. #ifdef RPI
  232. char systems[][9] = {"Raspbian", "Debian", "NixOS"};
  233. int detected = 0;
  234. for(int i=0; i<3; i++) {
  235. if (strstr(issue_str, systems[i]) != NULL) {
  236. printf("%s\n", systems[i]);
  237. detected = 1;
  238. }
  239. }
  240. if (!detected) {
  241. printf("not recognized\n");
  242. printf("Built for Raspberry Pi, but unable to detect environment.\n");
  243. printf("Perhaps you meant to 'make JETSON' instead?\n");
  244. return -1;
  245. }
  246. #endif
  247. #ifdef JETSON
  248. char system[] = {"Ubuntu"};
  249. if (strstr(issue_str, system) != NULL) {
  250. printf("%s\n", system);
  251. } else {
  252. printf("not recognized\n");
  253. printf("Built for Jetson, but unable to detect environment.\n");
  254. printf("Perhaps you meant to 'make RPI' instead?\n");
  255. return -1;
  256. }
  257. #endif
  258. return 0;
  259. }
  260. void DEV_GPIO_Init(void)
  261. {
  262. #ifdef RPI
  263. EPD_RST_PIN = 17;
  264. EPD_DC_PIN = 25;
  265. EPD_CS_PIN = 8;
  266. EPD_PWR_PIN = 18;
  267. EPD_BUSY_PIN = 24;
  268. EPD_MOSI_PIN = 10;
  269. EPD_SCLK_PIN = 11;
  270. #elif JETSON
  271. EPD_RST_PIN = GPIO17;
  272. EPD_DC_PIN = GPIO25;
  273. EPD_CS_PIN = SPI0_CS0;
  274. EPD_PWR_PIN = GPIO18;
  275. EPD_BUSY_PIN = GPIO24;
  276. EPD_MOSI_PIN = SPI0_MOSI;
  277. EPD_SCLK_PIN = SPI0_SCLK;
  278. #endif
  279. DEV_GPIO_Mode(EPD_BUSY_PIN, 0);
  280. DEV_GPIO_Mode(EPD_RST_PIN, 1);
  281. DEV_GPIO_Mode(EPD_DC_PIN, 1);
  282. DEV_GPIO_Mode(EPD_CS_PIN, 1);
  283. DEV_GPIO_Mode(EPD_PWR_PIN, 1);
  284. // DEV_GPIO_Mode(EPD_MOSI_PIN, 0);
  285. // DEV_GPIO_Mode(EPD_SCLK_PIN, 1);
  286. DEV_Digital_Write(EPD_CS_PIN, 1);
  287. DEV_Digital_Write(EPD_PWR_PIN, 1);
  288. }
  289. void DEV_SPI_SendnData(UBYTE *Reg)
  290. {
  291. UDOUBLE size;
  292. size = sizeof(Reg);
  293. for(UDOUBLE i=0 ; i<size ; i++)
  294. {
  295. DEV_SPI_SendData(Reg[i]);
  296. }
  297. }
  298. void DEV_SPI_SendData(UBYTE Reg)
  299. {
  300. UBYTE i,j=Reg;
  301. DEV_GPIO_Mode(EPD_MOSI_PIN, 1);
  302. DEV_Digital_Write(EPD_CS_PIN, 0);
  303. for(i = 0; i<8; i++)
  304. {
  305. DEV_Digital_Write(EPD_SCLK_PIN, 0);
  306. if (j & 0x80)
  307. {
  308. DEV_Digital_Write(EPD_MOSI_PIN, 1);
  309. }
  310. else
  311. {
  312. DEV_Digital_Write(EPD_MOSI_PIN, 0);
  313. }
  314. DEV_Digital_Write(EPD_SCLK_PIN, 1);
  315. j = j << 1;
  316. }
  317. DEV_Digital_Write(EPD_SCLK_PIN, 0);
  318. DEV_Digital_Write(EPD_CS_PIN, 1);
  319. }
  320. UBYTE DEV_SPI_ReadData()
  321. {
  322. UBYTE i,j=0xff;
  323. DEV_GPIO_Mode(EPD_MOSI_PIN, 0);
  324. DEV_Digital_Write(EPD_CS_PIN, 0);
  325. for(i = 0; i<8; i++)
  326. {
  327. DEV_Digital_Write(EPD_SCLK_PIN, 0);
  328. j = j << 1;
  329. if (DEV_Digital_Read(EPD_MOSI_PIN))
  330. {
  331. j = j | 0x01;
  332. }
  333. else
  334. {
  335. j= j & 0xfe;
  336. }
  337. DEV_Digital_Write(EPD_SCLK_PIN, 1);
  338. }
  339. DEV_Digital_Write(EPD_SCLK_PIN, 0);
  340. DEV_Digital_Write(EPD_CS_PIN, 1);
  341. return j;
  342. }
  343. /******************************************************************************
  344. function: Module Initialize, the library and initialize the pins, SPI protocol
  345. parameter:
  346. Info:
  347. ******************************************************************************/
  348. UBYTE DEV_Module_Init(void)
  349. {
  350. printf("/***********************************/ \r\n");
  351. if(DEV_Equipment_Testing() < 0) {
  352. return 1;
  353. }
  354. #ifdef RPI
  355. #ifdef USE_BCM2835_LIB
  356. if(!bcm2835_init()) {
  357. printf("bcm2835 init failed !!! \r\n");
  358. return 1;
  359. } else {
  360. printf("bcm2835 init success !!! \r\n");
  361. }
  362. // GPIO Config
  363. DEV_GPIO_Init();
  364. bcm2835_spi_begin(); //Start spi interface, set spi pin for the reuse function
  365. bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); //High first transmission
  366. bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); //spi mode 0
  367. bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128); //Frequency
  368. bcm2835_spi_chipSelect(BCM2835_SPI_CS0); //set CE0
  369. bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); //enable cs0
  370. #elif USE_WIRINGPI_LIB
  371. //if(wiringPiSetup() < 0)//use wiringpi Pin number table
  372. if(wiringPiSetupGpio() < 0) { //use BCM2835 Pin number table
  373. printf("set wiringPi lib failed !!! \r\n");
  374. return 1;
  375. } else {
  376. printf("set wiringPi lib success !!! \r\n");
  377. }
  378. // GPIO Config
  379. DEV_GPIO_Init();
  380. wiringPiSPISetup(0,10000000);
  381. // wiringPiSPISetupMode(0, 32000000, 0);
  382. #elif USE_LGPIO_LIB
  383. char buffer[NUM_MAXBUF];
  384. FILE *fp;
  385. fp = popen("cat /proc/cpuinfo | grep 'Raspberry Pi 5'", "r");
  386. if (fp == NULL) {
  387. Debug("It is not possible to determine the model of the Raspberry PI\n");
  388. return -1;
  389. }
  390. if(fgets(buffer, sizeof(buffer), fp) != NULL)
  391. {
  392. GPIO_Handle = lgGpiochipOpen(4);
  393. if (GPIO_Handle < 0)
  394. {
  395. Debug( "gpiochip4 Export Failed\n");
  396. return -1;
  397. }
  398. }
  399. else
  400. {
  401. GPIO_Handle = lgGpiochipOpen(0);
  402. if (GPIO_Handle < 0)
  403. {
  404. Debug( "gpiochip0 Export Failed\n");
  405. return -1;
  406. }
  407. }
  408. SPI_Handle = lgSpiOpen(0, 0, 10000000, 0);
  409. DEV_GPIO_Init();
  410. #elif USE_DEV_LIB
  411. printf("Write and read /dev/spidev0.0 \r\n");
  412. GPIOD_Export();
  413. DEV_GPIO_Init();
  414. DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
  415. DEV_HARDWARE_SPI_setSpeed(10000000);
  416. #endif
  417. #elif JETSON
  418. #ifdef USE_DEV_LIB
  419. DEV_GPIO_Init();
  420. printf("Software spi\r\n");
  421. SYSFS_software_spi_begin();
  422. SYSFS_software_spi_setBitOrder(SOFTWARE_SPI_MSBFIRST);
  423. SYSFS_software_spi_setDataMode(SOFTWARE_SPI_Mode0);
  424. SYSFS_software_spi_setClockDivider(SOFTWARE_SPI_CLOCK_DIV4);
  425. #elif USE_HARDWARE_LIB
  426. printf("Write and read /dev/spidev0.0 \r\n");
  427. DEV_GPIO_Init();
  428. DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
  429. #endif
  430. #endif
  431. printf("/***********************************/ \r\n");
  432. return 0;
  433. }
  434. /******************************************************************************
  435. function: Module exits, closes SPI and BCM2835 library
  436. parameter:
  437. Info:
  438. ******************************************************************************/
  439. void DEV_Module_Exit(void)
  440. {
  441. #ifdef RPI
  442. #ifdef USE_BCM2835_LIB
  443. DEV_Digital_Write(EPD_CS_PIN, LOW);
  444. DEV_Digital_Write(EPD_PWR_PIN, LOW);
  445. DEV_Digital_Write(EPD_DC_PIN, LOW);
  446. DEV_Digital_Write(EPD_RST_PIN, LOW);
  447. bcm2835_spi_end();
  448. bcm2835_close();
  449. #elif USE_WIRINGPI_LIB
  450. DEV_Digital_Write(EPD_CS_PIN, 0);
  451. DEV_Digital_Write(EPD_PWR_PIN, 0);
  452. DEV_Digital_Write(EPD_DC_PIN, 0);
  453. DEV_Digital_Write(EPD_RST_PIN, 0);
  454. #elif USE_LGPIO_LIB
  455. // DEV_Digital_Write(EPD_CS_PIN, 0);
  456. // DEV_Digital_Write(EPD_PWR_PIN, 0);
  457. // DEV_Digital_Write(EPD_DC_PIN, 0);
  458. // DEV_Digital_Write(EPD_RST_PIN, 0);
  459. // lgSpiClose(SPI_Handle);
  460. // lgGpiochipClose(GPIO_Handle);
  461. #elif USE_DEV_LIB
  462. DEV_HARDWARE_SPI_end();
  463. DEV_Digital_Write(EPD_CS_PIN, 0);
  464. DEV_Digital_Write(EPD_PWR_PIN, 0);
  465. DEV_Digital_Write(EPD_DC_PIN, 0);
  466. DEV_Digital_Write(EPD_RST_PIN, 0);
  467. GPIOD_Unexport(EPD_PWR_PIN);
  468. GPIOD_Unexport(EPD_DC_PIN);
  469. GPIOD_Unexport(EPD_RST_PIN);
  470. GPIOD_Unexport(EPD_BUSY_PIN);
  471. GPIOD_Unexport_GPIO();
  472. #endif
  473. #elif JETSON
  474. #ifdef USE_DEV_LIB
  475. SYSFS_GPIO_Unexport(EPD_CS_PIN);
  476. SYSFS_GPIO_Unexport(EPD_PWR_PIN;
  477. SYSFS_GPIO_Unexport(EPD_DC_PIN);
  478. SYSFS_GPIO_Unexport(EPD_RST_PIN);
  479. SYSFS_GPIO_Unexport(EPD_BUSY_PIN);
  480. #elif USE_HARDWARE_LIB
  481. Debug("not support");
  482. #endif
  483. #endif
  484. }