From ee9ff9faed66c2f83a2c7e38d1b1ab3a2a798cd3 Mon Sep 17 00:00:00 2001 From: Noah Gaeta Date: Thu, 5 Dec 2024 12:24:04 -0500 Subject: [PATCH] feat: new firmware --- grbl/config.h | 7 +++++++ grbl/grbl.h | 4 +++- grbl/limits.c | 14 +++++++++++++ grbl/limits.h | 2 ++ grbl/main.c | 2 ++ grbl/report.c | 6 ++++-- grbl/spi.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ grbl/spi.h | 2 ++ grbl/vcm.c | 11 ++++++++++ grbl/vcm.h | 2 ++ 10 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 grbl/spi.c create mode 100644 grbl/spi.h create mode 100644 grbl/vcm.c create mode 100644 grbl/vcm.h diff --git a/grbl/config.h b/grbl/config.h index 54e099a..b6e8cba 100644 --- a/grbl/config.h +++ b/grbl/config.h @@ -29,6 +29,13 @@ #define config_h #include "grbl.h" // For Arduino IDE compatibility. +enum MachineType { + RCMINI, + BAMBOO +} + +#define MACHINE_TYPE MachineType::BAMBOO + // Define CPU pin map and default settings. // NOTE: OEMs can avoid the need to maintain/update the defaults.h and cpu_map.h files and use only diff --git a/grbl/grbl.h b/grbl/grbl.h index dd8e781..b8a25cb 100644 --- a/grbl/grbl.h +++ b/grbl/grbl.h @@ -22,7 +22,7 @@ #define grbl_h // Grbl versioning system -#define GRBL_VERSION "1.1h" +#define GRBL_VERSION "1.2h" #define GRBL_VERSION_BUILD "20190830" // Define standard libraries used by Grbl. @@ -60,6 +60,8 @@ #include "spindle_control.h" #include "stepper.h" #include "jog.h" +#include "vcm.h" +#include "spi.h" // --------------------------------------------------------------------------------------- // COMPILE-TIME ERROR CHECKING OF DEFINE VALUES: diff --git a/grbl/limits.c b/grbl/limits.c index 1348c14..5cd2df6 100644 --- a/grbl/limits.c +++ b/grbl/limits.c @@ -95,6 +95,20 @@ uint8_t limits_get_state() } +uint8_t pinch_roller_get_state() +{ + if (MACHINE_TYPE === MachineType::BAMBOO) + { + // Check PC3 state + if (PINC & (1 << PINC3)) + { + return 1; + } + } + + return 0; +} + // This is the Limit Pin Change Interrupt, which handles the hard limit feature. A bouncing // limit switch can cause a lot of problems, like false readings and multiple interrupt calls. // If a switch is triggered at all, something bad has happened and treat it as such, regardless diff --git a/grbl/limits.h b/grbl/limits.h index 33fe095..c7b5e15 100644 --- a/grbl/limits.h +++ b/grbl/limits.h @@ -32,6 +32,8 @@ void limits_disable(); // Returns limit state as a bit-wise uint8 variable. uint8_t limits_get_state(); +uint8_t pinch_roller_get_state(); + // Perform one portion of the homing cycle based on the input settings. void limits_go_home(uint8_t cycle_mask); diff --git a/grbl/main.c b/grbl/main.c index 96f2aba..4dc1479 100644 --- a/grbl/main.c +++ b/grbl/main.c @@ -91,6 +91,8 @@ int main(void) coolant_init(); limits_init(); probe_init(); + vcm_init(); + motor_spi_init(); plan_reset(); // Clear block buffer and planner variables st_reset(); // Clear stepper subsystem variables. diff --git a/grbl/report.c b/grbl/report.c index 000836f..804cd11 100644 --- a/grbl/report.c +++ b/grbl/report.c @@ -369,7 +369,7 @@ void report_execute_startup_message(char *line, uint8_t status_code) // Prints build info line void report_build_info(char *line) { - printPgmString(PSTR("[VER:" GRBL_VERSION "." GRBL_VERSION_BUILD ":")); + printPgmString(PSTR("[VER:" GRBL_VERSION "." GRBL_VERSION_BUILD ":" MACHINE_TYPE ":" )); printString(line); report_util_feedback_line_feed(); printPgmString(PSTR("[OPT:")); // Generate compile-time build option list @@ -568,7 +568,8 @@ void report_realtime_status() uint8_t lim_pin_state = limits_get_state(); uint8_t ctrl_pin_state = system_control_get_state(); uint8_t prb_pin_state = probe_get_state(); - if (lim_pin_state | ctrl_pin_state | prb_pin_state) { + uint8_t pinch_roller_state = pinch_roller_get_state(); + if ((lim_pin_state | ctrl_pin_state | prb_pin_state) || pinch_roller_state) { printPgmString(PSTR("|Pn:")); if (prb_pin_state) { serial_write('P'); } if (lim_pin_state) { @@ -586,6 +587,7 @@ void report_realtime_status() if (bit_istrue(lim_pin_state,bit(X_AXIS))) { serial_write('X'); } if (bit_istrue(lim_pin_state,bit(Y_AXIS))) { serial_write('Y'); } if (bit_istrue(lim_pin_state,bit(Z_AXIS))) { serial_write('Z'); } + if (pinch_roller_state) { serial_write('R'); } #endif } if (ctrl_pin_state) { diff --git a/grbl/spi.c b/grbl/spi.c new file mode 100644 index 0000000..c6c96d7 --- /dev/null +++ b/grbl/spi.c @@ -0,0 +1,56 @@ +#include "grbl.h" + +#define CS1_PORT PORTC // Chip Select for Driver 1 +#define CS1_PIN PC0 + +#define CS2_PORT PORTC // Chip Select for Driver 2 +#define CS2_PIN PC1 + +#define SPI_DDR DDRB +#define SPI_PORT PORTB +#define MOSI PB3 +#define MISO PB4 +#define SCK PB5 + +void spi_init() { + SPI_DDR |= (1 << MOSI) | (1 << SCK); // Set MOSI and SCK as output + SPI_DDR &= ~(1 << MISO); // Set MISO as input + SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0); // Enable SPI, Master, Fclk/16 +} + +void spi_write(uint8_t cs_port, uint8_t cs_pin, uint8_t address, uint8_t data) { + // Pull CS low to select device + cs_port &= ~(1 << cs_pin); + _delay_us(1); // Short delay for setup + + // Send address + SPDR = address; + while (!(SPSR & (1 << SPIF))); // Wait for transmission to complete + + // Send data + SPDR = data; + while (!(SPSR & (1 << SPIF))); // Wait for transmission to complete + + // Pull CS high to deselect device + cs_port |= (1 << cs_pin); + _delay_us(1); // Short delay for setup +} + +/** + * CTRL1 Control Register (address = 0x04) [Default = 0Fh] + * BIT = 2-0 + * SETTING = DECAY MODE + * 111b = Smart tune Ripple Control + */ +void motor_spi_init() { + // Configure Chip Select pins as outputs + DDRC |= (1 << CS1_PIN) | (1 << CS2_PIN); + CS1_PORT |= (1 << CS1_PIN); // Set CS high (deselect) + CS2_PORT |= (1 << CS2_PIN); // Set CS high (deselect) + + // Send configuration to Driver 1 + spi_write(CS1_PORT, CS1_PIN, 0x04, 0x07); + + // Send configuration to Driver 2 + spi_write(CS2_PORT, CS2_PIN, 0x04, 0x07); +} \ No newline at end of file diff --git a/grbl/spi.h b/grbl/spi.h new file mode 100644 index 0000000..3fa0aba --- /dev/null +++ b/grbl/spi.h @@ -0,0 +1,2 @@ + +void motor_spi_init(); \ No newline at end of file diff --git a/grbl/vcm.c b/grbl/vcm.c new file mode 100644 index 0000000..ca35548 --- /dev/null +++ b/grbl/vcm.c @@ -0,0 +1,11 @@ +#include "grbl.h" + +void vcm_init() +{ + if (MACHINE_TYPE === MachineType::BAMBOO) + { + DDRD |= (1 << DDD7); + // Set pin 7 to HIGH + PORTD |= (1 << PORTD7); + } +} \ No newline at end of file diff --git a/grbl/vcm.h b/grbl/vcm.h new file mode 100644 index 0000000..d3f8e1f --- /dev/null +++ b/grbl/vcm.h @@ -0,0 +1,2 @@ + +void vcm_init(); \ No newline at end of file