Mozzi  version 2016-12-11-17:03
sound synthesis library for Arduino
twi_nonblock.cpp
1 /*
2  * twi_nonblock.cpp
3  *
4  * Copyright 2012 Marije Baalman.
5  *
6  */
7 
8 
9 // Added by TB2014 for Mozzi library, to hide code from Teensy 3.1
10 #if !(defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO))
11 
12 #include "twi_nonblock.h"
13 
14 #include <avr/interrupt.h>
15 
16 uint8_t twi_writeAddress;
17 uint8_t * twi_writeData;
18 uint8_t twi_writeLength;
19 
20 uint8_t twi_readAddress;
21 // uint8_t * twi_writeData;
22 uint8_t twi_readLength;
23 
24 /*
25  * Function twi_init
26  * Desc readys twi pins and sets twi bitrate
27  * Input none
28  * Output none
29  */
30 void initialize_twi_nonblock(){
31  rxBufferIndex = 0;
32  rxBufferLength = 0;
33 
34  txBufferIndex = 0;
35  txBufferLength = 0;
36 
37  // initialize state
38  twi_state = TWI_READY;
39 
40  #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
41  // activate internal pull-ups for twi
42  // as per note from atmega8 manual pg167
43  sbi(PORTC, 4);
44  sbi(PORTC, 5);
45  #else
46  // activate internal pull-ups for twi
47  // as per note from atmega128 manual pg204
48  sbi(PORTD, 0);
49  sbi(PORTD, 1);
50  #endif
51 
52  // initialize twi prescaler and bit rate
53  cbi(TWSR, TWPS0);
54  cbi(TWSR, TWPS1);
55  TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
56 
57  /* twi bit rate formula from atmega128 manual pg 204
58  SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
59  note: TWBR should be 10 or higher for master mode
60  It is 72 for a 16mhz Wiring board with 100kHz TWI */
61 
62  // enable twi module, acks, and twi interrupt
63  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
64 }
65 
66 
67 uint8_t twowire_requestFromBlocking(uint8_t address, uint8_t quantity)
68 {
69  // clamp to buffer length
70  if(quantity > BUFFER_LENGTH){
71  quantity = BUFFER_LENGTH;
72  }
73  // perform blocking read into buffer
74  uint8_t read = twi_readFromBlocking(address, rxBuffer, quantity);
75  // set rx buffer iterator vars
76  rxBufferIndex = 0;
77  rxBufferLength = read;
78 
79  return read;
80 }
81 
82 void twowire_beginTransmission( uint8_t address ){
83  // indicate that we are transmitting
84  transmitting = 1;
85  // set address of targeted slave
86  txAddress = address;
87  // reset tx buffer iterator vars
88  txBufferIndex = 0;
89  txBufferLength = 0;
90 }
91 
92 void twowire_send( uint8_t data ){
93  if(transmitting){
94  // in master transmitter mode
95  // don't bother if buffer is full
96  if(txBufferLength >= BUFFER_LENGTH){
97  return;
98  }
99  // put byte in tx buffer
100  txBuffer[txBufferIndex] = data;
101  ++txBufferIndex;
102  // update amount in buffer
103  txBufferLength = txBufferIndex;
104  }
105 }
106 
107 uint8_t twowire_endTransmission(void)
108 {
109  // transmit buffer (blocking)
110  int8_t ret = twi_writeToBlocking(txAddress, txBuffer, txBufferLength, 1);
111  // reset tx buffer iterator vars
112  txBufferIndex = 0;
113  txBufferLength = 0;
114  // indicate that we are done transmitting
115  transmitting = 0;
116  return ret;
117 }
118 
119 /*
120  * Function twi_readFrom
121  * Desc attempts to become twi bus master and read a
122  * series of bytes from a device on the bus
123  * Input address: 7bit i2c device address
124  * data: pointer to byte array
125  * length: number of bytes to read into array
126  * Output number of bytes read
127  */
129 uint8_t twi_readFromBlocking(uint8_t address, uint8_t* data, uint8_t length)
130 {
131  uint8_t i;
132 
133  // ensure data will fit into buffer
134  if(TWI_BUFFER_LENGTH < length){
135  return 0;
136  }
137 
138  // wait until twi is ready, become master receiver
139  while(TWI_READY != twi_state){
140  continue;
141  }
142 
143  twi_state = TWI_MRX;
144  // reset error state (0xFF.. no error occured)
145  twi_error = 0xFF;
146 
147  // initialize buffer iteration vars
148  twi_masterBufferIndex = 0;
149  twi_masterBufferLength = length-1; // This is not intuitive, read on...
150  // On receive, the previously configured ACK/NACK setting is transmitted in
151  // response to the received byte before the interrupt is signalled.
152  // Therefor we must actually set NACK when the _next_ to last byte is
153  // received, causing that NACK to be sent in response to receiving the last
154  // expected byte of data.
155 
156  // build sla+w, slave device address + w bit
157  twi_slarw = TW_READ;
158  twi_slarw |= address << 1;
159 
160  // send start condition
161  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
162 
163  // wait for read operation to complete
164  while(TWI_MRX == twi_state){
165  continue;
166  }
167 
168  if (twi_masterBufferIndex < length)
169  length = twi_masterBufferIndex;
170 
171  // copy twi buffer to data
172  for(i = 0; i < length; ++i){
173  data[i] = twi_masterBuffer[i];
174  }
175 
176  return length;
177 }
178 
179 
180 
182 
183 
184 uint8_t twi_initiateReadFrom(uint8_t address, uint8_t length)
185 {
186 
187  // ensure data will fit into buffer
188  if(TWI_BUFFER_LENGTH < length){
189  return 0;
190  }
191 
192  twi_readLength = length;
193  twi_readAddress = address;
194 
195  if ( TWI_READY == twi_state ){
196  twi_continueReadFrom();
197  } else {
198  twi_state = TWI_PRE_MRX;
199  }
200  if (twi_error == 0xFF)
201  return 0; // success
202  else if (twi_error == TW_MT_SLA_NACK)
203  return 2; // error: address send, nack received
204  else if (twi_error == TW_MT_DATA_NACK)
205  return 3; // error: data send, nack received
206  else
207  return 4; // other twi error
208 }
209 
210 
211 
212 void twi_continueReadFrom(){
213 
214  twi_state = TWI_MRX;
215  // reset error state (0xFF.. no error occured)
216  twi_error = 0xFF;
217 
218  // initialize buffer iteration vars
219  twi_masterBufferIndex = 0;
220  twi_masterBufferLength = twi_readLength-1; // This is not intuitive, read on...
221  // On receive, the previously configured ACK/NACK setting is transmitted in
222  // response to the received byte before the interrupt is signalled.
223  // Therefor we must actually set NACK when the _next_ to last byte is
224  // received, causing that NACK to be sent in response to receiving the last
225  // expected byte of data.
226 
227  // build sla+w, slave device address + w bit
228  twi_slarw = TW_READ;
229  twi_slarw |= twi_readAddress << 1;
230 
231  // send start condition
232  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
233 }
234 
235 
236 
237 uint8_t twi_readMasterBuffer( uint8_t* data, uint8_t length ){
238  uint8_t i;
239  if (twi_masterBufferIndex < length)
240  length = twi_masterBufferIndex;
241 
242  // copy twi buffer to data
243  for(i = 0; i < length; ++i){
244  data[i] = twi_masterBuffer[i];
245  }
246 
247  return length;
248 }
249 
250 
251 
253 
254 
255 /*
256  * Function twi_writeTo
257  * Desc attempts to become twi bus master and write a
258  * series of bytes to a device on the bus
259  * Input address: 7bit i2c device address
260  * data: pointer to byte array
261  * length: number of bytes in array
262  * wait: boolean indicating to wait for write or not
263  * Output 0 .. success
264  * 1 .. length to long for buffer
265  * 2 .. address send, NACK received
266  * 3 .. data send, NACK received
267  * 4 .. other twi error (lost bus arbitration, bus error, ..)
268  */
270 uint8_t twi_writeToBlocking(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait)
271 {
272  uint8_t i;
273 
274  // ensure data will fit into buffer
275  if(TWI_BUFFER_LENGTH < length){
276  return 1;
277  }
278 
279  // wait until twi is ready, become master transmitter
280  while(TWI_READY != twi_state){
281  continue;
282  }
283 
284  twi_state = TWI_MTX;
285  // reset error state (0xFF.. no error occured)
286  twi_error = 0xFF;
287 
288  // initialize buffer iteration vars
289  twi_masterBufferIndex = 0;
290  twi_masterBufferLength = length;
291 
292  // copy data to twi buffer
293  for(i = 0; i < length; ++i){
294  twi_masterBuffer[i] = data[i];
295  }
296 
297  // build sla+w, slave device address + w bit
298  twi_slarw = TW_WRITE;
299  twi_slarw |= address << 1;
300 
301  // send start condition
302  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
303 
304  // wait for write operation to complete
305  while(wait && (TWI_MTX == twi_state)){
306  continue;
307  }
308 
309  if (twi_error == 0xFF)
310  return 0; // success
311  else if (twi_error == TW_MT_SLA_NACK)
312  return 2; // error: address send, nack received
313  else if (twi_error == TW_MT_DATA_NACK)
314  return 3; // error: data send, nack received
315  else
316  return 4; // other twi error
317 }
318 
319 
320 
322 
323 
324 uint8_t twi_initiateWriteTo(uint8_t address, uint8_t* data, uint8_t length )
325 {
326  // ensure data will fit into buffer
327  if(TWI_BUFFER_LENGTH < length){
328  return 1;
329  }
330  twi_writeAddress = address;
331  twi_writeData = data;
332  twi_writeLength = length;
333 
334  if ( TWI_READY == twi_state ){
335  twi_continueWriteTo();
336  } else {
337  twi_state = TWI_PRE_MTX;
338  }
339  if (twi_error == 0xFF)
340  return 0; // success
341  else if (twi_error == TW_MT_SLA_NACK)
342  return 2; // error: address send, nack received
343  else if (twi_error == TW_MT_DATA_NACK)
344  return 3; // error: data send, nack received
345  else
346  return 4; // other twi error
347 }
348 
349 
350 
351 void twi_continueWriteTo(){
352  uint8_t i;
353  // wait until twi is ready, become master transmitter
354 // while(TWI_READY != twi_state){
355 // continue;
356 // }
357 
358  twi_state = TWI_MTX;
359  // reset error state (0xFF.. no error occured)
360  twi_error = 0xFF;
361 
362  // initialize buffer iteration vars
363  twi_masterBufferIndex = 0;
364  twi_masterBufferLength = twi_writeLength;
365 
366  // copy data to twi buffer
367  for(i = 0; i < twi_writeLength; ++i){
368  twi_masterBuffer[i] = twi_writeData[i];
369  }
370 
371  // build sla+w, slave device address + w bit
372  twi_slarw = TW_WRITE;
373  twi_slarw |= twi_writeAddress << 1;
374 
375  // send start condition
376  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
377 }
378 
379 
380 // -----------end non-blocking --------------------
381 
382 
383 /*
384  * Function twi_reply
385  * Desc sends byte or readys receive line
386  * Input ack: byte indicating to ack or to nack
387  * Output none
388  */
389 void twi_reply(uint8_t ack)
390 {
391  // transmit master read ready signal, with or without ack
392  if(ack){
393  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
394  }else{
395  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
396  }
397 }
398 
399 
400 
401 /*
402  * Function twi_stop
403  * Desc relinquishes bus master status
404  * Input none
405  * Output none
406  */
407 void twi_stop(void)
408 {
409  // send stop condition
410  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
411 
412  // wait for stop condition to be exectued on bus
413  // TWINT is not set after a stop condition!
414  while(TWCR & _BV(TWSTO)){ //FIXME: does this cause a delay?
415  continue;
416  }
417 
418  twi_oldstate = twi_state;
419  // update twi state
420  twi_state = TWI_READY;
421  if ( twi_oldstate == TWI_PRE_MTX ){
422  twi_continueWriteTo();
423  } else if ( twi_oldstate == TWI_PRE_MRX ){
424  twi_continueReadFrom();
425  }
426 }
427 
428 
429 
430 /*
431  * Function twi_releaseBus
432  * Desc releases bus control
433  * Input none
434  * Output none
435  */
436 void twi_releaseBus(void)
437 {
438  // release bus
439  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
440 
441  twi_oldstate = twi_state;
442  // update twi state
443  twi_state = TWI_READY;
444  if ( twi_oldstate == TWI_PRE_MTX ){
445  twi_continueWriteTo();
446  } else if ( twi_oldstate == TWI_PRE_MRX ){
447  twi_continueReadFrom();
448  }
449 }
450 
451 // SIGNAL(TWI_vect)
452 // ISR(TWI_vect, ISR_NOBLOCK )
453 ISR(TWI_vect)
454 {
455  switch(TW_STATUS){
456  // All Master
457  case TW_START: // sent start condition
458  case TW_REP_START: // sent repeated start condition
459  // copy device address and r/w bit to output register and ack
460  TWDR = twi_slarw;
461  twi_reply(1);
462  break;
463 
464  // Master Transmitter
465  case TW_MT_SLA_ACK: // slave receiver acked address
466  case TW_MT_DATA_ACK: // slave receiver acked data
467  // if there is data to send, send it, otherwise stop
468  if(twi_masterBufferIndex < twi_masterBufferLength){
469  // copy data to output register and ack
470  TWDR = twi_masterBuffer[twi_masterBufferIndex++];
471  twi_reply(1);
472  }else{
473  twi_stop();
474  }
475  break;
476  case TW_MT_SLA_NACK: // address sent, nack received
477  twi_error = TW_MT_SLA_NACK;
478  twi_stop();
479  break;
480  case TW_MT_DATA_NACK: // data sent, nack received
481  twi_error = TW_MT_DATA_NACK;
482  twi_stop();
483  break;
484  case TW_MT_ARB_LOST: // lost bus arbitration
485  twi_error = TW_MT_ARB_LOST;
486  twi_releaseBus();
487  break;
488 
489  // Master Receiver
490  case TW_MR_DATA_ACK: // data received, ack sent
491  // put byte into buffer
492  twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
493  case TW_MR_SLA_ACK: // address sent, ack received
494  // ack if more bytes are expected, otherwise nack
495  if(twi_masterBufferIndex < twi_masterBufferLength){
496  twi_reply(1);
497  }else{
498  twi_reply(0);
499  }
500  break;
501  case TW_MR_DATA_NACK: // data received, nack sent
502  // put final byte into buffer
503  twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
504  case TW_MR_SLA_NACK: // address sent, nack received
505  twi_stop();
506  break;
507  // TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case
508 
509 // // Slave Receiver
510 // case TW_SR_SLA_ACK: // addressed, returned ack
511 // case TW_SR_GCALL_ACK: // addressed generally, returned ack
512 // case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack
513 // case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack
514 // // enter slave receiver mode
515 // twi_state = TWI_SRX;
516 // // indicate that rx buffer can be overwritten and ack
517 // twi_rxBufferIndex = 0;
518 // twi_reply(1);
519 // break;
520 // case TW_SR_DATA_ACK: // data received, returned ack
521 // case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack
522 // // if there is still room in the rx buffer
523 // if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
524 // // put byte in buffer and ack
525 // twi_rxBuffer[twi_rxBufferIndex++] = TWDR;
526 // twi_reply(1);
527 // }else{
528 // // otherwise nack
529 // twi_reply(0);
530 // }
531 // break;
532 // case TW_SR_STOP: // stop or repeated start condition received
533 // // put a null char after data if there's room
534 // if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
535 // twi_rxBuffer[twi_rxBufferIndex] = '\0';
536 // }
537 // // sends ack and stops interface for clock stretching
538 // twi_stop();
539 // // callback to user defined callback
540 // twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex);
541 // // since we submit rx buffer to "wire" library, we can reset it
542 // twi_rxBufferIndex = 0;
543 // // ack future responses and leave slave receiver state
544 // twi_releaseBus();
545 // break;
546 // case TW_SR_DATA_NACK: // data received, returned nack
547 // case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack
548 // // nack back at master
549 // twi_reply(0);
550 // break;
551 //
552 // // Slave Transmitter
553 // case TW_ST_SLA_ACK: // addressed, returned ack
554 // case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack
555 // // enter slave transmitter mode
556 // twi_state = TWI_STX;
557 // // ready the tx buffer index for iteration
558 // twi_txBufferIndex = 0;
559 // // set tx buffer length to be zero, to verify if user changes it
560 // twi_txBufferLength = 0;
561 // // request for txBuffer to be filled and length to be set
562 // // note: user must call twi_transmit(bytes, length) to do this
563 // twi_onSlaveTransmit();
564 // // if they didn't change buffer & length, initialize it
565 // if(0 == twi_txBufferLength){
566 // twi_txBufferLength = 1;
567 // twi_txBuffer[0] = 0x00;
568 // }
569 // // transmit first byte from buffer, fall
570 // case TW_ST_DATA_ACK: // byte sent, ack returned
571 // // copy data to output register
572 // TWDR = twi_txBuffer[twi_txBufferIndex++];
573 // // if there is more to send, ack, otherwise nack
574 // if(twi_txBufferIndex < twi_txBufferLength){
575 // twi_reply(1);
576 // }else{
577 // twi_reply(0);
578 // }
579 // break;
580 // case TW_ST_DATA_NACK: // received nack, we are done
581 // case TW_ST_LAST_DATA: // received ack, but we are done already!
582 // // ack future responses
583 // twi_reply(1);
584 // // leave slave receiver state
585 // twi_state = TWI_READY;
586 // break;
587 
588  // All
589  case TW_NO_INFO: // no state information
590  break;
591  case TW_BUS_ERROR: // bus error, illegal stop/start
592  twi_error = TW_BUS_ERROR;
593  twi_stop();
594  break;
595  }
596 }
597 
598 #endif