Skip to content

Commit

Permalink
Important: switched to v1.5.0 of the SDK.
Browse files Browse the repository at this point in the history
Updated for spindle handling refactoring.
Added MQTT client API option and added wifi settings for country id and AP BSSID (MAC).
  • Loading branch information
terjeio committed Feb 13, 2023
1 parent 563c490 commit 845d7b4
Show file tree
Hide file tree
Showing 6 changed files with 685 additions and 252 deletions.
16 changes: 12 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ cmake_minimum_required(VERSION 3.13)

option(ADD_HPGL "Add HPGL plugin" OFF)
option(ADD_WIFI "Add WiFi networking" OFF)
option(ADD_mDNS "Add mDNS service" OFF) # Set LWIP_DIR below to the root path of lwIP sources when ON!
# Set LWIP_DIR below to the root path of lwIP sources when the following is set ON!
option(ADD_mDNS "Add mDNS service" OFF)
option(ADD_MQTT "Add MQTT client API" OFF)

if(ADD_WIFI)
set(PICO_BOARD pico_w)
Expand All @@ -12,8 +14,8 @@ include(pico_sdk_import.cmake)
if(ADD_WIFI)
include(networking/CMakeLists.txt)
include(webui/CMakeLists.txt)
if(ADD_mDNS)
set(LWIP_DIR /home/pi/pico/pico-sdk/lib/lwip) # LWIP_DIR
if(ADD_mDNS OR ADD_MQTT)
set(LWIP_DIR /home/pi/pico/pico-sdk/lib/lwip) # LWIP_DIRzz
include(${LWIP_DIR}/src/Filelists.cmake)
endif()
endif()
Expand Down Expand Up @@ -63,7 +65,7 @@ target_compile_definitions(grblHAL PUBLIC LITTLEFS_ENABLE=1)

