00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef WGPIO_H
00030 #define WGPIO_H
00031
00032
00033 #include "transceiver.h"
00034 #include "ioutil.h"
00035 #include "timer.h"
00036
00037
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
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
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 }
00220 #endif
00221 #endif