Skip to content

Commit

Permalink
Add support for PCB v1.3 audio switch, update build types for PCB ver…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
LouDnl committed Mar 9, 2025
1 parent 019a8bb commit 98726dc
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 30 deletions.
18 changes: 13 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ cmake_minimum_required(VERSION 3.17)
set(PROJECT_NAME usbsidpico)
set(PROJECT_MANUFACTURER "LouD")
set(PRODUCT_STRING "USBSID-Pico")
set(MAGIC_SMOKE "20250227")
set(MAGIC_SMOKE "20250309")
set(PROJECT_VERSION "0.3.1-BETA.${MAGIC_SMOKE}") # Must be the same as in config.h

### Want a cookie?
Expand Down Expand Up @@ -209,17 +209,21 @@ message("Compile time flags: ${COMPILE_OPTS}")

### Executable names
list(APPEND FILENAMES
${PROJECT_FILENAME}
${PROJECT_FILENAME}-v1.0
${PROJECT_FILENAME}-v1.3
)
list(APPEND PICOTYPES
"LED"
"LED_AUDIOSWITCH"
)
if(${PICO_BOARD} STREQUAL "pico")
list(APPEND FILENAMES
${PROJECT_FILENAME}-rgb
${PROJECT_FILENAME}-v1.0-rgb
${PROJECT_FILENAME}-v1.3-rgb
)
list(APPEND PICOTYPES
"RGB"
"RGB_AUDIOSWITCH"
)
endif()

Expand Down Expand Up @@ -335,9 +339,13 @@ foreach(FILENAME PICOTYPE IN ZIP_LISTS FILENAMES PICOTYPES)
message(STATUS "Building ${FILENAME} with PICOTYPE=${PICOTYPE} and MAGIC_SMOKE=${MAGIC_SMOKE}")
# executable
add_executable(${BUILD} ${SOURCEFILES})
# RGB LED
if(PICOTYPE STREQUAL "RGB")
# BUILD TYPES
if(PICOTYPE STREQUAL "RGB" OR PICOTYPE STREQUAL "RGB_AUDIOSWITCH")
message("Building ")
target_compile_definitions(${BUILD} PRIVATE USBSID USE_RGB=1)
target_compile_definitions(${BUILD} PRIVATE USBSID HAS_AUDIOSWITCH=1)
elseif(PICOTYPE STREQUAL "LED_AUDIOSWITCH")
target_compile_definitions(${BUILD} PRIVATE USBSID HAS_AUDIOSWITCH=1)
else()
target_compile_definitions(${BUILD} PRIVATE USBSID)
endif()
Expand Down
76 changes: 59 additions & 17 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ extern void enable_sid(bool unmute);
extern void disable_sid(void);
extern void mute_sid(void);
extern void reset_sid_registers(void);
extern void toggle_audio_switch(void);
extern void set_audio_switch(bool state);

/* Midi externals */
extern void midi_bus_operation(uint8_t a, uint8_t b);
Expand Down Expand Up @@ -114,6 +116,7 @@ const char *int_ext[2] = { "Internal", "External" };
const char *enabled[2] = { "Disabled", "Enabled" };
const char *true_false[2] = { "False", "True" };
const char *single_dual[2] = { "Dual SID", "Single SID" };
const char *mono_stereo[2] = { "Mono", "Stereo" };

