void TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart, uint8_t blocking) { // Wait until ready while (!isTWIReady()) {_delay_us(1);} // Reset the I2C stuff TWCR = (1 << TWINT)|(1 << TWSTO); TWIInit(); // Set repeated start mode TWIInfo.repStart = repStart; // Copy transmit info to global variables TWITransmitBuffer = (uint8_t *)TXdata; TXBuffLen = dataLen; TXBuffIndex = 0; // If a repeated start has been sent, then devices are already listening for an address // and another start does not need to be sent. if (TWIInfo.mode == RepeatedStartSent) { TWIInfo.mode = Initializing; TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer TWISendTransmit(); // Send the data } else // Otherwise, just send the normal start signal to begin transmission. { TWIInfo.mode = Initializing; TWISendStart(); } if(blocking){ // Wait until ready while (!isTWIReady()){_delay_us(1);} } }
void DAC::beginTransmission() volatile { TWISendStart(); TWIWaitForInterrupt(); /*if(TWIGetStatusCode() != TWI_START_SC) uart_puts("No start sc\n\r");*/ TWDR = DAC_ADDRESS_W; TWIClearInterruptFlag(); TWIWaitForInterrupt(); /*if(TWIGetStatusCode() != TWI_SLAVE_W_ACK_SC) uart_puts("No address ack\n\r");*/ TWDR = DAC_CONTROL_BYTE; TWIClearInterruptFlag(); TWIWaitForInterrupt(); /*if(TWIGetStatusCode() != TWI_SLAVE_DATA_ACK_SC) uart_puts("No control byte ack\n\r"); uart_putint(TWIGetStatusCode());*/ }
uint8_t TWITransmitData(void *const TXdata, uint8_t dataLen, uint8_t repStart) { if (dataLen <= TXMAXBUFLEN) { // Wait until ready while (!isTWIReady()) {_delay_us(1);} // Set repeated start mode TWIInfo.repStart = repStart; // Copy data into the transmit buffer uint8_t *data = (uint8_t *)TXdata; for (int i = 0; i < dataLen; i++) { TWITransmitBuffer[i] = data[i]; } // Copy transmit info to global variables TXBuffLen = dataLen; TXBuffIndex = 0; // If a repeated start has been sent, then devices are already listening for an address // and another start does not need to be sent. if (TWIInfo.mode == RepeatedStartSent) { TWIInfo.mode = Initializing; TWDR = TWITransmitBuffer[TXBuffIndex++]; // Load data to transmit buffer TWISendTransmit(); // Send the data } else // Otherwise, just send the normal start signal to begin transmission. { TWIInfo.mode = Initializing; TWISendStart(); } } else { return 1; // return an error if data length is longer than buffer } return 0; }