2
0

DEV_Config.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <fcntl.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. /**
  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(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. int i;
  192. int fd;
  193. char value_str[20];
  194. fd = open("/etc/issue", O_RDONLY);
  195. printf("Current environment: ");
  196. while(1) {
  197. if (fd < 0) {
  198. Debug( "Read failed Pin\n");
  199. return -1;
  200. }
  201. for(i=0;; i++) {
  202. if (read(fd, &value_str[i], 1) < 0) {
  203. Debug( "failed to read value!\n");
  204. return -1;
  205. }
  206. if(value_str[i] ==32) {
  207. printf("\r\n");
  208. break;
  209. }
  210. printf("%c",value_str[i]);
  211. }
  212. break;
  213. }
  214. #ifdef RPI
  215. if(i<5) {
  216. printf("Unrecognizable\r\n");
  217. } else {
  218. char RPI_System[10] = {"Raspbian"};
  219. for(i=0; i<6; i++) {
  220. if(RPI_System[i]!= value_str[i]) {
  221. printf("Please make JETSON !!!!!!!!!!\r\n");
  222. return -1;
  223. }
  224. }
  225. }
  226. #endif
  227. #ifdef JETSON
  228. if(i<5) {
  229. Debug("Unrecognizable\r\n");
  230. } else {
  231. char JETSON_System[10]= {"Ubuntu"};
  232. for(i=0; i<6; i++) {
  233. if(JETSON_System[i]!= value_str[i] ) {
  234. printf("Please make RPI !!!!!!!!!!\r\n");
  235. return -1;
  236. }
  237. }
  238. }
  239. #endif
  240. return 0;
  241. }
  242. void DEV_GPIO_Init(void)
  243. {
  244. #ifdef RPI
  245. EPD_RST_PIN = 17;
  246. EPD_DC_PIN = 25;
  247. EPD_CS_PIN = 8;
  248. EPD_BUSY_PIN = 24;
  249. #elif JETSON
  250. EPD_RST_PIN = GPIO17;
  251. EPD_DC_PIN = GPIO25;
  252. EPD_CS_PIN = SPI0_CS0;
  253. EPD_BUSY_PIN = GPIO24;
  254. #endif
  255. DEV_GPIO_Mode(EPD_RST_PIN, 1);
  256. DEV_GPIO_Mode(EPD_DC_PIN, 1);
  257. DEV_GPIO_Mode(EPD_CS_PIN, 1);
  258. DEV_GPIO_Mode(EPD_BUSY_PIN, 0);
  259. DEV_Digital_Write(EPD_CS_PIN, 1);
  260. }
  261. /******************************************************************************
  262. function: Module Initialize, the library and initialize the pins, SPI protocol
  263. parameter:
  264. Info:
  265. ******************************************************************************/
  266. UBYTE DEV_Module_Init(void)
  267. {
  268. printf("/***********************************/ \r\n");
  269. if(DEV_Equipment_Testing() < 0) {
  270. return 1;
  271. }
  272. #ifdef RPI
  273. #ifdef USE_BCM2835_LIB
  274. if(!bcm2835_init()) {
  275. printf("bcm2835 init failed !!! \r\n");
  276. return 1;
  277. } else {
  278. printf("bcm2835 init success !!! \r\n");
  279. }
  280. // GPIO Config
  281. DEV_GPIO_Init();
  282. bcm2835_spi_begin(); //Start spi interface, set spi pin for the reuse function
  283. bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); //High first transmission
  284. bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); //spi mode 0
  285. bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128); //Frequency
  286. bcm2835_spi_chipSelect(BCM2835_SPI_CS0); //set CE0
  287. bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); //enable cs0
  288. #elif USE_WIRINGPI_LIB
  289. //if(wiringPiSetup() < 0)//use wiringpi Pin number table
  290. if(wiringPiSetupGpio() < 0) { //use BCM2835 Pin number table
  291. printf("set wiringPi lib failed !!! \r\n");
  292. return 1;
  293. } else {
  294. printf("set wiringPi lib success !!! \r\n");
  295. }
  296. // GPIO Config
  297. DEV_GPIO_Init();
  298. wiringPiSPISetup(0,10000000);
  299. // wiringPiSPISetupMode(0, 32000000, 0);
  300. #elif USE_DEV_LIB
  301. printf("Write and read /dev/spidev0.0 \r\n");
  302. DEV_GPIO_Init();
  303. DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
  304. DEV_HARDWARE_SPI_setSpeed(10000000);
  305. #endif
  306. #elif JETSON
  307. #ifdef USE_DEV_LIB
  308. DEV_GPIO_Init();
  309. printf("Software spi\r\n");
  310. SYSFS_software_spi_begin();
  311. SYSFS_software_spi_setBitOrder(SOFTWARE_SPI_MSBFIRST);
  312. SYSFS_software_spi_setDataMode(SOFTWARE_SPI_Mode0);
  313. SYSFS_software_spi_setClockDivider(SOFTWARE_SPI_CLOCK_DIV4);
  314. #elif USE_HARDWARE_LIB
  315. printf("Write and read /dev/spidev0.0 \r\n");
  316. DEV_GPIO_Init();
  317. DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
  318. #endif
  319. #endif
  320. printf("/***********************************/ \r\n");
  321. return 0;
  322. }
  323. /******************************************************************************
  324. function: Module exits, closes SPI and BCM2835 library
  325. parameter:
  326. Info:
  327. ******************************************************************************/
  328. void DEV_Module_Exit(void)
  329. {
  330. #ifdef RPI
  331. #ifdef USE_BCM2835_LIB
  332. DEV_Digital_Write(EPD_CS_PIN, LOW);
  333. DEV_Digital_Write(EPD_DC_PIN, LOW);
  334. DEV_Digital_Write(EPD_RST_PIN, LOW);
  335. bcm2835_spi_end();
  336. bcm2835_close();
  337. #elif USE_WIRINGPI_LIB
  338. DEV_Digital_Write(EPD_CS_PIN, 0);
  339. DEV_Digital_Write(EPD_DC_PIN, 0);
  340. DEV_Digital_Write(EPD_RST_PIN, 0);
  341. #elif USE_DEV_LIB
  342. DEV_HARDWARE_SPI_end();
  343. DEV_Digital_Write(EPD_CS_PIN, 0);
  344. DEV_Digital_Write(EPD_DC_PIN, 0);
  345. DEV_Digital_Write(EPD_RST_PIN, 0);
  346. #endif
  347. #elif JETSON
  348. #ifdef USE_DEV_LIB
  349. SYSFS_GPIO_Unexport(EPD_CS_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. }