Vit (10.05.2013 07:50 - 10:13, просмотров: 118) ответил Apтём на Как выглядит использование getchar и putchar и они сами?
накалякал чегой-то типо кусочек приема
#define SERIAL1_RXBUF_SIZE 64
#define SERIAL1_RXBUF_SIZE_MASK (SERIAL1_RXBUF_SIZE-1U)
u8 Serial1_RxBuf[SERIAL1_RXBUF_SIZE];
u16 Serial1_RxBuf_RdIdx;
u16 Serial1_RxBuf_WrIdx;
int Serial1_RxBuf_Putchar(int ch){//for ISR
u16 WrIdx = Serial1_RxBuf_WrIdx;
WrIdx++;
if((WrIdx - Serial1_RxBuf_RdIdx) >= SERIAL1_RXBUF_SIZE) return EOF;
Serial1_RxBuf[WrIdx & SERIAL1_RXBUF_SIZE_MASK] = ch;
Serial1_RxBuf_WrIdx = WrIdx;
return ch;
}
int Serial1_RxBuf_Getchar(void){//for application
unsigned int ch;
u16 WrIdx;
WrIdx = Serial1_RxBuf_WrIdx;//atomic
if(Serial1_RxBuf_RdIdx == WrIdx){
return EOF;
}
Serial1_RxBuf_RdIdx++;//atomic
ch = Serial1_RxBuf[Serial1_RxBuf_RdIdx & SERIAL1_RXBUF_SIZE_MASK];//u8 to unsigned int
return (int)ch;
}
int Serial1_RxBuf_Getchar(void);
int Serial1_TxBuf_Putchar(int);
int Serial2_RxBuf_Getchar(void);
int Serial2_TxBuf_Putchar(int);
int (*App_Getchar)(void);
int (*App_Putchar)(int);
int init(int channel){
int rc = -1;
if(channel == 1){
App_Getchar = Serial1_RxBuf_Getchar;
App_Putchar = Serial1_TxBuf_Putchar;
rc = 1;
}else if(channel == 2){
App_Getchar = Serial2_RxBuf_Getchar;
App_Putchar = Serial2_TxBuf_Putchar;
rc = 2;
}
return rc;
}
static buf[Buf_Size];
static int len;
int Rx_Frame_Transport(int mode)
{
static CPU_Time_t ts;
static int st = 0;
int ch;
if(mode = MODE_RxRestart){
mode = MODE_Rx_Proceed;
st = 0;
}
if(mode != MODE_Rx_Proceed){
return mode;
}
ch = (*App_Getchar)();
switch(st){
case 0:
if(ch == EOF){ break;}
ts = CPU_Time();
len = 0;
buf[len++] = ch;
st++;
//break;
ch = (*App_Getchar)();//next
case 1:
if(ch == EOF){
if(CPU_Time_Elapsed(ts) > CPU_uS(FRAME_BREAK_TIME)){
if(!CRC_Calc(buf, len)){
mode = MODE_RX_Complete;
//st = 0;
}else{
st = 0;//restart
}
}
}else{
do{
buf[len++] = ch;
if(len >= Buf_Size){
st = 0;//restart
ch = EOF;
continue;
}
ch = (*App_Getchar)();//next
}while(ch != EOF);
ts = CPU_Time();
}
break;
}
return mode;
}
bool Rx_Parser(int mode){
bool done = false;
do{
if(mode != MODE_RX_Complete){
break;// goto QUIT;
}
// parse
// buf, len
...
done = true;
}while(0);//QUIT:
return done;
}