Ymodem ESP32 1.0.0
Loading...
Searching...
No Matches
YmodemUtils.cpp
Go to the documentation of this file.
1
15#include "YmodemUtils.h"
16
17void IRAM_ATTR LED_toggle()
18{
19#if YMODEM_LED_ACT
20 if (GPIO.out & (1 << YMODEM_LED_ACT)) {
21 GPIO.out_w1tc = (1 << YMODEM_LED_ACT);
22 }
23 else {
24 GPIO.out_w1ts = (1 << YMODEM_LED_ACT);
25 }
26#endif
27}
28
29unsigned short crc16(const unsigned char* buf, unsigned long count)
30{
31 unsigned short crc = 0;
32 int i;
33
34 while (count--) {
35 crc = crc ^ *buf++ << 8;
36
37 for (i = 0; i < 8; i++) {
38 if (crc & 0x8000)
39 crc = crc << 1 ^ 0x1021;
40 else
41 crc = crc << 1;
42 }
43 }
44 return crc;
45}
46
47int32_t Receive_Byte(unsigned char* c, uint32_t timeout)
48{
49 unsigned char ch;
50 int len = uart_read_bytes(EX_UART_NUM, &ch, 1, timeout / portTICK_RATE_MS);
51 if (len <= 0)
52 return -1;
53
54 *c = ch;
55 return 0;
56}
57
59{
60 uint8_t ch[64];
61 while (uart_read_bytes(EX_UART_NUM, ch, 64, 100 / portTICK_RATE_MS) > 0)
62 ;
63}
64
65uint32_t Send_Byte(char c)
66{
67 int err = uart_write_bytes(EX_UART_NUM, &c, 1);
68 if (err < 0) {
69 return -1;
70 }
71 return 0;
72}
73
74void send_CA()
75{
78}
79
81{
83}
84
86{
89}
90
92{
94}
95
97{
99}
100
101int32_t Receive_Packet(uint8_t* data, int* length, uint32_t timeout)
102{
103 int count, packet_size, i;
104 unsigned char ch;
105 *length = 0;
106
107 // receive 1st byte
108 if (Receive_Byte(&ch, timeout) < 0) {
109 return -1;
110 }
111
112 switch (ch) {
113 case SOH:
114 packet_size = PACKET_SIZE;
115 break;
116 case STX:
117 packet_size = PACKET_1K_SIZE;
118 break;
119 case EOT:
120 *length = 0;
121 return 0;
122 case CA:
123 if (Receive_Byte(&ch, timeout) < 0) {
124 return -2;
125 }
126 if (ch == CA) {
127 *length = -1;
128 return 0;
129 }
130 else
131 return -1;
132 case ABORT1:
133 case ABORT2:
134 return -2;
135 default:
136 vTaskDelay(100 / portTICK_RATE_MS);
137 uart_consume();
138 return -1;
139 }
140
141 *data = (uint8_t)ch;
142 uint8_t* dptr = data + 1;
143 count = packet_size + PACKET_OVERHEAD - 1;
144
145 for (i = 0; i < count; i++) {
146 if (Receive_Byte(&ch, timeout) < 0) {
147 return -1;
148 }
149 *dptr++ = (uint8_t)ch;
150 ;
151 }
152
153 if (data[PACKET_SEQNO_INDEX] != ((data[PACKET_SEQNO_COMP_INDEX] ^ 0xff) & 0xff)) {
154 *length = -2;
155 return 0;
156 }
157 if (crc16(&data[PACKET_HEADER], packet_size + PACKET_TRAILER) != 0) {
158 *length = -2;
159 return 0;
160 }
161
162 *length = packet_size;
163 return 0;
164}
#define CRC16
Definition YmodemDef.h:42
#define PACKET_HEADER
Definition YmodemDef.h:29
#define EX_UART_NUM
Definition YmodemDef.h:18
#define PACKET_SEQNO_COMP_INDEX
Definition YmodemDef.h:28
#define YMODEM_LED_ACT
Definition YmodemDef.h:23
#define PACKET_1K_SIZE
Definition YmodemDef.h:33
#define ACK
Definition YmodemDef.h:39
#define ABORT1
Definition YmodemDef.h:44
#define NAK
Definition YmodemDef.h:40
#define PACKET_SEQNO_INDEX
Definition YmodemDef.h:27
#define ABORT2
Definition YmodemDef.h:45
#define PACKET_OVERHEAD
Definition YmodemDef.h:31
#define EOT
Definition YmodemDef.h:38
#define STX
Definition YmodemDef.h:37
#define SOH
Definition YmodemDef.h:36
#define PACKET_TRAILER
Definition YmodemDef.h:30
#define CA
Definition YmodemDef.h:41
#define PACKET_SIZE
Definition YmodemDef.h:32
void IRAM_ATTR LED_toggle()
Toggles the state of an LED.
void send_CRC16()
Sends the CRC16 checksum.
void send_ACKCRC16()
Sends an acknowledgment with CRC16.
unsigned short crc16(const unsigned char *buf, unsigned long count)
Computes the CRC-16 checksum for a given buffer.
int32_t Receive_Packet(uint8_t *data, int *length, uint32_t timeout)
Receives a packet of data.
void send_CA()
Sends the CA (Cancel) signal.
uint32_t Send_Byte(char c)
Sends a single byte of data.
void send_ACK()
Sends an acknowledgment (ACK) signal.
int32_t Receive_Byte(unsigned char *c, uint32_t timeout)
Receives a byte from a communication interface.
void send_NAK()
Sends a Negative Acknowledgement (NAK) signal.
void uart_consume()
Consumes data from the UART buffer.
Ymodem utility functions.