vasko ozo
/
TxRxService
20190816
TxRxService.cpp@28:55cd0598543b, 2019-08-11 (annotated)
- Committer:
- VASKO
- Date:
- Sun Aug 11 18:31:23 2019 +0000
- Revision:
- 28:55cd0598543b
- Parent:
- 27:21b821ead49c
"Wcom" instead of "pc".
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 | 28:55cd0598543b | 3 | UsedSerialClass Wcom(WorkTx,WorkRx); |
VASKO | 11:6519744c77f9 | 4 | |
VASKO | 11:6519744c77f9 | 5 | uint8_t ArrTx[ArrTxSize] = ArrTxInitializer; |
VASKO | 24:fba9a58e6fbd | 6 | MsgType ArrRx; |
VASKO | 15:30e45bd5902e | 7 | MsgType RxBuff; |
VASKO | 15:30e45bd5902e | 8 | MsgType TxBuff; |
VASKO | 11:6519744c77f9 | 9 | uint8_t *pArrTx = &ArrTx[0]; |
VASKO | 24:fba9a58e6fbd | 10 | uint8_t *pArrRx = &ArrRx.cmd; |
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 | 28:55cd0598543b | 32 | Wcom.putc(*pArrTx); |
VASKO | 14:b113f676ef42 | 33 | if(!ArrTxBusy) { |
VASKO | 14:b113f676ef42 | 34 | ArrTxBusy = 1; |
VASKO | 28:55cd0598543b | 35 | Wcom.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 | 28:55cd0598543b | 41 | if ( ++pArrTx <= &ArrTx[ArrTxSize-1] ) Wcom.putc(*pArrTx); |
VASKO | 11:6519744c77f9 | 42 | else { |
VASKO | 15:30e45bd5902e | 43 | if(TxBuffFull) CopyArr_InitTx(&TxBuff.cmd); |
VASKO | 14:b113f676ef42 | 44 | else { |
VASKO | 28:55cd0598543b | 45 | Wcom.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 | 24:fba9a58e6fbd | 51 | TxRxStates SendMsg(uint8_t *parr){ |
VASKO | 24:fba9a58e6fbd | 52 | if(TxBuffFull){ return TxBuffBusy; } |
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 | 24:fba9a58e6fbd | 59 | return TxOK; |
VASKO | 11:6519744c77f9 | 60 | }//StartTx |
VASKO | 11:6519744c77f9 | 61 | |
VASKO | 11:6519744c77f9 | 62 | void IntrRx() { |
VASKO | 28:55cd0598543b | 63 | uint8_t _ch = Wcom.getc(); |
VASKO | 23:39d96e160cf1 | 64 | if(_ch == HeaderByte){ |
VASKO | 23:39d96e160cf1 | 65 | if((++HBctr) >= HeaderLength){ |
VASKO | 23:39d96e160cf1 | 66 | HdrRcvd = 1; |
VASKO | 24:fba9a58e6fbd | 67 | pArrRx = &ArrRx.cmd; |
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 | 24:fba9a58e6fbd | 76 | if ( pArrRx++ >= &ArrRx.cs ){ |
VASKO | 24:fba9a58e6fbd | 77 | pArrRx = &ArrRx.cmd; |
VASKO | 24:fba9a58e6fbd | 78 | memcpy(&RxBuff.cmd, &ArrRx.cmd, sizeof(RxBuff)); |
VASKO | 23:39d96e160cf1 | 79 | RxBuffFull = 1; |
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 | 28:55cd0598543b | 87 | Wcom.baud(BaudRate); |
VASKO | 23:39d96e160cf1 | 88 | #endif |
VASKO | 28:55cd0598543b | 89 | Wcom.attach(IntrRx, Serial::RxIrq); |
VASKO | 28:55cd0598543b | 90 | Wcom.attach(NULL, Serial::TxIrq);//я УПЁРТЫЙ дебил |
VASKO | 24:fba9a58e6fbd | 91 | } |
VASKO | 24:fba9a58e6fbd | 92 | |
VASKO | 24:fba9a58e6fbd | 93 | TxRxStates GetMsg(uint8_t *parr){ |
VASKO | 24:fba9a58e6fbd | 94 | if(!RxBuffFull) return RxBuffEmpty; |
VASKO | 24:fba9a58e6fbd | 95 | else{ |
VASKO | 24:fba9a58e6fbd | 96 | memcpy(parr, &RxBuff.cmd, sizeof(RxBuff)); |
VASKO | 24:fba9a58e6fbd | 97 | RxBuffFull = 0; |
VASKO | 24:fba9a58e6fbd | 98 | if(GetCheckSum(&RxBuff.cmd) == RxBuff.cs) return RxRcvd; |
VASKO | 24:fba9a58e6fbd | 99 | else return RxCS_Err; |
VASKO | 24:fba9a58e6fbd | 100 | } |
VASKO | 24:fba9a58e6fbd | 101 | }//GetMsg |