Skip to content

Commit 49843f1

Browse files
Merge pull request #54 from maejima-fumika/execute-macro-benchmarks
Execute macro benchmarks
2 parents cbf2415 + b440713 commit 49843f1

File tree

22 files changed

+356
-191
lines changed

22 files changed

+356
-191
lines changed

microcontroller/core/include/main-thread.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
#include <stdint.h>
55
#include "c-runtime.h"
66

7-
void bs_main_thread_init();
7+
#define CORE_TEXT_SECTION __attribute__((section(".core_text")))
88

9-
void bs_main_thread_reset();
9+
void CORE_TEXT_SECTION bs_main_thread_init();
1010

11-
void bs_main_thread_set_main(int32_t id, void* address);
11+
void CORE_TEXT_SECTION bs_main_thread_reset();
1212

13-
void bs_main_thread_set_event(value_t fn);
13+
void CORE_TEXT_SECTION bs_main_thread_set_main(int32_t id, void* address);
14+
15+
void CORE_TEXT_SECTION bs_main_thread_set_event(value_t fn);
16+
17+
void CORE_TEXT_SECTION bs_main_thread_set_event_from_isr(void* fn);
1418

1519
#endif /* __BS_MAIN_THREAD__ */

microcontroller/core/include/protocol.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
#include "memory.h"
66
#include "ble.h"
77

8+
#define CORE_TEXT_SECTION __attribute__((section(".core_text")))
89

9-
void bs_protocol_write_log(char* message);
10+
void CORE_TEXT_SECTION bs_protocol_write_log(char* message);
1011

11-
void bs_protocol_write_error(char* message);
12+
void CORE_TEXT_SECTION bs_protocol_write_error(char* message);
1213

13-
void bs_protocol_write_profile(uint8_t fid, char* profile);
14+
void CORE_TEXT_SECTION bs_protocol_write_profile(uint8_t fid, char* profile);
1415

15-
void bs_protocol_write_execution_time(int32_t id, float time);
16+
void CORE_TEXT_SECTION bs_protocol_write_execution_time(int32_t id, float time);
1617

17-
void bs_protocol_write_memory_layout(bs_memory_layout_t* layout);
18+
void CORE_TEXT_SECTION bs_protocol_write_memory_layout(bs_memory_layout_t* layout);
1819

19-
void bs_protocol_read(uint8_t* buffer, uint32_t len);
20+
void CORE_TEXT_SECTION bs_protocol_read(uint8_t* buffer, uint32_t len);
2021

2122
#endif /* __BS_PROTOCOL__ */

