# board.h

Go to the documentation of this file.

    00001 /* Copyright (c) 2007-2009 Axel Wachtler
    00002    All rights reserved.
    00003
    00004    Redistribution and use in source and binary forms, with or without
    00005    modification, are permitted provided that the following conditions
    00006    are met:
    00007
    00008    * Redistributions of source code must retain the above copyright
    00009      notice, this list of conditions and the following disclaimer.
    00010    * Redistributions in binary form must reproduce the above copyright
    00011      notice, this list of conditions and the following disclaimer in the
    00012      documentation and/or other materials provided with the distribution.
    00013    * Neither the name of the authors nor the names of its contributors
    00014      may be used to endorse or promote products derived from this software
    00015      without specific prior written permission.
    00016
    00017    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    00018    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00019    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00020    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    00021    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    00022    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    00023    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    00024    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    00025    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    00026    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    00027    POSSIBILITY OF SUCH DAMAGE. */
    00028
    00029 /* $Id$ */
    00035 #ifndef BOARD_H
    00036 #define BOARD_H
    00037
    00038 #ifndef F_CPU
    00039 # error "F_CPU is undefined"
    00040 #endif
    00041
    00042 /* === includes ============================================================ */
    00043 #include <stdlib.h>
    00044 #include <stdint.h>
    00045 #include <inttypes.h>
    00046 #include <avr/io.h>
    00047 #include <avr/sleep.h>
    00048 #include <avr/interrupt.h>
    00049 #include <util/delay.h>
    00050 #include <avr/pgmspace.h>
    00051 #include <avr/eeprom.h>
    00052 #include <util/crc16.h>
    00053 #include "const.h"
    00054 #include "board_cfg.h"
    00055
    00061 /* === macros ============================================================== */
    00074 #define DELAY_US(x)  _delay_ms(x/1000.0)
    00075
    00078 #define DELAY_MS(x)  _delay_ms(x)
    00079
    00080 #ifndef PULLUP_KEYS
    00081
    00084 # define PULLUP_KEYS (0)
    00085 #endif
    00086
    00087
    00091 #define SLEEP_ON_IDLE()\
    00092         do{\
    00093             set_sleep_mode(SLEEP_MODE_IDLE);\
    00094             sleep_mode();\
    00095         }while(0);
    00096
    00097
    00098 #ifdef NO_TIMER
    00099 //# define HAVE_MALLOC_TIMERS
    00101 # define TIMER_POOL_SIZE  (0)
    00102 # define TIMER_INIT() do{}while(0)
    00103 # define TIMER_IRQ   TIMER1_OVF_vect
    00104 #endif
    00105
    00106 #ifndef HIF_TYPE
    00107
    00108 # define NO_HIF (1)
    00109 # define HIF_TYPE (HIF_NONE)
    00110 #endif
    00111
    00112 #ifndef HIF_IO_ENABLE
    00113
    00114 # define HIF_IO_ENABLE() do{}while(0)
    00115 #endif
    00116
    00117 #define HIF_TYPE_IS_UART  ((HIF_TYPE >= HIF_UART_0) && ( HIF_TYPE <= HIF_UART_1))
    00118 #define HIF_TYPE_IS_USB   ((HIF_TYPE == HIF_FT245) || (HIF_TYPE == HIF_AT90USB))
    00119
    00120 /* === Radio Control Pins === */
    00121 #ifndef TRX_RESET_INIT
    00122
    00123 # define TRX_RESET_INIT() DDR_TRX_RESET |= MASK_TRX_RESET
    00124 #endif
    00125
    00126 #ifndef TRX_RESET_HIGH
    00127
    00128 # define TRX_RESET_HIGH() PORT_TRX_RESET |= MASK_TRX_RESET
    00129 #endif
    00130
    00131 #ifndef TRX_RESET_LOW
    00132
    00133 # define TRX_RESET_LOW()  PORT_TRX_RESET &= ~MASK_TRX_RESET
    00134 #endif
    00135
    00136 #ifndef TRX_SLPTR_INIT
    00137
    00138 # define TRX_SLPTR_INIT() DDR_TRX_SLPTR |= MASK_TRX_SLPTR
    00139 #endif
    00140
    00141 #ifndef TRX_SLPTR_HIGH
    00142
    00143 # define TRX_SLPTR_HIGH() PORT_TRX_SLPTR |= MASK_TRX_SLPTR
    00144 #endif
    00145
    00146 #ifndef TRX_SLPTR_LOW
    00147
    00148 # define TRX_SLPTR_LOW()  PORT_TRX_SLPTR &= ~MASK_TRX_SLPTR
    00149 #endif
    00150
    00151 #if ! defined(DI_TRX_IRQ) && ! defined(EI_TRX_IRQ)
    00152   /* the functions (disable,enable)_all_trx_irqs are defined in atmga_rfa1.h */
    00153 # define DI_TRX_IRQ disable_all_trx_irqs
    00154 # define EI_TRX_IRQ enable_all_trx_irqs
    00155 #endif
    00156
    00157 #if defined (DBG_PORT) && defined (DBG_DDR) && defined (DBG_PIN)
    00158 # define DBG_INIT() do{DBG_DDR |= DBG_PIN; DBG_PORT &= ~DBG_PIN;}while(0)
    00159 # define DBG_SET() do{DBG_PORT |= DBG_PIN;}while(0)
    00160 # define DBG_CLR() do{DBG_PORT &= ~DBG_PIN;}while(0)
    00161 # define DBG_TOGGLE() do{DBG_PORT ^= DBG_PIN;}while(0)
    00162 #else
    00163 # define DBG_INIT() do{}while(0)
    00164 # define DBG_SET() do{}while(0)
    00165 # define DBG_CLR() do{}while(0)
    00166 # define DBG_TOGGLE() do{}while(0)
    00167 #endif
    00168 /* === types =============================================================== */
    00169
    00175 typedef struct
    00176 {
    00177     uint16_t short_addr;
    00178     uint16_t pan_id;
    00179     uint64_t ieee_addr;
    00180     uint8_t  channel;
    00181     uint8_t _reserved_[2];
    00182     uint8_t crc;
    00183 } node_config_t;
    00184
    00185 static inline uint8_t get_node_config(node_config_t *ncfg)
    00186 {
    00187     uint8_t i = sizeof(node_config_t);
    00188     uint8_t *pram = (uint8_t*)ncfg;
    00189     uint8_t crc = 0;
    00190     do
    00191     {
    00192     #if FLASHEND > 0xffffL
    00193         *pram = pgm_read_byte_far(((long)FLASHEND - i + 1));
    00194     #else
    00195         *pram = pgm_read_byte_near((FLASHEND - i + 1));
    00196     #endif
    00197
    00198         crc = _crc_ibutton_update(crc, *pram++);
    00199     }
    00200     while(--i);
    00201     return crc;
    00202 }
    00203
    00204 static inline uint8_t get_node_config_eeprom(node_config_t *ncfg, uint8_t * offset)
    00205 {
    00206     uint8_t i = sizeof(node_config_t);
    00207     uint8_t *pram = (uint8_t*)ncfg;
    00208     uint8_t crc = 0;
    00209     do
    00210     {
    00211         *pram = eeprom_read_byte( (const uint8_t *) offset++ );
    00212         crc = _crc_ibutton_update(crc, *pram++);
    00213     }
    00214     while(--i);
    00215     return crc;
    00216 }
    00217
    00218 static inline void jump_to_bootloader(void)
    00219 {
    00220     typedef void (*func_ptr_t)(void) __attribute__((noreturn));
    00221     const func_ptr_t jmp_boot = (func_ptr_t)(BOOT_LOADER_ADDRESS * 2);
    00222     jmp_boot();
    00223 }
    00224
    00225 /* === prototypes ========================================================== */
    00226 #ifdef __cplusplus
    00227 extern "C" {
    00228 #endif
    00229
    00230 #ifdef __cplusplus
    00231 } /* extern "C" */
    00232 #endif
    00233
    00235 #endif /* #ifndef BOARD_H */