Skip to content
5 changes: 5 additions & 0 deletions firmware/bootloader/openblt_chibios/openblt_net.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "pch.h"

#include "wifi_socket.h"
#include "wifi_sd_firmware_updater.h"
#include "thread_controller.h"
#include "socket/include/socket.h"

Expand Down Expand Up @@ -40,6 +41,10 @@ void NetDeferredInit() {

didInit = true;

// In the bootloader the SD-card WiFi firmware update never runs, so unblock
// WifiHelperThread (which waits on this semaphore before it calls initWifi()).
signalWifiSdUpdateComplete();

initWifi();

wifiInitFinisher.startThread();
Expand Down
8 changes: 7 additions & 1 deletion firmware/console/binary_log/sd_file_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,13 @@ static void sdTriggerLogger() {
#endif /* EFI_TOOTH_LOGGER */
}

static THD_WORKING_AREA(sdCardLoggerStack, 3 * UTILITY_THREAD_STACK_SIZE); // MMC monitor thread
#if EFI_WIFI
// WiFi needs extra stack, as WiFi firmware update happens from here
static THD_WORKING_AREA(sdCardLoggerStack, 6 * UTILITY_THREAD_STACK_SIZE);
#else // not EFI_WIFI
static THD_WORKING_AREA(sdCardLoggerStack, 4 * UTILITY_THREAD_STACK_SIZE);
#endif

static THD_FUNCTION(sdCardLoggerThread, arg) {
(void)arg;
chRegSetThreadName("MMC Card Logger");
Expand Down
7 changes: 4 additions & 3 deletions firmware/ext/atwinc1500/spi_flash/source/spi_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ sint8 spi_flash_erase(uint32 u32Offset, uint32 u32Sz)
uint32 t;
t = GetTickCount();
#endif
M2M_PRINT("\r\n>Start erasing...\r\n");
M2M_INFO(">Start erasing...");
for(i = u32Offset; i < (u32Sz +u32Offset); i += (16*FLASH_PAGE_SZ))
{
ret += spi_flash_write_enable();
Expand All @@ -696,14 +696,15 @@ sint8 spi_flash_erase(uint32 u32Offset, uint32 u32Sz)
ret += spi_flash_read_status_reg(&tmp);
do
{
nm_bsp_sleep(10);
if(ret != M2M_SUCCESS) goto ERR;
ret += spi_flash_read_status_reg(&tmp);
}while(tmp & 0x01);

}
M2M_PRINT("Done\r\n");
M2M_INFO("Done!");
#ifdef PROFILING
M2M_PRINT("#Erase time = %f sec\n", (GetTickCount()-t)/1000.0);
M2M_INFO("#Erase time = %f sec\n", (GetTickCount()-t)/1000.0);
#endif
ERR:
return ret;
Expand Down
3 changes: 3 additions & 0 deletions firmware/hw_layer/mmc_card.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "buffered_writer.h"

#include <array>

BaseBlockDevice* initializeMmcBlockDevice();
void stopMmcBlockDevice();

Expand Down Expand Up @@ -45,5 +47,6 @@ namespace sd_mem {
FATFS* getFs();
FIL* getLogFileFd();
SdLogBufferWriter& getLogBuffer();
std::array<uint8_t, 512>& getWifiUpdateBuffer();
} // namespace sd_mem
#endif // EFI_PROD_CODE
15 changes: 10 additions & 5 deletions firmware/hw_layer/mmc_card_attach.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ struct {
FATFS fs;
FIL file;
SdLogBufferWriter logBuffer;
std::array<uint8_t, 512> wifiUpdateBuffer;
} usedPart;

static_assert(sizeof(usedPart) <= 2048);
static_assert(sizeof(usedPart) <= 4096);

// Fill the struct out to a full MPU region
uint8_t padding[2048 - sizeof(usedPart)];
} mmcCardCacheControlledStorage SDMMC_MEMORY(2048);
uint8_t padding[4096 - sizeof(usedPart)];
} mmcCardCacheControlledStorage SDMMC_MEMORY(4096);

