Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USB terminal #84

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions lib/swio_self.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#ifndef SWIOSELFH
#define SWIOSELFH
#define T1COEFF 2

#define DMCONTROL 0x10
#define DMSTATUS 0x11
#define DMHARTINFO 0x12
#define DMABSTRACTCS 0x16
#define DMCOMMAND 0x17
#define DMABSTRACTAUTO 0x18
#define DMPROGBUF0 0x20
#define DMPROGBUF1 0x21
#define DMPROGBUF2 0x22
#define DMPROGBUF3 0x23
#define DMPROGBUF4 0x24
#define DMPROGBUF5 0x25
#define DMPROGBUF6 0x26
#define DMPROGBUF7 0x27
#define DMCPBR 0x7C
#define DMCFGR 0x7D
#define DMSHDWCFGR 0x7E

#define DISABLE_SWIO AFIO->PCFR1 |= AFIO_PCFR1_SWJ_CFG_DISABLE
#define ENABLE_SWIO AFIO->PCFR1 &= ~(AFIO_PCFR1_SWJ_CFG_DISABLE)

static inline void PrecDelay(int delay) {
asm volatile(
"1: addi %[delay], %[delay], -1\n"
"bne %[delay], x0, 1b\n" :[delay]"+r"(delay) );
}

static inline void Send1Bit(uint8_t t1coeff) {
// Low for a nominal period of time.
// High for a nominal period of time.
DISABLE_SWIO;
funDigitalWrite(PD1, 0);
ENABLE_SWIO;
PrecDelay(t1coeff);

DISABLE_SWIO;
funDigitalWrite(PD1, 1);
ENABLE_SWIO;
PrecDelay(t1coeff);
}

static inline void Send0Bit(uint8_t t1coeff) {
// Low for a LONG period of time.
// High for a nominal period of time.
DISABLE_SWIO;
funDigitalWrite(PD1, 0);
ENABLE_SWIO;
PrecDelay(t1coeff*4);

DISABLE_SWIO;
funDigitalWrite(PD1, 1);
ENABLE_SWIO;
PrecDelay(t1coeff);
}

static void MCFWriteReg32(uint8_t command, uint32_t value, uint8_t t1coeff) {

__disable_irq();
Send1Bit(t1coeff);
uint32_t mask;
for (mask = 1<<6; mask; mask >>= 1){
if (command & mask) Send1Bit(t1coeff);
else Send0Bit(t1coeff);
}
Send1Bit(t1coeff);
for (mask = 1<<31; mask; mask >>= 1) {
if (value & mask) Send1Bit(t1coeff);
else Send0Bit(t1coeff);
}
__enable_irq();
Delay_Ms(8);
}

void attempt_unlock(uint8_t t1coeff) {
if (*DMSTATUS_SENTINEL) {
RCC->APB2PCENR |= RCC_APB2Periph_GPIOD;
funPinMode(PD1, GPIO_Speed_10MHz | GPIO_CNF_OUT_PP);
MCFWriteReg32(DMSHDWCFGR, 0x5aa50000 | (1<<10), t1coeff);
MCFWriteReg32(DMCFGR, 0x5aa50000 | (1<<10), t1coeff);
*DMDATA0 = 0; // Clear garbage before using it for prints
// The next steps seems unneeded for the purpose
// MCFWriteReg32(DMCFGR, 0x5aa50000 | (1<<10), t1coeff);
// MCFWriteReg32(DMABSTRACTAUTO, 0x00000000, t1coeff);
// MCFWriteReg32(DMCONTROL, 0x80000001 | (1<<10), t1coeff);
// MCFWriteReg32(DMCONTROL, 0x40000001 | (1<<10), t1coeff);
}
}
#endif
72 changes: 68 additions & 4 deletions rv003usb/rv003usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@

#include "ch32fun.h"

