From fb5a1612bcadf14460a88de08a065f4a5229f02f Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Mon, 4 Apr 2011 13:40:25 -0400 Subject: ring_buffer tweaks. Adding basic ring_buffer test (cherry-pick from master). Changing rb_safe_insert()'s type to match that of rb_push_insert(). (Makes it easier to pass around insertion functions.) --- examples/test-ring-buffer-insertion.cpp | 114 ++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 examples/test-ring-buffer-insertion.cpp (limited to 'examples/test-ring-buffer-insertion.cpp') diff --git a/examples/test-ring-buffer-insertion.cpp b/examples/test-ring-buffer-insertion.cpp new file mode 100644 index 0000000..8372a96 --- /dev/null +++ b/examples/test-ring-buffer-insertion.cpp @@ -0,0 +1,114 @@ +/* + * Simple ring_buffer test. + * + * Does a basic test of functionality on rb_full_count(), rb_reset(), + * rb_push_insert(), and rb_safe_insert(). + * + * To test (no external hardware required): + * + * - Connect a serial monitor to SerialUSB + * - Press any key + * + * This file is released into the public domain. + */ + +#include "wirish.h" + +#include "ring_buffer.h" + +#define BUF_SIZE 64 +ring_buffer ring_buf; +ring_buffer *rb; +uint8 rb_buffer[BUF_SIZE]; + +void test_rb_push_insert(int num_bytes_to_insert); +void test_rb_safe_insert(int num_bytes_to_insert); +void test_rb_insertion_function(int num_bytes_to_insert, + int (*insertion_fn)(ring_buffer*, uint8), + const char insertion_fn_name[]); +void print_rb_contents(void); + +void setup() { + rb = &ring_buf; + rb_init(rb, BUF_SIZE, rb_buffer); + + while (!SerialUSB.available()) + ; + + SerialUSB.println("Beginning test."); + SerialUSB.println(); +} + +void loop() { + test_rb_push_insert(63); + SerialUSB.println("------------------------------"); + test_rb_push_insert(64); + SerialUSB.println("------------------------------"); + test_rb_safe_insert(63); + SerialUSB.println("------------------------------"); + test_rb_safe_insert(64); + SerialUSB.println("------------------------------"); + + SerialUSB.println(); + SerialUSB.println("Test finished."); + while (true) + ; +} + +void test_rb_push_insert(int num_bytes_to_insert) { + test_rb_insertion_function(num_bytes_to_insert, + rb_push_insert, + "rb_push_insert()"); +} + +void test_rb_safe_insert(int num_bytes_to_insert) { + test_rb_insertion_function(num_bytes_to_insert, + rb_safe_insert, + "rb_safe_insert()"); +} + +void test_rb_insertion_function(int num_bytes_to_insert, + int (*insertion_fn)(ring_buffer *, uint8), + const char insertion_fn_name[]) { + SerialUSB.println("resetting ring buffer."); + rb_reset(rb); + print_rb_contents(); + + SerialUSB.print(insertion_fn_name); + SerialUSB.print("-ing "); + SerialUSB.print(num_bytes_to_insert); + SerialUSB.println(" bytes."); + for (uint8 i = 1; i <= num_bytes_to_insert; i++) + insertion_fn(rb, i); + + uint16 count = rb_full_count(rb); + SerialUSB.print("rb_full_count(rb) = "); + SerialUSB.println(count); + + print_rb_contents(); +} + +void print_rb_contents() { + uint16 count = rb_full_count(rb); + SerialUSB.print("ring buffer contents: "); + for (uint16 i = 0; i < count; i++) { + SerialUSB.print((int)rb_remove(rb)); + if (i < count - 1) SerialUSB.print(", "); + } + SerialUSB.println(); +} + +// 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; +} -- cgit v1.2.3 From 11a0da5c63faffd45bde43da2777a9771cfdf5e9 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Mon, 2 May 2011 14:33:05 -0400 Subject: /examples/ cleanups. --- examples/blinky.cpp | 9 ++--- examples/debug-dtrrts.cpp | 5 +-- examples/spi_master.cpp | 65 +++++++++++++++++---------------- examples/test-dac.cpp | 9 +++-- examples/test-ring-buffer-insertion.cpp | 2 +- examples/test-serial-flush.cpp | 20 +++++----- examples/test-serialusb.cpp | 31 +++++++--------- examples/test-systick.cpp | 15 +++----- examples/test-timers.cpp | 9 ++--- examples/vga-leaf.cpp | 21 ++++------- examples/vga-scope.cpp | 17 ++++----- 11 files changed, 91 insertions(+), 112 deletions(-) (limited to 'examples/test-ring-buffer-insertion.cpp') diff --git a/examples/blinky.cpp b/examples/blinky.cpp index 209a6e6..91d1a47 100644 --- a/examples/blinky.cpp +++ b/examples/blinky.cpp @@ -2,11 +2,8 @@ #include "wirish.h" -// Use the pin attached to the built-in LED -#define PIN BOARD_LED_PIN - void setup() { - pinMode(PIN, OUTPUT); + pinMode(BOARD_LED_PIN, OUTPUT); } int toggle = 1; @@ -14,7 +11,7 @@ int toggle = 1; void loop() { // You could just use toggleLED() instead, but this illustrates // the use of digitalWrite(): - digitalWrite(PIN, toggle); + digitalWrite(BOARD_LED_PIN, toggle); toggle ^= 1; delay(100); } @@ -28,7 +25,7 @@ __attribute__((constructor)) void premain() { int main(void) { setup(); - while (1) { + while (true) { loop(); } return 0; diff --git a/examples/debug-dtrrts.cpp b/examples/debug-dtrrts.cpp index 61c061a..3829208 100644 --- a/examples/debug-dtrrts.cpp +++ b/examples/debug-dtrrts.cpp @@ -3,12 +3,9 @@ #include "wirish.h" #include "usb.h" -#define LED_PIN BOARD_LED_PIN -#define PWM_PIN 2 - void setup() { /* Set up the LED to blink */ - pinMode(LED_PIN, OUTPUT); + pinMode(BOARD_LED_PIN, OUTPUT); /* Send a message out USART2 */ Serial2.begin(9600); diff --git a/examples/spi_master.cpp b/examples/spi_master.cpp index 06cad02..af3e709 100644 --- a/examples/spi_master.cpp +++ b/examples/spi_master.cpp @@ -1,61 +1,62 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 LeafLabs LLC. * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, copy, + * modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * ****************************************************************************/ + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + *****************************************************************************/ /** - * @brief Sample main.cpp file. Sends "Hello world!" out SPI1. + * @brief Sample main.cpp file. Sends "Hello world!" out SPI1. * - * SPI1 is set up to be a master transmitter at 4.5MHz, little endianness, - * and SPI mode 0. - * - * Pin 10 is used as Chip Select + * SPI1 is set up to be a master transmitter at 4.5MHz, little + * endianness, and SPI mode 0. * + * Pin 10 is used as slave select. */ #include "wirish.h" -#define CS 10 +#define NSS 10 byte buf[] = "Hello world!"; HardwareSPI spi1(1); void setup() { - /* Set up chip select as output */ - pinMode(CS, OUTPUT); + /* Set up chip select as output */ + pinMode(NSS, OUTPUT); - /* CS is usually active low, so initialize it high */ - digitalWrite(CS, HIGH); + /* NSS is usually active LOW, so initialize it HIGH */ + digitalWrite(NSS, HIGH); - /* Initialize SPI */ + /* Initialize SPI */ spi1.begin(SPI_4_5MHZ, LSBFIRST, 0); } void loop() { - /* Send message */ - digitalWrite(CS, LOW); - spi1.send(buf, sizeof buf); - digitalWrite(CS,HIGH); + /* Send message */ + digitalWrite(NSS, LOW); + spi1.write(buf, sizeof buf); + digitalWrite(NSS, HIGH); delay(1000); } @@ -68,7 +69,7 @@ __attribute__((constructor)) void premain() { int main(void) { setup(); - while (1) { + while (true) { loop(); } return 0; diff --git a/examples/test-dac.cpp b/examples/test-dac.cpp index ba766a4..40ae5d5 100644 --- a/examples/test-dac.cpp +++ b/examples/test-dac.cpp @@ -13,7 +13,7 @@ uint16 count = 0; void setup() { pinMode(BOARD_LED_PIN, OUTPUT); - digitalWrite(BOARD_LED_PIN,1); + digitalWrite(BOARD_LED_PIN, HIGH); Serial1.begin(9600); Serial1.println("**** Beginning DAC test"); @@ -36,11 +36,14 @@ void loop() { dac_write_channel(DAC, 2, count); } -int main(void) { +__attribute__((constructor)) void premain() { init(); +} + +int main(void) { setup(); - while (1) { + while (true) { loop(); } return 0; diff --git a/examples/test-ring-buffer-insertion.cpp b/examples/test-ring-buffer-insertion.cpp index 8372a96..e86372a 100644 --- a/examples/test-ring-buffer-insertion.cpp +++ b/examples/test-ring-buffer-insertion.cpp @@ -4,7 +4,7 @@ * Does a basic test of functionality on rb_full_count(), rb_reset(), * rb_push_insert(), and rb_safe_insert(). * - * To test (no external hardware required): + * To test: * * - Connect a serial monitor to SerialUSB * - Press any key diff --git a/examples/test-serial-flush.cpp b/examples/test-serial-flush.cpp index 6c4e100..adc9c3e 100644 --- a/examples/test-serial-flush.cpp +++ b/examples/test-serial-flush.cpp @@ -4,23 +4,21 @@ #include "wirish.h" -#define COMM Serial1 - void setup() { - COMM.begin(9600); - COMM.println("Hello world!"); + Serial1.begin(9600); + Serial1.println("Hello world!"); } void loop() { - COMM.println("Waiting for multiple input..."); - while (COMM.available() < 5) + Serial1.println("Waiting for multiple input..."); + while (Serial1.available() < 5) ; - COMM.println(COMM.read()); - COMM.println(COMM.read()); - COMM.flush(); + Serial1.println(Serial1.read()); + Serial1.println(Serial1.read()); + Serial1.flush(); - if (COMM.available()) { - COMM.println("FAIL! Still had junk in the buffer..."); + if (Serial1.available()) { + Serial1.println("FAIL! Still had junk in the buffer..."); } } diff --git a/examples/test-serialusb.cpp b/examples/test-serialusb.cpp index 678c2b9..15ab913 100644 --- a/examples/test-serialusb.cpp +++ b/examples/test-serialusb.cpp @@ -3,27 +3,24 @@ #include "wirish.h" #include "usb.h" -#define LED_PIN BOARD_LED_PIN -#define BUT_PIN BOARD_BUTTON_PIN - -uint32 state = 0; #define QUICKPRINT 0 #define BIGSTUFF 1 #define NUMBERS 2 #define SIMPLE 3 #define ONOFF 4 +uint32 state = 0; + void setup() { /* Set up the LED to blink */ - pinMode(LED_PIN, OUTPUT); - - /* Set up the Button */ - pinMode(BUT_PIN, INPUT); + pinMode(BOARD_LED_PIN, OUTPUT); + /* Set up Serial2 for use as a debug channel */ Serial2.begin(9600); - Serial2.println("This is the debug channel. Press BUT."); - - waitForButtonPress(0); + Serial2.println("This is the debug channel. Press any key."); + while (!Serial2.available()) + ; + Serial2.read(); } uint8 c1 = '-'; @@ -32,14 +29,15 @@ void loop() { toggleLED(); delay(1000); - if(isButtonPressed()) { + if (Serial2.available()) { + Serial2.read(); state++; } - switch(state) { + switch (state) { case QUICKPRINT: - for(int i = 0; i<30; i++) { - usbSendBytes(&c1,1); + for (int i = 0; i < 30; i++) { + usbSendBytes(&c1, 1); SerialUSB.print('.'); SerialUSB.print('|'); } @@ -121,9 +119,8 @@ __attribute__((constructor)) void premain() { int main(void) { setup(); - while (1) { + while (true) { loop(); } return 0; } - diff --git a/examples/test-systick.cpp b/examples/test-systick.cpp index bbf0adf..78c7307 100644 --- a/examples/test-systick.cpp +++ b/examples/test-systick.cpp @@ -1,16 +1,11 @@ // Tests the SysTick enable/disable functions -// + #include "wirish.h" #include "systick.h" -#define LED_PIN BOARD_LED_PIN -#define PWM_PIN 2 -#define BUT BOARD_BUTTON_PIN - void setup() { - /* Set up the LED to blink */ - pinMode(LED_PIN, OUTPUT); - pinMode(BUT, INPUT); + pinMode(BOARD_LED_PIN, OUTPUT); + pinMode(BOARD_BUTTON_PIN, INPUT); } bool disable = true; @@ -24,7 +19,7 @@ void loop() { for(i = 0; i < 150000; i++) ; - if(isButtonPressed()) { + if (isButtonPressed()) { if (disable) { systick_disable(); SerialUSB.println("Disabling SysTick"); @@ -47,7 +42,7 @@ __attribute__((constructor)) void premain() { int main(void) { setup(); - while (1) { + while (true) { loop(); } return 0; diff --git a/examples/test-timers.cpp b/examples/test-timers.cpp index a4fbc8a..545597f 100644 --- a/examples/test-timers.cpp +++ b/examples/test-timers.cpp @@ -26,7 +26,7 @@ uint16 val2 = 10000; uint16 val3 = 10000; uint16 val4 = 10000; -// FIXME high density timer test (especially basic timers + DAC) +// FIXME [0.1.0] high density timer test (especially basic timers + DAC) timer_dev *timers[] = {TIMER1, TIMER2, TIMER3, TIMER4}; voidFuncPtr handlers[] = {handler1, handler2, handler3, handler4}; @@ -46,7 +46,6 @@ void setup() { // Send a message out Serial2 Serial2.begin(115200); Serial2.println("*** Initializing timers..."); - Serial2.println("foo"); timer_foreach(initTimer); Serial2.println("*** Done. Beginning timer test."); } @@ -110,7 +109,7 @@ void loop() { testSetTimerPeriod(30000); Serial2.println("Sanity check (with hand-coded reload and prescaler for " - "72 MHz timers):"); + "72 MHz timers):"); timer_set_mode(TIMER4, TIMER_CH1, TIMER_OUTPUT_COMPARE); timer_set_prescaler(TIMER4, 33); timer_set_reload(TIMER4, 65454); @@ -238,7 +237,7 @@ void testTimerChannels(timer_dev *dev) { } } -// FIXME move this into the new wirish timer implementation +// FIXME [0.0.10] move this into the new wirish timer implementation void setTimerPeriod(timer_dev *dev, uint32 period_us) { if (!period_us) { // FIXME handle this case @@ -291,7 +290,7 @@ __attribute__((constructor)) void premain() { int main(void) { setup(); - while (1) { + while (true) { loop(); } return 0; diff --git a/examples/vga-leaf.cpp b/examples/vga-leaf.cpp index 9d9fce2..e663639 100644 --- a/examples/vga-leaf.cpp +++ b/examples/vga-leaf.cpp @@ -28,18 +28,12 @@ Created 20 July 2010 By Bryan Newbold for LeafLabs This code is released with no strings attached. - - Modified 4 March 2011 - By Marti Bolivar - Disabled SysTick, kept up-to-date with libmaple. */ // FIXME: generalize for Native and Mini #include "wirish.h" -#define LED_PIN BOARD_LED_PIN - // Pinouts -- you also must change the GPIO macros below if you change // these #define VGA_R 6 // STM32: A8 @@ -110,7 +104,7 @@ uint32 logo[y_max][x_max] = { void setup() { // Setup our pins - pinMode(LED_PIN, OUTPUT); + pinMode(BOARD_LED_PIN, OUTPUT); pinMode(VGA_R, OUTPUT); pinMode(VGA_G, OUTPUT); pinMode(VGA_B, OUTPUT); @@ -166,7 +160,6 @@ void loop() { // Everything happens in the interrupts! } - // This ISR will end horizontal sync for most of the image and // setup the vertical sync for higher line counts void isr_porch(void) { @@ -174,22 +167,22 @@ void isr_porch(void) { y++; logo_y = map(y, 0, 478, 0, y_max); // Back to the top - if(y >= 523) { + if (y >= 523) { y = 1; logo_y = 0; v_active = true; return; } // Other vsync stuff below the image - if(y >= 492) { + if (y >= 492) { VGA_V_HIGH; return; } - if(y >= 490) { + if (y >= 490) { VGA_V_LOW; return; } - if(y >= 479) { + if (y >= 479) { v_active = false; return; } @@ -207,7 +200,7 @@ void isr_start(void) { VGA_R_HIGH; // For each "pixel", go ON_COLOR or OFF_COLOR - for(x = 0; x < 16; x++) { + for (x = 0; x < 16; x++) { // setting the color several times is just an easy way to // delay, so the image is wider. if you only do the following // once, you'll be able to make the logo array a lot wider: @@ -242,7 +235,7 @@ __attribute__((constructor)) void premain() { int main(void) { setup(); - while (1) { + while (true) { loop(); } return 0; diff --git a/examples/vga-scope.cpp b/examples/vga-scope.cpp index 66e72e9..9aa8bcb 100644 --- a/examples/vga-scope.cpp +++ b/examples/vga-scope.cpp @@ -40,7 +40,6 @@ // FIXME: generalize for Native and Mini -#define LED_PIN BOARD_LED_PIN #define ANALOG_PIN 18 // Pinouts -- you also must change the GPIO macros below if you change @@ -90,9 +89,9 @@ void isr_stop(void); void isr_update(void); void setup() { - pinMode(LED_PIN, OUTPUT); + pinMode(BOARD_LED_PIN, OUTPUT); pinMode(ANALOG_PIN, INPUT_ANALOG); - digitalWrite(LED_PIN, 1); + digitalWrite(BOARD_LED_PIN, 1); pinMode(VGA_R, OUTPUT); pinMode(VGA_G, OUTPUT); pinMode(VGA_B, OUTPUT); @@ -138,26 +137,26 @@ void setup() { uint16 y = 0; uint16 val = 0; bool v_active = true; -const uint16 x_max = 60; // empirically (and lazily) determined +const uint16 x_max = 60; // empirically (and sloppily) determined void isr_porch(void) { VGA_H_HIGH; y++; val = map(analogRead(ANALOG_PIN), 0, 4095, 0, x_max); - if(y >= 523) { + if (y >= 523) { y = 1; v_active = true; return; } - if(y >= 492) { + if (y >= 492) { VGA_V_HIGH; return; } - if(y >= 490) { + if (y >= 490) { VGA_V_LOW; return; } - if(y >= 479) { + if (y >= 479) { v_active = false; return; } @@ -199,7 +198,7 @@ __attribute__((constructor)) void premain() { int main(void) { setup(); - while (1) { + while (true) { loop(); } return 0; -- cgit v1.2.3