Skip to content
Snippets Groups Projects
pgm-wch-linke.c 23.13 KiB
// Tricky: You need to use wch link to use WCH-LinkRV.
//  you can always uninstall it in device manager.  It will be under USB devices or something like that at the bottom.

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "libusb.h"
#include "minichlink.h"

struct LinkEProgrammerStruct
{
	void * internal;
	libusb_device_handle * devh;
	int lasthaltmode; // For non-003 chips
};

static void printChipInfo(enum RiscVChip chip) {
	switch(chip) {
		case CH32V10x:
			fprintf(stderr, "Detected: CH32V10x\n");
			break;
		case CH57x:
			fprintf(stderr, "Detected: CH57x\n");
			break;
		case CH56x:
			fprintf(stderr, "Detected: CH56x\n");
			break;
		case CH32V20x:
			fprintf(stderr, "Detected: CH32V20x\n");
			break;
		case CH32V30x:
			fprintf(stderr, "Detected: CH32V30x\n");
			break;
		case CH58x:
			fprintf(stderr, "Detected: CH58x\n");
			break;
		case CH32V003:
			fprintf(stderr, "Detected: CH32V003\n");
			break;
	}
}

static int checkChip(enum RiscVChip chip) {
	switch(chip) {
		case CH32V003:
			return 0; // Use direct mode
		case CH32V10x:
		case CH32V20x:
		case CH32V30x:
			return 1; // Use binary blob mode
		case CH56x:
		case CH57x:
		case CH58x:
		default:
			return -1; // Not supported yet
	}
}

// For non-ch32v003 chips.
//static int LEReadBinaryBlob( void * d, uint32_t offset, uint32_t amount, uint8_t * readbuff );
static int InternalLinkEHaltMode( void * d, int mode );
static int LEWriteBinaryBlob( void * d, uint32_t address_to_write, uint32_t len, uint8_t * blob );

#define WCHTIMEOUT 5000
#define WCHCHECK(x) if( (status = x) ) { fprintf( stderr, "Bad USB Operation on " __FILE__ ":%d (%d)\n", __LINE__, status ); exit( status ); }

void wch_link_command( libusb_device_handle * devh, const void * command_v, int commandlen, int * transferred, uint8_t * reply, int replymax )
{