#include <inttypes.h>


Go to the source code of this file.
Defines | |
| #define | CPU_FREQ 8000000L |
| #define | TWI_FREQ 100000L |
| #define | TWI_BUFFER_LENGTH 32 |
| #define | TWI_READY 0 |
| #define | TWI_MRX 1 |
| #define | TWI_MTX 2 |
| #define | TWI_SRX 3 |
| #define | TWI_STX 4 |
Functions | |
| void | twi_init (void) |
| void | twi_setAddress (uint8_t) |
| uint8_t | twi_readFrom (uint8_t, uint8_t *, uint8_t) |
| uint8_t | twi_writeTo (uint8_t, uint8_t *, uint8_t, uint8_t) |
| uint8_t | twi_transmit (uint8_t *, uint8_t) |
| void | twi_attachSlaveRxEvent (void(*)(uint8_t *, int)) |
| void | twi_attachSlaveTxEvent (void(*)(void)) |
| void | twi_reply (uint8_t) |
| void | twi_stop (void) |
| void | twi_releaseBus (void) |
| void | twi_close (void) |
| #define CPU_FREQ 8000000L |
| #define TWI_BUFFER_LENGTH 32 |
Definition at line 37 of file twi.h.
Referenced by SIGNAL(), twi_init(), twi_readFrom(), twi_transmit(), and twi_writeTo().
| #define TWI_FREQ 100000L |
| #define TWI_MRX 1 |
| #define TWI_MTX 2 |
| #define TWI_READY 0 |
Definition at line 40 of file twi.h.
Referenced by SIGNAL(), twi_init(), twi_readFrom(), twi_releaseBus(), twi_stop(), and twi_writeTo().
| #define TWI_STX 4 |
| void twi_attachSlaveRxEvent | ( | void(*)(uint8_t *, int) | ) |
Definition at line 247 of file twi.c.
References twi_onSlaveReceive.
Referenced by TwoWire::begin().
00248 { 00249 twi_onSlaveReceive = function; 00250 }

| void twi_attachSlaveTxEvent | ( | void(*)(void) | ) |
Definition at line 258 of file twi.c.
References twi_onSlaveTransmit.
Referenced by TwoWire::begin().
00259 { 00260 twi_onSlaveTransmit = function; 00261 }

