family.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "msp430.h"
  29. //--------------------------------------------------------------------+
  30. // Forward USB interrupt events to TinyUSB IRQ Handler
  31. //--------------------------------------------------------------------+
  32. void __attribute__ ((interrupt(USB_UBM_VECTOR))) USB_UBM_ISR(void)
  33. {
  34. tud_int_handler(0);
  35. }
  36. //--------------------------------------------------------------------+
  37. // MACRO TYPEDEF CONSTANT ENUM
  38. //--------------------------------------------------------------------+
  39. uint32_t cnt = 0;
  40. static void SystemClock_Config(void)
  41. {
  42. WDTCTL = WDTPW + WDTHOLD; // Disable watchdog.
  43. // Increase VCore to level 2- required for 16 MHz operation on this MCU.
  44. PMMCTL0 = PMMPW + PMMCOREV_2;
  45. UCSCTL3 = SELREF__XT2CLK; // FLL is fed by XT2.
  46. // XT1 used for ACLK (default- not used in this demo)
  47. P5SEL |= BIT4; // Required to enable XT1
  48. // Loop until XT1 fault flag is cleared.
  49. do
  50. {
  51. UCSCTL7 &= ~XT1LFOFFG;
  52. }while(UCSCTL7 & XT1LFOFFG);
  53. // XT2 is 4 MHz an external oscillator, use PLL to boost to 16 MHz.
  54. P5SEL |= BIT2; // Required to enable XT2.
  55. // Loop until XT2 fault flag is cleared
  56. do
  57. {
  58. UCSCTL7 &= ~XT2OFFG;
  59. }while(UCSCTL7 & XT2OFFG);
  60. // Kickstart the DCO into the correct frequency range, otherwise a
  61. // fault will occur.
  62. // FIXME: DCORSEL_6 should work according to datasheet params, but generates
  63. // a fault. I am not sure why it faults.
  64. UCSCTL1 = DCORSEL_7;
  65. UCSCTL2 = FLLD_2 + 3; // DCO freq = D * (N + 1) * (FLLREFCLK / n)
  66. // DCOCLKDIV freq = (N + 1) * (FLLREFCLK / n)
  67. // N = 3, D = 2, thus DCO freq = 32 MHz.
  68. // MCLK configured for 16 MHz using XT2.
  69. // SMCLK configured for 8 MHz using XT2.
  70. UCSCTL4 |= SELM__DCOCLKDIV + SELS__DCOCLKDIV;
  71. UCSCTL5 |= DIVM__16 + DIVS__2;
  72. // Now wait till everything's stabilized.
  73. do
  74. {
  75. UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
  76. SFRIFG1 &= ~OFIFG;
  77. }while(SFRIFG1 & OFIFG);
  78. // Configure Timer A to use SMCLK as a source. Count 1000 ticks at 1 MHz.
  79. TA0CCTL0 |= CCIE;
  80. TA0CCR0 = 999; // 1000 ticks.
  81. TA0CTL |= TASSEL_2 + ID_3 + MC__UP; // Use SMCLK, divide by 8, start timer.
  82. // Initialize USB power and PLL.
  83. USBKEYPID = USBKEY;
  84. // VUSB enabled automatically.
  85. // Wait two milliseconds to stabilize, per manual recommendation.
  86. uint32_t ms_elapsed = board_millis();
  87. do
  88. {
  89. while((board_millis() - ms_elapsed) < 2);
  90. }while(!(USBPWRCTL & USBBGVBV));
  91. // USB uses XT2 (4 MHz) directly. Enable the PLL.
  92. USBPLLDIVB |= USBPLL_SETCLK_4_0;
  93. USBPLLCTL |= (UPFDEN | UPLLEN);
  94. // Wait until PLL locks. Check every 2ms, per manual.
  95. ms_elapsed = board_millis();
  96. do
  97. {
  98. USBPLLIR &= ~USBOOLIFG;
  99. while((board_millis() - ms_elapsed) < 2);
  100. }while(USBPLLIR & USBOOLIFG);
  101. USBKEYPID = 0;
  102. }
  103. uint32_t wait = 0;
  104. void board_init(void)
  105. {
  106. __bis_SR_register(GIE); // Enable interrupts.
  107. SystemClock_Config();
  108. // Enable basic I/O.
  109. P1DIR |= LED_PIN; // LED output.
  110. P1REN |= BUTTON_PIN; // Internal resistor enable.
  111. P1OUT |= BUTTON_PIN; // Pullup.
  112. // Enable the backchannel UART (115200)
  113. P4DIR |= BIT5;
  114. P4SEL |= (BIT5 | BIT4);
  115. UCA1CTL1 |= (UCSSEL__SMCLK | UCSWRST); // Hold in reset, use SMCLK.
  116. UCA1BRW = 4;
  117. UCA1MCTL |= (UCBRF_3 | UCBRS_5 | UCOS16); // Overampling mode, 115200 baud.
  118. // Copied from manual.
  119. UCA1CTL1 &= ~UCSWRST;
  120. // Set up USB pins.
  121. USBKEYPID = USBKEY;
  122. USBPHYCTL |= PUSEL; // Convert USB D+/D- pins to USB functionality.
  123. USBKEYPID = 0;
  124. }
  125. //--------------------------------------------------------------------+
  126. // Board porting API
  127. //--------------------------------------------------------------------+
  128. void board_led_write(bool state)
  129. {
  130. if(state)
  131. {
  132. LED_PORT |= LED_PIN;
  133. }
  134. else
  135. {
  136. LED_PORT &= ~LED_PIN;
  137. }
  138. }
  139. uint32_t board_button_read(void)
  140. {
  141. return ((P1IN & BIT1) >> 1) == BUTTON_STATE_ACTIVE;
  142. }
  143. int board_uart_read(uint8_t * buf, int len)
  144. {
  145. for(int i = 0; i < len; i++)
  146. {
  147. // Wait until something to receive (cleared by reading buffer).
  148. while(!(UCA1IFG & UCRXIFG));
  149. buf[i] = UCA1RXBUF;
  150. }
  151. return len;
  152. }
  153. int board_uart_write(void const * buf, int len)
  154. {
  155. const char * char_buf = (const char *) buf;
  156. for(int i = 0; i < len; i++)
  157. {
  158. // Wait until TX buffer is empty (cleared by writing buffer).
  159. while(!(UCA1IFG & UCTXIFG));
  160. UCA1TXBUF = char_buf[i];
  161. }
  162. return len;
  163. }
  164. #if CFG_TUSB_OS == OPT_OS_NONE
  165. volatile uint32_t system_ticks = 0;
  166. void __attribute__ ((interrupt(TIMER0_A0_VECTOR))) TIMER0_A0_ISR (void)
  167. {
  168. system_ticks++;
  169. // TAxCCR0 CCIFG resets itself as soon as interrupt is invoked.
  170. }
  171. uint32_t board_millis(void)
  172. {
  173. uint32_t systick_mirror;
  174. // 32-bit update is not atomic on MSP430. We can read the bottom 16-bits,
  175. // an interrupt occurs, updates _all_ 32 bits, and then we return a
  176. // garbage value. And I've seen it happen!
  177. TA0CCTL0 &= ~CCIE;
  178. systick_mirror = system_ticks;
  179. TA0CCTL0 |= CCIE;
  180. return systick_mirror;
  181. }
  182. #endif