Skip to content

Commit 107b196

Browse files
committed
update nffs to mynewt 1.0.0
1 parent 0191a8f commit 107b196

File tree

14 files changed

+618
-105
lines changed

14 files changed

+618
-105
lines changed

cores/nRF5/nrf52_flash.c

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
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+
7291
static SemaphoreHandle_t _evt_sem = NULL;
7392
static 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+

cores/nRF5/nrf52_flash.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,21 @@
3737
#define NRF52_FLASH_H_
3838

3939
#include <stdint.h>
40+
#include <hal/hal_flash_int.h>
4041

4142
#ifdef __cplusplus
4243
extern "C" {
4344
#endif
4445

4546
#define NRF52K_FLASH_SECTOR_SZ 4096
4647

47-
int nrf52_flash_init(void);
48-
int nrf52_flash_erase_sector(uint32_t sector_address);
49-
int nrf52_flash_write(uint32_t address, const void *src, uint32_t num_bytes);
48+
// const struct hal_flash* dev is required for nffs flash interface but not used
49+
// by nrf52_flash, just simply pass NULL when invoked
50+
51+
int nrf52_flash_init(const struct hal_flash *dev);
52+
int nrf52_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address);
53+
int nrf52_flash_write(const struct hal_flash *dev, uint32_t address, const void *src, uint32_t num_bytes);
54+
int nrf52k_flash_read(const struct hal_flash *dev, uint32_t address, void *dst, uint32_t num_bytes);
5055

5156
#ifdef __cplusplus
5257
}

libraries/nffs/library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=Newtron Flash Filesystem
2-
version=0.1.0
2+
version=1.0.0
33
author=Apache Software Foundation
44
maintainer=Adafruit <[email protected]>
55
sentence=Newtron Flash Filesystem
66
paragraph=Newtron Flash Filesystem
77
category=Communication
8-
url=https://github.com/adafruit/Adafruit_nRF52_Arduino
8+
url=http://mynewt.apache.org/latest/os/modules/fs/nffs/nffs
99
architectures=*
1010
includes=NewtNffs.h

libraries/nffs/src/Nffs.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737

3838
#include "syscfg/syscfg.h"
3939
#include "hal/hal_flash.h"
40-
41-
#define NFFS_AREA_MAX 8
40+
#include "flash_map/flash_map.h"
4241

4342
ApacheNffs Nffs;
4443

@@ -54,20 +53,21 @@ bool ApacheNffs::begin(void)
5453
if (_initialized) return true;
5554
_initialized = true;
5655

57-
// Init flash
58-
nrf52_flash_init();
56+
// Init flash map (required before accessing flash API)
57+
flash_map_init();
5958

6059
/* Initialize nffs's internal state. */
6160
errnum = nffs_init();
6261
VERIFY_STATUS( errnum, false );
6362

6463
/* Convert the set of flash blocks we intend to use for nffs into an array
65-
* of nffs area descriptors.
64+
* of nffs area descriptors. Number of descriptor must be larger than
65+
* number of sectors for nffs a.k.a 7
6666
*/
67-
struct nffs_area_desc descs[NFFS_AREA_MAX + 1];
68-
int cnt = NFFS_AREA_MAX;
67+
struct nffs_area_desc descs[NFFS_SECTOR_NUM + 1];
68+
int cnt = NFFS_SECTOR_NUM;
6969

70-
errnum = nffs_misc_desc_from_flash_area( MYNEWT_VAL(NFFS_FLASH_AREA), &cnt, descs);
70+
errnum = nffs_misc_desc_from_flash_area( FLASH_AREA_NFFS, &cnt, descs);
7171
VERIFY_STATUS( errnum, false );
7272

7373
/* Attempt to restore an existing nffs file system from flash. */
@@ -78,18 +78,19 @@ bool ApacheNffs::begin(void)
7878
{
7979
PRINT_MESS("No FS detected, format");
8080
errnum = nffs_format(descs);
81-
VERIFY_STATUS( errnum, false );
8281
}
8382

