Introduction
In this blog I shall use the CY8CKIT-028-TFT display shield. I will add the code to talk to the LCD display and the accelerometer. The display is mainly to show me useful debugging information. The accelerometer will serve to provide gesture movement dataset in real-time.
There are many good example programs done by Infineon (Official information found on Infineon Github). I have adapted much of the code from them:
- https://github.com/Infineon/CY8CKIT-028-TFT
- https://github.com/Infineon/display-tft-st7789v#quick-start
- https://github.com/Infineon/sensor-motion-bmi160
Interfacing with LCD Library
Right click on the project inside Project Explorer > ModusToolbox > Library Manager
Here we will be enabling the low-level library for the display, and a high-level emwin middleware which is a powerful library for creating GUI.
Turn on these libraries:
- Board Utils > CY8CKIT-028-TFT
- PSoC 6 Middleware > emwin
At the bottom, we can also see the changes that are about to be made. In this case, the tool automatically a few extra libraries for us. This is because the shield, in addition to the TFT LCD screen, also incorporates a motion sensor, ambient light sensor and PDM microphone.
Once I'm done with my choices, choose Update at the bottom right. ModusToolbox will process our changes. Click Close once it is successful
In the Makefile, this to COMPONENTS. It is to indicate we are using a Non-TouchScreen.
COMPONENTS+=EMWIN_NOSNTS
At the top of my main file, I also added a hello world code.
#include "cy_tft.h" // Library for CY8CKIT-028-TFT
#include "GUI.h" // Library for emWin
/* Init the display */
cy_tft_io_init();
GUI_Init();
GUI_SetFont(GUI_FONT_32B_1);
GUI_DispString("Hello World");
To make it look more professional, I created a bitmap in Paint.
Bitmap |
---|
And I generated a bitmap using the emWin generator.
Up until this point, please see my Github diffs. It shows what changes I have made to the code
Interfacing with the Motion Sensor
As previously, we have added the library for the display shield (Board Utils > CY8CKIT-028-TFT), it has also included the BMI160 library for the motion sensor which is on the same shield.
In the main.c file, include the library.
#include "mtb_bmi160.h" // Library for Motion Sensor
As for the motion sensor, as it is a lot more complicated, I created a new RTOS task to initialise it. Importantly, the BMI160 library depends on some system delays, thus initialising must be done in an RTOS thread after the scheduler has started.
I first initialise the I2C hardware pins, and then initialise the sensor library
/* Initialize i2c for motion sensor */
result = cyhal_i2c_init(&i2c, CY8CKIT_028_TFT_PIN_IMU_I2C_SDA, CY8CKIT_028_TFT_PIN_IMU_I2C_SCL, NULL);
/* Initialize motion sensor */
mtb_bmi160_t motion_sensor;
result = mtb_bmi160_init_i2c(&motion_sensor, &i2c, MTB_BMI160_DEFAULT_ADDRESS);
In a continuous loop, I can get the data at a fixed timing. (In this case every 100ms)
mtb_bmi160_read(&motion_sensor, &data);
vTaskDelay(100);
// Get the readings from the `data` struct:
// data.accel.x, data.accel.y, data.accel.z,
// data.gyro.x, data.gyro.y, data.gyro.z
For the full code, please see my Github commit for the changes:
Solving a conflict with Motion Sensor I2C
Unfortunately, there is a conflict with the I2C communication which randomly causes the processor to go into a halt (or crash) after reading the sensor data.
After investigation, it appears that the CapSense tuning functions use the EzI2C HAL initialization library (https://www.cypress.com/documentation/component-datasheets/ezi2c-slave) which conflicts with the standard CyHAL I2C functions.
(ie. both libraries should be used independently and cannot be used at the same time)
As a workaround, I decided to just comment out the tuning functions. I'm not sure what exactly the effects of tuning are, but CapSense seems to work fine without tuning too.
The changes are documented in this commit:
After doing those changes, the sensor works reliably!
In the next blog...
I will integrate the accelerometer to an data forwarder for Edge Impulse. I plan to use Edge Impulse to do inference of accelerometer data to detect gestures using TinyML to characterise the data for gesture control.