initial release
Diff: Components/STMPE1600/stmpe1600_class.h
- Revision:
- 14:8320b5ff96fa
- Parent:
- 9:367d1f390cb2
- Child:
- 15:44e6c9013bff
--- a/Components/STMPE1600/stmpe1600_class.h Thu Jun 15 13:33:35 2017 +0000 +++ b/Components/STMPE1600/stmpe1600_class.h Mon Aug 07 14:30:21 2017 +0000 @@ -39,17 +39,17 @@ /* Includes ------------------------------------------------------------------*/ #include "DevI2C.h" -#define STMPE1600_DEF_DEVICE_ADDRESS (uint8_t)0x42*2 +#define STMPE1600_DEF_DEVICE_ADDRESS (uint8_t)0x42*2 #define STMPE1600_DEF_DIGIOUT_LVL 1 /** STMPE1600 registr map **/ -#define ChipID_0_7 (uint8_t)0x00 -#define ChipID_8_15 (uint8_t)0x01 -#define VersionId (uint8_t)0x02 -#define SYS_CTRL (uint8_t)0x03 -#define IEGPIOR_0_7 (uint8_t)0x08 +#define CHIP_ID_0_7 (uint8_t)0x00 +#define CHIP_ID_8_15 (uint8_t)0x01 +#define VERSION_ID (uint8_t)0x02 +#define SYS_CTRL (uint8_t)0x03 +#define IEGPIOR_0_7 (uint8_t)0x08 #define IEGPIOR_8_15 (uint8_t)0x09 -#define ISGPIOR_0_7 (uint8_t)0x0A +#define ISGPIOR_0_7 (uint8_t)0x0A #define ISGPIOR_8_15 (uint8_t)0x0B #define GPMR_0_7 (uint8_t)0x10 #define GPMR_8_15 (uint8_t)0x11 @@ -60,250 +60,270 @@ #define GPIR_0_7 (uint8_t)0x16 #define GPIR_8_15 (uint8_t)0x17 -#define SOFT_RESET (uint8_t)0x80 +#define SOFT_RESET (uint8_t)0x80 - typedef enum { +typedef enum { // GPIO Expander pin names - GPIO_0=0, + GPIO_0 = 0, GPIO_1, GPIO_2, - GPIO_3, + GPIO_3, GPIO_4, GPIO_5, GPIO_6, - GPIO_7, + GPIO_7, GPIO_8, GPIO_9, GPIO_10, - GPIO_11, + GPIO_11, GPIO_12, GPIO_13, GPIO_14, GPIO_15, - NOT_CON -} ExpGpioPinName; + NOT_CON +} ExpGpioPinName; typedef enum { INPUT = 0, OUTPUT, NOT_CONNECTED -}ExpGpioPinDirection; +} ExpGpioPinDirection; /* Classes -------------------------------------------------------------------*/ /** Class representing a single stmpe1600 GPIO expander output pin */ -class STMPE1600DigiOut { - - public: +class Stmpe1600DigiOut +{ + +public: /** Constructor * @param[in] &i2c device I2C to be used for communication * @param[in] outpinname the desired out pin name to be created - * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS) - * @param[in] lvl the default ot pin level - */ - STMPE1600DigiOut (DevI2C &i2c, ExpGpioPinName outpinname, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS, bool lvl=STMPE1600_DEF_DIGIOUT_LVL): dev_i2c(i2c), expdevaddr(DevAddr), exppinname(outpinname) + * @param[in] DevAddr the stmpe1600 I2C device address (deft STMPE1600_DEF_DEVICE_ADDRESS) + * @param[in] lvl the default ot pin level + */ + Stmpe1600DigiOut(DevI2C &i2c, ExpGpioPinName out_pin_name, uint8_t dev_addr = STMPE1600_DEF_DEVICE_ADDRESS, + bool lvl = STMPE1600_DEF_DIGIOUT_LVL) : dev_i2c(i2c), exp_dev_addr(dev_addr), exp_pin_name(out_pin_name) { - uint8_t data[2]; - if (exppinname == NOT_CON) return; - /* set the exppinname as output */ - dev_i2c.i2c_read(data, expdevaddr, GPDR_0_7, 1); - dev_i2c.i2c_read(&data[1], expdevaddr, GPDR_8_15, 1); - *(uint16_t*)data = *(uint16_t*)data | (1<<(uint16_t)exppinname); // set gpio as out - dev_i2c.i2c_write(data, expdevaddr, GPDR_0_7, 1); - dev_i2c.i2c_write(&data[1], expdevaddr, GPDR_8_15, 1); - write(lvl); - } + uint8_t data[2]; - /** - * @brief Write on the out pin - * @param[in] lvl level to write - * @return 0 on Success - */ - void write (int lvl) - { - uint8_t data[2]; - if (exppinname == NOT_CON) return; - /* set the exppinname state to lvl */ - dev_i2c.i2c_read(data, expdevaddr, GPSR_0_7, 2); - *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(~(1<<(uint16_t)exppinname)); // set pin mask - if (lvl) *(uint16_t*)data = *(uint16_t*)data | (uint16_t)(1<<(uint16_t)exppinname); - dev_i2c.i2c_write(data, expdevaddr, GPSR_0_7, 2); + if (exp_pin_name == NOT_CON) + return; + /* set the exp_pin_name as output */ + dev_i2c.i2c_read(data, exp_dev_addr, GPDR_0_7, 1); + dev_i2c.i2c_read(&data[1], exp_dev_addr, GPDR_8_15, 1); + * (uint16_t *) data = * (uint16_t *) data | (1 << (uint16_t) exp_pin_name); // set gpio as out + dev_i2c.i2c_write(data, exp_dev_addr, GPDR_0_7, 1); + dev_i2c.i2c_write(&data[1], exp_dev_addr, GPDR_8_15, 1); + write(lvl); } - /** - * @brief Overload assignement operator - */ - STMPE1600DigiOut& operator=(int lvl) + /** + * @brief Write on the out pin + * @param[in] lvl level to write + * @return 0 on Success + */ + void write(int lvl) { - write (lvl); - return *this; - } - - private: - DevI2C &dev_i2c; - uint8_t expdevaddr; - ExpGpioPinName exppinname; + uint8_t data[2]; + + if (exp_pin_name == NOT_CON) + return; + /* set the exp_pin_name state to lvl */ + dev_i2c.i2c_read(data, exp_dev_addr, GPSR_0_7, 2); + * (uint16_t *) data = * (uint16_t *) data & (uint16_t)(~(1 << (uint16_t) exp_pin_name)); // set pin mask + if (lvl) + * (uint16_t *) data = * (uint16_t *) data | (uint16_t)(1 << (uint16_t) exp_pin_name); + dev_i2c.i2c_write(data, exp_dev_addr, GPSR_0_7, 2); + } + + /** + * @brief Overload assignement operator + */ + Stmpe1600DigiOut &operator= (int lvl) + { + write(lvl); + return *this; + } + +private: + DevI2C &dev_i2c; + uint8_t exp_dev_addr; + ExpGpioPinName exp_pin_name; }; /* Classes -------------------------------------------------------------------*/ /** Class representing a single stmpe1600 GPIO expander input pin */ -class STMPE1600DigiIn -{ - public: - /** Constructor - * @param[in] &i2c device I2C to be used for communication - * @param[in] inpinname the desired input pin name to be created - * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS) - */ - STMPE1600DigiIn (DevI2C &i2c, ExpGpioPinName inpinname, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS): dev_i2c(i2c), expdevaddr(DevAddr), exppinname(inpinname) +class Stmpe1600DigiIn +{ +public: + /** Constructor + * @param[in] &i2c device I2C to be used for communication + * @param[in] inpinname the desired input pin name to be created + * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS) + */ + Stmpe1600DigiIn(DevI2C &i2c, ExpGpioPinName in_pin_name, + uint8_t dev_addr = STMPE1600_DEF_DEVICE_ADDRESS) : dev_i2c(i2c), exp_dev_addr(dev_addr), + exp_pin_name(in_pin_name) { - uint8_t data[2]; - if (exppinname == NOT_CON) return; - /* set the exppinname as input pin direction */ - dev_i2c.i2c_read(data, expdevaddr, GPDR_0_7, 2); - *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(~(1<<(uint16_t)exppinname)); // set gpio as in - dev_i2c.i2c_write(data, expdevaddr, GPDR_0_7, 2); - } + uint8_t data[2]; + + if (exp_pin_name == NOT_CON) + return; + /* set the exp_pin_name as input pin direction */ + dev_i2c.i2c_read(data, exp_dev_addr, GPDR_0_7, 2); + * (uint16_t *) data = * (uint16_t *) data & (uint16_t)(~(1 << (uint16_t) exp_pin_name)); // set gpio as in + dev_i2c.i2c_write(data, exp_dev_addr, GPDR_0_7, 2); + } - /** - * @brief Read the input pin - * @return The pin logical state 0 or 1 - */ - bool read () + /** + * @brief Read the input pin + * @return The pin logical state 0 or 1 + */ + bool read() { - uint8_t data[2]; - if (exppinname == NOT_CON) return 0; - /* read the exppinname */ - dev_i2c.i2c_read(data, expdevaddr, GPMR_0_7, 2); - *(uint16_t*)data = *(uint16_t*)data & (uint16_t)(1<<(uint16_t)exppinname); // mask the in gpio - if (data[0] || data[1]) return 1; - return 0; + uint8_t data[2]; + + if (exp_pin_name == NOT_CON) + return false; + /* read the exp_pin_name */ + dev_i2c.i2c_read(data, exp_dev_addr, GPMR_0_7, 2); + * (uint16_t *) data = * (uint16_t *) data & (uint16_t)(1 << (uint16_t) exp_pin_name); // mask the in gpio + if (data[0] || data[1]) + return true; + return false; } - - operator int() - { - return read(); - } - - private: - DevI2C &dev_i2c; - uint8_t expdevaddr; - ExpGpioPinName exppinname; + + operator int() + { + return read(); + } + +private: + DevI2C &dev_i2c; + uint8_t exp_dev_addr; + ExpGpioPinName exp_pin_name; }; /* Classes -------------------------------------------------------------------*/ /** Class representing a whole stmpe1600 component (16 gpio) */ -class STMPE1600 { - - public: +class Stmpe1600 +{ + +public: /** Constructor * @param[in] &i2c device I2C to be used for communication * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS) - */ - STMPE1600 (DevI2C &i2c, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS ) : dev_i2c(i2c) - { - dev_i2c = i2c; - expdevaddr = DevAddr; - writeSYS_CTRL (SOFT_RESET); - - GPDR0_15 = (uint16_t)0; // gpio dir all IN - write16bitReg (GPDR_0_7, &GPDR0_15); - GPSR0_15 = (uint16_t)0x0ffff; // gpio status all 1 - write16bitReg (GPSR_0_7, &GPSR0_15); + */ + Stmpe1600(DevI2C &i2c, uint8_t dev_addr = STMPE1600_DEF_DEVICE_ADDRESS) : dev_i2c(i2c) + { + dev_i2c = i2c; + exp_dev_addr = dev_addr; + write_sys_ctrl(SOFT_RESET); + + gpdr0_15 = (uint16_t) 0; // gpio dir all IN + write_16bit_reg(GPDR_0_7, &gpdr0_15); + gpsr0_15 = (uint16_t) 0x0ffff; // gpio status all 1 + write_16bit_reg(GPSR_0_7, &gpsr0_15); + } + + /** + * @brief Write the SYS_CTRL register + * @param[in] Data to be written (bit fields) + */ + void write_sys_ctrl(uint8_t data) // data = SOFT_RESET reset the device + { + dev_i2c.i2c_write(&data, exp_dev_addr, SYS_CTRL, 1); + } + + /** + * @brief Set the out pin + * @param[in] The pin name + * @return 0 on Success + */ + bool set_gpio(ExpGpioPinName pin_name) + { + if (pin_name == NOT_CON) + return true; + gpsr0_15 = gpsr0_15 | ((uint16_t) 0x0001 << pin_name); + write_16bit_reg(GPSR_0_7, &gpsr0_15); + return false; + } + + /** + * @brief Clear the out pin + * @param[in] The pin name + * @return 0 on Success + */ + bool clear_gpio(ExpGpioPinName pin_name) + { + if (pin_name == NOT_CON) + return true; + gpsr0_15 = gpsr0_15 & (~((uint16_t) 0x0001 << pin_name)); + write_16bit_reg(GPSR_0_7, &gpsr0_15); + return false; } - /** - * @brief Write the SYS_CTRL register - * @param[in] Data to be written (bit fields) - */ - void writeSYS_CTRL (uint8_t data) // data = SOFT_RESET reset the device + /** + * @brief Read the input pin + * @param[in] The pin name + * @return The logical pin level + */ + bool read_gpio(ExpGpioPinName pin_name) { - dev_i2c.i2c_write(&data, expdevaddr, SYS_CTRL, 1); + uint16_t gpmr0_15; + if (pin_name == NOT_CON) + return true; + read_16bit_reg(GPMR_0_7, &gpmr0_15); + gpmr0_15 = gpmr0_15 & ((uint16_t) 0x0001 << pin_name); + if (gpmr0_15) + return true; + return false; } - - /** - * @brief Set the out pin - * @param[in] The pin name - * @return 0 on Success - */ - bool setGPIO (ExpGpioPinName PinName) - { - if (PinName == NOT_CON) return true; - GPSR0_15 = GPSR0_15 | ((uint16_t)0x0001<<PinName); - write16bitReg (GPSR_0_7 , &GPSR0_15); - return false; - } - - /** - * @brief Clear the out pin - * @param[in] The pin name - * @return 0 on Success - */ - bool clrGPIO (ExpGpioPinName PinName) - { - if (PinName == NOT_CON) return true; - GPSR0_15 = GPSR0_15 & (~((uint16_t)0x0001<<PinName)); - write16bitReg (GPSR_0_7 , &GPSR0_15); - return false; - } - - /** - * @brief Read the input pin - * @param[in] The pin name - * @return The logical pin level - */ - bool rdGPIO (ExpGpioPinName PinName) - { - uint16_t gpmr0_15; - if (PinName == NOT_CON) return true; - read16bitReg (GPMR_0_7, &gpmr0_15); - gpmr0_15 = gpmr0_15 & ((uint16_t)0x0001<<PinName); - if (gpmr0_15) return true; - return false; - } - - /** - * @brief Set the pin direction - * @param[in] The pin name - * @param[in] The pin direction - * @return 0 on success - */ - bool setGPIOdir (ExpGpioPinName PinName, ExpGpioPinDirection PinDir) - { - if (PinName == NOT_CON || PinDir == NOT_CONNECTED) return true; - GPDR0_15 = GPDR0_15 & (~((uint16_t)0x0001<<PinName)); // clear the Pin - GPDR0_15 = GPDR0_15 | ((uint16_t)PinDir<<PinName); - write16bitReg (GPDR_0_7 , &GPDR0_15); - return false; - } + + /** + * @brief Set the pin direction + * @param[in] The pin name + * @param[in] The pin direction + * @return 0 on success + */ + bool set_gpio_dir(ExpGpioPinName pin_name, ExpGpioPinDirection pin_dir) + { + if (pin_name == NOT_CON || pin_dir == NOT_CONNECTED) + return true; + gpdr0_15 = gpdr0_15 & (~((uint16_t) 0x0001 << pin_name)); // clear the Pin + gpdr0_15 = gpdr0_15 | ((uint16_t) pin_dir << pin_name); + write_16bit_reg(GPDR_0_7, &gpdr0_15); + return false; + } - /** - * @brief Read a 16 bits register - * @param[in] The register address - * @param[in] The pointer to the read data - */ - void read16bitReg (uint8_t Reg16Addr, uint16_t *Reg16Data) + /** + * @brief Read a 16 bits register + * @param[in] The register address + * @param[in] The pointer to the read data + */ + void read_16bit_reg(uint8_t reg16_addr, uint16_t *reg16_data) { - dev_i2c.i2c_read((uint8_t*)Reg16Data, expdevaddr, Reg16Addr, 2); - } + dev_i2c.i2c_read((uint8_t *) reg16_data, exp_dev_addr, reg16_addr, 2); + } - /** - * @brief Write a 16 bits register - * @param[in] The register address - * @param[in] The pointer to the data to be written - */ - void write16bitReg (uint8_t Reg16Addr, uint16_t *Reg16Data) + /** + * @brief Write a 16 bits register + * @param[in] The register address + * @param[in] The pointer to the data to be written + */ + void write_16bit_reg(uint8_t reg16_addr, uint16_t *reg16_data) { - dev_i2c.i2c_write((uint8_t*)Reg16Data, expdevaddr, Reg16Addr, 2); - } + dev_i2c.i2c_write((uint8_t *) reg16_data, exp_dev_addr, reg16_addr, 2); + } - private: - DevI2C &dev_i2c; - uint16_t GPDR0_15; // local copy of bit direction reg - uint16_t GPSR0_15; // local copy of bit status reg - uint8_t expdevaddr; // expander device i2c addr +private: + DevI2C &dev_i2c; + uint16_t gpdr0_15; // local copy of bit direction reg + uint16_t gpsr0_15; // local copy of bit status reg + uint8_t exp_dev_addr; // expander device i2c addr }; #endif // __STMPE1600_CLASS