PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
SPI_Demo.cpp

Write "Hello world!" out via SPI protocol and receive an echo

cmake_minimum_required(VERSION 3.12)
find_package(PropWare REQUIRED)
project(SPI_Demo)
create_simple_executable(${PROJECT_NAME} SPI_Demo.cpp)
// Includes
static const Port::Mask MOSI = Port::Mask::P0;
static const Port::Mask MISO = Port::Mask::P1;
static const Port::Mask SCLK = Port::Mask::P2;
static const Port::Mask CS = Port::Mask::P6;
static const uint32_t FREQ = 100000;
static const SPI::Mode MODE = SPI::Mode::MODE_0;
static const SPI::BitMode BITMODE = SPI::BitMode::MSB_FIRST;
int main () {
char string[] = "Hello world!\n"; // Create the test string
char *s; // Create a pointer variable that can be incremented in a loop
char in; // Create an input variable to store received values from SPI
SPI spi = SPI::get_instance();
// Initialize SPI module, giving it pin masks for the physical pins,
// frequency for the clock, mode of SPI, and bitmode
spi.set_mosi(MOSI);
spi.set_miso(MISO);
spi.set_sclk(SCLK);
spi.set_clock(FREQ);
spi.set_mode(MODE);
spi.set_bit_mode(BITMODE);
// Set chip select as an output (Note: the SPI module does not control chip
// select)
Pin cs(CS, Pin::Dir::OUT);
SimplePort debugLEDs(Port::P16, 8, Pin::Dir::OUT);
while (1) {
s = string; // Set the pointer to the beginning of the string
while (*s) { // Loop until we read the null-terminator
cs.clear(); // Enable the SPI slave attached to CS
spi.shift_out(8, (uint32_t) *s); // Output the next character of the string
cs.set();
in = (char) 0xff; // Reset input variable
while (in != *s) {
cs.clear();
in = (char) spi.shift_in(8); // Read in a value from the SPI device
cs.set();
}
// Increment the character pointer
++s;
// Print the character to the screen
}
// Signal that the entire string has been sent
debugLEDs.toggle();
}
}
MISO
static const Port::Mask MISO
Definition: SPI_Demo.cpp:39
PropWare::SPI
SPI serial communications library; Core functionality comes from a dedicated assembly cog.
Definition: spi.h:43
spi.h
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
PropWare::Port
Flexible port that can have any pin enabled or disabled. Pins are independent of each other.
Definition: port.h:38
CS
static const Port::Mask CS
Definition: SPI_Demo.cpp:43
simpleport.h
main
int main(void)
Definition: GraphicsTest.c:20
PropWare.h
CLKFREQ
#define CLKFREQ
Returns the current clock frequency.
Definition: propeller.h:46
string
void string(char *str)
Display a character string on the oLED display.
Definition: oled_string.c:7
MOSI
static const Port::Mask MOSI
Definition: SPI_Demo.cpp:37
pwOut
PropWare::Printer pwOut
Most common use of printing in PropWare applications (not thread safe; see PropWare::pwSyncOut for mu...
waitcnt
#define waitcnt(a)
Wait until system counter reaches a value.
Definition: propeller.h:176
MODE
static const SPI::Mode MODE
Definition: SPI_Demo.cpp:48
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
char
PropWare::SimplePort
The PropWare::SimplePort is the recommended way to use data ports on the Propeller....
Definition: simpleport.h:36
SCLK
static const Port::Mask SCLK
Definition: SPI_Demo.cpp:41
SPI
Definition: SPI.h:11
FREQ
static const uint32_t FREQ
Definition: SPI_Demo.cpp:46
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
BITMODE
static const SPI::BitMode BITMODE
Definition: SPI_Demo.cpp:50
PropWare::Printer::put_char
void put_char(const char c) const
Print a single character.
Definition: printer.h:175