aboutsummaryrefslogtreecommitdiff
path: root/src/platforms/native/usbuart.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/platforms/native/usbuart.c')
-rw-r--r--src/platforms/native/usbuart.c64
1 files changed, 34 insertions, 30 deletions
diff --git a/src/platforms/native/usbuart.c b/src/platforms/native/usbuart.c
index 35a7d7d..3d07b33 100644
--- a/src/platforms/native/usbuart.c
+++ b/src/platforms/native/usbuart.c
@@ -26,62 +26,64 @@
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/cdc.h>
-#include "platform.h"
+#include <platform.h>
void usbuart_init(void)
{
+#if defined(BLACKMAGIC)
/* On mini hardware, UART and SWD share connector pins.
* Don't enable UART if we're being debugged. */
if ((platform_hwversion() == 1) && (SCS_DEMCR & SCS_DEMCR_TRCENA))
return;
+#endif
- rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN);
+ rcc_peripheral_enable_clock(&USBUSART_APB_ENR, USBUSART_CLK_ENABLE);
- /* UART1 TX to 'alternate function output push-pull' */
- gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
- GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
+ /* UART TX to 'alternate function output push-pull' */
+ gpio_set_mode(USBUSART_PORT, GPIO_MODE_OUTPUT_2_MHZ,
+ GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USBUSART_TX_PIN);
/* Setup UART parameters. */
- usart_set_baudrate(USART1, 38400);
- usart_set_databits(USART1, 8);
- usart_set_stopbits(USART1, USART_STOPBITS_1);
- usart_set_mode(USART1, USART_MODE_TX_RX);
- usart_set_parity(USART1, USART_PARITY_NONE);
- usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
+ usart_set_baudrate(USBUSART, 38400);
+ usart_set_databits(USBUSART, 8);
+ usart_set_stopbits(USBUSART, USART_STOPBITS_1);
+ usart_set_mode(USBUSART, USART_MODE_TX_RX);
+ usart_set_parity(USBUSART, USART_PARITY_NONE);
+ usart_set_flow_control(USBUSART, USART_FLOWCONTROL_NONE);
/* Finally enable the USART. */
- usart_enable(USART1);
+ usart_enable(USBUSART);
/* Enable interrupts */
- USART1_CR1 |= USART_CR1_RXNEIE;
- nvic_set_priority(NVIC_USART1_IRQ, IRQ_PRI_USART1);
- nvic_enable_irq(NVIC_USART1_IRQ);
+ USBUSART_CR1 |= USART_CR1_RXNEIE;
+ nvic_set_priority(USBUSART_IRQ, IRQ_PRI_USBUSART);
+ nvic_enable_irq(USBUSART_IRQ);
}
void usbuart_set_line_coding(struct usb_cdc_line_coding *coding)
{
- usart_set_baudrate(USART1, coding->dwDTERate);
- usart_set_databits(USART1, coding->bDataBits);
+ usart_set_baudrate(USBUSART, coding->dwDTERate);
+ usart_set_databits(USBUSART, coding->bDataBits);
switch(coding->bCharFormat) {
case 0:
- usart_set_stopbits(USART1, USART_STOPBITS_1);
+ usart_set_stopbits(USBUSART, USART_STOPBITS_1);
break;
case 1:
- usart_set_stopbits(USART1, USART_STOPBITS_1_5);
+ usart_set_stopbits(USBUSART, USART_STOPBITS_1_5);
break;
case 2:
- usart_set_stopbits(USART1, USART_STOPBITS_2);
+ usart_set_stopbits(USBUSART, USART_STOPBITS_2);
break;
}
switch(coding->bParityType) {
case 0:
- usart_set_parity(USART1, USART_PARITY_NONE);
+ usart_set_parity(USBUSART, USART_PARITY_NONE);
break;
case 1:
- usart_set_parity(USART1, USART_PARITY_ODD);
+ usart_set_parity(USBUSART, USART_PARITY_ODD);
break;
case 2:
- usart_set_parity(USART1, USART_PARITY_EVEN);
+ usart_set_parity(USBUSART, USART_PARITY_EVEN);
break;
}
}
@@ -94,16 +96,18 @@ void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep)
int len = usbd_ep_read_packet(dev, CDCACM_UART_ENDPOINT,
buf, CDCACM_PACKET_SIZE);
+#if defined(BLACKMAGIC)
/* Don't bother if uart is disabled.
* This will be the case on mini while we're being debugged.
*/
if(!(RCC_APB2ENR & RCC_APB2ENR_USART1EN))
return;
+#endif
- gpio_set(LED_PORT, LED_UART);
+ gpio_set(LED_PORT_UART, LED_UART);
for(int i = 0; i < len; i++)
- usart_send_blocking(USART1, buf[i]);
- gpio_clear(LED_PORT, LED_UART);
+ usart_send_blocking(USBUSART, buf[i]);
+ gpio_clear(LED_PORT_UART, LED_UART);
}
static uint8_t uart_usb_buf[CDCACM_PACKET_SIZE];
@@ -112,7 +116,7 @@ static uint8_t uart_usb_buf_size;
void usbuart_usb_in_cb(usbd_device *dev, uint8_t ep)
{
if (!uart_usb_buf_size) {
- gpio_clear(LED_PORT, LED_UART);
+ gpio_clear(LED_PORT_UART, LED_UART);
return;
}
@@ -120,11 +124,11 @@ void usbuart_usb_in_cb(usbd_device *dev, uint8_t ep)
uart_usb_buf_size = 0;
}
-void usart1_isr(void)
+void USBUSART_ISR(void)
{
- char c = usart_recv(USART1);
+ char c = usart_recv(USBUSART);
- gpio_set(LED_PORT, LED_UART);
+ gpio_set(LED_PORT_UART, LED_UART);
/* Try to send now */
if (usbd_ep_write_packet(usbdev, CDCACM_UART_ENDPOINT, &c, 1) == 1)