Mozzi  version 2016-12-11-17:03
sound synthesis library for Arduino
twi_nonblock.h
1 /*
2  * twi_nonblock.h
3  *
4  * Copyright 2012 Marije Baalman.
5  *
6  */
7 
8 #ifndef TWI_NONBLOCK_H_
9 #define TWI_NONBLOCK_H_
10 
11 // Added by TB2014 for Teensy 3 port
12 #if !(defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO)) // hide all code from Teensy 3.1
13 
14 
15 #include "Arduino.h"
16 
17 #include <compat/twi.h>
18 
19 #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
20 #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
21 
22 // --- twi reading variables
23  #ifndef TWI_FREQ
24  #define TWI_FREQ 100000L
25  #endif
26 
27  #ifndef TWI_BUFFER_LENGTH
28  #define TWI_BUFFER_LENGTH 32
29  #endif
30 
31  #define TWI_READY 0
32  #define TWI_PRE_MRX 1
33  #define TWI_MRX 2
34  #define TWI_PRE_MTX 3
35  #define TWI_MTX 4
36  #define TWI_SRX 5
37  #define TWI_STX 6
38 
39 static volatile uint8_t twi_state;
40 static volatile uint8_t twi_oldstate;
41 // static uint8_t twiint_masrw;
42 static uint8_t twi_slarw;
43 
44 static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
45 static volatile uint8_t twi_masterBufferIndex;
46 static uint8_t twi_masterBufferLength;
47 
48 static volatile uint8_t twi_error;
49 
50 #define BUFFER_LENGTH 32
51 static uint8_t rxBuffer[BUFFER_LENGTH];
52 static uint8_t rxBufferIndex = 0;
53 static uint8_t rxBufferLength = 0;
54 
55 static uint8_t txAddress = 0;
56 static uint8_t txBuffer[BUFFER_LENGTH];
57 static uint8_t txBufferIndex = 0;
58 static uint8_t txBufferLength = 0;
59 
60 static uint8_t transmitting;
61 
62 
63 void initialize_twi_nonblock();
64 
65 uint8_t twowire_requestFrom(uint8_t address, uint8_t quantity);
66 void twowire_beginTransmission( uint8_t address );
67 void twowire_send( uint8_t data );
68 uint8_t twowire_endTransmission(void);
69 
71 uint8_t twi_initiateReadFrom(uint8_t address, uint8_t length);
72 void twi_continueReadFrom();
73 
74 uint8_t twi_readMasterBuffer( uint8_t* data, uint8_t length );
75 
76 uint8_t twi_initiateWriteTo(uint8_t address, uint8_t* data, uint8_t length );
77 void twi_continueWriteTo();
78 
79 
80 void twi_reply(uint8_t ack);
81 void twi_stop(void);
82 void twi_releaseBus(void);
83 
85 uint8_t twi_readFromBlocking(uint8_t address, uint8_t* data, uint8_t length);
86 uint8_t twi_writeToBlocking(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait);
87 
88 #endif
89 #endif