#if RV003USB_USB_TERMINAL
#if FUNCONF_USE_DEBUGPRINTF
#include "../lib/swio_self.h" // Needed for unlocking DM
#else
#define RV003USB_USB_TERMINAL 0
#endif
#endif

#define ENDPOINT0_SIZE 8 //Fixed for USB 1.1, Low Speed.

#if RV003USB_EVENT_DEBUGGING
Expand Down Expand Up @@ -156,12 +164,22 @@ void usb_pid_handle_in( uint32_t addr, uint8_t * data, uint32_t endp, uint32_t u
}
#endif

tosend = 0;

// Handle IN (sending data back to PC)
// Do this down here.
// We do this because we are required to have an in-endpoint. We don't
// have to do anything with it, though.
#if RV003USB_USB_TERMINAL
if( e->opaque == (uint8_t *)DMDATA0 )
{
usb_send_data( (void *)DMDATA0, ENDPOINT0_SIZE, 0, sendtok );
*DMDATA0 = 0;
*DMDATA1 = 0;
e->opaque = 0;
return;
}
#endif


uint8_t * tsend = e->opaque;

int offset = (e->count)<<3;
Expand Down Expand Up @@ -204,7 +222,7 @@ void usb_pid_handle_data( uint32_t this_token, uint8_t * data, uint32_t which_da
e->toggle_out = !e->toggle_out;


#if RV003USB_HANDLE_USER_DATA || RV003USB_USE_REBOOT_FEATURE_REPORT
#if RV003USB_HANDLE_USER_DATA || RV003USB_USE_REBOOT_FEATURE_REPORT || RV003USB_USB_TERMINAL
if( epno || ( !ist->setup_request && length > 3 ) )
{
#if RV003USB_USE_REBOOT_FEATURE_REPORT
Expand All @@ -223,7 +241,28 @@ void usb_pid_handle_data( uint32_t this_token, uint8_t * data, uint32_t which_da
}
}
#endif
#if RV003USB_USB_TERMINAL
if( epno == 0 && data_in[0] == 0xfd )
{
uint32_t *base = __builtin_assume_aligned( data_in, 4 );

*DMDATA0 = base[0];
*DMDATA1 = base[1];
*DMDATA0 &= ~( 0xFF );

int i;
for( i = 1; i < 8; i++ )
{
if( data_in[i] == 0 )
{
*DMDATA0 |= i + 3;
break;
}
}

goto just_ack;
}
#endif

#if RV003USB_HANDLE_USER_DATA
usb_handle_user_data( e, epno, data_in, length, ist );
Expand Down Expand Up @@ -292,12 +331,37 @@ void usb_pid_handle_data( uint32_t this_token, uint8_t * data, uint32_t which_da
#endif
}
else
#if RV003USB_HID_FEATURES
#if RV003USB_HID_FEATURES || RV003USB_USB_TERMINAL
if( reqShl == (0x01a1>>1) )
{
// Class read request.
// The host wants to read back from us. hid_get_feature_report
e->max_len = 1; // If 0 - terminal is sent to the stratosphere
#if RV003USB_HID_FEATURES
usb_handle_hid_get_report_start( e, wLength, wvi );
#endif
#if RV003USB_USB_TERMINAL
if( ( wvi & 0xff ) == 0xfd )
{
if( !*DMSTATUS_SENTINEL )
{
if( ( *DMDATA0 & 0x80 ) )
{
e->opaque = (uint8_t *)DMDATA0;
e->max_len = ( *DMDATA0 & 0xf ) - 4;
// e->max_len = 8;
}
else if( ( *DMDATA0 & 0xc0 ) == 0xc0 )
{
*DMDATA0 = 0;
}
}
else
{
attempt_unlock( 2 );
}
}
#endif
}
else
#endif
Expand Down
4 changes: 4 additions & 0 deletions rv003usb/rv003usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
*/
#endif

#ifndef RV003USB_USB_TERMINAL
#define RV003USB_USB_TERMINAL 0
#endif


#ifndef __ASSEMBLER__

Expand Down