Subversion Repositories HomeAutomation

Rev

Rev 73 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 73 Rev 127
Line 1... Line 1...
1
/**
1
/**
2
 * CAN Interface. This is a general high-level interface for CAN communication.
2
 * CAN Interface. This is a general high-level interface for CAN communication.
3
 * Underlying CAN controllers are abstracted by this interface, so the
3
 * Underlying CAN controllers are abstracted by this interface, so the
4
 * application needs not work directly against the CAN controllers.
4
 * application needs not work directly against the CAN controllers.
5
 *
5
 *
6
 * @date    2006-10-28
6
 * @date    2006-11-19
7
 *
7
 *
8
 * @author  Jimmy Myhrman
8
 * @author  Jimmy Myhrman
9
 * @author  Martin Thomas
9
 * @author  Martin Thomas
10
 *  
10
 *  
11
 */
11
 */
Line 28... Line 28...
28
/*-----------------------------------------------------------------------------
28
/*-----------------------------------------------------------------------------
29
 * Functions
29
 * Functions
30
 *---------------------------------------------------------------------------*/
30
 *---------------------------------------------------------------------------*/
31
 
31
 
32
/**
32
/**
33
 * Initializes the CAN interface.
33
 * Initializes the CAN interface. Edit can_cfg.h to choose bitrate.
34
 *
34
 *
35
 * @param bitrate
-
 
36
 *      The bitrate that should be used.
-
 
37
 * @return
35
 * @return
38
 *      CAN_OK if initialization was successful.
36
 *      CAN_OK if initialization was successful.
39
 *      CAN_FAILINIT otherwise.
37
 *      CAN_FAILINIT otherwise.
40
 */
38
 */
