DEV_Config.c 11 KB

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