microcontroller/core/src/main-thread.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,30 @@ typedef union {
3535

3636
static QueueHandle_t task_item_queue;
3737

38-
static void main_thread_init() {
38+
static bool is_code_installed(bs_memory_layout_t* memory_layout) {
39+
uint8_t* dflash_address = (uint8_t*)memory_layout->dflash_address;
40+
return dflash_address[0] == 1;
41+
}
42+
43+
static void main_thread_init(bs_memory_layout_t* memory_layout) {
3944
BS_LOG_INFO("Initialize main thread")
40-
bs_memory_init();
4145
gc_initialize();
46+
bs_memory_init();
47+
bs_memory_get_layout(memory_layout);
4248
task_item_queue = xQueueCreate(TASK_ITEM_QUEUE_LENGTH, sizeof(task_item_u));
49+
if (is_code_installed(memory_layout)) {
50+
BS_LOG_INFO("Execute installed code");
51+
uint32_t dram_code_length = *(uint32_t*)(memory_layout->dflash_address + 1);
52+
bs_protocol_read((uint8_t*)memory_layout->dflash_address + 5, dram_code_length);
53+
} else {
54+
BS_LOG_INFO("Reset memory")
55+
bs_memory_reset();
56+
}
4357
}
4458

4559
static void main_thread_reset() {
4660
BS_LOG_INFO("Reset main thread")
47-
bs_memory_reset();
61+
bs_memory_ram_reset();
4862
gc_initialize();
4963
xQueueReset(task_item_queue);
5064
}
@@ -63,7 +77,8 @@ static void task_call_event(value_t fn) {
6377
}
6478

6579
void main_thread(void *arg) {
66-
main_thread_init();
80+
bs_memory_layout_t memory_layout;
81+
main_thread_init(&memory_layout);
6782

6883
while (true) {
6984
task_item_u task_item;
@@ -79,8 +94,6 @@ void main_thread(void *arg) {
7994
break;
8095
case TASK_RESET:
8196
main_thread_reset();
82-
bs_memory_layout_t memory_layout;
83-
bs_memory_get_layout(&memory_layout);
8497
bs_protocol_write_memory_layout(&memory_layout);
8598
break;
8699
default:
@@ -91,7 +104,7 @@ void main_thread(void *arg) {
91104
}
92105

93106
void bs_main_thread_init() {
94-
xTaskCreatePinnedToCore(main_thread, "bs_main_thread", 4096 * 16, NULL, 5, NULL, 0);
107+
xTaskCreatePinnedToCore(main_thread, "bs_main_thread", 4096 * 16, NULL, 1, NULL, 0);
95108
}
96109

97110
void bs_main_thread_reset() {
@@ -113,4 +126,12 @@ void bs_main_thread_set_event(value_t fn) {
113126
task_item.call_event.task = TASK_CALL_EVENT;
114127
task_item.call_event.fn = fn;
115128
xQueueSend(task_item_queue, &task_item, portMAX_DELAY);
129+
}
130+
131+
void bs_main_thread_set_event_from_isr(void* fn) {
132+
portBASE_TYPE yield = pdFALSE;
133+
task_item_u task_item;
134+
task_item.call_event.task = TASK_CALL_EVENT;
135+
task_item.call_event.fn = (value_t)fn;
136+
xQueueSendFromISR(task_item_queue, &task_item, &yield);
116137
}

microcontroller/ports/esp32/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ cmake_minimum_required(VERSION 3.16)
77
set(EXTRA_COMPONENT_DIRS
88
"../../core"
99
"../../../modules/std"
10-
# "../../../modules/gpio"
11-
# "../../../modules/timer"
12-
# "../../../modules/display"
13-
# "../../../modules/button"
10+
"../../../modules/gpio"
11+
"../../../modules/timer"
12+
"../../../modules/display"
13+
"../../../modules/button"
1414
)
1515

1616
include($ENV{IDF_PATH}/tools/cmake/project.cmake)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ typedef struct {
1616

1717
void bs_memory_init();
1818

19+
void bs_memory_ram_reset();
20+
1921
void bs_memory_reset();
2022

2123
void bs_memory_get_layout(bs_memory_layout_t* layout);

microcontroller/ports/esp32/main/memory.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ void bs_memory_init() {
7676
}
7777

7878

79+
void bs_memory_ram_reset() {
80+
iram_reset();
81+
dram_reset();
82+
}
83+
84+
7985
void bs_memory_reset() {
8086
iram_reset();
8187
dram_reset();

modules/button/button.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "driver/gpio.h"
22
#include "c-runtime.h"
3-
#include "shell.h"
3+
#include "main-thread.h"
44
#include "button.h"
55

66

@@ -28,7 +28,7 @@ void fbody_000001buttonOnPressed(value_t self, int32_t _buttonPin, value_t _call
2828
is_isr_installed = true;
2929
}
3030
set_global_variable(&global_rootset0_000001.values[_buttonPin], _callback);
31-
gpio_isr_handler_add(_buttonPin, bs_event_push_from_isr, (void*)func_rootset.values[0]);
31+
gpio_isr_handler_add(_buttonPin, bs_main_thread_set_event_from_isr, (void*)func_rootset.values[0]);
3232
}
3333
DELETE_ROOT_SET(func_rootset)
3434
}

modules/display/display-helper.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "esp_system.h"
44
#include "driver/spi_master.h"
55
#include "driver/gpio.h"
6+
#include "freertos/FreeRTOS.h"
7+
#include "freertos/task.h"
68

79

810
#define LCD_HOST HSPI_HOST
@@ -508,3 +510,74 @@ uint8_t* get_font8x8_char(char chr) {
508510
return font8x8 + idx * 8;
509511
}
510512

513+
void display_reset() {
514+
gpio_set_level(PIN_NUM_BCKL, 0);
515+
gpio_set_level(PIN_NUM_RST, 0);
516+
vTaskDelay(50 / portTICK_PERIOD_MS);
517+
gpio_set_level(PIN_NUM_RST, 1);
518+
vTaskDelay(50 / portTICK_PERIOD_MS);
519+
}
520+
521+
void display_init() {
522+
spi_init();
523+
gpio_init();
524+
display_reset();
525+
int cmd = 0;
526+
//Send all the commands
527+
while (init_cmds[cmd].databytes!=0xff) {
528+
spi_write_cmd(init_cmds[cmd].cmd);
529+
spi_write_data(init_cmds[cmd].data, init_cmds[cmd].databytes&0x1F);
530+
if (init_cmds[cmd].databytes&0x80) {
531+
vTaskDelay(100 / portTICK_PERIOD_MS);
532+
}
533+
cmd++;
534+
}
535+
536+
spi_write_cmd(MADCTL);
537+
uint8_t data[16] = {0x08};
538+
spi_write_data(data, 1&0x1F);
539+
spi_write_cmd(DISPLAY_INVERSION_ON);
540+
spi_write_cmd(WAKE);
541+
vTaskDelay(120 / portTICK_PERIOD_MS);
542+
spi_write_cmd(DISPLAY_ON);
543+
gpio_set_level(PIN_NUM_BCKL, 1);
544+
}
545+
546+
uint16_t display_color(int32_t r, int32_t g, int32_t b) {
547+
uint16_t color = 0;
548+
color |= ((r >> 3) << 11);
549+
color |= ((g >> 2) << 5);
550+
color |= ((b >> 3) << 0);
551+
color = (color >> 8) | (color << 8); // big-endian
552+
return color;
553+
}
554+
555+
void display_fill(uint16_t color) {
556+
display_draw(fill, color, color);
557+
}
558+
559+
void display_show_heart_icon(uint16_t color, uint16_t background) {
560+
display_draw(heart, color, background);
561+
}
562+
563+
void display_show_small_heart_icon(uint16_t color, uint16_t background) {
564+
display_draw(small_heart, color, background);
565+
}
566+
567+
void display_show_happy_face_icon(uint16_t color, uint16_t background) {
568+
display_draw(happy_face, color, background);
569+
}
570+
571+
void display_show_sad_face_icon(uint16_t color, uint16_t background) {
572+
display_draw(sad_face, color, background);
573+
}
574+
575+
void display_show_string(char* str, uint16_t color, uint16_t background) {
576+
display_text(str, color, background);
577+
}
578+
579+
void display_show_integer(int32_t integer, uint16_t color, uint16_t background) {
580+
char buff[12]; // Max num length + blank
581+
sprintf(buff, "%ld", integer);
582+
display_text(&buff, color, background);
583+
}

modules/display/display-helper.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef __BS_Display_Helper__
2+
#define __BS_Display_Helper__
3+
4+
#include <stdint.h>
5+
6+
void display_init();
7+
8+
uint16_t display_color(int32_t r, int32_t g, int32_t b);
9+
10+
void display_fill(uint16_t color);
11+
12+
void display_show_heart_icon(uint16_t color, uint16_t background);
13+
14+
void display_show_small_heart_icon(uint16_t color, uint16_t background);
15+
16+
void display_show_happy_face_icon(uint16_t color, uint16_t background);
17+
18+
void display_show_sad_face_icon(uint16_t color, uint16_t background);
19+
20+
void display_show_string(char* str, uint16_t color, uint16_t background);
21+
22+
void display_show_integer(int32_t integer, uint16_t color, uint16_t background);
23+
24+
#endif /* __BS_Display_Helper__ */

modules/display/display.bs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type {integer} from "../../bs-utils"
22

3+
code`#include "display-helper.h"`
34

45
export class Display {
56
ICON_HEART: integer;
@@ -12,30 +13,38 @@ export class Display {
1213
this.ICON_SMALL_HEART = 1;
1314
this.ICON_HAPPY_FACE = 2;
1415
this.ICON_SAD_FACE = 3;
15-
this.reset();
16-
}
17-
18-
public reset() {
19-
16+
code`display_init()`;
2017
}
2118

2219
public fill(color: integer) {
23-
20+
code`display_fill(_color)`;
2421
}
2522

2623
public showIcon(icon: integer, color: integer, background: integer) {
27-
24+
if (icon === this.ICON_HEART)
25+
code`display_show_heart_icon(_color, _background)`;
26+
else if (icon === this.ICON_SMALL_HEART)
27+
code`display_show_small_heart_icon(_color, _background)`;
28+
else if (icon === this.ICON_HAPPY_FACE)
29+
code`display_show_happy_face_icon(_color, _background)`;
30+
else if (icon === this.ICON_SAD_FACE)
31+
code`display_show_sad_face_icon(_color, _background)`;
32+
else
33+
code`runtime_error("** display module error: unknown icon.")`
2834
}
2935

3036
public showString(str: string, color: integer, background: integer) {
31-
37+
code`char* text = gc_string_to_cstr(_str)`;
38+
code`display_show_string(text, _color, _background)`;
3239
}
3340

3441
public showInt(int: integer, color: integer, background: integer) {
35-
42+
code`display_show_integer(_int, _color, _background)`;
3643
}
3744

3845
public color(r: integer, g: integer, b: integer):integer {
39-
return 2;
46+
const color = 0;
47+
code`_color = display_color(_r, _g, _b)`;
48+
return color;
4049
}
4150
}

0 commit comments

Comments
 (0)