Browse Source

General cleanup of HAL code

pull/1/head
Scott Lahteine 7 years ago
parent
commit
b13099de3f
  1. 51
      Marlin/src/HAL/HAL_LPC1768/Wire.cpp

51
Marlin/src/HAL/HAL_LPC1768/Wire.cpp

@ -75,7 +75,8 @@ void TwoWire::begin(void) {
PINSEL_CFG_Type PinCfg; PINSEL_CFG_Type PinCfg;
PinCfg.OpenDrain = 0; PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0; PinCfg.Pinmode = 0;
#if ((USEDI2CDEV_M == 0))
#if USEDI2CDEV_M == 0
PinCfg.Funcnum = 1; PinCfg.Funcnum = 1;
PinCfg.Pinnum = 27; PinCfg.Pinnum = 27;
PinCfg.Portnum = 0; PinCfg.Portnum = 0;
@ -83,7 +84,8 @@ void TwoWire::begin(void) {
PinCfg.Pinnum = 28; PinCfg.Pinnum = 28;
PINSEL_ConfigPin(&PinCfg); // SCL0 / D58 AUX-1 PINSEL_ConfigPin(&PinCfg); // SCL0 / D58 AUX-1
#endif #endif
#if ((USEDI2CDEV_M == 1))
#if USEDI2CDEV_M == 1
PinCfg.Funcnum = 3; PinCfg.Funcnum = 3;
PinCfg.Pinnum = 0; PinCfg.Pinnum = 0;
PinCfg.Portnum = 0; PinCfg.Portnum = 0;
@ -91,7 +93,8 @@ void TwoWire::begin(void) {
PinCfg.Pinnum = 1; PinCfg.Pinnum = 1;
PINSEL_ConfigPin(&PinCfg); // SCL1 / D21 SCL PINSEL_ConfigPin(&PinCfg); // SCL1 / D21 SCL
#endif #endif
#if ((USEDI2CDEV_M == 2))
#if USEDI2CDEV_M == 2
PinCfg.Funcnum = 2; PinCfg.Funcnum = 2;
PinCfg.Pinnum = 10; PinCfg.Pinnum = 10;
PinCfg.Portnum = 0; PinCfg.Portnum = 0;
@ -109,9 +112,8 @@ void TwoWire::begin(void) {
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) { uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
// clamp to buffer length // clamp to buffer length
if(quantity > BUFFER_LENGTH){ if (quantity > BUFFER_LENGTH)
quantity = BUFFER_LENGTH; quantity = BUFFER_LENGTH;
}
// perform blocking read into buffer // perform blocking read into buffer
I2C_M_SETUP_Type transferMCfg; I2C_M_SETUP_Type transferMCfg;
@ -166,23 +168,17 @@ uint8_t TwoWire::endTransmission(void) {
// indicate that we are done transmitting // indicate that we are done transmitting
transmitting = 0; transmitting = 0;
if (status == SUCCESS) return status == SUCCESS ? 0 : 4;
return 0; // success
else
return 4; // other error
} }
// must be called after beginTransmission(address) // must be called after beginTransmission(address)
size_t TwoWire::write(uint8_t data) { size_t TwoWire::write(uint8_t data) {
if (transmitting) { if (transmitting) {
// don't bother if buffer is full // don't bother if buffer is full
if (txBufferLength >= BUFFER_LENGTH) { if (txBufferLength >= BUFFER_LENGTH) return 0;
return 0;
}
// put byte in tx buffer // put byte in tx buffer
txBuffer[txBufferIndex] = data; txBuffer[txBufferIndex++] = data;
++txBufferIndex;
// update amount in buffer // update amount in buffer
txBufferLength = txBufferIndex; txBufferLength = txBufferIndex;
@ -196,39 +192,24 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity) {
size_t sent = 0; size_t sent = 0;
if (transmitting) if (transmitting)
for (sent = 0; sent < quantity; ++sent) for (sent = 0; sent < quantity; ++sent)
if (!write(data[sent])) if (!write(data[sent])) break;
break;
return sent; return sent;
} }
// must be called after requestFrom(address, numBytes) // Must be called after requestFrom(address, numBytes)
int TwoWire::available(void) { int TwoWire::available(void) {
return rxBufferLength - rxBufferIndex; return rxBufferLength - rxBufferIndex;
} }
// must be called after requestFrom(address, numBytes) // Must be called after requestFrom(address, numBytes)
int TwoWire::read(void) { int TwoWire::read(void) {
int value = -1; return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex++] : -1;
// get each successive byte on each call
if(rxBufferIndex < rxBufferLength) {
value = rxBuffer[rxBufferIndex];
++rxBufferIndex;
} }
return value; // Must be called after requestFrom(address, numBytes)
}
// must be called after requestFrom(address, numBytes)
int TwoWire::peek(void) { int TwoWire::peek(void) {
int value = -1; return rxBufferIndex < rxBufferLength ? rxBuffer[rxBufferIndex] : -1;
if(rxBufferIndex < rxBufferLength){
value = rxBuffer[rxBufferIndex];
}
return value;
} }
// Preinstantiate Objects ////////////////////////////////////////////////////// // Preinstantiate Objects //////////////////////////////////////////////////////

Loading…
Cancel
Save