#define USBSID_DEFAULT_CONFIG_INIT { \
.magic = MAGIC_SMOKE, \
Expand All @@ -122,6 +125,7 @@ const char *single_dual[2] = { "Dual SID", "Single SID" };
.clock_rate = DEFAULT, \
.raster_rate = R_DEFAULT, \
.lock_clockrate = false, \
.stereo_en = STEREO_ENABLED, \
.socketOne = { \
.enabled = true, \
.dualsid = false, \
Expand Down Expand Up @@ -323,6 +327,7 @@ void read_config(Config* config)
config_array[54] = (int)config->Midi.enabled;
config_array[55] = (int)config->FMOpl.enabled;
config_array[56] = config->FMOpl.sidno;
config_array[57] = config->stereo_en;
config_array[63] = 0xFF; /* Terminator byte */

return;
Expand Down Expand Up @@ -505,15 +510,15 @@ void handle_config_request(uint8_t * buffer)
case SET_CONFIG:
CFG("[CMD] SET_CONFIG\n");
switch (buffer[1]) {
case 0: /* clock_rate */
case 0: /* clock_rate */
/* will always be available to change the setting since it doesn't apply it */
usbsid_config.clock_rate = clockrates[(int)buffer[2]];
usbsid_config.raster_rate = rasterrates[(int)buffer[2]]; /* Experimental */
if (buffer[3] == 0 || buffer[3] == 1) { /* Verify correct data */
usbsid_config.lock_clockrate = (bool)buffer[3];
}
break;
case 1: /* socketOne */
case 1: /* socketOne */
switch (buffer[2]) {
case 0: /* enabled */
if (buffer[3] <= 1) { /* 1 or 0 */
Expand Down Expand Up @@ -547,7 +552,7 @@ void handle_config_request(uint8_t * buffer)
break;
};
break;
case 2: /* socketTwo */
case 2: /* socketTwo */
switch (buffer[2]) {
case 0: /* enabled */
if (buffer[3] <= 1) { /* 1 or 0 */
Expand Down Expand Up @@ -586,7 +591,7 @@ void handle_config_request(uint8_t * buffer)
break;
};
break;
case 3: /* LED */
case 3: /* LED */
switch (buffer[2]) {
case 0: /* enabled */
if (buffer[3] <= 1) { /* 1 or 0 */
Expand All @@ -606,7 +611,7 @@ void handle_config_request(uint8_t * buffer)
break;
};
break;
case 4: /* RGBLED */
case 4: /* RGBLED */
switch (buffer[2]) {
case 0: /* enabled */
if (buffer[3] <= 1) { /* 1 or 0 */
Expand Down Expand Up @@ -646,15 +651,25 @@ void handle_config_request(uint8_t * buffer)
break;
}
break;
case 5: /* CDC */
case 6: /* WEBUSB */
case 7: /* ASID */
case 8: /* MIDI */
case 5: /* CDC */
case 6: /* WEBUSB */
case 7: /* ASID */
case 8: /* MIDI */
break;
case 9: /* FMOpl */
case 9: /* FMOpl */
usbsid_config.FMOpl.enabled = (bool)buffer[2];
usbsid_config.FMOpl.sidno = verify_fmopl_sidno();
break;
case 10: /* Audio switch */
#if defined(HAS_AUDIOSWITCH)
usbsid_config.stereo_en =
(buffer[2] == 0 || buffer[2] == 1)
? (bool)buffer[2]
: true; /* Default to 1 ~ stereo if incorrect value */
#else
usbsid_config.stereo_en = false;
#endif
break;
default:
break;
};
Expand Down Expand Up @@ -714,7 +729,7 @@ void handle_config_request(uint8_t * buffer)
CFG("[CMD] TRIPLE_SID SOCKET 2\n");
set_socket_config(buffer[1], true, false, usbsid_config.socketOne.chiptype, true, true, 1, false);
break;
case LOAD_MIDI_STATE: /* Load from config into midimachine and apply to SIDs */
case LOAD_MIDI_STATE: /* Load from config into midimachine and apply to SIDs */
CFG("[LOAD_MIDI_STATE]\n");
for (int i = 0; i < 4; i++) {
CFG("[SID %d]", (i + 1));
Expand All @@ -728,7 +743,7 @@ void handle_config_request(uint8_t * buffer)
CFG("\n");
}
break;
case SAVE_MIDI_STATE: /* Save from midimachine into config and save to flash */
case SAVE_MIDI_STATE: /* Save from midimachine into config and save to flash */
CFG("[SAVE_MIDI_STATE]\n");
for (int i = 0; i < 4; i++) {
CFG("[SID %d]", (i + 1));
Expand All @@ -741,7 +756,7 @@ void handle_config_request(uint8_t * buffer)
}
save_config(&usbsid_config);
break;
case RESET_MIDI_STATE: /* Reset all settings to zero */
case RESET_MIDI_STATE: /* Reset all settings to zero */
CFG("[RESET_MIDI_STATE]\n");
for (int i = 0; i < 4; i++) {
CFG("[SID %d]", (i + 1));
Expand All @@ -756,20 +771,20 @@ void handle_config_request(uint8_t * buffer)
save_config(&usbsid_config);
/* mcu_reset(); */
break;
case SET_CLOCK: /* Change SID clock frequency by array id */
case SET_CLOCK: /* Change SID clock frequency by array id */
CFG("[CMD] SET_CLOCK\n");
/* locked clockrate check is done in apply_clockrate */
bool suspend_sids = (buffer[2] == 1) ? true : false; /* Set RES low while changing clock? */
apply_clockrate((int)buffer[1], suspend_sids);
break;
case GET_CLOCK: /* Returns the clockrate as array id in byte 0 */
case GET_CLOCK: /* Returns the clockrate as array id in byte 0 */
CFG("[CMD] GET_CLOCK\n");
int clk_rate_id = return_clockrate();
memset(write_buffer_p, 0, 64);
write_buffer_p[0] = clk_rate_id;
write_back_data(1);
break;
case LOCK_CLOCK: /* Locks the clockrate from being changed, saved in config */
case LOCK_CLOCK: /* Locks the clockrate from being changed, saved in config */
CFG("[CMD] LOCK_CLOCK\n");
if (buffer[1] == 0 || buffer[1] == 1) { /* Verify correct data */
usbsid_config.lock_clockrate = (bool)buffer[1];
Expand All @@ -783,7 +798,29 @@ void handle_config_request(uint8_t * buffer)
apply_config(false);
}
break;
case DETECT_SIDS:
case TOGGLE_AUDIO: /* Toggle the audio state regardless of config setting */
CFG("[CMD] TOGGLE_AUDIO\n");
toggle_audio_switch(); /* if HAS_AUDIOSWITCH is not defined, this doesn't do anything */
break;
case SET_AUDIO: /* Set the audio state from buffer setting (saves config if provided) */
CFG("[CMD] SET_AUDIO\n");
#if defined(HAS_AUDIOSWITCH)
usbsid_config.stereo_en =
(buffer[1] == 0 || buffer[1] == 1)
? (bool)buffer[1]
: true; /* Default to 1 ~ stereo if incorrect value */
set_audio_switch(usbsid_config.stereo_en);
if (buffer[2] == 1) { /* Save and apply if set to a 1 */
CFG("[SET_AUDIO] SAVE_CONFIG\n");
save_config(&usbsid_config);
load_config(&usbsid_config);
apply_config(false);
}
#else
usbsid_config.stereo_en = false;
#endif
break;
case DETECT_SIDS: /* Detect SID types per socket */
CFG("[CMD] DETECT_SIDS\n");
detect_sid_types();
memset(write_buffer_p, 0 ,64); /* Empty the write buffer pointer */
Expand Down Expand Up @@ -972,6 +1009,11 @@ void print_config_settings(void)
CFG("[CONFIG] [FMOpl] SIDno %d\n",
usbsid_config.FMOpl.sidno);

#if defined(HAS_AUDIOSWITCH)
CFG("[CONFIG] [AUDIO_SWITCH] %s\n",
mono_stereo[(int)usbsid_config.stereo_en]);
#endif

CFG("[CONFIG] PRINT SETTINGS END\n");

return;
Expand Down
22 changes: 15 additions & 7 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
#define MAGIC_SMOKE 19700101 /* DATEOFRELEASE */
#endif
#ifndef PROJECT_VERSION
#define PROJECT_VERSION "0.3.1-BETA.20250227" /* Must be the same as in CMakeLists.txt */
#define PROJECT_VERSION "0.3.1-BETA.20250309" /* Must be the same as in CMakeLists.txt */
#endif

#ifdef PICO_DEFAULT_LED_PIN
Expand All @@ -93,6 +93,11 @@
#else
#define RGB_ENABLED false
#endif
#ifdef HAS_AUDIOSWITCH
#define STEREO_ENABLED true
#else
#define STEREO_ENABLED false
#endif

/* USBSID-Pico config struct */
typedef struct Config {
Expand Down Expand Up @@ -145,8 +150,9 @@ typedef struct Config {
int sidno; /* 0 = disabled, saves the sidno of the sid set to FMOpl */
bool enabled : 1; /* Requires a clone SID! */
} FMOpl; /* 9 */
bool external_clock : 1; /* enable / disable external oscillator */
bool lock_clockrate : 1; /* lock the set clockspeed from being changed */
bool external_clock : 1; /* enable / disable external oscillator */
bool lock_clockrate : 1; /* lock the set clockspeed from being changed */
bool stereo_en : 1; /* audio switch is off (mono) or on (stereo) ~ (PCB v1.3+ only) */
} Config;

extern Config usbsid_config; /* Make Config struct global */
Expand Down Expand Up @@ -192,11 +198,13 @@ enum
SAVE_MIDI_STATE = 0x61,
RESET_MIDI_STATE = 0x63,

USBSID_VERSION = 0x80,
USBSID_VERSION = 0x80, /* Read version identifier as uint32_t */

RESTART_BUS = 0x85,
RESTART_BUS_CLK = 0x86,
SYNC_PIOS = 0x87,
RESTART_BUS = 0x85, /* Restart DMA & PIO */
RESTART_BUS_CLK = 0x86, /* Restart PIO clocks */
SYNC_PIOS = 0x87, /* Sync PIO clocks */
TOGGLE_AUDIO = 0x88, /* Toggle mono <-> stereo (v1.2+ boards only) */
SET_AUDIO = 0x89, /* Set mono <-> stereo (v1.2+ boards only) */

TEST_FN = 0x99, /* TODO: Remove before v1 release */
};
Expand Down
26 changes: 26 additions & 0 deletions src/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ void init_gpio(void)
gpio_set_dir(CS1, GPIO_OUT);
gpio_set_dir(CS2, GPIO_OUT);
gpio_set_dir(RW, GPIO_OUT);
/* GPIO defaults for audio switch */
#if defined(HAS_AUDIOSWITCH)
gpio_set_dir(AU_SW, GPIO_OUT);
gpio_set_function(AU_SW, GPIO_FUNC_SIO);
gpio_put(AU_SW, usbsid_config.stereo_en); /* Default on ~ stereo */
#endif
return;
}

Expand Down Expand Up @@ -626,3 +632,23 @@ void reset_sid_registers(void)
}
return;
}

void toggle_audio_switch(void)
{ /* Toggle the SPST switch stereo <-> mono */
#if defined(HAS_AUDIOSWITCH)
static int audio_state = 0b1;
audio_state ^= 1;
CFG("[CONFIG] AUDIO SWITCH TO: %d\n", audio_state);
tPIN(AU_SW);
// gpio_put(AU_SW, audio_state); /* toggle mono <-> stereo */
#endif
}

void set_audio_switch(bool state)
{ /* Set the SPST switch */
#if defined(HAS_AUDIOSWITCH)
if (state) {
sPIN(AU_SW);
} else cPIN(AU_SW);
#endif
}
12 changes: 11 additions & 1 deletion src/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
#define CS2 21 /* Chip Select for 2 or 3 & 4 with SKPico */
#define PHI 22 /* Pico 1Mhz PWM out ~ External Clock In */

/* Audio switch (v1.3+ only) */
#if defined(HAS_AUDIOSWITCH)
#define AU_SW 15
#endif

/* LED */
#if defined(PICO_DEFAULT_LED_PIN)
#define BUILTIN_LED PICO_DEFAULT_LED_PIN /* 25 */
Expand All @@ -103,7 +108,9 @@

/* Unused */
#define NIL0 14
#ifndef HAS_AUDIOSWITCH
#define NIL1 15
#endif
#define NIL2 26
#define NIL3 27
#define NIL4 28
Expand All @@ -112,7 +119,10 @@
#define PIO_PINDIRMASK 0x3C3FFF /* 0b00000000001111000011111111111111 18 GPIO pins */

/* Util */
#define bPIN(i) ( 1 << i )
#define bPIN(P) (1 << P)
#define sPIN(P) { sio_hw->gpio_set = (1 << P); }
#define cPIN(P) { sio_hw->gpio_clr = (1 << P); }
#define tPIN(P) { sio_hw->gpio_togl = (1 << P); }

/* IRQ's */
#define PIO_IRQ0 0
Expand Down

0 comments on commit 98726dc

Please sign in to comment.