libmaple/examples/test-spi-roundtrip.cpp
Marti Bolivar 954f9e5065 Move public headers to include directories; related cleanups.
Move libmaple/*.h to (new) libmaple/include/libmaple/. The new
accepted way to include a libmaple header foo.h is with:

    #include <libmaple/foo.h>

This is more polite in terms of the include namespace. It also allows
us to e.g. implement the Arduino SPI library at all (which has header
SPI.h; providing it was previously impossible on case-insensitive
filesystems due to libmaple's spi.h).

Similarly for Wirish.

The old include style (#include "header.h") is now deprecated.

libmaple/*.h:

- Change include guard #defines from _FOO_H_ to _LIBMAPLE_FOO_H_.
- Add license headers where they're missing
- Add conditional extern "C" { ... } blocks where they're missing
  (they aren't always necessary, but we might was well do it against
  the future, while we're at it.).
- Change includes from #include "foo.h" to #include <libmaple/foo.h>.
- Move includes after extern "C".
- Remove extra trailing newlines

Note that this doesn't include the headers under libmaple/usb/ or
libmaple/usb/usb_lib. These will get fixed later.

libmaple/*.c:

- Change includes from #include "foo.h" to #include <libmaple/foo.h>.

Makefile:

- Add I$(LIBMAPLE_PATH)/include/libmaple to GLOBAL_FLAGS.  This allows
  for users (including Wirish) to migrate their code, but should go
  away ASAP, since it slows down compilation.

Wirish:

- Move wirish/**/*.h to (new) wirish/include/wirish/.  This ignores
  the USB headers, which, as usual, are getting handled after
  everything else.

- Similarly generify wirish/boards/ structure. For each supported
  board "foo", move wirish/boards/foo.h and wirish/boards/foo.cpp to
  wirish/boards/foo/include/board/board.h and
  wirish/boards/foo/board.cpp, respectively. Also remove the #ifdef
  hacks around the .cpp files.

- wirish/rules.mk: put wirish/boards/foo/include in the include path
  (and add wirish/boards/foo/board.cpp to the list of sources to be
  compiled). This allows saying:

      #include <board/board.h>

  instead of the hack currently in place. We can allow the user to
  override this setting later to make adding custom board definitions
  easier.

- Disable -Werror in libmaple/rules.mk, as the current USB warnings
  don't let the olimex_stm32_h103 board compile. We can re-enable
  -Werror once we've moved the board-specific bits out of libmaple
  proper.

libraries, examples:

- Update includes accordingly.
- Miscellaneous cosmetic fixups.

Signed-off-by: Marti Bolivar <mbolivar@leaflabs.com>
2012-04-11 16:56:50 -04:00

193 lines
5 KiB
C++

