readsend.cpp


1
/*------------------------------------------------------|
2
|                            |
3
|  Version 1.3    Date: 09/10/2009          |
4
|  Author:      Dennis Schuethe            |
5
|  Company:    Anders Electronics          |
6
|  file name:    readsend.cpp            |
7
|                            |
8
|  Description:                    |
9
|    This subprogramm includes the functions      |
10
|    for receive and transmit a message        |
11
|    over the CAN bus.                |
12
|    The transmition of a message is only done by  |
13
|    the TX0 register and only for standart      |
14
|    identifier.                    |
15
|    The receive function reads messages only for  |
16
|    standart identifier, but from both registers  |
17
|------------------------------------------------------*/
18
19
20
extern "C"
21
{
22
#include "main.h"
23
24
/*send specific data to MCP2515 and enables transmission-------------------------------------------*/
25
26
// send only via TXB0 register
27
int sendMessageStdId(tStdID *identifier, BYTE *pDataIn, unsigned char dataLength)
28
{
29
  //local variables
30
  BYTE idBits10to3= identifier->stdID >> 3;
31
  BYTE idBits2to0 = identifier->stdID << 5;
32
  int i= 0;
33
  int j= 0;
34
  short returnValue;
35
  
36
  while( (readRegister(TXB0CTRL) & 0x08 != 0x00) BREAKAFTER ) i++; //Check if last message has been send before start writing new message
37
  if (BREAK) //if the while loop one line above has been quited by register readout
38
  {
39
    i=0;
40
    resetFlags( 0x04);  // reset the transmission complete flag
41
42
    // all addresses needed for sending data
43
    BYTE addresses[6] = {TXB0D0, TXB0D2, TXB0D4, TXB0D6, TXB0EID0, TXB0SIDH};
44
45
    //variables for transmission via SPI
46
    DWORD speed = SPISPEED;
47
    BYTE dataIn[4];
48
    dataIn[0]= WRITE_SPI;
49
    DWORD lengthIn = 4;
50
    BYTE dataOut[4];
51
    DWORD bytesreturn = 0;
52
    
53
    // write data into the data registers
54
    while( j < dataLength) {  
55
      dataIn[1]= addresses[i++]; 
56
      dataIn[2]= pDataIn[j++]; 
57
      if( j < dataLength) dataIn[3]= pDataIn[j++];
58
      SPIRW
59
    }
60
61
    i= 4;
62
    // write how many data bytes will be sent
63
    dataIn[1]= addresses[i++];
64
    dataIn[2]= 0x00;
65
    dataIn[3]= dataLength;
66
    SPIRW
67
68
    // write the std identifier into the registers
69
    dataIn[1]= addresses[i];
70
    dataIn[2]= idBits10to3;
71
    dataIn[3]= idBits2to0;
72
    SPIRW
73
74
    // enable transmission of the stored data
75
    dataIn[0] = RTS_TXB0; 
76
    lengthIn = 1;
77
    SPIRW
78
79
    returnValue = 0;
80
  }  
81
82
  else
83
    returnValue = ERR;
84
85
  return returnValue;
86
}
87
/********************************************************/
88
89
90
/*-read-data from MCP2515 and resets the reading flags----------------------------------------*/
91
int readMessageStdId(BYTE *pDataOut , tStdID *pID, int *dataLength)
92
{
93
  short returnValue = -1;
94
  short i=0,j=0;
95
  BYTE msgaddress[8] = { RXB0D0 , RXB0D2, RXB0D4, RXB0D6,
96
               RXB1D0, RXB1D2, RXB1D4, RXB1D6  };
97
  BYTE dataIn[4]={RX_STATUS , 0x00, 0x00 , 0x00};
98
  BYTE dataOut[4] = {0x00, 0x00, 0x00, 0x00};
99
  DWORD speed = SPISPEED;
100
  DWORD lengthIn = 4;
101
  DWORD bytesreturn = 0;
102
  SPIRW
103
104
  for (i=0; i < 8 ; i++) pDataOut[i]= (i << 4 | i);// initialization of the data out
105
106
  if (dataOut[1] == dataOut[2])
107
  {
108
109
    switch (dataOut[1] & 0xC0){
110
111
      // no message in any receive register
112
      case 0x00: 
113
        returnValue = 0; 
114
        break;
115
116
      // Message in Buffer 0 is reading out
117
      case 0x40:
118
        if ( readRegister(RXB0CTRL) & 0x08 == 0x08)  //a remote transfer request arrived
119
        {
120
          dataIn[0] = READ_RX_BF00; //read the SIDH and SIDL registers of BF0
121
          SPIRW
122
          pID->stdID = (dataOut[1] << 3) | (dataOut[2] >> 5); //write identifier into the address
123
          returnValue = 11; // signalisation of a remote transfer request
124
        }
125
        else
126
        {
127
          dataIn[0] = READ_RX_BF00; //read the SIDH and SIDL registers of BF0
128
          SPIRW
129
          pID->stdID = (dataOut[1] << 3) | (dataOut[2] >> 5); //write identifier into the address
130
          
131
          *dataLength = (int) readRegister(RXB0DLC) & 0x0f; //read how much data bytes has been sent
132
          dataIn[0] = READ_SPI;
133
          i = 0;
134
          // read out the data from data register 0 to 7
135
          for (j=0 ; j< *dataLength ; j++)
136
          {
137
            dataIn[1] = msgaddress[i++];
138
            SPIRW
139
            pDataOut[j] = dataOut[2];
140
            if(++j < *dataLength)
141
            pDataOut[j] = dataOut[3];
142
          }
143
          
144
          returnValue = 10; //msg read out from register 0
145
146
        }
147
        resetFlags(0x01);
148
        break;
149
150
      // Message in Buffer 1 reading out
151
      case 0x80:
152
        if ( readRegister(RXB1CTRL) & 0x08 == 0x08)  //a remote transfer request arrived
153
        {
154
          dataIn[0] = READ_RX_BF10; //read the SIDH and SIDL registers of BF0
155
          SPIRW
156
          pID->stdID = (dataOut[1] << 3) | (dataOut[2] >> 5); //write identifier into the address
157
          returnValue = 21; // signalisation of a remote transfer request
158
        }
159
        else
160
        {
161
          dataIn[0] = READ_RX_BF10; //read the SIDH and SIDL registers of BF1
162
          SPIRW
163
          pID->stdID = (dataOut[1] << 3) | (dataOut[2] >> 5); //write identifier into the address
164
          
165
          
166
          *dataLength = (int) readRegister(RXB1DLC) & 0x0f; //read how much data bytes has been sent
167
          dataIn[0] = READ_SPI;
168
          i = 0;
169
          // read out the data from data register 0 to 7
170
          for (j=0 ; j< *dataLength ; j++)
171
          {
172
            dataIn[1] = msgaddress[i++];
173
            SPIRW
174
            pDataOut[j] = dataOut[2];
175
            if(++j < *dataLength)
176
            pDataOut[++j] = dataOut[3];
177
          }
178
          
179
          returnValue = 20; //msg read out from register 1
180
181
        }
182
        resetFlags(0x02);
183
        break;
184
185
      // data in both registers, reading only the BF0
186
      case 0xC0:
187
        if ( readRegister(RXB0CTRL) & 0x08 == 0x08)  //a remote transfer request arrived
188
        {
189
          dataIn[0] = READ_RX_BF00; //read the SIDH and SIDL registers of BF0
190
          SPIRW
191
          pID->stdID = (dataOut[1] << 3) | (dataOut[2] >> 5); //write identifier into the address
192
          returnValue = 31; // signalisation of a remote transfer request
193
        }
194
        else
195
        {
196
          dataIn[0] = READ_RX_BF00; //read the SIDH and SIDL registers of BF0
197
          SPIRW
198
          pID->stdID = (dataOut[1] << 3) | (dataOut[2] >> 5); //write identifier into the address
199
          
200
          *dataLength = (int) readRegister(RXB0DLC) & 0x0f; //read how much data bytes has been sent
201
          dataIn[0] = READ_SPI;
202
          i = 0;
203
          // read out the data from data register 0 to 7
204
          for (j=0 ; j<*dataLength ; j++)
205
          {
206
            dataIn[1] = msgaddress[i++];
207
            SPIRW
208
            pDataOut[++j] = dataOut[2];
209
            if(j+1 < *dataLength)
210
            pDataOut[j] = dataOut[3];
211
          }
212
          
213
          returnValue = 30; //msg read out from register 0
214
215
        }
216
        resetFlags(0x01);
217
        break;
218
    }
219
  }
220
  else
221
    returnValue = -1; //failure-two different data bytes are read from the status register
222
223
  return returnValue;
224
}
225
/********************************************************/
226
}