board.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Return number of read bytes
  58. int board_uart_read(uint8_t* buf, int len);
  59. // Send characters to UART
  60. // Return number of sent bytes
  61. int board_uart_write(void const * buf, int len);
  62. #if CFG_TUSB_OS == OPT_OS_NONE
  63. // Get current milliseconds, must be implemented when no RTOS is used
  64. uint32_t board_millis(void);
  65. #elif CFG_TUSB_OS == OPT_OS_FREERTOS
  66. static inline uint32_t board_millis(void)
  67. {
  68. return ( ( ((uint64_t) xTaskGetTickCount()) * 1000) / configTICK_RATE_HZ );
  69. }
  70. #elif CFG_TUSB_OS == OPT_OS_MYNEWT
  71. static inline uint32_t board_millis(void)
  72. {
  73. return os_time_ticks_to_ms32( os_time_get() );
  74. }
  75. #elif CFG_TUSB_OS == OPT_OS_PICO
  76. #include "pico/time.h"
  77. static inline uint32_t board_millis(void)
  78. {
  79. return to_ms_since_boot(get_absolute_time());
  80. }
  81. #elif CFG_TUSB_OS == OPT_OS_RTTHREAD
  82. static inline uint32_t board_millis(void)
  83. {
  84. return (((uint64_t)rt_tick_get()) * 1000 / RT_TICK_PER_SECOND);
  85. }
  86. #else
  87. #error "board_millis() is not implemented for this OS"
  88. #endif
  89. //--------------------------------------------------------------------+
  90. // Helper functions
  91. //--------------------------------------------------------------------+
  92. static inline void board_led_on(void)
  93. {
  94. board_led_write(true);
  95. }
  96. static inline void board_led_off(void)
  97. {
  98. board_led_write(false);
  99. }
  100. // TODO remove
  101. static inline void board_delay(uint32_t ms)
  102. {
  103. uint32_t start_ms = board_millis();
  104. while (board_millis() - start_ms < ms)
  105. {
  106. #if CFG_TUD_ENABLED
  107. // take chance to run usb background
  108. tud_task();
  109. #endif
  110. }
  111. }
  112. // stdio getchar() is blocking, this is non-blocking version
  113. int board_getchar(void);
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif /* _BSP_BOARD_H_ */
  118. /** @} */