Skip to content
This repository was archived by the owner on Mar 7, 2026. It is now read-only.

Commit 8ee5019

Browse files
committed
puya: dry code in puya_07x_flash_prepare()
1 parent c104489 commit 8ee5019

1 file changed

Lines changed: 36 additions & 24 deletions

File tree

src/target/puya.c

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* see section "4.4. Flash configuration bytes" of the RM.
4848
*/
4949
#define PUYA_00A_FLASH_TIMING_CAL_BASE 0x1fff0f1cU
50+
#define PUYA_07X_FLASH_TIMING_CAL_BASE 0x1fff3238U
5051

5152
/* This config word is undocumented, but the Puya-ISP boot code
5253
* uses it to determine the valid flash/ram size.
@@ -175,26 +176,22 @@ static bool puya_00a_flash_prepare(target_flash_s *flash)
175176
hsi_fs = 0;
176177
DEBUG_TARGET("HSI frequency selection is %d\n", hsi_fs);
177178

178-
const uint32_t eppara0 = target_mem32_read32(flash->t, PUYA_00A_FLASH_TIMING_CAL_BASE + hsi_fs * 20 + 0);
179-
const uint32_t eppara1 = target_mem32_read32(flash->t, PUYA_00A_FLASH_TIMING_CAL_BASE + hsi_fs * 20 + 4);
180-
const uint32_t eppara2 = target_mem32_read32(flash->t, PUYA_00A_FLASH_TIMING_CAL_BASE + hsi_fs * 20 + 8);
181-
const uint32_t eppara3 = target_mem32_read32(flash->t, PUYA_00A_FLASH_TIMING_CAL_BASE + hsi_fs * 20 + 12);
182-
const uint32_t eppara4 = target_mem32_read32(flash->t, PUYA_00A_FLASH_TIMING_CAL_BASE + hsi_fs * 20 + 16);
183-
DEBUG_TARGET("PY32 flash timing cal 0: %08" PRIx32 "\n", eppara0);
184-
DEBUG_TARGET("PY32 flash timing cal 1: %08" PRIx32 "\n", eppara1);
185-
DEBUG_TARGET("PY32 flash timing cal 2: %08" PRIx32 "\n", eppara2);
186-
DEBUG_TARGET("PY32 flash timing cal 3: %08" PRIx32 "\n", eppara3);
187-
DEBUG_TARGET("PY32 flash timing cal 4: %08" PRIx32 "\n", eppara4);
179+
uint32_t eppara[5] = {0};
188180

189-
target_mem32_write32(flash->t, PUYA_FLASH_TS0, eppara0 & 0xffU);
190-
target_mem32_write32(flash->t, PUYA_FLASH_TS1, (eppara0 >> 16U) & 0x1ffU);
191-
target_mem32_write32(flash->t, PUYA_FLASH_TS3, (eppara0 >> 8U) & 0xffU);
192-
target_mem32_write32(flash->t, PUYA_FLASH_TS2P, eppara1 & 0xffU);
193-
target_mem32_write32(flash->t, PUYA_FLASH_TPS3, (eppara1 >> 16U) & 0x7ffU);
194-
target_mem32_write32(flash->t, PUYA_FLASH_PERTPE, eppara2 & 0x1ffffU);
195-
target_mem32_write32(flash->t, PUYA_FLASH_SMERTPE, eppara3 & 0x1ffffU);
196-
target_mem32_write32(flash->t, PUYA_FLASH_PRGTPE, eppara4 & 0xffffU);
197-
target_mem32_write32(flash->t, PUYA_FLASH_PRETPE, (eppara4 >> 16U) & 0x3fffU);
181+
for (uint16_t i = 0; i < 5; i++) {
182+
eppara[i] = target_mem32_read32(flash->t, PUYA_00A_FLASH_TIMING_CAL_BASE + hsi_fs * 20 + i * 4U);
183+
DEBUG_TARGET("PY32 flash timing cal %u: %08" PRIx32 "\n", i, eppara[i]);
184+
}
185+
186+
target_mem32_write32(flash->t, PUYA_FLASH_TS0, eppara[0] & 0xffU);
187+
target_mem32_write32(flash->t, PUYA_FLASH_TS1, (eppara[0] >> 16U) & 0x1ffU);
188+
target_mem32_write32(flash->t, PUYA_FLASH_TS3, (eppara[0] >> 8U) & 0xffU);
189+
target_mem32_write32(flash->t, PUYA_FLASH_TS2P, eppara[1] & 0xffU);
190+
target_mem32_write32(flash->t, PUYA_FLASH_TPS3, (eppara[1] >> 16U) & 0x7ffU);
191+
target_mem32_write32(flash->t, PUYA_FLASH_PERTPE, eppara[2] & 0x1ffffU);
192+
target_mem32_write32(flash->t, PUYA_FLASH_SMERTPE, eppara[3] & 0x1ffffU);
193+
target_mem32_write32(flash->t, PUYA_FLASH_PRGTPE, eppara[4] & 0xffffU);
194+
target_mem32_write32(flash->t, PUYA_FLASH_PRETPE, (eppara[4] >> 16U) & 0x3fffU);
198195

199196
return true;
200197
}
@@ -210,18 +207,22 @@ static bool puya_07x_flash_prepare(target_flash_s *flash)
210207
hsi_fs = 0;
211208
DEBUG_TARGET("HSI frequency selection is %d\n", hsi_fs);
212209

210+
<<<<<<< HEAD
213211
const uint32_t eppara0 = target_mem32_read32(flash->t, 0x1FFF3238 + 4 * 0x28);
214212
const uint32_t eppara1 = target_mem32_read32(flash->t, 0x1FFF3240 + 4 * 0x28);
215213
const uint32_t eppara2 = target_mem32_read32(flash->t, 0x1FFF3248 + 4 * 0x28);
216214
const uint32_t eppara3 = target_mem32_read32(flash->t, 0x1FFF3250 + 4 * 0x28);
217215
const uint32_t eppara4 = target_mem32_read32(flash->t, 0x1FFF3258 + 4 * 0x28);
216+
=======
217+
uint32_t eppara[5] = {0};
218+
>>>>>>> 07d70751 (puya: dry code in puya_07x_flash_prepare())
218219

219-
DEBUG_TARGET("PY32 flash timing cal 0: %08" PRIx32 "\n", eppara0);
220-
DEBUG_TARGET("PY32 flash timing cal 1: %08" PRIx32 "\n", eppara1);
221-
DEBUG_TARGET("PY32 flash timing cal 2: %08" PRIx32 "\n", eppara2);
222-
DEBUG_TARGET("PY32 flash timing cal 3: %08" PRIx32 "\n", eppara3);
223-
DEBUG_TARGET("PY32 flash timing cal 4: %08" PRIx32 "\n", eppara4);
220+
for (uint16_t i = 0; i < 5; i++) {
221+
eppara[i] = target_mem32_read32(flash->t, PUYA_07X_FLASH_TIMING_CAL_BASE + 4 * 0x28 + i * 8U);
222+
DEBUG_TARGET("PY32 flash timing cal %u: %08" PRIx32 "\n", i, eppara[i]);
223+
}
224224

225+
<<<<<<< HEAD
225226
target_mem32_write32(flash->t, PUYA_FLASH_TS0, eppara0 & 0xffU);
226227
target_mem32_write32(flash->t, PUYA_FLASH_TS1, (eppara0 >> 16U) & 0x1ffU);
227228
target_mem32_write32(flash->t, PUYA_FLASH_TS3, (eppara0 >> 8U) & 0xffU);
@@ -231,6 +232,17 @@ static bool puya_07x_flash_prepare(target_flash_s *flash)
231232
target_mem32_write32(flash->t, PUYA_FLASH_SMERTPE, eppara3 & 0x1ffffU);
232233
target_mem32_write32(flash->t, PUYA_FLASH_PRGTPE, eppara4 & 0xffffU);
233234
target_mem32_write32(flash->t, PUYA_FLASH_PRETPE, (eppara4 >> 16U) & 0xFFFFU); // diff
235+
=======
236+
target_mem32_write32(flash->t, PUYA_FLASH_TS0, eppara[0] & 0xffU);
237+
target_mem32_write32(flash->t, PUYA_FLASH_TS1, (eppara[0] >> 16U) & 0x1ffU);
238+
target_mem32_write32(flash->t, PUYA_FLASH_TS3, (eppara[0] >> 8U) & 0xffU);
239+
target_mem32_write32(flash->t, PUYA_FLASH_TS2P, eppara[1] & 0xffU);
240+
target_mem32_write32(flash->t, PUYA_FLASH_TPS3, (eppara[1] >> 16U) & 0x7ffU);
241+
target_mem32_write32(flash->t, PUYA_FLASH_PERTPE, eppara[2] & 0x1ffffU);
242+
target_mem32_write32(flash->t, PUYA_FLASH_SMERTPE, eppara[3] & 0x1ffffU);
243+
target_mem32_write32(flash->t, PUYA_FLASH_PRGTPE, eppara[4] & 0xffffU);
244+
target_mem32_write32(flash->t, PUYA_FLASH_PRETPE, (eppara[4] >> 16U) & 0xffffU); // diff
245+
>>>>>>> 07d70751 (puya: dry code in puya_07x_flash_prepare())
234246

235247
return true;
236248
}

0 commit comments

Comments
 (0)