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