hello

Dependencies:   mbed

Committer:
GeofferyOmlette
Date:
Mon Jul 14 15:13:45 2014 +0000
Revision:
0:f2ba1ee4ee06
hello

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GeofferyOmlette 0:f2ba1ee4ee06 1 #include "mbed.h"
GeofferyOmlette 0:f2ba1ee4ee06 2
GeofferyOmlette 0:f2ba1ee4ee06 3 #include <vector>
GeofferyOmlette 0:f2ba1ee4ee06 4
GeofferyOmlette 0:f2ba1ee4ee06 5 Serial pc(USBTX, USBRX); // tx, rx
GeofferyOmlette 0:f2ba1ee4ee06 6 Serial c2c(p13, p14); // tx, rx
GeofferyOmlette 0:f2ba1ee4ee06 7 DigitalInOut nREQ(p15);
GeofferyOmlette 0:f2ba1ee4ee06 8 DigitalInOut nRDY(p16);
GeofferyOmlette 0:f2ba1ee4ee06 9
GeofferyOmlette 0:f2ba1ee4ee06 10
GeofferyOmlette 0:f2ba1ee4ee06 11 // Fletcher checksum calculation, not optimised
GeofferyOmlette 0:f2ba1ee4ee06 12 static uint16_t checkSum(uint8_t length_byte, uint8_t* buf){
GeofferyOmlette 0:f2ba1ee4ee06 13 // length is treated as 16-bit for checksum calculation
GeofferyOmlette 0:f2ba1ee4ee06 14 uint16_t sum1 = length_byte % 255;
GeofferyOmlette 0:f2ba1ee4ee06 15 uint16_t sum2 = sum1;
GeofferyOmlette 0:f2ba1ee4ee06 16
GeofferyOmlette 0:f2ba1ee4ee06 17 for (int i = 0; i < length_byte; i++) {
GeofferyOmlette 0:f2ba1ee4ee06 18 sum1 = (sum1 + buf[i]) % 255;
GeofferyOmlette 0:f2ba1ee4ee06 19 sum2 = (sum2 + sum1) % 255;
GeofferyOmlette 0:f2ba1ee4ee06 20 }
GeofferyOmlette 0:f2ba1ee4ee06 21
GeofferyOmlette 0:f2ba1ee4ee06 22 return ((sum2 << 8) | sum1);
GeofferyOmlette 0:f2ba1ee4ee06 23 }
GeofferyOmlette 0:f2ba1ee4ee06 24
GeofferyOmlette 0:f2ba1ee4ee06 25 void sendTestPacket(){
GeofferyOmlette 0:f2ba1ee4ee06 26 pc.printf("\n\rTX: wait for nRDY... ");
GeofferyOmlette 0:f2ba1ee4ee06 27
GeofferyOmlette 0:f2ba1ee4ee06 28 nREQ = 0;
GeofferyOmlette 0:f2ba1ee4ee06 29 nREQ.mode(PinMode(OpenDrain));
GeofferyOmlette 0:f2ba1ee4ee06 30 nREQ.output();
GeofferyOmlette 0:f2ba1ee4ee06 31
GeofferyOmlette 0:f2ba1ee4ee06 32 nRDY = 1;
GeofferyOmlette 0:f2ba1ee4ee06 33 nRDY.input();
GeofferyOmlette 0:f2ba1ee4ee06 34
GeofferyOmlette 0:f2ba1ee4ee06 35 // wait for nRDY
GeofferyOmlette 0:f2ba1ee4ee06 36 while(nRDY == 1)
GeofferyOmlette 0:f2ba1ee4ee06 37 wait(0.1);
GeofferyOmlette 0:f2ba1ee4ee06 38
GeofferyOmlette 0:f2ba1ee4ee06 39 pc.printf("\n\rgot nRDY\n\r");
GeofferyOmlette 0:f2ba1ee4ee06 40 const uint8_t len = 0;
GeofferyOmlette 0:f2ba1ee4ee06 41
GeofferyOmlette 0:f2ba1ee4ee06 42 c2c.putc(len);
GeofferyOmlette 0:f2ba1ee4ee06 43 while(!c2c.writeable())
GeofferyOmlette 0:f2ba1ee4ee06 44 ;
GeofferyOmlette 0:f2ba1ee4ee06 45 pc.printf("wrote len=%d\n\r", len);
GeofferyOmlette 0:f2ba1ee4ee06 46 uint8_t data[len==0? 256 : len];
GeofferyOmlette 0:f2ba1ee4ee06 47 for(int i = 0; i < (len==0? 256 : len); i++){
GeofferyOmlette 0:f2ba1ee4ee06 48 data[i] = uint8_t((i + 31) & 0xff);
GeofferyOmlette 0:f2ba1ee4ee06 49 c2c.putc(data[i]);
GeofferyOmlette 0:f2ba1ee4ee06 50 pc.printf(".");
GeofferyOmlette 0:f2ba1ee4ee06 51 while(!c2c.writeable())
GeofferyOmlette 0:f2ba1ee4ee06 52 ;
GeofferyOmlette 0:f2ba1ee4ee06 53 }
GeofferyOmlette 0:f2ba1ee4ee06 54 pc.printf("\n\rwrote data\n\r", len);
GeofferyOmlette 0:f2ba1ee4ee06 55 uint16_t csum = checkSum(len, data);
GeofferyOmlette 0:f2ba1ee4ee06 56 c2c.putc(uint8_t(csum & 0xff));
GeofferyOmlette 0:f2ba1ee4ee06 57 while(!c2c.writeable())
GeofferyOmlette 0:f2ba1ee4ee06 58 ;
GeofferyOmlette 0:f2ba1ee4ee06 59 c2c.putc(uint8_t((csum >> 8) & 0xff));
GeofferyOmlette 0:f2ba1ee4ee06 60 pc.printf("\n\rwrote checksum\n\r", len);
GeofferyOmlette 0:f2ba1ee4ee06 61 }
GeofferyOmlette 0:f2ba1ee4ee06 62
GeofferyOmlette 0:f2ba1ee4ee06 63 void receiveTestPacket(){
GeofferyOmlette 0:f2ba1ee4ee06 64 pc.printf("RX: wait for nREQ...");
GeofferyOmlette 0:f2ba1ee4ee06 65
GeofferyOmlette 0:f2ba1ee4ee06 66 nREQ = 0;
GeofferyOmlette 0:f2ba1ee4ee06 67 nRDY = 0;
GeofferyOmlette 0:f2ba1ee4ee06 68 nREQ.mode(PinMode(OpenDrain | PullUp));
GeofferyOmlette 0:f2ba1ee4ee06 69 nRDY.mode(PinMode(OpenDrain | PullUp));
GeofferyOmlette 0:f2ba1ee4ee06 70 nREQ.output();
GeofferyOmlette 0:f2ba1ee4ee06 71 nRDY.output();
GeofferyOmlette 0:f2ba1ee4ee06 72
GeofferyOmlette 0:f2ba1ee4ee06 73 // nREQ.input();
GeofferyOmlette 0:f2ba1ee4ee06 74 // nRDY = 1;
GeofferyOmlette 0:f2ba1ee4ee06 75 // nRDY.mode(PinMode(OpenDrain));
GeofferyOmlette 0:f2ba1ee4ee06 76
GeofferyOmlette 0:f2ba1ee4ee06 77 while(nREQ == 0)
GeofferyOmlette 0:f2ba1ee4ee06 78 wait(0.1);
GeofferyOmlette 0:f2ba1ee4ee06 79
GeofferyOmlette 0:f2ba1ee4ee06 80 pc.printf("\n\rgot REQ\n\r");
GeofferyOmlette 0:f2ba1ee4ee06 81 nRDY = 1;
GeofferyOmlette 0:f2ba1ee4ee06 82 int len = c2c.getc();
GeofferyOmlette 0:f2ba1ee4ee06 83 std::vector<uint8_t> msg;
GeofferyOmlette 0:f2ba1ee4ee06 84 for(int i = 0; i < len; i++)
GeofferyOmlette 0:f2ba1ee4ee06 85 msg.push_back(c2c.getc());
GeofferyOmlette 0:f2ba1ee4ee06 86
GeofferyOmlette 0:f2ba1ee4ee06 87 uint16_t csum = 0;
GeofferyOmlette 0:f2ba1ee4ee06 88 for(int i =0; i < 2; i++)
GeofferyOmlette 0:f2ba1ee4ee06 89 csum |= (uint16_t(c2c.getc()) << (i*8));
GeofferyOmlette 0:f2ba1ee4ee06 90
GeofferyOmlette 0:f2ba1ee4ee06 91
GeofferyOmlette 0:f2ba1ee4ee06 92 pc.printf("len=%d\n\r", len);
GeofferyOmlette 0:f2ba1ee4ee06 93 for(int i = 0; i < len; i++)
GeofferyOmlette 0:f2ba1ee4ee06 94 pc.printf("0x%x ", msg[i]);
GeofferyOmlette 0:f2ba1ee4ee06 95 pc.printf("\n\rcsum : 0x%x\n\r", csum);
GeofferyOmlette 0:f2ba1ee4ee06 96
GeofferyOmlette 0:f2ba1ee4ee06 97 uint16_t check_checksum = checkSum(len, &msg[0]);
GeofferyOmlette 0:f2ba1ee4ee06 98 pc.printf("check: 0x%x\n\r", check_checksum);
GeofferyOmlette 0:f2ba1ee4ee06 99
GeofferyOmlette 0:f2ba1ee4ee06 100 nRDY = 0;
GeofferyOmlette 0:f2ba1ee4ee06 101 }
GeofferyOmlette 0:f2ba1ee4ee06 102
GeofferyOmlette 0:f2ba1ee4ee06 103 int main() {
GeofferyOmlette 0:f2ba1ee4ee06 104 pc.printf("\n\r--------\n\rc2c UART Test\n\r");
GeofferyOmlette 0:f2ba1ee4ee06 105
GeofferyOmlette 0:f2ba1ee4ee06 106 c2c.baud(38400);
GeofferyOmlette 0:f2ba1ee4ee06 107
GeofferyOmlette 0:f2ba1ee4ee06 108 // while(1) {
GeofferyOmlette 0:f2ba1ee4ee06 109 // if(c2c.readable()) {
GeofferyOmlette 0:f2ba1ee4ee06 110 // pc.putc(c2c.getc());
GeofferyOmlette 0:f2ba1ee4ee06 111 // }
GeofferyOmlette 0:f2ba1ee4ee06 112 // }
GeofferyOmlette 0:f2ba1ee4ee06 113
GeofferyOmlette 0:f2ba1ee4ee06 114 if(c2c.readable())
GeofferyOmlette 0:f2ba1ee4ee06 115 pc.printf("Leftover characters:");
GeofferyOmlette 0:f2ba1ee4ee06 116 while(c2c.readable())
GeofferyOmlette 0:f2ba1ee4ee06 117 pc.printf("0x%x ", int(c2c.getc()));
GeofferyOmlette 0:f2ba1ee4ee06 118 pc.printf("\n\r");
GeofferyOmlette 0:f2ba1ee4ee06 119
GeofferyOmlette 0:f2ba1ee4ee06 120
GeofferyOmlette 0:f2ba1ee4ee06 121 // receive rx_count things
GeofferyOmlette 0:f2ba1ee4ee06 122 int rx_count = 1;
GeofferyOmlette 0:f2ba1ee4ee06 123 while(rx_count > 0){
GeofferyOmlette 0:f2ba1ee4ee06 124 receiveTestPacket();
GeofferyOmlette 0:f2ba1ee4ee06 125 }
GeofferyOmlette 0:f2ba1ee4ee06 126
GeofferyOmlette 0:f2ba1ee4ee06 127 // sendTestPacket();
GeofferyOmlette 0:f2ba1ee4ee06 128 //
GeofferyOmlette 0:f2ba1ee4ee06 129 pc.printf("\n\r--- complete ---\n\r");
GeofferyOmlette 0:f2ba1ee4ee06 130 }