| 
									
										
										
										
											2023-03-17 16:12:49 +07:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2019-03-20 16:11:42 +07:00
										 |  |  |  * The MIT License (MIT) | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-05-14 11:48:05 +07:00
										 |  |  |  * Copyright (c) 2019 Ha Thach (tinyusb.org) | 
					
						
							| 
									
										
										
										
											2019-03-20 16:11:42 +07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  |  * all copies or substantial portions of the Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
					
						
							|  |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  |  * THE SOFTWARE. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of the TinyUSB stack. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-03-02 13:41:35 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | #ifndef _BOARD_API_H_
 | 
					
						
							|  |  |  | #define _BOARD_API_H_
 | 
					
						
							| 
									
										
										
										
											2018-03-02 13:41:35 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef __cplusplus
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | extern "C" { | 
					
						
							| 
									
										
										
										
											2018-03-02 13:41:35 +07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdint.h>
 | 
					
						
							|  |  |  | #include <stdbool.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-23 22:31:07 +07:00
										 |  |  | #include "tusb.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-29 23:23:06 +07:00
										 |  |  | #if CFG_TUSB_OS == OPT_OS_FREERTOS
 | 
					
						
							|  |  |  | #if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
 | 
					
						
							|  |  |  |   // ESP-IDF need "freertos/" prefix in include path.
 | 
					
						
							|  |  |  |   // CFG_TUSB_OS_INC_PATH should be defined accordingly.
 | 
					
						
							|  |  |  |   #include "freertos/FreeRTOS.h"
 | 
					
						
							|  |  |  |   #include "freertos/semphr.h"
 | 
					
						
							|  |  |  |   #include "freertos/queue.h"
 | 
					
						
							|  |  |  |   #include "freertos/task.h"
 | 
					
						
							|  |  |  |   #include "freertos/timers.h"
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |   #include "FreeRTOS.h"
 | 
					
						
							|  |  |  |   #include "semphr.h"
 | 
					
						
							|  |  |  |   #include "queue.h"
 | 
					
						
							|  |  |  |   #include "task.h"
 | 
					
						
							|  |  |  |   #include "timers.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 20:28:22 +01:00
										 |  |  | // Define the default baudrate
 | 
					
						
							|  |  |  | #ifndef CFG_BOARD_UART_BAUDRATE
 | 
					
						
							|  |  |  | #define CFG_BOARD_UART_BAUDRATE 115200   ///< Default baud rate
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-03-23 22:31:07 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-22 22:06:48 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // Board Porting API
 | 
					
						
							| 
									
										
										
										
											2019-04-01 18:31:20 +07:00
										 |  |  | // For simplicity, only one LED and one Button are used
 | 
					
						
							| 
									
										
										
										
											2019-03-22 22:06:48 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2018-03-02 13:41:35 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-22 22:06:48 +07:00
										 |  |  | // Initialize on-board peripherals : led, button, uart and USB
 | 
					
						
							| 
									
										
										
										
											2018-03-02 13:41:35 +07:00
										 |  |  | void board_init(void); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-31 19:09:40 +07:00
										 |  |  | // Init board after tinyusb is initialized
 | 
					
						
							|  |  |  | void board_init_after_tusb(void) TU_ATTR_WEAK; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-22 22:06:48 +07:00
										 |  |  | // Turn LED on or off
 | 
					
						
							| 
									
										
										
										
											2019-04-02 18:16:04 +07:00
										 |  |  | void board_led_write(bool state); | 
					
						
							| 
									
										
										
										
											2018-03-13 16:30:53 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-01 22:46:39 +07:00
										 |  |  | // Control led pattern using phase duration in ms.
 | 
					
						
							|  |  |  | // For each phase, LED is toggle then repeated, board_led_task() is required to be called
 | 
					
						
							|  |  |  | //void board_led_pattern(uint32_t const phase_ms[], uint8_t count);
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-01 18:31:20 +07:00
										 |  |  | // Get the current state of button
 | 
					
						
							|  |  |  | // a '1' means active (pressed), a '0' means inactive.
 | 
					
						
							|  |  |  | uint32_t board_button_read(void); | 
					
						
							| 
									
										
										
										
											2019-03-22 22:06:48 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 20:42:34 +07:00
										 |  |  | // Get board unique ID for USB serial number. Return number of bytes. Note max_len is typically 16
 | 
					
						
							|  |  |  | TU_ATTR_WEAK size_t board_get_unique_id(uint8_t id[], size_t max_len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Get characters from UART. Return number of read bytes
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | int board_uart_read(uint8_t *buf, int len); | 
					
						
							| 
									
										
										
										
											2019-03-22 22:06:48 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 20:42:34 +07:00
										 |  |  | // Send characters to UART. Return number of sent bytes
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | int board_uart_write(void const *buf, int len); | 
					
						
							| 
									
										
										
										
											2019-03-22 22:06:48 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-23 22:31:07 +07:00
										 |  |  | #if CFG_TUSB_OS == OPT_OS_NONE
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | // Get current milliseconds, must be implemented when no RTOS is used
 | 
					
						
							|  |  |  | uint32_t board_millis(void); | 
					
						
							| 
									
										
										
										
											2020-03-11 12:32:29 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-23 22:31:07 +07:00
										 |  |  | #elif CFG_TUSB_OS == OPT_OS_FREERTOS
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | static inline uint32_t board_millis(void) { | 
					
						
							|  |  |  |   return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ ); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-11 12:32:29 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-23 22:31:07 +07:00
										 |  |  | #elif CFG_TUSB_OS == OPT_OS_MYNEWT
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | static inline uint32_t board_millis(void) { | 
					
						
							|  |  |  |   return os_time_ticks_to_ms32( os_time_get() ); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-11 12:32:29 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-19 19:52:07 -06:00
										 |  |  | #elif CFG_TUSB_OS == OPT_OS_PICO
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | #include "pico/time.h"
 | 
					
						
							|  |  |  | static inline uint32_t board_millis(void) { | 
					
						
							|  |  |  |   return to_ms_since_boot(get_absolute_time()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-07-01 22:46:39 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-07 22:44:49 +08:00
										 |  |  | #elif CFG_TUSB_OS == OPT_OS_RTTHREAD
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | static inline uint32_t board_millis(void) { | 
					
						
							|  |  |  |   return (((uint64_t)rt_tick_get()) * 1000 / RT_TICK_PER_SECOND); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-01-19 19:52:07 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-02 19:32:25 +07:00
										 |  |  | #elif CFG_TUSB_OS == OPT_OS_CUSTOM
 | 
					
						
							|  |  |  | // Implement your own board_millis() in any of .c file
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-12 10:43:16 +07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2020-03-11 12:32:29 +07:00
										 |  |  |   #error "board_millis() is not implemented for this OS"
 | 
					
						
							| 
									
										
										
										
											2019-03-23 22:31:07 +07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-03-23 16:51:07 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-22 22:06:48 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // Helper functions
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | static inline void board_led_on(void) { | 
					
						
							| 
									
										
										
										
											2019-04-02 18:16:04 +07:00
										 |  |  |   board_led_write(true); | 
					
						
							| 
									
										
										
										
											2018-03-13 16:30:53 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | static inline void board_led_off(void) { | 
					
						
							| 
									
										
										
										
											2019-04-02 18:16:04 +07:00
										 |  |  |   board_led_write(false); | 
					
						
							| 
									
										
										
										
											2018-03-13 16:30:53 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 20:42:34 +07:00
										 |  |  | // Get USB Serial number string from unique ID if available. Return number of character.
 | 
					
						
							|  |  |  | // Input is string descriptor from index 1 (index 0 is type + len)
 | 
					
						
							|  |  |  | static inline size_t board_usb_get_serial(uint16_t desc_str1[], size_t max_chars) { | 
					
						
							| 
									
										
										
										
											2023-08-04 00:20:47 +07:00
										 |  |  |   uint8_t uid[16] TU_ATTR_ALIGNED(4); | 
					
						
							|  |  |  |   size_t uid_len; | 
					
						
							| 
									
										
										
										
											2023-08-03 20:42:34 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if ( board_get_unique_id ) { | 
					
						
							| 
									
										
										
										
											2023-08-04 00:20:47 +07:00
										 |  |  |     uid_len = board_get_unique_id(uid, sizeof(uid)); | 
					
						
							| 
									
										
										
										
											2023-08-03 20:42:34 +07:00
										 |  |  |   }else { | 
					
						
							|  |  |  |     // fixed serial string is 01234567889ABCDEF
 | 
					
						
							| 
									
										
										
										
											2023-08-04 00:20:47 +07:00
										 |  |  |     uint32_t* uid32 = (uint32_t*) (uintptr_t) uid; | 
					
						
							|  |  |  |     uid32[0] = 0x67452301; | 
					
						
							|  |  |  |     uid32[1] = 0xEFCDAB89; | 
					
						
							|  |  |  |     uid_len = 8; | 
					
						
							| 
									
										
										
										
											2023-08-03 20:42:34 +07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 00:20:47 +07:00
										 |  |  |   if ( uid_len > max_chars / 2 ) uid_len = max_chars / 2; | 
					
						
							| 
									
										
										
										
											2023-08-03 20:42:34 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 00:20:47 +07:00
										 |  |  |   for ( size_t i = 0; i < uid_len; i++ ) { | 
					
						
							| 
									
										
										
										
											2023-08-03 20:42:34 +07:00
										 |  |  |     for ( size_t j = 0; j < 2; j++ ) { | 
					
						
							|  |  |  |       const char nibble_to_hex[16] = { | 
					
						
							|  |  |  |           '0', '1', '2', '3', '4', '5', '6', '7', | 
					
						
							|  |  |  |           '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2023-08-04 00:20:47 +07:00
										 |  |  |       uint8_t const nibble = (uid[i] >> (j * 4)) & 0xf; | 
					
						
							| 
									
										
										
										
											2023-08-03 20:42:34 +07:00
										 |  |  |       desc_str1[i * 2 + (1 - j)] = nibble_to_hex[nibble]; // UTF-16-LE
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 00:20:47 +07:00
										 |  |  |   return 2 * uid_len; | 
					
						
							| 
									
										
										
										
											2023-08-03 20:42:34 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 12:32:29 +07:00
										 |  |  | // TODO remove
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | static inline void board_delay(uint32_t ms) { | 
					
						
							| 
									
										
										
										
											2019-05-21 13:31:02 +07:00
										 |  |  |   uint32_t start_ms = board_millis(); | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  |   while ( board_millis() - start_ms < ms ) { | 
					
						
							| 
									
										
										
										
											2019-05-29 16:55:54 +07:00
										 |  |  |     // take chance to run usb background
 | 
					
						
							| 
									
										
										
										
											2023-08-03 17:19:47 +07:00
										 |  |  |     #if CFG_TUD_ENABLED
 | 
					
						
							| 
									
										
										
										
											2019-05-29 16:55:54 +07:00
										 |  |  |     tud_task(); | 
					
						
							| 
									
										
										
										
											2019-08-24 23:30:35 +07:00
										 |  |  |     #endif
 | 
					
						
							| 
									
										
										
										
											2023-08-03 17:19:47 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     #if CFG_TUH_ENABLED
 | 
					
						
							|  |  |  |     tuh_task(); | 
					
						
							|  |  |  |     #endif
 | 
					
						
							| 
									
										
										
										
											2019-05-29 16:55:54 +07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-18 22:31:35 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-21 16:28:54 +07:00
										 |  |  | // stdio getchar() is blocking, this is non-blocking version
 | 
					
						
							|  |  |  | int board_getchar(void); | 
					
						
							| 
									
										
										
										
											2019-03-20 20:58:27 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-02 13:41:35 +07:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:50:52 +07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-02 13:41:35 +07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:44:05 +07:00
										 |  |  | #endif
 |