mehrdadh commented on code in PR #13885:
URL: https://github.com/apache/tvm/pull/13885#discussion_r1099250461


##########
apps/microtvm/zephyr/template_project/microtvm_api_server.py:
##########
@@ -413,6 +419,9 @@ def _create_prj_conf(
                 "CONFIG_UART_INTERRUPT_DRIVEN=y\n"
                 "\n"
             )
+            if config_led and not self._is_qemu(board, use_fvp):

Review Comment:
   done



##########
apps/microtvm/zephyr/template_project/microtvm_api_server.py:
##########
@@ -658,13 +669,31 @@ def generate_project(self, model_library_format_path, 
standalone_crt_dir, projec
             API_SERVER_DIR / "crt_config" / "crt_config.h", crt_config_dir / 
"crt_config.h"
         )
 
-        # Populate src/
+        # Populate src and include
         src_dir = project_dir / "src"
-        if project_type != "host_driven" or self._is_fvp(zephyr_board, 
use_fvp):
-            shutil.copytree(API_SERVER_DIR / "src" / project_type, src_dir)
-        else:
-            src_dir.mkdir()
-            shutil.copy2(API_SERVER_DIR / "src" / project_type / "main.c", 
src_dir)
+        src_dir.mkdir()
+        include_dir = project_dir / "include" / "tvm"
+        include_dir.mkdir(parents=True)
+        src_project_tyoe_dir = API_SERVER_DIR / "src" / project_type

Review Comment:
   done



##########
apps/microtvm/arduino/template_project/src/example_project/project.ino:
##########
@@ -17,10 +17,10 @@
  * under the License.
  */
 
-#include "src/model.h"
+#include "src/standalone_crt/include/tvm/runtime/crt/platform.h"
 
 void setup() {
-  TVMInitialize();
+  TVMPlatformInitialize();

Review Comment:
   TVMInitialize was defined only in Arduino and some other standalone cases. 
My goal was to make this call more generic to implement all platform specific 
initialization that are necessary for other APIs. Things like memory 
initialization, timer, etc.



##########
apps/microtvm/zephyr/template_project/src/aot_standalone_demo/main.c:
##########
@@ -229,13 +180,13 @@ void serial_callback(char* message, int len_bytes) {
 }
 
 void main(void) {
+  UARTInit();
+  TVMPlatformInitialize();
   g_cmd_buf_ind = 0;

Review Comment:
   UartInit initialize both transmit and receive



##########
apps/microtvm/arduino/template_project/src/example_project/platform.c:
##########
@@ -77,14 +88,19 @@ tvm_crt_error_t TVMPlatformTimerStop(double* 
elapsed_time_seconds) {
   return kTvmErrorNoError;
 }
 
-tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* buffer, size_t num_bytes) {
+// Fill a buffer with random data.
+__attribute__((weak)) tvm_crt_error_t TVMPlatformGenerateRandom(uint8_t* 
buffer, size_t num_bytes) {

Review Comment:
   The idea here is that `platform.c` has one implementation of these functions 
which is added by default to the project. But in case the user wants to change 
the implementation, they could reimplement it in their `main.c`
   That's why it is defined as weak.



##########
apps/microtvm/zephyr/template_project/src/aot_standalone_demo/main.c:
##########
@@ -53,135 +48,91 @@ const unsigned char CMD_INFER[] = "infer";
 #define CMD_SIZE 80u
 #define CMD_TERMINATOR '%'
 
-size_t TVMPlatformFormatMessage(char* out_buf, size_t out_buf_size_bytes, 
const char* fmt,
-                                va_list args) {
-  return vsnprintk(out_buf, out_buf_size_bytes, fmt, args);
-}
+static uint8_t main_rx_buf[128];
+static uint8_t g_cmd_buf[128];
+static size_t g_cmd_buf_ind;
 
-void TVMLogf(const char* msg, ...) {
-  char buffer[256];
-  int size;
-  va_list args;
-  va_start(args, msg);
-  size = vsprintf(buffer, msg, args);
-  va_end(args);
-  TVMPlatformWriteSerial(buffer, (uint32_t)size);
-}
+static const struct device* g_microtvm_uart;
+#define RING_BUF_SIZE_BYTES (TVM_CRT_MAX_PACKET_SIZE_BYTES + 100)
 
-void TVMPlatformAbort(tvm_crt_error_t error) {
-  TVMLogf("TVMPlatformAbort: %08x\n", error);
-  sys_reboot(SYS_REBOOT_COLD);
-  for (;;)
-    ;
-}
+// Ring buffer used to store data read from the UART on rx interrupt.
+RING_BUF_DECLARE(uart_rx_rbuf, RING_BUF_SIZE_BYTES);
 
-tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLDevice dev, 
void** out_ptr) {
-  return StackMemoryManager_Allocate(&app_workspace, num_bytes, out_ptr);
+uint32_t UartTxWrite(const char* data, uint32_t size) {
+  for (uint32_t i = 0; i < size; i++) {
+    uart_poll_out(g_microtvm_uart, data[i]);
+  }
+  return size;
 }
 
-tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLDevice dev) {
-  return StackMemoryManager_Free(&app_workspace, ptr);
+uint32_t UartRxRead(uint8_t* data, uint32_t data_size_bytes) {
+  unsigned int key = irq_lock();
+  uint32_t bytes_read = ring_buf_get(&uart_rx_rbuf, data, data_size_bytes);
+  irq_unlock(key);
+  return bytes_read;
 }
 
-void timer_expiry_function(struct k_timer* timer_id) { return; }
-
-#define MILLIS_TIL_EXPIRY 200
-#define TIME_TIL_EXPIRY (K_MSEC(MILLIS_TIL_EXPIRY))
-struct k_timer g_microtvm_timer;
-uint32_t g_microtvm_start_time;
-int g_microtvm_timer_running = 0;
-
-// Called to start system timer.
-tvm_crt_error_t TVMPlatformTimerStart() {
-  if (g_microtvm_timer_running) {
-    TVMLogf("timer already running");
-    return kTvmErrorPlatformTimerBadState;
-  }
-
-  k_timer_start(&g_microtvm_timer, TIME_TIL_EXPIRY, TIME_TIL_EXPIRY);
-  g_microtvm_start_time = k_cycle_get_32();
-  g_microtvm_timer_running = 1;
-  return kTvmErrorNoError;
+// Initialize UART
+void UARTInit() {

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to