FreeSoC2 Hookup Guide V14
Writing Code in Arduino
Differences Between FreeSoC2 and Other Arduino Boards
The core support for the PSoC is made to closely resemble the Arduino Uno R3, and by and large, code which runs on the Uno R3 will work on the FreeSoC2 with little or no change. Here are some of the differences to be aware of:
- The hardware serial port on pins 0 and 1 is
Serial1
, rather thanSerial
, as on the Arduino Leonardo. Serial prints to the computer, via the USB port. - The AREF pin is not connected on the FreeSoC2.
- random() returns a 24-bit value instead of a 32-bit value.
- Some of the more advanced string manipulation is, as yet, unimplemented.
- Pin numbers should be defined as
long
rather thanint
.
We've ported the most core libraries to the FreeSoC: SPI, Wire (I2C), and Servo, and we've created a driver library for the WS281x programmable RGB LEDs. Other native Arduino libraries may or may not work. Here's a list of libraries we've verified with the FreeSoC2 in Arduino:
Extra Serial Port
As mentioned above, all Serial
commands go through the USB connection the target maintains with the PC. This is awkward, though, because it means that text sent immediately after reset will often be missed in the time between the code beginning to run and the user opening the terminal window.
There's an option for the FreeSoC2, however: when the Debugger USB port is connected to the PC, it will enumerate as, among other things, another serial port. That port can be used to send and receive data from the Arduino application via the Serial1
interface. We'll demonstrate that below.
Example
This example will show you how to access pins not in the normal Arduino range, as well as using the serial ports.
Remember to select the "PSoC Dev Board" in the boards menu!
language:c
/******************************************************************************
Example.ino
Simple example demonstrating the use of Serial ports and extra pins on the
FreeSoC2 board within the Arduino IDE.
14 May 2015
Developed/Tested with:
FreeSoC2
Arduino.cc IDE 1.6.4
This code is beerware; if you see me (or any other SparkFun employee) at the
local, and you've found our code helpful, please buy us a round!
Distributed as-is; no warranty is given.
******************************************************************************/
// Note the use of long here instead of int! This is important.
const long LEDPin = 13; // As on most Arduino compatible boards, there's an LED
// tied to pin 13. pinMode() works the same with the
// FreeSoC2 as it does normally.
const long buttonPin = P1_D2; // Pins can also be referred to by port number
// and pin number. These numbers are given on the board
// as P<port>.<pin>. P1_D2 is connected to the user
// button on one side ang ground on the other.
void setup()
{
// pinMode() functions are unchanged.
pinMode(LEDPin, OUTPUT);
// The onboard user button needs to be a pullup-enabled input.
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600); // This is the same as it is on the Arduino Leonardo.
// It represents the logical serial port connection to
// the PC.
Serial1.begin(9600); // Again, same as the Leonardo. This port is the physical
// IO pins (0 and 1) on the headers. It can be accessed
// via the KitProg serial port which is available when
// the Debugger port is connected.
}
void loop()
{
// digitalRead() is unchanged.
int pinDown = digitalRead(buttonPin);
// Check for button pressed...
if (pinDown == LOW)
{
Serial1.println("KitProg! Button pressed!");
Serial.println("USB! Button pressed!");
digitalWrite(LEDPin, HIGH);
}
else
{
digitalWrite(LEDPin, LOW);
}
delay(250);
}