px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_at45d.h
1#ifndef __PX_AT45D_H__
2#define __PX_AT45D_H__
3/* =============================================================================
4 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
5 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
6 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
7 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
8 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
9
10 Copyright (c) 2010 Pieter Conradie <https://piconomix.com>
11
12 License: MIT
13 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
14
15 Title: px_at45d.h : Adesto AT45D Serial DataFlash Driver
16 Author(s): Pieter Conradie
17 Creation Date: 2010-04-15
18
19============================================================================= */
20/**
21 * @ingroup DEVICES_MEM
22 * @defgroup PX_AT45D px_at45d.h : Adesto AT45D DataFlash Driver
23 *
24 * Driver that communicates with an Adesto AT45D DataFlash using SPI.
25 *
26 * File(s):
27 * - devices/mem/inc/px_at45d.h
28 * - devices/mem/inc/px_at45d_cfg_template.h
29 * - devices/mem/src/px_at45d.c
30 *
31 * Reference:
32 * - [Adesto AT45DB041E](http://www.adestotech.com/sites/default/files/datasheets/doc8783.pdf) 4Mbit DataFlash datasheet
33 *
34 * Example:
35 *
36 * @include devices/mem/test/px_at45d_test.c
37 *
38 * @{
39 */
40
41/* _____PROJECT INCLUDES_____________________________________________________ */
42#include "px_defs.h"
43#include "px_spi.h"
44
45// Include project specific configuration. See "px_at45d_cfg_template.h"
46#include "px_at45d_cfg.h"
47
48// Check that all project specific options have been specified in "px_at45d_cfg.h"
49#ifndef PX_AT45D_CFG_DEVICE
50#error "PX_AT45D_CFG_DEVICE not specified"
51#endif
52#ifndef PX_AT45D_CFG_PWR_OF_TWO_PAGE_SIZE
53#error "PX_AT45D_CFG_PWR_OF_TWO_PAGE_SIZE not specified"
54#endif
55
56#ifdef __cplusplus
57extern "C" {
58#endif
59/* _____DEFINITIONS__________________________________________________________ */
60/// @name List AT45D devices
61/// @{
62#define PX_AT45DB011 0 ///< 1M bit, 2.7-Volt Minimum Serial-Interface Flash with One 264-Byte SRAM Buffer
63#define PX_AT45DB021 1 ///< 2M bit, 2.7-Volt Minimum Serial-Interface Flash with One 264-Byte SRAM Buffer
64#define PX_AT45DB041 2 ///< 4M bit 2.5-Volt or 2.7-Volt DataFlash
65#define PX_AT45DB081 3 ///< 8M bit, 2.5 or 2.7-Volt Only Serial-Interface Flash
66#define PX_AT45DB161 4 ///< 16M bit, 2.7-Volt Only Serial-Interface Flash with two SRAM Data Buffers
67#define PX_AT45DB321 5 ///< 32M bit, 2.7-Volt Only Serial Interface Flash
68#define PX_AT45DB642 6 ///< 64M bit, 2.7-Volt Dual-Interface Flash with two 1056-Byte SRAM
69/// @}
70
71// Determine number of pages and page size according to device specified
72#if (PX_AT45D_CFG_DEVICE == PX_AT45DB011)
73#define PX_AT45D_PAGES 512ul
74#define PX_AT45D_PAGE_SIZE (256 + (PX_AT45D_CFG_PWR_OF_TWO_PAGE_SIZE==0 ? 8 : 0))
75#elif (PX_AT45D_CFG_DEVICE == PX_AT45DB021)
76#define PX_AT45D_PAGES 1024ul
77#define PX_AT45D_PAGE_SIZE (256 + (PX_AT45D_CFG_PWR_OF_TWO_PAGE_SIZE==0 ? 8 : 0))
78#elif (PX_AT45D_CFG_DEVICE == PX_AT45DB041)
79#define PX_AT45D_PAGES 2048ul
80#define PX_AT45D_PAGE_SIZE (256 + (PX_AT45D_CFG_PWR_OF_TWO_PAGE_SIZE==0 ? 8 : 0))
81#define PX_AT45D_DENSITY 0x1C
82#elif (PX_AT45D_CFG_DEVICE == PX_AT45DB081)
83#define PX_AT45D_PAGES 4096ul
84#define PX_AT45D_PAGE_SIZE (256 + (PX_AT45D_CFG_PWR_OF_TWO_PAGE_SIZE==0 ? 16 : 0))
85#elif (PX_AT45D_CFG_DEVICE == PX_AT45DB161)
86#define PX_AT45D_PAGES 4096ul
87#define PX_AT45D_PAGE_SIZE (512 + (PX_AT45D_CFG_PWR_OF_TWO_PAGE_SIZE==0 ? 16 : 0))
88#elif (PX_AT45D_CFG_DEVICE == PX_AT45DB321)
89#define PX_AT45D_PAGES 8192ul
90#define PX_AT45D_PAGE_SIZE (512 + (PX_AT45D_CFG_PWR_OF_TWO_PAGE_SIZE==0 ? 16 : 0))
91#elif (PX_AT45D_CFG_DEVICE == PX_AT45DB642)
92#define PX_AT45D_PAGES 8192ul
93#define PX_AT45D_PAGE_SIZE (1024 + (PX_AT45D_CFG_PWR_OF_TWO_PAGE_SIZE==0 ? 32 : 0))
94#else
95#error "Invalid AT45D device specified"
96#endif
97
98/// Flash size (in bytes)
99#define PX_AT45D_FLASH_SIZE_BYTES (PX_AT45D_PAGES * PX_AT45D_PAGE_SIZE)
100
101/// Maximum adress
102#define PX_AT45D_ADR_MAX (PX_AT45D_FLASH_SIZE_BYTES - 1)
103
104/// @name Status register
105/// @{
106#define PX_AT45D_STATUS_READY 7 ///< Ready flag
107#define PX_AT45D_STATUS_COMP 6 ///< Compare flag
108#define PX_AT45D_STATUS_DENSITY_MASK 0x3C ///< Density mask
109#define PX_AT45D_STATUS_PROTECT 1 ///< Protect flag
110#define PX_AT45D_STATUS_PAGE_SIZE 0 ///< Page size flag
111/// @}
112
113/// Maximum SPI Clock rate
114#define PX_AT45D_MAX_SPI_CLOCK_HZ 33000000
115/// SPI Clock / Data phase
116#define PX_AT45D_SPI_MODE PX_SPI_MODE0
117/// SPI Data order
118#define PX_AT45D_SPI_DATA_ORDER PX_SPI_DATA_ORDER_MSB
119
120/* _____TYPE DEFINITIONS_____________________________________________________ */
121/// Address size can be optimized, depending on maximum size of FLASH
122#if (PX_AT45D_ADR_MAX <= 0xffff)
123typedef uint16_t px_at45d_adr_t;
124#else
125typedef uint32_t px_at45d_adr_t;
126#endif
127
128/* _____GLOBAL VARIABLES_____________________________________________________ */
129
130/* _____GLOBAL FUNCTION DECLARATIONS_________________________________________ */
131/**
132 * Initialise driver
133 *
134 * @param handle SPI handle to use for SPI slave device
135 */
136void px_at45d_init(px_spi_handle_t * handle);
137
138/**
139 * Power down device to minimise power consumption.
140 */
141void px_at45d_deep_power_down(void);
142
143/**
144 * Power up device to resume communication.
145 */
147
148/**
149 * Power down device to minimise power consumption.
150 */
152
153/**
154 * Power up device to resume communication.
155 *
156 * After this function call, wait at least 120 us (see tXUDPD in datasheet)
157 * for DataFlash to wake up, otherwise it will will ignore the commands.
158 */
160
161/**
162 * Read data from DataFlash.
163 *
164 * This function reads data from DataFlash and stores it in the specified
165 * buf.
166 *
167 * @param[out] buf Buffer to store read data
168 * @param[in] adr 0 to PX_AT45D_ADR_MAX
169 * @param[in] nr_of_bytes Number of bytes to read
170 *
171 * @return uint16_t Number of bytes actually read
172 */
173uint16_t px_at45d_rd(void * buf, px_at45d_adr_t adr, uint16_t nr_of_bytes);
174
175/**
176 * Read a page from DataFlash.
177 *
178 * This function reads a page of data from DataFlash and stores it in the
179 * specified buf. The buf must be at least PX_AT45D_PAGE_SIZE bytes in
180 * size to accomodate a full page.
181 *
182 * The DataFlash has PX_AT45D_PAGES pages.
183 *
184 * @param[out] buf Buffer to store read data
185 * @param[in] page 0 to (PX_AT45D_PAGES - 1)
186 *
187 */
188void px_at45d_rd_page(void * buf, uint16_t page);
189
190/**
191 * Partial read of data in a page of DataFlash.
192 *
193 * This function reads part of a page of data from DataFlash and stores it in
194 * the specified buf. The buf must be at least @b nr_of_bytes in size
195 * to the read data.
196 *
197 * @note
198 *
199 * Only read up to the end of the specified page. If the page boundary is
200 * exceeded, the index will wrap to the start of the page, i.e. only the
201 * content of the specified page will be read.
202 *
203 * @param[out] buf Buffer to store read data
204 * @param[in] page 0 to (PX_AT45D_PAGES - 1)
205 * @param[in] start_byte_in_page Index of first byte to read (0 to
206 * PX_AT45D_PAGE_SIZE - 1)
207 * @param[in] nr_of_bytes Number of bytes to read
208 *
209 */
210void px_at45d_rd_page_offset(void * buf,
211 uint16_t page,
212 uint16_t start_byte_in_page,
213 uint16_t nr_of_bytes);
214
215/**
216 * Write a page from DataFlash.
217 *
218 * This function writes a page of data to DataFlash using the specified buf
219 * as the source. The buf must contain at least PX_AT45D_PAGE_SIZE bytes of
220 * data.
221 *
222 * The DataFlash has PX_AT45D_PAGES pages.
223 *
224 * @param[in] buf Buffer containing data to be written
225 * @param[in] page 0 to (PX_AT45D_PAGES - 1)
226 *
227 */
228void px_at45d_wr_page(const void * buf, uint16_t page);
229
230/**
231 * Partial write of data in a page of DataFlash.
232 *
233 * This function writes a part of a page of data to DataFlash using the
234 * specified buf as source. The buf must contain at least @b nr_of_bytes
235 * of data.
236 *
237 * @note
238 *
239 * Only write up to the end of the specified page. If the page boundary is
240 * exceeded, the index will wrap to the start of the page, i.e. only the
241 * content of the specified page will be written.
242 *
243 * @param[in] buf Buffer containing data to be written
244 * @param[in] page 0 to (PX_AT45D_PAGES - 1)
245 * @param[in] start_byte_in_page Index of first byte to write (0 to PX_AT45D_PAGE_SIZE - 1)
246 * @param[in] nr_of_bytes Number of bytes to write
247 *
248 */
249void px_at45d_wr_page_offset(const void * buf,
250 uint16_t page,
251 uint16_t start_byte_in_page,
252 uint16_t nr_of_bytes);
253
254/**
255 * Erase a page of DataFlash.
256 *
257 * This function erases a page of DataFlash. The DataFlash has PX_AT45D_PAGES
258 * pages.
259 *
260 * @param[in] page 0 to (PX_AT45D_PAGES - 1)
261 */
262void px_at45d_erase(uint16_t page);
263
264/**
265 * Check if DataFlash is ready for the next read or write access.
266 *
267 * When data is written to DataFlash, the microcontroller must wait until the
268 * operation is finished, before the next one is attempted. This function
269 * allows the microcontroller to do something else while waiting for the
270 * operation to finish.
271 *
272 * @retval true DataFlash is ready for next read or write access.
273 * @retval false DataFlash is busy writing.
274 */
275bool px_at45d_ready(void);
276
277/**
278 * Read the status register of the DataFlash.
279 *
280 * The status register contains flags (e.g. PX_AT45D_STATUS_READY) and the density
281 * value (e.g. PX_AT45D_DENSITY). This function can be used to check that a valid
282 * and working DataFlash device is connected.
283 *
284 * @return uint8_t Status register value
285 */
286uint8_t px_at45d_get_status(void);
287
288/**
289 * Check if page size of the DataFlash is a power of two.
290 *
291 * The status register contains a flag PX_AT45D_STATUS_PAGE_SIZE that reports the
292 * page size: 1 = power of two, 0 = not a power of two.
293 *
294 * @retval true Page size is a power of two
295 * @retval false Page size is not a power of two
296 */
298
299/**
300 * Sets the page size of the DataFlash to a power of two.
301 *
302 * A one-time programmable configuration register is written to configure the
303 * device for "power of 2" page size. Once it has been written, it can not be
304 * reconfigured again.
305 *
306 * The device must be power cycled for this configuration bit to come into
307 * effect.
308 *
309 * @retval true page size was set to a power of two
310 * @retval false page size was already set to a power of two
311 */
313
314/* _____MACROS_______________________________________________________________ */
315
316#ifdef __cplusplus
317}
318#endif
319
320/// @}
321#endif
void px_at45d_rd_page_offset(void *buf, uint16_t page, uint16_t start_byte_in_page, uint16_t nr_of_bytes)
Partial read of data in a page of DataFlash.
Definition: px_at45d.c:205
uint8_t px_at45d_get_status(void)
Read the status register of the DataFlash.
Definition: px_at45d.c:337
bool px_at45d_ready(void)
Check if DataFlash is ready for the next read or write access.
Definition: px_at45d.c:313
void px_at45d_wr_page(const void *buf, uint16_t page)
Write a page from DataFlash.
Definition: px_at45d.c:229
uint16_t px_at45d_rd(void *buf, px_at45d_adr_t adr, uint16_t nr_of_bytes)
Read data from DataFlash.
Definition: px_at45d.c:139
void px_at45d_resume_from_deep_power_down(void)
Power up device to resume communication.
Definition: px_at45d.c:115
bool px_at45d_set_page_size_to_pwr_of_two(void)
Sets the page size of the DataFlash to a power of two.
Definition: px_at45d.c:366
void px_at45d_erase(uint16_t page)
Erase a page of DataFlash.
Definition: px_at45d.c:296
void px_at45d_deep_power_down(void)
Power down device to minimise power consumption.
Definition: px_at45d.c:107
void px_at45d_ultra_deep_power_down(void)
Power down device to minimise power consumption.
Definition: px_at45d.c:123
void px_at45d_wr_page_offset(const void *buf, uint16_t page, uint16_t start_byte_in_page, uint16_t nr_of_bytes)
Partial write of data in a page of DataFlash.
Definition: px_at45d.c:246
uint32_t px_at45d_adr_t
Address size can be optimized, depending on maximum size of FLASH.
Definition: px_at45d.h:125
void px_at45d_init(px_spi_handle_t *handle)
Initialise driver.
Definition: px_at45d.c:99
bool px_at45d_page_size_is_pwr_of_two(void)
Check if page size of the DataFlash is a power of two.
Definition: px_at45d.c:350
void px_at45d_rd_page(void *buf, uint16_t page)
Read a page from DataFlash.
Definition: px_at45d.c:184
void px_at45d_resume_from_ultra_deep_power_down(void)
Power up device to resume communication.
Definition: px_at45d.c:131
Define SPI handle.
Definition: px_spi.h:126