Compare commits

...

4 Commits

11 changed files with 1653 additions and 1 deletions

6
fw/.gitignore vendored
View File

@ -1,3 +1,9 @@
.zen
.vscode
.ccls-cache
.cache
*.bin
*.lst
*.map
*.elf
*.hex

BIN
fw/CH32X035DS0.pdf Normal file

Binary file not shown.

BIN
fw/CH32X035RM.pdf Normal file

Binary file not shown.

View File

@ -1,6 +1,9 @@
#include <ch32fun.h>
int main(void) {
while(true);
unsigned int x = 0;
while(true) {
x = x+1;
}
return 0;
}

5
fw/programmer/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.pio
.zed
.vscode
.cache
compile_commands.json

View File

@ -0,0 +1,14 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:default]
platform = https://github.com/maxgerhardt/platform-raspberrypi
framework = arduino
board = rpipico

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,219 @@
#include <hardware/gpio.h>
#include <pico/time.h>
#include "bitbang_rvswdio.h"
#include "hardware/timer.h"
#include "lwipopts.h"
#include "pico.h"
#define PIN_SWDIO 10
#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
#define SWD_DELAY() busy_wait_us(1);
// microseconds between each register read/write
#define STOP_WAIT 8
// Single wire debug (SWDIO and SWCLK)
static inline void ConfigureIOForRVSWD(void)
{
// SWDIO, open drain (emulated) with pull-up
gpio_init(PIN_SWDIO);
gpio_set_pulls(PIN_SWDIO, true, false);
gpio_put(PIN_SWDIO, 0);
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)
static inline void ConfigureIOForRVSWIO(void)
{
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)
{
gpio_put(PIN_SWCLK, 0);
OD_PULL(PIN_SWDIO, value);
SWD_DELAY();
gpio_put(PIN_SWCLK, 1); // Data is sampled on rising edge of clock
SWD_DELAY();
}
bool rvswd_read_bit(void)
{
OD_PULL(PIN_SWDIO, 0);
gpio_put(PIN_SWCLK, 0);
OD_PULL(PIN_SWDIO, 1);
SWD_DELAY();
bool bit = gpio_get(PIN_SWDIO);
gpio_put(PIN_SWCLK, 1); // Data is output on rising edge of clock
SWD_DELAY();
return bit;
}
static void MCFWriteReg32( struct SWIOState * state, uint8_t command, uint32_t value )
{
// only supported mode is SWD
if (state->opmode != 2) {
BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" );
return;
}
noInterrupts();
rvswd_start();
// ADDR HOST
bool parity = false; // This time it's odd parity?
for (uint8_t position = 0; position < 7; position++) {
bool bit = (command >> (6 - position)) & 1;
rvswd_write_bit(bit);
if (bit) parity = !parity;
}
// Operation: write
rvswd_write_bit(1);
parity = !parity;
// Parity bit (even)
rvswd_write_bit(parity);
rvswd_read_bit(); // ???
rvswd_read_bit(); // Seems only need to be set for first transaction (We are ignoring that though)
rvswd_read_bit(); // ???
rvswd_write_bit(0); // 0 for register, 1 for value.
rvswd_write_bit(0); // ??? Seems to have something to do with halting.
// Data
parity = false; // This time it's even parity?
for (uint8_t position = 0; position < 32; position++) {
bool bit = (value >> (31 - position)) & 1;
rvswd_write_bit(bit);
if (bit) parity = !parity;
}
// Parity bit
rvswd_write_bit(parity);
rvswd_read_bit(); // ???
rvswd_read_bit(); // ???
rvswd_read_bit(); // ???
rvswd_write_bit(1); // 0 for register, 1 for value
rvswd_write_bit(0); // ??? Seems to have something to do with halting?
rvswd_stop();
interrupts();
sleep_us(STOP_WAIT);
}
static int MCFReadReg32( struct SWIOState * state, uint8_t command, uint32_t * value )
{
// only supported mode is SWD
if (state->opmode != 2) {
BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" );
return -1;
}
bool parity;
noInterrupts();
rvswd_start();
// ADDR HOST
parity = false;
for (uint8_t position = 0; position < 7; position++) {
bool bit = (command >> (6 - position)) & 1;
rvswd_write_bit(bit);
if (bit) parity = !parity;
}
// Operation: read
rvswd_write_bit(0);
// Parity bit (even)
rvswd_write_bit(parity);
rvswd_read_bit(); // ??
rvswd_read_bit(); // ??
rvswd_read_bit(); // ??
rvswd_write_bit(0); // 0 for register, 1 for value
rvswd_write_bit(0); // ??? Seems to have something to do with halting?
*value = 0;
// Data
parity = false;
uint32_t rval = 0;
for (uint8_t position = 0; position < 32; position++) {
bool bit = rvswd_read_bit();
rval <<= 1;
if (bit) {
rval |= 1;
parity ^= 1;
}
}
*value = rval;
// Parity bit
bool parity_read = rvswd_read_bit();
rvswd_read_bit(); // ??
rvswd_read_bit(); // ??
rvswd_read_bit(); // ??
rvswd_write_bit(1); // 1 for data
rvswd_write_bit(0); // ??? Seems to have something to do with halting?
rvswd_stop();
interrupts();
sleep_us(STOP_WAIT);
return (parity == parity_read) ? 0 : -1;
}

