blob: 783e8e71337870b058ced4aae158351223c73d40 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/*
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);
}
void USI_TWI_S::send(uint8_t data){ // send it back to master
usiTwiTransmitByte(data);
}
uint8_t USI_TWI_S::available(){ // the bytes available that haven't been read yet
return usiTwiAmountDataInReceiveBuffer();
//return usiTwiDataInReceiveBuffer(); // This is wrong as far as the Wire API is concerned since it returns boolean and not amount
}
uint8_t USI_TWI_S::receive(){ // returns the bytes received one at a time
return usiTwiReceiveByte();
}
// sets function called on slave write
void USI_TWI_S::onReceive( void (*function)(uint8_t) )
{
usi_onReceiverPtr = function;
}
// sets function called on slave read
void USI_TWI_S::onRequest( void (*function)(void) )
{
usi_onRequestPtr = function;
}
// This routine is no longer used now that the usiTwiSlave is completely
// interrupt driven. The function is maintained here so that programs
// written for the pre-interrupt driven version will still compile and function.
void TinyWireS_stop_check()
{
// empty functions
}
// Implement a delay loop that checks for the stop bit (basically direct copy of the stock arduino implementation from wiring.c)
// I don't think this function is necessary now that the
// usiTwiSlave is completely interrupt driven. I'm not sure, and the function is
// behaviorally harmless because TinyWireS_stop_check() is empty, so I'm leaving it alone.
void tws_delay(unsigned long ms)
{
uint16_t start = (uint16_t)micros();
while (ms > 0)
{
TinyWireS_stop_check();
if (((uint16_t)micros() - start) >= 1000)
{
ms--;
start += 1000;
}
}
}
// Preinstantiate Objects //////////////////////////////////////////////////////
USI_TWI_S TinyWireS = USI_TWI_S();
|