diff --git a/implementation/software/Makefile b/implementation/software/Makefile index b727add..095f467 100644 --- a/implementation/software/Makefile +++ b/implementation/software/Makefile @@ -5,6 +5,7 @@ BOARD ?= nucleo-f411re RIOTBASE ?= /home/wouter/Development/RIOT/ USEMODULE += xtimer +USEMODULE += checksum include $(RIOTBASE)/Makefile.include diff --git a/implementation/software/include/stepper.h b/implementation/software/include/stepper.h index cde16f2..e61e1d5 100644 --- a/implementation/software/include/stepper.h +++ b/implementation/software/include/stepper.h @@ -7,6 +7,11 @@ #include "periph/gpio.h" #include "periph/uart.h" #include "sensor.h" +#include "byteorder.h" + +#include +#include +#include #ifdef __cplusplus extern "C" { @@ -20,6 +25,31 @@ extern "C" { #define STEPPER_PARAM_ANGLE_DEFAULT (0) #define STEPPER_PARAM_HOMING_ANGLE_DEFAULT (20) +#define STEPPER_UART_SLAVE_ADDR (0x0) +#define STEPPER_UART_SYNC (0x05) +#define STEPPER_UART_REG_GCONF (0x00) +#define STEPPER_UART_REG_GSTAT (0x01) +#define STEPPER_UART_REG_IFCNT (0x02) +#define STEPPER_UART_REG_SLAVECONF (0x03) + +#define STEPPER_GCONF_BIT_I_SCALE_ANALOG (0x0 << 0) +#define STEPPER_GCONF_BIT_INTERNAL_RSENSE (0x1 << 1) // +#define STEPPER_GCONF_BIT_SPREADCYCLE (0x1 << 2) // enable spreadcycle +#define STEPPER_GCONF_BIT_SHAFT (0x0 << 3) // Non inverse +#define STEPPER_GCONF_BIT_PDN_DISABLE (0x1 << 6) // Set to 1 when using UART +#define STEPPER_GCONF_BIT_MSTEP_SELECT (0x1 << 7) + + // default datagram => 05 ff 00 00 00 01 01 bb +#define STEPPER_GCONF_ADDR (0x00) // 0x00 +#define STEPPER_GCONF_VALUES (0xC1000000UL) //(0x83800000UL) // 000001C1 set bit 0,6,7,8. 6: PDN disable is require for UART operation. + + // default datagram => 05 ff 6c 17 01 00 53 3e +#define STEPPER_CHOPCONF_ADDR (0x6c) // 6C +#define STEPPER_CHOPCONF_VALUES (0xB5810132UL)//(0xCA00004CUL) // 32000053 set dedge=0x1, intpol=0x1, mres=0x2, hstrt=0x5, toff=0x3. others are 0x0. +//#define STEPPER_CHOPCONF_VALUES [0x32,0x00,0x00,0x53] // set dedge=0x1, intpol=0x1, mres=0x2, hstrt=0x5, toff=0x3. others are 0x0. + + + /** * @brief Status and error codes */ @@ -70,7 +100,9 @@ int stepper_get_angle(stepper_t *dev, float *angle); int stepper_step(stepper_t *dev, stepper_direction_t *direction); +int stepper_write(stepper_t *dev, uint8_t reg, le_uint32_t *data); +int stepper_read(stepper_t *dev, uint8_t reg); #ifdef __cplusplus } diff --git a/implementation/software/include/stepper_params.h b/implementation/software/include/stepper_params.h index 0d0774c..82c2777 100644 --- a/implementation/software/include/stepper_params.h +++ b/implementation/software/include/stepper_params.h @@ -14,7 +14,7 @@ extern "C" { #define STEPPER_PARAM_DIR_PIN GPIO_PIN(0, 1) #endif #ifndef STEPPER_PARAM_UART -#define STEPPER_PARAM_UART UART_DEV(0) +#define STEPPER_PARAM_UART UART_DEV(1) #endif #ifndef STEPPER_PARAM_VEL_MAX #define STEPPER_PARAM_VEL_MAX STEPPER_PARAM_VEL_MAX_DEFAULT diff --git a/implementation/software/main.c b/implementation/software/main.c index 1731523..40c656c 100644 --- a/implementation/software/main.c +++ b/implementation/software/main.c @@ -11,8 +11,15 @@ int main(void) stepper_t dev; stepper_init(&dev, &stepper_params[0]); + xtimer_usleep(10000); + stepper_read(&dev, 0x0); + xtimer_usleep(10000); + stepper_read(&dev, 0x6C); + xtimer_usleep(10000); + stepper_read(&dev, 0x70); + xtimer_usleep(10000); while (1) { - xtimer_usleep(100000); + xtimer_usleep(100); stepper_step(&dev, STEPPER_FORWARD); } diff --git a/implementation/software/stepper.c b/implementation/software/stepper.c index 770444d..d97d19f 100644 --- a/implementation/software/stepper.c +++ b/implementation/software/stepper.c @@ -1,7 +1,7 @@ #include "periph/gpio.h" #include "periph/uart.h" - +#include "checksum/crc8.h" #include "include/stepper.h" #include "include/stepper_params.h" #include "include/sensor.h" @@ -12,6 +12,7 @@ #define STEP_PIN (dev->p.step_pin) #define DIR_PIN (dev->p.dir_pin) +#define UART (dev->p.uart) int stepper_init(stepper_t *dev, const stepper_params_t *params) { @@ -20,7 +21,15 @@ int stepper_init(stepper_t *dev, const stepper_params_t *params) dev->p = *params; gpio_init(STEP_PIN, GPIO_OUT); - + + uart_init(UART, 38400, NULL, NULL); + + le_uint32_t data; + data.u32 = STEPPER_GCONF_VALUES; + + stepper_write(dev, STEPPER_GCONF_ADDR, &data); + data.u32 = STEPPER_CHOPCONF_VALUES; + stepper_write(dev, STEPPER_CHOPCONF_ADDR, &data); return STEPPER_OK; @@ -35,6 +44,62 @@ int stepper_step(stepper_t *dev, stepper_direction_t *direction) return STEPPER_OK; } +uint8_t stepper_crc8(const uint8_t *data, size_t len, uint8_t g_polynom, uint8_t crc){ + for (size_t i=0; i < len; i++) + { + uint8_t current_byte = data[i]; + for (size_t i=0; i < 8; i++) + { + bool xor = (crc >> 7) ^ (current_byte & 0x01); + crc = crc <<1; + crc = xor ? crc ^ g_polynom : crc; + current_byte = current_byte >> 1; + } + } + return crc; +} + + + +int stepper_write(stepper_t *dev, uint8_t reg, le_uint32_t *data) +{ + uint8_t* message; + uint32_t be_data; + be_data = byteorder_swapl(data->u32); + message = (uint8_t*) malloc(8 * sizeof(uint8_t)); + message[0] = STEPPER_UART_SYNC; + message[1] = STEPPER_UART_SLAVE_ADDR; + message[2] = reg | 0x80; + memcpy(&message[3], data->u8, 4); + message[7] = 0x0; + + printf(data); + + uint8_t crc = stepper_crc8(message, 7, 0x07, 0x0); + message[7] = crc; + + uart_write(UART, message, 8); + printf("\n"); + for (int i = 0; i<8; i++) + { + printf("%0.2x",message[i]); + } + printf("\n"); + return STEPPER_OK; +} + +int stepper_read(stepper_t *dev, uint8_t reg) +{ + uint8_t* message; + message = (uint8_t*) malloc(4 * sizeof(uint8_t)); + message[0] = STEPPER_UART_SYNC; + message[1] = STEPPER_UART_SLAVE_ADDR; + message[2] = reg; + message[3] = stepper_crc8(message, 3, 0x07, 0x0); + + uart_write(UART, message, 4); +} +