Subversion Repositories NaviCtrl

Rev

Rev 693 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
691 holgerb 1
#include "91x_lib.h"
2
#include "91x_can.h"
3
#include "timer1.h"
4
#include "led.h"
5
#include "canbus.h"
6
#include "main.h"
7
#include "uart1.h"
8
 
9
canmsg CanMsg;
10
canmsg RxCanMsg;
11
volatile u32 frame_received_flag;
12
 
13
GPIO_InitTypeDef    GPIO_InitStructure;
14
CAN_InitTypeDef     CAN_InitStructure;
15
 
16
 
17
typedef  enum {
18
    NA,
19
    Priority_1,
20
    Priority_2,
21
    Priority_3
22
  }VIC_Priority;
23
 
24
  /* buffer for receive messages */
25
  canmsg RxCanMsg;
26
 
27
  /* used message object numbers */
28
  enum {
29
        CAN_TX_MSGOBJ = 0,
30
        CAN_RX_MSGOBJ = 1
31
  };
32
 
33
  /* array of pre-defined transmit messages */
34
  canmsg TxCanMsg[3] = {
35
        { CAN_STD_ID,      0x123, 4, { 0x01, 0x02, 0x04, 0x08 } },
36
        { CAN_STD_ID,      0x321, 2, { 0xAA, 0x55, 0xAA, 0x55 } },
37
        { CAN_EXT_ID, 0x12345678, 8, { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 } }
38
  };
39
 
40
void CAN_IO_Init(void)
41
{
42
// P5.0 alternate input 1, CAN_RX pin 
43
        GPIO_StructInit(&GPIO_InitStructure);
44
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
45
        GPIO_InitStructure.GPIO_Direction=GPIO_PinInput;
46
        GPIO_InitStructure.GPIO_IPInputConnected=GPIO_IPInputConnected_Enable;
47
        GPIO_InitStructure.GPIO_Alternate=GPIO_InputAlt1;
48
        GPIO_Init(GPIO5,&GPIO_InitStructure);
49
// P5.1 CAN_TX
50
        GPIO_StructInit(&GPIO_InitStructure);
51
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
52
        GPIO_InitStructure.GPIO_Direction=GPIO_PinOutput;
53
        GPIO_InitStructure.GPIO_Type=GPIO_Type_PushPull;
54
        GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt2;
55
        GPIO_Init(GPIO5,&GPIO_InitStructure);
56
}
57
 
58
 
59
 
60
/*******************************************************************************
61
* Function Name  : CAN_IRQHandler
62
* Description    : This function handles the CAN interrupt request
63
*******************************************************************************/
64
void CAN_IRQHandler(void)
65
{
66
   u32 msgobj = 0;
67
  if(CAN->IDR == 0x8000)        /* status interrupt */
68
  {
69
    (void)CAN->SR;      /* read the status register to clear*/
70
  }
71
  else if(CAN->IDR >= 1 && CAN->IDR <= 32)
72
  {
73
    /* get the message object number that caused the interrupt to occur */
74
    switch(msgobj = CAN->IDR - 1)
75
    {
76
      case  0 /* CAN_TX_MSGOBJ */:
77
        DebugOut.Analog[17]++;
78
                CAN_ReleaseTxMessage(msgobj);
79
        break;
80
 
81
      case 1 /* CAN_RX_MSGOBJ */:
82
        CAN_ReceiveMessage(msgobj, FALSE, &RxCanMsg);
83
        CAN_ReleaseRxMessage(msgobj);
84
        frame_received_flag = 1;
85
DebugOut.Analog[19]++; 
86
  break;
87
 
88
      default:
89
        CAN_ReleaseMessage(msgobj);
90
        break;
91
    }
92
  }
93
 
94
   /*write any value to VIC0 VAR*/  
95
   VIC0->VAR = 0xFF;   
96
 
97
}
98
 
99
void CanSend(void)
100
{
101
//  CAN_SetUnusedAllMsgObj();
102
//  CAN_SetTxMsgObj(CAN_TX_MSGOBJ, CAN_STD_ID, DISABLE);
103
//  CAN_SetRxMsgObj(CAN_RX_MSGOBJ, CAN_STD_ID, 0, CAN_LAST_STD_ID, TRUE);
104
//CAN_UpdateMsgObj(CAN_TX_MSGOBJ, &TxCanMsg[1]);
105
  TxCanMsg[1].Data[0] = DebugOut.Analog[24] / 256;
106
  TxCanMsg[1].Data[1] = DebugOut.Analog[24] % 256;
107
  CAN_SendMessage(CAN_TX_MSGOBJ, &TxCanMsg[1]);
108
}
109
 
110
void CanReceive(void)
111
{
112
 
113
}
114
 
115
 
116
void CanbusInit(void)
117
{
118
 UART1_PutString("\r\n Canbus init...");
119
 CAN_IO_Init();
120
 SCU_APBPeriphClockConfig(__CAN, ENABLE);
121
 SCU_APBPeriphReset(__CAN, DISABLE);
122
 
123
 VIC_Config(CAN_ITLine, VIC_IRQ, Priority_1);
124
 CAN_InitStructure.CAN_ConfigParameters=CAN_CR_IE;
125
 CAN_InitStructure.CAN_Bitrate = CAN_BITRATE_1M;
126
 CAN_Init(&CAN_InitStructure);
127
 VIC_ITCmd(CAN_ITLine, ENABLE);
128
 
129
 CAN_SetUnusedAllMsgObj();
130
 CAN_SetTxMsgObj(CAN_TX_MSGOBJ, CAN_STD_ID, DISABLE);
131
 CAN_SetRxMsgObj(CAN_RX_MSGOBJ, CAN_STD_ID, 0, CAN_LAST_STD_ID, TRUE);
132
 
133
 UART1_PutString("ok");
134
}
135
 
136
void ProcessCanBus(void)
137
{
138
 
139
 CanSend();
140
 CanReceive();
141
if(CAN_GetTransmitErrorCounter() > 200) CanbusInit();
142
 
143
//DebugOut.Analog[16] = CAN->SR ;
144
 
145
if(frame_received_flag)
146
 {
147
  frame_received_flag = 0;
148
  DebugOut.Analog[16] = RxCanMsg.Data[0] * 256 + RxCanMsg.Data[1];
149
 }
150
 
151
}                                        
152
 
153
 
154
 
155
 
156