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

142 lines
3.8 KiB
C
Raw Permalink Normal View History

2022-01-10 06:52:28 -05:00
#pragma once
//
// FILE: MCP23S08.h
// AUTHOR: Rob Tillaart
2024-07-06 05:18:29 -04:00
// VERSION: 0.5.3
2022-01-10 06:52:28 -05:00
// PURPOSE: Arduino library for SPI MCP23S08 8 channel port expander
// DATE: 2022-01-10
// URL: https://github.com/RobTillaart/MCP23S08
#include "Arduino.h"
#include "SPI.h"
2024-05-27 04:24:27 -04:00
#include "MCP23x08_registers.h"
2022-01-10 06:52:28 -05:00
2024-07-06 05:18:29 -04:00
#define MCP23S08_LIB_VERSION (F("0.5.3"))
2022-01-10 06:52:28 -05:00
2023-08-21 08:31:38 -04:00
// ERROR CODES
2022-01-10 06:52:28 -05:00
#define MCP23S08_OK 0x00
#define MCP23S08_PIN_ERROR 0x81
#define MCP23S08_SPI_ERROR 0x82
#define MCP23S08_VALUE_ERROR 0x83
#define MCP23S08_PORT_ERROR 0x84
2022-09-28 07:59:32 -04:00
#define MCP23S08_REGISTER_ERROR 0xFF
2024-05-27 04:24:27 -04:00
#define MCP23S08_INVALID_READ 0xFF
2022-01-10 06:52:28 -05:00
2023-12-01 11:16:25 -05:00
#ifndef __SPI_CLASS__
2024-05-27 04:24:27 -04:00
// MBED must be tested before RP2040
#if defined(ARDUINO_ARCH_MBED)
#define __SPI_CLASS__ SPIClass
#elif defined(ARDUINO_ARCH_RP2040)
2023-12-01 11:16:25 -05:00
#define __SPI_CLASS__ SPIClassRP2040
#else
#define __SPI_CLASS__ SPIClass
#endif
#endif
2023-08-21 08:31:38 -04:00
const uint32_t MCP23S08_TYP_SPI_SPEED = 8000000;
const uint32_t MCP23S08_MAX_SPI_SPEED = 10000000;
2022-01-10 06:52:28 -05:00
class MCP23S08
{
public:
2024-05-27 04:24:27 -04:00
// SOFTWARE SPI
2022-01-10 06:52:28 -05:00
MCP23S08(uint8_t select, uint8_t dataIn, uint8_t dataOut, uint8_t clock, uint8_t address = 0x00);
2024-05-27 04:24:27 -04:00
// HARDWARE SPI
2024-03-05 05:25:14 -05:00
MCP23S08(int select, __SPI_CLASS__* spi);
MCP23S08(int select, int address = 0x00, __SPI_CLASS__* spi = &SPI);
2022-01-10 06:52:28 -05:00
2024-03-05 05:25:14 -05:00
bool begin(bool pullup = true);
2023-08-21 08:31:38 -04:00
bool isConnected();
uint8_t getAddress();
2022-01-10 06:52:28 -05:00
2024-07-06 05:18:29 -04:00
2024-05-27 04:24:27 -04:00
// single pin interface
2024-07-06 05:18:29 -04:00
// mode = INPUT, OUTPUT, INPUT_PULLUP (= same as INPUT)
// do not use 0, 1 for mode.
2023-12-24 08:55:15 -05:00
bool pinMode1(uint8_t pin, uint8_t mode);
bool write1(uint8_t pin, uint8_t value);
uint8_t read1(uint8_t pin);
2022-01-10 06:52:28 -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);
2024-05-27 04:24:27 -04:00
// 8 pins interface
2024-07-06 05:18:29 -04:00
// mask = 0x00..0xFF bit pattern,
// bit 0 = output mode, bit 1 = input mode
// value = bit pattern.
bool pinMode8(uint8_t mask);
2022-01-10 06:52:28 -05:00
bool write8(uint8_t value);
int read8();
bool setPolarity8(uint8_t mask);
bool getPolarity8(uint8_t &mask);
bool setPullup8(uint8_t mask);
bool getPullup8(uint8_t &mask);
2024-05-27 04:24:27 -04:00
// INTERRUPTS (experimental)
// pin = 0..7, mode = { RISING, FALLING, CHANGE }
bool enableInterrupt(uint8_t pin, uint8_t mode);
bool disableInterrupt(uint8_t pin);
// which pins caused the INT?
uint8_t getInterruptFlagRegister();
uint8_t getInterruptCaptureRegister();
// polarity: 0 = LOW, 1 = HIGH, 2 = NONE/ODR
bool setInterruptPolarity(uint8_t polarity);
uint8_t getInterruptPolarity();
// SPI
2022-01-10 06:52:28 -05:00
// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed() { return _SPIspeed; };
2022-09-28 07:59:32 -04:00
// debugging
2022-01-10 06:52:28 -05:00
bool usesHWSPI() { return _hwSPI; };
int lastError();
2024-05-27 04:24:27 -04:00
// set/clear IOCR bit fields
bool enableControlRegister(uint8_t mask);
bool disableControlRegister(uint8_t mask);
2023-08-21 08:31:38 -04:00
// 0.2.0 experimental
2024-05-27 04:24:27 -04:00
bool enableHardwareAddress();
bool disableHardwareAddress();
2023-08-21 08:31:38 -04:00
2024-05-27 04:24:27 -04:00
protected:
// access to low level registers (just make these functions public).
2023-08-21 08:31:38 -04:00
// USE WITH CARE !!!
2022-01-10 06:52:28 -05:00
bool writeReg(uint8_t reg, uint8_t value);
uint8_t readReg(uint8_t reg);
uint8_t _address = 0;
uint8_t _select = 0;
uint8_t _dataOut = 0;
uint8_t _dataIn = 0;
uint8_t _clock = 0;
uint8_t _error = MCP23S08_OK;
2024-05-27 04:24:27 -04:00
bool _hwSPI = true;
2023-08-21 08:31:38 -04:00
2024-05-27 04:24:27 -04:00
// 10 MHz is maximum, 8 is a better clock divider on AVR.
uint32_t _SPIspeed = MCP23S08_TYP_SPI_SPEED;
2023-12-01 11:16:25 -05:00
__SPI_CLASS__ * _mySPI;
2024-05-27 04:24:27 -04:00
SPISettings _spi_settings;
2022-01-10 06:52:28 -05:00
uint8_t swSPI_transfer(uint8_t val);
};
2023-08-21 08:31:38 -04:00
// -- END OF FILE --
2022-01-10 06:52:28 -05:00