Skip to content

Commit

Permalink
Add support for PicoW and Pico2
Browse files Browse the repository at this point in the history
  • Loading branch information
LouDnl committed Nov 20, 2024
1 parent b268e88 commit a9684dd
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 28 deletions.
3 changes: 0 additions & 3 deletions src/asid.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
#include <stdlib.h>
#include <stdio.h>

/* Pico libs */
#include "pico/mem_ops.h"


/* SID Register order for ASID */
static const uint8_t asid_sid_registers[] =
Expand Down
43 changes: 31 additions & 12 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ uint8_t one, two, three, four;
const char* project_version = PROJECT_VERSION;
static uint8_t p_version_array[MAX_BUFFER_SIZE];

// static const Config usbsid_default_config = {
#define USBSID_DEFAULT_CONFIG_INIT { \
.magic = MAGIC_SMOKE, \
.default_config = 1, \
Expand All @@ -84,12 +83,12 @@ static uint8_t p_version_array[MAX_BUFFER_SIZE];
}, \
.LED = { \
.enabled = true, \
.idle_breathe = true \
.idle_breathe = LED_PWM \
}, \
.RGBLED = { \
.enabled = RGB_ENABLED, \
.idle_breathe = true, \
.brightness = 0x7F, /* Half of max brightness */ \
.idle_breathe = RGB_ENABLED, \
.brightness = (RGB_ENABLED ? 0x7F : 0), /* Half of max brightness or disabled if no RGB LED */ \
.sid_to_use = 1, \
}, \
.Cdc = { \
Expand Down Expand Up @@ -291,8 +290,13 @@ void handle_config_request(uint8_t * buffer)
usbsid_config.LED.enabled = (buffer[3] == 1) ? true : false;
break;
case 1: /* idle_breathe */
if (buffer[3] <= 1) /* 1 or 0 */
usbsid_config.LED.idle_breathe = (buffer[3] == 1) ? true : false;
if (buffer[3] <= 1) { /* 1 or 0 */
if (LED_PWM) {
usbsid_config.LED.idle_breathe = (buffer[3] == 1) ? true : false;
} else {
usbsid_config.LED.idle_breathe = false; /* Always false, no PWM LED on PicoW :( */
};
};
break;
default:
break;
Expand All @@ -301,19 +305,34 @@ void handle_config_request(uint8_t * buffer)
case 4: /* RGBLED */
switch (buffer[2]) {
case 0: /* enabled */
if (buffer[3] <= 1) /* 1 or 0 */
usbsid_config.RGBLED.enabled = (buffer[3] == 1) ? true : false;
if (buffer[3] <= 1) { /* 1 or 0 */
if (RGB_ENABLED) {
usbsid_config.RGBLED.enabled = (buffer[3] == 1) ? true : false;
} else {
usbsid_config.RGBLED.enabled = false; /* Always false if no RGB LED */
};
};
break;
case 1: /* idle_breathe */
if (buffer[3] <= 1) /* 1 or 0 */
usbsid_config.RGBLED.idle_breathe = (buffer[3] == 1) ? true : false;
if (buffer[3] <= 1) { /* 1 or 0 */
if (RGB_ENABLED) {
usbsid_config.RGBLED.idle_breathe = (buffer[3] == 1) ? true : false;
} else {
usbsid_config.RGBLED.idle_breathe = false; /* Always false if no RGB LED */
};
};
break;
case 2: /* brightness */
usbsid_config.RGBLED.brightness = buffer[3];
if (RGB_ENABLED) {
usbsid_config.RGBLED.brightness = buffer[3];
} else {
usbsid_config.RGBLED.brightness = 0; /* No brightness needed if no RGB LED */
};
break;
case 3: /* sid_to_use */
if (buffer[3] <= 3)
if (buffer[3] <= 3) {
usbsid_config.RGBLED.sid_to_use = buffer[3];
};
break;
default:
break;
Expand Down
9 changes: 7 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include <string.h>

/* Pico libs */
#include "pico/mem_ops.h"
#include "pico/flash.h"
#include "pico/stdlib.h"

Expand All @@ -56,7 +55,13 @@
#define MAGIC_SMOKE 19700101 /* DATEOFRELEASE */
#endif
#ifndef PROJECT_VERSION
#define PROJECT_VERSION "0.2.2-BETA.20241117" /* Must be the same as in CMakeLists.txt */
#define PROJECT_VERSION "0.2.2-BETA.20241119" /* Must be the same as in CMakeLists.txt */
#endif

#ifdef PICO_DEFAULT_LED_PIN
#define LED_PWM true
#else
#define LED_PWM false
#endif
#ifdef USE_RGB
#define RGB_ENABLED true
Expand Down
23 changes: 15 additions & 8 deletions src/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,31 @@ void init_gpio()

void init_vu(void)
{
#if defined(PICO_DEFAULT_LED_PIN) /* Cannot use VU on PicoW :( */
/* PWM led */
gpio_init( BUILTIN_LED );
gpio_init(BUILTIN_LED);
gpio_set_dir(BUILTIN_LED, GPIO_OUT);
gpio_set_function(BUILTIN_LED, GPIO_FUNC_PWM);
/* Init Vu */
int led_pin_slice = pwm_gpio_to_slice_num( BUILTIN_LED );
int led_pin_slice = pwm_gpio_to_slice_num(BUILTIN_LED);
pwm_config configLED = pwm_get_default_config();
pwm_config_set_clkdiv( &configLED, 1 );
pwm_config_set_wrap( &configLED, 65535 ); /* LED max */
pwm_init( led_pin_slice, &configLED, true );
gpio_set_drive_strength( BUILTIN_LED, GPIO_DRIVE_STRENGTH_2MA );
pwm_set_gpio_level( BUILTIN_LED, 0 ); /* turn off led */
pwm_config_set_clkdiv(&configLED, 1);
pwm_config_set_wrap(&configLED, 65535); /* LED max */
pwm_init(led_pin_slice, &configLED, true);
gpio_set_drive_strength(BUILTIN_LED, GPIO_DRIVE_STRENGTH_2MA);
pwm_set_gpio_level(BUILTIN_LED, 0); /* turn off led */

#if defined(USE_RGB)
{ /* Init RGB */
gpio_set_drive_strength( WS2812_PIN, GPIO_DRIVE_STRENGTH_2MA );
gpio_set_drive_strength(WS2812_PIN, GPIO_DRIVE_STRENGTH_2MA);
}
#endif
#elif defined(CYW43_WL_GPIO_LED_PIN)
/* For Pico W devices we need to initialise the driver etc */
cyw43_arch_init();
/* Ask the wifi "driver" to set the GPIO on or off */
cyw43_arch_gpio_put(BUILTIN_LED, usbsid_config.LED.enabled);
#endif
}

void setup_piobus(void)
Expand Down
12 changes: 11 additions & 1 deletion src/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@
/* Pico libs */
#include "pico/stdlib.h"
#include "pico/types.h"
#include "pico/mem_ops.h"

/* Pico W devices use a GPIO on the WIFI chip for the LED,
* so when building for Pico W, CYW43_WL_GPIO_LED_PIN will be defined */
#ifdef CYW43_WL_GPIO_LED_PIN
#include "pico/cyw43_arch.h"
#endif

/* Hardware api's */
#include "hardware/gpio.h"
Expand Down Expand Up @@ -82,7 +87,12 @@
#define PHI 22 /* Pico 1Mhz PWM out ~ External Clock In */

/* LED */
#if defined(PICO_DEFAULT_LED_PIN)
#define BUILTIN_LED PICO_DEFAULT_LED_PIN /* 25 */
#elif defined(CYW43_WL_GPIO_LED_PIN)
#define BUILTIN_LED CYW43_WL_GPIO_LED_PIN /* Warning, No PWM available on LED! */
#endif

#if defined(USE_RGB)
#define WS2812_PIN 23
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#include <math.h>

/* Pico libs */
#include "pico/mem_ops.h"
#include "pico/stdlib.h"


typedef enum {
Expand Down
4 changes: 4 additions & 0 deletions src/usbsid.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ void __not_in_flash_func(handle_buffer_task)(uint8_t * itf, uint32_t * n)
/* It goes up and it goes down */
void led_vumeter_task(void)
{
#if LED_PWM
if (to_ms_since_boot(get_absolute_time()) - start_ms < breathe_interval_ms) {
return; /* not enough time */
}
Expand Down Expand Up @@ -452,11 +453,13 @@ void led_vumeter_task(void)
sid_memory[0x0E], sid_memory[0x0F], sid_memory[0x10], sid_memory[0x11], sid_memory[0x12], sid_memory[0x13], sid_memory[0x14]);

}
#endif
}

/* Mouth breather! */
void led_breathe_task(void)
{
#if LED_PWM
if (usbdata == 0 && dtype == ntype) {
if (to_ms_since_boot(get_absolute_time()) - start_ms < breathe_interval_ms) {
return; /* not enough time */
Expand Down Expand Up @@ -495,6 +498,7 @@ void led_breathe_task(void)
}
#endif
}
#endif
}


Expand Down
1 change: 0 additions & 1 deletion src/usbsid.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
#include "pico/types.h" /* absolute_time_t */
#include "pico/multicore.h" /* Multicore */
#include "pico/sem.h" /* Semaphores */
#include "pico/mem_ops.h" /* Optimized memory handling functions string.h replacement */

/* Hardware api's */
#include "hardware/clocks.h"
Expand Down

0 comments on commit a9684dd

Please sign in to comment.