This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

writing and reading from I2c eeprom ?

Dear Members,

Where can I find from software development kit the example for writing and reading from I2C EEPROM ?

Thanks

Parents Reply Children
  • The error code corresponds to NRF_ERROR_DRV_TWI_ERR_ANACK, which indicates that it received a NACK when it tried to address the sensor. There can be several reasons for this to happen. You should probably verify the communcation with the TWI scanner example first, as Awneil suggested.

  • TWI scanner result :

    I can not see 24C16 address

    Is it conflict between my LCD and 24C16 ?

  • Is it conflict

    As always, it's important to start simple; solve one problem at a time - don't leap straight into a whole pile of potential problems!

    https://www.avrfreaks.net/forum/help-it-doesnt-work

    So start with just the EEPROM on the bus - nothing else.

  • I can write it,

    but it's not complete , is it related with timing ?

    The output when reading 

    nfo> app: 7E0:
                                                                  

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: 07
                                                                    

    nfo> app: AD
                                             

    I want to write al 0xAD but only the end is AD ??

    Code :

    * Function uses the TWI interface to write data into EEPROM memory.
     *
     * @param     addr  Start address to write.
     * @param[in] pdata Pointer to data to send.
     * @param     size  Byte count of data to send.
     * @attention       Maximum number of bytes that may be written is @ref EEPROM_SEQ_WRITE_MAX.
     *                  In sequential write, all data must be in the same page
     *                  (higher address bits do not change).
     *
     * @return NRF_SUCCESS or reason of error.
     *
     * @attention If you wish to communicate with real EEPROM memory chip, check its readiness
     * after writing the data.
     */
    static ret_code_t eeprom_write(uint16_t addr, uint8_t const * pdata, size_t size)
    {
        ret_code_t ret;
        /* Memory device supports only a limited number of bytes written in sequence */
        if (size > (EEPROM_SEQ_WRITE_MAX_BYTES))
        {
            return NRF_ERROR_INVALID_LENGTH;
        }
        /* All written data must be in the same page */
        if ((addr / (EEPROM_SEQ_WRITE_MAX_BYTES)) != ((addr + size - 1) / (EEPROM_SEQ_WRITE_MAX_BYTES)))
        {
            return NRF_ERROR_INVALID_ADDR;
        }
        do
        {
    			  nrf_delay_ms(25);
            uint8_t buffer[EEPROM_ADDRESS_LEN_BYTES + EEPROM_SEQ_WRITE_MAX_BYTES]; /* Addr + data */
    
            memcpy(buffer, &addr, EEPROM_ADDRESS_LEN_BYTES);
            memcpy(buffer + EEPROM_ADDRESS_LEN_BYTES, pdata, size);
            ret = nrf_drv_twi_tx(&m_twi_master, EEPROM_ADDR, buffer, size + EEPROM_ADDRESS_LEN_BYTES, false);
    			  
        }while (0);
        return ret;
    }
    
    void eeprom_cmd_clear()
    {
        
    
        uint8_t clear_val = 0xAD;
        size_t addr;
    
        for (addr = 0; addr <EEPROM_SIZE; ++addr)
        {
            ret_code_t err_code;
            err_code = eeprom_write(addr, &clear_val, 1);
    			   if (NRF_SUCCESS != err_code)
            {
    					NRF_LOG_INFO("communication error 24C16 By Rixtronix LAB.\r\n");
              NRF_LOG_INFO("Error code %u",err_code);  
                return;
            }
            NRF_LOG_INFO("CLEARING 24C16 By Rixtronix LAB...\r\n");
        }
    		NRF_LOG_INFO("FINISHED CLEARING 24C16 By Rixtronix LAB.\r\n");
        
    }
    
    

    What do I miss here  ?

    Thanks

  • #define EEPROM_SEQ_WRITE_MAX_BYTES    200
    #define EEPROM_ADDR                   0x50
    #define EEPROM_ADDRESS_LEN_BYTES   2
    #define EEPROM_SIZE                   (2048u) //!< 24C16 EEPROM size.
    #define MASTER_TWI_INST     0       //!< TWI interface used as a master accessing EEPROM memory.
    #define IN_LINE_PRINT_CNT   (16u)   //!< Number of data bytes printed in a single line.

Related