aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/lib/tinyusb/hw/bsp/msp432e4/family.c
blob: 3d2d4085e7488d6363e18f0f29a1cb9ad1325245 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2019 Ha Thach (tinyusb.org)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * This file is part of the TinyUSB stack.
 */

#include "bsp/board.h"
#include "board.h"
#include "msp.h"

//--------------------------------------------------------------------+
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
void USB0_IRQHandler(void)
{
#if CFG_TUH_ENABLED
  tuh_int_handler(0);
#endif
#if CFG_TUD_ENABLED
  tud_int_handler(0);
#endif
}

//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+

void board_init(void)
{
  unsigned bits;
  /* Turn off power domains that unused peripherals belong to */
  SYSCTL->PCCAN  = 0u;
#ifdef __MCU_HAS_LCD0__
  SYSCTL->PCLCD  = 0u;
#endif
  SYSCTL->PCEMAC = 0u;
  SYSCTL->PCEPHY = 0u;
  SYSCTL->PCCCM  = 0u;

  /* --- Setup system clock --- */
  /* Start power-up process of the main oscillator */
  SYSCTL->MOSCCTL = SYSCTL_MOSCCTL_OSCRNG;
  while (!(SYSCTL->RIS & SYSCTL_RIS_MOSCPUPRIS)) ; /* Wait for completion */
  SYSCTL->MISC = SYSCTL_MISC_MOSCPUPMIS; /* Clear the completion interrupt status */
  /* Set the main oscillator to PLL reference clock */
  SYSCTL->RSCLKCFG = SYSCTL_RSCLKCFG_PLLSRC_MOSC;
  /* PLL freq. = (MOSC freq. / 10) * 96 = 240MHz */
  SYSCTL->PLLFREQ1 = (4 << SYSCTL_PLLFREQ1_N_S) | (1 << SYSCTL_PLLFREQ1_Q_S);
  SYSCTL->PLLFREQ0 = (96 << SYSCTL_PLLFREQ0_MINT_S) | SYSCTL_PLLFREQ0_PLLPWR;
  /* Set BCHT=6, BCE=0, WS=5 for 120MHz system clock */
  SYSCTL->MEMTIM0 = SYSCTL_MEMTIM0_EBCHT_3_5 | (5 << SYSCTL_MEMTIM0_EWS_S) |
    SYSCTL_MEMTIM0_FBCHT_3_5 | (5 << SYSCTL_MEMTIM0_FWS_S) | SYSCTL_MEMTIM0_MB1;
  /* Wait for completion of PLL power-up process */
  while (!(SYSCTL->RIS & SYSCTL_RIS_PLLLRIS)) ;
  SYSCTL->MISC = SYSCTL_MISC_PLLLMIS; /* Clear the completion interrupt status */
  /* Switch the system clock to PLL/4 */
  SYSCTL->RSCLKCFG = SYSCTL_RSCLKCFG_MEMTIMU | SYSCTL_RSCLKCFG_ACG |
         SYSCTL_RSCLKCFG_USEPLL | SYSCTL_RSCLKCFG_PLLSRC_MOSC | (1 << SYSCTL_RSCLKCFG_PSYSDIV_S);

  SystemCoreClockUpdate();
#if CFG_TUSB_OS == OPT_OS_NONE
  SysTick_Config(SystemCoreClock / 1000);
#elif CFG_TUSB_OS == OPT_OS_FREERTOS
  // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
  NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
#endif

  /* USR_LED1 ON1 */
  bits              = TU_BIT(CLK_LED);
  SYSCTL->RCGCGPIO |= bits;
  while (bits != (SYSCTL->RCGCGPIO & bits)) ;
  GPIO_LED->DIR     = TU_BIT(GPIO_LED_PIN);
  GPIO_LED->DEN     = TU_BIT(GPIO_LED_PIN);

  /* USR_SW1 PJ0 */
  bits              = TU_BIT(CLK_BUTTON);
  SYSCTL->RCGCGPIO |= bits;
  while (bits != (SYSCTL->RCGCGPIO & bits)) ;
  GPIO_BUTTON->PUR  = TU_BIT(GPIO_BUTTON_PIN);
  GPIO_BUTTON->DEN  = TU_BIT(GPIO_BUTTON_PIN);

  /* UART PA0,1 */
  bits              = TU_BIT(0);
  SYSCTL->RCGCGPIO |= bits;
  while (bits != (SYSCTL->RCGCGPIO & bits)) ;
  GPIOA->AFSEL      = 3u;
  GPIOA->PCTL       = 0x11u;
  GPIOA->DEN        = 3u;

  SYSCTL->RCGCUART |= 1u << 0;
  while (!(SYSCTL->PRUART & (1u << 0))) ;
  UART0->CTL        = 0;
  UART0->IBRD       = 8;  /* 8.68056 = 16MHz / (16 * 115200) */
  UART0->FBRD       = 44; /* 0.6875 = 44/64 -> 115108bps (0.08%) */
  UART0->LCRH       = UART_LCRH_WLEN_8 | UART_LCRH_FEN;
  UART0->CC         = UART_CC_CS_PIOSC; /* Set the baud clock to PIOSC */
  UART0->CTL        = UART_CTL_RXE | UART_CTL_TXE | UART_CTL_UARTEN;

  /* USB PB0(ID) PB1(VBUS) PL6,7(DP,DM) */
  bits              = TU_BIT(1) | TU_BIT(10);
  SYSCTL->RCGCGPIO |= bits;
  while (bits != (SYSCTL->RCGCGPIO & bits)) ;
  GPIOB->AMSEL      = TU_BIT(0) | TU_BIT(1);
  GPIOL->AMSEL      = TU_BIT(6) | TU_BIT(7);

#if CFG_TUH_ENABLED
  /* USB PD6(EPEN) */
  bits              = TU_BIT(3);
  SYSCTL->RCGCGPIO |= bits;
  while (bits != (SYSCTL->RCGCGPIO & bits)) ;
  GPIOD->AFSEL      = TU_BIT(6);
  GPIOD->PCTL       = 0x05000000u;
  GPIOD->DEN        = TU_BIT(6);
#endif

  SYSCTL->RCGCUSB   = 1u; /* Open the clock gate for SYSCLK */
  while (!(SYSCTL->PRUSB & (1u << 0))) ;
  USB0->CC          = USB_CC_CLKEN | (3u << USB_CC_CLKDIV_S); /* 60MHz = 240MHz / 4 */
  __DMB(); /* Wait for completion of opening of the clock gate */

  SYSCTL->SRUSB     = 1u;
  for (int i = 0; i < 16; ++i) __NOP();
  SYSCTL->SRUSB     = 0u;

  USB0->CC          = USB_CC_CLKEN | (3u << USB_CC_CLKDIV_S); /* 60MHz = 240MHz / 4 */
  __DMB(); /* Wait for completion of opening of the clock gate */
#if CFG_TUH_ENABLED
  USB0->GPCS = USB_GPCS_DEVMOD_OTG;
  USB0->EPC  = USB_EPC_EPENDE | USB_EPC_EPEN_HIGH;
#endif
#if CFG_TUD_ENABLED
  USB0->GPCS = USB_GPCS_DEVMOD_DEVVBUS;
#endif
}

//--------------------------------------------------------------------+
// Board porting API
//--------------------------------------------------------------------+

void board_led_write(bool state)
{
  if (state)
    GPIO_LED->DATA |= TU_BIT(GPIO_LED_PIN);
  else
    GPIO_LED->DATA &= ~TU_BIT(GPIO_LED_PIN);
}

uint32_t board_button_read(void)
{
  return (GPIO_BUTTON->DATA & TU_BIT(GPIO_BUTTON_PIN)) ? 0u : 1u;
}

int board_uart_read(uint8_t * buf, int len)
{
  for (int i = 0; i < len; ++i) {
    while (UART0->FR & UART_FR_RXFE) ;
    *buf++ = UART0->DR;
  }
  return len;
}

int board_uart_write(void const * buf, int len)
{
  uint8_t const *p = (uint8_t const *)buf;
  for (int i = 0; i < len; ++i) {
    while (UART0->FR & UART_FR_TXFF) ;
    UART0->DR = *p++;
  }
  return len;
}

#if CFG_TUSB_OS  == OPT_OS_NONE
volatile uint32_t system_ticks = 0u;
void SysTick_Handler(void)
{
  system_ticks++;
}

uint32_t board_millis(void)
{
  return system_ticks;
}
#endif