Author Topic: memory  (Read 4814 times)

0 Members and 1 Guest are viewing this topic.

Offline charloTopic starter

  • Contributor
  • Posts: 10
  • Country: fr
memory
« on: May 31, 2016, 02:39:54 pm »
Hi,

Does anyone have any idea to how it would be possible for me to create two functions, one for reading and one for writing data in the memory evaluation board. So I want to write in the RAM of my card and thus pass through the DMA is in my case as "DMAC".

I did not really happen if someone an idea ....

Thank you in advance.
« Last Edit: June 06, 2016, 09:16:45 pm by charlo »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #1 on: May 31, 2016, 05:18:31 pm »
I don't understand what do you need exactly. If you are working with RAM, then why erase? If you are working with Flash, then there is no way to use DMA with that.
Alex
 

Offline charloTopic starter

  • Contributor
  • Posts: 10
  • Country: fr
memory
« Reply #2 on: May 31, 2016, 06:17:26 pm »
Sorry!

I already try using the flash memory but in the datasheet  I do not understand how it works. For example, to write to flash memory I do not see where is the register where I put the address, data ...
I already search the internet, but nothing clear.

To write to the Flash memory, I need several parameters such as data, the source address and destination address but I do not understand the datasheet NVM functionality.

Then to read into memory, I must communicate an address.

What I seek to do in short is to create two functions to read and write to flash memory.

I was talking about the memory RAM seen before I was gone the above saw that I could not do it with flash
« Last Edit: June 06, 2016, 09:17:32 pm by charlo »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #3 on: May 31, 2016, 06:28:45 pm »
Here is code sample for writing into flash. Remember you can only erase in 256-byte and write in 64-byte chunks and flash has limited number of erase/write cycles.

Code: [Select]
void flash_init(void)
{
  PM->AHBMASK.reg |= PM_AHBMASK_NVMCTRL;
  PM->APBBMASK.reg |= PM_APBBMASK_NVMCTRL;
  NVMCTRL->CTRLB.bit.MANW = 0;
}

void flash_write(uint32_t offset, uint32_t *buf)
{
  NVMCTRL->ADDR.reg = offset >> 1;

  NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_ER;
  while (0 == NVMCTRL->INTFLAG.bit.READY);

  for (uint32_t i = 0; i < NVMCTRL_ROW_SIZE / sizeof(uint32_t); i++)
    *(uint32_t *)(offset + i * sizeof(uint32_t)) = buf[i];
}

There is no way to use DMA for this.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #4 on: May 31, 2016, 06:46:54 pm »
Buf is the data you want to write. You need to read out the ADC result into a temporary buffer in RAM and then call this function to write this buffer into the Flash.

Offset is the offset in flash where you want this buffer to go.

Flash memory is mapped into the address space, just read it as you normally would read any location:
Code: [Select]
uint8_t byte = *(uint8_t *)address;
But again, why do you want to write ADC results in to Flash? This is not something you normally want to do. And with limited number of write cycles, you will just trash your flash.
Alex
 

Offline charloTopic starter

  • Contributor
  • Posts: 10
  • Country: fr
Re: SAMD11 Read and Write in the memory
« Reply #5 on: May 31, 2016, 06:57:22 pm »
But suddenly, if the number of flash memory cycles is limited it is better to me to read and write my data in RAM? But I fear that it saturates being data that I use about every 1 second ADC.

So if I must imperatively use the RAM, I must use the DMA since I can not do anything without DMA?

But there are no clear examples for programming the RAM from the examples that are on the Atmel Studio software. What I seek to do is just a stokage space of my data and be able to write and read in order to realize the processing of my data later
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #6 on: May 31, 2016, 07:53:19 pm »
better to me to read and write my data in RAM?
What do you mean "better"? RAM is normally used for storing volatile data. Most application never write anything into the flash.

But I fear that it saturates being data that I use about every 1 second ADC.
I don't understand this.

since I can not do anything without DMA?
Why do you think that?

But there are no clear examples for programming the RAM
You don't "program" RAM. You create variable in C (presumably that's what you are using) and assign values to those variables. Compiler places variables in RAM.

You probably need to start from learning general C and MCU principles.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #7 on: May 31, 2016, 08:11:12 pm »
How do you print it on the terminal?
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #8 on: May 31, 2016, 08:20:38 pm »
Ok, but what is the code that does actual printing? In order to help you, I need to know what you are doing at the moment.

Also, I strongly suggest to not use ASF, it will give you the wrong impression of what MCU programming looks like.

