app_main.cpp
1 2 // Includes from CubeMX Generated files 3 #include "main.h" 4 5 // Peripheral 6 #include "main.h" 7 #include "gpdma.h" 8 #include "i2c.h" 9 #include "icache.h" 10 #include "iwdg.h" 11 #include "spi.h" 12 #include "usart.h" 13 #include "usb_otg.h" 14 #include "gpio.h" 15 16 // Includes for FreeRTOS 17 #include "FreeRTOS.h" 18 #include "task.h" 19 20 #include "bm_l2.h" 21 #include "bristlemouth.h" 22 #include "bootloader_helper.h" 23 #include "bsp.h" 24 #include "cli.h" 25 #include "debug.h" 26 #include "debug_adin_raw.h" 27 #include "debug_gpio.h" 28 #include "debug_i2c.h" 29 #include "debug_ina232.h" 30 #include "debug_memfault.h" 31 #include "debug_rtc.h" 32 #include "debug_spi.h" 33 #include "debug_sys.h" 34 #include "debug_uart.h" 35 #include "debug_w25.h" 36 #include "gpioISR.h" 37 #include "htu21d.h" 38 #include "ina232.h" 39 #include "io.h" 40 #include "lpm.h" 41 #include "memfault_platform_core.h" 42 #include "ms5803.h" 43 #include "pca9535.h" 44 #include "serial.h" 45 #include "serial_console.h" 46 #include "stm32_rtc.h" 47 #include "usb.h" 48 #include "w25.h" 49 #include "watchdog.h" 50 #include "debug_htu.h" 51 #include "timer_callback_handler.h" 52 #ifdef USE_BOOTLOADER 53 #include "mcuboot_cli.h" 54 #endif 55 56 #include <stdio.h> 57 58 static void defaultTask(void *parameters); 59 60 #ifndef DEBUG_USE_USART1 61 SerialHandle_t usart1 = { 62 .device = USART1, 63 .name = "debug", 64 .txPin = &DEBUG_TX, 65 .rxPin = &DEBUG_RX, 66 .interruptPin = NULL, 67 .txStreamBuffer = NULL, 68 .rxStreamBuffer = NULL, 69 .txBufferSize = 4096, 70 .rxBufferSize = 512, 71 .rxBytesFromISR = serialGenericRxBytesFromISR, 72 .getTxBytesFromISR = serialGenericGetTxBytesFromISR, 73 .processByte = NULL, 74 .data = NULL, 75 .enabled = false, 76 .flags = 0, 77 .preTxCb = NULL, 78 .postTxCb = NULL, 79 }; 80 #endif /// DEBUG_USE_USART1 81 82 // Serial console USB device 83 SerialHandle_t usbCLI = { 84 .device = (void *)0, // Using CDC 0 85 .name = "vcp-cli", 86 .txPin = NULL, 87 .rxPin = NULL, 88 .interruptPin = NULL, 89 .txStreamBuffer = NULL, 90 .rxStreamBuffer = NULL, 91 .txBufferSize = 1024, 92 .rxBufferSize = 512, 93 .rxBytesFromISR = NULL, 94 .getTxBytesFromISR = NULL, 95 .processByte = NULL, 96 .data = NULL, 97 .enabled = false, 98 .flags = 0, 99 .preTxCb = NULL, 100 .postTxCb = NULL, 101 }; 102 103 // "bristlemouth" USB serial - Use TBD 104 SerialHandle_t usbPcap = { 105 .device = (void *)1, // Using CDC 1 106 .name = "vcp-bm", 107 .txPin = NULL, 108 .rxPin = NULL, 109 .interruptPin = NULL, 110 .txStreamBuffer = NULL, 111 .rxStreamBuffer = NULL, 112 .txBufferSize = 2048, 113 .rxBufferSize = 64, 114 .rxBytesFromISR = NULL, 115 .getTxBytesFromISR = NULL, 116 .processByte = NULL, 117 .data = NULL, 118 .enabled = false, 119 .flags = 0, 120 .preTxCb = NULL, 121 .postTxCb = NULL, 122 }; 123 124 125 static const DebugI2C_t debugI2CInterfaces[] = { 126 {1, &i2c1} 127 }; 128 129 static const DebugSPI_t debugSPIInterfaces[] = { 130 {2, &spi2}, 131 {3, &spi3}, 132 }; 133 134 // TODO - move this to some debug file? 135 static const DebugGpio_t debugGpioPins[] = { 136 {"adin_cs", &ADIN_CS, GPIO_OUT}, 137 {"adin_int", &ADIN_INT, GPIO_IN}, 138 {"adin_pwr", &ADIN_PWR, GPIO_OUT}, 139 {"bm_int", &BM_INT, GPIO_IN}, 140 {"bm_cs", &BM_CS, GPIO_OUT}, 141 {"flash_cs", &FLASH_CS, GPIO_OUT}, 142 {"boot", &BOOT, GPIO_IN}, 143 {"vusb_detect", &VUSB_DETECT, GPIO_IN}, 144 {"adin_rst", &ADIN_RST, GPIO_OUT}, 145 {"boost_en", &BOOST_EN, GPIO_OUT}, 146 {"vbus_sw_en", &VBUS_SW_EN, GPIO_OUT}, 147 {"led_g", &LED_G, GPIO_OUT}, 148 {"led_r", &LED_R, GPIO_OUT}, 149 {"tp10", &TP10, GPIO_OUT}, 150 }; 151 152 #ifndef DEBUG_USE_USART1 153 extern "C" void USART1_IRQHandler(void) { 154 serialGenericUartIRQHandler(&usart1); 155 } 156 #endif // DEBUG_USE_USART1 157 158 static INA::INA232 debugIna1(&i2c1, I2C_INA_PODL_ADDR); 159 static INA::INA232 *debugIna[NUM_INA232_DEV] = { 160 &debugIna1, 161 }; 162 163 extern "C" int main(void) { 164 165 // Before doing anything, check if we should enter ROM bootloader 166 enterBootloaderIfNeeded(); 167 168 HAL_Init(); 169 170 SystemClock_Config(); 171 SystemPower_Config_ext(); 172 173 // If you NEED to have an interrupt based timer, or other interrupts running before the 174 // scheduler starts, you can enable them here. The reason for this is that FreeRTOS will 175 // disable interrupts when calling FreeRTOS API functions before the scheduler starts. 176 // In our case, this is done in some class constructors that utilize pvPortMalloc, 177 // or other FreeRTOS API calls. This means that when __libc_init_array is called, 178 // interrupts are disabled, and the timer interrupt will no longer be available until 179 // the scheduler starts. This is a problem if you are initializing a peripheral that 180 // includes a delay, see MX_USB_OTG_FS_PCD_Init() for an example where HAL_Delay() 181 // is called. It is highly recommended to avoid this by initializing everything in the 182 // default task. See https://www.freertos.org/FreeRTOS_Support_Forum_Archive/March_2017/freertos_What_is_normal_method_for_running_initialization_code_in_FreerTOS_92042073j.html 183 // for more details. 184 // portENABLE_INTERRUPTS(); 185 186 // Enable hardfault on divide-by-zero 187 SCB->CCR |= 0x10; 188 189 BaseType_t rval = xTaskCreate( 190 defaultTask, 191 "Default", 192 // TODO - verify stack size 193 128 * 4, 194 NULL, 195 // Start with very high priority during boot then downgrade 196 // once done initializing everything 197 2, 198 NULL); 199 configASSERT(rval == pdTRUE); 200 201 // Start FreeRTOS scheduler 202 vTaskStartScheduler(); 203 204 /* We should never get here as control is now taken by the scheduler */ 205 206 while (1){}; 207 } 208 209 extern SerialHandle_t usart3; 210 211 static void defaultTask( void *parameters ) { 212 213 (void)parameters; 214 215 MX_GPIO_Init(); 216 MX_USART1_UART_Init(); 217 MX_USART3_UART_Init(); 218 MX_USB_OTG_FS_PCD_Init(); 219 MX_GPDMA1_Init(); 220 MX_ICACHE_Init(); 221 MX_IWDG_Init(); 222 223 usbMspInit(); 224 225 rtcInit(); 226 227 // Initialize low power manager 228 lpmInit(); 229 230 // Inhibit low power mode during boot process 231 lpmPeripheralActive(LPM_BOOT); 232 233 startIWDGTask(); 234 startSerial(); 235 236 startCLI(); 237 pca9535StartIRQTask(); 238 239 // Use USB for serial console if USB for bringup 240 startSerialConsole(&usbCLI); 241 242 gpioISRStartTask(); 243 244 memfault_platform_boot(); 245 memfault_platform_start(); 246 247 bspInit(); 248 249 usbInit(&VUSB_DETECT, usb_is_connected); 250 251 usart3.txPin = &BM_MOSI_TX3; 252 usart3.rxPin = &BM_SCK_RX3; 253 startDebugUart(); 254 timer_callback_handler_init(); 255 spiflash::W25 debugW25(&spi2, &FLASH_CS); 256 debugSysInit(); 257 debugAdinRawInit(); 258 debugGpioInit(debugGpioPins, sizeof(debugGpioPins)/sizeof(DebugGpio_t)); 259 debugI2CInit(debugI2CInterfaces, sizeof(debugI2CInterfaces)/sizeof(DebugI2C_t)); 260 debugSPIInit(debugSPIInterfaces, sizeof(debugSPIInterfaces)/sizeof(DebugSPI_t)); 261 debugW25Init(&debugW25); 262 debugINA232Init(debugIna, NUM_INA232_DEV); 263 #ifndef DEBUG_USE_USART1 264 debugMemfaultInit(&usart1); 265 #endif // DEBUG_USE_USART1 266 debugRTCInit(); 267 #ifdef USE_BOOTLOADER 268 mcubootCliInit(); 269 #endif 270 271 // Commenting out while we test usart1 272 // lpmPeripheralInactive(LPM_BOOT); 273 274 while(1) { 275 vTaskDelay(10000); 276 } 277 278 }