From db9445ceba9830b0d36c3a0d20c6dc2a4d6b1083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Thu, 26 Feb 2015 13:49:58 +0100 Subject: [PATCH] add TWI library --- lib/twi.c | 239 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/twi.h | 42 ++++++++++ 2 files changed, 281 insertions(+) create mode 100644 lib/twi.c create mode 100644 lib/twi.h diff --git a/lib/twi.c b/lib/twi.c new file mode 100644 index 0000000..d0a540c --- /dev/null +++ b/lib/twi.c @@ -0,0 +1,239 @@ +/* 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<. + * + */ +/* This library handles the Two-Wire Interface (TWI)/Inter-Integrated Circuit (I²C) configuration */ + +/* TWI/I2C clock speed, in Hz */ +#define F_TWI 100000UL +/* + * Maximal number of iterations to wait for a device to respond for a selection + * Should be large enough to allow for a pending write to* complete, but low enough to properly abort an infinite loop in case a slave is broken or not present at all. + * With 100 kHz TWI clock, transfering the start condition and SLA+R/W packet takes about 10 µs. + * The longest write period is supposed to not exceed ~ 10 ms. + * Thus, normal operation should not require more than 100 iterations to get the device to respond to a selection. + */ +#define MAX_ITER 400 +/* card (slave) address (7 MSB) */ +#define SLA 0xA0 + +/* initialize 2-wire interface */ +void twi_init(void); +/* read 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); +/* write 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);