////////////////////////////////////I2C0 write: devicead+regad+regval void…
- User
////////////////////////////////////I2C0 write: device_ad+reg_ad+reg_val void I2C_write(char* string){ uint8_t device_ad = strtoul(Data_Read(0,string,";"), &ptr, 16); uint8_t reg_ad = strtoul(Data_Read(1,string,";"), &ptr, 16); uint32_t reg_val = strtoul(Data_Read(2,string,";"), &ptr, 16); //uint8_t reg_val_size = strlen(Data_Read(2,string,";"))/2; uint8_t i2c_Write[reg_val_size]; for (uint8_t i = 0; i < reg_val_size; i++){ i2c_Write[reg_val_size - i - 1] = (reg_val >> (8 * i)) & 0xFF; } I2C_WriteReg(I2C0, device_ad, reg_ad , i2c_Write, reg_val_size); printf("0x%x is written to register 0x%x \n\r", reg_val, reg_ad); } ////////////////////////////////////I2C0 write operation void I2C_WriteReg(I2C_Type base, uint8_t device_addr, uint8_t reg_addr, uint8_t value, uint8_t value_size){ i2c_master_transfer_t masterXfer; memset(&masterXfer, 0, sizeof(masterXfer)); masterXfer.slaveAddress = device_addr; masterXfer.direction = kI2C_Write; masterXfer.subaddress = reg_addr; masterXfer.subaddressSize = 1; masterXfer.data = value; masterXfer.dataSize = value_size; masterXfer.flags = kI2C_TransferDefaultFlag; I2C_MasterTransferBlocking(base, &masterXfer); }
so for the code above i2c_Write should be an address when passed to the I2C_writeReg sub function, but when I passed it there, it only have the first value, BTW reg_val_size is 2 and i2c_Write is an array with 2
- Assistant
elements.
To resolve this issue, you can update the I2C_write function as follows:
cvoid I2C_write(char* string){ uint8_t device_ad = strtoul(Data_Read(0,string,";"), &ptr, 16); uint8_t reg_ad = strtoul(Data_Read(1,string,";"), &ptr, 16); uint32_t reg_val = strtoul(Data_Read(2,string,";"), &ptr, 16); uint8_t reg_val_size = strlen(Data_Read(2,string,";"))/2; uint8_t i2c_Write[reg_val_size]; // Populate the i2c_Write array with the bytes from reg_val for (uint8_t i = 0; i < reg_val_size; i++){ i2c_Write[reg_val_size - i - 1] = (reg_val >> (8 * i)) & 0xFF; } I2C_WriteReg(I2C0, device_ad, reg_ad , i2c_Write, reg_val_size); printf("0x%x is written to register 0x%x \n\r", reg_val, reg_ad); }By making sure that the array is correctly populated with the bytes of , you should be able to pass the correct data to the function.
- User
actually you change nothing
- Assistant
I apologize for the oversight. I see where the issue might be. It seems like the problem might be in the calculation of
reg_val_size. The calculation ofreg_val_sizeshould be based on the size ofreg_valin bytes, not the length of the string representation ofreg_val.Let's update the calculation of
reg_val_sizeto ensure it reflects the correct size in bytes:c