Compare commits
No commits in common. "6bc40874746aee19916dd4cac36daa7900f595c9" and "5a6488535d543dff02b42a3e8a52c69eeabac800" have entirely different histories.
6bc4087474
...
5a6488535d
1
fw/.gitignore
vendored
1
fw/.gitignore
vendored
@ -7,4 +7,3 @@
|
|||||||
*.map
|
*.map
|
||||||
*.elf
|
*.elf
|
||||||
*.hex
|
*.hex
|
||||||
compile_commands.json
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
all : build
|
all : flash
|
||||||
|
|
||||||
TARGET:=main
|
TARGET:=blink
|
||||||
TARGET_MCU:=CH32X035
|
TARGET_MCU:=CH32X035
|
||||||
|
|
||||||
include ch32fun/ch32fun/ch32fun.mk
|
include ch32fun/ch32fun/ch32fun.mk
|
||||||
|
|||||||
9
fw/blink.c
Normal file
9
fw/blink.c
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include <ch32fun.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
unsigned int x = 0;
|
||||||
|
while(true) {
|
||||||
|
x = x+1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@ -1,7 +0,0 @@
|
|||||||
#include <ch32fun.h>
|
|
||||||
|
|
||||||
__attribute__((noreturn)) int main(void)
|
|
||||||
{
|
|
||||||
while (1) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -188,7 +188,7 @@ static int InitializeSWDSWIO( struct SWIOState * state )
|
|||||||
if( MCFReadReg32( state, DMSTATUS, &dmstatus ) != 0 ||
|
if( MCFReadReg32( state, DMSTATUS, &dmstatus ) != 0 ||
|
||||||
MCFReadReg32( state, DMCONTROL, &dmcontrol ) != 0 )
|
MCFReadReg32( state, DMCONTROL, &dmcontrol ) != 0 )
|
||||||
{
|
{
|
||||||
BB_PRINTF_DEBUG( "Could not read from RVSWD connection\n" );
|
//BB_PRINTF_DEBUG( "Could not read from RVSWD connection\n" );
|
||||||
state->opmode = 0;
|
state->opmode = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -198,7 +198,7 @@ static int InitializeSWDSWIO( struct SWIOState * state )
|
|||||||
( ( dmstatus >> 8 ) & 0xf ) != 0x03 ) ||
|
( ( dmstatus >> 8 ) & 0xf ) != 0x03 ) ||
|
||||||
dmcontrol != 1 )
|
dmcontrol != 1 )
|
||||||
{
|
{
|
||||||
BB_PRINTF_DEBUG( "DMSTATUS invalid (Probably no RVSWD chip)\n" );
|
//BB_PRINTF_DEBUG( "DMSTATUS invalid (Probably no RVSWD chip)\n" );
|
||||||
state->opmode = 0;
|
state->opmode = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -207,7 +207,7 @@ static int InitializeSWDSWIO( struct SWIOState * state )
|
|||||||
BB_PRINTF_DEBUG( "Found RVSWD interface\n" );
|
BB_PRINTF_DEBUG( "Found RVSWD interface\n" );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
BB_PRINTF_DEBUG( "TIMEOUT\n" );
|
//printf( "TIMEOUT\n" );
|
||||||
return -55;
|
return -55;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -11,8 +11,13 @@
|
|||||||
#define PIN_SWCLK 11
|
#define PIN_SWCLK 11
|
||||||
|
|
||||||
|
|
||||||
|
// open drain emulation, the pin is set with output '0' and is switched between input or output
|
||||||
|
// depending on the wanted value, in the high state the line is pulled high by the pull-up and
|
||||||
|
// in the low state the line in forced low
|
||||||
|
#define OD_PULL(pin, value) gpio_set_dir((pin), (value) ? GPIO_IN : GPIO_OUT)
|
||||||
|
|
||||||
// wait time between line transitions
|
// wait time between line transitions
|
||||||
#define SWD_DELAY() busy_wait_us(2);
|
#define SWD_DELAY() busy_wait_us(1);
|
||||||
|
|
||||||
// microseconds between each register read/write
|
// microseconds between each register read/write
|
||||||
#define STOP_WAIT 8
|
#define STOP_WAIT 8
|
||||||
@ -20,33 +25,58 @@
|
|||||||
// Single wire debug (SWDIO and SWCLK)
|
// Single wire debug (SWDIO and SWCLK)
|
||||||
static inline void ConfigureIOForRVSWD(void)
|
static inline void ConfigureIOForRVSWD(void)
|
||||||
{
|
{
|
||||||
// SWCLK forced, starts at 1
|
|
||||||
gpio_init(PIN_SWCLK);
|
|
||||||
gpio_set_pulls(PIN_SWCLK, false, false);
|
|
||||||
gpio_put(PIN_SWCLK, 1);
|
|
||||||
gpio_set_dir(PIN_SWCLK, GPIO_OUT);
|
|
||||||
|
|
||||||
// SWDIO, open drain (emulated) with pull-up
|
// SWDIO, open drain (emulated) with pull-up
|
||||||
gpio_init(PIN_SWDIO);
|
gpio_init(PIN_SWDIO);
|
||||||
gpio_set_pulls(PIN_SWDIO, true, false);
|
gpio_set_pulls(PIN_SWDIO, true, false);
|
||||||
gpio_put(PIN_SWDIO, 1);
|
gpio_put(PIN_SWDIO, 0);
|
||||||
gpio_set_dir(PIN_SWDIO, GPIO_IN);
|
gpio_set_dir(PIN_SWDIO, GPIO_IN);
|
||||||
|
|
||||||
|
gpio_init(PIN_SWCLK);
|
||||||
|
gpio_set_pulls(PIN_SWCLK, false, false);
|
||||||
|
gpio_put(PIN_SWCLK, 0);
|
||||||
|
gpio_set_dir(PIN_SWCLK, GPIO_OUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Single wire input-output SDI (just SWDIO)
|
// Single wire input-output SDI (just SWDIO)
|
||||||
static inline void ConfigureIOForRVSWIO(void)
|
static inline void ConfigureIOForRVSWIO(void)
|
||||||
{
|
{
|
||||||
//BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" );
|
BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline void rvswd_start(void)
|
||||||
|
{
|
||||||
|
// Start with both lines high
|
||||||
|
gpio_put(PIN_SWCLK, 1);
|
||||||
|
OD_PULL(PIN_SWDIO, 1);
|
||||||
|
//SWD_DELAY();
|
||||||
|
|
||||||
|
// Pull data low
|
||||||
|
OD_PULL(PIN_SWDIO, 0);
|
||||||
|
SWD_DELAY();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline void rvswd_stop(void)
|
||||||
|
{
|
||||||
|
gpio_put(PIN_SWCLK, 0);
|
||||||
|
SWD_DELAY();
|
||||||
|
|
||||||
|
OD_PULL(PIN_SWDIO, 0);
|
||||||
|
SWD_DELAY();
|
||||||
|
|
||||||
|
gpio_put(PIN_SWCLK, 1);
|
||||||
|
SWD_DELAY();
|
||||||
|
|
||||||
|
OD_PULL(PIN_SWDIO, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void rvswd_write_bit(bool value)
|
void rvswd_write_bit(bool value)
|
||||||
{
|
{
|
||||||
gpio_put(PIN_SWCLK, 0);
|
gpio_put(PIN_SWCLK, 0);
|
||||||
gpio_put(PIN_SWDIO, value);
|
OD_PULL(PIN_SWDIO, value);
|
||||||
gpio_set_dir(PIN_SWDIO, GPIO_OUT);
|
|
||||||
gpio_set_dir(PIN_SWDIO, GPIO_IN);
|
|
||||||
SWD_DELAY();
|
SWD_DELAY();
|
||||||
gpio_put(PIN_SWCLK, 1); // Data is sampled on rising edge of clock
|
gpio_put(PIN_SWCLK, 1); // Data is sampled on rising edge of clock
|
||||||
SWD_DELAY();
|
SWD_DELAY();
|
||||||
@ -55,68 +85,56 @@ void rvswd_write_bit(bool value)
|
|||||||
|
|
||||||
bool rvswd_read_bit(void)
|
bool rvswd_read_bit(void)
|
||||||
{
|
{
|
||||||
gpio_put(PIN_SWDIO, 1);
|
OD_PULL(PIN_SWDIO, 0);
|
||||||
gpio_set_dir(PIN_SWDIO, GPIO_IN);
|
|
||||||
gpio_put(PIN_SWCLK, 0);
|
gpio_put(PIN_SWCLK, 0);
|
||||||
|
OD_PULL(PIN_SWDIO, 1);
|
||||||
SWD_DELAY();
|
SWD_DELAY();
|
||||||
bool bit = gpio_get(PIN_SWDIO);
|
bool bit = gpio_get(PIN_SWDIO);
|
||||||
|
|
||||||
gpio_put(PIN_SWCLK, 1); // Data is output on rising edge of clock
|
gpio_put(PIN_SWCLK, 1); // Data is output on rising edge of clock
|
||||||
SWD_DELAY();
|
SWD_DELAY();
|
||||||
return bit;
|
return bit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static inline void rvswd_stop(void)
|
|
||||||
{
|
|
||||||
gpio_put(PIN_SWCLK, 0);
|
|
||||||
SWD_DELAY();
|
|
||||||
gpio_put(PIN_SWDIO, 0);
|
|
||||||
gpio_set_dir(PIN_SWDIO, GPIO_OUT);
|
|
||||||
SWD_DELAY();
|
|
||||||
gpio_put(PIN_SWCLK, 1);
|
|
||||||
SWD_DELAY();
|
|
||||||
gpio_put(PIN_SWDIO, 1);
|
|
||||||
gpio_set_dir(PIN_SWDIO, GPIO_IN);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void MCFWriteReg32( struct SWIOState * state, uint8_t command, uint32_t value )
|
static void MCFWriteReg32( struct SWIOState * state, uint8_t command, uint32_t value )
|
||||||
{
|
{
|
||||||
// only supported mode is SWD
|
// only supported mode is SWD
|
||||||
if (state->opmode != 2) {
|
if (state->opmode != 2) {
|
||||||
//BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" );
|
BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
noInterrupts();
|
noInterrupts();
|
||||||
|
rvswd_start();
|
||||||
// start transaction
|
|
||||||
gpio_put(PIN_SWDIO, 0);
|
|
||||||
gpio_set_dir(PIN_SWDIO, GPIO_OUT);
|
|
||||||
SWD_DELAY();
|
|
||||||
|
|
||||||
// ADDR HOST
|
// ADDR HOST
|
||||||
bool parity = true;
|
bool parity = false; // This time it's odd parity?
|
||||||
for (uint32_t mask = 1<<6; mask; mask >>= 1) {
|
for (uint8_t position = 0; position < 7; position++) {
|
||||||
bool bit = !!(command & mask);
|
bool bit = (command >> (6 - position)) & 1;
|
||||||
parity ^= bit;
|
|
||||||
rvswd_write_bit(bit);
|
rvswd_write_bit(bit);
|
||||||
|
if (bit) parity = !parity;
|
||||||
}
|
}
|
||||||
|
|
||||||
rvswd_write_bit(1); // Operation: write
|
// Operation: write
|
||||||
|
rvswd_write_bit(1);
|
||||||
|
parity = !parity;
|
||||||
|
|
||||||
|
// Parity bit (even)
|
||||||
rvswd_write_bit(parity);
|
rvswd_write_bit(parity);
|
||||||
|
|
||||||
rvswd_read_bit(); // ???
|
rvswd_read_bit(); // ???
|
||||||
rvswd_read_bit(); // Seems only need to be set for first transaction (We are ignoring that though)
|
rvswd_read_bit(); // Seems only need to be set for first transaction (We are ignoring that though)
|
||||||
rvswd_read_bit(); // ???
|
rvswd_read_bit(); // ???
|
||||||
rvswd_write_bit(0); // 0 for register, 1 for value.
|
rvswd_write_bit(0); // 0 for register, 1 for value.
|
||||||
rvswd_write_bit(0); // ??? Seems to have something to do with halting.
|
rvswd_write_bit(0); // ??? Seems to have something to do with halting.
|
||||||
|
|
||||||
// DATA
|
// Data
|
||||||
parity = false; // This time it's even parity?
|
parity = false; // This time it's even parity?
|
||||||
for (uint32_t mask = 1<<31; mask; mask >>= 1) {
|
for (uint8_t position = 0; position < 32; position++) {
|
||||||
bool bit = !!(value & mask);
|
bool bit = (value >> (31 - position)) & 1;
|
||||||
parity ^= bit;
|
|
||||||
rvswd_write_bit(bit);
|
rvswd_write_bit(bit);
|
||||||
|
if (bit) parity = !parity;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parity bit
|
// Parity bit
|
||||||
@ -138,60 +156,64 @@ static int MCFReadReg32( struct SWIOState * state, uint8_t command, uint32_t * v
|
|||||||
{
|
{
|
||||||
// only supported mode is SWD
|
// only supported mode is SWD
|
||||||
if (state->opmode != 2) {
|
if (state->opmode != 2) {
|
||||||
//BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" );
|
BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" );
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
noInterrupts();
|
bool parity;
|
||||||
|
|
||||||
// start transaction
|
noInterrupts();
|
||||||
gpio_put(PIN_SWDIO, 0);
|
rvswd_start();
|
||||||
gpio_set_dir(PIN_SWDIO, GPIO_OUT);
|
|
||||||
SWD_DELAY();
|
|
||||||
|
|
||||||
// ADDR HOST
|
// ADDR HOST
|
||||||
bool parity = false;
|
parity = false;
|
||||||
for (uint8_t mask = 1<<6; mask; mask >>= 1) {
|
for (uint8_t position = 0; position < 7; position++) {
|
||||||
bool bit = !!(command & mask);
|
bool bit = (command >> (6 - position)) & 1;
|
||||||
parity ^= bit;
|
|
||||||
rvswd_write_bit(bit);
|
rvswd_write_bit(bit);
|
||||||
|
if (bit) parity = !parity;
|
||||||
}
|
}
|
||||||
|
|
||||||
rvswd_write_bit(0); // Operation: read
|
// Operation: read
|
||||||
|
rvswd_write_bit(0);
|
||||||
|
|
||||||
|
// Parity bit (even)
|
||||||
rvswd_write_bit(parity);
|
rvswd_write_bit(parity);
|
||||||
|
|
||||||
rvswd_read_bit(); // ??
|
rvswd_read_bit(); // ??
|
||||||
rvswd_read_bit(); // ??
|
rvswd_read_bit(); // ??
|
||||||
rvswd_read_bit(); // ??
|
rvswd_read_bit(); // ??
|
||||||
|
|
||||||
rvswd_write_bit(0); // 0 for register, 1 for value
|
rvswd_write_bit(0); // 0 for register, 1 for value
|
||||||
rvswd_write_bit(0); // ??? Seems to have something to do with halting?
|
rvswd_write_bit(0); // ??? Seems to have something to do with halting?
|
||||||
|
|
||||||
// DATA
|
|
||||||
*value = 0;
|
*value = 0;
|
||||||
|
|
||||||
|
// Data
|
||||||
parity = false;
|
parity = false;
|
||||||
uint32_t rval = 0;
|
uint32_t rval = 0;
|
||||||
for (uint8_t position = 0; position < 32; position++) {
|
for (uint8_t position = 0; position < 32; position++) {
|
||||||
bool bit = rvswd_read_bit();
|
bool bit = rvswd_read_bit();
|
||||||
rval <<= 1;
|
rval <<= 1;
|
||||||
rval |= bit;
|
if (bit) {
|
||||||
parity ^= bit;
|
rval |= 1;
|
||||||
|
parity ^= 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
*value = rval;
|
*value = rval;
|
||||||
|
|
||||||
// Parity bit
|
// Parity bit
|
||||||
bool parity_read = rvswd_read_bit();
|
bool parity_read = rvswd_read_bit();
|
||||||
if (parity_read != parity) goto read_end;
|
|
||||||
|
|
||||||
rvswd_read_bit(); // ??
|
rvswd_read_bit(); // ??
|
||||||
rvswd_read_bit(); // ??
|
rvswd_read_bit(); // ??
|
||||||
rvswd_read_bit(); // ??
|
rvswd_read_bit(); // ??
|
||||||
rvswd_write_bit(1); // 0 for register, 1 for value
|
|
||||||
|
rvswd_write_bit(1); // 1 for data
|
||||||
rvswd_write_bit(0); // ??? Seems to have something to do with halting?
|
rvswd_write_bit(0); // ??? Seems to have something to do with halting?
|
||||||
|
|
||||||
rvswd_stop();
|
rvswd_stop();
|
||||||
|
|
||||||
read_end:
|
|
||||||
interrupts();
|
interrupts();
|
||||||
|
|
||||||
sleep_us(STOP_WAIT);
|
sleep_us(STOP_WAIT);
|
||||||
BB_PRINTF_DEBUG("wrong parity: %d\n", parity_read);
|
|
||||||
return (parity == parity_read) ? 0 : -1;
|
return (parity == parity_read) ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,59 +1,90 @@
|
|||||||
#include "pico/time.h"
|
#include "pico/time.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <SerialUSB.h>
|
#include <SerialUSB.h>
|
||||||
|
|
||||||
#define BB_PRINTF_DEBUG(...) Serial.printf(__VA_ARGS__)
|
//#define BB_PRINTF_DEBUG(...) Serial.printf(__VA_ARGS__)
|
||||||
//#define BB_PRINTF_DEBUG(...)
|
#define BB_PRINTF_DEBUG(...)
|
||||||
#include "bitbang_rvswdio.h"
|
#include "bitbang_rvswdio.h"
|
||||||
#include "bitbang_rvswdio_pico.h"
|
#include "bitbang_rvswdio_pico.h"
|
||||||
|
|
||||||
SWIOState state = {.opmode = 2};
|
#define PROTOCOL_START '!'
|
||||||
|
#define PROTOCOL_ACK '+'
|
||||||
|
#define PROTOCOL_TEST '?'
|
||||||
|
#define PROTOCOL_POWER_ON 'p'
|
||||||
|
#define PROTOCOL_POWER_OFF 'P'
|
||||||
|
#define PROTOCOL_WRITE_REG 'w'
|
||||||
|
#define PROTOCOL_READ_REG 'r'
|
||||||
|
|
||||||
void setup() {
|
bool last_dtr = false;
|
||||||
|
SWIOState state = {.opmode = 2};
|
||||||
|
|
||||||
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
Serial.setTimeout(100);
|
Serial.setTimeout(100);
|
||||||
}
|
ConfigureIOForRVSWD();
|
||||||
|
|
||||||
|
// We do NOT block waiting for Serial here,
|
||||||
|
// we handle the connection dynamically in the loop.
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void setup1()
|
void setup1()
|
||||||
{
|
{
|
||||||
pinMode(LED_BUILTIN, OUTPUT);
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
// Monitor DTR line to simulate Arduino Reset behavior
|
||||||
|
bool current_dtr = Serial.dtr();
|
||||||
|
if (current_dtr && !last_dtr) {
|
||||||
|
// minichlink just opened the port
|
||||||
|
ConfigureIOForRVSWD();
|
||||||
|
while (Serial.available() > 0) { Serial.read(); }
|
||||||
|
delay(100);
|
||||||
|
Serial.write(PROTOCOL_START); // Announce readiness
|
||||||
|
}
|
||||||
|
last_dtr = current_dtr;
|
||||||
|
|
||||||
if (Serial.available() > 0) {
|
if (Serial.available() > 0) {
|
||||||
char cmd = Serial.read();
|
char cmd = Serial.read();
|
||||||
int ret = 0;
|
|
||||||
uint8_t reg;
|
uint8_t reg;
|
||||||
|
uint32_t val;
|
||||||
|
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case '!':
|
case PROTOCOL_TEST:
|
||||||
state = {};
|
Serial.write(PROTOCOL_ACK);
|
||||||
ret = InitializeSWDSWIO(&state);
|
|
||||||
if (ret != 0) {
|
|
||||||
Serial.printf("error initializing SWD: %d\n", ret);
|
|
||||||
break;
|
break;
|
||||||
}
|
case PROTOCOL_POWER_ON:
|
||||||
ret = DetermineChipTypeAndSectorInfo(&state, NULL);
|
// Not needed for rvswd
|
||||||
if (ret != 0) {
|
sleep_us(10);
|
||||||
Serial.printf("failed to determine chip type: %d\n", ret);
|
Serial.write(PROTOCOL_ACK);
|
||||||
break;
|
break;
|
||||||
}
|
case PROTOCOL_POWER_OFF:
|
||||||
Serial.printf("chip type is %x\n", state.target_chip_type);
|
// Not needed for rvswd
|
||||||
|
sleep_us(10);
|
||||||
|
Serial.write(PROTOCOL_ACK);
|
||||||
break;
|
break;
|
||||||
default:
|
case PROTOCOL_WRITE_REG:
|
||||||
Serial.printf("unknown command '%c'\n", cmd);
|
if (Serial.readBytes((char*)®, sizeof(uint8_t)) != 1) break;
|
||||||
|
if (Serial.readBytes((char*)&val, sizeof(uint32_t)) != 4) break;
|
||||||
|
MCFWriteReg32(&state, reg, val);
|
||||||
|
Serial.write(PROTOCOL_ACK);
|
||||||
|
break;
|
||||||
|
case PROTOCOL_READ_REG:
|
||||||
|
if (Serial.readBytes((char*)®, sizeof(uint8_t)) != 1) break;
|
||||||
|
MCFReadReg32(&state, reg, &val);
|
||||||
|
Serial.write((char*)&val, sizeof(uint32_t));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop1()
|
void loop1()
|
||||||
{
|
{
|
||||||
digitalWrite(LED_BUILTIN, 1);
|
digitalWrite(LED_BUILTIN, 1);
|
||||||
delay(200);
|
delay(200);
|
||||||
digitalWrite(LED_BUILTIN, 0);
|
digitalWrite(LED_BUILTIN, 0);
|
||||||
delay(200);
|
delay(200);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user