Clean up common SWD stuff.

This commit is contained in:
Gareth McMullin 2016-07-01 11:51:17 +12:00
parent 9b8e2c3ad1
commit 5832d8a42a
7 changed files with 120 additions and 304 deletions

View File

@ -1,6 +1,6 @@
PROBE_HOST ?= native PROBE_HOST ?= native
PLATFORM_DIR = platforms/$(PROBE_HOST) PLATFORM_DIR = platforms/$(PROBE_HOST)
VPATH += platforms/common $(PLATFORM_DIR) VPATH += $(PLATFORM_DIR) platforms/common
ENABLE_DEBUG ?= ENABLE_DEBUG ?=
ifneq ($(V), 1) ifneq ($(V), 1)
@ -53,6 +53,7 @@ SRC = \
stm32l0.c \ stm32l0.c \
stm32l4.c \ stm32l4.c \
swdptap.c \ swdptap.c \
swdptap_generic.c \
target.c \ target.c \
include $(PLATFORM_DIR)/Makefile.inc include $(PLATFORM_DIR)/Makefile.inc

View File

@ -50,6 +50,16 @@ int adiv5_swdp_scan(void)
ADIv5_DP_t *dp = (void*)calloc(1, sizeof(*dp)); ADIv5_DP_t *dp = (void*)calloc(1, sizeof(*dp));
swdptap_init(); swdptap_init();
/* Switch from JTAG to SWD mode */
swdptap_seq_out(0xFFFF, 16);
for(int i = 0; i < 50; i++)
swdptap_bit_out(1);
swdptap_seq_out(0xE79E, 16); /* 0b0111100111100111 */
for(int i = 0; i < 50; i++)
swdptap_bit_out(1);
swdptap_seq_out(0, 16);
/* Read the SW-DP IDCODE register to syncronise */ /* Read the SW-DP IDCODE register to syncronise */
/* This could be done with adiv_swdp_low_access(), but this doesn't /* This could be done with adiv_swdp_low_access(), but this doesn't
* allow the ack to be checked here. */ * allow the ack to be checked here. */

View File

@ -22,10 +22,14 @@
#define __SWDPTAP_H #define __SWDPTAP_H
int swdptap_init(void); int swdptap_init(void);
void swdptap_reset(void);
/* Primitive functions */
bool swdptap_bit_in(void);
void swdptap_bit_out(bool val);
/* High level functions, provided as weak in swdptap_generic.c */
uint32_t swdptap_seq_in(int ticks); uint32_t swdptap_seq_in(int ticks);
uint8_t swdptap_seq_in_parity(uint32_t *data, int ticks); bool swdptap_seq_in_parity(uint32_t *data, int ticks);
void swdptap_seq_out(uint32_t MS, int ticks); void swdptap_seq_out(uint32_t MS, int ticks);
void swdptap_seq_out_parity(uint32_t MS, int ticks); void swdptap_seq_out_parity(uint32_t MS, int ticks);

View File

@ -20,24 +20,24 @@
/* This file implements the low-level SW-DP interface. */ /* This file implements the low-level SW-DP interface. */
#include <stdio.h>
#include "general.h" #include "general.h"
#include "swdptap.h" #include "swdptap.h"
#include "gdb_packet.h" int swdptap_init(void)
{
return 0;
}
static void swdptap_turnaround(uint8_t dir) static void swdptap_turnaround(uint8_t dir)
{ {
static uint8_t olddir = 0; static uint8_t olddir = 0;
DEBUG("%s", dir ? "\n-> ":"\n<- ");
/* Don't turnaround if direction not changing */ /* Don't turnaround if direction not changing */
if(dir == olddir) return; if(dir == olddir) return;
olddir = dir; olddir = dir;
DEBUG("%s", dir ? "\n-> ":"\n<- ");
if(dir) if(dir)
SWDIO_MODE_FLOAT(); SWDIO_MODE_FLOAT();
gpio_set(SWCLK_PORT, SWCLK_PIN); gpio_set(SWCLK_PORT, SWCLK_PIN);
@ -46,10 +46,12 @@ static void swdptap_turnaround(uint8_t dir)
SWDIO_MODE_DRIVE(); SWDIO_MODE_DRIVE();
} }
static uint8_t swdptap_bit_in(void) bool swdptap_bit_in(void)
{ {
uint16_t ret; uint16_t ret;
swdptap_turnaround(1);
ret = gpio_get(SWDIO_PORT, SWDIO_PIN); ret = gpio_get(SWDIO_PORT, SWDIO_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN); gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_clear(SWCLK_PORT, SWCLK_PIN); gpio_clear(SWCLK_PORT, SWCLK_PIN);
@ -59,95 +61,14 @@ static uint8_t swdptap_bit_in(void)
return ret != 0; return ret != 0;
} }
static void swdptap_bit_out(uint8_t val) void swdptap_bit_out(bool val)
{ {
DEBUG("%d", val); DEBUG("%d", val);
swdptap_turnaround(0);
gpio_set_val(SWDIO_PORT, SWDIO_PIN, val); gpio_set_val(SWDIO_PORT, SWDIO_PIN, val);
gpio_set(SWCLK_PORT, SWCLK_PIN); gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_clear(SWCLK_PORT, SWCLK_PIN); gpio_clear(SWCLK_PORT, SWCLK_PIN);
} }
int swdptap_init(void)
{
/* This must be investigated in more detail.
* As described in STM32 Reference Manual... */
swdptap_reset();
swdptap_seq_out(0xE79E, 16); /* 0b0111100111100111 */
swdptap_reset();
swdptap_seq_out(0, 16);
return 0;
}
void swdptap_reset(void)
{
swdptap_turnaround(0);
/* 50 clocks with TMS high */
for(int i = 0; i < 50; i++) swdptap_bit_out(1);
}
uint32_t swdptap_seq_in(int ticks)
{
uint32_t index = 1;
uint32_t ret = 0;
swdptap_turnaround(1);
while(ticks--) {
if(swdptap_bit_in()) ret |= index;
index <<= 1;
}
return ret;
}
uint8_t swdptap_seq_in_parity(uint32_t *ret, int ticks)
{
uint32_t index = 1;
uint8_t parity = 0;
*ret = 0;
swdptap_turnaround(1);
while(ticks--) {
if(swdptap_bit_in()) {
*ret |= index;
parity ^= 1;
}
index <<= 1;
}
if(swdptap_bit_in()) parity ^= 1;
return parity;
}
void swdptap_seq_out(uint32_t MS, int ticks)
{
swdptap_turnaround(0);
while(ticks--) {
swdptap_bit_out(MS & 1);
MS >>= 1;
}
}
void swdptap_seq_out_parity(uint32_t MS, int ticks)
{
uint8_t parity = 0;
swdptap_turnaround(0);
while(ticks--) {
swdptap_bit_out(MS & 1);
parity ^= MS;
MS >>= 1;
}
swdptap_bit_out(parity & 1);
}

