Ruffy
Ruffy

Reputation: 27

Control TMC2209 Stepper Motor Driver with STM32L452RET

i am trying to run a TMC2209 Stepper Motor Driver (BIGTREETECH TMC2209 V1.3 Module enter link description here) with a STM32L452 Nucleo Board. I am using PC9 as an Enable Pin, PC8 as a STEP Pin, PC7 as an DIR Pin and PC6 as DIAG EXTI. I want to use the UART communication interface (Uart1) for reading out stepper motor states and transmit control commands (velocity, acceleration, movement angle....)(during Debug: it seems to work transmitting commands). I have connected it as described in the Datasheet (single transmit line with a resistor). enter image description here

The code is leaned against the examples and driver from Trinamic (https://github.com/trinamic) I have also followed the steps provided by Trinamic :

1.Implement the tmcXXXX_readWriteArray() function in in your code. This function provides the necessary hardware access to the TMC-API.

void tmc2209_readWriteArray(uint8_t channel, uint8_t *data, size_t writeLength, size_t readLength)
{


    HAL_UART_Transmit(&huart1, data, writeLength, HAL_MAX_DELAY);

    uint8_t buffer[9];
    
    if (readLength) {

        HAL_StatusTypeDef rx_status = HAL_UART_Receive(&huart1, buffer, readLength+1, 10); 


        last_tmc_read_attempt_ms = HAL_GetTick();
        memcpy(data, &(buffer[1]), readLength);

    }
}

2.Call tmcXXXX_init() once for each Trinamic IC in your design. This function initializes an IC object which represents one physical IC.

    void STEPPER_INIT(void)
{

        tmc_fillCRC8Table(0x07, true, 0);


        //  load whatever we've got from either boot into motor stat


        tmc2209_init(&TMC2209, 0, 0, &TMC2209_config, &tmc2209_defaultRegisterResetState[0]);

        TMC2209_config.shadowRegister[TMC2209_GCONF] = 0x00000040;
        TMC2209_config.shadowRegister[TMC2209_IHOLD_IRUN] = 0x00071703;
        TMC2209_config.shadowRegister[TMC2209_TPOWERDOWN] = 0x00000008;
        TMC2209_config.shadowRegister[TMC2209_CHOPCONF] = 0x10000053;
        TMC2209_config.shadowRegister[TMC2209_PWMCONF] = 0xC10D0024;

        


        TMC2209_config.shadowRegister[TMC2209_GCONF] &= (~TMC2209_I_SCALE_ANALOG_MASK);
        TMC2209_config.shadowRegister[TMC2209_GCONF] |= TMC2209_PDN_DISABLE_MASK;
        TMC2209_config.shadowRegister[TMC2209_GCONF] |= TMC2209_MSTEP_REG_SELECT_MASK;
        TMC2209_config.shadowRegister[TMC2209_GCONF] |= TMC2209_SHAFT_MASK;
        TMC2209_config.shadowRegister[TMC2209_GCONF] &= (~TMC2209_MULTISTEP_FILT_MASK);

        TMC2209_config.shadowRegister[TMC2209_CHOPCONF] &= (~TMC2209_MRES_MASK);
        TMC2209_config.shadowRegister[TMC2209_CHOPCONF] |= (0 << TMC2209_MRES_SHIFT) &  TMC2209_MRES_MASK;
        TMC2209_config.shadowRegister[TMC2209_CHOPCONF] |= (0xA << TMC2209_TOFF_SHIFT) &  TMC2209_TOFF_MASK;
        TMC2209_config.shadowRegister[TMC2209_CHOPCONF] |= TMC2209_DEDGE_MASK;


        TMC2209_config.shadowRegister[TMC2209_COOLCONF] = 0;
        TMC2209_config.shadowRegister[TMC2209_TCOOLTHRS] = 0xFFFFFFFF;

        TMC2209_config.shadowRegister[TMC2209_IHOLD_IRUN] &= (~TMC2209_IHOLDDELAY_MASK);
        TMC2209_config.shadowRegister[TMC2209_IHOLD_IRUN] |= (3 << TMC2209_IHOLDDELAY_SHIFT) &  TMC2209_IHOLDDELAY_MASK;

        StepDir_init(STEPDIR_PRECISION);
        StepDir_setPins(0);
        StepDir_setVelocityMax(0, 51200);
        StepDir_setAcceleration(0, 51200);

}

(The definition of the shadow registers are taken from this https://github.com/AlistairSymonds/SympleAstroFocus example) 3.Call tmcXXXX_periodicJob() periodically. Pass a millisecond timestamp as the tick parameter.

static void periodicJob(uint32_t tick)
{
    tmc2209_periodicJob(&TMC2209, tick);
    StepDir_periodicJob(0);
}
  

4.After initializing, calling tmcXXXX_reset() or tmcXXXX_restore(), the TMC-API will write multiple registers to the IC (referred to as IC configuration).

5.Per call to tmcXXXX_periodicJob(), one register will be written until IC configuration is completed.

6.Once the IC configuration is completed, you can use tmcXXXX_readInt() and tmcXXXX_writeInt() to read and write registers.

    int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_USART1_UART_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */

    STEPPER_INIT();

    HAL_TIM_Base_Start_IT(&htim3);

    tmc2209_reset(&TMC2209);
    tmc2209_restore(&TMC2209);





  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {



      if(enable == 1){
          HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_RESET);
      }
      else if(enable == 0){
          HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_SET);
      }

      
      right(0,20000);
      periodicJob(HAL_GetTick());


      



    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

So actually the task is to adjust the StepDir.h and StepDir.c files which are provided from Trinamic in their example of the TMC Evalsystem : (https://github.com/trinamic/TMC-EvalSystem) to my hardware platform (and setup the main function accordingly). The driver files TMC2209.c and TMC2209.h (+ Ramp and Helper Functions) are leaved untouched.

My motor is just bouncing around and is not moving in a specific direction.

Does someone has a similar problem and adjusted the examples from Trinamic to another STM32 Chip?

(I can also provide my main.c, StepDic.c and StepDir.h files if it is necessary.)

Upvotes: 0

Views: 1803

Answers (1)

gege
gege

Reputation: 1

had my TMC2209 driven stepper stuttering yesterday. Rason was that motor coils were not connected correctly. You may want to verify yours with an Ohm Meter

BR

Upvotes: 0

Related Questions