// todo - would prefer this was provided as a callback. extern "C" int _gettimeofday( struct timeval *tv, void *tzvp ) { uint32_t t = HAL_Timer_Milliseconds(); // get uptime in nanoseconds tv->tv_sec = t / 1000; // convert to seconds tv->tv_usec = ( t % 1000 )*1000; // get remaining microseconds return 0; // return non-zero for error } // end _gettimeofday()
int Spark_Prepare_For_Firmware_Update(FileTransfer::Descriptor& file, uint32_t flags, void* reserved) { if (file.store==FileTransfer::Store::FIRMWARE) { // address is relative to the OTA region. Normally will be 0. file.file_address = HAL_OTA_FlashAddress() + file.chunk_address; // chunk_size 0 indicates defaults. if (file.chunk_size==0) { file.chunk_size = HAL_OTA_ChunkSize(); file.file_length = HAL_OTA_FlashLength(); } } int result = 0; if (flags & 1) { // only check address } else { uint32_t start = HAL_Timer_Milliseconds(); system_set_flag(SYSTEM_FLAG_OTA_UPDATE_PENDING, 1, nullptr); volatile bool flag = false; system_notify_event(firmware_update_pending, 0, nullptr, set_flag, (void*)&flag); System.waitCondition([&flag]{return flag;}, timeRemaining(start, 30000)); system_set_flag(SYSTEM_FLAG_OTA_UPDATE_PENDING, 0, nullptr); if (System.updatesEnabled()) // application event is handled asynchronously { RGB.control(true); RGB.color(RGB_COLOR_MAGENTA); SPARK_FLASH_UPDATE = 1; TimingFlashUpdateTimeout = 0; system_notify_event(firmware_update, firmware_update_begin, &file); HAL_FLASH_Begin(file.file_address, file.file_length, NULL); } else { result = 1; // updates disabled } } return result; }
uint32_t timeRemaining(uint32_t start, uint32_t duration) { uint32_t elapsed = HAL_Timer_Milliseconds()-start; return (elapsed>=duration) ? 0 : duration-elapsed; }