family.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include "bsp/board.h"
  27. #include "board.h"
  28. #include "broadcom/cpu.h"
  29. #include "broadcom/gpio.h"
  30. #include "broadcom/interrupts.h"
  31. #include "broadcom/mmu.h"
  32. #include "broadcom/caches.h"
  33. #include "broadcom/vcmailbox.h"
  34. // LED
  35. #define LED_PIN 18
  36. #define LED_STATE_ON 1
  37. // UART TX
  38. #define UART_TX_PIN 14
  39. //--------------------------------------------------------------------+
  40. // Forward USB interrupt events to TinyUSB IRQ Handler
  41. //--------------------------------------------------------------------+
  42. void USB_IRQHandler(void)
  43. {
  44. tud_int_handler(0);
  45. }
  46. //--------------------------------------------------------------------+
  47. // MACRO TYPEDEF CONSTANT ENUM
  48. //--------------------------------------------------------------------+
  49. //--------------------------------------------------------------------+
  50. // Board porting API
  51. //--------------------------------------------------------------------+
  52. void board_init(void)
  53. {
  54. setup_mmu_flat_map();
  55. init_caches();
  56. // LED
  57. gpio_set_function(LED_PIN, GPIO_FUNCTION_OUTPUT);
  58. gpio_set_pull(LED_PIN, BP_PULL_NONE);
  59. board_led_write(true);
  60. // Uart
  61. COMPLETE_MEMORY_READS;
  62. AUX->ENABLES_b.UART_1 = true;
  63. UART1->IER = 0;
  64. UART1->CNTL = 0;
  65. UART1->LCR_b.DATA_SIZE = UART1_LCR_DATA_SIZE_MODE_8BIT;
  66. UART1->MCR = 0;
  67. UART1->IER = 0;
  68. uint32_t source_clock = vcmailbox_get_clock_rate_measured(VCMAILBOX_CLOCK_CORE);
  69. UART1->BAUD = ((source_clock / (115200 * 8)) - 1);
  70. UART1->CNTL |= UART1_CNTL_TX_ENABLE_Msk;
  71. COMPLETE_MEMORY_READS;
  72. gpio_set_function(UART_TX_PIN, GPIO_FUNCTION_ALT5);
  73. // Turn on USB peripheral.
  74. vcmailbox_set_power_state(VCMAILBOX_DEVICE_USB_HCD, true);
  75. // Timer 1/1024 second tick
  76. SYSTMR->CS_b.M1 = 1;
  77. SYSTMR->C1 = SYSTMR->CLO + 977;
  78. BP_EnableIRQ(TIMER_1_IRQn);
  79. BP_SetPriority(USB_IRQn, 0x00);
  80. BP_ClearPendingIRQ(USB_IRQn);
  81. BP_EnableIRQ(USB_IRQn);
  82. BP_EnableIRQs();
  83. }
  84. void board_led_write(bool state)
  85. {
  86. gpio_set_value(LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON));
  87. }
  88. uint32_t board_button_read(void)
  89. {
  90. return 0;
  91. }
  92. int board_uart_read(uint8_t* buf, int len)
  93. {
  94. (void) buf; (void) len;
  95. return 0;
  96. }
  97. int board_uart_write(void const * buf, int len)
  98. {
  99. for (int i = 0; i < len; i++) {
  100. const char* cbuf = buf;
  101. while (!UART1->STAT_b.TX_READY) {}
  102. if (cbuf[i] == '\n') {
  103. UART1->IO = '\r';
  104. while (!UART1->STAT_b.TX_READY) {}
  105. }
  106. UART1->IO = cbuf[i];
  107. }
  108. return len;
  109. }
  110. #if CFG_TUSB_OS == OPT_OS_NONE
  111. volatile uint32_t system_ticks = 0;
  112. void TIMER_1_IRQHandler(void)
  113. {
  114. system_ticks++;
  115. SYSTMR->C1 += 977;
  116. SYSTMR->CS_b.M1 = 1;
  117. }
  118. uint32_t board_millis(void)
  119. {
  120. return system_ticks;
  121. }
  122. #endif
  123. void HardFault_Handler (void)
  124. {
  125. // asm("bkpt");
  126. }
  127. // Required by __libc_init_array in startup code if we are compiling using
  128. // -nostdlib/-nostartfiles.
  129. void _init(void)
  130. {
  131. }