Skip to content

Commit

Permalink
- Fix STM32 support and add support for Raspberry Pi Pico and other …
Browse files Browse the repository at this point in the history
…rp2040 boards

    - Add support for custom SERCOM interface of Arduino SAMD devices. Force SDA SCL to use GPIO numeration for STM32 bug (https://www.mischianti.org/forums/topic/compatible-with-stm32duino/).
    - Force SDA SCL to use GPIO numeration (https://www.mischianti.org/forums/topic/cannot-set-sda-clk-on-esp8266/).
    - Fix the SDA SCL type #58 and add basic support for SAMD device.
  • Loading branch information
xreef committed Feb 16, 2023
1 parent a300ac8 commit 5b1a7ee
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 62 deletions.
140 changes: 105 additions & 35 deletions PCF8591.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "PCF8591.h"
#include "Wire.h"


/**
* Constructor
* @param address: i2c address
Expand All @@ -37,61 +38,130 @@ PCF8591::PCF8591(uint8_t address){

_address = address;
};
#if !defined(__AVR) && !defined(__STM32F1__)

#if !defined(__AVR) && !defined(ARDUINO_ARCH_SAMD) && !defined(TEENSYDUINO)
/**
*
* Constructor
* @param address: i2c address
* @param sda: sda pin
* @param scl: scl pin
*/
PCF8591::PCF8591(uint8_t address, uint8_t sda, uint8_t scl){
PCF8591::PCF8591(uint8_t address, int sda, int scl){
_wire = &Wire;

_address = address;
_sda = sda;
_scl = scl;
};

#ifdef ESP32
/**
* Constructor
* @param address: i2c address
*/
PCF8591::PCF8591(TwoWire *pWire, uint8_t address){
_wire = pWire;

_address = address;
};
/**
*
* @param address: i2c address
* @param sda: sda pin
* @param scl: scl pin
*/
PCF8591::PCF8591(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl){
_wire = pWire;

_address = address;
_sda = sda;
_scl = scl;
};
#endif

#endif
#if defined(ESP32) || defined(ARDUINO_ARCH_SAMD)|| defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32)
/**
* Constructor
* @param address: i2c address
*/
PCF8591::PCF8591(TwoWire *pWire, uint8_t address){
_wire = pWire;

_address = address;
};

#endif
#if defined(ESP32)
/**
* Constructor
* @param address: i2c address
* @param sda: sda pin
* @param scl: scl pin
*/
PCF8591::PCF8591(TwoWire *pWire, uint8_t address, int sda, int scl){
_wire = pWire;

_address = address;
_sda = sda;
_scl = scl;
};

#endif




///**
// * Constructor
// * @param address: i2c address
// */
//PCF8591::PCF8591(uint8_t address){
// _wire = &Wire;
//
// _address = address;
//};
//#if !defined(__AVR) && !defined(__STM32F1__)
// /**
// *
// * @param address: i2c address
// * @param sda: sda pin
// * @param scl: scl pin
// */
// PCF8591::PCF8591(uint8_t address, uint8_t sda, uint8_t scl){
// _wire = &Wire;
//
// _address = address;
// _sda = sda;
// _scl = scl;
// };
//
// #ifdef ESP32
// /**
// * Constructor
// * @param address: i2c address
// */
// PCF8591::PCF8591(TwoWire *pWire, uint8_t address){
// _wire = pWire;
//
// _address = address;
// };
// /**
// *
// * @param address: i2c address
// * @param sda: sda pin
// * @param scl: scl pin
// */
// PCF8591::PCF8591(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl){
// _wire = pWire;
//
// _address = address;
// _sda = sda;
// _scl = scl;
// };
//
// #endif
//
//#endif

/**
* wake up i2c controller
*/
void PCF8591::begin(){
#ifndef __AVR
_wire->begin(_sda, _scl);
#else
// Default pin for AVR some problem on software emulation
// #define SCL_PIN _scl
// #define SDA_PIN _sda
_wire->begin();
#endif
#if !defined(__AVR) && !defined(ARDUINO_ARCH_SAMD) && !defined(TEENSYDUINO)
DEBUG_PRINT(F("begin(sda, scl) -> "));DEBUG_PRINT(_sda);DEBUG_PRINT(F(" "));DEBUG_PRINTLN(_scl);
// _wire->begin(_sda, _scl);
#ifdef ARDUINO_ARCH_STM32
_wire->begin((uint32_t)_sda, (uint32_t)_scl);
#elif defined(ARDUINO_ARCH_RP2040)
_wire->setSCL(_scl);
_wire->setSDA(_sda);
_wire->begin();
#else
_wire->begin((int)_sda, (int)_scl);
#endif
#else
// Default pin for AVR some problem on software emulation
// #define SCL_PIN _scl
// #define SDA_PIN _sda
_wire->begin();
#endif
}

/**
Expand Down
62 changes: 39 additions & 23 deletions PCF8591.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,27 @@

#include <math.h>

#define AIN0 B00000000
#define AIN1 B00000001
#define AIN2 B00000010
#define AIN3 B00000011
#define AIN0 0b00000000
#define AIN1 0b00000001
#define AIN2 0b00000010
#define AIN3 0b00000011

#define CHANNEL0 B00000000
#define CHANNEL1 B00000001
#define CHANNEL2 B00000010
#define CHANNEL3 B00000011
#define CHANNEL0 0b00000000
#define CHANNEL1 0b00000001
#define CHANNEL2 0b00000010
#define CHANNEL3 0b00000011

#define AUTOINCREMENT_READ B00000100
#define AUTOINCREMENT_READ 0b00000100

#define SINGLE_ENDED_INPUT B00000000
#define TREE_DIFFERENTIAL_INPUT B00010000
#define TWO_SINGLE_ONE_DIFFERENTIAL_INPUT B00100000
#define TWO_DIFFERENTIAL_INPUT B00110000
#define SINGLE_ENDED_INPUT 0b00000000
#define TREE_DIFFERENTIAL_INPUT 0b00010000
#define TWO_SINGLE_ONE_DIFFERENTIAL_INPUT 0b00100000
#define TWO_DIFFERENTIAL_INPUT 0b00110000

#define ENABLE_OUTPUT B01000000
#define DISABLE_OUTPUT B01000000
#define ENABLE_OUTPUT 0b01000000
#define DISABLE_OUTPUT 0b01000000

#define OUTPUT_MASK B01000000
#define OUTPUT_MASK 0b01000000

class PCF8591 {
public:
Expand All @@ -86,16 +86,32 @@ class PCF8591 {

PCF8591(uint8_t address);

#if !defined(__AVR) && !defined(__STM32F1__)
PCF8591(uint8_t address, uint8_t sda, uint8_t scl);

#ifdef ESP32
PCF8591(TwoWire *pWire, uint8_t address);
PCF8591(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl);
#endif
#if !defined(__AVR) && !defined(ARDUINO_ARCH_SAMD) && !defined(TEENSYDUINO)
PCF8591(uint8_t address, int sda, int scl);
#endif

#if defined(ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32)
///// changes for second i2c bus
PCF8591(TwoWire *pWire, uint8_t address);
#endif
#if defined(ESP32)
PCF8591(TwoWire *pWire, uint8_t address, int sda, int scl);
#endif



// PCF8591(uint8_t address);
//
//#if !defined(__AVR) && !defined(__STM32F1__)
// PCF8591(uint8_t address, uint8_t sda, uint8_t scl);
//
// #ifdef ESP32
// PCF8591(TwoWire *pWire, uint8_t address);
// PCF8591(TwoWire *pWire, uint8_t address, uint8_t sda, uint8_t scl);
// #endif
//
//#endif

void begin(void);
struct AnalogInput analogReadAll(byte readType = SINGLE_ENDED_INPUT);
uint8_t analogRead(uint8_t channel, byte readType = SINGLE_ENDED_INPUT);
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ You can find updated version of documentation on my site​ [PCF8591](https://ww

Library to use i2c analog IC with arduino and esp8266. Can read analog value and write analog value with only 2 wire (perfect for ESP-01).

06/04/2022: v1.0.2 Fix package size
- 16/02/2023: v1.1.0
- Fix STM32 support and add support for Raspberry Pi Pico and other rp2040 boards
- Add support for custom SERCOM interface of Arduino SAMD devices. Force SDA SCL to use GPIO numeration for STM32 bug (https://www.mischianti.org/forums/topic/compatible-with-stm32duino/).
- Force SDA SCL to use GPIO numeration (https://www.mischianti.org/forums/topic/cannot-set-sda-clk-on-esp8266/).
- Fix the SDA SCL type #58 and add basic support for SAMD device.
- 06/04/2022: v1.0.2 Fix package size

Tutorial:

Expand Down
24 changes: 24 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "PCF8591 library",
"version": "1.1.0",
"keywords": "analog, digital, i2c, encoder, expander, pcf8591, pcf8591a, esp32, esp8266, stm32, SAMD, Arduino, wire, Raspberry, rp2040",
"description": "PCF8591 library. i2c digital expander for i2c digital expander for Arduino, Raspberry Pi Pico and rp2040 boards, esp32, SMT32 and ESP8266. Can read write digital values with only 2 wire. Very simple to use and encoder support.",
"homepage": "https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/",
"authors":
[
{
"name": "Renzo Mischianti",
"email": "renzo.mischianti@gmail.com",
"maintainer": true,
"url": "https://www.mischianti.org"
}
],
"repository": {
"type": "git",
"url": "https://github.com/xreef/PCF8591_library"
},
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
"headers": ["PCF8591.h, PCF8591_library.h"]
}
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=PCF8591 library
version=1.0.2
version=1.1.0
author=Renzo Mischianti <renzo.mischianti@gmail.com>
maintainer=Renzo Mischianti <renzo.mischianti@gmail.com>
sentence=PCF8591, library for Arduino, ESP8266 and esp32
paragraph=Library to use pcf8591 i2c analog IC with Arduino, esp32 and esp8266. Can read analog value and write analog value with only 2 wire.
sentence=PCF8591, library for Arduino, Raspberry Pi Pico and rp2040 boards, esp32, SMT32 and ESP8266.
paragraph=Library to use pcf8591 i2c analog IC with Arduino, Raspberry Pi Pico and rp2040 boards, esp32, SMT32 and ESP8266. Can read analog value and write analog value with only 2 wire.
category=Signal Input/Output
url=https://www.mischianti.org/2019/01/03/pcf8591-i2c-analog-i-o-expander/
repository=https://github.com/xreef/PCF8591_library
Expand Down

0 comments on commit 5b1a7ee

Please sign in to comment.