| void twi_close | ( | void | ) |
Definition at line 315 of file twi.c.
References cbi, sbi, twi_masterBuffer, twi_rxBuffer, and twi_txBuffer.
Referenced by TwoWire::close().
00316 { 00317 00318 // de-activate internal pull-up resistors 00319 cbi(PORTD, 0); 00320 cbi(PORTD, 1); 00321 00322 00323 sbi(TWSR, TWPS0); 00324 sbi(TWSR, TWPS1); 00325 00326 // disable twi module, acks, and twi interrupt 00327 sbi(TWCR,TWINT); 00328 sbi(TWCR,TWSTO); 00329 cbi(TWCR,TWEA); 00330 cbi(TWCR,TWSTA); 00331 cbi(TWCR,TWWC); 00332 sbi(TWCR,TWEN); 00333 cbi(TWCR,TWIE); 00334 cbi(PRR0,PRTWI); 00335 00336 free(twi_masterBuffer); 00337 free(twi_txBuffer); 00338 free(twi_rxBuffer); 00339 }

| void twi_init | ( | void | ) |
Definition at line 62 of file twi.c.
References cbi, CPU_FREQ, sbi, TWI_BUFFER_LENGTH, TWI_FREQ, twi_masterBuffer, TWI_READY, twi_rxBuffer, twi_state, and twi_txBuffer.
Referenced by TwoWire::begin().
00063 { 00064 // initialize state 00065 twi_state = TWI_READY; 00066 00067 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) 00068 // activate internal pull-ups for twi 00069 // as per note from atmega8 manual pg167 00070 sbi(PORTC, 4); 00071 sbi(PORTC, 5); 00072 #else 00073 // activate internal pull-ups for twi 00074 // as per note from atmega128 manual pg204 00075 // this also applies to mega1281 00076 sbi(PORTD, 0); 00077 sbi(PORTD, 1); 00078 #endif 00079 00080 // initialize twi prescaler and bit rate 00081 cbi(TWSR, TWPS0); 00082 cbi(TWSR, TWPS1); 00083 TWBR = ((CPU_FREQ / TWI_FREQ) - 16) / 2; // FIXME!! 00084 00085 /* twi bit rate formula from atmega128 manual pg 204 00086 SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR)) 00087 note: TWBR should be 10 or higher for master mode 00088 It is 72 for a 16mhz Wiring board with 100kHz TWI */ 00089 00090 // enable twi module, acks, and twi interrupt 00091 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA); 00092 00093 // allocate buffers 00094 twi_masterBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t)); 00095 twi_txBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t)); 00096 twi_rxBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t)); 00097 }

| uint8_t twi_readFrom | ( | uint8_t | , | |
| uint8_t * | , | |||
| uint8_t | ||||
| ) |
Definition at line 120 of file twi.c.
References TWI_BUFFER_LENGTH, twi_masterBuffer, twi_masterBufferIndex, twi_masterBufferLength, TWI_MRX, TWI_READY, twi_slarw, and twi_state.
Referenced by TwoWire::requestFrom().
00121 { 00122 uint8_t i; 00123 00124 // ensure data will fit into buffer 00125 if(TWI_BUFFER_LENGTH < length){ 00126 return 1; 00127 } 00128 00129 // wait until twi is ready, become master receiver 00130 while(TWI_READY != twi_state){ 00131 continue; 00132 } 00133 twi_state = TWI_MRX; 00134 00135 // initialize buffer iteration vars 00136 twi_masterBufferIndex = 0; 00137 twi_masterBufferLength = length; 00138 00139 // build sla+w, slave device address + w bit 00140 twi_slarw = TW_READ; 00141 twi_slarw |= address << 1; 00142 00143 // send start condition 00144 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); 00145 00146 // wait for read operation to complete 00147 while(TWI_MRX == twi_state){ 00148 continue; 00149 } 00150 00151 // copy twi buffer to data 00152 for(i = 0; i < length; ++i){ 00153 data[i] = twi_masterBuffer[i]; 00154 } 00155 00156 return 0; 00157 }

| void twi_releaseBus | ( | void | ) |
Definition at line 306 of file twi.c.
References TWI_READY, and twi_state.
Referenced by SIGNAL().
00307 { 00308 // release bus 00309 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT); 00310 00311 // update twi state 00312 twi_state = TWI_READY; 00313 }

| void twi_reply | ( | uint8_t | ) |
Definition at line 269 of file twi.c.
Referenced by SIGNAL().
00270 { 00271 // transmit master read ready signal, with or without ack 00272 if(ack){ 00273 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA); 00274 }else{ 00275 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT); 00276 } 00277 }

| void twi_setAddress | ( | uint8_t | ) |
Definition at line 105 of file twi.c.
Referenced by TwoWire::begin().

| void twi_stop | ( | void | ) |
Definition at line 285 of file twi.c.
References TWI_READY, and twi_state.
Referenced by SIGNAL().
00286 { 00287 // send stop condition 00288 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); 00289 00290 // wait for stop condition to be exectued on bus 00291 // TWINT is not set after a stop condition! 00292 while(TWCR & _BV(TWSTO)){ 00293 continue; 00294 } 00295 00296 // update twi state 00297 twi_state = TWI_READY; 00298 }

| uint8_t twi_transmit | ( | uint8_t * | , | |
| uint8_t | ||||
| ) |
Definition at line 218 of file twi.c.
References TWI_BUFFER_LENGTH, twi_state, TWI_STX, twi_txBuffer, and twi_txBufferLength.
Referenced by TwoWire::send().
00219 { 00220 uint8_t i; 00221 00222 // ensure data will fit into buffer 00223 if(TWI_BUFFER_LENGTH < length){ 00224 return 1; 00225 } 00226 00227 // ensure we are currently a slave transmitter 00228 if(TWI_STX != twi_state){ 00229 return 2; 00230 } 00231 00232 // set length and copy data into tx buffer 00233 twi_txBufferLength = length; 00234 for(i = 0; i < length; ++i){ 00235 twi_txBuffer[i] = data[i]; 00236 } 00237 00238 return 0; 00239 }

| uint8_t twi_writeTo | ( | uint8_t | , | |
| uint8_t * | , | |||
| uint8_t | , | |||
| uint8_t | ||||
| ) |
Definition at line 169 of file twi.c.
References TWI_BUFFER_LENGTH, twi_masterBuffer, twi_masterBufferIndex, twi_masterBufferLength, TWI_MTX, TWI_READY, twi_slarw, and twi_state.
Referenced by TwoWire::endTransmission().
00170 { 00171 uint8_t i; 00172 00173 // ensure data will fit into buffer 00174 if(TWI_BUFFER_LENGTH < length){ 00175 return 1; 00176 } 00177 00178 // wait until twi is ready, become master transmitter 00179 while(TWI_READY != twi_state){ 00180 continue; 00181 } 00182 twi_state = TWI_MTX; 00183 00184 // initialize buffer iteration vars 00185 twi_masterBufferIndex = 0; 00186 twi_masterBufferLength = length; 00187 00188 // copy data to twi buffer 00189 for(i = 0; i < length; ++i){ 00190 twi_masterBuffer[i] = data[i]; 00191 } 00192 00193 // build sla+w, slave device address + w bit 00194 twi_slarw = TW_WRITE; 00195 twi_slarw |= address << 1; 00196 00197 // send start condition 00198 TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); 00199 00200 // wait for write operation to complete 00201 while(wait && (TWI_MTX == twi_state)){ 00202 continue; 00203 } 00204 00205 return 0; 00206 }

1.5.6