Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
idf_component_register(SRC_DIRS src/source
INCLUDE_DIRS "src"
)
target_compile_options(${COMPONENT_LIB} PRIVATE -Wfatal-errors)
3 changes: 3 additions & 0 deletions src/TMCStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include <bcm2835.h>
#include "source/bcm2835_spi.h"
#include "source/bcm2835_stream.h"
#elif defined(ESP_PLATFORM)
#include "source/ESP32_Serial.h"
#include "source/ESP32_SPI.h"
#elif defined(__has_include)
#if __has_include(<Arduino.h>)
#include <Arduino.h>
Expand Down
25 changes: 25 additions & 0 deletions src/source/ESP32_Adapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "ESP32_Adapter.h"

#ifdef ESP_PLATFORM
void pinMode( uint16_t num, gpio_dir_t mode ) {
switch( mode ) {
case OUTPUT:
gpio_set_direction( (gpio_num_t)num, GPIO_MODE_OUTPUT );
break;
case INPUT:
gpio_set_direction( (gpio_num_t)num, GPIO_MODE_INPUT );
gpio_set_pull_mode( (gpio_num_t)num, GPIO_FLOATING );
break;
case INPUT_PULLUP:
gpio_set_direction( (gpio_num_t)num, GPIO_MODE_INPUT );
gpio_set_pull_mode( (gpio_num_t)num, GPIO_PULLUP_ONLY );
break;
}
}
void digitalWrite( uint16_t num, uint32_t level ) {
gpio_set_level( (gpio_num_t)num, level );
}
int digitalRead( uint16_t num ) {
return gpio_get_level( (gpio_num_t)num );
}
#endif
16 changes: 16 additions & 0 deletions src/source/ESP32_Adapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#ifdef ESP_PLATFORM
#include <cstdint>
#include <driver/gpio.h>
#define LOW 0
#define HIGH 1
typedef enum {
OUTPUT,
INPUT,
INPUT_PULLUP
} gpio_dir_t;
void pinMode( uint16_t num, gpio_dir_t mode );
void digitalWrite( uint16_t num, uint32_t level );
int digitalRead( uint16_t num );
#endif
7 changes: 7 additions & 0 deletions src/source/ESP32_SPI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifdef ESP_PLATFORM

#include "ESP32_SPI.h"

SPIClass SPI;

#endif
48 changes: 48 additions & 0 deletions src/source/ESP32_SPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

/*
****************************************************
DUMMY IMPLEMENTATION
****************************************************
DOES NOT ACTUALLY DO ANYTHING
****************************************************
*/

#ifdef ESP_PLATFORM

#include <cstdint>

#define MSBFIRST 0
#define SPI_MODE0 0
#define SPI_MODE1 1
#define SPI_MODE2 2
#define SPI_MODE3 3

class SPIClass;

struct SPISettings
{
friend class SPIClass;
SPISettings(uint32_t s, uint32_t o, uint32_t m) {
speed = s;
order = o;
mode = m;
}

uint32_t speed;
uint32_t order;
uint32_t mode;
};

class SPIClass
{
public:
void beginTransaction(SPISettings settings) {}
void endTransaction() {}
uint8_t transfer(uint8_t) {
return 0;
}
};

extern SPIClass SPI;
#endif
80 changes: 80 additions & 0 deletions src/source/ESP32_Serial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "ESP32_Serial.h"

#ifdef ESP_PLATFORM

#define LOG_LOCAL_LEVEL ESP_LOG_INFO
#define TAG_EPS32_SERIAL "ESP32_UART"
#include <esp_log.h>
#include <hal/gpio_types.h>

ESP32_Serial::ESP32_Serial( int baud_rate, int rx_pin, int tx_pin, uart_port_t uart_num_new ) {
uart_num = uart_num_new;
ready = false;
uart_config_t uart_config = {
.baud_rate = baud_rate,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 0,
.source_clk = UART_SCLK_APB,
};
if( ESP_OK == uart_driver_install( uart_num, 2048, 0, 0, NULL, 0 ) ) {
if( ESP_OK == uart_param_config( uart_num, &uart_config ) ) {
if( ESP_OK == uart_set_pin( uart_num, tx_pin, rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE ) ) {
ready = true;
}
else {
ESP_LOGE( TAG_EPS32_SERIAL, "Unable to set UART pins." );
}
}
else {
ESP_LOGE( TAG_EPS32_SERIAL, "Unable to configure UART." );
}
}
else {
ESP_LOGE( TAG_EPS32_SERIAL, "Unable to install UART driver." );
}
}

int16_t ESP32_Serial::read() {
if( !ready ) {
ESP_LOGE( TAG_EPS32_SERIAL, "Trying to read from not ready UART." );
return -1;
}
uint8_t data = 0;
int len = uart_read_bytes( uart_num, &data, 1, 0 );
ESP_LOGD( TAG_EPS32_SERIAL, "Read data: %X (length %d)", data, len );
if( len < 1 ) return -1;
else return data;
}

uint8_t ESP32_Serial::write( const uint8_t data ) {
if( !ready ) {
ESP_LOGE( TAG_EPS32_SERIAL, "Trying to write to not ready UART." );
return 0;
}
ESP_LOGD( TAG_EPS32_SERIAL, "Write data: %X", data );
int len = uart_write_bytes( uart_num, (const char*)&data, 1);
ESP_LOGD( TAG_EPS32_SERIAL, "Number of bytes written: %d", len );
if( len < 1 ) return 0;
else return len;
}

