summaryrefslogtreecommitdiff
path: root/hw/bsp/stm32wba/family.c
blob: 615a71cf542b1bc3cc5eab37b2074f5ec56086a9 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2025 Dalton Caron
 *
 * 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.
 */

/* metadata:
   manufacturer: STMicroelectronics
*/

#include <stdbool.h>

#include "stm32wbaxx_hal.h"
#include "bsp/board_api.h"
#include "board.h"

//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM
//--------------------------------------------------------------------+
#ifndef USART_TIMEOUT_TICKS
#define USART_TIMEOUT_TICKS 1000
#endif

static UART_HandleTypeDef uart_handle;

//--------------------------------------------------------------------+
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
void USB_OTG_HS_IRQHandler(void) {
  tud_int_handler(0);
}

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

static void board_gpio_configuration(void) {
  GPIO_InitTypeDef gpio_init = {0};

  USART_GPIO_CLK_EN();
  USART_CLK_EN();

  // Configure USART TX pin
  gpio_init.Pin = USART_TX_PIN;
  gpio_init.Mode = GPIO_MODE_AF_PP;
  gpio_init.Pull = GPIO_NOPULL;
  gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
  gpio_init.Alternate = USART_GPIO_AF;
  HAL_GPIO_Init(USART_TX_GPIO_PORT, &gpio_init);

  // Configure USART RX pin
  gpio_init.Pin = USART_RX_PIN;
  gpio_init.Mode = GPIO_MODE_AF_PP;
  gpio_init.Pull = GPIO_PULLUP;
  gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
  gpio_init.Alternate = USART_GPIO_AF;
  HAL_GPIO_Init(USART_RX_GPIO_PORT, &gpio_init);

  // Configure the LED
  LED_CLK_EN();
  gpio_init.Pin = LED_PIN;
  gpio_init.Mode = GPIO_MODE_OUTPUT_PP;
  gpio_init.Pull = GPIO_PULLUP;
  gpio_init.Speed = GPIO_SPEED_FREQ_LOW;
  gpio_init.Alternate = 0;
  HAL_GPIO_Init(LED_PORT, &gpio_init);

  // Default LED state is off
  board_led_write(false);

  // Configure the button
  BUTTON_CLK_EN();
  gpio_init.Pin = BUTTON_PIN;
  gpio_init.Mode = GPIO_MODE_INPUT;
  gpio_init.Pull = GPIO_PULLUP;
  gpio_init.Speed = GPIO_SPEED_FREQ_LOW;
  gpio_init.Alternate = 0;
  HAL_GPIO_Init(BUTTON_PORT, &gpio_init);

  // Configure USB DM and DP pins. This is optional, and maintained only for user guidance.
  gpio_init.Pin = (GPIO_PIN_7 | GPIO_PIN_6);
  gpio_init.Mode = GPIO_MODE_INPUT;
  gpio_init.Pull = GPIO_NOPULL;
  gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOD, &gpio_init);
}

static void board_uart_configuration(void) {
  uart_handle = ( UART_HandleTypeDef){
      .Instance = USART_DEV,
      .Init.BaudRate = CFG_BOARD_UART_BAUDRATE,
      .Init.WordLength = UART_WORDLENGTH_8B,
      .Init.StopBits = UART_STOPBITS_1,
      .Init.Parity = UART_PARITY_NONE,
      .Init.HwFlowCtl = UART_HWCONTROL_NONE,
      .Init.Mode = UART_MODE_TX_RX,
      .Init.OverSampling = UART_OVERSAMPLING_16
  };
  HAL_UART_Init(&uart_handle);
  HAL_UARTEx_EnableFifoMode(&uart_handle);
}

