Implemented TMC2209 Register Read/Write
This commit is contained in:
@@ -9,6 +9,68 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "tmc2209/tmc2209.hpp"
|
||||
#include "pico/stdio.h"
|
||||
#include "hardware/uart.h"
|
||||
|
||||
#define LED_PIN 25
|
||||
|
||||
#define ENABLE0_PIN 2
|
||||
#define ENABLE1_PIN 3
|
||||
|
||||
#define STEP0_PIN 6
|
||||
#define STEP1_PIN 7
|
||||
#define DIR0_PIN 8
|
||||
#define DIR1_PIN 9
|
||||
|
||||
int main() {
|
||||
gpio_init(ENABLE0_PIN);
|
||||
gpio_set_dir(ENABLE0_PIN, GPIO_OUT);
|
||||
gpio_put(ENABLE0_PIN, 0);
|
||||
|
||||
gpio_init(ENABLE1_PIN);
|
||||
gpio_set_dir(ENABLE1_PIN, GPIO_OUT);
|
||||
gpio_put(ENABLE1_PIN, 0);
|
||||
|
||||
gpio_init(STEP0_PIN);
|
||||
gpio_set_dir(STEP0_PIN, GPIO_OUT);
|
||||
gpio_put(STEP0_PIN, 0);
|
||||
|
||||
gpio_init(STEP1_PIN);
|
||||
gpio_set_dir(STEP1_PIN, GPIO_OUT);
|
||||
gpio_put(STEP1_PIN, 0);
|
||||
|
||||
gpio_init(DIR0_PIN);
|
||||
gpio_set_dir(DIR0_PIN, GPIO_OUT);
|
||||
gpio_put(DIR0_PIN, 0);
|
||||
|
||||
gpio_init(DIR1_PIN);
|
||||
gpio_set_dir(DIR1_PIN, GPIO_OUT);
|
||||
gpio_put(DIR1_PIN, 0);
|
||||
|
||||
TMC2209_UART uart_driver0 = TMC2209_UART(uart0, 0, 19200, 0, 1);
|
||||
TMC2209_UART uart_driver1 = TMC2209_UART(uart1, 0, 19200, 4, 5);
|
||||
uart_driver0.init();
|
||||
uart_driver1.init();
|
||||
|
||||
sleep_ms(100);
|
||||
|
||||
volatile uint32_t res1 = uart_driver1.read(0x00);
|
||||
volatile uint32_t res0 = uart_driver0.read(0x00);
|
||||
|
||||
//TMC2209_Registers registers(uart_driver);
|
||||
//registers.read_all_state();
|
||||
|
||||
gpio_init(LED_PIN);
|
||||
gpio_set_dir(LED_PIN, GPIO_OUT);
|
||||
|
||||
while (true) {
|
||||
sleep_ms(50);
|
||||
gpio_put(LED_PIN, 1);
|
||||
sleep_ms(50);
|
||||
gpio_put(LED_PIN, 0);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
80
motor-control/firmware/src/tmc2209/uart_interface.cpp
Normal file
80
motor-control/firmware/src/tmc2209/uart_interface.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico.h"
|
||||
#include "hardware/uart.h"
|
||||
|
||||
#include "tmc2209.hpp"
|
||||
|
||||
TMC2209_UART::TMC2209_UART(
|
||||
uart_inst_t *uart_inst,
|
||||
uint8_t node_address,
|
||||
uint baudrate,
|
||||
uint tx_pin,
|
||||
uint rx_pin
|
||||
) {
|
||||
this->uart_inst = uart_inst;
|
||||
this->node_address = node_address & 0b11;
|
||||
this->baudrate = baudrate;
|
||||
this->tx_pin = tx_pin;
|
||||
this->rx_pin = rx_pin;
|
||||
}
|
||||
|
||||
void TMC2209_UART::init() {
|
||||
gpio_set_function(this->tx_pin, UART_FUNCSEL_NUM(this->uart_inst, this->tx_pin));
|
||||
gpio_set_function(this->rx_pin, UART_FUNCSEL_NUM(this->uart_inst, this->rx_pin));
|
||||
|
||||
// UART format: 8 data bits, 1 stop bit, no parity
|
||||
// -> https://www.analog.com/media/en/technical-documentation/data-sheets/TMC2209_datasheet_rev1.09.pdf
|
||||
uart_set_format(this->uart_inst, 8, 1, UART_PARITY_NONE);
|
||||
|
||||
// Clear to Send (CTS) and Request to Send (RTS) are not used
|
||||
// (This is for automaticlly telling the other side that it is ready to receive/send data)
|
||||
uart_set_hw_flow(this->uart_inst, false, false);
|
||||
|
||||
uart_set_fifo_enabled(this->uart_inst, true);
|
||||
|
||||
uart_init(this->uart_inst, this->baudrate);
|
||||
}
|
||||
|
||||
uint8_t TMC2209_UART::calc_crc8_atm(uint8_t *data, uint8_t size) {
|
||||
int i,j;
|
||||
uint8_t crc = 0; // CRC located in last byte of message
|
||||
uint8_t currentByte;
|
||||
|
||||
for (i=0; i<size; i++) { // Execute for all bytes of a message
|
||||
currentByte = data[i]; // Retrieve a byte to be sent from Array
|
||||
|
||||
for (j=0; j<8; j++) {
|
||||
if ((crc >> 7) ^ (currentByte & 0x01)) { // update CRC based result of XOR operation
|
||||
crc = (crc << 1) ^ 0x07;
|
||||
} else {
|
||||
crc = (crc << 1);
|
||||
}
|
||||
currentByte = currentByte >> 1;
|
||||
} // for CRC bit
|
||||
} // for message byte
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint32_t TMC2209_UART::read(uint8_t address) {
|
||||
uint8_t data[4] = {
|
||||
0b01010101, // Sync byte
|
||||
this->node_address, // Node address
|
||||
(address & (uint8_t)(0x7F)), // Register address, last bit is 0 for read
|
||||
0
|
||||
};
|
||||
|
||||
data[3] = this->calc_crc8_atm(data, 3);
|
||||
|
||||
uart_write_blocking(this->uart_inst, data, 4);
|
||||
|
||||
uint8_t response[12];
|
||||
uart_read_blocking(this->uart_inst, response, 12);
|
||||
|
||||
if(response[11] != calc_crc8_atm(&response[4], 7)) {
|
||||
this->crc_error_flag = true;
|
||||
return 0;
|
||||
} else {
|
||||
return *((uint32_t*) &response[7]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user