Discussion in "ARM Development" started by    sachinmm    Sep 29, 2015.
Tue Sep 29 2015, 01:15 pm
#1
/*hello sir i am facing error in I2C programming on ARM7 lpc2148 platform.....i am interfacing EEPROM with I2C..
there are two file I2C.c FOR DEFINITION and I2C.h FOR DECLARATION

my declaration is void __irq I2C0_Status(void); //for interupt
my definition is void __irq I2C0_Status(void)
{
Status_Flag = 0xFF; //update status flag
Status = I20STAT ; //Read Status byte
I20CONCLR = 0x28; //I2C interrupt Clear bit and START flag Clear bit.
VICVectAddr = 0x00; //Acknowledge Interrupt
}
my UART INIT function
void i2c_init()
{
PINSEL0 = 0x00000050; //set the pin SDA & SCK
I20CONCLR = 0x6C; //clearing the bits of AAC/SIC/STAT/I2ENC in I2C0CONCLR register for controlling the i2c operation
I20CONSET = 0x40; //set the I2EN bit enable the i2c interface
I20SCLH = 80; //Count for SCL HIGH time period selection
I20SCLL = 70; //Count for SCL LOW time period selection

// Init VIC for I2C0
VICIntSelect = 0x00000000; // Setting all interrupts as IRQ(Vectored)
VICVectCntl0 = 0x20 | 9; // Assigning Highest Priority Slot to I2C0 and enabling this slot
VICVectAddr0 = (unsigned long)I2C0_Status; // Storing vector address of I2C0
VICIntEnable = 0x00000200;
}

when i am compiling i got error
I2C.c:99: error: parse error before "I2C0_Status" =??
I2C.h:31: error: parse error before "I2C0_Status" =??

*/
Tue Sep 29 2015, 01:16 pm
#2
i am confused in "__irq"....how it is uesd??


