_boot.py
1 import os 2 import sys 3 import time 4 import json 5 import machine 6 from machine import I2C 7 import axp202 8 9 sys.path.append('') 10 sys.path.append('.') 11 12 # chdir to "/sd" or "/flash" 13 devices = os.listdir("/") 14 if "sd" in devices: 15 os.chdir("/sd") 16 sys.path.append('/sd') 17 else: 18 os.chdir("/flash") 19 sys.path.append('/flash') 20 21 22 print("[MaixPy] init end") # for IDE 23 for i in range(200): 24 time.sleep_ms(1) # wait for key interrupt(for maixpy ide) 25 26 27 # check IDE mode 28 ide_mode_conf = "/flash/ide_mode.conf" 29 ide = True 30 try: 31 f = open(ide_mode_conf) 32 f.close() 33 except Exception: 34 ide = False 35 36 if ide: 37 os.remove(ide_mode_conf) 38 from machine import UART 39 import lcd 40 lcd.init(color=lcd.PINK) 41 repl = UART.repl_uart() 42 repl.init(1500000, 8, None, 1, read_buf_len=2048, ide=True, from_ide=False) 43 sys.exit() 44 45 46 ## initialize pmu 47 i2c = I2C(I2C.I2C0, freq = 400000, scl = 30, sda = 31) 48 p = None 49 try: 50 p = axp202.PMU(i2c, 0x35) 51 except: 52 p = axp202.PMU(i2c, 0x34) 53 else: 54 p.setShutdownTime(axp202.AXP202_SHUTDOWN_TIME_4S) 55 p.setLDO2Voltage(1800) 56 p.enablePower(axp202.AXP192_LDO2) 57 p.enablePower(6) 58 p.setLDO3Mode(1) 59 60 61 ## display banner 62 banner = ''' 63 __ __ _____ __ __ _____ __ __ 64 | \/ | /\ |_ _| \ \ / / | __ \ \ \ / / 65 | \ / | / \ | | \ V / | |__) | \ \_/ / 66 | |\/| | / /\ \ | | > < | ___/ \ / 67 | | | | / ____ \ _| |_ / . \ | | | | 68 |_| |_| /_/ \_\ |_____| /_/ \_\ |_| |_| 69 70 Co-op by Sipeed : https://www.sipeed.com 71 ''' 72 print(banner) 73 dirList = os.listdir() 74 75 76 # detect boot.py 77 if "boot.py" in dirList: 78 with open("boot.py") as f: 79 exec(f.read()) 80 else: 81 # detect boot.py 82 boot_py = ''' 83 try: 84 import os, Maix, gc, lcd, image 85 from fpioa_manager import fm 86 from Maix import FPIOA, GPIO 87 gc.collect() 88 lcd.init(freq=15000000) 89 fm.register(17,fm.fpioa.GPIO0) 90 led=GPIO(GPIO.GPIO0,GPIO.OUT) 91 led.value(1) 92 loading = image.Image(size=(lcd.width(), lcd.height())) 93 loading.draw_rectangle((0, 0, lcd.width(), lcd.height()), fill=True, color=(255, 0, 0)) 94 info = "Welcome to MaixPy" 95 loading.draw_string(int(lcd.width()//2 - len(info) * 5), (lcd.height())//4, info, color=(255, 255, 255), scale=2, mono_space=0) 96 v = sys.implementation.version 97 vers = 'V{}.{}.{} : maixpy.sipeed.com'.format(v[0],v[1],v[2]) 98 loading.draw_string(int(lcd.width()//2 - len(info) * 6), (lcd.height())//3 + 20, vers, color=(255, 255, 255), scale=1, mono_space=1) 99 lcd.display(loading) 100 del loading, v, info, vers 101 gc.collect() 102 finally: 103 gc.collect() 104 ''' 105 106 with open('boot.py', "wb") as f: 107 f.write(boot_py) 108 109 with open("boot.py") as f: 110 exec(f.read()) 111 112 113 ## detect config.json 114 config = { 115 "type": "twatch", 116 "lcd": { 117 "height": 240, 118 "width": 240, 119 "invert": 1, 120 "dir": 96, 121 "lcd_type": 0 122 }, 123 "board_info": { 124 "BOOT_KEY": 16, 125 "LED_R": 12, 126 "LED_G": 13, 127 "LED_B": 14, 128 "WIFI_TX": 6, 129 "WIFI_RX": 7, 130 "WIFI_EN": 8, 131 "I2S0_MCLK": 13, 132 "I2S0_SCLK": 21, 133 "I2S0_WS": 18, 134 "I2S0_IN_D0": 35, 135 "I2S0_OUT_D2": 34, 136 "SPI0_MISO": 26, 137 "SPI0_CLK": 27, 138 "SPI0_MOSI": 28, 139 "SPI0_CS0": 29, 140 "MIC0_WS": 30, 141 "MIC0_DATA": 31, 142 "MIC0_BCK": 32, 143 "I2S_WS": 33, 144 "I2S_DA": 34, 145 "I2S_BCK": 35 146 } 147 } 148 149 try: 150 with open('config.json', 'rb') as f: 151 tmp = json.loads(f.read()) 152 if tmp["type"] != config["type"]: 153 raise Exception('config.json does not match') 154 sys.exit() 155 except Exception as e: 156 print('config.json no exist') 157 print('Generating default config.json') 158 159 with open('config.json', "w") as f: 160 cfg = json.dumps(config) 161 f.write(cfg) 162 163 print('config.json has been generated') 164 print('restarting') 165 time.sleep_ms(100) 166 167 import machine 168 machine.reset()