Content originally posted in LPCWare by fumio yoshizawa on Mon Oct 19 21:23:26 MST 2015
Thank you for reply.
My code as follow.
I can't read and write. Please advice for me.
#include "lpc17xx_i2c.h"
#include "lpc17xx_libcfg.h"
#include "lpc17xx_pinsel.h"
/************************** PRIVATE DEFINITIONS *************************/
/** Used I2C device as slave definition */
#define USEDI2CDEV_M1 //
/** Own Slave address in Slave I2C device */
#define I2CDEV_S_ADDR(0xA0) // 24LC64 EEPROM
/** Max buffer length */
#define BUFFER_SIZE0x10
#define I2CDEV_M LPC_I2C1
static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len);
static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len);
/** These global variables below used in interrupt mode - Slave device ----------------*/
uint8_t Master_Buf[BUFFER_SIZE];
uint8_t master_test[2];
void Buffer_Init(uint8_t type);
/*-------------------------MAIN FUNCTION------------------------------*/
int main(void)
{
PINSEL_CFG_Type PinCfg;
I2C_M_SETUP_Type transferMCfg;
uint32_t tempp;
uint8_t *pdat;
/* I2C block ------------------------------------------------------------------- */
/*
* Init I2C pin connect
*/
PinCfg.OpenDrain = PINSEL_PINMODE_OPENDRAIN;
PinCfg.Pinmode = PINSEL_PINMODE_PULLUP;
// P0.19 P0.20 SDA SCL
PinCfg.Funcnum = 3;
PinCfg.Pinnum = 0;
PinCfg.Portnum = 19;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 20;
PINSEL_ConfigPin(&PinCfg);
// Initialize Slave I2C peripheral
I2C_Init(I2CDEV_M, 100000); //I2CDEV_M = LPC_I2C1
I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE); //
/* Initialize buffer */
Buffer_Init(0);
/*
I2CRead(I2CDEV_S_ADDR, pdat,2);
master_test[0] = 0xAA;
master_test[1] = 0x55;
I2CWrite(I2CDEV_S_ADDR, master_test, 2);
I2CRead(I2CDEV_S_ADDR, pdat,2);
printf("data = %x\n",pdat);
printf("data = %x\n",pdat++);
*/
// Start I2C slave device first
transferMCfg.sl_addr7bit = I2CDEV_S_ADDR;
transferMCfg.tx_data = NULL ;
transferMCfg.tx_length = 0;
transferMCfg.rx_data = Master_Buf;
transferMCfg.rx_length = sizeof(Master_Buf);
transferMCfg.retransmissions_max = 3;
if(I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING) == SUCCESS){
printf("read success\n\r");
}
else {
printf("read fail\n\r");
}
pdat = Master_Buf; //
for (tempp = 0; tempp < sizeof(Master_Buf); tempp++){
printf("data = %x\n",pdat++);
}
// Transmit and receive --------------------------------------------------------
// Initialize buffer
Buffer_Init(0);
master_test[0] = 0xAA;
master_test[1] = 0x55;
// Start I2C slave device first
transferMCfg.sl_addr7bit = I2CDEV_S_ADDR;
transferMCfg.tx_data = master_test ;
transferMCfg.tx_length = sizeof(master_test);
transferMCfg.rx_data = Master_Buf;
transferMCfg.rx_length = sizeof(Master_Buf);
transferMCfg.retransmissions_max = 3;
if(I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING)== SUCCESS){
printf("write success\n\r");
}
else {
printf("write fail\n\r");
}
pdat = Master_Buf;
for (tempp = 0; tempp < sizeof(Master_Buf); tempp++){
printf("data = %x\n",pdat++);
}
I2C_DeInit(I2CDEV_M);
// Loop forever
while(1);
}
/*********************************************************************//**
* @briefInitialize buffer
* @param[in]type:
* - 0: Initialize Master_Buf with increment value from 0
* Fill all member in Slave_Buf with 0
* - 1: Initialize Slave_Buf with increment value from 0
* Fill all member in Master_Buf with 0
* @return None
**********************************************************************/
void Buffer_Init(uint8_t type)
{
uint8_t i;
if (type)
{
for (i = 0; i < BUFFER_SIZE; i++) {
Master_Buf = i;
}
}
else
{
for (i = 0; i < BUFFER_SIZE; i++) {
Master_Buf = 0;
}
}
}
static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len)
{
I2C_M_SETUP_Type rxsetup;
rxsetup.sl_addr7bit = addr;
rxsetup.tx_data = NULL; // Get address to read at writing address
rxsetup.tx_length = 0;
rxsetup.rx_data = buf;
rxsetup.rx_length = len;
rxsetup.retransmissions_max = 3;
if (I2C_MasterTransferData(I2CDEV_M, &rxsetup, I2C_TRANSFER_POLLING) == SUCCESS){
printf("read success\n\r");
return (0);
} else {
printf("read fail\n\r");
return (-1);
}
}
static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len)
{
I2C_M_SETUP_Type txsetup;
txsetup.sl_addr7bit = addr;
txsetup.tx_data = buf;
txsetup.tx_length = len;
txsetup.rx_data = NULL;
txsetup.rx_length = 0;
txsetup.retransmissions_max = 3;
if (I2C_MasterTransferData(I2CDEV_M, &txsetup, I2C_TRANSFER_POLLING) == SUCCESS){
printf("write success\n\r");
return (0);
} else {
printf("write fail\n\r");
return (-1);
}
}