Skip to content

Commit

Permalink
Update 035 work, getting close to USB
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlohr committed Apr 19, 2024
1 parent df3cdc8 commit 5f0474c
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 49 deletions.
36 changes: 18 additions & 18 deletions ch32v003fun/ch32v003fun.c
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ void DMA2_Channel10_IRQHandler( void ) __attribute__((section(".text.vector_han
void DMA2_Channel11_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
#endif

#if defined( CH32X03X )
#if defined( CH32X03x)
void USART2_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void EXTI15_8_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
void EXTI25_16_IRQHandler( void ) __attribute__((section(".text.vector_handler"))) __attribute((weak,alias("DefaultIRQHandler"))) __attribute__((used));
Expand Down Expand Up @@ -940,23 +940,23 @@ void InterruptVectorDefault()
.word TIM1_TRG_COM_IRQHandler /* TIM1 Trigger and Commutation */ \n\
.word TIM1_CC_IRQHandler /* TIM1 Capture Compare */ \n\
.word TIM2_IRQHandler /* TIM2 */ \n"
#if defined( CH32X03X )
" .word USART2_IRQn = 39, /* UART2 Interrupt */ \n\
.word EXTI15_8_IRQn = 40, /* External Line[8:15] Interrupt */ \n\
.word EXTI25_16_IRQn = 41, /* External Line[25:16] Interrupt */ \n\
.word USART3_IRQn = 42, /* UART2 Interrupt */ \n\
.word USART4_IRQn = 43, /* UART2 Interrupt */ \n\
.word DMA1_Channel8_IRQn = 44, /* DMA1 Channel 8 global Interrupt */ \n\
.word USBFS_IRQn = 45, /* USB Full-Speed Interrupt */ \n\
.word USBFS_WakeUp_IRQn = 46, /* USB Full-Speed Wake-Up Interrupt */ \n\
.word PIOC_IRQn = 47, /* Programmable IO Controller Interrupt */ \n\
.word OPA_IRQn = 48, /* Op Amp Interrupt */ \n\
.word USBPD_IRQn = 49, /* USB Power Delivery Interrupt */ \n\
.word USBPD_WKUP_IRQn = 50, /* USB Power Delivery Wake-Up Interrupt */ \n\
.word TIM2_CC_IRQn = 51, /* Timer 2 Compare Global Interrupt */ \n\
.word TIM2_TRG_IRQn = 52, /* Timer 2 Trigger Global Interrupt */ \n\
.word TIM2_BRK_IRQn = 53, /* Timer 2 Brk Global Interrupt */ \n\
.word TIM3_IRQn = 54, /* Timer 3 Global Interrupt */"
#if defined( CH32X03x )
" .word USART2_IRQHandler /* UART2 Interrupt */ \n\
.word EXTI15_8_IRQHandler /* External Line[8:15] Interrupt */ \n\
.word EXTI25_16_IRQHandler /* External Line[25:16] Interrupt */ \n\
.word USART3_IRQHandler /* UART2 Interrupt */ \n\
.word USART4_IRQHandler /* UART2 Interrupt */ \n\
.word DMA1_Channel8_IRQHandler /* DMA1 Channel 8 global Interrupt */ \n\
.word USBFS_IRQHandler /* USB Full-Speed Interrupt */ \n\
.word USBFS_WakeUp_IRQHandler /* USB Full-Speed Wake-Up Interrupt */ \n\
.word PIOC_IRQHandler /* Programmable IO Controller Interrupt */ \n\
.word OPA_IRQHandler /* Op Amp Interrupt */ \n\
.word USBPD_IRQHandler /* USB Power Delivery Interrupt */ \n\
.word USBPD_WKUP_IRQHandler /* USB Power Delivery Wake-Up Interrupt */ \n\
.word TIM2_CC_IRQHandler /* Timer 2 Compare Global Interrupt */ \n\
.word TIM2_TRG_IRQHandler /* Timer 2 Trigger Global Interrupt */ \n\
.word TIM2_BRK_IRQHandler /* Timer 2 Brk Global Interrupt */ \n\
.word TIM3_IRQHandler /* Timer 3 Global Interrupt */"
#endif
);
#endif
Expand Down
61 changes: 38 additions & 23 deletions ch32v003fun/ch32v003fun.h
Original file line number Diff line number Diff line change
Expand Up @@ -1700,8 +1700,8 @@ typedef struct
/* USB Full Speed Device Mode */
typedef struct
{
__IO uint8_t BASE_CTLR;
__IO uint8_t UDEV_CTLR; // or host ctlr
__IO uint8_t BASE_CTRL; //XXX (spelling)
__IO uint8_t UDEV_CTRL; // or host ctlr
__IO uint8_t INT_EN;
__IO uint8_t DEV_ADDR;
__IO uint8_t RESERVED0;
Expand All @@ -1720,25 +1720,25 @@ typedef struct
__IO uint32_t UEP2_DMA; // Also HOST_RX_DMA
__IO uint32_t UEP3_DMA; // Also HOST_TX_DMA

//__IO uint32_t UEP0_CTLR;
//__IO uint32_t UEP0_CTRL;
__IO uint16_t UEP0_TX_LEN;
__IO uint16_t UEP0_CTLR_H;
__IO uint16_t UEP0_CTRL_H;

//__IO uint32_t UEP1_CTLR;
//__IO uint32_t UEP1_CTRL;
__IO uint16_t UEP1_TX_LEN;
__IO uint16_t UEP1_CTLR_H; // Also HOST_SETUP
__IO uint16_t UEP1_CTRL_H; // Also HOST_SETUP

//__IO uint32_t UEP2_CTLR;
//__IO uint32_t UEP2_CTRL;
__IO uint16_t UEP2_TX_LEN; // Also HOST_PID
__IO uint16_t UEP2_CTLR_H; // Also HOST_RX_CTL
__IO uint16_t UEP2_CTRL_H; // Also HOST_RX_CTL

//__IO uint32_t UEP3_CTLR;
//__IO uint32_t UEP3_CTRL;
__IO uint16_t UEP3_TX_LEN; // Also HOST_TX_LEN
__IO uint16_t UEP3_CTLR_H; // Also HOST_TX_CTL
__IO uint16_t UEP3_CTRL_H; // Also HOST_TX_CTL

//__IO uint32_t UEP4_CTLR;
//__IO uint32_t UEP4_CTRL;
__IO uint16_t UEP4_TX_LEN;
__IO uint16_t UEP4_CTLR_H;
__IO uint16_t UEP4_CTRL_H;

__IO uint32_t RESERVED3[8];

Expand All @@ -1748,27 +1748,39 @@ typedef struct

__IO uint32_t RESERVED4;

//__IO uint32_t UEP5_CTLR;
//__IO uint32_t UEP5_CTRL;
__IO uint16_t UEP5_TX_LEN;
__IO uint16_t UEP5_CTLR_H;
__IO uint16_t UEP5_CTRL_H;

//__IO uint32_t UEP6_CTLR;
//__IO uint32_t UEP6_CTRL;
__IO uint16_t UEP6_TX_LEN;
__IO uint16_t UEP6_CTLR_H;
__IO uint16_t UEP6_CTRL_H;

//__IO uint32_t UEP7_CTLR;
//__IO uint32_t UEP7_CTRL;
__IO uint16_t UEP7_TX_LEN;
__IO uint16_t UEP7_CTLR_H;
__IO uint16_t UEP7_CTRL_H;

__IO uint32_t UEPX_MOD;
} USBFS_TypeDef;

#define USB_PHY_V33 (1<<6)
#define USB_IOEN (1<<7)

#define UDP_PUE_00 (0b00<<2)
#define UDP_PUE_01 (0b01<<2)
#define UDP_PUE_10 (0b10<<2)
#define UDP_PUE_11 (0b11<<2)
#define UDM_PUE_00 (0b00<<0)
#define UDM_PUE_01 (0b01<<0)
#define UDM_PUE_10 (0b10<<0)
#define UDM_PUE_11 (0b11<<0)

/* USB Host Mode */

typedef struct
{
__IO uint8_t RESERVED0;
__IO uint8_t HOST_CTLR; // or host ctlr
__IO uint8_t HOST_CTRL;
__IO uint8_t RESERVED1;
__IO uint8_t RESERVED2;
__IO uint8_t RESERVED3;
Expand All @@ -1778,14 +1790,14 @@ typedef struct
__IO uint16_t RESERVED7;
__IO uint16_t RESERVED8;
__IO uint8_t RESERVED9;
__IO uint8_t HOST_EP_MOD; // Also HOST_EP_MOD
__IO uint8_t HOST_EP_MOD;
__IO uint8_t RESERVED10;
__IO uint8_t RESERVED11;

__IO uint32_t RESERVED12;
__IO uint32_t RESERVED13;
__IO uint32_t HOST_RX_DMA; // Also HOST_RX_DMA
__IO uint32_t HOST_TX_DMA; // Also HOST_TX_DMA
__IO uint32_t HOST_RX_DMA;
__IO uint32_t HOST_TX_DMA;

__IO uint16_t RESERVED14;
__IO uint16_t RESERVED15;
Expand Down Expand Up @@ -5517,6 +5529,8 @@ typedef struct
#define RCC_FLITFEN ((uint16_t)0x0010) /* FLITF clock enable */
#define RCC_CRCEN ((uint16_t)0x0040) /* CRC clock enable */
#define RCC_USBHD ((uint16_t)0x1000)
#define RCC_USBFS ((uint16_t)0x1000)
#define RCC_USBPD ((uint16_t)0x20000)

/****************** Bit definition for RCC_APB2PCENR register *****************/
#define RCC_AFIOEN ((uint32_t)0x00000001) /* Alternate Function I/O clock enable */
Expand Down Expand Up @@ -11077,7 +11091,8 @@ typedef struct
#define USART_FLAG_FE ((uint16_t)0x0002)
#define USART_FLAG_PE ((uint16_t)0x0001)

#if defined(CH32V10x)
// While not truly CH32X035, we can re-use some of the USB register defs.
#if defined(CH32V10x) | defined(CH32X03x)
/* ch32v10x_usb.h ------------------------------------------------------------*/

#ifndef NULL
Expand Down
2 changes: 1 addition & 1 deletion ch32v003fun/ch32v003fun.mk
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ else
TARGET_MCU_LD:=1
else ifeq ($(findstring CH32X03,$(TARGET_MCU)),CH32X03) # CH32X033, X035
TARGET_MCU_PACKAGE?=CH32X035F8U6
CFLAGS_ARCH+= -march=rv32imac \
CFLAGS_ARCH+=-march=rv32imac \
-mabi=ilp32 \
-DCH32X03x=1

Expand Down
10 changes: 10 additions & 0 deletions examples_x035/sandbox/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
all : flash

TARGET:=sandbox
TARGET_MCU:=CH32X035

include ../../ch32v003fun/ch32v003fun.mk

flash : cv_flash
clean : cv_clean

9 changes: 9 additions & 0 deletions examples_x035/sandbox/funconfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _FUNCONFIG_H
#define _FUNCONFIG_H

// Though this should be on by default we can extra force it on.
#define FUNCONF_USE_DEBUGPRINTF 1
#define FUNCONF_DEBUGPRINTF_TIMEOUT (1<<31) // Wait for a very very long time.

#endif

43 changes: 43 additions & 0 deletions examples_x035/sandbox/sandbox.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* Small example showing how to use the SWIO programming pin to
do printf through the debug interface */

#include "ch32v003fun.h"
#include <stdio.h>

uint32_t count;

int last = 0;
void handle_debug_input( int numbytes, uint8_t * data )
{
last = data[0];
count += numbytes;
}

int main()
{
SystemInit();

funGpioInitAll();

funPinMode( PA0, GPIO_CFGLR_OUT_10Mhz_PP );

printf( "%08x\n", &USBPD->DMA );

while(1);

#if 0
while(1)
{
GPIOA->BSHR = 1; // Turn on GPIOs
printf( "+%lu %lu\n", count++ );
Delay_Ms(100);
int i;
for( i = 0; i < 10000; i++ )
poll_input();
GPIOA->BSHR = (1<<16); // Turn off GPIODs
printf( "-%lu[%c]\n", count++, last );
Delay_Ms(100);
}
#endif
}

11 changes: 11 additions & 0 deletions examples_x035/usbdevice.incomplete/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
all : flash

TARGET:=usbdevice
TARGET_MCU:=CH32X035
ADDITIONAL_C_FILES:=fsusb.c

include ../../ch32v003fun/ch32v003fun.mk

flash : cv_flash
clean : cv_clean

127 changes: 127 additions & 0 deletions examples_x035/usbdevice.incomplete/fsusb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include "fsusb.h"
#include "ch32v003fun.h"

uint32_t USBDEBUG0, USBDEBUG1, USBDEBUG2;
uint8_t EP0_DATA[64] __attribute__((aligned(32)));

void USBFS_IRQHandler() __attribute__((section(".text.vector_handler"))) __attribute__((interrupt));

#define CRB_U_IS_NAK (1<<7)
#define CTOG_MATCH_SYNC (1<<6)
#define CRB_U_SIE_FREE (1<<5)
#define CRB_UIF_FIFO_OV (1<<4)
#define CRB_UIF_HST_SOF (1<<3)
#define CRB_UIF_SUSPEND (1<<2)
#define CRB_UIF_TRANSFER (1<<1)
#define CRB_UIF_BUS_RST (1<<0)
#define CSETUP_ACT (1<<15)
#define CRB_UIS_TOG_OK (1<<14)
#define CMASK_UIS_TOKEN (3<<12)
#define CMASK_UIS_ENDP (0xf<<8)

#define CUIS_TOKEN_OUT 0x0
#define CUIS_TOKEN_SOF 0x1
#define CUIS_TOKEN_IN 0x2
#define CUIS_TOKEN_SETUP 0x3

void USBFS_IRQHandler()
{
// Combined FG + ST flag.
uint16_t intfgst = *(uint16_t*)(&USBFSD->INT_FG);

GPIOA->BSHR = 1;

// TODO: Check if needs to be do-while to re-check.


if( intfgst & CRB_UIF_TRANSFER )
{
switch ( ( intfgst & CMASK_UIS_TOKEN) >> 12)
{
case CUIS_TOKEN_IN:
switch ( intst & ( USBFS_UIS_TOKEN_M

/* while( ( flags = USBFS->INT_FG & 0x9f ) )
{
if( flags & RB_UIF_TRANSFER )
{
// Handle transfer.
USBDEBUG0++;
}
if( flags & RB_UIF_BUS_RST )
{
// Handle transfer.
USBDEBUG0+=1000;
}
//USBDEBUG0 = USBFS->INT_FG;
USBFS->INT_FG = flags;
}
*/

*(uint16_t*)(&USBFSD->INT_FG) = intfgst;
//intfgst = *(uint16_t*)(&USBFSD->INT_FG);
GPIOA->BSHR = 1<<16;
}

void USBFS_Poll()
{
USBDEBUG2 = USBFS->INT_ST;//EP0_DATA[1];
USBDEBUG1 = USBFS->MIS_ST;
}

int FSUSBSetup()
{
RCC->APB2PCENR |= RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC;
RCC->AHBPCENR |= RCC_USBFS;

NVIC_EnableIRQ( USBFS_IRQn );

AFIO->CTLR |= USB_PHY_V33;

USBFS->BASE_CTRL = RB_UC_RESET_SIE | RB_UC_CLR_ALL;
USBFS->BASE_CTRL = RB_UC_DEV_PU_EN | RB_UC_INT_BUSY | RB_UC_DMA_EN;

// Enter device mode.
USBFS->INT_EN = RB_UIE_SUSPEND | RB_UIE_TRANSFER | RB_UIE_BUS_RST;
USBFS->UEP4_1_MOD = 0;
USBFS->UEP2_3_MOD = 0;
USBFS->UEP567_MOD = 0;
USBFS->UEP0_DMA = ((intptr_t)EP0_DATA) & 0x7fff;
USBFS->DEV_ADDR = 0x00;
USBFS->UDEV_CTRL = RB_UD_PD_DIS | RB_UD_PORT_EN;

// Go on-bus.

// From the TRM:
//
// USB multiplexing IO pins enable:
//
// Enabling USB requires, in addition to USB_IOEN set to
// 1, the setting of: MODE=0 in GPIO configuration register
// GPIOC_CFGXR corresponding to PC16 and PC17 to
// select the input mode.
//
// for USB device, CNF=10 corresponding to PC17 to select
// the input mode with up and down For USB devices, PC17
// corresponding to CNF=10 selects the input mode with up
// and down pull, PC17 corresponding to bit 1 in
// GPIOC_OUTDR selects the up pull, and PC16
// corresponding to CNF=01 selects the floating input.


AFIO->CTLR = (AFIO->CTLR & ~(UDP_PUE_11 | UDM_PUE_11 )) | USB_PHY_V33 | USB_IOEN | UDP_PUE_11; //1.5k pullup

// Enable PC16/17 Alternate Function (USB)
// According to EVT, GPIO16 = GPIO_Mode_IN_FLOATING, GPIO17 = GPIO_Mode_IPU
GPIOC->CFGXR &= ~(0xf<<(4*0));
GPIOC->CFGXR |= (GPIO_CFGLR_IN_FLOAT)<<(4*0); // MSBs are CNF, LSBs are MODE
GPIOC->CFGXR &= ~(0xf<<(4*1));
GPIOC->CFGXR |= (GPIO_CFGLR_IN_PUPD)<<(4*1);
GPIOC->BSXR = 1<<1; // PC17 on.

USBFS->UDEV_CTRL = RB_UD_PORT_EN;

// Go on-bus.
return 0;
}

Loading

0 comments on commit 5f0474c

Please sign in to comment.