Skip to content

Commit

Permalink
Added USB serial and flash storage of settings
Browse files Browse the repository at this point in the history
NOTE: needs latest Pico SDK!
  • Loading branch information
terjeio committed Mar 6, 2021
1 parent f68227a commit e20d5c4
Show file tree
Hide file tree
Showing 10 changed files with 663 additions and 45 deletions.
18 changes: 15 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_executable(grblHAL
main.c
driver.c
serial.c
flash.c
i2c.c
PCA9654E.c
eeprom/eeprom_24AAxxx.c
Expand All @@ -35,15 +36,26 @@ add_executable(grblHAL
grbl/system.c
grbl/tool_change.c
grbl/my_plugin.c
usb_serial.c
stdio_usb_descriptors.c
)

pico_generate_pio_header(grblHAL ${CMAKE_CURRENT_LIST_DIR}/driverPIO.pio)

target_sources(grblHAL PRIVATE driver.c)
target_compile_definitions(grblHAL PUBLIC XRP2040)

# Pull in our pico_stdlib which pulls in commonly used features
target_link_libraries(grblHAL PRIVATE pico_stdlib hardware_uart hardware_pio hardware_i2c hardware_gpio hardware_pwm hardware_clocks)
target_include_directories(grblHAL PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(grblHAL PRIVATE
tinyusb_device_unmarked
pico_stdlib
hardware_uart
hardware_pio
hardware_i2c
hardware_gpio
hardware_pwm
hardware_clocks
hardware_flash
)

# create map/bin/hex file etc.
pico_add_extra_outputs(grblHAL)
70 changes: 33 additions & 37 deletions driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ typedef union {
} pio_steps_t;

static pio_steps_t pio_steps = { .delay = 20, .length = 100 };
uint32_t pirq = 0;
static uint pulse, timer;
static uint16_t pulse_length, pulse_delay;
static bool pwmEnabled = false, IOInitDone = false;
static axes_signals_t next_step_outbits;
static spindle_pwm_t spindle_pwm;
static status_code_t (*on_unknown_sys_command)(uint_fast16_t state, char *line, char *lcline);
static debounce_t debounce;
static volatile uint32_t elapsed_ticks = 0;
static volatile bool ms_event = false;
static probe_state_t probe; /* = {
.connected = On
}; */
Expand Down Expand Up @@ -612,32 +613,32 @@ static coolant_state_t coolantGetState (void)
// Helper functions for setting/clearing/inverting individual bits atomically (uninterruptable)
static void bitsSetAtomic (volatile uint_fast16_t *ptr, uint_fast16_t bits)
{
// __disable_irq();
__disable_irq();
*ptr |= bits;
// __enable_irq();
__enable_irq();
}

static uint_fast16_t bitsClearAtomic (volatile uint_fast16_t *ptr, uint_fast16_t bits)
{
// __disable_irq();
__disable_irq();
uint_fast16_t prev = *ptr;
*ptr &= ~bits;
// __enable_irq();
__enable_irq();
return prev;
}

static uint_fast16_t valueSetAtomic (volatile uint_fast16_t *ptr, uint_fast16_t value)
{
// __disable_irq();
__disable_irq();
uint_fast16_t prev = *ptr;
*ptr = value;
// __enable_irq();
__enable_irq();
return prev;
}

static uint32_t getElapsedTicks (void)
{
return 0; //uwTick;
return elapsed_ticks;
}

#if MPG_MODE_ENABLE
Expand Down Expand Up @@ -985,6 +986,16 @@ static bool driver_setup (settings_t *settings)
return IOInitDone;
}

#if USB_SERIAL_CDC
static void execute_realtime (uint_fast16_t state)
{
if(ms_event) {
ms_event = false;
usb_execute_realtime(state);
}
}
#endif

// Initialize HAL pointers, setup serial comms and enable EEPROM
// NOTE: Grbl is not yet configured (from EEPROM data), driver_setup() will be called when done

Expand All @@ -994,14 +1005,12 @@ bool driver_init (void)

// irq_set_exclusive_handler(-1, systick_handler);



//mpu_hw->rvr = 999;
//mpu_hw->csr = M0PLUS_SYST_CSR_TICKINT_BITS|M0PLUS_SYST_CSR_ENABLE_BITS;
// M0PLUS_SYST_CSR_CLKSOURCE_BITS - set to use processor clock
systick_hw->rvr = 999;
systick_hw->cvr = 0;
systick_hw->csr = M0PLUS_SYST_CSR_TICKINT_BITS|M0PLUS_SYST_CSR_ENABLE_BITS;

#if USB_SERIAL_CDC
usbInit();
usb_serialInit();
#else
serialInit(115200);
#endif
Expand Down Expand Up @@ -1062,13 +1071,14 @@ bool driver_init (void)
hal.set_value_atomic = valueSetAtomic;

#if USB_SERIAL_CDC
hal.stream.read = usbGetC;
hal.stream.write = usbWriteS;
hal.stream.write_all = usbWriteS;
hal.stream.get_rx_buffer_available = usbRxFree;
hal.stream.reset_read_buffer = usbRxFlush;
hal.stream.cancel_read_buffer = usbRxCancel;
hal.stream.suspend_read = usbSuspendInput;
hal.stream.read = usb_serialGetC;
hal.stream.write = usb_serialWriteS;
hal.stream.write_all = usb_serialWriteS;
hal.stream.get_rx_buffer_available = usb_serialRxFree;
hal.stream.reset_read_buffer = usb_serialRxFlush;
hal.stream.cancel_read_buffer = usb_serialRxCancel;
hal.stream.suspend_read = usb_serialSuspendInput;
grbl.on_execute_realtime = execute_realtime;
#else
hal.stream.read = serialGetC;
hal.stream.write = serialWriteS;
Expand Down Expand Up @@ -1165,9 +1175,6 @@ bool driver_init (void)
// Main stepper driver
void STEPPER_TIMER_IRQHandler (void)
{
//irq_clear(PIO1_IRQ_0);

pirq = pio1->irq;
stepper_timer_irq_clear(pio1);

hal.stepper.interrupt_callback();
Expand Down Expand Up @@ -1258,10 +1265,8 @@ static void gpio_int_handler (uint gpio, uint32_t events)
// Interrupt handler for 1 ms interval timer
void isr_systick (void)
{
static uint32_t cnt = 0;

cnt++;
/*
ms_event = true;
elapsed_ticks++;

#if SDCARD_ENABLE
static uint32_t fatfs_ticks = 10;
Expand All @@ -1270,13 +1275,4 @@ void isr_systick (void)
fatfs_ticks = 10;
}
#endif
uwTick += uwTickFreq;
if(delay.ms && !(--delay.ms)) {
if(delay.callback) {
delay.callback();
delay.callback = NULL;
}
}
*/
}
28 changes: 26 additions & 2 deletions driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#define ODOMETER_ENABLE 0
#endif
#ifndef PPI_ENABLE
#define PPI_ENABLE 0
#define PPI_ENABLE 0
#endif
#ifndef EEPROM_ENABLE
#define EEPROM_ENABLE 0
Expand Down Expand Up @@ -136,7 +136,7 @@
// End configuration

#if EEPROM_ENABLE == 0
#define FLASH_ENABLE 0
#define FLASH_ENABLE 1
#else
#define FLASH_ENABLE 0
#endif
Expand Down Expand Up @@ -168,4 +168,28 @@

bool driver_init (void);

/**
\brief Enable IRQ Interrupts
\details Enables IRQ interrupts by clearing the I-bit in the CPSR.
Can only be executed in Privileged modes.
*/

// While waiting for CMSIS headers...:

static inline void __enable_irq(void)
{
__asm volatile ("cpsie i" : : : "memory");
}

/**
\brief Disable IRQ Interrupts
\details Disables IRQ interrupts by setting the I-bit in the CPSR.
Can only be executed in Privileged modes.
*/
static inline void __disable_irq(void)
{
__asm volatile ("cpsid i" : : : "memory");
}


#endif // __DRIVER_H__
61 changes: 61 additions & 0 deletions flash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
flash.c - driver code for RP2040 ARM processor
Part of grblHAL
Copyright (c) 2021 Terje Io
This code reads/writes the whole RAM-based emulated EPROM contents from/to flash
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/

/*
IMPORTANT! If both cores are in use synchronization has to be added since flash cannot be read during programming
See 4.1.8. hardware_flash in the SDK documentation.
*/

#include <string.h>

#include "hardware/flash.h"
#include "hardware/regs/addressmap.h"

#include "driver.h"

#define FLASH_TARGET_OFFSET (1024 * 512)

static const uint8_t *flash_target = (const uint8_t *)(XIP_BASE + FLASH_TARGET_OFFSET); // Last page start adress

bool memcpy_from_flash (uint8_t *dest)
{
memcpy(dest, flash_target, hal.nvs.size);

return true;
}

bool memcpy_to_flash (uint8_t *source)
{
if (!memcmp(source, flash_target, hal.nvs.size))
return true;

__disable_irq();
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, source, FLASH_PAGE_SIZE * (hal.nvs.size / FLASH_PAGE_SIZE + (hal.nvs.size % FLASH_PAGE_SIZE ? 1 :0)));
__enable_irq();

return !memcmp(source, flash_target, hal.nvs.size);
}
30 changes: 30 additions & 0 deletions flash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
flash.h - driver code for RP2040 ARM processor
Part of grblHAL
Copyright (c) 2021 Terje Io
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _flash_h_
#define _flash_h_

bool memcpy_from_flash (uint8_t *dest);
bool memcpy_to_flash (uint8_t *source);

#endif
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@

int main (void)
{
grbl_enter();
}
grbl_enter();
}
2 changes: 1 addition & 1 deletion my_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// Configuration
// Uncomment to enable.

//#define USB_SERIAL_CDC 1 // Serial communication via native USB.
#define USB_SERIAL_CDC 1 // Serial communication via native USB.
//#define SDCARD_ENABLE 1 // Run gcode programs from SD card, requires sdcard plugin.
//#define KEYPAD_ENABLE 1 // I2C keypad for jogging etc., requires keypad plugin.
//#define ODOMETER_ENABLE 1 // Odometer plugin.
Expand Down
Loading

0 comments on commit e20d5c4

Please sign in to comment.