Jari há 3 meses atrás
pai
commit
d7bde514e2
3 ficheiros alterados com 45 adições e 29 exclusões
  1. 15 9
      OpenPuck/fw_update.cpp
  2. 19 9
      OpenPuck/fw_update.h
  3. 11 11
      OpenPuck/webusb_config.cpp

+ 15 - 9
OpenPuck/fw_update.cpp

@@ -138,7 +138,8 @@ uint8_t fwupBegin(uint32_t size, uint32_t crc32)
 	s_crc = crc32;
 	s_base = base;
 	s_off = 0;
-	s_erased = base; // pages erased lazily, one per chunk at most (85 ms CPU stall each)
+	s_erased =
+		base; // pages erased lazily, one per chunk at most (85 ms CPU stall each)
 	s_active = true;
 	return FWUP_OK;
 }
@@ -172,9 +173,10 @@ uint8_t fwupEnd(void)
 	if (!s_active) {
 		// duplicate END (the panel retried because our ack got lost): if the commit it is asking about
 		// is exactly the one already on the meta page, re-report success instead of ERR_STATE.
-		const volatile FwupMeta *m = (const volatile FwupMeta *)FWUP_META;
-		if (s_size && m->magic == FWUP_META_MAGIC && m->size == s_size &&
-		    m->crc == s_crc)
+		const volatile FwupMeta *m =
+			(const volatile FwupMeta *)FWUP_META;
+		if (s_size && m->magic == FWUP_META_MAGIC &&
+		    m->size == s_size && m->crc == s_crc)
 			return FWUP_OK;
 		return FWUP_ERR_STATE;
 	}
@@ -211,7 +213,8 @@ void fwupAbort(void)
 // 1..N-1 are written forward, then the vector page is written BACKWARD so word 0 -- the app-validity marker
 // the bootloader checks -- is the final word written anywhere in the app. Interrupt the sequence at any
 // point and the board reads as app-less: the UF2 bootloader keeps it, drag-and-drop recovers it.
