PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
SPI_Demo.cpp
Go to the documentation of this file.
1 
26 // Includes
27 #include <PropWare/PropWare.h>
30 
31 using PropWare::Port;
32 using PropWare::SPI;
33 using PropWare::Pin;
35 
37 static const Port::Mask MOSI = Port::Mask::P0;
39 static const Port::Mask MISO = Port::Mask::P1;
41 static const Port::Mask SCLK = Port::Mask::P2;
43 static const Port::Mask CS = Port::Mask::P6;
44 
46 static const uint32_t FREQ = 100000;
48 static const SPI::Mode MODE = SPI::Mode::MODE_0;
50 static const SPI::BitMode BITMODE = SPI::BitMode::MSB_FIRST;
51 
59 int main () {
60  char string[] = "Hello world!\n"; // Create the test string
61  char *s; // Create a pointer variable that can be incremented in a loop
62  char in; // Create an input variable to store received values from SPI
63  SPI spi = SPI::get_instance();
64 
65  // Initialize SPI module, giving it pin masks for the physical pins,
66  // frequency for the clock, mode of SPI, and bitmode
67  spi.set_mosi(MOSI);
68  spi.set_miso(MISO);
69  spi.set_sclk(SCLK);
70  spi.set_clock(FREQ);
71  spi.set_mode(MODE);
72  spi.set_bit_mode(BITMODE);
73 
74  // Set chip select as an output (Note: the SPI module does not control chip
75  // select)
76  Pin cs(CS, Pin::Dir::OUT);
77 
78  SimplePort debugLEDs(Port::P16, 8, Pin::Dir::OUT);
79 
80  while (1) {
81  s = string; // Set the pointer to the beginning of the string
82  while (*s) { // Loop until we read the null-terminator
83 
84  waitcnt(CLKFREQ/100 + CNT);
85 
86  cs.clear(); // Enable the SPI slave attached to CS
87  spi.shift_out(8, (uint32_t) *s); // Output the next character of the string
88 
89  cs.set();
90 
91  waitcnt(CLKFREQ/100 + CNT);
92  in = (char) 0xff; // Reset input variable
93  while (in != *s) {
94  cs.clear();
95  in = (char) spi.shift_in(8); // Read in a value from the SPI device
96  cs.set();
97  }
98 
99  // Increment the character pointer
100  ++s;
101 
102  // Print the character to the screen
103  pwOut.put_char(in);
104  }
105 
106  // Signal that the entire string has been sent
107  debugLEDs.toggle();
108  }
109 }
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
PropWare::Port::toggle
void toggle() const
Toggle the output value of a port.
Definition: port.h:272
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
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