How can I use STM32F103 to read TMC2226's register

41 Views Asked by At

According to the TMC2226 datasheet, I can easily modify the TMC2226 chip's register address through STM32's UART4. The code is as follows:

void MOTOR_Send_Command(const uint8_t *Data, uint8_t RegAddr) {
    //[0:7]  sync + reserved = 0xA0
    //[8:15] 8 bit node address = 0
    //[16:23]RW+7 bit register addr   W = 1; R = 0
    //[24:55]32 bit data
    //[56:63]CRC
    uint8_t datagram[8] = {0x05, 0x00};
    datagram[2] = RegAddr | 0x80;
    for (int i = 3, j = 0; i < 7; ++i, ++j) {
        datagram[i] = Data[j];
    }
    swuart_calcCRC(datagram, 8);
    HAL_UART_Transmit(&huart4, datagram, 8, 0xFF);
}

The crc verification code has been given in the data sheet:

void swuart_calcCRC(uint8_t *datagram, uint8_t datagramLength) {
    int i, j;
    uint8_t *crc = datagram + (datagramLength - 1);//在数据的最后开辟一个位
    uint8_t currentByte;

    *crc = 0;

    for (i = 0; i < (datagramLength - 1); i++) {      // Execute for all bytes of a message
        currentByte = datagram[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
}

However, I am facing difficulties when trying to read the TMC2226's registers. I can only read the first byte of data and am unable to read any additional bytes. The code is as follows:

void MOTOR_ReadRegister(uint8_t *data, uint8_t RegAddr) {
    uint8_t datagram[4] = {0x05, 0x00};

    datagram[2] = RegAddr & 0x7F;
    swuart_calcCRC(datagram, 4);
    HAL_UART_Transmit(&huart4, datagram, 4, 0xFF);

    HAL_UART_Receive(&huart4, data, 8, 0xFF);
}

When I use a serial port assistant tool to read the data, the result is always: 05 00 00 00 00 00 00 00.

Could this issue be related to the way I'm reading the data through UART4, or is it possible that my TMC2226 settings are incorrect? Any suggestions or insights would be greatly appreciated.

I have tried modifying the code from datagram[2] = RegAddr & 0x7F; to datagram[2] = RegAddr & 0xEF;, but this did not resolve the issue. Additionally, I attempted to change the 'SENDDELAY' through the serial port, but this also did not lead to any change. Below is the data structure diagram for both read and write operations.

Write Access Datagram Structure

Read Access Datagram Structure

Read Access REPLY DATAGRAM STRUCTURE

0

There are 0 best solutions below