|
| 1 | +/* |
| 2 | + * This sketch is a modified version of the SDUBoot example from the SDU |
| 3 | + * library in the Arduino SAMD core. |
| 4 | + * |
| 5 | + * SDUBoot.ino is converted with build.sh into HEX values in a .h file |
| 6 | + * in 'boot' folder of the SDU library. The SDU library with support of |
| 7 | + * the linker script inserts the binary representation of this sketch |
| 8 | + * before the actual sketch. This way this sketch acts as a second stage |
| 9 | + * bootloader always uploaded with the sketch's binary. |
| 10 | + * |
| 11 | + * To make this second stage bootloader as small as possible while it is |
| 12 | + * still an Arduino sketch, it can't be built with standard boards |
| 13 | + * configurations of UNO R4 Minima or Wifi. |
| 14 | + * Additionally the libfsp.a is built without code flash writing support |
| 15 | + * and this sketch needs it. |
| 16 | + * |
| 17 | + * I created a custom board definition to build this sketch to a binary |
| 18 | + * size less than 0x6000 bytes. |
| 19 | + * https://github.com/JAndrassy/my_boards/tree/master/renesas-uno |
| 20 | + * The custom unor4_sdu variant has peripherals support removed with many |
| 21 | + * 'HOWMANY' settings in pins_arduino.h set to 0 to make the SDUBoot sketch |
| 22 | + * bin even smaller. |
| 23 | + * The bin size reduction for libfsp.a and the sketch is described here |
| 24 | + * https://github.com/arduino/ArduinoCore-renesas/discussions/118 |
| 25 | + */ |
| 26 | + |
| 27 | +#include <SD.h> |
| 28 | +#include <r_flash_lp.h> |
| 29 | + |
| 30 | +#define SDU_START 0x4000 |
| 31 | +#define SDU_SIZE 0x6000 |
| 32 | + |
| 33 | +#define SKETCH_START (uint32_t*)(SDU_START + SDU_SIZE) |
| 34 | + |
| 35 | +#ifndef SDCARD_SS_PIN |
| 36 | +#define SDCARD_SS_PIN 4 |
| 37 | +#endif |
| 38 | + |
| 39 | +#define UPDATE_FILE "UPDATE.BIN" |
| 40 | + |
| 41 | +flash_lp_instance_ctrl_t ctrl; |
| 42 | +flash_cfg_t cfg; |
| 43 | + |
| 44 | +void stopAgt(); //add to core/time.cpp as { main_timer.close();} |
| 45 | + |
| 46 | +void setup() { |
| 47 | + |
| 48 | + delay(1); |
| 49 | + |
| 50 | + if (SD.begin(SDCARD_SS_PIN) && SD.exists(UPDATE_FILE)) { |
| 51 | + |
| 52 | + File updateFile = SD.open(UPDATE_FILE); |
| 53 | + uint32_t updateSize = updateFile.size(); |
| 54 | + bool updateFlashed = false; |
| 55 | + |
| 56 | + if (updateSize > SDU_SIZE) { |
| 57 | + // skip the SDU section |
| 58 | + updateFile.seek(SDU_SIZE); |
| 59 | + updateSize -= SDU_SIZE; |
| 60 | + |
| 61 | + cfg.data_flash_bgo = false; |
| 62 | + cfg.p_callback = nullptr; |
| 63 | + cfg.p_context = nullptr; |
| 64 | + cfg.ipl = (BSP_IRQ_DISABLED); |
| 65 | + cfg.irq = FSP_INVALID_VECTOR; |
| 66 | + |
| 67 | + fsp_err_t rv = R_FLASH_LP_Open(&ctrl, &cfg); |
| 68 | + if (rv == FSP_SUCCESS) { |
| 69 | + |
| 70 | + uint32_t flashAddress = (uint32_t) SKETCH_START; |
| 71 | + |
| 72 | + // erase the pages |
| 73 | + __disable_irq(); |
| 74 | + rv = R_FLASH_LP_Erase(&ctrl, flashAddress, (updateSize / BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE) + 1); |
| 75 | + __enable_irq(); |
| 76 | + if (rv == FSP_SUCCESS) { |
| 77 | + |
| 78 | + uint8_t buffer[32 * BSP_FEATURE_FLASH_LP_CF_WRITE_SIZE]; |
| 79 | + |
| 80 | + // write the pages |
| 81 | + for (uint32_t i = 0; i < updateSize; i += sizeof(buffer)) { |
| 82 | + updateFile.read(buffer, sizeof(buffer)); |
| 83 | + __disable_irq(); |
| 84 | + R_FLASH_LP_Write(&ctrl, (uint32_t) &buffer, flashAddress, sizeof(buffer)); |
| 85 | + __enable_irq(); |
| 86 | + flashAddress += sizeof(buffer); |
| 87 | + } |
| 88 | + |
| 89 | + updateFlashed = true; |
| 90 | + } |
| 91 | + |
| 92 | + R_FLASH_LP_Close(&ctrl); |
| 93 | + } |
| 94 | + |
| 95 | + updateFile.close(); |
| 96 | + |
| 97 | + if (updateFlashed) { |
| 98 | + SD.remove(UPDATE_FILE); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + stopAgt(); |
| 104 | + |
| 105 | + SysTick->CTRL = 0; |
| 106 | + |
| 107 | + // Disable MSP monitoring. |
| 108 | + R_MPU_SPMON->SP[0].CTL = 0; |
| 109 | + |
| 110 | + // jump to the sketch |
| 111 | + __set_MSP(*SKETCH_START); |
| 112 | + |
| 113 | + //Reset vector table address |
| 114 | + SCB->VTOR = ((uint32_t) (SKETCH_START ) & SCB_VTOR_TBLOFF_Msk); |
| 115 | + |
| 116 | + // address of Reset_Handler is written by the linker at the beginning of the .text section (see linker script) |
| 117 | + uint32_t resetHandlerAddress = (uint32_t) *(SKETCH_START + 1); |
| 118 | + // jump to reset handler |
| 119 | + asm("bx %0"::"r"(resetHandlerAddress)); |
| 120 | +} |
| 121 | + |
| 122 | +void loop() { |
| 123 | +} |
0 commit comments