MotorControlModuleSDFM_TMS3.../Projects/EFC_Application/UMLibrary/driver/chipset/MCP23S17.hh

196 lines
5.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* MCP23S17.h
*
* Created on: 19 июл. 2019 г.
* Author: titov
*/
#ifndef SOURCE_DRIVER_CHIPSET_MCP23S17_H_
#define SOURCE_DRIVER_CHIPSET_MCP23S17_H_
#include "PackagerMCP23S17.hpp"
#include "../SerialPortAdapter.hpp"
#include "../PinIO.hh"
#include "../PinIOOverride.hh"
#include "../PortMultiplexer.hpp"
#include "../../systemic/RegisterEncoder.hpp"
namespace driver { namespace chipset {
namespace MCP23S17 {
typedef PackagerMCP23S17 Packager;
typedef uint16_t Latency;
typedef SerialPortAdapter<PackagerMCP23S17::Frame, Latency, false> SerialPortRegister;
typedef PinIO<Packager::Data> PinIO;
typedef PinIOOverride<Packager::Data> PinIOOverride;
typedef PortMultiplexer<Packager::Data> PortMultiplexer;
//Струткура адресов регистров управления при парном доступе.
struct ControlRegistersPairedAcronims {
enum Acronims {
IODIR = 0x00, //Управление направлением порта.
IPOL = 0x02, //Конфигурация полярности регистров порта.
GPINTEN = 0x04, //Управление прерыванием по изменению для каждого пина порта.
DEFVAL = 0x06, //Значение по умолчанию для сравнения.
INTCON = 0x08, //Контроль сравнение ассоциированного пина для прерывания по изменению.
IOCON = 0x0A, //Поля настройки устройства.
GPPU = 0x0C, //Управление резисторами PULL-UP для пинов порта.
INTF = 0x0E, //Условия прерывания пинов порта.
INTCAP = 0x10, //Значение пина порта при срабатывание прерывания.
GPIO = 0x12, //Отображение состояния порта.
OLAT = 0x14, //Доступ к защелка порта.
};
};
//! Перечисление возможных адресов устройст на шине передачи данных
enum HardwareAddress
{
A0 = 0,
A1, A2, A3, A4, A5, A6, A7
};
//! Перечисление порядковых номеров пинов в регистре порта
enum PinOrderNum
{
GPA0 = 0,
GPA1, GPA2, GPA3, GPA4, GPA5, GPA6, GPA7,
GPB0, GPB1, GPB2, GPB3, GPB4, GPB5, GPB6, GPB7,
};
//! Перечисление способов адрессации
enum BANKOption
{
REGISTER_ASSOCIATED_WITH_EACH_PORT = 1, //!<Registers associated with each port are separated into different banks.
REGISTER_IN_SAME_BANK = 0, //!<The registers are in the same bank (addresses are sequential).
};
//! Перечисление состояний зеркалирования пинов.
enum MIRROROption
{
INT_PIN_INTERNALLY_CONNECTED = 1, //!<The INT pins are internally connected
INT_PIN_NOT_CONNECTED = 0, //!<The INT pins are not connected. INTA is associated with PORTA and INTB is associated with
};
//! Перечисление состояний опции последовательного доступа к регистрам.
enum SEQOPOption
{
SEQUENTIAL_OPERATION_DISABLED = 1, //!<Sequential operation disabled, address pointer does not increment
SEQUENTIAL_OPERATION_ENABLED = 0, //!<Sequential operation enabled, address pointer increments
};
//! Перечисление состояния наростания выхода SDA
enum DISSLWOption
{
SLEW_RATE_DISABLED = 1, //!<Slew rate disabled
SLEW_RATE_ENABLED = 0, //!<Slew rate enabled
};
//! Перечисление состояния пинов адрессации.
enum HAENOption
{
ENABLES_ADDRESS_PINS = 1, //!<Enables the MCP23S17 address pins.
DISABLES_ADDRESS_PINS = 0, //!<Disables the MCP23S17 address pins
};
//! Перечисление конфигураций пина прерывания
enum ODROption
{
OPEN_DRAIN_OUTPUT = 1, //!<Open-drain output (overrides the INTPOL bit.)
ACTIVE_DRIVE_OUTPUT = 0, //!<Active driver output (INTPOL bit sets the polarity.)
};
//! Перечисление активного уровня пина прерывания
enum INTPOLOption
{
ACTIVE_HIGH = 1, //!<Active-high
ACTIVE_LOW = 0, //!<Active-low
};
//! Структура регистра управления MCP23S17
struct ConfigurationContent
{
uint16_t unimplemented : 1;
INTPOLOption intpol_option : 1;
ODROption odr_option : 1;
HAENOption haen_option : 1;
DISSLWOption disslw_option : 1;
SEQOPOption seqop_option : 1;
MIRROROption mirror_option : 1;
BANKOption bank_option : 1;
uint16_t reserved_bits : 8;
};
struct ConfigurationRegister {
ConfigurationRegister() {
content.bank_option = REGISTER_IN_SAME_BANK;
content.mirror_option = INT_PIN_NOT_CONNECTED;
content.seqop_option = SEQUENTIAL_OPERATION_ENABLED;
content.disslw_option = SLEW_RATE_ENABLED;
content.haen_option = ENABLES_ADDRESS_PINS;
content.odr_option = ACTIVE_DRIVE_OUTPUT;
content.intpol_option = ACTIVE_LOW;
content.unimplemented = 0;
content.reserved_bits = 0;
}
ConfigurationContent content;
operator uint16_t() {
//todo: !!!UB!!!
union {
ConfigurationContent cfg_reg;
uint16_t serialized;
} serialized;
serialized.cfg_reg = content;
return serialized.serialized | (serialized.serialized<<8); //todo: Подыскать другое решение.
}
};
//! Структура параметров микросхемы MCP23S17
struct Parameters
{
typedef uint16_t OptionRegister;
static const unsigned int allowed_options = 7;
enum AllowedPinOptionsRegisters {
OLAT_REGISTER,
DIRECTION_REGISTER,
INPUT_POLARITY_REGISTER,
INTERRUPT_ON_CHANGE_CONTROL_REGISTER,
DEFAULT_COMPARE_REGISTER_INTERRUPT_ON_CHANGE,
INTERRUPT_CONTROL_REGISTER,
PULL_UP_RESISTOR_REGISTER,
};
OptionRegister pin_option[allowed_options];
};
enum { WRITE_REGISTER_FLAG = 0, READ_REGISTER_FLAG = 1 };
}
}}
#endif /* SOURCE_DRIVER_CHIPSET_MCP23S17_H_ */