DEV_Config.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. /**
  33. * GPIO
  34. **/
  35. int EPD_RST_PIN;
  36. int EPD_DC_PIN;
  37. int EPD_CS_PIN;
  38. int EPD_BUSY_PIN;
  39. int EPD_PWR_PIN;
  40. /**
  41. * GPIO read and write
  42. **/
  43. void DEV_Digital_Write(UWORD Pin, UBYTE Value)
  44. {
  45. #ifdef RPI
  46. #ifdef USE_BCM2835_LIB
  47. bcm2835_gpio_write(Pin, Value);
  48. #elif USE_WIRINGPI_LIB
  49. digitalWrite(Pin, Value);
  50. #elif USE_DEV_LIB
  51. GPIOD_Write(Pin, Value);
  52. #endif
  53. #endif
  54. #ifdef JETSON
  55. #ifdef USE_DEV_LIB
  56. SYSFS_GPIO_Write(Pin, Value);
  57. #elif USE_HARDWARE_LIB
  58. Debug("not support");
  59. #endif
  60. #endif
  61. }
  62. UBYTE DEV_Digital_Read(UWORD Pin)
  63. {
  64. UBYTE Read_value = 0;
  65. #ifdef RPI
  66. #ifdef USE_BCM2835_LIB
  67. Read_value = bcm2835_gpio_lev(Pin);
  68. #elif USE_WIRINGPI_LIB
  69. Read_value = digitalRead(Pin);
  70. #elif USE_DEV_LIB
  71. Read_value = GPIOD_Read(Pin);
  72. #endif
  73. #endif
  74. #ifdef JETSON
  75. #ifdef USE_DEV_LIB
  76. Read_value = SYSFS_GPIO_Read(Pin);
  77. #elif USE_HARDWARE_LIB
  78. Debug("not support");
  79. #endif
  80. #endif
  81. return Read_value;
  82. }
  83. /**
  84. * SPI
  85. **/
  86. void DEV_SPI_WriteByte(uint8_t Value)
  87. {
  88. #ifdef RPI
  89. #ifdef USE_BCM2835_LIB
  90. bcm2835_spi_transfer(Value);
  91. #elif USE_WIRINGPI_LIB
  92. wiringPiSPIDataRW(0,&Value,1);
  93. #elif USE_DEV_LIB
  94. DEV_HARDWARE_SPI_TransferByte(Value);
  95. #endif
  96. #endif
  97. #ifdef JETSON
  98. #ifdef USE_DEV_LIB
  99. SYSFS_software_spi_transfer(Value);
  100. #elif USE_HARDWARE_LIB
  101. Debug("not support");
  102. #endif
  103. #endif
  104. }
  105. void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len)
  106. {
  107. #ifdef RPI
  108. #ifdef USE_BCM2835_LIB
  109. char rData[Len];
  110. bcm2835_spi_transfernb((char *)pData,rData,Len);
  111. #elif USE_WIRINGPI_LIB
  112. wiringPiSPIDataRW(0, pData, Len);
  113. #elif USE_DEV_LIB
  114. DEV_HARDWARE_SPI_Transfer(pData, Len);
  115. #endif
  116. #endif
  117. #ifdef JETSON
  118. #ifdef USE_DEV_LIB
  119. //JETSON nano waits for hardware SPI
  120. Debug("not support");
  121. #elif USE_HARDWARE_LIB
  122. Debug("not support");
  123. #endif
  124. #endif
  125. }
  126. /**
  127. * GPIO Mode
  128. **/
  129. void DEV_GPIO_Mode(UWORD Pin, UWORD Mode)
  130. {
  131. #ifdef RPI
  132. #ifdef USE_BCM2835_LIB
  133. if(Mode == 0 || Mode == BCM2835_GPIO_FSEL_INPT) {
  134. bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_INPT);
  135. } else {
  136. bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_OUTP);
  137. }
  138. #elif USE_WIRINGPI_LIB
  139. if(Mode == 0 || Mode == INPUT) {
  140. pinMode(Pin, INPUT);
  141. pullUpDnControl(Pin, PUD_UP);
  142. } else {
  143. pinMode(Pin, OUTPUT);
  144. // Debug (" %d OUT \r\n",Pin);
  145. }
  146. #elif USE_DEV_LIB
  147. if(Mode == 0 || Mode == GPIOD_IN) {
  148. GPIOD_Direction(Pin, GPIOD_IN);
  149. // Debug("IN Pin = %d\r\n",Pin);
  150. } else {
  151. GPIOD_Direction(Pin, GPIOD_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_BUSY_PIN, 0);
  250. DEV_GPIO_Mode(EPD_RST_PIN, 1);
  251. DEV_GPIO_Mode(EPD_DC_PIN, 1);
  252. DEV_GPIO_Mode(EPD_CS_PIN, 1);
  253. DEV_GPIO_Mode(EPD_PWR_PIN, 1);
  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. GPIOD_Export();
  299. DEV_GPIO_Init();
  300. DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
  301. DEV_HARDWARE_SPI_setSpeed(10000000);
  302. #endif
  303. #elif JETSON
  304. #ifdef USE_DEV_LIB
  305. DEV_GPIO_Init();
  306. printf("Software spi\r\n");
  307. SYSFS_software_spi_begin();
  308. SYSFS_software_spi_setBitOrder(SOFTWARE_SPI_MSBFIRST);
  309. SYSFS_software_spi_setDataMode(SOFTWARE_SPI_Mode0);
  310. SYSFS_software_spi_setClockDivider(SOFTWARE_SPI_CLOCK_DIV4);
  311. #elif USE_HARDWARE_LIB
  312. printf("Write and read /dev/spidev0.0 \r\n");
  313. DEV_GPIO_Init();
  314. DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
  315. #endif
  316. #endif
  317. printf("/***********************************/ \r\n");
  318. return 0;
  319. }
  320. /******************************************************************************
  321. function: Module exits, closes SPI and BCM2835 library
  322. parameter:
  323. Info:
  324. ******************************************************************************/
  325. void DEV_Module_Exit(void)
  326. {
  327. #ifdef RPI
  328. #ifdef USE_BCM2835_LIB
  329. DEV_Digital_Write(EPD_CS_PIN, LOW);
  330. DEV_Digital_Write(EPD_PWR_PIN, LOW);
  331. DEV_Digital_Write(EPD_DC_PIN, LOW);
  332. DEV_Digital_Write(EPD_RST_PIN, LOW);
  333. bcm2835_spi_end();
  334. bcm2835_close();
  335. #elif USE_WIRINGPI_LIB
  336. DEV_Digital_Write(EPD_CS_PIN, 0);
  337. DEV_Digital_Write(EPD_PWR_PIN, 0);
  338. DEV_Digital_Write(EPD_DC_PIN, 0);
  339. DEV_Digital_Write(EPD_RST_PIN, 0);
  340. #elif USE_DEV_LIB
  341. DEV_HARDWARE_SPI_end();
  342. DEV_Digital_Write(EPD_CS_PIN, 0);
  343. DEV_Digital_Write(EPD_PWR_PIN, 0);
  344. DEV_Digital_Write(EPD_DC_PIN, 0);
  345. DEV_Digital_Write(EPD_RST_PIN, 0);
  346. GPIOD_Unexport(EPD_PWR_PIN);
  347. GPIOD_Unexport(EPD_DC_PIN);
  348. GPIOD_Unexport(EPD_RST_PIN);
  349. GPIOD_Unexport(EPD_BUSY_PIN);
  350. GPIOD_Unexport_GPIO();
  351. #endif
  352. #elif JETSON
  353. #ifdef USE_DEV_LIB
  354. SYSFS_GPIO_Unexport(EPD_CS_PIN);
  355. SYSFS_GPIO_Unexport(EPD_PWR_PIN;
  356. SYSFS_GPIO_Unexport(EPD_DC_PIN);
  357. SYSFS_GPIO_Unexport(EPD_RST_PIN);
  358. SYSFS_GPIO_Unexport(EPD_BUSY_PIN);
  359. #elif USE_HARDWARE_LIB
  360. Debug("not support");
  361. #endif
  362. #endif
  363. }