diff --git a/src/command.c b/src/command.c index df88ee53..692229dd 100644 --- a/src/command.c +++ b/src/command.c @@ -311,7 +311,7 @@ static bool cmd_convert_tdio(target *t, int argc, const char **argv) (void)t; (void) argv; (void) argc; - platform_convert_tdio(); + usbuart_convert_tdio(); return true; } diff --git a/src/platforms/jeff/platform.c b/src/platforms/jeff/platform.c index cd1bdc96..67856622 100644 --- a/src/platforms/jeff/platform.c +++ b/src/platforms/jeff/platform.c @@ -95,13 +95,11 @@ static void adc_init(void) { gpio_config_special(ADC_PORT, ADC_POS_PIN, SOC_GPIO_PERIPH_B); /* +input */ gpio_config_special(ADC_PORT, ADC_REF_PIN, SOC_GPIO_PERIPH_B); /* reference */ - //gpio_config_special(PORTA, GPIO6, SOC_GPIO_PERIPH_B); /* ground (-input) */ set_periph_clk(GCLK1, GCLK_ID_ADC); periph_clk_en(GCLK_ID_ADC, 1); - //adc_enable(ADC_REFCTRL_VREFA,0,ADC_INPUTCTRL_PIN6,ADC_INPUTCTRL_PIN16); - adc_enable(ADC_REFCTRL_VREFA,0,ADC_INPUTCTRL_GND,ADC_INPUTCTRL_PIN16); + adc_enable(ADC_REFCTRL_VREFA,0,ADC_INPUTCTRL_GND,ADC_MUXPOS); } void platform_init(void) @@ -110,7 +108,7 @@ void platform_init(void) usb_setup(); - gpio_config_special(TCK_PORT, TCK_PIN, SOC_GPIO_NONE); + //gpio_config_special(TCK_PORT, TCK_PIN, SOC_GPIO_NONE); //gpio_config_special(TMS_PORT, TMS_PIN, SOC_GPIO_NONE); gpio_config_output(LED_PORT, LED_IDLE_RUN, 0); @@ -118,6 +116,8 @@ void platform_init(void) gpio_config_output(TCK_PORT, TCK_PIN, 0); gpio_config_output(TDI_PORT, TDI_PIN, 0); + gpio_config_output(TMS_PORT, TMS_DIR_PIN, 0); + /* enable both input and output with pullup disabled by default */ PORT_DIRSET(SWDIO_PORT) = SWDIO_PIN; PORT_PINCFG(SWDIO_PORT, SWDIO_PIN_NUM) |= GPIO_PINCFG_INEN | GPIO_PINCFG_PULLEN; @@ -129,12 +129,26 @@ void platform_init(void) gpio_config_input(TDO_PORT, TDO_PIN, 0); gpio_config_output(SRST_PORT, SRST_PIN, GPIO_OUT_FLAG_DEFAULT_HIGH); - gpio_set(SRST_PORT, SRST_PIN); + gpio_clear(SRST_PORT, SRST_PIN); /* setup uart led, disable by default*/ gpio_config_output(LED_PORT_UART, LED_UART, 0);//GPIO_OUT_FLAG_DEFAULT_HIGH); gpio_clear(LED_PORT_UART, LED_UART); + + /* NOT SURE IF NEED THIS */ + /* Enable internal pull-up on PWR_BR so that we don't drive + TPWR locally or inadvertently supply power to the target. */ + /* + if (1) { + gpio_set(PWR_BR_PORT, PWR_BR_PIN); + gpio_config_input(PWR_BR_PORT, PWR_BR_PIN, GPIO_IN_FLAG_PULLUP); + } else { + gpio_set(PWR_BR_PORT, PWR_BR_PIN); + gpio_set_mode(PWR_BR_PORT, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_OPENDRAIN, PWR_BR_PIN); + }*/ + timing_init(); usbuart_init(); cdcacm_init(); @@ -144,7 +158,7 @@ void platform_init(void) void platform_srst_set_val(bool assert) { volatile int i; - if (assert) { + if (!assert) { gpio_clear(SRST_PORT, SRST_PIN); for(i = 0; i < 10000; i++) asm("nop"); } else { @@ -154,7 +168,7 @@ void platform_srst_set_val(bool assert) bool platform_srst_get_val(void) { - return gpio_get(SRST_PORT, SRST_PIN) == 0; + return gpio_get(SRST_PORT, SRST_PIN) != 0; } void platform_delay(uint32_t ms) @@ -172,7 +186,7 @@ const char *platform_target_voltage(void) adc_start(); while (!(1&(ADC->intflag))); - voltage = ((330*adc_result())>>12); + voltage = ((485*adc_result())>>12); /* 330 without divider, 485 with it */ out[0] = '0' + (char)(voltage/100); out[2] = '0' + (char)((voltage/10) % 10); @@ -205,23 +219,3 @@ char *serialno_read(char *s) void platform_request_boot(void) { } - -void platform_convert_tdio(void) -{ -#if 0 - gpio_config_special(PORTA, GPIO16, SOC_GPIO_PERIPH_C); /* TX */ - gpio_config_special(PORTA, GPIO19, SOC_GPIO_PERIPH_C); /* RX */ - - /* Select and Enable system clock */ - set_periph_clk(GCLK0, GCLK_ID_SERCOM1_CORE); // TODO which GCLK#? - periph_clk_en(GCLK_ID_SERCOM1_CORE, 1); - - usart_enable(1, 115200); - - usart_enable_rx_interrupt(1); - usart_enable_tx_interrupt(1); -#else - //gpio_config_special(TMS_PORT, TMS_PIN, SOC_GPIO_PERIPH_G); - gpio_config_special(TCK_PORT, TCK_PIN, SOC_GPIO_PERIPH_G); -#endif -} diff --git a/src/platforms/jeff/platform.h b/src/platforms/jeff/platform.h index 74066c58..496b8509 100644 --- a/src/platforms/jeff/platform.h +++ b/src/platforms/jeff/platform.h @@ -46,6 +46,7 @@ extern uint8_t running_status; #define TMS_PORT PORTA #define TMS_PIN GPIO1 +#define TMS_DIR_PIN GPIO9 #define TCK_PORT PORTA #define TCK_PIN GPIO2 @@ -79,6 +80,7 @@ extern uint8_t running_status; #define ADC_PORT PORTA #define ADC_REF_PIN GPIO3 #define ADC_POS_PIN GPIO8 +#define ADC_MUXPOS 16 #else @@ -91,9 +93,9 @@ extern uint8_t running_status; #define TDO_PORT JTAG_PORT #define TMS_DIR_PIN GPIO15 #define TMS_PIN GPIO0 -#define TCK_PIN GPIO1 -#define TDI_PIN GPIO5 -#define TDO_PIN GPIO4 +#define TCK_PIN GPIO6 +#define TDI_PIN GPIO16 +#define TDO_PIN GPIO19 #define SWDIO_DIR_PORT JTAG_PORT #define SWDIO_PORT JTAG_PORT @@ -105,7 +107,7 @@ extern uint8_t running_status; #define TRST_PORT PORTA #define TRST_PIN GPIO27 -#define PWR_BR_PORT GPIOA +#define PWR_BR_PORT PORTA #define PWR_BR_PIN GPIO28 #define SRST_PORT PORTA #define SRST_PIN GPIO8 @@ -124,7 +126,7 @@ extern uint8_t running_status; #define LED_ERROR LED_2 /* Red */ #define UART_TX_PIN GPIO4 -#define UART_RX_PIN GPIO5 +#define UART_RX_PIN GPIO7 #define UART_PERIPH SOC_GPIO_PERIPH_D #define SWO_PORT JTAG_PORT @@ -132,20 +134,24 @@ extern uint8_t running_status; #define ADC_PORT PORTA #define ADC_REF_PIN GPIO3 -#define ADC_POS_PIN TRGT_SENSE +#define ADC_POS_PIN GPIO2 +#define ADC_MUXPOS 0 #endif #define TMS_SET_MODE() { \ gpio_config_output(TMS_PORT, TMS_PIN, 0); \ + gpio_set(TMS_PORT, TMS_DIR_PIN); \ } #define SWDIO_MODE_FLOAT() do { \ PORT_DIRCLR(SWDIO_PORT) = SWDIO_PIN; \ gpio_set(SWDIO_PORT, SWDIO_PIN); \ + gpio_clear(TMS_PORT, TMS_DIR_PIN); \ } while(0) #define SWDIO_MODE_DRIVE() do { \ PORT_DIRSET(SWDIO_PORT) = SWDIO_PIN; \ + gpio_set(TMS_PORT, TMS_DIR_PIN); \ } while(0) /* extern usbd_driver samd21_usb_driver; */ @@ -196,5 +202,5 @@ static inline int platform_hwversion(void) } void uart_pop(void); -void platform_convert_tdio(void); +void usbuart_convert_tdio(void); #endif diff --git a/src/platforms/samd/usbuart.c b/src/platforms/samd/usbuart.c index 624f65c1..27a2508b 100644 --- a/src/platforms/samd/usbuart.c +++ b/src/platforms/samd/usbuart.c @@ -39,9 +39,11 @@ #include "queue.h" -#define USART_NUM 0 #define Q_SIZE 1024 +//#define USART_NUM 0 +static uint8_t USART_NUM = 0; + usbd_device * usbdev; /* input and output ring buffer */ @@ -85,20 +87,60 @@ void usbuart_init(void) usart_enable_tx_interrupt(USART_NUM); } +void usbuart_convert_tdio(void) +{ +#if 1 + if (USART_NUM) { + usart_disable(1); + USART_NUM = 0; + usbuart_init(); + return; + } +#endif + gpio_config_special(PORTA, TDI_PIN, SOC_GPIO_PERIPH_C); /* TX */ + gpio_config_special(PORTA, TDO_PIN, SOC_GPIO_PERIPH_C); /* RX */ + + /* disable USART0 (we will be using USART1 now) */ + usart_disable(0); + + USART_NUM = 1; + + /* Select and Enable system clock */ + set_periph_clk(GCLK0, GCLK_ID_SERCOM1_CORE); + periph_clk_en(GCLK_ID_SERCOM1_CORE, 1); + + usart_setup(1, 115200); + 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); +} + 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); usart_set_sbmode(USART_NUM, sbmode); usart_set_parity(USART_NUM, parity, form); + + usart_enable(USART_NUM, 0); +#else + (void) coding; + return; +#endif } void usbuart_usb_out_cb(usbd_device *dev, uint8_t ep)