Project

General

Profile

Revision 287

implemented final, working version of uart and protocol

View differences:

uart.c
284 284

  
285 285
queue_t *out = NULL;
286 286
queue_t *in  = NULL;
287
void (*process)(const uint8_t*, const size_t);
287 288

  
288
int nctp_init(void){
289
int nctp_init(void (*proc_func)(const uint8_t*, const size_t)){
289 290
    out = queue_ctor(); if(out == NULL) return NULL_PTR;
290 291
    in  = queue_ctor(); if(in  == NULL) return NULL_PTR;
292
    process = proc_func;
291 293
    return SUCCESS;
292 294
}
293 295
int nctp_free(void){
......
326 328
    }
327 329
    return SUCCESS;
328 330
}
329

  
330 331
static int nctp_transmit(void){
331 332
    if(!queue_empty(out)){
332 333
        int ret = uart_send_char(COM1_ADDR, *(uint8_t*)queue_top(out));
......
335 336
    }else return SUCCESS;
336 337
}
337 338

  
338
static void process(){
339
static void nctp_process_received(){
339 340
    free(queue_top(in)); queue_pop(in);
341
    size_t sz = 1024; uint8_t *p = malloc(sz*sizeof(uint8_t));
342
    size_t i = 0;
340 343
    while(*(uint8_t*)queue_top(in) != NCTP_END){
341
        printf("%c", *(uint8_t*)queue_top(in));
344
        //printf("%c\n", *(uint8_t*)queue_top(in));
345
        p[i++] = *(uint8_t*)queue_top(in);
342 346
        free(queue_top(in)); queue_pop(in);
347
        if(i >= sz) p = realloc(p, sz=2*sz);
343 348
    }
344
    printf("\n");
345 349
    free(queue_top(in)); queue_pop(in);
350
    process(p, i);
351
    free(p);
346 352
}
347 353
static int nctp_receive(void){
348 354
    int ret;
......
354 360
        queue_push(in, tmp);
355 361
        if(c == NCTP_END) ++num_ends;
356 362
    }
357
    while(num_ends-- > 0) process();
363
    while(num_ends-- > 0) nctp_process_received();
358 364
    return SUCCESS;
359 365
}
360 366

  
......
370 376
        }
371 377
    }
372 378
}
373

  
374
/// HLTP
375
int hltp_send_string(const char *p){
376
    uint8_t* ptr[1]; ptr[0] = (uint8_t*)p;
377
    size_t    sz[1]; sz[0] = strlen(p)+1;
378
    return nctp_send(1, ptr, sz);
379
}

Also available in: Unified diff