From 0ad254fc9d0eff9df0eeed71a48c66a5fc3057db Mon Sep 17 00:00:00 2001 From: David Crocker Date: Wed, 21 Sep 2022 08:53:49 +0100 Subject: Fixes for ESP32C3 on D3Mini --- src/Config/Pins_Duet3_MB6HC.h | 2 +- src/Networking/ESP8266WiFi/WiFiInterface.cpp | 62 +++++++++------------- .../ESP8266WiFi/WifiFirmwareUploader.cpp | 33 ++++-------- 3 files changed, 35 insertions(+), 62 deletions(-) diff --git a/src/Config/Pins_Duet3_MB6HC.h b/src/Config/Pins_Duet3_MB6HC.h index ba4f363f..2af57679 100644 --- a/src/Config/Pins_Duet3_MB6HC.h +++ b/src/Config/Pins_Duet3_MB6HC.h @@ -408,7 +408,7 @@ constexpr Pin APIN_SerialWiFi_TXD = PortDPin(19); constexpr Pin APIN_SerialWiFi_RXD = PortDPin(18); constexpr GpioPinFunction SerialWiFiPeriphMode = GpioPinFunction::C; -constexpr Pin EspResetPin = PortCPin(14); // Low on this in holds the WiFi module in reset (ESP_EN) +constexpr Pin EspEnablePin = PortCPin(14); // Low on this in holds the WiFi module in reset (ESP_EN) constexpr Pin EspDataReadyPin = PortAPin(2); // Input from the WiFi module indicating that it wants to transfer data (ESP GPIO0) constexpr Pin SamTfrReadyPin = PortEPin(2); // Output from the SAM to the WiFi module indicating we can accept a data transfer (ESP GPIO8) constexpr Pin SamCsPin = PortCPin(25); // SPI NPCS pin, input from WiFi module diff --git a/src/Networking/ESP8266WiFi/WiFiInterface.cpp b/src/Networking/ESP8266WiFi/WiFiInterface.cpp index a8cb72cf..e30509aa 100644 --- a/src/Networking/ESP8266WiFi/WiFiInterface.cpp +++ b/src/Networking/ESP8266WiFi/WiFiInterface.cpp @@ -533,11 +533,11 @@ void WiFiInterface::Start() noexcept { // The ESP8266 is held in a reset state by a pulldown resistor until we enable it. // Make sure the ESP8266 is in the reset state +#if !WIFI_USES_ESP32 pinMode(EspResetPin, OUTPUT_LOW); +#endif -#if !WIFI_USES_ESP32 pinMode(EspEnablePin, OUTPUT_LOW); -#endif // Set up our transfer request pin (GPIO4) as an output and set it low pinMode(SamTfrReadyPin, OUTPUT_LOW); @@ -589,10 +589,11 @@ void WiFiInterface::Stop() noexcept MutexLocker lock(interfaceMutex); digitalWrite(SamTfrReadyPin, false); // tell the ESP we can't receive - digitalWrite(EspResetPin, false); // put the ESP back into reset + #if !WIFI_USES_ESP32 - digitalWrite(EspEnablePin, false); + digitalWrite(EspResetPin, false); // put the ESP back into reset #endif + digitalWrite(EspEnablePin, false); DisableEspInterrupt(); // ignore IRQs from the transfer request pin NVIC_DisableIRQ(ESP_SPI_IRQn); @@ -1522,16 +1523,17 @@ static bool spi_dma_check_rx_complete() noexcept #endif #if USE_XDMAC - const uint32_t status = xdmac_channel_get_status(XDMAC); - const uint32_t channelStatus = XDMAC->XDMAC_CHID[DmacChanWiFiRx].XDMAC_CC; - if ( ((status & (1 << DmacChanWiFiRx)) == 0) // channel is not enabled - || (((channelStatus & XDMAC_CC_RDIP) == XDMAC_CC_RDIP_DONE) && ((channelStatus & XDMAC_CC_WRIP) == XDMAC_CC_WRIP_DONE)) // controller is neither reading nor writing via this channel - ) + if ((XDMAC->XDMAC_GS & (1u << DmacChanWiFiRx)) == 0) { - // Disable the channel. - // We also need to set the resume bit, otherwise it remains suspended when we re-enable it. - xdmac_channel_disable(XDMAC, DmacChanWiFiRx); - xdmac_channel_readwrite_resume(XDMAC, DmacChanWiFiRx); + return true; // channel is not enabled + } + + if ((XDMAC->XDMAC_CHID[DmacChanWiFiRx].XDMAC_CC & (XDMAC_CC_RDIP | XDMAC_CC_WRIP)) == 0) + { +// XDMAC->XDMAC_GSWF = (1u << DmacChanWiFiRx); // flush the channel +// while ((XDMAC->XDMAC_CHID[DmacChanWiFiRx].XDMAC_CIS & XDMAC_CIS_FIS) == 0) { } + XDMAC->XDMAC_GD = (1u << DmacChanWiFiRx); // disable the channel + while ((XDMAC->XDMAC_GS & (1u << DmacChanWiFiRx)) != 0) { } // wait for disable to complete return true; } #endif @@ -1563,12 +1565,6 @@ static void spi_tx_dma_setup(const void *buf, uint32_t transferLength) noexcept #if USE_XDMAC xdmac_disable_interrupt(XDMAC, DmacChanWiFiTx); - const uint32_t xdmaint = (XDMAC_CIE_BIE | - XDMAC_CIE_DIE | - XDMAC_CIE_FIE | - XDMAC_CIE_RBIE | - XDMAC_CIE_WBIE | - XDMAC_CIE_ROIE); xdmac_tx_cfg.mbr_ubc = transferLength; xdmac_tx_cfg.mbr_sa = reinterpret_cast(buf); @@ -1590,7 +1586,6 @@ static void spi_tx_dma_setup(const void *buf, uint32_t transferLength) noexcept xdmac_configure_transfer(XDMAC, DmacChanWiFiTx, &xdmac_tx_cfg); xdmac_channel_set_descriptor_control(XDMAC, DmacChanWiFiTx, 0); - xdmac_channel_disable_interrupt(XDMAC, DmacChanWiFiTx, xdmaint); #endif #if USE_DMAC_MANAGER @@ -1624,12 +1619,6 @@ static void spi_rx_dma_setup(void *buf, uint32_t transferLength) noexcept #if USE_XDMAC xdmac_disable_interrupt(XDMAC, DmacChanWiFiRx); - const uint32_t xdmaint = (XDMAC_CIE_BIE | - XDMAC_CIE_DIE | - XDMAC_CIE_FIE | - XDMAC_CIE_RBIE | - XDMAC_CIE_WBIE | - XDMAC_CIE_ROIE); xdmac_rx_cfg.mbr_ubc = transferLength; xdmac_rx_cfg.mbr_da = reinterpret_cast(buf); @@ -1651,7 +1640,6 @@ static void spi_rx_dma_setup(void *buf, uint32_t transferLength) noexcept xdmac_configure_transfer(XDMAC, DmacChanWiFiRx, &xdmac_rx_cfg); xdmac_channel_set_descriptor_control(XDMAC, DmacChanWiFiRx, 0); - xdmac_channel_disable_interrupt(XDMAC, DmacChanWiFiRx, xdmaint); #endif #if USE_DMAC_MANAGER @@ -2021,13 +2009,13 @@ void WiFiInterface::SpiInterrupt() noexcept // Start the ESP void WiFiInterface::StartWiFi() noexcept { - digitalWrite(EspResetPin, true); - #if !WIFI_USES_ESP32 + digitalWrite(EspResetPin, true); delayMicroseconds(150); // ESP8266 datasheet specifies minimum 100us from releasing reset to power up - digitalWrite(EspEnablePin, true); #endif + digitalWrite(EspEnablePin, true); + #if !SAME5x SetPinFunction(APIN_SerialWiFi_TXD, SerialWiFiPeriphMode); // connect the pins to the UART SetPinFunction(APIN_SerialWiFi_RXD, SerialWiFiPeriphMode); // connect the pins to the UART @@ -2046,11 +2034,11 @@ void WiFiInterface::StartWiFi() noexcept // Reset the ESP8266 and leave held in reset void WiFiInterface::ResetWiFi() noexcept { +#if !WIFI_USES_ESP32 pinMode(EspResetPin, OUTPUT_LOW); // assert ESP8266 /RESET +#endif -#if !WIFI_USES_ESP32 pinMode(EspEnablePin, OUTPUT_LOW); -#endif #if !defined(SAME5x) pinMode(APIN_SerialWiFi_TXD, INPUT_PULLUP); // just enable pullups on TxD and RxD pins @@ -2079,13 +2067,13 @@ void WiFiInterface::ResetWiFiForUpload(bool external) noexcept serialRunning = false; } +#if !WIFI_USES_ESP32 // Make sure the ESP8266 is in the reset state pinMode(EspResetPin, OUTPUT_LOW); +#endif -#if !WIFI_USES_ESP32 // Power down the ESP8266 pinMode(EspEnablePin, OUTPUT_LOW); -#endif // Set up our transfer request pin (GPIO4) as an output and set it low pinMode(SamTfrReadyPin, OUTPUT_LOW); @@ -2117,14 +2105,14 @@ void WiFiInterface::ResetWiFiForUpload(bool external) noexcept #endif } +#if !WIFI_USES_ESP32 // Release the reset on the ESP8266 digitalWrite(EspResetPin, true); + delayMicroseconds(150); // ESP8266 datasheet specifies minimum 100us from releasing reset to power up +#endif -#if !WIFI_USES_ESP32 // Take the ESP8266 out of power down - delayMicroseconds(150); // ESP8266 datasheet specifies minimum 100us from releasing reset to power up digitalWrite(EspEnablePin, true); -#endif } #endif // HAS_WIFI_NETWORKING diff --git a/src/Networking/ESP8266WiFi/WifiFirmwareUploader.cpp b/src/Networking/ESP8266WiFi/WifiFirmwareUploader.cpp index 4b2dc953..c9fc3e3f 100644 --- a/src/Networking/ESP8266WiFi/WifiFirmwareUploader.cpp +++ b/src/Networking/ESP8266WiFi/WifiFirmwareUploader.cpp @@ -466,18 +466,17 @@ WifiFirmwareUploader::EspUploadResult WifiFirmwareUploader::Sync(uint16_t timeou // Send a command to the device to begin the Flash process. WifiFirmwareUploader::EspUploadResult WifiFirmwareUploader::flashBegin(uint32_t offset, uint32_t size) noexcept { - // determine the number of blocks represented by the size - const uint32_t blkCnt = (size + EspFlashBlockSize - 1) / EspFlashBlockSize; - - // ensure that the address is on a block boundary - size += offset & (EspFlashBlockSize - 1); - offset &= ~(EspFlashBlockSize - 1); + // Determine the number of blocks represented by the size + const uint32_t blkCnt = (size + (EspFlashBlockSize - 1)) / EspFlashBlockSize; + // Determine the erase size parameter to pass in the FLASH_BEGIN command +#if WIFI_USES_ESP32 + const uint32_t erase_size = size; +#else // Calculate the number of sectors to erase const uint32_t sector_size = 4 * 1024; uint32_t num_sectors = (size + (sector_size - 1))/sector_size; -#if !WIFI_USES_ESP32 const uint32_t start_sector = offset / sector_size; const uint32_t sectors_per_block = 16; @@ -498,9 +497,9 @@ WifiFirmwareUploader::EspUploadResult WifiFirmwareUploader::flashBegin(uint32_t { num_sectors = (num_sectors - head_sectors); } -#endif const uint32_t erase_size = num_sectors * sector_size; +#endif // begin the Flash process #if WIFI_USES_ESP32 @@ -604,22 +603,8 @@ WifiFirmwareUploader::EspUploadResult WifiFirmwareUploader::flashWriteBlock(uint WifiFirmwareUploader::EspUploadResult WifiFirmwareUploader::DoErase(uint32_t address, uint32_t size) noexcept { - const uint32_t sectorsPerBlock = 16; - const uint32_t sectorSize = 4096; - const uint32_t numSectors = (size + sectorSize - 1)/sectorSize; - const uint32_t startSector = address/sectorSize; - uint32_t headSectors = sectorsPerBlock - (startSector % sectorsPerBlock); - - if (numSectors < headSectors) - { - headSectors = numSectors; - } - const uint32_t eraseSize = (numSectors < 2 * headSectors) - ? (numSectors + 1) / 2 * sectorSize - : (numSectors - headSectors) * sectorSize; - - MessageF("Erasing %u bytes...\n", eraseSize); - return flashBegin(uploadAddress, eraseSize); + MessageF("Erasing %u bytes...\n", size); + return flashBegin(uploadAddress, size); } void WifiFirmwareUploader::Spin() noexcept -- cgit v1.2.3