📄 canop.c
字号:
/***************************************************************************
canop.c - description
-------------------
begin : Fri May 17 2002
copyright : (C) 2002 by Raphael Zulliger
email : zulli@hsr.ch
***************************************************************************/
/***************************************************************************
* *
* This library is Copyright (c) Raphael Zulliger <zulli@gmx.net>. *
* It is licensed under the GNU Library General Public License (LGPL). *
* *
***************************************************************************/
/***************************************************************************
* *
* *
***************************************************************************/
#include <canop.h>
#include <can18xx8.h>
#include <p18c658.h>
#define FILL_IN_DATA { FALSE, 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 }, }
s_rx_buffer_message RAM canMessageRxBuffer[RX_CAN_BUFFER_SIZE]; // !!!!!! this var should really be initialized!
#undef FILL_IN_DATA
void setupCanInterface( sInitParameter* pParameter )
{
BYTE bBrp;
// assuming all values (SJW, PHSEG1/2, Propseg are ok like this, you can
// calculate the value of BRP following:
// BRP = Fosc/(2*8*Baudrate), where [Baudrate] = bit/s
// because wProcSpeed and bBaudRateIndex are divided by 1000 (e.g. 4Mhz = 4000, 125Kbit = 125)
// this equation should be correct
bBrp = (DWORD)(pParameter->wProcSpeed) / (DWORD)(16 * pParameter->wBaudRateIndex);
CANInitialize(1, bBrp, 3, 3, 1, CAN_CONFIG_ALL_VALID_MSG );
CANSetOperationMode(CAN_OP_MODE_NORMAL);
}
void closeCanInterface( void )
{
}
BYTE canSend(BYTE notused, Message *m)
{
BOOL cs_res;
while( !CANIsTxReady( ) );
cs_res = CANSendMessage( m->cob_id.w, // 0x201 ist f黵 das Beckhoff-Modul
&m->data[0],
m->len,
CAN_TX_PRIORITY_0 & CAN_TX_STD_FRAME & CAN_TX_NO_RTR_FRAME );
if( cs_res == TRUE )
return (BYTE)0x01;
else
return (BYTE)0x00;
}
BYTE canReceive(BYTE notused, Message *m)
{
BYTE cr_res = (BYTE)0x01; // 0x01 means failure;
enum CAN_RX_MSG_FLAGS msgFlags;
unsigned long cr_id;
BYTE cr_data[8];
BYTE cr_len;
if( CANIsRxReady( ) )
{
if( CANReceiveMessage( &cr_id,
cr_data,
&cr_len,
&msgFlags ) == TRUE )
// why doesn't this work!?
// if( CANReceiveMessage( (unsigned long*)&m->cob_id.w,
// (BYTE*)&m->data[0],
// (BYTE*)&m->len,
// &msgFlags ) == TRUE )
{
m->len = cr_len;
m->cob_id.w = cr_id;
// doesn't work like this?!
// memcpy( (void*)m->data, (void*)data, len );
memcpy( (void*)&m->data[0], (void*)&cr_data[0], cr_len );
if( !(msgFlags & CAN_RX_INVALID_MSG) )
cr_res = (BYTE)0x00;
}
}
return cr_res;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -