- Main Page
- Related Pages
- Modules
- Data Structures
- Files

# wgpio.h

    00001 /* Copyright (c) 2007, 2008 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 #ifndef WGPIO_H
    00030 #define WGPIO_H
    00031
    00032 /* === includes ============================================================ */
    00033 #include "transceiver.h"
    00034 #include "ioutil.h"
    00035 #include "timer.h"
    00036
    00037 /* === macros ============================================================== */
    00038 #if (BOARD_TYPE == STB_230) || (BOARD_TYPE == STB_230B)
    00039     #define ACTOR_INIT() DDRE |= (1<<PE4)
    00040     #define ACTOR_ON()   PORTE |= (1<<PE4)
    00041     #define ACTOR_OFF()  PORTE &= ~(1<<PE4)
    00042 #else
    00043     #define ACTOR_INIT()
    00044     #define ACTOR_ON()
    00045     #define ACTOR_OFF()
    00046 #endif
    00047
    00048 #define PANID   (0x4f49)
    00049 #define CHANNEL (15)
    00050 #define FCTL_KEY_FRAME ( FCTL_DATA |  FCTL_IPAN  | FCTL_DST_SHORT | FCTL_SRC_SHORT )
    00051
    00052
    00053 #define SHORT_PRESS_TICKS  (MSEC(10))
    00054 #define LONG_PRESS_TICKS   (MSEC(1000))
    00055
    00056 #define NONE_PRESS   (0)
    00057 #define SHORT_PRESS  (1)
    00058 #define LONG_PRESS   (2)
    00059
    00060 #define IGNORE       (255)
    00061 /* === types =============================================================== */
    00065 typedef struct
    00066 {
    00067     uint8_t state;
    00068     uint8_t role;
    00069 } actor_ctx_t;
    00070
    00071 typedef enum SHORTENUM
    00072 {
    00073     NONE = 0x00,
    00074     CMD_SWITCH = 's',
    00075     CMD_STATUS = 'r',
    00076     CMD_TX_DONE_OK = 'T',
    00077     CMD_TX_DONE_NOK = 't',
    00078     CMD_QRY_STATUS = 'q',
    00079 } event_code_t;
    00080
    00081 typedef struct
    00082 {
    00083     event_code_t cmd;
    00084     uint8_t state;
    00085 } event_t;
    00086
    00087 typedef enum SHORTENUM
    00088 {
    00089     APP_RUNNING,
    00090     APP_STATUS_PENDING,
    00091     APP_CONFIG,
    00092     TX_SWITCH_IN_PROGRESS,
    00093     TX_STATUS_IN_PROGRESS,
    00094     TRX_SLEEP,
    00095     TRX_TRANSMIT,
    00096     TRX_RECEIVE,
    00097 } app_state_t;
    00098
    00099 typedef struct
    00100 {
    00101     uint16_t frmctl;
    00102     uint8_t  seq;
    00103     uint16_t dstpan;
    00104     uint16_t dstaddr;
    00105     uint16_t srcaddr;
    00106     event_code_t  cmd;
    00107     uint8_t  state;
    00108     uint16_t crc;
    00109 } key_frame_t;
    00110
    00111 typedef struct
    00112 {
    00113     uint8_t key;
    00114     uint8_t tmo;
    00115     uint8_t slpmode;
    00116     uint8_t qrycnt;
    00117     event_t rx;
    00118     event_t tx;
    00119     app_state_t appstate;
    00120 } app_ctx_t;
    00121
    00122 /* === prototypes ========================================================== */
    00123 #ifdef __cplusplus
    00124 extern "C" {
    00125 #endif
    00126 static void wgpio_init(void);
    00127 static void wgpio_idle(uint8_t idlemode);
    00128 static void wgpio_actor_update(uint8_t state);
    00129 static bool wgpio_send_status(event_code_t cmd, uint8_t state);
    00130
    00131 static void state_app_running(app_ctx_t *pevents);
    00132 static void state_tx_switch_in_progress(app_ctx_t *ctx);
    00133 static void state_tx_status_in_progress(app_ctx_t *ctx);
    00134 static void state_app_status_pending(app_ctx_t *ctx);
    00135 static void state_app_config(app_ctx_t *ctx);
    00136
    00137 static void status_led_handle(void);
    00138 static void status_led_config(uint8_t val, uint8_t blnk);
    00139
    00140 static inline void transceiver_init(uint8_t channel, uint16_t addr)
    00141 {
    00142
    00143     trx_io_init(DEFAULT_SPI_RATE);
    00144     LED_SET_VALUE(1);
    00145
    00146     trx_init();
    00147     LED_SET_VALUE(2);
    00148
    00149     if(trx_identify() != 1)
    00150     {
    00151         while(1);
    00152     };
    00153     LED_SET_VALUE(3);
    00154
    00155 #if defined(TRX_IRQ_TRX_END)
    00156     trx_reg_write(RG_IRQ_MASK, TRX_IRQ_TRX_END);
    00157 #elif defined(TRX_IRQ_RX_END) && defined(TRX_IRQ_TX_END)
    00158     trx_reg_write(RG_IRQ_MASK, TRX_IRQ_RX_END | TRX_IRQ_TX_END);
    00159 #else
    00160 #  error "Unknown TRX_END IRQ bits"
    00161 #endif
    00162     trx_bit_write(SR_TX_AUTO_CRC_ON, 1);
    00163     trx_bit_write(SR_CHANNEL, channel);
    00164     trx_set_shortaddr(addr);
    00165     trx_set_panid(PANID);
    00166
    00167     LED_SET_VALUE(0);
    00168     trx_reg_write(RG_TRX_STATE, CMD_RX_AACK_ON);
    00169 }
    00170
    00171 void inline transceiver_send_frame(uint8_t *pdata, uint8_t sz)
    00172 {
    00173     trx_reg_write(RG_TRX_STATE, CMD_FORCE_TRX_OFF);
    00174     trx_reg_write(RG_TRX_STATE, CMD_TX_ARET_ON);
    00175     trx_frame_write(sz, pdata);
    00176     TRX_SLPTR_HIGH();
    00177     TRX_SLPTR_LOW();
    00178 }
    00179
    00188 static uint8_t debounce_key0(void)
    00189 {
    00190 uint8_t ret, tmp;
    00191 static uint16_t ocnt=0;
    00192
    00193     ret = NONE_PRESS;
    00194     tmp = (KEY_GET() & 1);
    00195     if (tmp != 0)
    00196     {
    00197         ocnt ++;
    00198         if(ocnt > LONG_PRESS_TICKS)
    00199         {
    00200             ocnt = LONG_PRESS_TICKS;
    00201         }
    00202     }
    00203     else
    00204     {
    00205         if(ocnt > SHORT_PRESS_TICKS)
    00206         {
    00207             ret = SHORT_PRESS;
    00208         }
    00209         if(ocnt >= LONG_PRESS_TICKS)
    00210         {
    00211             ret = LONG_PRESS;
    00212         }
    00213         ocnt = 0;
    00214     }
    00215     return ret;
    00216 }
    00217
    00218 #ifdef __cplusplus
    00219 } /* extern "C" */
    00220 #endif
    00221 #endif  /* #ifndef WGPIO_H */