Example files moved to base directory. Added template for socketCAN drivers (based on BECK SC 243)

This commit is contained in:
JanezXD 2015-07-30 19:58:06 +02:00
parent 8104f87056
commit 12f33a4a7b
29 changed files with 2943 additions and 1469 deletions

7
.gitignore vendored
View File

@ -1,3 +1,10 @@
canopennode
*.o
doc/
#eclipse
.cproject
.project
.settings/

3173
Doxyfile

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,8 @@
# Makefile for CANopenNode, basic compile with no CAN device.
STACK_SRC = ../stack
STACKDRV_SRC = ../stack/drvTemplate
STACK_SRC = stack
STACKDRV_SRC = stack/drvTemplate
INCLUDE_DIRS = $(STACK_SRC) \
@ -30,14 +30,16 @@ CC = gcc
CFLAGS = -Wall -I$(INCLUDE_DIRS)
%.o: %.c
$(CC) $(CFLAGS) -c -o $*.o $<
canopennode: $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o canopennode
.PHONY: all clean
all: canopennode
clean:
rm -f $(OBJS) canopennode
%.o: %.c
$(CC) $(CFLAGS) -c -o $*.o $<
canopennode: $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o canopennode

105
README Normal file
View File

@ -0,0 +1,105 @@
CANopenNode is an opensource CANopen Stack.
CANopen is the internationally standardized (EN 50325-4) CAN-based higher-layer protocol for embedded control system. For more information on CANopen see http://www.can-cia.org/
Stack is written in ANSI C in object-oriented way. Code is documented. License is LGPL V2.1.
Variables (communication, device, custom) are ordered in CANopen object dictionary and are accessible from both: C code and from CAN network.
CANopen Features:
- NMT slave to start, stop, reset device.
- Heartbeat producer/consumer error control.
- PDO linking and dynamic mapping for fast exchange of process variables.
- SDO expedited, segmented and block transfer for service access to all parameters.
- SDO master.
- Emergency message.
- Sync producer/consumer.
- Nonvolatile storage.
Supported microcontrollers:
- Template for new microcontroller support for CANopenNode:
- Contributed by Janez Paternoster
- Status:
- Well commented.
- Should compile on any system.
- No interface to any CAN hardware.
- Template for parameter storage via objects 0x1010 and 0x1011.
- Linux socketCAN:
- Contributed by Janez Paternoster
- Status:
- ...
- eCos RTOS:
- The driver supports any hardware that is supported by the eCos CAN framework.
See http://ecos.sourceware.org/
- Contributed by Uwe Kindler:
- See: http://sourceforge.net/p/canopennode/discussion/387151/thread/7603e3b5/
- Tested (sep 2013):
- Olimex LCP-E2294-1MB board: https://www.olimex.com/Products/ARM/NXP/LPC-E2294-1MB/
- Olimex LPC-L2294-1MB board: https://www.olimex.com/Products/ARM/NXP/LPC-L2294-1MB/
- Status:
- stable.
- Parameter storage via objects 0x1010 and 0x1011 (eCos generic flash support).
- Microchip PIC32MX:
- Contributed by Janez Paternoster
- Tested (date):
-
- Status:
- Stable.
- Parameter storage via objects 0x1010 and 0x1011 (external eeprom).
- Microchip PIC24H and dsPIC33F:
- Contributed by Janez Paternoster
- Tested (date):
-
- Status:
- stable/unconfirmed
- Microchip dsPIC30F:
- Contributed by Janez Paternoster
- Tested (?):
- NXP LPC177x (Cortex M3) and FreeRTOS.
- Contributed by AmitH(sourceforge) (17.9.2014):
- See: http://sourceforge.net/p/canopennode/discussion/387151/thread/d1b43992
- Status:
- unconfirmed
- Atmel SAM3X:
- Used with Atmel ASF library.
- Contributed by Olof Larsson (dec 2014):
- See: http://sourceforge.net/p/canopennode/discussion/387151/thread/8e789d60/
- Status:
- unconfirmed
- ST STM32:
- Contributed by ?
- Status:
- unknown
Other known implementations with source code:
- AD ADSP-CM408 mixed signal controller
- http://www.analog.com/en/processors-dsp/cm4xx/adsp-cm408f/products/cm40x-ez/eb.html
- Contributed by Analog devices, Inc. (dec 2014):
- See: http://sourceforge.net/p/canopennode/discussion/387151/thread/bfbcab97/
- Package and setup user guide are available at:
- http://sourceforge.net/projects/canopennode-for-adsp-cm408f/
- http://sourceforge.net/projects/canopennode-for-adsp-cm408f/files
Discontinued implementations from earlier versions of CANopenNode:
- Microchip PIC18F
- BECK IPC Embedded Web-Controller SC243
History of the project:
Project was initially hosted on http://sourceforge.net/projects/canopennode/
It started in 2004 with PIC18F microcontrollers from Microchip.
Fresh, cleaned repository of CANopenNode stack started on 25.7.2015.
For older history see http://sourceforge.net/p/canopennode/code_complete/

