Transferring data to EEPROM via I2C

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Transferring data to EEPROM via I2C

1,201 次查看
Selkraps
Contributor I

Hello everybody,

I'm new to LPC1769 and MCUXpresso. I have started a project which aims to send a data like my name to EEPROM using I2C. I will include my code below and I have no idea why the functions do not work. I have tried to verify if the transfer works by using LEDs and tracking the readData variable while debugging but it seems like the WriteData function doesn't work. Thank you for your help.

#if defined (__USE_LPCOPEN)
#if defined(NO_BOARD_LIB)
#include "chip.h"
#else
#include "board.h"
#endif
#endif

#include <cr_section_macros.h>
#include <string.h>

// Define parameters
#define I2C_INTERFACE I2C0
#define I2C_CLK_DIVIDER (40)
#define EEPROM_I2C_ADDR (0x50)
#define EEPROM_WRITE_ADDR (0x00)
#define EEPROM_READ_ADDR (0x00)
#define LED_GREEN_PORT 3
#define LED_GREEN_PIN 26
#define LED_RED_PORT 0
#define LED_RED_PIN 22

 

//Function to initialize I2C
void Init_I2C(void) {

Chip_I2C_Init(I2C_INTERFACE);
Chip_I2C_SetClockRate(I2C_INTERFACE, 100000);
Chip_I2C_SetMasterEventHandler(I2C_INTERFACE, Chip_I2C_EventHandlerPolling);
}

//Function to initialize GPIO for LED
void Init_LED(void) {

Chip_GPIO_Init(LPC_GPIO);
Chip_GPIO_SetPinDIROutput(LPC_GPIO, LED_GREEN_PORT, LED_GREEN_PIN);
Chip_GPIO_SetPinDIROutput(LPC_GPIO, LED_RED_PORT, LED_RED_PIN);
LED_Off(LED_GREEN_PORT, LED_GREEN_PIN);
LED_Off(LED_RED_PORT, LED_RED_PIN);
}

//Function to write to EEPROM
void Write_EEPROM(char* data, int len) {

I2C_XFER_T xfer;
uint8_t txBuffer[len + 1];

// Prepare buffer with EEPROM address and data
txBuffer[0] = EEPROM_WRITE_ADDR;
memcpy(&txBuffer[1], data, len);

// Set up I2C transfer
xfer.slaveAddr = EEPROM_I2C_ADDR;
xfer.txBuff = txBuffer;
xfer.txSz = sizeof(txBuffer);
xfer.rxBuff = NULL;
xfer.rxSz = 0;

// Perform I2C transfer
if (Chip_I2C_MasterTransfer(I2C_INTERFACE, &xfer) != I2C_STATUS_DONE) {
LED_On(LED_RED_PORT, LED_RED_PIN);
} else {
LED_Off(LED_RED_PORT, LED_RED_PIN);
}
}

//Function to read from EEPROM
void Read_EEPROM(char* buffer, int len) {
// Read data from EEPROM
I2C_XFER_T xfer;
uint8_t txBuffer = EEPROM_READ_ADDR;

// Set up I2C transfer for writing address
xfer.slaveAddr = EEPROM_I2C_ADDR;
xfer.txBuff = &txBuffer;
xfer.txSz = 1;
xfer.rxBuff = NULL;
xfer.rxSz = 0;

if (Chip_I2C_MasterTransfer(I2C_INTERFACE, &xfer) != I2C_STATUS_DONE) {
LED_On(LED_RED_PORT, LED_RED_PIN);
return;
}

// Set up I2C transfer for reading data
xfer.txBuff = NULL;
xfer.txSz = 0;
xfer.rxBuff = buffer;
xfer.rxSz = len;

// Perform I2C transfer
if (Chip_I2C_MasterTransfer(I2C_INTERFACE, &xfer) != I2C_STATUS_DONE) {
LED_On(LED_RED_PORT, LED_RED_PIN);
} else {
LED_Off(LED_RED_PORT, LED_RED_PIN);
}
}

void LED_On(int port, int pin) {
Chip_GPIO_SetPinState(LPC_GPIO, port, pin, true);
}

void LED_Off(int port, int pin) {
Chip_GPIO_SetPinState(LPC_GPIO, port, pin, false);
}

int main(void) {

#if defined (__USE_LPCOPEN)
// Read clock settings and update SystemCoreClock variable
SystemCoreClockUpdate();
#if !defined(NO_BOARD_LIB)
// Set up and initialize all required blocks and
// functions related to the board hardware
Board_Init();
// Set the LED to the state of "On"
Board_LED_Set(0, true);
#endif
#endif

// Initialize I2C and LEDs
Init_I2C();
Init_LED();

// Define the data to write and buffer for reading
char writeData[] = "ozan";
char readData[sizeof(writeData)] = {0};

// Write data to EEPROM
Write_EEPROM(writeData, sizeof(writeData));

// Read data from EEPROM
Read_EEPROM(readData, sizeof(readData));

// Compare the written and read data
if (strcmp(writeData, readData) == 0) {
// Turn on green LED if successful
LED_On(LED_GREEN_PORT, LED_GREEN_PIN);
} else {
// Turn on red LED if unsuccessful
LED_On(LED_RED_PORT, LED_RED_PIN);
}

// Force the counter to be placed into memory
volatile static int i = 0 ;
// Enter an infinite loop, just incrementing a counter
while(1) {
i++ ;
// "Dummy" NOP to allow source level single
// stepping of tight while() loop
__asm volatile ("nop");
}
return 0 ;
}

0 项奖励
回复
3 回复数

1,123 次查看
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello  @Selkraps 

Transferring data from LPC1769 to EEPROM via I2C, strongly recommend you refer to the I2C demo under LPCopen. 

This example simulates two slave devices (default by I2C0), an EEPROM (0x5A) and an IO Extension device (0x5B). The EEPROM simulation is just to show how the I2Ccan be used as a device. Detail description about this demo please refer to readme.txt under project folder. You can refer to the code. If still have issue with yours. Please use a logic analyzer measure the signal. View the problem according to the I2C protocol.

LPCopen demo can directly import from MCUXpresso IDE. I made a video for the steps, please see attachment. Also you can download demo from website: https://www.nxp.com/design/design-center/software/software-library/lpcopen-software-development-plat...  

 

BR

Alice

 

0 项奖励
回复

1,068 次查看
Selkraps
Contributor I

Hello @alice_a,

First of all, thank you for your reply. I have found the example you are talking about and took a look at it and I think I understand the concept now. I have a small problem though. Is it possible to use I2C and EEPROM in this way by using microUSB cable? I try to check if the program works by tracking a variable while debugging because I don't have an access to a UART cable now but it doesn't work. This made me wonder if it is possible to do it with a microUSB cable. Thank you for your help.

 

0 项奖励
回复

1,003 次查看
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello @Selkraps 

Thanks for your feedback. 

Is your board a demo board from NXP or custom board?

If demo board lpcxpresso1769, we can use usb port print out the value of variable.

If there is no method print with UART or USB, you can also print to Console in MCUXpresso IDE. 

BR

Alice 

0 项奖励
回复