_boot.py
  1  import os, sys, time
  2  
  3  sys.path.append('')
  4  sys.path.append('.')
  5  
  6  # chdir to "/sd" or "/flash"
  7  devices = os.listdir("/")
  8  if "sd" in devices:
  9      os.chdir("/sd")
 10      sys.path.append('/sd')
 11  else:
 12      os.chdir("/flash")
 13  sys.path.append('/flash')
 14  del devices
 15  
 16  print("[MaixPy] init end") # for IDE
 17  for i in range(200):
 18      time.sleep_ms(1) # wait for key interrupt(for maixpy ide)
 19  del i
 20  
 21  # check IDE mode
 22  ide_mode_conf = "/flash/ide_mode.conf"
 23  ide = True
 24  try:
 25      f = open(ide_mode_conf)
 26      f.close()
 27      del f
 28  except Exception:
 29      ide = False
 30  
 31  if ide:
 32      os.remove(ide_mode_conf)
 33      from machine import UART
 34      import lcd
 35      lcd.init(color=lcd.PINK)
 36      repl = UART.repl_uart()
 37      repl.init(1500000, 8, None, 1, read_buf_len=2048, ide=True, from_ide=False)
 38      sys.exit()
 39  del ide, ide_mode_conf
 40  
 41  # detect boot.py
 42  main_py = '''
 43  try:
 44      import gc, lcd, image
 45      gc.collect()
 46      lcd.init()
 47      loading = image.Image(size=(lcd.width(), lcd.height()))
 48      loading.draw_rectangle((0, 0, lcd.width(), lcd.height()), fill=True, color=(255, 0, 0))
 49      info = "Welcome to MaixPy"
 50      loading.draw_string(int(lcd.width()//2 - len(info) * 5), (lcd.height())//4, info, color=(255, 255, 255), scale=2, mono_space=0)
 51      v = sys.implementation.version
 52      vers = 'V{}.{}.{} : maixpy.sipeed.com'.format(v[0],v[1],v[2])
 53      loading.draw_string(int(lcd.width()//2 - len(info) * 6), (lcd.height())//3 + 20, vers, color=(255, 255, 255), scale=1, mono_space=1)
 54      lcd.display(loading)
 55      del loading, v, info, vers
 56      gc.collect()
 57  finally:
 58      gc.collect()
 59  '''
 60  
 61  flash_ls = os.listdir()
 62  if not "main.py" in flash_ls:
 63      f = open("main.py", "wb")
 64      f.write(main_py)
 65      f.close()
 66      del f
 67  del main_py
 68  
 69  flash_ls = os.listdir("/flash")
 70  try:
 71      sd_ls = os.listdir("/sd")
 72  except Exception:
 73      sd_ls = []
 74  if "cover.boot.py" in sd_ls:
 75      code0 = ""
 76      if "boot.py" in flash_ls:
 77          with open("/flash/boot.py") as f:
 78              code0 = f.read()
 79      with open("/sd/cover.boot.py") as f:
 80          code=f.read()
 81      if code0 != code:
 82          with open("/flash/boot.py", "w") as f:
 83              f.write(code)
 84          import machine
 85          machine.reset()
 86  
 87  if "cover.main.py" in sd_ls:
 88      code0 = ""
 89      if "main.py" in flash_ls:
 90          with open("/flash/main.py") as f:
 91              code0 = f.read()
 92      with open("/sd/cover.main.py") as f:
 93          code = f.read()
 94      if code0 != code:
 95          with open("/flash/main.py", "w") as f:
 96              f.write(code)
 97          import machine
 98          machine.reset()
 99  
100  try:
101      del flash_ls
102      del sd_ls
103      del code0
104      del code
105  except Exception:
106      pass
107  
108  banner = '''
109   __  __              _____  __   __  _____   __     __
110  |  \/  |     /\     |_   _| \ \ / / |  __ \  \ \   / /
111  | \  / |    /  \      | |    \ V /  | |__) |  \ \_/ /
112  | |\/| |   / /\ \     | |     > <   |  ___/    \   /
113  | |  | |  / ____ \   _| |_   / . \  | |         | |
114  |_|  |_| /_/    \_\ |_____| /_/ \_\ |_|         |_|
115  
116  Official Site : https://www.sipeed.com
117  Wiki          : https://maixpy.sipeed.com
118  '''
119  print(banner)
120  del banner
121  
122  try:
123    from machine import I2C
124    axp173 = I2C(I2C.I2C3, freq=100000, scl=24, sda=27)
125    axp173.writeto_mem(0x34, 0x27, 0x20, mem_size=8)
126    axp173.writeto_mem(0x34, 0x28, 0x0C, mem_size=8)
127    axp173.writeto_mem(0x34, 0x36, 0xCC, mem_size=8)
128    del axp173
129  except Exception as e:
130    print(e)
131  
132  import json
133  
134  config = {
135  "type": "amigo_tft",
136  "lcd": {
137      "height": 320,
138      "width": 480,
139      "invert": 0,
140      "dir": 40,
141      "lcd_type": 1
142  },
143  "sdcard":{
144      "sclk":11,
145      "mosi":10,
146      "miso":6,
147      "cs":26
148  },
149  "board_info": {
150      'BOOT_KEY': 23,
151      'LED_R': 14,
152      'LED_G': 15,
153      'LED_B': 17,
154      'LED_W': 32,
155      'BACK': 31,
156      'ENTER': 23,
157      'NEXT': 20,
158      'WIFI_TX': 6,
159      'WIFI_RX': 7,
160      'WIFI_EN': 8,
161      'I2S0_MCLK': 13,
162      'I2S0_SCLK': 21,
163      'I2S0_WS': 18,
164      'I2S0_IN_D0': 35,
165      'I2S0_OUT_D2': 34,
166      'I2C_SDA': 27,
167      'I2C_SCL': 24,
168      'SPI_SCLK': 11,
169      'SPI_MOSI': 10,
170      'SPI_MISO': 6,
171      'SPI_CS': 12,
172  }
173  }
174  
175  cfg = json.dumps(config)
176  print(cfg)
177  
178  try:
179      with open('/flash/config.json', 'rb') as f:
180          tmp = json.loads(f.read())
181      # print(tmp)
182      if tmp["type"] != config["type"]:
183          raise Exception('config.json no exist')
184  except Exception as e:
185      with open('/flash/config.json', "w") as f:
186          f.write(cfg)
187      import machine
188      machine.reset()