Commit b7516914 authored by Dave Griffiths's avatar Dave Griffiths

Merge branch 'master' of github.com:nebogeo/weavingcode

parents de6a186a 9b4c942c
#include <avr/io.h>
#include <util/twi.h>
#include <avr/interrupt.h>
#include "I2C_slave.h"
void I2C_init(uint8_t address){
// load address into TWI address register
TWAR = address;
// set the TWCR to enable address matching and enable TWI, clear TWINT, enable TWI interrupt
TWCR = (1<<TWIE) | (1<<TWEA) | (1<<TWINT) | (1<<TWEN);
}
void I2C_stop(void){
// clear acknowledge and enable bits
TWCR &= ~( (1<<TWEA) | (1<<TWEN) );
}
ISR(TWI_vect){
// temporary stores the received data
uint8_t data;
// own address has been acknowledged
if( (TWSR & 0xF8) == TW_SR_SLA_ACK ){
buffer_address = 0xFF;
// clear TWI interrupt flag, prepare to receive next byte and acknowledge
TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
}
else if( (TWSR & 0xF8) == TW_SR_DATA_ACK ){ // data has been received in slave receiver mode
// save the received byte inside data
data = TWDR;
// check wether an address has already been transmitted or not
if(buffer_address == 0xFF){
buffer_address = data;
// clear TWI interrupt flag, prepare to receive next byte and acknowledge
TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
}
else{ // if a databyte has already been received
// store the data at the current address
i2cbuffer[buffer_address] = data;
// increment the buffer address
buffer_address++;
// if there is still enough space inside the buffer
if(buffer_address < 0xFF){
// clear TWI interrupt flag, prepare to receive next byte and acknowledge
TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
}
else{
// clear TWI interrupt flag, prepare to receive last byte and don't acknowledge
TWCR |= (1<<TWIE) | (1<<TWINT) | (0<<TWEA) | (1<<TWEN);
}
}
}
else if( (TWSR & 0xF8) == TW_ST_DATA_ACK)
{ // device has been addressed to be a transmitter
// copy data from TWDR to the temporary memory
data = TWDR;
// if no buffer read address has been sent yet
if( buffer_address == 0xFF ){
buffer_address = data;
}
PORTD=buffer_address;
// copy the specified buffer address into the TWDR register for transmission
TWDR = i2cbuffer[buffer_address];
// increment buffer read address
buffer_address++;
// if there is another buffer address that can be sent
if(buffer_address < 0xFF){
// clear TWI interrupt flag, prepare to send next byte and receive acknowledge
TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
}
else{
// clear TWI interrupt flag, prepare to send last byte and receive not acknowledge
TWCR |= (1<<TWIE) | (1<<TWINT) | (0<<TWEA) | (1<<TWEN);
}
}
else if((TWSR & 0xF8) == TW_ST_DATA_NACK)
{ // device has been addressed to be a transmitter
// copy data from TWDR to the temporary memory
data = TWDR;
// if no buffer read address has been sent yet
if( buffer_address == 0xFF ){
buffer_address = data;
}
//PORTD=buffer_address;
// copy the specified buffer address into the TWDR register for transmission
TWDR = i2cbuffer[buffer_address];
// increment buffer read address
buffer_address++;
TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN) |
(0<<TWSTA) | (0<<TWSTO) | (0<<TWWC);
}
else{
// if none of the above apply prepare TWI to be addressed again
//PORTD=(TWSR & 0xF8);
TWCR |= (1<<TWIE) | (1<<TWEA) | (1<<TWEN);
}
}
#ifndef I2C_SLAVE_H
#define I2C_SLAVE_H
volatile uint8_t buffer_address;
volatile uint8_t i2cbuffer[0xFF];
void I2C_init(uint8_t address);
void I2C_stop(void);
ISR(TWI_vect);
#endif // I2C_SLAVE_H
This diff is collapsed.
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#include "I2C_slave.h"
// buffer used to convert integer to string
char buffer[3];
#define LED PB0
void set_led_state(unsigned char s)
{
if (s) PORTB|=_BV(LED);
else PORTB&=~_BV(LED);
}
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
int main(void){
I2C_init(0x32<<1); // initalize as slave with address 0x32
// allow interrupts
sei();
DDRD |= 0xff;
PORTD = 0xff;
for (int i=0; i<0xFF; i++) {
i2cbuffer[i]=0;
}
int led_counter = 0;
while(1)
{
// if (rxbuffer[0]!=0) {
// DDRB |= _BV( LED );
// set_led_state(1);
// }
_delay_ms(500);
// set_led_state(0);
// PORTD = 0x00;
_delay_ms(500);
// PORTD = 0xff;
// set_led_state(1);
}
return 0;
}
#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);
}
}
//#define F_CPU 1200000UL
#include <avr/io.h>
#include <util/delay.h>
#include "tinywirem/TinyWireM.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);
}
int main(void)
{
sei();
DDRB = 0x00;
DDRB |= _BV( LED );
TinyWireM.begin();
u32 led_counter = 0;
while(1) {
TinyWireM.requestFrom(0x32,1); // Request 1 byte from slave
u8 t = TinyWireM.receive(); // get the temperature
set_led_state((led_counter++)%2);
_delay_ms(1000);
}
}
#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_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
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
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