View File

@ -1,31 +0,0 @@
CANopenNode is an opensource CANopen Stack.
CANopen is the internationally standardized (EN 50325-4) CAN-based higher-layer protocol for embedded control system. For more information on CANopen see http://www.can-cia.org/
Stack is written in ANSI C in object-oriented way. Code is documented. License is LGPL V2.1.
Variables (communication, device, custom) are ordered in CANopen object dictionary and are accessible from both: C code and from CAN network.
CANopen Features:
- NMT slave to start, stop, reset device.
- Heartbeat producer/consumer error control.
- PDO linking and dynamic mapping for fast exchange of process variables.
- SDO expedited, segmented and block transfer for service access to all parameters.
- SDO master.
- Emergency message.
- Sync producer/consumer.
- Nonvolatile storage.
Supported controllers:
- Microchip: PIC32, PIC24H, dsPIC33F, dsPIC30F
- Linux SocketCAN
- eCos RTOS with all supported microcontrollers
- Third parity contributors: LPC177x_8x, SAM3X, STM32
History of the project:
Project was initially hosted on http://sourceforge.net/projects/canopennode/
It started in 2004 with PIC18F microcontrollers from Microchip.
Fresh, cleaned repository of CANopenNode stack started on 25.7.2015.
For older history see http://sourceforge.net/p/canopennode/code_complete/

View File

@ -1,5 +0,0 @@
Driver files for NXP LPC177x (Cortex M3) and FreeRTOS.
Contributed by AmitH(sourceforge)
Date of contribution: 17.9.2014
See: http://sourceforge.net/p/canopennode/discussion/387151/thread/d1b43992

View File

@ -1,5 +0,0 @@
CANopenNode driver for Microchip PIC24H_dsPIC33F microcontrollers.
Contributed by Janez Paternoster
Status: stable/unconfirmed

View File

@ -1,7 +0,0 @@
CANopenNode driver for Microchip PIC32 microcontrollers.
Contributed by Janez Paternoster
Status:
- Stable.
- Parameter storage via objects 0x1010 and 0x1011 (external eeprom).

View File

@ -1,7 +0,0 @@
Driver files for SAM3X using the Atmel ASF library.
Contributed by Joshua(sourceforge)
Date of contribution: 12.12.2014
See: https://sourceforge.net/p/canopennode/discussion/387151/thread/8e789d60/
Status: unverified

View File

