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 (1)
    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_DEFAULT_BAUDRATE
    00107 # define HIF_DEFAULT_BAUDRATE (9600)
    00108 #endif
    00109
    00110 #ifndef HIF_TYPE
    00111
    00112 # define NO_HIF (1)
    00113 # define HIF_TYPE (HIF_NONE)
    00114 #endif
    00115
    00116 #ifndef HIF_IO_ENABLE
    00117
    00118 # define HIF_IO_ENABLE() do{}while(0)
    00119 #endif
    00120
    00121 #define HIF_TYPE_IS_UART  ((HIF_TYPE >= HIF_UART_0) && ( HIF_TYPE <= HIF_UART_1))
    00122 #define HIF_TYPE_IS_USB   ((HIF_TYPE == HIF_FT245) || (HIF_TYPE == HIF_AT90USB))
    00123
    00124 /* === Radio Control Pins === */
    00125 #ifndef TRX_RESET_INIT
    00126
    00127 # define TRX_RESET_INIT() DDR_TRX_RESET |= MASK_TRX_RESET
    00128 #endif
    00129
    00130 #ifndef TRX_RESET_HIGH
    00131
    00132 # define TRX_RESET_HIGH() PORT_TRX_RESET |= MASK_TRX_RESET
    00133 #endif
    00134
    00135 #ifndef TRX_RESET_LOW
    00136
    00137 # define TRX_RESET_LOW()  PORT_TRX_RESET &= ~MASK_TRX_RESET
    00138 #endif
    00139
    00140 #ifndef TRX_SLPTR_INIT
    00141
    00142 # define TRX_SLPTR_INIT() DDR_TRX_SLPTR |= MASK_TRX_SLPTR
    00143 #endif
    00144
    00145 #ifndef TRX_SLPTR_HIGH
    00146
    00147 # define TRX_SLPTR_HIGH() PORT_TRX_SLPTR |= MASK_TRX_SLPTR
    00148 #endif
    00149
    00150 #ifndef TRX_SLPTR_LOW
    00151
    00152 # define TRX_SLPTR_LOW()  PORT_TRX_SLPTR &= ~MASK_TRX_SLPTR
    00153 #endif
    00154
    00155 #if ! defined(DI_TRX_IRQ) && ! defined(EI_TRX_IRQ)
    00156   /* the functions (disable,enable)_all_trx_irqs are defined in atmga_rfa1.h */
    00157 # define DI_TRX_IRQ disable_all_trx_irqs
    00158 # define EI_TRX_IRQ enable_all_trx_irqs
    00159 #endif
    00160
    00161 #if defined (DBG_PORT) && defined (DBG_DDR) && defined (DBG_PIN)
    00162 # define DBG_INIT() do{DBG_DDR |= DBG_PIN; DBG_PORT &= ~DBG_PIN;}while(0)
    00163 # define DBG_SET() do{DBG_PORT |= DBG_PIN;}while(0)
    00164 # define DBG_CLR() do{DBG_PORT &= ~DBG_PIN;}while(0)
    00165 # define DBG_TOGGLE() do{DBG_PORT ^= DBG_PIN;}while(0)
    00166 #else
    00167 # define DBG_INIT() do{}while(0)
    00168 # define DBG_SET() do{}while(0)
    00169 # define DBG_CLR() do{}while(0)
    00170 # define DBG_TOGGLE() do{}while(0)
    00171 #endif
    00172
    00173
    00174 /* === WiBO Control === */
    00175 #if ! defined(NO_KEYS) && 0 == 1
    00176 # define WIBO_FLAVOUR_KEYPRESS (1)
    00177 # define WIBO_FLAVOUR_KEYPRESS_KEYNB (0)
    00178 #endif
    00179
    00180 #if defined(GPIOR0) && 0 == 1
    00181 # define WIBO_FLAVOUR_MAILBOX (1)
    00182 # define WIBO_FLAVOUR_MAILBOX_REGISTER (GPIOR0)
    00183 # define WIBO_FLAVOUR_MAILBOX_CODE (0xA5)
    00184 #endif
    00185
    00186 #if defined(WIBO_FLAVOUR_MAILBOX)
    00187 # define WIBO_MAILBOX_SET() \
    00188          do{WIBO_FLAVOUR_MAILBOX_REGISTER = WIBO_FLAVOUR_MAILBOX_CODE;}while(0);
    00189 # define WIBO_MAILBOX_CLR() \
    00190          do{WIBO_FLAVOUR_MAILBOX_REGISTER = ~WIBO_FLAVOUR_MAILBOX_CODE;}while(0);
    00191 #endif
    00192
    00193 /* === types =============================================================== */
    00194
    00203 typedef struct
    00204 {
    00206     uint16_t short_addr;
    00208     uint16_t pan_id;
    00210     uint64_t ieee_addr;
    00212     uint8_t  channel;
    00214     uint8_t _reserved_[2];
    00216     uint8_t crc;
    00217 } node_config_t;
    00218
    00228 static inline uint8_t get_node_config(node_config_t *ncfg)
    00229 {
    00230     uint8_t i = sizeof(node_config_t);
    00231     uint8_t *pram = (uint8_t*)ncfg;
    00232     uint8_t crc = 0, zcnt = 0;
    00233     do
    00234     {
    00235     #if FLASHEND > 0xffffL
    00236         *pram = pgm_read_byte_far(((long)FLASHEND - i + 1));
    00237     #else
    00238         *pram = pgm_read_byte_near((FLASHEND - i + 1));
    00239     #endif
    00240
    00241         crc = _crc_ibutton_update(crc, *pram);
    00242         zcnt += *pram ? 0 : 1;
    00243         pram ++;
    00244     }
    00245     while(--i);
    00246
    00247     /* return true if crc does not match or all */
    00248     if (zcnt == sizeof(node_config_t))
    00249     {
    00250         crc = 0x55;
    00251     }
    00252
    00253     return crc;
    00254 }
    00255
    00266 static inline uint8_t get_node_config_eeprom(node_config_t *ncfg, uint8_t * offset)
    00267 {
    00268     uint8_t i = sizeof(node_config_t);
    00269     uint8_t *pram = (uint8_t*)ncfg;
    00270     uint8_t crc = 0, zcnt = 0;
    00271     do
    00272     {
    00273         *pram = eeprom_read_byte( (const uint8_t *) offset++ );
    00274         crc = _crc_ibutton_update(crc, *pram);
    00275         zcnt += *pram ? 0 : 1;
    00276         pram ++;
    00277     }
    00278     while(--i);
    00279     if (zcnt == sizeof(node_config_t))
    00280     {
    00281         crc = 0x55;
    00282     }
    00283     return crc;
    00284 }
    00285
    00295 static inline void store_node_config_eeprom(node_config_t *ncfg, uint8_t * offset)
    00296 {
    00297     uint8_t i = sizeof(node_config_t) - sizeof(uint8_t);
    00298     uint8_t *pram = (uint8_t*)ncfg;
    00299     uint8_t crc = 0;
    00300     do
    00301     {
    00302         eeprom_write_byte( (uint8_t *)offset++, *pram );
    00303         crc = _crc_ibutton_update(crc, *pram++);
    00304     }
    00305     while(--i);
    00306     eeprom_write_byte((uint8_t *)offset, crc );
    00307 }
    00308
    00312 static inline void jump_to_bootloader(void)
    00313 {
    00314     typedef void (*func_ptr_t)(void) __attribute__((noreturn));
    00315     const func_ptr_t jmp_boot = (func_ptr_t) BOOT_LOADER_ADDRESS;
    00316 #if defined(WIBO_FLAVOUR_MAILBOX)
    00317     WIBO_MAILBOX_SET();
    00318 #endif
    00319     jmp_boot();
    00320 }
    00321
    00322 /* === prototypes ========================================================== */
    00323 #ifdef __cplusplus
    00324 extern "C" {
    00325 #endif
    00326
    00327 #ifdef __cplusplus
    00328 } /* extern "C" */
    00329 #endif
    00330
    00332 #endif /* #ifndef BOARD_H */