{{ ───────────────────────────────────────────────── File: EEPROM.spin Version: 1.0 Copyright (c) 2018 Bueno Systems, Inc. See end of file for terms of use. Author: Philip C. Pilgrim ───────────────────────────────────────────────── Call start first to establish pin numbers and to initialize the EEPROM. There is no stop method, since no new cog gets started. This routine drives SCL high. As such it is not compatible with I2C busses that require clock-stretching. However, it also precludes the necessity of a pull-up resistor on SCL, which is the case with some Propeller boards. }} CON DEVICE_ADDR = $a0 DEVICE_READ = $01 MAX_ARRAY = 1024 'Arbitrary limit (8 pages) to prevent runaway array writes. VAR byte scl, sda PUB start(scl_pin, sda_pin) 'Set up pin numbers and initialize the EEPROM. scl := scl_pin sda := sda_pin _i2c_start _i2c_stop PUB write_long(addr, data) 'Write a long data value to EEPROM at address addr. write(addr, data, 4) PUB write_word(addr, data) 'Write a word data value to EEPROM at address addr. write(addr, data, 2) PUB write_byte(addr, data) 'Write a byte data value to EEPROM at address addr. write(addr, data, 1) PUB write(addr, data, nbytes) | i 'Write nbytes of a data value to EEPROM at address addr, starting with the LSB. write_array(addr, @data, nbytes #> 0 <# 4) PUB write_array(addr, data_addr, nbytes) 'Write an array to EEPROM, starting at address addr, reading nbytes bytes from array at data_addr. nbytes := nbytes #> 0 <# MAX_ARRAY repeat while nbytes _i2c_send_addr(DEVICE_ADDR, addr) repeat _i2c_send_byte(byte[data_addr++]) while (--nbytes and ++addr & 127) _i2c_stop PUB read_long(addr) 'Return a long value read from EEPROM at address addr. return read(addr, 4) PUB read_word(addr) 'Return a word value read from EEPROM at address addr. return read(addr, 2) PUB read_byte(addr) 'Return a byte value read from EEPROM at address addr. return read(addr, 1) PUB read(addr, nbytes) 'Return an nbytes-wide value read from EEPROM at address addr, starting with the LSB read_array(addr, @result, nbytes #> 0 <# 4) PUB read_array(addr, data_addr, nbytes) 'Read nbytes bytes from the EEPROM at address addr, writing them to memory beginning a data_addr. nbytes #>= 0 _i2c_send_addr(DEVICE_ADDR, addr) _i2c_stop _i2c_start _i2c_send_byte(DEVICE_ADDR | DEVICE_READ) repeat while nbytes-- byte[data_addr++] := _i2c_receive_byte(nbytes <> 0) _i2c_stop ''-------[ Base I2C Methods... ]------------------------------------------------ PRI _i2c_send_addr(dev_addr, addr) repeat _i2c_stop _i2c_start until _i2c_send_byte(dev_addr) _i2c_send_byte(addr.byte[1]) _i2c_send_byte(addr.byte[0]) PRI _i2c_send_byte(value) value := ((!value) >< 8) repeat 8 dira[sda] := value dira[scl] := false dira[scl] := true value >>= 1 dira[sda] := false dira[scl] := false result := not(ina[sda]) dira[scl] := true dira[sda] := true PRI _i2c_receive_byte(aknowledge) {{ Receive a byte from the sensor. }} dira[sda] := false repeat 8 result <<= 1 dira[scl] := false result |= ina[sda] dira[scl] := true dira[sda] := aknowledge dira[scl] := false dira[scl] := true dira[sda] := true PRI _i2c_start {{ Send an I2C start condition. }} outa[sda] := false outa[scl] := false dira[sda] := true dira[scl] := true PRI _i2c_stop {{ Send an I2C stop condition. }} dira[scl] := false dira[sda] := false {{ ┌──────────────────────────────────────────────────────────────────────────────────────┐ │ TERMS OF USE: MIT License │ ├──────────────────────────────────────────────────────────────────────────────────────┤ │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. │ └──────────────────────────────────────────────────────────────────────────────────────┘ }}