Skip to content

Commit

Permalink
Fix Andrew's PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
guberti committed Aug 12, 2021
1 parent 96133bc commit 93b9606
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 213 deletions.
5 changes: 3 additions & 2 deletions apps/microtvm/arduino/example_project/project.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

void setup() {
TVMInitialize();
//TVMExecute(input_data, output_data);
// If desired, initialize the RNG with random noise
// randomSeed(analogRead(0));
}

void loop() {
// put your main code here, to run repeatedly:
//TVMExecute(input_data, output_data);
}
15 changes: 4 additions & 11 deletions apps/microtvm/arduino/example_project/src/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
* under the License.
*/

#ifndef TVM_IMPLEMENTATION_ARDUINO
#define TVM_IMPLEMENTATION_ARDUINO

#include "model.h"

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

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

unsigned long g_utvm_start_time;

#define MILLIS_TIL_EXPIRY 200

unsigned long g_utvm_start_time_micros;
int g_utvm_timer_running = 0;

tvm_crt_error_t TVMPlatformTimerStart() {
if (g_utvm_timer_running) {
return kTvmErrorPlatformTimerBadState;
}
g_utvm_timer_running = 1;
g_utvm_start_time = micros();
g_utvm_start_time_micros = micros();
return kTvmErrorNoError;
}

Expand All @@ -77,7 +72,7 @@ tvm_crt_error_t TVMPlatformTimerStop(double* elapsed_time_seconds) {
return kTvmErrorPlatformTimerBadState;
}
g_utvm_timer_running = 0;
unsigned long g_utvm_stop_time = micros() - g_utvm_start_time;
unsigned long g_utvm_stop_time = micros() - g_utvm_start_time_micros;
*elapsed_time_seconds = ((double)g_utvm_stop_time) / 1e6;
return kTvmErrorNoError;
}
Expand All @@ -97,5 +92,3 @@ void TVMExecute(void* input_data, void* output_data) {
TVMPlatformAbort(kTvmErrorPlatformCheckFailure);
}
}

#endif
14 changes: 8 additions & 6 deletions apps/microtvm/arduino/example_project/src/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
* under the License.
*/

#ifndef IMPLEMENTATION_H_
#define IMPLEMENTATION_H_

#define WORKSPACE_SIZE $workspace_size_bytes

#ifdef __cplusplus
Expand All @@ -28,11 +25,16 @@ extern "C" {

void TVMInitialize();

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

#ifdef __cplusplus
} // extern "C"
#endif

#endif // IMPLEMENTATION_H_
24 changes: 12 additions & 12 deletions apps/microtvm/arduino/host_driven/project.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "src/standalone_crt/include/tvm/runtime/crt/microtvm_rpc_server.h"
#include "src/standalone_crt/include/tvm/runtime/crt/logging.h"
#include "src/model.h"
microtvm_rpc_server_t server;

// Called by TVM to write serial data to the UART.
Expand All @@ -32,22 +31,23 @@ void setup() {
server = MicroTVMRpcServerInit(write_serial, NULL);
TVMLogf("microTVM Arduino runtime - running");
Serial.begin(115200);

// If desired, initialize the RNG with random noise
// randomSeed(analogRead(0));
}

void loop() {
int to_read = Serial.available();
// Read at most 128 bytes at a time to prevent stack blowup
int to_read = min(Serial.available(), 128);

uint8_t data[to_read];
size_t bytes_read = Serial.readBytes(data, to_read);
size_t bytes_remaining = Serial.readBytes(data, to_read);
uint8_t* arr_ptr = data;
uint8_t** data_ptr = &arr_ptr;
if (bytes_read > 0) {
size_t bytes_remaining = bytes_read;
while (bytes_remaining > 0) {
// Pass the received bytes to the RPC server.
tvm_crt_error_t err = MicroTVMRpcServerLoop(server, data_ptr, &bytes_remaining);
if (err != kTvmErrorNoError && err != kTvmErrorFramingShortPacket) {
TVMPlatformAbort(err);
}
while (bytes_remaining > 0) {
// Pass the received bytes to the RPC server.
tvm_crt_error_t err = MicroTVMRpcServerLoop(server, &arr_ptr, &bytes_remaining);
if (err != kTvmErrorNoError && err != kTvmErrorFramingShortPacket) {
TVMPlatformAbort(err);
}
}
}
25 changes: 0 additions & 25 deletions apps/microtvm/arduino/host_driven/src/model.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,13 @@
* under the License.
*/

#ifndef TVM_IMPLEMENTATION_ARDUINO
#define TVM_IMPLEMENTATION_ARDUINO

#include "model.h"

#include "Arduino.h"
#include "standalone_crt/include/tvm/runtime/crt/internal/aot_executor/aot_executor.h"
#include "stdarg.h"
#include "standalone_crt/include/tvm/runtime/crt/internal/aot_executor/aot_executor.h"

// Blink code for debugging purposes
void TVMPlatformAbort(tvm_crt_error_t error) {
for (;;) {
#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(750);
#endif
}
TVMLogf("TVMPlatformAbort: %08x\n", error);
for (;;);
}

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

unsigned long g_utvm_start_time;

#define MILLIS_TIL_EXPIRY 200

unsigned long g_utvm_start_time_micros;
int g_utvm_timer_running = 0;

tvm_crt_error_t TVMPlatformTimerStart() {
if (g_utvm_timer_running) {
return kTvmErrorPlatformTimerBadState;
}
g_utvm_timer_running = 1;
g_utvm_start_time = micros();
g_utvm_start_time_micros = micros();
return kTvmErrorNoError;
}

Expand All @@ -80,7 +61,7 @@ tvm_crt_error_t TVMPlatformTimerStop(double* elapsed_time_seconds) {
return kTvmErrorPlatformTimerBadState;
}
g_utvm_timer_running = 0;
unsigned long g_utvm_stop_time = micros() - g_utvm_start_time;
unsigned long g_utvm_stop_time = micros() - g_utvm_start_time_micros;
*elapsed_time_seconds = ((double)g_utvm_stop_time) / 1e6;
return kTvmErrorNoError;
}
Expand All @@ -91,5 +72,3 @@ tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) {
}
return kTvmErrorNoError;
}

#endif
Loading

0 comments on commit 93b9606

Please sign in to comment.