DEV_Config.c 8.8 KB

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