Thursday, December 28, 2017

Cypress PSoC BLE - It's Alive! Well at least the beginning of the prototype

Now on to the fun stuff...

I will start working with the PSoC Creator IDE and the PSoC 4 BLE Pioneer Kit (CY8CKIT-042-BLE-A) .  I will not start with the blinking LED or any of the tutorials that are explained on the Cypress website.  You can find the intro videos through their website or on YouTube.  I will dig into the meat of things right away.  So to start with the fun stuff let's get the LCD working.  As I mentioned in the previous post I have a 1.44" LCD with the ILI9163 driver.  To understand what is needed to interface let's look at the details of the board.  Let's first look at the board we have and the pin configuration.


As you can see in the image we have the following pin connections:


  1. LED
  2. SCK
  3. SDA
  4. A0
  5. RESET
  6. CS
  7. GND
  8. VCC
Now we need to determine how to connect these pins to our Pioneer Kit to start working with the LCD.  We can always use a bit banging method, but for this case we will use the built in SPI functions.  According to the PSoC 4XX8 BLE 4.2 Family Datasheet there are two SCB (Serial Communication Blocks) that can be used.  These support  I2C, UART, or SPI interfaces.  In our case one of the two SCB's will use the SPI interface to interface with the LCD.  With these we have a few different options for pins to choose (see Table 4. Port Pin Connections from the datasheet).  The options are as follows:


For this project I have chosen the following pins to use for the SPI communications:

PSoC Board LCD Desciption
P1[4] SDA SPI Data (MOSI)
P1[6] CS  Chip/Slave Select
P1[7] SCK SPI Clock

*Note: The datasheet shows the pin reference with a period for example P1.4.  Once we get to the IDE you will see the pins are referenced using brackets, for example P1[4].  For the remaining parts of this blog I will match what is shown in the IDE.  

So you are probably wondering what about the fourth pin.  Each section shows four pins and I only show three.  For the LCD I have chosen there is no pin for the MISO (Master In Slave Out).  What this means is there is only one way communication.  From the master device (PSoC) to the slave device (LCD).  So now we have three of the eight pins for the LCD assigned, but what do we do for the remaining.  Below shows the other pins I will use to interface with the LCD.  You can find more information on the Pioneer Kit at the this link (CY8CKIT-042-BLE-A Bluetooth Low Energy Pioneer Kit Guide).

PSoC Board LCD Desciption
V3.3 LED LED Backlight
V3.3 VCC LCD Power
GND GND LCD Ground
P1[0] A0 Data/Command
P1[1] RESET Reset pin


For now I will connect these pins to the LCD through a breadboard and some jumper wires.  This will help give me something to physically work with an prototype.  Now that  have the connections made 
I can open up the PSoC Creator IDE and start on the software side.

Open up the PSoC Creator IDE and create a new blank project.  We will walk through adding piece by piece.  For me I drew a block to represent the LCD and connections so I could easily reference it.  Then I show the connections to the different pins/blocks.  The image below shows what I created in the IDE TopDesign.cysch.  It is not required but for me I can easily reference this for my connections.


I am going to start with the easier part which are the two pins outside of the SPI block.  They are labeled LCD_DC and LCD_Reset.  To create these you can drag the Digital Output Pin (found under the Ports and Pins) to the schematic.


Once this is dragged in we can double click on the symbol in the schematic or right click and go to "Configure".  Once you do this a window will open up.  There are three sections we will need to update.

  1. Name - Pick a name of the pin that makes sense to you (I chose LCD_Reset)
  2. HW connection - Uncheck this as we will not connect this to another block in the schematic.
  3. Initial drive state - Make this Low(0).  This is the initial state of the pin on power up.
Once you have this configure, go ahead and press the "OK" button at the bottom the window.




Now I will configure the other pin call LCD_DC.  See the screenshot below.



Before I define the pins these are connected to I will move on to another block that needs to be configured.  This is the SCB for the SPI communications to the LCD.  Under the component catalog tree find the "Communications" section, then find the "SPI" section and you should find a component labeled SPI (SCB mode).  There will be a version number listed after that which may or may not match depending on your version of IDE.  See image below for the communication block that was added to my schematic.


