/***************************************************************************** * | File : DEV_Config.c * | Author : Waveshare team * | Function : Hardware underlying interface * | Info : *---------------- * | This version: V3.0 * | Date : 2019-06-20 * | 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 the 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" #ifdef USE_BCM2835_LIB #include #elif USE_WIRINGPI_LIB #include #include #endif /** * GPIO read and write **/ void DEV_Digital_Write(UWORD Pin, UBYTE Value) { #ifdef USE_BCM2835_LIB bcm2835_gpio_write(Pin, Value); #elif USE_WIRINGPI_LIB digitalWrite(Pin, Value); #endif } UBYTE DEV_Digital_Read(UWORD Pin) { UBYTE Read_value = 0; #ifdef USE_BCM2835_LIB Read_value = bcm2835_gpio_lev(Pin); #elif USE_WIRINGPI_LIB Read_value = digitalRead(Pin); #endif return Read_value; } /** * SPI **/ void DEV_SPI_WriteByte(UBYTE Value) { #ifdef USE_BCM2835_LIB bcm2835_spi_transfer(Value); #elif USE_WIRINGPI_LIB wiringPiSPIDataRW(0,&Value,1); #endif } /** * delay x ms **/ void DEV_Delay_ms(UDOUBLE xms) { #ifdef USE_BCM2835_LIB bcm2835_delay(xms); #elif USE_WIRINGPI_LIB delay(xms); #endif } /****************************************************************************** function: Module Initialize, the BCM2835 library and initialize the pins, SPI protocol parameter: Info: ******************************************************************************/ UBYTE DEV_Module_Init(void) { #ifdef USE_BCM2835_LIB if(!bcm2835_init()) { printf("bcm2835 init failed !!! \r\n"); return 1; } else { printf("bcm2835 init success !!! \r\n"); } // GPIO Config bcm2835_gpio_fsel(EPD_RST_PIN, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(EPD_DC_PIN, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(EPD_CS_PIN, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(EPD_BUSY_PIN, BCM2835_GPIO_FSEL_INPT); 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 pinMode(EPD_RST_PIN, OUTPUT); pinMode(EPD_DC_PIN, OUTPUT); pinMode(EPD_CS_PIN, OUTPUT); pinMode(EPD_BUSY_PIN, INPUT); wiringPiSPISetup(0,32000000); // wiringPiSPISetupMode(0, 32000000, 0); #endif return 0; } /****************************************************************************** function: Module exits, closes SPI and BCM2835 library parameter: Info: ******************************************************************************/ void DEV_Module_Exit(void) { #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); #endif }