{ +------------------------------------------------+ |Filename: i2c_phipi.spin | |Author: Phil Pilgrim | |Description: Master driver for I2C slave devices| |Copyright (c) 2021 | |See end of file for terms of use. | +------------------------------------------------+ This driver assumes that SCL, along with SDA, has a pull-up to Vdd. It accommodates clock-stretching from the slave device. } CON ACK = 1 NAK = 0 VAR byte scl, sda long scl_mask PUB init(sclpin, sdapin) {{ Assign pin values to SCL and SCA. }} scl := sclpin sda := sdapin scl_mask := 1 << scl PUB send_addr(dev_addr) {{ Send the device address. }} repeat stop start until send_byte(dev_addr) PUB send_bytes(addr, count) {{ Send count bytes from address addr to the device. }} repeat count send_byte(byte[addr++]) PUB send_byte(value) {{ Send a single byte to the device }} value := ((!value) >< 8) repeat 8 dira[sda] := value dira[scl] := false dira[scl] := true value >>= 1 dira[sda] := false dira[scl] := false waitpeq(scl_mask, scl_mask, 0) result := not(ina[sda]) dira[scl] := true dira[sda] := true PUB receive_bytes(addr, count, acknak) {{ Receive multiple bytes from the device. Terminate with ACK if acknak == 1; NAK, otherwise. }} repeat count - 1 byte[addr++] := receive_byte(ACK) byte[addr] := receive_byte(acknak) PUB receive_byte(acknak) {{ Receive a byte from the device. }} dira[sda] := false repeat 8 result <<= 1 dira[scl] := false waitpeq(scl_mask, scl_mask, 0) result |= ina[sda] dira[scl] := true dira[sda] := acknak dira[scl] := false waitpeq(scl_mask, scl_mask, 0) dira[scl] := true dira[sda] := true PUB start {{ Send an I2C start condition. }} outa[sda] := false outa[scl] := false dira[sda] := true dira[scl] := true PUB stop {{ Send an I2C stop condition. }} dira[scl] := false waitpeq(scl_mask, scl_mask, 0) 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. -------------------------------------------------------------------------------------------------------- }