DSTATUS disk_initialize ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) { switch (pdrv) { case SPI : sFLASH_Init(); DBG_MSG("sFLASH_ReadID()=0x%x", sFLASH_ReadID()); return 0; default: return STA_NODISK; } return STA_NOINIT; }
/** * @brief sFLASH_If_Init * Memory initialization routine. * @param None * @retval MAL_OK if operation is successeful, MAL_FAIL else. */ uint16_t sFLASH_If_Init(void) { sFLASH_Init(); return MAL_OK; }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s) before to branch to application main. */ /* Initialize LEDs mounted on EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); /* Initialize the SPI FLASH driver */ sFLASH_Init(); /* Get SPI Flash ID */ FlashID = sFLASH_ReadID(); /* Check the SPI Flash ID */ if (FlashID == sFLASH_M25P64_ID) { /* OK: Turn on LD1 */ STM_EVAL_LEDOn(LED1); /* Perform a write in the Flash followed by a read of the written data */ /* Erase SPI FLASH Sector to write on */ sFLASH_EraseSector(FLASH_SECTOR_TO_ERASE); /* Write Tx_Buffer data to SPI FLASH memory */ sFLASH_WriteBuffer(Tx_Buffer, FLASH_WRITE_ADDRESS, BufferSize); /* Read data from SPI FLASH memory */ sFLASH_ReadBuffer(Rx_Buffer, FLASH_READ_ADDRESS, BufferSize); /* Check the correctness of written dada */ TransferStatus1 = Buffercmp(Tx_Buffer, Rx_Buffer, BufferSize); /* TransferStatus1 = PASSED, if the transmitted and received data by SPI1 are the same */ /* TransferStatus1 = FAILED, if the transmitted and received data by SPI1 are different */ /* Perform an erase in the Flash followed by a read of the written data */ /* Erase SPI FLASH Sector to write on */ sFLASH_EraseSector(FLASH_SECTOR_TO_ERASE); /* Read data from SPI FLASH memory */ sFLASH_ReadBuffer(Rx_Buffer, FLASH_READ_ADDRESS, BufferSize); /* Check the correctness of erasing operation dada */ for (Index = 0; Index < BufferSize; Index++) { if (Rx_Buffer[Index] != 0xFF) { TransferStatus2 = FAILED; } } /* TransferStatus2 = PASSED, if the specified sector part is erased */ /* TransferStatus2 = FAILED, if the specified sector part is not well erased */ } else { /* Error: Turn on LD2 */ STM_EVAL_LEDOn(LED2); } while (1) {} }
/******************************************************************************* * Function Name : HAL_Core_Config. * Description : Called in startup routine, before calling C++ constructors. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void HAL_Core_Config(void) { // this ensures the stm32_it.c functions aren't dropped by the linker, thinking // they are unused. Without this none of the interrupts handlers are linked. linkme(); DECLARE_SYS_HEALTH(ENTERED_SparkCoreConfig); #ifdef DFU_BUILD_ENABLE /* Set the Vector Table(VT) base location at 0x5000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x5000); USE_SYSTEM_FLAGS = 1; #endif #ifdef SWD_JTAG_DISABLE /* Disable the Serial Wire JTAG Debug Port SWJ-DP */ GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); #else #ifdef SWD_ENABLE_JTAG_DISABLE /* Disable JTAG, but enable SWJ-DP */ GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); #endif #endif Set_System(); SysTick_Configuration(); /* Enable CRC clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); HAL_RTC_Configuration(); /* Execute Stop mode if STOP mode flag is set via System.sleep(pin, mode) */ HAL_Core_Execute_Stop_Mode(); LED_SetRGBColor(RGB_COLOR_WHITE); LED_On(LED_RGB); #ifdef IWDG_RESET_ENABLE // ToDo this needs rework for new bootloader /* Check if the system has resumed from IWDG reset */ if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET) { /* IWDGRST flag set */ IWDG_SYSTEM_RESET = 1; /* Clear reset flags */ RCC_ClearFlag(); } /* We are duplicating the IWDG call here for compatibility with old bootloader */ /* Set IWDG Timeout to 3 secs */ IWDG_Reset_Enable(3 * TIMING_IWDG_RELOAD); #endif #ifdef DFU_BUILD_ENABLE Load_SystemFlags(); #endif sFLASH_Init(); module_user_init_hook(); }
/******************************************************************************* * Function Name : HAL_Core_Config. * Description : Called in startup routine, before calling C++ constructors. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void HAL_Core_Config(void) { DECLARE_SYS_HEALTH(ENTERED_SparkCoreConfig); #ifdef DFU_BUILD_ENABLE //Currently this is done through WICED library API so commented. //NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x20000); USE_SYSTEM_FLAGS = 1; #endif Set_System(); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE); //Wiring pins default to inputs #if !defined(USE_SWD_JTAG) && !defined(USE_SWD) for (pin_t pin=0; pin<=19; pin++) HAL_Pin_Mode(pin, INPUT); #if PLATFORM_ID==8 // Additional pins for P1 for (pin_t pin=24; pin<=29; pin++) HAL_Pin_Mode(pin, INPUT); #endif #if PLATFORM_ID==10 // Additional pins for Electron for (pin_t pin=24; pin<=35; pin++) HAL_Pin_Mode(pin, INPUT); #endif #endif HAL_Core_Config_systick_configuration(); HAL_RTC_Configuration(); HAL_RNG_Configuration(); #ifdef DFU_BUILD_ENABLE Load_SystemFlags(); #endif LED_SetRGBColor(RGB_COLOR_WHITE); LED_On(LED_RGB); // override the WICED interrupts, specifically SysTick - there is a bug // where WICED isn't ready for a SysTick until after main() has been called to // fully intialize the RTOS. HAL_Core_Setup_override_interrupts(); #if MODULAR_FIRMWARE // write protect system module parts if not already protected FLASH_WriteProtectMemory(FLASH_INTERNAL, CORE_FW_ADDRESS, USER_FIRMWARE_IMAGE_LOCATION - CORE_FW_ADDRESS, true); #endif #ifdef HAS_SERIAL_FLASH //Initialize Serial Flash sFLASH_Init(); #else FLASH_AddToFactoryResetModuleSlot(FLASH_INTERNAL, INTERNAL_FLASH_FAC_ADDRESS, FLASH_INTERNAL, USER_FIRMWARE_IMAGE_LOCATION, FIRMWARE_IMAGE_SIZE, FACTORY_RESET_MODULE_FUNCTION, MODULE_VERIFY_CRC|MODULE_VERIFY_FUNCTION|MODULE_VERIFY_DESTINATION_IS_START_ADDRESS); //true to verify the CRC during copy also #endif }
void LLFlashUtil::init() { #ifdef PLATFORM_PHOTON sFLASH_Init(); #endif }
bool FLASH_CopyMemory(flash_device_t sourceDeviceID, uint32_t sourceAddress, flash_device_t destinationDeviceID, uint32_t destinationAddress, uint32_t length, uint8_t module_function, uint8_t flags) { #ifdef USE_SERIAL_FLASH uint8_t serialFlashData[4]; #endif uint32_t internalFlashData = 0; uint32_t endAddress = sourceAddress + length - 1; if (!FLASH_CheckCopyMemory(sourceDeviceID, sourceAddress, destinationDeviceID, destinationAddress, length, module_function, flags)) { return false; } if (FLASH_EraseMemory(destinationDeviceID, destinationAddress, length) != true) { return false; } if (sourceDeviceID == FLASH_SERIAL) { #ifdef USE_SERIAL_FLASH /* Initialize SPI Flash */ sFLASH_Init(); #endif } if (destinationDeviceID == FLASH_INTERNAL) { /* Unlock the internal flash program erase controller */ FLASH_Unlock(); } /* Program source to destination */ while (sourceAddress < endAddress) { if (sourceDeviceID == FLASH_INTERNAL) { /* Read data from internal flash source address */ internalFlashData = (*(__IO uint32_t*) sourceAddress); } #ifdef USE_SERIAL_FLASH else if (sourceDeviceID == FLASH_SERIAL) { /* Read data from serial flash source address */ sFLASH_ReadBuffer(serialFlashData, sourceAddress, 4); } #endif if (destinationDeviceID == FLASH_INTERNAL) { #ifdef USE_SERIAL_FLASH if (sourceDeviceID == FLASH_SERIAL) { internalFlashData = (uint32_t)(serialFlashData[0] | (serialFlashData[1] << 8) | (serialFlashData[2] << 16) | (serialFlashData[3] << 24)); } #endif /* Program data to internal flash destination address */ FLASH_Status status = FLASH_ProgramWord(destinationAddress, internalFlashData); /* If program operation fails, return Failure */ if (status != FLASH_COMPLETE) { return false; } } #ifdef USE_SERIAL_FLASH else if (destinationDeviceID == FLASH_SERIAL) { if (sourceDeviceID == FLASH_INTERNAL) { serialFlashData[0] = (uint8_t)(internalFlashData & 0xFF); serialFlashData[1] = (uint8_t)((internalFlashData & 0xFF00) >> 8); serialFlashData[2] = (uint8_t)((internalFlashData & 0xFF0000) >> 16); serialFlashData[3] = (uint8_t)((internalFlashData & 0xFF000000) >> 24); } /* Program data to serial flash destination address */ sFLASH_WriteBuffer(serialFlashData, destinationAddress, 4); } #endif sourceAddress += 4; destinationAddress += 4; }
bool FLASH_EraseMemory(flash_device_t flashDeviceID, uint32_t startAddress, uint32_t length) { uint32_t eraseCounter = 0; uint32_t numPages = 0; if (FLASH_CheckValidAddressRange(flashDeviceID, startAddress, length) != true) { return false; } if (flashDeviceID == FLASH_INTERNAL) { /* Check which sector has to be erased */ uint16_t flashSector = FLASH_SectorToErase(FLASH_INTERNAL, startAddress); if (flashSector > FLASH_Sector_11) { return false; } /* Disable memory write protection if any */ FLASH_WriteProtectMemory(FLASH_INTERNAL, startAddress, length, false); /* Unlock the Flash Program Erase Controller */ FLASH_Unlock(); /* Define the number of Internal Flash pages to be erased */ numPages = FLASH_PagesMask(length, INTERNAL_FLASH_PAGE_SIZE); /* Clear All pending flags */ FLASH_ClearFlags(); /* Erase the Internal Flash pages */ for (eraseCounter = 0; (eraseCounter < numPages); eraseCounter++) { FLASH_Status status = FLASH_EraseSector(flashSector + (8 * eraseCounter), VoltageRange_3); /* If erase operation fails, return Failure */ if (status != FLASH_COMPLETE) { return false; } } /* Locks the FLASH Program Erase Controller */ FLASH_Lock(); return true; } else if (flashDeviceID == FLASH_SERIAL) { #ifdef USE_SERIAL_FLASH /* Initialize SPI Flash */ sFLASH_Init(); /* Define the number of External Flash pages to be erased */ numPages = FLASH_PagesMask(length, sFLASH_PAGESIZE); /* Erase the SPI Flash pages */ for (eraseCounter = 0; (eraseCounter < numPages); eraseCounter++) { sFLASH_EraseSector(startAddress + (sFLASH_PAGESIZE * eraseCounter)); } /* Return Success */ return true; #endif } /* Return Failure */ return false; }