Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Regarding reading values from EEPROM

Status
Not open for further replies.

achar.deepak

Member level 1
Joined
Dec 12, 2016
Messages
33
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,696
Hi all,

I have stuck in reading the value from EEPROM. For every time it is reading 0 only. I have attached sample code year from RTC. I am storing year in the eeprom. It is not reading. Please help.

Code:
struct menu year_function(struct menu Initialsetup)
{
	unsigned int value_read3;
	
	int keyvalue;
	
	unsigned char eeprom_value = 0;
	
	unsigned char status=0;
	
	if(Initialsetup.language == 0)
	Initialsetup.ChoiceOfLanguage = 0;
	else
	Initialsetup.ChoiceOfLanguage = Initialsetup.language;
	
	lcdClear();
	
	Initialsetup.Current_mode = Third_mode_year;
	
	Initialsetup.value_store = 2000+year;
	
	AT25160B_init();
	
	while(1)
	{
		keyvalue = keyboard_get_key_state();
		
		if(keyvalue == UP)
		{
			if(Initialsetup.Current_mode == Third_mode_year)
			{
				Initialsetup.value_store++;
				if(Initialsetup.value_store >= 2040) Initialsetup.value_store = 2014;
			}
		}
		if(keyvalue == DOWN)
		{
			if(Initialsetup.Current_mode == Third_mode_year)
			{
				Initialsetup.value_store--;
				if(Initialsetup.value_store <= 2014) Initialsetup.value_store = 2030;
			}
		}
		if((keyvalue==ENTER)&&(status == 0))
		{
			delay_ms(100);
			
			Initialsetup.datetime[0] = Initialsetup.value_store;
			Initialsetup.datetime[0] = Initialsetup.datetime[0] % 100;
			year = Initialsetup.datetime[0];
			
			spi_eeprom_Write(0x000f, &year, sizeof(year));
			value_read3 = spi_eeprom_Read(0x000f);
			lcdClear();
			lcdMoveCursor(1,5);
			print_integer(value_read3);
			delay_s(3);
			
			Initialsetup.Current_mode = Fourth_mode_month;
			
			status = 1;
			return Initialsetup;
		}
		if((keyvalue == ESC)&&(status == 0))
		{
			Initialsetup_DisplayChange_strings(Initialsetup);
			
			Initialsetup = country_menu_function(Initialsetup);
			
			if(Initialsetup.value_store == -1)
			{
				return Initialsetup;
			}
			
			Initialsetup = year_function(Initialsetup);
			
			status = 1;
			
			return  Initialsetup;
		}
		if((keyvalue == NEXT)&&(status == 0))
		{
			Initialsetup.datetime[0] = Initialsetup.value_store;
			status = 1;
			return Initialsetup;
		}
		
		if((keyvalue == ZERO)&&(status == 1))
		{
			status = 0;
		}
		
		Initialsetup_Display_string(Initialsetup);
	}
}

Thanks
Deepak
 

Your eeprom data for year is two bytes and you have to store it as two bytes and read them as two bytes.
 

Hi,

I am writing to EEPROM like this. I am getting error as conflicting types for 'spi_eeprom_Write'. Please help.

Code:
void spi_eeprom_Write(uint16_t memAddr, uint16_t data[],uint16_t len)
{
	delay_ms(2);
	spi_eeprom_Write_Enable();	
	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	uint16_t packet[3]={EEPROM_DATA_WRITE,memAddr>>16,memAddr&0xFFFF};
	spi_write_packet(AT25160B_SPI,packet,3);
	spi_write_packet(AT25160B_SPI,data,len);
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	delay_ms(100);
	
	return;
}

Thanks
Deepak
 

Show the code of spi_write_packet() function.

If you use

Code:
memAddr>>16

then address will be always 0 because 16 bit value is right shifted 16 times.

Data will be always 0 because above issue.

Show Circuit. How is WP pin connected ?
 
Last edited:

Hi,

The original code or writing a character is this. I modified the code as above which I posted.

