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


##########
apps/microtvm/arduino/template_project/src/example_project/platform.c:
##########
@@ -45,16 +46,18 @@ void TVMPlatformAbort(tvm_crt_error_t error) {
   }
 }
 
-void TVMLogf(const char* msg, ...) {}
-
+// Called by TVM when memory allocation is required.
 tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLDevice dev, 
void** out_ptr) {
   return StackMemoryManager_Allocate(&app_workspace, num_bytes, out_ptr);
 }
 
+// Called by TVM to free an allocated memory.

Review Comment:
   ```suggestion
   // Called by TVM to free allocated memory.
   ```



##########
apps/microtvm/zephyr/template_project/src/aot_standalone_demo/platform.c:
##########
@@ -16,19 +16,64 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#include "zephyr_uart.h"
 
+#include "tvm/platform.h"
+
+#include <assert.h>

Review Comment:
   Why are we adding `assert.h`?



##########
apps/microtvm/zephyr/template_project/src/mlperftiny/platform.h:
##########
@@ -59,4 +62,24 @@ void TVMInfer(void* input_ptr);
  */
 int8_t QuantizeFloatToInt8(float value, float scale, int zero_point);
 
-#endif /* APPS_MICROTVM_ZEPHYR_TEMPLATE_PROJECT_SRC_MLPERFTINY_TVMRUNTIME_H_ */
+/*!
+ * \brief Read Uart Rx buffer.
+ */
+char TVMPlatformUartRxRead();
+
+/*!
+ * \brief Write data in serial.
+ * \param data Pointer to data to write.
+ * \param size Size of data in bytes.
+ *
+ * \return Number of write in bytes.
+ */
+uint32_t TVMPlatformWriteSerial(const char* data, uint32_t size);
+
+/*!
+ * \brief Initialize Uart.

Review Comment:
   ```suggestion
    * \brief Initialize UART.
   ```



##########
apps/microtvm/arduino/template_project/src/host_driven/platform.c:
##########
@@ -21,27 +21,32 @@
 #include "standalone_crt/include/tvm/runtime/crt/error_codes.h"
 #include "stdarg.h"
 
+// Called by TVM when a message needs to be formatted.
+__attribute__((weak)) size_t TVMPlatformFormatMessage(char* out_buf, size_t 
out_buf_size_bytes,
+                                                      const char* fmt, va_list 
args) {
+  return vsnprintf(out_buf, out_buf_size_bytes, fmt, args);
+}
+
+// Called by TVM when an internal invariant is violated, and execution cannot 
continue.
 // Blink code for debugging purposes
-void TVMPlatformAbort(tvm_crt_error_t error) {
+__attribute__((weak)) void TVMPlatformAbort(tvm_crt_error_t error) {
   TVMLogf("TVMPlatformAbort: 0x%08x\n", error);
   for (;;)
     ;
 }
 
-size_t TVMPlatformFormatMessage(char* out_buf, size_t out_buf_size_bytes, 
const char* fmt,
-                                va_list args) {
-  return vsnprintf(out_buf, out_buf_size_bytes, fmt, args);
-}
-
-tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLDevice dev, 
void** out_ptr) {
+// Called by TVM when memory allocation is required.
+__attribute__((weak)) tvm_crt_error_t TVMPlatformMemoryAllocate(size_t 
num_bytes, DLDevice dev,
+                                                                void** 
out_ptr) {
   if (num_bytes == 0) {
     num_bytes = sizeof(int);
   }
   *out_ptr = malloc(num_bytes);
   return (*out_ptr == NULL) ? kTvmErrorPlatformNoMemory : kTvmErrorNoError;
 }
 
-tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLDevice dev) {
+// Called by TVM to free an allocated memory.

Review Comment:
   ```suggestion
   // Called by TVM to free allocated memory.
   ```



##########
apps/microtvm/arduino/template_project/src/host_driven/platform.c:
##########
@@ -21,27 +21,32 @@
 #include "standalone_crt/include/tvm/runtime/crt/error_codes.h"
 #include "stdarg.h"
 
+// Called by TVM when a message needs to be formatted.
+__attribute__((weak)) size_t TVMPlatformFormatMessage(char* out_buf, size_t 
out_buf_size_bytes,
+                                                      const char* fmt, va_list 
args) {
+  return vsnprintf(out_buf, out_buf_size_bytes, fmt, args);
+}
+
+// Called by TVM when an internal invariant is violated, and execution cannot 
continue.
 // Blink code for debugging purposes

Review Comment:
   Remove comment since it seems like this function does not blink.



##########
apps/microtvm/arduino/template_project/src/example_project/platform.c:
##########
@@ -28,6 +28,7 @@ static uint8_t g_aot_memory[WORKSPACE_SIZE]
     __attribute__((aligned(TVM_RUNTIME_ALLOC_ALIGNMENT_BYTES)));
 tvm_workspace_t app_workspace;
 
+// Called by TVM when an internal invariant is violated, and execution cannot 
continue.
 // Blink code for debugging purposes
 void TVMPlatformAbort(tvm_crt_error_t error) {

Review Comment:
   Should we make these function declarations weak in Arduino, since they're 
weak in Zephyr?



##########
apps/microtvm/zephyr/template_project/src/host_driven/platform.h:
##########
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef TVM_APPS_MICROTVM_ZEPHYR_HOST_DRIVEN_PLATFORM_H_
+#define TVM_APPS_MICROTVM_ZEPHYR_HOST_DRIVEN_PLATFORM_H_
+
+#include <zephyr/drivers/gpio.h>
+
+#ifdef CONFIG_LED
+#define LED0_NODE DT_ALIAS(led0)
+// #define LED0 DT_GPIO_LABEL(LED0_NODE, gpios)
+// #define LED0_PIN DT_GPIO_PIN(LED0_NODE, gpios)
+// #define LED0_FLAGS DT_GPIO_FLAGS(LED0_NODE, gpios)
+// static const struct device* led0_pin;

Review Comment:
   Delete these?



##########
apps/microtvm/arduino/template_project/src/example_project/platform.c:
##########
@@ -45,16 +46,18 @@ void TVMPlatformAbort(tvm_crt_error_t error) {
   }
 }
 
-void TVMLogf(const char* msg, ...) {}
-
+// Called by TVM when memory allocation is required.
 tvm_crt_error_t TVMPlatformMemoryAllocate(size_t num_bytes, DLDevice dev, 
void** out_ptr) {
   return StackMemoryManager_Allocate(&app_workspace, num_bytes, out_ptr);
 }
 
+// Called by TVM to free an allocated memory.
 tvm_crt_error_t TVMPlatformMemoryFree(void* ptr, DLDevice dev) {
   return StackMemoryManager_Free(&app_workspace, ptr);
 }
 
+void TVMLogf(const char* msg, ...) {}

Review Comment:
   Add a comment here explaining what this does?



##########
apps/microtvm/zephyr/template_project/src/aot_standalone_demo/platform.c:
##########
@@ -16,19 +16,64 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#include "zephyr_uart.h"
 
+#include "tvm/platform.h"
+
+#include <assert.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <zephyr/drivers/uart.h>
+#include <zephyr/sys/reboot.h>
 #include <zephyr/sys/ring_buffer.h>
 
 #include "crt_config.h"
+#include "dlpack/dlpack.h"
+#include "tvm/runtime/crt/error_codes.h"
+#include "tvmgen_default.h"
 
 static const struct device* g_microtvm_uart;
 #define RING_BUF_SIZE_BYTES (TVM_CRT_MAX_PACKET_SIZE_BYTES + 100)
 
 // Ring buffer used to store data read from the UART on rx interrupt.
 RING_BUF_DECLARE(uart_rx_rbuf, RING_BUF_SIZE_BYTES);
 
+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);
+}
+
+// Called by TVM when a message needs to be formatted.
+__attribute__((weak)) 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);
+}
+
+// Called by TVM when an internal invariant is violated, and execution cannot 
continue.
+__attribute__((weak)) void TVMPlatformAbort(tvm_crt_error_t error) {
+  TVMLogf("TVMPlatformAbort: %08x\n", error);
+  sys_reboot(SYS_REBOOT_COLD);
+  for (;;)
+    ;

Review Comment:
   Does `sys_reboot` ever return? I'm not sure we need this infinite loop.



-- 
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