@@ -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
2834static QueueHandle_t task_queue ;
@@ -37,21 +43,25 @@ uint32_t *dram;
3743size_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
4858static 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
5767static 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
6980static 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
92115static 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+
179235void 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" );
0 commit comments