vasko ozo
/
TxRxService
20190816
TxRxService.cpp@23:39d96e160cf1, 2019-08-08 (annotated)
- Committer:
- VASKO
- Date:
- Thu Aug 08 13:24:28 2019 +0000
- Revision:
- 23:39d96e160cf1
- Parent:
- 17:d180adbbd61f
- Child:
- 24:fba9a58e6fbd
Rx header detection performed and added. Statistics displayed. Duplex at 115200.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
VASKO | 11:6519744c77f9 | 1 | #include "PrjDefs.h" |
VASKO | 11:6519744c77f9 | 2 | |
VASKO | 17:d180adbbd61f | 3 | Serial pc(WorkTx,WorkRx); |
VASKO | 11:6519744c77f9 | 4 | |
VASKO | 11:6519744c77f9 | 5 | uint8_t ArrTx[ArrTxSize] = ArrTxInitializer; |
VASKO | 11:6519744c77f9 | 6 | uint8_t ArrRx[ArrRxSize] = ArrRxInitializer; |
VASKO | 15:30e45bd5902e | 7 | MsgType RxBuff; |
VASKO | 15:30e45bd5902e | 8 | MsgType TxBuff; |
VASKO | 11:6519744c77f9 | 9 | uint8_t *pArrTx = &ArrTx[0]; |
VASKO | 23:39d96e160cf1 | 10 | uint8_t *pArrRx = &ArrRx[HeaderLength]; |
VASKO | 13:b9e066badefa | 11 | bool RxBuffFull = 0; |
VASKO | 11:6519744c77f9 | 12 | bool ArrTxBusy = 0; |
VASKO | 11:6519744c77f9 | 13 | bool TxBuffFull = 0; |
VASKO | 23:39d96e160cf1 | 14 | bool HdrRcvd = 0; |
VASKO | 23:39d96e160cf1 | 15 | bool MsgEnabled = 0; |
VASKO | 23:39d96e160cf1 | 16 | uint8_t HBctr = 0; |
VASKO | 11:6519744c77f9 | 17 | |
VASKO | 15:30e45bd5902e | 18 | uint8_t GetCheckSum(uint8_t *p){ |
VASKO | 15:30e45bd5902e | 19 | uint8_t cs = 0; |
VASKO | 16:459ad1bd82fd | 20 | for(int i = 0; i < (sizeof(MsgType)-1); i++){ |
VASKO | 15:30e45bd5902e | 21 | cs = cs + *(p++); |
VASKO | 15:30e45bd5902e | 22 | cs = cs + 1; |
VASKO | 15:30e45bd5902e | 23 | } |
VASKO | 15:30e45bd5902e | 24 | return cs; |
VASKO | 15:30e45bd5902e | 25 | } |
VASKO | 15:30e45bd5902e | 26 | |
VASKO | 14:b113f676ef42 | 27 | void IntrTx(); |
VASKO | 14:b113f676ef42 | 28 | |
VASKO | 14:b113f676ef42 | 29 | void CopyArr_InitTx(uint8_t *parr){ |
VASKO | 16:459ad1bd82fd | 30 | memcpy(&ArrTx[HeaderLength], parr, sizeof(TxBuff)); |
VASKO | 14:b113f676ef42 | 31 | pArrTx = &ArrTx[0]; |
VASKO | 14:b113f676ef42 | 32 | pc.putc(*pArrTx); |
VASKO | 14:b113f676ef42 | 33 | if(!ArrTxBusy) { |
VASKO | 14:b113f676ef42 | 34 | ArrTxBusy = 1; |
VASKO | 14:b113f676ef42 | 35 | pc.attach(IntrTx, Serial::TxIrq); |
VASKO | 14:b113f676ef42 | 36 | } |
VASKO | 14:b113f676ef42 | 37 | TxBuffFull = 0; |
VASKO | 14:b113f676ef42 | 38 | } |
VASKO | 14:b113f676ef42 | 39 | |
VASKO | 11:6519744c77f9 | 40 | void IntrTx() { |
VASKO | 11:6519744c77f9 | 41 | if ( ++pArrTx <= &ArrTx[ArrTxSize-1] ) pc.putc(*pArrTx); |
VASKO | 11:6519744c77f9 | 42 | else { |
VASKO | 15:30e45bd5902e | 43 | if(TxBuffFull) CopyArr_InitTx(&TxBuff.cmd); |
VASKO | 14:b113f676ef42 | 44 | else { |
VASKO | 14:b113f676ef42 | 45 | pc.attach(NULL, Serial::TxIrq); |
VASKO | 14:b113f676ef42 | 46 | ArrTxBusy = 0; |
VASKO | 14:b113f676ef42 | 47 | } |
VASKO | 11:6519744c77f9 | 48 | } |
VASKO | 11:6519744c77f9 | 49 | }//IntrTx |
VASKO | 11:6519744c77f9 | 50 | |
VASKO | 14:b113f676ef42 | 51 | StartTxStates StartTx(uint8_t *parr){ |
VASKO | 14:b113f676ef42 | 52 | if(TxBuffFull){ return TxBusy; } |
VASKO | 16:459ad1bd82fd | 53 | *(parr + (sizeof(MsgType)-1)) = GetCheckSum(parr); |
VASKO | 16:459ad1bd82fd | 54 | if(ArrTxBusy) { //ВНИМАНИЕ!!! IntrTx последнего байта пакета!!! НЕАТОМАРНЫЙ КУСОК КОДА!!! |
VASKO | 16:459ad1bd82fd | 55 | memcpy(&TxBuff.cmd, parr, sizeof(TxBuff)); //ВНИМАНИЕ!!! IntrTx последнего байта пакета!!! НЕАТОМАРНЫЙ КУСОК КОДА!!! |
VASKO | 16:459ad1bd82fd | 56 | TxBuffFull=1; //ВНИМАНИЕ!!! IntrTx последнего байта пакета!!! НЕАТОМАРНЫЙ КУСОК КОДА!!! |
VASKO | 16:459ad1bd82fd | 57 | if(!ArrTxBusy) CopyArr_InitTx(&TxBuff.cmd);//ВНИМАНИЕ!!! IntrTx последнего байта пакета!!! НЕАТОМАРНЫЙ КУСОК КОДА!!! |
VASKO | 14:b113f676ef42 | 58 | } else CopyArr_InitTx(parr); |
VASKO | 14:b113f676ef42 | 59 | return StartOK; |
VASKO | 11:6519744c77f9 | 60 | }//StartTx |
VASKO | 11:6519744c77f9 | 61 | |
VASKO | 11:6519744c77f9 | 62 | void IntrRx() { |
VASKO | 23:39d96e160cf1 | 63 | uint8_t _ch = pc.getc(); |
VASKO | 23:39d96e160cf1 | 64 | if(_ch == HeaderByte){ |
VASKO | 23:39d96e160cf1 | 65 | if((++HBctr) >= HeaderLength){ |
VASKO | 23:39d96e160cf1 | 66 | HdrRcvd = 1; |
VASKO | 23:39d96e160cf1 | 67 | pArrRx = &ArrRx[HeaderLength]; |
VASKO | 23:39d96e160cf1 | 68 | MsgEnabled = 0; |
VASKO | 23:39d96e160cf1 | 69 | } |
VASKO | 23:39d96e160cf1 | 70 | }else{ |
VASKO | 23:39d96e160cf1 | 71 | HBctr = 0; |
VASKO | 23:39d96e160cf1 | 72 | if(HdrRcvd) { HdrRcvd = 0; MsgEnabled = 1; } |
VASKO | 23:39d96e160cf1 | 73 | } |
VASKO | 23:39d96e160cf1 | 74 | if(MsgEnabled){ |
VASKO | 23:39d96e160cf1 | 75 | *pArrRx = _ch; |
VASKO | 23:39d96e160cf1 | 76 | if ( pArrRx++ >= &ArrRx[ArrRxSize-1] ){ |
VASKO | 23:39d96e160cf1 | 77 | pArrRx = &ArrRx[HeaderLength]; |
VASKO | 23:39d96e160cf1 | 78 | RxBuffFull = 1; |
VASKO | 23:39d96e160cf1 | 79 | memcpy(&RxBuff.cmd, &ArrRx[HeaderLength], sizeof(RxBuff)); |
VASKO | 23:39d96e160cf1 | 80 | MsgEnabled = 0; |
VASKO | 23:39d96e160cf1 | 81 | } |
VASKO | 23:39d96e160cf1 | 82 | }//if(MsgEnabled) |
VASKO | 11:6519744c77f9 | 83 | }//IntrRx |
VASKO | 11:6519744c77f9 | 84 | |
VASKO | 11:6519744c77f9 | 85 | void TxRxServiceInit(){ |
VASKO | 23:39d96e160cf1 | 86 | #ifdef BaudRate |
VASKO | 23:39d96e160cf1 | 87 | pc.baud(BaudRate); |
VASKO | 23:39d96e160cf1 | 88 | #endif |
VASKO | 14:b113f676ef42 | 89 | pc.attach(IntrRx, Serial::RxIrq); |
VASKO | 11:6519744c77f9 | 90 | pc.attach(NULL, Serial::TxIrq);//я УПЁРТЫЙ дебил |
VASKO | 16:459ad1bd82fd | 91 | } |