This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Programming to external EEPROM

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. :)

Parents
  • 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);
      }
    }
    

Reply
  • 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);
      }
    }
    

Children
  • #ifndef _SSC0_H
    #define _SSC0_H 1
    
    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;
    
    enum eepromStatus {
      RDY_ = 0x01, /* "1" indicates the write cycle is in progress */
      WEN  = 0x02, /* "1" indicates the device is WRITE ENABLED */
      BP0  = 0x04, /* protection range */
      BP1  = 0x08, /* protection range */
      WPEN = 0x80, /* Write enable see data sheet table */
    };
    
    typedef struct {
      EepromCommandType cmd;
      U8  hb;
      U8  lb;
      volatile BoolFalseTrueType busy;
      U16 bytesRx;
      U16 bytesTx;
    
      U16 txCnt;
      S8 *txptr;
    
      U16 rxCnt;
      S8 *rxptr;
      S8 data;
      S8 *baseptr;
    } EEPROM_CONTROL_TYPE;
    
    /* prototypes */
    void SSC0_Init(void);
    void ASC0_vInit(void);
    U8 EepromRead(U16 address, S8 *des, U16 cnt, U16 my_adress2);
    U8 EepromWrite(U16 address, S8 *src, U16 cnt, U16 my_adress1);
    U8 EepromReadStatus(U16 my_adress1);
    U8 EepromBusy(void);
    U8 EepromWriteEnable(U16 my_adress1);
    
    /* interrupt vector numbers */
    #define SSC0TINT    0x2D
    #define SSC0RINT    0x2E
    #define ASC0_TBINT   0x47
    #define ASC0_RINT    0x2B
    
    #endif  /* ifndef _SSC0_H */
    

  • didn't you say that 'EepromStartTx' cannot be changed without disabling your EEPROM? let me ask again: post the modified version of it, and only that function, that does not work. that is the source of the problem, isn't it (at least, according to you)?

  • In opinion of Chris, only the enum EepromCommandType must be changed (I have already changed it) and modify the start address of pointer, but i don't know how to do it. This is the modified version by me.

    _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)+MY_ADRESS;  /* reset pointer to first element */
      SSC0_TB         = *eeprom.txptr++;     /* write the first byte to the SSC0 tx buffer */
      }
    
    

  • Hello,
    Tamir, Chris, my nigthmare has ended. I have tested the last configuration of Chris, and the program works perfectly. I read /write the bytes in the wished address. It isn't necessary to add a new parameter in the
    EepromStartTx function. Only the pointer has to be increased by 1.

    _inline void EepromStartTx() {
      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 */
      }
    
    


    Thanks very much for your help.