1
0
mirror of https://github.com/elua/elua.git synced 2025-01-08 20:56:17 +08:00

Merge pull request #22 from mauriciobomfim/master

Added CAN support to STR912 plus MMCFS pin configuration. Tested on Olimex STR9-E912.
This commit is contained in:
Gabriel Duarte 2013-07-09 08:33:25 -07:00
commit f71d8b4329
9 changed files with 1277 additions and 17 deletions

View File

@ -12,13 +12,14 @@ return {
linenoise = { shell_lines = 10, lua_lines = 50 },
rpc = { uart = 0, speed = 115200 },
adc = { buf_size = 2 },
xmodem = true
xmodem = true,
mmcfs = { spi = 0, cs_port = 2, cs_pin = 7 },
},
config = {
vtmr = { num = 4, freq = 16 },
},
modules = {
generic = { 'all', '-net', '-can' },
generic = { 'all', '-net' },
platform = 'all'
}
}

932
src/platform/str9/91x_can.c Normal file
View File

@ -0,0 +1,932 @@
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* File Name : 91x_can.c
* Author : MCD Application Team
* Version : V2.1
* Date : 12/22/2008
* Description : This file provides all the CAN firmware functions.
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "91x_can.h"
#include "91x_scu.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/* Macro Name : xxx_ID_MSK, xxx_ID_ARB */
/* Description : Form the Mask and Arbitration registers value to filter */
/* a range of identifiers or a fixed identifier, for standard*/
/* and extended IDs */
/*----------------------------------------------------------------------------*/
#define RANGE_ID_MSK(range_start, range_end) (~((range_end) - (range_start)))
#define RANGE_ID_ARB(range_start, range_end) ((range_start) & (range_end))
#define FIXED_ID_MSK(id) RANGE_ID_MSK((id), (id))
#define FIXED_ID_ARB(id) RANGE_ID_ARB((id), (id))
#define STD_RANGE_ID_MSK(range_start, range_end) ((u16)((RANGE_ID_MSK((range_start), (range_end)) & 0x7FF) << 2))
#define STD_RANGE_ID_ARB(range_start, range_end) ((u16)(RANGE_ID_ARB((range_start), (range_end)) << 2))
#define STD_FIXED_ID_MSK(id) ((u16)((FIXED_ID_MSK(id) & 0x7FF) << 2))
#define STD_FIXED_ID_ARB(id) ((u16)(FIXED_ID_ARB(id) << 2))
#define EXT_RANGE_ID_MSK_L(range_start, range_end) \
((u16)(RANGE_ID_MSK((range_start), (range_end))))
#define EXT_RANGE_ID_MSK_H(range_start, range_end) \
((u16)(RANGE_ID_MSK((range_start), (range_end)) >> 16) & 0x1FFF)
#define EXT_RANGE_ID_ARB_L(range_start, range_end) \
((u16)(RANGE_ID_ARB((range_start), (range_end))))
#define EXT_RANGE_ID_ARB_H(range_start, range_end) \
((u16)(RANGE_ID_ARB((range_start), (range_end)) >> 16) & 0x1FFF)
#define EXT_FIXED_ID_MSK_L(id) ((u16)(FIXED_ID_MSK(id)))
#define EXT_FIXED_ID_MSK_H(id) ((u16)(FIXED_ID_MSK(id) >> 16 ) & 0x1FFF)
#define EXT_FIXED_ID_ARB_L(id) ((u16)(FIXED_ID_ARB(id)))
#define EXT_FIXED_ID_ARB_H(id) ((u16)(FIXED_ID_ARB(id) >> 16) & 0x1FFF)
/* macro to format the timing register value from the timing parameters*/
#define CAN_TIMING(tseg1, tseg2, sjw, brp) ((((tseg2-1) & 0x07) << 12) | (((tseg1-1) & 0x0F) << 8) | (((sjw-1) & 0x03) << 6) | ((brp-1) & 0x3F))
/* Private variables ---------------------------------------------------------*/
/* array of pre-defined timing parameters for standard bitrates*/
u16 CanTimings[] = { /* value bitrate NTQ TSEG1 TSEG2 SJW BRP */
CAN_TIMING(11, 4, 4, 5), /* 0x3AC4 100 kbit/s 16 11 4 4 5 */
CAN_TIMING(11, 4, 4, 4), /* 0x3AC3 125 kbit/s 16 11 4 4 4 */
CAN_TIMING( 4, 3, 3, 4), /* 0x2383 250 kbit/s 8 4 3 3 4 */
CAN_TIMING(13, 2, 1, 1), /* 0x1C00 500 kbit/s 16 13 2 1 1 */
CAN_TIMING( 4, 3, 1, 1), /* 0x2300 1 Mbit/s 8 4 3 1 1 */
};
/* Private function prototypes -----------------------------------------------*/
static u32 GetFreeIF(void);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : CAN_DeInit
* Description : Deinitializes the CAN peripheral registers to their default
* reset values.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAN_DeInit (void)
{
/* Reset the CAN registers values*/
SCU_APBPeriphReset(__CAN,ENABLE); /*CAN peripheral is under Reset */
SCU_APBPeriphReset(__CAN,DISABLE); /*CAN peripheral Reset off*/
}
/*******************************************************************************
* Function Name : CAN_Init
* Description : Initializes the CAN peripheral according to the specified
* parameters in the CAN_InitStruct.
* Input : CAN_InitStruct: pointer to a CAN_InitTypeDef structure that
* contains the configuration information for the CAN peripheral.
* Output : None
* Return : None
*******************************************************************************/
void CAN_Init(CAN_InitTypeDef* CAN_InitStruct)
{
CAN_EnterInitMode(CAN_CR_CCE | CAN_InitStruct->CAN_ConfigParameters);
CAN_SetBitrate(CAN_InitStruct->CAN_Bitrate);
CAN_LeaveInitMode();
CAN_LeaveTestMode();
}
/*******************************************************************************
* Function Name : CAN_StructInit
* Description : Fills each CAN_InitStruct member with its reset value.
* Input : CAN_InitStruct : pointer to a CAN_InitTypeDef structure which
* will be initialized.
* Output : None
* Return : None.
*******************************************************************************/
void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
{
/* Reset CAN init structure parameters values */
CAN_InitStruct->CAN_ConfigParameters = 0x0;
CAN_InitStruct->CAN_Bitrate = 0x2301;
}
/*******************************************************************************
* Function Name : CAN_SetBitrate
* Description : Setups a standard CAN bitrate.
* Input : bitrate: specifies the bit rate.
* Output : None
* Return : None
*******************************************************************************/
void CAN_SetBitrate(u32 bitrate)
{
CAN->BTR = CanTimings[bitrate]; /* write the predefined timing value */
CAN->BRPR = 0; /* clear the Extended Baud Rate Prescaler */
}
/*******************************************************************************
* Function Name : CAN_SetTiming
* Description : Setups the CAN timing with specific parameters
* Input : - tseg1: specifies Time Segment before the sample point.
* This parameter must be a number between 1 and 16.
* - tseg2: Time Segment after the sample point. This parameter
* must be a number between 1 and 8.
* - sjw: Synchronisation Jump Width. This parameter must be
* a number between 1 and 4.
* - brp: Baud Rate Prescaler. This parameter must be a number
* between 1 and 1024.
* Output : None
* Return : None
*******************************************************************************/
void CAN_SetTiming(u32 tseg1, u32 tseg2, u32 sjw, u32 brp)
{
CAN->BTR = CAN_TIMING(tseg1, tseg2, sjw, brp);
CAN->BRPR = ((brp-1) >> 6) & 0x0F;
}
/*******************************************************************************
* Function Name : GetFreeIF
* Description : Searchs the first free message interface, starting from 0.
* Input : None
* Output : None
* Return : A free message interface number (0 or 1) if found, else 2
*******************************************************************************/
static u32 GetFreeIF(void)
{
if ((CAN->sMsgObj[0].CRR & CAN_CRR_BUSY) == 0)
return 0;
else if ((CAN->sMsgObj[1].CRR & CAN_CRR_BUSY) == 0)
return 1;
else
return 2;
}
/*******************************************************************************
* Function Name : CAN_SetUnusedMsgObj
* Description : Configures the message object as unused
* Input : msgobj: specifies the Message object number, from 0 to 31.
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Interface to treat the message
* - ERROR: No interface to treat the message
*******************************************************************************/
ErrorStatus CAN_SetUnusedMsgObj(u32 msgobj)
{
u32 msg_if=0;
if ((msg_if = GetFreeIF()) == 2)
{
return ERROR;
}
CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
| CAN_CMR_MASK
| CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
CAN->sMsgObj[msg_if].M1R = 0;
CAN->sMsgObj[msg_if].M2R = 0;
CAN->sMsgObj[msg_if].A1R = 0;
CAN->sMsgObj[msg_if].A2R = 0;
CAN->sMsgObj[msg_if].MCR = 0;
CAN->sMsgObj[msg_if].DA1R = 0;
CAN->sMsgObj[msg_if].DA2R = 0;
CAN->sMsgObj[msg_if].DB1R = 0;
CAN->sMsgObj[msg_if].DB2R = 0;
CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
return SUCCESS;
}
/*******************************************************************************
* Function Name : CAN_SetTxMsgObj
* Description : Configures the message object as TX.
* Input : - msgobj: specifies the Message object number, from 0 to 31.
* - idType: specifies the identifier type of the frames that
* will be transmitted using this message object.
* This parameter can be one of the following values:
* - CAN_STD_ID (standard ID, 11-bit)
* - CAN_EXT_ID (extended ID, 29-bit)
* - RemoteEN : specifies if the CAN message will answer remote
* frames with exactly matching ID; It can be
* - ENABLE : remote frame with matching ID is answered
* - DISABLE : remote frames with matching ID is not
* answered
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Interface to treat the message
* - ERROR: No interface to treat the message
*******************************************************************************/
ErrorStatus CAN_SetTxMsgObj(u32 msgobj, u32 idType, FunctionalState RemoteEN)
{
u32 msg_if=0;
if ((msg_if = GetFreeIF()) == 2)
{
return ERROR;
}
CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
| CAN_CMR_MASK
| CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
CAN->sMsgObj[msg_if].M1R = 0xFFFF;
CAN->sMsgObj[msg_if].A1R = 0;
if (idType == CAN_STD_ID)
{
CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR | 0x1FFF;
CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR;
}
else
{
CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR | CAN_M2R_MXTD | 0x1FFF;
CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR | CAN_A2R_XTD;
}
CAN->sMsgObj[msg_if].MCR = CAN_MCR_TXIE | CAN_MCR_EOB | ( RemoteEN ? CAN_MCR_RMTEN : 0)
| ( RemoteEN ? CAN_MCR_UMASK : 0);
CAN->sMsgObj[msg_if].DA1R = 0;
CAN->sMsgObj[msg_if].DA2R = 0;
CAN->sMsgObj[msg_if].DB1R = 0;
CAN->sMsgObj[msg_if].DB2R = 0;
CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
return SUCCESS;
}
/*******************************************************************************
* Function Name : CAN_SetRxMsgObj
* Description : Configures the message object as RX.
* Input : - msgobj: specifies the Message object number, from 0 to 31.
* - idType: specifies the identifier type of the frames that
* will be transmitted using this message object.
* This parameter can be one of the following values:
* - CAN_STD_ID (standard ID, 11-bit)
* - CAN_EXT_ID (extended ID, 29-bit)
* - idLow: specifies the low part of the identifier range used
* for acceptance filtering.
* - idHigh: specifies the high part of the identifier range
* used for acceptance filtering.
* - singleOrFifoLast: specifies the end-of-buffer indicator.
* This parameter can be one of the following values:
* - TRUE: for a single receive object or a FIFO receive
* object that is the last one of the FIFO.
* - FALSE: for a FIFO receive object that is not the
* last one.
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Interface to treat the message
* - ERROR: No interface to treat the message
*******************************************************************************/
ErrorStatus CAN_SetRxMsgObj(u32 msgobj, u32 idType, u32 idLow, u32 idHigh, bool singleOrFifoLast)
{
u32 msg_if=0;
if ((msg_if = GetFreeIF()) == 2)
{
return ERROR;
}
CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
| CAN_CMR_MASK
| CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
if (idType == CAN_STD_ID)
{
CAN->sMsgObj[msg_if].M1R = 0xFFFF;
CAN->sMsgObj[msg_if].M2R = CAN_M2R_MXTD | STD_RANGE_ID_MSK(idLow, idHigh);
CAN->sMsgObj[msg_if].A1R = 0;
CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | STD_RANGE_ID_ARB(idLow, idHigh);
}
else
{
CAN->sMsgObj[msg_if].M1R = EXT_RANGE_ID_MSK_L(idLow, idHigh);
CAN->sMsgObj[msg_if].M2R = CAN_M2R_MXTD | EXT_RANGE_ID_MSK_H(idLow, idHigh);
CAN->sMsgObj[msg_if].A1R = EXT_RANGE_ID_ARB_L(idLow, idHigh);
CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_XTD | EXT_RANGE_ID_ARB_H(idLow, idHigh);
}
CAN->sMsgObj[msg_if].MCR = CAN_MCR_RXIE | CAN_MCR_UMASK | (singleOrFifoLast ? CAN_MCR_EOB : 0);
CAN->sMsgObj[msg_if].DA1R = 0;
CAN->sMsgObj[msg_if].DA2R = 0;
CAN->sMsgObj[msg_if].DB1R = 0;
CAN->sMsgObj[msg_if].DB2R = 0;
CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
return SUCCESS;
}
/*******************************************************************************
* Function Name : CAN_SetUnusedAllMsgObj
* Description : Configures all the message objects as unused.
* Input : None
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Interface to treat the message
* - ERROR: No interface to treat the messageNone
*******************************************************************************/
ErrorStatus CAN_SetUnusedAllMsgObj(void)
{
u32 i=0;
for (i = 0; i < 32; i++)
{
if ( CAN_SetUnusedMsgObj(i) == ERROR)
return ERROR;
}
return SUCCESS;
}
/*******************************************************************************
* Function Name : CAN_ReleaseMessage
* Description : Releases the message object
* Input : - msgobj: specifies the Message object number, from 0 to 31.
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Interface to treat the message
* - ERROR: No interface to treat the message
*******************************************************************************/
ErrorStatus CAN_ReleaseMessage(u32 msgobj)
{
u32 msg_if=0;
if ((msg_if = GetFreeIF()) == 2)
{
return ERROR;
}
CAN->sMsgObj[msg_if].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
return SUCCESS;
}
/*******************************************************************************
* Function Name : CAN_UpdateMsgObj
* Description : Updates the CAN message object with the pCanMsg fields, it
* does not start the transmission of the CAN message object
* Input 1 : message object number, from 0 to 31
* Input 2 : pointer to the message structure containing data to transmit
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Interface to treat the message
* - ERROR: No interface to treat the message
*******************************************************************************/
ErrorStatus CAN_UpdateMsgObj(u32 msgobj, canmsg* pCanMsg)
{
if (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
return ERROR;
/* read the Arbitration and Message Control */
CAN->sMsgObj[0].CMR = CAN_CMR_ARB | CAN_CMR_CONTROL;
CAN->sMsgObj[0].CRR = 1 + msgobj;
while (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
{
/*Wait*/
}
/* update the contents needed for transmission */
CAN->sMsgObj[0].CMR = CAN_CMR_WRRD
| CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
if ((CAN->sMsgObj[0].A2R & CAN_A2R_XTD) == 0)
{
/* standard ID */
CAN->sMsgObj[0].A1R = 0;
CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000)
| STD_FIXED_ID_ARB(pCanMsg->Id);
}
else
{
/* extended ID*/
CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000)
| EXT_FIXED_ID_ARB_H(pCanMsg->Id);
}
CAN->sMsgObj[0].MCR = (CAN->sMsgObj[0].MCR & 0xFEF0) | CAN_MCR_NEWDAT
| pCanMsg->Dlc;
CAN->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1] << 8) | pCanMsg->Data[0];
CAN->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3] << 8) | pCanMsg->Data[2];
CAN->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5] << 8) | pCanMsg->Data[4];
CAN->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7] << 8) | pCanMsg->Data[6];
CAN->sMsgObj[0].CRR = 1 + msgobj;
while ( CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
{
/* wait */
}
return SUCCESS;
}
/*******************************************************************************
* Function Name : CAN_SendMessage
* Description : Start transmission of a message
* Input : - msgobj: specifies the Message object number, from 0 to 31.
* : - pCanMsg: pointer to the message structure containing data
* to transmit.
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Transmission OK
* - ERROR: No transmission
*******************************************************************************/
ErrorStatus CAN_SendMessage(u32 msgobj, canmsg* pCanMsg)
{
if (CAN_UpdateMsgObj(msgobj, pCanMsg) == ERROR)
return ERROR;
CAN->SR &= ~CAN_SR_TXOK;
return ( CAN_TransmitRequest( msgobj));
}
/*******************************************************************************
* Function Name : CAN_TransmitRequest
* Description : This function requests the transmission of a message object
* Input : msgobj: number of the message object that should be
* transmitted
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Transmission OK
* - ERROR: No transmission started
*******************************************************************************/
ErrorStatus CAN_TransmitRequest( u32 msgobj )
{
u16 msg_if;
if ((msg_if = GetFreeIF()) == 2)
return ERROR;
/* Set the transmit request in the command mask register */
CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD | CAN_CMR_TXRQSTNEWDAT;
/* Write the message object number in the command request register */
CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
return SUCCESS;
}
/*******************************************************************************
* Function Name : CAN_ReceiveMessage
* Description : Gets the message, if received.
* Input : - msgobj: specifies the Message object number, from 0 to 31.
* - release: specifies the message release indicator.
* This parameter can be one of the following values:
* - TRUE: the message object is released when getting
* the data.
* - FALSE: the message object is not released.
* - pCanMsg: pointer to the message structure where received
* data is copied.
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Reception OK
* - ERROR: No message pending
*******************************************************************************/
ErrorStatus CAN_ReceiveMessage(u32 msgobj, bool release, canmsg* pCanMsg)
{
u32 tempId;
if (!CAN_GetMsgReceiveStatus(msgobj))
{
return ERROR;
}
CAN->SR &= ~CAN_SR_RXOK;
/* read the message contents*/
CAN->sMsgObj[1].CMR = CAN_CMR_MASK
| CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_CLRINTPND
| (release ? CAN_CMR_TXRQSTNEWDAT : 0)
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
CAN->sMsgObj[1].CRR = 1 + msgobj;
if (CAN->sMsgObj[1].CRR & CAN_CRR_BUSY)
{
return ERROR;
}
if ((CAN->sMsgObj[1].A2R & CAN_A2R_XTD) == 0)
{
/* standard ID*/
pCanMsg->IdType = CAN_STD_ID;
pCanMsg->Id = (CAN->sMsgObj[1].A2R >> 2) & 0x07FF;
}
else
{
/* extended ID*/
pCanMsg->IdType = CAN_EXT_ID;
tempId = ((u32)(CAN->sMsgObj[1].A2R & 0x1FFF) << 16);
pCanMsg->Id = CAN->sMsgObj[1].A1R | tempId;
}
pCanMsg->Dlc = CAN->sMsgObj[1].MCR & 0x0F;
pCanMsg->Data[0] = (u8) CAN->sMsgObj[1].DA1R;
pCanMsg->Data[1] = (u8)(CAN->sMsgObj[1].DA1R >> 8);
pCanMsg->Data[2] = (u8) CAN->sMsgObj[1].DA2R;
pCanMsg->Data[3] = (u8)(CAN->sMsgObj[1].DA2R >> 8);
pCanMsg->Data[4] = (u8) CAN->sMsgObj[1].DB1R;
pCanMsg->Data[5] = (u8)(CAN->sMsgObj[1].DB1R >> 8);
pCanMsg->Data[6] = (u8) CAN->sMsgObj[1].DB2R;
pCanMsg->Data[7] = (u8)(CAN->sMsgObj[1].DB2R >> 8);
return SUCCESS;
}
/*******************************************************************************
* Function Name : CAN_WaitEndOfTx
* Description : Waits until current transmission is finished.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAN_WaitEndOfTx(void)
{
while ((CAN->SR & CAN_SR_TXOK) == 0)
{
/*Wait*/
}
CAN->SR &= ~CAN_SR_TXOK;
}
/*******************************************************************************
* Function Name : CAN_BasicSendMessage
* Description : Starts transmission of a message in BASIC mode. This mode
* does not use the message RAM.
* Input : pCanMsg: Pointer to the message structure containing data to
* transmit.
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Transmission OK
* - ERROR: No transmission
*******************************************************************************/
ErrorStatus CAN_BasicSendMessage(canmsg* pCanMsg)
{
/* clear NewDat bit in IF2 to detect next reception*/
CAN->sMsgObj[1].MCR &= ~CAN_MCR_NEWDAT;
CAN->SR &= ~CAN_SR_TXOK;
CAN->sMsgObj[0].CMR = CAN_CMR_WRRD
| CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
if (pCanMsg->IdType == CAN_STD_ID)
{
/* standard ID*/
CAN->sMsgObj[0].A1R = 0;
CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | STD_FIXED_ID_ARB(pCanMsg->Id);
}
else
{
/* extended ID */
CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | CAN_A2R_XTD | EXT_FIXED_ID_ARB_H(pCanMsg->Id);
}
CAN->sMsgObj[0].MCR = (CAN->sMsgObj[0].MCR & 0xFCF0) | pCanMsg->Dlc;
CAN->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1]<<8) | pCanMsg->Data[0];
CAN->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3]<<8) | pCanMsg->Data[2];
CAN->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5]<<8) | pCanMsg->Data[4];
CAN->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7]<<8) | pCanMsg->Data[6];
/* request the transmission*/
CAN->sMsgObj[0].CRR = CAN_CRR_BUSY;
return SUCCESS;
}
/*******************************************************************************
* Function Name : CAN_BasicReceiveMessage
* Description : Gets the message in BASIC mode, if received. This mode does
* not use the message RAM.
* Input : pCanMsg: pointer to the message structure where message is copied.
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Reception OK
* - ERROR: No message pending
*******************************************************************************/
ErrorStatus CAN_BasicReceiveMessage(canmsg* pCanMsg)
{
u32 tmpId;
if ((CAN->sMsgObj[1].MCR & CAN_MCR_NEWDAT) == 0)
{
return ERROR;
}
CAN->SR &= ~CAN_SR_RXOK;
CAN->sMsgObj[1].CMR = CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
if ((CAN->sMsgObj[1].A2R & CAN_A2R_XTD) == 0)
{
/* standard ID*/
pCanMsg->IdType = CAN_STD_ID;
pCanMsg->Id = (CAN->sMsgObj[1].A2R >> 2) & 0x07FF;
}
else
{
/* extended ID*/
pCanMsg->IdType = CAN_EXT_ID;
tmpId = ((u32)(CAN->sMsgObj[1].A2R & 0x1FFF) << 16);
pCanMsg->Id = CAN->sMsgObj[1].A1R | tmpId;
}
pCanMsg->Dlc = CAN->sMsgObj[1].MCR & 0x0F;
pCanMsg->Data[0] = (u8) CAN->sMsgObj[1].DA1R;
pCanMsg->Data[1] = (u8)(CAN->sMsgObj[1].DA1R >> 8);
pCanMsg->Data[2] = (u8) CAN->sMsgObj[1].DA2R;
pCanMsg->Data[3] = (u8)(CAN->sMsgObj[1].DA2R >> 8);
pCanMsg->Data[4] = (u8) CAN->sMsgObj[1].DB1R;
pCanMsg->Data[5] = (u8)(CAN->sMsgObj[1].DB1R >> 8);
pCanMsg->Data[6] = (u8) CAN->sMsgObj[1].DB2R;
pCanMsg->Data[7] = (u8)(CAN->sMsgObj[1].DB2R >> 8);
return SUCCESS;
}
/*******************************************************************************
* Function Name : CAN_EnterInitMode
* Description : Switchs the CAN into initialization mode. This function must
* be used in conjunction with CAN_LeaveInitMode().
* Input : InitMask: specifies the CAN configuration in normal mode.
* Output : None
* Return : None
*******************************************************************************/
void CAN_EnterInitMode(u8 InitMask)
{
CAN->CR = InitMask | CAN_CR_INIT;
CAN->SR = 0; /* reset the status*/
}
/*******************************************************************************
* Function Name : CAN_LeaveInitMode
* Description : Leaves the initialization mode (switch into normal mode).
* This function must be used in conjunction with CAN_EnterInitMode().
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAN_LeaveInitMode(void)
{
CAN->CR &= ~(CAN_CR_INIT | CAN_CR_CCE);
}
/*******************************************************************************
* Function Name : CAN_EnterTestMode
* Description : Switchs the CAN into test mode. This function must be used in
* conjunction with CAN_LeaveTestMode().
* Input : TestMask: specifies the configuration in test modes.
* Output : None
* Return : None
*******************************************************************************/
void CAN_EnterTestMode(u8 TestMask)
{
CAN->CR |= CAN_CR_TEST;
CAN->TESTR |= TestMask;
}
/*******************************************************************************
* Function Name : CAN_LeaveTestMode
* Description : Leaves the current test mode (switch into normal mode).
* This function must be used in conjunction with CAN_EnterTestMode().
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAN_LeaveTestMode(void)
{
CAN->CR |= CAN_CR_TEST;
CAN->TESTR &= ~(CAN_TESTR_LBACK | CAN_TESTR_SILENT | CAN_TESTR_BASIC);
CAN->CR &= ~CAN_CR_TEST;
}
/*******************************************************************************
* Function Name : CAN_ReleaseTxMessage
* Description : Releases the transmit message object.
* Input : - msgobj: specifies the Message object number, from 0 to 31.
* Output : None
* Return : None
*******************************************************************************/
void CAN_ReleaseTxMessage(u32 msgobj)
{
CAN->sMsgObj[0].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
CAN->sMsgObj[0].CRR = 1 + msgobj;
}
/*******************************************************************************
* Function Name : CAN_ReleaseRxMessage
* Description : Releases the receive message object.
* Input : - msgobj: specifies the Message object number, from 0 to 31.
* Output : None
* Return : None
*******************************************************************************/
void CAN_ReleaseRxMessage(u32 msgobj)
{
CAN->sMsgObj[1].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
CAN->sMsgObj[1].CRR = 1 + msgobj;
}
/*******************************************************************************
* Function Name : CAN_GetMsgReceiveStatus
* Description : Test the waiting status of a received message
* Input 1 : message object number, from 0 to 31
* Output : None
* Return : SET value if the corresponding message object has
* received a message waiting to be copied, else RESET
*******************************************************************************/
FlagStatus CAN_GetMsgReceiveStatus(u32 msgobj)
{
if( msgobj < 16 )
{
if ( CAN->ND1R & (1 << msgobj) )
return SET;
else
return RESET;
}
else
{
if ( CAN->ND2R & (1 << (msgobj - 16) ) )
return SET;
else
return RESET;
}
}
/*******************************************************************************
* Function Name : CAN_GetMsgTransmitRequestStatus
* Description : Test the request status of a transmitted message
* Input 1 : message object number, from 0 to 31
* Output : None
* Return : SET if the corresponding message is requested
* to transmit, else RESET
*******************************************************************************/
FlagStatus CAN_GetMsgTransmitRequestStatus(u32 msgobj)
{
if( msgobj < 16 )
{
if ( CAN->TXR1R & (1 << msgobj) )
return SET;
else
return RESET;
}
else
{
if ( CAN->TXR2R & (1 << (msgobj - 16) ) )
return SET;
else
return RESET;
}
}
/*******************************************************************************
* Function Name : CAN_GetMsgInterruptStatus
* Description : Test the interrupt status of a message object
* Input 1 : message object number, from 0 to 31
* Output : None
* Return : SET if the corresponding message has an
* interrupt pending, else RESET
*******************************************************************************/
FlagStatus CAN_GetMsgInterruptStatus(u32 msgobj)
{
if( msgobj < 16 )
{
if ( CAN->IP1R & (1 << msgobj) )
return SET;
else
return RESET;
}
else
{
if ( CAN->IP2R & (1 << (msgobj - 16) ) )
return SET;
else
return RESET;
}
}
/*******************************************************************************
* Function Name : CAN_GetMsgValidStatus
* Description : Test the validity of a message object (ready to use)
* Input 1 : message object number, from 0 to 31
* Output : None
* Return : SET if the corresponding message object is valid
* else RESET
*******************************************************************************/
FlagStatus CAN_GetMsgValidStatus(u32 msgobj)
{
if( msgobj < 16 )
{
if ( CAN->MV1R & (1 << msgobj) )
return SET;
else
return RESET;
}
else
{
if ( CAN->MV2R & (1 << (msgobj - 16) ) )
return SET;
else
return RESET;
}
}
/*******************************************************************************
* Function Name : CAN_GetFlagStatus
* Description : Returns the flasg status of the flag passed in parameter
* Input : One of the following parameters:
* - CAN_SR_TXOK
* - CAN_SR_RXOK
* - CAN_SR_EPASS
* - CAN_SR_EWARN
* - CAN_SR_BOFF
* Output : None
* Return : 1 if the flag is set else 0
*******************************************************************************/
FlagStatus CAN_GetFlagStatus ( u32 CAN_Flag )
{
if( CAN->SR & CAN_Flag)
{
return SET;
}
else
return RESET;
}
/*******************************************************************************
* Function Name : CAN_GetTransmitErrorCounter
* Description : Reads the CAN cell transmit error counter
* Input : None
* Output : None
* Return : Transmit Error Counter value between 0..255
*******************************************************************************/
u32 CAN_GetTransmitErrorCounter ( void )
{
return( CAN->ERR & 0x00FF );
}
/*******************************************************************************
* Function Name : CAN_GetReceiveErrorCounter
* Description : Reads the CAN cell receive error counter
* Input : None
* Output : None
* Return : Receive Error Counter value between 0..127
*******************************************************************************/
u32 CAN_GetReceiveErrorCounter ( void )
{
return ( ( CAN->ERR & 0x7F00 ) >> 8);
}
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

