Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
SerialPacketizer.h
1 /*
2  * Copyright (c) 2013-14, 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 _serial_packetizer_h_
32 #define _serial_packetizer_h_
33 
34 #include "Packetizer.h"
35 #include "UartPeripheral.h"
36 #include "bootloader_common.h"
37 #include "packet/serial_packet.h"
38 
41 
42 namespace blfwk
43 {
44 // Forward declarations.
45 class Peripheral;
46 
51 {
52 public:
54  SerialPacketizer(UartPeripheral *peripheral, uint32_t packetTimeoutMs);
55 
57  virtual ~SerialPacketizer();
58 
60  virtual UartPeripheral *getPeripheral() { return dynamic_cast<UartPeripheral *>(m_peripheral); }
67  virtual status_t readPacket(uint8_t **packet, uint32_t *packetLength, packet_type_t packetType);
68 
73  virtual status_t writePacket(const uint8_t *packet, uint32_t byteCount, packet_type_t packetType);
74 
76  virtual void abortPacket();
77 
79  virtual void sync();
80 
82  virtual void finalize();
83 
85  virtual void enableSimulatorPump() {}
87  virtual void pumpSimulator() {}
89  virtual void setAborted(bool aborted) {}
91  virtual uint32_t getMaxPacketSize();
92 
94  void host_delay(uint32_t milliseconds);
95 
108  virtual status_t ping(int retries, unsigned int delay, ping_response_t *response, int comSpeed);
109 
110 protected:
112  status_t send_deferred_ack();
113 
117  status_t serial_packet_read(uint8_t **packet, uint32_t *packetLength, packet_type_t packetType);
118 
120  status_t serial_packet_write(const uint8_t *packet, uint32_t byteCount, packet_type_t packetType);
121 
126  void serial_packet_abort();
127 
130 
132  status_t serial_packet_send_sync(uint8_t framingPacketType);
133 
135  status_t serial_send_ping_response();
136 
138  status_t wait_for_ack_packet();
139 
141  status_t read_data_packet(framing_data_packet_t *packet, uint8_t *data, packet_type_t packetType);
142 
144  status_t read_start_byte(framing_header_t *header);
145 
147  status_t read_header(framing_header_t *header);
148 
150  status_t read_length(framing_data_packet_t *packet);
151 
153  status_t read_crc16(framing_data_packet_t *packet);
154 
156  uint16_t calculate_framing_crc16(framing_data_packet_t *packet, const uint8_t *data);
157 
158  serial_data_t m_serialContext;
159 };
160 
161 } // namespace blfwk
162 
164 
165 #endif // _serial_packetizer_h_
166 
168 // EOF
virtual void sync()
Send framing packet ack.
Definition: SerialPacketizer.cpp:111
Interface class for packetization of commands and data.
Definition: Packetizer.h:59
status_t serial_send_ping_response()
Send a ping message back in response to a ping.
Definition: SerialPacketizer.cpp:470
virtual void abortPacket()
Abort data phase.
Definition: SerialPacketizer.cpp:105
uint16_t calculate_framing_crc16(framing_data_packet_t *packet, const uint8_t *data)
Calculate crc over framing data packet.
Definition: SerialPacketizer.cpp:650
Definition: BlfwkErrors.h:16
Peripheral that talks to the target device over COM port hardware.
Definition: UartPeripheral.h:45
virtual status_t ping(int retries, unsigned int delay, ping_response_t *response, int comSpeed)
Send a ping packet and receive an ack.
Definition: SerialPacketizer.cpp:134
status_t serial_packet_read(uint8_t **packet, uint32_t *packetLength, packet_type_t packetType)
Read packet using serial framing.
Definition: SerialPacketizer.cpp:267
void serial_packet_abort()
Abort data phase.
Definition: SerialPacketizer.cpp:387
status_t serial_packet_write(const uint8_t *packet, uint32_t byteCount, packet_type_t packetType)
Write packet using serial framing.
Definition: SerialPacketizer.cpp:330
virtual status_t readPacket(uint8_t **packet, uint32_t *packetLength, packet_type_t packetType)
Read a packet.
Definition: SerialPacketizer.cpp:99
virtual void finalize()
Finalize.
Definition: SerialPacketizer.cpp:87
virtual status_t writePacket(const uint8_t *packet, uint32_t byteCount, packet_type_t packetType)
Write a packet.
Definition: SerialPacketizer.cpp:93
status_t read_length(framing_data_packet_t *packet)
Read from peripheral until packet length found.
Definition: SerialPacketizer.cpp:618
virtual uint32_t getMaxPacketSize()
Return the max packet size.
Definition: SerialPacketizer.cpp:117
status_t read_crc16(framing_data_packet_t *packet)
Read from peripheral until crc16 is found.
Definition: SerialPacketizer.cpp:634
void host_delay(uint32_t milliseconds)
Delay milliseconds.
Definition: SerialPacketizer.cpp:123
virtual void enableSimulatorPump()
Enable simulator command processor pump.
Definition: SerialPacketizer.h:85
virtual ~SerialPacketizer()
Destructor.
Definition: SerialPacketizer.cpp:81
virtual void setAborted(bool aborted)
Set aborted flag.
Definition: SerialPacketizer.h:89
SerialPacketizer(UartPeripheral *peripheral, uint32_t packetTimeoutMs)
Constructor.
Definition: SerialPacketizer.cpp:73
status_t read_data_packet(framing_data_packet_t *packet, uint8_t *data, packet_type_t packetType)
Read from peripheral until entire data framing packet read.
Definition: SerialPacketizer.cpp:507
status_t wait_for_ack_packet()
Wait for an ACK, handling NAKs as needed.
Definition: SerialPacketizer.cpp:420
virtual void pumpSimulator()
Pump simulator command processor.
Definition: SerialPacketizer.h:87
virtual UartPeripheral * getPeripheral()
Peripheral accessor.
Definition: SerialPacketizer.h:60
status_t serial_packet_send_sync(uint8_t framingPacketType)
Send a sync packet of the specified type.
Definition: SerialPacketizer.cpp:401
status_t read_start_byte(framing_header_t *header)
Read from peripheral until start byte found.
Definition: SerialPacketizer.cpp:563
Provides source and sink for packets that go over the serial peripherals.
Definition: SerialPacketizer.h:50
status_t send_deferred_ack()
Send ACK if needed.
Definition: SerialPacketizer.cpp:486
Peripheral * m_peripheral
Peripheral to send/receive bytes on.
Definition: Packetizer.h:113
uint32_t serial_packet_get_max_packet_size()
Get max packet size.
Definition: SerialPacketizer.cpp:395
status_t read_header(framing_header_t *header)
Read from peripheral until packet header found.
Definition: SerialPacketizer.cpp:604