board.h 3.9 KB

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