├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE.md ├── NUCLEO_H7A3ZI_Q ├── LICENSE.md ├── anomaly_detection │ ├── AD01.ioc │ └── tinyml_api │ │ ├── internally_implemented.c │ │ ├── internally_implemented.h │ │ ├── st_port.c │ │ ├── st_port.h │ │ ├── submitter_implemented.c │ │ └── submitter_implemented.h ├── image_classification │ ├── IC01.ioc │ └── tinyml_api │ │ ├── internally_implemented.c │ │ ├── internally_implemented.h │ │ ├── st_port.c │ │ ├── st_port.h │ │ ├── submitter_implemented.c │ │ └── submitter_implemented.h ├── keyword_spotting │ ├── KWS01.ioc │ └── tinyml_api │ │ ├── internally_implemented.c │ │ ├── internally_implemented.h │ │ ├── st_port.c │ │ ├── st_port.h │ │ ├── submitter_implemented.c │ │ └── submitter_implemented.h └── visual_wake_words │ ├── VWW01.ioc │ └── tinyml_api │ ├── internally_implemented.c │ ├── internally_implemented.h │ ├── st_port.c │ ├── st_port.h │ ├── submitter_implemented.c │ └── submitter_implemented.h ├── NUCLEO_L4R5ZI ├── LICENSE.md ├── anomaly_detection │ ├── AD01.ioc │ └── tinyml_api │ │ ├── internally_implemented.c │ │ ├── internally_implemented.h │ │ ├── st_port.c │ │ ├── st_port.h │ │ ├── submitter_implemented.c │ │ └── submitter_implemented.h ├── image_classification │ ├── IC01.ioc │ └── tinyml_api │ │ ├── internally_implemented.c │ │ ├── internally_implemented.h │ │ ├── st_port.c │ │ ├── st_port.h │ │ ├── submitter_implemented.c │ │ └── submitter_implemented.h ├── keyword_spotting │ ├── KWS01.ioc │ └── tinyml_api │ │ ├── internally_implemented.c │ │ ├── internally_implemented.h │ │ ├── st_port.c │ │ ├── st_port.h │ │ ├── submitter_implemented.c │ │ └── submitter_implemented.h └── visual_wake_words │ ├── VWW01.ioc │ └── tinyml_api │ ├── internally_implemented.c │ ├── internally_implemented.h │ ├── st_port.c │ ├── st_port.h │ ├── submitter_implemented.c │ └── submitter_implemented.h ├── NUCLEO_U575ZI_Q ├── LICENSE.md ├── anomaly_detection │ ├── AD01.ioc │ └── tinyml_api │ │ ├── internally_implemented.c │ │ ├── internally_implemented.h │ │ ├── st_port.c │ │ ├── st_port.h │ │ ├── submitter_implemented.c │ │ └── submitter_implemented.h ├── image_classification │ ├── IC01.ioc │ └── tinyml_api │ │ ├── internally_implemented.c │ │ ├── internally_implemented.h │ │ ├── st_port.c │ │ ├── st_port.h │ │ ├── submitter_implemented.c │ │ └── submitter_implemented.h ├── keyword_spotting │ ├── KWS01.ioc │ └── tinyml_api │ │ ├── internally_implemented.c │ │ ├── internally_implemented.h │ │ ├── st_port.c │ │ ├── st_port.h │ │ ├── submitter_implemented.c │ │ └── submitter_implemented.h └── visual_wake_words │ ├── VWW01.ioc │ └── tinyml_api │ ├── internally_implemented.c │ ├── internally_implemented.h │ ├── st_port.c │ ├── st_port.h │ ├── submitter_implemented.c │ └── submitter_implemented.h ├── README.md ├── Release_Notes.html ├── Release_Notes.md ├── _htmresc ├── Add button.svg ├── Update.svg ├── favicon.png ├── mini-st_2020.css └── st_logo_2020.png └── doc ├── H7A3_config.gif ├── L4R5_config.gif ├── STM32_H7A3ZI_config.gif ├── STM32_l4_config.gif ├── U575_config.gif ├── U5_mx_config.gif ├── ad0_initial_workspace.JPG ├── cube_ide_config_final.gif ├── cube_ide_project.JPG ├── energy_mode_flag.JPG ├── folder_structure.JPG ├── h7_energy_mode_setup.JPG ├── h7_jumpers_performance_mode.JPG ├── h7_programmer_config.gif ├── h7xxitc.JPG ├── l4_energy_mode_setup.JPG ├── l4_jumpers_performance_mode.JPG ├── l4_programmer_config.gif ├── l4cube_ide_config_final.gif ├── l4cube_ide_project.JPG ├── l4energy_mode_flag.JPG ├── l4folder_structure.JPG ├── l4mainc.JPG ├── l4performance_mode_flag.JPG ├── l4release_build.JPG ├── l4xxitc.JPG ├── mainc.JPG ├── memory_analyzer_H7.png ├── performance_mode_flag.JPG ├── release_build.JPG ├── u5-release_build.JPG ├── u5_energy_mode_setup.JPG ├── u5_ide_config.gif ├── u5_ide_cproject_struct.JPG ├── u5_jumpers_performance_mode.JPG ├── u5_mainc.JPG ├── u5_performance_mode_flag.JPG ├── u5_programmer_config.gif ├── u5energy_mode_flag.JPG └── u5xxitc.JPG /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at https://www.st.com/content/st_com/en/contact-us.html. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing guide 2 | 3 | The issues and the pull-requests are not supported to submit problems or suggestions related to the software delivered in this repository. The STM32WL_SubGHz_Phy_Basic_Tx_Rx example is being delivered as-is, not necessarily supported by ST. 4 | 5 | For any question related to the product, the hardware performance or characteristics, the tools, the environment, you can submit it to the ST Community on the STM32 MCUs related page. 6 | 7 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | | Component | Copyright | License | 2 | |:--------- |:---------- |:------- | 3 | | ST_tinyml_submissions | STMicroelectronics | Apache-2.0 | 4 | | Tinyml_api | EEMBC and the MLPerf Authors | Apache-2.0 | 5 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/anomaly_detection/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/anomaly_detection/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOG_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOG_CLK_DISABLE(); 80 | } 81 | 82 | void USART3_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart3); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 0 90 | HAL_UART_Receive_IT(&huart3, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void USART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN LPUART1_IRQn 0 */ 102 | 103 | /* USER CODE END LPUART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&huart1); 105 | /* USER CODE BEGIN LPUART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 1 107 | HAL_UART_Receive_IT(&huart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END LPUART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 0 116 | if (HAL_UART_Receive_IT(&huart3, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&huart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/anomaly_detection/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef huart1; 33 | extern UART_HandleTypeDef huart3; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/anomaly_detection/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE (AI_NETWORK_IN_1_SIZE * 4 * 2) // histogram window: 5 slices, 128 freq bins, float32 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_AD01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/image_classification/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/image_classification/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOG_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOG_CLK_DISABLE(); 80 | } 81 | 82 | void USART3_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart3); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 0 90 | HAL_UART_Receive_IT(&huart3, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void USART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN LPUART1_IRQn 0 */ 102 | 103 | /* USER CODE END LPUART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&huart1); 105 | /* USER CODE BEGIN LPUART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 1 107 | HAL_UART_Receive_IT(&huart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END LPUART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 0 116 | if (HAL_UART_Receive_IT(&huart3, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&huart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/image_classification/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef huart1; 33 | extern UART_HandleTypeDef huart3; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/image_classification/tinyml_api/submitter_implemented.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. The reporting logic 14 | in th_results is copied from the original in EEMBC. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief C++ implementations of submitter_implemented.h 18 | 19 | #include "submitter_implemented.h" 20 | #include "st_port.h" 21 | 22 | extern uint8_t gp_buff[MAX_DB_INPUT_SIZE]; 23 | extern size_t g_buff_size; 24 | extern size_t g_buff_pos; 25 | int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 26 | int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 27 | 28 | // Implement this method to prepare for inference and preprocess inputs. 29 | void th_load_tensor(void) 30 | { 31 | for (int i = 0; i < AI_NETWORK_IN_1_SIZE; i++) 32 | { 33 | aiInData_int[i] = (int8_t)(gp_buff[i]- 128); 34 | } 35 | } 36 | 37 | // Add to this method to return real inference results. 38 | void th_results(void) 39 | { 40 | int8_t out_zero_point = *((int8_t *)ai_output->meta_info->intq_info->info->zeropoint); 41 | float converted=0.0; 42 | float out_scale = *(ai_output->meta_info->intq_info->info->scale); 43 | const int kCategoryCount = ai_output->size; 44 | char char_res[10]; 45 | /** 46 | * The results need to be printed back in exactly this format; if easier 47 | * to just modify this loop than copy to results[] above, do that. 48 | */ 49 | th_printf("m-results-["); 50 | 51 | for (size_t i = 0; i < kCategoryCount; i++) 52 | { 53 | converted = DequantizeInt8ToFloat(aiOutData[i], out_scale, out_zero_point); 54 | snprintf(char_res, 10, "%0.3f",converted); 55 | th_printf(char_res); 56 | if (i < (kCategoryCount - 1)) 57 | { 58 | th_printf(","); 59 | } 60 | } 61 | th_printf("]\r\n"); 62 | } 63 | 64 | // Implement this method with the logic to perform one inference cycle. 65 | void th_infer(void) 66 | { 67 | AI_Run(aiInData_int, aiOutData); 68 | } 69 | 70 | /// \brief optional API. 71 | void th_final_initialize(void) 72 | { 73 | rxInit(); 74 | AI_Init(); 75 | /* Clock gating */ 76 | 77 | __HAL_RCC_AXIRAM1_CLKGA_ENABLE(); 78 | __HAL_RCC_AXIRAM2_CLKGA_ENABLE(); 79 | __HAL_RCC_AXIRAM3_CLKGA_ENABLE(); 80 | __HAL_RCC_AHB12_CLKGA_ENABLE(); 81 | __HAL_RCC_AHB34_CLKGA_ENABLE(); 82 | __HAL_RCC_FLIFT_CLKGA_ENABLE(); 83 | __HAL_RCC_ECCRAM_CLKGA_ENABLE(); 84 | 85 | __HAL_RCC_GPIOE_CLK_DISABLE(); 86 | __HAL_RCC_GPIOC_CLK_DISABLE(); 87 | __HAL_RCC_GPIOF_CLK_DISABLE(); 88 | __HAL_RCC_GPIOH_CLK_DISABLE(); 89 | __HAL_RCC_GPIOA_CLK_DISABLE(); 90 | __HAL_RCC_GPIOB_CLK_DISABLE(); 91 | 92 | 93 | #if EE_CFG_ENERGY_MODE == 1 94 | __HAL_RCC_GPIOD_CLK_DISABLE(); 95 | #endif 96 | 97 | 98 | } 99 | void th_pre() { 100 | 101 | HAL_NVIC_DisableIRQ(SysTick_IRQn); 102 | 103 | /* Gate UART Clock, Eabel */ 104 | #if EE_CFG_ENERGY_MODE == 1 105 | /* Disable Timer and GPIO_Out Timestamp */ 106 | __HAL_RCC_USART1_CLK_DISABLE(); 107 | __HAL_RCC_TIM5_CLK_DISABLE(); 108 | __HAL_RCC_GPIOG_CLK_DISABLE(); 109 | #else 110 | __HAL_RCC_USART3_CLK_DISABLE(); 111 | #endif 112 | } 113 | 114 | void th_post() { 115 | #if EE_CFG_ENERGY_MODE == 1 116 | /* Enable Timer and GPIO_Out Timestamp */ 117 | __HAL_RCC_USART1_CLK_ENABLE(); 118 | __HAL_RCC_TIM5_CLK_ENABLE(); 119 | __HAL_RCC_GPIOG_CLK_ENABLE(); 120 | #else 121 | __HAL_RCC_USART3_CLK_ENABLE(); 122 | #endif 123 | 124 | HAL_NVIC_EnableIRQ(SysTick_IRQn); 125 | } 126 | 127 | 128 | char g_cmd_buf_copy[EE_CMD_SIZE + 1]; 129 | void th_command_ready(char volatile *p_command) 130 | { 131 | if (p_command[0]) 132 | { 133 | th_memcpy(g_cmd_buf_copy, (char *) p_command, EE_CMD_SIZE); 134 | ee_serial_command_parser_callback(g_cmd_buf_copy); 135 | p_command[0] = 0; 136 | } 137 | } 138 | 139 | // th_libc implementations. 140 | int th_strncmp(const char *str1, const char *str2, size_t n) { 141 | return strncmp(str1, str2, n); 142 | } 143 | 144 | char *th_strncpy(char *dest, const char *src, size_t n) { 145 | return strncpy(dest, src, n); 146 | } 147 | 148 | size_t th_strnlen(const char *str, size_t maxlen) { 149 | return strnlen(str, maxlen); 150 | } 151 | 152 | char *th_strcat(char *dest, const char *src) { return strcat(dest, src); } 153 | 154 | char *th_strtok(char *str1, const char *sep) { return strtok(str1, sep); } 155 | 156 | int th_atoi(const char *str) { return atoi(str); } 157 | 158 | void *th_memset(void *b, int c, size_t len) { return memset(b, c, len); } 159 | 160 | void *th_memcpy(void *dst, const void *src, size_t n) { 161 | return memcpy(dst, src, n); 162 | } 163 | 164 | /* N.B.: Many embedded *printf SDKs do not support all format specifiers. */ 165 | int th_vprintf(const char *format, va_list ap) { return vprintf(format, ap); } 166 | 167 | uint8_t buffer [128]; 168 | void th_printf(const char* fmt, ...) 169 | { 170 | va_list ap; 171 | int n; 172 | va_start(ap, fmt); 173 | n = vsnprintf ((char*)buffer, 128, fmt, ap); 174 | va_end(ap); 175 | //notify_uart(buffer, n); 176 | #if EE_CFG_ENERGY_MODE == 1 177 | if(HAL_UART_Transmit(&huart1, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 178 | { 179 | Error_Handler(); 180 | } 181 | #else 182 | if(HAL_UART_Transmit(&huart3, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 183 | { 184 | Error_Handler(); 185 | } 186 | #endif 187 | 188 | } 189 | 190 | char th_getchar() { return getchar(); } 191 | 192 | void th_serialport_initialize(void) 193 | { 194 | /* Shut down unused UART, set pins to analog input */ 195 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 196 | GPIO_InitStruct.Pin = GPIO_PIN_All; 197 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 198 | GPIO_InitStruct.Pull = GPIO_NOPULL; 199 | 200 | #if EE_CFG_ENERGY_MODE == 1 201 | __HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE); 202 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); 203 | __HAL_RCC_USART3_CLK_DISABLE(); 204 | #else 205 | __HAL_UART_ENABLE_IT(&huart3,UART_IT_RXNE); 206 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 207 | __HAL_RCC_USART1_CLK_DISABLE(); 208 | #endif 209 | 210 | HAL_TIM_Base_Start(&htim5); 211 | } 212 | 213 | void th_timestamp(void) 214 | { 215 | #if EE_CFG_ENERGY_MODE == 1 216 | /* USER CODE 1 BEGIN */ 217 | /* Step 1. Pull pin low */ 218 | clear_Timestamp_Port(); 219 | /* Step 2. Hold low for at least 1us */ 220 | US_Delay(1); 221 | set_Timestamp_Port(); 222 | /* Step 3. Release driver */ 223 | /* USER CODE 1 END */ 224 | #else 225 | 226 | /* USER CODE 2 BEGIN */ 227 | unsigned long microSeconds = __HAL_TIM_GET_COUNTER(&htim5); 228 | /* USER CODE 2 END */ 229 | /* This message must NOT be changed. */ 230 | th_printf(EE_MSG_TIMESTAMP, microSeconds); 231 | #endif 232 | } 233 | 234 | void th_timestamp_initialize(void) 235 | { 236 | th_printf(EE_MSG_TIMESTAMP_MODE); 237 | th_timestamp(); 238 | 239 | } 240 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/image_classification/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE AI_NETWORK_IN_1_SIZE 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_IC01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/keyword_spotting/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/keyword_spotting/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOG_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOG_CLK_DISABLE(); 80 | } 81 | 82 | void USART3_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart3); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 0 90 | HAL_UART_Receive_IT(&huart3, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void USART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN LPUART1_IRQn 0 */ 102 | 103 | /* USER CODE END LPUART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&huart1); 105 | /* USER CODE BEGIN LPUART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 1 107 | HAL_UART_Receive_IT(&huart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END LPUART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 0 116 | if (HAL_UART_Receive_IT(&huart3, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&huart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/keyword_spotting/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef huart1; 33 | extern UART_HandleTypeDef huart3; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/keyword_spotting/tinyml_api/submitter_implemented.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. The reporting logic 14 | in th_results is copied from the original in EEMBC. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief C++ implementations of submitter_implemented.h 18 | 19 | #include "submitter_implemented.h" 20 | #include "st_port.h" 21 | 22 | extern uint8_t gp_buff[MAX_DB_INPUT_SIZE]; 23 | extern size_t g_buff_size; 24 | extern size_t g_buff_pos; 25 | int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 26 | int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 27 | 28 | // Implement this method to prepare for inference and preprocess inputs. 29 | void th_load_tensor(void) 30 | { 31 | for (int i = 0; i < AI_NETWORK_IN_1_SIZE; i++) 32 | { 33 | aiInData_int[i] = (int8_t)(gp_buff[i]); 34 | } 35 | } 36 | 37 | // Add to this method to return real inference results. 38 | void th_results(void) 39 | { 40 | int8_t out_zero_point = *((int8_t *)ai_output->meta_info->intq_info->info->zeropoint); 41 | float converted = 0.0; 42 | float out_scale = *(ai_output->meta_info->intq_info->info->scale); 43 | const int kCategoryCount = ai_output->size; 44 | char char_res[10]; 45 | /** 46 | * The results need to be printed back in exactly this format; if easier 47 | * to just modify this loop than copy to results[] above, do that. 48 | */ 49 | th_printf("m-results-["); 50 | 51 | for (size_t i = 0; i < kCategoryCount; i++) 52 | { 53 | converted = DequantizeInt8ToFloat(aiOutData[i], out_scale, out_zero_point); 54 | snprintf(char_res, 10, "%0.3f",converted); 55 | th_printf(char_res); 56 | if (i < (kCategoryCount - 1)) 57 | { 58 | th_printf(","); 59 | } 60 | } 61 | th_printf("]\r\n"); 62 | } 63 | 64 | // Implement this method with the logic to perform one inference cycle. 65 | void th_infer(void) 66 | { 67 | AI_Run(aiInData_int, aiOutData); 68 | } 69 | 70 | /// \brief optional API. 71 | void th_final_initialize(void) 72 | { 73 | rxInit(); 74 | AI_Init(); 75 | /* Clock gating */ 76 | 77 | __HAL_RCC_AXIRAM1_CLKGA_ENABLE(); 78 | __HAL_RCC_AXIRAM2_CLKGA_ENABLE(); 79 | __HAL_RCC_AXIRAM3_CLKGA_ENABLE(); 80 | __HAL_RCC_AHB12_CLKGA_ENABLE(); 81 | __HAL_RCC_AHB34_CLKGA_ENABLE(); 82 | __HAL_RCC_FLIFT_CLKGA_ENABLE(); 83 | __HAL_RCC_ECCRAM_CLKGA_ENABLE(); 84 | 85 | __HAL_RCC_GPIOE_CLK_DISABLE(); 86 | __HAL_RCC_GPIOC_CLK_DISABLE(); 87 | __HAL_RCC_GPIOF_CLK_DISABLE(); 88 | __HAL_RCC_GPIOH_CLK_DISABLE(); 89 | __HAL_RCC_GPIOA_CLK_DISABLE(); 90 | __HAL_RCC_GPIOB_CLK_DISABLE(); 91 | 92 | 93 | #if EE_CFG_ENERGY_MODE == 1 94 | __HAL_RCC_GPIOD_CLK_DISABLE(); 95 | #endif 96 | 97 | 98 | } 99 | void th_pre() { 100 | 101 | HAL_NVIC_DisableIRQ(SysTick_IRQn); 102 | 103 | /* Gate UART Clock, Eabel */ 104 | #if EE_CFG_ENERGY_MODE == 1 105 | /* Disable Timer and GPIO_Out Timestamp */ 106 | __HAL_RCC_USART1_CLK_DISABLE(); 107 | __HAL_RCC_TIM5_CLK_DISABLE(); 108 | __HAL_RCC_GPIOG_CLK_DISABLE(); 109 | #else 110 | __HAL_RCC_USART3_CLK_DISABLE(); 111 | #endif 112 | } 113 | 114 | void th_post() { 115 | #if EE_CFG_ENERGY_MODE == 1 116 | /* Enable Timer and GPIO_Out Timestamp */ 117 | __HAL_RCC_USART1_CLK_ENABLE(); 118 | __HAL_RCC_TIM5_CLK_ENABLE(); 119 | __HAL_RCC_GPIOG_CLK_ENABLE(); 120 | #else 121 | __HAL_RCC_USART3_CLK_ENABLE(); 122 | #endif 123 | 124 | HAL_NVIC_EnableIRQ(SysTick_IRQn); 125 | } 126 | 127 | 128 | char g_cmd_buf_copy[EE_CMD_SIZE + 1]; 129 | void th_command_ready(char volatile *p_command) 130 | { 131 | if (p_command[0]) 132 | { 133 | th_memcpy(g_cmd_buf_copy, (char *) p_command, EE_CMD_SIZE); 134 | ee_serial_command_parser_callback(g_cmd_buf_copy); 135 | p_command[0] = 0; 136 | } 137 | } 138 | 139 | // th_libc implementations. 140 | int th_strncmp(const char *str1, const char *str2, size_t n) { 141 | return strncmp(str1, str2, n); 142 | } 143 | 144 | char *th_strncpy(char *dest, const char *src, size_t n) { 145 | return strncpy(dest, src, n); 146 | } 147 | 148 | size_t th_strnlen(const char *str, size_t maxlen) { 149 | return strnlen(str, maxlen); 150 | } 151 | 152 | char *th_strcat(char *dest, const char *src) { return strcat(dest, src); } 153 | 154 | char *th_strtok(char *str1, const char *sep) { return strtok(str1, sep); } 155 | 156 | int th_atoi(const char *str) { return atoi(str); } 157 | 158 | void *th_memset(void *b, int c, size_t len) { return memset(b, c, len); } 159 | 160 | void *th_memcpy(void *dst, const void *src, size_t n) { 161 | return memcpy(dst, src, n); 162 | } 163 | 164 | /* N.B.: Many embedded *printf SDKs do not support all format specifiers. */ 165 | int th_vprintf(const char *format, va_list ap) { return vprintf(format, ap); } 166 | 167 | uint8_t buffer [128]; 168 | void th_printf(const char* fmt, ...) 169 | { 170 | va_list ap; 171 | int n; 172 | va_start(ap, fmt); 173 | n = vsnprintf ((char*)buffer, 128, fmt, ap); 174 | va_end(ap); 175 | //notify_uart(buffer, n); 176 | #if EE_CFG_ENERGY_MODE == 1 177 | if(HAL_UART_Transmit(&huart1, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 178 | { 179 | Error_Handler(); 180 | } 181 | #else 182 | if(HAL_UART_Transmit(&huart3, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 183 | { 184 | Error_Handler(); 185 | } 186 | #endif 187 | 188 | } 189 | 190 | char th_getchar() { return getchar(); } 191 | 192 | void th_serialport_initialize(void) 193 | { 194 | /* In performance mode, we talk directly to the DUT at 115200 baud */ 195 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 196 | GPIO_InitStruct.Pin = GPIO_PIN_All; 197 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 198 | GPIO_InitStruct.Pull = GPIO_NOPULL; 199 | 200 | #if EE_CFG_ENERGY_MODE == 1 201 | __HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE); 202 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); 203 | __HAL_RCC_USART3_CLK_DISABLE(); 204 | #else 205 | __HAL_UART_ENABLE_IT(&huart3,UART_IT_RXNE); 206 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 207 | __HAL_RCC_USART1_CLK_DISABLE(); 208 | #endif 209 | 210 | HAL_TIM_Base_Start(&htim5); 211 | } 212 | 213 | void th_timestamp(void) 214 | { 215 | #if EE_CFG_ENERGY_MODE == 1 216 | /* USER CODE 1 BEGIN */ 217 | /* Step 1. Pull pin low */ 218 | clear_Timestamp_Port(); 219 | /* Step 2. Hold low for at least 1us */ 220 | US_Delay(1); 221 | set_Timestamp_Port(); 222 | /* Step 3. Release driver */ 223 | /* USER CODE 1 END */ 224 | #else 225 | 226 | /* USER CODE 2 BEGIN */ 227 | unsigned long microSeconds = __HAL_TIM_GET_COUNTER(&htim5); 228 | /* USER CODE 2 END */ 229 | /* This message must NOT be changed. */ 230 | th_printf(EE_MSG_TIMESTAMP, microSeconds); 231 | #endif 232 | } 233 | 234 | void th_timestamp_initialize(void) 235 | { 236 | th_printf(EE_MSG_TIMESTAMP_MODE); 237 | th_timestamp(); 238 | 239 | } 240 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/keyword_spotting/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE AI_NETWORK_IN_1_SIZE 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_KWS01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/visual_wake_words/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/visual_wake_words/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOG_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOG_CLK_DISABLE(); 80 | } 81 | 82 | void USART3_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart3); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 0 90 | HAL_UART_Receive_IT(&huart3, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void USART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN USART1_IRQn 0 */ 102 | 103 | /* USER CODE END USART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&huart1); 105 | /* USER CODE BEGIN USART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 1 107 | HAL_UART_Receive_IT(&huart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END USART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 0 116 | if (HAL_UART_Receive_IT(&huart3, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&huart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/visual_wake_words/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef huart1; 33 | extern UART_HandleTypeDef huart3; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/visual_wake_words/tinyml_api/submitter_implemented.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. The reporting logic 14 | in th_results is copied from the original in EEMBC. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief C++ implementations of submitter_implemented.h 18 | 19 | #include "submitter_implemented.h" 20 | #include "st_port.h" 21 | 22 | extern uint8_t gp_buff[MAX_DB_INPUT_SIZE]; 23 | extern size_t g_buff_size; 24 | extern size_t g_buff_pos; 25 | int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 26 | int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 27 | 28 | // Implement this method to prepare for inference and preprocess inputs. 29 | void th_load_tensor(void) 30 | { 31 | for (int i = 0; i < AI_NETWORK_IN_1_SIZE; i++) 32 | { 33 | aiInData_int[i] = (int8_t)(gp_buff[i]- 128); 34 | } 35 | } 36 | 37 | // Add to this method to return real inference results. 38 | void th_results(void) 39 | { 40 | int8_t out_zero_point = *((int8_t *)ai_output->meta_info->intq_info->info->zeropoint); 41 | float converted = 0.0; 42 | float out_scale = *(ai_output->meta_info->intq_info->info->scale); 43 | const int kCategoryCount = ai_output->size; 44 | char char_res[10]; 45 | /** 46 | * The results need to be printed back in exactly this format; if easier 47 | * to just modify this loop than copy to results[] above, do that. 48 | */ 49 | th_printf("m-results-["); 50 | 51 | for (size_t i = 0; i < kCategoryCount; i++) 52 | { 53 | converted = DequantizeInt8ToFloat(aiOutData[i], out_scale, out_zero_point); 54 | snprintf(char_res, 10, "%0.3f",converted); 55 | th_printf(char_res); 56 | if (i < (kCategoryCount - 1)) 57 | { 58 | th_printf(","); 59 | } 60 | } 61 | th_printf("]\r\n"); 62 | } 63 | 64 | // Implement this method with the logic to perform one inference cycle. 65 | void th_infer(void) 66 | { 67 | AI_Run(aiInData_int, aiOutData); 68 | } 69 | 70 | /// \brief optional API. 71 | void th_final_initialize(void) 72 | { 73 | rxInit(); 74 | AI_Init(); 75 | /* Clock gating */ 76 | 77 | __HAL_RCC_AXIRAM1_CLKGA_ENABLE(); 78 | __HAL_RCC_AXIRAM2_CLKGA_ENABLE(); 79 | __HAL_RCC_AXIRAM3_CLKGA_ENABLE(); 80 | __HAL_RCC_AHB12_CLKGA_ENABLE(); 81 | __HAL_RCC_AHB34_CLKGA_ENABLE(); 82 | __HAL_RCC_FLIFT_CLKGA_ENABLE(); 83 | __HAL_RCC_ECCRAM_CLKGA_ENABLE(); 84 | 85 | __HAL_RCC_GPIOE_CLK_DISABLE(); 86 | __HAL_RCC_GPIOC_CLK_DISABLE(); 87 | __HAL_RCC_GPIOF_CLK_DISABLE(); 88 | __HAL_RCC_GPIOH_CLK_DISABLE(); 89 | __HAL_RCC_GPIOA_CLK_DISABLE(); 90 | __HAL_RCC_GPIOB_CLK_DISABLE(); 91 | 92 | 93 | #if EE_CFG_ENERGY_MODE == 1 94 | __HAL_RCC_GPIOD_CLK_DISABLE(); 95 | #endif 96 | 97 | 98 | } 99 | void th_pre() { 100 | 101 | HAL_NVIC_DisableIRQ(SysTick_IRQn); 102 | 103 | /* Gate UART Clock, Eabel */ 104 | #if EE_CFG_ENERGY_MODE == 1 105 | /* Disable Timer and GPIO_Out Timestamp */ 106 | __HAL_RCC_USART1_CLK_DISABLE(); 107 | __HAL_RCC_TIM5_CLK_DISABLE(); 108 | __HAL_RCC_GPIOG_CLK_DISABLE(); 109 | #else 110 | __HAL_RCC_USART3_CLK_DISABLE(); 111 | #endif 112 | } 113 | 114 | void th_post() { 115 | #if EE_CFG_ENERGY_MODE == 1 116 | /* Enable Timer and GPIO_Out Timestamp */ 117 | __HAL_RCC_USART1_CLK_ENABLE(); 118 | __HAL_RCC_TIM5_CLK_ENABLE(); 119 | __HAL_RCC_GPIOG_CLK_ENABLE(); 120 | #else 121 | __HAL_RCC_USART3_CLK_ENABLE(); 122 | #endif 123 | 124 | HAL_NVIC_EnableIRQ(SysTick_IRQn); 125 | } 126 | 127 | 128 | char g_cmd_buf_copy[EE_CMD_SIZE + 1]; 129 | void th_command_ready(char volatile *p_command) 130 | { 131 | if (p_command[0]) 132 | { 133 | th_memcpy(g_cmd_buf_copy, (char *) p_command, EE_CMD_SIZE); 134 | ee_serial_command_parser_callback(g_cmd_buf_copy); 135 | p_command[0] = 0; 136 | } 137 | } 138 | 139 | // th_libc implementations. 140 | int th_strncmp(const char *str1, const char *str2, size_t n) { 141 | return strncmp(str1, str2, n); 142 | } 143 | 144 | char *th_strncpy(char *dest, const char *src, size_t n) { 145 | return strncpy(dest, src, n); 146 | } 147 | 148 | size_t th_strnlen(const char *str, size_t maxlen) { 149 | return strnlen(str, maxlen); 150 | } 151 | 152 | char *th_strcat(char *dest, const char *src) { return strcat(dest, src); } 153 | 154 | char *th_strtok(char *str1, const char *sep) { return strtok(str1, sep); } 155 | 156 | int th_atoi(const char *str) { return atoi(str); } 157 | 158 | void *th_memset(void *b, int c, size_t len) { return memset(b, c, len); } 159 | 160 | void *th_memcpy(void *dst, const void *src, size_t n) { 161 | return memcpy(dst, src, n); 162 | } 163 | 164 | /* N.B.: Many embedded *printf SDKs do not support all format specifiers. */ 165 | int th_vprintf(const char *format, va_list ap) { return vprintf(format, ap); } 166 | 167 | uint8_t buffer [128]; 168 | void th_printf(const char* fmt, ...) 169 | { 170 | va_list ap; 171 | int n; 172 | va_start(ap, fmt); 173 | n = vsnprintf ((char*)buffer, 128, fmt, ap); 174 | va_end(ap); 175 | //notify_uart(buffer, n); 176 | #if EE_CFG_ENERGY_MODE == 1 177 | if(HAL_UART_Transmit(&huart1, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 178 | { 179 | Error_Handler(); 180 | } 181 | #else 182 | if(HAL_UART_Transmit(&huart3, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 183 | { 184 | Error_Handler(); 185 | } 186 | #endif 187 | 188 | } 189 | 190 | char th_getchar() { return getchar(); } 191 | 192 | void th_serialport_initialize(void) 193 | { 194 | /* Shut down unused UART, set pins to analog input */ 195 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 196 | GPIO_InitStruct.Pin = GPIO_PIN_All; 197 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 198 | GPIO_InitStruct.Pull = GPIO_NOPULL; 199 | 200 | #if EE_CFG_ENERGY_MODE == 1 201 | __HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE); 202 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); 203 | __HAL_RCC_USART3_CLK_DISABLE(); 204 | #else 205 | __HAL_UART_ENABLE_IT(&huart3,UART_IT_RXNE); 206 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 207 | __HAL_RCC_USART1_CLK_DISABLE(); 208 | #endif 209 | 210 | HAL_TIM_Base_Start(&htim5); 211 | } 212 | 213 | void th_timestamp(void) 214 | { 215 | #if EE_CFG_ENERGY_MODE == 1 216 | /* USER CODE 1 BEGIN */ 217 | /* Step 1. Pull pin low */ 218 | clear_Timestamp_Port(); 219 | /* Step 2. Hold low for at least 1us */ 220 | US_Delay(1); 221 | set_Timestamp_Port(); 222 | /* Step 3. Release driver */ 223 | /* USER CODE 1 END */ 224 | #else 225 | 226 | /* USER CODE 2 BEGIN */ 227 | unsigned long microSeconds = __HAL_TIM_GET_COUNTER(&htim5); 228 | /* USER CODE 2 END */ 229 | /* This message must NOT be changed. */ 230 | th_printf(EE_MSG_TIMESTAMP, microSeconds); 231 | #endif 232 | } 233 | 234 | void th_timestamp_initialize(void) 235 | { 236 | th_printf(EE_MSG_TIMESTAMP_MODE); 237 | th_timestamp(); 238 | 239 | } 240 | -------------------------------------------------------------------------------- /NUCLEO_H7A3ZI_Q/visual_wake_words/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE AI_NETWORK_IN_1_SIZE 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_VWW01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/anomaly_detection/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/anomaly_detection/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOF_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOF_CLK_DISABLE(); 80 | } 81 | 82 | void USART3_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart3); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 1 90 | HAL_UART_Receive_IT(&huart3, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void LPUART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN LPUART1_IRQn 0 */ 102 | 103 | /* USER CODE END LPUART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&hlpuart1); 105 | /* USER CODE BEGIN LPUART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 0 107 | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END LPUART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 1 116 | if (HAL_UART_Receive_IT(&huart3, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&hlpuart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/anomaly_detection/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef hlpuart1; 33 | extern UART_HandleTypeDef huart3; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/anomaly_detection/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE (AI_NETWORK_IN_1_SIZE * 4 * 2) // histogram window: 5 slices, 128 freq bins, float32 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_AD01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/image_classification/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/image_classification/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOF_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOF_CLK_DISABLE(); 80 | } 81 | 82 | void USART3_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart3); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 1 90 | HAL_UART_Receive_IT(&huart3, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void LPUART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN LPUART1_IRQn 0 */ 102 | 103 | /* USER CODE END LPUART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&hlpuart1); 105 | /* USER CODE BEGIN LPUART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 0 107 | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END LPUART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 1 116 | if (HAL_UART_Receive_IT(&huart3, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&hlpuart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/image_classification/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef hlpuart1; 33 | extern UART_HandleTypeDef huart3; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/image_classification/tinyml_api/submitter_implemented.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | This file reflects a modified version of th_lib from EEMBC. The reporting logic 13 | in th_results is copied from the original in EEMBC. 14 | ==============================================================================*/ 15 | /// \file 16 | /// \brief C++ implementations of submitter_implemented.h 17 | 18 | #include "submitter_implemented.h" 19 | #include "st_port.h" 20 | 21 | extern uint8_t gp_buff[MAX_DB_INPUT_SIZE]; 22 | extern size_t g_buff_size; 23 | extern size_t g_buff_pos; 24 | int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 25 | int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 26 | 27 | // Implement this method to prepare for inference and preprocess inputs. 28 | void th_load_tensor(void) 29 | { 30 | for (int i = 0; i < AI_NETWORK_IN_1_SIZE; i++) 31 | { 32 | aiInData_int[i] = (int8_t)(gp_buff[i]- 128); 33 | } 34 | } 35 | 36 | // Add to this method to return real inference results. 37 | void th_results(void) 38 | { 39 | int8_t out_zero_point = *((int8_t *)ai_output->meta_info->intq_info->info->zeropoint); 40 | float converted = 0.0; 41 | float out_scale = *(ai_output->meta_info->intq_info->info->scale); 42 | const int kCategoryCount = ai_output->size; 43 | char char_res[10]; 44 | /** 45 | * The results need to be printed back in exactly this format; if easier 46 | * to just modify this loop than copy to results[] above, do that. 47 | */ 48 | th_printf("m-results-["); 49 | 50 | for (size_t i = 0; i < kCategoryCount; i++) 51 | { 52 | converted = DequantizeInt8ToFloat(aiOutData[i], out_scale, out_zero_point); 53 | snprintf(char_res, 10, "%0.3f",converted); 54 | th_printf(char_res); 55 | if (i < (kCategoryCount - 1)) 56 | { 57 | th_printf(","); 58 | } 59 | } 60 | th_printf("]\r\n"); 61 | } 62 | 63 | // Implement this method with the logic to perform one inference cycle. 64 | void th_infer(void) 65 | { 66 | AI_Run(aiInData_int, aiOutData); 67 | } 68 | 69 | /// \brief optional API. 70 | void th_final_initialize(void) 71 | { 72 | rxInit(); 73 | AI_Init(); 74 | 75 | } 76 | void th_pre() { 77 | 78 | __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); 79 | HAL_NVIC_DisableIRQ(SysTick_IRQn); 80 | 81 | /* Gate UART Clock, Eabel */ 82 | #if EE_CFG_ENERGY_MODE == 1 83 | /* Disable Timer and GPIO_Out Timestamp */ 84 | 85 | __HAL_RCC_GPIOF_CLK_DISABLE(); 86 | __HAL_RCC_TIM5_CLK_DISABLE(); 87 | __HAL_RCC_USART3_CLK_DISABLE(); 88 | #else 89 | __HAL_RCC_LPUART1_CLK_DISABLE(); 90 | #endif 91 | } 92 | 93 | void th_post() { 94 | 95 | #if EE_CFG_ENERGY_MODE == 1 96 | /* Enable Timer and GPIO_Out Timestamp */ 97 | __HAL_RCC_USART3_CLK_ENABLE(); 98 | __HAL_RCC_TIM5_CLK_ENABLE(); 99 | __HAL_RCC_GPIOF_CLK_ENABLE(); 100 | #else 101 | __HAL_RCC_LPUART1_CLK_ENABLE(); 102 | #endif 103 | 104 | HAL_NVIC_EnableIRQ(SysTick_IRQn); 105 | } 106 | 107 | 108 | char g_cmd_buf_copy[EE_CMD_SIZE + 1]; 109 | void th_command_ready(char volatile *p_command) 110 | { 111 | if (p_command[0]) 112 | { 113 | th_memcpy(g_cmd_buf_copy, (char *) p_command, EE_CMD_SIZE); 114 | ee_serial_command_parser_callback(g_cmd_buf_copy); 115 | p_command[0] = 0; 116 | } 117 | } 118 | 119 | // th_libc implementations. 120 | int th_strncmp(const char *str1, const char *str2, size_t n) { 121 | return strncmp(str1, str2, n); 122 | } 123 | 124 | char *th_strncpy(char *dest, const char *src, size_t n) { 125 | return strncpy(dest, src, n); 126 | } 127 | 128 | size_t th_strnlen(const char *str, size_t maxlen) { 129 | return strnlen(str, maxlen); 130 | } 131 | 132 | char *th_strcat(char *dest, const char *src) { return strcat(dest, src); } 133 | 134 | char *th_strtok(char *str1, const char *sep) { return strtok(str1, sep); } 135 | 136 | int th_atoi(const char *str) { return atoi(str); } 137 | 138 | void *th_memset(void *b, int c, size_t len) { return memset(b, c, len); } 139 | 140 | void *th_memcpy(void *dst, const void *src, size_t n) { 141 | return memcpy(dst, src, n); 142 | } 143 | 144 | /* N.B.: Many embedded *printf SDKs do not support all format specifiers. */ 145 | int th_vprintf(const char *format, va_list ap) { return vprintf(format, ap); } 146 | 147 | uint8_t buffer [128]; 148 | void th_printf(const char* fmt, ...) 149 | { 150 | va_list ap; 151 | int n; 152 | va_start(ap, fmt); 153 | n = vsnprintf ((char*)buffer, 128, fmt, ap); 154 | va_end(ap); 155 | //notify_uart(buffer, n); 156 | #if EE_CFG_ENERGY_MODE == 1 157 | if(HAL_UART_Transmit(&huart3, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 158 | { 159 | Error_Handler(); 160 | } 161 | #else 162 | if(HAL_UART_Transmit(&hlpuart1, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 163 | { 164 | Error_Handler(); 165 | } 166 | #endif 167 | 168 | } 169 | 170 | char th_getchar() { return getchar(); } 171 | 172 | void th_serialport_initialize(void) 173 | { 174 | /* Shut down unused UART, set pins to analog input */ 175 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 176 | GPIO_InitStruct.Pin = GPIO_PIN_All; 177 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 178 | GPIO_InitStruct.Pull = GPIO_NOPULL; 179 | 180 | #if EE_CFG_ENERGY_MODE == 1 181 | __HAL_UART_ENABLE_IT(&huart3,UART_IT_RXNE); 182 | HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); 183 | __HAL_RCC_LPUART1_CLK_DISABLE(); 184 | #else 185 | __HAL_UART_ENABLE_IT(&hlpuart1,UART_IT_RXNE); 186 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); 187 | __HAL_RCC_USART3_CLK_DISABLE(); 188 | #endif 189 | 190 | /* Start Timer */ 191 | HAL_TIM_Base_Start(&htim5); 192 | 193 | HAL_PWREx_DisableSRAM3ContentRetention(); 194 | HAL_PWREx_DisableSRAM2ContentRetention(); 195 | 196 | /* Gate clocks for unused pins */ 197 | __HAL_RCC_RNG_CLK_DISABLE(); 198 | __HAL_RCC_GPIOA_CLK_DISABLE(); 199 | __HAL_RCC_GPIOB_CLK_DISABLE(); 200 | __HAL_RCC_GPIOC_CLK_DISABLE(); 201 | __HAL_RCC_GPIOD_CLK_DISABLE(); 202 | __HAL_RCC_GPIOE_CLK_DISABLE(); 203 | __HAL_RCC_GPIOG_CLK_DISABLE(); 204 | __HAL_RCC_GPIOH_CLK_DISABLE(); 205 | __HAL_RCC_GPIOI_CLK_DISABLE(); 206 | 207 | } 208 | 209 | void th_timestamp(void) 210 | { 211 | #if EE_CFG_ENERGY_MODE == 1 212 | /* USER CODE 1 BEGIN */ 213 | /* Step 1. Pull pin low */ 214 | clear_Timestamp_Port(); 215 | /* Step 2. Hold low for at least 1us */ 216 | US_Delay(1); 217 | set_Timestamp_Port(); 218 | /* Step 3. Release driver */ 219 | /* USER CODE 1 END */ 220 | #else 221 | 222 | /* USER CODE 2 BEGIN */ 223 | unsigned long microSeconds = __HAL_TIM_GET_COUNTER(&htim5); 224 | /* USER CODE 2 END */ 225 | /* This message must NOT be changed. */ 226 | th_printf(EE_MSG_TIMESTAMP, microSeconds); 227 | #endif 228 | } 229 | 230 | void th_timestamp_initialize(void) 231 | { 232 | th_printf(EE_MSG_TIMESTAMP_MODE); 233 | th_timestamp(); 234 | 235 | } 236 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/image_classification/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE AI_NETWORK_IN_1_SIZE 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_IC01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/keyword_spotting/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/keyword_spotting/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOF_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOF_CLK_DISABLE(); 80 | } 81 | 82 | void USART3_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart3); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 1 90 | HAL_UART_Receive_IT(&huart3, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void LPUART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN LPUART1_IRQn 0 */ 102 | 103 | /* USER CODE END LPUART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&hlpuart1); 105 | /* USER CODE BEGIN LPUART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 0 107 | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END LPUART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 1 116 | if (HAL_UART_Receive_IT(&huart3, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&hlpuart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/keyword_spotting/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef hlpuart1; 33 | extern UART_HandleTypeDef huart3; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/keyword_spotting/tinyml_api/submitter_implemented.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | This file reflects a modified version of th_lib from EEMBC. The reporting logic 13 | in th_results is copied from the original in EEMBC. 14 | ==============================================================================*/ 15 | /// \file 16 | /// \brief C++ implementations of submitter_implemented.h 17 | 18 | #include "submitter_implemented.h" 19 | #include "st_port.h" 20 | 21 | extern uint8_t gp_buff[MAX_DB_INPUT_SIZE]; 22 | extern size_t g_buff_size; 23 | extern size_t g_buff_pos; 24 | int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 25 | int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 26 | 27 | // Implement this method to prepare for inference and preprocess inputs. 28 | void th_load_tensor(void) 29 | { 30 | for (int i = 0; i < AI_NETWORK_IN_1_SIZE; i++) 31 | { 32 | aiInData_int[i] = (int8_t)(gp_buff[i]); 33 | } 34 | } 35 | 36 | // Add to this method to return real inference results. 37 | void th_results(void) 38 | { 39 | int8_t out_zero_point = *((int8_t *)ai_output->meta_info->intq_info->info->zeropoint); 40 | float converted = 0.0; 41 | float out_scale = *(ai_output->meta_info->intq_info->info->scale); 42 | const int kCategoryCount = ai_output->size; 43 | char char_res[10]; 44 | /** 45 | * The results need to be printed back in exactly this format; if easier 46 | * to just modify this loop than copy to results[] above, do that. 47 | */ 48 | th_printf("m-results-["); 49 | 50 | for (size_t i = 0; i < kCategoryCount; i++) 51 | { 52 | converted = DequantizeInt8ToFloat(aiOutData[i], out_scale, out_zero_point); 53 | snprintf(char_res, 10, "%0.3f",converted); 54 | th_printf(char_res); 55 | if (i < (kCategoryCount - 1)) 56 | { 57 | th_printf(","); 58 | } 59 | } 60 | th_printf("]\r\n"); 61 | } 62 | 63 | // Implement this method with the logic to perform one inference cycle. 64 | void th_infer(void) 65 | { 66 | AI_Run(aiInData_int, aiOutData); 67 | } 68 | 69 | /// \brief optional API. 70 | void th_final_initialize(void) 71 | { 72 | rxInit(); 73 | AI_Init(); 74 | 75 | } 76 | void th_pre() { 77 | 78 | __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); 79 | HAL_NVIC_DisableIRQ(SysTick_IRQn); 80 | 81 | /* Gate UART Clock, Eabel */ 82 | #if EE_CFG_ENERGY_MODE == 1 83 | /* Disable Timer and GPIO_Out Timestamp */ 84 | 85 | __HAL_RCC_GPIOF_CLK_DISABLE(); 86 | __HAL_RCC_TIM5_CLK_DISABLE(); 87 | __HAL_RCC_USART3_CLK_DISABLE(); 88 | #else 89 | __HAL_RCC_LPUART1_CLK_DISABLE(); 90 | #endif 91 | } 92 | 93 | void th_post() { 94 | 95 | #if EE_CFG_ENERGY_MODE == 1 96 | /* Enable Timer and GPIO_Out Timestamp */ 97 | __HAL_RCC_USART3_CLK_ENABLE(); 98 | __HAL_RCC_TIM5_CLK_ENABLE(); 99 | __HAL_RCC_GPIOF_CLK_ENABLE(); 100 | #else 101 | __HAL_RCC_LPUART1_CLK_ENABLE(); 102 | #endif 103 | 104 | HAL_NVIC_EnableIRQ(SysTick_IRQn); 105 | } 106 | 107 | 108 | char g_cmd_buf_copy[EE_CMD_SIZE + 1]; 109 | void th_command_ready(char volatile *p_command) 110 | { 111 | if (p_command[0]) 112 | { 113 | th_memcpy(g_cmd_buf_copy, (char *) p_command, EE_CMD_SIZE); 114 | ee_serial_command_parser_callback(g_cmd_buf_copy); 115 | p_command[0] = 0; 116 | } 117 | } 118 | 119 | // th_libc implementations. 120 | int th_strncmp(const char *str1, const char *str2, size_t n) { 121 | return strncmp(str1, str2, n); 122 | } 123 | 124 | char *th_strncpy(char *dest, const char *src, size_t n) { 125 | return strncpy(dest, src, n); 126 | } 127 | 128 | size_t th_strnlen(const char *str, size_t maxlen) { 129 | return strnlen(str, maxlen); 130 | } 131 | 132 | char *th_strcat(char *dest, const char *src) { return strcat(dest, src); } 133 | 134 | char *th_strtok(char *str1, const char *sep) { return strtok(str1, sep); } 135 | 136 | int th_atoi(const char *str) { return atoi(str); } 137 | 138 | void *th_memset(void *b, int c, size_t len) { return memset(b, c, len); } 139 | 140 | void *th_memcpy(void *dst, const void *src, size_t n) { 141 | return memcpy(dst, src, n); 142 | } 143 | 144 | /* N.B.: Many embedded *printf SDKs do not support all format specifiers. */ 145 | int th_vprintf(const char *format, va_list ap) { return vprintf(format, ap); } 146 | 147 | uint8_t buffer [128]; 148 | void th_printf(const char* fmt, ...) 149 | { 150 | va_list ap; 151 | int n; 152 | va_start(ap, fmt); 153 | n = vsnprintf ((char*)buffer, 128, fmt, ap); 154 | va_end(ap); 155 | //notify_uart(buffer, n); 156 | #if EE_CFG_ENERGY_MODE == 1 157 | if(HAL_UART_Transmit(&huart3, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 158 | { 159 | Error_Handler(); 160 | } 161 | #else 162 | if(HAL_UART_Transmit(&hlpuart1, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 163 | { 164 | Error_Handler(); 165 | } 166 | #endif 167 | 168 | } 169 | 170 | char th_getchar() { return getchar(); } 171 | 172 | void th_serialport_initialize(void) 173 | { 174 | /* Shut down unused UART, set pins to analog input */ 175 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 176 | GPIO_InitStruct.Pin = GPIO_PIN_All; 177 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 178 | GPIO_InitStruct.Pull = GPIO_NOPULL; 179 | 180 | #if EE_CFG_ENERGY_MODE == 1 181 | __HAL_UART_ENABLE_IT(&huart3,UART_IT_RXNE); 182 | HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); 183 | __HAL_RCC_LPUART1_CLK_DISABLE(); 184 | #else 185 | __HAL_UART_ENABLE_IT(&hlpuart1,UART_IT_RXNE); 186 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); 187 | __HAL_RCC_USART3_CLK_DISABLE(); 188 | #endif 189 | 190 | /* Start Timer */ 191 | HAL_TIM_Base_Start(&htim5); 192 | 193 | HAL_PWREx_DisableSRAM3ContentRetention(); 194 | HAL_PWREx_DisableSRAM2ContentRetention(); 195 | 196 | /* Gate clocks for unused pins */ 197 | __HAL_RCC_RNG_CLK_DISABLE(); 198 | __HAL_RCC_GPIOA_CLK_DISABLE(); 199 | __HAL_RCC_GPIOB_CLK_DISABLE(); 200 | __HAL_RCC_GPIOC_CLK_DISABLE(); 201 | __HAL_RCC_GPIOD_CLK_DISABLE(); 202 | __HAL_RCC_GPIOE_CLK_DISABLE(); 203 | __HAL_RCC_GPIOG_CLK_DISABLE(); 204 | __HAL_RCC_GPIOH_CLK_DISABLE(); 205 | __HAL_RCC_GPIOI_CLK_DISABLE(); 206 | 207 | } 208 | 209 | void th_timestamp(void) 210 | { 211 | #if EE_CFG_ENERGY_MODE == 1 212 | /* USER CODE 1 BEGIN */ 213 | /* Step 1. Pull pin low */ 214 | clear_Timestamp_Port(); 215 | /* Step 2. Hold low for at least 1us */ 216 | US_Delay(1); 217 | set_Timestamp_Port(); 218 | /* Step 3. Release driver */ 219 | /* USER CODE 1 END */ 220 | #else 221 | 222 | /* USER CODE 2 BEGIN */ 223 | unsigned long microSeconds = __HAL_TIM_GET_COUNTER(&htim5); 224 | /* USER CODE 2 END */ 225 | /* This message must NOT be changed. */ 226 | th_printf(EE_MSG_TIMESTAMP, microSeconds); 227 | #endif 228 | } 229 | 230 | void th_timestamp_initialize(void) 231 | { 232 | th_printf(EE_MSG_TIMESTAMP_MODE); 233 | th_timestamp(); 234 | 235 | } 236 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/keyword_spotting/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE AI_NETWORK_IN_1_SIZE 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_KWS01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/visual_wake_words/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/visual_wake_words/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOF_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOF_CLK_DISABLE(); 80 | } 81 | 82 | void USART3_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart3); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 1 90 | HAL_UART_Receive_IT(&huart3, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void LPUART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN LPUART1_IRQn 0 */ 102 | 103 | /* USER CODE END LPUART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&hlpuart1); 105 | /* USER CODE BEGIN LPUART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 0 107 | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END LPUART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 1 116 | if (HAL_UART_Receive_IT(&huart3, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&hlpuart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/visual_wake_words/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef hlpuart1; 33 | extern UART_HandleTypeDef huart3; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/visual_wake_words/tinyml_api/submitter_implemented.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | This file reflects a modified version of th_lib from EEMBC. The reporting logic 13 | in th_results is copied from the original in EEMBC. 14 | ==============================================================================*/ 15 | /// \file 16 | /// \brief C++ implementations of submitter_implemented.h 17 | 18 | #include "submitter_implemented.h" 19 | #include "st_port.h" 20 | 21 | extern uint8_t gp_buff[MAX_DB_INPUT_SIZE]; 22 | extern size_t g_buff_size; 23 | extern size_t g_buff_pos; 24 | int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 25 | int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 26 | 27 | // Implement this method to prepare for inference and preprocess inputs. 28 | void th_load_tensor(void) 29 | { 30 | for (int i = 0; i < AI_NETWORK_IN_1_SIZE; i++) 31 | { 32 | aiInData_int[i] = (int8_t)(gp_buff[i]- 128); 33 | } 34 | } 35 | 36 | // Add to this method to return real inference results. 37 | void th_results(void) 38 | { 39 | int8_t out_zero_point = *((int8_t *)ai_output->meta_info->intq_info->info->zeropoint); 40 | float converted = 0.0; 41 | float out_scale = *(ai_output->meta_info->intq_info->info->scale); 42 | const int kCategoryCount = ai_output->size; 43 | char char_res[10]; 44 | /** 45 | * The results need to be printed back in exactly this format; if easier 46 | * to just modify this loop than copy to results[] above, do that. 47 | */ 48 | th_printf("m-results-["); 49 | 50 | for (size_t i = 0; i < kCategoryCount; i++) 51 | { 52 | converted = DequantizeInt8ToFloat(aiOutData[i], out_scale, out_zero_point); 53 | snprintf(char_res, 10, "%0.3f",converted); 54 | th_printf(char_res); 55 | if (i < (kCategoryCount - 1)) 56 | { 57 | th_printf(","); 58 | } 59 | } 60 | th_printf("]\r\n"); 61 | } 62 | 63 | // Implement this method with the logic to perform one inference cycle. 64 | void th_infer(void) 65 | { 66 | AI_Run(aiInData_int, aiOutData); 67 | } 68 | 69 | /// \brief optional API. 70 | void th_final_initialize(void) 71 | { 72 | rxInit(); 73 | AI_Init(); 74 | 75 | } 76 | void th_pre() { 77 | 78 | __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); 79 | HAL_NVIC_DisableIRQ(SysTick_IRQn); 80 | 81 | /* Gate UART Clock, Eabel */ 82 | #if EE_CFG_ENERGY_MODE == 1 83 | /* Disable Timer and GPIO_Out Timestamp */ 84 | 85 | __HAL_RCC_GPIOF_CLK_DISABLE(); 86 | __HAL_RCC_TIM5_CLK_DISABLE(); 87 | __HAL_RCC_USART3_CLK_DISABLE(); 88 | #else 89 | __HAL_RCC_LPUART1_CLK_DISABLE(); 90 | #endif 91 | } 92 | 93 | void th_post() { 94 | 95 | #if EE_CFG_ENERGY_MODE == 1 96 | /* Enable Timer and GPIO_Out Timestamp */ 97 | __HAL_RCC_USART3_CLK_ENABLE(); 98 | __HAL_RCC_TIM5_CLK_ENABLE(); 99 | __HAL_RCC_GPIOF_CLK_ENABLE(); 100 | #else 101 | __HAL_RCC_LPUART1_CLK_ENABLE(); 102 | #endif 103 | 104 | HAL_NVIC_EnableIRQ(SysTick_IRQn); 105 | } 106 | 107 | 108 | char g_cmd_buf_copy[EE_CMD_SIZE + 1]; 109 | void th_command_ready(char volatile *p_command) 110 | { 111 | if (p_command[0]) 112 | { 113 | th_memcpy(g_cmd_buf_copy, (char *) p_command, EE_CMD_SIZE); 114 | ee_serial_command_parser_callback(g_cmd_buf_copy); 115 | p_command[0] = 0; 116 | } 117 | } 118 | 119 | // th_libc implementations. 120 | int th_strncmp(const char *str1, const char *str2, size_t n) { 121 | return strncmp(str1, str2, n); 122 | } 123 | 124 | char *th_strncpy(char *dest, const char *src, size_t n) { 125 | return strncpy(dest, src, n); 126 | } 127 | 128 | size_t th_strnlen(const char *str, size_t maxlen) { 129 | return strnlen(str, maxlen); 130 | } 131 | 132 | char *th_strcat(char *dest, const char *src) { return strcat(dest, src); } 133 | 134 | char *th_strtok(char *str1, const char *sep) { return strtok(str1, sep); } 135 | 136 | int th_atoi(const char *str) { return atoi(str); } 137 | 138 | void *th_memset(void *b, int c, size_t len) { return memset(b, c, len); } 139 | 140 | void *th_memcpy(void *dst, const void *src, size_t n) { 141 | return memcpy(dst, src, n); 142 | } 143 | 144 | /* N.B.: Many embedded *printf SDKs do not support all format specifiers. */ 145 | int th_vprintf(const char *format, va_list ap) { return vprintf(format, ap); } 146 | 147 | uint8_t buffer [128]; 148 | void th_printf(const char* fmt, ...) 149 | { 150 | va_list ap; 151 | int n; 152 | va_start(ap, fmt); 153 | n = vsnprintf ((char*)buffer, 128, fmt, ap); 154 | va_end(ap); 155 | //notify_uart(buffer, n); 156 | #if EE_CFG_ENERGY_MODE == 1 157 | if(HAL_UART_Transmit(&huart3, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 158 | { 159 | Error_Handler(); 160 | } 161 | #else 162 | if(HAL_UART_Transmit(&hlpuart1, (uint8_t*)buffer, n, HAL_MAX_DELAY) != HAL_OK) 163 | { 164 | Error_Handler(); 165 | } 166 | #endif 167 | 168 | } 169 | 170 | char th_getchar() { return getchar(); } 171 | 172 | void th_serialport_initialize(void) 173 | { 174 | /* Shut down unused UART, set pins to analog input */ 175 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 176 | GPIO_InitStruct.Pin = GPIO_PIN_All; 177 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; 178 | GPIO_InitStruct.Pull = GPIO_NOPULL; 179 | 180 | #if EE_CFG_ENERGY_MODE == 1 181 | __HAL_UART_ENABLE_IT(&huart3,UART_IT_RXNE); 182 | HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); 183 | __HAL_RCC_LPUART1_CLK_DISABLE(); 184 | #else 185 | __HAL_UART_ENABLE_IT(&hlpuart1,UART_IT_RXNE); 186 | HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); 187 | __HAL_RCC_USART3_CLK_DISABLE(); 188 | #endif 189 | 190 | /* Start Timer */ 191 | HAL_TIM_Base_Start(&htim5); 192 | 193 | HAL_PWREx_DisableSRAM3ContentRetention(); 194 | HAL_PWREx_DisableSRAM2ContentRetention(); 195 | 196 | /* Gate clocks for unused pins */ 197 | __HAL_RCC_RNG_CLK_DISABLE(); 198 | __HAL_RCC_GPIOA_CLK_DISABLE(); 199 | __HAL_RCC_GPIOB_CLK_DISABLE(); 200 | __HAL_RCC_GPIOC_CLK_DISABLE(); 201 | __HAL_RCC_GPIOD_CLK_DISABLE(); 202 | __HAL_RCC_GPIOE_CLK_DISABLE(); 203 | __HAL_RCC_GPIOG_CLK_DISABLE(); 204 | __HAL_RCC_GPIOH_CLK_DISABLE(); 205 | __HAL_RCC_GPIOI_CLK_DISABLE(); 206 | 207 | } 208 | 209 | void th_timestamp(void) 210 | { 211 | #if EE_CFG_ENERGY_MODE == 1 212 | /* USER CODE 1 BEGIN */ 213 | /* Step 1. Pull pin low */ 214 | clear_Timestamp_Port(); 215 | /* Step 2. Hold low for at least 1us */ 216 | US_Delay(1); 217 | set_Timestamp_Port(); 218 | /* Step 3. Release driver */ 219 | /* USER CODE 1 END */ 220 | #else 221 | 222 | /* USER CODE 2 BEGIN */ 223 | unsigned long microSeconds = __HAL_TIM_GET_COUNTER(&htim5); 224 | /* USER CODE 2 END */ 225 | /* This message must NOT be changed. */ 226 | th_printf(EE_MSG_TIMESTAMP, microSeconds); 227 | #endif 228 | } 229 | 230 | void th_timestamp_initialize(void) 231 | { 232 | th_printf(EE_MSG_TIMESTAMP_MODE); 233 | th_timestamp(); 234 | 235 | } 236 | -------------------------------------------------------------------------------- /NUCLEO_L4R5ZI/visual_wake_words/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE AI_NETWORK_IN_1_SIZE 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_VWW01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/anomaly_detection/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/anomaly_detection/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOF_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOF_CLK_DISABLE(); 80 | } 81 | 82 | void USART1_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart1); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 0 90 | HAL_UART_Receive_IT(&huart1, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void LPUART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN LPUART1_IRQn 0 */ 102 | 103 | /* USER CODE END LPUART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&hlpuart1); 105 | /* USER CODE BEGIN LPUART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 1 107 | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END LPUART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 0 116 | if (HAL_UART_Receive_IT(&huart1, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&hlpuart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/anomaly_detection/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef hlpuart1; 33 | extern UART_HandleTypeDef huart1; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/anomaly_detection/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE (AI_NETWORK_IN_1_SIZE * 4 * 2) // histogram window: 5 slices, 128 freq bins, float32 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_AD01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/image_classification/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/image_classification/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOF_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOF_CLK_DISABLE(); 80 | } 81 | 82 | void USART1_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart1); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 0 90 | HAL_UART_Receive_IT(&huart1, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void LPUART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN LPUART1_IRQn 0 */ 102 | 103 | /* USER CODE END LPUART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&hlpuart1); 105 | /* USER CODE BEGIN LPUART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 1 107 | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END LPUART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 0 116 | if (HAL_UART_Receive_IT(&huart1, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&hlpuart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/image_classification/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef hlpuart1; 33 | extern UART_HandleTypeDef huart1; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/image_classification/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE AI_NETWORK_IN_1_SIZE 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_IC01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/keyword_spotting/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/keyword_spotting/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOF_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOF_CLK_DISABLE(); 80 | } 81 | 82 | void USART1_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart1); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 0 90 | HAL_UART_Receive_IT(&huart1, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void LPUART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN LPUART1_IRQn 0 */ 102 | 103 | /* USER CODE END LPUART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&hlpuart1); 105 | /* USER CODE BEGIN LPUART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 1 107 | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END LPUART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 0 116 | if (HAL_UART_Receive_IT(&huart1, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&hlpuart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/keyword_spotting/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef hlpuart1; 33 | extern UART_HandleTypeDef huart1; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/keyword_spotting/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE AI_NETWORK_IN_1_SIZE 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_KWS01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/visual_wake_words/tinyml_api/internally_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file is a modified version of the original EEMBC implementation of ee_lib. 14 | The file name has been changed and some functions removed. 15 | ==============================================================================*/ 16 | 17 | /// \file 18 | /// \brief Internally-implemented methods required to perform inference. 19 | 20 | #include 21 | #include 22 | 23 | #ifndef MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 24 | #define MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ 25 | 26 | #define EE_MONITOR_VERSION "2.2.0" 27 | #define EE_FW_VERSION "ULPMark for tinyML Firmware V0.0.1" 28 | 29 | /* Version 1.0 of the benchmark only supports these models */ 30 | #define EE_MODEL_VERSION_KWS01 "kws01" 31 | #define EE_MODEL_VERSION_VWW01 "vww01" 32 | #define EE_MODEL_VERSION_AD01 "ad01" 33 | #define EE_MODEL_VERSION_IC01 "ic01" 34 | 35 | typedef enum { EE_ARG_CLAIMED, EE_ARG_UNCLAIMED } arg_claimed_t; 36 | typedef enum { EE_STATUS_OK = 0, EE_STATUS_ERROR } ee_status_t; 37 | 38 | #define EE_DEVICE_NAME "dut" 39 | 40 | #define EE_CMD_SIZE 80u 41 | #define EE_CMD_DELIMITER " " 42 | #define EE_CMD_TERMINATOR '%' 43 | 44 | #define EE_CMD_NAME "name" 45 | #define EE_CMD_TIMESTAMP "timestamp" 46 | 47 | #define EE_MSG_READY "m-ready\r\n" 48 | #define EE_MSG_INIT_DONE "m-init-done\r\n" 49 | #define EE_MSG_NAME "m-name-%s-[%s]\r\n" 50 | 51 | #define EE_ERR_CMD "e-[Unknown command: %s]\r\n" 52 | 53 | void ee_serial_callback(char); 54 | void ee_serial_command_parser_callback(char *); 55 | void ee_benchmark_initialize(void); 56 | long ee_hexdec(char *); 57 | void ee_infer(size_t n, size_t n_warmup); 58 | size_t ee_get_buffer(uint8_t* buffer, size_t max_len); 59 | arg_claimed_t ee_buffer_parse(char *command); 60 | arg_claimed_t ee_profile_parse(char *command); 61 | 62 | #endif /* MLPERF_TINY_V0_1_API_INTERNALLY_IMPLEMENTED_H_ */ -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/visual_wake_words/tinyml_api/st_port.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.c 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #include"st_port.h" 22 | #include "internally_implemented.h" 23 | #include "submitter_implemented.h" 24 | 25 | static char eembc_char=0; 26 | uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 27 | ai_handle network ; 28 | 29 | ai_buffer* ai_input; 30 | ai_buffer* ai_output; 31 | 32 | 33 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point) 34 | { 35 | return ((float)value - (float) zero_point) * scale; 36 | } 37 | 38 | 39 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point) 40 | { 41 | 42 | float result = (value / scale) + (float) zero_point; 43 | 44 | return SATURATE_FLOAT_TO_INT8(result); 45 | } 46 | 47 | //=============================================== AI INIT ================================================ 48 | ai_handle act_addr[] = {pool0}; 49 | void AI_Init() 50 | { 51 | /* Create and initialize an instance of the model */ 52 | ai_network_create_and_init(&network, act_addr, NULL); 53 | ai_input = ai_network_inputs_get(network, NULL); 54 | ai_output = ai_network_outputs_get(network, NULL); 55 | } 56 | 57 | void AI_Run(int8_t *pIn, int8_t *pOut) 58 | { 59 | ai_input[0].data = AI_HANDLE_PTR(pIn); 60 | ai_output[0].data = AI_HANDLE_PTR(pOut); 61 | ai_network_run(network, ai_input, ai_output); 62 | } 63 | //============================================================================================================ 64 | 65 | void US_Delay(uint8_t us) // microseconds 66 | { 67 | int startTick = __HAL_TIM_GET_COUNTER(&htim5); 68 | while (__HAL_TIM_GET_COUNTER(&htim5) - startTick < us); 69 | } 70 | 71 | void clear_Timestamp_Port(void) 72 | { 73 | __HAL_RCC_GPIOF_CLK_ENABLE(); 74 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_RESET); 75 | } 76 | void set_Timestamp_Port(void) 77 | { 78 | HAL_GPIO_WritePin(GPIOF, GPIO_PIN_13, GPIO_PIN_SET); 79 | __HAL_RCC_GPIOF_CLK_DISABLE(); 80 | } 81 | 82 | void USART1_IRQHandler(void) 83 | { 84 | /* USER CODE BEGIN USART3_IRQn 0 */ 85 | 86 | /* USER CODE END USART3_IRQn 0 */ 87 | HAL_UART_IRQHandler(&huart1); 88 | /* USER CODE BEGIN USART3_IRQn 1 */ 89 | #if EE_CFG_ENERGY_MODE == 0 90 | HAL_UART_Receive_IT(&huart1, (uint8_t *)&eembc_char, 1); 91 | ee_serial_callback(eembc_char); 92 | #endif 93 | /* USER CODE END USART3_IRQn 1 */ 94 | } 95 | 96 | /** 97 | * @brief This function handles LPUART1 global interrupt. 98 | */ 99 | void LPUART1_IRQHandler(void) 100 | { 101 | /* USER CODE BEGIN LPUART1_IRQn 0 */ 102 | 103 | /* USER CODE END LPUART1_IRQn 0 */ 104 | HAL_UART_IRQHandler(&hlpuart1); 105 | /* USER CODE BEGIN LPUART1_IRQn 1 */ 106 | #if EE_CFG_ENERGY_MODE == 1 107 | HAL_UART_Receive_IT(&hlpuart1, (uint8_t *)&eembc_char, 1); 108 | ee_serial_callback(eembc_char); 109 | #endif 110 | /* USER CODE END LPUART1_IRQn 1 */ 111 | } 112 | 113 | void rxSetInterrupt(uint8_t *pData) 114 | { 115 | #if EE_CFG_ENERGY_MODE == 0 116 | if (HAL_UART_Receive_IT(&huart1, pData, 1) != HAL_OK) 117 | { 118 | Error_Handler(); 119 | } 120 | #else 121 | if (HAL_UART_Receive_IT(&hlpuart1, pData, 1) != HAL_OK) 122 | { 123 | Error_Handler(); 124 | } 125 | #endif 126 | } 127 | void rxInit(void) 128 | { 129 | rxSetInterrupt((uint8_t *)&eembc_char); 130 | } 131 | 132 | /* 133 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle) 134 | { 135 | if (UartHandle->Instance == USART2) 136 | { 137 | ee_serial_callback(g_rxByte); 138 | rxSetInterrupt((uint8_t *)&g_rxByte); 139 | } 140 | } 141 | */ 142 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/visual_wake_words/tinyml_api/st_port.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : st_port.h 5 | * @brief : Helper routines 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | * Copyright (c) 2022 STMicroelectronics. 10 | * All rights reserved. 11 | * 12 | * This software is licensed under terms that can be found in the LICENSE file 13 | * in the root directory of this software component. 14 | * If no LICENSE file comes with this software, it is provided AS-IS. 15 | * 16 | ****************************************************************************** 17 | */ 18 | /* USER CODE END Header */ 19 | 20 | 21 | #ifndef INC_ST_PORT_H_ 22 | #define INC_ST_PORT_H_ 23 | 24 | #include 25 | #include 26 | #include "main.h" 27 | #include "ai_platform.h" 28 | #include "network.h" 29 | #include "network_data.h" 30 | #include 31 | 32 | extern UART_HandleTypeDef hlpuart1; 33 | extern UART_HandleTypeDef huart1; 34 | extern TIM_HandleTypeDef htim5; 35 | 36 | 37 | //============================================================================================================== 38 | extern int8_t aiInData_int[AI_NETWORK_IN_1_SIZE]; 39 | extern int8_t aiOutData[AI_NETWORK_OUT_1_SIZE]; 40 | 41 | extern uint8_t pool0[AI_NETWORK_DATA_ACTIVATION_1_SIZE]; 42 | extern ai_handle network ; 43 | 44 | extern ai_buffer* ai_input; 45 | extern ai_buffer* ai_output; 46 | 47 | void AI_Init(); 48 | void AI_Run(int8_t *pIn, int8_t *pOut); 49 | 50 | //============================================================================================================ 51 | 52 | int8_t QuantizeFloatToInt8(float value, float scale, int zero_point); 53 | float DequantizeInt8ToFloat(int8_t value, float scale, int zero_point); 54 | 55 | #define MIN(a, b) ((a < b) ? a : b) 56 | #define MAX(a, b) ((a > b) ? a : b) 57 | #define SATURATE_FLOAT_TO_INT8(result) (int8_t) MIN(MAX(result, (float) INT8_MIN), (float) INT8_MAX); 58 | 59 | 60 | 61 | void clear_Timestamp_Port(void); 62 | void set_Timestamp_Port(void); 63 | void rxInit(void); 64 | 65 | void US_Delay(uint8_t us); 66 | 67 | #endif /* INC_ST_PORT_H_ */ 68 | -------------------------------------------------------------------------------- /NUCLEO_U575ZI_Q/visual_wake_words/tinyml_api/submitter_implemented.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 EEMBC and The MLPerf Authors. All Rights Reserved. 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | This file reflects a modified version of th_lib from EEMBC. All wrapped libc 14 | methods from th_libc.h and all testharness methods from th_lib.h are here. 15 | ==============================================================================*/ 16 | /// \file 17 | /// \brief Submitter-implemented methods required to perform inference. 18 | /// \detail All methods with names starting with th_ are to be implemented by 19 | /// the submitter. All basic I/O, inference and timer APIs must be implemented 20 | /// in order for the benchmark to output useful results, but some auxiliary 21 | /// methods default to an empty implementation. These methods are provided to 22 | /// enable submitter optimizations, and are not required for submission. 23 | 24 | #include "internally_implemented.h" 25 | #include "network.h" 26 | 27 | #ifndef MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 28 | #define MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 29 | 30 | /// \brief These defines set logging prefixes for test harness integration. 31 | /// \detail This API is designed for performance evaluation only. In order to 32 | /// gather energy measurments we recommend using the EEMBC test suite. 33 | #define EE_MSG_TIMESTAMP "m-lap-us-%lu\r\n" 34 | #define TH_VENDOR_NAME_STRING "STMicroelectronics" 35 | 36 | #define MAX_DB_INPUT_SIZE AI_NETWORK_IN_1_SIZE 37 | #define TH_MODEL_VERSION EE_MODEL_VERSION_VWW01 38 | 39 | 40 | 41 | // This is a visual cue to the user when reviewing logs or plugging an 42 | // unknown device into the system. 43 | #if EE_CFG_ENERGY_MODE == 1 44 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-energy\r\n" 45 | #else 46 | #define EE_MSG_TIMESTAMP_MODE "m-timestamp-mode-performance\r\n" 47 | #endif 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | /// \brief required core API 55 | void th_load_tensor(); 56 | void th_results(); 57 | void th_infer(); 58 | void th_timestamp(void); 59 | void th_printf(const char *fmt, ...); 60 | char th_getchar(); 61 | 62 | /// \brief optional API 63 | void th_serialport_initialize(void); 64 | void th_timestamp_initialize(void); 65 | void th_final_initialize(void); 66 | void th_pre(); 67 | void th_post(); 68 | void th_command_ready(char volatile *msg); 69 | 70 | /// \brief libc hooks 71 | int th_strncmp(const char *str1, const char *str2, size_t n); 72 | char *th_strncpy(char *dest, const char *src, size_t n); 73 | size_t th_strnlen(const char *str, size_t maxlen); 74 | char *th_strcat(char *dest, const char *src); 75 | char *th_strtok(/*@null@*/ char *str1, const char *sep); 76 | int th_atoi(const char *str); 77 | void *th_memset(void *b, int c, size_t len); 78 | void *th_memcpy(void *dst, const void *src, size_t n); 79 | int th_vprintf(const char *format, va_list ap); 80 | 81 | #endif // MLPERF_TINY_V0_1_API_SUBMITTER_IMPLEMENTED_H_ 82 | -------------------------------------------------------------------------------- /Release_Notes.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Release Notes for ST Microelectronics submission to Tiny ML Package 8 | 14 | 15 | 18 | 19 | 20 | 21 |
22 |
23 |
24 |