[ Edited Tue Sep 29 2015, 01:16 pm ]
Tue Sep 29 2015, 04:41 pm
#3
my I2C.c file=>>
/*************************************I2C.c***********************************************************
Description : this application code demostrates I2C interface with EEPROM on LPC2148 development board
Author : Sachin More
Date : 24/09/2015
Platform : LPC2148 Development Board.
Toolchain : arm-fsl-linux-gnueabi-gcc
********************************************************************************************************/
#include "I2C.h"
#include "LPC214x.h"
/**********************************I2C_Init starts******************************************************
Description : I2C initialization
Input parameter :
Return type : zero
*********************************************************************************************************/
void i2c_init()
{
PINSEL0 = 0x00000050; //set the pin SDA & SCK
I20CONCLR = 0x6C; //clearing the bits of AAC/SIC/STAT/I2ENC in I2C0CONCLR register for controlling the i2c operation
I20CONSET = 0x40; //set the I2EN bit enable the i2c interface
I20SCLH = 80; //Count for SCL HIGH time period selection
I20SCLL = 70; //Count for SCL LOW time period selection

/* Init VIC for I2C0 */
/*VICIntSelect = 0x00000000; // Setting all interrupts as IRQ(Vectored)
VICVectCntl0 = 0x20 | 9; // Assigning Highest Priority Slot to I2C0 and enabling this slot
VICVectAddr0 = (unsigned long)I2C0_Status; // Storing vector address of I2C0
VICIntEnable = 0x00000200; */
}
/**********************************uart0Init starts******************************************************
Description : UART0 initialization
Input parameter : PINSEL0/U0LCR/U0DLL/U0DLM/U0LCR
Return type : zero
*********************************************************************************************************/
void uart0Init()
{
PINSEL0 = 0X00000005; //this pin used as UART0
U0LCR = 0x83; //this register value transmit 8 bit charactor and set DLAB BIT TO 1
U0DLL = BAUDRATE&0xff; //contains the lower 8 bits of the divisor
U0DLM = BAUDRATE>>8; //contains the higher 8‐bit of the divisor
U0LCR = 0x03; //this register value set DLAB BIT TO 0
}
/**********************************uart0TransmitChar starts**********************************************
Description : transmitting the char over UART0
Input parameter : string
Return type : zero
*********************************************************************************************************/
void uart0TransmitChar(char data)
{
while (!(U0LSR & (1<<5))); //wait untill the THR is empty
U0THR = data; //if empty we can write to the registor
}
/**********************************uart0TransmitString starts********************************************
Description : transmitting the string over UART0
Input parameter : string
Return type : zero
*********************************************************************************************************/
void uart0TransmitString(char *string)
{
while(*string != '\0' ) //wait untill *string is point to null charactor
{
uart0TransmitChar(*(string++)); //increment pointer of string to the next charactor
}

}
/**********************************delay starts**********************************************************
Description : fuction for some delay
Input parameter : value
Return type : zero
*********************************************************************************************************/
void delay(int value)
{
int i,j;
for(i=0;i<value;i++);
for(j=0;j<5500000;j++);
}
/**********************************send starts************************************************************
Description : fuction for START flag is set
Input parameter :
Return type : zero
*********************************************************************************************************/
void Send_Start()
{
I20CONSET = 0x20; //setting START flag is set and enter in master mode to transmit start condition
}
/**********************************stop starts***********************************************************
Description : fuction for STOP flag is set
Input parameter :
Return type : zero
********************************************************************************************************/
void Send_Stop()
{
I20CONSET = 0x10; //setting STOP flag is set and enter in master mode to transmit stop condition
}
/*****************************I2C0_STATUS starts*********************************************************
Description : fuction for I2C status
Input parameter :
Return type : zero
*********************************************************************************************************/
/*void __irq I2C0_Status(void)
{
Status_Flag = 0xFF; //update status flag
Status = I20STAT ; //Read Status byte
I20CONCLR = 0x28; //I2C interrupt Clear bit and START flag Clear bit.
VICVectAddr = 0x00; //Acknowledge Interrupt
} */
/*****************************I2C_STATUS starts**********************************************************
Description : fuction for I2C status
Input parameter : char status code
Return type : unsigned char
*********************************************************************************************************/
unsigned char I2C_Status(unsigned char status_code)
{
while(Status_Flag==0);
Status_Flag=0;
if(Status!=status_code)
{
return 1;
}
else
{
return 0;
}
}
/*****************************I2C_Send starts***********************************************************
Description : This I2C_send function sends data to the EEPROM 24LC04
Input parameter : *Data & Len
Return type : unsigned char
********************************************************************************************************/
unsigned char I2C_Send(unsigned char *Data,unsigned char Len)
{
while(Len)
{
I20DAT=*Data;
if(I2C_Status(0x28))
{
return 1;
}
Len--;
Data++;
}
return 0;
}
/*****************************I2C_Read starts**********************************************************
Description : This function reads data from the EEPROM 24LC04
Input parameter : *Data & Len
Return type : unsigned char
*******************************************************************************************************/
unsigned char I2C_Read(unsigned char *Data,unsigned char Len)
{
while(Len)
{
if(Len==1) //Last byte
{
I20CONCLR=0x04; //set hardware to send nack
if(I2C_Status(0x58)) //last byte has been received and NACK has been returned
{
return 1;
}
*Data = I20DAT;
}
else
{
I20CONSET = 0x04; //set hardware to send ack
if(I2C_Status(0x50)) //0x50 means Byte has been received & ACK has been returned
{
return 1;
}
*Data = I20DAT;
}
Data++;
Len--;
}
return 0;
}
/*****************************EEPROM_Write starts*******************************************************
Description : This function writes data from the EEPROM 24LC04
Input parameter : BLOCK_NUMBER & BLOCK_ADDR
Return type : unsigned char
*******************************************************************************************************/
unsigned char EEPROM_Write(unsigned char BLOCK_NUMBER,unsigned char BLOCK_ADDR)
{
Send_Start();
if(I2C_Status(0x08)) //Start has been transmitted
{
return 1;
}
I20DAT = DEVICE_ADDR | BLOCK_NUMBER; // Send Address and block number to I2C0DAT register
if(I2C_Status(0x18)) //Device address, block num and write has been transmitted with ack signal is received
{
return 1;
}
I20DAT = BLOCK_ADDR; // Send block address
if(I2C_Status(0x28)) //Block address has been transmitted with ack received
{
return 1;
}
if(I2C_Send(I2C_WRITE,MAX_BUFFER_SIZE)) //Send Data to the EEPROM
{
Send_Stop();
return 1;
}
Send_Stop();
return 0;
}
/*****************************EEPROM_Read starts********************************************************
Description : This function read data from the EEPROM 24LC04
Input parameter : BLOCK_NUMBER & BLOCK_ADDR
Return type : unsigned char
*******************************************************************************************************/
unsigned char EEPROM_Read(unsigned int BLOCK_NUMBER,unsigned char BLOCK_ADDR)
{
Send_Start();
if(I2C_Status(0x08)) //Start has been transmitted
{
return 1;
}
I20DAT = DEVICE_ADDR | BLOCK_NUMBER; // Send Address
if(I2C_Status(0x18)) //Device address, block num and write has been transmitted
{
return 1;
}
I20DAT = BLOCK_ADDR;
if(I2C_Status(0x28)) //Block address has been transmitted with ack received
{
return 1;
}
Send_Start(); // Repeat Start
if(I2C_Status(0x10)) //Repeated Start has been transmitted
{
return 1;
}
I20DAT = DEVICE_ADDR | BLOCK_NUMBER | 0x01;
if(I2C_Status(0x40)) //Device address, block num and read has been transmitted
{
return 1;
}
if(I2C_Read(I2C_READ,MAX_BUFFER_SIZE)) //Receive 16bytes of Data from EEPROM
{
Send_Stop();
return 1;
}
Send_Stop();
return 0;
}
/*****************************main starts***********************************************************
Description : main function
Input parameter : zero
Return type : int
****************************************************************************************************/
int main(void)
{
PINSEL0 = 0x00000005; // Enable GPIO on all pins
PINSEL1 = 0x00000000;
PINSEL2 = 0x00000000;
IODIR1 = (1<<19) | (1<<18) | (1<<17) | (1<<16); // Set P1.16, P1.17, P1.18, P1.19 as Output
LED1_OFF();LED2_OFF();LED3_OFF();LED4_OFF();
uart0Init();
i2c_init();
sys_init();
LED1_ON(); //Write Indicator
if(EEPROM_Write(BLK_1,0x00))
{
uart0TransmitString("Write Failed");
LED3_ON();
}
LED1_OFF();
delay(1);
LED2_ON(); //Read indicator
if(EEPROM_Read(BLK_1,0x00))
{
uart0TransmitString("Read Failed");
LED3_ON();
}
else
{
uart0TransmitString(I2C_READ);
LED4_ON();
}
LED2_OFF();
while(1)
{

}
}