I have a number of very simple starter projects (including D11) that talk directly to the hardware - https://github.com/ataradov/mcu-starter-projects/tree/master/samd11
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #9 on: May 31, 2016, 08:29:42 pm »
Show me the line of your software that prints out the result. Where do you access that RESULT register right now?
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #10 on: May 31, 2016, 08:34:53 pm »
Well, resuNum is your variable in memory. What else do you need?
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #11 on: May 31, 2016, 08:39:55 pm »
I don't do private support. If you have questions, ask them here, others may benefit from my answers.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #12 on: May 31, 2016, 08:48:13 pm »
Well, you have this already, don't you? You really need to be more specific.
Alex
 
The following users thanked this post: charlo

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #13 on: May 31, 2016, 09:04:48 pm »
That looks reasonable, but I have not checked every single thing. You only need to set correction once after the initialization.

Please use [ code ] and [ /code ] tags to mark your code, so it is easier to read.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #14 on: June 01, 2016, 05:23:00 pm »
But why use the function to write to the flash memory I do not get the value 0x0 I want to address
You are really misunderstanding this thing. What 0 value you expect  to see?

You instructed it to copy 256 bytes from address 0x100 to address 0, which it did. But by doing this you actually corrupted your program, since there is interrupt table at the offset 0.

You need to get more experience with MCU programming before you get into advanced stuff like this. There is no way I can explain this in a simpler terms.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #15 on: June 01, 2016, 05:24:47 pm »
Also note that input buffer must be aligned at 4-byte boundary, that's why it is defined as "uint32 *buf". Anything else (like 0x41 in your first screenshot) will cause an undefined behavior.

LE: 0x41 is not "data". You can't write single bytes into flash. You must write the entire 256 bytes at once.

And again, don't write to address 0, that's where your program is located.

LLE: And I'm sure compiler complained about that use of 41. DO NOT IGNORE COMPILER MESSAGES.
« Last Edit: June 01, 2016, 05:27:21 pm by ataradov »
Alex
 

Offline charloTopic starter

  • Contributor
  • Posts: 10
  • Country: fr
Re: SAMD11 Read and Write in the memory
« Reply #16 on: June 01, 2016, 05:28:35 pm »
But the function write to flash memory allows me to write the value that I want to address I want? If I want to store a value in flash memory how can I do?
 

Offline charloTopic starter

  • Contributor
  • Posts: 10
  • Country: fr
Re: SAMD11 Read and Write in the memory
« Reply #17 on: June 01, 2016, 05:33:18 pm »
I just want to store a value in Flash memory such as 2142, which is an integer that I converted to 32-bit number
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #18 on: June 01, 2016, 05:37:02 pm »
I just want to store a value in Flash memory such as 2142, which is an integer that I converted to 32-bit number

Correct use of this function is something lie this:
Code: [Select]
uint32_t buffer[64]; // 256 bytes aligned at 4 byte boundary

buffer[0] = 2142; // Your value. You can save some other stuff as well. You have 256 bytes, that will be wasted otherwise.

flash_write(8192, buf);  // Write that buffer at offset 8192

And to read this value back:
Code: [Select]
uint32_t value = *(uint32_t *)8192;
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #19 on: June 01, 2016, 05:52:13 pm »
Go to the location 8192 in the Memory View window and you will see it.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #20 on: June 01, 2016, 05:55:07 pm »
8192, not 0x8192. There is a difference.
Alex
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #21 on: June 01, 2016, 05:59:52 pm »
Well, there is your value. 0x85e == 2142.

You can type decimal addresses directly, there is no need to convert manually.
Alex
 

Offline charloTopic starter

  • Contributor
  • Posts: 10
  • Country: fr
Re: SAMD11 Read and Write in the memory
« Reply #22 on: June 01, 2016, 06:01:59 pm »
aaaaaaaaaahhh thank you!
 

Offline charloTopic starter

  • Contributor
  • Posts: 10
  • Country: fr
Re: SAMD11 Read and Write in the memory
« Reply #23 on: June 01, 2016, 06:03:40 pm »

But if data is already registered there it will overwrite the data?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11228
  • Country: us
    • Personal site
Re: SAMD11 Read and Write in the memory
« Reply #24 on: June 01, 2016, 06:05:16 pm »
Registered where?

The whole 256 bytes will be overwritten, that's how Flash technology works.

If you need to preserve the old data, then read it into the buffer first, then make necessary modifications and write the whole buffer back.
Alex
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf