abse/smart-ring-flex
A tscircuit package created by abse.
- Version
- 0.0.24
- License
- unset
- Stars
- 0
firmware/onboard-programmer/ring-service-candidate/src/dap_io.h
// ===================================================================================
// CMSIS-DAP CONFIG I/O
// ===================================================================================
#pragma once
#include "ch554.h"
#include "gpio.h"
#include "config.h"
#include "delay.h"
extern uint16_t ring_swd_half_period_us;
void ring_probe_enable(void) __reentrant;
void ring_probe_reset(uint8_t value) __reentrant;
// SWD I/O pin manipulations
#define SWK_SET(val) PIN_write(PIN_SWK, val)
#define SWD_SET(val) PIN_write(PIN_SWD, val)
#define RST_SET(val) ring_probe_reset((uint8_t)(val))
#define LED_SET(val) ((void)0) // P14 is NOT a status LED
#define SWK_GET() PIN_read(PIN_SWK)
#define SWD_GET() PIN_read(PIN_SWD)
#define RST_GET() (!PIN_read(PIN_RST)) // commanded state; no physical feedback
// Initial SWD port setup (at firmware start)
#define PORT_SWD_SETUP() do { PIN_low(PIN_XLAT_EN); PIN_output(PIN_XLAT_EN); PIN_low(PIN_RST); PIN_output(PIN_RST); PIN_input(PIN_SWD); PIN_input(PIN_SWK); } while(0)
// Connect SWD port (setup port for data transmission)
#define PORT_SWD_CONNECT() do { PIN_low(PIN_XLAT_EN); RST_SET(1); SWD_SET(1); PIN_output_OD(PIN_SWD); SWK_SET(0); PIN_output(PIN_SWK); ring_probe_enable(); } while(0)
// Release translator before GPIO changes; target is independently powered.
#define PORT_SWD_DISCONNECT() do { PIN_low(PIN_XLAT_EN); RST_SET(1); PIN_input(PIN_SWD); PIN_input(PIN_SWK); } while(0)
// SWCLK clockout cycle
#define SWD_CLOCK_CYCLE() { \
DLY_us(ring_swd_half_period_us); \
SWK_SET(1); \
DLY_us(ring_swd_half_period_us); \
SWK_SET(0); \
}
// Write one bit via SWD
#define SWD_WRITE_BIT(bits) { \
SWD_SET((bits)&1); \
SWD_CLOCK_CYCLE(); \
}
// Read one bit via SWD
#define SWD_READ_BIT(bits) { \
bits = SWD_GET(); \
SWD_CLOCK_CYCLE(); \
}
// Set SWDIO pin to output
#define SWD_OUT_ENABLE() PIN_output_OD(PIN_SWD)
// Set SWDIO pin to input
#define SWD_OUT_DISABLE() PIN_input(PIN_SWD)
// HID transfer buffers
#define DAP_READ_BUF_PTR EP1_buffer
#define DAP_WRITE_BUF_PTR EP1_buffer + 64
extern __xdata uint8_t EP1_buffer[];
// DAP init function
#define DAP_init() {USB_init(); PORT_SWD_SETUP();}
extern void USB_init(void);