General cleanup of HAL code

This commit is contained in:
Scott Lahteine 2018-02-02 03:15:01 -06:00
parent 9b9350e010
commit b13099de3f
4 changed files with 53 additions and 72 deletions

View file

@ -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;
@ -102,17 +105,16 @@ void TwoWire::begin(void) {
// Initialize I2C peripheral // Initialize I2C peripheral
I2C_Init(I2CDEV_M, 100000); I2C_Init(I2CDEV_M, 100000);
// Enable Master I2C operation // Enable Master I2C operation
I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE); I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
} }
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;
transferMCfg.sl_addr7bit = address >> 1; // not sure about the right shift transferMCfg.sl_addr7bit = address >> 1; // not sure about the right shift
@ -158,7 +160,7 @@ uint8_t TwoWire::endTransmission(void) {
transferMCfg.rx_length = 0; transferMCfg.rx_length = 0;
transferMCfg.retransmissions_max = 3; transferMCfg.retransmissions_max = 3;
Status status = I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING); Status status = I2C_MasterTransferData(I2CDEV_M, &transferMCfg, I2C_TRANSFER_POLLING);
// reset tx buffer iterator vars // reset tx buffer iterator vars
txBufferIndex = 0; txBufferIndex = 0;
txBufferLength = 0; txBufferLength = 0;
@ -166,25 +168,19 @@ 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;
} }
@ -195,40 +191,25 @@ size_t TwoWire::write(uint8_t data) {
size_t TwoWire::write(const uint8_t *data, size_t quantity) { 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 //////////////////////////////////////////////////////

View file

@ -91,12 +91,12 @@ void spiSendBlock(uint8_t token, const uint8_t* buf) {
SPI.beginTransaction(spiConfig); SPI.beginTransaction(spiConfig);
SPDR = token; SPDR = token;
for (uint16_t i = 0; i < 512; i += 2) { for (uint16_t i = 0; i < 512; i += 2) {
while (!TEST(SPSR, SPIF)) { /* nada */ }; while (!TEST(SPSR, SPIF)) { /* nada */ };
SPDR = buf[i]; SPDR = buf[i];
while (!TEST(SPSR, SPIF)) { /* nada */ }; while (!TEST(SPSR, SPIF)) { /* nada */ };
SPDR = buf[i + 1]; SPDR = buf[i + 1];
} }
while (!TEST(SPSR, SPIF)) { /* nada */ }; while (!TEST(SPSR, SPIF)) { /* nada */ };
SPI.endTransaction(); SPI.endTransaction();
} }

View file

@ -60,7 +60,7 @@ public:
void init(uint8_t fourbitmode, pin_t rs, pin_t rw, pin_t enable, void init(uint8_t fourbitmode, pin_t rs, pin_t rw, pin_t enable,
pin_t d0, pin_t d1, pin_t d2, pin_t d3, pin_t d0, pin_t d1, pin_t d2, pin_t d3,
pin_t d4, pin_t d5, pin_t d6, pin_t d7); pin_t d4, pin_t d5, pin_t d6, pin_t d7);
void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS); void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
void clear(); void clear();
@ -81,10 +81,10 @@ public:
void setRowOffsets(int row1, int row2, int row3, int row4); void setRowOffsets(int row1, int row2, int row3, int row4);
void createChar(uint8_t, uint8_t[]); void createChar(uint8_t, uint8_t[]);
void setCursor(uint8_t, uint8_t); void setCursor(uint8_t, uint8_t);
virtual size_t write(uint8_t); virtual size_t write(uint8_t);
void command(uint8_t); void command(uint8_t);
using Print::write; using Print::write;
private: private:
void send(uint8_t, uint8_t); void send(uint8_t, uint8_t);

View file

