Contiki 3.x
flash.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Lars Schmertmann <SmallLars@t-online.de>.
3  * 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  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * This file is part of the Contiki operating system.
32  *
33  */
34 
35 /**
36  * \file
37  * App for easy usage of additional flash memory
38  *
39  * Purposes of the different flash blocks
40  * 1 : 0x18000 - 0x18FFF : Random access block 1.1
41  * 2 : 0x19000 - 0x19FFF : Random access block 1.2
42  * 3 : 0x1A000 - 0x1AFFF : Random access block 2.1
43  * 4 : 0x1B000 - 0x1BFFF : Random access block 2.2
44  * 5 : 0x1C000 - 0x1CFFF : Stack without pop function
45  * 6 : 0x1D000 - 0x1DFFF : Read only
46  * 7 : 0x1E000 - 0x1EFFF : Read only
47  * 8 : 0x1F000 - 0x1FFFF : System reserved
48  *
49  * This app only allows write access to blocks 1 - 5
50  * and read access to blocks 1 - 7.
51  *
52  * To use the stack in block 5 you need: flash_stack_init,
53  * flash_stack_push, flash_stack_size, flash_stack_read.
54  *
55  * To use the random access blocks 1.x and 2.x you need: flash_init,
56  * flash_getVar, flash_setVar, flash_cmp.
57  *
58  * Blocks 1.x and 2.x are accessible with adresses
59  * 0x0001 - 0x0FFF and 0x1001 - 0x1FFF.
60  *
61  * To be able to write to flash memory, its required to delete
62  * it first, but its only possible to delete a full block. So this
63  * app copies the data of a block, changing the requested data.
64  * Copying a block needs time. So when you only use the first N bytes,
65  * you can set FLASH_CONF_B1=N and FLASH_CONF_B2=N in your makefile
66  * to optimize speed.
67  *
68  * You can find an example in examples/econotag-flash-test.
69  *
70  * \author
71  * Lars Schmertmann <SmallLars@t-online.de>
72  */
73 
74 #ifndef FLASH_H_
75 #define FLASH_H_
76 
77 #include <nvm.h>
78 
79 #define FLASH_STACK 0x1C000
80 
81 typedef uint32_t flash_addr_t;
82 
83 /**
84  * \brief Initialize or clear random access blocks
85  *
86  * To use the random access blocks, you need to call this
87  * function first. You can also use it to delete all data
88  * in this blocks.
89  */
90 void flash_init();
91 
92 /**
93  * \brief Read data from flash memory
94  *
95  * Reads data from flash memory and stores it into RAM.
96  * You can read the flash area 0x18000 - 0x1EFFF. Addresses
97  * 0x0000 - 0x1FFF will be mapped to 0x18000 - 0x1BFFF.
98  *
99  * \param dest Memory area to store the data
100  * \param address Area in flash memory to read from
101  * \param numBytes Number of bytes to read
102  *
103  * \return gNvmErrNoError_c (0) if read was successfull
104  */
105 nvmErr_t flash_getVar(void *dest, flash_addr_t address, uint32_t numBytes);
106 
107 /**
108  * \brief Write data to flash memory
109  *
110  * Writes data to flash memory. Valid addresses are
111  * 0x0001 - 0x0FFF and 0x1001 - 0x1FFF -> Mapped to
112  * 0x18000 - 0x1BFFF.
113  *
114  * \param src Memory area with data to store in flash memory
115  * \param address Area in flash memory to write
116  * \param numBytes Number of bytes to write
117  *
118  * \return gNvmErrNoError_c (0) if write was successfull
119  */
120 nvmErr_t flash_setVar(void *src, flash_addr_t address, uint32_t numBytes);
121 
122 /**
123  * \brief Compares data from RAM with flash memory
124  *
125  * Compares data from RAM with flash memory.
126  * Valid addresses are 0x18000 - 0x1EFFF. Addresses
127  * 0x0 - 0x1FFF will be mapped to 0x18000 - 0x1BFFF.
128  *
129  * \param src Memory area with data to compare
130  * \param address Area in flash memory
131  * \param numBytes Number of bytes to compare
132  *
133  * \return 0 if data is matching
134  */
135 nvmErr_t flash_cmp(void *src, flash_addr_t address, uint32_t numBytes);
136 
137 /**
138  * \brief Stack initialisation
139  *
140  * Clears and initializes the stack.
141  */
142 void flash_stack_init();
143 
144 /**
145  * \brief Push data to stack
146  *
147  * Pushes numBytes from src to stack into flash memory.
148  *
149  * \param src Memory area with data to store in flash memory
150  * \param numBytes Number of bytes to write
151  *
152  * \return gNvmErrNoError_c (0) if push was successfull
153  */
154 nvmErr_t flash_stack_push(uint8_t *src, uint32_t numBytes);
155 
156 /**
157  * \brief Stacksize
158  *
159  * Returns the size of data in stack
160  *
161  * \return Number of bytes in stack
162  */
163 uint32_t flash_stack_size();
164 
165 /**
166  * \brief Read data from stack
167  *
168  * Reads data from stack (without removing it) and stores it into RAM.
169  *
170  * \param dest Memory area to store the data
171  * \param offset Position in stack to read from
172  * \param numBytes Number of bytes to read
173  *
174  * \return gNvmErrNoError_c (0) if read was successfull
175  */
176 #define flash_stack_read(dest, offset, numBytes) flash_getVar(dest, FLASH_STACK + (offset), numBytes)
177 
178 #endif /* FLASH_H_ */
uint32_t flash_stack_size()
Stacksize.
Definition: flash.c:191
nvmErr_t flash_cmp(void *src, flash_addr_t address, uint32_t numBytes)
Compares data from RAM with flash memory.
Definition: flash.c:163
void flash_stack_init()
Stack initialisation.
Definition: flash.c:174
nvmErr_t flash_stack_push(uint8_t *src, uint32_t numBytes)
Push data to stack.
Definition: flash.c:180
void flash_init()
Initialize or clear random access blocks.
Definition: flash.c:69
nvmErr_t flash_setVar(void *src, flash_addr_t address, uint32_t numBytes)
Write data to flash memory.
Definition: flash.c:110
nvmErr_t flash_getVar(void *dest, flash_addr_t address, uint32_t numBytes)
Read data from flash memory.
Definition: flash.c:92