View File

@ -0,0 +1,90 @@
#include "pico/time.h"
#include <Arduino.h>
#include <SerialUSB.h>
//#define BB_PRINTF_DEBUG(...) Serial.printf(__VA_ARGS__)
#define BB_PRINTF_DEBUG(...)
#include "bitbang_rvswdio.h"
#include "bitbang_rvswdio_pico.h"
#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'
bool last_dtr = false;
SWIOState state = {.opmode = 2};
void setup() {
Serial.begin(115200);
Serial.setTimeout(100);
ConfigureIOForRVSWD();
// We do NOT block waiting for Serial here,
// we handle the connection dynamically in the loop.
}
void setup1()
{
pinMode(LED_BUILTIN, OUTPUT);
}
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) {
char cmd = Serial.read();
uint8_t reg;
uint32_t val;
switch (cmd) {
case PROTOCOL_TEST:
Serial.write(PROTOCOL_ACK);
break;
case PROTOCOL_POWER_ON:
// Not needed for rvswd
sleep_us(10);
Serial.write(PROTOCOL_ACK);
break;
case PROTOCOL_POWER_OFF:
// Not needed for rvswd
sleep_us(10);
Serial.write(PROTOCOL_ACK);
break;
case PROTOCOL_WRITE_REG:
if (Serial.readBytes((char*)&reg, 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*)&reg, sizeof(uint8_t)) != 1) break;
MCFReadReg32(&state, reg, &val);
Serial.write((char*)&val, sizeof(uint32_t));
break;
}
}
}
void loop1()
{
digitalWrite(LED_BUILTIN, 1);
delay(200);
digitalWrite(LED_BUILTIN, 0);
delay(200);
}

199
fw/programmer/test/rvswd.c Normal file
View File

