blob: 782e0939c760762e25a510b7c439930f3d274618 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27/** \ingroup group_demo
28 * \defgroup group_board Boards Abstraction Layer
29 * @{ */
30
31#ifndef _BSP_BOARD_H_
32#define _BSP_BOARD_H_
33
34#ifdef __cplusplus
35 extern "C" {
36#endif
37
38#include <stdint.h>
39#include <stdbool.h>
40
41#include "ansi_escape.h"
42#include "tusb.h"
43
44#define CFG_BOARD_UART_BAUDRATE 115200
45
46//--------------------------------------------------------------------+
47// Board Porting API
48// For simplicity, only one LED and one Button are used
49//--------------------------------------------------------------------+
50
51// Initialize on-board peripherals : led, button, uart and USB
52void board_init(void);
53
54// Turn LED on or off
55void board_led_write(bool state);
56
57// Control led pattern using phase duration in ms.
58// For each phase, LED is toggle then repeated, board_led_task() is required to be called
59//void board_led_pattern(uint32_t const phase_ms[], uint8_t count);
60
61// Get the current state of button
62// a '1' means active (pressed), a '0' means inactive.
63uint32_t board_button_read(void);
64
65// Get characters from UART
66int board_uart_read(uint8_t* buf, int len);
67
68// Send characters to UART
69int board_uart_write(void const * buf, int len);
70
71#if CFG_TUSB_OS == OPT_OS_NONE
72 // Get current milliseconds, must be implemented when no RTOS is used
73 uint32_t board_millis(void);
74
75#elif CFG_TUSB_OS == OPT_OS_FREERTOS
76 static inline uint32_t board_millis(void)
77 {
78 return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ );
79 }
80
81#elif CFG_TUSB_OS == OPT_OS_MYNEWT
82 static inline uint32_t board_millis(void)
83 {
84 return os_time_ticks_to_ms32( os_time_get() );
85 }
86
87#elif CFG_TUSB_OS == OPT_OS_PICO
88 #include "pico/time.h"
89 static inline uint32_t board_millis(void)
90 {
91 return to_ms_since_boot(get_absolute_time());
92 }
93
94#elif CFG_TUSB_OS == OPT_OS_RTTHREAD
95 static inline uint32_t board_millis(void)
96 {
97 return (((uint64_t)rt_tick_get()) * 1000 / RT_TICK_PER_SECOND);
98 }
99
100#else
101 #error "board_millis() is not implemented for this OS"
102#endif
103
104//--------------------------------------------------------------------+
105// Helper functions
106//--------------------------------------------------------------------+
107static inline void board_led_on(void)
108{
109 board_led_write(true);
110}
111
112static inline void board_led_off(void)
113{
114 board_led_write(false);
115}
116
117// TODO remove
118static inline void board_delay(uint32_t ms)
119{
120 uint32_t start_ms = board_millis();
121 while (board_millis() - start_ms < ms)
122 {
123 #if TUSB_OPT_DEVICE_ENABLED
124 // take chance to run usb background
125 tud_task();
126 #endif
127 }
128}
129
130static inline int board_uart_getchar(void)
131{
132 uint8_t c;
133 return board_uart_read(&c, 1) ? (int) c : (-1);
134}
135
136static inline int board_uart_putchar(uint8_t c)
137{
138 return board_uart_write(&c, 1);
139}
140
141#ifdef __cplusplus
142 }
143#endif
144
145#endif /* _BSP_BOARD_H_ */
146
147/** @} */