grblHAL / RP2040

grblHAL driver for RP2040 (Pi Pico)
Other
103 stars 44 forks source link

Pico Zero #64

Closed Juank23 closed 1 year ago

Juank23 commented 1 year ago

Good morning, is there any way to flash the RGB led that has the pi zero , that indicates when it is working, I remain attentive RP2040-Zero-details-7

terjeio commented 1 year ago

You may add a plugin for that - here is how I did it for a STM32F411 board. The WS2812 is a bit harder to interface to as it has a serial interface...

Juank23 commented 1 year ago

Good morning, friend add those lines and edit the pin but at the time of compiling gives many errors, then I send you an image thank you BLINK

terjeio commented 1 year ago

The code that does not compile is STM32F411 specific and for a single LED switched on/off by a logic level. You have to replace that with code suitable for the RP2040 and the type of LED used. You may find suitable code by searching for it.

terjeio commented 1 year ago

I found this, it has a plain C example.

Juank23 commented 1 year ago

good day, friend I'm trying to flash the led of the 2040 zero but it does not work, the blink in the code no longer gives error but in the grblhal does not work, if I do it only the code blin works with any pin and less the rgb qeudo atento. blink

terjeio commented 1 year ago

Oops, I forgot that you have to enable my_plugin.c in the build for this MCU. You can check if the plugin is active by sending $I - it will add itself to the output when it is.

A plain LED will work when switching a GPIO pin on/off but not a WS2812, it uses serial commands with strict timing requirements...

Juank23 commented 1 year ago

I already did what you told me but it doesn't work, can you help me with an example to make it work in your code? BLINK ON

terjeio commented 1 year ago

Something like this:

/*

  my_plugin.c - user defined plugin that blinks the LED on a RP2040

  Part of grblHAL

  Public domain

*/

#include "driver.h"

const uint LED_PIN = 25;

static on_report_options_ptr on_report_options;
static on_execute_realtime_ptr on_execute_realtime;

// Add info about our plugin to the $I report.
static void on_report_my_options (bool newopt)
{
    on_report_options(newopt);

    if(!newopt)
        hal.stream.write("[PLUGIN:Blink LED v1.00]" ASCII_EOL);
}

static void blink_led (sys_state_t state)
{
    static bool led_on = false;
    static uint32_t ms = 0;

    if(hal.get_elapsed_ticks() >= ms) {
        ms = hal.get_elapsed_ticks() + 500; //ms
        led_on = !led_on;
        if(led_on)
            gpio_out(LED_PIN, 1);
        else
            gpio_out(LED_PIN, 0);
    }

    on_execute_realtime(state);
}

void my_plugin_init (void)
{
    // Add info about our plugin to the $I report.
    on_report_options = grbl.on_report_options;
    grbl.on_report_options = on_report_my_options;

    // Add blink LED function to grblHAL foreground process
    on_execute_realtime = grbl.on_execute_realtime;
    grbl.on_execute_realtime = blink_led;

    gpio_init(LED_PIN, 1);
    gpio_set_dir(LED_PIN, GPIO_OUT);
}

Save as _myplugin.c in the same directory as driver.c. This will not work with the WS2812 though. And I have not tested the code above with a plain LED either.

Juank23 commented 1 year ago

friend gives errors, and just modify the line that is OFF and without modifying the plugin also gives error, and does not work, I have another normal pi 2040 that has the led on pin 25 and also does not work ...... ![Uploading BLINK.jpg…]()

terjeio commented 1 year ago

Ok, I have fixed and tested the code for you - this works with a plain LED (with an appropriate series resistor in place):

/*

  my_plugin.c - user defined plugin that blinks the LED on a STM32F411 Blackpill

  Part of grblHAL

  Public domain

*/

#include "driver.h"
#include "hardware/gpio.h"

const uint LED_PIN = 25;

static on_report_options_ptr on_report_options;
static on_execute_realtime_ptr on_execute_realtime;

// Add info about our plugin to the $I report.
static void on_report_my_options (bool newopt)
{
    on_report_options(newopt);

    if(!newopt)
        hal.stream.write("[PLUGIN:Blink LED v1.00]" ASCII_EOL);
}

static void blink_led (sys_state_t state)
{
    static bool led_on = false;
    static uint32_t ms = 0;

    if(hal.get_elapsed_ticks() >= ms) {
        ms = hal.get_elapsed_ticks() + 500; //ms
        led_on = !led_on;
        if(led_on)
            gpio_put(LED_PIN, 1);
        else
            gpio_put(LED_PIN, 0);
    }

    on_execute_realtime(state);
}

void my_plugin_init (void)
{
    // Add info about our plugin to the $I report.
    on_report_options = grbl.on_report_options;
    grbl.on_report_options = on_report_my_options;

    // Add blink LED function to grblHAL foreground process
    on_execute_realtime = grbl.on_execute_realtime;
    grbl.on_execute_realtime = blink_led;

    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);
}

Be aware that if the selected pin is assigned by the board map as an input it will not work as the driver pin configuration takes place after my_plugini_init() is called. If assigned as an output it might work...

Juank23 commented 1 year ago

I put an external led and set it on a pin that is free but still the same error. error

terjeio commented 1 year ago

Save as _myplugin.c in the same directory as driver.c - do not modify the one in the grbl folder.

Juank23 commented 1 year ago

in the grbl folder there is a file with the following name, copy a new file where driver.c is located, my_plugin.c and the one that is in the grbl folder I left it blank but the same error continues, my question is that to activate my plugin is where it says OFF change it to ON or from where do I activate it?

Juank23 commented 1 year ago

Ready my friend, after several attempts and a couple of modifications it works now perfect, thanks for your help....