83+
VERIFY_STATUS( errnum, false );
84+
8485
return true;
8586
}
8687

8788
bool ApacheNffs::format(void)
8889
{
89-
struct nffs_area_desc descs[NFFS_AREA_MAX + 1];
90-
int cnt = NFFS_AREA_MAX;
90+
struct nffs_area_desc descs[NFFS_SECTOR_NUM + 1];
91+
int cnt = NFFS_SECTOR_NUM;
9192

92-
errnum = nffs_misc_desc_from_flash_area( MYNEWT_VAL(NFFS_FLASH_AREA), &cnt, descs);
93+
errnum = nffs_misc_desc_from_flash_area( FLASH_AREA_NFFS, &cnt, descs);
9394
VERIFY_STATUS( errnum, false );
9495

9596
errnum = nffs_format(descs);

libraries/nffs/src/fs/nffs/src/nffs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//#include "sysflash/sysflash.h"
2727
//#include "bsp/bsp.h"
2828
#include "hal/hal_flash.h"
29-
//#include "flash_map/flash_map.h"
29+
#include "flash_map/flash_map.h"
3030
#include "os/os_mempool.h"
3131
#include "os/os_mutex.h"
3232
#include "os/os_malloc.h"

libraries/nffs/src/fs/nffs/src/nffs_misc.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,6 @@
2525
#include "nffs/nffs.h"
2626
#include "nffs_priv.h"
2727

28-
static const struct flash_area sysflash_map_dfl =
29-
{
30-
.fa_id = 0,
31-
.fa_device_id = 0,
32-
.fa_off = 0x6D000,
33-
.fa_size = 7*4096, /* 28 kB */
34-
};
35-
36-
static int
37-
flash_area_open(uint8_t id, const struct flash_area **fap)
38-
{
39-
(void) id;
40-
*fap = &sysflash_map_dfl;
41-
return 0;
42-
}
43-
4428
/**
4529
* Determines if the file system contains a valid root directory. For the root
4630
* directory to be valid, it must be present and have the following traits:
@@ -489,7 +473,7 @@ nffs_misc_desc_from_flash_area(int id, int *cnt, struct nffs_area_desc *nad)
489473

490474
hf = hal_bsp_flash_dev(fa->fa_device_id);
491475
for (i = 0; i < hf->hf_sector_cnt; i++) {
492-
hf->hf_itf->hff_sector_info(i, &start, &size);
476+
hf->hf_itf->hff_sector_info(hf, i, &start, &size);
493477
if (start >= fa->fa_off && start < fa->fa_off + fa->fa_size) {
494478
if (first_idx == -1) {
495479
first_idx = i;
@@ -507,7 +491,7 @@ nffs_misc_desc_from_flash_area(int id, int *cnt, struct nffs_area_desc *nad)
507491

508492
move_on = 1;
509493
for (i = first_idx, j = 0; i < last_idx + 1; i++) {
510-
hf->hf_itf->hff_sector_info(i, &start, &size);
494+
hf->hf_itf->hff_sector_info(hf, i, &start, &size);
511495
if (move_on) {
512496
nad[j].nad_flash_id = fa->fa_device_id;
513497
nad[j].nad_offset = start;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#ifndef __HAL_BSP_H_
21+
#define __HAL_BSP_H_
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
#include <inttypes.h>
28+
29+
#if 0
30+
/*
31+
* Initializes BSP; registers flash_map with the system.
32+
*/
33+
void hal_bsp_init(void);
34+
#endif
35+
36+
/*
37+
* Return pointer to flash device structure, given BSP specific
38+
* flash id.
39+
*/
40+
struct hal_flash;
41+
const struct hal_flash *hal_bsp_flash_dev(uint8_t flash_id);
42+
43+
#if 0
44+
/*
45+
* Grows heap by given amount. XXX giving space back not implemented.
46+
*/
47+
void *_sbrk(int incr);
48+
49+
/*
50+
* Report which memory areas should be included inside a coredump.
51+
*/
52+
struct hal_bsp_mem_dump {
53+
void *hbmd_start;
54+
uint32_t hbmd_size;
55+
};
56+
57+
const struct hal_bsp_mem_dump *hal_bsp_core_dump(int *area_cnt);
58+
59+
/*
60+
* Get unique HW identifier/serial number for platform.
61+
* Returns the number of bytes filled in.
62+
*/
63+
#define HAL_BSP_MAX_ID_LEN 32
64+
int hal_bsp_hw_id(uint8_t *id, int max_len);
65+
66+
#define HAL_BSP_POWER_ON (1)
67+
#define HAL_BSP_POWER_WFI (2)
68+
#define HAL_BSP_POWER_SLEEP (3)
69+
#define HAL_BSP_POWER_DEEP_SLEEP (4)
70+
#define HAL_BSP_POWER_OFF (5)
71+
#define HAL_BSP_POWER_PERUSER (128)
72+
73+
int hal_bsp_power_state(int state);
74+
75+
/* Returns priority of given interrupt number */
76+
uint32_t hal_bsp_get_nvic_priority(int irq_num, uint32_t pri);
77+
#endif
78+
79+
#ifdef __cplusplus
80+
}
81+
#endif
82+
83+
#endif

