3434*/
3535/**************************************************************************/
3636
37+ #include <assert.h>
3738#include "Arduino.h"
3839#include "nrf52_flash.h"
3940
@@ -69,6 +70,24 @@ static void print_write_after(uint32_t address, uint32_t count)
6970
7071#endif
7172
73+ static int nrf52_flash_sector_info (void * dev , int idx , uint32_t * address , uint32_t * sz );
74+
75+ static const struct hal_flash_funcs nrf52k_flash_funcs = {
76+ .hff_read = nrf52k_flash_read ,
77+ .hff_write = nrf52_flash_write ,
78+ .hff_erase_sector = nrf52_flash_erase_sector ,
79+ .hff_sector_info = nrf52_flash_sector_info ,
80+ .hff_init = nrf52_flash_init
81+ };
82+
83+ const struct hal_flash nrf52k_flash_dev = {
84+ .hf_itf = & nrf52k_flash_funcs ,
85+ .hf_base_addr = 0x00000000 ,
86+ .hf_size = 512 * 1024 , /* XXX read from factory info? */
87+ .hf_sector_cnt = 128 , /* XXX read from factory info? */
88+ .hf_align = 1
89+ };
90+
7291static SemaphoreHandle_t _evt_sem = NULL ;
7392static volatile uint32_t _op_result ;
7493
@@ -78,14 +97,16 @@ void hal_flash_event_cb(uint32_t event)
7897 xSemaphoreGive (_evt_sem );
7998}
8099
81- int nrf52_flash_init (void )
100+ int nrf52_flash_init (const struct hal_flash * dev )
82101{
102+ (void ) dev ;
83103 _evt_sem = xSemaphoreCreateCounting (10 , 0 );
84104 return (_evt_sem != NULL ) ? 0 : 1 ;
85105}
86106
87- int nrf52_flash_erase_sector (uint32_t sector_address )
107+ int nrf52_flash_erase_sector (const struct hal_flash * dev , uint32_t sector_address )
88108{
109+ (void ) dev ;
89110 uint32_t err ;
90111
91112#if CFG_DEBUG_NFFS
@@ -124,8 +145,10 @@ static int write_and_wait(uint32_t addr, uint32_t const * const data, uint32_t s
124145 return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : (-1 );
125146}
126147
127- int nrf52_flash_write (uint32_t address , const void * src , uint32_t num_bytes )
148+ int nrf52_flash_write (const struct hal_flash * dev , uint32_t address , const void * src , uint32_t num_bytes )
128149{
150+ (void ) dev ;
151+
129152#if CFG_DEBUG_NFFS
130153 const uint32_t _num = num_bytes ;
131154 const uint32_t _addr = address ;
@@ -235,3 +258,32 @@ int nrf52_flash_write(uint32_t address, const void *src, uint32_t num_bytes)
235258 return 0 ;
236259}
237260
261+ int nrf52k_flash_read (const struct hal_flash * dev , uint32_t address , void * dst , uint32_t num_bytes )
262+ {
263+ (void ) dev ;
264+ memcpy (dst , (void * )address , num_bytes );
265+ return 0 ;
266+ }
267+
268+ static int nrf52_flash_sector_info (void * dev , int idx , uint32_t * address , uint32_t * sz )
269+ {
270+ (void ) dev ;
271+ assert (idx < nrf52k_flash_dev .hf_sector_cnt );
272+ * address = idx * NRF52K_FLASH_SECTOR_SZ ;
273+ * sz = NRF52K_FLASH_SECTOR_SZ ;
274+ return 0 ;
275+ }
276+
277+
278+ const struct hal_flash *
279+ hal_bsp_flash_dev (uint8_t id )
280+ {
281+ /*
282+ * Internal flash mapped to id 0.
283+ */
284+ if (id != 0 ) {
285+ return NULL ;
286+ }
287+ return & nrf52k_flash_dev ;
288+ }
289+
0 commit comments