View File

@ -29,9 +29,7 @@
#include "general.h" #include "general.h"
#include "swdptap.h" #include "swdptap.h"
static void swdptap_turnaround(uint8_t dir); static uint8_t olddir = 0;
static uint8_t swdptap_bit_in(void);
static void swdptap_bit_out(uint8_t val);
int swdptap_init(void) int swdptap_init(void)
{ {
@ -46,33 +44,17 @@ int swdptap_init(void)
} }
assert(ftdi_write_data(ftdic, (void*)"\xAB\xA8", 2) == 2); assert(ftdi_write_data(ftdic, (void*)"\xAB\xA8", 2) == 2);
olddir = 0;
/* This must be investigated in more detail.
* As described in STM32 Reference Manual... */
swdptap_seq_out(0xFFFF, 16);
swdptap_reset();
swdptap_seq_out(0xE79E, 16); /* 0b0111100111100111 */
swdptap_reset();
swdptap_seq_out(0, 16);
return 0; return 0;
} }
void swdptap_reset(void)
{
swdptap_turnaround(0);
/* 50 clocks with TMS high */
for(int i = 0; i < 50; i++)
swdptap_bit_out(1);
}
static void swdptap_turnaround(uint8_t dir) static void swdptap_turnaround(uint8_t dir)
{ {
static uint8_t olddir = 0;
platform_buffer_flush(); platform_buffer_flush();
if(dir == olddir) return; if (dir == olddir)
return;
olddir = dir; olddir = dir;
if(dir) /* SWDIO goes to input */ if(dir) /* SWDIO goes to input */
@ -85,10 +67,12 @@ static void swdptap_turnaround(uint8_t dir)
assert(ftdi_set_bitmode(ftdic, 0xAB, BITMODE_BITBANG) == 0); assert(ftdi_set_bitmode(ftdic, 0xAB, BITMODE_BITBANG) == 0);
} }
static uint8_t swdptap_bit_in(void) bool swdptap_bit_in(void)
{ {
uint8_t ret; uint8_t ret;
swdptap_turnaround(1);
ftdi_read_pins(ftdic, &ret); ftdi_read_pins(ftdic, &ret);
ret &= 0x08; ret &= 0x08;
ftdi_write_data(ftdic, (void *)"\xA1\xA0", 2); ftdi_write_data(ftdic, (void *)"\xA1\xA0", 2);
@ -96,75 +80,16 @@ static uint8_t swdptap_bit_in(void)
return ret; return ret;
} }
static void swdptap_bit_out(uint8_t val) void swdptap_bit_out(bool val)
{ {
uint8_t buf[3] = "\xA0\xA1\xA0"; uint8_t buf[3] = "\xA0\xA1\xA0";
if(val) { swdptap_turnaround(0);
if (val) {
for(int i = 0; i < 3; i++) for(int i = 0; i < 3; i++)
buf[i] |= 0x08; buf[i] |= 0x08;
} }
platform_buffer_write(buf, 3); platform_buffer_write(buf, 3);
} }
uint32_t swdptap_seq_in(int ticks)
{
uint32_t index = 1;
uint32_t ret = 0;
swdptap_turnaround(1);
while (ticks--) {
if (swdptap_bit_in())
ret |= index;
index <<= 1;
}
return ret;
}
uint8_t swdptap_seq_in_parity(uint32_t *ret, int ticks)
{
uint32_t index = 1;
uint8_t parity = 0;
*ret = 0;
swdptap_turnaround(1);
while (ticks--) {
if (swdptap_bit_in()) {
*ret |= index;
parity ^= 1;
}
index <<= 1;
}
if (swdptap_bit_in())
parity ^= 1;
return parity;
}
void swdptap_seq_out(uint32_t MS, int ticks)
{
swdptap_turnaround(0);
while (ticks--) {
swdptap_bit_out(MS & 1);
MS >>= 1;
}
}
void swdptap_seq_out_parity(uint32_t MS, int ticks)
{
uint8_t parity = 0;
swdptap_turnaround(0);
while (ticks--) {
swdptap_bit_out(MS & 1);
parity ^= MS;
MS >>= 1;
}
swdptap_bit_out(parity & 1);
}

