board.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 the default baudrate
  39. #ifndef CFG_BOARD_UART_BAUDRATE
  40. #define CFG_BOARD_UART_BAUDRATE 115200 ///< Default baud rate
  41. #endif
  42. //--------------------------------------------------------------------+
  43. // Board Porting API
  44. // For simplicity, only one LED and one Button are used
  45. //--------------------------------------------------------------------+
  46. // Initialize on-board peripherals : led, button, uart and USB
  47. void board_init(void);
  48. // Turn LED on or off
  49. void board_led_write(bool state);
  50. // Control led pattern using phase duration in ms.
  51. // For each phase, LED is toggle then repeated, board_led_task() is required to be called
  52. //void board_led_pattern(uint32_t const phase_ms[], uint8_t count);
  53. // Get the current state of button
  54. // a '1' means active (pressed), a '0' means inactive.
  55. uint32_t board_button_read(void);
  56. // Get characters from UART
  57. int board_uart_read(uint8_t* buf, int len);
  58. // Send characters to UART
  59. int board_uart_write(void const * buf, int len);
  60. #if CFG_TUSB_OS == OPT_OS_NONE
  61. // Get current milliseconds, must be implemented when no RTOS is used
  62. uint32_t board_millis(void);
  63. #elif CFG_TUSB_OS == OPT_OS_FREERTOS
  64. static inline uint32_t board_millis(void)
  65. {
  66. return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ );
  67. }
  68. #elif CFG_TUSB_OS == OPT_OS_MYNEWT
  69. static inline uint32_t board_millis(void)
  70. {
  71. return os_time_ticks_to_ms32( os_time_get() );
  72. }
  73. #elif CFG_TUSB_OS == OPT_OS_PICO
  74. #include "pico/time.h"
  75. static inline uint32_t board_millis(void)
  76. {
  77. return to_ms_since_boot(get_absolute_time());
  78. }
  79. #elif CFG_TUSB_OS == OPT_OS_RTTHREAD
  80. static inline uint32_t board_millis(void)
  81. {
  82. return (((uint64_t)rt_tick_get()) * 1000 / RT_TICK_PER_SECOND);
  83. }
  84. #else
  85. #error "board_millis() is not implemented for this OS"
  86. #endif
  87. //--------------------------------------------------------------------+
  88. // Helper functions
  89. //--------------------------------------------------------------------+
  90. static inline void board_led_on(void)
  91. {
  92. board_led_write(true);
  93. }
  94. static inline void board_led_off(void)
  95. {
  96. board_led_write(false);
  97. }
  98. // TODO remove
  99. static inline void board_delay(uint32_t ms)
  100. {
  101. uint32_t start_ms = board_millis();
  102. while (board_millis() - start_ms < ms)
  103. {
  104. #if CFG_TUD_ENABLED
  105. // take chance to run usb background
  106. tud_task();
  107. #endif
  108. }
  109. }
  110. static inline int board_uart_getchar(void)
  111. {
  112. uint8_t c;
  113. return board_uart_read(&c, 1) ? (int) c : (-1);
  114. }
  115. static inline int board_uart_putchar(uint8_t c)
  116. {
  117. return board_uart_write(&c, 1);
  118. }
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #endif /* _BSP_BOARD_H_ */
  123. /** @} */