Code:
void spi_eeprom_Write(uint16_t memAddr, uint8_t data[],uint8_t len)
{
	delay_ms(2);
	spi_eeprom_Write_Enable();	
	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	uint8_t packet[3]={EEPROM_DATA_WRITE,memAddr>>8,memAddr&0x00FF};
	spi_write_packet(AT25160B_SPI,packet,3);
	spi_write_packet(AT25160B_SPI,data,len);
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	delay_ms(100);
	
	return;
}

I have not written code for spi_write_packet.

WP is pulled up and given through 3.3V
 

This

Code:
uint8_t packet[3]={EEPROM_DATA_WRITE,memAddr>>8,memAddr&0x00FF};

is correct for writing 2 bytes that i supper byte and lower byte.


With this

Code:
uint16_t packet[3]={EEPROM_DATA_WRITE,memAddr>>16,memAddr&0xFFFF};

eeprom address will be always between 0 and 255.
 
Last edited:

Hi,

I am using same method only.

Code:
uint8_t packet[3]={EEPROM_DATA_WRITE,memAddr>>8,memAddr&0x00FF};
 

Can't answer more without looking at the

Code:
spi_write_packet()

function definition.

For which EEPROM the original code was written ? My previous post is updated.
 

Hi,

These are the inbuilt codes for spi_packet_write and spi_packet_read.

Code:
status_code_t spi_read_packet(volatile avr32_spi_t *spi,
uint8_t *data, size_t len)
{
	unsigned int timeout = SPI_TIMEOUT;
	uint8_t val;
	size_t i=0;
	while(len) {
		timeout = SPI_TIMEOUT;
		while (!spi_is_tx_ready(spi)) {
			if (!timeout--) {
				return ERR_TIMEOUT;
			}
		}
		spi_write_single(spi,CONFIG_SPI_MASTER_DUMMY);
		timeout = SPI_TIMEOUT;
		while (!spi_is_rx_ready(spi)) {
			if (!timeout--) {
				return ERR_TIMEOUT;
			}
		}
		spi_read_single(spi,&val);
		data[i] = val;
		i++;
		len--;
	}
	return STATUS_OK;
}

status_code_t spi_write_packet(volatile avr32_spi_t *spi, const uint8_t *data,
size_t len)
{
	unsigned int timeout = SPI_TIMEOUT;
	size_t i=0;
	uint8_t val;
	while(len) {
		timeout = SPI_TIMEOUT;
		while (!spi_is_tx_ready(spi)) {
			if (!timeout--) {
				return ERR_TIMEOUT;
			}
		}
		val = data[i];
		spi_write_single(spi,val);
		i++;
		len--;
	}
	return STATUS_OK;
}
 

Show example of passing data to this function.

Code:
spi_write_packet()
 

I am sorry. Show example of passing data to

Code:
spi_eeprom_Write()

function.
 

Hi.

Here is the code for EEPROM I have written.

Code:
/*
 * EEPROM.c
 *
 * Created: 2/7/2015 4:39:16 PM
 
 */

#include <avr32/io.h>
#include "EEPROM.h"
#include "spi.h"
#include "gpio.h"
#include "spi_master.h"
#include "lcd_display.h"
#include "delay.h"

uint8_t readdata;

struct spi_device SPI_DEVICE_EXAMPLE = {
	//! Board specific select id
	.id = AT25160B_SPI_NPCS
};

void spi_eeprom_Write_Enable(void)
{
	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	spi_write(AT25160B_SPI,EEPROM_WREN);// send command
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);	
}

void spi_eeprom_Write_Disable(void)
{
	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	spi_write(AT25160B_SPI,EEPROM_WRDI);// send command
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
}