@ -1,21 +1,21 @@
/* /*
Print.cpp - Base class that provides print() and println() Print.cpp - Base class that provides print() and println()
Copyright (c) 2008 David A. Mellis. All right reserved. Copyright (c) 2008 David A. Mellis. All right reserved.
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified 23 November 2006 by David A. Mellis Modified 23 November 2006 by David A. Mellis
Modified 03 August 2015 by Chuck Todd Modified 03 August 2015 by Chuck Todd
*/ */
@ -37,7 +37,7 @@ typedef signed long sint32_t;
/* default implementation: may be overridden */ /* default implementation: may be overridden */
size_t Print::write(const uint8_t *buffer, size_t size) size_t Print::write(const uint8_t *buffer, size_t size)
{ {
size_t n = 0; size_t n = 0;
while (size--) { while (size--) {
if (write(*buffer++)) n++; if (write(*buffer++)) n++;
@ -49,7 +49,7 @@ size_t Print::write(const uint8_t *buffer, size_t size)
size_t Print::print(const char str[]) size_t Print::print(const char str[])
{ {
//while(1); //while(1);
return write(str); return write(str);
} }
@ -195,15 +195,15 @@ size_t Print::printNumber(unsigned long n, uint8_t base) {
return write(str); return write(str);
} }
size_t Print::printFloat(double number, uint8_t digits) size_t Print::printFloat(double number, uint8_t digits)
{ {
size_t n = 0; size_t n = 0;
if (isnan(number)) return print("nan"); if (isnan(number)) return print("nan");
if (isinf(number)) return print("inf"); if (isinf(number)) return print("inf");
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
// Handle negative numbers // Handle negative numbers
if (number < 0.0) if (number < 0.0)
{ {
@ -215,7 +215,7 @@ size_t Print::printFloat(double number, uint8_t digits)
double rounding = 0.5; double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i) for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0; rounding /= 10.0;
number += rounding; number += rounding;
// Extract the integer part of the number and print it // Extract the integer part of the number and print it
@ -225,7 +225,7 @@ size_t Print::printFloat(double number, uint8_t digits)
// Print the decimal point, but only if there are digits beyond // Print the decimal point, but only if there are digits beyond
if (digits > 0) { if (digits > 0) {
n += print("."); n += print(".");
} }
// Extract digits from the remainder one at a time // Extract digits from the remainder one at a time
@ -234,14 +234,14 @@ size_t Print::printFloat(double number, uint8_t digits)
remainder *= 10.0; remainder *= 10.0;
int toPrint = int(remainder); int toPrint = int(remainder);
n += print(toPrint); n += print(toPrint);
remainder -= toPrint; remainder -= toPrint;
} }
return n; return n;
} }
#if (PrintfEnable == 1) #if (PrintfEnable == 1)
size_t Print::printf(const char *argList, ...) size_t Print::printf(const char *argList, ...)
{ {
const char *ptr; const char *ptr;
@ -279,7 +279,7 @@ size_t Print::printf(const char *argList, ...)
else else
{ {
numOfDigits = 0xff; numOfDigits = 0xff;
} }
switch(ch) /* Decode the type of the argument */ switch(ch) /* Decode the type of the argument */
@ -297,14 +297,14 @@ size_t Print::printf(const char *argList, ...)
case 'D': case 'D':
num_s32 = va_arg(argp, int); num_s32 = va_arg(argp, int);
print(num_s32, 10); print(num_s32, 10);
break; break;
case 'u': case 'u':
case 'U': /* Argument type is of integer, hence read 32bit unsigend data */ case 'U': /* Argument type is of integer, hence read 32bit unsigend data */
num_u32 = va_arg(argp, uint32_t); num_u32 = va_arg(argp, uint32_t);
print(num_u32, 10); print(num_u32, 10);
break; break;
@ -312,21 +312,21 @@ size_t Print::printf(const char *argList, ...)
case 'x': case 'x':
case 'X': /* Argument type is of hex, hence hexadecimal data from the argp */ case 'X': /* Argument type is of hex, hence hexadecimal data from the argp */
num_u32 = va_arg(argp, uint32_t); num_u32 = va_arg(argp, uint32_t);
print(num_u32, 16); print(num_u32, 16);
break; break;
case 'b': case 'b':
case 'B': /* Argument type is of binary,Read int and convert to binary */ case 'B': /* Argument type is of binary,Read int and convert to binary */
num_u32 = va_arg(argp, uint32_t); num_u32 = va_arg(argp, uint32_t);
print(num_u32, 2); print(num_u32, 2);
break; break;
case 'F': case 'F':
case 'f': /* Argument type is of float, hence read double data from the argp */ case 'f': /* Argument type is of float, hence read double data from the argp */
floatNum_f32 = va_arg(argp, double); floatNum_f32 = va_arg(argp, double);
printFloat(floatNum_f32,10); printFloat(floatNum_f32,10);
break; break;
@ -335,7 +335,7 @@ size_t Print::printf(const char *argList, ...)
case 'S': case 'S':
case 's': /* Argument type is of string, hence get the pointer to sting passed */ case 's': /* Argument type is of string, hence get the pointer to sting passed */
str = va_arg(argp, char *); str = va_arg(argp, char *);
print(str); print(str);
break; break;
@ -356,4 +356,4 @@ size_t Print::printf(const char *argList, ...)
} }
#endif #endif