View File

@ -1,124 +0,0 @@
#include "general.h"
#include "swdptap.h"
static void swdptap_turnaround(uint8_t dir)
{
static uint8_t olddir = 0;
DEBUG("%s", dir ? "\n-> ":"\n<- ");
/* Don't turnaround if direction not changing */
if(dir == olddir) return;
olddir = dir;
if(dir)
SWDIO_MODE_FLOAT();
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_clear(SWCLK_PORT, SWCLK_PIN);
if(!dir)
SWDIO_MODE_DRIVE();
}
static uint8_t swdptap_bit_in(void)
{
uint16_t ret;
ret = gpio_get(SWDIO_PORT, SWDIO_PIN);
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_clear(SWCLK_PORT, SWCLK_PIN);
DEBUG("%d", ret?1:0);
return ret != 0;
}
static void swdptap_bit_out(uint8_t val)
{
DEBUG("%d", val);
gpio_set_val(SWDIO_PORT, SWDIO_PIN, val);
gpio_set(SWCLK_PORT, SWCLK_PIN);
gpio_clear(SWCLK_PORT, SWCLK_PIN);
}
int
swdptap_init(void)
{
swdptap_reset();
swdptap_seq_out(0xE79E, 16); /* 0b0111100111100111 */
swdptap_reset();
swdptap_seq_out(0, 16);
return 0;
}
void
swdptap_reset(void)
{
swdptap_turnaround(0);
/* 50 clocks with TMS high */
for(int i = 0; i < 50; i++) swdptap_bit_out(1);
}
uint32_t
swdptap_seq_in(int ticks)
{
uint32_t index = 1;
uint32_t ret = 0;
swdptap_turnaround(1);
while(ticks--) {
if(swdptap_bit_in()) ret |= index;
index <<= 1;
}
return ret;
}
uint8_t
swdptap_seq_in_parity(uint32_t *ret, int ticks)
{
uint32_t index = 1;
uint8_t parity = 0;
*ret = 0;
swdptap_turnaround(1);
while(ticks--) {
if(swdptap_bit_in()) {
*ret |= index;
parity ^= 1;
}
index <<= 1;
}
if(swdptap_bit_in()) parity ^= 1;
return parity;
}
void
swdptap_seq_out(uint32_t MS, int ticks)
{
swdptap_turnaround(0);
while(ticks--) {
swdptap_bit_out(MS & 1);
MS >>= 1;
}
}
void
swdptap_seq_out_parity(uint32_t MS, int ticks)
{
uint8_t parity = 0;
swdptap_turnaround(0);
while(ticks--) {
swdptap_bit_out(MS & 1);
parity ^= MS;
MS >>= 1;
}
swdptap_bit_out(parity & 1);
}

79
src/swdptap_generic.c Normal file
View File

@ -0,0 +1,79 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2016 Black Sphere Technologies Ltd.
* Written by Gareth McMullin <gareth@blacksphere.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "general.h"
#include "swdptap.h"
uint32_t __attribute__((weak))
swdptap_seq_in(int ticks)
{
uint32_t index = 1;
uint32_t ret = 0;
while (ticks--) {
if (swdptap_bit_in())
ret |= index;
index <<= 1;
}
return ret;
}
bool __attribute__((weak))
swdptap_seq_in_parity(uint32_t *ret, int ticks)
{
uint32_t index = 1;
uint8_t parity = 0;
*ret = 0;
while (ticks--) {
if (swdptap_bit_in()) {
*ret |= index;
parity ^= 1;
}
index <<= 1;
}
if (swdptap_bit_in())
parity ^= 1;
return parity;
}
void __attribute__((weak))
swdptap_seq_out(uint32_t MS, int ticks)
{
while (ticks--) {
swdptap_bit_out(MS & 1);
MS >>= 1;
}
}
void __attribute__((weak))
swdptap_seq_out_parity(uint32_t MS, int ticks)
{
uint8_t parity = 0;
while (ticks--) {
swdptap_bit_out(MS & 1);
parity ^= MS;
MS >>= 1;
}
swdptap_bit_out(parity & 1);
}