Subversion Repositories NaviCtrl

Rev

Rev 710 | Rev 718 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 710 Rev 713
1
#include <string.h>
1
#include <string.h>
2
#include "91x_lib.h"
2
#include "91x_lib.h"
3
#include "91x_can.h"
3
#include "91x_can.h"
4
#include "timer1.h"
4
#include "timer1.h"
5
#include "led.h"
5
#include "led.h"
6
#include "canbus.h"
6
#include "canbus.h"
7
#include "compass.h"
7
#include "compass.h"
8
#include "main.h"
8
#include "main.h"
9
#include "uart0.h"
9
#include "uart0.h"
10
#include "uart1.h"
10
#include "uart1.h"
11
#include "GPS.h"
11
#include "GPS.h"
12
 
12
 
13
canmsg CanMsg;
13
canmsg CanMsg;
14
canmsg RxCanMsg;
14
canmsg RxCanMsg;
15
volatile u32 AllMsgsReceived;
15
volatile u32 AllMsgsReceived;
16
u32 CanbusTimeOut = 0;
16
u32 CanbusTimeOut = 0;
17
 
17
 
18
GPIO_InitTypeDef    GPIO_InitStructure;
18
GPIO_InitTypeDef    GPIO_InitStructure;
19
CAN_InitTypeDef     CAN_InitStructure;
19
CAN_InitTypeDef     CAN_InitStructure;
20
 
20
 
21
 
21
 
22
typedef  enum {
22
typedef  enum {
23
    NA,
23
    NA,
24
    Priority_1,
24
    Priority_1,
25
    Priority_2,
25
    Priority_2,
26
    Priority_3
26
    Priority_3
27
  }VIC_Priority;
27
  }VIC_Priority;
28
 
28
 
29
  /* buffer for receive messages */
29
  /* buffer for receive messages */
30
  canmsg RxCanTxMsg;
30
  canmsg RxCanTxMsg;
31
 
31
 
32
  /* used message object numbers */
32
  /* used message object numbers */
33
  enum {
33
  enum {
34
        CAN_TX_MSGOBJ = 0,
34
        CAN_TX_MSGOBJ = 0,
35
        CAN_RX_MSGOBJ = 1
35
        CAN_RX_MSGOBJ = 1
36
  };
36
  };
37
 
37
 
38
#define CAN_MSG_ADR_MA  0x200
38
#define CAN_MSG_ADR_MA  0x200
39
#define CAN_MSG_ADR_SL  0x300
39
#define CAN_MSG_ADR_SL  0x300
40
 
40
 
41
u32 CAN_IdTx; // I am Sending in this ID-Range
41
u32 CAN_IdTx; // I am Sending in this ID-Range
42
u32 CAN_IdRx; // I am Receiving in this ID-Range
42
u32 CAN_IdRx; // I am Receiving in this ID-Range
43
 
43
 
44
CanMessage_t CanTxMessage[MAX_CAN_MSG];
44
CanMessage_t CanTxMessage[MAX_CAN_MSG];
45
CanMessage_t CanRxMessage[MAX_CAN_MSG];
45
CanMessage_t CanRxMessage[MAX_CAN_MSG];
46
 
46
 
47
void CAN_IO_Init(void)
47
void CAN_IO_Init(void)
48
{
48
{
49
// P5.0 alternate input 1, CAN_RX pin 
49
// P5.0 alternate input 1, CAN_RX pin 
50
        GPIO_StructInit(&GPIO_InitStructure);
50
        GPIO_StructInit(&GPIO_InitStructure);
51
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
51
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
52
        GPIO_InitStructure.GPIO_Direction=GPIO_PinInput;
52
        GPIO_InitStructure.GPIO_Direction=GPIO_PinInput;
53
        GPIO_InitStructure.GPIO_IPInputConnected=GPIO_IPInputConnected_Enable;
53
        GPIO_InitStructure.GPIO_IPInputConnected=GPIO_IPInputConnected_Enable;
54
        GPIO_InitStructure.GPIO_Alternate=GPIO_InputAlt1;
54
        GPIO_InitStructure.GPIO_Alternate=GPIO_InputAlt1;
55
        GPIO_Init(GPIO5,&GPIO_InitStructure);
55
        GPIO_Init(GPIO5,&GPIO_InitStructure);
56
// P5.1 CAN_TX
56
// P5.1 CAN_TX
57
        GPIO_StructInit(&GPIO_InitStructure);
57
        GPIO_StructInit(&GPIO_InitStructure);
58
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
58
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
59
        GPIO_InitStructure.GPIO_Direction=GPIO_PinOutput;
59
        GPIO_InitStructure.GPIO_Direction=GPIO_PinOutput;
60
        GPIO_InitStructure.GPIO_Type=GPIO_Type_PushPull;
60
        GPIO_InitStructure.GPIO_Type=GPIO_Type_PushPull;
61
        GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt2;
61
        GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt2;
62
        GPIO_Init(GPIO5,&GPIO_InitStructure);
62
        GPIO_Init(GPIO5,&GPIO_InitStructure);
63
}
63
}
64
 
