Skip to content

Commit 93b9606

Browse files
committed
Fix Andrew's PR comments
1 parent 96133bc commit 93b9606

File tree

14 files changed

+189
-213
lines changed

14 files changed

+189
-213
lines changed

apps/microtvm/arduino/example_project/project.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121

2222
void setup() {
2323
TVMInitialize();
24-
//TVMExecute(input_data, output_data);
24+
// If desired, initialize the RNG with random noise
25+
// randomSeed(analogRead(0));
2526
}
2627

2728
void loop() {
28-
// put your main code here, to run repeatedly:
29+
//TVMExecute(input_data, output_data);
2930
}

apps/microtvm/arduino/example_project/src/model.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
* under the License.
1818
*/
1919

20-
#ifndef TVM_IMPLEMENTATION_ARDUINO
21-
#define TVM_IMPLEMENTATION_ARDUINO
22-
2320
#include "model.h"
2421

2522
#include "Arduino.h"
@@ -33,6 +30,7 @@ tvm_workspace_t app_workspace;
3330

3431
// Blink code for debugging purposes
3532
void TVMPlatformAbort(tvm_crt_error_t error) {
33+
TVMLogf("TVMPlatformAbort: %08x\n", error);
3634
for (;;) {
3735
#ifdef LED_BUILTIN
3836
digitalWrite(LED_BUILTIN, HIGH);
@@ -57,18 +55,15 @@ tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLDevice dev) {
5755
return StackMemoryManager_Free(&app_workspace, ptr);
5856
}
5957

60-
unsigned long g_utvm_start_time;
61-
62-
#define MILLIS_TIL_EXPIRY 200
63-
58+
unsigned long g_utvm_start_time_micros;
6459
int g_utvm_timer_running = 0;
6560

6661
tvm_crt_error_t TVMPlatformTimerStart() {
6762
if (g_utvm_timer_running) {
6863
return kTvmErrorPlatformTimerBadState;
6964
}
7065
g_utvm_timer_running = 1;
71-
g_utvm_start_time = micros();
66+
g_utvm_start_time_micros = micros();
7267
return kTvmErrorNoError;
7368
}
7469

@@ -77,7 +72,7 @@ tvm_crt_error_t TVMPlatformTimerStop(double* elapsed_time_seconds) {
7772
return kTvmErrorPlatformTimerBadState;
7873
}
7974
g_utvm_timer_running = 0;
80-
unsigned long g_utvm_stop_time = micros() - g_utvm_start_time;
75+
unsigned long g_utvm_stop_time = micros() - g_utvm_start_time_micros;
8176
*elapsed_time_seconds = ((double)g_utvm_stop_time) / 1e6;
8277
return kTvmErrorNoError;
8378
}
@@ -97,5 +92,3 @@ void TVMExecute(void* input_data, void* output_data) {
9792
TVMPlatformAbort(kTvmErrorPlatformCheckFailure);
9893
}
9994
}
100-
101-
#endif

apps/microtvm/arduino/example_project/src/model.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
* under the License.
1818
*/
1919

20-
#ifndef IMPLEMENTATION_H_
21-
#define IMPLEMENTATION_H_
22-
2320
#define WORKSPACE_SIZE $workspace_size_bytes
2421

2522
#ifdef __cplusplus
@@ -28,11 +25,16 @@ extern "C" {
2825

2926
void TVMInitialize();
3027

31-
// TODO template these void* values once MLF format has input and output data
28+
/* TODO template this function signature with the input and output
29+
* data types and sizes. For example:
30+
*
31+
* void TVMExecute(uint8_t input_data[9216], uint8_t output_data[3]);
32+
*
33+
* Note this can only be done once MLF has JSON metadata describing
34+
* inputs and outputs.
35+
*/
3236
void TVMExecute(void* input_data, void* output_data);
3337

3438
#ifdef __cplusplus
3539
} // extern "C"
3640
#endif
37-
38-
#endif // IMPLEMENTATION_H_

apps/microtvm/arduino/host_driven/project.ino

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include "src/standalone_crt/include/tvm/runtime/crt/microtvm_rpc_server.h"
2121
#include "src/standalone_crt/include/tvm/runtime/crt/logging.h"
22-
#include "src/model.h"
2322
microtvm_rpc_server_t server;
2423

2524
// Called by TVM to write serial data to the UART.
@@ -32,22 +31,23 @@ void setup() {
3231
server = MicroTVMRpcServerInit(write_serial, NULL);
3332
TVMLogf("microTVM Arduino runtime - running");
3433
Serial.begin(115200);
34+
35+
// If desired, initialize the RNG with random noise
36+
// randomSeed(analogRead(0));
3537
}
3638

