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