my I2C.h file ==>>
/**************************************I2C.h***********************************************************
Description : this file includes all function declaration of I2C interface with EEPROM on LPC2148 development board
Author : Sachin More
Date : 24/09/2015
Platform : LPC2148 Development Board.
Toolchain : arm-fsl-linux-gnueabi-gcc
******************************************************************************************************/
#include "LPC214x.h"
#include "startup.h"

//void uart0Init(); //initialization of UART0 function
#define BAUDRATE 60000000/(16*9600) //set the baud rate 9600
void uart0TransmitString(char *string); //declaration for transmit string function
void uart0TransmitChar(char data); //declaration for transmit char function
void delay(int value); //declaration for delay function
//void i2c_init(); //initialization of I2C function

/*IOSET1 for ON the LED'S*/
#define LED1_ON() IOSET1=(1<<16) //for I2C write indicator
#define LED2_ON() IOSET1=(1<<17) //for I2C read indicator
#define LED3_ON() IOSET1=(1<<18) //for Communication failure indicator
#define LED4_ON() IOSET1=(1<<19) //for Communication success indicator

/*IOCLR1 for off the LED'S*/
#define LED1_OFF() IOCLR1=(1<<16)
#define LED2_OFF() IOCLR1=(1<<17)
#define LED3_OFF() IOCLR1=(1<<18)
#define LED4_OFF() IOCLR1=(1<<19)

//void Send_Start(); //function for setting the START
//void Send_Stop(); //function for setting the STOP
//void __irq I2C0_Status(void); //for interupt
unsigned char I2C_Status(unsigned char status_code);
#define DEVICE_ADDR 0xA0 //device address is 0XA0
#define MAX_BUFFER_SIZE 16 //define 16 bits size
unsigned char I2C_WRITE[MAX_BUFFER_SIZE]={"Atishay Infotech"};
unsigned char I2C_READ[MAX_BUFFER_SIZE];
unsigned char Status=0;
unsigned char Status_Flag=0;
#define BLK_1 0x02 //block number of EEPROM replace with BLK_1 identifire
unsigned char I2C_Send(unsigned char *Data,unsigned char Len); //This function sends data to the EEPROM 24LC04
unsigned char I2C_Read(unsigned char *Data,unsigned char Len); //This function reads data from the EEPROM 24LC04
unsigned char EEPROM_Write(unsigned char BLOCK_NUMBER,unsigned char BLOCK_ADDR); //This function writes data to the EEPROM 24LC04
unsigned char EEPROM_Read(unsigned int BLOCK_NUMBER,unsigned char BLOCK_ADDR); //This function read data to the EEPROM 24LC04



but i dindn get output???
Wed Sep 30 2015, 12:01 am
#4


i am confused in "__irq"....how it is uesd??

sachinmm


How interrupt handlers are defined is different for every compiler.
Which one are you using ?

Check your help section and look for examples for your compiler.
Wed Sep 30 2015, 03:09 am
#5
__irq is just compiler directive to wrap IRQ routine properly. The main job of interrupt and IRQ handler mapping is done by vector interrupt controller "VIC"
/* Init VIC for I2C0 */
 /*VICIntSelect = 0x00000000; // Setting all interrupts as IRQ(Vectored)
 VICVectCntl0 = 0x20 | 9; // Assigning Highest Priority Slot to I2C0 and enabling this slot
 VICVectAddr0 = (unsigned long)I2C0_Status; // Storing vector address of I2C0
 VICIntEnable = 0x00000200; */


I see that you have commented that part of code, You need to look into datasheet to get more information on VIC.
Wed Sep 30 2015, 02:24 pm
#6
how to used polling method in this?? my code is compile sucessfully but not get the output on uart..


[ Edited Thu Oct 01 2015, 11:01 am ]
Thu Nov 05 2015, 05:28 pm
#7
Polling is not simpler either. I think its the way you're writing your code. Its better to use a switch case statement to handle all the states properly. You can get reference driver from NXP website to start with.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am