Stairs/module/src/lib/VeryTinyI2C.h

54 lines
1.4 KiB
C

/*
* Stairs lighting
* Copyright 2017 (c) Mark van Renswoude
*
* https://git.x2software.net/pub/Stairs
*
* Straight up port of David Johnson-Davies' TinyI2C to C
* and without the Arduino framework dependency.
* https://github.com/technoblogy/tiny-i2c
*/
#ifndef __VeryTinyI2C
#define __VeryTinyI2C
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <util/delay.h>
// Defines
//#define TWI_FAST_MODE
#ifdef TWI_FAST_MODE // TWI FAST mode timing limits. SCL = 100-400kHz
#define DELAY_T2TWI (_delay_us(2)) // >1.3us
#define DELAY_T4TWI (_delay_us(1)) // >0.6us
#else // TWI STANDARD mode timing limits. SCL <= 100kHz
#define DELAY_T2TWI (_delay_us(5)) // >4.7us
#define DELAY_T4TWI (_delay_us(4)) // >4.0us
#endif
#define TWI_NACK_BIT 0 // Bit position for (N)ACK bit.
// These are valid for the ATTiny2313
#define DDR_USI DDRB
#define DDR_USI_CL DDR_USI
#define PORT_USI PORTB
#define PORT_USI_CL PORT_USI
#define PIN_USI PINB
#define PIN_USI_CL PIN_USI
#define PORT_USI_SDA PB5
#define PORT_USI_SCL PB7
#define PIN_USI_SDA PINB5
#define PIN_USI_SCL PINB7
void i2c_init(void);
uint8_t i2c_read(void);
uint8_t i2c_readLast(void);
bool i2c_write(uint8_t data);
bool i2c_start(uint8_t address, int readcount);
bool i2c_restart(uint8_t address, int readcount);
void i2c_stop(void);
#endif