void AT25160B_init(void)
{
	//gpio of spi configuration
	static const gpio_map_t AT25160B_SPI_GPIO_MAP =
	{
		{AT25160B_SPI_SCK_PIN ,  AT25160B_SPI_SCK_FUNCTION },  // SPI Clock.
		{AT25160B_SPI_MISO_PIN , AT25160B_SPI_MISO_FUNCTION},  // MISO.
		{AT25160B_SPI_MOSI_PIN , AT25160B_SPI_MOSI_FUNCTION},  // MOSI.
		{AT25160B_SPI_NPCS0_PIN , AT25160B_SPI_NPCS0_FUNCTION}   // Chip Select NPCS.
	};
	
	gpio_enable_module(AT25160B_SPI_GPIO_MAP,sizeof(AT25160B_SPI_GPIO_MAP) / sizeof(AT25160B_SPI_GPIO_MAP[0]));
	gpio_enable_gpio_pin(AT25160B_SPI_HOLD);
	gpio_set_gpio_pin(AT25160B_SPI_HOLD);
	
	spi_master_init(AT25160B_SPI);
	spi_master_setup_device(AT25160B_SPI,&SPI_DEVICE_EXAMPLE, SPI_MODE_0,AT25160B_SPI_BAUDRATE, 0);
	spi_enable(AT25160B_SPI);
	spi_eeprom_Write_Enable();
}

uint8_t spi_eeprom_Read(uint16_t memAddr)
{
	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	uint8_t packet[3]={EEPROM_DATA_READ,memAddr>>8,memAddr&0x00FF};
	spi_write_packet(AT25160B_SPI,packet,3);
	spi_read_packet(AT25160B_SPI,&readdata,1);// read contents of memory address
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	delay_ms(100);
	return readdata;// return data
}

void spi_eeprom_Write(uint16_t memAddr, uint8_t data[],uint8_t len)
{
	delay_ms(2);
	spi_eeprom_Write_Enable();	
	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	uint8_t packet[3]={EEPROM_DATA_WRITE,memAddr>>8,memAddr&0x00FF};
	spi_write_packet(AT25160B_SPI,packet,3);
	spi_write_packet(AT25160B_SPI,data,len);
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	delay_ms(100);
	
	return;
}
 

You are getting conflicting types because

In this

Code:
void spi_eeprom_Write(uint16_t memAddr, uint16_t data[],uint16_t len)

second ardument is a array and the original function is expecting a pointer to int8.


Edit:


This code

Code:
void spi_eeprom_Write(uint16_t memAddr, uint16_t data[],uint16_t len)
{
	delay_ms(2);
	spi_eeprom_Write_Enable();	
	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	uint16_t packet[3]={EEPROM_DATA_WRITE,memAddr>>16,memAddr&0xFFFF};
	spi_write_packet(AT25160B_SPI,packet,3);
	spi_write_packet(AT25160B_SPI,data,len);
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	delay_ms(100);
	
	return;
}

posted in post #3 is wrong and the below code posted in post #5 is correct.

Code:
void spi_eeprom_Write(uint16_t memAddr, uint8_t data[],uint8_t len)
{
	delay_ms(2);
	spi_eeprom_Write_Enable();	
	spi_selectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	uint8_t packet[3]={EEPROM_DATA_WRITE,memAddr>>8,memAddr&0x00FF};
	spi_write_packet(AT25160B_SPI,packet,3);
	spi_write_packet(AT25160B_SPI,data,len);
	spi_unselectChip(AT25160B_SPI,AT25160B_SPI_NPCS);
	delay_ms(100);
	
	return;
}

because the below function

Code:
status_code_t spi_write_packet(volatile avr32_spi_t *spi, const uint8_t *data,
size_t len)
{
	unsigned int timeout = SPI_TIMEOUT;
	size_t i=0;
	uint8_t val;
	while(len) {
		timeout = SPI_TIMEOUT;
		while (!spi_is_tx_ready(spi)) {
			if (!timeout--) {
				return ERR_TIMEOUT;
			}
		}
		val = data[i];
		spi_write_single(spi,val);
		i++;
		len--;
	}
	return STATUS_OK;
}

expects pointer to uint8_t as the second argument and you have changed it as below

Code:
void spi_eeprom_Write(uint16_t memAddr, uint16_t data[],uint16_t len)

So, you are sending uint16_t to uint8_t*
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top