-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.eeprom.24xxxx.spin2
186 lines (144 loc) · 5.49 KB
/
memory.eeprom.24xxxx.spin2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
{
----------------------------------------------------------------------------------------------------
Filename: memory.eeprom.24xxxx.spin2
Description: Driver for 24xxxx series I2C EEPROMs
Author: Jesse Burt
Started: Apr 27, 2021
Updated: Aug 11, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
}
#include "memory.common.spin2h"
CON
{ default I/O configuration - these can be overridden by the parent object }
SCL = 0
SDA = 1
I2C_FREQ = 100_000
I2C_ADDR = 0
SLAVE_WR = core.SLAVE_ADDR
SLAVE_RD = core.SLAVE_ADDR|1
DEF_SCL = 0
DEF_SDA = 1
DEF_HZ = 100_000
DEF_ADDR = 0
I2C_MAX_FREQ= core.I2C_MAX_FREQ
ERASE_CELL = $FF
VAR
word _t_wr ' write cycle time
byte _page_size ' EE page size, in bytes
byte _addr_bits
OBJ
i2c: "com.i2c" ' I2C engine
core: "core.con.24xxxx" ' HW-specific constants
PUB null()
' This is not a top-level object
PUB start(): status
' Start using default I/O settings
return startx(SCL, SDA, I2C_FREQ, I2C_ADDR)
PUB startx(SCL_PIN, SDA_PIN, I2C_HZ, ADDR_BITS): status
' Start using custom I/O settings
' SCL_PIN: I2C serial clock
' SDA_PIN: I2C serial data
' I2C_HZ: I2C bus speed
' ADDR_BITS: optional address bits for alternate bus address
if ( lookdown(SCL_PIN: 0..63) and lookdown(SDA_PIN: 0..63) )
if ( status := i2c.init(SCL_PIN, SDA_PIN, I2C_HZ) )
waitms(1)
_addr_bits := (ADDR_BITS << 1)
ee_size(512) ' default to 512kbit EEPROM
if ( i2c.present(SLAVE_WR|_addr_bits) )
return
' if this point is reached, something above failed
' Double check I/O pin assignments, connections, power
' Lastly - make sure you have at least one free core/cog
set_write_cycle_time(10_000) ' max and slowest write cycle time
return FALSE
PUB stop()
' Stop the driver
i2c.deinit()
_page_size := 0
_t_wr := 0
_addr_bits := 0
PUB ee_size(size): curr_eesize
' Set EEPROM size, in kilobits
case size
1, 2:
_page_size := 8
4, 8, 16:
_page_size := 16
32, 64:
_page_size := 32
128, 256:
_page_size := 64
512:
_page_size := 128
1024, 2048:
_page_size := 256
other:
return
PUB page_size(): psz
' Page size of currently set EEPROM size
return _page_size
PUB rd_block_lsbf(ptr_buff, addr, nr_bytes) | cmd_pkt
' Read a block of memory starting at addr, LSB-first
cmd_pkt.byte[0] := (SLAVE_WR | _addr_bits)
cmd_pkt.byte[1] := addr.byte[1]
cmd_pkt.byte[2] := addr.byte[0]
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 3)
i2c.start()
i2c.write(SLAVE_RD | _addr_bits)
i2c.rdblock_lsbf(ptr_buff, nr_bytes, i2c.NAK)
i2c.stop()
PUB rd_block_msbf(ptr_buff, addr, nr_bytes) | cmd_pkt
' Read a block of memory starting at addr, MSB-first
cmd_pkt.byte[0] := (SLAVE_WR | _addr_bits)
cmd_pkt.byte[1] := addr.byte[1]
cmd_pkt.byte[2] := addr.byte[0]
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 3)
i2c.start()
i2c.write(SLAVE_RD | _addr_bits)
i2c.rdblock_msbf(ptr_buff, nr_bytes, i2c.NAK)
i2c.stop()
PUB set_write_cycle_time(t)
' Set the write cycle time, in microseconds
' Valid values:
' 5_000..10_000 (clamped to range)
_t_wr := 5_000 #> t <# 10_000
PUB wr_block_lsbf(addr, ptr_buff, nr_bytes) | cmd_pkt
' Write a block of memory starting at addr, LSB-first
cmd_pkt.byte[0] := (SLAVE_WR | _addr_bits)
cmd_pkt.byte[1] := addr.byte[1]
cmd_pkt.byte[2] := addr.byte[0]
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 3)
i2c.wrblock_lsbf(ptr_buff, nr_bytes)
i2c.stop()
waitus(_t_wr) ' Wait "Write cycle time"
PUB wr_block_msbf(addr, ptr_buff, nr_bytes) | cmd_pkt
' Write a block of memory starting at addr, MSB-first
cmd_pkt.byte[0] := (SLAVE_WR | _addr_bits)
cmd_pkt.byte[1] := addr.byte[1]
cmd_pkt.byte[2] := addr.byte[0]
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 3)
i2c.wrblock_msbf(ptr_buff, nr_bytes)
i2c.stop()
waitus(_t_wr) ' Wait "Write cycle time"
DAT
{
Copyright 2024 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}