if(ADD_WIFI)
target_compile_definitions(grblHAL PUBLIC WIFI_ENABLE=1)
target_compile_definitions(grblHAL PUBLIC NDEBUG=0)
target_compile_definitions(grblHAL PUBLIC NDEBUG)
target_compile_definitions(grblHAL PUBLIC WEBUI_AUTO_REPORT_INTERVAL=0)
target_sources(grblHAL PRIVATE
wifi.c
Expand All @@ -84,6 +86,12 @@ target_sources(grblHAL PRIVATE
${lwipmdns_SRCS}
)
endif()
if(ADD_MQTT)
target_compile_definitions(grblHAL PUBLIC MQTT_ENABLE=1)
target_sources(grblHAL PRIVATE
${lwipmqtt_SRCS}
)
endif()
endif()

if(ADD_HPGL)
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

A grblHAL driver for the Raspberry Pi Pico RP2040 processor on a [Pi Pico or Pi Pico W board](https://www.raspberrypi.org/products/raspberry-pi-pico/).

__Important__ download information can be found [here](https://github.com/grblHAL/core/wiki/Compiling-grblHAL).
__Important__ download information can be found [here](https://github.com/grblHAL/core/wiki/Compiling-grblHAL).
The project has been updated to/now uses [SDK version 1.5.0](https://github.com/raspberrypi/pico-sdk/releases).

The default build environment is Visual Studio Code, it is surprisingly easy to set up on Raspberry Pi - see the [Getting started](https://datasheets.raspberrypi.org/pico/getting-started-with-pico.pdf) documentation for how to for this and other platforms.

__Note:__ Before enabling mDNS and/or SSDP protocols for Pico W buggy code in the SDK has to be removed.
The broken code is in [cyw43_lwip.c](https://github.com/georgerobotics/cyw43-driver/blob/195dfcc10bb6f379e3dea45147590db2203d3c7b/src/cyw43_lwip.c#L176-L184), either remove this part of the code or inactivate it by adding a `x` in front of `LWIP_MDNS_RESPONDER` in line 176.
There are two issues with this code: first is that it is for a newer version of lwIP than is supplied with the SDK, second is that mDNS is enabled with a default hostname - not the run-time configurable hostname in from grblHAL.

---
2022-10-28
2023-02-13
76 changes: 38 additions & 38 deletions driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,13 @@ typedef union {
};
} pio_steps_t;

#if (!VFD_SPINDLE || N_SPINDLE > 1) && defined(SPINDLE_PORT)
#if DRIVER_SPINDLE_ENABLE && defined(SPINDLE_PORT)

#define DRIVER_SPINDLE

#if defined(SPINDLE_PWM_PIN)
static bool pwmEnabled = false;
static spindle_id_t spindle_id = -1;
static spindle_pwm_t spindle_pwm;
static void spindle_set_speed (uint_fast16_t pwm_value);
#endif
Expand Down Expand Up @@ -1161,45 +1162,41 @@ static void spindleSetStateVariable (spindle_state_t state, float rpm)
spindle_set_speed(state.on ? spindle_compute_pwm_value(&spindle_pwm, rpm, false) : spindle_pwm.off_value);
}

bool spindleConfig (void)
bool spindleConfig (spindle_ptrs_t *spindle)
{
static spindle_settings_t spindle = {0};
if(spindle == NULL)
return false;

if(hal.spindle.rpm_max > 0.0f && memcmp(&spindle, &settings.spindle, sizeof(spindle_settings_t))) {
uint32_t prescaler = settings.spindle.pwm_freq > 2000.0f ? 1 : (settings.spindle.pwm_freq > 200.0f ? 12 : 50);

uint32_t prescaler = settings.spindle.pwm_freq > 2000.0f ? 1 : (settings.spindle.pwm_freq > 200.0f ? 12 : 50);
if((spindle->cap.variable = !settings.spindle.flags.pwm_disable && spindle_precompute_pwm_values(spindle, &spindle_pwm, clock_get_hz(clk_sys) / prescaler))) {

memcpy(&spindle, &settings.spindle, sizeof(spindle_settings_t));
spindle->set_state = spindleSetStateVariable;

if((hal.spindle.cap.variable = !settings.spindle.flags.pwm_disable && spindle_precompute_pwm_values(&spindle_pwm, clock_get_hz(clk_sys) / prescaler))) {
// Get the default config for
pwm_config config = pwm_get_default_config();

hal.spindle.set_state = spindleSetStateVariable;
// Set divider, not using the 4 fractional bit part of the clock divider, only the integer part
pwm_config_set_clkdiv_int(&config, prescaler);
// Set the top value of the PWM => the period
pwm_config_set_wrap(&config, spindle_pwm.period);
// Set the off value of the PWM => off duty cycle (either 0 or the off value)
pwm_set_gpio_level(SPINDLE_PWM_PIN, spindle_pwm.off_value);

// Get the default config for
pwm_config config = pwm_get_default_config();
// Set polarity of the channel
uint channel = pwm_gpio_to_channel(SPINDLE_PWM_PIN); // Get which is associated with the PWM pin
pwm_config_set_output_polarity(&config, (!channel & settings.spindle.invert.pwm), (channel & settings.spindle.invert.pwm)); // Set the polarity of the pin's channel

// Set divider, not using the 4 fractional bit part of the clock divider, only the integer part
pwm_config_set_clkdiv_int(&config, prescaler);
// Set the top value of the PWM => the period
pwm_config_set_wrap(&config, spindle_pwm.period);
// Set the off value of the PWM => off duty cycle (either 0 or the off value)
pwm_set_gpio_level(SPINDLE_PWM_PIN, spindle_pwm.off_value);

// Set polarity of the channel
uint channel = pwm_gpio_to_channel(SPINDLE_PWM_PIN); // Get which is associated with the PWM pin
pwm_config_set_output_polarity(&config, (!channel & settings.spindle.invert.pwm), (channel & settings.spindle.invert.pwm)); // Set the polarity of the pin's channel
// Load the configuration into our PWM slice, and set it running.
pwm_init(pwm_gpio_to_slice_num(SPINDLE_PWM_PIN), &config, true);

// Load the configuration into our PWM slice, and set it running.
pwm_init(pwm_gpio_to_slice_num(SPINDLE_PWM_PIN), &config, true);

} else {
if(pwmEnabled)
hal.spindle.set_state((spindle_state_t){0}, 0.0f);
hal.spindle.set_state = spindleSetState;
}
} else {
if(pwmEnabled)
spindle->set_state((spindle_state_t){0}, 0.0f);
spindle->set_state = spindleSetState;
}
spindle_update_caps(hal.spindle.cap.variable ? &spindle_pwm : NULL);

spindle_update_caps(spindle, spindle->cap.variable ? &spindle_pwm : NULL);

return true;
}
Expand Down Expand Up @@ -1412,7 +1409,7 @@ void pinEnableIRQ (const input_signal_t *input, pin_irq_mode_t irq_mode)
}

// Configures peripherals when settings are initialized or changed
void settings_changed (settings_t *settings)
void settings_changed (settings_t *settings, settings_changed_flags_t changed)
{
#if USE_STEPDIR_MAP
stepdirmap_init(settings);
Expand All @@ -1427,8 +1424,11 @@ void settings_changed (settings_t *settings)
#endif

#ifdef SPINDLE_PWM_PIN
if(hal.spindle.config == spindleConfig)
spindleConfig();
if(changed.spindle) {
spindleConfig(spindle_get_hal(spindle_id, SpindleHAL_Configured));
if(spindle_id == spindle_get_default())
spindle_select(spindle_id);
}
#endif

#if SD_SHIFT_REGISTER
Expand Down Expand Up @@ -1871,7 +1871,7 @@ static bool driver_setup (settings_t *settings)

IOInitDone = settings->version == 22;

hal.settings_changed(settings);
hal.settings_changed(settings, (settings_changed_flags_t){0});
stepperSetDirOutputs((axes_signals_t){0});

#if PPI_ENABLE
Expand Down Expand Up @@ -1938,7 +1938,7 @@ bool driver_init (void)
systick_hw->csr = M0PLUS_SYST_CSR_TICKINT_BITS|M0PLUS_SYST_CSR_ENABLE_BITS;

hal.info = "RP2040";
hal.driver_version = "230125";
hal.driver_version = "230205";
hal.driver_options = "SDK_" PICO_SDK_VERSION_STRING;
hal.driver_url = GRBL_URL "/RP2040";
#ifdef BOARD_NAME
Expand Down Expand Up @@ -2032,17 +2032,17 @@ bool driver_init (void)
#else
.type = SpindleType_Basic,
#endif
#ifdef SPINDLE_DIRECTION_PIN
#if defined(SPINDLE_DIRECTION_PIN) || SPINDLE_PORT == GPIO_SR16 || SPINDLE_PORT == GPIO_IOEXPAND
.cap.direction = On,
#endif
.set_state = spindleSetState,
.get_state = spindleGetState
};

#ifdef SPINDLE_PWM_PIN
spindle_register(&spindle, "PWM");
spindle_id = spindle_register(&spindle, "PWM");
#else
spindle_register(&spindle, "Basic");
spindle_id = spindle_register(&spindle, "Basic");
#endif

#endif // DRIVER_SPINDLE
Expand Down
1 change: 1 addition & 0 deletions lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define LWIP_ETHERNET 1
#define LWIP_ICMP 1
#define LWIP_RAW 1
#define LWIP_IGMP 1
#define TCP_WND (8 * TCP_MSS)
#define TCP_MSS 1460
#define TCP_SND_BUF (8 * TCP_MSS)
Expand Down
Loading

0 comments on commit 845d7b4

Please sign in to comment.