// Tmc2209.h
#ifndef TMC2209_H
#define TMC2209_H

#include "pico/stdlib.h"
#include "Tmc_uart.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h> 
#include <math.h>
#include <string.h>

typedef struct {
    TMC_UART* serial_instance;
    int mtr_id; 
    uint8_t rFrame[4]; 
    uint8_t wFrame[8]; 
    uint64_t result[4];
} TMC2209;

void TMC2209_Init(TMC2209* tmc2209,TMC_UART* serial_instance);                                                       //ok
void TMC2209_Destroy(TMC2209* tmc2209);                                                             //idk
uint8_t compute_crc8_atm(const uint8_t *datagram, size_t length, uint8_t initial_value);            //ok
uint64_t* read_reg(TMC2209* tmc2209, int reg) ;                                                     //ok
uint32_t read_int(TMC2209* tmc2209, int reg);                                                       //ok
bool write_reg(TMC2209* tmc2209, int reg, int val);                                                 //ok
bool write_reg_check(TMC2209* tmc2209, int reg, int val) ;                                          //ok
void general_stat(TMC2209* tmc2209) ;                                                               //idk
void general_config(TMC2209* tmc2209);                                                              //idk
void driver_status(TMC2209* tmc2209);                                                               //idk
bool set_vactual(TMC2209* tmc2209, int value) ;                                                     //ok

void set_voltage_sense(TMC2209* tmc2209, bool enabled);                                       //ok
bool get_voltage_sense(TMC2209* tmc2209);                                                     //ok
void set_current_flow(TMC2209* tmc2209, double run_current, double hold_current_multiplier, int hold_current_delay, double Vref);//averif
void set_iscale_analog(TMC2209* tmc2209, bool enabled);                                       //ok
void set_interpolation(TMC2209* tmc2209, bool enabled);
void set_internal_resistor_sense(TMC2209* tmc2209, bool enabled);
void set_spread_cycle(TMC2209* tmc2209, bool enabled);
void set_microstepping_resolution(TMC2209* tmc2209, int msres);                               //ok
void set_microstep_resolution_regselect(TMC2209* tmc2209, bool enabled);                      //ok
void set_irun_ihold(TMC2209* tmc2209, double IHold, double IRun, int IHoldDelay);             //ok
void clear_general_stat(TMC2209* tmc2209);                                                    //ok
int get_microstepping_resolution(TMC2209* tmc2209);                                           //ok
#endif
