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