64
 
65
 
65
 
66
 
66
 
67
/*******************************************************************************
67
/*******************************************************************************
68
* Function Name  : CAN_IRQHandler
68
* Function Name  : CAN_IRQHandler
69
* Description    : This function handles the CAN interrupt request
69
* Description    : This function handles the CAN interrupt request
70
*******************************************************************************/
70
*******************************************************************************/
71
void CAN_IRQHandler(void)
71
void CAN_IRQHandler(void)
72
{
72
{
73
   u32 msgobj = 0;
73
   u32 msgobj = 0;
74
  if(CAN->IDR == 0x8000)        /* status interrupt */
74
  if(CAN->IDR == 0x8000)        /* status interrupt */
75
  {
75
  {
76
    (void)CAN->SR;      /* read the status register to clear*/
76
    (void)CAN->SR;      /* read the status register to clear*/
77
  }
77
  }
78
  else if(CAN->IDR >= 1 && CAN->IDR <= 32)
78
  else if(CAN->IDR >= 1 && CAN->IDR <= 32)
79
  {
79
  {
80
    /* get the message object number that caused the interrupt to occur */
80
    /* get the message object number that caused the interrupt to occur */
81
    switch(msgobj = CAN->IDR - 1)
81
    switch(msgobj = CAN->IDR - 1)
82
    {
82
    {
83
      case  0 /* CAN_TX_MSGOBJ */:
83
      case  0 /* CAN_TX_MSGOBJ */:
84
                CAN_ReleaseTxMessage(msgobj);
84
                CAN_ReleaseTxMessage(msgobj);
85
        break;
85
        break;
86
 
86
 
87
      case 1 /* CAN_RX_MSGOBJ */:
87
      case 1 /* CAN_RX_MSGOBJ */:
88
        CAN_ReceiveMessage(msgobj, FALSE, &RxCanMsg);
88
        CAN_ReceiveMessage(msgobj, FALSE, &RxCanMsg);
89
                CAN_ReleaseRxMessage(msgobj);
89
                CAN_ReleaseRxMessage(msgobj);
90
 
90
 
91
                if(RxCanMsg.Id >= CAN_IdRx && RxCanMsg.Id < CAN_IdRx + MAX_CAN_MSG)
91
                if(RxCanMsg.Id >= CAN_IdRx && RxCanMsg.Id < CAN_IdRx + MAX_CAN_MSG)
92
                 {
92
                 {
93
                        memcpy(&CanRxMessage[RxCanMsg.Id - CAN_IdRx], &RxCanMsg,sizeof(RxCanMsg));
93
                        memcpy(&CanRxMessage[RxCanMsg.Id - CAN_IdRx], &RxCanMsg,sizeof(RxCanMsg));
94
                 }
94
                 }
95
                if(RxCanMsg.Id == CAN_IdRx + MAX_CAN_MSG - 1)
95
                if(RxCanMsg.Id == CAN_IdRx + MAX_CAN_MSG - 1)
96
                 {
96
                 {
97
                  AllMsgsReceived = 1;
97
                  AllMsgsReceived = 1;
98
                 }
98
                 }
99
//              DebugOut.Analog[] = RxCanMsg.Id;
99
//              DebugOut.Analog[] = RxCanMsg.Id;
100
  break;
100
  break;
101
 
101
 
102
      default:
102
      default:
103
        CAN_ReleaseMessage(msgobj);
103
        CAN_ReleaseMessage(msgobj);
104
        break;
104
        break;
105
    }
105
    }
106
  }
106
  }
107
   
107
   
108
   /*write any value to VIC0 VAR*/  
108
   /*write any value to VIC0 VAR*/  
109
   VIC0->VAR = 0xFF;   
109
   VIC0->VAR = 0xFF;   
110
 
110
 
111
}
111
}
112
 
