changeset 167:827befd71be0 main

Demonstration & test jig for the LS7366 support code.
author Bob Cook <bob@bobcookdev.com>
date Tue, 01 Jan 2013 11:28:23 -0800
parents 586104541d77
children cf51b02db081
files main/demos/avr/ls7366-demo/jamfile main/demos/avr/ls7366-demo/main.cpp main/demos/avr/ls7366-demo/project_defs.h
diffstat 3 files changed, 273 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/demos/avr/ls7366-demo/jamfile	Tue Jan 01 11:28:23 2013 -0800
@@ -0,0 +1,44 @@
+# -----------------------------------------------------------------------------------------
+#
+#   Copyright (C) 2012 Bob Cook
+#
+#   Bob Cook Development, Robotics Library
+#   http://www.bobcookdev.com/rl/
+#    
+#   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 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.
+#
+# -----------------------------------------------------------------------------------------
+
+if $(TRACE) { echo "trace /demos/avr/ls7366-demo/jamfile" ; }
+
+SubDir TOP demos avr ls7366-demo ;
+
+# -----------------------------------------------------------------------------------------
+
+avr_executable
+    ls7366-demo atmega16m1
+    : main.cpp
+      packages.avr.device.pkg
+      packages.avr.redir.pkg
+      packages.avr.sensors.ls7366.pkg
+      packages.common.util.pkg
+    ;
+
+# -----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/demos/avr/ls7366-demo/main.cpp	Tue Jan 01 11:28:23 2013 -0800
@@ -0,0 +1,161 @@
+// ----------------------------------------------------------------------------------------
+//
+//  Copyright (C) 2012 Bob Cook
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+// 
+//  Demo for the LSI LS7366 Quadrature Decoder / Counter
+//
+//  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 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.
+//
+// ----------------------------------------------------------------------------------------
+
+#include <avr/interrupt.h>
+#include <avr/io.h>
+#include <avr/pgmspace.h>
+#include <avr/sleep.h>
+
+#include <ctype.h>
+#include <stdio.h>
+
+#include "project_defs.h"
+
+#include "packages/avr/device/int_helpers.h"
+#include "packages/avr/device/spinwait.h"
+
+#include "packages/avr/sensors/ls7366/ls7366.h"
+
+#include "packages/avr/redir/redir.h"
+
+#include "packages/common/util/misc.h"
+
+// always after other includes
+#include "packages/avr/device/workaround34734.h"
+
+// ----------------------------------------------------------------------------------------
+
+static inline void led_off() __attribute__((always_inline));
+static inline void led_off()
+{
+    PORTB &= ~( 1 << PB2 );
+}
+
+static inline void led_on() __attribute__((always_inline));
+static inline void led_on()
+{
+    PORTB |= ( 1 << PB2 );
+}
+
+static inline void led_toggle() __attribute__((always_inline));
+static inline void led_toggle()
+{
+    PORTB ^= ( 1 << PB2 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+void hw_init()
+{
+    //--    Turn off all interrupts.
+
+    cli();
+    interrupts_clear_all();
+
+    //--    Indicator LED on PORTB.2
+
+    DDRB |= ( 1 << PB2 );
+    led_off();
+
+    //--    Blink the LED just to say hello.
+    
+    for ( uint8_t i = 0; i < 5; i++ )
+    {
+        led_on();
+        spinwait_delay_ms( 75 );
+        led_off();
+        spinwait_delay_ms( 75 );
+    }
+
+    //--    Initialize the serial output.
+
+    redir_initialize();
+
+    //--    Initialize the LS7366 chip(s).
+
+    DDRD |= ( 1 << PD3 ); // this is the SS pin, required to be an output for SPI master
+    ls7366_init();
+
+    //--    Re-enable interrupts.
+
+    sei();
+}
+
+// ----------------------------------------------------------------------------------------
+
+int main()
+{
+    //--    Initialize the hardware and send a "hello we started" message.
+
+    hw_init();
+    printf_P( PSTR("\n\nStarting the LS7366 demonstration\n\n") );
+
+    uint32_t cntr = 0;
+    uint32_t cntr_last = 0;
+
+    uint32_t last_diffs[ 10 ];
+    uint32_t index = 0;
+
+    for ( ;; )
+    {
+        cntr = ls7366_read_counter_1();
+
+        uint8_t  dir;
+        uint32_t diff = ls7366_compute_diff( cntr_last, cntr, &dir );
+
+        last_diffs[ index++ ] = diff;
+        index %= 10;
+
+        if ( index == 0 )
+        {
+            uint32_t sum = 0;
+
+            for ( uint8_t i = 0; i < 10; ++i )
+            {
+                sum += last_diffs[ i ];
+            }
+
+            uint32_t avg = ( sum + 51 ) / 100;
+
+            printf_P( PSTR("\r average %ld (%ld per second) dir: %s        "),
+                      avg,
+                      sum,
+                      ( dir == 0 ? "up" : "down" ) );
+        }
+
+        cntr_last = cntr;
+
+        spinwait_delay_ms( 100 );
+    }
+
+    return 0;
+}
+
+// ----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/demos/avr/ls7366-demo/project_defs.h	Tue Jan 01 11:28:23 2013 -0800
@@ -0,0 +1,68 @@
+// ----------------------------------------------------------------------------------------
+//
+//  Copyright (C) 2012 Bob Cook
+//
+//  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 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.
+//
+// ----------------------------------------------------------------------------------------
+
+#if !defined( PROJECT_DEFS_H )
+#define PROJECT_DEFS_H
+
+// ----------------------------------------------------------------------------------------
+//  packages/avr/common
+
+#define PRJ_CPU_FREQ    16000000L
+
+// ----------------------------------------------------------------------------------------
+//  packages/avr/device
+
+#define PRJ_UART0_USE_POLLED_MODE
+
+// ----------------------------------------------------------------------------------------
+//  packages/avr/redir
+
+#define PRJ_REDIR_VIA_UART0
+#define PRJ_REDIR_UART_BAUDRATE         19200
+#define PRJ_REDIR_OUTPUT_DO_AUTO_CRLF
+
+// ----------------------------------------------------------------------------------------
+//  packages/avr/spi
+
+#define PRJ_SPI_ENABLE_MASTER
+#define PRJ_SPI_BUS_MODE_0
+#define PRJ_SPI_DATA_DIRECTION_MSB
+#define PRJ_SPI_CLOCK_FACTOR_DIV_16
+
+// ----------------------------------------------------------------------------------------
+//  packages/avr/sensors/ls7366
+
+#define PRJ_LS7366_ENABLE
+
+#define PRJ_LS7366_ENABLE_CHIP_1
+#define PRJ_LS7366_SELECT_1_DDR     DDRC
+#define PRJ_LS7366_SELECT_1_PORT    PORTC
+#define PRJ_LS7366_SELECT_1_PIN     PC0
+
+#define PRJ_LS7366_INC_COMPUTE_DIFF
+
+
+#endif // #if !defined( PROJECT_DEFS_H )
+// ----------------------------------------------------------------------------------------
+