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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00048 #ifndef IOUTIL_H
00049 #define IOUTIL_H
00050
00051
00052 #include <stdlib.h>
00053 #include <stdio.h>
00054
00055
00056 #include "board.h"
00057 #include "hif.h"
00058 #include "timer.h"
00059
00060
00061
00062
00063
00071 typedef struct
00072 {
00074 void * next;
00076 uint8_t used;
00078 uint8_t len;
00080 uint8_t istart;
00082 uint8_t iend;
00084 uint8_t data[];
00085 } buffer_t;
00086
00087
00088 typedef struct
00089 {
00091 uint8_t nb;
00093 uint8_t elsz;
00095 uint8_t pool[];
00096 } buffer_pool_t;
00097
00100
00101
00102
00103
00107 #if defined(NO_LEDS) || defined (DOXYGEN)
00108 # undef LED_NUMBER
00109
00110 # define LED_NUMBER (0)
00111 #endif
00112
00113 #if defined(NO_LEDS) || defined (DOXYGEN)
00114
00115 # define LED_INIT() do{}while(0)
00116 #elif !defined(LED_INIT)
00117 # if LEDS_INVERSE == 0
00118 # define LED_INIT() LED_DDR |= (LED_MASK); LED_PORT &= ~(LED_MASK)
00119 # else
00120 # define LED_INIT() LED_DDR |= (LED_MASK); LED_PORT |= (LED_MASK)
00121 # endif
00122 #endif
00123
00124 #if defined(NO_LEDS) || defined (DOXYGEN)
00125
00130 # define LED_SET_VALUE(x) do{}while(0)
00131 #elif !defined(LED_SET_VALUE)
00132 # if LEDS_INVERSE == 0
00133 # define LED_SET_VALUE(x) \
00134 do {\
00135 LED_PORT = (LED_PORT & ~LED_MASK) | ((x<<LED_SHIFT) & LED_MASK);\
00136 }while(0)
00137 # else
00138 # define LED_SET_VALUE(x) do {\
00139 LED_PORT = (LED_PORT & ~LED_MASK) | ((~x<<LED_SHIFT) & LED_MASK);\
00140 }while(0)
00141 # endif
00142 #endif
00143
00144 #if defined(NO_LEDS) || defined (DOXYGEN)
00145
00146 # define LED_GET_VALUE() 0
00147 #elif !defined(LED_GET_VALUE)
00148 # if LEDS_INVERSE == 0
00149 # define LED_GET_VALUE() ((LED_PORT & LED_MASK) >> LED_SHIFT)
00150 # else
00151 # define LED_GET_VALUE() ((~LED_PORT & LED_MASK) >> LED_SHIFT)
00152 # endif
00153 #endif
00154
00155
00156 #if defined(NO_LEDS) || defined (DOXYGEN)
00157
00158 # define LED_SET(ln) do{}while(0)
00159 #elif !defined(LED_SET)
00160 # if LEDS_INVERSE == 0
00161 # define LED_SET(ln) LED_PORT |= (_BV(ln+LED_SHIFT) & LED_MASK)
00162 # else
00163 # define LED_SET(ln) LED_PORT &= ~(_BV(ln+LED_SHIFT) & LED_MASK)
00164 # endif
00165 #endif
00166
00167
00168 #if defined(NO_LEDS) || defined (DOXYGEN)
00169
00170 # define LED_CLR(ln) do{}while(0)
00171 #elif !defined(LED_CLR)
00172 # if LEDS_INVERSE == 0
00173 # define LED_CLR(ln) LED_PORT &= ~(_BV(ln+LED_SHIFT) & LED_MASK)
00174 # else
00175 # define LED_CLR(ln) LED_PORT |= (_BV(ln+LED_SHIFT) & LED_MASK)
00176 # endif
00177 #endif
00178
00179 #if defined(NO_LEDS) || defined (DOXYGEN)
00180
00181 # define LED_VAL(msk,val) do{}while(0)
00182 #elif !defined(LED_VAL)
00183 # if LEDS_INVERSE == 0
00184 # define LED_VAL(msk,val) LED_PORT |= ((LED_MASK|msk) << LED_SHIFT); \
00185 LED_PORT |= ~((val << LED_SHIFT)& (LED_MASK|(msk<<LED_SHIFT)) )
00186 # else
00187 # define LED_VAL(msk,val) LED_PORT &= ~(LED_MASK|(msk<<LED_SHIFT)); LED_PORT |= ~(val & (LED_MASK|msk))
00188
00189 # endif
00190 #endif
00191
00192 #if defined(NO_LEDS) || defined (DOXYGEN)
00193
00194 # define LED_TOGGLE(ln) do{}while(0)
00195 #elif !defined(LED_TOGGLE)
00196 # define LED_TOGGLE(ln) LED_PORT ^= (_BV(ln+LED_SHIFT) & LED_MASK)
00197 #endif
00198
00200 #define LED_MAX_VALUE ((1<<LED_NUMBER)-1)
00201
00205
00206
00210 #if defined(NO_KEYS) || defined (DOXYGEN)
00211
00212 # define KEY_INIT()
00213
00215 # define KEY_GET() (0)
00216
00217 #else
00218 # if PULLUP_KEYS != 0
00219 # define PULL_MASK (MASK_KEY)
00220 # else
00221 # define PULL_MASK (0)
00222 # endif
00223 # if !defined KEY_INIT
00224 # define KEY_INIT() do{PORT_KEY |= PULL_MASK; DDR_KEY &= ~(MASK_KEY); }while(0)
00225 # endif
00226 # if INVERSE_KEYS == 0
00227 # define KEY_GET()\
00228 ((PIN_KEY & MASK_KEY) >> SHIFT_KEY)
00229 # else
00230 # define KEY_GET()\
00231 ((~PIN_KEY & MASK_KEY) >> SHIFT_KEY)
00232 # endif
00233 #endif
00234
00243 static inline uint8_t keys_debounced(void)
00244 {
00245 static uint8_t key_state;
00246 static uint8_t ct0, ct1;
00247 uint8_t i;
00248
00249
00250
00251
00252
00253
00254 i = key_state ^ KEY_GET();
00255
00256
00257
00258
00259
00260
00261
00262 ct0 = ~( ct0 & i );
00263 ct1 = (ct0 ^ ct1) & i;
00264 i &= ct0 & ct1;
00265 key_state ^= i;
00266
00267
00268
00269
00270
00271
00272 return key_state & i;
00273
00274 }
00275
00281 static inline void trap_if_key_pressed(void)
00282 {
00283
00284 KEY_INIT();
00285 DELAY_MS(10);
00286 if (KEY_GET())
00287 {
00288 LED_INIT();
00289 while(1)
00290 {
00291 DELAY_MS(400);
00292 LED_SET(0);
00293 DELAY_MS(10);
00294 LED_CLR(0);
00295 }
00296 }
00297 }
00298
00299 #if BOOT_LOADER_ADDRESS != 0
00300
00301 #define JUMP_BOOT_LOADER() \
00302 do {\
00303 void (*funcptr)( uint8_t flag ) = BOOT_LOADER_ADDRESS;\
00304 funcptr(42);\
00305 }while(0)
00306 #else
00307 #define JUMP_BOOT_LOADER()
00308 #endif
00309
00310
00311
00312
00313
00314 #ifdef __cplusplus
00315 extern "C" {
00316 #endif
00317
00321 #if 0
00322 #define BUFFER_SET_USED(b) do{ATOMIC_BLOCK(ATOMIC_FORCEON){b->used|=1}}while(0)
00323 #define BUFFER_SET_UNUSED(b) do{ATOMIC_BLOCK(ATOMIC_FORCEON){b->used&=~1}}while(0)
00324 #define BUFFER_IS_USED(b) ((b->used&1)!=0)
00325
00326 #define BUFFER_SET_LOCK(b) do{ATOMIC_BLOCK(ATOMIC_FORCEON){b->used|=2}}while(0)
00327 #define BUFFER_SET_UNLOCK(b) do{ATOMIC_BLOCK(ATOMIC_FORCEON){b->used&=~2}}while(0)
00328 #define BUFFER_IS_LOCKED(b) ((b->used&2)!=0)
00329 #endif
00330
00331 #define BUFFER_SET_USED(b) do{b->used|=1;}while(0)
00332 #define BUFFER_SET_UNUSED(b) do{b->used&=~1;}while(0)
00333 #define BUFFER_IS_USED(b) ((b->used&1)!=0)
00334
00335 #define BUFFER_SET_LOCK(b) do{b->used|=2;}while(0)
00336 #define BUFFER_SET_UNLOCK(b) do{b->used&=~2;}while(0)
00337 #define BUFFER_IS_LOCKED(b) ((b->used&2)!=0)
00338
00339
00340 #define BUFFER_SIZE(b) (b->iend - b->istart)
00341 #define BUFFER_PDATA(b) (b->data + b->istart)
00342 #define BUFFER_SEEK(b,offset) (b->data + (b->iend=offset))
00343
00344 #define BUFFER_GET_MEMBLOCK(b,pmem,size) \
00345 do{\
00346 b->used = 1;\
00347 pmem = (b->data + b->iend);\
00348 size = (b->len - b->iend);\
00349 }while(0)
00350
00351 #define BUFFER_UPDATE_MEMBLOCK(b,end) \
00352 do{\
00353 b->iend = end;\
00354 b->used = 0;\
00355 }while(0);
00356
00357 #define BUFFER_LAST_CHAR(b) \
00358 (b->iend <= b->istart) ? EOF : (char)b->data[b->iend-1]
00359 #define BUFFER_FREE_AT_END(b) (b->len - b->iend)
00360 #define BUFFER_FREE_AT_START(b) (b->istart)
00361 #define BUFFER_ELSZ(x) (sizeof(buffer_t) + (x))
00362 #define BUFFER_RESET(b,start) do{ b->iend = b->istart = start;}while(0)
00363 #define BUFFER_ADVANCE(b,more) do{ b->istart += more;}while(0)
00364
00366 buffer_t * buffer_init(void * pmem, uint8_t size, uint8_t start);
00368 int buffer_append_char(buffer_t *b, uint8_t c);
00370 int buffer_prepend_char(buffer_t *b, int c);
00372 int buffer_get_char(buffer_t *b);
00374 uint8_t buffer_append_block(buffer_t *b, void *pdata, uint8_t size);
00376 uint8_t buffer_prepend_block(buffer_t *b, void *pdata, uint8_t size);
00378 uint8_t buffer_get_block(buffer_t *b, void *pdata, uint8_t size);
00379
00380
00381 buffer_pool_t * buffer_pool_init(uint8_t *pmem, size_t memsz, uint8_t bsz);
00382 buffer_t * buffer_alloc(buffer_pool_t *ppool, uint8_t istart);
00383 void buffer_free(buffer_t * pbuf);
00384
00385
00386
00387
00388
00389
00390
00391 typedef struct
00392 {
00393 FILE bstream;
00394 buffer_t *pbufin;
00395 buffer_t *pbufout;
00396 void (*incb)(buffer_t *pbuf);
00397 void (*outcb)(buffer_t *pbuf);
00398 } buffer_stream_t;
00399
00400
00401
00402 int buffer_stream_init( buffer_stream_t *pbs,
00403 void (*incb)(buffer_t *pbuf),
00404 void (*outcb)(buffer_t *pbuf));
00405
00406 int buffer_stream_putchar(char c,FILE *f);
00407 int buffer_stream_getchar(FILE *f);
00408
00409
00410
00412 #ifdef __cplusplus
00413 }
00414 #endif
00415
00416 #endif
00417