Contiki-NG
ext-flash.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
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  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*---------------------------------------------------------------------------*/
31 /**
32  * \addtogroup dev
33  * @{
34  *
35  * \defgroup ext-flash Generic external SPI flash driver
36  *
37  * This is a generic driver for external SPI flash memories. The driver has
38  * been tested and works with multiple external SPI flash parts. The list of
39  * parts the driver has been tested against is shown in the README in this
40  * directory.
41  *
42  * If you successfully use this driver with a part that is not listed in the
43  * README, please let us know so we can update it.
44  *
45  * @{
46  *
47  * \file
48  * Header file for the external SPI flash API
49  */
50 /*---------------------------------------------------------------------------*/
51 #ifndef EXT_FLASH_H_
52 #define EXT_FLASH_H_
53 /*---------------------------------------------------------------------------*/
54 #include "dev/spi.h"
55 #include <stdint.h>
56 #include <stdlib.h>
57 #include <stdbool.h>
58 /*---------------------------------------------------------------------------*/
59 /**
60  * \brief Initialize storage driver.
61  * \param conf SPI bus configuration struct. NULL for default.
62  * \return True when successful.
63  */
64 bool ext_flash_open(const spi_device_t *conf);
65 
66 /**
67  * \brief Close the storage driver
68  * \param conf SPI bus configuration struct. NULL for default.
69  * \return True when successful.
70  *
71  * This call will put the device in its lower power mode (power down).
72  */
73 bool ext_flash_close(const spi_device_t *conf);
74 
75 /**
76  * \brief Read storage content
77  * \param conf SPI bus configuration struct. NULL for default.
78  * \param offset Address to read from
79  * \param length Number of bytes to read
80  * \param buf Buffer where to store the read bytes
81  * \return True when successful.
82  *
83  * buf must be allocated by the caller
84  */
85 bool ext_flash_read(const spi_device_t *conf, uint32_t offset, uint32_t length, uint8_t *buf);
86 
87 /**
88  * \brief Erase storage sectors corresponding to the range.
89  * \param conf SPI bus configuration struct. NULL for default.
90  * \param offset Address to start erasing
91  * \param length Number of bytes to erase
92  * \return True when successful.
93  *
94  * The erase operation will be sector-wise, therefore a call to this function
95  * will generally start the erase procedure at an address lower than offset
96  */
97 bool ext_flash_erase(const spi_device_t *conf, uint32_t offset, uint32_t length);
98 
99 /**
100  * \brief Write to storage sectors.
101  * \param conf SPI bus configuration struct. NULL for default.
102  * \param offset Address to write to
103  * \param length Number of bytes to write
104  * \param buf Buffer holding the bytes to be written
105  *
106  * \return True when successful.
107  */
108 bool ext_flash_write(const spi_device_t *conf, uint32_t offset, uint32_t length, const uint8_t *buf);
109 
110 /**
111  * \brief Initialise the external flash
112  * \param conf SPI bus configuration struct. NULL for default.
113  *
114  * This function will explicitly put the part in its lowest power mode
115  * (power-down).
116  *
117  * In order to perform any operation, the caller must first wake the device
118  * up by calling ext_flash_open()
119  */
120 bool ext_flash_init(const spi_device_t *conf);
121 /*---------------------------------------------------------------------------*/
122 #endif /* EXT_FLASH_H_ */
123 /*---------------------------------------------------------------------------*/
124 /**
125  * @}
126  * @}
127  */
bool ext_flash_read(const spi_device_t *conf, uint32_t offset, uint32_t length, uint8_t *buf)
Read storage content.
Definition: ext-flash.c:374
bool ext_flash_open(const spi_device_t *conf)
Initialize storage driver.
Definition: ext-flash.c:325
bool ext_flash_erase(const spi_device_t *conf, uint32_t offset, uint32_t length)
Erase storage sectors corresponding to the range.
Definition: ext-flash.c:475
Header file for the SPI HAL.
bool ext_flash_close(const spi_device_t *conf)
Close the storage driver.
Definition: ext-flash.c:355
bool ext_flash_init(const spi_device_t *conf)
Initialise the external flash.
Definition: ext-flash.c:527
SPI Device Configuration.
Definition: spi.h:100
bool ext_flash_write(const spi_device_t *conf, uint32_t offset, uint32_t length, const uint8_t *buf)
Write to storage sectors.
Definition: ext-flash.c:415