Release Notes for <ST Microelectronics submission to Tiny ML>

25 |

Copyright © 2021 STMicroelectronics

26 | 27 |
28 |

Purpose

29 |

The goal of MLPerf Tiny is to provide a representative set of deep neural nets and benchmarking code to compare performance between embedded devices. These devices typically run at between 10MHz and 250MHz, and can perform inference using less then 50mW of power.

30 |
31 |
32 |

Update history

33 |
34 | 35 |
36 |

Main changes

37 |

First release

38 |

Contents

39 |

The components flagged by “” have changed since the previous release. “” are new.

40 |

Components

41 | 42 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
Applications
43 |
NameVersionRelease note
ST_tinyml_submissionsV1.0.0
Tinyml_apiV0.7
64 |

Known limitations

65 |
    66 |
  • none
  • 67 |
68 |

Development toolchains and compilers

69 |
    70 |
  • STM32CubeMX v 6.4.0
  • 71 |
  • STM32CubeIDE v1.8.0
  • 72 |
73 |

Supported devices and boards

74 | 79 |

Backward compatibility

80 |
    81 |
  • none
  • 82 |
83 |

Dependencies

84 |

This software release is compatible with:

85 |
    86 |
  • X-CUBE-AI version 7.1.0
  • 87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |

visit: www.st.com

