Skip to content

Commit

Permalink
Improved SPI code.
Browse files Browse the repository at this point in the history
  • Loading branch information
terjeio committed Jun 19, 2024
1 parent 92977c3 commit 73d9e31
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 54 deletions.
1 change: 1 addition & 0 deletions main/boards/mks_dlc32_2_0_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#endif

#define BOARD_NAME "MKS DLC32 2.0"
#define BOARD_URL "https://github.com/makerbase-mks/MKS-DLC32"

#define USE_I2S_OUT
#define I2S_OUT_PIN_BASE 64
Expand Down
4 changes: 2 additions & 2 deletions main/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -2707,7 +2707,7 @@ static bool driver_setup (settings_t *settings)
sdcard_mount(NULL);

static const periph_pin_t sck = {
.function = Output_SCK,
.function = Output_SPICLK,
.group = PinGroup_SPI,
.pin = PIN_NUM_CLK,
.mode = { .mask = PINMODE_OUTPUT }
Expand Down Expand Up @@ -2811,7 +2811,7 @@ bool driver_init (void)
#else
hal.info = "ESP32";
#endif
hal.driver_version = "240408";
hal.driver_version = "240418";
hal.driver_url = GRBL_URL "/ESP32";
#ifdef BOARD_NAME
hal.board = BOARD_NAME;
Expand Down
11 changes: 9 additions & 2 deletions main/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,15 @@ extern SemaphoreHandle_t i2cBusy;
#define KEYPAD_TEST 0
#endif

#if (MODBUS_TEST + KEYPAD_TEST + MPG_TEST + TRINAMIC_TEST + (DEBUGOUT ? 1 : 0)) > (SP0 + SP1 + SP2)
#ifdef DEBUGOUT
#define DEBUG_TEST 1
#else
#define DEBUG_TEST 0
#endif

#if (MODBUS_TEST + KEYPAD_TEST + MPG_TEST + TRINAMIC_TEST + DEBUG_TEST) > (SP0 + SP1 + SP2)
#error "Too many options that requires a serial port are enabled!"
#elif (MODBUS_TEST + KEYPAD_TEST + MPG_TEST + TRINAMIC_TEST + DEBUGOUT)
#elif (MODBUS_TEST + KEYPAD_TEST + MPG_TEST + TRINAMIC_TEST + DEBUG_TEST) || SERIAL_STREAM == 1
#define SERIAL2_ENABLE 1
#else
#define SERIAL2_ENABLE 0
Expand All @@ -260,6 +266,7 @@ extern SemaphoreHandle_t i2cBusy;
#undef KEYPAD_TEST
#undef MPG_TEST
#undef TRINAMIC_TEST
#undef DEBUG_TEST

#if MPG_ENABLE
#if MPG_STREAM == 0
Expand Down
96 changes: 51 additions & 45 deletions main/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "driver/sdspi_host.h"
#include "driver/spi_master.h"

static spi_device_handle_t handle = 0;
static spi_device_handle_t handle = NULL;

void spi_init (void)
{
Expand All @@ -45,49 +45,55 @@ void spi_init (void)
.intr_flags = ESP_INTR_FLAG_IRAM
};

if(spi_bus_initialize(SDSPI_DEFAULT_HOST, &bus_config, 1) == ESP_OK) {

spi_device_interface_config_t devcfg = {
.clock_speed_hz = 1000000,
.mode = 0, //SPI mode 0
.spics_io_num = -1,
.queue_size = 1,
// .flags = SPI_DEVICE_POSITIVE_CS,
// .pre_cb = cs_high,
// .post_cb = cs_low,
.input_delay_ns = 0 //the EEPROM output the data half a SPI clock behind.
};
//Attach the EEPROM to the SPI bus
spi_bus_add_device(SDSPI_DEFAULT_HOST, &devcfg, &handle);

}
/*
static const periph_pin_t sck = {
.function = Output_SCK,
.group = PinGroup_SPI,
.port = GPIOC,
.pin = 10,
.mode = { .mask = PINMODE_OUTPUT }
};
static const periph_pin_t sdo = {
.function = Output_MOSI,
.group = PinGroup_SPI,
.port = GPIOC,
.pin = 11,
.mode = { .mask = PINMODE_NONE }
};
static const periph_pin_t sdi = {
.function = Input_MISO,
.group = PinGroup_SPI,
.port = GPIOC,
.pin = 12,
.mode = { .mask = PINMODE_NONE }
};
*/
init = true;
#if CONFIG_IDF_TARGET_ESP32S3
if((init = spi_bus_initialize(SDSPI_DEFAULT_HOST, &bus_config, SPI_DMA_CH_AUTO) == ESP_OK)) {
#else
#if PIN_NUM_CLK == GPIO_NUM_14
if((init = spi_bus_initialize(SPI2_HOST, &bus_config, 1) == ESP_OK)) { // 1 = SPI_DMA_CH1
#elif PIN_NUM_CLK == GPIO_NUM_18
if((init = spi_bus_initialize(SPI3_HOST, &bus_config, 1) == ESP_OK)) {
#else
if((init = spi_bus_initialize(SDSPI_DEFAULT_HOST, &bus_config, 1) == ESP_OK)) {
#endif
#endif
spi_device_interface_config_t devcfg = {
.clock_speed_hz = 1000000,
.mode = 0, //SPI mode 0
.spics_io_num = -1,
.queue_size = 1,
// .flags = SPI_DEVICE_POSITIVE_CS,
// .pre_cb = cs_high,
// .post_cb = cs_low,
.input_delay_ns = 0 //the EEPROM output the data half a SPI clock behind.
};

spi_bus_add_device(SPI2_HOST, &devcfg, &handle);

static const periph_pin_t sck = {
.function = Output_SPICLK,
.group = PinGroup_SPI,
.pin = PIN_NUM_CLK,
.mode = { .mask = PINMODE_OUTPUT }
};

static const periph_pin_t sdo = {
.function = Output_MOSI,
.group = PinGroup_SPI,
.pin = PIN_NUM_MOSI,
.mode = { .mask = PINMODE_NONE }
};

static const periph_pin_t sdi = {
.function = Input_MISO,
.group = PinGroup_SPI,
.pin = PIN_NUM_MISO,
.mode = { .mask = PINMODE_NONE }
};

hal.periph_port.register_pin(&sck);
hal.periph_port.register_pin(&sdo);
hal.periph_port.register_pin(&sdi);
}
}
}

Expand Down Expand Up @@ -126,7 +132,7 @@ uint8_t spi_put_byte (uint8_t byte)
spi_transaction_t t = {
.cmd = 0,
.length = 8,
.flags = SPI_TRANS_USE_TXDATA,
.flags = SPI_TRANS_USE_TXDATA|SPI_TRANS_MODE_OCT,
.tx_data[0] = byte,
.user = NULL,
};
Expand Down
11 changes: 6 additions & 5 deletions main/trinamic_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@

#include "driver.h"

#if defined(BOARD_XPRO_V5)|| defined(BOARD_BLOX) // || defined(BOARD_CNC_BOOSTERPACK)
#if defined(BOARD_XPRO_V5) || defined(BOARD_BLOX)

#include <math.h>
#include <string.h>

#include "driver.h"
#include "spi.h"
#include "i2s_out.h"
#include "grbl/protocol.h"
#include "grbl/settings.h"

#if TRINAMIC_SPI_ENABLE

#define SPI_BAUDRATEPRESCALER_32 32
#include "driver/spi_master.h"

static struct {
uint32_t pin;
Expand All @@ -48,7 +48,7 @@ TMC_spi_status_t tmc_spi_read (trinamic_motor_t driver, TMC_spi_datagram_t *reg)

uint8_t res;
uint_fast8_t idx = n_motors;
uint32_t f_spi = spi_set_speed(SPI_BAUDRATEPRESCALER_32);
uint32_t f_spi = spi_set_speed(SPI_MASTER_FREQ_10M);
volatile uint32_t dly = 100;

datagram[driver.seq].addr.value = reg->addr.value;
Expand Down Expand Up @@ -110,7 +110,7 @@ TMC_spi_status_t tmc_spi_write (trinamic_motor_t driver, TMC_spi_datagram_t *reg

uint8_t res;
uint_fast8_t idx = n_motors;
uint32_t f_spi = spi_set_speed(SPI_BAUDRATEPRESCALER_32);
uint32_t f_spi = spi_set_speed(SPI_MASTER_FREQ_10M);
volatile uint32_t dly = 100;

memcpy(&datagram[driver.seq], reg, sizeof(TMC_spi_datagram_t));
Expand Down Expand Up @@ -153,6 +153,7 @@ static void add_cs_pin (xbar_t *gpio, void *data)
static void if_init (uint8_t motors, axes_signals_t axisflags)
{
n_motors = motors;

hal.enumerate_pins(true, add_cs_pin, NULL);
}

Expand Down

0 comments on commit 73d9e31

Please sign in to comment.