Kinetis Bootloader  2.0.0
Common bootloader for Kinetis devices
serial_packet.h
1 /*
2  * Copyright (c) 2013, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef _packet_h
32 #define _packet_h
33 
34 #include "bootloader_common.h"
35 #include "bootloader/bl_peripheral.h"
36 #include "property/property.h"
37 
40 
42 // Declarations
44 
48 {
49  kSerialProtocol_Version_Name = 'P',
50  kSerialProtocol_Version_Major = 1,
51  kSerialProtocol_Version_Minor = 2,
52  kSerialProtocol_Version_Bugfix = 0
53 };
54 
57 {
58  kFramingPacketStartByte = 0x5a,
59  kFramingPacketType_Ack = 0xa1,
60  kFramingPacketType_Nak = 0xa2,
61  kFramingPacketType_AckAbort = 0xa3,
62  kFramingPacketType_Command = 0xa4,
63  kFramingPacketType_Data = 0xa5,
64  kFramingPacketType_Ping = 0xa6,
65  kFramingPacketType_PingResponse = 0xa7
66 };
67 
70 {
71  kHostMaxStartByteReadCount = 2,
74 };
75 
78 {
79  kIncomingPacketBufferSize = kMinPacketBufferSize,
80  kOutgoingPacketBufferSize = kMinPacketBufferSize
81 };
82 
85 {
86  kSerialModeCmd,
87  kSerialModeAck,
88  kSerialModeIdle
89 };
90 
92 typedef struct FramingHeader
93 {
94  uint8_t startByte;
95  uint8_t packetType;
97 
99 typedef struct FramingSyncPacket
100 {
103 
105 #pragma pack(1)
106 typedef struct FramingDataPacket
107 {
109  uint16_t length;
110  uint16_t crc16;
112 #pragma pack()
113 
115 typedef struct SerialFramingPacket
116 {
118  uint8_t data[kOutgoingPacketBufferSize];
120 
122 typedef struct SerialData
123 {
124  uint8_t data[kIncomingPacketBufferSize];
125  uint8_t callbackBuffer[kCallbackBufferSize];
127  volatile uint32_t writeOffset;
128  uint32_t readOffset;
129  bool isAckNeeded;
132 } serial_data_t;
133 
137 typedef struct PingResponse
138 {
140  uint16_t options;
141  uint16_t crc16;
143 
145 // Externs
147 
148 extern const peripheral_packet_interface_t g_framingPacketInterface;
149 
151 // Prototypes
153 
154 #if defined(__cplusplus)
155 extern "C" {
156 #endif // __cplusplus
157 
159 status_t serial_packet_init(const peripheral_descriptor_t *self);
160 
164 status_t serial_packet_read(const peripheral_descriptor_t *self,
165  uint8_t **packet,
166  uint32_t *packetLength,
167  packet_type_t packetType);
168 
170 status_t serial_packet_write(const peripheral_descriptor_t *self,
171  const uint8_t *packet,
172  uint32_t byteCount,
173  packet_type_t packetType);
174 
179 void serial_packet_abort(const peripheral_descriptor_t *self);
180 
182 status_t serial_packet_finalize(const peripheral_descriptor_t *self);
183 
185 uint32_t serial_packet_get_max_packet_size(const peripheral_descriptor_t *self);
186 
188 status_t serial_packet_send_sync(uint8_t framingPacketType);
189 
191 status_t serial_send_ping_response(const peripheral_descriptor_t *peripheral);
192 
194 void serial_packet_queue_byte(uint8_t byte);
195 
196 #if defined(BOOTLOADER_HOST)
197 void host_delay(uint32_t milliseconds);
198 #endif // BOOTLOADER_HOST
199 
200 #if defined(__cplusplus)
201 }
202 #endif // __cplusplus
203 
205 
206 #endif // _packet_h
207 // EOF
uint32_t serial_packet_get_max_packet_size(const peripheral_descriptor_t *self)
Get max packet size.
Definition: serial_packet.c:265
void serial_packet_abort(const peripheral_descriptor_t *self)
Abort data phase.
Definition: serial_packet.c:257
uint16_t length
Number of data bytes that follow.
Definition: serial_packet.h:109
_framing_packet_constants
Serial framing packet constants.
Definition: serial_packet.h:56
uint32_t readOffset
The offset into the buffer that the app has read out.
Definition: serial_packet.h:128
standard_version_t version
Serial framing protocol version.
Definition: serial_packet.h:139
packet_type_t
Packet types.
Definition: bl_peripheral.h:94
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
uint8_t packetType
Framing packet type.
Definition: serial_packet.h:95
bool isAckNeeded
True if need to send ACK to previously received packet.
Definition: serial_packet.h:129
Size for callback buffer, Must be power of 2 for easy wrap.
Definition: serial_packet.h:73
_timing_constants
Timeout and other constants.
Definition: serial_packet.h:69
framing_data_packet_t dataPacket
Packet header.
Definition: serial_packet.h:117
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
status_t serial_packet_init(const peripheral_descriptor_t *self)
Initialize component.
Definition: serial_packet.c:117
volatile uint32_t writeOffset
The offset into the buffer that the ISR will queue data into.
Definition: serial_packet.h:127
Serial framing data packet.
Definition: serial_packet.h:106
Peripheral Packet Interface.
Definition: bl_peripheral.h:101
_serial_packet_constants
Incoming data buffer allocation size.
Definition: serial_packet.h:77
bool isAckAbortNeeded
True if next ACK should be ACK Abort.
Definition: serial_packet.h:131
serial_framing_packet_t framingPacket
Buffer for outgoing packet.
Definition: serial_packet.h:126
void serial_packet_queue_byte(uint8_t byte)
Queues a byte received by the active peripheral.
Definition: serial_packet.c:110
Structure of version property.
Definition: bootloader_common.h:168
_serial_packet_mode
Packet state machine modes.
Definition: serial_packet.h:84
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
Version constants for serial framing protocol.
Definition: serial_packet.h:47
Format of global context data.
Definition: serial_packet.h:122
uint16_t options
Serial framing protocol options bitfield.
Definition: serial_packet.h:140
uint8_t startByte
#kFramingPacketStartByte
Definition: serial_packet.h:94
bool isBackToBackWrite
True if executing back-to-back write.
Definition: serial_packet.h:130
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
Serial ping response format.
Definition: serial_packet.h:137
Serial framing sync packet.
Definition: serial_packet.h:99
Serial framing header.
Definition: serial_packet.h:92
status_t serial_packet_finalize(const peripheral_descriptor_t *self)
Finalize.
Definition: serial_packet.c:123
Framing packet with data area.
Definition: serial_packet.h:115
uint16_t crc16
CRC-16 of data packet header and data.
Definition: serial_packet.h:110
Default value for receiving 1 byte timeout.
Definition: serial_packet.h:72
framing_header_t header
Framing packet header.
Definition: serial_packet.h:101
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:121