board.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  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. /** \ingroup group_demo
  27. * \defgroup group_board Boards Abstraction Layer
  28. * @{ */
  29. #ifndef _BSP_BOARD_H_
  30. #define _BSP_BOARD_H_
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #include <stdint.h>
  35. #include <stdbool.h>
  36. #include "ansi_escape.h"
  37. #include "tusb.h"
  38. #define CFG_BOARD_UART_BAUDRATE 115200
  39. //--------------------------------------------------------------------+
  40. // Board Porting API
  41. // For simplicity, only one LED and one Button are used
  42. //--------------------------------------------------------------------+
  43. // Initialize on-board peripherals : led, button, uart and USB
  44. void board_init(void);
  45. // Turn LED on or off
  46. void board_led_write(bool state);
  47. // Get the current state of button
  48. // a '1' means active (pressed), a '0' means inactive.
  49. uint32_t board_button_read(void);
  50. // Get characters from UART
  51. int board_uart_read(uint8_t* buf, int len);
  52. // Send characters to UART
  53. int board_uart_write(void const * buf, int len);
  54. #if CFG_TUSB_OS == OPT_OS_NONE
  55. // Get current milliseconds, must be implemented when no RTOS is used
  56. uint32_t board_millis(void);
  57. #elif CFG_TUSB_OS == OPT_OS_FREERTOS
  58. static inline uint32_t board_millis(void)
  59. {
  60. return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ );
  61. }
  62. #elif CFG_TUSB_OS == OPT_OS_MYNEWT
  63. static inline uint32_t board_millis(void)
  64. {
  65. return os_time_ticks_to_ms32( os_time_get() );
  66. }
  67. #elif CFG_TUSB_OS == OPT_OS_PICO
  68. #include "pico/time.h"
  69. static inline uint32_t board_millis(void)
  70. {
  71. return to_ms_since_boot(get_absolute_time());
  72. }
  73. #elif CFG_TUSB_OS == OPT_OS_RTTHREAD
  74. static inline uint32_t board_millis(void)
  75. {
  76. return (((uint64_t)rt_tick_get()) * 1000 / RT_TICK_PER_SECOND);
  77. }
  78. #else
  79. #error "board_millis() is not implemented for this OS"
  80. #endif
  81. //--------------------------------------------------------------------+
  82. // Helper functions
  83. //--------------------------------------------------------------------+
  84. static inline void board_led_on(void)
  85. {
  86. board_led_write(true);
  87. }
  88. static inline void board_led_off(void)
  89. {
  90. board_led_write(false);
  91. }
  92. // TODO remove
  93. static inline void board_delay(uint32_t ms)
  94. {
  95. uint32_t start_ms = board_millis();
  96. while (board_millis() - start_ms < ms)
  97. {
  98. #if TUSB_OPT_DEVICE_ENABLED
  99. // take chance to run usb background
  100. tud_task();
  101. #endif
  102. }
  103. }
  104. static inline int board_uart_getchar(void)
  105. {
  106. uint8_t c;
  107. return board_uart_read(&c, 1) ? (int) c : (-1);
  108. }
  109. static inline int board_uart_putchar(uint8_t c)
  110. {
  111. return board_uart_write(&c, 1);
  112. }
  113. #ifdef __cplusplus
  114. }
  115. #endif
  116. #endif /* _BSP_BOARD_H_ */
  117. /** @} */