モータードライバとWi-FiモジュールESP-WROOM-02をmbed LPC1114FN28に繋げて、RCWControllerからコントロールするプログラム

Dependencies:   mbed

Committer:
jksoft
Date:
Fri Jul 22 05:36:02 2016 +0000
Revision:
0:3c24a40c2343
??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:3c24a40c2343 1 #include "SoftSerialSendOnry.h"
jksoft 0:3c24a40c2343 2
jksoft 0:3c24a40c2343 3 int SoftSerialSendOnry::_putc(int c)
jksoft 0:3c24a40c2343 4 {
jksoft 0:3c24a40c2343 5 while(!writeable());
jksoft 0:3c24a40c2343 6 prepare_tx(c);
jksoft 0:3c24a40c2343 7 tx_bit = 0;
jksoft 0:3c24a40c2343 8 txticker.prime();
jksoft 0:3c24a40c2343 9 tx_handler();
jksoft 0:3c24a40c2343 10 return 0;
jksoft 0:3c24a40c2343 11 }
jksoft 0:3c24a40c2343 12
jksoft 0:3c24a40c2343 13 void SoftSerialSendOnry::send_break(void) {
jksoft 0:3c24a40c2343 14 while(!writeable());
jksoft 0:3c24a40c2343 15 tx_bit = 0; //Just to make sure it appears as non-writable to other threads/IRQs
jksoft 0:3c24a40c2343 16 tx->write(0);
jksoft 0:3c24a40c2343 17 wait_us((bit_period * _total_bits * 3) / 2);
jksoft 0:3c24a40c2343 18 tx->write(1);
jksoft 0:3c24a40c2343 19 tx_bit = -1;
jksoft 0:3c24a40c2343 20 }
jksoft 0:3c24a40c2343 21
jksoft 0:3c24a40c2343 22 int SoftSerialSendOnry::writeable(void)
jksoft 0:3c24a40c2343 23 {
jksoft 0:3c24a40c2343 24 if (!tx_en)
jksoft 0:3c24a40c2343 25 return false;
jksoft 0:3c24a40c2343 26 if (tx_bit == -1)
jksoft 0:3c24a40c2343 27 return true;
jksoft 0:3c24a40c2343 28 return false;
jksoft 0:3c24a40c2343 29 }
jksoft 0:3c24a40c2343 30
jksoft 0:3c24a40c2343 31 void SoftSerialSendOnry::tx_handler(void)
jksoft 0:3c24a40c2343 32 {
jksoft 0:3c24a40c2343 33 if (tx_bit == _total_bits) {
jksoft 0:3c24a40c2343 34 tx_bit = -1;
jksoft 0:3c24a40c2343 35 fpointer[TxIrq].call();
jksoft 0:3c24a40c2343 36 return;
jksoft 0:3c24a40c2343 37 }
jksoft 0:3c24a40c2343 38
jksoft 0:3c24a40c2343 39 //Flip output
jksoft 0:3c24a40c2343 40 int cur_out = tx->read();
jksoft 0:3c24a40c2343 41 tx->write(!cur_out);
jksoft 0:3c24a40c2343 42
jksoft 0:3c24a40c2343 43 //Calculate when to do it again
jksoft 0:3c24a40c2343 44 int count = bit_period;
jksoft 0:3c24a40c2343 45 tx_bit++;
jksoft 0:3c24a40c2343 46 while(((_char >> tx_bit) & 0x01) == !cur_out) {
jksoft 0:3c24a40c2343 47 count+=bit_period;
jksoft 0:3c24a40c2343 48 tx_bit++;
jksoft 0:3c24a40c2343 49 }
jksoft 0:3c24a40c2343 50
jksoft 0:3c24a40c2343 51 txticker.setNext(count);
jksoft 0:3c24a40c2343 52 }
jksoft 0:3c24a40c2343 53
jksoft 0:3c24a40c2343 54 void SoftSerialSendOnry::prepare_tx(int c)
jksoft 0:3c24a40c2343 55 {
jksoft 0:3c24a40c2343 56 _char = c << 1;
jksoft 0:3c24a40c2343 57
jksoft 0:3c24a40c2343 58 bool parity;
jksoft 0:3c24a40c2343 59 switch (_parity) {
jksoft 0:3c24a40c2343 60 case Forced1:
jksoft 0:3c24a40c2343 61 _char |= 1 << (_bits + 1);
jksoft 0:3c24a40c2343 62 case Even:
jksoft 0:3c24a40c2343 63 parity = false;
jksoft 0:3c24a40c2343 64 for (int i = 0; i<_bits; i++) {
jksoft 0:3c24a40c2343 65 if (((_char >> i) & 0x01) == 1)
jksoft 0:3c24a40c2343 66 parity = !parity;
jksoft 0:3c24a40c2343 67 }
jksoft 0:3c24a40c2343 68 _char |= parity << (_bits + 1);
jksoft 0:3c24a40c2343 69 case Odd:
jksoft 0:3c24a40c2343 70 parity = true;
jksoft 0:3c24a40c2343 71 for (int i = 0; i<_bits; i++) {
jksoft 0:3c24a40c2343 72 if (((_char >> i) & 0x01) == 1)
jksoft 0:3c24a40c2343 73 parity = !parity;
jksoft 0:3c24a40c2343 74 }
jksoft 0:3c24a40c2343 75 _char |= parity << (_bits + 1);
jksoft 0:3c24a40c2343 76 }
jksoft 0:3c24a40c2343 77
jksoft 0:3c24a40c2343 78 _char |= 0xFFFF << (1 + _bits + (bool)_parity);
jksoft 0:3c24a40c2343 79 _char &= ~(1<<_total_bits);
jksoft 0:3c24a40c2343 80 }