samd: TDI/O conversion, usart line coding

This commit is contained in:
Ryan 2018-09-05 12:10:43 -07:00
parent 50658a5c0a
commit a599b989cf
4 changed files with 68 additions and 30 deletions

View File

@ -309,9 +309,15 @@ static bool cmd_debug_bmp(target *t, int argc, const char **argv)
static bool cmd_convert_tdio(target *t, int argc, const char **argv)
{
(void)t;
(void) argv;
(void) argc;
usbuart_convert_tdio();
uint32_t res = 0;
if (argc > 1)
res = usbuart_convert_tdio(atoi(argv[1]));
else
res = usbuart_convert_tdio(0);
gdb_outf("BAUD: %d\n", res);
return true;
}

View File

@ -57,7 +57,7 @@ void sys_tick_handler(void)
time_ms += 10;
//uart_pop();
uart_pop();
}
uint32_t platform_time_ms(void)

View File

@ -46,16 +46,16 @@ extern uint8_t running_status;
#define TMS_PORT PORTA
#define TMS_PIN GPIO1
#define TMS_DIR_PIN GPIO9
#define TMS_DIR_PIN GPIO5
#define TCK_PORT PORTA
#define TCK_PIN GPIO2
#define TDI_PORT PORTA
#define TDI_PIN GPIO3
#define TDI_PIN GPIO16
#define TDO_PORT PORTA
#define TDO_PIN GPIO3
#define TDO_PIN GPIO19
#define SWO_PORT PORTA
#define SWO_PIN GPIO6
@ -73,14 +73,15 @@ extern uint8_t running_status;
#define LED_PORT_UART PORTA
#define LED_UART GPIO12
#define UART_TX_PIN GPIO4
#define UART_RX_PIN GPIO5
#define UART_PERIPH SOC_GPIO_PERIPH_D
#define UART_TX_PIN GPIO8
#define UART_RX_PIN GPIO9
#define UART_PERIPH SOC_GPIO_PERIPH_C
#define UART_PERIPH_2 SOC_GPIO_PERIPH_C
#define ADC_PORT PORTA
#define ADC_REF_PIN GPIO3
#define ADC_POS_PIN GPIO8
#define ADC_MUXPOS 16
#define ADC_POS_PIN GPIO4
#define ADC_MUXPOS 4
#else
@ -128,6 +129,7 @@ extern uint8_t running_status;
#define UART_TX_PIN GPIO4
#define UART_RX_PIN GPIO7
#define UART_PERIPH SOC_GPIO_PERIPH_D
#define UART_PERIPH_2 SOC_GPIO_PERIPH_C
#define SWO_PORT JTAG_PORT
#define SWO_PIN SWD_PIN
@ -202,5 +204,5 @@ static inline int platform_hwversion(void)
}
void uart_pop(void);
void usbuart_convert_tdio(void);
int usbuart_convert_tdio(uint32_t arg);
#endif

View File

@ -41,9 +41,12 @@
#define Q_SIZE 1024
//#define USART_NUM 0
/* Active USART number */
static uint8_t USART_NUM = 0;
/* Current Baud Rate setting */
static uint32_t current_baud = 115200;
usbd_device * usbdev;
/* input and output ring buffer */
@ -78,27 +81,30 @@ void usbuart_init(void)
gpio_config_special(PORTA, UART_TX_PIN, UART_PERIPH); /* tx pin */
gpio_config_special(PORTA, UART_RX_PIN, UART_PERIPH); /* rx pin */
/* enable clocking to sercom3 */
/* enable clocking to sercom0 */
set_periph_clk(GCLK0, GCLK_ID_SERCOM0_CORE);
periph_clk_en(GCLK_ID_SERCOM0_CORE, 1);
usart_enable(USART_NUM, 115200);
usart_enable(USART_NUM, current_baud);
usart_enable_rx_interrupt(USART_NUM);
usart_enable_tx_interrupt(USART_NUM);
}
void usbuart_convert_tdio(void)
int usbuart_convert_tdio(uint32_t arg)
{
#if 1
(void) arg;
if (USART_NUM) {
usart_disable(1);
USART_NUM = 0;
usbuart_init();
return;
return current_baud;
}
#endif
gpio_config_special(PORTA, TDI_PIN, SOC_GPIO_PERIPH_C); /* TX */
gpio_config_special(PORTA, TDO_PIN, SOC_GPIO_PERIPH_C); /* RX */
gpio_config_special(PORTA, TDI_PIN, UART_PERIPH_2); /* TX */
gpio_config_special(PORTA, TDO_PIN, UART_PERIPH_2); /* RX */
/* disable USART0 (we will be using USART1 now) */
usart_disable(0);
@ -109,27 +115,40 @@ void usbuart_convert_tdio(void)
set_periph_clk(GCLK0, GCLK_ID_SERCOM1_CORE);
periph_clk_en(GCLK_ID_SERCOM1_CORE, 1);
usart_setup(1, 115200);
usart_setup(1, current_baud);
usart_set_pads(1, 3, 0); /* uses different pads than the default */
usart_enable(1, 0); /* baud==0 so setup is skipped */
usart_enable_rx_interrupt(1);
usart_enable_tx_interrupt(1);
return current_baud;
/* FOR TESTING PURPOSES ONLY */
#else
if (arg){
usart_disable(USART_NUM);
usart_set_baudrate(USART_NUM, arg);
usart_enable(USART_NUM, 0);
current_baud = arg;
} else
usart_send(0, 65);
return current_baud;
#endif
}
void usbuart_set_line_coding(struct usb_cdc_line_coding *coding)
{
#if 1
uint8_t sbmode = (coding->bCharFormat == 2) ? 1 : 0;
uint8_t parity = (coding->bParityType == 1) ? 0 : 1;
uint8_t form = (coding->bParityType) ? 1 : 0;
uint8_t chsize = (form) ? coding->bDataBits + 1 : coding->bDataBits;
usart_disable(USART_NUM);
usart_setup(USART_NUM, coding->dwDTERate);
/* set baud rate */
//usart_set_baudrate(USART_NUM, coding->dwDTERate);
usart_set_baudrate(USART_NUM, coding->dwDTERate);
/* set data size, stop mode, and parity */
usart_set_chsize(USART_NUM, chsize);
@ -137,10 +156,8 @@ void usbuart_set_line_coding(struct usb_cdc_line_coding *coding)
usart_set_parity(USART_NUM, parity, form);
usart_enable(USART_NUM, 0);
#else
(void) coding;
return;
#endif
current_baud = coding->dwDTERate;
}
void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep)
@ -183,9 +200,9 @@ void usbuart_usb_in_cb(usbd_device *dev, uint8_t ep)
static void uart_rx_irq(void)
{
char c = UART(USART_NUM)->data;
/* bug?, need to re-enable rx complete interrupt */
INSERTBF(UART_INTENSET_RXC, 1, UART(0)->intenset);
INSERTBF(UART_INTENSET_RXC, 1, UART(USART_NUM)->intenset);
if (!qfull(rx.head, rx.tail, Q_SIZE)) {
rx.buf[rx.head] = c;
@ -214,3 +231,16 @@ void sercom0_isr(void)
if (GETBF(UART_INTFLAG_DRE, UART(USART_NUM)->intflag))
uart_tx_irq();
}
void sercom1_isr(void)
{
/* Turn on LED */
gpio_set(LED_PORT_UART, LED_UART);
if (GETBF(UART_INTFLAG_RXC, UART(USART_NUM)->intflag))
uart_rx_irq();
if (GETBF(UART_INTFLAG_DRE, UART(USART_NUM)->intflag))
uart_tx_irq();
}