Subversion Repositories NaviCtrl

Rev

Rev 691 | Rev 696 | Go to most recent revision | Details | Compare with Previous | 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
                CAN_ReleaseTxMessage(msgobj);
78
        break;
79
 
80
      case 1 /* CAN_RX_MSGOBJ */:
81
        CAN_ReceiveMessage(msgobj, FALSE, &RxCanMsg);
82
        CAN_ReleaseRxMessage(msgobj);
83
        frame_received_flag = 1;
84
DebugOut.Analog[19]++; 
85
  break;
86
 
87
      default:
88
        CAN_ReleaseMessage(msgobj);
89
        break;
90
    }
91
  }
92
 
93
   /*write any value to VIC0 VAR*/  
94
   VIC0->VAR = 0xFF;   
95
 
96
}
97
 
98
void CanSend(void)
99
{
100
//  CAN_SetUnusedAllMsgObj();
101
//  CAN_SetTxMsgObj(CAN_TX_MSGOBJ, CAN_STD_ID, DISABLE);
102
//  CAN_SetRxMsgObj(CAN_RX_MSGOBJ, CAN_STD_ID, 0, CAN_LAST_STD_ID, TRUE);
103
//CAN_UpdateMsgObj(CAN_TX_MSGOBJ, &TxCanMsg[1]);
104
  TxCanMsg[1].Data[0] = DebugOut.Analog[24] / 256;
105
  TxCanMsg[1].Data[1] = DebugOut.Analog[24] % 256;
106
  CAN_SendMessage(CAN_TX_MSGOBJ, &TxCanMsg[1]);
107
}
108
 
109
void CanReceive(void)
110
{
111
 
112
}
113
 
114
 
115
void CanbusInit(void)
116
{
117
 UART1_PutString("\r\n Canbus init...");
118
 CAN_IO_Init();
119
 SCU_APBPeriphClockConfig(__CAN, ENABLE);
120
 SCU_APBPeriphReset(__CAN, DISABLE);
121
 
122
 VIC_Config(CAN_ITLine, VIC_IRQ, Priority_1);
123
 CAN_InitStructure.CAN_ConfigParameters=CAN_CR_IE;
124
 CAN_InitStructure.CAN_Bitrate = CAN_BITRATE_1M;
125
 CAN_Init(&CAN_InitStructure);
126
 VIC_ITCmd(CAN_ITLine, ENABLE);
127
 
128
 CAN_SetUnusedAllMsgObj();
129
 CAN_SetTxMsgObj(CAN_TX_MSGOBJ, CAN_STD_ID, DISABLE);
130
 CAN_SetRxMsgObj(CAN_RX_MSGOBJ, CAN_STD_ID, 0, CAN_LAST_STD_ID, TRUE);
131
 
132
 UART1_PutString("ok");
133
}
134
 
135
void ProcessCanBus(void)
136
{
137
 
138
 CanSend();
139
 CanReceive();
140
if(CAN_GetTransmitErrorCounter() > 200) CanbusInit();
141
 
142
//DebugOut.Analog[16] = CAN->SR ;
143
 
144
if(frame_received_flag)
145
 {
146
  frame_received_flag = 0;
147
  DebugOut.Analog[16] = RxCanMsg.Data[0] * 256 + RxCanMsg.Data[1];
148
 }
149
 
150
}                                        
151
 
152
 
153
 
154
 
155