blob: b14de573a76d0c6d9c682ada872780f9999e48e3 [file] [log] [blame]
Paul Sokolovsky20d02712016-10-18 15:18:07 +03001/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2016 Paul Sokolovsky
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <stdio.h>
28#include "ets_sys.h"
29#include "etshal.h"
30#include "esp_mphal.h"
31#include "user_interface.h"
32#include "extmod/misc.h"
33
Paul Sokolovsky20d02712016-10-18 15:18:07 +030034NORETURN void call_user_start(void);
35void ets_printf(const char *fmt, ...);
36extern char flashchip;
37
38static const uint8_t default_init_data[] __attribute__((aligned(4))) = {
390x05, 0x00, 0x04, 0x02, 0x05, 0x05, 0x05, 0x02, 0x05, 0x00, 0x04, 0x05, 0x05, 0x04, 0x05, 0x05,
400x04, 0xfe, 0xfd, 0xff, 0xf0, 0xf0, 0xf0, 0xe0, 0xe0, 0xe0, 0xe1, 0x0a, 0xff, 0xff, 0xf8, 0x00,
410xf8, 0xf8, 0x52, 0x4e, 0x4a, 0x44, 0x40, 0x38, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 0x04, 0x05,
420x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
430xe1, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x93, 0x43, 0x00, 0x00, 0x00,
440x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
450x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
460x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
47};
48
49void firmware_start(void) {
50 // For SDK 1.5.2, either address has shifted and not mirrored in
51 // eagle.rom.addr.v6.ld, or extra initial member was added.
52 SpiFlashChip *flash = (SpiFlashChip*)(&flashchip + 4);
53
54 char buf[128];
55 SPIRead(flash->chip_size - 4 * 0x1000, buf, sizeof(buf));
56 /*for (int i = 0; i < sizeof(buf); i++) {
57 static char hexf[] = "%x ";
58 ets_printf(hexf, buf[i]);
59 }*/
60
61 bool inited = false;
62 for (int i = 0; i < sizeof(buf); i++) {
63 if (buf[i] != 0xff) {
64 inited = true;
65 break;
66 }
67 }
68
69 if (!inited) {
70 static char msg[] = "Writing init data\n";
71 ets_printf(msg);
72 SPIRead((uint32_t)&default_init_data - 0x40200000, buf, sizeof(buf));
73 SPIWrite(flash->chip_size - 4 * 0x1000, buf, sizeof(buf));
74 }
75
76 asm("j call_user_start");
77}