Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
renzenicolai committed Dec 27, 2024
0 parents commit 355c8d2
Show file tree
Hide file tree
Showing 11 changed files with 838 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
BasedOnStyle: Google
CommentPragmas: '(@copydoc)'

# Pointers
DerivePointerAlignment: false
PointerAlignment: Left

# Indents
IndentWidth: 4
TabWidth: 4
UseTab: "Never"
NamespaceIndentation: All
IndentGotoLabels: true
AlignConsecutiveMacros: true

# Line breaks
AlwaysBreakTemplateDeclarations: No
AllowShortFunctionsOnASingleLine: None
AllowShortEnumsOnASingleLine: false
KeepEmptyLinesAtTheStartOfBlocks: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
ColumnLimit: 120

# Includes
IncludeBlocks: Merge
21 changes: 21 additions & 0 deletions .github/workflows/upload_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Push components to Espressif Component Service

on:
push:
tags:
- v*

jobs:
upload_components:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Upload components to component service
uses: espressif/upload-components-ci-action@v1
with:
name: "rvswd"
version: ${{ github.ref_name }}
namespace: "nicolaielectronics"
api_token: ${{ secrets.IDF_COMPONENT_API_TOKEN }}
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
idf_component_register(
SRCS
"src/rvswd.c"
"src/rvswd_ch32v203.c"
INCLUDE_DIRS
"include"
REQUIRES
"driver"
)
11 changes: 11 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
MIT License (Extended)

Copyright (c) 2025 Nicolai Electronics

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is provided to do so, subject to the following conditions:

1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

2. Notwithstanding any other provision of this License, the Software shall not be used, copied, modified, distributed, or otherwise interacted with by the organization known as Stichting International Festivals for Creative Application of Technology Foundation (IFCAT), nor by any individuals or entities associated with Stichting International Festivals for Creative Application of Technology Foundation (IFCAT), whether directly or indirectly. This prohibition includes but is not limited to employees, affiliates, contractors, and any entities that are controlled or influenced by Stichting International Festivals for Creative Application of Technology Foundation (IFCAT).

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# General targets

.PHONY: all
all: format

# Formatting

.PHONY: format
format:
find . -iname '*.h' -o -iname '*.c' -o -iname '*.cpp' | xargs clang-format -i
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# RVSWD programmer component

This component enables an ESP32 host to program an RVSWD compatible target. RVSWD is a custom two wire programming protocol used by WCH in their CH32 line of microcontrollers.

Currently the component only supports and has only been tested on the **CH32V203** microcontroller.

## License

This project will be made available under unmodified MIT license starting September of 2025. Until then the component is made available under a [modified, extended variant of the MIT license](LICENSE) which explicitly excludes a specific organization from using this component.

7 changes: 7 additions & 0 deletions idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies:
cmake_utilities: 0.*
idf: '>=5.3'
description: RVSWD programmer
repository: git://github.com/Nicolai-Electronics/esp32-component-rvswd.git
issues: https://github.com/Nicolai-Electronics/esp32-component-rvswd/issues
url: https://github.com/Nicolai-Electronics/esp32-component-rvswd
29 changes: 29 additions & 0 deletions include/rvswd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

/**
* Copyright (c) 2025 Nicolai Electronics
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include <stdint.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"

typedef struct rvswd_handle {
gpio_num_t swdio;
gpio_num_t 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);
16 changes: 16 additions & 0 deletions include/rvswd_ch32v203.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

/**
* Copyright (c) 2025 Nicolai Electronics
*
* SPDX-License-Identifier: MIT
*/

#pragma once

#include "rvswd.h"

// Optional user-defined status update callback.
void ch32_status_callback(char const* msg, int progress, int total);

// Program and restart the CH32V203.
bool ch32_program(rvswd_handle_t* handle, void const* firmware, size_t firmware_len);
190 changes: 190 additions & 0 deletions src/rvswd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/**
* Copyright (c) 2025 Nicolai Electronics
*
* SPDX-License-Identifier: MIT
*/

#include "rvswd.h"
#include <inttypes.h>
#include <stdint.h>
#include "rom/ets_sys.h"

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,
};
esp_err_t res = gpio_config(&swio_cfg);
if (res != ESP_OK) {
return RVSWD_FAIL;
}

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,
};
res = gpio_config(&swck_cfg);
if (res != ESP_OK) {
return RVSWD_FAIL;
}

return RVSWD_OK;
}

rvswd_result_t rvswd_start(rvswd_handle_t* handle) {
// Start with both lines high
gpio_set_level(handle->swdio, true);
gpio_set_level(handle->swclk, true);
ets_delay_us(2);

// Pull data low
gpio_set_level(handle->swdio, false);
gpio_set_level(handle->swclk, true);
ets_delay_us(1);

// Pull clock low
gpio_set_level(handle->swdio, false);
gpio_set_level(handle->swclk, false);
ets_delay_us(1);
return RVSWD_OK;
}

rvswd_result_t rvswd_stop(rvswd_handle_t* handle) {
// Pull data low
gpio_set_level(handle->swdio, false);
ets_delay_us(1);
gpio_set_level(handle->swclk, true);
ets_delay_us(2);
// Let data float high
gpio_set_level(handle->swdio, true);
ets_delay_us(1);
return RVSWD_OK;
}

rvswd_result_t rvswd_reset(rvswd_handle_t* handle) {
gpio_set_level(handle->swdio, true);
ets_delay_us(1);
for (uint8_t i = 0; i < 100; i++) {
gpio_set_level(handle->swclk, false);
ets_delay_us(1);
gpio_set_level(handle->swclk, true);
ets_delay_us(1);
}
return rvswd_stop(handle);
}

void rvswd_write_bit(rvswd_handle_t* handle, bool value) {
gpio_set_level(handle->swdio, value);
gpio_set_level(handle->swclk, false);
gpio_set_level(handle->swclk, true); // Data is sampled on rising edge of clock
}

bool rvswd_read_bit(rvswd_handle_t* handle) {
gpio_set_level(handle->swdio, true);
gpio_set_level(handle->swclk, false);
gpio_set_level(handle->swclk, true); // Data is output on rising edge of clock
return gpio_get_level(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;
}
Loading

0 comments on commit 355c8d2

Please sign in to comment.