diff --git a/libraries/ESPhost/src/EspSpiDriver.cpp b/libraries/ESPhost/src/EspSpiDriver.cpp index 26b3a453..351e0fad 100644 --- a/libraries/ESPhost/src/EspSpiDriver.cpp +++ b/libraries/ESPhost/src/EspSpiDriver.cpp @@ -28,74 +28,40 @@ * Includes * ######## */ +#include #include "EspSpiDriver.h" /* ##################### * Configuration defines * ##################### */ - - -/* FSP SPI Channel */ -#ifdef USE_SCI_SPI_FSP_API -#define SPI_CHANNEL (0) -#else -#define SPI_CHANNEL (1) -#endif - -#define EXT_IRQ_CHANNEL (0) - #ifdef USE_ESP32_C3_DEVKIT_RUST_1 /* GPIOs */ -#define HANDSHAKE BSP_IO_PORT_05_PIN_05 -#define DATA_READY BSP_IO_PORT_08_PIN_02 -#define DATA_READY_PIN 33 +#define HANDSHAKE 60 +#define DATA_READY 33 /* SPI PIN definition */ -#define ESP_MISO BSP_IO_PORT_01_PIN_00 -#define ESP_MOSI BSP_IO_PORT_01_PIN_01 -#define ESP_CK BSP_IO_PORT_01_PIN_02 -#define ESP_CS BSP_IO_PORT_01_PIN_03 +#define SPIWIFI SPI1 +#define ESP_CS 48 #else /* GPIOs */ -#define ESP_RESET BSP_IO_PORT_08_PIN_04 -#define HANDSHAKE BSP_IO_PORT_08_PIN_06 -#define DATA_READY BSP_IO_PORT_08_PIN_03 -#define DATA_READY_PIN 100 +#define ESP_RESET 101 +#define HANDSHAKE 102 +#define DATA_READY 100 /* SPI PIN definition */ -#define ESP_MISO BSP_IO_PORT_01_PIN_00 -#define ESP_MOSI BSP_IO_PORT_01_PIN_01 -#define ESP_CK BSP_IO_PORT_01_PIN_02 -#define ESP_CS BSP_IO_PORT_01_PIN_04 +#define SPIWIFI SPI1 +#define ESP_CS 103 #endif /* ################# * PRIVATE Variables * ################# */ -static volatile unsigned int pi_msg_to_get_from_esp32 = 0; static volatile uint8_t esp32_spi_rx_buffer[MAX_SPI_BUFFER_SIZE]; static volatile uint8_t esp32_spi_tx_buffer[MAX_SPI_BUFFER_SIZE]; -static spi_instance_ctrl_t _esp_host_spi_ctrl; -static spi_cfg_t _esp_host_spi_cfg; -#ifdef USE_SCI_SPI_FSP_API -static sci_spi_extended_cfg_t _sci_spi_ext_cfg; -#else -static spi_extended_cfg_t _esp_host_spi_ext_cfg; -#endif - -static int spi_rx_waiting = 0; - -bool arePendingRxMsg() { - bool rv = spi_rx_waiting > 0 ? true : false; - spi_rx_waiting--; - return rv; -} - - static bool spi_transaction_in_progress = false; bool isSpiTransactionInProgress() { @@ -107,26 +73,12 @@ bool isSpiTransactionInProgress() { } return rv; } -void setSpiTransactionInPRogress() { spi_transaction_in_progress = true; } - -//static icu_instance_ctrl_t _esp_host_icu_ctrl; -//static external_irq_cfg_t _esp_host_icu_cfg; - -static spi_event_t _spi_cb_status = SPI_EVENT_TRANSFER_ABORTED; - - -//static CbkFuncRx_f esp_host_cb_rx_msg = nullptr; - /* ############################# * PRIVATE Functions declaration * ############################# */ -static void spi_callback(spi_callback_args_t *p_args); -static void ext_irq_callback(void); - - /* execute SPI communication, send the content of tx_buffer to ESP32, put the message received from ESP32 in rx_buffer */ static int esp_host_send_and_receive(void); @@ -160,118 +112,19 @@ int esp_host_spi_init(void) { /* ++++++++++++++++++++++++++++++++++ * GPIOs (HANDSHAKE and DATA_READY) * ++++++++++++++++++++++++++++++++++ */ - R_IOPORT_PinCfg(NULL, HANDSHAKE, IOPORT_CFG_PORT_DIRECTION_INPUT); - /* DATA READY is configure in attach interrupt function below */ - //#ifdef EXPLICIT_PIN_CONFIGURATION - R_IOPORT_PinCfg(NULL, DATA_READY, (uint32_t) (IOPORT_CFG_IRQ_ENABLE | IOPORT_CFG_PORT_DIRECTION_INPUT )); - - R_IOPORT_PinCfg(NULL, ESP_CS, IOPORT_CFG_PORT_DIRECTION_OUTPUT); - R_IOPORT_PinCfg(NULL, ESP_RESET, IOPORT_CFG_PORT_DIRECTION_OUTPUT); - //#endif + pinMode(HANDSHAKE, INPUT); + pinMode(DATA_READY, INPUT); + pinMode(ESP_CS, OUTPUT); + pinMode(ESP_RESET, OUTPUT); /* +++++ * SPI * +++++ */ - - /* INIT SPI peripheral PINs */ - #ifdef USE_SCI_SPI_FSP_API - R_IOPORT_PinCfg(&g_ioport_ctrl, ESP_MISO, (uint32_t) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_SCI0_2_4_6_8)); - R_IOPORT_PinCfg(&g_ioport_ctrl, ESP_MOSI, (uint32_t) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_SCI0_2_4_6_8)); - R_IOPORT_PinCfg(&g_ioport_ctrl, ESP_CK, (uint32_t) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_SCI0_2_4_6_8)); - #else - R_IOPORT_PinCfg(&g_ioport_ctrl, ESP_MISO, (uint32_t) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_SPI)); - R_IOPORT_PinCfg(&g_ioport_ctrl, ESP_MOSI, (uint32_t) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_SPI)); - R_IOPORT_PinCfg(&g_ioport_ctrl, ESP_CK, (uint32_t) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_SPI)); - #endif - //R_IOPORT_PinCfg(&g_ioport_ctrl, ESP_CS, (uint32_t) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_SCI0_2_4_6_8)); - - /* SPI configuration */ - #ifdef USE_SCI_SPI_FSP_API - _esp_host_spi_cfg.p_extend = &_sci_spi_ext_cfg;//_esp_host_spi_ext_cfg; - #else - _esp_host_spi_cfg.p_extend = &_esp_host_spi_ext_cfg; - #endif - - _esp_host_spi_cfg.p_callback = spi_callback; - _esp_host_spi_cfg.channel = SPI_CHANNEL; - _esp_host_spi_cfg.rxi_irq = FSP_INVALID_VECTOR; - _esp_host_spi_cfg.txi_irq = FSP_INVALID_VECTOR; - _esp_host_spi_cfg.tei_irq = FSP_INVALID_VECTOR; - _esp_host_spi_cfg.eri_irq = FSP_INVALID_VECTOR; - _esp_host_spi_cfg.rxi_ipl = (12); - _esp_host_spi_cfg.txi_ipl = (12); - _esp_host_spi_cfg.tei_ipl = (12); - _esp_host_spi_cfg.eri_ipl = (12); - _esp_host_spi_cfg.operating_mode = SPI_MODE_MASTER; - _esp_host_spi_cfg.clk_phase = SPI_CLK_PHASE_EDGE_ODD; - _esp_host_spi_cfg.clk_polarity = SPI_CLK_POLARITY_HIGH; - _esp_host_spi_cfg.mode_fault = SPI_MODE_FAULT_ERROR_DISABLE; - _esp_host_spi_cfg.bit_order = SPI_BIT_ORDER_MSB_FIRST; - _esp_host_spi_cfg.p_transfer_tx = NULL; - _esp_host_spi_cfg.p_transfer_rx = NULL; - _esp_host_spi_cfg.p_context = NULL; - - /* ????????????????????????? - * TODO: Use of DMA? or DTC? - * ????????????????????????? */ - - /* SPI extended configuration */ - - _esp_host_spi_ext_cfg.spi_clksyn = SPI_SSL_MODE_CLK_SYN; - _esp_host_spi_ext_cfg.spi_comm = SPI_COMMUNICATION_FULL_DUPLEX; - _esp_host_spi_ext_cfg.ssl_polarity = SPI_SSLP_HIGH; - _esp_host_spi_ext_cfg.ssl_select = SPI_SSL_SELECT_SSL0; - _esp_host_spi_ext_cfg.mosi_idle = SPI_MOSI_IDLE_VALUE_FIXING_DISABLE; - _esp_host_spi_ext_cfg.parity = SPI_PARITY_MODE_DISABLE; - _esp_host_spi_ext_cfg.byte_swap = SPI_BYTE_SWAP_DISABLE; - _esp_host_spi_ext_cfg.spck_div = { /* Actual calculated bitrate: 12000000. */ .spbr = 1, .brdv = 0 }; - _esp_host_spi_ext_cfg.spck_delay = SPI_DELAY_COUNT_1; - _esp_host_spi_ext_cfg.ssl_negation_delay = SPI_DELAY_COUNT_1; - _esp_host_spi_ext_cfg.next_access_delay = SPI_DELAY_COUNT_1; - - #ifdef USE_SCI_SPI_FSP_API - _sci_spi_ext_cfg.clk_div.cks = 0; - _sci_spi_ext_cfg.clk_div.brr = 11; - _sci_spi_ext_cfg.clk_div.mddr = 0; - - R_SCI_SPI_CalculateBitrate (24000000, &(_sci_spi_ext_cfg.clk_div), false); - #else - R_SPI_CalculateBitrate (24000000, &(_esp_host_spi_ext_cfg.spck_div)); - #endif - - /* +++++++++++ - * INTERRUPTS - * +++++++++++ */ - - SpiMasterIrqReq_t irq_req { - .ctrl = &_esp_host_spi_ctrl, - .cfg = &_esp_host_spi_cfg, - .hw_channel = (uint8_t)SPI_CHANNEL, - }; - #ifdef USE_SCI_SPI_FSP_API - if(!IRQManager::getInstance().addPeripheral(IRQ_SCI_SPI_MASTER, &irq_req)) { - #else - if(!IRQManager::getInstance().addPeripheral(IRQ_SPI_MASTER, &irq_req)) { - #endif - return ESP_HOSTED_SPI_DRIVER_INIT_IRQ_FAILED; - } - - - - /* +++++++++ - * OPEN SPI - * +++++++++ */ - #ifdef USE_SCI_SPI_FSP_API - /* Configure the SPI using the FSP HAL functionlity. */ - if (FSP_SUCCESS != R_SCI_SPI_Open(&_esp_host_spi_ctrl, &_esp_host_spi_cfg)) { - #else - if (FSP_SUCCESS != R_SPI_Open(&_esp_host_spi_ctrl, &_esp_host_spi_cfg)) { - #endif - return ESP_HOSTED_SPI_DRIVER_SPI_FAIL_OPEN; - } + digitalWrite(ESP_CS, HIGH); + SPIWIFI.begin(); - R_IOPORT_PinWrite(NULL, ESP_RESET, BSP_IO_LEVEL_HIGH); + digitalWrite(ESP_RESET, HIGH); spi_driver_initialized = true; return ESP_HOSTED_SPI_DRIVER_OK; } @@ -282,9 +135,7 @@ int esp_host_spi_init(void) { - false if ESP has no data to be transmitted */ bool esp_host_there_are_data_to_be_rx() { - bsp_io_level_t data_ready; - R_IOPORT_PinRead(NULL, DATA_READY, &data_ready); - bool rv = (data_ready == BSP_IO_LEVEL_HIGH); + bool rv = digitalRead(DATA_READY); #ifdef ESP_HOST_DEBUG_ENABLED_AVOID Serial.print("**** RX data? "); @@ -427,10 +278,8 @@ int esp_host_send_and_receive(void) { /* VERIFY IF ESP32 is ready to accept a SPI transaction */ bool esp_ready = false; - bsp_io_level_t handshake; do { - R_IOPORT_PinRead(NULL, HANDSHAKE, &handshake); - esp_ready = (handshake == BSP_IO_LEVEL_HIGH); + esp_ready = (digitalRead(HANDSHAKE) == HIGH); if(esp_ready) { #ifdef ESP_HOST_DEBUG_ENABLED_AVOID Serial.print("ESP ready? "); @@ -438,20 +287,19 @@ int esp_host_send_and_receive(void) { #endif break; } - R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MICROSECONDS); + delayMicroseconds(100); time_num++; } while(time_num < 5000); if(esp_ready) { + SPIWIFI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE2)); /* Put CS LOW */ - R_IOPORT_PinWrite(NULL, ESP_CS, BSP_IO_LEVEL_LOW); - R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MICROSECONDS); + digitalWrite(ESP_CS, LOW); + delayMicroseconds(100); /* memset RX buffer */ memset((void *)esp32_spi_rx_buffer,0x00, MAX_SPI_BUFFER_SIZE); - _spi_cb_status = SPI_EVENT_ERR_MODE_FAULT; - #ifdef ESP_HOST_DEBUG_ENABLED_AVOID Serial.println(" [SPI] Execute single SPI transaction:"); Serial.print(" [SPI] TX DATA: "); @@ -463,19 +311,10 @@ int esp_host_send_and_receive(void) { #endif - #ifdef USE_SCI_SPI_FSP_API - fsp_err_t err = R_SCI_SPI_WriteRead (&_esp_host_spi_ctrl, (void *)esp32_spi_tx_buffer, (void *)esp32_spi_rx_buffer, MAX_SPI_BUFFER_SIZE, SPI_BIT_WIDTH_8_BITS); - #else - fsp_err_t err = R_SPI_WriteRead (&_esp_host_spi_ctrl, (void *)esp32_spi_tx_buffer, (void *)esp32_spi_rx_buffer, MAX_SPI_BUFFER_SIZE, SPI_BIT_WIDTH_8_BITS); - #endif + for (int i = 0; i < MAX_SPI_BUFFER_SIZE; i++) { + esp32_spi_rx_buffer[i] = SPIWIFI.transfer(esp32_spi_tx_buffer[i]); + } - if(err == FSP_SUCCESS) { - bool exited = false; - /* wait for SPI transaction to finish */ - time_num = 0; - do { - - if(_spi_cb_status == SPI_EVENT_TRANSFER_COMPLETE) { rv = ESP_HOSTED_SPI_DRIVER_OK; //Serial.println("NO MORE IN PROGRESS"); @@ -487,56 +326,16 @@ int esp_host_send_and_receive(void) { } Serial.println(); #endif - exited = true; - } - else if(_spi_cb_status == SPI_EVENT_TRANSFER_ABORTED) { - rv = ESP_HOSTED_SPI_TX_RX_ABORTED; - exited = true; - } - R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MICROSECONDS); - time_num++; - } while(time_num < 5000 && !exited); spi_transaction_in_progress = false; - if(!exited) { - //Serial.print("SPI TIMEOUT"); - rv = ESP_HOSTED_SPI_TIMEOUT; - } - } - else { - #ifdef ESP_HOST_DEBUG_ENABLED - Serial.print("[ERROR]: SPI TRANSACTION FAILED! "); - Serial.println(err); - #endif - } } else { rv = ESP_HOSTED_SPI_ESP_NOT_READY; } /* in any case de-select ESP32 */ - R_IOPORT_PinWrite(NULL, ESP_CS, BSP_IO_LEVEL_HIGH); + digitalWrite(ESP_CS, HIGH); + SPIWIFI.endTransaction(); return rv; } - - - - -/* -------------------------------------------------------------------------- */ -static void spi_callback(spi_callback_args_t *p_args) { -/* -------------------------------------------------------------------------- */ - if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event) { - _spi_cb_status = SPI_EVENT_TRANSFER_COMPLETE; - } - else { - /* Updating the flag here to capture and handle all other error events */ - _spi_cb_status = SPI_EVENT_TRANSFER_ABORTED; - } -} - -/* -------------------------------------------------------------------------- */ -static void ext_irq_callback(void) { -/* -------------------------------------------------------------------------- */ - -} \ No newline at end of file diff --git a/libraries/ESPhost/src/EspSpiDriver.h b/libraries/ESPhost/src/EspSpiDriver.h index c2db4be7..ec29d60b 100644 --- a/libraries/ESPhost/src/EspSpiDriver.h +++ b/libraries/ESPhost/src/EspSpiDriver.h @@ -24,27 +24,14 @@ //#define ESP_HOST_DEBUG_ENABLED #include "Arduino.h" -#include "IRQManager.h" #include "CEspCommunication.h" -//#include "esp_host_protocol.h" - -#include "r_spi.h" -#include "r_sci_spi.h" - -using CbkFuncRx_f = void(*)(); #define ESP_HOSTED_SPI_DRIVER_OK 0 #define ESP_HOSTED_SPI_NOTHING_TO_TX_OR_RX 3 #define ESP_HOSTED_SPI_MESSAGE_RECEIVED 8 - -#define ESP_HOSTED_SPI_DRIVER_INIT_IRQ_FAILED 1 -#define ESP_HOSTED_SPI_DRIVER_SPI_FAIL_OPEN 2 -#define ESP_HOSTED_SPI_SPI_TRANSACTION_ERR 4 -#define ESP_HOSTED_SPI_ESP_NOT_READY 5 -#define ESP_HOSTED_SPI_TX_RX_ABORTED 6 -#define ESP_HOSTED_SPI_TIMEOUT 7 -#define ESP_HOSTED_MSG_PREPARATION_ERROR 9 +#define ESP_HOSTED_SPI_SPI_TRANSACTION_ERR 1 +#define ESP_HOSTED_SPI_ESP_NOT_READY 2 /* init the SPI driver */ int esp_host_spi_init(void); @@ -57,9 +44,5 @@ bool isEspSpiInitialized() ; bool arePendingRxMsg(); bool isSpiTransactionInProgress(); -void setSpiTransactionInPRogress(); - - - #endif \ No newline at end of file