112
 
113
void CanSend(void)
113
void CanSend(void)
114
{
114
{
115
static u32 index = 1;
115
static u32 index = 1;
116
 
116
 
117
  if(CAN_SendMessage(CAN_TX_MSGOBJ, (canmsg*) &CanTxMessage[index]) == SUCCESS)
117
  if(CAN_SendMessage(CAN_TX_MSGOBJ, (canmsg*) &CanTxMessage[index]) == SUCCESS)
118
   {
118
   {
119
    if(++index >= MAX_CAN_MSG)
119
    if(++index >= MAX_CAN_MSG)
120
         {
120
         {
121
                u32 i;
121
                u32 i;
122
                index = 0;
122
                index = 0;
123
                CanTxMessage[CAN_ID_STATUS].D.Byte[0] = ErrorCode;
123
                CanTxMessage[CAN_ID_STATUS].D.Byte[0] = ErrorCode;
124
 
124
 
125
                CanTxMessage[CAN_ID_FS_LON].D.sLong = GPS_FailsafePosition.Longitude;
125
                CanTxMessage[CAN_ID_FS_LON].D.sLong = GPS_FailsafePosition.Longitude;
126
                CanTxMessage[CAN_ID_FS_LAT].D.sLong = GPS_FailsafePosition.Latitude;
126
                CanTxMessage[CAN_ID_FS_LAT].D.sLong = GPS_FailsafePosition.Latitude;
127
 
127
 
128
                CanTxMessage[CAN_ID_STATUS].D.Byte[0] = ErrorCode;
128
                CanTxMessage[CAN_ID_STATUS].D.Byte[0] = ErrorCode;
129
                CanTxMessage[CAN_ID_STATUS].D.Byte[1] = FC.StatusFlags;
129
                CanTxMessage[CAN_ID_STATUS].D.Byte[1] = FC.StatusFlags;
130
                CanTxMessage[CAN_ID_STATUS].D.Byte[2] = FC.StatusFlags2;
130
                CanTxMessage[CAN_ID_STATUS].D.Byte[2] = FC.StatusFlags2;
131
                CanTxMessage[CAN_ID_STATUS].D.Byte[3] = FC.StatusFlags3;
131
                CanTxMessage[CAN_ID_STATUS].D.Byte[3] = FC.StatusFlags3;
132
                CanTxMessage[CAN_ID_STATUS].D.Byte[4] = NC_To_FC_Flags;
132
                CanTxMessage[CAN_ID_STATUS].D.Byte[4] = NC_To_FC_Flags;
133
                CanTxMessage[CAN_ID_STATUS].D.Byte[5] = EarthMagneticField/5; // in %
133
                CanTxMessage[CAN_ID_STATUS].D.Byte[5] = EarthMagneticField/5; // in %
134
                CanTxMessage[CAN_ID_STATUS].D.Int[3]  = GyroCompassCorrected;
134
                CanTxMessage[CAN_ID_STATUS].D.Int[3]  = GyroCompassCorrected;
135
 
135
 
136
                for(i=0; i<8;i++)
136
                for(i=0; i<8;i++)
137
                 {
137
                 {
138
                  CanTxMessage[CAN_ID_TEXT1].D.Byte[i] = ErrorMSG[i];
138
                  CanTxMessage[CAN_ID_TEXT1].D.Byte[i] = ErrorMSG[i];
139
                  CanTxMessage[CAN_ID_TEXT2].D.Byte[i] = ErrorMSG[i + 8];
139
                  CanTxMessage[CAN_ID_TEXT2].D.Byte[i] = ErrorMSG[i + 8];
140
                  CanTxMessage[CAN_ID_TEXT3].D.Byte[i] = ErrorMSG[i + 16];
140
                  CanTxMessage[CAN_ID_TEXT3].D.Byte[i] = ErrorMSG[i + 16];
141
                 }                                                                               
141
                 }                                                                               
142
         }
142
         }
143
   }
143
   }
144
}
144
}
145
 