@ -0,0 +1,199 @@
#include "rvswd.h"
#include <inttypes.h>
#include <stdint.h>
#include <hardware/gpio.h>
#include <pico/time.h>
// adapted from https://github.com/Nicolai-Electronics/esp32-component-rvswd
rvswd_result_t rvswd_init(rvswd_handle_t* handle) {
//gpio_config_t swio_cfg = {
// .pin_bit_mask = BIT64(handle->swdio),
// .mode = GPIO_MODE_INPUT_OUTPUT_OD,
// .pull_up_en = true,
// .pull_down_en = false,
// .intr_type = GPIO_INTR_DISABLE,
//};
gpio_init(handle->swdio);
gpio_set_pulls(handle->swdio, true, false);
gpio_put(handle->swdio, 0);
gpio_set_dir(handle->swdio, GPIO_IN);
//gpio_config_t swck_cfg = {
// .pin_bit_mask = BIT64(handle->swclk),
// .mode = GPIO_MODE_OUTPUT,
// .pull_up_en = false,
// .pull_down_en = false,
// .intr_type = GPIO_INTR_DISABLE,
//};
gpio_init(handle->swclk);
gpio_set_pulls(handle->swclk, false, false);
gpio_put(handle->swclk, 0);
gpio_set_dir(handle->swclk, GPIO_OUT);
return RVSWD_OK;
}
rvswd_result_t rvswd_start(rvswd_handle_t* handle) {
// Start with both lines high
// open drain emulation, input for high (pulled up) and out for low (forced)
gpio_set_dir(handle->swdio, GPIO_IN); // high
gpio_put(handle->swclk, 1);
sleep_us(2);
// Pull data low
gpio_set_dir(handle->swdio, GPIO_OUT); // low
gpio_put(handle->swclk, 1);
sleep_us(1);
// Pull clock low
gpio_set_dir(handle->swdio, GPIO_OUT); // low
gpio_put(handle->swclk, 0);
sleep_us(1);
return RVSWD_OK;
}
rvswd_result_t rvswd_stop(rvswd_handle_t* handle) {
// Pull data low
gpio_set_dir(handle->swdio, GPIO_OUT);
sleep_us(1);
gpio_put(handle->swclk, 1);
sleep_us(2);
// Let data float high
gpio_set_dir(handle->swdio, GPIO_IN);
sleep_us(1);
return RVSWD_OK;
}
rvswd_result_t rvswd_reset(rvswd_handle_t* handle) {
// set data floating high
gpio_set_dir(handle->swdio, GPIO_IN);
sleep_us(1);
// let clock go brrr
for (uint8_t i = 0; i < 100; i++) {
gpio_put(handle->swclk, 0);
sleep_us(1);
gpio_put(handle->swclk, 1);
sleep_us(1);
}
return rvswd_stop(handle);
}
#define OD_PULL(pin, value) gpio_set_dir((pin), (value) ? GPIO_IN : GPIO_OUT)
void rvswd_write_bit(rvswd_handle_t* handle, bool value) {
OD_PULL(handle->swdio, value);
gpio_put(handle->swclk, 0);
// FIXME: does this need a delay?
gpio_put(handle->swclk, 1); // Data is sampled on rising edge of clock
}
bool rvswd_read_bit(rvswd_handle_t* handle) {
// let data float high
gpio_set_dir(handle->swdio, GPIO_IN);
// pulse clock
gpio_put(handle->swclk, 0);
gpio_put(handle->swclk, 1); // Data is output on rising edge of clock
// FIXME: does this need a delay?
return gpio_get(handle->swdio);
}
rvswd_result_t rvswd_write(rvswd_handle_t* handle, uint8_t reg, uint32_t value) {
rvswd_start(handle);
// ADDR HOST
bool parity = false; // This time it's odd parity?
for (uint8_t position = 0; position < 7; position++) {
bool bit = (reg >> (6 - position)) & 1;
rvswd_write_bit(handle, bit);
if (bit) parity = !parity;
}
// Operation: write
rvswd_write_bit(handle, true);
parity = !parity;
// Parity bit (even)
rvswd_write_bit(handle, parity);
rvswd_write_bit(handle, 1);
rvswd_write_bit(handle, 0);
rvswd_write_bit(handle, 1);
rvswd_write_bit(handle, 0);
rvswd_write_bit(handle, 1);
// Data
parity = false; // This time it's even parity?
for (uint8_t position = 0; position < 32; position++) {
bool bit = (value >> (31 - position)) & 1;
rvswd_write_bit(handle, bit);
if (bit) parity = !parity;
}
// Parity bit
rvswd_write_bit(handle, parity);
rvswd_write_bit(handle, 1);
rvswd_write_bit(handle, 0);
rvswd_write_bit(handle, 1);
rvswd_write_bit(handle, 1);
rvswd_write_bit(handle, 1);
rvswd_stop(handle);
return RVSWD_OK;
}
rvswd_result_t rvswd_read(rvswd_handle_t* handle, uint8_t reg, uint32_t* value) {
bool parity;
rvswd_start(handle);
// ADDR HOST
parity = false;
for (uint8_t position = 0; position < 7; position++) {
bool bit = (reg >> (6 - position)) & 1;
rvswd_write_bit(handle, bit);
if (bit) parity = !parity;
}
// Operation: read
rvswd_write_bit(handle, false);
// Parity bit (even)
rvswd_write_bit(handle, parity);
rvswd_write_bit(handle, 1);
rvswd_write_bit(handle, 0);
rvswd_write_bit(handle, 1);
rvswd_write_bit(handle, 0);
rvswd_write_bit(handle, 1);
*value = 0;
// Data
parity = false;
for (uint8_t position = 0; position < 32; position++) {
bool bit = rvswd_read_bit(handle);
if (bit) {
*value |= 1 << (31 - position);
}
if (bit) parity = !parity;
}
// Parity bit
bool parity_read = rvswd_read_bit(handle);
rvswd_write_bit(handle, 1);
rvswd_write_bit(handle, 0);
rvswd_write_bit(handle, 1);
rvswd_write_bit(handle, 1);
rvswd_write_bit(handle, 1);
rvswd_stop(handle);
return (parity == parity_read) ? RVSWD_OK : RVSWD_FAIL;
}

View File

@ -0,0 +1,20 @@
#pragma once
#include <stdint.h>
typedef struct rvswd_handle {
int swdio;
int swclk;
} rvswd_handle_t;
typedef enum rvswd_result {
RVSWD_OK = 0,
RVSWD_FAIL = 1,
RVSWD_INVALID_ARGS = 2,
RVSWD_PARITY_ERROR = 3,
} rvswd_result_t;
rvswd_result_t rvswd_init(rvswd_handle_t* handle);
rvswd_result_t rvswd_reset(rvswd_handle_t* handle);
rvswd_result_t rvswd_write(rvswd_handle_t* handle, uint8_t reg, uint32_t value);
rvswd_result_t rvswd_read(rvswd_handle_t* handle, uint8_t reg, uint32_t* value);