-#define FWUP_RAMFUNC __attribute__((used, noinline, section(".data.opk_ramfunc")))
+#define FWUP_RAMFUNC \
+	__attribute__((used, noinline, section(".data.opk_ramfunc")))
 FWUP_RAMFUNC static void ramApply(uint32_t dst, uint32_t src, uint32_t size,
 				  uint32_t metaPage, uint32_t blSettings)
 {
@@ -334,8 +337,10 @@ void fwupApplyIfArmed(void)
 		  (uint32_t)~crc32Step(0xFFFFFFFFUL, (const uint8_t *)&v, 16);
 	ok = ok && v.size && v.size <= FWUP_MAX_IMG && (v.size & 3) == 0;
 	ok = ok && v.staged >= FWUP_APP_BASE + FWUP_MAX_IMG &&
-	     v.staged + v.size <= FWUP_META && (v.staged & (FWUP_PAGE - 1)) == 0;
-	ok = ok && crc32Flash(v.staged, v.size) == v.crc; // staged bytes still intact?
+	     v.staged + v.size <= FWUP_META &&
+	     (v.staged & (FWUP_PAGE - 1)) == 0;
+	ok = ok && crc32Flash(v.staged, v.size) ==
+			   v.crc; // staged bytes still intact?
 	ok = ok && vectorsPlausible(v.staged, v.size);
 	if (!ok) {
 		nvmcErasePage(FWUP_META);
@@ -346,8 +351,9 @@ void fwupApplyIfArmed(void)
 	// a fault inside the copier resets into the safe app-less/bootloader state instead of hanging forever.
 	__disable_irq();
 	if (!NRF_WDT->RUNSTATUS) {
-		NRF_WDT->CONFIG = (WDT_CONFIG_HALT_Pause << WDT_CONFIG_HALT_Pos) |
-				  (WDT_CONFIG_SLEEP_Run << WDT_CONFIG_SLEEP_Pos);
+		NRF_WDT->CONFIG =
+			(WDT_CONFIG_HALT_Pause << WDT_CONFIG_HALT_Pos) |
+			(WDT_CONFIG_SLEEP_Run << WDT_CONFIG_SLEEP_Pos);
 		NRF_WDT->CRV = 8UL * 32768UL - 1; // ~8 s, same as the app's own
 		NRF_WDT->RREN = WDT_RREN_RR0_Msk;
 		NRF_WDT->TASKS_START = 1;

+ 19 - 9
OpenPuck/fw_update.h

@@ -28,18 +28,28 @@
 // ACK status codes, echoed to the panel in the 0xAB frame ([0xAB][5][status][nextOff u32 LE]).
 enum {
 	FWUP_OK = 0,
-	FWUP_ERR_STATE = 1, // command out of sequence (no BEGIN, or END before all bytes)
-	FWUP_ERR_BOUNDS = 2, // image too big / would collide with the running app or config region
-	FWUP_ERR_OFFSET = 3, // chunk offset mismatch (ack carries the expected offset -- resync and resend)
+	FWUP_ERR_STATE =
+		1, // command out of sequence (no BEGIN, or END before all bytes)
+	FWUP_ERR_BOUNDS =
+		2, // image too big / would collide with the running app or config region
+	FWUP_ERR_OFFSET =
+		3, // chunk offset mismatch (ack carries the expected offset -- resync and resend)
 	FWUP_ERR_CRC = 4, // staged bytes don't match the announced CRC32
-	FWUP_ERR_VECTOR = 5, // staged image doesn't start with a plausible Cortex-M vector table
+	FWUP_ERR_VECTOR =
+		5, // staged image doesn't start with a plausible Cortex-M vector table
 };
 
-uint8_t fwupBegin(uint32_t size, uint32_t crc32); // 0x20: announce image; disarms any previous staged update
-uint8_t fwupChunk(uint32_t off, const uint8_t *d, uint8_t len); // 0x21: sequential data (len%4==0, <=128)
-uint8_t fwupEnd(void); // 0x22: verify staged CRC32 + vectors, commit the meta page (ARMS the update)
-void fwupAbort(void); // 0x24: disarm + drop any transfer state (staged bytes become inert garbage)
-uint32_t fwupNextOff(void); // next expected chunk offset (returned in every ack for panel resync)
+uint8_t fwupBegin(
+	uint32_t size,
+	uint32_t crc32); // 0x20: announce image; disarms any previous staged update
+uint8_t fwupChunk(uint32_t off, const uint8_t *d,
+		  uint8_t len); // 0x21: sequential data (len%4==0, <=128)
+uint8_t
+fwupEnd(void); // 0x22: verify staged CRC32 + vectors, commit the meta page (ARMS the update)
+void fwupAbort(
+	void); // 0x24: disarm + drop any transfer state (staged bytes become inert garbage)
+uint32_t fwupNextOff(
+	void); // next expected chunk offset (returned in every ack for panel resync)
 
 // Apply a committed staged update. Call FIRST in setup(): if a valid meta page is present this never returns
 // (copies staged->app from RAM and resets). Invalid/stale meta is erased and ignored.

+ 11 - 11
OpenPuck/webusb_config.cpp

@@ -563,17 +563,17 @@ void webusbPoll()
 			// Command length (fixed per opcode). 0x0D = write-one-bond-slot (27 B); 0x05/0x0E/0x0F/0x10 carry
 			// one value byte; 0x02 a field+value; 0x0A a 3-byte magic. Firmware update: 0x20 begin
 			// [size u32][crc32 u32], 0x21 data (6 B header + payload), 0x22/0x23/0x24 bare.
-			uint8_t need = (op == 0x0D) ? 27 :
-				       (op == 0x02) ? 3 :
-				       (op == 0x03 || op == 0x05 ||
-					op == 0x0E || op == 0x0F ||
-					op == 0x10) ?
-						      2 :
-				       (op == 0x0A) ? 4 :
-				       (op == 0x20) ? 9 :
-				       (op == 0x21) ?
-						      (uint8_t)(6 + (n >= 6 ? buf[5] : 0)) :
-						      1;
+			uint8_t need =
+				(op == 0x0D) ? 27 :
+				(op == 0x02) ? 3 :
+				(op == 0x03 || op == 0x05 || op == 0x0E ||
+				 op == 0x0F || op == 0x10) ?
+					       2 :
+				(op == 0x0A) ? 4 :
+				(op == 0x20) ? 9 :
+				(op == 0x21) ? (uint8_t)(6 + (n >= 6 ? buf[5] :
+								       0)) :
+					       1;
 			if (n < need)
 				break; // wait for more bytes
 			if (op == 0x01) {