Commit 231603ef authored by Dave Griffiths's avatar Dave Griffiths

added attiny85 code

parent 0ef23091
#include <avr/io.h>
#include <util/delay.h>
#include "usi_i2c_slave.h"
typedef unsigned char u8;
typedef unsigned int u32;
#define LED PB1
void set_led_state(u8 s)
{
if (s) PORTB|=_BV(LED);
else PORTB&=~_BV(LED);
}
extern char usi_i2c_slave_address;
extern char* USI_Slave_register_buffer[];
int main(void)
{
DDRB = 0x00;
DDRB |= _BV( LED );
set_led_state(0);
// activate pull up resistors
//PORTB|=_BV(LEFT_EYE);
//sei();
USI_I2C_Init(0x55);
char t=0;
USI_Slave_register_buffer[0] = &t;
USI_Slave_register_buffer[1] = &t;
USI_Slave_register_buffer[2] = &t;
USI_Slave_register_buffer[3] = &t;
USI_Slave_register_buffer[4] = &t;
USI_Slave_register_buffer[5] = &t;
USI_Slave_register_buffer[6] = &t;
USI_Slave_register_buffer[7] = &t;
//TinyWireM.requestFrom(0x01,1); // Request 1 byte from slave
//u8 t = TinyWireM.receive(); // get the temperature
u32 led_counter=0;
while(1) {
//set_led_state((led_counter++)%10000>5000);
}
}
//#define F_CPU 1200000UL
#define I2C_SLAVE_ADDRESS 0x45 // the 7-bit address
#include <avr/io.h>
#include <util/delay.h>
#include "tinywires/TinyWireS.h" // I2C Master lib for ATTinys which use USI
typedef unsigned char u8;
typedef unsigned int u32;
#define LED PB1
void set_led_state(u8 s)
{
if (s) PORTB|=_BV(LED);
else PORTB&=~_BV(LED);
}
//////////////////////////////////////////////////////////////
#ifndef TWI_RX_BUFFER_SIZE
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif
volatile uint8_t i2c_regs[] =
{
0xDE,
0xAD,
0xBE,
0xEF,
};
// Tracks the current register pointer position
volatile u8 reg_position;
const u8 reg_size = sizeof(i2c_regs);
/**
* This is called for each read request we receive, never put more than one byte of data (with TinyWireS.send) to the
* send-buffer when using this callback
*/
void requestEvent()
{
TinyWireS.send(i2c_regs[reg_position]);
// Increment the reg position on each read, and loop back to zero
reg_position++;
if (reg_position >= reg_size)
{
reg_position = 0;
}
}
/**
* The I2C data received -handler
*
* This needs to complete before the next incoming transaction (start, data, restart/stop) on the bus does
* so be quick, set flags for long running tasks to be called from the mainloop instead of running them directly,
*/
void receiveEvent(uint8_t howMany)
{
if (howMany < 1)
{
// Sanity-check
return;
}
if (howMany > TWI_RX_BUFFER_SIZE)
{
// Also insane number
return;
}
reg_position = TinyWireS.receive();
howMany--;
if (!howMany)
{
// This write was only to set the buffer for next read
return;
}
while(howMany--)
{
i2c_regs[reg_position] = TinyWireS.receive();
reg_position++;
if (reg_position >= reg_size)
{
reg_position = 0;
}
}
}
int main(void)
{
sei();
DDRB = 0x00;
DDRB |= _BV( LED );
DDRB |= _BV( PB3 );
// activate pull up resistors
//PORTB|=_BV(LEFT_EYE);
TinyWireS.begin(I2C_SLAVE_ADDRESS);
TinyWireS.onReceive(receiveEvent);
TinyWireS.onRequest(requestEvent);
//TinyWireM.begin(); // initialize I2C lib
//TinyWireM.requestFrom(0x01,1); // Request 1 byte from slave
//u8 t = TinyWireM.receive(); // get the temperature
u32 led_counter = 0;
while(1) {
TinyWireS_stop_check();
set_led_state((led_counter++)%10000>5000);
}
}
#include <avr/io.h>
#include <util/delay.h>
typedef unsigned char u8;
typedef unsigned int u32;
#define LED PB0
void set_led_state(u8 s)
{
if (s) PORTB|=_BV(LED);
else PORTB&=~_BV(LED);
}
int main(void)
{
DDRB = 0x00;
DDRB |= _BV( LED );
// activate pull up resistors
//PORTB|=_BV(LEFT_EYE);
u32 ledtime=0;
while(1) {
set_led_state((ledtime++)%2500<1200);
}
}
avr-gcc -DAVR -Wall -Os -mmcu=attiny85 -o i2c.elf i2c.c usi_i2c_slave.c
avr-objcopy --output-target=ihex i2c.elf i2c.ihex
avrdude -c usbtiny -p t85 -e -U flash:w:i2c.ihex -U lfuse:w:0xE2:m
avr-gcc -DAVR -Wall -Os -mmcu=attiny85 -o i2c.elf i2c_old.cpp tinywires/TinyWireS.cpp tinywires/usiTwiSlave.c
avr-objcopy --output-target=ihex i2c.elf i2c.ihex
avrdude -c usbtiny -p t85 -e -U flash:w:i2c.ihex -U lfuse:w:0xE2:m
This diff is collapsed.
/*-----------------------------------------------------*\
| USI I2C Slave Driver |
| |
| This library provides a robust, interrupt-driven I2C |
| slave implementation built on the ATTiny Universal |
| Serial Interface (USI) hardware. Slave operation is |
| implemented as a register bank, where each 'register' |
| is a pointer to an 8-bit variable in the main code. |
| This was chosen to make I2C integration transparent |
| to the mainline code and making I2C reads simple. |
| This library also works well with the Linux I2C-Tools |
| utilities i2cdetect, i2cget, i2cset, and i2cdump. |
| |
| Adam Honse (GitHub: CalcProgrammer1) - 7/29/2012 |
| -calcprogrammer1@gmail.com |
\*-----------------------------------------------------*/
#ifndef USI_I2C_SLAVE_H
#define USI_I2C_SLAVE_H
#include <avr/io.h>
#include <avr/interrupt.h>
//Microcontroller Dependent Definitions
#if defined (__AVR_ATtiny24__) | \
defined (__AVR_ATtiny44__) | \
defined (__AVR_ATtiny84__)
#define DDR_USI DDRA
#define PORT_USI PORTA
#define PIN_USI PINA
#define PORT_USI_SDA PA6
#define PORT_USI_SCL PA4
#define PIN_USI_SDA PINA6
#define PIN_USI_SCL PINA4
#endif
#if defined( __AVR_ATtiny25__ ) | \
defined( __AVR_ATtiny45__ ) | \
defined( __AVR_ATtiny85__ )
# define DDR_USI DDRB
# define PORT_USI PORTB
# define PIN_USI PINB
# define PORT_USI_SDA PB0
# define PORT_USI_SCL PB2
# define PIN_USI_SDA PINB0
# define PIN_USI_SCL PINB2
#endif
#if defined(__AVR_AT90Tiny2313__) | \
defined(__AVR_ATtiny2313__)
#define DDR_USI DDRB
#define PORT_USI PORTB
#define PIN_USI PINB
#define PORT_USI_SDA PB5
#define PORT_USI_SCL PB7
#define PIN_USI_SDA PINB5
#define PIN_USI_SCL PINB7
#endif
//USI I2C Initialize
// address - If slave, this parameter is the slave address
void USI_I2C_Init(char address);
#endif
/*
TinyWireM.cpp - a wrapper class for TWI/I2C Master library for the ATtiny on Arduino
1/21/2011 BroHogan - brohoganx10 at gmail dot com
**** See TinyWireM.h for Credits and Usage information ****
This library 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 2.1 of the License, or 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.
*/
extern "C" {
//#include "USI_TWI_Master.h"
//#include <USI_TWI_Master.h>
//#include <USI_TWI_Master\USI_TWI_Master.h>
//#include <USI_TWI_Master/USI_TWI_Master.h>
}
#include "USI_TWI_Master.h"
#include "TinyWireM.h"
// Initialize Class Variables //////////////////////////////////////////////////
uint8_t USI_TWI::USI_Buf[USI_BUF_SIZE]; // holds I2C send and receive data
uint8_t USI_TWI::USI_BufIdx = 0; // current number of bytes in the send buff
uint8_t USI_TWI::USI_LastRead = 0; // number of bytes read so far
uint8_t USI_TWI::USI_BytesAvail = 0; // number of bytes requested but not read
// Constructors ////////////////////////////////////////////////////////////////
USI_TWI::USI_TWI(){
}
// Public Methods //////////////////////////////////////////////////////////////
//int USI_TWI::peek(){}
//void USI_TWI::flush(){}
void USI_TWI::begin(){ // initialize I2C lib
USI_TWI_Master_Initialise();
}
void USI_TWI::beginTransmission(uint8_t slaveAddr){ // setup address & write bit
USI_BufIdx = 0;
USI_Buf[USI_BufIdx] = (slaveAddr<<TWI_ADR_BITS) | USI_SEND;
}
size_t USI_TWI::write(uint8_t data){ // buffers up data to send
if (USI_BufIdx >= USI_BUF_SIZE) return 0; // dont blow out the buffer
USI_BufIdx++; // inc for next byte in buffer
USI_Buf[USI_BufIdx] = data;
return 1;
}
uint8_t USI_TWI::endTransmission() {
endTransmission(1);
}
uint8_t USI_TWI::endTransmission(uint8_t stop){ // actually sends the buffer
bool xferOK = false;
uint8_t errorCode = 0;
xferOK = USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work
USI_BufIdx = 0;
if (xferOK) {
if (stop) {
errorCode = USI_TWI_Master_Stop();
if (errorCode == 0) {
errorCode = USI_TWI_Get_State_Info();
return errorCode;
}
}
return 0;
}
else { // there was an error
errorCode = USI_TWI_Get_State_Info(); // this function returns the error number
return errorCode;
}
}
uint8_t USI_TWI::requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // setup for receiving from slave
bool xferOK = false;
uint8_t errorCode = 0;
USI_LastRead = 0;
USI_BytesAvail = numBytes; // save this off in a global
numBytes++; // add extra byte to transmit header
USI_Buf[0] = (slaveAddr<<TWI_ADR_BITS) | USI_RCVE; // setup address & Rcve bit
xferOK = USI_TWI_Start_Read_Write(USI_Buf,numBytes); // core func that does the work
// USI_Buf now holds the data read
if (xferOK) {
errorCode = USI_TWI_Master_Stop();
if (errorCode == 0) {
errorCode = USI_TWI_Get_State_Info();
return errorCode;
}
return 0;
}
else { // there was an error
errorCode = USI_TWI_Get_State_Info(); // this function returns the error number
return errorCode;
}
}
int USI_TWI::read(){ // returns the bytes received one at a time
USI_LastRead++; // inc first since first uint8_t read is in USI_Buf[1]
return USI_Buf[USI_LastRead];
}
int USI_TWI::available(){ // the bytes available that haven't been read yet
return USI_BytesAvail - (USI_LastRead);
}
// Preinstantiate Objects //////////////////////////////////////////////////////
USI_TWI TinyWireM = USI_TWI();
/*
TinyWireM.h - a wrapper(+) class for TWI/I2C Master library for the ATtiny on Arduino
1/21/2011 BroHogan - brohoganx10 at gmail dot com
Thanks to 'jkl' for the gcc version of Atmel's USI_TWI_Master code
http://www.cs.cmu.edu/~dst/ARTSI/Create/PC%20Comm/
I added Atmel's original Device dependant defines section back into USI_TWI_Master.h
NOTE! - It's very important to use pullups on the SDA & SCL lines! More so than with the Wire lib.
USAGE is modeled after the standard Wire library . . .
Put in setup():
TinyWireM.begin(){ // initialize I2C lib
To Send:
TinyWireM.beginTransmission(uint8_t slaveAddr){ // setup slave's address (7 bit address - same as Wire)
TinyWireM.send(uint8_t data){ // buffer up bytes to send - can be called multiple times
someByte = TinyWireM.endTransmission(){ // actually send the bytes in the buffer
// returns (optional) 0 = sucess or see USI_TWI_Master.h for error codes
To Receive:
someByte = TinyWireM.requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // reads 'numBytes' from slave's address
// (usage optional) returns 0= success or see USI_TWI_Master.h for error codes
someByte = TinyWireM.receive(){ // returns the next byte in the received buffer - called multiple times
someByte = TinyWireM.available(){ // returns the number of unread bytes in the received buffer
TODO: (by others!)
- merge this class with TinyWireS for master & slave support in one library
This library 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 2.1 of the License, or 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.
*/
#ifndef TinyWireM_h
#define TinyWireM_h
#include <inttypes.h>
//#include "Arduino.h"
typedef unsigned int size_t;
#define USI_SEND 0 // indicates sending to TWI
#define USI_RCVE 1 // indicates receiving from TWI
#define USI_BUF_SIZE 18 // bytes in message buffer
//class USI_TWI : public Stream
class USI_TWI
{
private:
static uint8_t USI_Buf[]; // holds I2C send and receive data
static uint8_t USI_BufIdx; // current number of bytes in the send buff
static uint8_t USI_LastRead; // number of bytes read so far
static uint8_t USI_BytesAvail; // number of bytes requested but not read
public:
USI_TWI();
void begin();
void beginTransmission(uint8_t);
size_t write(uint8_t);
inline size_t write(uint8_t* d, uint8_t n) { uint16_t i; for (i = 0; i < n; i++) write(d[i]); return (size_t)n; }
inline size_t write(unsigned long n) { return write((uint8_t)n); }
inline size_t write(long n) { return write((uint8_t)n); }
inline size_t write(unsigned int n) { return write((uint8_t)n); }
inline size_t write(int n) { return write((uint8_t)n); }
void send(uint8_t b) { write(b); }
void send(uint8_t *d, uint8_t n) { write(d, n); }
void send(int n) { write((uint8_t)n); }
uint8_t endTransmission();
uint8_t endTransmission(uint8_t);
uint8_t requestFrom(uint8_t, uint8_t);
int read();
int available();
int peek(void);
void flush(void);
uint8_t receive(void) {
int c = read();
if (c < 0) return 0;
return c;
}
};
extern USI_TWI TinyWireM;
#endif
This diff is collapsed.
/*****************************************************************************
*
*
* File USI_TWI_Master.h compiled with gcc
* Date Friday, 10/31/08 Boo!
* Updated by jkl
*
*
* Supported devices : All device with USI module can be used.
* The example is written for the ATtiny2313
*
* AppNote : AVR310 - Using the USI module as a TWI Master
*
* This is modified to just do I2C communication on ATtiny2313 running at
* 1MHz. Fast mode is probably possible, but would need a faster clock
* and has not been tested.
*
* 12/15/08 Added declaration of USI_TWI_Start_Memory_Read -jkl
****************************************************************************/
//********** Defines **********//
// Defines controlling timing limits - SCL <= 100KHz.
// For use with _delay_us()
#define T2_TWI 5 // >4,7us
#define T4_TWI 4 // >4,0us
// Defines error code generating
//#define PARAM_VERIFICATION
//#define NOISE_TESTING
#define SIGNAL_VERIFY // This should probably be on always.
/****************************************************************************
Bit and byte definitions
****************************************************************************/
#define TWI_READ_BIT 0 // Bit position for R/W bit in "address byte".
#define TWI_ADR_BITS 1 // Bit position for LSB of the slave address bits in the init byte.
#define TWI_NACK_BIT 0 // Bit position for (N)ACK bit.
// Note these have been renumbered from the Atmel Apps Note. Most likely errors are now
// lowest numbers so they're easily recognized as LED flashes.
#define USI_TWI_NO_DATA 0x08 // Transmission buffer is empty
#define USI_TWI_DATA_OUT_OF_BOUND 0x09 // Transmission buffer is outside SRAM space
#define USI_TWI_UE_START_CON 0x07 // Unexpected Start Condition
#define USI_TWI_UE_STOP_CON 0x06 // Unexpected Stop Condition
#define USI_TWI_UE_DATA_COL 0x05 // Unexpected Data Collision (arbitration)
#define USI_TWI_NO_ACK_ON_DATA 0x02 // The slave did not acknowledge all data
#define USI_TWI_NO_ACK_ON_ADDRESS 0x01 // The slave did not acknowledge the address
#define USI_TWI_MISSING_START_CON 0x03 // Generated Start Condition not detected on bus
#define USI_TWI_MISSING_STOP_CON 0x04 // Generated Stop Condition not detected on bus
#define USI_TWI_BAD_MEM_READ 0x0A // Error during external memory read
// Device dependant defines ADDED BACK IN FROM ORIGINAL ATMEL .H
#if defined(__AVR_AT90Mega169__) | defined(__AVR_ATmega169__) | \
defined(__AVR_AT90Mega165__) | defined(__AVR_ATmega165__) | \
defined(__AVR_ATmega325__) | defined(__AVR_ATmega3250__) | \
defined(__AVR_ATmega645__) | defined(__AVR_ATmega6450__) | \
defined(__AVR_ATmega329__) | defined(__AVR_ATmega3290__) | \
defined(__AVR_ATmega649__) | defined(__AVR_ATmega6490__)
#define DDR_USI DDRE
#define PORT_USI PORTE
#define PIN_USI PINE
#define PORT_USI_SDA PORTE5
#define PORT_USI_SCL PORTE4
#define PIN_USI_SDA PINE5
#define PIN_USI_SCL PINE4
#endif
#if defined(__AVR_ATtiny25__) | defined(__AVR_ATtiny45__) | defined(__AVR_ATtiny85__) | \
defined(__AVR_AT90Tiny26__) | defined(__AVR_ATtiny26__)
#define DDR_USI DDRB
#define PORT_USI PORTB
#define PIN_USI PINB
#define PORT_USI_SDA PORTB0
#define PORT_USI_SCL PORTB2
#define PIN_USI_SDA PINB0
#define PIN_USI_SCL PINB2
#endif
#if defined(__AVR_ATtiny84__) | defined(__AVR_ATtiny44__)
# define DDR_USI DDRA
# define PORT_USI PORTA
# define PIN_USI PINA
# define PORT_USI_SDA PORTA6
# define PORT_USI_SCL PORTA4
# define PIN_USI_SDA PINA6
# define PIN_USI_SCL PINA4
#endif
#if defined(__AVR_AT90Tiny2313__) | defined(__AVR_ATtiny2313__)
#define DDR_USI DDRB
#define PORT_USI PORTB
#define PIN_USI PINB
#define PORT_USI_SDA PORTB5
#define PORT_USI_SCL PORTB7
#define PIN_USI_SDA PINB5
#define PIN_USI_SCL PINB7
#endif
/* From the original .h
// Device dependant defines - These for ATtiny2313. // CHANGED FOR ATtiny85
#define DDR_USI DDRB
#define PORT_USI PORTB
#define PIN_USI PINB
#define PORT_USI_SDA PORTB0 // was PORTB5 - N/U
#define PORT_USI_SCL PORTB2 // was PORTB7 - N/U
#define PIN_USI_SDA PINB0 // was PINB5
#define PIN_USI_SCL PINB2 // was PINB7
*/
// General defines
#define TRUE 1
#define FALSE 0
//********** Prototypes **********//
void USI_TWI_Master_Initialise( void );
unsigned char USI_TWI_Start_Random_Read( unsigned char * , unsigned char );
unsigned char USI_TWI_Start_Read_Write( unsigned char * , unsigned char );
unsigned char USI_TWI_Master_Stop( void );
unsigned char USI_TWI_Get_State_Info( void );
/*
TinyWireS.cpp - a wrapper class for Don Blake's usiTwiSlave routines.
Provides TWI/I2C Slave functionality on ATtiny processers in Arduino environment.
1/23/2011 BroHogan - brohoganx10 at gmail dot com
**** See TinyWireS.h for Credits and Usage information ****
This library 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 2.1 of the License, or 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.
*/
extern "C" {
#include <inttypes.h>
#include "usiTwiSlave.h"
#include <avr/interrupt.h>
}
#include "TinyWireS.h"
//#include "Arduino.h"
// Constructors ////////////////////////////////////////////////////////////////
USI_TWI_S::USI_TWI_S(){
}
// Public Methods //////////////////////////////////////////////////////////////
void USI_TWI_S::begin(uint8_t slaveAddr){ // initialize I2C lib
usiTwiSlaveInit(slaveAddr);
}