void board_init(void) {
  board_system_clock_config();
  board_gpio_configuration();
  board_uart_configuration();

  #ifdef USB_OTG_HS
  // STM32WBA65/64/62 only has 1 USB HS port

  #if CFG_TUSB_OS == OPT_OS_NONE
  // 1ms tick timer
  SysTick_Config(SystemCoreClock / 1000);
  #elif CFG_TUSB_OS == OPT_OS_FREERTOS
  // Explicitly disable systick to prevent its ISR from running before scheduler start
  SysTick->CTRL &= ~1U;

  // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
  NVIC_SetPriority(USB_OTG_HS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
  #endif

  // USB clock enable
  __HAL_RCC_USB_OTG_HS_CLK_ENABLE();
  __HAL_RCC_USB_OTG_HS_PHY_CLK_ENABLE();

  // See the reference manual section 11.4.7 for the USB OTG powering sequence

  // Remove the VDDUSB power isolation
  PWR->SVMCR |= PWR_SVMCR_USV;

  // Enable VDD11USB supply by clearing VDD11USBDIS to 0
  PWR->VOSR &= ~PWR_VOSR_VDD11USBDIS;

  // Enable USB OTG internal power by setting USBPWREN to 1
  PWR->VOSR |= PWR_VOSR_USBPWREN;

  // Wait for VDD11USB supply to be ready in VDD11USBRDY = 1
  while ((PWR->VOSR & PWR_VOSR_VDD11USBRDY) == 0) {}

  // Enable USB OTG booster by setting USBBOOSTEN to 1
  PWR->VOSR |= PWR_VOSR_USBBOOSTEN;

  // Wait for USB OTG booster to be ready in USBBOOSTRDY = 1
  while ((PWR->VOSR & PWR_VOSR_USBBOOSTRDY) == 0) {}

  // Enable USB power on Pwrctrl CR2 register
  PWR->SVMCR |= PWR_SVMCR_USV;

  // Set the reference clock selection (must match the clock source)
  SYSCFG->OTGHSPHYCR &= ~SYSCFG_OTGHSPHYCR_CLKSEL;
  SYSCFG->OTGHSPHYCR |= SYSCFG_OTGHSPHYCR_CLKSEL_0 | SYSCFG_OTGHSPHYCR_CLKSEL_1 |
      SYSCFG_OTGHSPHYCR_CLKSEL_3;// 32MHz clock

  // Configuring the SYSCFG registers OTG_HS PHY
  SYSCFG->OTGHSPHYCR |= SYSCFG_OTGHSPHYCR_EN;
  #endif // USB_OTG_HS
}

void board_led_write(bool state) {
  GPIO_PinState pin_state = (GPIO_PinState)(state ? LED_STATE_ON : (1 - LED_STATE_ON));
  HAL_GPIO_WritePin(LED_PORT, LED_PIN, pin_state);
}

uint32_t board_button_read(void) { return HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN) == BUTTON_STATE_ACTIVE; }

int board_uart_read(uint8_t *buf, int len) {
  int count = 0;
  while (count < len) {
    if (__HAL_UART_GET_FLAG(&uart_handle, UART_FLAG_RXNE)) {
      buf[count] = (uint8_t) uart_handle.Instance->RDR;
      count++;
    } else {
      break;
    }
  }
  return count;
}

int board_uart_write(void const *buf, int len) {
  const uint8_t *p = (const uint8_t *) buf;
  int count = 0;
  while (count < len) {
    if (__HAL_UART_GET_FLAG(&uart_handle, UART_FLAG_TXE)) {
      uart_handle.Instance->TDR = p[count];
      count++;
    } else {
      break;
    }
  }
  return count;
}

#if CFG_TUSB_OS == OPT_OS_NONE
volatile uint32_t system_ticks = 0;

void SysTick_Handler(void) {
  HAL_IncTick();
  system_ticks++;
}

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

void HardFault_Handler(void) { asm( "bkpt 1" ); }

// Required by __libc_init_array in startup code if we are compiling using -nostdlib/-nostartfiles.
void _init(void);

void _init(void) {}