2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
* \file Source\ARM7_LPC2000\can.c
|
|
|
|
* \brief Bootloader CAN communication interface source file.
|
|
|
|
* \ingroup Target_ARM7_LPC2000
|
|
|
|
* \internal
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* C O P Y R I G H T
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) 2011 by Feaser http://www.feaser.com All rights reserved
|
|
|
|
*
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* L I C E N S E
|
|
|
|
*----------------------------------------------------------------------------------------
|
|
|
|
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
2016-07-25 01:33:52 -07:00
|
|
|
* You have received a copy of the GNU General Public License along with OpenBLT. It
|
2016-04-30 15:52:15 -07:00
|
|
|
* should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy.
|
2016-03-01 06:24:23 -08:00
|
|
|
*
|
2013-07-31 08:48:23 -07:00
|
|
|
* \endinternal
|
2011-11-10 09:55:56 -08:00
|
|
|
****************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Include files
|
|
|
|
****************************************************************************************/
|
|
|
|
#include "boot.h" /* bootloader generic header */
|
|
|
|
|
|
|
|
|
|
|
|
#if (BOOT_COM_CAN_ENABLE > 0)
|
|
|
|
/****************************************************************************************
|
|
|
|
* Macro definitions
|
|
|
|
****************************************************************************************/
|
2018-04-25 06:49:24 -07:00
|
|
|
/** \brief Timeout for transmitting a CAN message in milliseconds. */
|
|
|
|
#define CAN_MSG_TX_TIMEOUT_MS (50u)
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief Transmit buffer 1 idle bit. */
|
2016-03-01 06:24:23 -08:00
|
|
|
#define CAN_TBS1 (0x00000004)
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief Transmit buffer 1 complete bit. */
|
|
|
|
#define CAN_TCS1 (0x00000008)
|
|
|
|
/** \brief Receive buffer release bit. */
|
|
|
|
#define CAN_RRB (0x04)
|
|
|
|
/** \brief Receive buffer status bit. */
|
|
|
|
#define CAN_RBS (0x01)
|
|
|
|
/** \brief Transmission request bit. */
|
|
|
|
#define CAN_TR (0x01)
|
|
|
|
/** \brief Select tx buffer 1 for transmit bit. */
|
|
|
|
#define CAN_STB1 (0x20)
|
2017-08-16 09:48:49 -07:00
|
|
|
/** \brief Frame format bit. 0 for 11-bit and 1 for 29-bit CAN identifiers. */
|
|
|
|
#define CAN_FF (0x80000000)
|
2011-11-10 09:55:56 -08:00
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Register definitions
|
|
|
|
****************************************************************************************/
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CANAFMR CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CANAFMR (*((volatile blt_int8u *) 0xE003C000))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1MOD CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1MOD (*((volatile blt_int32u *) 0xE0044000))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1IER CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1IER (*((volatile blt_int32u *) 0xE0044010))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1GSR CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1GSR (*((volatile blt_int32u *) 0xE0044008))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1BTR CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1BTR (*((volatile blt_int32u *) 0xE0044014))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1TFI1 CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1TFI1 (*((volatile blt_int32u *) 0xE0044030))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1TID1 CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1TID1 (*((volatile blt_int32u *) 0xE0044034))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1TDA1 CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1TDA1 (*((volatile blt_int32u *) 0xE0044038))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1TDB1 CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1TDB1 (*((volatile blt_int32u *) 0xE004403C))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1CMR CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1CMR (*((volatile blt_int32u *) 0xE0044004))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1SR CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1SR (*((volatile blt_int32u *) 0xE004401C))
|
2017-08-16 09:48:49 -07:00
|
|
|
/** \brief CAN1RFS CAN controller register. */
|
|
|
|
#define CAN1RFS (*((volatile blt_int32u *) 0xE0044020))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1RID CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1RID (*((volatile blt_int32u *) 0xE0044024))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1RDA CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1RDA (*((volatile blt_int32u *) 0xE0044028))
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN1RDB CAN controller register. */
|
2011-11-10 09:55:56 -08:00
|
|
|
#define CAN1RDB (*((volatile blt_int32u *) 0xE004402C))
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Type definitions
|
|
|
|
****************************************************************************************/
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief Structure type for grouping CAN bus timing related information. */
|
2011-11-10 09:55:56 -08:00
|
|
|
typedef struct t_can_bus_timing
|
|
|
|
{
|
2013-07-31 08:48:23 -07:00
|
|
|
blt_int8u tseg1; /**< CAN time segment 1 */
|
|
|
|
blt_int8u tseg2; /**< CAN time segment 2 */
|
|
|
|
} tCanBusTiming;
|
2011-11-10 09:55:56 -08:00
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************************
|
|
|
|
* Local constant declarations
|
|
|
|
****************************************************************************************/
|
2013-07-31 08:48:23 -07:00
|
|
|
/** \brief CAN bittiming table for dynamically calculating the bittiming settings.
|
2016-03-01 06:24:23 -08:00
|
|
|
* \details According to the CAN protocol 1 bit-time can be made up of between 8..25
|
|
|
|
* time quanta (TQ). The total TQ in a bit is SYNC + TSEG1 + TSEG2 with SYNC
|
|
|
|
* always being 1. The sample point is (SYNC + TSEG1) / (SYNC + TSEG1 + SEG2) *
|
2013-07-31 08:48:23 -07:00
|
|
|
* 100%. This array contains possible and valid time quanta configurations with
|
|
|
|
* a sample point between 68..78%.
|
2011-11-10 09:55:56 -08:00
|
|
|
*/
|
|
|
|
static const tCanBusTiming canTiming[] =
|
2016-03-01 06:24:23 -08:00
|
|
|
{
|
|
|
|
/* TQ | TSEG1 | TSEG2 | SP */
|
|
|
|
/* ------------------------- */
|
|
|
|
{ 5, 2 }, /* 8 | 5 | 2 | 75% */
|
|
|
|
{ 6, 2 }, /* 9 | 6 | 2 | 78% */
|
|
|
|
{ 6, 3 }, /* 10 | 6 | 3 | 70% */
|
|
|
|
{ 7, 3 }, /* 11 | 7 | 3 | 73% */
|
|
|
|
{ 8, 3 }, /* 12 | 8 | 3 | 75% */
|
|
|
|
{ 9, 3 }, /* 13 | 9 | 3 | 77% */
|
|
|
|
{ 9, 4 }, /* 14 | 9 | 4 | 71% */
|
|
|
|
{ 10, 4 }, /* 15 | 10 | 4 | 73% */
|
|
|
|
{ 11, 4 }, /* 16 | 11 | 4 | 75% */
|
|
|
|
{ 12, 4 }, /* 17 | 12 | 4 | 76% */
|
|
|
|
{ 12, 5 }, /* 18 | 12 | 5 | 72% */
|
|
|
|
{ 13, 5 }, /* 19 | 13 | 5 | 74% */
|
|
|
|
{ 14, 5 }, /* 20 | 14 | 5 | 75% */
|
|
|
|
{ 15, 5 }, /* 21 | 15 | 5 | 76% */
|
|
|
|
{ 15, 6 }, /* 22 | 15 | 6 | 73% */
|
|
|
|
{ 16, 6 }, /* 23 | 16 | 6 | 74% */
|
|
|
|
{ 16, 7 }, /* 24 | 16 | 7 | 71% */
|
|
|
|
{ 16, 8 } /* 25 | 16 | 8 | 68% */
|
2011-11-10 09:55:56 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
2016-03-01 06:24:23 -08:00
|
|
|
** \brief Search algorithm to match the desired baudrate to a possible bus
|
2013-07-31 08:48:23 -07:00
|
|
|
** timing configuration.
|
|
|
|
** \param baud The desired baudrate in kbps. Valid values are 10..1000.
|
|
|
|
** \param btr Pointer to where the value for register CANxBTR will be stored.
|
2016-03-01 06:24:23 -08:00
|
|
|
** \return BLT_TRUE if the CAN bustiming register values were found, BLT_FALSE
|
2013-07-31 08:48:23 -07:00
|
|
|
** otherwise.
|
2011-11-10 09:55:56 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
static blt_bool CanGetSpeedConfig(blt_int16u baud, blt_int32u *btr)
|
|
|
|
{
|
|
|
|
blt_int16u prescaler;
|
|
|
|
blt_int8u cnt;
|
|
|
|
|
|
|
|
/* loop through all possible time quanta configurations to find a match */
|
|
|
|
for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++)
|
|
|
|
{
|
|
|
|
if ((BOOT_CPU_SYSTEM_SPEED_KHZ % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0)
|
|
|
|
{
|
|
|
|
/* compute the prescaler that goes with this TQ configuration */
|
|
|
|
prescaler = BOOT_CPU_SYSTEM_SPEED_KHZ/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1));
|
|
|
|
|
|
|
|
/* make sure the prescaler is valid */
|
2016-03-01 06:24:23 -08:00
|
|
|
if ((prescaler > 0) && (prescaler <= 1024))
|
2011-11-10 09:55:56 -08:00
|
|
|
{
|
|
|
|
/* store the prescaler and bustiming register value */
|
|
|
|
*btr = prescaler - 1;
|
|
|
|
*btr |= ((canTiming[cnt].tseg2 - 1) << 20) | ((canTiming[cnt].tseg1 - 1) << 16);
|
|
|
|
/* found a good bus timing configuration */
|
|
|
|
return BLT_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* could not find a good bus timing configuration */
|
|
|
|
return BLT_FALSE;
|
|
|
|
} /*** end of CanGetSpeedConfig ***/
|
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Initializes the CAN controller and synchronizes it to the CAN bus.
|
|
|
|
** \return none.
|
2011-11-10 09:55:56 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void CanInit(void)
|
|
|
|
{
|
|
|
|
blt_bool result;
|
2014-05-26 16:14:30 -07:00
|
|
|
blt_int32u btr_reg_value=0;
|
2016-03-01 06:24:23 -08:00
|
|
|
|
|
|
|
/* the current implementation supports CAN1, which has channel index 0. throw an
|
|
|
|
* assertion error in case a different CAN channel is configured.
|
2011-11-10 09:55:56 -08:00
|
|
|
*/
|
2016-03-01 06:24:23 -08:00
|
|
|
ASSERT_CT(BOOT_COM_CAN_CHANNEL_INDEX == 0);
|
2011-11-10 09:55:56 -08:00
|
|
|
/* configure acceptance filter for bypass mode so it receives all messages */
|
|
|
|
CANAFMR = 0x00000002L;
|
|
|
|
/* take CAN controller offline and go into reset mode */
|
2016-03-01 06:24:23 -08:00
|
|
|
CAN1MOD = 1;
|
2011-11-10 09:55:56 -08:00
|
|
|
/* disable all interrupts. driver only needs to work in polling mode */
|
|
|
|
CAN1IER = 0;
|
|
|
|
/* reset CAN controller status */
|
2016-03-01 06:24:23 -08:00
|
|
|
CAN1GSR = 0;
|
2011-11-10 09:55:56 -08:00
|
|
|
/* configure the bittiming */
|
|
|
|
result = CanGetSpeedConfig(BOOT_COM_CAN_BAUDRATE/1000, &btr_reg_value);
|
|
|
|
/* check that a valid baudrate configuration was found */
|
|
|
|
ASSERT_RT(result == BLT_TRUE);
|
|
|
|
/* write the bittiming configuration to the register */
|
2016-03-01 06:24:23 -08:00
|
|
|
CAN1BTR = btr_reg_value;
|
2011-11-10 09:55:56 -08:00
|
|
|
/* enter normal operating mode and synchronize to the CAN bus */
|
|
|
|
CAN1MOD = 0;
|
|
|
|
} /*** end of CanInit ***/
|
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Transmits a packet formatted for the communication interface.
|
|
|
|
** \param data Pointer to byte array with data that it to be transmitted.
|
|
|
|
** \param len Number of bytes that are to be transmitted.
|
|
|
|
** \return none.
|
2011-11-10 09:55:56 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
|
|
|
void CanTransmitPacket(blt_int8u *data, blt_int8u len)
|
|
|
|
{
|
2018-04-25 06:49:24 -07:00
|
|
|
blt_int32u timeout;
|
|
|
|
|
2011-11-10 09:55:56 -08:00
|
|
|
/* check that transmit buffer 1 is ready to accept a new message */
|
|
|
|
ASSERT_RT((CAN1SR & CAN_TBS1) != 0);
|
|
|
|
/* write dlc and configure message as a standard message with 11-bit identifier */
|
2016-03-01 06:24:23 -08:00
|
|
|
CAN1TFI1 = (len << 16);
|
2011-11-10 09:55:56 -08:00
|
|
|
/* write the message identifier */
|
|
|
|
CAN1TID1 = BOOT_COM_CAN_TX_MSG_ID;
|
2017-08-16 09:48:49 -07:00
|
|
|
/* is it a 29-bit CAN identifier? */
|
|
|
|
if ( (BOOT_COM_CAN_TX_MSG_ID & 0x80000000) != 0)
|
|
|
|
{
|
|
|
|
/* configure identifier as 29-bit extended. */
|
|
|
|
CAN1TFI1 |= CAN_FF;
|
|
|
|
/* Reset the mask bit. */
|
|
|
|
CAN1TID1 &= ~0x80000000;
|
|
|
|
}
|
2011-11-10 09:55:56 -08:00
|
|
|
/* write the first set of 4 data bytes */
|
|
|
|
CAN1TDA1 = (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
|
|
|
|
/* write the second set of 4 data bytes */
|
|
|
|
CAN1TDB1 = (data[7] << 24) + (data[6] << 16) + (data[5] << 8) + data[4];
|
|
|
|
/* write transmission request for transmit buffer 1 */
|
|
|
|
CAN1CMR = CAN_TR | CAN_STB1;
|
2018-04-25 06:49:24 -07:00
|
|
|
/* set timeout time to wait for transmission completion */
|
|
|
|
timeout = TimerGet() + CAN_MSG_TX_TIMEOUT_MS;
|
2011-11-10 09:55:56 -08:00
|
|
|
/* wait for transmit completion */
|
2016-03-01 06:24:23 -08:00
|
|
|
while ((CAN1SR & CAN_TCS1) == 0)
|
2011-11-10 09:55:56 -08:00
|
|
|
{
|
|
|
|
/* keep the watchdog happy */
|
|
|
|
CopService();
|
2018-04-25 06:49:24 -07:00
|
|
|
/* break loop upon timeout. this would indicate a hardware failure or no other
|
|
|
|
* nodes connected to the bus.
|
|
|
|
*/
|
|
|
|
if (TimerGet() > timeout)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2011-11-10 09:55:56 -08:00
|
|
|
}
|
|
|
|
} /*** end of CanTransmitPacket ***/
|
|
|
|
|
|
|
|
|
2013-07-31 08:48:23 -07:00
|
|
|
/************************************************************************************//**
|
|
|
|
** \brief Receives a communication interface packet if one is present.
|
|
|
|
** \param data Pointer to byte array where the data is to be stored.
|
2018-02-09 08:59:41 -08:00
|
|
|
** \param len Pointer where the length of the packet is to be stored.
|
2013-07-31 08:48:23 -07:00
|
|
|
** \return BLT_TRUE is a packet was received, BLT_FALSE otherwise.
|
2011-11-10 09:55:56 -08:00
|
|
|
**
|
|
|
|
****************************************************************************************/
|
2018-02-09 08:59:41 -08:00
|
|
|
blt_bool CanReceivePacket(blt_int8u *data, blt_int8u *len)
|
2011-11-10 09:55:56 -08:00
|
|
|
{
|
2017-08-16 09:48:49 -07:00
|
|
|
blt_int32u rxMsgId;
|
2018-02-09 08:59:41 -08:00
|
|
|
blt_int8u rxMsgDlc;
|
2017-08-16 09:48:49 -07:00
|
|
|
blt_bool result = BLT_FALSE;
|
|
|
|
|
2011-11-10 09:55:56 -08:00
|
|
|
/* check if a new message was received */
|
2017-08-16 09:48:49 -07:00
|
|
|
if ((CAN1SR & CAN_RBS) != 0)
|
2011-11-10 09:55:56 -08:00
|
|
|
{
|
2017-08-16 09:48:49 -07:00
|
|
|
/* read out the CAN message identifier */
|
|
|
|
rxMsgId = CAN1RID;
|
|
|
|
/* was is a 29-bit extended CAN identifier? */
|
|
|
|
if ((CAN1RFS & CAN_FF) != 0)
|
|
|
|
{
|
|
|
|
/* set mask bit. */
|
|
|
|
rxMsgId |= 0x80000000;
|
|
|
|
}
|
|
|
|
/* see if this is the message identifier that we are interested in */
|
|
|
|
if (rxMsgId == BOOT_COM_CAN_RX_MSG_ID)
|
|
|
|
{
|
2018-02-09 08:59:41 -08:00
|
|
|
/* store the message data length */
|
|
|
|
rxMsgDlc = ((blt_int8u)(CAN1RFS >> 16)) & 0x0Fu;
|
|
|
|
if (rxMsgDlc > 8)
|
|
|
|
{
|
|
|
|
rxMsgDlc = 8;
|
|
|
|
}
|
|
|
|
*len = rxMsgDlc;
|
2017-08-16 09:48:49 -07:00
|
|
|
/* store the message data */
|
|
|
|
data[0] = (blt_int8u)CAN1RDA;
|
|
|
|
data[1] = (blt_int8u)(CAN1RDA >> 8);
|
|
|
|
data[2] = (blt_int8u)(CAN1RDA >> 16);
|
|
|
|
data[3] = (blt_int8u)(CAN1RDA >> 24);
|
|
|
|
data[4] = (blt_int8u)CAN1RDB;
|
|
|
|
data[5] = (blt_int8u)(CAN1RDB >> 8);
|
|
|
|
data[6] = (blt_int8u)(CAN1RDB >> 16);
|
|
|
|
data[7] = (blt_int8u)(CAN1RDB >> 24);
|
|
|
|
/* update the result. */
|
|
|
|
result = BLT_TRUE;
|
|
|
|
}
|
|
|
|
/* release the receive buffer */
|
|
|
|
CAN1CMR = CAN_RRB;
|
2011-11-10 09:55:56 -08:00
|
|
|
}
|
2017-08-16 09:48:49 -07:00
|
|
|
/* give the result back to the caller. */
|
|
|
|
return result;
|
2011-11-10 09:55:56 -08:00
|
|
|
} /*** end of CanReceivePacket ***/
|
|
|
|
#endif /* BOOT_COM_CAN_ENABLE > 0 */
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************** end of can.c **************************************/
|