Esempio n. 1
0
/*! \brief  This function will upload a frame from the radio transceiver's frame 
 *          buffer.
 *          
 *          If the frame currently available in the radio transceiver's frame buffer
 *          is out of the defined bounds. Then the frame length, lqi value and crc
 *          be set to zero. This is done to indicate an error.
 *
 *  \param  rx_frame    Pointer to the data structure where the frame is stored.
 *
 *  \ingroup hal_avr_api
 */
 void hal_frame_read( hal_rx_frame_t *rx_frame )
{
    uint8_t frame_length;
    AVR_ENTER_CRITICAL_REGION( );
    
    HAL_SS_LOW( );
    
    //Send frame read command.

    frame_length=hal_RF_SPI_Send_Data(HAL_TRX_CMD_FR);	//
    frame_length=hal_RF_SPI_Send_Data(frame_length);

   //delay_ms(1);

   
    /*Check for correct frame length.*/
    if ((frame_length >= HAL_MIN_FRAME_LENGTH) && (frame_length <= HAL_MAX_FRAME_LENGTH)) {
		uint16_t crc = 0;
		uint8_t *rx_data = (rx_frame->data);
		uint8_t tempData;

		rx_frame->length = frame_length; //Store frame length.

		/*Upload frame buffer to data pointer. Calculate CRC.*/
		do{
			tempData = hal_RF_SPI_Send_Data(frame_length);
			*rx_data++ = tempData;      
			crc = crc_ccitt_update( crc, tempData );
		} while (--frame_length > 0);
        
        /*Read LQI value for this frame.*/
        rx_frame->lqi = hal_RF_SPI_Send_Data( tempData);

        HAL_SS_HIGH( );
        
        /*Check calculated crc, and set crc field in hal_rx_frame_t accordingly.*/
        if (crc == HAL_CALCULATED_CRC_OK) {
            rx_frame->crc = true; 
        } else { rx_frame->crc = false; }
    } else {
        
        HAL_SS_HIGH( );
        
        rx_frame->length = 0;
        rx_frame->lqi    = 0;
        rx_frame->crc    = false;    
    }
    
    AVR_LEAVE_CRITICAL_REGION( );
}
Esempio n. 2
0
ssize_t frame_send(FILE *out, void *data, size_t nbytes)
{
	char *d;
	char *end;
	uint16_t crc = FRAME_CRC_INIT;

	fputc(FRAME_START, out);
	for(d = data, end = d + nbytes; d < end; d++) {
		char c = *d;
		crc = crc_ccitt_update(crc, c);
		SEND_BYTE(out, c);
	}

	crc = htons(crc);

	SEND_BYTE(out, crc >> 8);
	SEND_BYTE(out, crc & 0xff);

	fputc(FRAME_START, out);

	fflush(out);
	return nbytes;
}
Esempio n. 3
0
ssize_t frame_recv(FILE *in, void *vbuf, size_t nbytes)
{
	uint16_t crc = FRAME_CRC_INIT;
	size_t i;
	char *buf = vbuf;
	bool recv_started = false;
	bool is_escaped = false;

	for(i = 0;;) {
		int data = fgetc(in);

		if (data == EOF) {
			return EOF;
		}

		if (data < 0) {
			return data;
		}

		if (ferror(in)) {
			return -255;
		}

		if (data == FRAME_START) {
			if (recv_started) {
				if (i != 0) {
					/* success */
					if (crc == 0) {
						ungetc(data, in);
						return i - 2;
					} else {
						fprintf(stderr, "crc = %d\n", crc);
						i = 0;
						crc = FRAME_CRC_INIT;
						continue;
					}
				}
			} else {
				recv_started = true;
			}
			continue;
		}

		if (!recv_started)
			continue;

		if (data == FRAME_RESET) {
			/* restart recv */
			i = 0;
			continue;
		}

		if (data == FRAME_ESC) {
			is_escaped = true;
			continue;
		}

		if (is_escaped) {
			is_escaped = false;
			data ^= FRAME_ESC_MASK;
		}

		crc = crc_ccitt_update(crc, (uint8_t)(data & 0xff));

		if (i < nbytes) {
			buf[i] = data;
			i++;
		} else {
			return -ENOSPC;
		}
	}
	return i;
}