size_t ESP32_Serial::available() {
if( !ready ) {
ESP_LOGE( TAG_EPS32_SERIAL, "Trying to get available bytes from not ready UART." );
return 0;
}
size_t len = 0;
if( ESP_OK == uart_get_buffered_data_len( uart_num, &len ) ) {
ESP_LOGD( TAG_EPS32_SERIAL, "Number of bytes available: %d", len );
return len;
}
else {
ESP_LOGE( TAG_EPS32_SERIAL, "Error getting available bytes." );
return 0;
}
}

#endif // ESP_PLATFORM
20 changes: 20 additions & 0 deletions src/source/ESP32_Serial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#ifdef ESP_PLATFORM
#include <cstdint>

#include <driver/uart.h>

class ESP32_Serial {
public:
ESP32_Serial( int baud_rate, int rx_pin = 16, int tx_pin = 17, uart_port_t uart_num_new = UART_NUM_2 );

int16_t read();
uint8_t write( const uint8_t data );
size_t available();

private:
bool ready;
uart_port_t uart_num;
};
#endif
1 change: 1 addition & 0 deletions src/source/SERIAL_SWITCH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SERIAL_SWITCH.cpp - Serial Switch for Arduino / Raspberry Pi
*/
#include "SERIAL_SWITCH.h"
#include "ESP32_Adapter.h"

SSwitch::SSwitch( const uint16_t pin1, const uint16_t pin2, const uint8_t address) :
p1(pin1),
Expand Down
1 change: 1 addition & 0 deletions src/source/SERIAL_SWITCH.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <Arduino.h>
#endif
#endif
#include <cstdint>

#include "TMC_platforms.h"

Expand Down
1 change: 1 addition & 0 deletions src/source/SW_SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SW_SPI.cpp - Software SPI for Arduino or Raspberry Pi
*/
#include "SW_SPI.h"
#include "ESP32_Adapter.h"

SW_SPIClass::SW_SPIClass(uint16_t mosi, uint16_t miso, uint16_t sck) :
mosi_pin(mosi),
Expand Down
8 changes: 5 additions & 3 deletions src/source/SW_SPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
#pragma once

#include <cstdint>

#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#elif defined(bcm2835)
Expand All @@ -22,16 +24,16 @@ class SW_SPIClass {
public:
SW_SPIClass(uint16_t sw_mosi_pin, uint16_t sw_miso_pin, uint16_t sw_sck_pin);
void init();
void begin() {};
void begin() {}
uint8_t transfer(uint8_t ulVal);
uint16_t transfer16(uint16_t data);
void endTransaction() {};
void endTransaction() {}
private:
const uint16_t mosi_pin,
miso_pin,
sck_pin;

#if defined(ARDUINO_ARCH_AVR)
#ifdef ARDUINO_ARCH_AVR
fastio_bm mosi_bm,
miso_bm,
sck_bm;
Expand Down
10 changes: 6 additions & 4 deletions src/source/TMC2130Stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
int8_t TMC2130Stepper::chain_length = 0;
uint32_t TMC2130Stepper::spi_speed = 16000000/8;

TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, float RS, int8_t link) :
TMCStepper(RS),
_pinCS(pinCS),
link_index(link)
#ifndef ESP_PLATFORM
TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, float RS, int8_t link) :
TMCStepper(RS),
_pinCS(pinCS),
link_index(link)
{
defaults();

if (link > chain_length)
chain_length = link;
}
#endif

TMC2130Stepper::TMC2130Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) :
TMCStepper(default_RS),
Expand Down
4 changes: 3 additions & 1 deletion src/source/TMC2130Stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

class TMC2130Stepper : public TMCStepper {
public:
TMC2130Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1);
#ifndef ESP_PLATFORM
TMC2130Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1);
#endif
TMC2130Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1);
TMC2130Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1);
void begin();
Expand Down
6 changes: 4 additions & 2 deletions src/source/TMC2160Stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#include "../TMCStepper.h"
#include "TMC_MACROS.h"

TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, float RS, int8_t link) : TMC2130Stepper(pinCS, RS, link)
{ defaults(); }
#ifndef ESP_PLATFORM
TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, float RS, int8_t link) : TMC2130Stepper(pinCS, RS, link)
{ defaults(); }
#endif
TMC2160Stepper::TMC2160Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link) :
TMC2130Stepper(pinCS, RS, pinMOSI, pinMISO, pinSCK, link)
{ defaults(); }
Expand Down
4 changes: 3 additions & 1 deletion src/source/TMC2160Stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

class TMC2160Stepper : public TMC2130Stepper {
public:
TMC2160Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1);
#ifndef ESP_PLATFORM
TMC2160Stepper(uint16_t pinCS, float RS = default_RS, int8_t link_index = -1);
#endif
TMC2160Stepper(uint16_t pinCS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1);
TMC2160Stepper(uint16_t pinCS, float RS, uint16_t pinMOSI, uint16_t pinMISO, uint16_t pinSCK, int8_t link_index = -1);
void begin();
Expand Down
Loading