@ -1,5 +1,5 @@
/*
* CAN module object for Microchip STM32F103 microcontroller.
* CAN module object for ST STM32F103 microcontroller.
*
* @file CO_driver.c
* @author Janez Paternoster

View File

@ -1,5 +1,5 @@
/*
* CAN module object for Microchip STM32F103 microcontroller.
* CAN module object for ST STM32F103 microcontroller.
*
* @file CO_driver.h
* @author Janez Paternoster

View File

@ -1,6 +0,0 @@
Driver files for STM32
Contributed by ?
Date of contribution: ?
Status: unknown

View File

@ -41,11 +41,11 @@
*/
static CO_SDO_abortCode_t CO_ODF_1010(CO_ODF_arg_t *ODF_arg);
static CO_SDO_abortCode_t CO_ODF_1010(CO_ODF_arg_t *ODF_arg){
CO_EE_t *ee;
//CO_EE_t *ee;
uint32_t value;
CO_SDO_abortCode_t ret = CO_SDO_AB_NONE;
ee = (CO_EE_t*) ODF_arg->object;
//ee = (CO_EE_t*) ODF_arg->object;
value = CO_getUint32(ODF_arg->data);
if(!ODF_arg->reading){
@ -78,11 +78,11 @@ static CO_SDO_abortCode_t CO_ODF_1010(CO_ODF_arg_t *ODF_arg){
*/
static CO_SDO_abortCode_t CO_ODF_1011(CO_ODF_arg_t *ODF_arg);
static CO_SDO_abortCode_t CO_ODF_1011(CO_ODF_arg_t *ODF_arg){
CO_EE_t *ee;
//CO_EE_t *ee;
uint32_t value;
CO_SDO_abortCode_t ret = CO_SDO_AB_NONE;
ee = (CO_EE_t*) ODF_arg->object;
//ee = (CO_EE_t*) ODF_arg->object;
value = CO_getUint32(ODF_arg->data);
if(!ODF_arg->reading){

View File

@ -1,9 +0,0 @@
Template for new microcontroller support for CANopenNode.
Contributed by Janez Paternoster
Status:
- Well commented.
- Should compile on any system.
- No interface to any CAN hardware.
- Template for parameter storage via objects 0x1010 and 0x1011.

View File

@ -1,5 +0,0 @@
CANopenNode driver for Microchip dsPIC30F microcontrollers.
Contributed by Janez Paternoster
Last usage: Long time ago

View File

@ -1,17 +0,0 @@
CANopenNode driver for eCos CAN framework.
The driver supports any hardware that is supported by the eCos CAN framework.
See http://ecos.sourceware.org/
The driver was developed and tested with the Olimex LCP-E2294-1MB board:
https://www.olimex.com/Products/ARM/NXP/LPC-E2294-1MB/
and with the Olimex LPC-L2294-1MB board:
https://www.olimex.com/Products/ARM/NXP/LPC-L2294-1MB/
Contributed by Uwe Kindler
Date of contribution: 6.10.2013
See: http://sourceforge.net/p/canopennode/discussion/387151/thread/7603e3b5/
Status:
- stable
- Parameter storage via objects 0x1010 and 0x1011 (eCos generic flash support).

460
stack/socketCAN/CO_driver.c Normal file
View File

@ -0,0 +1,460 @@
/*
* CAN module object for BECK SC243 computer.
*
* @file CO_driver.c
* @version SVN: \$Id$
* @author Janez Paternoster
* @copyright 2004 - 2013 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"
#include <string.h> /* for memcpy */
#ifdef USE_CAN_CALLBACKS
int CAN1callback(CanEvent event, const CanMsg *msg);
int CAN2callback(CanEvent event, const CanMsg *msg);
#endif
/******************************************************************************/
void CO_CANsetConfigurationMode(uint16_t CANbaseAddress){
canEnableRx(CANbaseAddress, FALSE);
}
/******************************************************************************/
void CO_CANsetNormalMode(uint16_t CANbaseAddress){
canEnableRx(CANbaseAddress, TRUE);
}
/******************************************************************************/
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;
/* Configure object variables */
CANmodule->CANbaseAddress = CANbaseAddress;
CANmodule->rxArray = rxArray;
CANmodule->rxSize = rxSize;
CANmodule->txArray = txArray;
CANmodule->txSize = txSize;
CANmodule->bufferInhibitFlag = CO_false; /* True, if CAN message was sent, reset by interrupt. */
CANmodule->firstCANtxMessage = CO_true;
CANmodule->error = 0;
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 = CO_false;
}
/* initialize port */
CanError e = canInit(CANbaseAddress, CANbitRate, 0);
if(e == CAN_ERROR_ILLEGAL_BAUDRATE)
e = canInit(CANbaseAddress, 125, 0);
switch(e){
case CO_ERROR_NO: break;
case CAN_ERROR_OUT_OF_MEMORY: return CO_ERROR_OUT_OF_MEMORY;
default: return CO_ERROR_ILLEGAL_ARGUMENT;
}
/* Set acceptance filters to accept all messages with standard identifier, also accept rtr */
CanFilterSc2x3 filter;
filter.controllerType = CAN_FILTER_CONTROLLER_TYPE_SC2X3;
filter.structVer = 1;
filter.mode = CAN_FILTER_SC2X3_MODE_32_BIT;
filter.filters.f32[0].idMask = canEncodeId(0x7FF, FALSE, TRUE);
filter.filters.f32[0].idValue = canEncodeId(0x7FF, FALSE, FALSE);
switch(canSetFilter(CANbaseAddress, (CanFilter *)&filter)){
case CO_ERROR_NO: break;
default: return CO_ERROR_ILLEGAL_ARGUMENT;
}
/* purge buffers */
canEnableRx(CANbaseAddress, FALSE);
canPurgeRx(CANbaseAddress);
canPurgeTx(CANbaseAddress, FALSE);
/* register callback function */
#ifdef USE_CAN_CALLBACKS
if(CANbaseAddress == CAN_PORT_CAN1)
canRegisterCallback(CANbaseAddress, CAN1callback, (1 << CAN_EVENT_RX) | (1 << CAN_EVENT_TX) | (1 << CAN_EVENT_BUS_OFF) | (1 << CAN_EVENT_OVERRUN));
else if(CANbaseAddress == CAN_PORT_CAN2)
canRegisterCallback(CANbaseAddress, CAN2callback, (1 << CAN_EVENT_RX) | (1 << CAN_EVENT_TX) | (1 << CAN_EVENT_BUS_OFF) | (1 << CAN_EVENT_OVERRUN));
#endif
return CO_ERROR_NO;
}
/******************************************************************************/
void CO_CANmodule_disable(CO_CANmodule_t *CANmodule){
canDeinit(CANmodule->CANbaseAddress);
}
/******************************************************************************/
uint16_t CO_CANrxMsg_readIdent(const CO_CANrxMsg_t *rxMsg){
return (uint16_t) canDecodeId(rxMsg->ident);
}
/******************************************************************************/
CO_ReturnError_t CO_CANrxBufferInit(
CO_CANmodule_t *CANmodule,
uint16_t index,
uint16_t ident,
uint16_t mask,
CO_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;
/* Configure CAN identifier and CAN mask, bit aligned with CAN module. */
/* No hardware filtering is used. */
buffer->ident = canEncodeId(ident, FALSE, rtr);
buffer->mask = canEncodeId(mask, FALSE, FALSE);
}
else{
ret = CO_ERROR_ILLEGAL_ARGUMENT;
}
return ret;
}
/******************************************************************************/
CO_CANtx_t *CO_CANtxBufferInit(
CO_CANmodule_t *CANmodule,
uint16_t index,
uint16_t ident,
CO_bool_t rtr,
uint8_t noOfBytes,
CO_bool_t syncFlag)
{
CO_CANtx_t *buffer = NULL;
if((CANmodule != NULL) && (index < CANmodule->txSize)){
/* get specific buffer */
buffer = &CANmodule->txArray[index];
/* CAN identifier, bit aligned with CAN module registers */
buffer->ident = canEncodeId(ident, FALSE, rtr);
buffer->DLC = noOfBytes;
buffer->bufferFull = CO_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;
CanError canErr = CAN_ERROR_NO;
#ifdef USE_CAN_CALLBACKS
/* 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, ERROR_CAN_TX_OVERFLOW, CO_EMC_CAN_OVERRUN, canDecodeId(buffer->ident));
}
err = CO_ERROR_TX_OVERFLOW;
}
CO_DISABLE_INTERRUPTS();
/* if CAN TX buffer is free, copy message to it */
if(CANmodule->bufferInhibitFlag == CO_false && CANmodule->CANtxCount == 0){
canErr = canSend(CANmodule->CANbaseAddress, (const CanMsg*) buffer, FALSE);
CANmodule->bufferInhibitFlag = CO_true; /* indicate, that message is on CAN module */
#ifdef CO_LOG_CAN_MESSAGES
memcpy((void*)&CANmodule->txRecord, (void*)buffer, sizeof(CO_CANtx_t));
#endif
}
/* if no buffer is free, message will be sent by interrupt */
else{
buffer->bufferFull = CO_true;
CANmodule->CANtxCount++;
}
CO_ENABLE_INTERRUPTS();
#else
CO_DISABLE_INTERRUPTS();
canErr = canSend(CANmodule->CANbaseAddress, (const CanMsg*) buffer, FALSE);
#ifdef CO_LOG_CAN_MESSAGES
void CO_logMessage(const CanMsg *msg);
CO_logMessage((const CanMsg*) buffer);
#endif
CO_ENABLE_INTERRUPTS();
#endif
if(canErr != CAN_ERROR_NO){
CO_errorReport((CO_EM_t*)CANmodule->em, CO_EM_GENERIC_ERROR, CO_EMC_GENERIC, canErr);
err = CO_ERROR_TX_OVERFLOW;
}
return err;
}
/******************************************************************************/
void CO_CANclearPendingSyncPDOs(CO_CANmodule_t *CANmodule){
#ifdef USE_CAN_CALLBACKS
uint32_t tpdoDeleted = 0U;
CO_DISABLE_INTERRUPTS();
/* Abort message from CAN module, if there is synchronous TPDO.
* Functionality is not used. */
/* delete also pending synchronous TPDOs in TX buffers */
if(CANmodule->CANtxCount != 0U){
uint16_t i;
CO_CANtx_t *buffer = &CANmodule->txArray[0];
for(i = CANmodule->txSize; i > 0U; i--){
if(buffer->bufferFull){
if(buffer->syncFlag){
buffer->bufferFull = CO_false;
CANmodule->CANtxCount--;
tpdoDeleted = 2U;
}
}
buffer++;
}
}
CO_ENABLE_INTERRUPTS();
if(tpdoDeleted != 0U){
CO_errorReport((CO_EM_t*)CANmodule->em, CO_EM_TPDO_OUTSIDE_WINDOW, CO_EMC_COMMUNICATION, tpdoDeleted);
}
#endif
}
/******************************************************************************/
void CO_CANverifyErrors(CO_CANmodule_t *CANmodule){
#if function_canGetErrorCounters_works_without_loosing_messages
unsigned rxErrors, txErrors;
CO_EM_t* em = (CO_EM_t*)CANmodule->em;
uint32_t err;
canGetErrorCounters(CANmodule->CANbaseAddress, &rxErrors, &txErrors);
if(txErrors > 0xFFFF) txErrors = 0xFFFF;
if(rxErrors > 0xFF) rxErrors = 0xFF;
err = ((uint32_t)txErrors << 16) | ((uint32_t)rxErrors << 8) | CANmodule->error;
if(CANmodule->errOld != err){
CANmodule->errOld = err;
if(txErrors >= 256U){ /* bus off */
CO_errorReport(em, CO_EM_CAN_TX_BUS_OFF, CO_EMC_BUS_OFF_RECOVERED, err);
}
else{ /* not bus off */
CO_errorReset(em, CO_EM_CAN_TX_BUS_OFF, err);
if((rxErrors >= 96U) || (txErrors >= 96U)){ /* bus warning */
CO_errorReport(em, CO_EM_CAN_BUS_WARNING, CO_EMC_NO_ERROR, err);
}
if(rxErrors >= 128U){ /* RX bus passive */
CO_errorReport(em, CO_EM_CAN_RX_BUS_PASSIVE, CO_EMC_CAN_PASSIVE, err);
}
else{
CO_errorReset(em, CO_EM_CAN_RX_BUS_PASSIVE, err);
}
if(txErrors >= 128U){ /* TX bus passive */
if(!CANmodule->firstCANtxMessage){
CO_errorReport(em, CO_EM_CAN_TX_BUS_PASSIVE, CO_EMC_CAN_PASSIVE, err);
}
}
else{
CO_bool_t isError = CO_isError(em, CO_EM_CAN_TX_BUS_PASSIVE);
if(isError){
CO_errorReset(em, CO_EM_CAN_TX_BUS_PASSIVE, err);
CO_errorReset(em, CO_EM_CAN_TX_OVERFLOW, err);
}
}
if((rxErrors < 96U) && (txErrors < 96U)){ /* no error */
CO_bool_t isError = CO_isError(em, CO_EM_CAN_BUS_WARNING);
if(isError){
CO_errorReset(em, CO_EM_CAN_BUS_WARNING, err);
CO_errorReset(em, CO_EM_CAN_TX_OVERFLOW, err);
}
}
}
if(CANmodule->error & 0x02){ /* CAN RX bus overflow */
CO_errorReport(em, CO_EM_CAN_RXB_OVERFLOW, CO_EMC_CAN_OVERRUN, err);
}
}
#endif
}
#ifdef USE_CAN_CALLBACKS
/******************************************************************************/
int CO_CANinterrupt(CO_CANmodule_t *CANmodule, CanEvent event, const CanMsg *msg){
/* receive interrupt (New CAN messagge is available in RX FIFO buffer) */
if(event == CAN_EVENT_RX){
CO_CANrxMsg_t *rcvMsg; /* pointer to received message in CAN module */
uint16_t i; /* index of received message */
CO_CANrx_t *buffer = NULL; /* receive message buffer from CO_CANmodule_t object. */
CO_bool_t msgMatched = CO_false;
rcvMsg = (CO_CANrxMsg_t *) msg; /* structures are aligned */
/* 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(i = CANmodule->rxSize; i > 0; i--){
if(((rcvMsg->ident ^ buffer->ident) & buffer->mask) == 0){
msgMatched = CO_true;
break;
}
buffer++;
}
/* Call specific function, which will process the message */
if(msgMatched && (buffer != NULL) && (buffer->pFunct != NULL)){
buffer->pFunct(buffer->object, rcvMsg);
}
#ifdef CO_LOG_CAN_MESSAGES
void CO_logMessage(const CanMsg *msg);
CO_logMessage(msg);
#endif
return 0;
}
/* transmit interrupt (TX buffer is free) */
if(event == CAN_EVENT_TX){
/* First CAN message (bootup) was sent successfully */
CANmodule->firstCANtxMessage = CO_false;
/* clear flag from previous message */
CANmodule->bufferInhibitFlag = CO_false;
#ifdef CO_LOG_CAN_MESSAGES
void CO_logMessage(const CanMsg *msg);
CO_logMessage((const CanMsg*) &CANmodule->txRecord);
#endif
/* Are there any new messages waiting to be send */
if(CANmodule->CANtxCount > 0U){
uint16_t i; /* index of transmitting message */
/* first buffer */
CO_CANtx_t *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 = CO_false;
CANmodule->CANtxCount--;
CANmodule->bufferInhibitFlag = CO_true; /* indicate, that message is on CAN module */
/* Copy message to CAN buffer and exit for loop. */
canSend(CANmodule->CANbaseAddress, (const CanMsg*) buffer, FALSE);
#ifdef CO_LOG_CAN_MESSAGES
memcpy((void*)&CANmodule->txRecord, (void*)buffer, sizeof(CO_CANtx_t));
#endif
break; /* exit for loop */
}
buffer++;
}/* end of for loop */
/* Clear counter if no more messages */
if(i == 0U){
CANmodule->CANtxCount = 0U;
}
}
return 0;
}
if(event == CAN_EVENT_BUS_OFF){
CANmodule->error |= 0x01;
return 0;
}
if(event == CAN_EVENT_OVERRUN){
CANmodule->error |= 0x02;
return 0;
}
return 0;
}
#else
void CO_CANreceive(CO_CANmodule_t *CANmodule){
/* pool the messages from receive buffer */
while(canPeek(CANmodule->CANbaseAddress, 0) == CAN_ERROR_NO){
CO_CANrxMsg_t rcvMsg;
uint16_t i; /* index of received message */
CO_CANrx_t *buffer;/* receive message buffer from CO_CANmodule_t object. */
CO_bool_t msgMatched = CO_false;
canRecv(CANmodule->CANbaseAddress, (CanMsg*)&rcvMsg, 0);
buffer = &CANmodule->rxArray[0];
for(i = CANmodule->rxSize; i > 0; i--){
if(((rcvMsg.ident ^ buffer->ident) & buffer->mask) == 0){
msgMatched = CO_true;
break;
}
buffer++;
}
/* Call specific function, which will process the message */
if(msgMatched && (buffer->pFunct != 0)){
buffer->pFunct(buffer->object, &rcvMsg);
}
#ifdef CO_LOG_CAN_MESSAGES
void CO_logMessage(const CanMsg *msg);
CO_logMessage((CanMsg*)&rcvMsg);
#endif
}
}
#endif

206
stack/socketCAN/CO_driver.h Normal file
View File

@ -0,0 +1,206 @@
/*
* CAN module object for BECK SC243 computer.
*
* @file CO_driver.h
* @version SVN: \$Id$
* @author Janez Paternoster
* @copyright 2004 - 2013 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
#include <clib.h> /* processor header file */
#include <stddef.h> /* for 'NULL' */
#include <stdint.h> /* for 'int8_t' to 'uint64_t' */
/* Peripheral addresses */
#define ADDR_CAN1 CAN_PORT_CAN1
#define ADDR_CAN2 CAN_PORT_CAN2
/* Disabling interrupts */
#define CO_DISABLE_INTERRUPTS() MaskInterrupts()
#define CO_ENABLE_INTERRUPTS() EnableInterrupts()
/* Other configuration */
#define CO_LOG_CAN_MESSAGES /* Call external function for each received
or transmitted CAN message. */
#define CO_SDO_BUFFER_SIZE 889 /* Override default SDO buffer size. */
//#define USE_CAN_CALLBACKS /* If defined, callbacks will be used for CAN RX and TX instead pooling.
/* Data types */
typedef unsigned char CO_bool_t;
typedef enum{
CO_false = 0,
CO_true = 1
}CO_boolval_t;
/* int8_t to uint64_t are defined in stdint.h */
typedef float float32_t;
typedef double float64_t;
typedef char char_t;
typedef unsigned char oChar_t;
typedef unsigned char domain_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. In SC2x3 this
* structure has the same alignment as in the _CanMsg_ structure from canAPI.h */
typedef struct{
uint32_t ident;
uint8_t DLC;
uint8_t data[8];
}CO_CANrxMsg_t;
/* Received message object */
typedef struct{
uint32_t ident;
uint32_t mask;
void *object;
void (*pFunct)(void *object, const CO_CANrxMsg_t *message);
}CO_CANrx_t;
/* Transmit message object. In SC2x3 this structure has the same alignment
* of first three members as in the _CanMsg_ structure from canAPI.h */
typedef struct{
uint32_t ident;
uint8_t DLC;
uint8_t data[8];
volatile CO_bool_t bufferFull;
volatile CO_bool_t syncFlag;
}CO_CANtx_t;
/* CAN module object. */
typedef struct{
uint16_t CANbaseAddress;
#ifdef CO_LOG_CAN_MESSAGES
CO_CANtx_t txRecord;
#endif
CO_CANrx_t *rxArray;
uint16_t rxSize;
CO_CANtx_t *txArray;
uint16_t txSize;
volatile CO_bool_t bufferInhibitFlag;
volatile CO_bool_t firstCANtxMessage;
volatile uint8_t error;
volatile uint16_t CANtxCount;
uint32_t errOld;
void *em;
}CO_CANmodule_t;
/* Endianes */
#define CO_BIG_ENDIAN
/* Request CAN configuration or normal mode */
void CO_CANsetConfigurationMode(uint16_t CANbaseAddress);
void CO_CANsetNormalMode(uint16_t CANbaseAddress);
/* Initialize CAN module object. */
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);
/* 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,
CO_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,
CO_bool_t rtr,
uint8_t noOfBytes,
CO_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.
*
* @param event From _CanCallback_ function from _CAN API_ for SC243.
* @param msg From _CanCallback_ function from _CAN API_ for SC243.
*
* @return For _CanCallback_ function from _CAN API_ for SC243.
*/
int CO_CANinterrupt(CO_CANmodule_t *CANmodule, CanEvent event, const CanMsg *msg);
#endif

