This library assign registar and send 4bytes data in serial. Auto attachment interruption.

Dependents:   WRS_mechanamu_test

Committer:
sgrsn
Date:
Fri Jun 29 12:50:08 2018 +0000
Revision:
2:ea92ff9abc8c
Parent:
1:ee6809d3cee3
change baudrate

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sgrsn 0:56fce4868747 1 #include "PacketSerial.h"
sgrsn 0:56fce4868747 2
sgrsn 1:ee6809d3cee3 3 /*sample code coudinator**********************************
sgrsn 1:ee6809d3cee3 4 #include "mbed.h"
sgrsn 1:ee6809d3cee3 5 #include "PacketSerial.h"
sgrsn 1:ee6809d3cee3 6
sgrsn 1:ee6809d3cee3 7 int32_t Registar[128] = {};
sgrsn 1:ee6809d3cee3 8 PacketSerial device(p13, p14, Registar);
sgrsn 1:ee6809d3cee3 9
sgrsn 1:ee6809d3cee3 10 int main()
sgrsn 1:ee6809d3cee3 11 {
sgrsn 1:ee6809d3cee3 12 int32_t i = 0;
sgrsn 1:ee6809d3cee3 13 while(1)
sgrsn 1:ee6809d3cee3 14 {
sgrsn 1:ee6809d3cee3 15 i++;
sgrsn 1:ee6809d3cee3 16 device.writeData(i, 0x10);
sgrsn 1:ee6809d3cee3 17 //wait_ms(100);
sgrsn 1:ee6809d3cee3 18 }
sgrsn 1:ee6809d3cee3 19 }
sgrsn 1:ee6809d3cee3 20 *********************************************************/
sgrsn 1:ee6809d3cee3 21
sgrsn 1:ee6809d3cee3 22 /*sample code router**************************************
sgrsn 1:ee6809d3cee3 23 #include "mbed.h"
sgrsn 1:ee6809d3cee3 24 #include "PacketSerial.h"
sgrsn 1:ee6809d3cee3 25
sgrsn 1:ee6809d3cee3 26 int32_t Registar[128] = {};
sgrsn 1:ee6809d3cee3 27 PacketSerial device(p13, p14, Registar);
sgrsn 1:ee6809d3cee3 28
sgrsn 1:ee6809d3cee3 29 int main()
sgrsn 1:ee6809d3cee3 30 {
sgrsn 1:ee6809d3cee3 31 while(1)
sgrsn 1:ee6809d3cee3 32 {
sgrsn 1:ee6809d3cee3 33 printf("%d\r\n", Registar[0x10]);
sgrsn 1:ee6809d3cee3 34 }
sgrsn 1:ee6809d3cee3 35 }
sgrsn 1:ee6809d3cee3 36 *********************************************************/
sgrsn 1:ee6809d3cee3 37
sgrsn 1:ee6809d3cee3 38
sgrsn 2:ea92ff9abc8c 39 PacketSerial::PacketSerial(PinName tx, PinName rx, int32_t* registar, int baudrate) : Serial(tx, rx)
sgrsn 0:56fce4868747 40 {
sgrsn 0:56fce4868747 41 _registar = registar;
sgrsn 2:ea92ff9abc8c 42 Serial::baud(baudrate);
sgrsn 0:56fce4868747 43 Serial::attach(this, &PacketSerial::readData, Serial::RxIrq);
sgrsn 0:56fce4868747 44 }
sgrsn 0:56fce4868747 45
sgrsn 0:56fce4868747 46 void PacketSerial::writeData(int32_t data, uint8_t reg)
sgrsn 0:56fce4868747 47 {
sgrsn 0:56fce4868747 48 uint8_t dataBytes[4] =
sgrsn 0:56fce4868747 49 {
sgrsn 0:56fce4868747 50 (data >> 24) & 0xFF,
sgrsn 0:56fce4868747 51 (data >> 16) & 0xFF,
sgrsn 0:56fce4868747 52 (data >> 8) & 0xFF,
sgrsn 0:56fce4868747 53 (data >> 0) & 0xFF
sgrsn 0:56fce4868747 54 };
sgrsn 0:56fce4868747 55
sgrsn 0:56fce4868747 56 Serial::putc(HEAD_BYTE);
sgrsn 0:56fce4868747 57 Serial::putc(reg);
sgrsn 0:56fce4868747 58 uint8_t checksum = 0;
sgrsn 0:56fce4868747 59 for (uint8_t i = 0; i < 4; ++i)
sgrsn 0:56fce4868747 60 {
sgrsn 0:56fce4868747 61 if ((dataBytes[i] == ESCAPE_BYTE) || (dataBytes[i] == HEAD_BYTE))
sgrsn 0:56fce4868747 62 {
sgrsn 0:56fce4868747 63 Serial::putc(ESCAPE_BYTE);
sgrsn 0:56fce4868747 64 checksum += ESCAPE_BYTE;
sgrsn 0:56fce4868747 65 Serial::putc(dataBytes[i] ^ ESCAPE_MASK);
sgrsn 0:56fce4868747 66 checksum += dataBytes[i] ^ ESCAPE_MASK;
sgrsn 0:56fce4868747 67 }
sgrsn 0:56fce4868747 68 else
sgrsn 0:56fce4868747 69 {
sgrsn 0:56fce4868747 70 Serial::putc(dataBytes[i]);
sgrsn 0:56fce4868747 71 checksum += dataBytes[i];
sgrsn 0:56fce4868747 72 }
sgrsn 0:56fce4868747 73 }
sgrsn 0:56fce4868747 74
sgrsn 0:56fce4868747 75 // 末尾にチェックサムを追加で送信する
sgrsn 0:56fce4868747 76 Serial::putc(checksum);
sgrsn 0:56fce4868747 77 }
sgrsn 0:56fce4868747 78
sgrsn 0:56fce4868747 79 void PacketSerial::writeFloatData(float data, uint8_t reg)
sgrsn 0:56fce4868747 80 {
sgrsn 0:56fce4868747 81 int integer = (int)data;
sgrsn 0:56fce4868747 82 uint32_t decimal = (uint32_t)((data-integer)*1000000);
sgrsn 0:56fce4868747 83 writeData(integer, reg);
sgrsn 0:56fce4868747 84 writeData(decimal, reg+1);
sgrsn 0:56fce4868747 85 }
sgrsn 0:56fce4868747 86
sgrsn 0:56fce4868747 87 float PacketSerial::getFloatData(uint8_t reg)
sgrsn 0:56fce4868747 88 {
sgrsn 0:56fce4868747 89 return *(_registar + reg) + ((float)*(_registar + reg+1))/1000000;
sgrsn 0:56fce4868747 90 }
sgrsn 0:56fce4868747 91
sgrsn 0:56fce4868747 92 void PacketSerial::readData()
sgrsn 0:56fce4868747 93 {
sgrsn 0:56fce4868747 94 uint8_t bytes[4] = {0,0,0,0};
sgrsn 0:56fce4868747 95 int8_t checksum = 0;
sgrsn 0:56fce4868747 96 wait_us(500);
sgrsn 0:56fce4868747 97 uint8_t data = Serial::getc();
sgrsn 0:56fce4868747 98 wait_us(500);
sgrsn 0:56fce4868747 99
sgrsn 0:56fce4868747 100 if (data == HEAD_BYTE)
sgrsn 0:56fce4868747 101 {
sgrsn 0:56fce4868747 102 uint8_t reg = Serial::getc();
sgrsn 0:56fce4868747 103 for (int i = 0; i < 4; ++i)
sgrsn 0:56fce4868747 104 {
sgrsn 0:56fce4868747 105 uint8_t d = Serial::getc();
sgrsn 0:56fce4868747 106 wait_us(500);
sgrsn 0:56fce4868747 107 if (d == ESCAPE_BYTE)
sgrsn 0:56fce4868747 108 {
sgrsn 0:56fce4868747 109 uint8_t nextByte = Serial::getc();
sgrsn 0:56fce4868747 110 bytes[i] = nextByte ^ ESCAPE_MASK;
sgrsn 0:56fce4868747 111 checksum += (d + nextByte);
sgrsn 0:56fce4868747 112 }
sgrsn 0:56fce4868747 113 else
sgrsn 0:56fce4868747 114 {
sgrsn 0:56fce4868747 115 bytes[i] = d;
sgrsn 0:56fce4868747 116 checksum += d;
sgrsn 0:56fce4868747 117 }
sgrsn 0:56fce4868747 118 }
sgrsn 0:56fce4868747 119 int8_t checksum_recv = Serial::getc();
sgrsn 0:56fce4868747 120 wait_us(500);
sgrsn 0:56fce4868747 121 int32_t DATA = 0x00;
sgrsn 0:56fce4868747 122 for(int i = 0; i < 4; i++)
sgrsn 0:56fce4868747 123 {
sgrsn 0:56fce4868747 124 DATA |= (((int32_t)bytes[i]) << (24 - (i*8)));
sgrsn 0:56fce4868747 125 }
sgrsn 0:56fce4868747 126
sgrsn 0:56fce4868747 127 if (checksum == checksum_recv)
sgrsn 0:56fce4868747 128 {
sgrsn 0:56fce4868747 129 *(_registar + reg) = DATA;
sgrsn 0:56fce4868747 130 }
sgrsn 0:56fce4868747 131 else
sgrsn 0:56fce4868747 132 {
sgrsn 0:56fce4868747 133 // data error
sgrsn 0:56fce4868747 134 }
sgrsn 0:56fce4868747 135 }
sgrsn 0:56fce4868747 136 }