DEV_Config.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*****************************************************************************
  2. * | File : DEV_Config.c
  3. * | Author : Waveshare team
  4. * | Function : Hardware underlying interface
  5. * | Info :
  6. *----------------
  7. * | This version: V2.0
  8. * | Date : 2020-06-17
  9. * | Info : Basic version
  10. *
  11. ******************************************************************************/
  12. #include "DEV_Config.h"
  13. #include <unistd.h>
  14. uint32_t fd;
  15. #if USE_DEV_LIB
  16. int GPIO_Handle;
  17. int SPI_Handle;
  18. int I2C_Handle;
  19. #endif
  20. /*****************************************
  21. GPIO
  22. *****************************************/
  23. void DEV_Digital_Write(UWORD Pin, UBYTE Value)
  24. {
  25. #ifdef USE_BCM2835_LIB
  26. bcm2835_gpio_write(Pin, Value);
  27. #elif USE_WIRINGPI_LIB
  28. digitalWrite(Pin, Value);
  29. #elif USE_DEV_LIB
  30. lgGpioWrite(GPIO_Handle, Pin, Value);
  31. #endif
  32. }
  33. UBYTE DEV_Digital_Read(UWORD Pin)
  34. {
  35. UBYTE Read_value = 0;
  36. #ifdef USE_BCM2835_LIB
  37. Read_value = bcm2835_gpio_lev(Pin);
  38. #elif USE_WIRINGPI_LIB
  39. Read_value = digitalRead(Pin);
  40. #elif USE_DEV_LIB
  41. Read_value = lgGpioRead(GPIO_Handle,Pin);
  42. #endif
  43. return Read_value;
  44. }
  45. void DEV_GPIO_Mode(UWORD Pin, UWORD Mode)
  46. {
  47. #ifdef USE_BCM2835_LIB
  48. if(Mode == 0 || Mode == BCM2835_GPIO_FSEL_INPT){
  49. bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_INPT);
  50. }else {
  51. bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_OUTP);
  52. }
  53. #elif USE_WIRINGPI_LIB
  54. if(Mode == 0 || Mode == INPUT){
  55. pinMode(Pin, INPUT);
  56. pullUpDnControl(Pin, PUD_UP);
  57. }else{
  58. pinMode(Pin, OUTPUT);
  59. // printf (" %d OUT \r\n",Pin);
  60. }
  61. #elif USE_DEV_LIB
  62. if(Mode == 0 || Mode == LG_SET_INPUT){
  63. lgGpioClaimInput(GPIO_Handle,LFLAGS,Pin);
  64. // printf("IN Pin = %d\r\n",Pin);
  65. }else{
  66. lgGpioClaimOutput(GPIO_Handle, LFLAGS, Pin, LG_LOW);
  67. // printf("OUT Pin = %d\r\n",Pin);
  68. }
  69. #endif
  70. }
  71. /**
  72. * delay x ms
  73. **/
  74. void DEV_Delay_ms(UDOUBLE xms)
  75. {
  76. #ifdef USE_BCM2835_LIB
  77. bcm2835_delay(xms);
  78. #elif USE_WIRINGPI_LIB
  79. delay(xms);
  80. #elif USE_DEV_LIB
  81. lguSleep(xms/1000.0);
  82. #endif
  83. }
  84. static void DEV_GPIO_Init(void)
  85. {
  86. DEV_GPIO_Mode(OLED_CS, 1);
  87. DEV_GPIO_Mode(OLED_RST, 1);
  88. DEV_GPIO_Mode(OLED_DC, 1);
  89. }
  90. /******************************************************************************
  91. function: Module Initialize, the library and initialize the pins, SPI protocol
  92. parameter:
  93. Info:
  94. ******************************************************************************/
  95. UBYTE DEV_ModuleInit(void)
  96. {
  97. #ifdef USE_BCM2835_LIB
  98. if(!bcm2835_init()) {
  99. printf("bcm2835 init failed !!! \r\n");
  100. return 1;
  101. } else {
  102. printf("bcm2835 init success !!! \r\n");
  103. }
  104. DEV_GPIO_Init();
  105. #if USE_SPI
  106. printf("USE_SPI\r\n");
  107. bcm2835_spi_begin(); //Start spi interface, set spi pin for the reuse function
  108. bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); //High first transmission
  109. bcm2835_spi_setDataMode(BCM2835_SPI_MODE3); //spi mode 3
  110. bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_32); //Frequency
  111. bcm2835_spi_chipSelect(BCM2835_SPI_CS0); //set CE0
  112. bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); //enable cs0
  113. #elif USE_IIC
  114. OLED_DC_0;
  115. OLED_CS_0;
  116. printf("USE_IIC\r\n");
  117. bcm2835_i2c_begin();
  118. bcm2835_i2c_setSlaveAddress(0x3c);
  119. /**********************************************************/
  120. #endif
  121. #elif USE_WIRINGPI_LIB
  122. //if(wiringPiSetup() < 0) {//use wiringpi Pin number table
  123. if(wiringPiSetupGpio() < 0) { //use BCM2835 Pin number table
  124. printf("set wiringPi lib failed !!! \r\n");
  125. return 1;
  126. } else {
  127. printf("set wiringPi lib success !!! \r\n");
  128. }
  129. DEV_GPIO_Init();
  130. #if USE_SPI
  131. printf("USE_SPI\r\n");
  132. //wiringPiSPISetup(0,9000000);
  133. wiringPiSPISetupMode(0, 9000000, 3);
  134. #elif USE_IIC
  135. OLED_DC_0;
  136. OLED_CS_0;
  137. printf("USE_IIC\r\n");
  138. fd = wiringPiI2CSetup(0x3c);
  139. #endif
  140. #elif USE_DEV_LIB
  141. char buffer[NUM_MAXBUF];
  142. FILE *fp1;
  143. fp1 = popen("cat /proc/cpuinfo | grep 'Raspberry Pi 5'", "r");
  144. if (fp1 == NULL) {
  145. printf("It is not possible to determine the model of the Raspberry PI\n");
  146. return -1;
  147. }
  148. if(fgets(buffer, sizeof(buffer), fp1) != NULL)
  149. {
  150. GPIO_Handle = lgGpiochipOpen(4);
  151. if (GPIO_Handle < 0)
  152. {
  153. printf( "gpiochip4 Export Failed\n");
  154. return -1;
  155. }
  156. }
  157. else
  158. {
  159. GPIO_Handle = lgGpiochipOpen(0);
  160. if (GPIO_Handle < 0)
  161. {
  162. printf( "gpiochip0 Export Failed\n");
  163. return -1;
  164. }
  165. }
  166. DEV_GPIO_Init();
  167. #if USE_SPI
  168. printf("USE_SPI\r\n");
  169. SPI_Handle = lgSpiOpen(0, 0, 10000000, 0);
  170. #elif USE_IIC
  171. printf("USE_IIC\r\n");
  172. OLED_DC_0;
  173. OLED_CS_0;
  174. I2C_Handle = lgI2cOpen(1, 0x3c, 0);
  175. #endif
  176. #endif
  177. return 0;
  178. }
  179. void DEV_SPI_WriteByte(uint8_t Value)
  180. {
  181. #ifdef USE_BCM2835_LIB
  182. bcm2835_spi_transfer(Value);
  183. #elif USE_WIRINGPI_LIB
  184. wiringPiSPIDataRW(0,&Value,1);
  185. #elif USE_DEV_LIB
  186. // printf("write data is %d\r\n", Value);
  187. lgSpiWrite(SPI_Handle,(char*)&Value, 1);
  188. #endif
  189. }
  190. void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len)
  191. {
  192. #ifdef USE_BCM2835_LIB
  193. char rData[Len];
  194. bcm2835_spi_transfernb(pData,rData,Len);
  195. #elif USE_WIRINGPI_LIB
  196. wiringPiSPIDataRW(0, pData, Len);
  197. #elif USE_DEV_LIB
  198. lgSpiWrite(SPI_Handle,(char*) pData, Len);
  199. #endif
  200. }
  201. void I2C_Write_Byte(uint8_t value, uint8_t Cmd)
  202. {
  203. #ifdef USE_BCM2835_LIB
  204. char wbuf[2]={Cmd, value};
  205. bcm2835_i2c_write(wbuf, 2);
  206. #elif USE_WIRINGPI_LIB
  207. int ref;
  208. //wiringPiI2CWrite(fd,Cmd);
  209. ref = wiringPiI2CWriteReg8(fd, (int)Cmd, (int)value);
  210. while(ref != 0) {
  211. ref = wiringPiI2CWriteReg8 (fd, (int)Cmd, (int)value);
  212. if(ref == 0)
  213. break;
  214. }
  215. #elif USE_DEV_LIB
  216. lgI2cWriteI2CBlockData(I2C_Handle,Cmd,(char *)&value,1);
  217. #endif
  218. }
  219. /******************************************************************************
  220. function: Module exits, closes SPI and BCM2835 library
  221. parameter:
  222. Info:
  223. ******************************************************************************/
  224. void DEV_ModuleExit(void)
  225. {
  226. #ifdef USE_BCM2835_LIB
  227. bcm2835_spi_end();
  228. bcm2835_i2c_end();
  229. bcm2835_close();
  230. #elif USE_WIRINGPI_LIB
  231. OLED_CS_0;
  232. OLED_RST_1;
  233. OLED_DC_0;
  234. #elif USE_DEV_LIB
  235. #endif
  236. }