145
 
146
void CanReceive(void)
146
void CanReceive(void)
147
{
147
{
148
 
148
 
149
}
149
}
150
 
150
 
151
 
151
 
152
void CanbusInit(void)
152
void CanbusInit(void)
153
{
153
{
154
 UART1_PutString("\r\n Canbus init...");
154
 UART1_PutString("\r\n Canbus init...");
155
 CAN_IO_Init();
155
 CAN_IO_Init();
156
 SCU_APBPeriphClockConfig(__CAN, ENABLE);
156
 SCU_APBPeriphClockConfig(__CAN, ENABLE);
157
 SCU_APBPeriphReset(__CAN, DISABLE);
157
 SCU_APBPeriphReset(__CAN, DISABLE);
158
 
158
 
159
 VIC_Config(CAN_ITLine, VIC_IRQ, Priority_1);
159
 VIC_Config(CAN_ITLine, VIC_IRQ, Priority_1);
160
 CAN_InitStructure.CAN_ConfigParameters=CAN_CR_IE;
160
 CAN_InitStructure.CAN_ConfigParameters=CAN_CR_IE;
161
 CAN_InitStructure.CAN_Bitrate = CAN_BITRATE_1M;
161
 CAN_InitStructure.CAN_Bitrate = CAN_BITRATE_1M;
162
 CAN_Init(&CAN_InitStructure);
162
 CAN_Init(&CAN_InitStructure);
163
 VIC_ITCmd(CAN_ITLine, ENABLE);
163
 VIC_ITCmd(CAN_ITLine, ENABLE);
164
 
164
 
165
 CAN_SetUnusedAllMsgObj();
165
 CAN_SetUnusedAllMsgObj();
166
 CAN_SetTxMsgObj(CAN_TX_MSGOBJ, CAN_STD_ID, DISABLE);
166
 CAN_SetTxMsgObj(CAN_TX_MSGOBJ, CAN_STD_ID, DISABLE);
167
 CAN_SetRxMsgObj(CAN_RX_MSGOBJ, CAN_STD_ID, 0, CAN_LAST_STD_ID, TRUE);
167
 CAN_SetRxMsgObj(CAN_RX_MSGOBJ, CAN_STD_ID, 0, CAN_LAST_STD_ID, TRUE);
168
 
168
 
169
u32 i;
169
u32 i;
170
 
170
 
171
if(IamMaster == SLAVE)
171
if(IamMaster == SLAVE)
172
{
172
{
173
 CAN_IdRx = CAN_MSG_ADR_MA;
173
 CAN_IdRx = CAN_MSG_ADR_MA;
174
 CAN_IdTx = CAN_MSG_ADR_SL;
174
 CAN_IdTx = CAN_MSG_ADR_SL;
175
}
175
}
176
else
176
else
177
{
177
{
178
 CAN_IdRx = CAN_MSG_ADR_SL;
178
 CAN_IdRx = CAN_MSG_ADR_SL;
179
 CAN_IdTx = CAN_MSG_ADR_MA;
179
 CAN_IdTx = CAN_MSG_ADR_MA;
180
}
180
}
181
 
181
 
182
for(i=0; i< MAX_CAN_MSG;i++)
182
for(i=0; i< MAX_CAN_MSG;i++)
183
 {
183
 {
184
        // clear TX Buffer
184
        // clear TX Buffer
185
        CanTxMessage[i].IdType = CAN_STD_ID;
185
        CanTxMessage[i].IdType = CAN_STD_ID;
186
        CanTxMessage[i].Id = i + CAN_IdTx;
186
        CanTxMessage[i].Id = i + CAN_IdTx;
187
        CanTxMessage[i].Length = 8;
187
        CanTxMessage[i].Length = 8;
188
        CanTxMessage[i].D.Long = 0;
188
        CanTxMessage[i].D.Long = 0;
189
 
189
 
190
        // clear receiving Buffer
190
        // clear receiving Buffer
191
        CanRxMessage[i].Id = 0;
191
        CanRxMessage[i].Id = 0;
192
        CanRxMessage[i].D.Long = 0;
192
        CanRxMessage[i].D.Long = 0;
193
 }
193
 }
194
 
194
 
195
CanTxMessage[CAN_ID_VERSION].D.Byte[0]  = 0;
195
CanTxMessage[CAN_ID_VERSION].D.Byte[0]  = 0;
196
CanTxMessage[CAN_ID_VERSION].D.Byte[1]  = Parameter.ActiveSetting;
196
CanTxMessage[CAN_ID_VERSION].D.Byte[1]  = Parameter.ActiveSetting;
197
CanTxMessage[CAN_ID_VERSION].D.Byte[2]  = GPS_Version/1000;
197
CanTxMessage[CAN_ID_VERSION].D.Byte[2]  = GPS_Version/1000;
198
CanTxMessage[CAN_ID_VERSION].D.Byte[3]  = UART_VersionInfo.HWMajor;
198
CanTxMessage[CAN_ID_VERSION].D.Byte[3]  = UART_VersionInfo.HWMajor;
199
CanTxMessage[CAN_ID_VERSION].D.Byte[4]  = CAN_SLAVE_COMPATIBLE;
199
CanTxMessage[CAN_ID_VERSION].D.Byte[4]  = CAN_SLAVE_COMPATIBLE;
200
CanTxMessage[CAN_ID_VERSION].D.Byte[5]  = VERSION_PATCH;
200
CanTxMessage[CAN_ID_VERSION].D.Byte[5]  = VERSION_PATCH;
201
CanTxMessage[CAN_ID_VERSION].D.Byte[6]  = VERSION_MINOR;
201
CanTxMessage[CAN_ID_VERSION].D.Byte[6]  = VERSION_MINOR;
202
CanTxMessage[CAN_ID_VERSION].D.Byte[7]  = VERSION_MAJOR;
202
CanTxMessage[CAN_ID_VERSION].D.Byte[7]  = VERSION_MAJOR;
203
 
203
 
204
 UART1_PutString("ok");
204
 UART1_PutString("ok");
205
}
205
}
206
 
