Skip to content

Commit

Permalink
Some tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
wagiminator committed May 26, 2023
1 parent c0b9af9 commit c10e5d8
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 73 deletions.
Binary file modified C64_DiskMaster64/software/mcu/diskmaster64.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion C64_DiskMaster64/software/mcu/diskmaster64.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ===================================================================================
// Project: DiskMaster64 - USB to Commodore Floppy Disk Drive Adapter
// Version: v1.1
// Version: v1.2
// Year: 2022
// Author: Stefan Wagner
// Github: https://github.com/wagiminator
Expand Down
3 changes: 2 additions & 1 deletion C64_DiskMaster64/software/mcu/src/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ typedef USB_HUB_DESCR __xdata *PXUSB_HUB_DESCR;
typedef struct _USB_HID_DESCR {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdHID;
uint8_t bcdHIDL;
uint8_t bcdHIDH;
uint8_t bCountryCode;
uint8_t bNumDescriptors;
uint8_t bDescriptorTypeX;
Expand Down
80 changes: 26 additions & 54 deletions C64_DiskMaster64/software/mcu/src/usb_cdc.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// ===================================================================================
// USB CDC Functions for CH551, CH552 and CH554
// Basic USB CDC Functions for CH551, CH552 and CH554 * v1.2 *
// ===================================================================================

#include "ch554.h"
#include "usb.h"
#include "usb_cdc.h"
#include "usb_descr.h"
#include "usb_handler.h"
Expand All @@ -14,20 +13,23 @@

// Initialize line coding
__xdata CDC_LINE_CODING_TYPE CDC_lineCodingB = {
.baudrate = 57600, // baudrate 57600
.baudrate = 115200, // baudrate 115200
.stopbits = 0, // 1 stopbit
.parity = 0, // no parity
.databits = 8 // 8 databits
};

volatile __xdata uint8_t CDC_controlLineState = 0; // control line state
// Variables
volatile __xdata uint8_t CDC_controlLineState = 0; // control line state
volatile __xdata uint8_t CDC_readByteCount = 0; // number of data bytes in IN buffer
volatile __xdata uint8_t CDC_readPointer = 0; // data pointer for fetching
volatile __xdata uint8_t CDC_writePointer = 0; // data pointer for writing
volatile __bit CDC_writeBusyFlag = 0; // flag of whether upload pointer is busy
__xdata uint8_t CDC_writePointer = 0; // data pointer for writing

#define CDC_DTR_flag (CDC_controlLineState & 1)
#define CDC_RTS_flag ((CDC_controlLineState >> 1) & 1)
// CDC class requests
#define SET_LINE_CODING 0x20 // host configures line coding
#define GET_LINE_CODING 0x21 // host reads configured line coding
#define SET_CONTROL_LINE_STATE 0x22 // generates RS-232/V.24 style control signals

// ===================================================================================
// Front End Functions
Expand All @@ -47,7 +49,7 @@ uint8_t CDC_available(void) {

// Check if OUT buffer is ready to be written
__bit CDC_ready(void) {
return(CDC_DTR_flag && !CDC_writeBusyFlag);
return(!CDC_writeBusyFlag);
}

// Flush the OUT buffer
Expand All @@ -62,8 +64,8 @@ void CDC_flush(void) {

// Write single character to OUT buffer
void CDC_write(char c) {
while(!CDC_ready()); // wait for ready to write
EP2_buffer[MAX_PACKET_SIZE + CDC_writePointer++] = c; // write character
while(CDC_writeBusyFlag); // wait for ready to write
EP2_buffer[64 + CDC_writePointer++] = c; // write character
if(CDC_writePointer == EP2_SIZE) CDC_flush(); // flush if buffer full
}

Expand All @@ -82,52 +84,17 @@ void CDC_println(char* str) {
// Read single character from IN buffer
char CDC_read(void) {
char data;
while(!CDC_available()); // wait for data
while(!CDC_readByteCount); // wait for data
data = EP2_buffer[CDC_readPointer++]; // get character
if(--CDC_readByteCount == 0) // dec number of bytes in buffer
UEP2_CTRL = UEP2_CTRL & ~MASK_UEP_R_RES | UEP_R_RES_ACK;// request new data if empty
return data;
}

// Get DTR flag
__bit CDC_getDTR(void) {
return CDC_DTR_flag;
}

// Get RTS flag
__bit CDC_getRTS(void) {
return CDC_RTS_flag;
}

// ===================================================================================
// CDC-Specific USB Handler Functions
// ===================================================================================

// CDC class requests
#define SET_LINE_CODING 0x20 // host configures line coding
#define GET_LINE_CODING 0x21 // host reads configured line coding
#define SET_CONTROL_LINE_STATE 0x22 // generates RS-232/V.24 style control signals

// Set line coding handler
void CDC_setLineCoding(void) {
uint8_t i;
for(i=0; i<((sizeof(CDC_lineCodingB)<=USB_RX_LEN)?sizeof(CDC_lineCodingB):USB_RX_LEN); i++)
((uint8_t*)&CDC_lineCodingB)[i] = EP0_buffer[i]; // receive line coding from host
}

// Get line coding handler
uint8_t CDC_getLineCoding(void) {
uint8_t i;
for(i=0; i<sizeof(CDC_lineCodingB); i++)
EP0_buffer[i] = ((uint8_t*)&CDC_lineCodingB)[i]; // transmit line coding to host
return sizeof(CDC_lineCodingB);
}

// Set control line state handler
void CDC_setControlLineState(void) {
CDC_controlLineState = EP0_buffer[2]; // read control line state
}

// Setup CDC endpoints
void CDC_setup(void) {
UEP1_DMA = (uint16_t)EP1_buffer; // EP1 data transfer address
Expand All @@ -151,27 +118,32 @@ void CDC_reset(void) {

// Handle non-standard control requests
uint8_t CDC_control(void) {
uint8_t i;
if((USB_setupBuf->bRequestType & USB_REQ_TYP_MASK) == USB_REQ_TYP_CLASS) {
switch(USB_setupBuf->bRequest) {
case GET_LINE_CODING: // 0x21 currently configured
return CDC_getLineCoding();
case SET_CONTROL_LINE_STATE: // 0x22 generates RS-232/V.24 style control signals
CDC_setControlLineState();
case GET_LINE_CODING: // 0x21 currently configured
for(i=0; i<sizeof(CDC_lineCodingB); i++)
EP0_buffer[i] = ((uint8_t*)&CDC_lineCodingB)[i]; // transmit line coding to host
return sizeof(CDC_lineCodingB);
case SET_CONTROL_LINE_STATE: // 0x22 generates RS-232/V.24 style control signals
CDC_controlLineState = EP0_buffer[2]; // read control line state
return 0;
case SET_LINE_CODING: // 0x20 Configure
case SET_LINE_CODING: // 0x20 Configure
return 0;
default:
return 0xFF; // command not supported
return 0xFF; // command not supported
}
}
else return 0xFF;
}

// Endpoint 0 OUT handler
void CDC_EP0_OUT(void) {
if(SetupReq == SET_LINE_CODING) { // set line coding
uint8_t i;
if(SetupReq == SET_LINE_CODING) { // set line coding
if(U_TOG_OK) {
CDC_setLineCoding();
for(i=0; i<((sizeof(CDC_lineCodingB)<=USB_RX_LEN)?sizeof(CDC_lineCodingB):USB_RX_LEN); i++)
((uint8_t*)&CDC_lineCodingB)[i] = EP0_buffer[i]; // receive line coding from host
UEP0_T_LEN = 0;
UEP0_CTRL |= UEP_R_RES_ACK | UEP_T_RES_ACK; // send 0-length packet
}
Expand Down
18 changes: 13 additions & 5 deletions C64_DiskMaster64/software/mcu/src/usb_cdc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ===================================================================================
// USB CDC Functions for CH551, CH552 and CH554
// Basic USB CDC Functions for CH551, CH552 and CH554 * v1.2 *
// ===================================================================================

#pragma once
Expand All @@ -16,9 +16,16 @@ void CDC_print(char* str); // write string to OUT buffer
void CDC_println(char* str); // write string with newline to OUT buffer and flush
uint8_t CDC_available(void); // check number of bytes in the IN buffer
__bit CDC_ready(void); // check if OUT buffer is ready to be written
__bit CDC_getDTR(void); // get DTR flag
__bit CDC_getRTS(void); // get RTS flag
#define CDC_writeflush(c) {CDC_write(c);CDC_flush();}
#define CDC_writeflush(c) {CDC_write(c);CDC_flush();} // write & flush char

// ===================================================================================
// CDC Control Line State
// ===================================================================================
extern volatile __xdata uint8_t CDC_controlLineState; // control line state
#define CDC_DTR_flag (CDC_controlLineState & 1) // DTR flag
#define CDC_RTS_flag ((CDC_controlLineState >> 1) & 1) // RTS flag
#define CDC_getDTR() (CDC_DTR_flag) // get DTR flag
#define CDC_getRTS() (CDC_RTS_flag) // get RTS flag

// ===================================================================================
// CDC Line Coding
Expand All @@ -31,4 +38,5 @@ typedef struct _CDC_LINE_CODING_TYPE {
} CDC_LINE_CODING_TYPE, *PCDC_LINE_CODING_TYPE;

extern __xdata CDC_LINE_CODING_TYPE CDC_lineCodingB;
#define CDC_lineCoding ((PCDC_LINE_CODING_TYPE)CDC_lineCodingB)
#define CDC_lineCoding ((PCDC_LINE_CODING_TYPE)CDC_lineCodingB)
#define CDC_getBAUD() (CDC_lineCoding->baudrate)
17 changes: 7 additions & 10 deletions C64_DiskMaster64/software/mcu/src/usb_handler.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// ===================================================================================
// USB Handler for CH551, CH552 and CH554
// USB Handler for CH551, CH552 and CH554 * v1.2 *
// ===================================================================================

#include "ch554.h"
#include "usb_handler.h"

uint16_t SetupLen;
uint8_t SetupReq, UsbConfig;
// ===================================================================================
// Variables
// ===================================================================================
volatile uint16_t SetupLen;
volatile uint8_t SetupReq, UsbConfig;
__code uint8_t *pDescr;

// ===================================================================================
Expand Down Expand Up @@ -38,7 +41,6 @@ void USB_EP0_copyDescr(uint8_t len) {
// ===================================================================================
// Endpoint Handler
// ===================================================================================

void USB_EP0_SETUP(void) {
uint8_t len = USB_RX_LEN;
if(len == (sizeof(USB_SETUP_REQ))) {
Expand Down Expand Up @@ -93,9 +95,6 @@ void USB_EP0_SETUP(void) {
#ifdef USB_STR_DESCR_i9
case 9: pDescr = USB_STR_DESCR_i9; break;
#endif
#ifdef USB_STR_DESCR_ixee
case 0xee: pDescr = USB_STR_DESCR_ixee; break;
#endif
default: pDescr = USB_STR_DESCR_ix; break;
}
len = pDescr[0]; // descriptor length
Expand Down Expand Up @@ -326,7 +325,6 @@ void USB_EP0_OUT(void) {
// ===================================================================================
// USB Interrupt Service Routine
// ===================================================================================

#pragma save
#pragma nooverlay
void USB_interrupt(void) { // inline not really working in multiple files in SDCC
Expand Down Expand Up @@ -437,15 +435,14 @@ void USB_interrupt(void) { // inline not really working in multiple files in S
// ===================================================================================
// USB Init Function
// ===================================================================================

void USB_init(void) {
USB_CTRL = bUC_DEV_PU_EN // USB internal pull-up enable
| bUC_INT_BUSY // Return NAK if USB INT flag not clear
| bUC_DMA_EN; // DMA enable
UDEV_CTRL = bUD_PD_DIS // Disable UDP/UDM pulldown resistor
| bUD_PORT_EN; // Enable port, full-speed

#if F_CPU < 12000000 // Set low-speed mode if SysFreq < 12 MHz
#if F_CPU < 6000000 // Set low-speed mode if SysFreq < 6 MHz
USB_CTRL |= bUC_LOW_SPEED;
UDEV_CTRL |= bUD_LOW_SPEED;
#endif
Expand Down
5 changes: 3 additions & 2 deletions C64_DiskMaster64/software/mcu/src/usb_handler.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ===================================================================================
// USB Handler for CH551, CH552 and CH554
// USB Handler for CH551, CH552 and CH554 * v1.2 *
// ===================================================================================

#pragma once
Expand All @@ -10,7 +10,8 @@
// Variables
// ===================================================================================
#define USB_setupBuf ((PUSB_SETUP_REQ)EP0_buffer)
extern uint8_t SetupReq;
extern volatile uint8_t SetupReq;
extern volatile uint16_t SetupLen;

// ===================================================================================
// Custom External USB Handler Functions
Expand Down
Binary file modified C64_DiskMaster64/software/pc/libs/diskmaster64.bin
Binary file not shown.

0 comments on commit c10e5d8

Please sign in to comment.