PX4 Firmware
PX4 Autopilot Software http://px4.io
flashfs.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file param.h
36  *
37  * Global flash based parameter store.
38  *
39  * This provides the mechanisms to interface to the PX4
40  * parameter system but replace the IO with non file based flash
41  * i/o routines. So that the code my be implemented on a SMALL memory
42  * foot print device.
43  *
44  */
45 
46 #ifndef _SYSTEMLIB_FLASHPARAMS_NUTTX_PARAM_H
47 #define _SYSTEMLIB_FLASHPARAMS_NUTTX_PARAM_H
48 
49 #include <stdint.h>
50 #include <stdbool.h>
51 
52 
53 
54 /****************************************************************************
55  * Pre-processor Definitions
56  ****************************************************************************/
57 /*
58  * PARAMETER_BUFFER_SIZE must be defined larger then the maximum parameter
59  * memory needed to commit the recored + ~20 bytes. For the syslib's parameter
60  * this would be the size of the bson representations of the data
61  */
62 #if !defined(PARAMETER_BUFFER_SIZE)
63 #define PARAMETER_BUFFER_SIZE 512
64 #endif
65 
67 
68 /*
69  * Define the interface data a flash_file_token_t
70  * is like a file name
71  *
72  */
73 typedef uint32_t flash_file_tokens_t;
74 
75 typedef struct flash_file_token_t {
76  union {
78  uint8_t n[sizeof(flash_file_tokens_t)];
79  };
81 
82 /*
83  * Define the parameter "file name" Currently there is only
84  * and it is hard coded. If more are added the
85  * parameter_flashfs_write would need to support a backing buffer
86  * for when a sector is erased.
87  */
89 
90 /* Define the elements of the array passed to the
91  * parameter_flashfs_init function
92  *
93  * For example
94  * static sector_descriptor_t sector_map[] = {
95  * {1, 16 * 1024, 0x08004000},
96  * {2, 16 * 1024, 0x08008000},
97  * {0, 0, 0},
98  *
99  */
100 typedef struct sector_descriptor_t {
101  uint8_t page;
102  uint32_t size;
103  uint32_t address;
105 
106 
107 /****************************************************************************
108  * Name: parameter_flashfs_init
109  *
110  * Description:
111  * This helper function advances the flash entry header pointer to the
112  * locations of the next entry.
113  *
114  * Input Parameters:
115  * fconfig - A pointer to an null entry terminated array of
116  * flash_file_sector_t
117  * buffer - A pointer to a memory to make available to callers
118  * for write operations. When allocated to the caller
119  * space is reserved in the front for the
120  * flash_entry_header_t.
121  * If this is passes as NULL. The buffer will be
122  * allocated from the heap on calls to
123  * parameter_flashfs_alloc and fread on calls
124  * to parameter_flashfs_free
125  *
126  * size - The size of the buffer in bytes. Should be be 0 if buffer
127  * is NULL
128  *
129  * Returned value:
130  * - A pointer to the next file header location
131  *
132  *
133  ****************************************************************************/
134 
135 __EXPORT int parameter_flashfs_init(sector_descriptor_t *fconfig, uint8_t *buffer, uint16_t size);
136 
137 /****************************************************************************
138  * Name: parameter_flashfs_read
139  *
140  * Description:
141  * This function returns a pointer to the locations of the data associated
142  * with the file token. On successful return *buffer will be set to Flash
143  * location and *buf_size the length of the user data.
144  *
145  * Input Parameters:
146  * token - File Token File to read
147  * buffer - A pointer to a pointer that will receive the address
148  * in flash of the data of this "files" data
149  * buf_size - A pointer to receive the number of bytes in the "file"
150  *
151  * Returned value:
152  * On success number of bytes read or a negative errno value,
153  *
154  *
155  ****************************************************************************/
156 
157 __EXPORT int parameter_flashfs_read(flash_file_token_t ft, uint8_t **buffer, size_t *buf_size);
158 
159 /****************************************************************************
160  * Name: parameter_flashfs_write
161  *
162  * Description:
163  * This function writes user data from the buffer allocated with a previous call
164  * to parameter_flashfs_alloc. flash starting at the given address
165  *
166  * Input Parameters:
167  * token - File Token File to read
168  * buffer - A pointer to a buffer with buf_size bytes to be written
169  * to the flash. This buffer must be allocated
170  * with a previous call to parameter_flashfs_alloc
171  * buf_size - Number of bytes to write
172  *
173  * Returned value:
174  * On success the number of bytes written On Error a negative value of errno
175  *
176  ****************************************************************************/
177 
178 __EXPORT int parameter_flashfs_write(flash_file_token_t ft, uint8_t *buffer, size_t buf_size);
179 
180 /****************************************************************************
181  * Name: parameter_flashfs_erase
182  *
183  * Description:
184  * This function erases the sectors that were passed to parameter_flashfs_init
185  *
186  * Input Parameters:
187  *
188  * Returned value:
189  * On success the number of bytes erased
190  * On Error a negative value of errno
191  *
192  ****************************************************************************/
193 
195 
196 /****************************************************************************
197  * Name: parameter_flashfs_alloc
198  *
199  * Description:
200  * This function is called to get a buffer to use in a subsequent call
201  * to parameter_flashfs_write. The address returned is advanced into the
202  * buffer to reserve space for the flash entry header.
203  *
204  * Input Parameters:
205  * token - File Token File to read (not used)
206  * buffer - Memory of buf_size length suitable for calling
207  * parameter_flashfs_write
208  * buf_size - The maximum number of bytes that can be written to
209  * the buffer
210  *
211  * Returned value:
212  * On success the number of bytes written On Error a negative value of errno
213  *
214  ****************************************************************************/
215 
216 __EXPORT int parameter_flashfs_alloc(flash_file_token_t ft, uint8_t **buffer, size_t *buf_size);
217 
218 
219 /****************************************************************************
220  * Name: parameter_flashfs_free
221  *
222  * Description:
223  * Frees dynamically allocated memory
224  *
225  *
226  ****************************************************************************/
227 
229 
231 #endif /* _SYSTEMLIB_FLASHPARAMS_NUTTX_PARAM_H */
#define __END_DECLS
Definition: visibility.h:59
__BEGIN_DECLS typedef uint32_t flash_file_tokens_t
Definition: flashfs.h:73
flash_file_tokens_t t
Definition: flashfs.h:77
Definition: I2C.hpp:51
__EXPORT const flash_file_token_t parameters_token
struct sector_descriptor_t sector_descriptor_t
__EXPORT void parameter_flashfs_free(void)
#define __BEGIN_DECLS
Definition: visibility.h:58
struct flash_file_token_t flash_file_token_t
__EXPORT int parameter_flashfs_alloc(flash_file_token_t ft, uint8_t **buffer, size_t *buf_size)
__EXPORT int parameter_flashfs_init(sector_descriptor_t *fconfig, uint8_t *buffer, uint16_t size)
uint32_t address
Definition: flashfs.h:103
__EXPORT int parameter_flashfs_read(flash_file_token_t ft, uint8_t **buffer, size_t *buf_size)
__EXPORT int parameter_flashfs_write(flash_file_token_t ft, uint8_t *buffer, size_t buf_size)
uint8_t n[sizeof(flash_file_tokens_t)]
Definition: flashfs.h:78
__EXPORT int parameter_flashfs_erase(void)