waveshare_epaper

Waveshare e-paper display shenanigans
git clone git://bsandro.tech/waveshare_epaper
Log | Files | Refs | README

DEV_Config.c (8881B)


      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 /**
     34  * GPIO
     35 **/
     36 int EPD_RST_PIN;
     37 int EPD_DC_PIN;
     38 int EPD_CS_PIN;
     39 int EPD_BUSY_PIN;
     40 
     41 /**
     42  * GPIO read and write
     43 **/
     44 void DEV_Digital_Write(UWORD Pin, UBYTE Value)
     45 {
     46 #ifdef RPI
     47 #ifdef USE_BCM2835_LIB
     48 	bcm2835_gpio_write(Pin, Value);
     49 #elif USE_WIRINGPI_LIB
     50 	digitalWrite(Pin, Value);
     51 #elif USE_DEV_LIB
     52 	SYSFS_GPIO_Write(Pin, Value);
     53 #endif
     54 #endif
     55 
     56 #ifdef JETSON
     57 #ifdef USE_DEV_LIB
     58 	SYSFS_GPIO_Write(Pin, Value);
     59 #elif USE_HARDWARE_LIB
     60 	Debug("not support");
     61 #endif
     62 #endif
     63 }
     64 
     65 UBYTE DEV_Digital_Read(UWORD Pin)
     66 {
     67 	UBYTE Read_value = 0;
     68 #ifdef RPI
     69 #ifdef USE_BCM2835_LIB
     70 	Read_value = bcm2835_gpio_lev(Pin);
     71 #elif USE_WIRINGPI_LIB
     72 	Read_value = digitalRead(Pin);
     73 #elif USE_DEV_LIB
     74 	Read_value = SYSFS_GPIO_Read(Pin);
     75 #endif
     76 #endif
     77 
     78 #ifdef JETSON
     79 #ifdef USE_DEV_LIB
     80 	Read_value = SYSFS_GPIO_Read(Pin);
     81 #elif USE_HARDWARE_LIB
     82 	Debug("not support");
     83 #endif
     84 #endif
     85 	return Read_value;
     86 }
     87 
     88 /**
     89  * SPI
     90 **/
     91 void DEV_SPI_WriteByte(uint8_t Value)
     92 {
     93 #ifdef RPI
     94 #ifdef USE_BCM2835_LIB
     95 	bcm2835_spi_transfer(Value);
     96 #elif USE_WIRINGPI_LIB
     97 	wiringPiSPIDataRW(0,&Value,1);
     98 #elif USE_DEV_LIB
     99 	DEV_HARDWARE_SPI_TransferByte(Value);
    100 #endif
    101 #endif
    102 
    103 #ifdef JETSON
    104 #ifdef USE_DEV_LIB
    105 	SYSFS_software_spi_transfer(Value);
    106 #elif USE_HARDWARE_LIB
    107 	Debug("not support");
    108 #endif
    109 #endif
    110 }
    111 
    112 void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len)
    113 {
    114 #ifdef RPI
    115 #ifdef USE_BCM2835_LIB
    116 	char rData[Len];
    117 	bcm2835_spi_transfernb(pData,rData,Len);
    118 #elif USE_WIRINGPI_LIB
    119 	wiringPiSPIDataRW(0, pData, Len);
    120 #elif USE_DEV_LIB
    121 	DEV_HARDWARE_SPI_Transfer(pData, Len);
    122 #endif
    123 #endif
    124 
    125 #ifdef JETSON
    126 #ifdef USE_DEV_LIB
    127 	//JETSON nano waits for hardware SPI
    128 	Debug("not support");
    129 #elif USE_HARDWARE_LIB
    130 	Debug("not support");
    131 #endif
    132 #endif
    133 }
    134 
    135 /**
    136  * GPIO Mode
    137 **/
    138 void DEV_GPIO_Mode(UWORD Pin, UWORD Mode)
    139 {
    140 #ifdef RPI
    141 #ifdef USE_BCM2835_LIB
    142 	if(Mode == 0 || Mode == BCM2835_GPIO_FSEL_INPT) {
    143 		bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_INPT);
    144 	} else {
    145 		bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_OUTP);
    146 	}
    147 #elif USE_WIRINGPI_LIB
    148 	if(Mode == 0 || Mode == INPUT) {
    149 		pinMode(Pin, INPUT);
    150 		pullUpDnControl(Pin, PUD_UP);
    151 	} else {
    152 		pinMode(Pin, OUTPUT);
    153 		// Debug (" %d OUT \r\n",Pin);
    154 	}
    155 #elif USE_DEV_LIB
    156 	SYSFS_GPIO_Export(Pin);
    157 	if(Mode == 0 || Mode == SYSFS_GPIO_IN) {
    158 		SYSFS_GPIO_Direction(Pin, SYSFS_GPIO_IN);
    159 		// Debug("IN Pin = %d\r\n",Pin);
    160 	} else {
    161 		SYSFS_GPIO_Direction(Pin, SYSFS_GPIO_OUT);
    162 		// Debug("OUT Pin = %d\r\n",Pin);
    163 	}
    164 #endif
    165 #endif
    166 
    167 #ifdef JETSON
    168 #ifdef USE_DEV_LIB
    169 	SYSFS_GPIO_Export(Pin);
    170 	SYSFS_GPIO_Direction(Pin, Mode);
    171 #elif USE_HARDWARE_LIB
    172 	Debug("not support");
    173 #endif
    174 #endif
    175 }
    176 
    177 /**
    178  * delay x ms
    179 **/
    180 void DEV_Delay_ms(UDOUBLE xms)
    181 {
    182 #ifdef RPI
    183 #ifdef USE_BCM2835_LIB
    184 	bcm2835_delay(xms);
    185 #elif USE_WIRINGPI_LIB
    186 	delay(xms);
    187 #elif USE_DEV_LIB
    188 	UDOUBLE i;
    189 	for(i=0; i < xms; i++) {
    190 		usleep(1000);
    191 	}
    192 #endif
    193 #endif
    194 
    195 #ifdef JETSON
    196 	UDOUBLE i;
    197 	for(i=0; i < xms; i++) {
    198 		usleep(1000);
    199 	}
    200 #endif
    201 }
    202 
    203 static int DEV_Equipment_Testing(void)
    204 {
    205 	int i;
    206 	int fd;
    207 	char value_str[20];
    208 	fd = open("/etc/issue", O_RDONLY);
    209     printf("Current environment: ");
    210 	while(1) {
    211 		if (fd < 0) {
    212 			Debug( "Read failed Pin\n");
    213 			return -1;
    214 		}
    215 		for(i=0;; i++) {
    216 			if (read(fd, &value_str[i], 1) < 0) {
    217 				Debug( "failed to read value!\n");
    218 				return -1;
    219 			}
    220 			if(value_str[i] ==32) {
    221 				printf("\r\n");
    222 				break;
    223 			}
    224 			printf("%c",value_str[i]);
    225 		}
    226 		break;
    227 	}
    228 #ifdef RPI
    229 	if(i<5) {
    230 		printf("Unrecognizable\r\n");
    231 	} else {
    232 		char RPI_System[10]   = {"Debian"};
    233 		for(i=0; i<6; i++) {
    234 			if(RPI_System[i]!= value_str[i]) {
    235 				printf("Please make JETSON !!!!!!!!!!\r\n");
    236 				return -1;
    237 			}
    238 		}
    239 	}
    240 #endif
    241 #ifdef JETSON
    242 	if(i<5) {
    243 		Debug("Unrecognizable\r\n");
    244 	} else {
    245 		char JETSON_System[10]= {"Ubuntu"};
    246 		for(i=0; i<6; i++) {
    247 			if(JETSON_System[i]!= value_str[i] ) {
    248 				printf("Please make RPI !!!!!!!!!!\r\n");
    249 				return -1;
    250 			}
    251 		}
    252 	}
    253 #endif
    254 	return 0;
    255 }
    256 
    257 
    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_BUSY_PIN    = 24;
    266 #elif JETSON
    267 	EPD_RST_PIN     = GPIO17;
    268 	EPD_DC_PIN      = GPIO25;
    269 	EPD_CS_PIN      = SPI0_CS0;
    270 	EPD_BUSY_PIN    = GPIO24;
    271 #endif
    272 
    273 	DEV_GPIO_Mode(EPD_RST_PIN, 1);
    274 	DEV_GPIO_Mode(EPD_DC_PIN, 1);
    275 	DEV_GPIO_Mode(EPD_CS_PIN, 1);
    276 	DEV_GPIO_Mode(EPD_BUSY_PIN, 0);
    277 
    278 	DEV_Digital_Write(EPD_CS_PIN, 1);
    279 }
    280 /******************************************************************************
    281 function:	Module Initialize, the library and initialize the pins, SPI protocol
    282 parameter:
    283 Info:
    284 ******************************************************************************/
    285 UBYTE DEV_Module_Init(void)
    286 {
    287     printf("/***********************************/ \r\n");
    288 	if(DEV_Equipment_Testing() < 0) {
    289 		return 1;
    290 	}
    291 #ifdef RPI
    292 #ifdef USE_BCM2835_LIB
    293 	if(!bcm2835_init()) {
    294 		printf("bcm2835 init failed  !!! \r\n");
    295 		return 1;
    296 	} else {
    297 		printf("bcm2835 init success !!! \r\n");
    298 	}
    299 
    300 	// GPIO Config
    301 	DEV_GPIO_Init();
    302 
    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 
    310 #elif USE_WIRINGPI_LIB
    311 	//if(wiringPiSetup() < 0)//use wiringpi Pin number table
    312 	if(wiringPiSetupGpio() < 0) { //use BCM2835 Pin number table
    313 		printf("set wiringPi lib failed	!!! \r\n");
    314 		return 1;
    315 	} else {
    316 		printf("set wiringPi lib success !!! \r\n");
    317 	}
    318 
    319 	// GPIO Config
    320 	DEV_GPIO_Init();
    321 	wiringPiSPISetup(0,10000000);
    322 	// wiringPiSPISetupMode(0, 32000000, 0);
    323 #elif USE_DEV_LIB
    324 	printf("Write and read /dev/spidev0.0 \r\n");
    325 	DEV_GPIO_Init();
    326 	DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
    327     DEV_HARDWARE_SPI_setSpeed(10000000);
    328 #endif
    329 
    330 #elif JETSON
    331 #ifdef USE_DEV_LIB
    332 	DEV_GPIO_Init();
    333 	printf("Software spi\r\n");
    334 	SYSFS_software_spi_begin();
    335 	SYSFS_software_spi_setBitOrder(SOFTWARE_SPI_MSBFIRST);
    336 	SYSFS_software_spi_setDataMode(SOFTWARE_SPI_Mode0);
    337 	SYSFS_software_spi_setClockDivider(SOFTWARE_SPI_CLOCK_DIV4);
    338 #elif USE_HARDWARE_LIB
    339 	printf("Write and read /dev/spidev0.0 \r\n");
    340 	DEV_GPIO_Init();
    341 	DEV_HARDWARE_SPI_begin("/dev/spidev0.0");
    342 #endif
    343 
    344 #endif
    345     printf("/***********************************/ \r\n");
    346 	return 0;
    347 }
    348 
    349 /******************************************************************************
    350 function:	Module exits, closes SPI and BCM2835 library
    351 parameter:
    352 Info:
    353 ******************************************************************************/
    354 void DEV_Module_Exit(void)
    355 {
    356 #ifdef RPI
    357 #ifdef USE_BCM2835_LIB
    358 	DEV_Digital_Write(EPD_CS_PIN, LOW);
    359 	DEV_Digital_Write(EPD_DC_PIN, LOW);
    360 	DEV_Digital_Write(EPD_RST_PIN, LOW);
    361 
    362 	bcm2835_spi_end();
    363 	bcm2835_close();
    364 #elif USE_WIRINGPI_LIB
    365 	DEV_Digital_Write(EPD_CS_PIN, 0);
    366 	DEV_Digital_Write(EPD_DC_PIN, 0);
    367 	DEV_Digital_Write(EPD_RST_PIN, 0);
    368 #elif USE_DEV_LIB
    369 	DEV_HARDWARE_SPI_end();
    370 	DEV_Digital_Write(EPD_CS_PIN, 0);
    371 	DEV_Digital_Write(EPD_DC_PIN, 0);
    372 	DEV_Digital_Write(EPD_RST_PIN, 0);
    373 #endif
    374 
    375 #elif JETSON
    376 #ifdef USE_DEV_LIB
    377 	SYSFS_GPIO_Unexport(EPD_CS_PIN);
    378 	SYSFS_GPIO_Unexport(EPD_DC_PIN);
    379 	SYSFS_GPIO_Unexport(EPD_RST_PIN);
    380 	SYSFS_GPIO_Unexport(EPD_BUSY_PIN);
    381 #elif USE_HARDWARE_LIB
    382 	Debug("not support");
    383 #endif
    384 #endif
    385 }