#include #include #include #include #include "comm.h" #include "exceptions.h" /* host to device */ enum { MSGTYPE_REQUEST_IDENT = 1, MSGTYPE_LOAD_OBJECT, MSGTYPE_RUN_KERNEL, MSGTYPE_SET_BAUD_RATE, MSGTYPE_SWITCH_CLOCK, }; /* device to host */ enum { MSGTYPE_LOG = 1, MSGTYPE_MESSAGE_UNRECOGNIZED, MSGTYPE_IDENT, MSGTYPE_OBJECT_LOADED, MSGTYPE_INCORRECT_LENGTH, MSGTYPE_CRC_FAILED, MSGTYPE_OBJECT_UNRECOGNIZED, MSGTYPE_KERNEL_FINISHED, MSGTYPE_KERNEL_EXCEPTION, MSGTYPE_KERNEL_STARTUP_FAILED, MSGTYPE_RPC_REQUEST, MSGTYPE_CLOCK_SWITCH_COMPLETED, MSGTYPE_CLOCK_SWITCH_FAILED, }; static int receive_int(void) { unsigned int r; int i; r = 0; for(i=0;i<4;i++) { r <<= 8; r |= (unsigned char)uart_read(); } return r; } static char receive_char(void) { return uart_read(); } static void send_int(int x) { int i; for(i=0;i<4;i++) { uart_write((x & 0xff000000) >> 24); x <<= 8; } } static void send_sint(short int i) { uart_write((i >> 8) & 0xff); uart_write(i & 0xff); } static void send_char(char c) { uart_write(c); } static void receive_sync(void) { char c; int recognized; recognized = 0; while(recognized < 4) { c = uart_read(); if(c == 0x5a) recognized++; else recognized = 0; } } static void receive_and_load_object(object_loader load_object) { int length; int i; unsigned char buffer[256*1024]; unsigned int crc; length = receive_int(); if(length > sizeof(buffer)) { send_char(MSGTYPE_INCORRECT_LENGTH); return; } crc = receive_int(); for(i=0;i (sizeof(kernel_name)-1)) { send_char(MSGTYPE_INCORRECT_LENGTH); return; } for(i=0;i> 8, (char *)value + p); send_char(0); return p; } return 0; } int comm_rpc(int rpc_num, ...) { int type_tag; int eid; int retval; send_char(MSGTYPE_RPC_REQUEST); send_sint(rpc_num); va_list args; va_start(args, rpc_num); while((type_tag = va_arg(args, int))) send_value(type_tag, type_tag == 'n' ? NULL : va_arg(args, void *)); va_end(args); send_char(0); eid = receive_int(); retval = receive_int(); if(eid != EID_NONE) exception_raise(eid); return retval; } void comm_log(const char *fmt, ...) { va_list args; int len; char outbuf[256]; int i; va_start(args, fmt); len = vscnprintf(outbuf, sizeof(outbuf), fmt, args); va_end(args); send_char(MSGTYPE_LOG); send_sint(len); for(i=0;i