From ee05577dd62c64d0e6a2e497b75710c7a1351557 Mon Sep 17 00:00:00 2001 From: JF Date: Mon, 11 May 2020 18:50:37 +0200 Subject: Fix race conditions on SPI and integrate the SPI NOR Flash driver into DFUService (WIP) --- src/Components/Ble/DfuService.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/Components/Ble/DfuService.cpp') diff --git a/src/Components/Ble/DfuService.cpp b/src/Components/Ble/DfuService.cpp index 22983a83..0c8d357c 100644 --- a/src/Components/Ble/DfuService.cpp +++ b/src/Components/Ble/DfuService.cpp @@ -15,9 +15,10 @@ int DfuServiceCallback(uint16_t conn_handle, uint16_t attr_handle, return dfuService->OnServiceData(conn_handle, attr_handle, ctxt); } -DfuService::DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController) : +DfuService::DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, Pinetime::Drivers::SpiNorFlash& spiNorFlash) : systemTask{systemTask}, bleController{bleController}, + spiNorFlash{spiNorFlash}, characteristicDefinition{ { .uuid = (ble_uuid_t *) &packetCharacteristicUuid, @@ -104,6 +105,15 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { applicationSize = om->om_data[8] + (om->om_data[9] << 8) + (om->om_data[10] << 16) + (om->om_data[11] << 24); NRF_LOG_INFO("[DFU] -> Start data received : SD size : %d, BT size : %d, app size : %d", softdeviceSize, bootloaderSize, applicationSize); + for(int erased = 0; erased < applicationSize; erased += 0x1000) { + spiNorFlash.SectorErase(erased); + + auto p = spiNorFlash.ProgramFailed(); + auto e = spiNorFlash.EraseFailed(); + NRF_LOG_INFO("[DFU] Erasing sector %d - %d-%d", erased, p, e); + + } + uint8_t data[] {16, 1, 1}; SendNotification(connectionHandle, data, 3); state = States::Init; @@ -128,10 +138,15 @@ int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf *om) { case States::Data: { nbPacketReceived++; + + spiNorFlash.Write(bytesReceived, om->om_data, om->om_len); + bytesReceived += om->om_len; bleController.FirmwareUpdateCurrentBytes(bytesReceived); NRF_LOG_INFO("[DFU] -> Bytes received : %d in %d packets", bytesReceived, nbPacketReceived); + + if((nbPacketReceived % nbPacketsToNotify) == 0) { uint8_t data[5]{static_cast(Opcodes::PacketReceiptNotification), (uint8_t)(bytesReceived&0x000000FFu),(uint8_t)(bytesReceived>>8u), (uint8_t)(bytesReceived>>16u),(uint8_t)(bytesReceived>>24u) }; -- cgit v1.2.3