Skip to content

Commit

Permalink
Fix for issue #74, now keeps motor enable signals high until startup …
Browse files Browse the repository at this point in the history
…is completed to avoid current surges with Trinamic drivers.
  • Loading branch information
terjeio committed Sep 19, 2023
1 parent 1fbb5ef commit 6be7de3
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
41 changes: 41 additions & 0 deletions MCP3221.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
MCP3221.c - analog input from a MCP3221 I2C ADC
Driver code for RP2040 processor
Part of grblHAL
Copyright (c) 2021-2023 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/>.
*/

#include "i2c.h"
#include "MCP3221.h"

uint16_t MCP3221_read (void)
{
uint8_t value[2];

i2c_receive(MCP3221_ADDRESS, value, 2, true);

return (value[0] << 8) | value[1];
}

bool MCP3221_init (void)
{
i2c_init();

return i2c_send(MCP3221_ADDRESS, NULL, 0, true);
}
31 changes: 31 additions & 0 deletions MCP3221.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
MCP3221.h - analog input from a MCP3221 I2C ADC
Driver code for RP2040 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/>.
*/

#pragma once

#define MCP3221_ADDRESS 0b1001101

uint16_t MCP3221_read (void);
bool MCP3221_init (void);

/*EOF*/
9 changes: 6 additions & 3 deletions driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -1868,12 +1868,15 @@ static bool driver_setup (settings_t *settings)
for(uint_fast8_t i = 0; i < sizeof(outputpin) / sizeof(output_signal_t); i++) {
if(outputpin[i].port == GPIO_OUTPUT && outputpin[i].group != PinGroup_AuxOutputAnalog) {
outputpin[i].bit = 1 << outputpin[i].pin;

gpio_init(outputpin[i].pin);
if(outputpin[i].id == PinGroup_StepperEnable || outputpin[i].id == Output_SdCardCS)
DIGITAL_OUT(outputpin[i].bit, 1);

gpio_set_dir_out_masked(outputpin[i].bit);

if(outputpin[i].group == PinGroup_SpindlePWM)
gpio_set_function(outputpin[i].pin, GPIO_FUNC_PWM);
else if(outputpin[i].id == Output_SdCardCS)
DIGITAL_OUT(outputpin[i].bit, 1);
}
}

Expand Down Expand Up @@ -2061,7 +2064,7 @@ bool driver_init(void)
systick_hw->csr = M0PLUS_SYST_CSR_TICKINT_BITS | M0PLUS_SYST_CSR_ENABLE_BITS;

hal.info = "RP2040";
hal.driver_version = "230915";
hal.driver_version = "230919";
hal.driver_options = "SDK_" PICO_SDK_VERSION_STRING;
hal.driver_url = GRBL_URL "/RP2040";
#ifdef BOARD_NAME
Expand Down
50 changes: 50 additions & 0 deletions ioports.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ static io_ports_data_t digital;
static input_signal_t *aux_in;
static output_signal_t *aux_out;
static ioport_bus_t invert_digital_out;
#if MCP3221_ENABLE
static xbar_t analog_in;
static uint_fast8_t analog_n_in;
static enumerate_pins_ptr on_enumerate_pins;
#endif

static void digital_out (uint8_t port, bool on)
{
Expand Down Expand Up @@ -108,6 +113,10 @@ static int32_t wait_on_input (io_port_type_t type, uint8_t port, wait_mode_t wai
port = ioports_map(digital.in, port);
value = get_input(&aux_in[port], (settings.ioport.invert_in.mask >> port) & 0x01, wait_mode, timeout);
}
#if MCP3221_ENABLE
else if(port < analog_n_in)
value = (int32_t)MCP3221_read();
#endif

return value;
}
Expand Down Expand Up @@ -185,6 +194,10 @@ static xbar_t *get_pin_info (io_port_type_t type, io_port_direction_t dir, uint8
info = &pin;
}
}
#if MCP3221_ENABLE
else if(dir == Port_Input && port == 0)
info = &analog_in;
#endif

return info;
}
Expand Down Expand Up @@ -245,6 +258,13 @@ static bool claim (io_port_type_t type, io_port_direction_t dir, uint8_t *port,
*port = hal.port.num_digital_out;
}
}
#if MCP3221_ENABLE
else if(dir == Port_Input && (ok = *port == 0 && analog_in.mode.analog && !analog_in.mode.claimed)) {
hal.port.num_analog_in--;
analog_in.mode.claimed = On;
analog_in.description = description;
}
#endif

return ok;
}
Expand Down Expand Up @@ -283,6 +303,17 @@ bool swap_pins (io_port_type_t type, io_port_direction_t dir, uint8_t port_a, ui
return ok;
}

#if MCP3221_ENABLE

static void enumerate_pins (bool low_level, pin_info_ptr pin_info, void *data)
{
on_enumerate_pins(low_level, pin_info, data);

pin_info(&analog_in, data);
}

#endif

static void on_settings_loaded (void)
{
uint_fast8_t port = digital.out.n_ports;
Expand Down Expand Up @@ -333,6 +364,25 @@ void ioports_init (pin_group_pins_t *aux_inputs, pin_group_pins_t *aux_outputs)

ioports_add_settings(on_settings_loaded, on_setting_changed);
}

#if MCP3221_ENABLE

analog_in.function = Input_Analog_Aux0;
analog_in.group = PinGroup_AuxInput;
analog_in.pin = 0;
analog_in.port = "MCP3221:";

if(MCP3221_init()) {
analog_in.mode.analog = On;
hal.port.num_analog_in = analog_n_in = 1;
};

analog_in.description = analog_in.mode.analog ? "E0" : "No power";

on_enumerate_pins = hal.enumerate_pins;
hal.enumerate_pins = enumerate_pins;

#endif
}

#endif

0 comments on commit 6be7de3

Please sign in to comment.