Tool/software:
Hi,
I’m working with a 24V DC BLDC motor and have successfully configured the EVM (Evaluation Module). The motor and EVM are communicating properly over I2C.
I configured the EVM using Motor Studio and saved the settings to the EEPROM. When I change the value of the variable at address 0xEC (ALGO_DEBUG1), the motor should rotate. This works as expected when I use the Motor GUI and EVM.
However, I’m now trying to achieve the same result using the STM32F4 controller and the EVM, but it's not working.
Can anyone help me troubleshoot this issue?
For context, I’m receiving a HAL_OK from the function, so the I2C communication appears to be working.
I’ve attached my code for reference.
Thanks in advance!
Noam.
HAL_StatusTypeDef MCF8315A_Write(uint8_t mem_sec, uint8_t mem_page, uint16_t mem_addr, uint32_t data, uint8_t dlen, uint8_t crc_en) {
uint8_t control_word[3];
uint8_t data_bytes[4]; // Adjust size if DLEN indicates more data
uint8_t packet[9]; // Adjust size to fit control word + data + CRC if needed
uint8_t packet_size = 0;
uint8_t target_id = 0x01; // Default I2C target ID (could be modified if needed)
uint8_t rw_bit = 0x00; // R/W bit is 0 for write operation
uint8_t OP_R_W = 0X00;
// Construct control word
// Assuming OP_R_W is defined to be either 0 for write or 1 for read
control_word[0] = (crc_en << 6) | (dlen << 4) | (mem_sec & 0x0F) | (OP_R_W << 7); // CW23, CW22, CW21-20, CW19-16, CW15-12
control_word[1] = (mem_page << 4) | ((mem_addr >> 8) & 0x0F);
control_word[2] = mem_addr & 0xFF;
// Convert data to little-endian format
data_bytes[0] = (uint8_t)(data & 0xFF);
data_bytes[1] = (uint8_t)((data >> 8) & 0xFF);
data_bytes[2] = (uint8_t)((data >> 16) & 0xFF);
data_bytes[3] = (uint8_t)((data >> 24) & 0xFF);
// Construct the first byte for the Target ID and R/W bit
uint8_t i2c_address_byte = (target_id << 1) | rw_bit; // 7-bit Target ID and R/W bit (0 for write)
// Add the I2C address byte as the first byte of the packet
packet[0] = i2c_address_byte; // No need to increment packet_size yet
packet_size = 1; // Update packet_size to reflect that the address byte is added
// Construct packet
memcpy(packet + packet_size, control_word, 3);
packet_size = 4;
memcpy(packet + packet_size, data_bytes, 4);
packet_size += 4;
// Calculate and append CRC if enabled
if (crc_en) {
uint8_t crc = calculate_crc8(packet, packet_size);
packet[packet_size] = crc;
packet_size++;
}
// Transmit packet and check for success
HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(&hi2c1, MCF8315A_I2C_ADDR, packet, packet_size, HAL_MAX_DELAY);
// Return the transmission status
return status;
}
on the main:
void send_algo_debug1() {
uint8_t mem_sec = 0x0; // Memory Section
uint8_t mem_page = 0x0; // Memory Page
uint16_t mem_addr = 0x00EC; // Register address for ALGO_DEBUG1
uint32_t data = 0xBFFF0000; // Data value to write
uint8_t dlen = 0x01; // Data length (4 bytes for 32-bit)
uint8_t crc_en = 0; // Enable CRC
// Send data via I2C
uint8_t status = MCF8315A_Write(mem_sec, mem_page, mem_addr, data, dlen, crc_en);
// Check if the transmission was successful
if (status == HAL_OK) {
printf("ALGO_DEBUG1 written successfully.\n");
} else {
printf("Error writing ALGO_DEBUG1!\n");
}
}