166
src/platform/str9/91x_can.h Normal file
View File

@ -0,0 +1,166 @@
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* File Name : 91x_can.h
* Author : MCD Application Team
* Version : V2.1
* Date : 12/22/2008
* Description : This file contains all the functions prototypes for the
* CAN bus firmware library.
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __91x_CAN_H
#define __91x_CAN_H
/* Includes ------------------------------------------------------------------*/
#include "91x_map.h"
/* Exported types ------------------------------------------------------------*/
/* CAN Init structure define */
typedef struct
{
u8 CAN_ConfigParameters;
u32 CAN_Bitrate;
}CAN_InitTypeDef;
/* Exported constants --------------------------------------------------------*/
/* Standard bitrates available*/
enum
{
CAN_BITRATE_100K,
CAN_BITRATE_125K,
CAN_BITRATE_250K,
CAN_BITRATE_500K,
CAN_BITRATE_1M
};
/* Control register*/
#define CAN_CR_TEST 0x0080
#define CAN_CR_CCE 0x0040
#define CAN_CR_DAR 0x0020
#define CAN_CR_EIE 0x0008
#define CAN_CR_SIE 0x0004
#define CAN_CR_IE 0x0002
#define CAN_CR_INIT 0x0001
/* Status register */
#define CAN_SR_BOFF 0x0080
#define CAN_SR_EWARN 0x0040
#define CAN_SR_EPASS 0x0020
#define CAN_SR_RXOK 0x0010
#define CAN_SR_TXOK 0x0008
#define CAN_SR_LEC 0x0007
/* Test register*/
#define CAN_TESTR_RX 0x0080
#define CAN_TESTR_TX1 0x0040
#define CAN_TESTR_TX0 0x0020
#define CAN_TESTR_LBACK 0x0010
#define CAN_TESTR_SILENT 0x0008
#define CAN_TESTR_BASIC 0x0004
/* IFn / Command Request register*/
#define CAN_CRR_BUSY 0x8000
/* IFn / Command Mask register*/
#define CAN_CMR_WRRD 0x0080
#define CAN_CMR_MASK 0x0040
#define CAN_CMR_ARB 0x0020
#define CAN_CMR_CONTROL 0x0010
#define CAN_CMR_CLRINTPND 0x0008
#define CAN_CMR_TXRQSTNEWDAT 0x0004
#define CAN_CMR_DATAA 0x0002
#define CAN_CMR_DATAB 0x0001
/* IFn / Mask 2 register*/
#define CAN_M2R_MXTD 0x8000
#define CAN_M2R_MDIR 0x4000
/* IFn / Arbitration 2 register*/
#define CAN_A2R_MSGVAL 0x8000
#define CAN_A2R_XTD 0x4000
#define CAN_A2R_DIR 0x2000
/* IFn / Message Control register*/
#define CAN_MCR_NEWDAT 0x8000
#define CAN_MCR_MSGLST 0x4000
#define CAN_MCR_INTPND 0x2000
#define CAN_MCR_UMASK 0x1000
#define CAN_MCR_TXIE 0x0800
#define CAN_MCR_RXIE 0x0400
#define CAN_MCR_RMTEN 0x0200
#define CAN_MCR_TXRQST 0x0100
#define CAN_MCR_EOB 0x0080
/* Wake-up modes*/
enum
{
CAN_WAKEUP_ON_EXT,
CAN_WAKEUP_ON_CAN
};
/* CAN message structure*/
typedef struct
{
u32 IdType;
u32 Id;
u8 Dlc;
u8 Data[8];
} canmsg;
/* Message ID types*/
enum
{
CAN_STD_ID,
CAN_EXT_ID
};
/* Message ID limits*/
#define CAN_LAST_STD_ID ((1<<11) - 1)
#define CAN_LAST_EXT_ID ((1L<<29) - 1)
/* Exported functions ------------------------------------------------------- */
void CAN_DeInit (void);
void CAN_Init(CAN_InitTypeDef* CAN_InitStruct);
void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct);
void CAN_SetBitrate(u32 bitrate);
void CAN_SetTiming(u32 tseg1, u32 tseg2, u32 sjw, u32 brp);
ErrorStatus CAN_SetUnusedMsgObj(u32 msgobj);
ErrorStatus CAN_SetTxMsgObj(u32 msgobj, u32 idType, FunctionalState RemoteEN);
ErrorStatus CAN_SetRxMsgObj(u32 msgobj, u32 idType, u32 idLow, u32 idHigh, bool singleOrFifoLast);
ErrorStatus CAN_SetUnusedAllMsgObj(void);
ErrorStatus CAN_ReleaseMessage(u32 msgobj);
ErrorStatus CAN_UpdateMsgObj(u32 msgobj, canmsg* pCanMsg);
ErrorStatus CAN_SendMessage(u32 msgobj, canmsg* pCanMsg);
ErrorStatus CAN_TransmitRequest( u32 msgobj );
ErrorStatus CAN_ReceiveMessage(u32 msgobj, bool release, canmsg* pCanMsg);
void CAN_WaitEndOfTx(void);
ErrorStatus CAN_BasicSendMessage(canmsg* pCanMsg);
ErrorStatus CAN_BasicReceiveMessage(canmsg* pCanMsg);
void CAN_EnterInitMode(u8 InitMask);
void CAN_LeaveInitMode(void);
void CAN_EnterTestMode(u8 TestMask);
void CAN_LeaveTestMode(void);
void CAN_ReleaseTxMessage(u32 msgobj);
void CAN_ReleaseRxMessage(u32 msgobj);
FlagStatus CAN_GetMsgReceiveStatus(u32 msgobj);
FlagStatus CAN_GetMsgTransmitRequestStatus(u32 msgobj);
FlagStatus CAN_GetMsgInterruptStatus(u32 msgobj);
FlagStatus CAN_GetMsgValidStatus(u32 msgobj);
FlagStatus CAN_GetFlagStatus ( u32 CAN_Flag );
u32 CAN_GetTransmitErrorCounter ( void );
u32 CAN_GetReceiveErrorCounter ( void );
#endif /* __91x_CAN_H */
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

