1/*
2  Copyright (c) 2020 Arduino LLC.  All right reserved.
3
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  See the GNU Lesser General Public License for more details.
13
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17*/
18
19#include <FlashStorage.h>
20#include <MKRNB.h>
21
22#define SBU_START    0x2000
23#define SBU_SIZE     0x8000
24
25#define SKETCH_START (uint32_t*)(SBU_START + SBU_SIZE)
26
27static constexpr char UPDATE_FILE_NAME[] = "UPDATE.BIN";
28static constexpr char CHECK_FILE_NAME[] = "UPDATE.OK";
29
30FlashClass mcu_flash;
31
32NBFileUtils  fileUtils;
33
34extern "C" void __libc_init_array(void);
35
36int main()
37{
38  init();
39
40  __libc_init_array();
41
42  delay(1);
43
44  constexpr size_t blockSize = 512;
45  fileUtils.begin();
46  Serial1.begin(115200);
47  Serial1.print("SBU start. ");
48
49  bool update_success = false;
50
51  // Try to update only if update file
52  // has been download successfully.
53
54  if (fileUtils.listFile(CHECK_FILE_NAME)) {
55    Serial1.println("Update file exists");
56    uint32_t updateSize = fileUtils.listFile(UPDATE_FILE_NAME);
57    size_t cycles = (updateSize / blockSize) + 1;
58
59    if (updateSize > SBU_SIZE) {
60      updateSize = updateSize - SBU_SIZE - SBU_START;
61      /* Erase the MCU flash */
62      uint32_t flash_address = (uint32_t)SKETCH_START;
63      mcu_flash.erase((void*)flash_address, updateSize);
64
65      for (auto i = 0; i < cycles; i++) {
66        uint8_t block[blockSize] { 0 };
67        digitalWrite(LED_BUILTIN, LOW);
68        uint32_t read_bytes = fileUtils.readBlock(UPDATE_FILE_NAME, (i * blockSize) + SBU_SIZE + SBU_START, blockSize, block);
69        digitalWrite(LED_BUILTIN, HIGH);
70        mcu_flash.write((void*)flash_address, block, read_bytes);
71        flash_address += read_bytes;
72      }
73      update_success = true;
74    }
75    if (update_success) {
76      fileUtils.deleteFile(UPDATE_FILE_NAME);
77      fileUtils.deleteFile(CHECK_FILE_NAME);
78    }
79  }
80  else {
81    Serial1.println("Update file does not exist");
82    delay(100);
83  }
84
85boot:
86  /* Jump to the sketch */
87  __set_MSP(*SKETCH_START);
88
89  /* Reset vector table address */
90  SCB->VTOR = ((uint32_t)(SKETCH_START) & SCB_VTOR_TBLOFF_Msk);
91
92  /* Address of Reset_Handler is written by the linker at the beginning of the .text section (see linker script) */
93  uint32_t resetHandlerAddress = (uint32_t) * (SKETCH_START + 1);
94  /* Jump to reset handler */
95  asm("bx %0"::"r"(resetHandlerAddress));
96}
97