1
1
/* *
2
- * @file MC23LC1024 .h
2
+ * @file MC23LCXXX .h
3
3
* @version 1.0
4
4
*
5
5
* @section License
16
16
* Lesser General Public License for more details.
17
17
*/
18
18
19
- #ifndef MC23LC1024_H
20
- #define MC23LC1024_H
19
+ #ifndef MC23LCXXX_H
20
+ #define MC23LCXXX_H
21
21
22
22
#include " SPI.h"
23
23
#include " Storage.h"
24
24
25
25
/* *
26
- * Storage device driver for Microchip 23LC1024, 1M bit SPI Serial
27
- * SRAM .
26
+ * Storage device driver for Microchip 23LCXXX, SPI Serial SRAM.
27
+ * @param[in] KBYTE size .
28
28
* @param[in] SS_PIN slave select board pin.
29
29
* @param[in] FREQ max bus frequency for device (default 16 MHz).
30
- * @section Circuit
31
- * @code
32
- * 23LC1024
33
- * +------------+
34
- * (SS)----------------1-|CS U VCC|-8----------------(VCC)
35
- * (MISO)--------------2-|SO HOLD|-7---------(VCC/PULLUP)
36
- * (VCC/PULLUP)--------3-|NU SCK|-6----------------(SCK)
37
- * (GND)---------------4-|VSS SI|-5---------------(MOSI)
38
- * +------------+
39
- * @endcode
40
30
*/
41
- template <BOARD::pin_t SS_PIN, uint32_t FREQ = 16000000L >
42
- class MC23LC1024 : public Storage ,
31
+ template <uint16_t KBYTE, BOARD::pin_t SS_PIN, uint32_t FREQ = 16000000L >
32
+ class MC23LCXXX : public Storage ,
43
33
protected SPI::Device<0 , MSBFIRST, FREQ, SS_PIN>
44
34
{
45
35
public:
@@ -49,9 +39,10 @@ class MC23LC1024 : public Storage,
49
39
/* *
50
40
* Construct and initiate device driver with given slave select
51
41
* board pin, max bus frequency, and bus manager.
42
+ * @param[in] spi bus manager.
52
43
*/
53
- MC23LC1024 (SPI& spi) :
54
- Storage (128 * 1024UL ),
44
+ MC23LCXXX (SPI& spi) :
45
+ Storage (KBYTE * 1024UL ),
55
46
SPI::Device<0 , MSBFIRST, FREQ, SS_PIN>(spi)
56
47
{}
57
48
@@ -69,12 +60,21 @@ class MC23LC1024 : public Storage,
69
60
{
70
61
uint8_t * sp = (uint8_t *) &src;
71
62
header_t header;
63
+ size_t size;
72
64
header.cmd = READ;
73
- header.addr [0 ] = sp[2 ];
74
- header.addr [1 ] = sp[1 ];
75
- header.addr [2 ] = sp[0 ];
65
+ if (KBYTE > 64 ) {
66
+ header.addr [0 ] = sp[2 ];
67
+ header.addr [1 ] = sp[1 ];
68
+ header.addr [2 ] = sp[0 ];
69
+ size = sizeof (header);
70
+ }
71
+ else {
72
+ header.addr [0 ] = sp[1 ];
73
+ header.addr [1 ] = sp[0 ];
74
+ size = sizeof (header) - 1 ;
75
+ }
76
76
acquire ();
77
- write (&header, sizeof (header) );
77
+ write (&header, size );
78
78
read (dst, count);
79
79
release ();
80
80
return (count);
@@ -94,12 +94,21 @@ class MC23LC1024 : public Storage,
94
94
{
95
95
uint8_t * dp = (uint8_t *) &dst;
96
96
header_t header;
97
+ size_t size;
97
98
header.cmd = WRITE;
98
- header.addr [0 ] = dp[2 ];
99
- header.addr [1 ] = dp[1 ];
100
- header.addr [2 ] = dp[0 ];
99
+ if (KBYTE > 64 ) {
100
+ header.addr [0 ] = dp[2 ];
101
+ header.addr [1 ] = dp[1 ];
102
+ header.addr [2 ] = dp[0 ];
103
+ size = sizeof (header);
104
+ }
105
+ else {
106
+ header.addr [0 ] = dp[1 ];
107
+ header.addr [1 ] = dp[0 ];
108
+ size = sizeof (header) - 1 ;
109
+ }
101
110
acquire ();
102
- write (&header, sizeof (header) );
111
+ write (&header, size );
103
112
write (src, count);
104
113
release ();
105
114
return (count);
@@ -109,7 +118,7 @@ class MC23LC1024 : public Storage,
109
118
/* * Command and address header. */
110
119
struct header_t {
111
120
uint8_t cmd; // !< Command code.
112
- uint8_t addr[3 ]; // !< 24-bit address in MSB order.
121
+ uint8_t addr[3 ]; // !< 16/ 24-bit address in MSB order.
113
122
} __attribute__((packed));
114
123
115
124
/* * Command codes. */
@@ -126,4 +135,60 @@ class MC23LC1024 : public Storage,
126
135
using SPI::Device<0 ,MSBFIRST,FREQ,SS_PIN>::write;
127
136
using SPI::Device<0 ,MSBFIRST,FREQ,SS_PIN>::release;
128
137
};
138
+
139
+ /* *
140
+ * Storage device driver for Microchip 23LC512, 512 Kbit SPI Serial
141
+ * SRAM.
142
+ * @param[in] SS_PIN slave select board pin.
143
+ * @param[in] FREQ max bus frequency for device (default 16 MHz).
144
+ * @section Circuit
145
+ * @code
146
+ * 23LC512
147
+ * +------------+
148
+ * (SS)----------------1-|CS U VCC|-8----------------(VCC)
149
+ * (MISO)--------------2-|SO HOLD|-7---------(VCC/PULLUP)
150
+ * (VCC/PULLUP)--------3-|NU SCK|-6----------------(SCK)
151
+ * (GND)---------------4-|VSS SI|-5---------------(MOSI)
152
+ * +------------+
153
+ * @endcode
154
+ */
155
+ template <BOARD::pin_t SS_PIN, uint32_t FREQ = 16000000L >
156
+ class MC23LC512 : public MC23LCXXX <64 , SS_PIN, FREQ> {
157
+ public:
158
+ /* *
159
+ * Construct and initiate device driver with given slave select
160
+ * board pin, max bus frequency, and bus manager.
161
+ * @param[in] spi bus manager.
162
+ */
163
+ MC23LC512 (SPI& spi) : MC23LCXXX<64 , SS_PIN, FREQ>(spi) {}
164
+ };
165
+
166
+ /* *
167
+ * Storage device driver for Microchip 23LC1024, 1M bit SPI Serial
168
+ * SRAM.
169
+ * @param[in] SS_PIN slave select board pin.
170
+ * @param[in] FREQ max bus frequency for device (default 16 MHz).
171
+ * @section Circuit
172
+ * @code
173
+ * 23LC1024
174
+ * +------------+
175
+ * (SS)----------------1-|CS U VCC|-8----------------(VCC)
176
+ * (MISO)--------------2-|SO HOLD|-7---------(VCC/PULLUP)
177
+ * (VCC/PULLUP)--------3-|NU SCK|-6----------------(SCK)
178
+ * (GND)---------------4-|VSS SI|-5---------------(MOSI)
179
+ * +------------+
180
+ * @endcode
181
+ */
182
+ template <BOARD::pin_t SS_PIN, uint32_t FREQ = 16000000L >
183
+ class MC23LC1024 : public MC23LCXXX <128 , SS_PIN, FREQ>
184
+ {
185
+ public:
186
+ /* *
187
+ * Construct and initiate device driver with given slave select
188
+ * board pin, max bus frequency, and bus manager.
189
+ * @param[in] spi bus manager.
190
+ */
191
+ MC23LC1024 (SPI& spi) : MC23LCXXX<128 , SS_PIN, FREQ>(spi) {}
192
+ };
193
+
129
194
#endif
0 commit comments