1
0
Fork 0

Added driver for Freescale MCF5282 from archives. Not tested.

This commit is contained in:
Janez_D 2015-08-20 19:10:08 +02:00
parent b80c058291
commit 50d2855326
4 changed files with 896 additions and 1 deletions

6
README
View file

@ -76,6 +76,12 @@ Supported microcontrollers:
- Status:
- unconfirmed
- Freescale MCF5282 (ColdFire V2)
- Contributed by Laurent Grosbois (mar 2012):
- See: http://sourceforge.net/p/canopennode/code_complete/ci/master/tree/mcf5282_ongoingdev/
- Status:
- unconfirmed
- ST STM32:
- Contributed by ?
- Status:

465
stack/MCF5282/CO_driver.c Normal file
View file

@ -0,0 +1,465 @@
/*
* CAN module object for Freescale MCF5282 ColdFire V2 microcontroller.
*
* @file CO_driver.c
* @author Janez Paternoster
* @author Laurent Grosbois
* @copyright 2004 - 2012 Janez Paternoster
*
* This file is part of CANopenNode, an opensource CANopen Stack.
* Project home page is <http://canopennode.sourceforge.net>.
* For more information on CANopen see <http://www.can-cia.org/>.
*
* CANopenNode is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CO_driver.h"
#include "CO_Emergency.h"
extern const CO_CANbitRateData_t CO_CANbitRateData[8];
/******************************************************************************/
void CO_CANsetConfigurationMode(uint16_t CANbaseAddress){
/* sets the module as running */
MCF_FlexCAN_CANMCR &= ~MCF_FlexCAN_CANMCR_STOP;
/* enter debug mode */
MCF_FlexCAN_CANMCR |= MCF_FlexCAN_CANMCR_FRZ | MCF_FlexCAN_CANMCR_HALT;
/* wait for entering in the mode */
while(!(MCF_FlexCAN_CANMCR&MCF_FlexCAN_CANMCR_FRZACK)){};
}
/******************************************************************************/
void CO_CANsetNormalMode(uint16_t CANbaseAddress){
/* sets the module as running & exit debug mode */
MCF_FlexCAN_CANMCR &= ~MCF_FlexCAN_CANMCR_STOP & ~MCF_FlexCAN_CANMCR_FRZ & ~MCF_FlexCAN_CANMCR_HALT;
/* wait for entering in the mode */
while(!(MCF_FlexCAN_CANMCR&&MCF_FlexCAN_CANMCR_NOTRDY)){};
}
/******************************************************************************/
CO_ReturnError_t CO_CANmodule_init(
CO_CANmodule_t *CANmodule,
uint16_t CANbaseAddress,
CO_CANrx_t rxArray[],
uint16_t rxSize,
CO_CANtx_t txArray[],
uint16_t txSize,
uint16_t CANbitRate)
{
uint16_t i;
uint8_t nb_CANbuff = 16; //16 CAN buffers
/* Configure object variables */
CANmodule->CANbaseAddress = CANbaseAddress;
CANmodule->CANmsgBuffSize = nb_CANbuff;
CANmodule->rxArray = rxArray;
CANmodule->rxSize = rxSize;
CANmodule->txArray = txArray;
CANmodule->txSize = txSize;
CANmodule->useCANrxFilters = false; //no filters or ((rxSize <= xx) ? true : false;)
CANmodule->bufferInhibitFlag = false;
CANmodule->firstCANtxMessage = true;
CANmodule->CANtxCount = 0U;
CANmodule->errOld = 0U;
CANmodule->em = NULL;
for(i=0U; i<rxSize; i++){
rxArray[i].ident = 0U;
rxArray[i].pFunct = NULL;
}
for(i=0U; i<txSize; i++){
txArray[i].bufferFull = false;
}
/**** HARDWARE CONFIGURATION ****/
/* soft reset */
MCF_FlexCAN_CANMCR |= MCF_FlexCAN_CANMCR_SOFTRST;
/* Tx & Rx pin modes */
MCF_FlexCAN_CANCTRL0 &= ~MCF_FlexCAN_CANCTRL0_BOFFMSK & ~MCF_FlexCAN_CANCTRL0_ERRMSK & ~MCF_FlexCAN_CANCTRL0_RXMODE & ~MCF_FlexCAN_CANCTRL0_TXMODE(0);
/* - no Bus off interrupt
- no error interrupt
- Rx mode : 0 is dominant bit
- Tx mode : full CMOS positive */
//reset ctrl registers
MCF_FlexCAN_CANCTRL1 = 0x00;
MCF_FlexCAN_CANCTRL2 = 0x00;
/* Configure CAN timing */
switch(CANbitRate){
default:
case 125: i=3; break;
case 1000: i=7; break;
}
MCF_FlexCAN_CANCTRL1 |= MCF_FlexCAN_CANCTRL1_PROPSEG(CO_CANbitRateData[i].PROP);
MCF_FlexCAN_CANCTRL2 |= MCF_FlexCAN_CANCTRL2_RJW(CO_CANbitRateData[i].SJW);
MCF_FlexCAN_CANCTRL2 |= MCF_FlexCAN_CANCTRL2_PSEG1(CO_CANbitRateData[i].phSeg1);
MCF_FlexCAN_CANCTRL2 |= MCF_FlexCAN_CANCTRL2_PSEG2(CO_CANbitRateData[i].phSeg2);
MCF_FlexCAN_PRESDIV = CO_CANbitRateData[i].BRP;
/*** here should go specific option concerning CTRL1 & CTRL2 registers ***/
/* CAN module hardware filters */
//clear all filter control registers
MCF_FlexCAN_RXGMASK = 0x00; //mask for Rx[0-13]
MCF_FlexCAN_RX14MASK = 0x00; //mask for Rx14
MCF_FlexCAN_RX15MASK = 0x00; //mask for Rx15
//Set masks to accept all all messages with standard 11-bit identifier
MCF_FlexCAN_RXGMASK = 0x00080000;
MCF_FlexCAN_RX14MASK = 0x00080000;
MCF_FlexCAN_RX15MASK = 0x00080000;
/*
if(CANmodule->useCANrxFilters){
//CAN module filters are used, they will be configured with
//CO_CANrxBufferInit() functions, called by separate CANopen
//init functions.
//Configure all masks so, that received message must match filter
MCF_FlexCAN_RXGMASK = 0xFFE80000;
MCF_FlexCAN_RX14MASK = 0xFFE80000;
MCF_FlexCAN_RX15MASK = 0xFFE80000;
}
else{
//CAN module filters are not used, all messages with standard 11-bit
//identifier will be received
//Configure all mask so, that all messages with standard identifier are accepted
MCF_FlexCAN_RXGMASK = 0x00080000;
MCF_FlexCAN_RX14MASK = 0x00080000;
MCF_FlexCAN_RX15MASK = 0x00080000;
}*/
/* CAN Module configuration register */
MCF_FlexCAN_CANMCR &= ~MCF_FlexCAN_CANMCR_STOP & ~MCF_FlexCAN_CANMCR_FRZ & ~MCF_FlexCAN_CANMCR_HALT;
/* configure buffers 0-13 as input buffers */
for(i=0;i<14;i++)
{
MCF_CANMB_CTRL(i) = MCF_CANMB_CTRL_CODE(0b0100);
}
/* configure buffers 14-15 as output buffers */
MCF_CANMB_CTRL14 = MCF_CANMB_CTRL_CODE(0b1000);
MCF_CANMB_CTRL15 = MCF_CANMB_CTRL_CODE(0b1000);
/* CAN interrupt registers */
//Enable all buffer interrupts (can be either Rx or Tx interrupt depending on buffer configuration)
MCF_FlexCAN_IMASK = 0xFF;
MCF_FlexCAN_IFLAG = 0xFF;
return CO_ERROR_NO;
}
/******************************************************************************/
void CO_CANmodule_disable(CO_CANmodule_t *CANmodule){
CO_CANsetConfigurationMode(CANmodule->CANbaseAddress);
}
/******************************************************************************/
uint16_t CO_CANrxMsg_readIdent(const CO_CANrxMsg_t *rxMsg){
return (uint16_t) (rxMsg->sid>>5);
}
/******************************************************************************/
CO_ReturnError_t CO_CANrxBufferInit(
CO_CANmodule_t *CANmodule,
uint16_t index,
uint16_t ident,
uint16_t mask,
bool_t rtr,
void *object,
void (*pFunct)(void *object, const CO_CANrxMsg_t *message))
{
CO_ReturnError_t ret = CO_ERROR_NO;
if((CANmodule!=NULL) && (object!=NULL) && (pFunct!=NULL) && (index < CANmodule->rxSize)){
/* buffer, which will be configured */
CO_CANrx_t *buffer = &CANmodule->rxArray[index];
/* Configure object variables */
buffer->object = object;
buffer->pFunct = pFunct;
/* CAN identifier and CAN mask, bit aligned with CAN module */
buffer->ident = (uint16_t) ((ident & 0x07FF)<<5);
if(rtr){
buffer->ident |= 0x0010;
}
buffer->mask = (uint16_t) (((mask & 0x07FF)<<5) | 0x0080);
/* Set CAN hardware module filter and mask. */
if(CANmodule->useCANrxFilters){
//filters are not used.
}
}
else{
ret = CO_ERROR_ILLEGAL_ARGUMENT;
}
return ret;
}
/******************************************************************************/
CO_CANtx_t *CO_CANtxBufferInit(
CO_CANmodule_t *CANmodule,
uint16_t index,
uint16_t ident,
bool_t rtr,
uint8_t noOfBytes,
bool_t syncFlag)
{
CO_CANtx_t *buffer = NULL;
if((CANmodule != NULL) && (index < CANmodule->txSize)){
/* get specific buffer */
buffer = &CANmodule->txArray[index];
/* CAN identifier, DLC and rtr, bit aligned with CAN module transmit buffer */
buffer->ident = (uint16_t) ((ident & 0x07FF)<<5);
if(rtr) buffer->ident |= 0x0010;
buffer->DLC = noOfBytes;
buffer->bufferFull = false;
buffer->syncFlag = syncFlag;
}
return buffer;
}
/******************************************************************************/
CO_ReturnError_t CO_CANsend(CO_CANmodule_t *CANmodule, CO_CANtx_t *buffer){
CO_ReturnError_t err = CO_ERROR_NO;
uint16_t addr = CANmodule->CANbaseAddress;
uint8_t i;
/* Verify overflow */
if(buffer->bufferFull){
if(!CANmodule->firstCANtxMessage){
/* don't set error, if bootup message is still on buffers */
CO_errorReport((CO_EM_t*)CANmodule->em, CO_EM_CAN_TX_OVERFLOW, CO_EMC_CAN_OVERRUN, buffer->CMSGSID);
}
err = CO_ERROR_TX_OVERFLOW;
}
/* Try to find a free sending buffer */
/* Here, we use buffer 14 & 15 for Tx */
i=14; //initial sending buffer index
while( (MCF_CANMB_CTRL(i) != MCF_CANMB_CTRL_CODE(0b1000))&&
(MCF_CANMB_CTRL(i) != MCF_CANMB_CTRL_CODE(0b0100))&&
(MCF_CANMB_CTRL(i) != MCF_CANMB_CTRL_CODE(0b1010))&&
i<16)//end sending buffer index+1
{
i++;
}
CO_DISABLE_INTERRUPTS();
/* if CAN TX buffer is free, copy message to it */
if(i<16){
MCF_CANMB_CTRL(i) = 0x0000|MCF_CANMB_CTRL_CODE(0b1000); //Tx MB inactive
MCF_CANMB_SID(i) = buffer->ident;
MCF_CANMB_DATA_WORD_1(i) = (uint16_t) (((buffer->data[0])<<8) | (buffer->data[1]));
MCF_CANMB_DATA_WORD_2(i) = (uint16_t) (((buffer->data[2])<<8) | (buffer->data[3]));
MCF_CANMB_DATA_WORD_3(i) = (uint16_t) (((buffer->data[4])<<8) | (buffer->data[5]));
MCF_CANMB_DATA_WORD_4(i) = (uint16_t) (((buffer->data[6])<<8) | (buffer->data[7]));
MCF_CANMB_CTRL(i) = (uint8_t) (MCF_CANMB_CTRL_CODE(0b1000) | MCF_CANMB_CTRL_LENGTH(buffer->DLC) ); //Tx MB active
}
/* if no buffer is free, message will be sent by interrupt */
else{
buffer->bufferFull = true;
CANmodule->CANtxCount++;
}
CO_ENABLE_INTERRUPTS();
return err;
}
/******************************************************************************/
void CO_CANclearPendingSyncPDOs(CO_CANmodule_t *CANmodule){
CO_DISABLE_INTERRUPTS();
if(CANmodule->bufferInhibitFlag){
MCF_CANMB_CTRL14 = MCF_CANMB_CTRL_CODE(0b1000); //clear TXREQ
MCF_CANMB_CTRL15 = MCF_CANMB_CTRL_CODE(0b1000); //clear TXREQ
CO_ENABLE_INTERRUPTS();
CO_errorReport((CO_emergencyReport_t*)CANmodule->em, ERROR_TPDO_OUTSIDE_WINDOW, 0);
}
else{
CO_ENABLE_INTERRUPTS();
}
}
/******************************************************************************/
void CO_CANverifyErrors(CO_CANmodule_t *CANmodule){
unsigned rxErrors, txErrors, ESTAT;
uint32_t err;
CO_emergencyReport_t* em = (CO_emergencyReport_t*)CANmodule->em;
rxErrors = MCF_FlexCAN_RXECTR;
txErrors = MCF_FlexCAN_TXECTR;
ESTAT = MCF_FlexCAN_ESTAT & 0xFF00;
if(txErrors > 0xFFFF) txErrors = 0xFFFF;
if(rxErrors > 0xFF) rxErrors = 0xFF;
err = (uint32_t) (txErrors << 16 | rxErrors << 8 | ESTAT>>8);
if(CANmodule->errOld != err){
CANmodule->errOld = err;
if(txErrors >= 256){ //bus off
CO_errorReport(em, ERROR_CAN_TX_BUS_OFF, err);
}
else{ //not bus off
CO_errorReset(em, ERROR_CAN_TX_BUS_OFF, err);
if(rxErrors >= 96 || txErrors >= 96){ //bus warning
CO_errorReport(em, ERROR_CAN_BUS_WARNING, err);
}
if(rxErrors >= 128){ //RX bus passive
CO_errorReport(em, ERROR_CAN_RX_BUS_PASSIVE, err);
}
else{
CO_errorReset(em, ERROR_CAN_RX_BUS_PASSIVE, err);
}
if(txErrors >= 128){ //TX bus passive
if(!CANmodule->firstCANtxMessage){
CO_errorReport(em, ERROR_CAN_TX_BUS_PASSIVE, err);
}
}
else{
int16_t wasCleared;
wasCleared = CO_errorReset(em, ERROR_CAN_TX_BUS_PASSIVE, err);
if(wasCleared == 1) CO_errorReset(em, ERROR_CAN_TX_OVERFLOW, err);
}
if(rxErrors < 96 && txErrors < 96){ //no error
int16_t wasCleared;
wasCleared = CO_errorReset(em, ERROR_CAN_BUS_WARNING, err);
if(wasCleared == 1) CO_errorReset(em, ERROR_CAN_TX_OVERFLOW, err);
}
}
if(ESTAT&MCF_FlexCAN_ESTAT_TXWARN && ESTAT&MCF_FlexCAN_ESTAT_RXWARN){//bus warning
CO_errorReport(em, ERROR_CAN_BUS_WARNING, err);
}
else{
CO_errorReset(em, ERROR_CAN_BUS_WARNING, err);
}
}
}
/******************************************************************************/
void CO_CANinterrupt(CO_CANmodule_t *CANmodule, uint16_t ICODE){
/* receive interrupt (New CAN message is available in one of the Rx buffers) */
if(ICODE <= 13){
CO_CANrxMsg_t *rcvMsg; /* pointer to received message in CAN module */
uint16_t index; /* index of received message */
uint16_t rcvMsgIdent; /* identifier of the received message */
CO_CANrx_t *buffer = NULL; /* receive message buffer from CO_CANmodule_t object. */
bool_t msgMatched = false;
rcvMsg = &MCF_CANMB_MSG(ICODE); //structures are aligned
rcvMsgIdent = (uint16_t) rcvMsg->sid;
if(rcvMsg->RTR) rcvMsgIdent |= 0x0800;
/* CAN module filters are not used, message with any standard 11-bit identifier */
/* has been received. Search rxArray form CANmodule for the same CAN-ID. */
buffer = &CANmodule->rxArray[0];
for(index = CANmodule->rxSize; index > 0U; index--){
if(((rcvMsgIdent ^ buffer->ident) & buffer->mask) == 0U){
msgMatched = true;
break;
}
buffer++;
}
/* Call specific function, which will process the message */
if(msgMatched && (buffer != NULL) && (buffer->pFunct != NULL)){
buffer->pFunct(buffer->object, rcvMsg);
}
}
/* transmit interrupt (TX buffer 14 or 15 has finished sending) */
else if(ICODE >14 ){
/* First CAN message (bootup) was sent successfully */
CANmodule->firstCANtxMessage = false;
/* clear flag from previous message */
CANmodule->bufferInhibitFlag = false;
/* Are there any new messages waiting to be send */
if(CANmodule->CANtxCount > 0U){
uint16_t i; /* index of transmitting message */
CO_CANtx_t *buffer; /* Tx buffer */
/* first buffer */
buffer = &CANmodule->txArray[0];
/* search through whole array of pointers to transmit message buffers. */
for(i = CANmodule->txSize; i > 0U; i--){
/* if message buffer is full, send it. */
if(buffer->bufferFull){
buffer->bufferFull = false;
CANmodule->CANtxCount--;
/* Copy message to CAN buffer */
CANmodule->bufferInhibitFlag = buffer->syncFlag;
MCF_CANMB_CTRL(ICODE) = 0x0000|MCF_CANMB_CTRL_CODE(0b1000); //Tx MB inactive
MCF_CANMB_SID(ICODE) = buffer->ident;
MCF_CANMB_DATA_WORD_1(ICODE) = (uint16_t) (((buffer->data[0])<<8) | (buffer->data[1]));
MCF_CANMB_DATA_WORD_2(ICODE) = (uint16_t) (((buffer->data[2])<<8) | (buffer->data[3]));
MCF_CANMB_DATA_WORD_3(ICODE) = (uint16_t) (((buffer->data[4])<<8) | (buffer->data[5]));
MCF_CANMB_DATA_WORD_4(ICODE) = (uint16_t) (((buffer->data[6])<<8) | (buffer->data[7]));
MCF_CANMB_CTRL(ICODE) = (uint8_t) (MCF_CANMB_CTRL_CODE(0b1000) | MCF_CANMB_CTRL_LENGTH(buffer->DLC) ); //Tx MB active
break; /* exit for loop */
}
buffer++;
}/* end of for loop */
/* Clear counter if no more messages */
if(i == 0U){
CANmodule->CANtxCount = 0U;
}
}
}
}

