So I'm trying to read a bit of data via SWD. I've tried two chips as target - LPC1110 and STM32F030F4 (the first of them I used for other schematic, but I've reset the flash to make sure the pins are not configured improperly).
I use Arduino as a host. I wire D3 to SWCLK and D4 to SWDIO (with pull-up to 3.3V line). I fail miserably with both chips - i.e. when I expect some ACK be read - it only shows HIGH state on the line all the time.
Here is my code. Could anyone have a look and tell where I went wrong:
int swdck = 3;
int swdio = 4;
void setup() {
pinMode(swdck, OUTPUT);
digitalWrite(swdck, HIGH);
pinMode(swdio, INPUT);
digitalWrite(swdio, HIGH);
Serial.begin(9600);
}
// write some bits from X (least significant first) to SWD
void clockOut(int x, int bits) {
pinMode(swdio, OUTPUT);
while (bits > 0) {
digitalWrite(swdck, LOW);
digitalWrite(swdio, (x & 1) ? HIGH : LOW);
delay(1);
digitalWrite(swdck, HIGH);
delay(1);
bits--;
x >>= 1;
}
pinMode(swdio, INPUT);
digitalWrite(swdio, HIGH);
}
// read some bits from SWD, least significant first
int clockIn(int bits) {
pinMode(swdio, INPUT);
digitalWrite(swdio, HIGH);
int i, res;
res = 0;
for (i = 0; i < bits; i++) {
digitalWrite(swdck, LOW);
delay(1);
if (digitalRead(swdio) == HIGH) {
res += (1 << i);
}
digitalWrite(swdck, HIGH);
delay(1);
}
return res;
}
void loop() {
int x;
// unlock sequence though it probably is not needed
clockIn(26); clockIn(26); // more than 50 clocks
clockOut(0xE79E, 16); // command to switch to SWD
clockIn(26); clockIn(26);
clockOut(0x25, 7); // 0b0100101 - request to read Debug-port-0, i.e. ID
x = clockIn(5); // there should be ACK bits somewhere... but I get 0x1111
clockIn(26); clockIn(26); // perform more readings for case anything is returned
Serial.println(x);
delay(1000);
}