-
Notifications
You must be signed in to change notification settings - Fork 2
/
af_i32.c
142 lines (119 loc) · 2.96 KB
/
af_i32.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdint.h>
#include "modem.h"
#include "rmt.h"
#include "afio.h"
#define BUF_SIZE 1024
#define ITEM32_SIZE (BUF_SIZE*8+1)
#define RMT_TICK (APB_CLK / RMT_CLK_DIV)
#define TXD_DURATION (RMT_TICK / BAUD_RATE) // 1/1200 us
#define TXD_DURATION_MARK (1000*1000 / BAUD_RATE_MARK) // 1/1200 us
#define TXD_DURATION_SPACE (1000*1000 / BAUD_RATE_SPACE) // 1/1200 us
/* Euclidean Algorithm */
int euclid(int a, int b)
{
int dividend;
int divisor;
int remainder;
int quotient;
if (a <= 0 || b <= 0) return -1;
if (a > b) {
dividend = a;
divisor = b;
} else {
dividend = b;
divisor = a;
}
remainder = dividend % divisor;
if (remainder == 0) return divisor;
return euclid(divisor, remainder);
}
void make_item32(FILE *fp, char buf[], int bit_length)
{
rmt_item32_t item32[ITEM32_SIZE];
rmt_item16_t *item16 = (rmt_item16_t *)item32;
int i;
int j;
int size;
int index;
struct {
uint32_t size:16;
uint32_t id:16;
} header;
int level;
int duration;
int bit;
int cnt;
int du;
int gcd, dividend;
static int divisor = 0, remainder = 0;
/* initialize pulse length calculation parameter */
if (divisor == 0) {
gcd = euclid(RMT_TICK, BAUD_RATE); // calculation Great Common Divider
dividend = RMT_TICK / gcd; // 1 bit time is dividend / divisor
divisor = BAUD_RATE / gcd;
if (divisor > 1) { // need fraction processing
remainder = dividend % divisor;
}
fprintf(stderr, "af_i32: gcd: %d, divisor: %d, remainder: %d\n", gcd, divisor, remainder);
}
if (bit_length > BUF_SIZE * 8) return;
cnt = remainder / 2; // prepair for DDA
index = 0;
duration = 0;
level = 1; // assume idle state is MARK(1)
for (i = 0; i < bit_length; i++) {
bit = (buf[i / 8] >> (i % 8)) & 1;
du = TXD_DURATION;
// fractional correction using DDA
if (divisor != 1) {
if (cnt >= divisor) {
du++; // increment frequency is remainder / divisor
cnt -= divisor;
}
cnt += remainder;
}
if (bit == level) {
duration += du;
} else {
while (duration > 0) {
int d = (duration < 32768) ? duration : 32767;
item16[index].duration = d;
item16[index].level = level;
index++;
duration -= d;
}
level = bit;
duration = du;
}
}
if (level != 1) { // not idle state
item16[index].duration = duration;
item16[index].level = level;
index++;
}
/* end mark */
item16[index].duration = 0;
item16[index].level = 0;
index++;
size = (index + 1) / 2;
#define ID_TX_DATA 0x0001
/* header */
header.id = ID_TX_DATA;
header.size = size;
fwrite((char *)&header, sizeof(header), 1, fp);
fwrite((char *)item32, sizeof(item32[0]), size, fp);
}
int main(int argc, char *argv[])
{
FILE *ifp = stdin;
FILE *ofp = stdout;
char buf[BUF_SIZE];
int bit_length;
while ((bit_length = afio_read(ifp, buf, BUF_SIZE)) >= 0) {
if (bit_length > 0) {
make_item32(ofp, buf, bit_length);
}
}
return 0;
}