Skip to content

Commit 7186f5f

Browse files
authored
Merge pull request #35 from maejima-fumika/fix-bugs
Fix bugs and add other changes.
2 parents 4e18a92 + 81332e2 commit 7186f5f

File tree

42 files changed

+1532
-261
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1532
-261
lines changed

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ services:
88
volumes:
99
- ./server:/bluescript/server
1010
- /bluescript/server/node_modules
11-
- /bluescript/server/temp-files
11+
# - /bluescript/server/temp-files
1212
- ./modules:/bluescript/modules
1313
- ./microcontroller:/bluescript/microcontroller
1414
working_dir: /bluescript/server

microcontroller/ports/esp32/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
# CMakeLists in this exact order for cmake to work correctly
55
cmake_minimum_required(VERSION 3.16)
66

7-
set(EXTRA_COMPONENT_DIRS "../../core" "../../../modules/std" "../../../modules/gpio" "../../../modules/timer")
7+
set(EXTRA_COMPONENT_DIRS
8+
"../../core"
9+
"../../../modules/std"
10+
"../../../modules/gpio"
11+
"../../../modules/timer"
12+
"../../../modules/display"
13+
"../../../modules/button"
14+
)
815

916
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
1017
project(bluescript)

microcontroller/ports/esp32/main/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
idf_component_register(SRCS "main.c" "ble.c" "shell.c" "logger.c" "event.c"
1+
idf_component_register(SRCS "main.c" "ble.c" "shell.c" "logger.c"
22
INCLUDE_DIRS "include"
33
LDFRAGMENTS linker.lf
44
WHOLE_ARCHIVE)

microcontroller/ports/esp32/main/include/cmd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ typedef enum {
1616
BS_CMD_RESULT_EXECTIME,
1717
BS_CMD_RESULT_PROFILE,
1818

19+
BS_CMD_CALL_EVENT,
20+
1921
// Sentinel
2022
BS_CMD_END
2123
} bs_cmd_t;

microcontroller/ports/esp32/main/include/shell.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <stdlib.h>
66
#include <string.h>
77

8+
#define PORT_TEXT_SECTION __attribute__((section(".port_text")))
9+
810
/**
911
* Set data.
1012
*/
@@ -17,6 +19,20 @@ void bs_shell_receptionist(uint8_t *task_data, int data_len);
1719
void bs_shell_register_sender(void (* sender)(uint8_t*, uint32_t));
1820

1921

22+
23+
24+
/**
25+
* Push a callback function event to the event queue.
26+
*/
27+
void PORT_TEXT_SECTION bs_event_push(void *callback);
28+
29+
30+
/**
31+
* Push a callback function event to the event queue from isr.
32+
*/
33+
void PORT_TEXT_SECTION bs_event_push_from_isr(void *callback);
34+
35+
2036
/**
2137
* A task for execute code.
2238
* It pops an entry-point from the execution queue and jupts to it.

microcontroller/ports/esp32/main/logger.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "esp_log.h"
33
#include "freertos/FreeRTOS.h"
44
#include "freertos/semphr.h"
5+
#include "freertos/semphr.h"
6+
#include "esp_timer.h"
57

68
#include "include/logger.h"
79
#include "include/cmd.h"

microcontroller/ports/esp32/main/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void app_main(void) {
1515
bs_logger_register_sender(bs_ble_send_notification);
1616
bs_shell_register_sender(bs_ble_send_notification);
1717

18-
xTaskCreatePinnedToCore(bs_shell_task, "bs_shell_task", 4096 * 16, NULL, 1, NULL, 0);
18+
xTaskCreatePinnedToCore(bs_shell_task, "bs_shell_task", 4096 * 16, NULL, 5, NULL, 0);
1919
xTaskCreatePinnedToCore(bs_logger_task, "bs_logger_task", 4096, NULL, 1, NULL, 0);
20-
xTaskCreatePinnedToCore(bs_event_handler_task, "bs_event_handler_task", 4096, NULL, 1, NULL, 0);
20+
// xTaskCreatePinnedToCore(bs_event_handler_task, "bs_event_handler_task", 4096, NULL, 5, NULL, 0);
2121
}

microcontroller/ports/esp32/main/shell.c

Lines changed: 96 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ typedef union {
2121

2222
struct task_jump {
2323
bs_cmd_t cmd;
24+
int32_t id;
2425
uint32_t to;
2526
} jump;
27+
28+
struct task_event_call {
29+
bs_cmd_t cmd;
30+
value_t fn;
31+
} event_call;
2632
} task_item_u;
2733

2834
static QueueHandle_t task_queue;
@@ -37,21 +43,25 @@ uint32_t *dram;
3743
size_t dram_size;
3844

3945
// variables for flash partition
40-
#define TEXT_PARTITION_LABEL "text"
41-
static esp_partition_t *text_partition = NULL;
42-
static uint8_t *virtual_flash_ptr = NULL;
43-
static esp_partition_mmap_handle_t virtual_flash_hdlr;
46+
#define IFLASH_PARTITION_LABEL "iflash"
47+
#define DFLASH_PARTITION_LABEL "dflash"
48+
static esp_partition_t *iflash_partition = NULL;
49+
static esp_partition_t *dflash_partition = NULL;
50+
static uint8_t *virtual_iflash_ptr = NULL;
51+
static uint8_t *virtual_dflash_ptr = NULL;
52+
static esp_partition_mmap_handle_t virtual_iflash_hdlr;
53+
static esp_partition_mmap_handle_t virtual_dflash_hdlr;
4454

4555

4656
// RAM
4757

4858
static void ram_init() {
4959
iram_size = heap_caps_get_largest_free_block(MALLOC_CAP_EXEC | MALLOC_CAP_32BIT) - 4;
5060
iram = heap_caps_malloc(iram_size, MALLOC_CAP_EXEC | MALLOC_CAP_32BIT);
51-
dram_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
61+
dram_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT) / 2;
5262
dram = heap_caps_malloc(dram_size, MALLOC_CAP_8BIT);
53-
ESP_LOGI(BS_SHELL_TAG, "IRAM Size: %d\n", iram_size);
54-
ESP_LOGI(BS_SHELL_TAG, "DRAM Size: %d\n", dram_size);
63+
ESP_LOGI(BS_SHELL_TAG, "IRAM Address: %p Size: %d\n", iram, (int)iram_size);
64+
ESP_LOGI(BS_SHELL_TAG, "DRAM Address: %p Size: %d\n", dram, (int)dram_size);
5565
}
5666

5767
static void ram_reset() {
@@ -65,33 +75,48 @@ static void ram_memcpy(uint8_t* ram_dest, uint8_t *src, size_t len) {
6575

6676

6777
// Flash
78+
static void dflash_memcpy(uint8_t* dest, uint8_t *src, size_t len);
6879

6980
static void flash_init() {
70-
text_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, TEXT_PARTITION_LABEL);
71-
ESP_ERROR_CHECK(esp_partition_erase_range(text_partition, 0, text_partition->size));
72-
esp_partition_mmap(text_partition, 0, text_partition->size, ESP_PARTITION_MMAP_INST, &virtual_flash_ptr, &virtual_flash_hdlr);
81+
iflash_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, IFLASH_PARTITION_LABEL);
82+
dflash_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, DFLASH_PARTITION_LABEL);
83+
esp_partition_mmap(iflash_partition, 0, iflash_partition->size, ESP_PARTITION_MMAP_INST, &virtual_iflash_ptr, &virtual_iflash_hdlr);
84+
esp_partition_mmap(dflash_partition, 0, dflash_partition->size, ESP_PARTITION_MMAP_DATA, &virtual_dflash_ptr, &virtual_dflash_hdlr);
85+
ESP_LOGI(BS_SHELL_TAG, "IFlash Address: 0x%x VAddress: %p Size: %d\n", (int)iflash_partition->address, virtual_iflash_ptr, (int)iflash_partition->size);
86+
ESP_LOGI(BS_SHELL_TAG, "DFlash Address: 0x%x VAddress: %p Size: %d\n", (int)dflash_partition->address, virtual_dflash_ptr, (int)dflash_partition->size);
87+
}
88+
89+
static uint32_t iflash_read_address() {
90+
return (uint32_t)virtual_iflash_ptr;
7391
}
7492

75-
static void flash_reset() {
76-
ESP_ERROR_CHECK(esp_partition_erase_range(text_partition, 0, text_partition->size));
93+
static uint32_t dflash_read_address() {
94+
return (uint32_t)virtual_dflash_ptr;
7795
}
7896

79-
static uint32_t flash_read_address() {
80-
return (uint32_t)virtual_flash_ptr;
97+
static uint32_t iflash_read_size() {
98+
return (uint32_t)iflash_partition->size;
8199
}
82100

83-
static uint32_t flash_read_size() {
84-
return (uint32_t)text_partition->size;
101+
static uint32_t dflash_read_size() {
102+
return (uint32_t)dflash_partition->size;
85103
}
86104

87-
static void flash_memcpy(uint8_t* flash_dest, uint8_t *src, size_t len) {
88-
uint8_t* offset = flash_dest - virtual_flash_ptr;
89-
ESP_ERROR_CHECK(esp_partition_write(text_partition, offset, src, len));
105+
static void iflash_memcpy(uint8_t* dest, uint8_t *src, size_t len) {
106+
uint8_t* offset = dest - virtual_iflash_ptr;
107+
ESP_ERROR_CHECK(esp_partition_write(iflash_partition, offset, src, len));
108+
}
109+
110+
static void dflash_memcpy(uint8_t* dest, uint8_t *src, size_t len) {
111+
uint8_t* offset = dest - virtual_dflash_ptr;
112+
ESP_ERROR_CHECK(esp_partition_write(dflash_partition, offset, src, len));
90113
}
91114

92115
static void bs_memcpy(uint8_t* dest, uint8_t *src, size_t len) {
93-
if (virtual_flash_ptr <= dest && dest <= virtual_flash_ptr + flash_read_size()) {
94-
flash_memcpy(dest, src, len);
116+
if (virtual_iflash_ptr <= dest && dest <= virtual_iflash_ptr + iflash_read_size()) {
117+
iflash_memcpy(dest, src, len);
118+
} else if (virtual_dflash_ptr <= dest && dest <= virtual_dflash_ptr + dflash_read_size()) {
119+
dflash_memcpy(dest, src, len);
95120
} else {
96121
ram_memcpy(dest, src, len);
97122
}
@@ -111,19 +136,20 @@ void bs_shell_receptionist(uint8_t *task_data, int data_len) {
111136
{
112137
uint32_t address = *(uint32_t*)(task_data + (idx+1));
113138
uint32_t size = *(uint32_t*)(task_data + (idx+5));
114-
ESP_LOGI(BS_SHELL_TAG, "Load %d bytes to %x", (int)size, (int)address);
139+
ESP_LOGI(BS_SHELL_TAG, "Load %d bytes to %x, time: %d", (int)size, (int)address, (int)esp_timer_get_time());
115140
bs_memcpy(address, task_data + (idx+9), size);
116141
idx += (9 + size);
117142
break;
118143
}
119144
case BS_CMD_JUMP:
120-
// | cmd(1byte) | address(4byte) |
145+
// | cmd(1byte) | id(4byte) | address(4byte) |
121146
{
122147
task_item_u task;
123148
task.jump.cmd = BS_CMD_JUMP;
124-
task.jump.to = *(uint32_t*)(task_data + (idx+1));
149+
task.jump.id = *(uint32_t*)(task_data + (idx+1));
150+
task.jump.to = *(uint32_t*)(task_data + (idx+5));
125151
xQueueSend(task_queue, &task, portMAX_DELAY);
126-
idx += 5;
152+
idx += 9;
127153
break;
128154
}
129155
case BS_CMD_RESET:
@@ -163,36 +189,71 @@ static void send_result_meminfo() {
163189
*(uint32_t*)(result+ 5) = iram_size;
164190
*(uint32_t*)(result+ 9) = dram;
165191
*(uint32_t*)(result+13) = dram_size;
166-
*(uint32_t*)(result+17) = flash_read_address();
167-
*(uint32_t*)(result+21) = flash_read_size();
168-
result_sender(result, 25);
192+
*(uint32_t*)(result+17) = iflash_read_address();
193+
*(uint32_t*)(result+21) = iflash_read_size();
194+
*(uint32_t*)(result+25) = dflash_read_address();
195+
*(uint32_t*)(result+29) = dflash_read_size();
196+
result_sender(result, 33);
169197
}
170198

171-
static void send_result_exectime(float exectime) {
172-
uint8_t result[5];
199+
static void send_result_exectime(int32_t id, float exectime) {
200+
uint8_t result[9];
173201
result[0] = BS_CMD_RESULT_EXECTIME;
174-
*(float*)(result+1) = exectime;
175-
result_sender(result, 5);
202+
*(int32_t*)(result+1) = id;
203+
*(float*)(result+5) = exectime;
204+
result_sender(result, 9);
205+
}
206+
207+
void execute_installed_code() {
208+
if (virtual_dflash_ptr[0] != 1) {
209+
ESP_ERROR_CHECK(esp_partition_erase_range(iflash_partition, 0, iflash_partition->size));
210+
ESP_ERROR_CHECK(esp_partition_erase_range(dflash_partition, 0, dflash_partition->size));
211+
} else {
212+
puts("I am execute_installed_code");
213+
uint32_t length = *(uint32_t*)(virtual_dflash_ptr + 1);
214+
bs_shell_receptionist(virtual_dflash_ptr + 5, length);
215+
}
216+
}
217+
218+
// Event
219+
void bs_event_push(void* event_fn) {
220+
task_item_u task;
221+
task.event_call.cmd = BS_CMD_CALL_EVENT;
222+
task.event_call.fn = event_fn;
223+
xQueueSend(task_queue, &task, portMAX_DELAY);
176224
}
177225

178226

227+
void bs_event_push_from_isr(void* event_fn) {
228+
portBASE_TYPE yield = pdFALSE;
229+
task_item_u task;
230+
task.event_call.cmd = BS_CMD_CALL_EVENT;
231+
task.event_call.fn = event_fn;
232+
xQueueSendFromISR(task_queue, &task, &yield);
233+
}
234+
179235
void bs_shell_task(void *arg) {
180236
task_item_u task_item;
181237
task_queue = xQueueCreate(TASK_QUEUE_LENGTH, sizeof(task_item_u));
182238
shell_init();
183-
shell_reset();
239+
// shell_reset();
240+
execute_installed_code();
184241

185242
while (true) {
186243
xQueueReceive(task_queue, &task_item, portMAX_DELAY);
187244

188245
switch (task_item.cmd) {
189246
case BS_CMD_JUMP:
190-
ESP_LOGI(BS_SHELL_TAG, "Jump to %x", (int)task_item.jump.to);
247+
ESP_LOGI(BS_SHELL_TAG, "Jump to %x, id: %d", (int)task_item.jump.to, (int)task_item.jump.id);
191248
uint32_t start_us = esp_timer_get_time();
192249
try_and_catch((void *)task_item.jump.to);
193250
uint32_t end_us = esp_timer_get_time();
194251
float exectime = (float)(end_us - start_us) / 1000.0;
195-
send_result_exectime(exectime);
252+
send_result_exectime(task_item.jump.id, exectime);
253+
break;
254+
case BS_CMD_CALL_EVENT:
255+
ESP_LOGI(BS_SHELL_TAG, "CALL_EVENT");
256+
((void (*)(value_t))gc_function_object_ptr(task_item.event_call.fn, 0))(get_obj_property(task_item.event_call.fn, 2));
196257
break;
197258
case BS_CMD_RESET:
198259
ESP_LOGI(BS_SHELL_TAG, "Soft reset");
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
nvs, data, nvs, 0x9000, 0x6000,
22
phy_init, data, phy, 0xf000, 0x1000,
33
factory, app, factory, 0x10000, 1M,
4-
text, data, , , 1M,
4+
iflash, data, , , 500K,
5+
dflash, data, , , 500K,

modules/button/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
idf_component_register(SRCS "button.c"
2+
INCLUDE_DIRS "."
3+
REQUIRES core driver main
4+
# WHOLE_ARCHIVE
5+
)
6+
7+
target_compile_options(${COMPONENT_LIB} PRIVATE -mtext-section-literals)

0 commit comments

Comments
 (0)