![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
demo block-based serial transfer
main.cpp@0:0d85b515c87d, 2018-01-29 (annotated)
- Committer:
- bwang
- Date:
- Mon Jan 29 10:49:38 2018 +0000
- Revision:
- 0:0d85b515c87d
demo;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwang | 0:0d85b515c87d | 1 | #include "mbed.h" |
bwang | 0:0d85b515c87d | 2 | |
bwang | 0:0d85b515c87d | 3 | #define PAGE_SIZE 4096 |
bwang | 0:0d85b515c87d | 4 | #define HEADER_SIZE 10 |
bwang | 0:0d85b515c87d | 5 | #define PACKET_SIZE 8 |
bwang | 0:0d85b515c87d | 6 | #define BAUD_RATE 921600 |
bwang | 0:0d85b515c87d | 7 | #define F_SW 5000.0f |
bwang | 0:0d85b515c87d | 8 | |
bwang | 0:0d85b515c87d | 9 | #define clip(a) if ((a > 254)) a = 254 |
bwang | 0:0d85b515c87d | 10 | |
bwang | 0:0d85b515c87d | 11 | Serial serial4(A0, A1); |
bwang | 0:0d85b515c87d | 12 | Ticker tick; |
bwang | 0:0d85b515c87d | 13 | DigitalOut test(PC_8); |
bwang | 0:0d85b515c87d | 14 | DigitalOut test2(PC_6); |
bwang | 0:0d85b515c87d | 15 | DigitalOut led(LED1); |
bwang | 0:0d85b515c87d | 16 | |
bwang | 0:0d85b515c87d | 17 | int index = 0; |
bwang | 0:0d85b515c87d | 18 | char *front, *back, *tmp; |
bwang | 0:0d85b515c87d | 19 | char buf1[PAGE_SIZE], buf2[PAGE_SIZE]; |
bwang | 0:0d85b515c87d | 20 | |
bwang | 0:0d85b515c87d | 21 | unsigned char a = 0, b = 32, c = 64, d = 96, e = 128, f = 160, g = 192, h = 224; |
bwang | 0:0d85b515c87d | 22 | void tic() { |
bwang | 0:0d85b515c87d | 23 | test = 1; |
bwang | 0:0d85b515c87d | 24 | volatile int x; |
bwang | 0:0d85b515c87d | 25 | for (x = 0; x < 1000; x++) { |
bwang | 0:0d85b515c87d | 26 | } |
bwang | 0:0d85b515c87d | 27 | front[index] = 0xff; |
bwang | 0:0d85b515c87d | 28 | front[index+1] = a++; |
bwang | 0:0d85b515c87d | 29 | front[index+2] = b++; |
bwang | 0:0d85b515c87d | 30 | front[index+3] = c++; |
bwang | 0:0d85b515c87d | 31 | front[index+4] = d++; |
bwang | 0:0d85b515c87d | 32 | front[index+5] = e++; |
bwang | 0:0d85b515c87d | 33 | front[index+6] = f++; |
bwang | 0:0d85b515c87d | 34 | front[index+7] = g++; |
bwang | 0:0d85b515c87d | 35 | front[index+8] = h++; |
bwang | 0:0d85b515c87d | 36 | if (a == 0xff) a = 0; |
bwang | 0:0d85b515c87d | 37 | if (b == 0xff) b = 0; |
bwang | 0:0d85b515c87d | 38 | if (c == 0xff) c = 0; |
bwang | 0:0d85b515c87d | 39 | if (d == 0xff) d = 0; |
bwang | 0:0d85b515c87d | 40 | if (e == 0xff) e = 0; |
bwang | 0:0d85b515c87d | 41 | if (f == 0xff) f = 0; |
bwang | 0:0d85b515c87d | 42 | if (g == 0xff) g = 0; |
bwang | 0:0d85b515c87d | 43 | if (h == 0xff) h = 0; |
bwang | 0:0d85b515c87d | 44 | index += PACKET_SIZE+1; |
bwang | 0:0d85b515c87d | 45 | test = 0; |
bwang | 0:0d85b515c87d | 46 | } |
bwang | 0:0d85b515c87d | 47 | |
bwang | 0:0d85b515c87d | 48 | int main() { |
bwang | 0:0d85b515c87d | 49 | front = buf1; |
bwang | 0:0d85b515c87d | 50 | back = buf2; |
bwang | 0:0d85b515c87d | 51 | |
bwang | 0:0d85b515c87d | 52 | serial4.baud(BAUD_RATE); |
bwang | 0:0d85b515c87d | 53 | tick.attach(tic, 1/F_SW); |
bwang | 0:0d85b515c87d | 54 | |
bwang | 0:0d85b515c87d | 55 | for (;;) { |
bwang | 0:0d85b515c87d | 56 | if (index == PAGE_SIZE-HEADER_SIZE) { |
bwang | 0:0d85b515c87d | 57 | test2 = 1; |
bwang | 0:0d85b515c87d | 58 | led = 1; |
bwang | 0:0d85b515c87d | 59 | index = 0; |
bwang | 0:0d85b515c87d | 60 | tmp = front; |
bwang | 0:0d85b515c87d | 61 | front = back; |
bwang | 0:0d85b515c87d | 62 | back = tmp; |
bwang | 0:0d85b515c87d | 63 | for (int i = 0; i < PAGE_SIZE-HEADER_SIZE; i++) serial4.putc(back[i]); |
bwang | 0:0d85b515c87d | 64 | led = 0; |
bwang | 0:0d85b515c87d | 65 | test2 = 0; |
bwang | 0:0d85b515c87d | 66 | } |
bwang | 0:0d85b515c87d | 67 | } |
bwang | 0:0d85b515c87d | 68 | } |