unsigned int mem_Write(unsigned int addr,unsigned char *data, unsigned int data_len) { unsigned int i=0; unsigned char b; i2c_Start(); i2c_Write(MEM_ADDRESS); if (i2c_Timeout()) goto Timeout; b = addr>>8; i2c_Write(b); if (i2c_Timeout()) goto Timeout; b = addr&0xFF; i2c_Write(b); if (i2c_Timeout()) goto Timeout; for (i=0; i<data_len; i++) { i2c_Write(data[i]); if (i2c_Timeout()) goto Timeout; } i2c_Stop(); return i; Timeout: return i; }
unsigned int mem_Read(unsigned int addr,unsigned char *data, unsigned int data_len) { unsigned int i=0; unsigned char b; i2c_Start(); i2c_Write(MEM_ADDRESS); if (i2c_Timeout()) goto Timeout; b = addr>>8; i2c_Write(b); if (i2c_Timeout()) goto Timeout; b = addr&0xFF; i2c_Write(b); if (i2c_Timeout()) goto Timeout; //i2c_Stop(); i2c_Start(); i2c_Write(MEM_ADDRESS | 1); if (i2c_Timeout()) goto Timeout; for (i=0; i<data_len; i++) { data[i] = i2c_Read(); clear_CPU_operation_detection(); if (i<data_len-1) i2c_Ack(); else i2c_Nack(); } i2c_Stop(); return i; Timeout: return i; }
/** * @brief User callback that manages the Timeout error. * @param pDevInitStruct . * @retval None. */ uint32_t CPAL_TIMEOUT_UserCallback(CPAL_InitTypeDef* pDevInitStruct) { i2c_Timeout(); /* Update CPAL states */ pDevInitStruct->CPAL_State = CPAL_STATE_READY; pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_NONE; pDevInitStruct->wCPAL_Timeout = CPAL_I2C_TIMEOUT_DEFAULT; /* DeInitialize CPAL device */ CPAL_I2C_DeInit(pDevInitStruct); /* Initialize CPAL device with the selected parameters */ CPAL_I2C_Init(pDevInitStruct); return CPAL_PASS; }