<commit_before>#include "TMC2130_Basics.h"

TMC2130_Basics::TMC2130_Basics(uint8_t csPin, uint8_t axisNumber)
{
  _csPin = csPin;
  _status = 0;
  _axisNumber = axisNumber;
  //_debug = "";
}

// initialize the driver with its CS/SS pin
void TMC2130_Basics::init() {
  pinMode(_csPin, OUTPUT);
  digitalWrite(_csPin, HIGH);
  // init_SPI();
  read_STAT();
}

// initialize SPI
// void TMC2130_Basics::init_SPI() {
//   SPI.setDataMode(FB_TMC_SPI_DATA_MODE);
//   SPI.setBitOrder(FB_TMC_SPI_BIT_ORDER);
//   //SPI.setClockDivider(FB_TMC_SPI_CLOCK_DIVIDER);/**
//   SPI.setClockDivider(SPI_CLOCK_DIV4);
//   SPI.begin();
// }

// read status
uint8_t TMC2130_Basics::read_STAT()
{
  //init_SPI();
  SPI.beginTransaction(SPISettings(FB_TMC_SPI_CLOCK_DIVIDER, FB_TMC_SPI_BIT_ORDER, FB_TMC_SPI_DATA_MODE));
  digitalWrite(_csPin, LOW);

  // read address
  _status = SPI.transfer(0x00);

  // flush 4 bytes
  for (int i = 0; i < 4; i++) {
    SPI.transfer(0x00);
  }

  digitalWrite(_csPin, HIGH);
  SPI.endTransaction();

  return _status;
}

// read a register
uint8_t TMC2130_Basics::read_REG(uint8_t address, uint32_t *data)
{
  // init_SPI();
  SPI.beginTransaction(SPISettings(FB_TMC_SPI_CLOCK_DIVIDER, FB_TMC_SPI_BIT_ORDER, FB_TMC_SPI_DATA_MODE));
  digitalWrite(_csPin, LOW);

  // read address
  _status = SPI.transfer(address&~FB_TMC_WRITE);

  // flush 4 bytes
  for (int i = 0; i < 4; i++) {
    SPI.transfer(0x00);
  }

  digitalWrite(_csPin, HIGH);
  // restart transmission
  digitalWrite(_csPin, LOW);

  // read address
  _status = SPI.transfer(address&~FB_TMC_WRITE);

  // retrieve data
  *data = SPI.transfer(0x00) & 0xFF;
  *data <<= 8;
  *data |= SPI.transfer(0x00) & 0xFF;
  *data <<= 8;
  *data |= SPI.transfer(0x00) & 0xFF;
  *data <<= 8;
  *data |= SPI.transfer(0x00) & 0xFF;

  digitalWrite(_csPin, HIGH);
  SPI.endTransaction();

  return _status;
}

// write to a register
uint8_t TMC2130_Basics::write_REG(uint8_t address, uint32_t data)
{
  SPI.beginTransaction(SPISettings(FB_TMC_SPI_CLOCK_DIVIDER, FB_TMC_SPI_BIT_ORDER, FB_TMC_SPI_DATA_MODE));
  digitalWrite(_csPin, LOW);

  // write address
  _status = SPI.transfer(address | FB_TMC_WRITE);

  // write data
  SPI.transfer((data >> 24UL) & 0xFF);
  SPI.transfer((data >> 16UL) & 0xFF);
  SPI.transfer((data >> 8UL) & 0xFF);
  SPI.transfer((data >> 0UL) & 0xFF);

  digitalWrite(_csPin, HIGH);
  SPI.endTransaction();

  return _status;
}

