Skip to content

Commit

Permalink
fixed file system and softspi modules and ESP32 SPI
Browse files Browse the repository at this point in the history
- reverted #695
- reverted Arduino SPI ESP32 #691 code changes
- fixed file system Grbl commands parsing
- fixed softspi start function fallback
  • Loading branch information
Paciente8159 committed Jul 20, 2024
1 parent 1c03f38 commit f26bdc4
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 113 deletions.
12 changes: 4 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@

# Changelog

## [1.9.5] - 20-04-2024

### Fixed

- fixed incomplete code propagation of #696. This prevented Grbl/System commands to propagate correctly and directly affected mount and unmount command of the SD card module (#705)

## [1.9.4] - 19-04-2024
## [1.9.4] - 20-04-2024 (republished)

[@patryk3211](https://github.com/patryk3211) - fixed STM32Fx boards SPI implementation (#699)

Expand All @@ -24,12 +18,15 @@

- modified flags for ESP32 to force Arduino SPI version (some boards seem to have problems with the SDK version) (#695)
- modified/merged entry point of user and architecture/board custom Grbl/System commands. This also fixed an issue that made impossible to pass arguments to user commands when the board had custom commands, since the buffer was parsed before command was evaluated for execution. (#696)
- reverted #695 and #691 for ESP32 Arduino SPI code

### Fixed

- fixed STM32Fx boards compilation errors is probe pin was undefined (#698)
- fixed STM32Fx boards SPI implementation (#699)
- fixed ESP32 boards SPI frequency/mode configuration (#703)
- fixed incomplete code propagation of #696. This prevented Grbl/System commands to propagate correctly and directly affected mount and unmount command of the SD card module (#705)
- fixed file system commands parsing

## [1.9.3] - 07-04-2024

Expand Down Expand Up @@ -1669,7 +1666,6 @@ Version 1.1.0 comes with many added features and improvements over the previous

### Initial release

[1.9.5]: https://github.com/Paciente8159/uCNC/releases/tag/v1.9.5
[1.9.4]: https://github.com/Paciente8159/uCNC/releases/tag/v1.9.4
[1.9.3]: https://github.com/Paciente8159/uCNC/releases/tag/v1.9.3
[1.9.2]: https://github.com/Paciente8159/uCNC/releases/tag/v1.9.2
Expand Down
2 changes: 1 addition & 1 deletion uCNC/src/cnc_build.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extern "C"
#endif

#define CNC_MAJOR_MINOR_VERSION "1.9"
#define CNC_PATCH_VERSION ".5"
#define CNC_PATCH_VERSION ".4"

#define CNC_VERSION CNC_MAJOR_MINOR_VERSION CNC_PATCH_VERSION

Expand Down
33 changes: 12 additions & 21 deletions uCNC/src/hal/mcus/esp32/esp32_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ extern "C"
#endif

#ifdef BOARD_HAS_CUSTOM_SYSTEM_COMMANDS
ADD_EVENT_LISTENER(grbl_cmd, mcu_custom_grbl_cmd);
ADD_EVENT_LISTENER(grbl_cmd, mcu_custom_grbl_cmd);
#endif
}

Expand Down Expand Up @@ -1170,40 +1170,31 @@ extern "C"
#if defined(MCU_HAS_SPI) && defined(USE_ARDUINO_SPI_LIBRARY)
#include <SPI.h>
SPIClass *esp32spi = NULL;
static uint8_t spi_mode = 0;
static uint32_t spi_freq = 1000000UL;
extern "C"
{
void mcu_spi_config(uint8_t mode, uint32_t freq)
void mcu_spi_init(void)
{
if (esp32spi != NULL)
{
esp32spi->end();
esp32spi = NULL;
}

#if (SPI_CLK_BIT == 14 || SPI_CLK_BIT == 25)
esp32spi = new SPIClass(HSPI);
#else
esp32spi = new SPIClass(VSPI);
#endif
esp32spi->begin(SPI_CLK_BIT, SPI_SDI_BIT, SPI_SDO_BIT, SPI_CS_BIT);
esp32spi->setFrequency(freq);
esp32spi->setDataMode(mode);
}

uint8_t mcu_spi_xmit(uint8_t data)
{
data = esp32spi->transfer(data);
return data;
esp32spi->begin(SPI_CLK_BIT, SPI_SDI_BIT, SPI_SDO_BIT, -1);
}

void mcu_spi_start(uint8_t mode, uint32_t frequency)
void mcu_spi_config(uint8_t mode, uint32_t freq)
{
esp32spi->beginTransaction(SPISettings(frequency, MSBFIRST, mode));
spi_freq = freq;
spi_mode = mode;
esp32spi->setFrequency(freq);
esp32spi->setDataMode(mode);
}

void mcu_spi_stop(void)
uint8_t mcu_spi_xmit(uint8_t data)
{
esp32spi->endTransaction();
return esp32spi->transfer(data);
}
}

Expand Down
110 changes: 59 additions & 51 deletions uCNC/src/hal/mcus/esp32/mcu_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ void esp32_wifi_bt_init(void);
void esp32_wifi_bt_flush(uint8_t *buffer);
void esp32_wifi_bt_process(void);

#ifdef USE_ARDUINO_SPI_LIBRARY
void mcu_spi_init(void);
#endif

#if !defined(RAM_ONLY_SETTINGS) && !defined(USE_ARDUINO_EEPROM_LIBRARY)
#include <nvs.h>
#include <esp_partition.h>
Expand Down Expand Up @@ -82,8 +86,8 @@ MCU_CALLBACK void mcu_gpio_isr(void *type);
#define I2S_SAMPLE_RATE (F_STEP_MAX * 2)
#endif
#define I2S_SAMPLES_PER_BUFFER (I2S_SAMPLE_RATE / 500) // number of samples per 2ms (0.002/1 = 1/500)
#define I2S_BUFFER_COUNT 5 // DMA buffer size 5 * 2ms = 10ms stored motions (can be adjusted but may cause to much or too little latency)
#define I2S_SAMPLE_US (1000000UL / I2S_SAMPLE_RATE) // (1s/250KHz = 0.000004s = 4us)
#define I2S_BUFFER_COUNT 5 // DMA buffer size 5 * 2ms = 10ms stored motions (can be adjusted but may cause to much or too little latency)
#define I2S_SAMPLE_US (1000000UL / I2S_SAMPLE_RATE) // (1s/250KHz = 0.000004s = 4us)

#ifdef ITP_SAMPLE_RATE
#undef ITP_SAMPLE_RATE
Expand Down Expand Up @@ -130,23 +134,23 @@ static void IRAM_ATTR esp32_i2s_stream_task(void *param)
i2s_event_t evt;
portTickType xLastWakeTimeUpload = xTaskGetTickCount();
i2s_config_t i2s_config = {
.mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
.sample_rate = I2S_SAMPLE_RATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // 1-channels
.communication_format = I2S_COMM_FORMAT_STAND_I2S | I2S_COMM_FORMAT_STAND_MSB,
.dma_buf_count = I2S_BUFFER_COUNT,
.dma_buf_len = I2S_SAMPLES_PER_BUFFER,
.use_apll = false,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // Interrupt level 1
.tx_desc_auto_clear = false,
.fixed_mclk = 0};
.mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
.sample_rate = I2S_SAMPLE_RATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // 1-channels
.communication_format = I2S_COMM_FORMAT_STAND_I2S | I2S_COMM_FORMAT_STAND_MSB,
.dma_buf_count = I2S_BUFFER_COUNT,
.dma_buf_len = I2S_SAMPLES_PER_BUFFER,
.use_apll = false,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // Interrupt level 1
.tx_desc_auto_clear = false,
.fixed_mclk = 0};

i2s_pin_config_t pin_config = {
.bck_io_num = IC74HC595_I2S_CLK,
.ws_io_num = IC74HC595_I2S_WS,
.data_out_num = IC74HC595_I2S_DATA,
.data_in_num = -1 // Not used
.bck_io_num = IC74HC595_I2S_CLK,
.ws_io_num = IC74HC595_I2S_WS,
.data_out_num = IC74HC595_I2S_DATA,
.data_in_num = -1 // Not used
};
QueueHandle_t i2s_dma_queue;

Expand Down Expand Up @@ -201,12 +205,12 @@ static void IRAM_ATTR esp32_i2s_stream_task(void *param)
I2SREG.fifo_conf.dscr_en = 0;
I2SREG.conf.tx_start = 0;
I2SREG.int_clr.val = 0xFFFFFFFF;
I2SREG.clkm_conf.clka_en = 0; // Use PLL/2 as reference
I2SREG.clkm_conf.clka_en = 0; // Use PLL/2 as reference
I2SREG.clkm_conf.clkm_div_num = 2; // reset value of 4
I2SREG.clkm_conf.clkm_div_a = 1; // 0 at reset, what about divide by 0?
I2SREG.clkm_conf.clkm_div_b = 0; // 0 at reset
I2SREG.fifo_conf.tx_fifo_mod = 3; // 32 bits single channel data
I2SREG.conf_chan.tx_chan_mod = 3; //
I2SREG.clkm_conf.clkm_div_a = 1; // 0 at reset, what about divide by 0?
I2SREG.clkm_conf.clkm_div_b = 0; // 0 at reset
I2SREG.fifo_conf.tx_fifo_mod = 3; // 32 bits single channel data
I2SREG.conf_chan.tx_chan_mod = 3; //
I2SREG.sample_rate_conf.tx_bits_mod = 32;
I2SREG.conf.tx_msb_shift = 0;
I2SREG.conf.rx_msb_shift = 0;
Expand Down Expand Up @@ -319,8 +323,8 @@ static FORCEINLINE void servo_reset(void)
#endif
}

#define start_servo_timeout(timeout) \
{ \
#define start_servo_timeout(timeout) \
{ \
servo_tick_alarm = servo_tick_counter + timeout + 64; \
}

Expand Down Expand Up @@ -519,7 +523,7 @@ MCU_CALLBACK void mcu_itp_isr(void *arg)

timer_group_clr_intr_status_in_isr(ITP_TIMER_TG, ITP_TIMER_IDX);
/* After the alarm has been triggered
we need enable it again, so it is triggered the next time */
we need enable it again, so it is triggered the next time */
timer_group_enable_alarm_in_isr(ITP_TIMER_TG, ITP_TIMER_IDX);
}

Expand All @@ -542,12 +546,12 @@ void mcu_init(void)
#ifdef MCU_HAS_UART2
// initialize UART
const uart_config_t uart2config = {
.baud_rate = BAUDRATE2,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB};
.baud_rate = BAUDRATE2,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB};
// We won't use a buffer for sending data.
uart_param_config(UART2_PORT, &uart2config);
uart_set_pin(UART2_PORT, TX2_BIT, RX2_BIT, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
Expand Down Expand Up @@ -590,12 +594,12 @@ void mcu_init(void)
#ifdef MCU_HAS_UART
// initialize UART
const uart_config_t uartconfig = {
.baud_rate = BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB};
.baud_rate = BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB};
// We won't use a buffer for sending data.
uart_param_config(UART_PORT, &uartconfig);
uart_set_pin(UART_PORT, TX_BIT, RX_BIT, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
Expand All @@ -611,7 +615,7 @@ void mcu_init(void)
itpconfig.auto_reload = true;
timer_init(ITP_TIMER_TG, ITP_TIMER_IDX, &itpconfig);
/* Timer's counter will initially start from value below.
Also, if auto_reload is set, this value will be automatically reload on alarm */
Also, if auto_reload is set, this value will be automatically reload on alarm */
timer_set_counter_value(ITP_TIMER_TG, ITP_TIMER_IDX, 0x00000000ULL);
/* Configure the alarm value and the interrupt on alarm. */
timer_set_alarm_value(ITP_TIMER_TG, ITP_TIMER_IDX, (uint64_t)(getApbFrequency() / (ITP_SAMPLE_RATE * 2)));
Expand All @@ -628,21 +632,25 @@ void mcu_init(void)
xTaskCreatePinnedToCore(mcu_rtc_task, "rtcTask", 2048, NULL, 7, NULL, CONFIG_ARDUINO_RUNNING_CORE);

#ifdef MCU_HAS_SPI
#ifndef USE_ARDUINO_SPI_LIBRARY
spi_bus_config_t spiconf = {
.miso_io_num = SPI_SDI_BIT,
.mosi_io_num = SPI_SDO_BIT,
.sclk_io_num = SPI_CLK_BIT,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.data4_io_num = -1,
.data5_io_num = -1,
.data6_io_num = -1,
.data7_io_num = -1,
.max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE,
.flags = 0,
.intr_flags = 0};
.miso_io_num = SPI_SDI_BIT,
.mosi_io_num = SPI_SDO_BIT,
.sclk_io_num = SPI_CLK_BIT,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.data4_io_num = -1,
.data5_io_num = -1,
.data6_io_num = -1,
.data7_io_num = -1,
.max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE,
.flags = 0,
.intr_flags = 0};
// Initialize the SPI bus
spi_bus_initialize(SPI_PORT, &spiconf, SPI_DMA_DISABLED);
#else
mcu_spi_init();
#endif
mcu_spi_config(SPI_MODE, SPI_FREQ);
#endif

Expand Down Expand Up @@ -1093,7 +1101,7 @@ void mcu_config_timeout(mcu_timeout_delgate fp, uint32_t timeout)
timer_init(ONESHOT_TIMER_TG, ONESHOT_TIMER_IDX, &config);

/* Timer's counter will initially start from value below.
Also, if auto_reload is set, this value will be automatically reload on alarm */
Also, if auto_reload is set, this value will be automatically reload on alarm */
timer_set_counter_value(ONESHOT_TIMER_TG, ONESHOT_TIMER_IDX, 0x00000000ULL);

/* Configure the alarm value and the interrupt on alarm. */
Expand Down
54 changes: 22 additions & 32 deletions uCNC/src/modules/file_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ bool fs_cmd_parser(void *args)
{
grbl_cmd_args_t *cmd = args;
char params[RX_BUFFER_CAPACITY]; /* get remaining command parammeters */
uint8_t has_arg = (cmd->next_char == '=');
memset(params, 0, sizeof(params));

if (!strcmp("LS", (char *)(cmd->cmd)))
Expand All @@ -459,54 +458,45 @@ bool fs_cmd_parser(void *args)

if (!strcmp("CD", (char *)(cmd->cmd)))
{
if (has_arg)
{
int8_t len = parser_get_grbl_cmd_arg(params, RX_BUFFER_CAPACITY);
int8_t len = parser_get_grbl_cmd_arg(params, RX_BUFFER_CAPACITY);

if (len < 0)
{
*(cmd->error) = STATUS_INVALID_STATEMENT;
return EVENT_HANDLED;
}

fs_cd(params);
*(cmd->error) = STATUS_OK;
if (len < 0)
{
*(cmd->error) = STATUS_INVALID_STATEMENT;
return EVENT_HANDLED;
}

fs_cd(params);
*(cmd->error) = STATUS_OK;
return EVENT_HANDLED;
}

if (!strcmp("LPR", (char *)(cmd->cmd)))
{
if (has_arg)
{
int8_t len = parser_get_grbl_cmd_arg(params, RX_BUFFER_CAPACITY);
int8_t len = parser_get_grbl_cmd_arg(params, RX_BUFFER_CAPACITY);

if (len < 0)
{
*(cmd->error) = STATUS_INVALID_STATEMENT;
return EVENT_HANDLED;
}
fs_file_print(params);
*(cmd->error) = STATUS_OK;
if (len < 0)
{
*(cmd->error) = STATUS_INVALID_STATEMENT;
return EVENT_HANDLED;
}
fs_file_print(params);
*(cmd->error) = STATUS_OK;
return EVENT_HANDLED;
}

if (!strcmp("RUN", (char *)(cmd->cmd)))
{
if (has_arg)
{
int8_t len = parser_get_grbl_cmd_arg(params, RX_BUFFER_CAPACITY);
int8_t len = parser_get_grbl_cmd_arg(params, RX_BUFFER_CAPACITY);

if (len < 0)
{
*(cmd->error) = STATUS_INVALID_STATEMENT;
return EVENT_HANDLED;
}
fs_file_run(params);
*(cmd->error) = STATUS_OK;
if (len < 0)
{
*(cmd->error) = STATUS_INVALID_STATEMENT;
return EVENT_HANDLED;
}
fs_file_run(params);
*(cmd->error) = STATUS_OK;
return EVENT_HANDLED;
}

return EVENT_CONTINUE;
Expand Down
3 changes: 3 additions & 0 deletions uCNC/src/modules/softspi.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ void softspi_start(softspi_port_t *port)
if (port->start)
{
port->start(port->spimode, port->spifreq);
return;
}

softspi_config(port, port->spimode, port->spifreq);
}

void softspi_stop(softspi_port_t *port)
Expand Down

0 comments on commit f26bdc4

Please sign in to comment.