/***************************************************************************** * | 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 "config.h" #include "DEV_Config.h" #include "driver/gpio.h" #include "driver/spi_master.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include #define SPI_PORT SPI2_HOST // /** // * GPIO // **/ // int EPD_RST_PIN; // int EPD_DC_PIN; // int EPD_CS_PIN; // int EPD_BUSY_PIN; // int EPD_CLK_PIN; // int EPD_MOSI_PIN; spi_device_handle_t spi_device; /** * GPIO read and write **/ void DEV_Digital_Write(UWORD Pin, UBYTE Value) { gpio_set_level(Pin, Value); } UBYTE DEV_Digital_Read(UWORD Pin) { return gpio_get_level(Pin); } /** * SPI **/ void DEV_SPI_WriteByte(uint8_t Value) { spi_transaction_t trans; memset(&trans, 0, sizeof(spi_transaction_t)); trans.flags = SPI_TRANS_USE_TXDATA; trans.length = 8; trans.tx_data[0] = Value; spi_device_transmit(spi_device, &trans); } void DEV_SPI_Write_nByte(uint8_t *pData, uint32_t Len) { if (Len == 0) return; spi_transaction_t trans; memset(&trans, 0, sizeof(spi_transaction_t)); trans.length = 8*Len; trans.tx_buffer = pData; spi_device_transmit(spi_device, &trans); } /** * GPIO Mode **/ void DEV_GPIO_Mode(UWORD Pin, UWORD Mode) { gpio_config_t io_conf = { .pin_bit_mask = (1ULL << Pin), .intr_type = GPIO_INTR_DISABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE, .pull_up_en = GPIO_PULLUP_DISABLE }; if(Mode == 0 || Mode == GPIO_MODE_INPUT) { io_conf.mode = GPIO_MODE_INPUT; io_conf.pull_up_en = GPIO_PULLUP_ENABLE; } else { io_conf.mode = GPIO_MODE_OUTPUT; } gpio_config(&io_conf); } /** * delay x ms **/ void DEV_Delay_ms(UDOUBLE xms) { vTaskDelay(pdMS_TO_TICKS(xms)); } void DEV_GPIO_Init(void) { // EPD_RST_PIN = 12; // EPD_DC_PIN = 8; // EPD_BUSY_PIN = 13; // EPD_CS_PIN = 9; // EPD_CLK_PIN = 10; // EPD_MOSI_PIN = 11; 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 protocol parameter: Info: ******************************************************************************/ UBYTE DEV_Module_Init(void) { // GPIO Config DEV_GPIO_Init(); spi_bus_config_t buscfg = { .mosi_io_num = SPI_MOSI, .miso_io_num = -1, .sclk_io_num = SPI_CLK, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = 4096 }; esp_err_t ret = spi_bus_initialize(SPI_PORT, &buscfg, 1); ESP_ERROR_CHECK(ret); spi_device_interface_config_t devcfg = { .clock_speed_hz = 4000 * 1000, .mode = 0, .queue_size = 7, .spics_io_num = -1 }; // Attach the device configuration to the initialized bus ret = spi_bus_add_device(SPI_PORT, &devcfg, &spi_device); ESP_ERROR_CHECK(ret); printf("DEV_Module_Init OK \r\n"); return 0; } void DEV_GPIO_Init_1(void) { //spi_deinit(SPI_PORT); //gpio_set_function(EPD_CLK_PIN, GPIO_FUNC_SPI); //gpio_set_function(EPD_MOSI_PIN, GPIO_FUNC_SPI); } void DEV_SPI_Init(void) { //spi_init(SPI_PORT, 4000 * 1000); //gpio_set_function(EPD_CLK_PIN, GPIO_FUNC_SPI); //gpio_set_function(EPD_MOSI_PIN, GPIO_FUNC_SPI); } void DEV_SPI_SendData(UBYTE Reg) { UBYTE i,j=Reg; DEV_GPIO_Mode(SPI_MOSI, 1); DEV_GPIO_Mode(SPI_CLK, 1); DEV_Digital_Write(EPD_CS_PIN, 0); for(i = 0; i<8; i++) { DEV_Digital_Write(SPI_CLK, 0); if (j & 0x80) { DEV_Digital_Write(SPI_MOSI, 1); } else { DEV_Digital_Write(SPI_MOSI, 0); } DEV_Digital_Write(SPI_CLK, 1); j = j << 1; } DEV_Digital_Write(SPI_CLK, 0); DEV_Digital_Write(EPD_CS_PIN, 1); } UBYTE DEV_SPI_ReadData(void) { UBYTE i,j=0xff; DEV_GPIO_Mode(SPI_MOSI, 0); DEV_GPIO_Mode(SPI_CLK, 1); DEV_Digital_Write(EPD_CS_PIN, 0); for(i = 0; i<8; i++) { DEV_Digital_Write(SPI_CLK, 0); j = j << 1; if (DEV_Digital_Read(SPI_MOSI)) { j = j | 0x01; } else { j= j & 0xfe; } DEV_Digital_Write(SPI_CLK, 1); } DEV_Digital_Write(SPI_CLK, 0); DEV_Digital_Write(EPD_CS_PIN, 1); return j; } /****************************************************************************** function: Module exits, closes SPI and BCM2835 library parameter: Info: ******************************************************************************/ void DEV_Module_Exit(void) { }