Creating a project about VL6180XA1 for TT_Mxx

Committer:
ThunderSoft
Date:
Fri Apr 26 09:34:53 2019 +0000
Revision:
3:7a97a01bad5e
Parent:
0:293917667c17
"update the mbed-os code to support TT_M4G9"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ThunderSoft 0:293917667c17 1 #ifndef STMPE1600_H
ThunderSoft 0:293917667c17 2 #define STMPE1600_H
ThunderSoft 0:293917667c17 3
ThunderSoft 0:293917667c17 4 #include "mbed.h"
ThunderSoft 0:293917667c17 5
ThunderSoft 0:293917667c17 6 #define STMPE1600_DEF_DEVICE_ADDRESS (uint8_t)0x42*2
ThunderSoft 0:293917667c17 7 #define STMPE1600_DEF_DIGIOUT_LVL 1
ThunderSoft 0:293917667c17 8
ThunderSoft 0:293917667c17 9 /** STMPE1600 registr map **/
ThunderSoft 0:293917667c17 10 #define ChipID_0_7 (uint8_t)0x00
ThunderSoft 0:293917667c17 11 #define ChipID_8_15 (uint8_t)0x01
ThunderSoft 0:293917667c17 12 #define VersionId (uint8_t)0x02
ThunderSoft 0:293917667c17 13 #define SYS_CTRL (uint8_t)0x03
ThunderSoft 0:293917667c17 14 #define IEGPIOR_0_7 (uint8_t)0x08
ThunderSoft 0:293917667c17 15 #define IEGPIOR_8_15 (uint8_t)0x09
ThunderSoft 0:293917667c17 16 #define ISGPIOR_0_7 (uint8_t)0x0A
ThunderSoft 0:293917667c17 17 #define ISGPIOR_8_15 (uint8_t)0x0B
ThunderSoft 0:293917667c17 18 #define GPMR_0_7 (uint8_t)0x10
ThunderSoft 0:293917667c17 19 #define GPMR_8_15 (uint8_t)0x11
ThunderSoft 0:293917667c17 20 #define GPSR_0_7 (uint8_t)0x12
ThunderSoft 0:293917667c17 21 #define GPSR_8_15 (uint8_t)0x13
ThunderSoft 0:293917667c17 22 #define GPDR_0_7 (uint8_t)0x14
ThunderSoft 0:293917667c17 23 #define GPDR_8_15 (uint8_t)0x15
ThunderSoft 0:293917667c17 24 #define GPIR_0_7 (uint8_t)0x16
ThunderSoft 0:293917667c17 25 #define GPIR_8_15 (uint8_t)0x17
ThunderSoft 0:293917667c17 26
ThunderSoft 0:293917667c17 27 #define SOFT_RESET (uint8_t)0x80
ThunderSoft 0:293917667c17 28
ThunderSoft 0:293917667c17 29
ThunderSoft 0:293917667c17 30 #define STMPE1600_DEFAULT_ID 0x01
ThunderSoft 0:293917667c17 31
ThunderSoft 0:293917667c17 32 typedef enum {
ThunderSoft 0:293917667c17 33 // GPIO Expander pin names
ThunderSoft 0:293917667c17 34 GPIO_0= 0,
ThunderSoft 0:293917667c17 35 GPIO_1 ,
ThunderSoft 0:293917667c17 36 GPIO_2 ,
ThunderSoft 0:293917667c17 37 GPIO_3 ,
ThunderSoft 0:293917667c17 38 GPIO_4 ,
ThunderSoft 0:293917667c17 39 GPIO_5 ,
ThunderSoft 0:293917667c17 40 GPIO_6 ,
ThunderSoft 0:293917667c17 41 GPIO_7 ,
ThunderSoft 0:293917667c17 42 GPIO_8 ,
ThunderSoft 0:293917667c17 43 GPIO_9 ,
ThunderSoft 0:293917667c17 44 GPIO_10 ,
ThunderSoft 0:293917667c17 45 GPIO_11 ,
ThunderSoft 0:293917667c17 46 GPIO_12 ,
ThunderSoft 0:293917667c17 47 GPIO_13 ,
ThunderSoft 0:293917667c17 48 GPIO_14 ,
ThunderSoft 0:293917667c17 49 GPIO_15 ,
ThunderSoft 0:293917667c17 50 NOT_CON
ThunderSoft 0:293917667c17 51 } exp_gpio_pin_name;
ThunderSoft 0:293917667c17 52
ThunderSoft 0:293917667c17 53 typedef enum {
ThunderSoft 0:293917667c17 54 INPUT = 0,
ThunderSoft 0:293917667c17 55 OUTPUT,
ThunderSoft 0:293917667c17 56 NOT_CONNECTED
ThunderSoft 0:293917667c17 57 } exp_gpio_pin_direction;
ThunderSoft 0:293917667c17 58
ThunderSoft 0:293917667c17 59
ThunderSoft 0:293917667c17 60 class STMPE1600 {
ThunderSoft 0:293917667c17 61 public:
ThunderSoft 0:293917667c17 62 /** Constructor
ThunderSoft 0:293917667c17 63 * @param[in] &i2c device I2C to be used for communication
ThunderSoft 0:293917667c17 64 * @param[in] DevAddr the stmpe1600 I2C device addres (deft STMPE1600_DEF_DEVICE_ADDRESS)
ThunderSoft 0:293917667c17 65 */
ThunderSoft 0:293917667c17 66 STMPE1600 (I2C *i2c, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS );
ThunderSoft 0:293917667c17 67
ThunderSoft 0:293917667c17 68 /**
ThunderSoft 0:293917667c17 69 * @brief Write the SYS_CTRL register
ThunderSoft 0:293917667c17 70 * @param[in] Data to be written (bit fields)
ThunderSoft 0:293917667c17 71 */
ThunderSoft 0:293917667c17 72 void write_sys_ctrl (uint8_t data) // data = SOFT_RESET reset the device
ThunderSoft 0:293917667c17 73 {
ThunderSoft 0:293917667c17 74 // _dev_i2c->i2c_write((uint8_t*)SYS_CTRL, _expdevaddr, data, 1);
ThunderSoft 0:293917667c17 75 write(&data,SYS_CTRL,1);
ThunderSoft 0:293917667c17 76 }
ThunderSoft 0:293917667c17 77
ThunderSoft 0:293917667c17 78 /**
ThunderSoft 0:293917667c17 79 * @brief Set the out pin
ThunderSoft 0:293917667c17 80 * @param[in] The pin name
ThunderSoft 0:293917667c17 81 * @return 0 on Success
ThunderSoft 0:293917667c17 82 */
ThunderSoft 0:293917667c17 83 bool set_gpio (exp_gpio_pin_name pin_name)
ThunderSoft 0:293917667c17 84 {
ThunderSoft 0:293917667c17 85 if (pin_name == NOT_CON) return true;
ThunderSoft 0:293917667c17 86 _gpsro_15 = _gpsro_15 | ((uint16_t)0x0001<<pin_name);
ThunderSoft 0:293917667c17 87 write_16_bit_reg (GPSR_0_7 , &_gpsro_15);
ThunderSoft 0:293917667c17 88 return false;
ThunderSoft 0:293917667c17 89 }
ThunderSoft 0:293917667c17 90
ThunderSoft 0:293917667c17 91 /**
ThunderSoft 0:293917667c17 92 * @brief Clear the out pin
ThunderSoft 0:293917667c17 93 * @param[in] The pin name
ThunderSoft 0:293917667c17 94 * @return 0 on Success
ThunderSoft 0:293917667c17 95 */
ThunderSoft 0:293917667c17 96 bool clr_gpio (exp_gpio_pin_name pin_name)
ThunderSoft 0:293917667c17 97 {
ThunderSoft 0:293917667c17 98 if (pin_name == NOT_CON) return true;
ThunderSoft 0:293917667c17 99 _gpsro_15 = _gpsro_15 & (~((uint16_t)0x0001<<pin_name));
ThunderSoft 0:293917667c17 100 write_16_bit_reg (GPSR_0_7 , &_gpsro_15);
ThunderSoft 0:293917667c17 101 return false;
ThunderSoft 0:293917667c17 102 }
ThunderSoft 0:293917667c17 103
ThunderSoft 0:293917667c17 104 /**
ThunderSoft 0:293917667c17 105 * @brief Read the input pin
ThunderSoft 0:293917667c17 106 * @param[in] The pin name
ThunderSoft 0:293917667c17 107 * @return The logical pin level
ThunderSoft 0:293917667c17 108 */
ThunderSoft 0:293917667c17 109 bool rd_gpio (exp_gpio_pin_name pin_name)
ThunderSoft 0:293917667c17 110 {
ThunderSoft 0:293917667c17 111 uint16_t gpmr0_15;
ThunderSoft 0:293917667c17 112 if (pin_name == NOT_CON) return true;
ThunderSoft 0:293917667c17 113 read_16_bit_reg (GPMR_0_7, &gpmr0_15);
ThunderSoft 0:293917667c17 114 gpmr0_15 = gpmr0_15 & ((uint16_t)0x0001<<pin_name);
ThunderSoft 0:293917667c17 115 if (gpmr0_15) {
ThunderSoft 0:293917667c17 116 return true;
ThunderSoft 0:293917667c17 117 }
ThunderSoft 0:293917667c17 118 return false;
ThunderSoft 0:293917667c17 119 }
ThunderSoft 0:293917667c17 120
ThunderSoft 0:293917667c17 121 /**
ThunderSoft 0:293917667c17 122 * @brief Set the pin direction
ThunderSoft 0:293917667c17 123 * @param[in] The pin name
ThunderSoft 0:293917667c17 124 * @param[in] The pin direction
ThunderSoft 0:293917667c17 125 * @return 0 on success
ThunderSoft 0:293917667c17 126 */
ThunderSoft 0:293917667c17 127 bool set_gpio_dir (exp_gpio_pin_name pin_name, exp_gpio_pin_direction pin_dir)
ThunderSoft 0:293917667c17 128 {
ThunderSoft 0:293917667c17 129 if (pin_name == NOT_CON || pin_dir == NOT_CONNECTED) {
ThunderSoft 0:293917667c17 130 return true;
ThunderSoft 0:293917667c17 131 }
ThunderSoft 0:293917667c17 132 _gpdro_15 = _gpdro_15 & (~((uint16_t)0x0001<<pin_name)); // clear the Pin
ThunderSoft 0:293917667c17 133 _gpdro_15 = _gpdro_15 | ((uint16_t)pin_dir<<pin_name);
ThunderSoft 0:293917667c17 134 write_16_bit_reg (GPDR_0_7 , &_gpdro_15);
ThunderSoft 0:293917667c17 135 return false;
ThunderSoft 0:293917667c17 136 }
ThunderSoft 0:293917667c17 137
ThunderSoft 0:293917667c17 138 /**
ThunderSoft 0:293917667c17 139 * @brief Read a 16 bits register
ThunderSoft 0:293917667c17 140 * @param[in] The register address
ThunderSoft 0:293917667c17 141 * @param[in] The pointer to the read data
ThunderSoft 0:293917667c17 142 */
ThunderSoft 0:293917667c17 143 void read_16_bit_reg (uint8_t reg_16_addr, uint16_t *reg_16_data)
ThunderSoft 0:293917667c17 144 {
ThunderSoft 0:293917667c17 145 uint8_t data[2];
ThunderSoft 0:293917667c17 146 read(data,reg_16_addr,2);
ThunderSoft 0:293917667c17 147 *reg_16_data = data[1] << 8 | data[0];
ThunderSoft 0:293917667c17 148 }
ThunderSoft 0:293917667c17 149
ThunderSoft 0:293917667c17 150 /**
ThunderSoft 0:293917667c17 151 * @brief Write a 16 bits register
ThunderSoft 0:293917667c17 152 * @param[in] The register address
ThunderSoft 0:293917667c17 153 * @param[in] The pointer to the data to be written
ThunderSoft 0:293917667c17 154 */
ThunderSoft 0:293917667c17 155 void write_16_bit_reg (uint8_t reg_16_addr, uint16_t *reg_16_data)
ThunderSoft 0:293917667c17 156 {
ThunderSoft 0:293917667c17 157 uint8_t data[2];
ThunderSoft 0:293917667c17 158 data[1] = (*reg_16_data >> 8) & 0xff;
ThunderSoft 0:293917667c17 159 data[0] = *reg_16_data & 0xff;
ThunderSoft 0:293917667c17 160 write(data,reg_16_addr,2);
ThunderSoft 0:293917667c17 161 }
ThunderSoft 0:293917667c17 162
ThunderSoft 0:293917667c17 163 /**
ThunderSoft 0:293917667c17 164 * @brief Read STMPE1600 device id.
ThunderSoft 0:293917667c17 165 * @return device ID 0x01
ThunderSoft 0:293917667c17 166 */
ThunderSoft 0:293917667c17 167 uint8_t readID();
ThunderSoft 0:293917667c17 168
ThunderSoft 0:293917667c17 169 /**
ThunderSoft 0:293917667c17 170 * @brief Test the STMPE1600 device whether connect.
ThunderSoft 0:293917667c17 171 * @return [true] connect [false] disconnect.
ThunderSoft 0:293917667c17 172 */
ThunderSoft 0:293917667c17 173 bool testConnect();
ThunderSoft 0:293917667c17 174
ThunderSoft 0:293917667c17 175 protected:
ThunderSoft 0:293917667c17 176
ThunderSoft 0:293917667c17 177 /**
ThunderSoft 0:293917667c17 178 * @brief Init device.
ThunderSoft 0:293917667c17 179 */
ThunderSoft 0:293917667c17 180 void init();
ThunderSoft 0:293917667c17 181
ThunderSoft 0:293917667c17 182 /**
ThunderSoft 0:293917667c17 183 * @brief Read data from STMPE1600.
ThunderSoft 0:293917667c17 184 * @param[out] The pointer to the read data.
ThunderSoft 0:293917667c17 185 * @param[in] The register address.
ThunderSoft 0:293917667c17 186 * @param[in] The pointer to the data size.
ThunderSoft 0:293917667c17 187 */
ThunderSoft 0:293917667c17 188 void read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead);
ThunderSoft 0:293917667c17 189
ThunderSoft 0:293917667c17 190 /**
ThunderSoft 0:293917667c17 191 * @brief Write data to STMPE1600.
ThunderSoft 0:293917667c17 192 * @param[in] The data pointer of write.
ThunderSoft 0:293917667c17 193 * @param[in] The register address.
ThunderSoft 0:293917667c17 194 * @param[in] The pointer to the data size.
ThunderSoft 0:293917667c17 195 */
ThunderSoft 0:293917667c17 196 void write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite);
ThunderSoft 0:293917667c17 197
ThunderSoft 0:293917667c17 198 private:
ThunderSoft 0:293917667c17 199 I2C *_dev_i2c;
ThunderSoft 0:293917667c17 200 uint16_t _gpdro_15; // local copy of bit direction reg
ThunderSoft 0:293917667c17 201 uint16_t _gpsro_15; // local copy of bit status reg
ThunderSoft 0:293917667c17 202 uint8_t address; // expander device i2c addr
ThunderSoft 0:293917667c17 203 };
ThunderSoft 0:293917667c17 204
ThunderSoft 0:293917667c17 205
ThunderSoft 0:293917667c17 206
ThunderSoft 0:293917667c17 207
ThunderSoft 0:293917667c17 208 #endif