Hello. I need some help. I'm trying to write, read and erase an external EEPROM (25AA040 MICROCHIP) via SPI from XC164CS board, for example, one byte at 0x00 adress of the eeprom, but i don't know how to do it. Into the datasheet, I can see some instructions but i don't know what to do with them: img28.imagevenue.com/img.php Could anyone help me? Thank you very much. :)
I never worked with SPI but rather with I2C, so I don't know for sure. I think that your datasheet might tell you what you need to place on the data bus and what you need to place on the address bus in order to communicate with your device. A simple assignment into a memory mapped region will get the job done.
Perhaps you would like to look at the application note on the Infineon web site...
Interfacing the XC16x Microcontroller to a Serial SPI EEPROM -description (ap1609510_XC16x_SPI_EEPROM.pdf)
There is a pdf with code.
www.infineon.com/.../channel.html
I don't have any type of hardware to test the programs :(. I can only use the ASC0 and hyperterminal to show the bytes I read/write from EEPROM and for every tests. In my project we don't have much money to buy debuggers, such as uLink jejeje.
Hello Chris, Despite I have tried so much, I am not able to read what I have written in the EEPROM. I am trying to store everything I receive from EEPROM in an array located in reception interruption. I only get as answer [00][FF]. What could it be the problem? Thank you.
John, Despite our desire to help you, we are not mind readers. Please post the core of your code - just the essentials so that it can be analyzed.
Tamir, if you want, I will send you the keil proyect to your mail right?
John, I don't have your hardware so sending me your code is not going to help a lot. Besides I am at work and I don't time too much time...Just post a couple of code snippets: your peripheral configuration, ISRs, and buffers that interact with incoming data. Are you sure your external bus interface is correctly configured? Did you try to minimize the scope of the program to the absolute minimum, so that you can rule out external interferences? That can you and us, too.
Hello John,
I don't see the same issue so something must be different in your implementation. What pins are connected from the serial EERPOM to what pins of the XC164CS. What about the WR and HOLD pins (they are tied high in my configuration)?
The read and write routines are completely interrupt driven and are the same implementation only the instruction (sent to the serial EEPROM) is changed.
You would need to isolate your problem/issue and properly communicate the defect for someone else to understand what the cause might be and provide suggestions.
If you run the code using the simulator you will see that the interrupts are more than two. You would not have the correct data but the transfers should work.
-Chris
void SSC0_viRx(void) interrupt SSC0RINT { if (eeprom.bytesRx < eeprom.rxCnt) { eeprom.bytesRx++; if ((eeprom.cmd == READ) && (eeprom.bytesRx >= START_RX_MSG)) { *eeprom.rxptr++ = (S8) (SSC0_RB); } else { eeprom.data = (S8) (SSC0_RB); /* */ my_array[j] = SSC0_RB; j++; if (j == NUMBER) { flag = 1; ASC0_TBIC_IR = 1; } } if (eeprom.bytesRx >= eeprom.rxCnt) { eeprom.busy = false; CS_EEPROM = high; } } } void ASC0_viTxBuffer(void) interrupt ASC0_TBINT using RB_LEVEL14 { if ((j > 0) && (flag == 1)) { j--; ASC0_TBUF = mi_array[j]; if (j == 0) flag = 0; } }
void SSC0_viRx(void) interrupt SSC0RINT { if (eeprom.bytesRx < eeprom.rxCnt) { eeprom.bytesRx++; if ((eeprom.cmd == READ) && (eeprom.bytesRx >= START_RX_MSG)) { *eeprom.rxptr++ = (S8) (SSC0_RB); } else { eeprom.data = (S8) (SSC0_RB); /* */ my_array[j] = SSC0_RB; j++; if (j == NUMBER) { flag = 1; ASC0_TBIC_IR = 1; } } if (eeprom.bytesRx >= eeprom.rxCnt) { eeprom.busy = false; CS_EEPROM = high; } } } void ASC0_viTxBuffer(void) interrupt ASC0_TBINT using RB_LEVEL14 { if ((j > 0) && (flag == 1)) { j--; ASC0_TBUF = my_array[j]; if (j == 0) flag = 0; } }
Hi John,
You have changed the behavior of the interrupt service routines as I defined them (Which is fine but I think you missed what was going on). Concerning the receive interrupt you are unloading the data stream in the wrong place. Concerning the transmit interrupt you aren't sending any more bytes which is a problem.
Remember you still need to transmit to read.
Have a look again to the differences...
void SSC0_viTx(void) interrupt SSC0TINT { if (eeprom.bytesTx < eeprom.txCnt) { eeprom.bytesTx++; if (TxMode == WRITE) { /* write mode */ if ((eeprom.cmd == WRITE) && (eeprom.bytesTx == START_TX_MSG)) { eeprom.txptr = eeprom.baseptr; /* switch pointer to data */ } SSC0_TB = *eeprom.txptr++; /* write user data */ } else { /* ok READ mode */ if ((eeprom.cmd == READ) && (eeprom.bytesTx >= START_TX_MSG)) { SSC0_TB = 0; /* when reading data just send zeros */ } else { SSC0_TB = *eeprom.txptr++; /* send command and address */ } } } } void SSC0_viRx(void) interrupt SSC0RINT { if (eeprom.bytesRx < eeprom.rxCnt) { eeprom.bytesRx++; if ((eeprom.cmd == READ) && (eeprom.bytesRx >= START_RX_MSG)) { /* data stream read here! */ *eeprom.rxptr++ = (S8) (SSC0_RB); /* read user data */ } else { eeprom.data = (S8) (SSC0_RB); /* used for non-user data */ } if (eeprom.bytesRx >= eeprom.rxCnt) { eeprom.busy = false; /* indicate the resource is free */ CS_EEPROM = high; /* all data sent, release /CS */ } } }
Ok after I sent the last post I realized you posting the code for the ASC0 interrupt (not the SSC ts interrupt) to send the data that was read from the serial EEPROM. So my comments to the SSC0 interrupt are still valid.
Ok, I just obey you and it is the result :)
... #define NUMBER 48 const S8 txBuf[] = "Welcome to XC164CS Serial EEPROM reading/writing"; ... void SSC0_viRx(void) interrupt SSC0RINT { if (eeprom.bytesRx < eeprom.rxCnt) { eeprom.bytesRx++; if ((eeprom.cmd == READ) && (eeprom.bytesRx >= START_RX_MSG)) { ///////////////////////////////////////////////// my_array[j] = SSC0_RB; j++; if (j == NUMBER) { flag = 1; ASC0_TBIC_IR = 1; } ///////////////////////////////////////////////// *eeprom.rxptr++ = (S8) (SSC0_RB); /* read user data */ } else { eeprom.data = (S8) (SSC0_RB); /* used for non-user data */ } if (eeprom.bytesRx >= eeprom.rxCnt) { eeprom.busy = false; /* indicate the resource is free */ CS_EEPROM = high; /* all data sent, release /CS */ } } } void ASC0_viTxBuffer(void) interrupt ASC0_TBINT using RB_LEVEL14 { if ((j > 0) && (flag == 1)) { j--; ASC0_TBUF = my_array[j]; if (j == 0) flag = 0; } }
Hyperterminal: img171.imagevenue.com/img.php
It's succesfull :)
Now, I would like to read SC461CX only. What parameters do I have to introduce in the readEeprom function?
First you need to figure out why you are reading the bytes in the reverse order (reading right to left). You want to read from the XC164CS not SC461CX.
Not sure what you are asking as an example was supplied... address -> linear address of the serial EEPROM where you want to start reading from. &rxBuf[0] -> pointer to the first byte to receive the data from the serial EPEROM. txSize -> the amount of bytes you want to read from the serial EEPROM.
/* read back the bytes we wrote to the EEPROM */ EepromRead(address, &rxBuf[0], (U16) txSize); while(EepromBusy());
Since the EepromRead routine is served with interrupts and can also be in the background. If you need the data immedatly then you need to wait for the EEPROM to be free (not busy).
1-.The reverse order doesn't matter. It has easy solution. Just look at ASC0 interrupt. The last byte read from EEPROM is going to be the first one to be stored in my_array[j].
2-. img206.imagevenue.com/img.php
I tried programming whit this code but it do
/* read back the bytes we wrote to the EEPROM */ EepromRead(11, &rxBuf[11], (U16) 7); while(EepromBusy());
img230.imagevenue.com/img.php
What might be the problem?
Basically the enum represents 16-bits on the XC166 so need to adjust for this in the eeprom struct.
Then, shall I only have to adjust the EEPROM struct? I will try. By the way, we have already bought the Keil uLink2 JTAG Debugger for training my project and future developments :)
You would need to change the values in the enum EepromCommandType and also modify the start address of the variable eeprom.txptr in the function EepromStartTx.
1-. Aren't the values of EepromCommandType enum constants?
2-.
... eeprom.txptr = (S8 *)(&eeprom); /* reset pointer to first element */ ...
This type of instruction is new for me and I dont know it very well. I know that the pointer is reseted to fist element but could you explain me a bit? How to change the value of eeprom.txptr Thank you
John, First allow me to recommend that you refer to a C text book to extend your understanding of pointers. I don't know what kind of application your are writing, but do remember that making a mistake with pointers is likely to kill your controller, and could risk lives in the process! as to your question:
eeprom.txptr = (S8 *)(&eeprom);
'eeprom' is a data structure that contains a member pointer to a 8 bit data type (that is 'txptr'. we can assume it is a unsigned char). that member is pointed to the beginning of 'eeprom'. that means that dereference of 'txptr' will be an unsigned char, or the first byte of the 'eeprom' structure, where I guess some text is stored (see the structure layout). note that incrementing the value of txptr by 1 will point it to the next unsigned char of the structure.
&eeprom
returns the address of 'eeprom', and that address is then casted to a unsigned char pointer so that the assignment will not cause the compiler to complain.
Ok Tamir, I understand your text. I have a new question. If I have to increase the pointer "x" times, is necessary to intriduce a new parameter in the function? For example:
_inline void EepromStartTx(U16 MY_ADRESS) { CS_EEPROM = low; eeprom.bytesTx = 1; /* decrement the transmit count by one */ eeprom.bytesRx = 0; eeprom.txptr = (S8 *)(&eeprom); /* reset pointer to first element */ eeprom.txptr = eeprom.txptr + MY_ADRESS; /* put the pointer in my first element */ SSC0_TB = *eeprom.txptr++; /* write the first byte to the SSC0 tx buffer */ }
eeprom.txptr = (S8 *)(&eeprom); /* reset pointer to first element */ eeprom.txptr = eeprom.txptr + MY_ADRESS; /* put the pointer in my first element */
can be compacted to
eeprom.txptr = (S8 *)(&eeprom) + MY_ADRESS ;
I would expect a function prototype like this:
void EepromStartTx(U16 MY_ADRESS, unsigned char *ap_buffer, unsigned int a_buffer_length)
note that I removed the inline directive as it might probably be required to insert a loop into your function.
Its imposible. If I modify anything in EepromStartTxthe EEPROM only returns zeros. Any suggestion?? I just need to know this :(
how then do you expect to solve your problem is you cannot change anything? there is a logical empirical explanation as to why it does not work anymore. why don't you post a revision that does not work.
The code is too long to post here. Take the proyect at this adress: rapidshare.com/.../eeprom.rar.html
I am not installing any of this rapidshare junk on my PC, and anyway I don't have time to review your code. The product hits the market by the end of this week! depart from a working revision, change your EEPROM write function, and post it here if it does not work.
I am not sure how you are going to maintain code that you didn't write or don't understand. The driver starts with the serial command and address then switches to the user address by a counter in the interrupt routine.
typedef enum { WRSR = 0x0100, /* Write Status Register */ WRITE = 0x0200, /* Write Data to Memory Array */ READ = 0x0300, /* Read Data from Memory Array */ WRDI = 0x0400, /* Reset Write Enable Latch */ RDSR = 0x0500, /* Read Status Register */ WREN = 0x0600, /* Set Write Enable Latch */ } EepromCommandType;
_inline void EepromStartTx(void) { CS_EEPROM = low; eeprom.bytesTx = 1; /* decrement the transmit count by one */ eeprom.bytesRx = 0; eeprom.txptr = (S8 *)(&eeprom)+1; /* reset pointer to first element */ SSC0_TB = *eeprom.txptr++; /* write the first byte to the SSC0 tx buffer */ }
#include <XC164.h> #include <intrins.h> #include "types.h" #include "utilities.h" #include "ssc0.h" #include "variables.h"
const S8 txBuf[] = "Welcome"; const U16 txSize = (sizeof(txBuf) - 1); S8 rxBuf[64];
/**/ void main (void) { U16 address = 0; /* EEPROM address */ U16 my_adress1 = 0; /* Write Adress */ U16 my_adress2 = 3; /* Read Adress */
SSC0_Init(); /* initialize the SSC peripheral */ ASC0_vInit(); /* initialize the ASC peripheral */ PSW_IEN = 1; /* globally enable interrupts */
DP9 = 0x0010; // load direction register
flag = 0;
/* write bytes to the EEPROM */ EepromWriteEnable(0); while(EepromBusy());
/* max page size if 64 bytes in a write */ EepromWrite(address, (S8 *) &txBuf[0], (U16) txSize,0); while(EepromBusy());
/* wait for WRITE command to complete */ while((EepromReadStatus(0) & RDY_) == true);
/* read back the bytes we wrote to the EEPROM */ EepromRead(address, &rxBuf[0], 6, 0); while(EepromBusy());
while(1) { _nop_(); /* loop forever */ } }
where is "EepromStartTx"?
What you have said, I understand it. What I don't know to do is how to modify the pointer to change the writing/reading start adress that I want.
_inline void EepromStartTx(U16 MY_ADRESS) { CS_EEPROM = low; eeprom.bytesTx = 1; /* decrement the transmit count by one */ eeprom.bytesRx = 0; eeprom.txptr = (S8 *)(&eeprom)+1; /* reset pointer to first element */ SSC0_TB = *eeprom.txptr++; /* write the first byte to the SSC0 tx buffer */ } /**/ void SSC0_viTx(void) interrupt SSC0TINT { if (eeprom.bytesTx < eeprom.txCnt) { eeprom.bytesTx++; if (TxMode == WRITE) /* write mode */ { if ((eeprom.cmd == WRITE) && (eeprom.bytesTx == START_TX_MSG)) { eeprom.txptr = eeprom.baseptr; /* switch pointer to data */ } SSC0_TB = *eeprom.txptr++; /* write user data */ } else { /* ok READ mode */ if ((eeprom.cmd == READ) && (eeprom.bytesTx >= START_TX_MSG)) { SSC0_TB = 0; /* when reading data just send zeros */ } else { SSC0_TB = *eeprom.txptr++; /* send command and address */ } } } } /**/ void SSC0_viRx(void) interrupt SSC0RINT { if (eeprom.bytesRx < eeprom.rxCnt) { eeprom.bytesRx++; if ((eeprom.cmd == READ) && (eeprom.bytesRx >= START_RX_MSG)) { ///////////////////////////////////////////////// mi_array[j] = SSC0_RB; j++; if (j == NUMERO) { flag = 1; ASC0_TBIC_IR = 1; } ///////////////////////////////////////////////// *eeprom.rxptr++ = (S8) (SSC0_RB); /* read user data */ } else { eeprom.data = (S8) (SSC0_RB); /* used for non-user data */ } if (eeprom.bytesRx >= eeprom.rxCnt) { eeprom.busy = false; /* indicate the resource is free */ CS_EEPROM = high; /* all data sent, release /CS */ } } }
U8 EepromWrite(U16 address, S8 *src, U16 cnt, U16 MY_ADRESS) { U8 result = failure; if (eeprom.busy == false) { TxMode = WRITE; eeprom.busy = true; eeprom.cmd = WRITE; eeprom.baseptr = src; eeprom.lb = (U8) (address); eeprom.hb = (U8) (address >> 8u); eeprom.txCnt = eeprom.rxCnt = cnt + 3; EepromStartTx(MY_ADRESS); result = success; } return (result); } /**/ U8 EepromRead(U16 address, S8 *des, U16 cnt, U16 MY_ADRESS) { U8 result = failure; if (eeprom.busy == false) { TxMode = READ; eeprom.busy = true; eeprom.cmd = READ; eeprom.rxptr = des; eeprom.lb = (U8) (address); eeprom.hb = (U8) (address >> 8); eeprom.txCnt = eeprom.rxCnt = cnt + 3; EepromStartTx(MY_ADRESS); result = success; } return (result); } /**/ U8 EepromWriteEnable(U16 MY_ADRESS) { U8 result = failure; if (eeprom.busy == false) { TxMode = WRITE; eeprom.busy = true; eeprom.cmd = WREN; eeprom.rxptr = &eeprom.data; eeprom.txCnt = eeprom.rxCnt = 1; EepromStartTx(MY_ADRESS); result = success; } return (result); } /**/ U8 EepromReadStatus(U16 MY_ADRESS) { while(eeprom.busy == true); /* wait for transfer to complete */ TxMode = WRITE; eeprom.busy = true; eeprom.cmd = RDSR; /* write RDSR command in eeprom tx buffer */ eeprom.txCnt = eeprom.rxCnt = 2; EepromStartTx(MY_ADRESS); /* kick start the SSC transmission */ /* Wait for the result, this should be changed to be part of the OS so it * is not a waiting task */ while(eeprom.busy == true); /* wait for transfer to complete */ return(eeprom.data); /* return the status byte read from the EEPROM */ } /**/ U8 EepromBusy(void) { if (eeprom.busy == true) { return(true); } else { return(false); } }
View all questions in Keil forum