/*
j1939_6.c
This file contains the library routines for the J1939 C Library for PIC16
devices. Please refer to the J1939 C Library User Guide for information
on configuring and using this library.
This file requires the following files to be linked:
spi16.c
This file requires the following header files:
pic.h (HI-TECH)
j1939cfg.h
j1939pro.h
j1939_16.h
mcp2515.h
spi16.h
Version Date Description
----------------------------------------------------------------------
v1.00 2003/12/11 Initial release
v1.01 2004/01/28 Corrected Request/Response mechanism
Copyright 2004 Kimberly Otten Software Consulting
*/
#include <pic.h>
#include "j1939cfg.h" // Also includes spi16.h, mcp2515.h, J1939_16.h, and j1939pro.h
// Internal definitions
#define ADDRESS_CLAIM_TX 1
#define ADDRESS_CLAIM_RX 2
// Global variables. Some of these will be visible to the CA.
J1939_RX_QUEUE_BANK unsigned char CA_Name[J1939_DATA_LENGTH];
unsigned char CommandedAddress;
unsigned char CommandedAddressSource;
#ifdef J1939_ACCEPT_CMDADD
J1939_RX_QUEUE_BANK unsigned char CommandedAddressName[J1939_DATA_LENGTH];
#endif
unsigned char ContentionWaitTime;
unsigned char J1939_Address;
J1939_FLAG J1939_Flags;
J1939_TX_QUEUE_BANK J1939_MESSAGE OneMessage;
J1939_RX_QUEUE_BANK unsigned char RXHead;
J1939_RX_QUEUE_BANK unsigned char RXTail;
J1939_RX_QUEUE_BANK unsigned char RXQueueCount;
J1939_RX_QUEUE_BANK J1939_MESSAGE RXQueue[J1939_RX_QUEUE_SIZE];
J1939_TX_QUEUE_BANK unsigned char TXHead;
J1939_TX_QUEUE_BANK unsigned char TXTail;
J1939_TX_QUEUE_BANK unsigned char TXQueueCount;
J1939_TX_QUEUE_BANK J1939_MESSAGE TXQueue[J1939_TX_QUEUE_SIZE];
// Code definitions for common functions, to make it a little easier to read.
#define SELECT_MCP J1939_CS_PIN = 0;
#define UNSELECT_MCP J1939_CS_PIN = 1;
// Function Prototypes
#ifdef J1939_ACCEPT_CMDADD
unsigned char CA_AcceptCommandedAddress( void );
#endif
/*********************************************************************
CompareName
This routine compares the passed in array data NAME with the CA's
current NAME as stored in CA_Name.
Parameters: unsigned char * Array of NAME bytes
Return: -1 - CA_Name is less than the data
0 - CA_Name is equal to the data
1 - CA_Name is greater than the data
*********************************************************************/
#ifndef J1939_POLL_MCP
#pragma interrupt_level 0
#endif
signed char CompareName( J1939_RX_QUEUE_BANK unsigned char *OtherName )
{
unsigned char i;
for (i = 0; (i<J1939_DATA_LENGTH) && (OtherName[i] == CA_Name[i]); i++);
if (i == J1939_DATA_LENGTH)
return 0;
else if (CA_Name[i] < OtherName[i] )
return -1;
else
return 1;
}
/*********************************************************************
CopyName
This routine copies the CA's NAME into the message buffer's data array.
We can afford to make this a function, since another function is always
called after this is done, and we won't be using any additional stack
space.
Parameters: None
Return: None
*********************************************************************/
void CopyName(void)
{
unsigned char i;
for (i=0; i<J1939_DATA_LENGTH; i++)
OneMessage.Msg.Data[i] = CA_Name[i];
}
/*********************************************************************
SetAddressFilter
This routine sets filter 2 to the specified value (destination address).
It is used to allow reception of messages sent to this node specifically
or simply to the global address if this node does not have an address
(Address will be J1939_GLOBAL_ADDRESS).
NOTE: We can only use one stack level from here, so MCP_Write calls
have been replaced by their equivalent inline code.
Parameters: unsigned char J1939 Address of this CA
Return: None
*********************************************************************/
#ifndef J1939_POLL_MCP
#pragma interrupt_level 0
#endif
void SetAddressFilter( unsigned char Address )
{
unsigned char Status;
SELECT_MCP;
#ifdef SPI_USE_ONLY_INLINE_DEFINITIONS
WRITESPI( MCP_WRITE );
WRITESPI( MCP_CANCTRL );
WRITESPI( MODE_CONFIG + J1939_CLKOUT + J1939_CLKOUT_PS );
#else
WriteSPI( MCP_WRITE );
WriteSPI( MCP_CANCTRL );
WriteSPI( MODE_CONFIG + J1939_CLKOUT + J1939_CLKOUT_PS );
#endif
UNSELECT_MCP;
// MCP_Write( MCP_CANCTRL, MODE_CONFIG + J1939_CLKOUT + J1939_CLKOUT_PS );
do
{
SELECT_MCP;
#ifdef SPI_USE_ONLY_INLINE_DEFINITIONS
WRITESPI( MCP_READ );
WRITESPI( MCP_CANSTAT );
READSPI( Status );
#else
WriteSPI( MCP_READ );
WriteSPI( MCP_CANSTAT );
Status = ReadSPI();
#endif
UNSELECT_MCP;
} while ((Status & MODE_MASK) != MODE_CONFIG);
SELECT_MCP;
#ifdef SPI_USE_ONLY_INLINE_DEFINITIONS
WRITESPI( MCP_WRITE );
WRITESPI( MCP_RXF2EID8 );
WRITESPI( Address );
#else
WriteSPI( MCP_WRITE );
WriteSPI( MCP_RXF2EID8 );
WriteSPI( Address );
#endif
UNSELECT_MCP;
SELECT_MCP;
#ifdef SPI_USE_ONLY_INLINE_DEFINITIONS
WRITESPI( MCP_WRITE );
WRITESPI( MCP_CANCTRL );
WRITESPI( MODE_NORMAL + J1939_CLKOUT + J1939_CLKOUT_PS );
#else
WriteSPI( MCP_WRITE );
WriteSPI( MCP_CANCTRL );
WriteSPI( MODE_NORMAL + J1939_CLKOUT + J1939_CLKOUT_PS );
#endif
UNSELECT_MCP;
// MCP_Write( MCP_CANCTRL, MODE_NORMAL + J1939_CLKOUT + J1939_CLKOUT_PS );
}
/*********************************************************************
MCP_Modify
This function modifies the designated MCP address.
Parameters: unsigned char Address Address of the MCP register.
unsigned char Mask Bits to modify in the register
unsigned char Data Data value for the MCP register.
Return: None
*********************************************************************/
/*
This function has been brought in-line to save stack space.
void MCP_Modify( unsigned char Address, unsigned char Mask, unsigned char Data )
{
SELECT_MCP;
WriteSPI( MCP_BITMOD );
WriteSPI( Address );
WriteSPI( Mask );
WriteSPI( Data );
UNSELECT_MCP;
}
*/
/*********************************************************************
MCP_Write
This function writes a value to the designated MCP address.
Parameters: unsigned char Address Address of the MCP register.
unsigned char Data Data value for the MCP register.
Return: None
*********************************************************************/
void MCP_Write( unsigned char Address, unsigned char Data )
{
SELECT_MCP;
#ifdef SPI_USE_ONLY_INLINE_DEFINITIONS
WRITESPI( MCP_WRITE );
WRITESPI( Address );
WRITESPI( Data );
#else
WriteSPI( MCP_WRITE );
WriteSPI( Address );
WriteSPI( Data );
#endif
UNSELECT_MCP;
}
/*********************************************************************
SendOneMessage
This routine sends the message located at the pointer passed in. It
also uses the message's data length field to determine how much of the
data to load. At this point, all of the data fields, such as data
length, priority, and source address, must be set. This routine will
set up the CAN bits, such as the extended identifier bit and the
remote transmission request bit.
NOTE: Only transmit buffers 0 and 1 are used, to guarantee that the
messages appear on the bus in the order that they are sent to the
MCP2515.
Parameters: J1939_MESSAGE far * Pointer to message to send
Return: None
*********************************************************************/
#ifndef J1939_POLL_MCP
#pragma interrupt_level 0
#endif
void SendOneMessage( J1939_TX_QUEUE_BANK J1939_MESSAGE *MsgPtr )
{
// unsigned char oldSIDL;
unsigned char MCP_Load;
unsigned char Loop;
unsigned char MCP_Send;
unsigned char Temp;
// Set up the final pieces of the message and make sure DataLength isn't
// out of spec.
MsgPtr->Msg.Res = 0;
MsgPtr->Msg.RTR = 0;
if
cn_2515_1939_1.01.zip_Caterpillar Inc._J1939 C_J1939-01_SAE J193
版权申诉
172 浏览量
2022-07-14
13:16:16
上传
评论
收藏 17KB ZIP 举报
![avatar](https://profile-avatar.csdnimg.cn/3b38fb294f114a0a8dfd7bc633aed231_weixin_42660494.jpg!1)
alvarocfc
- 粉丝: 112
- 资源: 1万+
最新资源
- 优先编码器除法电微分运算电路 全加器函数发生电路等电路经典Multisim仿真实验源文件合集(25个).zip
- 2331308JS课堂案例.zip
- STM32H750VBT6单片机最小系统开发板AD设计硬件(原理图+PCB+3D封装库)工程文件.zip
- 基于74LS161+ 74LS192芯片实现倒计时定时器Multisim仿真源文件,Multisim10以上版本可打开运行
- 科大讯飞语音引擎 jar包 demo,科大讯飞语音合成引擎3.0,支持4.0系统以上,文字转语音输出.zip
- Java架构面试笔试专题资料及经验(含答案)SpringBoot面试Linux面试专题及答案 合集.zip
- 头歌c语言实验答案tion-model-for-ne开发笔记
- docker配置使用-model-for-networK开发demo
- docker配置使用vaWeb-mas笔记
- c语言连接两个字符串-mas开发笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)