Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@1:fcdfb51a34c8, 2021-04-04 (annotated)
- Committer:
- mohamedwajdi
- Date:
- Sun Apr 04 19:59:00 2021 +0000
- Revision:
- 1:fcdfb51a34c8
- Parent:
- 0:ecb5de3a3147
- Child:
- 2:c4ac4dc5673f
TP3 RBT 2GII Ex2
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mohamedwajdi | 1:fcdfb51a34c8 | 1 | // Table des variables, des bits et des Registres d’E/S |
| mohamedwajdi | 1:fcdfb51a34c8 | 2 | //_______________________________________________________________________________ |
| mohamedwajdi | 1:fcdfb51a34c8 | 3 | // Ports et variables d’E/S | Bits et Reg. d’E/S | Ecriture Modbus | Adresse |
| mohamedwajdi | 1:fcdfb51a34c8 | 4 | // ------------------------------------------------------------------------------ |
| mohamedwajdi | 1:fcdfb51a34c8 | 5 | // Led2 : PB_8 | bit 0 de BitsOut[0] | oui (fonction 5) | 0x1000 |
| mohamedwajdi | 1:fcdfb51a34c8 | 6 | // BP1 : PA_8 | bit 0 de BitsIn[0] | non | 0x2000 |
| mohamedwajdi | 1:fcdfb51a34c8 | 7 | // BP2 : PB_12 | bit 1 de BitsIn[0] | non | 0x2001 |
| mohamedwajdi | 1:fcdfb51a34c8 | 8 | // Rap_Cyc * 100 (pour Moteur) | RegOut[0] | oui (fonction 6) | 0x3000 |
| mohamedwajdi | 1:fcdfb51a34c8 | 9 | // Pot.read() * 100 (pour Led1)| RegOut[1] | non | 0x3001 |
| mohamedwajdi | 1:fcdfb51a34c8 | 10 | // Pot.read_u16 () (pour Pot) | RegIn[0] | non | 0x4000 |
| mohamedwajdi | 1:fcdfb51a34c8 | 11 | //------------------------------------------------------------------------------- |
| mohamedwajdi | 0:ecb5de3a3147 | 12 | #include "mbed.h" |
| mohamedwajdi | 0:ecb5de3a3147 | 13 | #include "mb.h" |
| mohamedwajdi | 0:ecb5de3a3147 | 14 | |
| mohamedwajdi | 0:ecb5de3a3147 | 15 | #define BITS_OUT_START 0x1001 //Bits de sorties (fonctions 1 et 5) |
| mohamedwajdi | 0:ecb5de3a3147 | 16 | #define BITS_OUT_NBITS 16 |
| mohamedwajdi | 0:ecb5de3a3147 | 17 | #define BITS_OUT_BYTES BITS_OUT_NBITS/8 //16/8 = 2 octets |
| mohamedwajdi | 0:ecb5de3a3147 | 18 | |
| mohamedwajdi | 0:ecb5de3a3147 | 19 | #define BITS_IN_START 0x2001 //Bits d'entées (fonction 2) |
| mohamedwajdi | 0:ecb5de3a3147 | 20 | #define BITS_IN_NBITS 16 |
| mohamedwajdi | 0:ecb5de3a3147 | 21 | #define BITS_IN_BYTES BITS_IN_NBITS/8 //16/8 = 2 octets |
| mohamedwajdi | 0:ecb5de3a3147 | 22 | |
| mohamedwajdi | 0:ecb5de3a3147 | 23 | #define REG_OUT_START 0x3001 //Registres de sorties (fonctions 3 et 6) |
| mohamedwajdi | 0:ecb5de3a3147 | 24 | #define REG_OUT_NREGS 4 |
| mohamedwajdi | 0:ecb5de3a3147 | 25 | |
| mohamedwajdi | 0:ecb5de3a3147 | 26 | #define REG_IN_START 0x4001 //Registres d'entées (fonction 4) |
| mohamedwajdi | 0:ecb5de3a3147 | 27 | #define REG_IN_NREGS 4 |
| mohamedwajdi | 0:ecb5de3a3147 | 28 | |
| mohamedwajdi | 0:ecb5de3a3147 | 29 | #define SLAVE_ID 0x07 |
| mohamedwajdi | 0:ecb5de3a3147 | 30 | |
| mohamedwajdi | 0:ecb5de3a3147 | 31 | static USHORT BitsOutStart = BITS_OUT_START; // (fonctions 1 et 5) |
| mohamedwajdi | 0:ecb5de3a3147 | 32 | static UCHAR BitsOut[BITS_OUT_BYTES] = {0xA5,0x5A}; |
| mohamedwajdi | 0:ecb5de3a3147 | 33 | static USHORT BitsInStart = BITS_IN_START; // (fonction 2) |
| mohamedwajdi | 0:ecb5de3a3147 | 34 | static UCHAR BitsIn[BITS_IN_BYTES] = {0x3C,0xC3}; |
| mohamedwajdi | 0:ecb5de3a3147 | 35 | static USHORT RegOutStart = REG_OUT_START; // (fonctions 3 et 6) |
| mohamedwajdi | 0:ecb5de3a3147 | 36 | static USHORT RegOut[REG_OUT_NREGS] = {0x0123,0x4567,0x89AB,0xCDEF}; |
| mohamedwajdi | 0:ecb5de3a3147 | 37 | static USHORT RegInStart = REG_IN_START; // (fonction 4) |
| mohamedwajdi | 0:ecb5de3a3147 | 38 | static USHORT RegIn[REG_IN_NREGS] = {0x1122,0x3344,0x5566,0x7788}; |
| mohamedwajdi | 0:ecb5de3a3147 | 39 | |
| mohamedwajdi | 0:ecb5de3a3147 | 40 | int main() { |
| mohamedwajdi | 0:ecb5de3a3147 | 41 | eMBInit( MB_RTU, SLAVE_ID, 0, 9600, MB_PAR_NONE ); |
| mohamedwajdi | 0:ecb5de3a3147 | 42 | eMBEnable(); |
| mohamedwajdi | 0:ecb5de3a3147 | 43 | |
| mohamedwajdi | 0:ecb5de3a3147 | 44 | PwmOut moteur(PB_11); |
| mohamedwajdi | 1:fcdfb51a34c8 | 45 | PwmOut Led1(..........); |
| mohamedwajdi | 0:ecb5de3a3147 | 46 | AnalogIn Pot(PA_0); |
| mohamedwajdi | 0:ecb5de3a3147 | 47 | DigitalIn BP1(PA_8,PullUp); |
| mohamedwajdi | 1:fcdfb51a34c8 | 48 | DigitalIn BP2(..................); |
| mohamedwajdi | 0:ecb5de3a3147 | 49 | DigitalOut Led2(PB_8); |
| mohamedwajdi | 1:fcdfb51a34c8 | 50 | moteur.period_ms(.......); |
| mohamedwajdi | 1:fcdfb51a34c8 | 51 | RegOut[0]=20; //RegOut[0] = rapport cyclique du moteur * 100 = 20% |
| mohamedwajdi | 1:fcdfb51a34c8 | 52 | while(1) { |
| mohamedwajdi | 0:ecb5de3a3147 | 53 | //**** 1 ****// |
| mohamedwajdi | 0:ecb5de3a3147 | 54 | eMBPoll(); |
| mohamedwajdi | 0:ecb5de3a3147 | 55 | //**** 2 ****// |
| mohamedwajdi | 1:fcdfb51a34c8 | 56 | if(BitsOut[0] & 0x01) Led2=1; else Led2=0; |
| mohamedwajdi | 1:fcdfb51a34c8 | 57 | moteur = RegOut[0]/100.0; |
| mohamedwajdi | 0:ecb5de3a3147 | 58 | //**** 3 ****// |
| mohamedwajdi | 1:fcdfb51a34c8 | 59 | if(!BP1 && RegOut[0] <100) { |
| mohamedwajdi | 1:fcdfb51a34c8 | 60 | RegOut[0] = RegOut[0] + 10; |
| mohamedwajdi | 1:fcdfb51a34c8 | 61 | moteur = RegOut[0]/.........; |
| mohamedwajdi | 0:ecb5de3a3147 | 62 | wait(0.5); |
| mohamedwajdi | 0:ecb5de3a3147 | 63 | } |
| mohamedwajdi | 1:fcdfb51a34c8 | 64 | if(!BP2 && RegOut[0] > ....){ |
| mohamedwajdi | 1:fcdfb51a34c8 | 65 | RegOut[0] = RegOut[0] - ....; |
| mohamedwajdi | 1:fcdfb51a34c8 | 66 | moteur = RegOut[0]/.........; |
| mohamedwajdi | 1:fcdfb51a34c8 | 67 | wait(......); |
| mohamedwajdi | 0:ecb5de3a3147 | 68 | } |
| mohamedwajdi | 1:fcdfb51a34c8 | 69 | Led1 = Pot.read(); |
| mohamedwajdi | 0:ecb5de3a3147 | 70 | //**** 4 ****// |
| mohamedwajdi | 0:ecb5de3a3147 | 71 | RegOut[1] = Pot.read() * .......; |
| mohamedwajdi | 0:ecb5de3a3147 | 72 | //**** 5 ****// |
| mohamedwajdi | 1:fcdfb51a34c8 | 73 | if(Led2) BitsOut[0] |=.....; |
| mohamedwajdi | 1:fcdfb51a34c8 | 74 | else BitsOut[0] &=.....; |
| mohamedwajdi | 1:fcdfb51a34c8 | 75 | if(BP1) BitsIn[0] |=.....; |
| mohamedwajdi | 1:fcdfb51a34c8 | 76 | else BitsIn[0] &=.....; |
| mohamedwajdi | 1:fcdfb51a34c8 | 77 | if(BP2) BitsIn[0] |=.....; |
| mohamedwajdi | 1:fcdfb51a34c8 | 78 | else BitsIn[0] &=.....; |
| mohamedwajdi | 1:fcdfb51a34c8 | 79 | RegIn[0] = Pot.read_u16(); |
| mohamedwajdi | 0:ecb5de3a3147 | 80 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 81 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 82 | |
| mohamedwajdi | 0:ecb5de3a3147 | 83 | |
| mohamedwajdi | 0:ecb5de3a3147 | 84 | |
| mohamedwajdi | 0:ecb5de3a3147 | 85 | |
| mohamedwajdi | 0:ecb5de3a3147 | 86 | |
| mohamedwajdi | 0:ecb5de3a3147 | 87 | |
| mohamedwajdi | 0:ecb5de3a3147 | 88 | |
| mohamedwajdi | 0:ecb5de3a3147 | 89 | |
| mohamedwajdi | 0:ecb5de3a3147 | 90 | |
| mohamedwajdi | 0:ecb5de3a3147 | 91 | |
| mohamedwajdi | 0:ecb5de3a3147 | 92 | |
| mohamedwajdi | 0:ecb5de3a3147 | 93 | |
| mohamedwajdi | 0:ecb5de3a3147 | 94 | |
| mohamedwajdi | 0:ecb5de3a3147 | 95 | |
| mohamedwajdi | 0:ecb5de3a3147 | 96 | |
| mohamedwajdi | 0:ecb5de3a3147 | 97 | |
| mohamedwajdi | 0:ecb5de3a3147 | 98 | |
| mohamedwajdi | 0:ecb5de3a3147 | 99 | |
| mohamedwajdi | 0:ecb5de3a3147 | 100 | |
| mohamedwajdi | 0:ecb5de3a3147 | 101 | |
| mohamedwajdi | 0:ecb5de3a3147 | 102 | |
| mohamedwajdi | 0:ecb5de3a3147 | 103 | |
| mohamedwajdi | 0:ecb5de3a3147 | 104 | |
| mohamedwajdi | 0:ecb5de3a3147 | 105 | |
| mohamedwajdi | 0:ecb5de3a3147 | 106 | |
| mohamedwajdi | 0:ecb5de3a3147 | 107 | |
| mohamedwajdi | 0:ecb5de3a3147 | 108 | |
| mohamedwajdi | 0:ecb5de3a3147 | 109 | |
| mohamedwajdi | 0:ecb5de3a3147 | 110 | |
| mohamedwajdi | 0:ecb5de3a3147 | 111 | |
| mohamedwajdi | 0:ecb5de3a3147 | 112 | |
| mohamedwajdi | 0:ecb5de3a3147 | 113 | |
| mohamedwajdi | 0:ecb5de3a3147 | 114 | |
| mohamedwajdi | 0:ecb5de3a3147 | 115 | |
| mohamedwajdi | 0:ecb5de3a3147 | 116 | // Procedure résevée pour les bits de sorties (Foction 1 et 5) |
| mohamedwajdi | 0:ecb5de3a3147 | 117 | // pucRegBuffer => coils buffer usAddress => coils address |
| mohamedwajdi | 0:ecb5de3a3147 | 118 | // usNCoils => coils number eMode => read or write |
| mohamedwajdi | 0:ecb5de3a3147 | 119 | eMBErrorCode |
| mohamedwajdi | 0:ecb5de3a3147 | 120 | eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode ) |
| mohamedwajdi | 0:ecb5de3a3147 | 121 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 122 | eMBErrorCode eStatus = MB_ENOERR; |
| mohamedwajdi | 0:ecb5de3a3147 | 123 | int iIntRegIndex; |
| mohamedwajdi | 0:ecb5de3a3147 | 124 | int iIntBufNum; |
| mohamedwajdi | 0:ecb5de3a3147 | 125 | int iIntBitNum; |
| mohamedwajdi | 0:ecb5de3a3147 | 126 | int iExtRegIndex=0; |
| mohamedwajdi | 0:ecb5de3a3147 | 127 | int iExtBufNum; |
| mohamedwajdi | 0:ecb5de3a3147 | 128 | int iExtBitNum; |
| mohamedwajdi | 0:ecb5de3a3147 | 129 | UCHAR ucTemp; |
| mohamedwajdi | 0:ecb5de3a3147 | 130 | if( ( usAddress >= BITS_OUT_START ) |
| mohamedwajdi | 0:ecb5de3a3147 | 131 | && ( usAddress + usNCoils <= BITS_OUT_START + BITS_OUT_NBITS ) ) |
| mohamedwajdi | 0:ecb5de3a3147 | 132 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 133 | iIntRegIndex = ( int )( usAddress - BitsOutStart ); |
| mohamedwajdi | 0:ecb5de3a3147 | 134 | |
| mohamedwajdi | 0:ecb5de3a3147 | 135 | while( usNCoils > 0 ) |
| mohamedwajdi | 0:ecb5de3a3147 | 136 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 137 | iIntBufNum=iIntRegIndex/8; |
| mohamedwajdi | 0:ecb5de3a3147 | 138 | iIntBitNum=iIntRegIndex%8; |
| mohamedwajdi | 0:ecb5de3a3147 | 139 | iExtBufNum=iExtRegIndex/8; |
| mohamedwajdi | 0:ecb5de3a3147 | 140 | iExtBitNum=iExtRegIndex%8; |
| mohamedwajdi | 0:ecb5de3a3147 | 141 | |
| mohamedwajdi | 0:ecb5de3a3147 | 142 | switch ( eMode ) |
| mohamedwajdi | 0:ecb5de3a3147 | 143 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 144 | case MB_REG_READ: |
| mohamedwajdi | 0:ecb5de3a3147 | 145 | // Read coils |
| mohamedwajdi | 0:ecb5de3a3147 | 146 | if(iExtBitNum==0){ |
| mohamedwajdi | 0:ecb5de3a3147 | 147 | pucRegBuffer[iExtBufNum]=0; |
| mohamedwajdi | 0:ecb5de3a3147 | 148 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 149 | ucTemp=(BitsOut[iIntBufNum]>>iIntBitNum) & 1; |
| mohamedwajdi | 0:ecb5de3a3147 | 150 | pucRegBuffer[iExtBufNum]|=ucTemp<<iExtBitNum; |
| mohamedwajdi | 0:ecb5de3a3147 | 151 | break; |
| mohamedwajdi | 0:ecb5de3a3147 | 152 | |
| mohamedwajdi | 0:ecb5de3a3147 | 153 | case MB_REG_WRITE: |
| mohamedwajdi | 0:ecb5de3a3147 | 154 | // Write coils |
| mohamedwajdi | 0:ecb5de3a3147 | 155 | ucTemp=BitsOut[iIntBufNum]&(~(1<<iIntBitNum)); |
| mohamedwajdi | 0:ecb5de3a3147 | 156 | ucTemp|=((pucRegBuffer[iExtBufNum]>>iExtBitNum) & 1)<<iIntBitNum; |
| mohamedwajdi | 0:ecb5de3a3147 | 157 | BitsOut[iIntBufNum]=ucTemp; |
| mohamedwajdi | 0:ecb5de3a3147 | 158 | break; |
| mohamedwajdi | 0:ecb5de3a3147 | 159 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 160 | iIntRegIndex++; |
| mohamedwajdi | 0:ecb5de3a3147 | 161 | iExtRegIndex++; |
| mohamedwajdi | 0:ecb5de3a3147 | 162 | usNCoils--; |
| mohamedwajdi | 0:ecb5de3a3147 | 163 | |
| mohamedwajdi | 0:ecb5de3a3147 | 164 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 165 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 166 | else |
| mohamedwajdi | 0:ecb5de3a3147 | 167 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 168 | eStatus = MB_ENOREG; |
| mohamedwajdi | 0:ecb5de3a3147 | 169 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 170 | return eStatus; |
| mohamedwajdi | 0:ecb5de3a3147 | 171 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 172 | |
| mohamedwajdi | 0:ecb5de3a3147 | 173 | // Procedure résevée pour les bits d'entrées (Foction 2) |
| mohamedwajdi | 0:ecb5de3a3147 | 174 | // pucRegBuffer => discrete buffer usAddress => discrete address |
| mohamedwajdi | 0:ecb5de3a3147 | 175 | // usNDiscrete => discrete number |
| mohamedwajdi | 0:ecb5de3a3147 | 176 | eMBErrorCode |
| mohamedwajdi | 0:ecb5de3a3147 | 177 | eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) |
| mohamedwajdi | 0:ecb5de3a3147 | 178 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 179 | eMBErrorCode eStatus = MB_ENOERR; |
| mohamedwajdi | 0:ecb5de3a3147 | 180 | int iIntRegIndex; |
| mohamedwajdi | 0:ecb5de3a3147 | 181 | int iIntBufNum; |
| mohamedwajdi | 0:ecb5de3a3147 | 182 | int iIntBitNum; |
| mohamedwajdi | 0:ecb5de3a3147 | 183 | int iExtRegIndex=0; |
| mohamedwajdi | 0:ecb5de3a3147 | 184 | int iExtBufNum; |
| mohamedwajdi | 0:ecb5de3a3147 | 185 | int iExtBitNum; |
| mohamedwajdi | 0:ecb5de3a3147 | 186 | UCHAR ucTemp; |
| mohamedwajdi | 0:ecb5de3a3147 | 187 | if( ( usAddress >= BITS_IN_START ) |
| mohamedwajdi | 0:ecb5de3a3147 | 188 | && ( usAddress + usNDiscrete <= BITS_IN_START + BITS_IN_NBITS ) ) |
| mohamedwajdi | 0:ecb5de3a3147 | 189 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 190 | iIntRegIndex = ( int )( usAddress - BitsInStart ); |
| mohamedwajdi | 0:ecb5de3a3147 | 191 | |
| mohamedwajdi | 0:ecb5de3a3147 | 192 | while( usNDiscrete > 0 ) |
| mohamedwajdi | 0:ecb5de3a3147 | 193 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 194 | iIntBufNum=iIntRegIndex/8; |
| mohamedwajdi | 0:ecb5de3a3147 | 195 | iIntBitNum=iIntRegIndex%8; |
| mohamedwajdi | 0:ecb5de3a3147 | 196 | iExtBufNum=iExtRegIndex/8; |
| mohamedwajdi | 0:ecb5de3a3147 | 197 | iExtBitNum=iExtRegIndex%8; |
| mohamedwajdi | 0:ecb5de3a3147 | 198 | |
| mohamedwajdi | 0:ecb5de3a3147 | 199 | // Read discrete inputs |
| mohamedwajdi | 0:ecb5de3a3147 | 200 | if(iExtBitNum==0){ |
| mohamedwajdi | 0:ecb5de3a3147 | 201 | pucRegBuffer[iExtBufNum]=0; |
| mohamedwajdi | 0:ecb5de3a3147 | 202 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 203 | ucTemp=(BitsIn[iIntBufNum]>>iIntBitNum) & 1; |
| mohamedwajdi | 0:ecb5de3a3147 | 204 | pucRegBuffer[iExtBufNum]|=ucTemp<<iExtBitNum; |
| mohamedwajdi | 0:ecb5de3a3147 | 205 | |
| mohamedwajdi | 0:ecb5de3a3147 | 206 | iIntRegIndex++; |
| mohamedwajdi | 0:ecb5de3a3147 | 207 | iExtRegIndex++; |
| mohamedwajdi | 0:ecb5de3a3147 | 208 | usNDiscrete--; |
| mohamedwajdi | 0:ecb5de3a3147 | 209 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 210 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 211 | else |
| mohamedwajdi | 0:ecb5de3a3147 | 212 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 213 | eStatus = MB_ENOREG; |
| mohamedwajdi | 0:ecb5de3a3147 | 214 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 215 | return eStatus; |
| mohamedwajdi | 0:ecb5de3a3147 | 216 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 217 | |
| mohamedwajdi | 0:ecb5de3a3147 | 218 | // Procedure résevée pour les registres de sorties (Foction 3 et 6) |
| mohamedwajdi | 0:ecb5de3a3147 | 219 | // pucRegBuffer => holding register buffer usAddress holding => register address |
| mohamedwajdi | 0:ecb5de3a3147 | 220 | // usNRegs holding register number eMode => read or write |
| mohamedwajdi | 0:ecb5de3a3147 | 221 | eMBErrorCode |
| mohamedwajdi | 0:ecb5de3a3147 | 222 | eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) |
| mohamedwajdi | 0:ecb5de3a3147 | 223 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 224 | eMBErrorCode eStatus = MB_ENOERR; |
| mohamedwajdi | 0:ecb5de3a3147 | 225 | int iRegIndex; |
| mohamedwajdi | 0:ecb5de3a3147 | 226 | |
| mohamedwajdi | 0:ecb5de3a3147 | 227 | if( ( usAddress >= REG_OUT_START ) && |
| mohamedwajdi | 0:ecb5de3a3147 | 228 | ( usAddress + usNRegs <= REG_OUT_START + REG_OUT_NREGS ) ) |
| mohamedwajdi | 0:ecb5de3a3147 | 229 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 230 | iRegIndex = ( int )( usAddress - RegOutStart ); |
| mohamedwajdi | 0:ecb5de3a3147 | 231 | switch ( eMode ) |
| mohamedwajdi | 0:ecb5de3a3147 | 232 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 233 | //*** Pass current register values to the protocol stack. *** |
| mohamedwajdi | 0:ecb5de3a3147 | 234 | case MB_REG_READ: |
| mohamedwajdi | 0:ecb5de3a3147 | 235 | while( usNRegs > 0 ) |
| mohamedwajdi | 0:ecb5de3a3147 | 236 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 237 | *pucRegBuffer++ = ( UCHAR ) ( RegOut[iRegIndex] >> 8 ); |
| mohamedwajdi | 0:ecb5de3a3147 | 238 | *pucRegBuffer++ = ( UCHAR ) ( RegOut[iRegIndex] & 0xFF ); |
| mohamedwajdi | 0:ecb5de3a3147 | 239 | iRegIndex++; |
| mohamedwajdi | 0:ecb5de3a3147 | 240 | usNRegs--; |
| mohamedwajdi | 0:ecb5de3a3147 | 241 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 242 | break; |
| mohamedwajdi | 0:ecb5de3a3147 | 243 | |
| mohamedwajdi | 0:ecb5de3a3147 | 244 | //***Update current register values with new values from the protocol stack.*** |
| mohamedwajdi | 0:ecb5de3a3147 | 245 | case MB_REG_WRITE: |
| mohamedwajdi | 0:ecb5de3a3147 | 246 | while( usNRegs > 0 ) |
| mohamedwajdi | 0:ecb5de3a3147 | 247 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 248 | RegOut[iRegIndex] = *pucRegBuffer++ << 8; |
| mohamedwajdi | 0:ecb5de3a3147 | 249 | RegOut[iRegIndex] |= *pucRegBuffer++; |
| mohamedwajdi | 0:ecb5de3a3147 | 250 | iRegIndex++; |
| mohamedwajdi | 0:ecb5de3a3147 | 251 | usNRegs--; |
| mohamedwajdi | 0:ecb5de3a3147 | 252 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 253 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 254 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 255 | else |
| mohamedwajdi | 0:ecb5de3a3147 | 256 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 257 | eStatus = MB_ENOREG; |
| mohamedwajdi | 0:ecb5de3a3147 | 258 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 259 | return eStatus; |
| mohamedwajdi | 0:ecb5de3a3147 | 260 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 261 | // Procedure résevée pour les registres de sorties (Foction 4) |
| mohamedwajdi | 0:ecb5de3a3147 | 262 | // pucRegBuffer => input register buffer usAddress => input register address |
| mohamedwajdi | 0:ecb5de3a3147 | 263 | // usNRegs input register number |
| mohamedwajdi | 0:ecb5de3a3147 | 264 | eMBErrorCode |
| mohamedwajdi | 0:ecb5de3a3147 | 265 | eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) |
| mohamedwajdi | 0:ecb5de3a3147 | 266 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 267 | eMBErrorCode eStatus = MB_ENOERR; |
| mohamedwajdi | 0:ecb5de3a3147 | 268 | int iRegIndex; |
| mohamedwajdi | 0:ecb5de3a3147 | 269 | |
| mohamedwajdi | 0:ecb5de3a3147 | 270 | if( ( usAddress >= REG_IN_START ) |
| mohamedwajdi | 0:ecb5de3a3147 | 271 | && ( usAddress + usNRegs <= REG_IN_START + REG_IN_NREGS ) ) |
| mohamedwajdi | 0:ecb5de3a3147 | 272 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 273 | iRegIndex = ( int )( usAddress - RegInStart ); |
| mohamedwajdi | 0:ecb5de3a3147 | 274 | while( usNRegs > 0 ) |
| mohamedwajdi | 0:ecb5de3a3147 | 275 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 276 | *pucRegBuffer++ = ( unsigned char )( RegIn[iRegIndex] >> 8 ); |
| mohamedwajdi | 0:ecb5de3a3147 | 277 | *pucRegBuffer++ = ( unsigned char )( RegIn[iRegIndex] & 0xFF ); |
| mohamedwajdi | 0:ecb5de3a3147 | 278 | iRegIndex++; |
| mohamedwajdi | 0:ecb5de3a3147 | 279 | usNRegs--; |
| mohamedwajdi | 0:ecb5de3a3147 | 280 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 281 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 282 | else |
| mohamedwajdi | 0:ecb5de3a3147 | 283 | { |
| mohamedwajdi | 0:ecb5de3a3147 | 284 | eStatus = MB_ENOREG; |
| mohamedwajdi | 0:ecb5de3a3147 | 285 | } |
| mohamedwajdi | 0:ecb5de3a3147 | 286 | return eStatus; |
| mohamedwajdi | 0:ecb5de3a3147 | 287 | } |