3739
void loop() {
38-
int to_read = Serial.available();
40+
// Read at most 128 bytes at a time to prevent stack blowup
41+
int to_read = min(Serial.available(), 128);
42+
3943
uint8_t data[to_read];
40-
size_t bytes_read = Serial.readBytes(data, to_read);
44+
size_t bytes_remaining = Serial.readBytes(data, to_read);
4145
uint8_t* arr_ptr = data;
42-
uint8_t** data_ptr = &arr_ptr;
43-
if (bytes_read > 0) {
44-
size_t bytes_remaining = bytes_read;
45-
while (bytes_remaining > 0) {
46-
// Pass the received bytes to the RPC server.
47-
tvm_crt_error_t err = MicroTVMRpcServerLoop(server, data_ptr, &bytes_remaining);
48-
if (err != kTvmErrorNoError && err != kTvmErrorFramingShortPacket) {
49-
TVMPlatformAbort(err);
50-
}
46+
while (bytes_remaining > 0) {
47+
// Pass the received bytes to the RPC server.
48+
tvm_crt_error_t err = MicroTVMRpcServerLoop(server, &arr_ptr, &bytes_remaining);
49+
if (err != kTvmErrorNoError && err != kTvmErrorFramingShortPacket) {
50+
TVMPlatformAbort(err);
5151
}
5252
}
5353
}

apps/microtvm/arduino/host_driven/src/model.h

Lines changed: 0 additions & 25 deletions
This file was deleted.

apps/microtvm/arduino/host_driven/src/model.c renamed to apps/microtvm/arduino/host_driven/src/model_support.c

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,13 @@
1717
* under the License.
1818
*/
1919

20-
#ifndef TVM_IMPLEMENTATION_ARDUINO
21-
#define TVM_IMPLEMENTATION_ARDUINO
22-
23-
#include "model.h"
24-
25-
#include "Arduino.h"
26-
#include "standalone_crt/include/tvm/runtime/crt/internal/aot_executor/aot_executor.h"
2720
#include "stdarg.h"
21+
#include "standalone_crt/include/tvm/runtime/crt/internal/aot_executor/aot_executor.h"
2822

2923
// Blink code for debugging purposes
3024
void TVMPlatformAbort(tvm_crt_error_t error) {
31-
for (;;) {
32-
#ifdef LED_BUILTIN
33-
digitalWrite(LED_BUILTIN, HIGH);
34-
delay(250);
35-
digitalWrite(LED_BUILTIN, LOW);
36-
delay(250);
37-
digitalWrite(LED_BUILTIN, HIGH);
38-
delay(250);
39-
digitalWrite(LED_BUILTIN, LOW);
40-
delay(750);
41-
#endif
42-
}
25+
TVMLogf("TVMPlatformAbort: %08x\n", error);
26+
for (;;);
4327
}
4428

4529
size_t TVMPlatformFormatMessage(char* out_buf, size_t out_buf_size_bytes, const char* fmt,
@@ -60,18 +44,15 @@ tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLDevice dev) {
6044
return kTvmErrorNoError;
6145
}
6246

63-
unsigned long g_utvm_start_time;
64-
65-
#define MILLIS_TIL_EXPIRY 200
66-
47+
unsigned long g_utvm_start_time_micros;
6748
int g_utvm_timer_running = 0;
6849

6950
tvm_crt_error_t TVMPlatformTimerStart() {
7051
if (g_utvm_timer_running) {
7152
return kTvmErrorPlatformTimerBadState;
7253
}
7354
g_utvm_timer_running = 1;
74-
g_utvm_start_time = micros();
55+
g_utvm_start_time_micros = micros();
7556
return kTvmErrorNoError;
7657
}
7758

@@ -80,7 +61,7 @@ tvm_crt_error_t TVMPlatformTimerStop(double* elapsed_time_seconds) {
8061
return kTvmErrorPlatformTimerBadState;
8162
}
8263
g_utvm_timer_running = 0;
83-
unsigned long g_utvm_stop_time = micros() - g_utvm_start_time;
64+
unsigned long g_utvm_stop_time = micros() - g_utvm_start_time_micros;
8465
*elapsed_time_seconds = ((double)g_utvm_stop_time) / 1e6;
8566
return kTvmErrorNoError;
8667
}
@@ -91,5 +72,3 @@ tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) {
9172
}
9273
return kTvmErrorNoError;
9374
}
94-
95-
#endif

0 commit comments

Comments
 (0)