| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 | /****************************************************************************** | File      	:   DEV_Config.c* | Author      :   Waveshare team* | Function    :   Hardware underlying interface* | Info        :*----------------* |	This version:   V3.0* | Date        :   2019-07-31* | Info        :   ## Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documnetation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of theex Software, and to permit persons to  whom the Software is# furished to do so, subject to the following conditions:## The above copyright notice and this permission notice shall be included in# all copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN# THE SOFTWARE.#******************************************************************************/#include "DEV_Config.h"#include <fcntl.h>/** * GPIO**/int EPD_RST_PIN;int EPD_DC_PIN;int EPD_CS_PIN;int EPD_BUSY_PIN;/** * GPIO read and write**/void DEV_Digital_Write(UWORD Pin, UBYTE Value){#ifdef RPI#ifdef USE_BCM2835_LIB	bcm2835_gpio_write(Pin, Value);#elif USE_WIRINGPI_LIB	digitalWrite(Pin, Value);#elif USE_DEV_LIB	SYSFS_GPIO_Write(Pin, Value);#endif#endif#ifdef JETSON#ifdef USE_DEV_LIB	SYSFS_GPIO_Write(Pin, Value);#elif USE_HARDWARE_LIB	Debug("not support");#endif#endif}UBYTE DEV_Digital_Read(UWORD Pin){	UBYTE Read_value = 0;#ifdef RPI#ifdef USE_BCM2835_LIB	Read_value = bcm2835_gpio_lev(Pin);#elif USE_WIRINGPI_LIB	Read_value = digitalRead(Pin);#elif USE_DEV_LIB	Read_value = SYSFS_GPIO_Read(Pin);#endif#endif#ifdef JETSON#ifdef USE_DEV_LIB	Read_value = SYSFS_GPIO_Read(Pin);#elif USE_HARDWARE_LIB	Debug("not support");#endif#endif	return Read_value;}/** * SPI**/void DEV_SPI_WriteByte(uint8_t Value){#ifdef RPI#ifdef USE_BCM2835_LIB	bcm2835_spi_transfer(Value);#elif USE_WIRINGPI_LIB	wiringPiSPIDataRW(0,&Value,1);#elif USE_DEV_LIB	DEV_HARDWARE_SPI_TransferByte(Value);#endif#endif#ifdef JETSON#ifdef USE_DEV_LIB	SYSFS_software_spi_transfer(Value);#elif USE_HARDWARE_LIB	Debug("not support");#endif#endif}void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len){#ifdef RPI#ifdef USE_BCM2835_LIB	char rData[Len];	bcm2835_spi_transfernb((char *)pData,rData,Len);#elif USE_WIRINGPI_LIB	wiringPiSPIDataRW(0, pData, Len);#elif USE_DEV_LIB	DEV_HARDWARE_SPI_Transfer(pData, Len);#endif#endif#ifdef JETSON#ifdef USE_DEV_LIB	//JETSON nano waits for hardware SPI	Debug("not support");#elif USE_HARDWARE_LIB	Debug("not support");#endif#endif}/** * GPIO Mode**/void DEV_GPIO_Mode(UWORD Pin, UWORD Mode){#ifdef RPI#ifdef USE_BCM2835_LIB	if(Mode == 0 || Mode == BCM2835_GPIO_FSEL_INPT) {		bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_INPT);	} else {		bcm2835_gpio_fsel(Pin, BCM2835_GPIO_FSEL_OUTP);	}#elif USE_WIRINGPI_LIB	if(Mode == 0 || Mode == INPUT) {		pinMode(Pin, INPUT);		pullUpDnControl(Pin, PUD_UP);	} else {		pinMode(Pin, OUTPUT);		// Debug (" %d OUT \r\n",Pin);	}#elif USE_DEV_LIB	SYSFS_GPIO_Export(Pin);	if(Mode == 0 || Mode == SYSFS_GPIO_IN) {		SYSFS_GPIO_Direction(Pin, SYSFS_GPIO_IN);		// Debug("IN Pin = %d\r\n",Pin);	} else {		SYSFS_GPIO_Direction(Pin, SYSFS_GPIO_OUT);		// Debug("OUT Pin = %d\r\n",Pin);	}#endif#endif#ifdef JETSON#ifdef USE_DEV_LIB	SYSFS_GPIO_Export(Pin);	SYSFS_GPIO_Direction(Pin, Mode);#elif USE_HARDWARE_LIB	Debug("not support");#endif#endif}/** * delay x ms**/void DEV_Delay_ms(UDOUBLE xms){#ifdef RPI#ifdef USE_BCM2835_LIB	bcm2835_delay(xms);#elif USE_WIRINGPI_LIB	delay(xms);#elif USE_DEV_LIB	UDOUBLE i;	for(i=0; i < xms; i++) {		usleep(1000);	}#endif#endif#ifdef JETSON	UDOUBLE i;	for(i=0; i < xms; i++) {		usleep(1000);	}#endif}static int DEV_Equipment_Testing(void){	int i;	int fd;	char value_str[20];	fd = open("/etc/issue", O_RDONLY);    printf("Current environment: ");	while(1) {		if (fd < 0) {			Debug( "Read failed Pin\n");			return -1;		}		for(i=0;; i++) {			if (read(fd, &value_str[i], 1) < 0) {				Debug( "failed to read value!\n");				return -1;			}			if(value_str[i] ==32) {				printf("\r\n");				break;			}			printf("%c",value_str[i]);		}		break;	}#ifdef RPI	if(i<5) {		printf("Unrecognizable\r\n");	} else {		char RPI_System[10]   = {"Raspbian"};		for(i=0; i<6; i++) {			if(RPI_System[i]!= value_str[i]) {				printf("Please make JETSON !!!!!!!!!!\r\n");				return -1;			}		}	}#endif#ifdef JETSON	if(i<5) {		Debug("Unrecognizable\r\n");	} else {		char JETSON_System[10]= {"Ubuntu"};		for(i=0; i<6; i++) {			if(JETSON_System[i]!= value_str[i] ) {				printf("Please make RPI !!!!!!!!!!\r\n");				return -1;			}		}	}#endif	return 0;}void DEV_GPIO_Init(void){#ifdef RPI	EPD_RST_PIN     = 17;	EPD_DC_PIN      = 25;	EPD_CS_PIN      = 8;	EPD_BUSY_PIN    = 24;#elif JETSON	EPD_RST_PIN     = GPIO17;	EPD_DC_PIN      = GPIO25;	EPD_CS_PIN      = SPI0_CS0;	EPD_BUSY_PIN    = GPIO24;#endif	DEV_GPIO_Mode(EPD_RST_PIN, 1);	DEV_GPIO_Mode(EPD_DC_PIN, 1);	DEV_GPIO_Mode(EPD_CS_PIN, 1);	DEV_GPIO_Mode(EPD_BUSY_PIN, 0);	DEV_Digital_Write(EPD_CS_PIN, 1);}/******************************************************************************function:	Module Initialize, the library and initialize the pins, SPI protocolparameter:Info:******************************************************************************/UBYTE DEV_Module_Init(void){    printf("/***********************************/ \r\n");	if(DEV_Equipment_Testing() < 0) {		return 1;	}#ifdef RPI#ifdef USE_BCM2835_LIB	if(!bcm2835_init()) {		printf("bcm2835 init failed  !!! \r\n");		return 1;	} else {		printf("bcm2835 init success !!! \r\n");	}	// GPIO Config	DEV_GPIO_Init();	bcm2835_spi_begin();                                         //Start spi interface, set spi pin for the reuse function	bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);     //High first transmission	bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                  //spi mode 0	bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128);  //Frequency	bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                     //set CE0	bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);     //enable cs0#elif USE_WIRINGPI_LIB	//if(wiringPiSetup() < 0)//use wiringpi Pin number table	if(wiringPiSetupGpio() < 0) { //use BCM2835 Pin number table		printf("set wiringPi lib failed	!!! \r\n");		return 1;	} else {		printf("set wiringPi lib success !!! \r\n");	}	// GPIO Config	DEV_GPIO_Init();	wiringPiSPISetup(0,10000000);	// wiringPiSPISetupMode(0, 32000000, 0);#elif USE_DEV_LIB	printf("Write and read /dev/spidev0.0 \r\n");	DEV_GPIO_Init();	DEV_HARDWARE_SPI_begin("/dev/spidev0.0");    DEV_HARDWARE_SPI_setSpeed(10000000);#endif#elif JETSON#ifdef USE_DEV_LIB	DEV_GPIO_Init();	printf("Software spi\r\n");	SYSFS_software_spi_begin();	SYSFS_software_spi_setBitOrder(SOFTWARE_SPI_MSBFIRST);	SYSFS_software_spi_setDataMode(SOFTWARE_SPI_Mode0);	SYSFS_software_spi_setClockDivider(SOFTWARE_SPI_CLOCK_DIV4);#elif USE_HARDWARE_LIB	printf("Write and read /dev/spidev0.0 \r\n");	DEV_GPIO_Init();	DEV_HARDWARE_SPI_begin("/dev/spidev0.0");#endif#endif    printf("/***********************************/ \r\n");	return 0;}/******************************************************************************function:	Module exits, closes SPI and BCM2835 libraryparameter:Info:******************************************************************************/void DEV_Module_Exit(void){#ifdef RPI#ifdef USE_BCM2835_LIB	DEV_Digital_Write(EPD_CS_PIN, LOW);	DEV_Digital_Write(EPD_DC_PIN, LOW);	DEV_Digital_Write(EPD_RST_PIN, LOW);	bcm2835_spi_end();	bcm2835_close();#elif USE_WIRINGPI_LIB	DEV_Digital_Write(EPD_CS_PIN, 0);	DEV_Digital_Write(EPD_DC_PIN, 0);	DEV_Digital_Write(EPD_RST_PIN, 0);#elif USE_DEV_LIB	DEV_HARDWARE_SPI_end();	DEV_Digital_Write(EPD_CS_PIN, 0);	DEV_Digital_Write(EPD_DC_PIN, 0);	DEV_Digital_Write(EPD_RST_PIN, 0);#endif#elif JETSON#ifdef USE_DEV_LIB	SYSFS_GPIO_Unexport(EPD_CS_PIN);	SYSFS_GPIO_Unexport(EPD_DC_PIN);	SYSFS_GPIO_Unexport(EPD_RST_PIN);	SYSFS_GPIO_Unexport(EPD_BUSY_PIN);#elif USE_HARDWARE_LIB	Debug("not support");#endif#endif}
 |