diff --git a/src/atsam/sam3_usb.c b/src/atsam/sam3_usb.c index d9101478..b990be88 100644 --- a/src/atsam/sam3_usb.c +++ b/src/atsam/sam3_usb.c @@ -179,29 +179,6 @@ handle_end_reset(void) UOTGHS->UOTGHS_DEVICR = UOTGHS_DEVICR_EORSTC; } -void -usbserial_init(void) -{ - // Setup USB clock - enable_pclock(ID_UOTGHS); - PMC->CKGR_UCKR = CKGR_UCKR_UPLLCOUNT(3) | CKGR_UCKR_UPLLEN; - while (!(PMC->PMC_SR & PMC_SR_LOCKU)) - ; - PMC->PMC_USB = PMC_USB_USBS | PMC_USB_USBDIV(0); - PMC->PMC_SCER = PMC_SCER_UOTGCLK; - - // Enable USB - UOTGHS->UOTGHS_CTRL = (UOTGHS_CTRL_UIMOD | UOTGHS_CTRL_OTGPADE - | UOTGHS_CTRL_USBE); - UOTGHS->UOTGHS_DEVCTRL = UOTGHS_DEVCTRL_SPDCONF_FORCED_FS; - - // Enable interrupts - NVIC_SetPriority(UOTGHS_IRQn, 1); - NVIC_EnableIRQ(UOTGHS_IRQn); - UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_EORSTES; -} -DECL_INIT(usbserial_init); - void __visible UOTGHS_Handler(void) { @@ -227,3 +204,26 @@ UOTGHS_Handler(void) if (s & (UOTGHS_DEVISR_PEP_0 << USB_CDC_EP_BULK_IN)) usb_notify_bulk_in(); } + +void +usbserial_init(void) +{ + // Setup USB clock + enable_pclock(ID_UOTGHS); + PMC->CKGR_UCKR = CKGR_UCKR_UPLLCOUNT(3) | CKGR_UCKR_UPLLEN; + while (!(PMC->PMC_SR & PMC_SR_LOCKU)) + ; + PMC->PMC_USB = PMC_USB_USBS | PMC_USB_USBDIV(0); + PMC->PMC_SCER = PMC_SCER_UOTGCLK; + + // Enable USB + UOTGHS->UOTGHS_CTRL = (UOTGHS_CTRL_UIMOD | UOTGHS_CTRL_OTGPADE + | UOTGHS_CTRL_USBE); + UOTGHS->UOTGHS_DEVCTRL = UOTGHS_DEVCTRL_SPDCONF_FORCED_FS; + + // Enable interrupts + NVIC_SetPriority(UOTGHS_IRQn, 1); + NVIC_EnableIRQ(UOTGHS_IRQn); + UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_EORSTES; +} +DECL_INIT(usbserial_init); diff --git a/src/atsam/sam4_usb.c b/src/atsam/sam4_usb.c index 0ba85f4b..33e886c5 100644 --- a/src/atsam/sam4_usb.c +++ b/src/atsam/sam4_usb.c @@ -160,26 +160,6 @@ usb_set_configure(void) UDP->UDP_GLB_STAT |= UDP_GLB_STAT_CONFG; } -DECL_CONSTANT_STR("RESERVE_PINS_USB", "PB10,PB11"); - -void -usbserial_init(void) -{ - // Enable clocks - enable_pclock(ID_UDP); - PMC->PMC_USB = PMC_USB_USBDIV(5 - 1); // PLLA=240Mhz; divide by 5 for 48Mhz - PMC->PMC_SCER = PMC_SCER_UDP; - - // Enable USB pullup - UDP->UDP_TXVC = UDP_TXVC_PUON | UDP_TXVC_TXVDIS; - - // Enable interrupts - UDP->UDP_ICR = 0xffffffff; - NVIC_SetPriority(UDP_IRQn, 1); - NVIC_EnableIRQ(UDP_IRQn); -} -DECL_INIT(usbserial_init); - // Configure endpoint 0 after usb reset completes static void handle_end_reset(void) @@ -216,3 +196,23 @@ UDP_Handler(void) if (s & (1<PMC_USB = PMC_USB_USBDIV(5 - 1); // PLLA=240Mhz; divide by 5 for 48Mhz + PMC->PMC_SCER = PMC_SCER_UDP; + + // Enable USB pullup + UDP->UDP_TXVC = UDP_TXVC_PUON | UDP_TXVC_TXVDIS; + + // Enable interrupts + UDP->UDP_ICR = 0xffffffff; + NVIC_SetPriority(UDP_IRQn, 1); + NVIC_EnableIRQ(UDP_IRQn); +} +DECL_INIT(usbserial_init); diff --git a/src/atsam/serial.c b/src/atsam/serial.c index 051256d4..62c8ac44 100644 --- a/src/atsam/serial.c +++ b/src/atsam/serial.c @@ -34,6 +34,28 @@ static const uint32_t tx_pin = GPIO('A', 10); DECL_CONSTANT_STR("RESERVE_PINS_serial", "PA9,PA10"); #endif +void __visible +Serial_IRQ_Handler(void) +{ + uint32_t status = Port->UART_SR; + if (status & UART_SR_RXRDY) + serial_rx_byte(Port->UART_RHR); + if (status & UART_SR_TXRDY) { + uint8_t data; + int ret = serial_get_tx_byte(&data); + if (ret) + Port->UART_IDR = UART_IDR_TXRDY; + else + Port->UART_THR = data; + } +} + +void +serial_enable_tx_irq(void) +{ + Port->UART_IER = UART_IDR_TXRDY; +} + void serial_init(void) { @@ -57,25 +79,3 @@ serial_init(void) Port->UART_CR = UART_CR_RXEN | UART_CR_TXEN; } DECL_INIT(serial_init); - -void __visible -Serial_IRQ_Handler(void) -{ - uint32_t status = Port->UART_SR; - if (status & UART_SR_RXRDY) - serial_rx_byte(Port->UART_RHR); - if (status & UART_SR_TXRDY) { - uint8_t data; - int ret = serial_get_tx_byte(&data); - if (ret) - Port->UART_IDR = UART_IDR_TXRDY; - else - Port->UART_THR = data; - } -} - -void -serial_enable_tx_irq(void) -{ - Port->UART_IER = UART_IDR_TXRDY; -}