41
CanReturn_t CanInit(const CanBitrate_t bitrate) {
39
Can_Return_t Can_Init() {
42
 
-
 
-
 
40
    Can_Bitrate_t bitrate = CAN_BITRATE;
43
#ifdef CAN_CONTROLLER_MCP2515
41
#ifdef CAN_CONTROLLER_MCP2515
44
    if (mcp2515_init(bitrate) != MCP2515_OK) {
42
    if (MCP2515_Init(bitrate) != MCP2515_OK) {
45
        return CAN_FAILINIT;
43
        return CAN_FAILINIT;
46
    }
44
    }
47
    if (mcp2515_setCANCTRL_Mode(MODE_NORMAL) != MCP2515_OK) {
45
    if (MCP2515_SetCanCtrlMode(MODE_NORMAL) != MCP2515_OK) {
48
        return CAN_FAILINIT;
46
        return CAN_FAILINIT;
49
    }
47
    }
50
    return CAN_OK;
48
    return CAN_OK;
51
#endif
49
#endif
52
 
50
 
Line 63... Line 61...
63
 *      Pointer to the CAN message storage buffer.
61
 *      Pointer to the CAN message storage buffer.
64
 * @return
62
 * @return
65
 *      CAN_OK if the message was successfully sent (or put in queue).
63
 *      CAN_OK if the message was successfully sent (or put in queue).
66
 *      CAN_FAILTX if the controller is busy AND the transmission queue is full.
64
 *      CAN_FAILTX if the controller is busy AND the transmission queue is full.
67
 */
65
 */
68
CanReturn_t CanSend(CanMessage_t* msg) {
66
Can_Return_t Can_Send(Can_Message_t* msg) {
69
 
67
 
70
#ifdef CAN_CONTROLLER_MCP2515
68
#ifdef CAN_CONTROLLER_MCP2515
71
    uint8_t res, txbuf_n;
69
    uint8_t res, txbuf_n;
72
    res = mcp2515_getNextFreeTXBuf(&txbuf_n); // info = addr.
70
    res = MCP2515_GetNextFreeTXBuf(&txbuf_n); // info = addr.
73
    if (res == MCP_ALLTXBUSY) {
71
    if (res == MCP_ALLTXBUSY) {
74
        return CAN_FAILTX;
72
        return CAN_FAILTX;
75
    }
73
    }
76
    mcp2515_write_canMsg(txbuf_n, msg);
74
    MCP2515_WriteCanMsg(txbuf_n, msg);
77
    mcp2515_start_transmit(txbuf_n);
75
    MCP2515_StartTransmit(txbuf_n);
78
    return CAN_OK;
76
    return CAN_OK;
79
#endif
77
#endif
80
 
78
 
81
}
79
}
82
 
80
 
Line 90... Line 88...
90
 *      Pointer to the message storage buffer.
88
 *      Pointer to the message storage buffer.
91
 * @return
89
 * @return
92
 *      CAN_OK if a received message was successfully copied into the buffer.
90
 *      CAN_OK if a received message was successfully copied into the buffer.
93
 *      CAN_NOMSG if there are no received messages available.
91
 *      CAN_NOMSG if there are no received messages available.
94
 */
92
 */
95
CanReturn_t CanReceive(CanMessage_t *msg) {
93
Can_Return_t Can_Receive(Can_Message_t *msg) {
96
   
94
   
97
#ifdef CAN_CONTROLLER_MCP2515
95
#ifdef CAN_CONTROLLER_MCP2515
98
    uint8_t stat, res;
96
    uint8_t stat, res;
99
    stat = mcp2515_readStatus();
97
    stat = MCP2515_ReadStatus();
100
    if (stat & MCP_STAT_RX0IF) {
98
    if (stat & MCP_STAT_RX0IF) {
101
        // Msg in Buffer 0
99
        // Msg in Buffer 0
102
        mcp2515_read_canMsg( MCP_RXBUF_0, msg);
100
        MCP2515_ReadCanMsg( MCP_RXBUF_0, msg);
103
        mcp2515_modifyRegister(MCP_CANINTF, MCP_RX0IF, 0);
101
        MCP2515_ModifyRegister(MCP_CANINTF, MCP_RX0IF, 0);
104
        res = CAN_OK;
102
        res = CAN_OK;
105
    }
103
    }
106
    else if (stat & MCP_STAT_RX1IF) {
104
    else if (stat & MCP_STAT_RX1IF) {
107
        // Msg in Buffer 1
105
        // Msg in Buffer 1
108
        mcp2515_read_canMsg( MCP_RXBUF_1, msg);
106
        MCP2515_ReadCanMsg( MCP_RXBUF_1, msg);
109
        mcp2515_modifyRegister(MCP_CANINTF, MCP_RX1IF, 0);
107
        MCP2515_ModifyRegister(MCP_CANINTF, MCP_RX1IF, 0);
110
        res = CAN_OK;
108
        res = CAN_OK;
111
    }
109
    }
112
    else {
110
    else {
113
        res = CAN_NOMSG;
111
        res = CAN_NOMSG;
114
    }  
112
    }  
Line 123... Line 121...
123
 *
121
 *
124
 * @return
122
 * @return
125
 *      CAN_MSGAVAIL if there are messages available.
123
 *      CAN_MSGAVAIL if there are messages available.
126
 *      CAN_NOMSG if the queue is empty.
124
 *      CAN_NOMSG if the queue is empty.
127
 */
125
 */
128
CanReturn_t CanReceiveAvailable(void) {
126
Can_Return_t Can_ReceiveAvailable(void) {
129
   
127
   
130
#ifdef CAN_CONTROLLER_MCP2515
128
#ifdef CAN_CONTROLLER_MCP2515
131
    uint8_t res;
129
    uint8_t res;
132
    res = mcp2515_readStatus(); // RXnIF in Bit 1 and 0
130
    res = MCP2515_ReadStatus(); // RXnIF in Bit 1 and 0
133
    if ( res & MCP_STAT_RXIF_MASK ) {
131
    if ( res & MCP_STAT_RXIF_MASK ) {
134
        return CAN_MSGAVAIL;
132
        return CAN_MSGAVAIL;
135
    }
133
    }
136
    else {
134
    else {
137
        return CAN_NOMSG;
135
        return CAN_NOMSG;
Line 148... Line 146...
148
 *      CAN_OK if the controller is OK.
146
 *      CAN_OK if the controller is OK.
149
 *      CAN_CTRLERROR if errors have occured.
147
 *      CAN_CTRLERROR if errors have occured.
150
 * @todo
148
 * @todo
151
 *      Styr upp denna funktion lite bättre.
149
 *      Styr upp denna funktion lite bättre.
152
 */
150
 */
153
CanReturn_t CanCheckError(void) {
151
Can_Return_t Can_CheckError(void) {
154
   
152
   
155
#ifdef CAN_CONTROLLER_MCP2515
153
#ifdef CAN_CONTROLLER_MCP2515
156
    uint8_t eflg = mcp2515_readRegister(MCP_EFLG);
154
    uint8_t eflg = MCP2515_ReadRegister(MCP_EFLG);
157
    if (eflg & MCP_EFLG_ERRORMASK) {
155
    if (eflg & MCP_EFLG_ERRORMASK) {
158
        return CAN_CTRLERROR;
156
        return CAN_CTRLERROR;
159
    }
157
    }
160
    else {
158
    else {
161
        return CAN_OK;
159
        return CAN_OK;