GY-63_MS5611/libraries/MCP23S17/MCP23S17.h

146 lines
3.8 KiB
C
Raw Normal View History

2022-01-07 14:23:20 -05:00
#pragma once
//
// FILE: MCP23S17.h
// AUTHOR: Rob Tillaart
2023-08-17 08:24:58 -04:00
// VERSION: 0.2.6
2022-01-07 14:23:20 -05:00
// PURPOSE: Arduino library for SPI MCP23S17 16 channel port expander
// DATE: 2021-12-30
2022-01-10 06:58:20 -05:00
// URL: https://github.com/RobTillaart/MCP23S17
2022-01-07 14:23:20 -05:00
#include "Arduino.h"
#include "SPI.h"
2022-10-24 05:13:46 -04:00
#include "MCP23S17_registers.h"
2022-01-07 14:23:20 -05:00
2023-08-14 06:56:15 -04:00
2023-08-17 08:24:58 -04:00
#define MCP23S17_LIB_VERSION (F("0.2.6"))
2022-01-07 14:23:20 -05:00
2022-10-24 05:13:46 -04:00
// ERROR CODES
2022-01-07 14:23:20 -05:00
#define MCP23S17_OK 0x00
#define MCP23S17_PIN_ERROR 0x81
#define MCP23S17_SPI_ERROR 0x82
#define MCP23S17_VALUE_ERROR 0x83
#define MCP23S17_PORT_ERROR 0x84
2022-07-01 06:10:38 -04:00
#define MCP23S17_REGISTER_ERROR 0xFF
#define MCP23S17_INVALID_READ 0xFF
const uint32_t MCP23S17_TYP_SPI_SPEED = 8000000;
const uint32_t MCP23S17_MAX_SPI_SPEED = 10000000;
2022-01-07 14:23:20 -05:00
class MCP23S17
{
public:
2023-02-04 10:29:14 -05:00
// SOFTWARE SPI
2022-01-07 14:23:20 -05:00
MCP23S17(uint8_t select, uint8_t dataIn, uint8_t dataOut, uint8_t clock, uint8_t address = 0x00);
2023-02-04 10:29:14 -05:00
// HARDWARE SPI
2022-07-01 06:10:38 -04:00
MCP23S17(uint8_t select, SPIClass* spi);
MCP23S17(uint8_t select, uint8_t address = 0x00, SPIClass* spi = &SPI);
2022-01-07 14:23:20 -05:00
2022-01-10 06:58:20 -05:00
bool begin();
2022-07-01 06:10:38 -04:00
bool isConnected();
2023-08-14 06:56:15 -04:00
uint8_t getAddress(); // default returns 0x00
2022-01-07 14:23:20 -05:00
2022-09-28 06:56:28 -04:00
// single pin interface
2023-08-14 06:56:15 -04:00
// mode: 0 = OUTPUT, 1 = INPUT, 1 = INPUT_PULLUP (==INPUT)
2022-01-10 06:58:20 -05:00
bool pinMode(uint8_t pin, uint8_t mode);
bool digitalWrite(uint8_t pin, uint8_t value);
uint8_t digitalRead(uint8_t pin);
2022-01-07 14:23:20 -05:00
2022-01-10 06:58:20 -05:00
bool setPolarity(uint8_t pin, bool reversed);
bool getPolarity(uint8_t pin, bool &reversed);
bool setPullup(uint8_t pin, bool pullup);
bool getPullup(uint8_t pin, bool &pullup);
2022-01-07 14:23:20 -05:00
2022-09-28 06:56:28 -04:00
// 8 pins interface
// port = 0..1
// value = bit pattern
2022-01-10 06:58:20 -05:00
bool pinMode8(uint8_t port, uint8_t value);
bool write8(uint8_t port, uint8_t value);
int read8(uint8_t port);
bool setPolarity8(uint8_t port, uint8_t mask);
bool getPolarity8(uint8_t port, uint8_t &mask);
bool setPullup8(uint8_t port, uint8_t mask);
bool getPullup8(uint8_t port, uint8_t &mask);
2022-09-28 06:56:28 -04:00
// 16 pins interface
// value = bit pattern
2022-01-10 06:58:20 -05:00
bool pinMode16(uint16_t value);
bool write16(uint16_t value);
uint16_t read16();
2022-01-07 14:23:20 -05:00
2022-01-10 06:58:20 -05:00
bool setPolarity16(uint16_t mask);
bool getPolarity16(uint16_t &mask);
bool setPullup16(uint16_t mask);
bool getPullup16(uint16_t &mask);
2022-01-07 14:23:20 -05:00
// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed() { return _SPIspeed; };
2022-09-28 06:56:28 -04:00
// debugging
2022-01-07 14:23:20 -05:00
bool usesHWSPI() { return _hwSPI; };
int lastError();
2022-10-24 05:13:46 -04:00
// set/clear IOCR bit fields (0.2.3 experimental)
void enableControlRegister(uint8_t mask);
void disableControlRegister(uint8_t mask);
2023-08-14 06:56:15 -04:00
// 0.2.5 experimental
void enableHardwareAddress();
void disableHardwareAddress();
// ESP32 specific
#if defined(ESP32)
void selectHSPI();
void selectVSPI();
bool usesHSPI();
bool usesVSPI();
// to overrule the ESP32s default hardware pins
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
#endif
2022-10-24 05:13:46 -04:00
2022-01-07 14:23:20 -05:00
private:
2022-10-24 05:13:46 -04:00
// access to low level registers (just make these two functions public).
// USE WITH CARE !!!
2022-01-10 06:58:20 -05:00
bool writeReg(uint8_t reg, uint8_t value);
uint8_t readReg(uint8_t reg);
2023-08-17 08:24:58 -04:00
bool writeReg16(uint8_t reg, uint16_t value);
uint16_t readReg16(uint8_t reg);
2022-01-10 06:58:20 -05:00
2022-10-24 05:13:46 -04:00
2022-01-10 06:58:20 -05:00
uint8_t _address = 0;
uint8_t _select = 0;
uint8_t _dataOut = 0;
uint8_t _dataIn = 0;
uint8_t _clock = 0;
uint8_t _error = MCP23S17_OK;
2022-01-07 14:23:20 -05:00
2022-10-24 05:13:46 -04:00
bool _hwSPI = true;
// 10 MHz is maximum, 8 is a better clock divider on AVR.
2022-07-01 06:10:38 -04:00
uint32_t _SPIspeed = MCP23S17_TYP_SPI_SPEED;
SPIClass * _mySPI;
2022-01-07 14:23:20 -05:00
SPISettings _spi_settings;
uint8_t swSPI_transfer(uint8_t val);
2023-08-14 06:56:15 -04:00
#if defined(ESP32)
bool _useHSPI = true;
#endif
2022-01-07 14:23:20 -05:00
};
2023-02-04 10:29:14 -05:00
// -- END OF FILE --
2022-01-07 14:23:20 -05:00