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 ;
}