424
stack/MCF5282/CO_driver.h Normal file
View file

@ -0,0 +1,424 @@
/*
* CAN module object for Freescale MCF5282 ColdFire V2 microcontroller.
*
* @file CO_driver.h
* @author Janez Paternoster
* @author Laurent Grosbois
* @copyright 2004 - 2014 Janez Paternoster
*
* This file is part of CANopenNode, an opensource CANopen Stack.
* Project home page is <http://canopennode.sourceforge.net>.
* For more information on CANopen see <http://www.can-cia.org/>.
*
* CANopenNode is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CO_DRIVER_H
#define CO_DRIVER_H
/* For documentation see file drvTemplate/CO_driver.h */
#include "mcf5282.h" /* processor header file */
#include <stddef.h> /* for 'NULL' */
#include <stdint.h> /* for 'int8_t' to 'uint64_t' */
/* CAN module base address */
#define ADDR_CAN1 0
#define ADDR_CAN2 (_CAN2_BASE_ADDRESS - _CAN1_BASE_ADDRESS)
/* Disabling interrupts */
extern unsigned int CO_interruptStatus;
#define CO_DISABLE_INTERRUPTS() asm{ move.w #0x2700,sr};
#define CO_ENABLE_INTERRUPTS() asm{ move.w #0x2000,sr};
/* MACRO : get information from Rx buffer */
#define MCF_CANMB_MSG(x) (*(CO_CANrxMsg_t *)(&__IPSBAR[0x1C0080 + ((x)*0x10)]))
/* Data types */
/* int8_t to uint64_t are defined in stdint.h */
typedef unsigned char bool_t;
typedef float float32_t;
typedef long double float64_t;
typedef char char_t;
typedef unsigned char oChar_t;
typedef unsigned char domain_t;
/* CAN bit rates
*
* CAN bit rates are initializers for array of eight CO_CANbitRateData_t
* objects.
*
* Macros are not used by driver itself, they may be used by application with
* combination with object CO_CANbitRateData_t.
* Application must declare following global variable depending on CO_FSYS used:
* const CO_CANbitRateData_t CO_CANbitRateData[8] = {CO_CANbitRateDataInitializers};
*
* There are initializers for eight objects, which corresponds to following
* CAN bit rates (in kbps): 10, 20, 50, 125, 250, 500, 800, 1000.
*
* CO_FSYS is internal instruction cycle clock frequency in kHz units. See
* PIC32MX documentation for more information on FSYS.
*
* Available values for FSYS:
* kbps = | 10 | 20 | 50 | 125 | 250 | 500 | 800 | 1000
* -------+----+----+----+-----+-----+-----+-----+-----
* 4 Mhz | O | O | O | O | p | - | - | -
* 8 Mhz | O | O | O | O | O | p | - | -
* 12 Mhz | O | O | O | O | p | p | - | -
* 16 Mhz | O | O | O | O | O | O | p | p
* 20 Mhz | O | O | O | O | O | O | - | p
* 24 Mhz | O | O | O | O | O | p | O | p
* 32 Mhz | p | O | O | O | O | O | p | O
* 36 Mhz | - | O | O | O | O | O | - | O
* 40 Mhz | - | O | O | O | O | O | p | O
* 48 Mhz | - | O | O | O | O | O | O | p
* 56 Mhz | - | p | O | O | O | p | (p) | p
* 64 Mhz | - | p | O | O | O | O | O | O
* 72 Mhz | - | - | O | O | O | O | O | O
* 80 Mhz | - | - | O | O | O | O | p | O
* ----------------------------------------------------
* (O=optimal; p=possible; -=not possible)
*/
#ifdef CO_FSYS
/* Macros, which divides K into (SJW + PROP + PhSeg1 + PhSeg2) */
#define TQ_x_7 1, 2, 3, 1
#define TQ_x_8 1, 2, 3, 2
#define TQ_x_9 1, 2, 4, 2
#define TQ_x_10 1, 3, 4, 2
#define TQ_x_12 1, 3, 6, 2
#define TQ_x_14 1, 4, 7, 2
#define TQ_x_15 1, 4, 8, 2 /* good timing */
#define TQ_x_16 1, 5, 8, 2 /* good timing */
#define TQ_x_17 1, 6, 8, 2 /* good timing */
#define TQ_x_18 1, 7, 8, 2 /* good timing */
#define TQ_x_19 1, 8, 8, 2 /* good timing */
#define TQ_x_20 1, 8, 8, 3 /* good timing */
#define TQ_x_21 1, 8, 8, 4
#define TQ_x_22 1, 8, 8, 5
#define TQ_x_23 1, 8, 8, 6
#define TQ_x_24 1, 8, 8, 7
#define TQ_x_25 1, 8, 8, 8
#if CO_FSYS == 4000
#define CO_CANbitRateDataInitializers \
{10, TQ_x_20}, /*CAN=10kbps*/ \
{5, TQ_x_20}, /*CAN=20kbps*/ \
{2, TQ_x_20}, /*CAN=50kbps*/ \
{1, TQ_x_16}, /*CAN=125kbps*/ \
{1, TQ_x_8 }, /*CAN=250kbps*/ \
{1, TQ_x_8 }, /*Not possible*/ \
{1, TQ_x_8 }, /*Not possible*/ \
{1, TQ_x_8 } /*Not possible*/
#elif CO_FSYS == 8000
#define CO_CANbitRateDataInitializers \
{25, TQ_x_16}, /*CAN=10kbps*/ \
{10, TQ_x_20}, /*CAN=20kbps*/ \
{5, TQ_x_16}, /*CAN=50kbps*/ \
{2, TQ_x_16}, /*CAN=125kbps*/ \
{1, TQ_x_16}, /*CAN=250kbps*/ \
{1, TQ_x_8 }, /*CAN=500kbps*/ \
{1, TQ_x_8 }, /*Not possible*/ \
{1, TQ_x_8 } /*Not possible*/
#elif CO_FSYS == 12000
#define CO_CANbitRateDataInitializers \
{40, TQ_x_15}, /*CAN=10kbps*/ \
{20, TQ_x_15}, /*CAN=20kbps*/ \
{8, TQ_x_15}, /*CAN=50kbps*/ \
{3, TQ_x_16}, /*CAN=125kbps*/ \
{2, TQ_x_12}, /*CAN=250kbps*/ \
{1, TQ_x_12}, /*CAN=500kbps*/ \
{1, TQ_x_12}, /*Not possible*/ \
{1, TQ_x_12} /*Not possible*/
#elif CO_FSYS == 16000
#define CO_CANbitRateDataInitializers \
{50, TQ_x_16}, /*CAN=10kbps*/ \
{25, TQ_x_16}, /*CAN=20kbps*/ \
{10, TQ_x_16}, /*CAN=50kbps*/ \
{4, TQ_x_16}, /*CAN=125kbps*/ \
{2, TQ_x_16}, /*CAN=250kbps*/ \
{1, TQ_x_16}, /*CAN=500kbps*/ \
{1, TQ_x_10}, /*CAN=800kbps*/ \
{1, TQ_x_8 } /*CAN=1000kbps*/
#elif CO_FSYS == 20000
#define CO_CANbitRateDataInitializers \
{50, TQ_x_20}, /*CAN=10kbps*/ \
{25, TQ_x_20}, /*CAN=20kbps*/ \
{10, TQ_x_20}, /*CAN=50kbps*/ \
{5, TQ_x_16}, /*CAN=125kbps*/ \
{2, TQ_x_20}, /*CAN=250kbps*/ \
{1, TQ_x_20}, /*CAN=500kbps*/ \
{1, TQ_x_20}, /*Not possible*/ \
{1, TQ_x_10} /*CAN=1000kbps*/
#elif CO_FSYS == 24000
#define CO_CANbitRateDataInitializers \
{63, TQ_x_19}, /*CAN=10kbps*/ \
{40, TQ_x_15}, /*CAN=20kbps*/ \
{15, TQ_x_16}, /*CAN=50kbps*/ \
{6, TQ_x_16}, /*CAN=125kbps*/ \
{3, TQ_x_16}, /*CAN=250kbps*/ \
{2, TQ_x_12}, /*CAN=500kbps*/ \
{1, TQ_x_15}, /*CAN=800kbps*/ \
{1, TQ_x_12} /*CAN=1000kbps*/
#elif CO_FSYS == 32000
#define CO_CANbitRateDataInitializers \
{64, TQ_x_25}, /*CAN=10kbps*/ \
{50, TQ_x_16}, /*CAN=20kbps*/ \
{20, TQ_x_16}, /*CAN=50kbps*/ \
{8, TQ_x_16}, /*CAN=125kbps*/ \
{4, TQ_x_16}, /*CAN=250kbps*/ \
{2, TQ_x_16}, /*CAN=500kbps*/ \
{2, TQ_x_10}, /*CAN=800kbps*/ \
{1, TQ_x_16} /*CAN=1000kbps*/
#elif CO_FSYS == 36000
#define CO_CANbitRateDataInitializers \
{50, TQ_x_18}, /*CAN=10kbps*/ \
{50, TQ_x_18}, /*CAN=20kbps*/ \
{20, TQ_x_18}, /*CAN=50kbps*/ \
{8, TQ_x_18}, /*CAN=125kbps*/ \
{4, TQ_x_18}, /*CAN=250kbps*/ \
{2, TQ_x_18}, /*CAN=500kbps*/ \
{2, TQ_x_18}, /*Not possible*/ \
{1, TQ_x_18} /*CAN=1000kbps*/
#elif CO_FSYS == 40000
#define CO_CANbitRateDataInitializers \
{50, TQ_x_20}, /*Not possible*/ \
{50, TQ_x_20}, /*CAN=20kbps*/ \
{25, TQ_x_16}, /*CAN=50kbps*/ \
{10, TQ_x_16}, /*CAN=125kbps*/ \
{5, TQ_x_16}, /*CAN=250kbps*/ \
{2, TQ_x_20}, /*CAN=500kbps*/ \
{1, TQ_x_25}, /*CAN=800kbps*/ \
{1, TQ_x_20} /*CAN=1000kbps*/
#elif CO_FSYS == 48000
#define CO_CANbitRateDataInitializers \
{63, TQ_x_19}, /*Not possible*/ \
{63, TQ_x_19}, /*CAN=20kbps*/ \
{30, TQ_x_16}, /*CAN=50kbps*/ \
{12, TQ_x_16}, /*CAN=125kbps*/ \
{6, TQ_x_16}, /*CAN=250kbps*/ \
{3, TQ_x_16}, /*CAN=500kbps*/ \
{2, TQ_x_15}, /*CAN=800kbps*/ \
{2, TQ_x_12} /*CAN=1000kbps*/
#elif CO_FSYS == 56000
#define CO_CANbitRateDataInitializers \
{61, TQ_x_23}, /*Not possible*/ \
{61, TQ_x_23}, /*CAN=20kbps*/ \
{35, TQ_x_16}, /*CAN=50kbps*/ \
{14, TQ_x_16}, /*CAN=125kbps*/ \
{7, TQ_x_16}, /*CAN=250kbps*/ \
{4, TQ_x_14}, /*CAN=500kbps*/ \
{5, TQ_x_7 }, /*CAN=800kbps*/ \
{2, TQ_x_14} /*CAN=1000kbps*/
#elif CO_FSYS == 64000
#define CO_CANbitRateDataInitializers \
{64, TQ_x_25}, /*Not possible*/ \
{64, TQ_x_25}, /*CAN=20kbps*/ \
{40, TQ_x_16}, /*CAN=50kbps*/ \
{16, TQ_x_16}, /*CAN=125kbps*/ \
{8, TQ_x_16}, /*CAN=250kbps*/ \
{4, TQ_x_16}, /*CAN=500kbps*/ \
{2, TQ_x_20}, /*CAN=800kbps*/ \
{2, TQ_x_16} /*CAN=1000kbps*/
#elif CO_FSYS == 72000
#define CO_CANbitRateDataInitializers \
{40, TQ_x_18}, /*Not possible*/ \
{40, TQ_x_18}, /*Not possible*/ \
{40, TQ_x_18}, /*CAN=50kbps*/ \
{16, TQ_x_18}, /*CAN=125kbps*/ \
{8, TQ_x_18}, /*CAN=250kbps*/ \
{4, TQ_x_18}, /*CAN=500kbps*/ \
{3, TQ_x_15}, /*CAN=800kbps*/ \
{2, TQ_x_18} /*CAN=1000kbps*/
#elif CO_FSYS == 80000
#define CO_CANbitRateDataInitializers \
{40, TQ_x_20}, /*Not possible*/ \
{40, TQ_x_20}, /*Not possible*/ \
{40, TQ_x_20}, /*CAN=50kbps*/ \
{16, TQ_x_20}, /*CAN=125kbps*/ \
{8, TQ_x_20}, /*CAN=250kbps*/ \
{4, TQ_x_20}, /*CAN=500kbps*/ \
{2, TQ_x_25}, /*CAN=800kbps*/ \
{2, TQ_x_20} /*CAN=1000kbps*/
#else
#error define_CO_FSYS CO_FSYS not supported
#endif
#endif
/* Structure contains timing coefficients for CAN module.
*
* CAN baud rate is calculated from following equations:
* Fsys - System clock (MAX 80MHz for PIC32MX)
* TQ = 2 * BRP / Fsys - Time Quanta
* BaudRate = 1 / (TQ * K) - Can bus Baud Rate
* K = SJW + PROP + PhSeg1 + PhSeg2 - Number of Time Quantas
*/
typedef struct{
uint8_t BRP; /* (1...64) Baud Rate Prescaler */
uint8_t SJW; /* (1...4) SJW time */
uint8_t PROP; /* (1...8) PROP time */
uint8_t phSeg1; /* (1...8) Phase Segment 1 time */
uint8_t phSeg2; /* (1...8) Phase Segment 2 time */
}CO_CANbitRateData_t;
/* Return values */
typedef enum{
CO_ERROR_NO = 0,
CO_ERROR_ILLEGAL_ARGUMENT = -1,
CO_ERROR_OUT_OF_MEMORY = -2,
CO_ERROR_TIMEOUT = -3,
CO_ERROR_ILLEGAL_BAUDRATE = -4,
CO_ERROR_RX_OVERFLOW = -5,
CO_ERROR_RX_PDO_OVERFLOW = -6,
CO_ERROR_RX_MSG_LENGTH = -7,
CO_ERROR_RX_PDO_LENGTH = -8,
CO_ERROR_TX_OVERFLOW = -9,
CO_ERROR_TX_PDO_WINDOW = -10,
CO_ERROR_TX_UNCONFIGURED = -11,
CO_ERROR_PARAMETERS = -12,
CO_ERROR_DATA_CORRUPT = -13,
CO_ERROR_CRC = -14
}CO_ReturnError_t;
/* CAN receive message structure as aligned in CAN module. */
typedef struct{
unsigned timestamp :8; /* 8 bits timestamp, see MCF5282 documentation */
unsigned code :4; /* Message Buffer code. see MCF52825 documentation */
unsigned DLC :4; /* Data length code */
unsigned sid :11;/* Standard Identifier - 11bits */
unsigned RTR :1; /* Remote Transmission Request bit */
unsigned :4;
unsigned timestamp16 :16;/* See MCF5282 documentation */
uint8_t data[8]; /* 8 data bytes */
}CO_CANrxMsg_t;
/* Received message object */
typedef struct{
uint16_t ident;
uint16_t mask;
void *object;
void (*pFunct)(void *object, const CO_CANrxMsg_t *message);
}CO_CANrx_t;
/* Transmit message object. */
typedef struct{
uint8_t DLC;
uint16_t ident;
uint8_t data[8];
volatile bool_t bufferFull;
volatile bool_t syncFlag;
}CO_CANtx_t;
/* CAN module object. */
typedef struct{
uint16_t CANbaseAddress;
CO_CANrxMsg_t *CANmsgBuff;
uint8_t CANmsgBuffSize;
CO_CANrx_t *rxArray;
uint16_t rxSize;
CO_CANtx_t *txArray;
uint16_t txSize;
volatile bool_t useCANrxFilters;
volatile bool_t bufferInhibitFlag;
volatile bool_t firstCANtxMessage;
volatile uint16_t CANtxCount;
uint32_t errOld;
void *em;
}CO_CANmodule_t;
/* Endianes */
#define CO_LITTLE_ENDIAN
/* Request CAN configuration or normal mode */
void CO_CANsetConfigurationMode(uint16_t CANbaseAddress);
void CO_CANsetNormalMode(uint16_t CANbaseAddress);
/* Initialize CAN module object.
*
* MCF5282 FlexCAN configuration: 16 buffers are available.
* Buffers [0..13] are used for reception
* Buffers [14..15] are used for reception
*/
CO_ReturnError_t CO_CANmodule_init(
CO_CANmodule_t *CANmodule,
uint16_t CANbaseAddress,
CO_CANrx_t rxArray[],
uint16_t rxSize,
CO_CANtx_t txArray[],
uint16_t txSize,
uint16_t CANbitRate); /* Valid values are (in kbps): 125, 1000. If value is illegal, bitrate defaults to 125. */
/* Switch off CANmodule. */
void CO_CANmodule_disable(CO_CANmodule_t *CANmodule);
/* Read CAN identifier */
uint16_t CO_CANrxMsg_readIdent(const CO_CANrxMsg_t *rxMsg);
/* Configure CAN message receive buffer. */
CO_ReturnError_t CO_CANrxBufferInit(
CO_CANmodule_t *CANmodule,
uint16_t index,
uint16_t ident,
uint16_t mask,
bool_t rtr,
void *object,
void (*pFunct)(void *object, const CO_CANrxMsg_t *message));
/* Configure CAN message transmit buffer. */
CO_CANtx_t *CO_CANtxBufferInit(
CO_CANmodule_t *CANmodule,
uint16_t index,
uint16_t ident,
bool_t rtr,
uint8_t noOfBytes,
bool_t syncFlag);
/* Send CAN message. */
CO_ReturnError_t CO_CANsend(CO_CANmodule_t *CANmodule, CO_CANtx_t *buffer);
/* Clear all synchronous TPDOs from CAN module transmit buffers. */
void CO_CANclearPendingSyncPDOs(CO_CANmodule_t *CANmodule);
/* Verify all errors of CAN module. */
void CO_CANverifyErrors(CO_CANmodule_t *CANmodule);
/* CAN interrupt receives and transmits CAN messages. */
void CO_CANinterrupt(CO_CANmodule_t *CANmodule, uint16_t ICODE);
#endif

View file

@ -481,7 +481,7 @@ void CO_CANinterrupt(CO_CANmodule_t *CANmodule){
uint8_t ICODE;
ICODE = (uint8_t) CAN_REG(CANmodule->CANbaseAddress, C_VEC) & 0x7F;
/* receive interrupt (New CAN messagge is available in RX FIFO 0 buffer) */
/* receive interrupt (New CAN message is available in RX FIFO 0 buffer) */
if(ICODE == 0){
CO_CANrxMsg_t *rcvMsg; /* pointer to received message in CAN module */
uint16_t index; /* index of received message */