Skip to content

Adding functionality and ESP32-S3 support #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3cd74e1
Added ability to set gain
Mirageofmage Nov 20, 2022
c9de707
Fixed union structure size assignments, analogRead
Mirageofmage Dec 3, 2022
e9efdbf
Wrong file in git commit?
Mirageofmage Dec 3, 2022
da8f8d9
Print to serial only in debug mode
Mirageofmage Dec 9, 2022
ea76dd5
Merge branch 'main' into 370
Mirageofmage Dec 9, 2022
d15d4bc
Added Boost Functionality
Mirageofmage Jan 16, 2023
f2c412e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 16, 2023
9e56cbd
bump version
nerdyscout Oct 10, 2023
ea96690
add R
nerdyscout Oct 10, 2023
b2d7bc7
move code
nerdyscout Oct 10, 2023
4c2f696
add tests
nerdyscout Oct 10, 2023
20c064e
[pre-commit.ci] pre-commit autoupdate (#6)
pre-commit-ci[bot] Oct 10, 2023
df9e059
fix cpplint things
nerdyscout Oct 10, 2023
8d20f96
use enum instead of strongly txped enum classes
nerdyscout Oct 10, 2023
d13c262
fix
nerdyscout Oct 10, 2023
22d9c51
adding pcbs
nerdyscout Oct 10, 2023
02ea10c
add examples
nerdyscout Oct 15, 2023
229ef96
test runs on hardware
nerdyscout Oct 15, 2023
db73e4e
fix uno board settings
nerdyscout Oct 15, 2023
a9c7599
make PSPI address and speed changeable from outside of header file
nerdyscout Oct 15, 2023
19a45ad
wip
nerdyscout Nov 1, 2023
6173f11
add (test): setup regression tests on real hardware
nerdyscout Nov 2, 2023
4538bd0
refactor(settings): make settings its own class
nerdyscout Nov 5, 2023
27c2ec5
default settings
nerdyscout Nov 28, 2023
a4d5a4b
Reintroduce interrupt management
Mirageofmage Dec 27, 2023
6062f55
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 27, 2023
bb6f975
Merge commit '27c2ec5dab48bdf627a1fd1746bbecc70a4f000c' into 370-develop
Mirageofmage Jan 23, 2024
3c60b2a
Pull develop branch into 370
Mirageofmage Jan 24, 2024
26bb585
Merge branch 'main' into 370
Mirageofmage Jan 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions lib/MCP3x6x/MCP3x6x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,10 @@ void MCP3x6x::_reverse_array(uint8_t *array, size_t size) {

MCP3x6x::status_t MCP3x6x::_transfer(uint8_t *data, uint8_t addr, size_t size) {
_spi->beginTransaction(SPISettings(MCP3x6x_SPI_SPEED, MCP3x6x_SPI_ORDER, MCP3x6x_SPI_MODE));
noInterrupts();
digitalWrite(_pinCS, LOW);
_status.raw = _spi->transfer(addr);
_spi->transfer(data, size);
digitalWrite(_pinCS, HIGH);
interrupts();
_spi->endTransaction();
return _status;
}
Expand Down Expand Up @@ -297,21 +295,18 @@ uint8_t MCP3x6x::_getChannel(uint32_t raw) {
int32_t MCP3x6x::analogRead(mux_t ch) {
// MuxMode
if (settings.scan.channel.raw == 0) {
#ifdef MCP3x6x_DEBUG
Serial.println("mux");
#endif
//Serial.println("mux");
settings.mux = ch;
_status = write(settings.mux);
_status = conversion();
while (!_status.dr) {
_status = read(&adcdata);
}
// while (!_status.dr) {
// _status = read(&adcdata);
// }
_status = read(&adcdata);
return result.raw[(uint8_t)adcdata.channelid] = adcdata.value;
}

#ifdef MCP3x6x_DEBUG
Serial.println("scan");
#endif
// ScanMode
for (size_t i = 0; i < sizeof(_channelID); i++) {
if (_channelID[i] == ch.raw) {
Expand Down Expand Up @@ -379,6 +374,16 @@ void MCP3x6x::setAveraging(osr rate) {
_status = write(settings.config1);
}

void MCP3x6x::setGain(gain gain) {
settings.config2.gain = gain;
_status = write(settings.config2);
}

void MCP3x6x::setBoost(boost boost) {
settings.config2.boost = boost;
_status = write(settings.config2);
}

int32_t MCP3x6x::analogReadContinuous(mux_t ch) {
if (isContinuous()) {
for (size_t i = 0; i < sizeof(_channelID); i++) {
Expand Down
44 changes: 29 additions & 15 deletions lib/MCP3x6x/MCP3x6x.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ class MCP3x6x {
typedef union {
struct {
struct __attribute__((__packed__)) {
bool por; //!< status: power on reset
bool crccfg; //!< status: crc
bool dr; //!< status: data ready
bool por : 1; //!< status: power on reset
bool crccfg: 1; //!< status: crc
bool dr : 1; //!< status: data ready
};
uint8_t : 1; //!< !addr[0]
uint8_t addr : 2; //!< addresse
Expand Down Expand Up @@ -321,7 +321,7 @@ class MCP3x6x {
* @brief configuration register 0
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1576710>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1576710>MCP346x.pdf</a>
*/
typedef union Config0 {
struct {
Expand All @@ -338,7 +338,7 @@ class MCP3x6x {
* @brief configuration register 1
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269089>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269089>MCP346x.pdf</a>
*/
typedef union config1 {
struct {
Expand All @@ -353,7 +353,7 @@ class MCP3x6x {
* @brief configuration register 2
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269283>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269283>MCP346x.pdf</a>
*/
typedef union Config2 {
struct {
Expand All @@ -369,7 +369,7 @@ class MCP3x6x {
* @brief configuration register 3
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269504>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269504>MCP346x.pdf</a>
*/
typedef union Config3 {
struct {
Expand All @@ -387,7 +387,7 @@ class MCP3x6x {
* @brief interrupt request register
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269747>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1269747>MCP346x.pdf</a>
*/
typedef union Irq {
struct {
Expand All @@ -406,7 +406,7 @@ class MCP3x6x {
* @brief multiplexer register
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1273028>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1273028>MCP346x.pdf</a>
*/
typedef union Mux {
/**
Expand All @@ -426,7 +426,7 @@ class MCP3x6x {
* @brief scan mode settings register
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270252>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270252>MCP346x.pdf</a>
*/
typedef union Scan {
struct {
Expand All @@ -452,7 +452,7 @@ class MCP3x6x {
* @brief timer delay value register
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270583>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270583>MCP346x.pdf</a>
*/
typedef union Timer {
uint8_t raw[3]; //!< Selection Bits for the Time Interval Between Two Consecutive Scan Cycles
Expand All @@ -462,7 +462,7 @@ class MCP3x6x {
* @brief offset calibration register
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270742>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270742>MCP346x.pdf</a>
*/
typedef union Offset {
uint8_t raw[3]; //!< Offset Error Digital Calibration Code (two’s complement, MSb first coding)
Expand All @@ -472,7 +472,7 @@ class MCP3x6x {
* @brief gain calibration register
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270900>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1270900>MCP346x.pdf</a>
*/
typedef union Gain {
uint8_t raw[3]; //!< Gain Error Digital Calibration Code (unsigned, MSb first coding)
Expand All @@ -482,7 +482,7 @@ class MCP3x6x {
* @brief SPI write mode locking password value register
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1271641>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1271641>MCP346x.pdf</a>
*/
typedef union Lock {
uint8_t raw; //!< Write Access Password Entry Code
Expand All @@ -492,7 +492,7 @@ class MCP3x6x {
* @brief crc configuration register
*
* <a
* href=https://ww1.microchip.com/downloads/aemDocuments/documents/APID/ProductDocuments/DataSheets/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1272118>MCP346x.pdf</a>
* href=https://nerdyscout.github.io/Arduino_MCP3x6x_Library/MCP3461-2-4R-Family-Data-Sheet-DS20006404C.pdf#G1.1272118>MCP346x.pdf</a>
*/
typedef union Crccfg {
uint8_t raw[2]; //!< CRC-16 Checksum Value
Expand Down Expand Up @@ -978,6 +978,20 @@ class MCP3x6x {
*/
void setAveraging(osr rate);

/**
* @brief set gain
*
* @param gain
*/
void setGain(gain gain);

/**
* @brief set gain
*
* @param gain
*/
void setBoost(boost boost);

/**
* @brief set resolution to specific amount of bits
*
Expand Down