memory - Page 1
Forum section: electronics / general

author: charlo
author_rank: Contributor
timestamp: May 31, 2016, 02:39:54 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: May 31, 2016, 05:18:31 pm
content: 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.

author: charlo
author_rank: Contributor
timestamp: May 31, 2016, 06:17:26 pm
content: 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

author: ataradov
author_rank: Super Contributor
timestamp: May 31, 2016, 06:28:45 pm
content: 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: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;}There is no way to use DMA for this.

author: ataradov
author_rank: Super Contributor
timestamp: May 31, 2016, 06:46:54 pm
content: 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: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.

author: charlo
author_rank: Contributor
timestamp: May 31, 2016, 06:57:22 pm
content: 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

author: ataradov
author_rank: Super Contributor
timestamp: May 31, 2016, 07:53:19 pm
content: Quote from: charlo on May 31, 2016, 06:57:22 pmWhat do you mean "better"? RAM is normally used for storing volatile data. Most application never write anything into the flash.Quote from: charlo on May 31, 2016, 06:57:22 pmI don't understand this.Quote from: charlo on May 31, 2016, 06:57:22 pmWhy do you think that?Quote from: charlo on May 31, 2016, 06:57:22 pmYou 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.

author: ataradov
author_rank: Super Contributor
timestamp: May 31, 2016, 08:11:12 pm
content: How do you print it on the terminal?

author: ataradov
author_rank: Super Contributor
timestamp: May 31, 2016, 08:20:38 pm
content: 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

author: ataradov
author_rank: Super Contributor
timestamp: May 31, 2016, 08:29:42 pm
content: Show me the line of your software that prints out the result. Where do you access that RESULT register right now?

author: ataradov
author_rank: Super Contributor
timestamp: May 31, 2016, 08:34:53 pm
content: Well, resuNum is your variable in memory. What else do you need?

author: ataradov
author_rank: Super Contributor
timestamp: May 31, 2016, 08:39:55 pm
content: I don't do private support. If you have questions, ask them here, others may benefit from my answers.

author: ataradov
author_rank: Super Contributor
timestamp: May 31, 2016, 08:48:13 pm
content: Well, you have this already, don't you? You really need to be more specific.

author: ataradov
author_rank: Super Contributor
timestamp: May 31, 2016, 09:04:48 pm
content: 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 01, 2016, 05:23:00 pm
content: Quote from: charlo on June 01, 2016, 05:08:11 pmYou 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.

author: ataradov
author_rank: Super Contributor
timestamp: June 01, 2016, 05:24:47 pm
content: 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.

author: charlo
author_rank: Contributor
timestamp: June 01, 2016, 05:28:35 pm
content: 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?

author: charlo
author_rank: Contributor
timestamp: June 01, 2016, 05:33:18 pm
content: I just want to store a value in Flash memory such as 2142, which is an integer that I converted to 32-bit number

author: ataradov
author_rank: Super Contributor
timestamp: June 01, 2016, 05:37:02 pm
content: Quote from: charlo on June 01, 2016, 05:33:18 pmCorrect use of this function is something lie this:Code:uint32_t buffer; // 256 bytes aligned at 4 byte boundarybuffer = 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 8192And to read this value back:Code:uint32_t value = *(uint32_t *)8192;

author: ataradov
author_rank: Super Contributor
timestamp: June 01, 2016, 05:52:13 pm
content: Go to the location 8192 in the Memory View window and you will see it.

author: ataradov
author_rank: Super Contributor
timestamp: June 01, 2016, 05:55:07 pm
content: 8192, not 0x8192. There is a difference.

author: ataradov
author_rank: Super Contributor
timestamp: June 01, 2016, 05:59:52 pm
content: Well, there is your value. 0x85e == 2142.You can type decimal addresses directly, there is no need to convert manually.

author: charlo
author_rank: Contributor
timestamp: June 01, 2016, 06:01:59 pm
content: aaaaaaaaaahhh thank you!

author: charlo
author_rank: Contributor
timestamp: June 01, 2016, 06:03:40 pm
content: But if data is already registered there it will overwrite the data?

author: ataradov
author_rank: Super Contributor
timestamp: June 01, 2016, 06:05:16 pm
content: 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.

author: charlo
author_rank: Contributor
timestamp: June 01, 2016, 06:14:23 pm
content: So if I want to fo a function read_flash, I simply do:Code:uint32_t flash_read (uint32_t *adr){uint32_t data;data=*(uint32_t *)adr;returndata;}[\code]

author: ataradov
author_rank: Super Contributor
timestamp: June 01, 2016, 06:16:38 pm
content: Correct code:Code:uint32_t flash_read (uint32_t adr){return *(uint32_t *)adr;}But why would you need a function for this?Please fix your code tags.

author: charlo
author_rank: Contributor
timestamp: June 01, 2016, 06:18:37 pm
content: To get a more readable and clear code for me

author: ataradov
author_rank: Super Contributor
timestamp: June 01, 2016, 06:20:52 pm
content: Quote from: charlo on June 01, 2016, 06:18:37 pmIn that case you may want to have a family of functions to read 8- and 16-bit values as well.PS: There is not going to be any more replies from me until you fix the code tags in message #38.

author: ataradov
author_rank: Super Contributor
timestamp: June 01, 2016, 06:33:01 pm
content: I don't support or endorse ASF, I have no idea how to do it there.There is a simple low-level driver example for SERCOM UART in the LwMesh stack -http://www.atmel.com/tools/lightweight_mesh.aspx. The example there is for D21, but SERCOM peripheral is the same on D11 and D21.PS: Your closing code tag is still wrong.

author: charlo
author_rank: Contributor
timestamp: June 01, 2016, 06:47:12 pm
content: Ok well I'll look at that. Anyway I thank you for the time you devoted to me and the patience you have shown.thank you very much