1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-23 21:01:25 +01:00

Temporarily use Wire for I2C.

This commit is contained in:
Neil McKechnie 2021-08-25 10:26:45 +01:00
parent bad9e866f8
commit 2469629cbb
3 changed files with 15 additions and 15 deletions

View File

@ -111,7 +111,7 @@
*
*/
//#define I2C_USE_WIRE
#define I2C_USE_WIRE
#ifndef I2C_NO_INTERRUPTS
#define I2C_USE_INTERRUPTS
#endif
@ -235,7 +235,7 @@ private:
// Mark volatile as they are updated by IRC and read/written elsewhere.
static I2CRB * volatile queueHead;
static I2CRB * volatile queueTail;
static volatile uint8_t status;
static volatile uint8_t state;
static I2CRB * volatile currentRequest;
static volatile uint8_t txCount;

View File

@ -136,7 +136,7 @@ void I2CManagerClass::I2C_handleInterrupt() {
} else { // Nothing left to send or receive
TWDR = 0xff; // Default condition = SDA released
TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA)|(1<<TWSTO); // Send Stop
status = I2C_STATUS_OK;
state = I2C_STATUS_OK;
}
break;
case TWI_MRX_DATA_ACK: // Data byte has been received and ACK transmitted
@ -159,7 +159,7 @@ void I2CManagerClass::I2C_handleInterrupt() {
bytesToReceive--;
}
TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA)|(1<<TWSTO); // Send Stop
status = I2C_STATUS_OK;
state = I2C_STATUS_OK;
break;
case TWI_START: // START has been transmitted
case TWI_REP_START: // Repeated START has been transmitted
@ -175,7 +175,7 @@ void I2CManagerClass::I2C_handleInterrupt() {
case TWI_MTX_DATA_NACK: // Data byte has been transmitted and NACK received
TWDR = 0xff; // Default condition = SDA released
TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA)|(1<<TWSTO); // Send Stop
status = I2C_STATUS_NEGATIVE_ACKNOWLEDGE;
state = I2C_STATUS_NEGATIVE_ACKNOWLEDGE;
break;
case TWI_ARB_LOST: // Arbitration lost
// Restart transaction from start.
@ -185,7 +185,7 @@ void I2CManagerClass::I2C_handleInterrupt() {
default:
TWDR = 0xff; // Default condition = SDA released
TWCR = (1<<TWEN)|(1<<TWINT)|(1<<TWEA)|(1<<TWSTO); // Send Stop
status = I2C_STATUS_TRANSMIT_ERROR;
state = I2C_STATUS_TRANSMIT_ERROR;
}
}

View File

@ -41,7 +41,7 @@
void I2CManagerClass::_initialise()
{
queueHead = queueTail = NULL;
status = I2C_STATE_FREE;
state = I2C_STATE_FREE;
I2C_init();
}
@ -60,8 +60,8 @@ void I2CManagerClass::_setClock(unsigned long i2cClockSpeed) {
void I2CManagerClass::startTransaction() {
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
I2CRB *t = queueHead;
if ((status == I2C_STATE_FREE) && (t != NULL)) {
status = I2C_STATE_ACTIVE;
if ((state == I2C_STATE_FREE) && (t != NULL)) {
state = I2C_STATE_ACTIVE;
currentRequest = t;
rxCount = txCount = 0;
// Copy key fields to static data for speed.
@ -135,7 +135,7 @@ void I2CManagerClass::checkForTimeout() {
unsigned long currentMicros = micros();
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
I2CRB *t = queueHead;
if (t && timeout > 0) {
if (state==I2C_STATE_ACTIVE && t!=0 && timeout > 0) {
// Check for timeout
if (currentMicros - startTime > timeout) {
// Excessive time. Dequeue request
@ -148,7 +148,7 @@ void I2CManagerClass::checkForTimeout() {
// Try close and init, not entirely satisfactory but sort of works...
I2C_close(); // Shutdown and restart twi interface
I2C_init();
status = I2C_STATE_FREE;
state = I2C_STATE_FREE;
// Initiate next queued request
startTransaction();
@ -178,7 +178,7 @@ void I2CManagerClass::handleInterrupt() {
// Experimental -- perform the post processing with interrupts enabled.
//interrupts();
if (status!=I2C_STATUS_PENDING) {
if (state!=I2C_STATE_ACTIVE && state != I2C_STATE_FREE) {
// Remove completed request from head of queue
I2CRB * t;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
@ -187,10 +187,10 @@ void I2CManagerClass::handleInterrupt() {
queueHead = t->nextRequest;
if (!queueHead) queueTail = queueHead;
t->nBytes = rxCount;
t->status = status;
t->status = state;
}
// I2C state machine is now free for next request
status = I2C_STATE_FREE;
state = I2C_STATE_FREE;
}
// Start next request (if any)
I2CManager.startTransaction();
@ -201,7 +201,7 @@ void I2CManagerClass::handleInterrupt() {
I2CRB * volatile I2CManagerClass::queueHead = NULL;
I2CRB * volatile I2CManagerClass::queueTail = NULL;
I2CRB * volatile I2CManagerClass::currentRequest = NULL;
volatile uint8_t I2CManagerClass::status = I2C_STATE_FREE;
volatile uint8_t I2CManagerClass::state = I2C_STATE_FREE;
volatile uint8_t I2CManagerClass::txCount;
volatile uint8_t I2CManagerClass::rxCount;
volatile uint8_t I2CManagerClass::operation;