-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxfer.c
122 lines (99 loc) · 2.62 KB
/
xfer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* LIX OS
* Copyright (c) 2021 Lone Dynamics Corporation. All rights reserved.
*
* Redistribution and use in source, binary or physical forms, with or without
* modification, is permitted provided that the following condition is met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* THIS HARDWARE, SOFTWARE, DATA AND/OR DOCUMENTATION ("THE ASSETS") IS
* PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE ASSETS OR THE USE OR OTHER
* DEALINGS IN THE ASSETS. USE AT YOUR OWN RISK.
*
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "xfer.h"
#include "../common/zucker.h"
void getchars(char *buf, int len);
uint32_t crc32b(char *data, uint32_t len);
void myputch(int c);
int mygetch(void);
void getchars(char *buf, int len) {
int c;
for (int i = 0; i < len; i++) {
while ((c = mygetch()) == EOF);
buf[i] = (uint8_t)c;
};
}
void myputch(int c)
{
while ((reg_uart_ctrl & UART_CTRL_TXBUSY) == UART_CTRL_TXBUSY);
reg_uart_data = (char)c;
}
int mygetch(void)
{
if ((reg_uart_ctrl & UART_CTRL_DR) != UART_CTRL_DR)
return EOF;
else
return (char)reg_uart_data;
}
uint32_t xfer_recv(uint32_t addr_ptr)
{
uint32_t addr = addr_ptr;
uint32_t bytes = 0;
uint32_t crc_ours;
uint32_t crc_theirs;
char buf_data[252];
char buf_crc[4];
int cmd;
int datasize;
while (1) {
while ((cmd = mygetch()) == EOF);
buf_data[0] = (uint8_t)cmd;
if ((char)cmd == 'L') {
while ((datasize = mygetch()) == EOF);
buf_data[1] = (uint8_t)datasize;
getchars(&buf_data[2], datasize);
getchars(buf_crc, 4);
crc_ours = crc32b(buf_data, 2 + datasize);
crc_theirs = buf_crc[0] | (buf_crc[1] << 8) |
(buf_crc[2] << 16) | (buf_crc[3] << 24);
if (crc_ours == crc_theirs) {
memcpy((void *)addr, &buf_data[2], datasize);
addr += datasize;
bytes += datasize;
myputch('A');
} else {
myputch('N');
}
}
if ((char)cmd == 'D') {
break;
}
}
return bytes;
}
uint32_t crc32b(char *data, uint32_t len) {
uint32_t byte, crc, mask;
crc = 0xffffffff;
for (int i = 0; i < len; i++) {
byte = data[i];
crc = crc ^ byte;
for (int j = 7; j >= 0; j--) {
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xedb88320 & mask);
}
i = i + 1;
}
return ~crc;
}