// alter a register using a bitmask
uint8_t TMC2130_Basics::alter_REG(uint8_t address, uint32_t data, uint32_t mask)
{
  uint32_t oldData, newData;

  read_REG(address, &oldData);

  newData = (oldData&~mask) | (data&mask);

  write_REG(address, newData);
<commit_msg>change tmc spi clock frequency<commit_after>#include "TMC2130_Basics.h"

TMC2130_Basics::TMC2130_Basics(uint8_t csPin, uint8_t axisNumber)
{
  _csPin = csPin;
  _status = 0;
  _axisNumber = axisNumber;
  //_debug = "";
}

// initialize the driver with its CS/SS pin
void TMC2130_Basics::init() {
  pinMode(_csPin, OUTPUT);
  digitalWrite(_csPin, HIGH);
  // init_SPI();
  read_STAT();
}

// initialize SPI
// void TMC2130_Basics::init_SPI() {
//   SPI.setDataMode(FB_TMC_SPI_DATA_MODE);
//   SPI.setBitOrder(FB_TMC_SPI_BIT_ORDER);
//   //SPI.setClockDivider(FB_TMC_SPI_CLOCK_DIVIDER);/**
//   SPI.setClockDivider(SPI_CLOCK_DIV4);
//   SPI.begin();
// }

// read status
uint8_t TMC2130_Basics::read_STAT()
{
  //init_SPI();
  SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV4, FB_TMC_SPI_BIT_ORDER, FB_TMC_SPI_DATA_MODE));
  digitalWrite(_csPin, LOW);

  // read address
  _status = SPI.transfer(0x00);

  // flush 4 bytes
  for (int i = 0; i < 4; i++) {
    SPI.transfer(0x00);
  }

  digitalWrite(_csPin, HIGH);
  SPI.endTransaction();

  return _status;
}

// read a register
uint8_t TMC2130_Basics::read_REG(uint8_t address, uint32_t *data)
{
  // init_SPI();
  SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV4, FB_TMC_SPI_BIT_ORDER, FB_TMC_SPI_DATA_MODE));
  digitalWrite(_csPin, LOW);

  // read address
  _status = SPI.transfer(address&~FB_TMC_WRITE);

  // flush 4 bytes
  for (int i = 0; i < 4; i++) {
    SPI.transfer(0x00);
  }

  digitalWrite(_csPin, HIGH);
  // restart transmission
  digitalWrite(_csPin, LOW);

  // read address
  _status = SPI.transfer(address&~FB_TMC_WRITE);

  // retrieve data
  *data = SPI.transfer(0x00) & 0xFF;
  *data <<= 8;
  *data |= SPI.transfer(0x00) & 0xFF;
  *data <<= 8;
  *data |= SPI.transfer(0x00) & 0xFF;
  *data <<= 8;
  *data |= SPI.transfer(0x00) & 0xFF;

  digitalWrite(_csPin, HIGH);
  SPI.endTransaction();

  return _status;
}

// write to a register
uint8_t TMC2130_Basics::write_REG(uint8_t address, uint32_t data)
{
  SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV4, FB_TMC_SPI_BIT_ORDER, FB_TMC_SPI_DATA_MODE));
  digitalWrite(_csPin, LOW);

  // write address
  _status = SPI.transfer(address | FB_TMC_WRITE);

  // write data
  SPI.transfer((data >> 24UL) & 0xFF);
  SPI.transfer((data >> 16UL) & 0xFF);
  SPI.transfer((data >> 8UL) & 0xFF);
  SPI.transfer((data >> 0UL) & 0xFF);

  digitalWrite(_csPin, HIGH);
  SPI.endTransaction();

  return _status;
}

// alter a register using a bitmask
uint8_t TMC2130_Basics::alter_REG(uint8_t address, uint32_t data, uint32_t mask)
{
  uint32_t oldData, newData;

  read_REG(address, &oldData);

  newData = (oldData&~mask) | (data&mask);

  write_REG(address, newData);

{'fraction_non_alphanumeric': 0.145176338869192, 'fraction_numerical': 0.03825340548609815, 'mean_word_length': 4.219084712755599, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5249770', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2140, 'n_words': 388}