/*
* Polling SPI loopback test.
*
* Bob is nowhere to be found, so Alice decides to talk to herself.
*
* Instructions: Connect SPI2 (Alice) to herself (i.e., MISO to MOSI).
* Connect to Alice via SerialUSB. Press any key to start.
*
* Alice will talk to herself for a little while. The sketch will
* report if Alice can't hear anything she says. She'll then start
* talking forever at various frequencies, bit orders, and modes. Use
* an oscilloscope to make sure she's not trying to lie about any of
* those things.
*
* This file is released into the public domain.
*
* Author: Marti Bolivar <mbolivar@leaflabs.com>
*/
#include <wirish/wirish.h>
HardwareSPI alice(2);
#define NFREQS 8
const SPIFrequency spi_freqs[] = {
SPI_140_625KHZ,
SPI_281_250KHZ,
SPI_562_500KHZ,
SPI_1_125MHZ,
SPI_2_25MHZ,
SPI_4_5MHZ,
SPI_9MHZ,
SPI_18MHZ,
};
#define TEST_BUF_SIZE 10
uint8 test_buf[TEST_BUF_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
void bad_assert(const char* file, int line, const char* exp) {
SerialUSB.println();
SerialUSB.print("ERROR: FAILED ASSERT(");
SerialUSB.print(exp);
SerialUSB.print("): ");
SerialUSB.print(file);
SerialUSB.print(": ");
SerialUSB.println(line);
throb();
}
#undef ASSERT
#define ASSERT(exp) \
if (exp) { \
} else { \
bad_assert(__FILE__, __LINE__, #exp); \
}
void haveConversation(uint32 bitOrder);
void soliloquies(uint32 bitOrder);
void setup() {
pinMode(BOARD_LED_PIN, OUTPUT);
SerialUSB.read();
}
void loop() {
SerialUSB.println("** Having a conversation, MSB first");
haveConversation(MSBFIRST);
SerialUSB.println("** Having a conversation, LSB first");
haveConversation(LSBFIRST);
SerialUSB.println();
SerialUSB.println("*** All done! It looks like everything worked.");
SerialUSB.println();
SerialUSB.println("** Alice will now wax eloquent in various styles. "
"Press any key for the next configuration.");
soliloquies(MSBFIRST);
soliloquies(LSBFIRST);
while (true)
;
}
void printFrequencyString(SPIFrequency frequency);
void chat(SPIFrequency frequency, uint32 bitOrder, uint32 mode);
void haveConversation(uint32 bitOrder) {
for (int f = 0; f < NFREQS; f++) {
for (int mode = 0; mode < 4; mode++) {
chat(spi_freqs[f], bitOrder, mode);
delay(10);
}
}
}
void chat(SPIFrequency frequency, uint32 bitOrder, uint32 mode) {
SerialUSB.print("Having a chat.\tFrequency: ");
printFrequencyString(frequency);
SerialUSB.print(",\tbitOrder: ");
SerialUSB.print(bitOrder == MSBFIRST ? "MSB" : "LSB");
SerialUSB.print(",\tmode: ");
SerialUSB.print(mode);
SerialUSB.print(".");
SerialUSB.print(" [1] ");
alice.begin(frequency, bitOrder, mode);
SerialUSB.print(" [2] ");
uint32 txed = 0;
while (txed < TEST_BUF_SIZE) {
ASSERT(alice.transfer(test_buf[txed]) == test_buf[txed]);
txed++;
}
SerialUSB.print(" [3] ");
alice.end();
SerialUSB.println(" ok.");
}
void soliloquy(SPIFrequency freq, uint32 bitOrder, uint32 mode);
void soliloquies(uint32 bitOrder) {
for (int f = 0; f < NFREQS; f++) {
for (int mode = 0; mode < 4; mode++) {
soliloquy(spi_freqs[f], bitOrder, mode);
}
}
}
void soliloquy(SPIFrequency frequency, uint32 bitOrder, uint32 mode) {
const uint8 repeat = 0xAE;
SerialUSB.print("Alice is giving a soliloquy (repeating 0x");
SerialUSB.print(repeat, HEX);
SerialUSB.print("). Frequency: ");
printFrequencyString(frequency);
SerialUSB.print(", bitOrder: ");
SerialUSB.print(bitOrder == MSBFIRST ? "big-endian" : "little-endian");
SerialUSB.print(", SPI mode: ");
SerialUSB.println(mode);
alice.begin(frequency, bitOrder, mode);
while (!SerialUSB.available()) {
alice.write(repeat);
delayMicroseconds(200);
}
SerialUSB.read();
}
void printFrequencyString(SPIFrequency frequency) {
switch (frequency) {
case SPI_18MHZ:
SerialUSB.print("18 MHz");
break;
case SPI_9MHZ:
SerialUSB.print("9 MHz");
break;
case SPI_4_5MHZ:
SerialUSB.print("4.5 MHz");
break;
case SPI_2_25MHZ:
SerialUSB.print("2.25 MHZ");
break;
case SPI_1_125MHZ:
SerialUSB.print("1.125 MHz");
break;
case SPI_562_500KHZ:
SerialUSB.print("562.500 KHz");
break;
case SPI_281_250KHZ:
SerialUSB.print("281.250 KHz");
break;
case SPI_140_625KHZ:
SerialUSB.print("140.625 KHz");
break;
}
}
// Force init to be called *first*, i.e. before static object allocation.
// Otherwise, statically allocated objects that need libmaple may fail.
__attribute__((constructor)) void premain() {
init();
}
int main(void) {
setup();
while (true) {
loop();
}
return 0;
}