libraries/nffs/src/hw/hal/include/hal/hal_flash.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,15 @@ extern "C" {
2626

2727
#include <inttypes.h>
2828

29+
int hal_flash_ioctl(uint8_t flash_id, uint32_t cmd, void *args);
2930
int hal_flash_read(uint8_t flash_id, uint32_t address, void *dst,
3031
uint32_t num_bytes);
3132
int hal_flash_write(uint8_t flash_id, uint32_t address, const void *src,
3233
uint32_t num_bytes);
3334
int hal_flash_erase_sector(uint8_t flash_id, uint32_t sector_address);
3435
int hal_flash_erase(uint8_t flash_id, uint32_t address, uint32_t num_bytes);
3536
uint8_t hal_flash_align(uint8_t flash_id);
36-
int nrf52_flash_init(void);
37-
38-
struct hal_flash;
39-
const struct hal_flash *hal_bsp_flash_dev(uint8_t flash_id);
37+
int hal_flash_init(void);
4038

4139
#ifdef __cplusplus
4240
}

libraries/nffs/src/hw/hal/include/hal/hal_flash_int.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,26 @@ extern "C" {
2929
/*
3030
* API that flash driver has to implement.
3131
*/
32+
struct hal_flash;
33+
3234
struct hal_flash_funcs {
33-
// int (*hff_read)(uint32_t address, void *dst, uint32_t num_bytes);
34-
// int (*hff_write)(uint32_t address, const void *src, uint32_t num_bytes);
35-
// int (*hff_erase_sector)(uint32_t sector_address);
36-
int (*hff_sector_info)(int idx, uint32_t *address, uint32_t *size);
37-
// int (*hff_init)(void);
35+
int (*hff_read)(const struct hal_flash *dev, uint32_t address, void *dst,
36+
uint32_t num_bytes);
37+
int (*hff_write)(const struct hal_flash *dev, uint32_t address,
38+
const void *src, uint32_t num_bytes);
39+
int (*hff_erase_sector)(const struct hal_flash *dev,
40+
uint32_t sector_address);
41+
int (*hff_sector_info)(const struct hal_flash *dev, int idx,
42+
uint32_t *address, uint32_t *size);
43+
int (*hff_init)(const struct hal_flash *dev);
3844
};
3945

4046
struct hal_flash {
4147
const struct hal_flash_funcs *hf_itf;
4248
uint32_t hf_base_addr;
4349
uint32_t hf_size;
4450
int hf_sector_cnt;
45-
int hf_align; /* Alignment requirement. 1 if unrestricted. */
51+
int hf_align; /* Alignment requirement. 1 if unrestricted. */
4652
};
4753

4854
/*

0 commit comments

Comments
 (0)