Once we have this added go ahead and open up the configuration.  If it is not already on the "Configuration" tab go ahead and click this.  You should have the radio button marked for SPI and feel free to rename to what you would like.  This name will be used in the API's when we start the coding so don't make it too complex. 


  
Once you have finished with the "Configuration" tab block on the "SPI Basic" tab.  This is where it get's a little complicated if you are not familiar with SPI communications.  I will not go through all of this but you should understand SPI communications when configuring.  Here is a link to the Wikipdia site for SPI Communications.  For our case we are using 8-bit transmission and the SPI mode = 0 (CPHA = 0 & CPOL = 0).  The chip select (or slave select) is active low which means when you pull this pin low the slave is active.  Then recall from before that this is a 3-wire SPI communications so we do not require the MISO pin connection.  Most of this information can be taken from the ILI9163 datasheet.  Along with this I needed to understand the PCB I have that came with the LCD.  Looking at the datasheet you can see there are several models such as SPI & parallel that can be configured for this driver chip.  It does take time to put the pieces together but this is where you do learn quite a bit of information.  It does get easier the more datasheets you review as well. 


The last tab we will review is the "SPI Adavanced" tab.  In this tab you should not need to configure anything but I will review quickly in case there are some differences.  I have the buffer size set to 8 and no interrupts selected.  As we get things working we may review this tab including the DMA portion.  Using the DMA could help take some of the load off of the microcontroller if needed.  For now we will keep it as is.


Wow, a lot of information just to get the schematic portion going.  In other microcontrollers you would need to configure a lot of this in code and review the datasheet of the microcontroller to understand how to configure.  We will get to the code portion, but next we need to configure the pins for the blocks we just entered in the schematic.  

To get to the pin configurations in the "workspace explorer" section (by default on the left) double click on the "Pins" section under Design Wide Resources.  On the right hand side you need to configure the pins for the blocks we entered under the schematic.  For now we have the following five pins to configure.
  • P1[4] SPI Data (MOSI)
  • P1[6] SPI Chip/Slave Select
  • P1[7] SPI Clock
  • P1[0] Data/Command
  • P1[1] Reset pin



 Once you have the pins configured go ahead and build the project so the API's can be built and we can start working with the API's.  You can press SHIFT+F6, right click on the project and select BUILD from the pull down menu or use the toolbar on top and go to the "Build" tab and select Build XYZ where XYZ is the name of your project.  If this is successful you should get a note that states "Build Succeeded: xx/yy/zz hh:mm:ss".  If you have made it this far you are ready to start the coding portion.

  
Now let's open up the main.c file located under Source Files.  As you can see this is built with some code already populated.  There is a #include "project.h".  If you go to this file you will see it has been populated based on the items we populated in the schematic.  This provides the references to the API's that we will be using.  There is also a line of code: 

CyGlobalIntEnable;

We will keep this line of code in for future use of interrupts. 


So to interface with the LCD there are a few functions we need to write.  One is the transmission of data through the SPI interface.  This allows us to send data (or commands) to the LCD through the SPI interface.  I have set up a function that sends an 8-bit value then a separate one that will send a 16-bit value (essentially splits the 16-bit value into two 8-bit values).  I also have a function called lcd_init.  This initializes and configures the LCD which is required before sending any data.  I have also created another function called lcd_setScreen which will set the entire LCD to the color defined.

So as seen in the image below to make all of this work you need to add the .c and .h file that has been created for the LCD (ili9163) to the project.  Next you need to include the lcd_ili9163.h file in the main.c.  Then after the main loop we will do the following:
  1. Start the SPI communications
  2. Initialize the LCD 
  3. Set the screen to BLACK
  4. Delay 1 second
  5. Set the screen to RED  
  6. Delay 1 second
  7. Set the screen to GREEN
  8. Delay 1 second
  9. Set the screen to BLUE
  10. Delay 1 second
  11. Enter the continuous for loop (where we do nothing)


I have uploaded the files on my Github account which can be found at the following link:


In the next blog I will add on to the existing LCD capabilities including the ability to display text and images.

Monday, December 25, 2017

Cypress PSoC BLE - The start of ble ble ble....................

