edouard@629: /* edouard@629: This file is part of CanFestival, a library implementing CanOpen Stack. edouard@629: edouard@629: Copyright (C): Cosateq GmbH & Co.KG edouard@629: http://www.cosateq.com/ edouard@629: http://www.scale-rt.com/ edouard@629: edouard@629: See COPYING file for copyrights details. edouard@629: edouard@629: This library is free software; you can redistribute it and/or edouard@629: modify it under the terms of the GNU Lesser General Public edouard@629: License as published by the Free Software Foundation; either edouard@629: version 2.1 of the License, or (at your option) any later version. edouard@629: edouard@629: This library is distributed in the hope that it will be useful, edouard@629: but WITHOUT ANY WARRANTY; without even the implied warranty of edouard@629: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU edouard@629: Lesser General Public License for more details. edouard@629: edouard@629: You should have received a copy of the GNU Lesser General Public edouard@629: License along with this library; if not, write to the Free Software edouard@629: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA edouard@629: */ edouard@629: edouard@629: /* edouard@629: CAN driver interface for CO-PCICAN card. edouard@629: */ edouard@629: edouard@629: #include edouard@629: #include edouard@629: #include edouard@629: edouard@629: #define NEED_PRINT_MESSAGE edouard@629: #include "can_driver.h" edouard@629: #include "def.h" edouard@629: #include "co_pcicanops.h" edouard@629: edouard@629: /* at the moment not threadsafe :-( */ edouard@629: static unsigned char selectedChannelRx = 0, selectedChannelTx = 0; edouard@629: edouard@629: static int TranslateBaudRate( char* optarg ) edouard@629: { edouard@629: /* values see documentation of CO-PCICAN */ edouard@629: if( !strcmp( optarg, "1M" ) ) return 0; edouard@629: if( !strcmp( optarg, "800K" ) ) return 1; edouard@629: if( !strcmp( optarg, "500K" ) ) return 2; edouard@629: if( !strcmp( optarg, "250K" ) ) return 3; edouard@629: if( !strcmp( optarg, "125K" ) ) return 4; edouard@629: if( !strcmp( optarg, "100K" ) ) return 5; edouard@629: if( !strcmp( optarg, "83.3K" ) ) return 6; edouard@629: if( !strcmp( optarg, "10K" ) ) return 7; edouard@629: edouard@629: return -1; edouard@629: } edouard@629: edouard@629: /*********CO-PCICAN specific functions to communicate with the board**********/ edouard@629: typedef struct edouard@629: { edouard@629: char used; edouard@629: CAN_HANDLE fd; edouard@629: void* receiveTask; edouard@629: void* d; edouard@629: } CANPort; /* taken from drivers/unix.c */ edouard@629: edouard@629: int get_fd_of_port( CAN_PORT port ) edouard@629: { edouard@629: CANPort *thisPort = (CANPort*)port; edouard@629: CAN_HANDLE thisHandle; edouard@629: int *pfd; edouard@629: edouard@629: if( thisPort == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: get_fd_of_port(): thisPort is NULL\n"); edouard@629: return -1; edouard@629: } edouard@629: edouard@629: thisHandle = thisPort->fd; edouard@629: edouard@629: if( thisHandle == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: get_fd_of_port(): thisHandle is NULL\n"); edouard@629: return -1; edouard@629: } edouard@629: edouard@629: pfd = (int*)thisHandle; edouard@629: edouard@629: /*MSG("can_copcican_linux: get_fd_of_port(): handle is %d\n", *pfd);*/ edouard@629: edouard@629: return *pfd; edouard@629: } edouard@629: edouard@629: int co_pcican_enter_run_mode( const int fd ) edouard@629: { edouard@629: co_pcican_config_t board_config; edouard@629: edouard@629: if( fd < 0 ) edouard@629: { edouard@629: MSG("can_copcican_linux: co_pcican_enter_run_mode(): invalid file descriptor\n"); edouard@629: return -1; edouard@629: } edouard@629: edouard@629: memset( &board_config, 0x00, sizeof(co_pcican_config_t) ); edouard@629: board_config.opcode = CMDQ_OPC_ENTER_RUN_MODE; edouard@629: edouard@629: return ioctl( fd, CAN_CONFIG, &board_config ); edouard@629: } edouard@629: edouard@629: int co_pcican_enter_config_mode( const int fd ) edouard@629: { edouard@629: co_pcican_config_t board_config; edouard@629: edouard@629: if( fd < 0 ) edouard@629: { edouard@629: MSG("can_copcican_linux: co_pcican_enter_config_mode(): invalid file descriptor\n"); edouard@629: return -1; edouard@629: } edouard@629: edouard@629: memset( &board_config, 0x00, sizeof(co_pcican_config_t) ); edouard@629: board_config.opcode = CMDQ_OPC_ENTER_CONFIG_MODE; edouard@629: edouard@629: return ioctl( fd, CAN_CONFIG, &board_config ); edouard@629: } edouard@629: edouard@629: int co_pcican_select_channel( const unsigned char channel, const unsigned int direction ) edouard@629: { edouard@629: if( channel >= NUM_CAN_CHANNELS ) edouard@629: { edouard@629: MSG("can_copcican_linux: co_pcican_select_channel(): invalid channel\n"); edouard@629: return -1; edouard@629: } edouard@629: edouard@629: /* at the moment not threadsafe :-( */ edouard@629: switch( direction ) edouard@629: { edouard@629: case RX: selectedChannelRx = channel; edouard@629: break; edouard@629: case TX: selectedChannelTx = channel; edouard@629: break; edouard@629: default: return -1; edouard@629: } edouard@629: edouard@629: return 0; edouard@629: } edouard@629: edouard@629: int co_pcican_configure_selected_channel( const int fd, s_BOARD *board, const unsigned int direction ) edouard@629: { edouard@629: co_pcican_config_t board_config; edouard@629: unsigned int selectedChannel; edouard@629: edouard@629: if( fd < 0 ) edouard@629: { edouard@629: MSG("can_copcican_linux: co_pcican_configure_selected_channel(): invalid file descriptor\n"); edouard@629: return -1; edouard@629: } edouard@629: edouard@629: if( board == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: co_pcican_configure_selected_channel(): board is NULL\n"); edouard@629: return -1; edouard@629: } edouard@629: edouard@629: if( board->baudrate == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: co_pcican_configure_selected_channel(): baudrate is NULL\n"); edouard@629: return -1; edouard@629: } edouard@629: edouard@629: switch( direction ) edouard@629: { edouard@629: case RX: selectedChannel = selectedChannelRx; edouard@629: break; edouard@629: case TX: selectedChannel = selectedChannelTx; edouard@629: break; edouard@629: default: selectedChannel = 0xff; edouard@629: } edouard@629: edouard@629: if( selectedChannel >= NUM_CAN_CHANNELS ) edouard@629: { edouard@629: MSG("can_copcican_linux: co_pcican_configure_selected_channel(): invalid channel selected\n"); edouard@629: return -1; edouard@629: } edouard@629: edouard@629: memset( &board_config, 0x00, sizeof(co_pcican_config_t) ); edouard@629: board_config.opcode = CMDQ_OPC_SET_CONFIG_CHANNEL; edouard@629: board_config.param[0] = selectedChannel; edouard@629: board_config.param[1] = TranslateBaudRate( board->baudrate ); edouard@629: edouard@629: return ioctl( fd, CAN_CONFIG, &board_config ); edouard@629: } edouard@629: edouard@629: /*********functions which permit to communicate with the board****************/ edouard@629: UNS8 canReceive_driver( CAN_HANDLE fd0, Message *m ) edouard@629: { edouard@629: co_pcican_message_t canmsg; edouard@629: UNS8 ret = 0; edouard@629: int *pfd = (int*)fd0; edouard@629: edouard@629: if( pfd == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: canReceive_driver(): file descriptor is NULL\n"); edouard@629: return 1; edouard@629: } edouard@629: edouard@629: if( *pfd < 0 ) edouard@629: { edouard@629: MSG("can_copcican_linux: canReceive_driver(): invalid file descriptor\n"); edouard@629: return 1; edouard@629: } edouard@629: edouard@629: if( selectedChannelRx >= NUM_CAN_CHANNELS ) edouard@629: { edouard@629: MSG("can_copcican_linux: canReceive_driver(): invalid channel selected\n"); edouard@629: return 1; edouard@629: } edouard@629: edouard@629: if( m == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: canReceive_driver(): message is NULL\n"); edouard@629: return 1; edouard@629: } edouard@629: edouard@629: memset( &canmsg, 0x00, sizeof(co_pcican_message_t) ); edouard@629: canmsg.channelnum = selectedChannelRx; edouard@629: edouard@629: ioctl( *pfd, CAN_READ, &canmsg ); edouard@629: edouard@629: if( canmsg.timestamp_lo == 0 ) edouard@629: { edouard@629: memset( m, 0x00, sizeof(Message) ); edouard@629: edouard@629: m->cob_id = 0xffff; /* set to invalid so nothing happens */ edouard@629: } edouard@629: else edouard@629: { edouard@629: m->len = canmsg.size; edouard@629: m->cob_id = canmsg.id; edouard@629: m->rtr = canmsg.type & MSG_RTR; edouard@629: edouard@629: if( !m->rtr ) edouard@629: { edouard@629: /* this is for safety */ edouard@629: if( m->len > 8 ) edouard@629: m->len = 8; edouard@629: edouard@629: memcpy( m->data, canmsg.data, m->len); edouard@629: } edouard@629: } edouard@629: edouard@629: return ret; edouard@629: } edouard@629: edouard@629: /***************************************************************************/ edouard@629: UNS8 canSend_driver( CAN_HANDLE fd0, Message *m ) edouard@629: { edouard@629: co_pcican_message_t canmsg; edouard@629: UNS8 ret = 0; edouard@629: int *pfd = (int*)fd0; edouard@629: edouard@629: if( pfd == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: canSend_driver(): file descriptor is NULL\n"); edouard@629: return 1; edouard@629: } edouard@629: edouard@629: if( *pfd < 0 ) edouard@629: { edouard@629: MSG("can_copcican_linux: canSend_driver(): invalid file descriptor\n"); edouard@629: return 1; edouard@629: } edouard@629: edouard@629: if( selectedChannelTx >= NUM_CAN_CHANNELS ) edouard@629: { edouard@629: MSG("can_copcican_linux: canSend_driver(): invalid channel selected\n"); edouard@629: return 1; edouard@629: } edouard@629: edouard@629: if( m == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: canSend_driver(): message is NULL\n"); edouard@629: return 1; edouard@629: } edouard@629: edouard@629: memset( &canmsg, 0x00, sizeof(co_pcican_message_t) ); edouard@629: canmsg.channelnum = selectedChannelTx; edouard@629: canmsg.size = m->len; edouard@629: canmsg.id = m->cob_id; edouard@629: edouard@629: if( canmsg.id >= 0x800 ) edouard@629: canmsg.type |= MSG_EXT; edouard@629: edouard@629: if( m->rtr ) edouard@629: { edouard@629: canmsg.type |= MSG_RTR; edouard@629: } edouard@629: else edouard@629: { edouard@629: /* this is for safety */ edouard@629: if( canmsg.size > 8 ) edouard@629: canmsg.size = 8; edouard@629: edouard@629: memcpy( canmsg.data, m->data, canmsg.size); edouard@629: } edouard@629: edouard@629: if( ioctl( *pfd, CAN_WRITE, &canmsg ) < 0 ) edouard@629: ret = 1; edouard@629: edouard@629: return ret; edouard@629: } edouard@629: edouard@629: /***************************************************************************/ edouard@629: CAN_HANDLE canOpen_driver( s_BOARD *board ) edouard@629: { edouard@629: int *pfd; edouard@629: edouard@629: if( board == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: canOpen_driver(): board is NULL\n"); edouard@629: return NULL; edouard@629: } edouard@629: edouard@629: if( board->busname == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: canOpen_driver(): busname is NULL\n"); edouard@629: return NULL; edouard@629: } edouard@629: edouard@629: /* create dynamically to avoid global variable */ edouard@629: pfd = (int*)malloc( sizeof(int) ); edouard@629: edouard@629: if( pfd == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: canOpen_driver(): file descriptor is NULL\n"); edouard@629: return NULL; edouard@629: } edouard@629: edouard@629: *pfd = open( board->busname, O_RDWR, 0 ); edouard@629: edouard@629: if( *pfd < 0 ) edouard@629: { edouard@629: MSG("can_copcican_linux: canOpen_driver(): invalid file descriptor\n"); edouard@629: edouard@629: /* clear resources if open failed */ edouard@629: free( pfd ); edouard@629: pfd = NULL; edouard@629: } edouard@629: edouard@629: /*MSG("can_copcican_linux: canOpen_driver(): handle is %d\n", *pfd);*/ edouard@629: edouard@629: return (CAN_HANDLE)pfd; edouard@629: } edouard@629: edouard@629: /***************************************************************************/ edouard@629: int canClose_driver( CAN_HANDLE fd0 ) edouard@629: { edouard@629: int *pfd = (int*)fd0; edouard@629: edouard@629: if( pfd == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: canClose_driver(): file descriptor is NULL\n"); edouard@629: return -1; edouard@629: } edouard@629: edouard@629: if( *pfd < 0 ) edouard@629: { edouard@629: MSG("can_copcican_linux: canClose_driver(): invalid file descriptor\n"); edouard@629: return -1; edouard@629: } edouard@629: edouard@629: close( *pfd ); edouard@629: free( pfd ); edouard@629: pfd = NULL; edouard@629: edouard@629: selectedChannelRx = 0; edouard@629: selectedChannelTx = 0; edouard@629: edouard@629: return 0; edouard@629: } edouard@629: edouard@629: /***************************************************************************/ edouard@629: UNS8 canChangeBaudRate_driver( CAN_HANDLE fd0, char* baud ) edouard@629: { edouard@629: s_BOARD board; edouard@629: UNS8 ret = 0; edouard@629: int *pfd = (int*)fd0; edouard@629: edouard@629: if( pfd == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: canChangeBaudRate_driver(): file descriptor is NULL\n"); edouard@629: return 1; edouard@629: } edouard@629: edouard@629: if( *pfd < 0 ) edouard@629: { edouard@629: MSG("can_copcican_linux: canChangeBaudRate_driver(): invalid file descriptor\n"); edouard@629: return 1; edouard@629: } edouard@629: edouard@629: if( baud == NULL ) edouard@629: { edouard@629: MSG("can_copcican_linux: canChangeBaudRate_driver(): baud is NULL\n"); edouard@629: return 1; edouard@629: } edouard@629: edouard@629: memset( &board, 0x00, sizeof(s_BOARD) ); edouard@629: board.baudrate = baud; edouard@629: edouard@629: if( co_pcican_configure_selected_channel( *pfd, &board, RX ) < 0 ) edouard@629: ret = 1; edouard@629: edouard@629: if( co_pcican_configure_selected_channel( *pfd, &board, TX ) < 0 ) edouard@629: ret = 1; edouard@629: edouard@629: return ret; edouard@629: } edouard@629: