Skip to content

Commit ac4b72e

Browse files
committed
Added usqlite3 module
some bug fixes and improvements if file system drivers - added 'trim' method to LittleFS
1 parent b058b1e commit ac4b72e

39 files changed

+240584
-1396
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Based on *kendryte-freertos-sdk* (modified to include some needed features not y
2222
* **Display** module ported from my [MicroPython for ESP32]
2323
(https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo/wiki/display) display module
2424
* Full **network** support for **WiFi** (based on ESP8266/ESP8285) and **GSM** modules.
25+
* **sqlite3** support
2526
* **pye**, full screen file editor (as python frozen module) is provided
2627
* **MPyTerm**, serial terminal emulator specially designed for MicroPython is provided.<br>
2728
Included are commands to syncronize MicroPython time from PC, change the REPL baudrate, transfer files to/from MicroPython filesystem (including transfering whole directories).<br>Fast block file transfer is used, protected with CRC.
@@ -54,8 +55,8 @@ You will not be able to use `git pull` to update the repository, but otherwise t
5455
## Build the MicroPython firmware
5556
<br>
5657

57-
> *Kendryte toolchain will be automatically downloaded and unpacked on* **first run** *of* **BUILD.sh** *script. It can take some time on slow Internet connection (~32 MB will be downloaded).*<br>
58-
> *kendryte-freertos-sdk* is included in the repository
58+
> _**Kendryte toolchain** will be automatically downloaded and unpacked on **first run** of **BUILD.sh** script.<br> It can take some time on slow Internet connection (~32 MB will be downloaded)._<br>
59+
> _**kendryte-freertos-sdk** is included in the repository._
5960
6061

6162
Change the working directory to **MicroPython_K210_LoBo/k210-freertos**.
@@ -65,7 +66,7 @@ To build the MicroPython firmware, run:
6566
./BUILD.sh
6667
```
6768

68-
To flush the built firmware to your K210 board, run:
69+
To flash the built firmware to your K210 board, run:
6970
```
7071
./kflash.py -p /dev/ttyUSB0 -b 2000000 -t MaixPy.bin
7172
```
@@ -74,7 +75,7 @@ Change */dev/ttyUSB0* to the port used to connect to the your board if needed.
7475

7576
---
7677

77-
```
78+
```console
7879
M (1371) [MAIXPY]: Configuration loaded from flash
7980
M (9298) [MAIXPY]: Heaps: FreeRTOS=3840 KB (1196 KB free), MPy=2560 KB, other=379 KB
8081

firmware/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# MicroPython for Kendryte K210
22

33

4-
To flush the pre-built firmware to your K210 board, run (in this directory):
4+
To flash the pre-built firmware to your K210 board, run (in this directory):
55

66
```
77
./kflash.py -p /dev/ttyUSB0 -b 2000000 -t MaixPy.bin

firmware/wifi/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ESP8266 AT FIRMWARE
2+
3+
4+
For detailed instructions about flashing the WiFi firmware see README.md in **flash** directory.
5+

firmware/wifi/flash/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The latest stable _esptool.py_ release can be installed from _pypi_ via pip:
4040
pip install esptool
4141
```
4242

43-
The initial firmware must be flashed to the ESP8266/ESP8285.<br>
43+
The **initial firmware** must be **flashed** to the ESP8266/ESP8285.<br>
4444
Bash script `flash.sh` is provided to make the flashing as easy as possible.<br>
4545
The correct `flash.sh` options must be specified, depending on the Flash type used:<br>
4646
Change the working directory to `this directory` and run `flash.sh`:<br>
@@ -83,15 +83,15 @@ Flash to ESP8266, 512KB, single firmware, no OTA:
8383
![Sipeed_DanDock](Sipeed_DanDock_wifi.jpg)
8484

8585

86-
1 Connect some _USBtoSerial_ adapter to the board, connetc adapter's Tx to the Rx pin on the image, adapter's Rx to the Tx pin on the image
87-
1 Connect the **ESP8285 boot** pin (the M1 module's corner pin near the WiFi antenna) to the ground before powering the the board, after power on, the pin can be released
88-
1 Power on the board (connect USB cable to the board's USB-C connector)
89-
1 Execute `.\flash.sh -eo -t 1MB -m DOUT` to erase the existing firmware
90-
1 Power off the board (disconnect USB cable from the board's USB-C connector)
91-
1 Connect the **ESP8285 boot** pin to the ground before powering the the board, after power on, the pin can be released
92-
1 Power on the board (connect USB cable to the board's USB-C connector)
93-
1 Execute `.\flash.sh -t 1MB -m DOUT` to flash the new firmware
94-
1 Power off the board
86+
1. Connect some _USBtoSerial_ adapter to the board, connetc adapter's Tx to the Rx pin on the image, adapter's Rx to the Tx pin on the image
87+
1. Connect the **ESP8285 boot** pin (the M1 module's corner pin near the WiFi antenna) to the ground before powering the the board, after power on, the pin can be released
88+
1. Power on the board (connect USB cable to the board's USB-C connector)
89+
1. Execute `.\flash.sh -eo -t 1MB -m DOUT` to erase the existing firmware
90+
1. Power off the board (disconnect USB cable from the board's USB-C connector)
91+
1. Connect the **ESP8285 boot** pin to the ground before powering the the board, after power on, the pin can be released
92+
1. Power on the board (connect USB cable to the board's USB-C connector)
93+
1. Execute `.\flash.sh -t 1MB -m DOUT` to flash the new firmware
94+
1. Power off the board
9595

9696
After booting to MicroPython, test the WiFi functionality:
9797

k210-freertos/MPyTerm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def EnterRawREPL(self, imprt, cmd, bdr=0):
130130
self.ReceiverToStdout = False
131131
time.sleep(0.1)
132132
dummy = self.uart.read()
133-
self.uart.timeout = 2
133+
self.uart.timeout = 4
134134
# enter raw REPL
135135
self.uart.write(b'\x01')
136136
resp = self.uart.read(28)

k210-freertos/main.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
#include <filesystem.h>
8484
#endif
8585

86+
// === FreeRTOS heap ===
87+
size_t configTOTAL_HEAP_SIZE = FREE_RTOS_TOTAL_HEAP_SIZE;
88+
// This is used by 'heap_4.c'
89+
uint8_t __attribute__((aligned(8))) ucHeap[FREE_RTOS_TOTAL_HEAP_SIZE];
90+
8691
static void *sys_rambuf;
8792

8893
extern handle_t mp_rtc_rtc0;
@@ -113,6 +118,7 @@ static bool task0_started = false;
113118
static bool flash_fs_ok = false;
114119
static bool exec_boot_py = true;
115120
static bool exec_main_py = true;
121+
static bool use_default_config = false;
116122

117123
//----------------------------
118124
size_t _check_remaining_heap()
@@ -422,8 +428,7 @@ int main()
422428
filesystem_rtc = mp_rtc_rtc0;
423429
#endif
424430

425-
sysctl_t sysctl __attribute__((unused));
426-
sysctl.peri.jtag_clk_bypass = 1;
431+
sysctl->peri.jtag_clk_bypass = 1;
427432

428433
// ==== Initialize GPIOHS for boot menu pin check====
429434
if (mpy_config.config.boot_menu_pin) {
@@ -444,13 +449,15 @@ int main()
444449
char sel_f = ' ';
445450
char sel_b = ' ';
446451
char sel_m = ' ';
452+
char sel_d = ' ';
447453
// boot menu pin is low, display the menu
448454
start_menu:
449455
timeout = mp_hal_ticks_ms() + 10000;
450456
mp_printf(&mp_plat_print, LOG_BOLD(LOG_COLOR_CYAN)"\nBoot menu, select an option\n"LOG_RESET_COLOR);
451457
mp_printf(&mp_plat_print, "%c"LOG_BOLD(LOG_COLOR_BROWN)"F"LOG_RESET_COLOR" - force format File system\n", sel_f);
452458
mp_printf(&mp_plat_print, "%c"LOG_BOLD(LOG_COLOR_BROWN)"B"LOG_RESET_COLOR" - do not execute 'boot.py'\n", sel_b);
453459
mp_printf(&mp_plat_print, "%c"LOG_BOLD(LOG_COLOR_BROWN)"M"LOG_RESET_COLOR" - do not execute 'main.py'\n", sel_m);
460+
mp_printf(&mp_plat_print, "%c"LOG_BOLD(LOG_COLOR_BROWN)"D"LOG_RESET_COLOR" - use default configuration\n", sel_d);
454461
mp_printf(&mp_plat_print, LOG_BOLD(LOG_COLOR_RED)" Q"LOG_RESET_COLOR" - exit the menu\n");
455462
while (1) {
456463
if (mp_hal_ticks_ms() > timeout) break;
@@ -473,10 +480,25 @@ int main()
473480
sel_f = '*';
474481
goto start_menu;
475482
}
483+
else if ((key == 'd') || (key == 'D')) {
484+
use_default_config = true;
485+
sel_d = '*';
486+
goto start_menu;
487+
}
476488
}
477489
}
478490
}
479491

492+
if (use_default_config) {
493+
vTaskDelay(2);
494+
mpy_config_set_default();
495+
vTaskDelay(2);
496+
sysctl->soft_reset.soft_reset = 1;
497+
while (1) {
498+
;
499+
}
500+
}
501+
480502
// ======================================
481503
// ==== Allocate MicroPython heap(s) ====
482504
// ======================================
@@ -495,11 +517,11 @@ int main()
495517
configASSERT(mp_task_pystack2);
496518
}
497519
LOGM(TAG_MAIN, "Heaps: FreeRTOS=%lu KB (%lu KB free), MPy_1=%u KB, MPy_2=%u KB, other=%lu KB",
498-
configTOTAL_HEAP_SIZE/1024, xPortGetFreeHeapSize()/1024, mpy_config.config.heap_size1/1024, mpy_config.config.heap_size2/1024, _check_remaining_heap()/1024);
520+
FREE_RTOS_TOTAL_HEAP_SIZE/1024, xPortGetFreeHeapSize()/1024, mpy_config.config.heap_size1/1024, mpy_config.config.heap_size2/1024, _check_remaining_heap()/1024);
499521
}
500522
else {
501523
LOGM(TAG_MAIN, "Heaps: FreeRTOS=%lu KB (%lu KB free), MPy=%u KB, other=%lu KB",
502-
configTOTAL_HEAP_SIZE/1024, xPortGetFreeHeapSize()/1024, mpy_config.config.heap_size1/1024, _check_remaining_heap()/1024);
524+
FREE_RTOS_TOTAL_HEAP_SIZE/1024, xPortGetFreeHeapSize()/1024, mpy_config.config.heap_size1/1024, _check_remaining_heap()/1024);
503525
}
504526

505527
// ======================================================

k210-freertos/mpy_support/modules/upysh.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import os
33
import time
4+
import gc
45

56
class LS:
67

@@ -99,6 +100,7 @@ def __call__(self, srcf, destf):
99100
return
100101
try:
101102
st = os.stat(srcf)
103+
src_size = st[6]
102104
except:
103105
print("Source not found", srcf)
104106
return
@@ -117,19 +119,48 @@ def __call__(self, srcf, destf):
117119
print("Error opening '{}'".format(destf))
118120
return
119121
try:
122+
size = src_size
123+
wrsize = 0
124+
n_try = 0
125+
buf = bytearray(4096)
120126
while True:
121-
buf = fs.read(1024)
122-
if len(buf) == 0:
123-
break
127+
try:
128+
x = fs.readinto(buf)
129+
except:
130+
print("Read Error during copy at {}".format(size))
131+
n_try += 1
132+
if n_try > 3:
133+
try:
134+
fd.close()
135+
fs.close()
136+
os.remove(destf)
137+
except:
138+
pass
139+
return
140+
time.sleep_ms(300)
141+
continue
142+
n_try = 0
143+
wrsize += x
144+
size -= x
145+
if x < len(buf) :
146+
if x > 0:
147+
buf = memoryview(buf)[:x]
148+
fd.write(buf)
124149
else:
125150
fd.write(buf)
126-
except:
151+
print("{} of {}".format(wrsize, src_size), end='\r')
152+
if size <= 0:
153+
print("")
154+
break
155+
except Exception as err:
127156
try:
157+
fs.close()
128158
fd.close()
129159
os.remove(destf)
130160
except:
131161
pass
132-
print("Error during copy")
162+
print("Error during copy at {} [{}]".format(size, err))
163+
return
133164
fd.close()
134165
fs.close()
135166
except:

0 commit comments

Comments
 (0)