View File

@ -2,7 +2,7 @@
local cpumode = ( builder:get_option( 'cpumode' ) or 'arm' ):lower()
specific_files = "startup912.s startup_generic.s platform.c 91x_scu.c 91x_fmi.c 91x_gpio.c 91x_uart.c 91x_tim.c 91x_vic.c interrupt.c str9_pio.c 91x_i2c.c 91x_wiu.c 91x_adc.c platform_int.c 91x_ssp.c"
specific_files = "startup912.s startup_generic.s platform.c 91x_scu.c 91x_fmi.c 91x_gpio.c 91x_uart.c 91x_tim.c 91x_vic.c interrupt.c str9_pio.c 91x_i2c.c 91x_wiu.c 91x_adc.c platform_int.c 91x_ssp.c 91x_can.c"
local ldscript = "str912fw44.lds"

View File

@ -13,7 +13,7 @@
#define NUM_UART 3
#define NUM_PWM 4
#define NUM_ADC 8
#define NUM_CAN 0
#define NUM_CAN 1
#define NUM_I2C 2
// One timer is reserved for the system timer / vtimers

View File

@ -25,6 +25,7 @@
#include "elua_adc.h"
#include "91x_adc.h"
#include "91x_ssp.h"
#include "91x_can.h"
#include "utils.h"
// ****************************************************************************
@ -42,6 +43,8 @@ const TIM_TypeDef* str9_timer_data[] = { TIM0, TIM1, TIM2, TIM3 };
static void platform_setup_adcs();
static void cans_init();
static void platform_config_scu()
{
volatile u16 i = 0xFFFF;
@ -118,16 +121,18 @@ static void platform_gpio_uart_setup()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit( &GPIO_InitStructure );
// RX
GPIO_StructInit( &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
GPIO_InitStructure.GPIO_Pin = uart_pin_data[ UART_RX_IDX ];
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;
GPIO_Init( ( GPIO_TypeDef* )uart_port_data[ UART_RX_IDX ], &GPIO_InitStructure );
// TX
GPIO_StructInit( &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Direction=GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = uart_pin_data[ UART_TX_IDX ];
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt3 ;
GPIO_Init( ( GPIO_TypeDef* )uart_port_data[ UART_TX_IDX ], &GPIO_InitStructure );
}
@ -167,6 +172,11 @@ int platform_init()
platform_setup_adcs();
#endif
#ifdef BUILD_CAN
// Setup CANs
cans_init();
#endif
// Initialize system timer
cmn_systimer_set_base_freq( 1000000 );
cmn_systimer_set_interrupt_freq( VTMR_FREQ_HZ );
@ -821,6 +831,156 @@ int platform_i2c_recv_byte( unsigned id, int ack )
return I2C_ReceiveData( pi2c );
}
// ****************************************************************************
// CAN
#if defined( BUILD_CAN )
static canmsg RxCanMsg;
static canmsg TxCanMsg;
static GPIO_InitTypeDef GPIO_InitStructure;
static CAN_InitTypeDef CAN_InitStructure;
static vu32 frame_received_flag = 0;
/* used message object numbers */
enum {
CAN_TX_STD_MSGOBJ = 0,
CAN_TX_EXT_MSGOBJ = 1,
CAN_RX_STD_MSGOBJ = 2,
CAN_RX_EXT_MSGOBJ = 3
};
/*******************************************************************************
* Function Name : CAN_IRQHandler
* Description : This function handles the CAN interrupt request
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAN_IRQHandler(void)
{
u32 msgobj = 0;
if(CAN->IDR == 0x8000) /* status interrupt */
(void)CAN->SR; /* read the status register to clear*/
else if(CAN->IDR >= 1 && CAN->IDR <= 32)
{
/* get the message object number that caused the interrupt to occur */
switch(msgobj = CAN->IDR - 1)
{
case 0: case 1:/* CAN_TX_MSGOBJ */
CAN_ReleaseTxMessage(msgobj);
break;
default:
CAN_ReceiveMessage(msgobj, FALSE, &RxCanMsg);
CAN_ReleaseRxMessage(msgobj);
frame_received_flag = 1;
break;
}
}
/*write any value to VIC0 VAR*/
VIC0->VAR = 0xFF;
}
void cans_init( void )
{
SCU_APBPeriphClockConfig(__CAN, ENABLE);
SCU_APBPeriphReset(__CAN, DISABLE);
#ifdef ELUA_BOARD_STRE912
/* P3.3 alternate input 1, CAN_RX pin 61*/
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Direction=GPIO_PinInput;
GPIO_InitStructure.GPIO_IPConnected=GPIO_IPConnected_Enable;
GPIO_InitStructure.GPIO_Alternate=GPIO_InputAlt1;
GPIO_Init(GPIO3,&GPIO_InitStructure);
/* P3.2 alternate output 2, CAN_TX pin 60*/
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Direction=GPIO_PinOutput;
GPIO_InitStructure.GPIO_Type=GPIO_Type_PushPull;
GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt2;
GPIO_Init(GPIO3,&GPIO_InitStructure);
#else // STR9-comStick
/* P5.0 alternate input 1, CAN_RX pin 12*/
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Direction=GPIO_PinInput;
GPIO_InitStructure.GPIO_IPConnected=GPIO_IPConnected_Enable;
GPIO_InitStructure.GPIO_Alternate=GPIO_InputAlt1;
GPIO_Init(GPIO5,&GPIO_InitStructure);
/* P5.1 alternate output 2, CAN_TX pin 18*/
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Direction=GPIO_PinOutput;
GPIO_InitStructure.GPIO_Type=GPIO_Type_PushPull;
GPIO_InitStructure.GPIO_Alternate=GPIO_OutputAlt2;
GPIO_Init(GPIO5,&GPIO_InitStructure);
#endif
}
u32 platform_can_setup( unsigned id, u32 clock )
{
//Timing input values
const u8 tseg1 = 2;
const u8 tseg2 = 1;
const u8 sjw = 4;
//PCLK clock
u32 fclk = SCU_GetPCLKFreqValue();
//Baud Rate Prescaler
//Check 'The insider's guide to the STR91x ARM 9'
//Section 4.13.2.1 Bit Timing
u32 brp = 1000 * fclk / clock / (1 + tseg1 + tseg2);
/* initialize the CAN, interrupts enabled */
CAN_InitStructure.CAN_ConfigParameters=CAN_CR_IE;
CAN_Init(&CAN_InitStructure);
//Set Can Timing
CAN_EnterInitMode(CAN_CR_CCE | CAN_InitStructure.CAN_ConfigParameters);
CAN_SetTiming(tseg1, tseg2, sjw, brp);
CAN_LeaveInitMode();
//Set message objects
CAN_SetUnusedAllMsgObj();
CAN_SetTxMsgObj(CAN_TX_STD_MSGOBJ, CAN_STD_ID, DISABLE);
CAN_SetTxMsgObj(CAN_TX_EXT_MSGOBJ, CAN_EXT_ID, DISABLE);
CAN_SetRxMsgObj(CAN_RX_STD_MSGOBJ, CAN_STD_ID, 0, CAN_LAST_STD_ID, TRUE);
CAN_SetRxMsgObj(CAN_RX_EXT_MSGOBJ, CAN_EXT_ID, 0, CAN_LAST_EXT_ID, TRUE);
return clock;
}
void platform_can_send( unsigned id, u32 canid, u8 idtype, u8 len, const u8 *data )
{
TxCanMsg.IdType = idtype;
TxCanMsg.Id = canid;
TxCanMsg.Dlc = len;
memcpy(TxCanMsg.Data, data, len);
CAN_SendMessage((idtype & ELUA_CAN_ID_EXT) ? CAN_TX_EXT_MSGOBJ : CAN_TX_STD_MSGOBJ, &TxCanMsg);
}
int platform_can_recv( unsigned id, u32 *canid, u8 *idtype, u8 *len, u8 *data )
{
// wait for a message
if ( frame_received_flag != 0){
*canid = ( u32 ) RxCanMsg.Id;
*idtype = RxCanMsg.IdType;
*len = RxCanMsg.Dlc;
memcpy( data, RxCanMsg.Data, *len );
frame_received_flag = 0;
return PLATFORM_OK;
}
else
return PLATFORM_UNDERFLOW;
}
#endif
// ****************************************************************************
// SPI

View File

@ -5,6 +5,10 @@
#define PLATFORM_HAS_SYSTIMER
#if NUM_CAN > 0
#define BUILD_CAN
#endif
// ID of the hardware timer used to implement physical timers
#define VTMR_TIMER_ID 3

View File

@ -281,6 +281,12 @@ void platform_int_init()
WIU->PR = 0xFFFFFFFF;
WIU->CTRL |= 2;
#ifdef BUILD_CAN
/* initialize the interrupt controller */
VIC_Config(CAN_ITLine, VIC_IRQ, p ++ );
/* enable global interrupt */
VIC_ITCmd(CAN_ITLine, ENABLE);
#endif
#ifdef INT_TMR_MATCH
VIC_Config( TIM0_ITLine, VIC_IRQ, 5 );

View File

@ -1,17 +1,8 @@
#ifndef __TYPE_H__
#define __TYPE_H__
#ifndef NULL
#define NULL ((void *)0)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (1)
#endif
#include <stdbool.h>
enum { FALSE = 0, TRUE = !FALSE };
typedef unsigned char BYTE;
typedef unsigned short WORD;