Microduino

Dependencies:   mbed

Fork of BalanceCar by Li Weiyi

Committer:
lixianyu
Date:
Sat Jun 04 03:16:52 2016 +0000
Revision:
0:a4d8f5b3c546
Child:
2:99785a1007a4
Pass compile!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:a4d8f5b3c546 1 #include "Microduino_Protocol_HardSer.h"
lixianyu 0:a4d8f5b3c546 2 #include "MicroduinoPinNames.h"
lixianyu 0:a4d8f5b3c546 3
lixianyu 0:a4d8f5b3c546 4 extern Timer g_Timer;
lixianyu 0:a4d8f5b3c546 5 //DigitalOut myled(D13);
lixianyu 0:a4d8f5b3c546 6 uint8_t getChecksum(uint8_t length, uint8_t cmd, uint8_t mydata[])
lixianyu 0:a4d8f5b3c546 7 {
lixianyu 0:a4d8f5b3c546 8 //三个参数分别为: 数据长度 , 指令代码 , 实际数据数组
lixianyu 0:a4d8f5b3c546 9 uint8_t checksum = 0;
lixianyu 0:a4d8f5b3c546 10 checksum ^= (length & 0xFF);
lixianyu 0:a4d8f5b3c546 11 checksum ^= (cmd & 0xFF);
lixianyu 0:a4d8f5b3c546 12 for (int i = 0; i < length; i++) checksum ^= (mydata[i] & 0xFF);
lixianyu 0:a4d8f5b3c546 13 return checksum;
lixianyu 0:a4d8f5b3c546 14 }
lixianyu 0:a4d8f5b3c546 15
lixianyu 0:a4d8f5b3c546 16 /* Protocol::Protocol(PRO_PORT *ser , byte _channel) {
lixianyu 0:a4d8f5b3c546 17 // common_init(); // Set everything to common state, then...
lixianyu 0:a4d8f5b3c546 18 this->channel = _channel;
lixianyu 0:a4d8f5b3c546 19 this->num = 0;
lixianyu 0:a4d8f5b3c546 20 this->sta = false;
lixianyu 0:a4d8f5b3c546 21 this->error = false;
lixianyu 0:a4d8f5b3c546 22 P_Serial = ser; // ...override P_Serial with value passed.
lixianyu 0:a4d8f5b3c546 23 } */
lixianyu 0:a4d8f5b3c546 24 static void uart_callback(void)
lixianyu 0:a4d8f5b3c546 25 {
lixianyu 0:a4d8f5b3c546 26 //myled = !myled;
lixianyu 0:a4d8f5b3c546 27 }
lixianyu 0:a4d8f5b3c546 28 void Protocol::begin(uint16_t _baud)
lixianyu 0:a4d8f5b3c546 29 {
lixianyu 0:a4d8f5b3c546 30 //myled = 0;
lixianyu 0:a4d8f5b3c546 31 //_timer.start();
lixianyu 0:a4d8f5b3c546 32 //P_Serial->begin(_baud);
lixianyu 0:a4d8f5b3c546 33 P_Serial->baud(_baud);
lixianyu 0:a4d8f5b3c546 34 //P_Serial->attach(uart_callback, Serial::RxIrq);
lixianyu 0:a4d8f5b3c546 35 //delay(20);
lixianyu 0:a4d8f5b3c546 36 //wait_ms(20);
lixianyu 0:a4d8f5b3c546 37 }
lixianyu 0:a4d8f5b3c546 38
lixianyu 0:a4d8f5b3c546 39 bool Protocol::available(bool _sta)
lixianyu 0:a4d8f5b3c546 40 {
lixianyu 0:a4d8f5b3c546 41 //if (P_Serial->available() > 0) {
lixianyu 0:a4d8f5b3c546 42 if (P_Serial->readable() > 0) {
lixianyu 0:a4d8f5b3c546 43 if (_sta) {
lixianyu 0:a4d8f5b3c546 44 this->inCache = this->inChar;
lixianyu 0:a4d8f5b3c546 45 //this->inChar = P_Serial->read();
lixianyu 0:a4d8f5b3c546 46 this->inChar = P_Serial->getc();
lixianyu 0:a4d8f5b3c546 47 this->buffer[num] = this->inChar;
lixianyu 0:a4d8f5b3c546 48
lixianyu 0:a4d8f5b3c546 49 if (this->num > BUFFER_MAX - 1) {
lixianyu 0:a4d8f5b3c546 50 this->num = 0;
lixianyu 0:a4d8f5b3c546 51 return false;
lixianyu 0:a4d8f5b3c546 52 } else {
lixianyu 0:a4d8f5b3c546 53 this->num++;
lixianyu 0:a4d8f5b3c546 54 }
lixianyu 0:a4d8f5b3c546 55 }
lixianyu 0:a4d8f5b3c546 56 return true;
lixianyu 0:a4d8f5b3c546 57 }
lixianyu 0:a4d8f5b3c546 58 return false;
lixianyu 0:a4d8f5b3c546 59 }
lixianyu 0:a4d8f5b3c546 60
lixianyu 0:a4d8f5b3c546 61 uint8_t Protocol::parse(uint16_t* _data, bool _mod)
lixianyu 0:a4d8f5b3c546 62 {
lixianyu 0:a4d8f5b3c546 63 if (available(!_mod)) {
lixianyu 0:a4d8f5b3c546 64 //time = millis();
lixianyu 0:a4d8f5b3c546 65 //time = _timer.read_ms();
lixianyu 0:a4d8f5b3c546 66 time = g_Timer.read_ms();
lixianyu 0:a4d8f5b3c546 67 do {
lixianyu 0:a4d8f5b3c546 68 if (this->sta) {
lixianyu 0:a4d8f5b3c546 69 this->sta = false;
lixianyu 0:a4d8f5b3c546 70 this->num = 0;
lixianyu 0:a4d8f5b3c546 71 if (this->inChar == this->channel) {
lixianyu 0:a4d8f5b3c546 72 this->error = false;
lixianyu 0:a4d8f5b3c546 73 if (!_mod) {
lixianyu 0:a4d8f5b3c546 74 return P_BUSY;
lixianyu 0:a4d8f5b3c546 75 }
lixianyu 0:a4d8f5b3c546 76 } else {
lixianyu 0:a4d8f5b3c546 77 this->error = true;
lixianyu 0:a4d8f5b3c546 78 return P_ERROR;
lixianyu 0:a4d8f5b3c546 79 }
lixianyu 0:a4d8f5b3c546 80 }
lixianyu 0:a4d8f5b3c546 81
lixianyu 0:a4d8f5b3c546 82 if (this->inChar == 0xBB && this->inCache == 0xAA) {
lixianyu 0:a4d8f5b3c546 83 this->sta = true;
lixianyu 0:a4d8f5b3c546 84 if (!_mod) {
lixianyu 0:a4d8f5b3c546 85 return P_BUSY;
lixianyu 0:a4d8f5b3c546 86 }
lixianyu 0:a4d8f5b3c546 87 }
lixianyu 0:a4d8f5b3c546 88
lixianyu 0:a4d8f5b3c546 89 if (this->num == (CHANNEL_NUM * 2 + 1) && !this->error) {
lixianyu 0:a4d8f5b3c546 90 this->inCache = this->buffer[CHANNEL_NUM * 2];
lixianyu 0:a4d8f5b3c546 91 this->buffer[CHANNEL_NUM * 2] = NULL;
lixianyu 0:a4d8f5b3c546 92 this->inChar = getChecksum(CHANNEL_NUM * 2, 200, this->buffer);
lixianyu 0:a4d8f5b3c546 93
lixianyu 0:a4d8f5b3c546 94 this->num = 0;
lixianyu 0:a4d8f5b3c546 95 if (!this->error && this->inCache == this->inChar) {
lixianyu 0:a4d8f5b3c546 96 for (uint8_t a = 0; a < CHANNEL_NUM; a++) {
lixianyu 0:a4d8f5b3c546 97 _data[a] = ((uint16_t)(this->buffer[a * 2])) | ((uint16_t)this->buffer[a * 2 + 1] << 8);
lixianyu 0:a4d8f5b3c546 98 }
lixianyu 0:a4d8f5b3c546 99 return P_FINE;
lixianyu 0:a4d8f5b3c546 100 } else {
lixianyu 0:a4d8f5b3c546 101 return P_ERROR;
lixianyu 0:a4d8f5b3c546 102 }
lixianyu 0:a4d8f5b3c546 103 } else if (!_mod) {
lixianyu 0:a4d8f5b3c546 104 return P_BUSY;
lixianyu 0:a4d8f5b3c546 105 }
lixianyu 0:a4d8f5b3c546 106 } while (_mod && (available(true) && g_Timer.read_ms() - time < 100));
lixianyu 0:a4d8f5b3c546 107
lixianyu 0:a4d8f5b3c546 108 if (_mod) {
lixianyu 0:a4d8f5b3c546 109 return P_TIMEOUT;
lixianyu 0:a4d8f5b3c546 110 }
lixianyu 0:a4d8f5b3c546 111 } else {
lixianyu 0:a4d8f5b3c546 112 return P_NONE;
lixianyu 0:a4d8f5b3c546 113 }
lixianyu 0:a4d8f5b3c546 114 }