/*********************************************************************** * Name: get_ant_msg * * Description: wait and recieve a message from the nRF24AP2 * * Author(s): Charles Wolf * * @param: * int max_wait -> timeout in 1mSec increments * UCHAR *MSG -> pointer to the message array * @return: * int -> 0 = message timeout * 1 = message recieved * Comments: * ANT Message Structure * SYNC (0xA4) 1 Byte * length 1 Byte * message ID 1 Byte * Data length * CheckSum 1 Byte * * ********************************************************************/ int get_ant_msg(int max_wait, UCHAR *MSG) { int count = 0; int i = 0; int outcome = 0; int length = 0; int checksum = 0; softuart_turn_rx_on(); softuart_flush_input_buffer(); while((count < max_wait) && (outcome != 1)) { //wait for data while((softuart_kbhit() < 1) && (count < max_wait)) { _delay_us(100); // 1bit = 208.333uSec count++; } if ( softuart_kbhit() > 0 ) { outcome = 2; //no sync //check for sync MSG[0] = softuart_getchar(); //softuart_getchar() is a possible place to get stuck waiting if(MSG[0] == MESG_TX_SYNC) { MSG[1] = softuart_getchar(); //length length = MSG[1]+2; if(length > 17) length = 17; //protect length for (i = 0; i < length; i++) { MSG[i+2] = softuart_getchar(); } checksum = checkSum(&MSG[0], length+3); if (checksum != MSG[length+4]) { outcome = 3; //bad checksum } else { outcome = 1; //message recieved } } } } softuart_turn_rx_off(); return outcome; }
/** Flushes the serial port Buffer * * This function reads one character from the serial port * if available. * */ void clixxIOSerial::flush(void) { // flush the buffer softuart_flush_input_buffer(); }
void motor_reqPos(uint8_t motor){ softuart_flush_input_buffer(motor); softuart_puts(motor, POSITION); }