This is to start with a simple, well I guess we will see how simple, design of a BLE (Bluetooth Low Energy) badge.  The idea is to have fun creating a name badge that can be controlled using an Android app.  So I project there will be quite a few gotchas that I will need to look out throughout the design process.  To start I am going to set my high level requirements to understand what we are trying to design.  Also this will help us understand by the end if I was successful in the final design.


High Level Requirements:  Create a name badge that can attached to a chain/lanyard to wear around a persons neck or attach to a shirt using a pin or equivalent.  The badge shall have a user interface which includes a screen for feedback and button(s) for control.  The screen shall be an LCD, LED or similar technology which can be controlled using a microcontroller.  The badge shall have the capability to display text and images with multiple colors.  The badge shall have the ability to communicate to an Android phone through BLE to allow the contents on the display to be updated.  The badge shall be powered by a coin cell battery.


So now that I have the high level requirements defined next step is to decompose these into more defined requirements to define our design in a little more detail and how I will achieve that.  Let's look at what it is going to take to meet each of these requirements.  Let's start with the very first one which is given below.

  • Create a name badge that can attached to a chain/lanyard to wear around a persons neck or attach to a shirt using a pin or equivalent.

Based on this I need to build the badge with some type of housing for the badge.  The housing will need to be designed with some type of pin to attach to a shirt or include a ring which can be connected to a lanyard.  I will start with a simple concept just to start understanding the look of the badge.  Below shows a preliminary design of a heart badge with a loop on top.  The LCD is shown in black in the center and below is a single touch button.  For our purpose this will be created using a 3D printer and PLA (Polylactic Acid) filament.





  • The badge shall have a user interface which includes a screen for feedback and button(s) for control.  
To meet this requirement I have chosen a Cypress PSoC 4 to drive the display and also provide the ability to interface with buttons.  The main reason I chose the Cypress PSoC  (Programmable System-on-Chip) is the flexibility of pin configurations, wide range of operating voltages, simple interface to capacitive touch buttons and a version that has built in BLE.  To help prototype this I will use the Pioneer kit (CY8CKIT-042-BLE-A) available through Cypress.

  

  • The screen shall be an LCD, LED or similar technology which can be controlled using a microcontroller.  The badge shall have the capability to display text and images with multiple colors. 

For the LCD there are a few things that need to be considered.
  1. Voltage
  2. Interface Type
  3. Size
  4. Connection type (ribbon, pins, etc)
To keep the cost low and ease the build I will select something readily available online (for now one that I have at my house).  The voltage selection I need to consider one of the other requirements which is to be powered by a coin cell battery.  For this project I will consider using a voltage of 3V.   The interface type I will use to start is one with a SPI interface (ILI9163 driver).  The size will be a 1.44" display (128 x 128) and the connection type will either be header pins or a FFC/FPC cable.  Many of these types of LCD's can be easily be found on Amazon for less that $10.  The drivers may vary but this can be managed through software.  An image is shown below of the type of display that will be used for this project.


  
  • The badge shall have the ability to communicate to an Android phone through BLE to allow the contents on the display to be updated.  
The microcontroller I selected has built-in BLE which can satisfy the badge side.  On the phone side I will need to develop an Android app which can communicate to the badge and provide a user interface that allows the data on the badge to be updated.  For now I will consider the following to be data that can be updated.
  1. Name (Person's name, it is a badge)
  2. Background Color
  3. Image (Allows person to select image to be displayed)
  4. Date & Time 
To achieve this I will use MIT App Inventor 2 which is a nice simple interface that makes it quick and easy to build Android apps.  Depending on the time and interest this may be moved over to an environment like Android Studio or equivalent.
  • The badge shall be powered by a coin cell battery.
This is important is it will drive the voltage level that will be used for the badge.  The selection for this will be 3V based on a  CR2032 lithium coin cell battery.  This also means that I will need to include some type of coin cell holder as part of the design.

Unfortunately I know this is the boring stuff which does not have a lot of technical content but it gives us the overview of the project and what I plan on designing.  For the next steps I will start digging into the prototyping phase.  I will start with some software development using the PSoC development kit to start lighting up some things.