board.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2018, hathach (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. */
  25. #include "board.h"
  26. #if 0
  27. #define LED_PHASE_MAX 8
  28. static struct
  29. {
  30. uint32_t phase[LED_PHASE_MAX];
  31. uint8_t phase_count;
  32. bool led_state;
  33. uint8_t current_phase;
  34. uint32_t current_ms;
  35. }led_pattern;
  36. void board_led_pattern(uint32_t const phase_ms[], uint8_t count)
  37. {
  38. memcpy(led_pattern.phase, phase_ms, 4*count);
  39. led_pattern.phase_count = count;
  40. // reset with 1st phase is on
  41. led_pattern.current_ms = board_millis();
  42. led_pattern.current_phase = 0;
  43. led_pattern.led_state = true;
  44. board_led_on();
  45. }
  46. void board_led_task(void)
  47. {
  48. if ( led_pattern.phase_count == 0 ) return;
  49. uint32_t const duration = led_pattern.phase[led_pattern.current_phase];
  50. // return if not enough time
  51. if (board_millis() - led_pattern.current_ms < duration) return;
  52. led_pattern.led_state = !led_pattern.led_state;
  53. board_led_write(led_pattern.led_state);
  54. led_pattern.current_ms += duration;
  55. led_pattern.current_phase++;
  56. if (led_pattern.current_phase == led_pattern.phase_count)
  57. {
  58. led_pattern.current_phase = 0;
  59. led_pattern.led_state = true;
  60. board_led_on();
  61. }
  62. }
  63. #endif
  64. //--------------------------------------------------------------------+
  65. // newlib read()/write() retarget
  66. //--------------------------------------------------------------------+
  67. #if defined(__MSP430__) || defined(__RX__)
  68. #define sys_write write
  69. #define sys_read read
  70. #else
  71. #define sys_write _write
  72. #define sys_read _read
  73. #endif
  74. #if defined(LOGGER_RTT)
  75. // Logging with RTT
  76. // If using SES IDE, use the Syscalls/SEGGER_RTT_Syscalls_SES.c instead
  77. #if !(defined __SES_ARM) && !(defined __SES_RISCV) && !(defined __CROSSWORKS_ARM)
  78. #include "SEGGER_RTT.h"
  79. TU_ATTR_USED int sys_write (int fhdl, const void *buf, size_t count)
  80. {
  81. (void) fhdl;
  82. SEGGER_RTT_Write(0, (const char*) buf, (int) count);
  83. return count;
  84. }
  85. TU_ATTR_USED int sys_read (int fhdl, char *buf, size_t count)
  86. {
  87. (void) fhdl;
  88. return SEGGER_RTT_Read(0, buf, count);
  89. }
  90. #endif
  91. #elif defined(LOGGER_SWO)
  92. // Logging with SWO for ARM Cortex
  93. #include "board_mcu.h"
  94. TU_ATTR_USED int sys_write (int fhdl, const void *buf, size_t count)
  95. {
  96. (void) fhdl;
  97. uint8_t const* buf8 = (uint8_t const*) buf;
  98. for(size_t i=0; i<count; i++)
  99. {
  100. ITM_SendChar(buf8[i]);
  101. }
  102. return count;
  103. }
  104. TU_ATTR_USED int sys_read (int fhdl, char *buf, size_t count)
  105. {
  106. (void) fhdl;
  107. (void) buf;
  108. (void) count;
  109. return 0;
  110. }
  111. #else
  112. // Default logging with on-board UART
  113. TU_ATTR_USED int sys_write (int fhdl, const void *buf, size_t count)
  114. {
  115. (void) fhdl;
  116. return board_uart_write(buf, (int) count);
  117. }
  118. TU_ATTR_USED int sys_read (int fhdl, char *buf, size_t count)
  119. {
  120. (void) fhdl;
  121. return board_uart_read((uint8_t*) buf, (int) count);
  122. }
  123. #endif