240
stack/socketCAN/eeprom.c Normal file
View File

@ -0,0 +1,240 @@
/*
* Eeprom object for BECK SC243 computer.
*
* @file eeprom.c
* @version SVN: \$Id$
* @author Janez Paternoster
* @copyright 2004 - 2013 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_SDO.h"
#include "CO_Emergency.h"
#include "eeprom.h"
#include "crc16-ccitt.h"
#include <string.h> /* for memcpy */
#include <stdlib.h> /* for malloc, free */
/******************************************************************************/
static CO_SDO_abortCode_t CO_ODF_1010(CO_ODF_arg_t *ODF_arg){
CO_EE_t *ee;
uint32_t value;
CO_SDO_abortCode_t ret = CO_SDO_AB_NONE;
ee = (CO_EE_t*) ODF_arg->object;
value = CO_getUint32(ODF_arg->data);
if(!ODF_arg->reading){
/* don't change the old value */
CO_memcpy(ODF_arg->data, (const uint8_t*)ODF_arg->ODdataStorage, 4U);
if(ODF_arg->subIndex == 1){
if(value == 0x65766173UL){
/* store parameters */
/* rename current file to .old */
remove(EE_ROM_FILENAME_OLD);
rename(EE_ROM_FILENAME, EE_ROM_FILENAME_OLD);
/* open a file */
FILE *fp = fopen(EE_ROM_FILENAME, "wb");
if(!fp){
rename(EE_ROM_FILENAME_OLD, EE_ROM_FILENAME);
return CO_SDO_AB_HW;
}
/* write data to the file */
fwrite((const void *)ee->OD_ROMAddress, 1, ee->OD_ROMSize, fp);
/* write CRC to the end of the file */
uint16_t CRC = crc16_ccitt((unsigned char*)ee->OD_ROMAddress, ee->OD_ROMSize, 0);
fwrite((const void *)&CRC, 1, 2, fp);
fclose(fp);
/* verify data */
void *buf = malloc(ee->OD_ROMSize + 4);
if(buf){
fp = fopen(EE_ROM_FILENAME, "rb");
uint32_t cnt = 0;
uint16_t CRC2 = 0;
if(fp){
cnt = fread(buf, 1, ee->OD_ROMSize, fp);
CRC2 = crc16_ccitt((unsigned char*)buf, ee->OD_ROMSize, 0);
/* read also two bytes of CRC */
cnt += fread(buf, 1, 4, fp);
fclose(fp);
}
free(buf);
if(cnt == (ee->OD_ROMSize + 2) && CRC == CRC2){
/* write successful */
return CO_SDO_AB_NONE;
}
}
/* error, set back the old file */
remove(EE_ROM_FILENAME);
rename(EE_ROM_FILENAME_OLD, EE_ROM_FILENAME);
return CO_SDO_AB_HW;
}
else
return CO_SDO_AB_DATA_TRANSF;
}
}
return ret;
}
/******************************************************************************/
static CO_SDO_abortCode_t CO_ODF_1011(CO_ODF_arg_t *ODF_arg){
CO_EE_t *ee;
uint32_t value;
CO_SDO_abortCode_t ret = CO_SDO_AB_NONE;
ee = (CO_EE_t*) ODF_arg->object;
value = CO_getUint32(ODF_arg->data);
if(!ODF_arg->reading){
/* don't change the old value */
CO_memcpy(ODF_arg->data, (const uint8_t*)ODF_arg->ODdataStorage, 4U);
if(ODF_arg->subIndex >= 1){
if(value == 0x64616F6CUL){
/* restore default parameters */
/* rename current file to .old, so it no longer exist */
remove(EE_ROM_FILENAME_OLD);
rename(EE_ROM_FILENAME, EE_ROM_FILENAME_OLD);
/* create an empty file */
FILE *fp = fopen(EE_ROM_FILENAME, "wt");
if(!fp){
rename(EE_ROM_FILENAME_OLD, EE_ROM_FILENAME);
return CO_SDO_AB_HW;
}
/* write one byte '-' to the file */
fputc('-', fp);
fclose(fp);
return CO_SDO_AB_NONE;
}
else
return CO_SDO_AB_DATA_TRANSF;
}
}
return ret;
}
/******************************************************************************/
CO_ReturnError_t CO_EE_init_1(
CO_EE_t *ee,
uint8_t *SRAMAddress,
uint8_t *OD_EEPROMAddress,
uint32_t OD_EEPROMSize,
uint8_t *OD_ROMAddress,
uint32_t OD_ROMSize)
{
/* configure object variables */
ee->pSRAM = (uint32_t*)SRAMAddress;
ee->OD_EEPROMAddress = (uint32_t*) OD_EEPROMAddress;
ee->OD_EEPROMSize = OD_EEPROMSize / 4;
ee->OD_ROMAddress = OD_ROMAddress;
ee->OD_ROMSize = OD_ROMSize;
ee->OD_EEPROMCurrentIndex = 0;
ee->OD_EEPROMWriteEnable = CO_false;
/* read the CO_OD_EEPROM from SRAM, first verify, if data are OK */
if(ee->pSRAM == 0) return CO_ERROR_OUT_OF_MEMORY;
uint32_t firstWordRAM = *(ee->OD_EEPROMAddress);
uint32_t firstWordEE = *(ee->pSRAM);
uint32_t lastWordEE = *(ee->pSRAM + ee->OD_EEPROMSize - 1);
if(firstWordRAM == firstWordEE && firstWordRAM == lastWordEE){
unsigned int i;
for(i=0; i<ee->OD_EEPROMSize; i++)
(ee->OD_EEPROMAddress)[i] = (ee->pSRAM)[i];
}
ee->OD_EEPROMWriteEnable = CO_true;
/* read the CO_OD_ROM from file and verify CRC */
void *buf = malloc(ee->OD_ROMSize);
if(buf){
CO_ReturnError_t ret = CO_ERROR_NO;
FILE *fp = fopen(EE_ROM_FILENAME, "rb");
uint32_t cnt = 0;
uint16_t CRC[2];
if(fp){
cnt = fread(buf, 1, ee->OD_ROMSize, fp);
/* read also two bytes of CRC from file */
cnt += fread(&CRC[0], 1, 4, fp);
CRC[1] = crc16_ccitt((unsigned char*)buf, ee->OD_ROMSize, 0);
fclose(fp);
}
if(cnt == 1 && *((char*)buf) == '-'){
ret = CO_ERROR_NO; /* file is empty, default values will be used, no error */
}
else if(cnt != (ee->OD_ROMSize + 2)){
ret = CO_ERROR_DATA_CORRUPT; /* file length does not match */
}
else if(CRC[0] != CRC[1]){
ret = CO_ERROR_CRC; /* CRC does not match */
}
else{
/* no errors, copy data into object dictionary */
memcpy(ee->OD_ROMAddress, buf, ee->OD_ROMSize);
}
free(buf);
return ret;
}
else return CO_ERROR_OUT_OF_MEMORY;
return CO_ERROR_NO;
}
/******************************************************************************/
void CO_EE_init_2(
CO_EE_t *ee,
CO_ReturnError_t eeStatus,
CO_SDO_t *SDO,
CO_EM_t *em)
{
CO_OD_configure(SDO, OD_H1010_STORE_PARAM_FUNC, CO_ODF_1010, (void*)ee, 0, 0U);
CO_OD_configure(SDO, OD_H1011_REST_PARAM_FUNC, CO_ODF_1011, (void*)ee, 0, 0U);
if(eeStatus != CO_ERROR_NO){
CO_errorReport(em, CO_EM_NON_VOLATILE_MEMORY, CO_EMC_HARDWARE, (uint32_t)eeStatus);
}
}
/******************************************************************************/
void CO_EE_process(CO_EE_t *ee){
if(ee && ee->OD_EEPROMWriteEnable){
/* verify next word */
unsigned int i = ee->OD_EEPROMCurrentIndex;
if(++i == ee->OD_EEPROMSize) i = 0;
ee->OD_EEPROMCurrentIndex = i;
/* update SRAM */
(ee->pSRAM)[i] = (ee->OD_EEPROMAddress)[i];
}
}

