|
| 1 | +/** |
| 2 | + * |
| 3 | + * Maple Mini bootloader transition sketch. |
| 4 | + * Based on sketch from Gregwar for Robotis OpenCM9.04, which is based on Maple bootloader code. |
| 5 | + * |
| 6 | + * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 7 | + * WARNING WARNING |
| 8 | + * WARNING This comes with NO WARRANTY, you have to be perfectly sure WARNING |
| 9 | + * WARNING and aware of what you are doing. WARNING |
| 10 | + * WARNING WARNING |
| 11 | + * WARNING Running this sketch will erase your bootloader to put the WARNING |
| 12 | + * WARNING New Maple Mini one. This is still a BETA version WARNING |
| 13 | + * WARNING WARNING |
| 14 | + * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +#include "maple_mini_boot20.h" |
| 19 | +#define BOARD_LED_PIN PB1 |
| 20 | + |
| 21 | + |
| 22 | +typedef struct { |
| 23 | + volatile uint32 CR; |
| 24 | + volatile uint32 CFGR; |
| 25 | + volatile uint32 CIR; |
| 26 | + volatile uint32 APB2RSTR; |
| 27 | + volatile uint32 APB1RSTR; |
| 28 | + volatile uint32 AHBENR; |
| 29 | + volatile uint32 APB2ENR; |
| 30 | + volatile uint32 APB1ENR; |
| 31 | + volatile uint32 BDCR; |
| 32 | + volatile uint32 CSR; |
| 33 | +} |
| 34 | +RCC_RegStruct; |
| 35 | + |
| 36 | +typedef enum { |
| 37 | + RESET = 0, SET = !RESET } |
| 38 | +FlagStatus, ITStatus; |
| 39 | + |
| 40 | +typedef enum { |
| 41 | + DISABLE = 0, ENABLE = !DISABLE} |
| 42 | +FunctionalState; |
| 43 | + |
| 44 | +typedef enum { |
| 45 | + ERROR = 0, SUCCESS = !ERROR} |
| 46 | +ErrorStatus; |
| 47 | + |
| 48 | +#define BOOTLOADER_FLASH ((uint32)0x08000000) |
| 49 | +#define PAGE_SIZE 1024 |
| 50 | + |
| 51 | +#define SET_REG(addr,val) do { *(volatile uint32*)(addr)=val; } while(0) |
| 52 | +#define GET_REG(addr) (*(volatile uint32*)(addr)) |
| 53 | + |
| 54 | + |
| 55 | +#define RCC ((uint32)0x40021000) |
| 56 | +#define FLASH ((uint32)0x40022000) |
| 57 | + |
| 58 | +#define RCC_CR RCC |
| 59 | +#define RCC_CFGR (RCC + 0x04) |
| 60 | +#define RCC_CIR (RCC + 0x08) |
| 61 | +#define RCC_AHBENR (RCC + 0x14) |
| 62 | +#define RCC_APB2ENR (RCC + 0x18) |
| 63 | +#define RCC_APB1ENR (RCC + 0x1C) |
| 64 | + |
| 65 | +#define FLASH_ACR (FLASH + 0x00) |
| 66 | +#define FLASH_KEYR (FLASH + 0x04) |
| 67 | +#define FLASH_OPTKEYR (FLASH + 0x08) |
| 68 | +#define FLASH_SR (FLASH + 0x0C) |
| 69 | +#define FLASH_CR (FLASH + 0x10) |
| 70 | +#define FLASH_AR (FLASH + 0x14) |
| 71 | +#define FLASH_OBR (FLASH + 0x1C) |
| 72 | +#define FLASH_WRPR (FLASH + 0x20) |
| 73 | + |
| 74 | +#define FLASH_KEY1 0x45670123 |
| 75 | +#define FLASH_KEY2 0xCDEF89AB |
| 76 | +#define FLASH_RDPRT 0x00A5 |
| 77 | +#define FLASH_SR_BSY 0x01 |
| 78 | +#define FLASH_CR_PER 0x02 |
| 79 | +#define FLASH_CR_PG 0x01 |
| 80 | +#define FLASH_CR_START 0x40 |
| 81 | + |
| 82 | +#define pRCC ((RCC_RegStruct *) RCC) |
| 83 | + |
| 84 | +char * bootloader; |
| 85 | + |
| 86 | +bool flashErasePage(uint32 pageAddr) { |
| 87 | + uint32 rwmVal = GET_REG(FLASH_CR); |
| 88 | + rwmVal = FLASH_CR_PER; |
| 89 | + SET_REG(FLASH_CR, rwmVal); |
| 90 | + |
| 91 | + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) { |
| 92 | + } |
| 93 | + SET_REG(FLASH_AR, pageAddr); |
| 94 | + SET_REG(FLASH_CR, FLASH_CR_START | FLASH_CR_PER); |
| 95 | + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) { |
| 96 | + } |
| 97 | + |
| 98 | + /* todo: verify the page was erased */ |
| 99 | + |
| 100 | + rwmVal = 0x00; |
| 101 | + SET_REG(FLASH_CR, rwmVal); |
| 102 | + |
| 103 | + return true; |
| 104 | +} |
| 105 | +bool flashErasePages(uint32 pageAddr, uint16 n) { |
| 106 | + while (n-- > 0) { |
| 107 | + if (!flashErasePage(pageAddr + 0x400 * n)) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return true; |
| 113 | +} |
| 114 | + |
| 115 | +bool flashWriteWord(uint32 addr, uint32 word) { |
| 116 | + volatile uint16 *flashAddr = (volatile uint16 *)addr; |
| 117 | + volatile uint32 lhWord = (volatile uint32)word & 0x0000FFFF; |
| 118 | + volatile uint32 hhWord = ((volatile uint32)word & 0xFFFF0000) >> 16; |
| 119 | + |
| 120 | + uint32 rwmVal = GET_REG(FLASH_CR); |
| 121 | + SET_REG(FLASH_CR, FLASH_CR_PG); |
| 122 | + |
| 123 | + /* apparently we need not write to FLASH_AR and can |
| 124 | + simply do a native write of a half word */ |
| 125 | + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) { |
| 126 | + } |
| 127 | + *(flashAddr + 0x01) = (volatile uint16)hhWord; |
| 128 | + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) { |
| 129 | + } |
| 130 | + *(flashAddr) = (volatile uint16)lhWord; |
| 131 | + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) { |
| 132 | + } |
| 133 | + |
| 134 | + rwmVal &= 0xFFFFFFFE; |
| 135 | + SET_REG(FLASH_CR, rwmVal); |
| 136 | + |
| 137 | + /* verify the write */ |
| 138 | + if (*(volatile uint32 *)addr != word) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + return true; |
| 143 | +} |
| 144 | + |
| 145 | +void flashLock() { |
| 146 | + /* take down the HSI oscillator? it may be in use elsewhere */ |
| 147 | + |
| 148 | + /* ensure all FPEC functions disabled and lock the FPEC */ |
| 149 | + SET_REG(FLASH_CR, 0x00000080); |
| 150 | +} |
| 151 | + |
| 152 | +void flashUnlock() { |
| 153 | + /* unlock the flash */ |
| 154 | + SET_REG(FLASH_KEYR, FLASH_KEY1); |
| 155 | + SET_REG(FLASH_KEYR, FLASH_KEY2); |
| 156 | +} |
| 157 | + |
| 158 | +/* Minimum_Source*/ |
| 159 | + |
| 160 | +void setupFLASH() { |
| 161 | + /* configure the HSI oscillator */ |
| 162 | + if ((pRCC->CR & 0x01) == 0x00) { |
| 163 | + uint32 rwmVal = pRCC->CR; |
| 164 | + rwmVal |= 0x01; |
| 165 | + pRCC->CR = rwmVal; |
| 166 | + } |
| 167 | + |
| 168 | + /* wait for it to come on */ |
| 169 | + while ((pRCC->CR & 0x02) == 0x00) { |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +bool writeChunk(uint32 *ptr, int size, const char *data) |
| 174 | +{ |
| 175 | + flashErasePage((uint32)(ptr)); |
| 176 | + |
| 177 | + for (int i = 0; i<size; i = i + 4) { |
| 178 | + if (!flashWriteWord((uint32)(ptr++), *((uint32 *)(data + i)))) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return true; |
| 184 | +} |
| 185 | + |
| 186 | +void setup() { |
| 187 | + pinMode(BOARD_LED_PIN, OUTPUT); |
| 188 | + digitalWrite(BOARD_LED_PIN, LOW); |
| 189 | + Serial.begin(9600); |
| 190 | + |
| 191 | + while (!(Serial.isConnected() && (Serial.getDTR() ))) |
| 192 | + { |
| 193 | + digitalWrite(BOARD_LED_PIN, !digitalRead(BOARD_LED_PIN)); |
| 194 | + delay(100); // fast blink |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +void loop() { |
| 199 | + digitalWrite(BOARD_LED_PIN, LOW);// turn off the led |
| 200 | + Serial.println ("*** This sketch will update the bootloader in the Maple Mini to the STM32duino bootloader ****"); |
| 201 | + Serial.println ("*** With this you can use up to 120KB of Flash and 20KB of RAM for a Sketch ****"); |
| 202 | + Serial.println ("*** Uploading is also considerably faster on OSX (and possibly faster on Linux) ****"); |
| 203 | + Serial.println (); |
| 204 | + Serial.println ("*** If you are not using a Maple mini. Do not continue ****"); |
| 205 | + Serial.println (); |
| 206 | + Serial.println ("*** WARNING. If the update fails your Maple mini may not have a functional bootloder. ****"); |
| 207 | + Serial.println ("*** YOU UPDATE AT YOUR OWN RISK ****"); |
| 208 | + Serial.println (); |
| 209 | + |
| 210 | + Serial.println ("While updating please do not remove the power."); |
| 211 | + Serial.println ("To confirm you , enter Y"); |
| 212 | + while (Serial.read() != 'Y') |
| 213 | + { |
| 214 | + delay(1); |
| 215 | + } |
| 216 | + Serial.println ("Proceeding with update, do not remove power."); |
| 217 | + |
| 218 | + bootloader = const_cast<char *>(maple_mini_boot20); |
| 219 | + |
| 220 | + |
| 221 | + setupFLASH(); |
| 222 | + flashUnlock(); |
| 223 | + |
| 224 | + int success = 1; |
| 225 | + int n = sizeof(bootloader); |
| 226 | + for (int i=0; i<n; i+=PAGE_SIZE) { |
| 227 | + int size = 0; |
| 228 | + uint32* chunk = (uint32 *)(BOOTLOADER_FLASH + i); |
| 229 | + |
| 230 | + size = n-i; |
| 231 | + if (size > PAGE_SIZE) size = PAGE_SIZE; |
| 232 | + |
| 233 | + if (!writeChunk(chunk, size, &bootloader[i])) { |
| 234 | + Serial.println (); |
| 235 | + Serial.println ("WARNING, Update Failed!! The sketch will restart in 3 seconds and you can try to flash again"); |
| 236 | + delay (3000); |
| 237 | + success = 0; |
| 238 | + break; |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + if (success){ |
| 243 | + flashLock(); |
| 244 | + Serial.println (); |
| 245 | + Serial.println ("Update completed successfully. Reboot now and replace this sketch"); |
| 246 | + while (1){ |
| 247 | + digitalWrite(BOARD_LED_PIN, LOW); |
| 248 | + delay(500); |
| 249 | + digitalWrite(BOARD_LED_PIN, HIGH); |
| 250 | + delay(500); |
| 251 | + } |
| 252 | + } |
| 253 | +} |
| 254 | + |
0 commit comments