/* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ /* This library handles the Two-Wire Interface (TWI)/Inter-Integrated Circuit (I²C) configuration * based on demo by Ruwan Jayanetti from AVR libc examples * http://www.nongnu.org/avr-libc/user-manual/group__twi__demo.html */ #include // Standard Integer Types #include // General utilities #include // Boolean #include // AVR device-specific IO definitions #include // 2-wire TWSR values #include // definitions /* initialize 2-wire interface */ void twi_init(void) { TWSR &= ~((1< bytes inyo using 2-wire interface, starting at * returns if it succeeded * 16 bits address is not supported */ bool twi_read_bytes(uint8_t addr, uint8_t len, uint8_t *buf) { uint16_t retry = 0; // number of retries bool rc = false; // return code /* number of times to retry */ restart: if (retry++ >= MAX_ITER) { return false; } /* first cycle: master transmitter mode */ start: /* send start */ TWCR = (1< 0) { if (len > 1) { TWCR = (1< bytes from on 2-wire interface starting at * returns if it successed * 16 bits address is not supported * ensure yourself the size of pages to be written */ bool twi_write_bytes(uint8_t addr, uint8_t len, uint8_t *buf) { uint16_t retry = 0; // number of retries bool rc = false; // return code /* number of times to retry */ restart: if (retry++ >= MAX_ITER) { return false; } /* master transmit mode */ start: /* start */ TWCR = (1< 0; len--) { TWDR = *buf++; TWCR = (1<