# board_stbrfa1.h

Go to the documentation of this file.

    00001 /* Copyright (c) 2011 Joerg Wunsch
    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$ */
    00118 #if defined(stb128rfa1)
    00119 # define BOARD_TYPE (BOARD_STB128RFA1)
    00120 # define BOARD_NAME "stb128rfa1"
    00121 # define RADIO_TYPE (RADIO_ATMEGA128RFA1_C)
    00122 #endif
    00123
    00124 #ifndef BOARD_STBRFA1_H
    00125 #define BOARD_STBRFA1_H
    00126
    00127 /*=== Compile time parameters ========================================*/
    00128
    00129 #ifndef DEFAULT_SPI_RATE
    00130 # define DEFAULT_SPI_RATE  (SPI_RATE_1_2)
    00131 #endif
    00132
    00133 /*=== radio interface definition =====================================*/
    00134 #if BOARD_TYPE == BOARD_STB230 || BOARD_TYPE == BOARD_STB230B
    00135 # include "base_rdk230.h"
    00136 #else
    00137 # include "base_rdk2xx.h"
    00138 #endif
    00139
    00140 /*
    00141  * The IO subsystem of the Sensor Terminal Board is, a little
    00142  * cumbersome to operate.  Besides the LEDs that are operated on an
    00143  * MMIO register and cannot be read back, the FT245 is attached to the
    00144  * external memory bus through a 74VHC245MTC bus driver which is by
    00145  * default enabled to drive the bus (rather than to read it).  Thus,
    00146  * much care must be taken to avoid a bus contention where both, the
    00147  * 74VHC245MTC and the AVR drive the multiplexed address/data bus.
    00148  *
    00149  * We avoid that bus contention by swapping the meaning of the chip
    00150  * select signal for the FT245 and the actual /WR line: both are ORed
    00151  * together, so for the actual effect towards the FT245, it does not
    00152  * matter whether we pulse /WR, or we pulse the correct high address
    00153  * selection.
    00154  */
    00155
    00156 static inline void hif_mmio_init(void)
    00157 {
    00158     DDRD |= 0xc0;               /* xmem A14...A15 */
    00159     PORTD = (PORTD & ~0xc0) | 0x80; /* high addr 0x80 => external
    00160                                      * SRAM */
    00161     PORTE &= ~0x10;             /* PE4: xmem /WR, activate to make the
    00162                                  * 74VHC245MTC not drive the bus */
    00163     PORTE |= 0x20;              /* PE5: xmem /RD, deactivate */
    00164     DDRE |= 0x30;               /* make /RD and /WR outputs */
    00165     PORTG &= ~0x04;             /* PG2: xmem ALE; not used but keep
    00166                                  * low all the time */
    00167     DDRG |= 0x04;
    00168 }
    00169
    00170 static inline void hif_usb_write(uint8_t val)
    00171 {
    00172     DDRB = 0xFF;
    00173     PORTB = val;
    00174     PORTD = (PORTD & ~0xc0);    /* select FT245 -> /WR pulse */
    00175     __asm volatile("nop");
    00176     PORTD = (PORTD & ~0xc0) | 0x80; /* re-select SRAM */
    00177     DDRB = 0;
    00178     PORTB = 0;                  /* avoid pullups */
    00179 }
    00180
    00181 static inline uint8_t hif_usb_read(void)
    00182 {
    00183     PORTE |= 0x10;              /* de-assert /WR */
    00184     PORTD = (PORTD & ~0xc0);    /* select FT245 */
    00185     PORTE &= ~0x20;             /* /RD pulse */
    00186     PORTE |= 0x20;
    00187     __asm volatile("nop");
    00188     uint8_t rv = PINB;
    00189     PORTD = (PORTD & ~0xc0) | 0x80; /* re-select SRAM */
    00190     PORTE &= ~0x10;                 /* re-assert /WR */
    00191
    00192     return rv;
    00193 }
    00194
    00195
    00196 static inline void hif_led_write(uint8_t val)
    00197 {
    00198     PORTE |= 0x10;              /* de-assert /WR */
    00199     DDRB = 0xFF;
    00200     PORTB = val;
    00201     PORTD = (PORTD & ~0xc0) | 0x40; /* select IO address */
    00202     PORTE &= ~0x10;                 /* /WR pulse */
    00203     PORTE |= 0x10;
    00204     __asm volatile("nop");
    00205     PORTD = (PORTD & ~0xc0) | 0x80; /* re-select SRAM */
    00206     DDRB = 0;
    00207     PORTB = 0;                  /* avoid pullups */
    00208     PORTE &= ~0x10;             /* re-assert /WR */
    00209 }
    00210
    00211 static inline uint8_t hif_key_read(void)
    00212 {
    00213     PORTE |= 0x10;              /* de-assert /WR */
    00214     PORTD = (PORTD & ~0xc0) | 0x40; /* select IO address */
    00215     PORTE &= ~0x20;             /* /RD pulse */
    00216     PORTE |= 0x20;
    00217     __asm volatile("nop");
    00218     uint8_t rv = PINB;
    00219     PORTD = (PORTD & ~0xc0) | 0x80; /* re-select SRAM */
    00220     PORTE &= ~0x10;                 /* re-assert /WR */
    00221
    00222     return rv;
    00223 }
    00224
    00225
    00226
    00227 /*=== LED access macros ==============================================*/
    00228 #if !defined(USE_RCB_LEDS)
    00229 /*=== use LEDs on STB (Memory Mapped) ===*/
    00230
    00231 # define LED_SHADOW    GPIOR2
    00232 # define LED_MASK      (0x03)
    00233 # define LED_SHIFT     (0)
    00234 # define LEDS_INVERSE  (1)
    00235 # define LED_NUMBER    (2)
    00236
    00237 # define LED_INIT()\
    00238         do{\
    00239             hif_mmio_init(); \
    00240             LED_SHADOW = LED_MASK;\
    00241             hif_led_write(LED_SHADOW);        \
    00242         }while(0)
    00243
    00244 # define LED_SET_VALUE(x) \
    00245         do{\
    00246             LED_SHADOW = (LED_SHADOW & ~LED_MASK) | ((~x<<LED_SHIFT) & LED_MASK);\
    00247             hif_led_write(LED_SHADOW);\
    00248         }while(0)
    00249
    00250 # define LED_GET_VALUE()\
    00251         ((~LED_SHADOW & LED_MASK) >> LED_SHIFT)
    00252
    00253 # define LED_SET(ln)\
    00254         do{\
    00255             LED_SHADOW &= ~(_BV(ln+LED_SHIFT) & LED_MASK);\
    00256             hif_led_write(LED_SHADOW);\
    00257         }while(0)
    00258
    00259 # define LED_CLR(ln)\
    00260         do{\
    00261             LED_SHADOW |= (_BV(ln+LED_SHIFT) & LED_MASK);\
    00262             hif_led_write(LED_SHADOW);\
    00263         }while(0)
    00264
    00265 # define LED_VAL(msk,val)\
    00266         do{\
    00267             LED_SHADOW &= ~(LED_MASK|(msk<<LED_SHIFT)); \
    00268             LED_SHADOW |= ~(val & (LED_MASK|msk));\
    00269             hif_led_write(LED_SHADOW);\
    00270         }while(0)
    00271
    00272
    00273 # define LED_TOGGLE(ln)\
    00274         do{\
    00275             LED_SHADOW ^= (_BV(ln+LED_SHIFT) & LED_MASK);\
    00276             hif_led_write(LED_SHADOW);\
    00277         }while(0)
    00278
    00279 #else
    00280 /*=== use LEDs on RCB (IO Mapped) ===*/
    00281 # define LED_PORT      PORTE
    00282 # define LED_DDR       DDRE
    00283 # define LED_MASK      (0x1c)
    00284 # define LED_SHIFT     (2)
    00285 # define LEDS_INVERSE  (1)
    00286 # define LED_NUMBER    (3)
    00287 #endif
    00288 /*=== KEY access macros ==============================================*/
    00289 #define PIN_KEY       (hif_key_read())
    00290 #define MASK_KEY      (0x1)
    00291 #define SHIFT_KEY     (0)
    00292 #define INVERSE_KEYS  (0)
    00293 #define PULLUP_KEYS   (0)
    00294 #define KEY_INIT      hif_mmio_init
    00295
    00296 /*=== Host Interface ================================================*/
    00297 #define HIF_TYPE      (HIF_FT245)
    00298 #define HIF_IO_ENABLE hif_mmio_init
    00299 #define HIF_USB_READ()  hif_usb_read()
    00300 #define HIF_USB_WRITE(x) hif_usb_write(x)
    00301 #define HIF_NO_DATA   (0x0100)
    00302 #define FT245_DDR    DDRE
    00303 #define FT245_PIN    PINE
    00304 #define FT245_TXE    _BV(6)
    00305 #define FT245_RXF    _BV(7)
    00306 #define FT245_INIT() do { \
    00307            FT245_DDR &= ~(FT245_TXE|FT245_RXF);\
    00308         } while(0)
    00309
    00310 #define FT245_TX_IS_BLOCKED()    (0 != (FT245_PIN & FT245_TXE))
    00311 #define FT245_RX_HAS_DATA()      (0 == (FT245_PIN & FT245_RXF))
    00312
    00313 /*=== TIMER Interface ===============================================*/
    00314 #define HWTMR_PRESCALE  (1)
    00315 #define HWTIMER_TICK    ((1.0*HWTMR_PRESCALE)/F_CPU)
    00316 #define HWTIMER_TICK_NB (0xFFFFUL+1)
    00317 #define HWTIMER_REG     (TCNT1)
    00318 #define TIMER_TICK      (HWTIMER_TICK_NB * HWTIMER_TICK)
    00319 #define TIMER_POOL_SIZE     (4)
    00320
    00322 #define TIMER_IRQ_vect   TIMER1_OVF_vect
    00323
    00333 # define TIMER_INIT() \
    00334     do{ \
    00335         TCCR1B |= _BV(CS10); \
    00336         TIMSK1 |= _BV(TOIE1); \
    00337     }while(0)
    00338
    00339 #endif /* BOARD_STB_H*/