px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_cat25m.h
1#ifndef __PX_CAT25M_H__
2#define __PX_CAT25M_H__
3/* =============================================================================
4 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
5 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
6 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
7 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
8 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
9
10 Copyright (c) 2014 Pieter Conradie <https://piconomix.com>
11
12 License: MIT
13 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
14
15 Title: px_cat25m.h : On Semiconductor CAT25M SPI EEPROM Driver
16 Author(s): Pieter Conradie
17 Creation Date: 2014-10-16
18
19============================================================================= */
20/**
21 * @ingroup DEVICES_MEM
22 * @defgroup PX_CAT25M px_cat25m.h : On Semiconductor CAT25M SPI EEPROM Driver
23 *
24 * Driver that communicates with an On Semiconductor CAT25M EEPROM using SPI.
25 *
26 * File(s):
27 * - devices/mem/inc/px_cat25m.h
28 * - devices/mem/inc/px_cat25m_cfg_template.h
29 * - devices/mem/src/px_cat25m.c
30 *
31 * Reference:
32 * - [On Semiconductor CAT25M01](http://www.onsemi.com/pub/Collateral/CAT25M01-D.PDF) 1 Mbit SPI Serial CMOS EEPROM datasheet
33 *
34 * @{
35 */
36
37/* _____PROJECT INCLUDES_____________________________________________________ */
38#include "px_defs.h"
39#include "px_spi.h"
40
41// Include project specific configuration. See "px_cat25m_cfg_template.h"
42#include "px_cat25m_cfg.h"
43
44// Check that all project specific options have been specified in "px_cat25m_cfg.h"
45#ifndef PX_CAT25M_CFG_DEVICE
46#error "PX_CAT25M_CFG_DEVICE not specified"
47#endif
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52/* _____DEFINITIONS__________________________________________________________ */
53/// @name List CAT25M devices
54/// @{
55#define PX_CAT25M01 0 /// 1 Mbit SPI Serial CMOS EEPROM
56#define PX_CAT25M02 1 /// 2 Mbit SPI Serial CMOS EEPROM
57/// @}
58
59// Determine number of pages and page size according to device specified
60#if (PX_CAT25M_CFG_DEVICE == PX_CAT25M01)
61#define PX_CAT25M_PAGES 512ul
62#define PX_CAT25M_PAGE_SIZE 256ul
63#elif (PX_CAT25M_CFG_DEVICE == PX_CAT25M02)
64#define PX_CAT25M_PAGES 1024ul
65#define PX_CAT25M_PAGE_SIZE 256ul
66#else
67#error "Invalid CAT25M device specified"
68#endif
69
70/// EEPROM size (in bytes)
71#define PX_CAT25M_EEPROM_SIZE_BYTES (PX_CAT25M_PAGES * PX_CAT25M_PAGE_SIZE)
72
73/// Maximum adress
74#define PX_CAT25M_ADR_MAX (PX_CAT25M_EEPROM_SIZE_BYTES - 1)
75
76/// @name Status register
77/// @{
78#define PX_CAT25M_STATUS_WPEN 7 ///< Write Protect Enable
79#define PX_CAT25M_STATUS_IPL 6 ///< Identification Page Latch
80#define PX_CAT25M_STATUS_LIP 4 ///< Lock Identification Page
81#define PX_CAT25M_STATUS_BP1 3 ///< Block Protect bit 1
82#define PX_CAT25M_STATUS_BP0 2 ///< Block Protect bit 0
83#define PX_CAT25M_STATUS_WEL 1 ///< Write Enable Latch
84#define PX_CAT25M_STATUS_RDY 0 ///< Ready (inverted)
85/// @}
86
87/// Maximum SPI Clock rate
88#define PX_CAT25M_MAX_SPI_CLOCK_HZ 10000000
89/// SPI Clock / Data phase
90#define PX_CAT25M_SPI_MODE PX_SPI_MODE0
91/// SPI Data order
92#define PX_CAT25M_SPI_DATA_ORDER PX_SPI_DATA_ORDER_MSB
93
94/* _____TYPE DEFINITIONS_____________________________________________________ */
95/// Address size
96typedef uint32_t px_cat25m_adr_t;
97
98/* _____GLOBAL VARIABLES_____________________________________________________ */
99
100/* _____GLOBAL FUNCTION DECLARATIONS_________________________________________ */
101/**
102 * Initialise driver.
103 *
104 * @param handle SPI handle to use for SPI slave device
105 */
106void px_cat25m_init(px_spi_handle_t * handle);
107
108/**
109 * Read data from EEPROM.
110 *
111 * This function reads data from EEPROM and stores it in the specified buffer.
112 *
113 * @param[out] buf Buffer to store read data
114 * @param[in] adr 0 to PX_CAT25M_ADR_MAX
115 * @param[in] nr_of_bytes Number of bytes to read
116 */
117void px_cat25m_rd(void * buf, px_cat25m_adr_t adr, size_t nr_of_bytes);
118
119/**
120 * Read a page from EEPROM.
121 *
122 * This function reads a page of data from EEPROM and stores it in the
123 * specified buffer. The buffer must be at least PX_CAT25M_PAGE_SIZE bytes in size
124 * to accomodate a full page.
125 *
126 * The EEPROM has PX_CAT25M_PAGES pages.
127 *
128 * @param[out] buf Buffer to store read data
129 * @param[in] page 0 to (PX_CAT25M_PAGES - 1)
130 *
131 */
132void px_cat25m_rd_page(void * buf, uint16_t page);
133
134/**
135 * Partial read of data in a page of EEPROM.
136 *
137 * This function reads part of a page of data from EEPROM and stores it in
138 * the specified buffer. The buffer must be at least @b nr_of_bytes in size
139 * to the read data.
140 *
141 * @note
142 *
143 * Only read up to the end of the specified page. If the page boundary is
144 * exceeded, the index will wrap to the start of the page, i.e. only the
145 * content of the specified page will be read.
146 *
147 * @param[out] buf Buffer to store read data
148 * @param[in] page 0 to (PX_CAT25M_PAGES - 1)
149 * @param[in] start_byte_in_page Index of first byte to read (0 to PX_CAT25M_PAGE_SIZE - 1)
150 * @param[in] nr_of_bytes Number of bytes to read
151 *
152 */
153void px_cat25m_rd_page_offset(void * buf,
154 uint16_t page,
155 uint8_t start_byte_in_page,
156 size_t nr_of_bytes);
157
158/**
159 * Write a page from EEPROM.
160 *
161 * This function writes a page of data to EEPROM using the specified
162 * buffer as the source. The buffer must contain at least PX_CAT25M_PAGE_SIZE bytes
163 * of data.
164 *
165 * The EEPROM has PX_CAT25M_PAGES pages.
166 *
167 * @param[in] buf Buffer containing data to be written
168 * @param[in] page 0 to (PX_CAT25M_PAGES - 1)
169 *
170 */
171void px_cat25m_wr_page(const void * buf, uint16_t page);
172
173/**
174 * Partial write of data in a page of EEPROM.
175 *
176 * This function writes a part of a page of data to EEPROM using the
177 * specified buffer as source. The buffer must contain at least @b nr_of_bytes
178 * of data.
179 *
180 * @note
181 *
182 * Only write up to the end of the specified page. If the page boundary is
183 * exceeded, the index will wrap to the start of the page, i.e. only the
184 * content of the specified page will be written.
185 *
186 * @param[in] buf Buffer containing data to be written
187 * @param[in] page 0 to (PX_CAT25M_PAGES - 1)
188 * @param[in] start_byte_in_page Index of first byte to write (0 to PX_CAT25M_PAGE_SIZE - 1)
189 * @param[in] nr_of_bytes Number of bytes to write
190 *
191 */
192void px_cat25m_wr_page_offset(const void * buf,
193 uint16_t page,
194 uint8_t start_byte_in_page,
195 size_t nr_of_bytes);
196
197/**
198 * Check if EEPROM is ready for the next read or write access.
199 *
200 * When data is written to EEPROM, the microcontroller must wait until the
201 * operation is finished, before the next one is attempted. This function
202 * allows the microcontroller to do something else while waiting for the
203 * operation to finish.
204 *
205 * @retval true EEPROM is ready for next read or write access.
206 * @retval false EEPROM is busy writing.
207 */
208bool px_cat25m_ready(void);
209
210/**
211 * Read the status register of the EEPROM.
212 *
213 * The status register contains flags (e.g. PX_CAT25M_STATUS_RDY). This function
214 * can be used to check that a valid and working EEPROM device is connected.
215 *
216 * @return uint8_t Status register value
217 */
218uint8_t px_cat25m_status_rd(void);
219
220/**
221 * Write to the status register of the EEPROM
222 *
223 * @param status Value to write to the status register
224 */
225void px_cat25m_status_wr(uint8_t status);
226
227/**
228 * Enable writing.
229 */
230void px_cat25m_wr_en(void);
231
232/**
233 * Disable writing.
234 */
235void px_cat25m_wr_dis(void);
236
237/* _____MACROS_______________________________________________________________ */
238
239#ifdef __cplusplus
240}
241#endif
242
243/// @}
244#endif
void px_cat25m_rd_page(void *buf, uint16_t page)
Read a page from EEPROM.
Definition: px_cat25m.c:78
void px_cat25m_wr_page(const void *buf, uint16_t page)
Write a page from EEPROM.
Definition: px_cat25m.c:116
bool px_cat25m_ready(void)
Check if EEPROM is ready for the next read or write access.
Definition: px_cat25m.c:162
void px_cat25m_rd(void *buf, px_cat25m_adr_t adr, size_t nr_of_bytes)
Read data from EEPROM.
Definition: px_cat25m.c:56
void px_cat25m_wr_page_offset(const void *buf, uint16_t page, uint8_t start_byte_in_page, size_t nr_of_bytes)
Partial write of data in a page of EEPROM.
Definition: px_cat25m.c:138
uint8_t px_cat25m_status_rd(void)
Read the status register of the EEPROM.
Definition: px_cat25m.c:174
void px_cat25m_wr_en(void)
Enable writing.
Definition: px_cat25m.c:197
uint32_t px_cat25m_adr_t
Address size.
Definition: px_cat25m.h:96
void px_cat25m_status_wr(uint8_t status)
Write to the status register of the EEPROM.
Definition: px_cat25m.c:187
void px_cat25m_wr_dis(void)
Disable writing.
Definition: px_cat25m.c:206
void px_cat25m_init(px_spi_handle_t *handle)
Initialise driver.
Definition: px_cat25m.c:48
void px_cat25m_rd_page_offset(void *buf, uint16_t page, uint8_t start_byte_in_page, size_t nr_of_bytes)
Partial read of data in a page of EEPROM.
Definition: px_cat25m.c:96
Define SPI handle.
Definition: px_spi.h:126