/* Setup I2C handle and parameters */ static void setupI2CMaster() { /* Enable I2C clock and reset I2C peripheral - the boot ROM does not do this */ Chip_I2C_Init(LPC_I2C); /* Perform a sanity check on the storage allocation */ if (LPC_I2CD_API->i2c_get_mem_size() > sizeof(i2cMasterHandleMEM)) { /* Example only: this should never happen and probably isn't needed for most I2C code. */ errorI2C(); } /* Setup the I2C handle */ i2cHandleMaster = LPC_I2CD_API->i2c_setup(LPC_I2C_BASE, i2cMasterHandleMEM); if (i2cHandleMaster == NULL) { errorI2C(); } /* Set I2C bitrate */ if (LPC_I2CD_API->i2c_set_bitrate(i2cHandleMaster, Chip_Clock_GetSystemClockRate(), I2C_BITRATE) != LPC_OK) { errorI2C(); } }
/* Setup I2C handle and parameters */ static void setupI2CSlave() { ErrorCode_t error_code; /* Enable I2C clock and reset I2C peripheral - the boot ROM does not do this */ Chip_I2C_Init(); /* Perform a sanity check on the storage allocation */ if (LPC_I2CD_API->i2c_get_mem_size() > sizeof(i2cSlaveHandleMEM)) { /* Example only: this should never happen and probably isn't needed for most I2C code. */ errorI2C(); } /* Setup the I2C handle */ i2cHandleSlave = LPC_I2CD_API->i2c_setup(LPC_I2C_BASE, i2cSlaveHandleMEM); if (i2cHandleSlave == NULL) { errorI2C(); } /* Set a single 7-bit I2C address, only 7-bit addressing is supported */ error_code = LPC_I2CD_API->i2c_set_slave_addr(i2cHandleSlave, I2C_ADDR_7BIT, 0xFF); if (error_code != LPC_OK) { DEBUGOUT("Error setting I2C slave address\r\n"); errorI2C(); } /* No need to set I2C clock rate in slave mode */ }
/* Master transmit in interrupt mode */ static void sendI2CMaster(uint16_t AddressI2C, bool ledStateOut, uint8_t regAddr) { uint8_t SendData[10]; I2C_PARAM_T param; I2C_RESULT_T result; ErrorCode_t error_code; int index = 0; SendData[index++] = (uint8_t) AddressI2C; SendData[index++] = (uint8_t) regAddr; /* I2C device regAddr */ SendData[index++] = (uint8_t) 0x00; SendData[index++] = (uint8_t) ledValues[ledValue]; SendData[index++] = (uint8_t) displayNumber; /* Setup I2C parameters for number of bytes with stop - appears as follows on bus: Start - address7 or address10upper - ack (10 bits addressing only) address10lower - ack value 1 - ack value 2 - ack - stop */ param.num_bytes_send = index; param.buffer_ptr_send = &SendData[0]; param.num_bytes_rec = 0; param.stop_flag = 1; param.func_pt = cbI2CComplete; /* Set timeout (much) greater than the transfer length */ //LPC_I2CD_API->i2c_set_timeout(i2cHandleMaster, 100000); /* Do master write transfer */ intErrCode = -1; /* Function is non-blocking, returned error should be LPC_OK, but isn't checked here */ error_code = LPC_I2CD_API->i2c_master_transmit_intr(i2cHandleMaster, ¶m, &result); /* Sleep until transfer is complete, but allow IRQ to wake system to handle I2C IRQ */ while (intErrCode == -1) { __WFI(); } /* Cast saved error code from callback */ error_code = (ErrorCode_t) intErrCode; // /* Completed without erors? */ if (error_code != LPC_OK) { /* Likely cause is NAK */ Print_Val("i2c_master_transmit error code : 0x", error_code); errorI2C(); } /* Note results are only valid when there are no errors */ }
/* Slave receive in interrupt mode */ static void readI2CSlave(void) { ErrorCode_t error_code; /* Setup receive buffer, receive buffer size, and receive callback */ paramRX.num_bytes_rec = 2; /* Address and single byte */ paramRX.buffer_ptr_rec = &recvBuff[0]; paramRX.func_pt = cbRXI2CComplete; /* Clear error code */ RXintErrCode = -1; /* Function is non-blocking */ error_code = LPC_I2CD_API->i2c_slave_receive_intr(i2cHandleSlave, ¶mRX, &resultRX); /* Completed without erors? */ if (error_code != LPC_OK) { DEBUGOUT("i2c_slave_receive_intr error code : %x\r\b", error_code); errorI2C(); } }
/* Slave transmit in interrupt mode */ static void sendI2CSlave(void) { ErrorCode_t error_code; /* Send 1 byte based on master request */ paramRX.num_bytes_rec = 1; /* Address byte */ paramRX.buffer_ptr_rec = &recvBuff[0]; paramTX.num_bytes_send = 1; paramTX.buffer_ptr_send = &tranBuff[0]; paramTX.func_pt = cbTXI2CComplete; /* Clear error code */ TXintErrCode = -1; /* Function is non-blocking */ error_code = LPC_I2CD_API->i2c_slave_transmit_intr(i2cHandleSlave, ¶mTX, &resultTX); /* Completed without erors? */ if (error_code != LPC_OK) { DEBUGOUT("i2c_slave_transmit_intr error code : %x\r\b", error_code); errorI2C(); } }