I need a correct code to read and send data to the serial port and avoid overflow, because when i have it, the application crashes at 'unhandled exception', i need a way to recover from an overflow. In the case of sending data , debugging the program i saw that the crash become when the SD2.oqueue.q_count reaches zero, but i don't know how to limit it and how to wait until the queue is empty, I tried to read it from SD2 struct but when i wait, the q_count dont change.
Code: Select all
static WORKING_AREA(wa_th_SDT, 256);
static msg_t th_SD2_envia(void *arg) {
chRegSetThreadName("SD2_envia");
(void)arg;
msg_t msg_pos;
S_Message_t msg_dat,msg_resend[SZ_BUF_RS];
uint32_t i,size,sizeframe,count,pos=0;
while(!chThdShouldTerminate()){if(SD2_ON){
if(chMBGetUsedCountI(&S_T_mailbox)>0){ //wait for messages in mailbox
while(chMBFetch(&S_T_mailbox, &msg_pos, MS2ST(1))!= RDY_OK);
chSysLock();
msg_dat = *(S_Message_t *)msg_pos; //Get the data, msg_pos is a pointer to structure
chSysUnlock();
//////////////////////
/// initialize dataT[] with the data stored in msg_dat
//////////////////////
count = sdWriteTimeout(&SD2,(uint8_t *)dataT,sizeframe,MS2ST(DM_SR_DATA));
if(count<(HEADER_SZ+size)){ //error sending, it retry S_RESEND_TIMES
if(msg_dat.ResendTimes < S_RESEND_TIMES){
msg_dat.ResendTimes++;
msg_resend[pos] = msg_dat;
chMBPost(&S_T_mailbox, (msg_t) &msg_resend[pos], MS2ST(10)); //put it in mailbox queue
pos++;
if(pos>=SZ_BUF_RS)pos=0;
}
}
}
chThdSleepMicroseconds(DU_CHECK_ST_MAILBOX); //Delay between mailbox check
}else{chThdSleepMilliseconds(100);}}
return 0;
}Im getting problems also whit the receiver thread.
Another question, I used mailboxes but i work with a packet struct, i post to the mailbox the pointer of the actual struct, and i fetch it but im not sure if that is the correct way to protect the data because other tread can overwrite the data stored in the pointer when it reaches a limit, i used chSysLock to minimize the time between fetch the pointer and copy data to other variable.
Thanks, and i sorry for my regular English.