serial_packet.h
/*
* Copyright (c) 2013, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _packet_h
#define _packet_h
#include "bootloader_common.h"
#include "bootloader/bl_peripheral.h"
#include "property/property.h"
// Declarations
enum _serial_protocol_version_constants
{
kSerialProtocol_Version_Name = 'P',
kSerialProtocol_Version_Major = 1,
kSerialProtocol_Version_Minor = 2,
kSerialProtocol_Version_Bugfix = 0
};
enum _framing_packet_constants
{
kFramingPacketStartByte = 0x5a,
kFramingPacketType_Ack = 0xa1,
kFramingPacketType_Nak = 0xa2,
kFramingPacketType_AckAbort = 0xa3,
kFramingPacketType_Command = 0xa4,
kFramingPacketType_Data = 0xa5,
kFramingPacketType_Ping = 0xa6,
kFramingPacketType_PingResponse = 0xa7
};
enum _timing_constants
{
kHostMaxStartByteReadCount = 2,
kDefaultByteReadTimeoutMs = 10,
kCallbackBufferSize = 64
};
enum _serial_packet_constants
{
kIncomingPacketBufferSize = kMinPacketBufferSize,
kOutgoingPacketBufferSize = kMinPacketBufferSize
};
enum _serial_packet_mode
{
kSerialModeCmd,
kSerialModeAck,
kSerialModeIdle
};
typedef struct FramingHeader
{
uint8_t startByte;
uint8_t packetType;
} framing_header_t;
typedef struct FramingSyncPacket
{
framing_header_t header;
} framing_sync_packet_t;
#pragma pack(1)
typedef struct FramingDataPacket
{
framing_header_t header;
uint16_t length;
uint16_t crc16;
} framing_data_packet_t;
#pragma pack()
typedef struct SerialFramingPacket
{
framing_data_packet_t dataPacket;
uint8_t data[kOutgoingPacketBufferSize];
} serial_framing_packet_t;
typedef struct SerialData
{
uint8_t data[kIncomingPacketBufferSize];
uint8_t callbackBuffer[kCallbackBufferSize];
serial_framing_packet_t framingPacket;
volatile uint32_t writeOffset;
uint32_t readOffset;
bool isAckNeeded;
bool isBackToBackWrite;
bool isAckAbortNeeded;
} serial_data_t;
typedef struct PingResponse
{
standard_version_t version;
uint16_t options;
uint16_t crc16;
} ping_response_t;
// Externs
extern const peripheral_packet_interface_t g_framingPacketInterface;
// Prototypes
#if defined(__cplusplus)
extern "C" {
#endif // __cplusplus
status_t serial_packet_init(const peripheral_descriptor_t *self);
status_t serial_packet_read(const peripheral_descriptor_t *self,
uint8_t **packet,
uint32_t *packetLength,
packet_type_t packetType);
status_t serial_packet_write(const peripheral_descriptor_t *self,
const uint8_t *packet,
uint32_t byteCount,
packet_type_t packetType);
void serial_packet_abort(const peripheral_descriptor_t *self);
status_t serial_packet_finalize(const peripheral_descriptor_t *self);
uint32_t serial_packet_get_max_packet_size(const peripheral_descriptor_t *self);
status_t serial_packet_send_sync(uint8_t framingPacketType);
status_t serial_send_ping_response(const peripheral_descriptor_t *peripheral);
void serial_packet_queue_byte(uint8_t byte);
#if defined(BOOTLOADER_HOST)
void host_delay(uint32_t milliseconds);
#endif // BOOTLOADER_HOST
#if defined(__cplusplus)
}
#endif // __cplusplus
#endif // _packet_h
// EOF
serial_packet_get_max_packet_size
uint32_t serial_packet_get_max_packet_size(const peripheral_descriptor_t *self)
Get max packet size.
Definition: serial_packet.c:265
serial_packet_abort
void serial_packet_abort(const peripheral_descriptor_t *self)
Abort data phase.
Definition: serial_packet.c:257
framing_data_packet_t::length
uint16_t length
Number of data bytes that follow.
Definition: serial_packet.h:109
_framing_packet_constants
_framing_packet_constants
Serial framing packet constants.
Definition: serial_packet.h:56
serial_data_t::readOffset
uint32_t readOffset
The offset into the buffer that the app has read out.
Definition: serial_packet.h:128
ping_response_t::version
standard_version_t version
Serial framing protocol version.
Definition: serial_packet.h:139
packet_type_t
packet_type_t
Packet types.
Definition: bl_peripheral.h:94
serial_packet_read
status_t serial_packet_read(const peripheral_descriptor_t *self, uint8_t **packet, uint32_t *packetLength, packet_type_t packetType)
Read packet using serial framing.
Definition: serial_packet.c:129
framing_header_t::packetType
uint8_t packetType
Framing packet type.
Definition: serial_packet.h:95
serial_data_t::isAckNeeded
bool isAckNeeded
True if need to send ACK to previously received packet.
Definition: serial_packet.h:129
kCallbackBufferSize
Size for callback buffer, Must be power of 2 for easy wrap.
Definition: serial_packet.h:73
_timing_constants
_timing_constants
Timeout and other constants.
Definition: serial_packet.h:69
serial_framing_packet_t::dataPacket
framing_data_packet_t dataPacket
Packet header.
Definition: serial_packet.h:117
serial_send_ping_response
status_t serial_send_ping_response(const peripheral_descriptor_t *peripheral)
Send a ping message back in response to a ping.
Definition: serial_packet.c:335
serial_packet_init
status_t serial_packet_init(const peripheral_descriptor_t *self)
Initialize component.
Definition: serial_packet.c:117
serial_data_t::writeOffset
volatile uint32_t writeOffset
The offset into the buffer that the ISR will queue data into.
Definition: serial_packet.h:127
framing_data_packet_t
Serial framing data packet.
Definition: serial_packet.h:106
peripheral_packet_interface_t
Peripheral Packet Interface.
Definition: bl_peripheral.h:101
_serial_packet_constants
_serial_packet_constants
Incoming data buffer allocation size.
Definition: serial_packet.h:77
serial_data_t::isAckAbortNeeded
bool isAckAbortNeeded
True if next ACK should be ACK Abort.
Definition: serial_packet.h:131
serial_data_t::framingPacket
serial_framing_packet_t framingPacket
Buffer for outgoing packet.
Definition: serial_packet.h:126
serial_packet_queue_byte
void serial_packet_queue_byte(uint8_t byte)
Queues a byte received by the active peripheral.
Definition: serial_packet.c:110
standard_version_t
Structure of version property.
Definition: bootloader_common.h:168
_serial_packet_mode
_serial_packet_mode
Packet state machine modes.
Definition: serial_packet.h:84
serial_packet_send_sync
status_t serial_packet_send_sync(uint8_t framingPacketType)
Send a sync packet of the specified type.
Definition: serial_packet.c:271
_serial_protocol_version_constants
_serial_protocol_version_constants
Version constants for serial framing protocol.
Definition: serial_packet.h:47
serial_data_t
Format of global context data.
Definition: serial_packet.h:122
ping_response_t::options
uint16_t options
Serial framing protocol options bitfield.
Definition: serial_packet.h:140
framing_header_t::startByte
uint8_t startByte
#kFramingPacketStartByte
Definition: serial_packet.h:94
serial_data_t::isBackToBackWrite
bool isBackToBackWrite
True if executing back-to-back write.
Definition: serial_packet.h:130
serial_packet_write
status_t serial_packet_write(const peripheral_descriptor_t *self, const uint8_t *packet, uint32_t byteCount, packet_type_t packetType)
Write packet using serial framing.
Definition: serial_packet.c:195
ping_response_t
Serial ping response format.
Definition: serial_packet.h:137
framing_sync_packet_t
Serial framing sync packet.
Definition: serial_packet.h:99
framing_header_t
Serial framing header.
Definition: serial_packet.h:92
serial_packet_finalize
status_t serial_packet_finalize(const peripheral_descriptor_t *self)
Finalize.
Definition: serial_packet.c:123
serial_framing_packet_t
Framing packet with data area.
Definition: serial_packet.h:115
framing_data_packet_t::crc16
uint16_t crc16
CRC-16 of data packet header and data.
Definition: serial_packet.h:110
kDefaultByteReadTimeoutMs
Default value for receiving 1 byte timeout.
Definition: serial_packet.h:72
framing_sync_packet_t::header
framing_header_t header
Framing packet header.
Definition: serial_packet.h:101
status_t
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:121