/** Enable the USART peripherals. * USART 6, 1 and 3 peripherals are configured * (connected to the FTDI, UARTA and UARTB ports on the Piksi respectively). */ void usarts_enable(u32 ftdi_baud, u32 uarta_baud, u32 uartb_baud, bool do_preconfigure_hooks) { /* Ensure that the first time around, we do the preconfigure hooks */ if (!all_uarts_enabled && !do_preconfigure_hooks) return; usart_support_init(); usart_support_set_parameters(SD_FTDI, ftdi_baud); usart_support_set_parameters(SD_UARTA, uarta_baud); usart_support_set_parameters(SD_UARTB, uartb_baud); chBSemObjectInit(&ftdi_state.claimed, FALSE); ftdi_state.configured = true; if (do_preconfigure_hooks) { board_preinit_hook(); log_info("Piksi Starting..."); log_info("Firmware Version: " GIT_VERSION ""); log_info("Built: " __DATE__ " " __TIME__ ""); if (uarta_usart.configure_telemetry_radio_on_boot) { radio_preconfigure_hook(UARTA, uarta_baud, "UARTA"); } if (uartb_usart.configure_telemetry_radio_on_boot) { radio_preconfigure_hook(UARTB, uartb_baud, "UARTB"); } } chBSemObjectInit(&uarta_state.claimed, FALSE); uarta_state.configured = true; chBSemObjectInit(&uartb_state.claimed, FALSE); uartb_state.configured = true; all_uarts_enabled = true; }
/** Enable the USART peripherals. * USART 6, 1 and 3 peripherals are configured * (connected to the FTDI, UARTA and UARTB ports on the Piksi respectively). */ void usarts_enable(u32 ftdi_baud, u32 uarta_baud, u32 uartb_baud, bool do_preconfigure_hooks) { /* Ensure that the first time around, we do the preconfigure hooks */ if (!all_uarts_enabled && !do_preconfigure_hooks) return; /* First give everything a clock. */ /* Clock the USARTs. */ RCC_APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_USART6EN; RCC_APB1ENR |= RCC_APB1ENR_USART3EN; /* GPIO pins corresponding to the USART. */ RCC_AHB1ENR |= RCC_AHB1ENR_IOPAEN | RCC_AHB1ENR_IOPCEN; /* Assign the GPIO pins appropriately: * * USART TX RX Connection * ---------------------------- * 6 PC6 PC7 FTDI * 1 PA9 PA10 UARTA * 3 PC10 PC11 UARTB */ gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO6 | GPIO7); gpio_set_af(GPIOC, GPIO_AF8, GPIO6 | GPIO7); gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO9 | GPIO10); gpio_set_af(GPIOA, GPIO_AF7, GPIO9 | GPIO10); gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO10 | GPIO11); gpio_set_af(GPIOC, GPIO_AF7, GPIO10 | GPIO11); usart_set_parameters(USART6, ftdi_baud); usart_set_parameters(USART1, uarta_baud); usart_set_parameters(USART3, uartb_baud); /* FTDI (USART6) TX - DMA2, stream 6, channel 5. */ usart_tx_dma_setup(&ftdi_state.tx, USART6, DMA2, 6, 5); /* FTDI (USART6) RX - DMA2, stream 1, channel 5. */ usart_rx_dma_setup(&ftdi_state.rx, USART6, DMA2, 1, 5); chBSemInit(&ftdi_state.claimed, FALSE); ftdi_state.configured = true; if (do_preconfigure_hooks) { /* TODO: Should this really be here? */ if ((RCC_CSR & 0xFF000000) != RCC_CSR_PINRSTF) { if (RCC_CSR & RCC_CSR_IWDGRSTF) log_error("Piksi has reset due to a watchdog timeout."); if (RCC_CSR & RCC_CSR_LPWRRSTF) log_error("Low power reset detected."); if (RCC_CSR & RCC_CSR_SFTRSTF) log_info("Software reset detected."); log_info("Reset reason: %02X", (unsigned int)(RCC_CSR >> 24)); } log_info("Piksi Starting..."); RCC_CSR |= RCC_CSR_RMVF; log_info("Firmware Version: " GIT_VERSION ""); log_info("Built: " __DATE__ " " __TIME__ ""); if (uarta_usart.configure_telemetry_radio_on_boot) { radio_preconfigure_hook(USART1, uarta_baud, "UARTA"); } if (uartb_usart.configure_telemetry_radio_on_boot) { radio_preconfigure_hook(USART3, uartb_baud, "UARTB"); } } /* UARTA (USART1) TX - DMA2, stream 7, channel 4. */ usart_tx_dma_setup(&uarta_state.tx, USART1, DMA2, 7, 4); /* UARTA (USART1) RX - DMA2, stream 2, channel 4. */ usart_rx_dma_setup(&uarta_state.rx, USART1, DMA2, 2, 4); chBSemInit(&uarta_state.claimed, FALSE); uarta_state.configured = true; /* UARTB (USART3) TX - DMA1, stream 3, channel 4. */ usart_tx_dma_setup(&uartb_state.tx, USART3, DMA1, 3, 4); /* UARTB (USART3) RX - DMA1, stream 1, channel 4. */ usart_rx_dma_setup(&uartb_state.rx, USART3, DMA1, 1, 4); chBSemInit(&uartb_state.claimed, FALSE); uartb_state.configured = true; all_uarts_enabled = true; }