96 |
97 |

Info

98 |
99 |
100 |
101 | 102 | 103 | -------------------------------------------------------------------------------- /Release_Notes.md: -------------------------------------------------------------------------------- 1 | --- 2 | pagetitle: Release Notes for ST Microelectronics submission to Tiny ML Package 3 | lang: en 4 | header-includes: 5 | --- 6 | 7 | ::: {.row} 8 | ::: {.col-sm-12 .col-lg-4} 9 | 10 |
11 | # Release Notes for \ 12 | Copyright © 2021 STMicroelectronics 13 | 14 | [![ST logo](_htmresc/st_logo_2020.png)](https://www.st.com){.logo} 15 |
16 | 17 | # Purpose 18 | 19 | The goal of MLPerf Tiny is to provide a representative set of deep neural nets 20 | and benchmarking code to compare performance between embedded devices. These devices 21 | typically run at between 10MHz and 250MHz, and can perform inference using less 22 | then 50mW of power. 23 | 24 | ::: 25 | 26 | ::: {.col-sm-12 .col-lg-8} 27 | # Update history 28 | 29 | ::: {.collapse} 30 | 31 | 32 |
33 | 34 | ## Main changes 35 | 36 | ### First release 37 | 38 | 39 | 40 | ## Contents 41 | 42 | The components flagged by "[]{.icon-st-update}" have changed since the 43 | previous release. "[]{.icon-st-add}" are new. 44 | 45 | Components 46 | 47 | Name Version Release note 48 | ------------------------------- ----------------- ------------------------------------------------------------------------------------------------------------------------- 49 | ST_tinyml_submissions V1.2.0 50 | Tinyml_api V0.7 51 | 52 | : Applications 53 | \ 54 | 55 | 56 | 57 | ## Known limitations 58 | 59 | - none 60 | 61 | ## Development toolchains and compilers 62 | - STM32CubeMX v6.10.0 63 | - STM32CubeIDE v1.14.1 64 | 65 | ## Supported devices and boards 66 | - [NUCLEO-H7A3ZI-Q](https://www.st.com/en/evaluation-tools/nucleo-h7a3zi-q.html#sample-buy) 67 | - [NUCLEO-U575ZI-Q](https://www.st.com/en/evaluation-tools/nucleo-u575zi-q.html#sample-buy) 68 | - [NUCLEO-L4R5ZI](https://www.st.com/en/evaluation-tools/nucleo-l4r5zi.html#sample-buy) 69 | 70 | ## Backward compatibility 71 | 72 | - none 73 | 74 | ## Dependencies 75 | 76 | 77 | This software release is compatible with: 78 | 79 | - X-CUBE-AI version 9.0.0 80 | 81 |
82 | ::: 83 | 84 | 85 | 86 | ::: 87 | ::: 88 | 89 |
90 | ::: {.columns} 91 | ::: {.column width="95%"} 92 | visit: [www.st.com](https://www.st.com/) 93 | ::: 94 | ::: {.column width="5%"} 95 | Info 96 | ::: 97 | ::: 98 |
99 | -------------------------------------------------------------------------------- /_htmresc/Add button.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /_htmresc/Update.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /_htmresc/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/_htmresc/favicon.png -------------------------------------------------------------------------------- /_htmresc/st_logo_2020.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/_htmresc/st_logo_2020.png -------------------------------------------------------------------------------- /doc/H7A3_config.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/H7A3_config.gif -------------------------------------------------------------------------------- /doc/L4R5_config.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/L4R5_config.gif -------------------------------------------------------------------------------- /doc/STM32_H7A3ZI_config.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/STM32_H7A3ZI_config.gif -------------------------------------------------------------------------------- /doc/STM32_l4_config.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/STM32_l4_config.gif -------------------------------------------------------------------------------- /doc/U575_config.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/U575_config.gif -------------------------------------------------------------------------------- /doc/U5_mx_config.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/U5_mx_config.gif -------------------------------------------------------------------------------- /doc/ad0_initial_workspace.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/ad0_initial_workspace.JPG -------------------------------------------------------------------------------- /doc/cube_ide_config_final.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/cube_ide_config_final.gif -------------------------------------------------------------------------------- /doc/cube_ide_project.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/cube_ide_project.JPG -------------------------------------------------------------------------------- /doc/energy_mode_flag.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/energy_mode_flag.JPG -------------------------------------------------------------------------------- /doc/folder_structure.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/folder_structure.JPG -------------------------------------------------------------------------------- /doc/h7_energy_mode_setup.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/h7_energy_mode_setup.JPG -------------------------------------------------------------------------------- /doc/h7_jumpers_performance_mode.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/h7_jumpers_performance_mode.JPG -------------------------------------------------------------------------------- /doc/h7_programmer_config.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/h7_programmer_config.gif -------------------------------------------------------------------------------- /doc/h7xxitc.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/h7xxitc.JPG -------------------------------------------------------------------------------- /doc/l4_energy_mode_setup.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/l4_energy_mode_setup.JPG -------------------------------------------------------------------------------- /doc/l4_jumpers_performance_mode.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/l4_jumpers_performance_mode.JPG -------------------------------------------------------------------------------- /doc/l4_programmer_config.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/l4_programmer_config.gif -------------------------------------------------------------------------------- /doc/l4cube_ide_config_final.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/l4cube_ide_config_final.gif -------------------------------------------------------------------------------- /doc/l4cube_ide_project.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/l4cube_ide_project.JPG -------------------------------------------------------------------------------- /doc/l4energy_mode_flag.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/l4energy_mode_flag.JPG -------------------------------------------------------------------------------- /doc/l4folder_structure.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/l4folder_structure.JPG -------------------------------------------------------------------------------- /doc/l4mainc.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/l4mainc.JPG -------------------------------------------------------------------------------- /doc/l4performance_mode_flag.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/l4performance_mode_flag.JPG -------------------------------------------------------------------------------- /doc/l4release_build.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/l4release_build.JPG -------------------------------------------------------------------------------- /doc/l4xxitc.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/l4xxitc.JPG -------------------------------------------------------------------------------- /doc/mainc.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/mainc.JPG -------------------------------------------------------------------------------- /doc/memory_analyzer_H7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/memory_analyzer_H7.png -------------------------------------------------------------------------------- /doc/performance_mode_flag.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/performance_mode_flag.JPG -------------------------------------------------------------------------------- /doc/release_build.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/release_build.JPG -------------------------------------------------------------------------------- /doc/u5-release_build.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/u5-release_build.JPG -------------------------------------------------------------------------------- /doc/u5_energy_mode_setup.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/u5_energy_mode_setup.JPG -------------------------------------------------------------------------------- /doc/u5_ide_config.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/u5_ide_config.gif -------------------------------------------------------------------------------- /doc/u5_ide_cproject_struct.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/u5_ide_cproject_struct.JPG -------------------------------------------------------------------------------- /doc/u5_jumpers_performance_mode.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/u5_jumpers_performance_mode.JPG -------------------------------------------------------------------------------- /doc/u5_mainc.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/u5_mainc.JPG -------------------------------------------------------------------------------- /doc/u5_performance_mode_flag.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/u5_performance_mode_flag.JPG -------------------------------------------------------------------------------- /doc/u5_programmer_config.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/u5_programmer_config.gif -------------------------------------------------------------------------------- /doc/u5energy_mode_flag.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/u5energy_mode_flag.JPG -------------------------------------------------------------------------------- /doc/u5xxitc.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/STMicroelectronics/stm32ai-perf/51ed4610609f981c113225c3cbf933a77498bec3/doc/u5xxitc.JPG --------------------------------------------------------------------------------