Direc access to sram address

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How to read / write to specific address on sram ?

I used this code but i think there is better choice to do this:


#define BASE_PARAM 0x1f80

volatile uint8_t* pPARAM = (uint8_t*) BASE_PARAM; //puntatore PARAMETRI alla ram

void ram_par_write_byte(uint8_t addr,uint8_t dato){
			pPARAM=(uint8_t *)(BASE_PARAM+addr); 				//imposta puntatore
			*pPARAM = dato; 						
			}

void ram_par_write_int(uint8_t addr,uint16_t dato){
			uint16_t temp;
			uint8_t div,resto;
			pPARAM=(uint8_t *)(BASE_PARAM+addr); 			//imposta puntatore
			temp = dato;			
			div = temp / 256;				
			*(pPARAM+1) = div; 				
			resto = (temp - (div * 256));
			*(pPARAM+0) = resto; 				
				}

uint8_t ram_par_read_byte(uint8_t addr){
			uint8_t dato;
			pPARAM=(uint8_t *)(BASE_PARAM+addr); 				 //imposta puntatore
			return dato=*pPARAM; 					 //read ram byte
			}

uint16_t ram_par_read_int(uint8_t addr){
			uint16_t dato;
			pPARAM=(uint8_t *)(BASE_PARAM+addr); 				 
			return dato=*pPARAM + (*(pPARAM+1)*256); //read ram int
			}



  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That seems fine but I don't wholly understand why your int write function is so complicated? Surely you just cast the address to be an int pointer then write through it? No point in using a pointer to 8 bits then having to split the int.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

unfortunately I do not know how you do, please can you show me?

I found an error with.....

uint32_t ram_par_read_dword(uint8_t addr)

void ram_par_write_dword(uint8_t addr,uint32_t dato)

and help would not hurt

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i made some variation but the error remain
i don't know if in write o read function
highest byte[3] e higher byte[2] are always 0xFF , the lower byte[0] and high[1] are correct!

void ram_par_write_dword(uint8_t addr,uint32_t dato){
			pPARAM=(uint8_t *)(BASE_PARAM+addr); 			//imposta puntatore
			*(pPARAM+3) = (dato >> 24) & 0xFF;
			*(pPARAM+2) = (dato >> 16) & 0xFF;
			*(pPARAM+1) = (dato >> 8) & 0xFF;
			*(pPARAM+0) = (dato >> 0) & 0xFF;
			}


uint32_t ram_par_read_dword(uint8_t addr){
			uint32_t dato; dato=0;
			pPARAM=(uint8_t *)(BASE_PARAM+addr); 			
			return dato = (*(pPARAM+3)<<24) | (*(pPARAM+2)<<16) | (*(pPARAM+1)<<8) | *pPARAM  ; //read ram dword
			}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
void ram_par_write_dword (uint8_t addr, uint32_t dato) {

    *(uint32_t*)(BASE_PARAM+addr) = dato;
}


uint32_t ram_par_read_dword (uint8_t addr) {

    return *(uint32_t*)(BASE_PARAM+addr);
} 

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BTW what's the point of absolute addressing anyway? Presumably there are two things "seeing" the same locations - why can't they both just access it symbolically?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If that is the case, is not "union" a common solution?

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define lvalat(addr, tipe)   (*(tipe *)(addr))

Why?

"Demons after money.
Whatever happened to the still beating heart of a virgin?
No one has any standards anymore." -- Giles

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst everything works perfectly, thank you all, I have to study more

clawson, I have a copy in ram of a set of EEROM parameters that need to be close

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

a long time since I was in avr freaks, there must come back more often ..
there is a nice atmosphere here :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why don't you just group all the parameters in a struct and block read/write it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Indeed. And why does the SRAM copy need to be at a hard address? And even if that is desired, why not use the linker to place the variables and then use symbolic access?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.