206
 
207
void ProcessCanBus(void)
207
void ProcessCanBus(void)
208
{
208
{
209
 u32 errorcnt = 0;
209
 u32 errorcnt = 0;
210
 CanSend();
210
 CanSend();
211
 CanReceive();
211
 CanReceive();
212
 
212
 
213
errorcnt = CAN_GetTransmitErrorCounter();
213
errorcnt = CAN_GetTransmitErrorCounter();
214
DebugOut.Analog[18] = errorcnt;
214
//DebugOut.Analog[18] = errorcnt;
215
//if(errorcnt > 200) CanbusInit();
215
//if(errorcnt > 200) CanbusInit();
216
 
216
 
217
//DebugOut.Analog[16] = CAN->SR ;
217
//DebugOut.Analog[16] = CAN->SR ;
-
 
218
if(CanbusTimeOut < 3)
-
 
219
{
-
 
220
  if(CanbusTimeOut == 1) Partner.ErrorCode = 39; // ERR: Canbus
-
 
221
  Partner.StatusFlags = 0;
-
 
222
  Partner.StatusFlags2 = 0;
-
 
223
  Partner.StatusFlags3 = 0;
-
 
224
  Partner.NC_To_FC_Flags = 0;
-
 
225
  Partner.MagnetField = 0;
-
 
226
  Partner.GyroCompassCorrected = -1;
-
 
227
}
218
 
228
 
219
if(AllMsgsReceived)
229
if(AllMsgsReceived)
220
 {
230
 {
221
  u32 i;
231
  u32 i;
222
  AllMsgsReceived = 0;
232
  AllMsgsReceived = 0;
223
//  DebugOut.Analog[16]++;      
233
//  DebugOut.Analog[16]++;      
224
  CanbusTimeOut = 1000;
234
  CanbusTimeOut = 1000;
225
  for(i=0; i<8;i++)
235
  for(i=0; i<8;i++)
226
   {
236
   {
227
          PartnerErrorMSG[i]    = CanRxMessage[CAN_ID_TEXT1].D.Byte[i];
237
          PartnerErrorMSG[i]    = CanRxMessage[CAN_ID_TEXT1].D.Byte[i];
228
          PartnerErrorMSG[i+8]  = CanRxMessage[CAN_ID_TEXT2].D.Byte[i];
238
          PartnerErrorMSG[i+8]  = CanRxMessage[CAN_ID_TEXT2].D.Byte[i];
229
          PartnerErrorMSG[i+16] = CanRxMessage[CAN_ID_TEXT3].D.Byte[i];
239
          PartnerErrorMSG[i+16] = CanRxMessage[CAN_ID_TEXT3].D.Byte[i];
230
   }                                                                             
240
   }                                                                             
231
  Partner.ErrorCode                     = CanRxMessage[CAN_ID_STATUS].D.Byte[0];
241
  Partner.ErrorCode                     = CanRxMessage[CAN_ID_STATUS].D.Byte[0];
232
  Partner.StatusFlags                   = CanRxMessage[CAN_ID_STATUS].D.Byte[1];
242
  Partner.StatusFlags                   = CanRxMessage[CAN_ID_STATUS].D.Byte[1];
233
  Partner.StatusFlags2                  = CanRxMessage[CAN_ID_STATUS].D.Byte[2];
243
  Partner.StatusFlags2                  = CanRxMessage[CAN_ID_STATUS].D.Byte[2];
234
  Partner.StatusFlags3                  = CanRxMessage[CAN_ID_STATUS].D.Byte[3];
244
  Partner.StatusFlags3                  = CanRxMessage[CAN_ID_STATUS].D.Byte[3];
235
  Partner.NC_To_FC_Flags                = CanRxMessage[CAN_ID_STATUS].D.Byte[4];
245
  Partner.NC_To_FC_Flags                = CanRxMessage[CAN_ID_STATUS].D.Byte[4];
236
  Partner.MagnetField                   = CanRxMessage[CAN_ID_STATUS].D.Byte[5];
246
  Partner.MagnetField                   = CanRxMessage[CAN_ID_STATUS].D.Byte[5];
237
  Partner.GyroCompassCorrected  = CanRxMessage[CAN_ID_STATUS].D.Int[3];
247
  Partner.GyroCompassCorrected  = CanRxMessage[CAN_ID_STATUS].D.Int[3];
238
 
248
 
239
  CanTxMessage[CAN_ID_VERSION].D.Byte[1] = Parameter.ActiveSetting;
249
  CanTxMessage[CAN_ID_VERSION].D.Byte[1] = Parameter.ActiveSetting;
240
 
250
 
241
if(IamMaster == SLAVE)
251
if(IamMaster == SLAVE)
242
  {
252
  {
243
                GPS_FailsafePosition.Longitude = CanRxMessage[CAN_ID_FS_LON].D.sLong;
253
                GPS_FailsafePosition.Longitude = CanRxMessage[CAN_ID_FS_LON].D.sLong;
244
                GPS_FailsafePosition.Latitude = CanRxMessage[CAN_ID_FS_LAT].D.sLong;
254
                GPS_FailsafePosition.Latitude = CanRxMessage[CAN_ID_FS_LAT].D.sLong;
245
//DebugOut.Analog[17] = CanRxMessage[CAN_ID_FS_LON].D.sLong / 100000;
255
//DebugOut.Analog[17] = CanRxMessage[CAN_ID_FS_LON].D.sLong / 100000;
246
//DebugOut.Analog[18] = CanRxMessage[CAN_ID_FS_LAT].D.sLong / 100000;
256
//DebugOut.Analog[18] = CanRxMessage[CAN_ID_FS_LAT].D.sLong / 100000;
247
  }
257
  }
248
 }
258
 }
-
 
259
 
249
}                                        
260
}                                        
250
 
261
 
251
 
262
 
252
 
263
 
253
 
264
 
254
 
265
 
255
 
266