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