From e83677b4b0ae5e2bd6d760694a4e8e1c25c406d3 Mon Sep 17 00:00:00 2001 From: Rohan Sahay Date: Mon, 16 Oct 2023 19:17:32 +0530 Subject: [PATCH] [Silabs] Adds refactored spi multiplexing with feature toggle for NCP combinations (#29414) * Adds DISPLAY_ENABLED macro for SPI multiplexed functions * Adds CONFIG_USE_EXTERNAL_FLASH macro for SPI multiplex functions * Added proper MACROs for the features that require multiplexing * temp commit with test file * Adds fix for crash on rs91x * Adds updated build commands * adds DEBUG logs * Adds bugfixes to MACROs * Adds additional logs * Adds fix to UART multiplexing * Remove comments * Adds ported changes * Adds use of SL_CATALOG_UARTDRV_USART_PRESENT, SL_CATALOG_UARTDRV_EUSART_PRESENT * Removes code since WF200 requires SL_CATALOG_UARTDRV_USART_PRESENT only * Restyler changes * Adds additional changes to wrap multiplexing with MACROs * Remove log commit * Adds dummy buffer length check * Restyler changes * Removes use of unnecessary MACRO * adds thread check * Adds comment * updated build script * Adds refactored includes and define macros * Adds spell fix * Adds feature macro for header include * Remove temp code * Adds refactored deassert * Comment reformatting --------- Co-authored-by: Rohan S <3526930+brosahay@users.noreply.github.com> --- examples/platform/silabs/display/demo-ui.c | 14 +- examples/platform/silabs/display/lcd.cpp | 3 - .../silabs/efr32/rs911x/hal/efx_spi.c | 202 ++++++++++-------- .../platform/silabs/efr32/spi_multiplex.h | 66 +++--- examples/platform/silabs/efr32/uart.cpp | 15 +- .../platform/silabs/efr32/wf200/efr_spi.c | 116 ++++++---- .../silabs/efr32/OTAImageProcessorImpl.cpp | 39 ++-- 7 files changed, 257 insertions(+), 198 deletions(-) diff --git a/examples/platform/silabs/display/demo-ui.c b/examples/platform/silabs/display/demo-ui.c index e430e01ddb82dd..082f79eea0e10f 100644 --- a/examples/platform/silabs/display/demo-ui.c +++ b/examples/platform/silabs/display/demo-ui.c @@ -23,11 +23,11 @@ #include "dmd/dmd.h" #include "em_types.h" #include "glib.h" +#if SL_WIFI && !SIWX_917 +#include "spi_multiplex.h" +#endif // SL_WIFI && !SIWX_917 #include #include -#if (defined(EFR32MG24) && defined(SL_WIFI)) -#include "spi_multiplex.h" -#endif // Main Logo and App image #define SILICONLABS_X_POSITION ((glibContext.pDisplayGeometry->xSize - SILICONLABS_BITMAP_WIDTH) / 2) @@ -102,13 +102,13 @@ void demoUIInit(GLIB_Context_t * context) sl_status_t updateDisplay(void) { -#if (defined(EFR32MG24) && defined(SL_WIFI)) +#if SL_LCDCTRL_MUX sl_wfx_host_pre_lcd_spi_transfer(); -#endif +#endif // SL_LCDCTRL_MUX sl_status_t status = DMD_updateDisplay(); -#if (defined(EFR32MG24) && defined(SL_WIFI)) +#if SL_LCDCTRL_MUX sl_wfx_host_post_lcd_spi_transfer(); -#endif +#endif // SL_LCDCTRL_MUX if (status != DMD_OK) return SL_STATUS_FAIL; return SL_STATUS_OK; diff --git a/examples/platform/silabs/display/lcd.cpp b/examples/platform/silabs/display/lcd.cpp index 63a91795487963..adcb4aea019e40 100644 --- a/examples/platform/silabs/display/lcd.cpp +++ b/examples/platform/silabs/display/lcd.cpp @@ -35,9 +35,6 @@ #include "sl_board_control.h" -#if (defined(EFR32MG24) && defined(SL_WIFI)) -#include "spi_multiplex.h" -#endif #define LCD_SIZE 128 #define QR_CODE_VERSION 4 #define QR_CODE_MODULE_SIZE 3 diff --git a/examples/platform/silabs/efr32/rs911x/hal/efx_spi.c b/examples/platform/silabs/efr32/rs911x/hal/efx_spi.c index aff756aad8888a..1e30b8bdf2f9c6 100644 --- a/examples/platform/silabs/efr32/rs911x/hal/efx_spi.c +++ b/examples/platform/silabs/efr32/rs911x/hal/efx_spi.c @@ -22,6 +22,11 @@ #include #include +#include "FreeRTOS.h" +#include "event_groups.h" +#include "semphr.h" +#include "task.h" + #include "dmadrv.h" #include "em_chip.h" #include "em_cmu.h" @@ -29,28 +34,24 @@ #include "em_device.h" #include "em_gpio.h" #include "em_ldma.h" -#if defined(EFR32MG12) -#include "em_usart.h" -#elif defined(EFR32MG24) -#include "em_eusart.h" -#endif +#include "gpiointerrupt.h" #include "spidrv.h" -#include "silabs_utils.h" - -#include "gpiointerrupt.h" #include "sl_device_init_clocks.h" -#include "sl_memlcd.h" +#include "sl_device_init_dpll.h" +#include "sl_device_init_hfxo.h" +#include "sl_spidrv_instances.h" #include "sl_status.h" -#include "FreeRTOS.h" -#include "event_groups.h" -#include "task.h" - -#include "btl_interface.h" +#include "silabs_utils.h" +#include "spi_multiplex.h" #include "wfx_host_events.h" #include "wfx_rsi.h" +#if defined(SL_CATALOG_POWER_MANAGER_PRESENT) +#include "sl_power_manager.h" +#endif + #ifdef CHIP_9117 #include "cmsis_os2.h" #include "sl_board_configuration.h" @@ -60,28 +61,48 @@ #include "sl_wifi_callback_framework.h" #include "sl_wifi_constants.h" #include "sl_wifi_types.h" + +// macro to drive semaphore block minimum timer in milli seconds +// ported from rsi_hal.h (rs911x) +#define RSI_SEM_BLOCK_MIN_TIMER_VALUE_MS (50) #else #include "rsi_board_configuration.h" #include "rsi_driver.h" -#endif +#endif // CHIP_9117 -#include "sl_device_init_dpll.h" -#include "sl_device_init_hfxo.h" +#if SL_BTLCTRL_MUX +#include "btl_interface.h" +#endif // SL_BTLCTRL_MUX +#if SL_LCDCTRL_MUX +#include "sl_memlcd.h" +#endif // SL_LCDCTRL_MUX +#if SL_MX25CTRL_MUX +#include "sl_mx25_flash_shutdown_usart_config.h" +#endif // SL_MX25CTRL_MUX -#define DEFAULT_SPI_TRASFER_MODE 0 -// Macro to drive semaphore block minimun timer in milli seconds -#define RSI_SEM_BLOCK_MIN_TIMER_VALUE_MS (50) -#if defined(SL_CATALOG_POWER_MANAGER_PRESENT) -#include "sl_power_manager.h" -#endif +#if defined(EFR32MG12) +#include "em_usart.h" + +#define SL_SPIDRV_HANDLE sl_spidrv_exp_handle + +#elif defined(EFR32MG24) +#include "em_eusart.h" +#include "sl_spidrv_eusart_exp_config.h" + +#define SL_SPIDRV_HANDLE sl_spidrv_eusart_exp_handle +#define SL_SPIDRV_EXP_BITRATE_MULTIPLEXED SL_SPIDRV_EUSART_EXP_BITRATE +#define SL_SPIDRV_UART_CONSOLE_BITRATE SL_UARTDRV_EUSART_VCOM_BAUDRATE +#define SL_SPIDRV_FRAME_LENGTH SL_SPIDRV_EUSART_EXP_FRAME_LENGTH + +#endif // EFR32MG12 || EFR32MG24 #define CONCAT(A, B) (A##B) #define SPI_CLOCK(N) CONCAT(cmuClock_USART, N) -#if defined(EFR32MG24) +#if SL_SPICTRL_MUX StaticSemaphore_t spi_sem_peripheral; -SemaphoreHandle_t spi_sem_sync_hdl; -#endif /* EFR32MG24 */ +static SemaphoreHandle_t spi_sem_sync_hdl; +#endif // SL_SPICTRL_MUX StaticSemaphore_t xEfxSpiIntfSemaBuffer; static SemaphoreHandle_t spiTransferLock; @@ -89,16 +110,6 @@ static TaskHandle_t spiInitiatorTaskHandle = NULL; static uint32_t dummy_buffer; /* Used for DMA - when results don't matter */ -#if defined(EFR32MG12) -#include "sl_spidrv_exp_config.h" -extern SPIDRV_Handle_t sl_spidrv_exp_handle; -#define SL_SPIDRV_HANDLE sl_spidrv_exp_handle -#elif defined(EFR32MG24) -#include "spi_multiplex.h" -#else -#error "Unknown platform" -#endif - // variable to identify spi configured for expansion header // EUSART configuration available on the SPIDRV static bool spi_enabled = false; @@ -175,14 +186,14 @@ void rsi_hal_board_init(void) spiTransferLock = xSemaphoreCreateBinaryStatic(&xEfxSpiIntfSemaBuffer); xSemaphoreGive(spiTransferLock); -#if defined(EFR32MG24) +#if SL_SPICTRL_MUX if (spi_sem_sync_hdl == NULL) { spi_sem_sync_hdl = xSemaphoreCreateBinaryStatic(&spi_sem_peripheral); } configASSERT(spi_sem_sync_hdl); xSemaphoreGive(spi_sem_sync_hdl); -#endif /* EFR32MG24 */ +#endif /* SL_SPICTRL_MUX */ /* GPIO INIT of MG12 & MG24 : Reset, Wakeup, Interrupt */ sl_wfx_host_gpio_init(); @@ -203,8 +214,7 @@ void sl_si91x_host_enable_high_speed_bus() // dummy function for wifi-sdk } -#if defined(EFR32MG24) - +#if SL_SPICTRL_MUX void SPIDRV_SetBaudrate(uint32_t baudrate) { if (EUSART_BaudrateGet(MY_USART) == baudrate) @@ -222,9 +232,11 @@ sl_status_t sl_wfx_host_spi_cs_assert(void) if (!spi_enabled) // Reduce sl_spidrv_init_instances { sl_spidrv_init_instances(); +#if defined(EFR32MG24) + GPIO_PinOutClear(SL_SPIDRV_EUSART_EXP_CS_PORT, SL_SPIDRV_EUSART_EXP_CS_PIN); +#endif // EFR32MG24 spi_enabled = true; } - GPIO_PinOutClear(SL_SPIDRV_EUSART_EXP_CS_PORT, SL_SPIDRV_EUSART_EXP_CS_PIN); return SL_STATUS_OK; } @@ -235,16 +247,21 @@ sl_status_t sl_wfx_host_spi_cs_deassert(void) if (ECODE_EMDRV_SPIDRV_OK != SPIDRV_DeInit(SL_SPIDRV_HANDLE)) { xSemaphoreGive(spi_sem_sync_hdl); + SILABS_LOG("%s error.", __func__); return SL_STATUS_FAIL; } +#if defined(EFR32MG24) + GPIO_PinOutSet(SL_SPIDRV_EUSART_EXP_CS_PORT, SL_SPIDRV_EUSART_EXP_CS_PIN); + GPIO->EUSARTROUTE[SL_SPIDRV_EUSART_EXP_PERIPHERAL_NO].ROUTEEN = PINOUT_CLEAR; +#endif // EFR32MG24 spi_enabled = false; } - GPIO_PinOutSet(SL_SPIDRV_EUSART_EXP_CS_PORT, SL_SPIDRV_EUSART_EXP_CS_PIN); - GPIO->EUSARTROUTE[SL_SPIDRV_EUSART_EXP_PERIPHERAL_NO].ROUTEEN = PINOUT_CLEAR; xSemaphoreGive(spi_sem_sync_hdl); return SL_STATUS_OK; } +#endif // SL_SPICTRL_MUX +#if SL_MX25CTRL_MUX sl_status_t sl_wfx_host_spiflash_cs_assert(void) { GPIO_PinOutClear(SL_MX25_FLASH_SHUTDOWN_CS_PORT, SL_MX25_FLASH_SHUTDOWN_CS_PIN); @@ -256,28 +273,31 @@ sl_status_t sl_wfx_host_spiflash_cs_deassert(void) GPIO_PinOutSet(SL_MX25_FLASH_SHUTDOWN_CS_PORT, SL_MX25_FLASH_SHUTDOWN_CS_PIN); return SL_STATUS_OK; } +#endif // SL_MX25CTRL_MUX +#if SL_BTLCTRL_MUX sl_status_t sl_wfx_host_pre_bootloader_spi_transfer(void) { - xSemaphoreTake(spi_sem_sync_hdl, portMAX_DELAY); - if (spi_enabled) +#if SL_SPICTRL_MUX + if (sl_wfx_host_spi_cs_deassert() != SL_STATUS_OK) { - if (ECODE_EMDRV_SPIDRV_OK != SPIDRV_DeInit(SL_SPIDRV_HANDLE)) - { - xSemaphoreGive(spi_sem_sync_hdl); - return SL_STATUS_FAIL; - } - spi_enabled = false; + return SL_STATUS_FAIL; } + xSemaphoreTake(spi_sem_sync_hdl, portMAX_DELAY); +#endif // SL_SPICTRL_MUX // bootloader_init takes care of SPIDRV_Init() int32_t status = bootloader_init(); if (status != BOOTLOADER_OK) { SILABS_LOG("bootloader_init error: %x", status); +#if SL_SPICTRL_MUX xSemaphoreGive(spi_sem_sync_hdl); +#endif // SL_SPICTRL_MUX return SL_STATUS_FAIL; } +#if SL_MX25CTRL_MUX sl_wfx_host_spiflash_cs_assert(); +#endif // SL_MX25CTRL_MUX return SL_STATUS_OK; } @@ -288,31 +308,39 @@ sl_status_t sl_wfx_host_post_bootloader_spi_transfer(void) if (status != BOOTLOADER_OK) { SILABS_LOG("bootloader_deinit error: %x", status); +#if SL_SPICTRL_MUX xSemaphoreGive(spi_sem_sync_hdl); +#endif // SL_SPICTRL_MUX return SL_STATUS_FAIL; } GPIO->USARTROUTE[SL_MX25_FLASH_SHUTDOWN_PERIPHERAL_NO].ROUTEEN = PINOUT_CLEAR; +#if SL_MX25CTRL_MUX sl_wfx_host_spiflash_cs_deassert(); +#endif // SL_MX25CTRL_MUX +#if SL_SPICTRL_MUX xSemaphoreGive(spi_sem_sync_hdl); +#endif // SL_SPICTRL_MUX return SL_STATUS_OK; } +#endif // SL_BTLCTRL_MUX +#if SL_LCDCTRL_MUX sl_status_t sl_wfx_host_pre_lcd_spi_transfer(void) { - xSemaphoreTake(spi_sem_sync_hdl, portMAX_DELAY); - if (spi_enabled) +#if SL_SPICTRL_MUX + if (sl_wfx_host_spi_cs_deassert() != SL_STATUS_OK) { - if (ECODE_EMDRV_SPIDRV_OK != SPIDRV_DeInit(SL_SPIDRV_HANDLE)) - { - xSemaphoreGive(spi_sem_sync_hdl); - return SL_STATUS_FAIL; - } - spi_enabled = false; + return SL_STATUS_FAIL; } + xSemaphoreTake(spi_sem_sync_hdl, portMAX_DELAY); +#endif // SL_SPICTRL_MUX // sl_memlcd_refresh takes care of SPIDRV_Init() if (SL_STATUS_OK != sl_memlcd_refresh(sl_memlcd_get())) { +#if SL_SPICTRL_MUX xSemaphoreGive(spi_sem_sync_hdl); +#endif // SL_SPICTRL_MUX + SILABS_LOG("%s error.", __func__); return SL_STATUS_FAIL; } return SL_STATUS_OK; @@ -323,11 +351,12 @@ sl_status_t sl_wfx_host_post_lcd_spi_transfer(void) USART_Enable(SL_MEMLCD_SPI_PERIPHERAL, usartDisable); CMU_ClockEnable(SPI_CLOCK(SL_MEMLCD_SPI_PERIPHERAL_NO), false); GPIO->USARTROUTE[SL_MEMLCD_SPI_PERIPHERAL_NO].ROUTEEN = PINOUT_CLEAR; +#if SL_SPICTRL_MUX xSemaphoreGive(spi_sem_sync_hdl); +#endif // SL_SPICTRL_MUX return SL_STATUS_OK; } - -#endif /* EFR32MG24 */ +#endif // SL_LCDCTRL_MUX /***************************************************************************** *@brief @@ -351,26 +380,30 @@ static void spi_dmaTransfertComplete(SPIDRV_HandleData_t * pxHandle, Ecode_t tra } /********************************************************************* - * @fn int16_t rsi_spi_transfer(uint8_t *tx_buf, uint8_t *rx_buf, uint16_t xlen, uint8_t mode) - * @brief - * Do a SPI transfer - Mode is 8/16 bit - But every 8 bit is aligned - * @param[in] tx_buf: - * @param[in] rx_buf: - * @param[in] xlen: - * @param[in] mode: - * @return - * None + * @fn int16_t rsi_spi_transfer(uint8_t *ptrBuf, uint16_t bufLen, uint8_t *valBuf, uint8_t mode) + * @param[in] uint8_t *tx_buff, pointer to the buffer with the data to be transfered + * @param[in] uint8_t *rx_buff, pointer to the buffer to store the data received + * @param[in] uint16_t transfer_length, Number of bytes to send and receive + * @param[in] uint8_t mode, To indicate mode 8 BIT/32 BIT mode transfers. + * @param[out] None + * @return 0, 0=success + * @section description + * This API is used to transfer/receive data to the Wi-Fi module through the SPI interface. **************************************************************************/ int16_t rsi_spi_transfer(uint8_t * tx_buf, uint8_t * rx_buf, uint16_t xlen, uint8_t mode) { -#if defined(EFR32MG24) +#if SL_SPICTRL_MUX sl_wfx_host_spi_cs_assert(); -#endif /* EFR32MG24 */ +#endif // SL_SPICTRL_MUX /* TODO: tx_buf and rx_buf needs to be replaced with a dummy buffer of length xlen to align with SDK of WiFi */ if (xlen <= MIN_XLEN || (tx_buf == NULL && rx_buf == NULL)) { + if (xlen > sizeof(dummy_buffer)) + { + return RSI_ERROR_INVALID_PARAM; // Ensuring that the dummy buffer won't corrupt the memory + } rx_buf = (uint8_t *) &dummy_buffer; tx_buf = (uint8_t *) &dummy_buffer; } @@ -423,24 +456,25 @@ int16_t rsi_spi_transfer(uint8_t * tx_buf, uint8_t * rx_buf, uint16_t xlen, uint } xSemaphoreGive(spiTransferLock); -#if defined(EFR32MG24) +#if SL_SPICTRL_MUX sl_wfx_host_spi_cs_deassert(); -#endif /* EFR32MG24 */ +#endif // SL_SPICTRL_MUX return rsiError; } +#ifdef CHIP_9117 /********************************************************************* - * @fn int16_t rsi_spi_transfer(uint8_t *tx_buf, uint8_t *rx_buf, uint16_t xlen, uint8_t mode) - * @brief - * Do a SPI transfer - Mode is 8/16 bit - But every 8 bit is aligned - * @param[in] tx_buf: - * @param[in] rx_buf: - * @param[in] xlen: - * @param[in] mode: - * @return - * None + * @fn int16_t sl_si91x_host_spi_transfer(uint8_t *tx_buf, uint8_t *rx_buf, uint16_t xlen) + * @param[in] uint8_t *tx_buff, pointer to the buffer with the data to be transferred + * @param[in] uint8_t *rx_buff, pointer to the buffer to store the data received + * @param[in] uint16_t transfer_length, Number of bytes to send and receive + * @param[out] None + * @return 0, 0=success + * @section description + * This API is used to transfer/receive data to the Wi-Fi module through the SPI interface. **************************************************************************/ sl_status_t sl_si91x_host_spi_transfer(const void * tx_buf, void * rx_buf, uint16_t xlen) { - return (rsi_spi_transfer((uint8_t *) tx_buf, rx_buf, xlen, DEFAULT_SPI_TRASFER_MODE)); + return (rsi_spi_transfer((uint8_t *) tx_buf, rx_buf, xlen, RSI_MODE_8BIT)); } +#endif // CHIP_9117 diff --git a/examples/platform/silabs/efr32/spi_multiplex.h b/examples/platform/silabs/efr32/spi_multiplex.h index 0c926b809fab1a..82eaf3991bdf4e 100644 --- a/examples/platform/silabs/efr32/spi_multiplex.h +++ b/examples/platform/silabs/efr32/spi_multiplex.h @@ -24,41 +24,32 @@ #pragma once -#if defined(EFR32MG24) +#ifndef SL_LCDCTRL_MUX +#define SL_LCDCTRL_MUX (EFR32MG24 && SL_WIFI && DISPLAY_ENABLED) +#endif // SL_LCDCTRL_MUX + +#ifndef SL_UARTCTRL_MUX +#define SL_UARTCTRL_MUX (EFR32MG24 && WF200_WIFI && ENABLE_CHIP_SHELL) +#endif // SL_UARTCTRL_MUX + +#ifndef SL_MX25CTRL_MUX +#define SL_MX25CTRL_MUX (EFR32MG24 && SL_WIFI && CONFIG_USE_EXTERNAL_FLASH) +#endif // SL_MX25CTRL_MUX + +#ifndef SL_BTLCTRL_MUX +#define SL_BTLCTRL_MUX (EFR32MG24 && SL_WIFI && CONFIG_USE_EXTERNAL_FLASH) +#endif // SL_BTLCTRL_MUX + +#ifndef SL_SPICTRL_MUX +#define SL_SPICTRL_MUX (EFR32MG24 && SL_WIFI && (SL_LCDCTRL_MUX || SL_UARTCTRL_MUX || SL_MX25CTRL_MUX || SL_BTLCTRL_MUX)) +#endif // SL_SPICTRL_MUX + +#if SL_SPICTRL_MUX #ifdef __cplusplus extern "C" { #endif -#include "FreeRTOS.h" -#include "em_eusart.h" -#include "semphr.h" -#include "sl_memlcd_display.h" -#include "sl_mx25_flash_shutdown_usart_config.h" -#include "sl_spidrv_instances.h" -#include "spidrv.h" - -#define SL_SPIDRV_LCD_BITRATE SL_MEMLCD_SCLK_FREQ -#define SL_SPIDRV_MX25_FLASH_BITRATE 16000000 - -#ifdef RS911X_WIFI -#include "sl_spidrv_eusart_exp_config.h" - -#define SL_SPIDRV_EXP_BITRATE_MULTIPLEXED SL_SPIDRV_EUSART_EXP_BITRATE -#define SL_SPIDRV_UART_CONSOLE_BITRATE SL_UARTDRV_EUSART_VCOM_BAUDRATE -#define SL_SPIDRV_FRAME_LENGTH SL_SPIDRV_EUSART_EXP_FRAME_LENGTH -#define SL_SPIDRV_HANDLE sl_spidrv_eusart_exp_handle - -#elif WF200_WIFI -#include "sl_spidrv_exp_config.h" -#include "sl_wfx_host_api.h" - -// TODO: (MATTER-1906) Investigate why using SL_SPIDRV_EXP_BITRATE is causing WF200 init failure -// REF: sl_spidrv_exp_config.h -#define SL_SPIDRV_EXP_BITRATE_MULTIPLEXED 10000000 -#define SL_SPIDRV_UART_CONSOLE_BITRATE SL_UARTDRV_USART_VCOM_BAUDRATE -#define SL_SPIDRV_FRAME_LENGTH SL_SPIDRV_EXP_FRAME_LENGTH -#define SL_SPIDRV_HANDLE sl_spidrv_exp_handle -#endif /* RS911X_WIFI || WF200_WIFI */ +#if SL_SPICTRL_MUX /**************************************************************************** * @fn void SPIDRV_SetBaudrate() * @brief @@ -87,7 +78,9 @@ sl_status_t sl_wfx_host_spi_cs_assert(void); *****************************************************************************/ sl_status_t sl_wfx_host_spi_cs_deassert(void); #endif /* RS911X_WIFI */ +#endif // SL_SPICTRL_MUX +#if SL_MUX25CTRL_MUX /**************************************************************************** * @fn sl_status_t sl_wfx_host_spiflash_cs_assert() * @brief @@ -105,7 +98,9 @@ sl_status_t sl_wfx_host_spiflash_cs_assert(void); * @return returns SL_STATUS_OK *****************************************************************************/ sl_status_t sl_wfx_host_spiflash_cs_deassert(void); +#endif // SL_MUX25CTRL_MUX +#if SL_BTLCTRL_MUX /**************************************************************************** * @fn sl_status_t sl_wfx_host_pre_bootloader_spi_transfer() * @brief @@ -123,7 +118,9 @@ sl_status_t sl_wfx_host_pre_bootloader_spi_transfer(void); * @return SL_STATUS_OK *****************************************************************************/ sl_status_t sl_wfx_host_post_bootloader_spi_transfer(void); +#endif // SL_BTLCTRL_MUX +#if SL_LCDCTRL_MUX /**************************************************************************** * @fn sl_status_t sl_wfx_host_pre_lcd_spi_transfer() * @brief @@ -141,8 +138,9 @@ sl_status_t sl_wfx_host_pre_lcd_spi_transfer(void); * @return SL_STATUS_OK *****************************************************************************/ sl_status_t sl_wfx_host_post_lcd_spi_transfer(void); +#endif // SL_LCDCTRL_MUX -#if defined(WF200_WIFI) +#if SL_UARTCTRL_MUX /**************************************************************************** * @fn sl_status_t sl_wfx_host_pre_uart_transfer() * @brief @@ -160,9 +158,9 @@ sl_status_t sl_wfx_host_pre_uart_transfer(void); * @return SL_STATUS_OK *****************************************************************************/ sl_status_t sl_wfx_host_post_uart_transfer(void); -#endif /* WF200_WIFI */ +#endif // SL_UARTCTRL_MUX #ifdef __cplusplus } #endif -#endif /* EFR32MG24 */ +#endif // SL_SPICTRL_MUX diff --git a/examples/platform/silabs/efr32/uart.cpp b/examples/platform/silabs/efr32/uart.cpp index 1576e67f4fc907..3941114218a664 100644 --- a/examples/platform/silabs/efr32/uart.cpp +++ b/examples/platform/silabs/efr32/uart.cpp @@ -32,11 +32,11 @@ extern "C" { #include "sl_board_control.h" #endif #include "sl_uartdrv_instances.h" +#if SL_WIFI +#include "spi_multiplex.h" +#endif // SL_WIFI #ifdef SL_CATALOG_UARTDRV_EUSART_PRESENT #include "sl_uartdrv_eusart_vcom_config.h" -#if (defined(EFR32MG24) && defined(WF200_WIFI)) -#include "spi_multiplex.h" -#endif /* EFR32MG24 && WF200_WIFI */ #endif #ifdef SL_CATALOG_UARTDRV_USART_PRESENT #include "sl_uartdrv_usart_vcom_config.h" @@ -474,18 +474,21 @@ void uartSendBytes(uint8_t * buffer, uint16_t nbOfBytes) #if defined(SL_CATALOG_POWER_MANAGER_PRESENT) sl_power_manager_add_em_requirement(SL_POWER_MANAGER_EM1); #endif - +#if SL_UARTCTRL_MUX + sl_wfx_host_pre_uart_transfer(); +#endif // SL_UARTCTRL_MUX #if (defined(EFR32MG24) && defined(WF200_WIFI)) // Blocking transmit for the MG24 + WF200 since UART TX is multiplexed with // WF200 SPI IRQ - sl_wfx_host_pre_uart_transfer(); UARTDRV_ForceTransmit(vcom_handle, (uint8_t *) buffer, nbOfBytes); - sl_wfx_host_post_uart_transfer(); #else // Non Blocking Transmit UARTDRV_Transmit(vcom_handle, (uint8_t *) buffer, nbOfBytes, UART_tx_callback); ulTaskNotifyTake(pdTRUE, portMAX_DELAY); #endif /* EFR32MG24 && WF200_WIFI */ +#if SL_UARTCTRL_MUX + sl_wfx_host_post_uart_transfer(); +#endif // SL_UARTCTRL_MUX #if defined(SL_CATALOG_POWER_MANAGER_PRESENT) sl_power_manager_remove_em_requirement(SL_POWER_MANAGER_EM1); diff --git a/examples/platform/silabs/efr32/wf200/efr_spi.c b/examples/platform/silabs/efr32/wf200/efr_spi.c index ab5a4e64ab73c1..4cb83427ca3a49 100644 --- a/examples/platform/silabs/efr32/wf200/efr_spi.c +++ b/examples/platform/silabs/efr32/wf200/efr_spi.c @@ -14,21 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -#include "sl_wfx_configuration_defaults.h" - -#include "sl_wfx.h" -#include "sl_wfx_board.h" -#include "sl_wfx_host_api.h" - -#include "dmadrv.h" -#include "em_bus.h" -#include "em_cmu.h" -#include "em_gpio.h" -#include "em_ldma.h" -#include "em_usart.h" -#include "spidrv.h" - #include #include #include @@ -37,34 +22,52 @@ #include "FreeRTOS.h" #include "semphr.h" +#include "dmadrv.h" +#include "em_bus.h" +#include "em_cmu.h" +#include "em_gpio.h" +#include "em_ldma.h" +#include "em_usart.h" #include "gpiointerrupt.h" - +#include "sl_spidrv_exp_config.h" +#include "sl_spidrv_instances.h" +#include "sl_wfx.h" #include "sl_wfx_board.h" +#include "sl_wfx_configuration_defaults.h" #include "sl_wfx_host.h" +#include "sl_wfx_host_api.h" #include "sl_wfx_task.h" +#include "spidrv.h" + +#include "spi_multiplex.h" #include "wfx_host_events.h" #if defined(SL_CATALOG_POWER_MANAGER_PRESENT) #include "sl_power_manager.h" #endif -#if defined(EFR32MG12) -#include "sl_spidrv_exp_config.h" -extern SPIDRV_Handle_t sl_spidrv_exp_handle; +#if SL_LCDCTRL_MUX +#include "sl_memlcd_display.h" +#define SL_SPIDRV_LCD_BITRATE SL_MEMLCD_SCLK_FREQ +#endif // SL_LCDCTRL_MUX +#if SL_MX25CTRL_MUX +#include "sl_mx25_flash_shutdown_usart_config.h" +#define SL_SPIDRV_MX25_FLASH_BITRATE 16000000 +#endif // SL_MX25CTRL_MUX + +// TODO: (MATTER-1906) Investigate why using SL_SPIDRV_EXP_BITRATE is causing WF200 init failure +// REF: sl_spidrv_exp_config.h +#define SL_SPIDRV_EXP_BITRATE_MULTIPLEXED 10000000 +#define SL_SPIDRV_UART_CONSOLE_BITRATE SL_UARTDRV_USART_VCOM_BAUDRATE +#define SL_SPIDRV_FRAME_LENGTH SL_SPIDRV_EXP_FRAME_LENGTH #define SL_SPIDRV_HANDLE sl_spidrv_exp_handle -#elif defined(EFR32MG24) -#include "sl_spidrv_eusart_exp_config.h" -#include "spi_multiplex.h" -#else -#error "Unknown platform" -#endif #define USART SL_WFX_HOST_PINOUT_SPI_PERIPHERAL -#if defined(EFR32MG24) +#if SL_SPICTRL_MUX StaticSemaphore_t spi_sem_peripheral; -SemaphoreHandle_t spi_sem_sync_hdl; -#endif /* EFR32MG24 */ +static SemaphoreHandle_t spi_sem_sync_hdl; +#endif // SL_SPICTRL_MUX StaticSemaphore_t xEfrSpiSemaBuffer; static SemaphoreHandle_t spi_sem; @@ -113,23 +116,23 @@ sl_status_t sl_wfx_host_init_bus(void) spi_sem = xSemaphoreCreateBinaryStatic(&xEfrSpiSemaBuffer); xSemaphoreGive(spi_sem); -#if defined(EFR32MG24) +#if SL_SPICTRL_MUX if (spi_sem_sync_hdl == NULL) { spi_sem_sync_hdl = xSemaphoreCreateBinaryStatic(&spi_sem_peripheral); } configASSERT(spi_sem_sync_hdl); xSemaphoreGive(spi_sem_sync_hdl); -#endif /* EFR32MG24 */ +#endif /* SL_SPICTRL_MUX */ return SL_STATUS_OK; } sl_status_t sl_wfx_host_deinit_bus(void) { vSemaphoreDelete(spi_sem); -#if defined(EFR32MG24) +#if SL_SPICTRL_MUX vSemaphoreDelete(spi_sem_sync_hdl); -#endif /* EFR32MG24 */ +#endif /* SL_SPICTRL_MUX */ // Stop DMAs. DMADRV_StopTransfer(rx_dma_channel); DMADRV_StopTransfer(tx_dma_channel); @@ -142,10 +145,10 @@ sl_status_t sl_wfx_host_deinit_bus(void) sl_status_t sl_wfx_host_spi_cs_assert() { -#if defined(EFR32MG24) +#if SL_SPICTRL_MUX xSemaphoreTake(spi_sem_sync_hdl, portMAX_DELAY); SPIDRV_SetBaudrate(SL_SPIDRV_EXP_BITRATE_MULTIPLEXED); -#endif /* EFR32MG24 */ +#endif /* SL_SPICTRL_MUX */ GPIO_PinOutClear(SL_SPIDRV_EXP_CS_PORT, SL_SPIDRV_EXP_CS_PIN); return SL_STATUS_OK; } @@ -153,9 +156,9 @@ sl_status_t sl_wfx_host_spi_cs_assert() sl_status_t sl_wfx_host_spi_cs_deassert() { GPIO_PinOutSet(SL_SPIDRV_EXP_CS_PORT, SL_SPIDRV_EXP_CS_PIN); -#if defined(EFR32MG24) +#if SL_SPICTRL_MUX xSemaphoreGive(spi_sem_sync_hdl); -#endif /* EFR32MG24 */ +#endif /* SL_SPICTRL_MUX */ return SL_STATUS_OK; } @@ -370,9 +373,7 @@ void sl_wfx_host_gpio_init(void) NVIC_SetPriority(GPIO_EVEN_IRQn, 5); NVIC_SetPriority(GPIO_ODD_IRQn, 5); } - -#if defined(EFR32MG24) - +#if SL_SPICTRL_MUX void SPIDRV_SetBaudrate(uint32_t baudrate) { if (USART_BaudrateGet(MY_USART) == baudrate) @@ -391,7 +392,8 @@ void SPIDRV_SetBaudrate(uint32_t baudrate) USART_InitSync(MY_USART, &usartInit); } - +#endif // SL_SPICTRL_MUX +#if SL_MX25CTRL_MUX sl_status_t sl_wfx_host_spiflash_cs_assert(void) { GPIO_PinOutClear(SL_MX25_FLASH_SHUTDOWN_CS_PORT, SL_MX25_FLASH_SHUTDOWN_CS_PIN); @@ -403,15 +405,21 @@ sl_status_t sl_wfx_host_spiflash_cs_deassert(void) GPIO_PinOutSet(SL_MX25_FLASH_SHUTDOWN_CS_PORT, SL_MX25_FLASH_SHUTDOWN_CS_PIN); return SL_STATUS_OK; } +#endif // SL_MX25CTRL_MUX +#if SL_BTLCTRL_MUX sl_status_t sl_wfx_host_pre_bootloader_spi_transfer(void) { +#if SL_SPICTRL_MUX xSemaphoreTake(spi_sem_sync_hdl, portMAX_DELAY); +#endif // SL_SPICTRL_MUX /* * Assert CS pin for EXT SPI Flash */ SPIDRV_SetBaudrate(SL_SPIDRV_MX25_FLASH_BITRATE); +#if SL_MX25CTRL_MUX sl_wfx_host_spiflash_cs_assert(); +#endif // SL_MX25CTRL_MUX return SL_STATUS_OK; } @@ -420,14 +428,22 @@ sl_status_t sl_wfx_host_post_bootloader_spi_transfer(void) /* * De-Assert CS pin for EXT SPI Flash */ +#if SL_MX25CTRL_MUX sl_wfx_host_spiflash_cs_deassert(); +#endif // SL_MX25CTRL_MUX +#if SL_SPICTRL_MUX xSemaphoreGive(spi_sem_sync_hdl); +#endif // SL_SPICTRL_MUX return SL_STATUS_OK; } +#endif // SL_BTLCTRL_MUX +#if SL_LCDCTRL_MUX sl_status_t sl_wfx_host_pre_lcd_spi_transfer(void) { +#if SL_SPICTRL_MUX xSemaphoreTake(spi_sem_sync_hdl, portMAX_DELAY); +#endif // SL_SPICTRL_MUX SPIDRV_SetBaudrate(SL_SPIDRV_LCD_BITRATE); /*LCD CS is handled as part of LCD gsdk*/ return SL_STATUS_OK; @@ -435,36 +451,50 @@ sl_status_t sl_wfx_host_pre_lcd_spi_transfer(void) sl_status_t sl_wfx_host_post_lcd_spi_transfer(void) { +#if SL_SPICTRL_MUX xSemaphoreGive(spi_sem_sync_hdl); +#endif // SL_SPICTRL_MUX return SL_STATUS_OK; } +#endif // SL_LCDCTRL_MUX +#if SL_UARTCTRL_MUX sl_status_t sl_wfx_host_pre_uart_transfer(void) { +#if SL_SPICTRL_MUX if (spi_sem_sync_hdl == NULL) { - // UART is initialized before host SPI interface - // spi_sem_sync_hdl will not be initalized during execution +#endif // SL_SPICTRL_MUX + // UART is initialized before host SPI interface + // spi_sem_sync_hdl will not be initalized during execution GPIO_PinModeSet(gpioPortA, 8, gpioModePushPull, 1); +#if SL_SPICTRL_MUX return SL_STATUS_OK; } +#endif // SL_SPICTRL_MUX sl_wfx_disable_irq(); sl_wfx_host_disable_platform_interrupt(); +#if SL_SPICTRL_MUX xSemaphoreTake(spi_sem_sync_hdl, portMAX_DELAY); +#endif // SL_SPICTRL_MUX GPIO_PinModeSet(gpioPortA, 8, gpioModePushPull, 1); return SL_STATUS_OK; } sl_status_t sl_wfx_host_post_uart_transfer(void) { +#if SL_SPICTRL_MUX if (spi_sem_sync_hdl == NULL) { return SL_STATUS_OK; } +#endif // SL_SPICTRL_MUX GPIO_PinModeSet(gpioPortA, 8, gpioModeInputPull, 1); +#if SL_SPICTRL_MUX xSemaphoreGive(spi_sem_sync_hdl); +#endif // SL_SPICTRL_MUX sl_wfx_host_enable_platform_interrupt(); sl_wfx_enable_irq(); return SL_STATUS_OK; } -#endif /* EFR32MG24 */ +#endif // SL_UARTCTRL_MUX diff --git a/src/platform/silabs/efr32/OTAImageProcessorImpl.cpp b/src/platform/silabs/efr32/OTAImageProcessorImpl.cpp index 85118f9559ac1c..a2544f879a8ff5 100644 --- a/src/platform/silabs/efr32/OTAImageProcessorImpl.cpp +++ b/src/platform/silabs/efr32/OTAImageProcessorImpl.cpp @@ -23,12 +23,9 @@ extern "C" { #include "btl_interface.h" #include "em_bus.h" // For CORE_CRITICAL_SECTION -#if (defined(EFR32MG24) && defined(SL_WIFI)) +#if SL_WIFI #include "spi_multiplex.h" -#ifdef WF200_WIFI // TODO: (MATTER-1905) clean up of MACROs -#include "sl_wfx_host_api.h" -#endif -#endif +#endif // SL_WIFI } #include @@ -170,23 +167,23 @@ void OTAImageProcessorImpl::HandleFinalize(intptr_t context) writeBuffer[writeBufOffset] = 0; writeBufOffset++; } -#if (defined(EFR32MG24) && defined(SL_WIFI)) +#if SL_BTLCTRL_MUX err = sl_wfx_host_pre_bootloader_spi_transfer(); if (err != SL_STATUS_OK) { ChipLogError(SoftwareUpdate, "sl_wfx_host_pre_bootloader_spi_transfer() error: %ld", err); return; } -#endif +#endif // SL_BTLCTRL_MUX CORE_CRITICAL_SECTION(err = bootloader_eraseWriteStorage(mSlotId, mWriteOffset, writeBuffer, kAlignmentBytes);) -#if (defined(EFR32MG24) && defined(SL_WIFI)) +#if SL_BTLCTRL_MUX err = sl_wfx_host_post_bootloader_spi_transfer(); if (err != SL_STATUS_OK) { ChipLogError(SoftwareUpdate, "sl_wfx_host_post_bootloader_spi_transfer() error: %ld", err); return; } -#endif +#endif // SL_BTLCTRL_MUX if (err) { ChipLogError(SoftwareUpdate, "bootloader_eraseWriteStorage() error: %ld", err); @@ -208,28 +205,28 @@ void OTAImageProcessorImpl::HandleApply(intptr_t context) // Force KVS to store pending keys such as data from StoreCurrentUpdateInfo() chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().ForceKeyMapSave(); -#if (defined(EFR32MG24) && defined(SL_WIFI)) +#if SL_BTLCTRL_MUX err = sl_wfx_host_pre_bootloader_spi_transfer(); if (err != SL_STATUS_OK) { ChipLogError(SoftwareUpdate, "sl_wfx_host_pre_bootloader_spi_transfer() error: %ld", err); return; } -#endif +#endif // SL_BTLCTRL_MUX CORE_CRITICAL_SECTION(err = bootloader_verifyImage(mSlotId, NULL);) if (err != SL_BOOTLOADER_OK) { ChipLogError(SoftwareUpdate, "bootloader_verifyImage() error: %ld", err); // Call the OTARequestor API to reset the state GetRequestorInstance()->CancelImageUpdate(); -#if (defined(EFR32MG24) && defined(SL_WIFI)) +#if SL_BTLCTRL_MUX err = sl_wfx_host_post_bootloader_spi_transfer(); if (err != SL_STATUS_OK) { ChipLogError(SoftwareUpdate, "sl_wfx_host_post_bootloader_spi_transfer() error: %ld", err); return; } -#endif +#endif // SL_BTLCTRL_MUX return; } @@ -239,25 +236,25 @@ void OTAImageProcessorImpl::HandleApply(intptr_t context) ChipLogError(SoftwareUpdate, "bootloader_setImageToBootload() error: %ld", err); // Call the OTARequestor API to reset the state GetRequestorInstance()->CancelImageUpdate(); -#if (defined(EFR32MG24) && defined(SL_WIFI)) +#if SL_BTLCTRL_MUX err = sl_wfx_host_post_bootloader_spi_transfer(); if (err != SL_STATUS_OK) { ChipLogError(SoftwareUpdate, "sl_wfx_host_post_bootloader_spi_transfer() error: %ld", err); return; } -#endif +#endif // SL_BTLCTRL_MUX return; } -#if (defined(EFR32MG24) && defined(SL_WIFI)) +#if SL_BTLCTRL_MUX err = sl_wfx_host_post_bootloader_spi_transfer(); if (err != SL_STATUS_OK) { ChipLogError(SoftwareUpdate, "sl_wfx_host_post_bootloader_spi_transfer() error: %ld", err); return; } -#endif +#endif // SL_BTLCTRL_MUX // This reboots the device CORE_CRITICAL_SECTION(bootloader_rebootAndInstall();) } @@ -310,23 +307,23 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context) if (writeBufOffset == kAlignmentBytes) { writeBufOffset = 0; -#if (defined(EFR32MG24) && defined(SL_WIFI)) +#if SL_BTLCTRL_MUX err = sl_wfx_host_pre_bootloader_spi_transfer(); if (err != SL_STATUS_OK) { ChipLogError(SoftwareUpdate, "sl_wfx_host_pre_bootloader_spi_transfer() error: %ld", err); return; } -#endif +#endif // SL_BTLCTRL_MUX CORE_CRITICAL_SECTION(err = bootloader_eraseWriteStorage(mSlotId, mWriteOffset, writeBuffer, kAlignmentBytes);) -#if (defined(EFR32MG24) && defined(SL_WIFI)) +#if SL_BTLCTRL_MUX err = sl_wfx_host_post_bootloader_spi_transfer(); if (err != SL_STATUS_OK) { ChipLogError(SoftwareUpdate, "sl_wfx_host_post_bootloader_spi_transfer() error: %ld", err); return; } -#endif +#endif // SL_BTLCTRL_MUX if (err) { ChipLogError(SoftwareUpdate, "bootloader_eraseWriteStorage() error: %ld", err);