namespace sd_mem {
FATFS* getFs() {
Expand All @@ -56,6 +57,10 @@ FIL* getLogFileFd() {
SdLogBufferWriter& getLogBuffer() {
return mmcCardCacheControlledStorage.usedPart.logBuffer;
}

std::array<uint8_t, 512>& getWifiUpdateBuffer() {
return mmcCardCacheControlledStorage.usedPart.wifiUpdateBuffer;
}
} // namespace sd_mem

#endif // !EFI_BOOTLOADER
Expand Down Expand Up @@ -132,8 +137,8 @@ BaseBlockDevice* initializeMmcBlockDevice() {
#if defined(STM32H7XX) && !EFI_BOOTLOADER
{
void* base = &mmcCardCacheControlledStorage;
static_assert(sizeof(mmcCardCacheControlledStorage) == 2048);
uint32_t size = MPU_RASR_SIZE_2K;
static_assert(sizeof(mmcCardCacheControlledStorage) == 4096);
uint32_t size = MPU_RASR_SIZE_4K;

mpuConfigureRegion(
MPU_REGION_5,
Expand Down
43 changes: 39 additions & 4 deletions firmware/hw_layer/mmc_card_mount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

#include "ff.h"
#include "mass_storage_init.h"
#include "usbconsole.h"

#if EFI_WIFI
#include "wifi_sd_firmware_updater.h"
#endif

static bool fs_ready = false;

Expand All @@ -18,6 +23,9 @@ void onUsbConnectedNotifyMmcI() {
#endif /* HAL_USE_USB_MSD */

bool mountSdFilesystem() {
Timer t;
t.reset();

auto cardBlockDevice = initializeMmcBlockDevice();

#if EFI_TUNER_STUDIO
Expand All @@ -27,18 +35,45 @@ bool mountSdFilesystem() {

// if no card, don't try to mount FS
if (!cardBlockDevice) {
#if EFI_WIFI
signalWifiSdUpdateComplete();
#endif
#if HAL_USE_USB_MSD
// No SD card - allow USB to enumerate so serial and INI drive still work
allowUsbEnumeration();
#endif
return false;
}

// Mount filesystem temporarily for WiFi update check
bool mounted = f_mount(sd_mem::getFs(), "/", 1) == FR_OK;

if (mounted) {
#if EFI_WIFI
// Check for WiFi firmware update/dump trigger files on SD card
tryUpdateWifiFirmwareFromSd();
tryDumpWifiFirmwareToSd();
#endif
// Unmount — we'll either hand the card to USB or re-mount for logging
f_mount(nullptr, "/", 0);
} else {
efiPrintf("SD card failed to mount filesystem");
}

#if EFI_WIFI
signalWifiSdUpdateComplete();
#endif

#if HAL_USE_USB_MSD
// Wait for the USB stack to wake up, or a 5 second timeout, whichever occurs first
// Allow USB to enumerate - the host will see the real SD card
allowUsbEnumeration();

// Wait for the USB enumeration, or a 5 second timeout, whichever occurs first
msg_t usbResult = usbConnectedSemaphore.wait(TIME_MS2I(5000));

bool hasUsb = usbResult == MSG_OK;

// If we have a device AND USB is connected, mount the card to USB, otherwise
// mount the null device and try to mount the filesystem ourselves
if (cardBlockDevice && hasUsb) {
if (hasUsb) {
// Mount the real card to USB
attachMsdSdCard(cardBlockDevice);

Expand Down
18 changes: 18 additions & 0 deletions firmware/hw_layer/ports/stm32/serial_over_usb/usbconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

static bool isUsbSerialInitialized = false;

#if HAL_USE_USB_MSD
// When MSD is enabled, defer USB bus connection until the SD card thread
// has finished any WiFi firmware updates and is ready to present media.
static chibios_rt::BinarySemaphore usbEnumerationAllowed(/* taken =*/true);
#endif

/**
* start USB serial using hard-coded communications pins (see comments inside the code)
*/
Expand All @@ -42,11 +48,23 @@ void usb_serial_start() {
chThdSleepMilliseconds(250);
#endif /* EFI_SKIP_USB_DISCONNECT */
usbStart(serusbcfg.usbp, &usbcfg);

#if HAL_USE_USB_MSD
// Wait for the SD card thread to signal that it's ready
usbEnumerationAllowed.wait();
#endif

usbConnectBus(serusbcfg.usbp);

isUsbSerialInitialized = true;
}

void allowUsbEnumeration() {
#if HAL_USE_USB_MSD
usbEnumerationAllowed.signal();
#endif
}

bool is_usb_serial_ready() {
return isUsbSerialInitialized && SDU1.config->usbp->state == USB_ACTIVE;
}
Expand Down
14 changes: 5 additions & 9 deletions firmware/hw_layer/ports/stm32/serial_over_usb/usbconsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@

#pragma once

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
void usb_serial_start();
bool is_usb_serial_ready();

void usb_serial_start(void);
bool is_usb_serial_ready(void);

#ifdef __cplusplus
}
#endif /* __cplusplus */
#if HAL_USE_USB_MSD
void allowUsbEnumeration();
#endif
2 changes: 1 addition & 1 deletion firmware/net/net.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ALLCPPSRC += \
$(PROJECT_DIR)/net/wifi_socket.cpp \
# $(PROJECT_DIR)/net/wifi_firmware_updater.cpp \
$(PROJECT_DIR)/net/wifi_sd_firmware_updater.cpp

ALLINC += \
$(PROJECT_DIR)/net \
Loading
Loading