0

how to use sd_flash_write? SDK13

metcenger gravatar image

asked 2017-06-19 14:54:25 +0200

I want to write at the end of memory ROM data in address 0x7FFFF

uint8_t tst_arr[1] = {6};
sd_flash_write((uint32_t*)0x7FFFF, (uint32_t*) &tst_arr, 1);

Is it correct?

But if I made it, in a memory watch I can't see any changes from 0xFF to my data (6).

edit retag flag offensive close delete report spam

1 answer

Sort by » oldest newest most voted
0
endnode gravatar image

answered 2017-06-19 15:01:28 +0200

No you cannot do it like this, flash writes should be 32-bit aligned. So if you want to write 8-bits to address 0x7FFFF then you should read 24-bits from address 0x7FFFC, concatenate it with your 8-bits and write resulting 32-bits back to address 0x7FFFC. Alternatively you can skip reading and put 0xFFFFFF at the beginning if you are sure that flash is erased before your write (or if you simply don't care about these preceding 3 bytes).

edit flag offensive delete publish link more

Comments

Thanks. Tell me pls easy way to write into flash 4 bytes? My ROM memory ends 0x7FFFF I suuppose, I should write into 7FFFB?

uint32_t tst_arr[1] = {6};

sd_flash_write((uint32_t*)0x7FFFB, tst_arr, 1);

Is it correct?

And how to erase this data flash?

Mikhail ( 2017-06-19 15:10:05 +0200 )editconvert to answer

Address for writing must be dividable modulo 4 without any reminder. E.g. 0x7FFFC / 4 = 131,071 + 0 while your suggested 0x7FFFB / 4 = 131,070 + 3. So 0x7FFFC is the right address (0x7FFFF is only the last offset in the flash memory, not the end).

When it comes to erasing the memory it works with flash technology only page by page so you need to erase whole last 4kB (on nRF51 it was 1kB). Again offset can be easily computed by taking any offset in that page and doing modulo 4096 (so address of last page is 0x7F000).

endnode ( 2017-06-19 15:19:56 +0200 )editconvert to answer

Thanks, but 0x7FFFC also doesn't work ( Any Idea?

Mikhail ( 2017-06-19 15:23:16 +0200 )editconvert to answer

Debug:) What are the response codes thrown by SD function sd_flash_write? Do you receive NRF_EVT_FLASH_OPERATION_SUCCESS in sys_evt_dispatch event handler?

endnode ( 2017-06-19 15:35:48 +0200 )editconvert to answer

There is no any events in sys_evt_dispatch when I call my function... Looks again to code:

uint32_t tst_arr[1] = {6};

sd_flash_write((uint32_t*)0x7FFFC, tst_arr, 1);

nrf_delay_ms(20);

What I did not correct? My be I should enable any global settings to write in flash? Or any else?

Mikhail ( 2017-06-19 15:44:13 +0200 )editconvert to answer

Oh no, never place nrf_delay_xxx anywhere in the code beside when you really want to have busy loop, it's occupying the MCU and so nothing else can run. First of all sd_flash_write is returning some uint32_t response code. You are not reading it so you should change that and check its NRF_SUCCESS. Print it to debug printout or something similar. To use all this you need to have several things in place like enabling Soft Device etc. Hard to say when not seeing the rest of the code. But in general the way forward is to do more debug on your side not pasting more code here to the forum...

endnode ( 2017-06-19 15:54:26 +0200 )editconvert to answer

When I check for error code in debug mode, I receive error:

APP_ERROR_CHECK (sd_flash_write((uint32_t*)0x7FFFC, tst_arr, 4));

error_code 16 from this line.

What does it mean? mean 16.

I removed delay.

16 means NRF_ERROR_INVALID_ADDR

ok. Where can I write data in to flash? In wich address?

Mikhail ( 2017-06-19 15:59:40 +0200 )editconvert to answer

Last parameter size should be number of 32-bit words to be written to flash hence 1 not 4. Also all this is in the documentation to SD API.

endnode ( 2017-06-19 16:12:16 +0200 )editconvert to answer

changed 4 to 1. address is 0x7FFFC also returned error, but now 17- busy ???

Mikhail ( 2017-06-19 16:18:28 +0200 )editconvert to answer

It seems that you are calling this function too soon after previous flash API call (of any sd_flash_xxx function). I believe API documentation in my previous link is very clear.

endnode ( 2017-06-19 16:25:24 +0200 )editconvert to answer

while ((sd_flash_write((uint32_t*)0x7FFFC, tst_arr, 1)) != NRF_SUCCESS);

doesn't return error. After this function I step next. Therefore sd_flash_write return success. But In the memory window without any differents. 0x7FFFC = 0xFF and nex 3 bytes = 0xFF. I readed Your link but no progress. I don't have any idea why doesn,t work.

Mikhail ( 2017-06-19 16:42:40 +0200 )editconvert to answer

You need to wait until you get NRF_EVT_FLASH_OPERATION_SUCCESS event which means that flash operation was processed. Only then you can continue (e.g. do another flash operation or go and verify the content). Are you sure that you read all 4 bytes from address 0x7FFFC when verifying? Are you doing it in nrfjprog or SEGGER J-Flash or something else?

endnode ( 2017-06-19 16:48:01 +0200 )editconvert to answer

I read bytes from Keil debug window. check it

Mikhail ( 2017-06-19 16:51:43 +0200 )editconvert to answer

there is no event NRF_EVT_FLASH_OPERATION_SUCCESS

Mikhail ( 2017-06-19 16:53:43 +0200 )editconvert to answer

No event means no flash operation finished means nothing to read, that makes sense. Maybe you cold go back to SDK and run some verified example from Nordic first...

endnode ( 2017-06-19 16:54:39 +0200 )editconvert to answer

let's try to check SDK example

Mikhail ( 2017-06-19 16:56:10 +0200 )editconvert to answer

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

Question Tools

2 followers

Stats

Asked: 2017-06-19 14:54:25 +0200

Seen: 60 times

Last updated: Jun 19