Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 #include <vector> 00004 00005 Serial pc(USBTX, USBRX); // tx, rx 00006 Serial c2c(p13, p14); // tx, rx 00007 DigitalInOut nREQ(p15); 00008 DigitalInOut nRDY(p16); 00009 00010 00011 // Fletcher checksum calculation, not optimised 00012 static uint16_t checkSum(uint8_t length_byte, uint8_t* buf){ 00013 // length is treated as 16-bit for checksum calculation 00014 uint16_t sum1 = length_byte % 255; 00015 uint16_t sum2 = sum1; 00016 00017 for (int i = 0; i < length_byte; i++) { 00018 sum1 = (sum1 + buf[i]) % 255; 00019 sum2 = (sum2 + sum1) % 255; 00020 } 00021 00022 return ((sum2 << 8) | sum1); 00023 } 00024 00025 void sendTestPacket(){ 00026 pc.printf("\n\rTX: wait for nRDY... "); 00027 00028 nREQ = 0; 00029 nREQ.mode(PinMode(OpenDrain)); 00030 nREQ.output(); 00031 00032 nRDY = 1; 00033 nRDY.input(); 00034 00035 // wait for nRDY 00036 while(nRDY == 1) 00037 wait(0.1); 00038 00039 pc.printf("\n\rgot nRDY\n\r"); 00040 const uint8_t len = 0; 00041 00042 c2c.putc(len); 00043 while(!c2c.writeable()) 00044 ; 00045 pc.printf("wrote len=%d\n\r", len); 00046 uint8_t data[len==0? 256 : len]; 00047 for(int i = 0; i < (len==0? 256 : len); i++){ 00048 data[i] = uint8_t((i + 31) & 0xff); 00049 c2c.putc(data[i]); 00050 pc.printf("."); 00051 while(!c2c.writeable()) 00052 ; 00053 } 00054 pc.printf("\n\rwrote data\n\r", len); 00055 uint16_t csum = checkSum(len, data); 00056 c2c.putc(uint8_t(csum & 0xff)); 00057 while(!c2c.writeable()) 00058 ; 00059 c2c.putc(uint8_t((csum >> 8) & 0xff)); 00060 pc.printf("\n\rwrote checksum\n\r", len); 00061 } 00062 00063 void receiveTestPacket(){ 00064 pc.printf("RX: wait for nREQ..."); 00065 00066 nREQ = 0; 00067 nRDY = 0; 00068 nREQ.mode(PinMode(OpenDrain | PullUp)); 00069 nRDY.mode(PinMode(OpenDrain | PullUp)); 00070 nREQ.output(); 00071 nRDY.output(); 00072 00073 // nREQ.input(); 00074 // nRDY = 1; 00075 // nRDY.mode(PinMode(OpenDrain)); 00076 00077 while(nREQ == 0) 00078 wait(0.1); 00079 00080 pc.printf("\n\rgot REQ\n\r"); 00081 nRDY = 1; 00082 int len = c2c.getc(); 00083 std::vector<uint8_t> msg; 00084 for(int i = 0; i < len; i++) 00085 msg.push_back(c2c.getc()); 00086 00087 uint16_t csum = 0; 00088 for(int i =0; i < 2; i++) 00089 csum |= (uint16_t(c2c.getc()) << (i*8)); 00090 00091 00092 pc.printf("len=%d\n\r", len); 00093 for(int i = 0; i < len; i++) 00094 pc.printf("0x%x ", msg[i]); 00095 pc.printf("\n\rcsum : 0x%x\n\r", csum); 00096 00097 uint16_t check_checksum = checkSum(len, &msg[0]); 00098 pc.printf("check: 0x%x\n\r", check_checksum); 00099 00100 nRDY = 0; 00101 } 00102 00103 int main() { 00104 pc.printf("\n\r--------\n\rc2c UART Test\n\r"); 00105 00106 c2c.baud(38400); 00107 00108 // while(1) { 00109 // if(c2c.readable()) { 00110 // pc.putc(c2c.getc()); 00111 // } 00112 // } 00113 00114 if(c2c.readable()) 00115 pc.printf("Leftover characters:"); 00116 while(c2c.readable()) 00117 pc.printf("0x%x ", int(c2c.getc())); 00118 pc.printf("\n\r"); 00119 00120 00121 // receive rx_count things 00122 int rx_count = 1; 00123 while(rx_count > 0){ 00124 receiveTestPacket(); 00125 } 00126 00127 // sendTestPacket(); 00128 // 00129 pc.printf("\n\r--- complete ---\n\r"); 00130 }
Generated on Fri Aug 19 2022 13:25:52 by
1.7.2