changeset 160:a498438cc538 main

Demonstration program for the "odr-sensor" board using a ATMega16m1.
author Bob Cook <bob@bobcookdev.com>
date Sun, 28 Oct 2012 16:27:12 -0700
parents 19626dab8d31
children 24c77b4f9c9d
files main/demos/avr/odr-sensor-demo/jamfile main/demos/avr/odr-sensor-demo/main.cpp main/demos/avr/odr-sensor-demo/project_defs.h
diffstat 3 files changed, 327 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/demos/avr/odr-sensor-demo/jamfile	Sun Oct 28 16:27:12 2012 -0700
@@ -0,0 +1,43 @@
+# -----------------------------------------------------------------------------------------
+#
+#   projects/odr-sensor-demo/jamfile
+#   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 /demo/avr/odr-sensor-demo/jamfile" ; }
+
+SubDir TOP demo avr odr-sensor-demo ;
+
+# -----------------------------------------------------------------------------------------
+
+avr_executable
+    odr-sensor-demo atmega16m1
+    : main.cpp
+      packages.avr.device.pkg
+      packages.common.util.pkg
+    ;
+
+# -----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/demos/avr/odr-sensor-demo/main.cpp	Sun Oct 28 16:27:12 2012 -0700
@@ -0,0 +1,241 @@
+// ----------------------------------------------------------------------------------------
+//
+//  Copyright (C) 2012 Bob Cook
+//    
+//  Bob Cook Development, Robotics Library
+//  http://www.bobcookdev.com/rl/
+// 
+//  Demonstration program for the odr-sensor board.
+//
+//  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/sleep.h>
+
+#include "project_defs.h"
+
+#include "packages/avr/device/int_helpers.h"
+#include "packages/avr/device/spinwait.h"
+
+#include "packages/common/util/misc.h"
+
+// ----------------------------------------------------------------------------------------
+
+static void leds_off()
+{
+    PORTB |= ( 1 << PB2 );
+    PORTC |= ( 1 << PC4 ) | ( 1 << PC5 );
+    PORTD |= ( 1 << PD7 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+static inline void led_red_off() __attribute__((always_inline));
+static inline void led_red_off()
+{
+    PORTC |= ( 1 << PC5 );
+}
+
+static inline void led_red_on() __attribute__((always_inline));
+static inline void led_red_on()
+{
+    PORTC &= ~( 1 << PC5 );
+}
+
+static inline void led_red_toggle() __attribute__((always_inline));
+static inline void led_red_toggle()
+{
+    PORTC ^= ( 1 << PC5 );
+}
+
+// ----------------------------------------------------------------------------------------
+
+static inline void led_yellow_off() __attribute__((always_inline));
+static inline void led_yellow_off()
+{
+    PORTC |= ( 1 << PC4 );
+}
+
+static inline void led_yellow_on() __attribute__((always_inline));
+static inline void led_yellow_on()
+{
+    PORTC &= ~( 1 << PC4 );
+}
+    
+static inline void led_yellow_toggle() __attribute__((always_inline));
+static inline void led_yellow_toggle()
+{
+    PORTC ^= ( 1 << PC4 );
+}
+    
+// ----------------------------------------------------------------------------------------
+
+static inline void led_green_2_off() __attribute__((always_inline));
+static inline void led_green_2_off()
+{
+    PORTB |= ( 1 << PB2 );
+}
+
+static inline void led_green_2_on() __attribute__((always_inline));
+static inline void led_green_2_on()
+{
+    PORTB &= ~( 1 << PB2 );
+}
+    
+static inline void led_green_2_toggle() __attribute__((always_inline));
+static inline void led_green_2_toggle()
+{
+    PORTB ^= ( 1 << PB2 );
+}
+    
+// ----------------------------------------------------------------------------------------
+
+static inline void led_green_1_off() __attribute__((always_inline));
+static inline void led_green_1_off()
+{
+    PORTD |= ( 1 << PD7 );
+}
+
+static inline void led_green_1_on() __attribute__((always_inline));
+static inline void led_green_1_on()
+{
+    PORTD &= ~( 1 << PD7 );
+}
+
+static inline void led_green_1_toggle() __attribute__((always_inline));
+static inline void led_green_1_toggle()
+{
+    PORTD ^= ( 1 << PD7 );
+}
+    
+// ----------------------------------------------------------------------------------------
+
+void hw_init()
+{
+    //--    Turn off all interrupts.
+
+    cli();
+    interrupts_clear_all();
+
+    //--    Indicator LEDs on PORTB.2, PORTC.4, PORTC.5, PORTD.7
+
+    DDRB |= ( 1 << PB2 );
+    DDRC |= ( 1 << PC4 ) | ( 1 << PC5 );
+    DDRD |= ( 1 << PD7 );
+    leds_off();
+
+    //--    Blink the LEDs just to say hello.
+    
+    for ( uint8_t i = 0; i < 5; i++ )
+    {
+        led_red_on();
+        led_yellow_on();
+        led_green_1_on();
+        led_green_2_on();
+        spinwait_delay_ms( 75 );
+        leds_off();
+        spinwait_delay_ms( 75 );
+    }
+
+    //--    Timer0 gives up a periodic "heartbeat" interrupt at ~100 Hz.
+
+    TCCR0A = 0
+
+        | ( 0 << COM0A1 ) | ( 0 << COM0A0 )                 // OC0A disconnected
+        | ( 0 << COM0B1 ) | ( 0 << COM0B0 )                 // OC0B disconnected
+        | ( 1 << WGM01  ) | ( 0 << WGM00  )                 // CTC mode
+        ;
+
+    TCCR0B = 0
+
+        | ( 0 << FOC0A ) | ( 0 << FOC0B )                   // no force output compare
+        | ( 0 << WGM02 )                                    // CTC mode
+        | ( 1 << CS02  ) | ( 0 << CS01  ) | ( 1 << CS00 )   // clk/1024
+        ;
+
+    OCR0A = 156;
+
+    TIMSK0 |= ( 1 << OCIE0A );
+
+    //--    Initialize the CAN hardware.
+
+//    if ( ! canmsg_init() )
+//    {
+//        odrson_fatal_error( odrson_fatal_error_can_init );
+//    }
+
+    //--    When we sleep we want the "idle" mode e.g. wake up on any interrupt.
+
+    set_sleep_mode( SLEEP_MODE_IDLE );
+
+    //--    Re-enable interrupts.
+
+    sei();
+}
+
+// ----------------------------------------------------------------------------------------
+
+ISR( TIMER0_COMPA_vect )
+{
+    static uint8_t count = 0;
+
+    ++count;
+}
+
+// ----------------------------------------------------------------------------------------
+
+int main()
+{
+    //--    Initialize the hardware and send a "hello we started" message.
+
+    hw_init();
+
+    //--    Blink once per half second.
+
+    for ( ;; )
+    {
+        led_green_1_on();
+        spinwait_delay_ms( 500 );
+        led_green_1_off();
+        spinwait_delay_ms( 500 );
+
+        led_green_2_on();
+        spinwait_delay_ms( 500 );
+        led_green_2_off();
+        spinwait_delay_ms( 500 );
+
+        led_yellow_on();
+        spinwait_delay_ms( 500 );
+        led_yellow_off();
+        spinwait_delay_ms( 500 );
+
+        led_red_on();
+        spinwait_delay_ms( 500 );
+        led_red_off();
+        spinwait_delay_ms( 500 );
+    }
+
+    return 0;
+}
+
+// ----------------------------------------------------------------------------------------
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/demos/avr/odr-sensor-demo/project_defs.h	Sun Oct 28 16:27:12 2012 -0700
@@ -0,0 +1,43 @@
+// ----------------------------------------------------------------------------------------
+//
+//  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/can
+
+#define PRJ_M1CAN_ENABLE
+#define PRJ_M1CAN_CANBUS_250_KHZ
+#define PRJ_M1CAN_TX_BUFFER_SIZE  8
+
+
+#endif // #if !defined( PROJECT_DEFS_H )
+// ----------------------------------------------------------------------------------------
+