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.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2014 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_cat25m.h : On Semiconductor CAT25M SPI EEPROM Driver
14 Author(s): Pieter Conradie
15 Creation Date: 2014-10-16
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20
21/* _____PROJECT INCLUDES_____________________________________________________ */
22#include "px_cat25m.h"
23
24/* _____LOCAL DEFINITIONS____________________________________________________ */
25/// @name commands
26/// @{
27#define PX_CAT25M_CMD_WREN 0x06 ///< Enable Write Operations
28#define PX_CAT25M_CMD_WRDI 0x04 ///< Disable Write Operations
29#define PX_CAT25M_CMD_RDSR 0x05 ///< Read Status Register
30#define PX_CAT25M_CMD_WRSR 0x01 ///< Write Status Register
31#define PX_CAT25M_CMD_READ 0x03 ///< Read Data from Memory
32#define PX_CAT25M_CMD_WRITE 0x02 ///< Write Data to Memory
33/// @}
34
35/* _____MACROS_______________________________________________________________ */
36
37/* _____GLOBAL VARIABLES_____________________________________________________ */
38
39/* _____LOCAL VARIABLES______________________________________________________ */
40static bool px_cat25m_ready_flag;
41static px_spi_handle_t * px_cat25m_spi_handle;
42
43/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
44
45/* _____LOCAL FUNCTIONS______________________________________________________ */
46
47/* _____GLOBAL FUNCTIONS_____________________________________________________ */
49{
50 // Set flag
51 px_cat25m_ready_flag = true;
52 // Save SPI slave device handle
53 px_cat25m_spi_handle = handle;
54}
55
56void px_cat25m_rd(void * buf, px_cat25m_adr_t adr, size_t nr_of_bytes)
57{
58 uint8_t spi_data[4];
59 uint8_t *bufffer_u8 = (uint8_t *)buf;
60
61 // See if specified address is out of bounds
62 if(adr > PX_CAT25M_ADR_MAX)
63 {
64 return;
65 }
66 // Wait until EEPROM is not busy
67 while(!px_cat25m_ready()) {;}
68 // Send command
69 spi_data[0] = PX_CAT25M_CMD_READ;
70 spi_data[1] = PX_U32_MH8(adr);
71 spi_data[2] = PX_U32_ML8(adr);
72 spi_data[3] = PX_U32_LO8(adr);
73 px_spi_wr(px_cat25m_spi_handle, spi_data, 4, PX_SPI_FLAG_START);
74 // Read data
75 px_spi_rd(px_cat25m_spi_handle, buf, nr_of_bytes, PX_SPI_FLAG_STOP);
76}
77
78void px_cat25m_rd_page(void * buf, uint16_t page)
79{
80 uint16_t i;
81 uint8_t * bufffer_u8 = (uint8_t *)buf;
82 uint8_t spi_data[4];
83
84 // Wait until EEPROM is not busy
85 while(!px_cat25m_ready()) {;}
86 // Send command
87 spi_data[0] = PX_CAT25M_CMD_READ;
88 spi_data[1] = PX_U16_HI8(page);
89 spi_data[2] = PX_U16_LO8(page);
90 spi_data[3] = 0;
91 px_spi_wr(px_cat25m_spi_handle, spi_data, 4, PX_SPI_FLAG_START);
92 // Read data
93 px_spi_rd(px_cat25m_spi_handle, buf, PX_CAT25M_PAGE_SIZE, PX_SPI_FLAG_STOP);
94}
95
97 uint16_t page,
98 uint8_t start_byte_in_page,
99 size_t nr_of_bytes)
100{
101 uint8_t * bufffer_u8 = (uint8_t *)buf;
102 uint8_t spi_data[4];
103
104 // Wait until EEPROM is not busy
105 while(!px_cat25m_ready()) {;}
106 // Send command
107 spi_data[0] = PX_CAT25M_CMD_READ;
108 spi_data[1] = PX_U16_HI8(page);
109 spi_data[2] = PX_U16_LO8(page);
110 spi_data[3] = start_byte_in_page;
111 px_spi_wr(px_cat25m_spi_handle, spi_data, 4, PX_SPI_FLAG_START);
112 // Read data
113 px_spi_rd(px_cat25m_spi_handle, buf, nr_of_bytes, PX_SPI_FLAG_STOP);
114}
115
116void px_cat25m_wr_page(const void * buf, uint16_t page)
117{
118 uint16_t i;
119 uint8_t * bufffer_u8 = (uint8_t *)buf;
120 uint8_t spi_data[4];
121
122 // Wait until EEPROM is not busy
123 while(!px_cat25m_ready()) {;}
124 // Enable write
126 // Send command
127 spi_data[0] = PX_CAT25M_CMD_WRITE;
128 spi_data[1] = PX_U16_HI8(page);
129 spi_data[2] = PX_U16_LO8(page);
130 spi_data[3] = 0;
131 px_spi_wr(px_cat25m_spi_handle, spi_data, 4, PX_SPI_FLAG_START);
132 // Write data
133 px_spi_wr(px_cat25m_spi_handle, buf, PX_CAT25M_PAGE_SIZE, PX_SPI_FLAG_STOP);
134 // Set flag to busy
135 px_cat25m_ready_flag = false;
136}
137
138void px_cat25m_wr_page_offset(const void * buf,
139 uint16_t page,
140 uint8_t start_byte_in_page,
141 size_t nr_of_bytes)
142{
143 uint8_t * bufffer_u8 = (uint8_t *)buf;
144 uint8_t spi_data[4];
145
146 // Wait until EEPROM is not busy
147 while(!px_cat25m_ready()) {;}
148 // Enable write
150 // Send command
151 spi_data[0] = PX_CAT25M_CMD_WRITE;
152 spi_data[1] = PX_U16_HI8(page);
153 spi_data[2] = PX_U16_LO8(page);
154 spi_data[3] = start_byte_in_page;
155 px_spi_wr(px_cat25m_spi_handle, spi_data, 4, PX_SPI_FLAG_START);
156 // Write data
157 px_spi_wr(px_cat25m_spi_handle, buf, nr_of_bytes, PX_SPI_FLAG_STOP);
158 // Set flag to busy
159 px_cat25m_ready_flag = false;
160}
161
163{
165 {
166 return true;
167 }
168 else
169 {
170 return false;
171 }
172}
173
175{
176 uint8_t spi_data[1];
177
178 // Send command
179 spi_data[0] = PX_CAT25M_CMD_RDSR;
180 px_spi_wr(px_cat25m_spi_handle, spi_data, 1, PX_SPI_FLAG_START);
181 // Read status
182 px_spi_rd(px_cat25m_spi_handle, spi_data, 1, PX_SPI_FLAG_STOP);
183
184 return spi_data[0];
185}
186
187void px_cat25m_status_wr(uint8_t status)
188{
189 uint8_t spi_data[2];
190
191 // Send command
192 spi_data[0] = PX_CAT25M_CMD_WRSR;
193 spi_data[1] = status;
194 px_spi_wr(px_cat25m_spi_handle, spi_data, 2, PX_SPI_FLAG_START_AND_STOP);
195}
196
198{
199 uint8_t spi_data[1];
200
201 // Send command
202 spi_data[0] = PX_CAT25M_CMD_WREN;
203 px_spi_wr(px_cat25m_spi_handle, spi_data, 1, PX_SPI_FLAG_START_AND_STOP);
204}
205
207{
208 uint8_t spi_data[1];
209
210 // Send command
211 spi_data[0] = PX_CAT25M_CMD_WRDI;
212 px_spi_wr(px_cat25m_spi_handle, spi_data, 1, PX_SPI_FLAG_START_AND_STOP);
213}
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
#define PX_CAT25M_STATUS_RDY
Ready (inverted)
Definition: px_cat25m.h:84
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
#define PX_CAT25M_ADR_MAX
Maximum adress.
Definition: px_cat25m.h:74
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 PX_U16_HI8(data)
Extract the high 8 bits of a 16-bit value (Most Significant Byte)
Definition: px_defs.h:225
#define PX_U32_LO8(data)
Extract the low 8 bits (bits 7..0) of a 32-bit value.
Definition: px_defs.h:240
#define PX_U32_MH8(data)
Extract the middle high 8 bits (bits 23..16) of a 32-bit value.
Definition: px_defs.h:234
#define PX_U32_ML8(data)
Extract the middle low 8 bits (bits 15..8) of a 32-bit value.
Definition: px_defs.h:237
#define PX_U16_LO8(data)
Extract the low 8 bits of a 16-bit value (Least Significant Byte)
Definition: px_defs.h:228
#define PX_SPI_FLAG_START_AND_STOP
Begin and finish SPI transaction.
Definition: px_spi.h:121
#define PX_SPI_FLAG_START
Begin SPI transaction (take SPI slave's Chip Select line low)
Definition: px_spi.h:117
void px_spi_wr(px_spi_handle_t *handle, const void *data, size_t nr_of_bytes, uint8_t flags)
Perform an SPI write transaction with an SPI slave.
Definition: px_spi.c:396
void px_spi_rd(px_spi_handle_t *handle, void *data, size_t nr_of_bytes, uint8_t flags)
Perform an SPI read transaction with an SPI slave.
Definition: px_spi.c:479
#define PX_SPI_FLAG_STOP
Finish SPI transaction (take SPI slave's Chip Select line high)
Definition: px_spi.h:119
Define SPI handle.
Definition: px_spi.h:126