uint8t I2Cdeviceaddr = strtoul(DataRead(0, str, ";"), NULL, 16); uint8t I2Cregaddr =…
- User
uint8_t I2C_device_addr = strtoul(Data_Read(0, str, ";"), NULL, 16); uint8_t I2C_reg_addr = strtoul(Data_Read(1, str, ";"), NULL, 16); char* I2C_rxBuff_value = Data_Read(2, str, ";"); char I2C_rxBuff_compare[10]; // buffer to store received value char* I2C_choose = Data_Read(3, str, ";"); uint8_t I2C_bit = strtoul(Data_Read(4, str, ";"), NULL, 10); char* I2C_compare = Data_Read(5, str, ";");
// Determine the comparison type int I2C_compare_int = (strcmp(I2C_compare, "equal") != 0); // Read the value from the register I2C_read(I2C_device_addr, I2C_reg_addr, I2C_rxBuff_compare); if (strcmp(I2C_choose, "bit") == 0) { // Check if the statement is check bit value char I2C_bit_value = (char)(hexToBin(I2C_rxBuff_compare))[reg_val_size * 8 - I2C_bit - 1]; // Checking bit value and print result if ((I2C_rxBuff_value[0] == I2C_bit_value) != I2C_compare_int) { printf("11111\n\r"); } else { printf("22222\n\r"); } } else { // Checking register value and print result if (strcmp(I2C_rxBuff_value, I2C_rxBuff_compare) == I2C_compare_int) { printf("333333\n\r"); } else { printf("444444\n\r"); } }do I need to free any variables
- Assistant
- User
cadence virtuoso highlight same net in schematic short keys
- Assistant
- User
similar products to max686 boost
- Assistant
- User
3.3v gpio opendrain what happen if I pull it up to a 5v
- Assistant
- User
////////////////////////////////I2C0 read: reg_ad; rxbuffer_char void I2C_read(uint8_t I2C_device_addr, uint8_t I2C_reg_addr, char* I2C_rxBuff_compare){ rxBuff = (uint8_t*)calloc(reg_val_size, sizeof(uint8_t)); I2C_ReadRegs(I2C0, I2C_device_addr, I2C_reg_addr,reg_val_size, rxBuff);// read data and save it to rxBuff // for loop to convert rxbuff hex value to rxBuff_char I2C_rxBuff_compare[reg_val_size * 2] = '\0'; rxBuff_char = (char*)calloc(2reg_val_size, sizeof(char)); for (int i = 0; i < reg_val_size; i++){ sprintf(rxBuff_char + i2, "%02x",rxBuff[i]); } free(rxBuff); free(rxBuff_char); }
void I2C_ReadRegs(I2C_Type base, uint8_t device_addr, uint8_t reg_addr, uint8_t value_size, uint8_t rxBuff){ i2c_master_transfer_t masterXfer; memset(&masterXfer, 0, sizeof(masterXfer)); masterXfer.slaveAddress = device_addr; masterXfer.direction = kI2C_Read; masterXfer.subaddress = reg_addr; masterXfer.subaddressSize = 1; masterXfer.data = rxBuff; masterXfer.dataSize = value_size; masterXfer.flags = kI2C_TransferDefaultFlag; I2C_MasterTransferBlocking(base, &masterXfer); }
could you optimize the code above. this is for I2C communication read using mcuxresso ide with Kl25z microcontroller
- Assistant
- User
for the code above what about when reg_val_size is unknow. it could be 2 or 4. wouldnt I use calloc in that case
- Assistant
- User
char I2C_bit_value = (char)(hexToBin(I2C_rxBuff_compare))[reg_val_size * 8 - I2C_bit - 1]; reg_val_size is 1 I2C_rxBuff_compare is a hex value which I read from I2C communication hexToBin is a hex to bin conversion function,
char* hexToBin(char* hexString) { char* binaryString = malloc(strlen(hexString) * 4 + 1); if (binaryString == NULL) { return NULL; }
for (int i = 0; i < strlen(hexString); i++) { switch(hexString[i]) { case '0': strcat(binaryString, "0000"); break; case '1': strcat(binaryString, "0001"); break; case '2': strcat(binaryString, "0010"); break; case '3': strcat(binaryString, "0011"); break; case '4': strcat(binaryString, "0100"); break; case '5': strcat(binaryString, "0101"); break; case '6': strcat(binaryString, "0110"); break; case '7': strcat(binaryString, "0111"); break; case '8': strcat(binaryString, "1000"); break; case '9': strcat(binaryString, "1001"); break; case 'A': case 'a': strcat(binaryString, "1010"); break; case 'B': case 'b': strcat(binaryString, "1011"); break; case 'C': case 'c': strcat(binaryString, "1100"); break; case 'D': case 'd': strcat(binaryString, "1101"); break; case 'E': case 'e': strcat(binaryString, "1110"); break; case 'F': case 'f': strcat(binaryString, "1111"); break; default: free(binaryString); return NULL; } } return binaryString; - Assistant
- User
what if the length of the string is changable can I still use char* binaryString = hexToBin(hexString);?
- Assistant
- User
HELP me fix this code where it use the function to printout a CRC with the hex 0b3f60
- Assistant