void test_that_rxbytecounter_is_reset_when_last_byte_is_received(){ receiveByte(3); // length = 3 receiveByte(1); receiveByte(2); assertEquals(0, rxbytecounter, "RX byte counter not reset"); }
// Receive a packet, copying it into a memory buffer and returning the buffer static uint8_t *receivePacket(int fd) { uint8_t packet_header[5]; uint8_t *packet_buffer; uint32_t length; uint32_t i; for (i = 0; i < 5; i++) { packet_header[i] = receiveByte(fd); } length = readU32LittleEndian(&(packet_header[1])); if (length > PACKET_LENGTH_LIMIT) { printf("Got absurdly large packet length of %d\n", length); printf("Exiting, since the packet is probably garbled\n"); exit(1); } packet_buffer = malloc(length + 5); memcpy(packet_buffer, packet_header, 5); for (i = 0; i < length; i++) { packet_buffer[i + 5] = receiveByte(fd); } return packet_buffer; }
uint8_t I2C::read(uint8_t address, uint8_t numberBytes, uint8_t *dataBuffer) { bytesAvailable = 0; bufferIndex = 0; if(numberBytes == 0){numberBytes++;} nack = numberBytes - 1; returnStatus = 0; returnStatus = start(); if(returnStatus){return(returnStatus);} returnStatus = sendAddress(SLA_R(address)); if(returnStatus){return(returnStatus);} for(uint8_t i = 0; i < numberBytes; i++) { if( i == nack ) { returnStatus = receiveByte(0); if(returnStatus != MR_DATA_NACK){return(returnStatus);} } else { returnStatus = receiveByte(1); if(returnStatus != MR_DATA_ACK){return(returnStatus);} } dataBuffer[i] = TWDR; bytesAvailable = i+1; totalBytes = i+1; } returnStatus = stop(); return(returnStatus); }
void test_that_command_is_not_called_when_not_all_bytes_are_ready(){ receiveByte(3); // length = 3 receiveByte(SPI_CMD_PT_TEST); // type that triggers test function on receive SPI_checkForReceivedData(); assertEquals(0, lastPackage[0], "No command should have been treated"); }
void timerHandlerReadByte1(void *T) { static volatile uint32_t stageOfReading = 0; static uint8_t whichByte = 0; static uint8_t whichDevice = 0; addressAndData *address = (addressAndData*)T; if(readingAllowed == TRUE) { if(0 == whichDevice) //accel { receiveByte(address->adr.addressDevice[0], (address->adr.subAddress[0] + whichByte), &(address->dane[whichByte])); whichByte++; if(whichByte == 6) { //readingAllowed = FALSE; whichDevice++; whichByte = 0; stageOfReading = 0; } } else if(1 == whichDevice) //gyro { receiveByte(address->adr.addressDevice[0], (address->adr.subAddress[1] + whichByte), &(address->dane[whichByte + 6])); whichByte++; if(whichByte == 6) { //readingAllowed = FALSE; whichDevice++; whichByte = 0; stageOfReading++; } } else if(2 == whichDevice) { receiveByte(address->adr.addressDevice[1], (address->adr.subAddress[2] + whichByte), &(address->dane[whichByte + 12])); whichByte++; if(whichByte == 6) { readingAllowed = FALSE; whichDevice = 0; whichByte = 0; stageOfReading++; } } } }
//-------------------------------------------------------------- // HIGH LEVEL MESSAGE FUNCTIONS //-------------------------------------------------------------- // This function will receive a full request message from a // RasPi and return proper confirmation bytes int * receiveMessage(void){ // Hold inital byte received uint8_t i=0; uint8_t j=0; uint8_t n=0; // Array to hold received data static int msgArray[10]; int dataByteIn[2]; // Receive initial byte for(j=0; j<2; j++){ dataByteIn[j] = receiveByte(); } // If message is not properly terminated write error code // to entire array and call commError() if(dataByteIn[1] != END_MSG){ for (i=0; i<4; i++){ msgArray[i] = UART_COMM_ERROR; } commError(); } // RasPi normal request message to AVR else if (dataByteIn[0] == RASPI_REQ_AVR){ // Transmit AVR ready to receive message transmitReady(); // Loop through until EOM reached and store in array do{ msgArray[n] = receiveByte(); n++; }while(msgArray[n-1] != END_MSG); transmitConfirm(); } // RasPi initialize request to AVR else if(dataByteIn[0] == RASPI_INIT_TO_AVR){ // Transmit AVR initialized ready byte transmitInitialize(); // Write initialized byte to array for(i=0; i<4; i++){ msgArray[i] = AVR_INIT_TO_RASPI; } } // Improper initial communication byte, call commError() else{ commError(); } return msgArray; }
void test_that_package_is_received_by_spi(){ receiveByte(3); // length = 3 receiveByte(SPI_CMD_PT_TEST); // type that triggers test function on receive receiveByte(2); SPI_checkForReceivedData(); assertEquals(3, lastPackage[0], "Incorrect package value 0"); assertEquals(SPI_CMD_PT_TEST, lastPackage[1], "Incorrect package value 0"); assertEquals(2, lastPackage[2], "Incorrect package value 0"); }
void test_that_only_first_command_is_treated_if_second_is_incomplete(){ receiveByte(3); // length = 3 receiveByte(SPI_CMD_PT_TEST); // type that triggers test function on receive receiveByte(4); receiveByte(4); // length = 3 receiveByte(SPI_CMD_PT_TEST); // type that triggers test function on receive SPI_checkForReceivedData(); assertEquals(3, lastPackage[0], "Wrong last command"); }
// This function will transmit a full message to RasPi uint8_t transmitMessage(volatile int sendData[4]){ // Initialize loop counter uint8_t i; uint8_t badSendFlag = 0; // Clear global interrupt to stop receive interrupt cli(); // Send AVR request RasPi byte transmitRequest(); // Wait for response from RasPi // **** This has possibility of hanging program -- needs improvement! if (receiveByte() == RASPI_READY){ // Remove EOM from buffer receiveByte(); // Loop to send all of data array for(i=0; i<4; i++){ transmitByte(sendData[i]); } // Transmit end of message byte transmitByte(END_MSG); // Check for good confirm byte if (receiveByte() == RASPI_REC_MSG_CONFIRM){ // Remove EOM from buffer receiveByte(); } else{ // Set bad send flag badSendFlag = 2; } } // If improper response is received, call commError() else{ // Set bad send flag badSendFlag = 1; } // If badSendFlag is set - call commError() if(badSendFlag != 0){ commError(); } // reenable global interrupts sei(); // Return value of badSendFlag return badSendFlag; }
int main(void) { DDRD = (1<<1); // habilita output no pino PD1 (TXD) DDRB = (1<<PB0); // LED na porta PB0 DDRB = (1<<PB5); // habilita LED usado no bootloader // (para piscar em status) char serialCharacter; LED_DDR=0xff; initUSART(); PORTB |= (1<<PB0); PORTB &= ~(1<<PB5); printString("Ola Mundo\r\n"); while(1) { serialCharacter = receiveByte(); PORTB ^= _BV(PB0); transmitByte(serialCharacter); //LED_PORT=serialCharacter; PORTB ^= _BV(PB5); } return(0); }
int main(void) { // Single character for serial TX/RX char a; // Enable output on all 8 bits in DDRB (but only PB0 and PB1 are used) DDRB = 0xff; // Enable pin change interrupt for the B pins, but only check PB0 and PB1. sei(); PCICR |= (1 << PCIE0); PCMSK0 |= ((1 << PB0) | (1 << PB1)); init_timer1(); initUSART(); pb[0] = PB0; pb[1] = PB1; while (1) { // for (uint8_t i=0; i<255; i++) // cmd[i] = 0; // char cmd[255]; // readString(cmd, 255); // if (strcmp(cmd, "V0_ON") == 0) // { // printString("\r\nYou wrote: "); // printString(cmd); // printString("\r\n"); // PORTB |= (1 << PB0); // } // if (strcmp(cmd, "V1_ON")) // PORTB |= (1 << PB1); // if (strcmp(cmd, "V0_OFF")) // PORTB &= ~(1 << PB0); // if (strcmp(cmd, "V1_OFF")) // PORTB &= ~(1 << PB1); // if (cmd == "V0_ON") set_bit(PORTB, PB0); // PORTB |= (1 << PB0); a = receiveByte(); transmitByte(a); if (a == '0') PORTB &= ~(1 << PB0); if (a == '1') PORTB |= (1 << PB0); if (a == '2') PORTB &= ~(1 << PB1); if (a == '3') PORTB |= (1 << PB1); // PORTB = a; } return 0; }
bool PBBP::receiveBytes(uint8_t *buf, uint8_t len) { while (len--) { if (!receiveByte(buf++)) return false; } return true; }
void readString( char myString[], uint8_t maxLength ) { char response; uint8_t i; i = 0; while ( i < ( maxLength - 1 ) ) { /* prevent over-runs */ response = receiveByte(); /* echo */ transmitByte(response); if ( response == '\r' ) { /* enter marks the end */ break; } else { /* add in a letter */ myString[ i ] = response; i++; } } /* terminal NULL character */ myString[ i ] = 0; }
int main(void) { //-----------INITS------------// DDRB |= 0x01; PORTB = 0x00; initUSART(); PORTB = 0x01; _delay_ms(100); PORTB = 0x00; _delay_ms(100); PORTB = 0x01; _delay_ms(1000); PORTB = 0x00; //-------EVENT LOOP-----------// while(1) { char in = receiveByte(); /* if (in == 'h') PORTB = 0x01; else if (in == 'l') PORTB = 0x00; */ PORTB = 0x01; for (int i=0; i<in; i++) _delay_ms(1); PORTB = 0x00; } return(0); }
int main(void) { char serialCharacter; lcd_init(LCD_DISP_ON); lcd_gotoxy(2,0); lcd_puts("Serial"); lcd_gotoxy(3,1); lcd_puts("LCD"); DDRD |= (1<<PD1); // habilita o pino PD1 como TXD (output) DDRB |= (1<<PB0); // habilita o LED no pino PB0 PORTB|=(1<<PB0); initUSART(); printString("Serial ok:"); PORTB&=~(1<<PB0); while(1) { PORTB|=(1<<PB0); serialCharacter = receiveByte(); PORTB&=~(1<<PB0); lcd_putc(serialCharacter); } }
/* Reads a z85 encoded string from serial interface and decodes the string into byte array of size size */ uint8_t receive_and_decode_Z85(uint8_t *data, size_t size) { // Accepts only strings bounded to 5 bytes /* if (strlen (string) % 5) */ /* return NULL; */ /* size_t decoded_size = strlen (string) * 4 / 5; */ /* byte *decoded = malloc (decoded_size); */ if (size % 4) // only multiples of 4 bytes are possible return 1; size_t size_encoded = size*5/4; size_t byte_nbr = 0; size_t char_nbr = 0; uint32_t value = 0; while (char_nbr < size_encoded) { // Accumulate value in base 85 /* value = value * 85 + decoder [(byte) string [char_nbr++] - 32]; */ value = value * 85 + decoder [receiveByte() - 32]; char_nbr++; if (char_nbr % 5 == 0) { // Output value in base 256 uint32_t divisor = 16777216; // = 256 * 256 * 256; while (divisor) { data [byte_nbr++] = value / divisor % 256; divisor /= 256; } value = 0; } } if (byte_nbr != size) return 1; return 0; }
bool PBBP::receiveAck() { bool first, second; if (!receiveBit(&first) || !receiveBit(&second)) return false; // Acks are sent as 01, nacks as 10. Since the 0 is dominant during // a bus conflict, a receiveing of 00 means both an ack and a nack was // sent. if (!first && !second ) { this->last_error = ACK_AND_NACK; return false; } else if (!second ) { // Read error code from the slave if (receiveByte(&this->last_slave_error)) { this->last_error = NACK; } else { this->last_error = NACK_NO_SLAVE_CODE; } return false; } else if (first) { this->last_error = NO_ACK_OR_NACK; return false; } else { return true; } }
void CDirectSerial::RXBufferEmpty () { ULONG dwRead; Bit8u chRead; USHORT errors = 0; ULONG ulParmLen = sizeof(errors); if (lastChance > 0) { receiveByte (ChanceChar); lastChance = 0; } else { // update RX if (DosRead (hCom, &chRead, 1, &dwRead) != NO_ERROR) { if (dwRead != 0) { //LOG_MSG("UART 0x%x: RX 0x%x", base,chRead); receiveByte (chRead); } } } // check for errors Bit8u errreg = 0; APIRET rc = DosDevIOCtl(hCom, IOCTL_ASYNC, ASYNC_GETCOMMERROR, 0, 0, 0, &errors, ulParmLen, &ulParmLen); if (rc != NO_ERROR && errors) { if (errors & 8) { LOG_MSG ("Serial port at 0x%x: line error: framing error.", base); errreg |= LSR_FRAMING_ERROR_MASK; } if (errors & 4) { LOG_MSG ("Serial port at 0x%x: line error: parity error.", base); errreg |= LSR_PARITY_ERROR_MASK; } } errors = 0; rc = DosDevIOCtl(hCom, IOCTL_ASYNC, ASYNC_GETCOMMEVENT, 0, 0, 0, &errors, ulParmLen, &ulParmLen); if (rc != NO_ERROR && errors) { if (errors & 6) { LOG_MSG ("Serial port at 0x%x: line error: break received.", base); errreg |= LSR_RX_BREAK_MASK; } } if (errreg != 0) { receiveError (errreg); } }
bool PBBP::enumerate(void (*callback)(uint8_t*)) { uint8_t b; if (!sendReset() || !sendByte(BC_CMD_ENUMERATE)) { if (this->last_error == NO_ACK_OR_NACK) { // Nobody on the bus return true; } // Other error return false; } uint8_t num_slaves = 0; while (num_slaves < this->max_slaves) { // Allocate room to store one more address uint8_t id[UNIQUE_ID_LENGTH]; uint8_t crc = 0; for (uint8_t i = 0; i < UNIQUE_ID_LENGTH; ++i) { if (!receiveByte(&id[i])) { if (i == 0 && this->last_error == NO_ACK_OR_NACK) { // Nobody responded, meaning all device are enumerated return true; } // Other error return false; } crc = pinoccio_crc_update(UNIQUE_ID_CRC_POLY, crc, id[i]); } if (crc != 0) { this->last_error = CRC_ERROR; return false; } callback(id); num_slaves++; } // See if there is one more if (receiveByte(&b)) { // Succesfully received a byte, there are more slaves! this->last_error = TOO_MANY_SLAVES; return false; } else { return (this->last_error == NO_ACK_OR_NACK); } }
// recv and decode byte ERecvState CChannelHandler::recv(unsigned char *byte) { static bool decryptNext; unsigned char rawByte; if(!decryptNext) { if(receiveByte(&rawByte)) { if(rawByte == '}') { return eRecvEnd; } else if(rawByte == '{') { return eRecvBegin; } else if(rawByte == '|') { decryptNext = true; } else { *byte = rawByte; return eRecvData; } } } if(decryptNext) { if(receiveByte(&rawByte)) { decryptNext = false; *byte = rawByte ^ 0x20; return eRecvData; } } return eRecvNothing; }
uint16_t SHT1x::readCommand(uint8_t command) { reset(); // Send command startCommand(); bool ok = sendByte(command); if(!ok) console.print(Error, "Error writing byte to sensor !!!"); // Wait for competition delayTimer.mDelay(320); // Receive result union { uint16_t value; uint8_t bytes[2]; } result; result.bytes[1] = receiveByte(); result.bytes[0] = receiveByte(); receiveByte(); return result.value; }
void CSerialDummy::handleUpperEvent(Bit16u type) { if(type==SERIAL_TX_EVENT) { //LOG_MSG("SERIAL_TX_EVENT"); #ifdef CHECKIT_TESTPLUG receiveByte(loopbackdata); #endif ByteTransmitted(); // tx timeout } else if(type==SERIAL_THR_EVENT){ //LOG_MSG("SERIAL_THR_EVENT"); ByteTransmitting(); setEvent(SERIAL_TX_EVENT,bytetime); } }
// // The polling process that gets characters // void getChars(void *user) { while(1) { double d = DEFAULT_RX_DELAY; bhmWaitDelay(d); if (can_receive()) { Uns8 c; if (bhmSerRead(channel, &c, 1)) { bhmTriggerEvent(charReceived); receiveByte(c); } } } }
void test_that_multiple_commands_may_be_treated(){ receiveByte(3); // length = 3 receiveByte(SPI_CMD_PT_TEST); // type that triggers test function on receive receiveByte(4); receiveByte(4); // length = 3 receiveByte(SPI_CMD_PT_TEST); // type that triggers test function on receive receiveByte(2); receiveByte(2); SPI_checkForReceivedData(); assertEquals(2, lastPackage[2], "Wrong last command"); }
// Receive real number from serial link. The real number should be in Q16.16 // representation. This is so that the device under test doesn't have to // do the conversion to floating-point. static double receiveDouble(void) { uint8_t buffer[4]; int j; for (j = 0; j < 4; j++) { buffer[j] = receiveByte(); } // The cast from uint32_t to fix16_t isn't platform-independent, because // the C99 specification doesn't make any guarantees about conversions // to signed integer types (...if the destination type cannot store the // source value, which will be the case if the fix16_t is negative). // But it should work on nearly every contemporary platform. return fix16_to_dbl((fix16_t)readU32LittleEndian(buffer)); }
uint8_t getNumber(void) { // Gets a numerical 0-255 from the serial port. // Converts from string to number. char hundreds = '0'; char tens = '0'; char ones = '0'; char thisChar = '0'; do { /* shift over */ hundreds = tens; tens = ones; ones = thisChar; thisChar = receiveByte(); /* get a new character */ transmitByte(thisChar); /* echo */ } while (thisChar != '\r'); /* until type return */ return (100 * (hundreds - '0') + 10 * (tens - '0') + ones - '0'); }
int main(void) { char byte; uint16_t timerValue; // -------- Inits --------- // initUSART(); initTimer1(); LED_DDR = 0xff; /* all LEDs for output */ BUTTON_PORT |= (1 << BUTTON); /* enable internal pull-up */ printString("\r\nReaction Timer:\r\n"); printString("---------------\r\n"); printString("Press any key to start.\r\n"); // ------ Event loop ------ // while (1) { byte = receiveByte(); /* press any key */ printString("\r\nGet ready..."); randomDelay(); printString("\r\nGo!\r\n"); LED_PORT = 0xff; /* light LEDs */ TCNT1 = 0; /* reset counter */ if (bit_is_clear(BUTTON_PIN, BUTTON)) { /* Button pressed _exactly_ as LEDs light up. Suspicious. */ printString("You're only cheating yourself.\r\n"); } else { // Wait until button pressed, save timer value. loop_until_bit_is_clear(BUTTON_PIN, BUTTON); timerValue = TCNT1 >> 4; /* each tick is approx 1/16 milliseconds, so we bit-shift divide */ printMilliseconds(timerValue); printComments(timerValue); } // Clear LEDs and start again. LED_PORT = 0x00; printString("Press any key to try again.\r\n"); } /* End event loop */ return (0); /* This line is never reached */ }
int main( void ) { unsigned long c; unsigned char recByte; initUart(); //wait to receive data c = 0; RI = 0; while( 1 ) { recByte = receiveByte(); xArrayAt4000[c++] = recByte; } return 0; }
int updateInBuffer(char *inBuffer) { int i = 0; TCNT1 = 0; int timerValue = (TCNT1 >> 4); int lastIndex = (RX_CAP-1); while ((i < lastIndex) && (timerValue <= UPDATE_RATE)) { if (i == 15) { inBuffer[i] = '\n'; } else { char byte = ((char) receiveByte()); inBuffer[i] = byte; } i++; //timerValue = (TCNT1 >> 4); } inBuffer[lastIndex] = '\0'; return 0; }
int main(void) { char serialCharacter; // -------- Inits --------- // LED_DDR = 0xff; /* set up LEDs for output */ initUSART(); printString("Hello World!\r\n"); /* to test */ // ------ Event loop ------ // while (1) { serialCharacter = receiveByte(); transmitByte(serialCharacter); LED_PORT = serialCharacter; /* display ascii/numeric value of character */ } /* End event loop */ return 0; }