This is only a preview of the September 2019 issue of Silicon Chip. You can view 58 of the 128 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "Build your own Gamer’s Seat with Four DoF":
Items relevant to "A new Micromite: the sensational Explore-28":
Items relevant to "Six-way Stereo Audio Input Selector with remote control":
Articles in this series:
Items relevant to "Ultrabrite LED Bicycle Light":
Purchase a printed copy of this issue for $10.00. |
Introduction to Programming – Part 2
˃Cypress’ System on a Chip
Analog/digital signals and debugging
Our last tutorial on using the Cypress Programmable
System on a Chip covered programming and using
these fascinating mixed analog/digital devices,
but being an introductory article, didn’t
go into great detail on how to use the
programmable analog features which
make them so unique. This followup article describes the more
powerful CY8CKIT-059 board
and explains how to use
its capabilities.
By Dennis Smith
O
ur previous article on using
Cypress PSoCs (October 2018;
siliconchip.com.au/Article/11269)
introduced the Cypress Semiconductor CY8CKIT-049-42XX development
code, a 32-bit ARM development platform. We described how to install and
use its integrated development environment (IDE), and provided a sample
program to read the temperature from
an NTC thermistor and display it on
an LCD screen.
This article is based on the more
powerful CY8CKIT-059 board, with
attached Serial Wire Debug adaptor
(SWD) and programmer, shown above.
Incidentally, the included SWD adaptor and programmer can also be used
with the 049-42XX boards.
The main product page for the CY8CKIT-059-5LP development board,
which contains links to extensive documentation and the relevant downloads, is at: siliconchip.com.au/link/
aaqn You may need to create a free
Cypress account to download the files.
The development board is available in Australia for just over $20 (at
the time of writing) from element14,
Mouser, RS Australia etc. The integrated development environment (IDE),
called PSoC Creator, complete with
compiler and device specific libraries
is available to download for free from
the product page above.
The CY8CKIT-059-5LP board is
92
Silicon Chip
designed around the CY8C5888LTILP097 chip (see www.cypress.com/
part/cy8c5888lti-lp097) which is
available in 68-pin QFN or 100-pin
TQFP packages (both surface-mounting). It contains an ARM Cortex-M3
32-bit CPU running at up to 80MHz,
256KB of flash memory and 64KB of
SRAM; which makes it quite powerful as microcontrollers go.
You can download the chip’s Product Overview document, which lists
all of its important features, at: www.
cypress.com/file/140796/
The chip also includes a coprocessor, called the Digital Filter Block
(DFB), which is a 24-bit digital signal
processor (DSP). A datasheet on this
co-processor can be found at: www.
cypress.com/file/315811/
The DFB is useful for certain tasks
like matrix/vector multiplication or
analog signal processing. Performing
these jobs using the DFB frees up the
main CPU for other tasks. It also includes programmable analog and digital components, such as ADCs, DACs,
timers/PWM inputs, serial communication etc.
CY8CKIT-059 Features
32-bit ARM microcontroller with 256KB flash and 64KB SRAM
24-channel direct memory access (DMA) controller
Capacitive touch sensing
Programmable analog blocks, including:
• one 8- to 20-bit delta-sigma analog-to-digital converter (ADC)
• two 12-bit successive approximation (SAR) ADCs
• analog multiplexers, which can make the ADCs multi-channel capable
• four 8-bit DACs
• four comparators
• four op amps
• four programmable analog blocks (continuous time or switched capacitor)
Programmable digital blocks:
• four timers/counters/PWM (pulse width modulators)
• 24 universal digital blocks (UDB), which include registers and an arithmetic
logic unit (ALU)
• Up to 72 input/output pins
• On-chip JTAG/debugging
• 1.2(1.7)-5.5V supply voltage
Australia’s electronics magazine
siliconchip.com.au
A removable USB-to-serial and Serial Wire Debug adaptor is included in
the kit. Once you have finished your
project, the adapter can be snapped off
and used as a general-purpose USB/
serial adaptor or as a Serial Wire Debug adaptor for any of the PSoC family boards. The adaptor can also be
used as a USB to general-purpose I/O
(GPIO) device.
Becoming adept at
programming PSoCs
As with other micros, there’s a bit of
a learning curve. The curve might be
somewhat steeper with a PSoC than,
say, an Arduino, but its ultimate capabilities are far greater.
It has a 32-bit CPU rather than 8-bit,
and much more memory, but is actually cheaper than most Arduino modules. Most importantly and uniquely,
though, it has the configurable analog
blocks which give you far greater flexibility and power when it comes to
signal processing.
Probably the biggest disadvantage of
the PSoC compared to other 8-bit and
32-bit micros is that its extra capabilities translate into somewhat higher
power requirements. You are also effectively stuck using the Creator IDE
for programming PSoC boards; while
it's possible to build the project yourself, it's impractical.
It's important to note that since the
community is smaller, fewer libraries
are available for these devices compared to Arduino. Although, given the
dodginess of some of the Arduino libraries we’ve come across, sometimes
having to write your own code may be
a blessing in disguise…
to your “C:\Users\<USERNAME>\
Documents\PSoC Creator” folder.
They can be placed in other folders if
you want, but for this article, we’ll assume you’ve used this default location.
Now plug the board into a USB 2.0
port on your PC. An orange LED on
the USB-to-serial adaptor indicates
that power is present, while a flashing blue LED indicates that all is well.
Now unplug the board and plug
it back in again, but this time, holding down the button at the rear of the
board while you plug in. The blue light
should now flash at a faster rate, indicating that the board is now in bootloader mode.
With the 049-42XX board described
in the previous article, the bootloader was the only way to load programs
into the chip. This method still works
with the CY8CKIT-059 board, but with
this board, you can load your program
straight into the board without having
to enter bootloader mode.
Preparing our first example
We’ve provided two example projects this time, both included in the
free download (ZIP package) on the
Silicon Chip website. We’ll start with
the “AnalogDebugExample” project
first.
Navigate to the “AnalogDebugExample.cydsn” folder and double click on
“AnalogDebugExample.cyprj”. This
will open PSoC Creator 4.2 IDE and
load the project files automatically.
Otherwise, you can open the IDE, click
“Open Existing Project” from the Start
page and navigate to the project folder.
You should now see the block diagram shown in Fig.1. If it is not visible, double click on the “TopDesign.
cysch” tab at the top of the left panel.
Now click on the “Connections” tab
at the bottom, and you should see the
wiring diagram shown in Fig.2. This
just provides a guide for how to configure the hardware and is not required
when you make your own projects.
You can also view what pins are
actively being used on the main IC by
going to the Pins item under Design
Wide Resources in the left sidebar of
the IDE. You should see the same diagram shown in Fig.3.
Connect two pushbutton switches
to your board, as Fig.2. You can wire
the components directly to the board,
or if you solder header strips to the
development board, you can plug it
into a breadboard and then connect
the switches that way. But if you are
planning to snap off the USB-to-serial
adaptor board later, don’t solder terminal strips on just yet.
Building and uploading
To build the project before programming the hardware, click on the build
icon in the tools bar just above the
Workspace Explorer window (you can
also just press CTRL+F5). If all is well,
Using PSoC Creator
If you need a refresher on using the
software, refer to the previous article
(October 2018) on how to get started
with Cypress PSoC, including downloading and installing the PSoC Creator IDE. Note that the IDE only supports Windows PCs (7-10).
If you already have the IDE installed, you only need to download
the “CY8CKIT-059 Kit Only” file from:
siliconchip.com.au/link/aaqn
Otherwise, you can download the
“CY8CKIT-059 Kit Setup” file below
it, which gives you the above files as
well as the IDE.
With the IDE installed, download
the project files for this article fromsiliconchip.com.au and unzip the files
siliconchip.com.au
Fig.1: block diagram of the first example program as shown in PSoC Creator.
This program uses an Arbitrary Waveform Generator (WaveDAC8) to generate a
tune that is played by pressing SW_1, and have its pitch/tempo varied via SW_2.
Australia’s electronics magazine
September 2019 93
Fig.2: wiring diagram for the first example program. Note the circuit diagram and the breadboard layout which show how
to connect an LM380 audio amplifier and speaker to the CY8CKIT-059 board.
you will see output similar to the text
below in the Output Window:
------- Build Started: 06/18/2018
14:06:49 Project: AnalogDebug,
Configuration: ARM GCC
5.4-2016-q2-update Debug ------Flash used: 6086 of 262144 bytes
(2.3%).
SRAM used: 2849 of 65536 bytes
(4.3%). Stack: 2048 bytes.
Heap: 128 bytes.
------- Build Succeeded: 06/18/2018
14:07:18 -------
Fig.3: the active pins of the main IC used in the “Analog Debug” example
program. You can view this by clicking on the “Pins” item under “Design Wide
Resources” in the left sidebar of the IDE.
94
Silicon Chip
Australia’s electronics magazine
If you get an error message in the
Output Window about missing binaries for the ARM GCC and MDK
toolchain, this is because the project
is set for a ‘debug’ build and the default toolchain in the build settings
is incorrect.
To fix this, go to Project → Build
Settings in the toolbar. In the Build
Settings sub-menu, the second entry
“Toolchain” should be set to “ARM
GCC 5.4-2016-q2-update” or similar
instead of the default “ARM MDK
Generic”.
Or it might be due to not having the
correct binaries for the 059 board. Just
having the 042 kit files from the previous article is not enough to successfully build this project.
siliconchip.com.au
After this, the project should build
without any errors, but you might get
two warnings about asynchronous
paths which you can safely ignore.
Unlike the PSoC 049-42XX boards,
it is not necessary to press a button
on the board before programming. If
programming is successful, the log
will show a message similar to the
one below:
Fig.4: a breakpoint (indicated in red) can
be set by either clicking to the left of the
line-number, or right-clicking the line and
selecting “Insert Breakpoint”. Breakpoints
determine when to temporarily stop code
execution during debugging.
------- Rebuild Succeeded:
06/22/2018 12:45:59 ------Programming device 'PSoC 5LP
CY8C5888LT*-LP097' with
file 'K:\ARM Development\
Cypress PSoC\Active Projects\
AnalogDebugExample\
AnalogDebug.cydsn\CortexM3\
ARM_MDK_Generic\Debug\
AnalogDebug.hex'.
Device ID Check
Erasing...
Programming of Flash Starting...
Protecting...
Verify Checksum...
Finished Programming
Device ‘PSoC 5LP CY8C5888LT*LP097’ was successfully
programmed at 06/22/2018
12:46:09.
If the chip is programmed successfully, and you have a suitable audio
amplifier connected, the opening
theme from “The Godfather” will play
each time you press SW_1. Pressing
SW_2 changes the pitch and tempo
while playing a piano scale. The music
is created using the built-in arbitrary
waveform generator (discussed later).
Interactive debugging
One of the problems with many microcontroller development systems,
including the popular Arduino environment, is that they don’t give you
an easy way to debug your program.
What do you do if you write some
code, upload it to the board, and it
doesn’t do what you expected? You
generally end up having to add lots
of print statements throughout your
code, so you can watch the serial console to find out what’s going wrong.
That can be time consuming.
Wouldn’t it be handy if you could
step through each line of your code and
check the value returned by a function
or from a calculation, while also being
able to see the value of each currently
active variable?
While this is theoretically possible
with an Arduino, it is not easy to orsiliconchip.com.au
Connection diagram for the PSoC CY8CKIT-059 board.
Source and prototyping guide: www.cypress.com/file/157971/download
Australia’s electronics magazine
September 2019 95
Fig.5: when debugging, the current point of execution is
indicated in yellow. If you click the tab at the bottom left
called “Locals”, you should be shown the current values
of all variables that are currently ‘in scope’. You can also
set a “Watchpoint” via right-clicking the variable, which
means that the value of that variable will always be
displayed when ‘visible’.
ganise. But PSoC Creator has in-built
debugging. It does this via the 0595LP’s USB-to-serial adaptor which
contains its own 32-bit ARM processor.
It receives commands from the IDE
and communicates with the 059-5LP’s
(or 049-42XX) CPU to execute code one
line at a time. Since the IDE has access
to the source code of your program (including all the library code written by
Cypress and others), it is possible to
step through each line of code with a
press of a key.
Using the example project, we can
step through the code and see what is
happening at each point. It helps to tell
the IDE how far through the program
we want to go before debugging should
start. To do this, we set a ‘breakpoint’.
Fig.4 shows what the IDE looks like
when we set a breakpoint at the first
statement in the code, as indicated by
the red blob to the left of the “CyGlobalIntEnable” statement.
To set a breakpoint, click in the
shaded vertical area to the left of line
numbering, or right-click the line you
96
Silicon Chip
want to break at, and select “Insert
Breakpoint”. Click again on the red
circle to remove the breakpoint. Breakpoints can be set on any line.
Once you have a breakpoint set,
click on the “Debug” drop-down menu
and select “debug” or press F5. The
IDE will compile the code and program the 059-5LP. It will then begin
execution of the code and stop at the
first breakpoint it detects. The yellow
highlighting shown in Fig.5 indicates
where the program execution is currently up to.
Now when you click on the Debug
drop-down menu, you will see some
additional menu items. The new menu
items show which function keys you
can use to step through the code; for
example, F10 will execute the current statement and then step to the
next one, F11 is similar but will step
into the function itself when relevant.
Try pressing F10, then F11 while the
“DAC_Start();” statement is highlighted. This will move the debugger to inside the scope of that function which
is located in the file DAC.c.
Australia’s electronics magazine
So if you want to find out what happens within a function that’s called on
the currently highlighted line, press
F11. If you are only interested in what
the function returns, press F10.
To see the value of a variable (eg,
Button_1_Pressed), hover the mouse
cursor over that variable and a popup
window will display details of the variable and its contents. Note, however,
that the variable must be ‘in scope’,
that is, local (or ‘visible’) to the function currently being executed.
By right-clicking on the variable, a
popup window appears that allows
a “Watchpoint” to be created, which
means that the IDE will always show
the value of that variable whenever it
is in-scope.
Arbitrary Waveform
Generation (WaveDAC8)
The above example program uses
the Arbitrary Waveform Generator
(WaveDAC8) component to play music. This component has an analog output which can provide sine, square,
triangle and sawtooth waveforms. It
siliconchip.com.au
also has the facility for the user to draw
their own waveform.
You can double-click on the WaveDAC8 component to change its parameters and hear the difference; for example, you can change the waveform
shape, and that will affect the timbre of
the sound it produces. The test waveforms are shown in Fig.6.
Once you’ve wired up the circuit
as described earlier, and programmed
the chip, pressing pushbutton SW_1
plays a theme song, while pressing
SW_2 plays a musical scale, first with
the “DAC” object, then in reverse with
“DAC_1”, which are both WaveDAC8
components.
This demonstrates the use of different waveforms, but the second DAC
could also be used for multi-channel
playback.
The “Voice_Write()” function modifies the contents of a CPU register
which enables one DAC while disabling the other.
To play the music, I’ve created an
array of values which represent the
frequency of each note in the tune.
These are passed (one at a time) to
the “PlayNote()” function, which alters the clock frequency driving each
WaveDAC8 unit, to produce a different
output frequency. The values stored
approximate the frequencies of the
notes on a piano.
To create a different song, make a
new array using TheGodFather[] as a
template and following the comments
in the code. There are ways to turn audio files, like mp3s, into arrays of bytes
which can then be used with this program. Of course, you wouldn’t easily
be able to fit more than snippets into
program memory.
By duplicating the “Volume” control component (a Programmable Gain
Amplifier) and the output potentio-
Fig.7: The “Analog_Alarm” program
alters the behaviour of an LED
depending on the input voltage
from PIN_1 using the chip’s in-built
configure hardware.
meter, you can turn this project into a
stereo synthesiser. With a little extra
programming you could also control
the gain of the PGA(s) with the software to set the volume.
A second example
If you wish to learn more about the
Analog components built into the
Cypress CY8CKIT-059-5LP, here is
a second sample project. It is called
“Analog_Alarm” and is included in
the download package for this article.
Open it up in the IDE, as you did with
the previous project.
You can see the block diagram for
this program in Fig.7. It uses two 8-bit
voltage DACs, a voltage reference,
three analog comparators, an XOR
gate, a 4-input analog multiplexer, a
lookup table and three PWM units to
produce a voltage alarm.
The input voltage comes from the
wiper of a potentiometer (variable resistor) connected to input port P3[0].
Depending on the position of the potentiometer, it will drive the onboard
LED as follows:
1. For any voltage below 1V, the onboard LED is off.
2. Between 1-2V, the LED pulsates.
3. Between 2-4V, the LED flashes
at 100Hz.
4. Above 4V, the LED remains continuously lit.
These voltage ranges can easily
be changed in the code. You can replace the potentiometer with a voltage source (0-5V only), and you have
a voltage monitor facility.
More example projects
Cypress code Examples: siliconchip.
com.au/link/aarf
100 Projects in 100 Days: siliconchip.
com.au/link/aarg
CY8CKIT-059 driving a VGA monitor: siliconchip.com.au/link/aarh
Open FPGA tools: github.com/
azonenberg/openfpga
CNC wood router using a 059-5LP:
github.com/holla2040/hyatt
Cypress buyout
Fig.6: the two waveforms used for DAC (left) and DAC_1 (right) shown in Fig.1.
siliconchip.com.au
Australia’s electronics magazine
Recently Cypress Semiconductors
was acquired by Infineon for approximately €9B. The acquisition seems to
be primarily for Cypress’ automotive
electronics, but their microcontrollers
like the CY8CKIT-059 shown in this
article are likely to continue being developed and sold.
SC
September 2019 97
|