void CircularBuffer_WriteNoIncrement(uint8_t data, Circular_Buffer_Type *Cbuff, int32_t *count, uint8_t *crc8, uint16_t *crc16) { /* Check if we have an error from previous write */ if (count >= 0) { /* Check if we have 2 bytes free, in case of data = SYNC */ if ((CircularBuffer_SpaceLeft(Cbuff) - *count) >= 2) { Cbuff->buffer[(Cbuff->head + *count) % Cbuff->size] = data; *count += 1; /* Only add CRCs of they are needed */ if (crc8 != NULL) *crc8 = CRC8_step(data, *crc8); if (crc16 != NULL) *crc16 = CRC16_step(data, *crc16); if (data == SYNC_BYTE) { Cbuff->buffer[(Cbuff->head + *count) % Cbuff->size] = SYNC_BYTE; *count += 1; } } else *count = -1; } }
/** * @brief Generates a message with data and CRC16 part. * * @param[in] command Command to generate message for. * @param[in] data Pointer to where the data is located. * @param[in] size Number of data bytes. * @param[out] Cbuff Pointer to the circular buffer to put the data in. * @return HAL_FAILED if the message didn't fit or HAL_SUCCESS * if it did fit. */ static bool GenerateGenericCommand(kfly_command_t command, uint8_t *data, const uint32_t size, circular_buffer_t *Cbuff) { uint32_t i; uint16_t crc16; uint8_t header[2] = {(uint8_t) command, size}; /* Calculate the CRC. */ crc16 = CRC16_step((uint8_t)command, 0xffff); crc16 = CRC16_step(size, crc16); for (i = 0; i < size; i++) crc16 = CRC16_step(data[i], crc16); /* Apply SLIP encoding on the fly and return the result. */ return GenerateSLIP_HBT(header, 2, data, size, (uint8_t *)&crc16, 2, Cbuff); }
void CircularBuffer_WriteSYNCNoIncrement(Circular_Buffer_Type *Cbuff, int32_t *count, uint8_t *crc8, uint16_t *crc16) { /* Check if we have 4 byte free for SYNC + Header */ if (CircularBuffer_SpaceLeft(Cbuff) >= 4) { Cbuff->buffer[(Cbuff->head + *count) % Cbuff->size] = SYNC_BYTE; *count += 1; /* When writing the SYNC CRC8 must be calculated */ *crc8 = CRC8_step(SYNC_BYTE, 0x00); if (crc16 != NULL) *crc16 = CRC16_step(SYNC_BYTE, 0xffff); } else *count = -1; }