diff --git a/src/ads7846.c b/src/ads7846.c index f10ccef..45efb3d 100644 --- a/src/ads7846.c +++ b/src/ads7846.c @@ -19,8 +19,6 @@ static inline double ads7846RangeMap(double oldMin, double oldMax, double value, } static inline bool ads7846GetAdcBit(){ - //a new control byte can be sent while receiving data - //this is valid behavior as long as the start of the last control byte was 16 or more clock cycles ago bool bit = ads7846OutputValue & 0x8000; ads7846OutputValue <<= 1; return bit; @@ -85,11 +83,17 @@ void ads7846SetChipSelect(bool value){ } bool ads7846ExchangeBit(bool bitIn){ + //chip data out is high when off + if(ads7846ChipSelect) + return true; + if(ads7846BitsToNextControl > 0) ads7846BitsToNextControl--; if(ads7846BitsToNextControl == 0){ //check for control bit + //a new control byte can be sent while receiving data + //this is valid behavior as long as the start of the last control byte was 16 or more clock cycles ago if(bitIn){ ads7846ControlByte = 0x01; ads7846BitsToNextControl = 15; diff --git a/src/hardwareRegistersAccessors.c.h b/src/hardwareRegistersAccessors.c.h index 395a64d..c3b3bae 100644 --- a/src/hardwareRegistersAccessors.c.h +++ b/src/hardwareRegistersAccessors.c.h @@ -305,19 +305,20 @@ static inline void setSpiCont2(uint16_t value){ uint16_t startBit = 1 << (bitCount - 1); uint16_t spi2Data = registerArrayRead16(SPIDATA2); bool spiClk2Enabled = !(registerArrayRead8(PESEL) & 0x04); - bool ads7846ChipSelect = !(getPortGValue() & 0x04);//this is unproven, but having it high makes the ADS7846 not work on hardware //uint16_t oldSpi2Data = spi2Data; //the input data is shifted into the unused bits if the transfer is less than 16 bits - for(uint8_t bits = 0; bits < bitCount; bits++){ - bool newBit = true; - - if(spiClk2Enabled && ads7846ChipSelect) - newBit = ads7846ExchangeBit(spi2Data & startBit); - - //debugLog("Sent Bit:%d\n", (bool)(spi2Data & startBit)); - spi2Data <<= 1; - spi2Data |= newBit; + if(spiClk2Enabled){ + //shift in valid data + for(uint8_t bits = 0; bits < bitCount; bits++){ + bool newBit = ads7846ExchangeBit(spi2Data & startBit); + spi2Data <<= 1; + spi2Data |= newBit; + } + } + else{ + //shift in 0s + spi2Data <<= bitCount; } registerArrayWrite16(SPIDATA2, spi2Data); diff --git a/unimplementedHardware.txt b/unimplementedHardware.txt index f732f9b..a8cc473 100644 --- a/unimplementedHardware.txt +++ b/unimplementedHardware.txt @@ -42,6 +42,7 @@ ADS7846: verify chip select(no test has been done yet, put everything points to port g bit 2) need to trigger a false PENIRQ interrupt on reading certain channels electrical noise on lines(conversions probably should have +-20 added to values)(probably no need for this, it works so theres no need to use less precise values) +in the edge case that SPICLK2 is disabled while using ADS7846 and a 1 was the last bit shifted out 0s will still be shifted in Fixed: