Subversion Repositories group.NITPanels

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13 pfowler 1
/*
2
 * twires.c
3
 *
4
 *  Created on: 16/07/2014
5
 *      Author: pfowler
6
 *
7
 *       USAGE is modeled after the TinyWireS . . . which is modeled on standard Wire library . . .
8
 *
9
 * Put in setup():
10
 *	TinyWireS.begin(I2C_SLAVE_ADDR);                 // initialize I2C lib & setup slave's address (7 bit - same as Wire)
11
 *
12
 * To Receive:
13
 *   someByte = TinyWireS.available(){                // returns the number of bytes in the received buffer
14
 *   someByte = TinyWireS.receive(){                  // returns the next byte in the received buffer
15
 *
16
 * To Send:
17
 *	TinyWireS.send(uint8_t data){                    // sends a requested byte to master
18
 *
19
 * TODO:	(by others!)
20
 *	- onReceive and onRequest handlers are not implemented.
21
 *	- merge this class with TinyWireM for master & slave support in one library
22
 */
23
 
24
#include <inttypes.h>
25
#include "usiTwiSlave.h"
26
#include <avr/interrupt.h>
27
#include "avrutil.h"
28
#include "twires.h"
29
 
30
 
31
void twires_begin(uint8_t slaveAddr) {
32
	usiTwiSlaveInit(slaveAddr);
33
}
34
 
35
void twires_send(uint8_t data) {
36
	usiTwiTransmitByte(data);
37
}
38
 
39
uint8_t twires_available() {
40
	return usiTwiAmountDataInReceiveBuffer();
41
}
42
 
43
uint8_t twires_receive() {
44
	return usiTwiReceiveByte();
45
}
46
 
47
void twires_onReceive( void (*function)(uint8_t) ) {
48
	usi_onReceiverPtr = function;
49
}
50
 
51
void twires_onRequest( void (*function)(void) ) {
52
	usi_onRequestPtr = function;
53
}
54
 
55
void twires_stop_check() {
56
	if (!usi_onReceiverPtr) {
57
		// No onreceive callback
58
		return;
59
	}
60
	if (!(USISR & ( 1 << USIPF ))) {
61
		//Stop not detected
62
		return;
63
	}
64
	uint8_t amount = usiTwiAmountDataInReceiveBuffer();
65
	if (amount==0) {
66
		return;
67
	}
68
	usi_onReceiverPtr(amount);
69
}
70
/*
71
 * Use this instead of the delay function. The twires_stop_check()
72
 * needs to be called lots of times to detect the stop condition
73
 */
74
void twires_delay(unsigned long ms) {
75
 
76
    uint16_t start = (uint16_t)millis();
77
    while (ms > 0)
78
    {
79
    	twires_stop_check();
80
        if (((uint16_t)millis() - start) >= 1)
81
        {
82
            ms--;
83
            start += 1;
84
        }
85
    }
86
 
87
}
88
 
89