diff --git a/examples/X_NUCLEO_53L0A1_Gesture_DirSwipe/X_NUCLEO_53L0A1_Gesture_DirSwipe.ino b/examples/X_NUCLEO_53L0A1_Gesture_DirSwipe/X_NUCLEO_53L0A1_Gesture_DirSwipe.ino index 7fc664f..a158ff7 100644 --- a/examples/X_NUCLEO_53L0A1_Gesture_DirSwipe/X_NUCLEO_53L0A1_Gesture_DirSwipe.ino +++ b/examples/X_NUCLEO_53L0A1_Gesture_DirSwipe/X_NUCLEO_53L0A1_Gesture_DirSwipe.ino @@ -52,9 +52,25 @@ #include #include +#ifdef ARDUINO_SAM_DUE +#define DEV_I2C Wire1 +#elif defined(ARDUINO_ARCH_STM32) #define DEV_I2C Wire +#else +#define DEV_I2C Wire +#endif #define SerialPort Serial +//For SAM compatibility where D8 and D2 are undefined +#ifndef D8 +#define D8 8 +#endif + + +#ifndef D2 +#define D2 2 +#endif + // Components. STMPE1600DigiOut *xshutdown_top; STMPE1600DigiOut *xshutdown_left; @@ -140,6 +156,26 @@ void setup() { // Initialize serial for output. SerialPort.begin(115200); +//NOTE: workaround in order to unblock the I2C bus on the Arduino Due +#ifdef ARDUINO_SAM_DUE + pinMode(71, OUTPUT); + pinMode(70, OUTPUT); + + for (int i = 0; i<10; i++){ + digitalWrite(70, LOW); + delay(3); + digitalWrite(71, HIGH); + delay(3); + digitalWrite(70, HIGH); + delay(3); + digitalWrite(71, LOW); + delay(3); + } + pinMode(70, INPUT); + pinMode(71, INPUT); +#endif +//End of workaround + // Initialize I2C bus. DEV_I2C.begin(); DEV_I2C.setClock(400000); @@ -190,7 +226,6 @@ void setup() { void loop() { int gesture_code; - int status; sensor_vl53l0x_left->StartMeasurement(); sensor_vl53l0x_right->StartMeasurement(); diff --git a/examples/X_NUCLEO_53L0A1_Gesture_Swipe1/X_NUCLEO_53L0A1_Gesture_Swipe1.ino b/examples/X_NUCLEO_53L0A1_Gesture_Swipe1/X_NUCLEO_53L0A1_Gesture_Swipe1.ino index 2ffb0ca..1b98b14 100644 --- a/examples/X_NUCLEO_53L0A1_Gesture_Swipe1/X_NUCLEO_53L0A1_Gesture_Swipe1.ino +++ b/examples/X_NUCLEO_53L0A1_Gesture_Swipe1/X_NUCLEO_53L0A1_Gesture_Swipe1.ino @@ -52,9 +52,25 @@ #include #include +#ifdef ARDUINO_SAM_DUE +#define DEV_I2C Wire1 +#elif defined(ARDUINO_ARCH_STM32) #define DEV_I2C Wire +#else +#define DEV_I2C Wire +#endif #define SerialPort Serial +//For SAM compatibility where D8 and D2 are undefined +#ifndef D8 +#define D8 8 +#endif + + +#ifndef D2 +#define D2 2 +#endif + // Components. STMPE1600DigiOut *xshutdown_top; STMPE1600DigiOut *xshutdown_left; @@ -115,6 +131,26 @@ void setup() { // Initialize serial for output. SerialPort.begin(115200); +//NOTE: workaround in order to unblock the I2C bus on the Arduino Due +#ifdef ARDUINO_SAM_DUE + pinMode(71, OUTPUT); + pinMode(70, OUTPUT); + + for (int i = 0; i<10; i++){ + digitalWrite(70, LOW); + delay(3); + digitalWrite(71, HIGH); + delay(3); + digitalWrite(70, HIGH); + delay(3); + digitalWrite(71, LOW); + delay(3); + } + pinMode(70, INPUT); + pinMode(71, INPUT); +#endif +//End of workaround + // Initialize I2C bus. DEV_I2C.begin(); @@ -157,7 +193,6 @@ void setup() { void loop() { int gesture_code; - int status; sensor_vl53l0x_top->StartMeasurement(); @@ -214,5 +249,3 @@ void loop() { break; } } - - diff --git a/examples/X_NUCLEO_53L0A1_Gesture_Tap1/X_NUCLEO_53L0A1_Gesture_Tap1.ino b/examples/X_NUCLEO_53L0A1_Gesture_Tap1/X_NUCLEO_53L0A1_Gesture_Tap1.ino index e84dd32..d438c6c 100644 --- a/examples/X_NUCLEO_53L0A1_Gesture_Tap1/X_NUCLEO_53L0A1_Gesture_Tap1.ino +++ b/examples/X_NUCLEO_53L0A1_Gesture_Tap1/X_NUCLEO_53L0A1_Gesture_Tap1.ino @@ -52,9 +52,25 @@ #include #include +#ifdef ARDUINO_SAM_DUE +#define DEV_I2C Wire1 +#elif defined(ARDUINO_ARCH_STM32) #define DEV_I2C Wire +#else +#define DEV_I2C Wire +#endif #define SerialPort Serial +//For SAM compatibility where D8 and D2 are undefined +#ifndef D8 +#define D8 8 +#endif + + +#ifndef D2 +#define D2 2 +#endif + // Components. STMPE1600DigiOut *xshutdown_top; STMPE1600DigiOut *xshutdown_left; @@ -115,6 +131,26 @@ void setup() { // Initialize serial for output. SerialPort.begin(115200); +//NOTE: workaround in order to unblock the I2C bus on the Arduino Due +#ifdef ARDUINO_SAM_DUE + pinMode(71, OUTPUT); + pinMode(70, OUTPUT); + + for (int i = 0; i<10; i++){ + digitalWrite(70, LOW); + delay(3); + digitalWrite(71, HIGH); + delay(3); + digitalWrite(70, HIGH); + delay(3); + digitalWrite(71, LOW); + delay(3); + } + pinMode(70, INPUT); + pinMode(71, INPUT); +#endif +//End of workaround + // Initialize I2C bus. DEV_I2C.begin(); @@ -157,7 +193,6 @@ void setup() { void loop() { int gesture_code; - int status; sensor_vl53l0x_top->StartMeasurement(); @@ -214,4 +249,3 @@ void loop() { break; } } - diff --git a/examples/X_NUCLEO_53L0A1_HelloWorld/X_NUCLEO_53L0A1_HelloWorld.ino b/examples/X_NUCLEO_53L0A1_HelloWorld/X_NUCLEO_53L0A1_HelloWorld.ino index 456ef56..a995aa4 100644 --- a/examples/X_NUCLEO_53L0A1_HelloWorld/X_NUCLEO_53L0A1_HelloWorld.ino +++ b/examples/X_NUCLEO_53L0A1_HelloWorld/X_NUCLEO_53L0A1_HelloWorld.ino @@ -50,9 +50,25 @@ #include #include +#ifdef ARDUINO_SAM_DUE +#define DEV_I2C Wire1 +#elif defined(ARDUINO_ARCH_STM32) #define DEV_I2C Wire +#else +#define DEV_I2C Wire +#endif #define SerialPort Serial +//For SAM compatibility where D8 and D2 are undefined +#ifndef D8 +#define D8 8 +#endif + + +#ifndef D2 +#define D2 2 +#endif + // Components. STMPE1600DigiOut *xshutdown_top; STMPE1600DigiOut *xshutdown_left; @@ -71,6 +87,26 @@ void setup() { // Initialize serial for output. SerialPort.begin(115200); +//NOTE: workaround in order to unblock the I2C bus on the Arduino Due +#ifdef ARDUINO_SAM_DUE + pinMode(71, OUTPUT); + pinMode(70, OUTPUT); + + for (int i = 0; i<10; i++){ + digitalWrite(70, LOW); + delay(3); + digitalWrite(71, HIGH); + delay(3); + digitalWrite(70, HIGH); + delay(3); + digitalWrite(71, LOW); + delay(3); + } + pinMode(70, INPUT); + pinMode(71, INPUT); +#endif +//End of workaround + // Initialize I2C bus. DEV_I2C.begin(); @@ -159,4 +195,3 @@ void loop() { SerialPort.println(report); } } - diff --git a/library.properties b/library.properties index 7282cef..269fafd 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=STM32duino X-NUCLEO-53L0A1 -version=1.0.1 +version=1.0.2 author=AST maintainer=stm32duino sentence=Allows controlling the VL53L0X sensors on board of X-NUCLEO-53L0A1 paragraph=This library provides simple measure distance in mm, single swipe gesture detection, directional (left/right) swipe gesture detection and single tap gesture detection. category=Device Control url=https://github.com/stm32duino/X-NUCLEO-53L0A1 -architectures=stm32 +architectures=stm32, sam diff --git a/src/stmpe1600_class.h b/src/stmpe1600_class.h index face6ba..c52b1e0 100644 --- a/src/stmpe1600_class.h +++ b/src/stmpe1600_class.h @@ -39,7 +39,7 @@ /* Includes ------------------------------------------------------------------*/ #include -#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 **/ @@ -60,111 +60,120 @@ #define GPIR_0_7 (uint8_t)0x16 #define GPIR_8_15 (uint8_t)0x17 -#define SOFT_RESET (uint8_t)0x80 - -typedef enum { - // GPIO Expander pin names - GPIO_0=0, - GPIO_1, - GPIO_2, - GPIO_3, - GPIO_4, - GPIO_5, - GPIO_6, - GPIO_7, - GPIO_8, - GPIO_9, - GPIO_10, - GPIO_11, - GPIO_12, - GPIO_13, - GPIO_14, - GPIO_15, - NOT_CON -} ExpGpioPinName; - -typedef enum { - EXP_GPIO_INPUT = 0, - EXP_GPIO_OUTPUT, - EXP_GPIO_NOT_CONNECTED + +typedef enum +{ + // GPIO Expander pin names + GPIO_0=0, + GPIO_1, + GPIO_2, + GPIO_3, + GPIO_4, + GPIO_5, + GPIO_6, + GPIO_7, + GPIO_8, + GPIO_9, + GPIO_10, + GPIO_11, + GPIO_12, + GPIO_13, + GPIO_14, + GPIO_15, + NOT_CON +} ExpGpioPinName; + +typedef enum +{ + EXP_GPIO_INPUT = 0, + EXP_GPIO_OUTPUT, + EXP_GPIO_NOT_CONNECTED } ExpGpioPinDirection; /* Classes -------------------------------------------------------------------*/ /** Class representing a single stmpe1600 GPIO expander output pin */ -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 (TwoWire *i2c, ExpGpioPinName outpinname, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS, bool lvl=STMPE1600_DEF_DIGIOUT_LVL): dev_i2c(i2c), expdevaddr(DevAddr), exppinname(outpinname) - { - uint8_t data[2]; +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 (TwoWire *i2c, ExpGpioPinName outpinname, uint8_t DevAddr=STMPE1600_DEF_DEVICE_ADDRESS, bool lvl=STMPE1600_DEF_DIGIOUT_LVL): dev_i2c(i2c), expdevaddr(DevAddr), exppinname(outpinname) + { + bit16Tobit8_t data; if (exppinname == NOT_CON) return; /* set the exppinname as output */ - STMPE1600DigiOut_I2CRead(data, expdevaddr, GPDR_0_7, 1); - STMPE1600DigiOut_I2CRead(&data[1], expdevaddr, GPDR_8_15, 1); - *(uint16_t*)data = *(uint16_t*)data | (1<<(uint16_t)exppinname); // set gpio as out - STMPE1600DigiOut_I2CWrite(data, expdevaddr, GPDR_0_7, 1); - STMPE1600DigiOut_I2CWrite(&data[1], expdevaddr, GPDR_8_15, 1); + STMPE1600DigiOut_I2CRead(&data.u8bit[0], expdevaddr, GPDR_0_7, 1); + STMPE1600DigiOut_I2CRead(&data.u8bit[1], expdevaddr, GPDR_8_15, 1); + data.u16bit = data.u16bit | (1<<(uint16_t)exppinname); // set gpio as out + STMPE1600DigiOut_I2CWrite(&data.u8bit[0], expdevaddr, GPDR_0_7, 1); + STMPE1600DigiOut_I2CWrite(&data.u8bit[1], expdevaddr, GPDR_8_15, 1); write(lvl); - } - - /** - * @brief Write on the out pin - * @param[in] lvl level to write - * @return 0 on Success - */ - void write (int 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) + { + bit16Tobit8_t data; if (exppinname == NOT_CON) return; /* set the exppinname state to lvl */ - STMPE1600DigiOut_I2CRead(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); - STMPE1600DigiOut_I2CWrite(data, expdevaddr, GPSR_0_7, 2); - } - - private: - TwoWire *dev_i2c; - uint8_t expdevaddr; - ExpGpioPinName exppinname; - - int STMPE1600DigiOut_I2CWrite(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, uint16_t NumByteToWrite) - { + STMPE1600DigiOut_I2CRead(data.u8bit, expdevaddr, GPSR_0_7, 2); + data.u16bit = data.u16bit & (uint16_t)(~(1<<(uint16_t)exppinname)); // set pin mask + if (lvl) data.u16bit = data.u16bit | (uint16_t)(1<<(uint16_t)exppinname); + STMPE1600DigiOut_I2CWrite(data.u8bit, expdevaddr, GPSR_0_7, 2); + } + +private: + TwoWire *dev_i2c; + uint8_t expdevaddr; + ExpGpioPinName exppinname; + + int STMPE1600DigiOut_I2CWrite(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, uint16_t NumByteToWrite) + { dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F))); - + dev_i2c->write(RegisterAddr); - for (int i = 0 ; i < NumByteToWrite ; i++) - dev_i2c->write(pBuffer[i]); - + for (uint16_t i = 0 ; i < NumByteToWrite ; i++) + dev_i2c->write(pBuffer[i]); + dev_i2c->endTransmission(true); - + return 0; - } + } - int STMPE1600DigiOut_I2CRead(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, uint16_t NumByteToRead) - { + int STMPE1600DigiOut_I2CRead(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, uint16_t NumByteToRead) + { dev_i2c->beginTransmission(((uint8_t)(((DeviceAddr) >> 1) & 0x7F))); dev_i2c->write(RegisterAddr); dev_i2c->endTransmission(false); - + dev_i2c->requestFrom(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)), (byte) NumByteToRead); int i=0; while (dev_i2c->available()) { - pBuffer[i] = dev_i2c->read(); - i++; + pBuffer[i] = dev_i2c->read(); + i++; } - + return 0; - } + } + + typedef union + { + uint16_t u16bit; + uint8_t u8bit[2]; + } bit16Tobit8_t; + }; #endif // __STMPE1600_CLASS