Peter Ward avatar Peter Ward committed 93e2802

Fixes to the TWI and Accelerometer APIs.

Comments (0)

Files changed (2)

include/arduino/accel.h

 
 static inline
 uint8_t _accel_recv(accel_t a, uint8_t reg) {
-    uint8_t rv;
+    uint8_t rv = 255;
     twi_write(a.twi, a.part_addr, &reg, 1);
     twi_read(a.twi, a.part_addr, &rv, 1);
     return rv;
             // TODO: is this sane?
             return 127; // ERROR
     }
-    return _accel_recv(a, dir);
+    return _accel_recv(a, reg);
 }
 
 static inline

include/arduino/twi.h

     // received, causing that NACK to be sent in response to receiving the last
     // expected byte of data.
     t.ram->master_buffer_left = length - 1;
+//    t.ram->master_buffer_left = length;
 
     // build sla+w, slave device address + w bit
     t.ram->slarw = TW_READ;
  * Output   none
  */
 static inline
-void _twi_reply(uint8_t ack)
+void _twi_reply(twi_t t, bool ack)
 {
     // transmit master read ready signal, with or without ack
-    if(ack){
-        TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
-    }else{
-        TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
+    if (ack) {
+        *(t.twcr) = (
+            (1 << t.twen) |
+            (1 << t.twie) |
+            (1 << t.twint) |
+            (1 << t.twea)
+        );
+    } else {
+        *(t.twcr) = (
+            (1 << t.twen) |
+            (1 << t.twie) |
+            (1 << t.twint)
+        );
     }
 }
 
         case TW_REP_START: // sent repeated start condition
             // copy device address and r/w bit to output register and ack
             *(t.twdr) = t.ram->slarw;
-            _twi_reply(1);
+            _twi_reply(t, 1);
             break;
 
         // Master Transmitter
                 // copy data to output register and ack
                 *(t.twdr) = *(t.ram->master_buffer++);
                 t.ram->master_buffer_left--;
-                _twi_reply(1);
+                _twi_reply(t, 1);
             } else {
                 _twi_stop(t);
             }
             t.ram->master_buffer_left--;
         case TW_MR_SLA_ACK:  // address sent, ack received
             // ack if more bytes are expected, otherwise nack
-            _twi_reply(t.ram->master_buffer_left > 0);
+            _twi_reply(t, t.ram->master_buffer_left > 0);
             break;
         case TW_MR_DATA_NACK: // data received, nack sent
             // put final byte into buffer
             *(t.ram->master_buffer++) = *(t.twdr);
             t.ram->master_buffer_left--;
         case TW_MR_SLA_NACK: // address sent, nack received
+            // SOMEHOW I'M GETTING HERE. :(
             _twi_stop(t);
             break;
             // TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case
             t.ram->state = TWI_STATE_SRX;
             // indicate that rx buffer can be overwritten and ack
             ring_buffer_clear(t.rx_buffer);
-            _twi_reply(1);
+            _twi_reply(t, 1);
             break;
         case TW_SR_DATA_ACK:       // data received, returned ack
         case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack
             // if there is still room in the rx buffer
             success = ring_buffer_append(t.rx_buffer, *(t.twdr));
-            _twi_reply(success);
+            _twi_reply(t, success);
             break;
         case TW_SR_STOP: // stop or repeated start condition received
             // sends ack and stops interface for clock stretching
         case TW_SR_DATA_NACK:       // data received, returned nack
         case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack
             // nack back at master
-            _twi_reply(0);
+            _twi_reply(t, 0);
             break;
 
         // Slave Transmitter
             val = ring_buffer_pop(t.tx_buffer);
             if (val != -1) {
                 *(t.twdr) = val;
-                _twi_reply(1);
+                _twi_reply(t, 1);
             } else {
-                _twi_reply(0);
+                _twi_reply(t, 0);
             }
             break;
         case TW_ST_DATA_NACK: // received nack, we are done 
         case TW_ST_LAST_DATA: // received ack, but we are done already!
             // ack future responses
-            _twi_reply(1);
+            _twi_reply(t, 1);
             // leave slave receiver state
             t.ram->state = TWI_STATE_READY;
             break;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.