99
stack/socketCAN/eeprom.h Normal file
View File

@ -0,0 +1,99 @@
/*
* Eeprom object for BECK SC243 computer.
*
* @file eeprom.h
* @version SVN: \$Id$
* @author Janez Paternoster
* @copyright 2004 - 2013 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 EEPROM_H
#define EEPROM_H
/* For documentation see file genericDriver/eeprom.h */
/*
* SC243 specific
*
* Usage of device file system or SRAM for storing non-volatile variables.
*
* Two blocks of CANopen Object Dictionary data are stored as non-volatile:
* OD_EEPROM - Stored is in internal battery powered SRAM from address 0. Data
* are stored automatically on change. No data corruption control
* is made. Data are load on startup.
* OD_ROM - Stored in file named "OD_ROM01.dat". Data integrity is
* verified with CRC.
* Data are stored on special CANopen command - Writing 0x65766173
* into Object dictionary (index 1010, subindex 1). Default values
* are restored after reset, if writing 0x64616F6C into (1011, 1).
* Backup is stored to "OD_ROM01.old".
*/
/* Filename */
#ifndef EE_ROM_FILENAME
#define EE_ROM_FILENAME "OD_ROM01.dat"
#endif
#ifndef EE_ROM_FILENAME_OLD
#define EE_ROM_FILENAME_OLD "OD_ROM01.old"
#endif
/* Eeprom object */
typedef struct{
uint32_t *OD_EEPROMAddress;
uint32_t OD_EEPROMSize;
uint8_t *OD_ROMAddress;
uint32_t OD_ROMSize;
uint32_t *pSRAM; /* SC243 specific: Pointer to start
address of the battery powered SRAM */
uint32_t OD_EEPROMCurrentIndex;
CO_bool_t OD_EEPROMWriteEnable;
}CO_EE_t;
/* First part of eeprom initialization.
* @param SRAMAddress Address of battery powered SRAM memory.
*/
CO_ReturnError_t CO_EE_init_1(
CO_EE_t *ee,
uint8_t *SRAMAddress,
uint8_t *OD_EEPROMAddress,
uint32_t OD_EEPROMSize,
uint8_t *OD_ROMAddress,
uint32_t OD_ROMSize);
/* Second part of eeprom initialization. */
void CO_EE_init_2(
CO_EE_t *ee,
CO_ReturnError_t eeStatus,
CO_SDO_t *SDO,
CO_EM_t *em);
/* Process eeprom object. */
void CO_EE_process(CO_EE_t *ee);
#endif