Nut/OS  4.10.3
API Reference
can_dev.h
Go to the documentation of this file.
00001 #ifndef _CAN_DEV_H_
00002 #define _CAN_DEV_H_
00003 
00004 /*
00005  * Copyright (C) 2004 by Ole Reinhardt<ole.reinhardt@kernelconcepts.de>
00006  * Kernel concepts (http://www.kernelconcepts.de) All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the distribution.
00017  * 3. Neither the name of the copyright holders nor the names of
00018  *    contributors may be used to endorse or promote products derived
00019  *    from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00022  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00025  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00028  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00029  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00030  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00031  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * For additional information see http://www.ethernut.de/
00035  *
00036  */
00037 
00043 /*
00044  * $Log$
00045  * Revision 1.7  2008/08/11 06:59:59  haraldkipp
00046  * BSD types replaced by stdint types (feature request #1282721).
00047  *
00048  * Revision 1.6  2007/09/08 03:01:11  hwmaier
00049  * Changes to support RX time-out, CAN_SetRxTimeout() added.
00050  *
00051  * Revision 1.5  2005/10/07 21:44:22  hwmaier
00052  * CAN_SetSpeed function added. Baudrates moved here.
00053  *
00054  * Revision 1.4  2004/08/25 15:48:21  olereinhardt
00055  * Added function to set an acceptance filter
00056  *
00057  * Revision 1.3  2004/08/02 09:59:53  olereinhardt
00058  * changed typing of CAN_TryRxFrame
00059  *
00060  * Revision 1.2  2004/06/23 10:15:35  olereinhardt
00061  * Added function for buffer monitoring (avail / free)
00062  *
00063  * Revision 1.1  2004/06/07 15:13:48  olereinhardt
00064  * Initial checkin
00065  *
00066  */
00067 
00068 #include <sys/types.h>
00069 #include <sys/device.h>
00070 
00075 
00076 #define CAN_IF_2A    0x01
00077 #define CAN_IF_2B    0x02
00078 
00079 
00080 /*
00081  * CAN Baud rate constants
00082  */
00083 #define CAN_SPEED_10K      0   ///< 10 kbit/s, max. cable length 5000 m
00084 #define CAN_SPEED_20K      1   ///< 20 kbit/s, max. cable length 2500 m
00085 #define CAN_SPEED_50K      2   ///< 50 kbit/s, max. cable length 1000 m
00086 #define CAN_SPEED_100K     3   ///< 100 kbit/s, max. cable length 600 m
00087 #define CAN_SPEED_125K     4   ///< 125 kbit/s, max. cable length 500 m
00088 #define CAN_SPEED_250K     5   ///< 250 kbit/s, max. cable length 250 m
00089 #define CAN_SPEED_500K     6   ///< 500 kbit/s, max. cable length 100 m
00090 #define CAN_SPEED_800K     7   ///< 800 kbit/s, max. cable length 50 m
00091 #define CAN_SPEED_1M       8   ///< 1 Mbit/s, max. cable length 25 m
00092 #define CAN_SPEED_CUSTOM 255
00093 
00094 
00100 struct _CANFRAME {              // todo: Implement flags
00101     uint32_t id;                  // Identifier
00102     uint8_t len;                 // Length of frame, max = 8
00103     uint8_t byte[8];
00104     uint8_t ext;                 // Boolean, extendet frame
00105     uint8_t rtr;                 // Boolean, remote transmition bit
00106 };
00107 
00112 typedef struct _CANFRAME CANFRAME;
00113 
00118 struct _CANINFO {
00119     HANDLE volatile can_rx_rdy;     
00120     HANDLE volatile can_tx_rdy;     
00121     uint32_t can_rx_frames;           
00122     uint32_t can_tx_frames;           
00123     uint32_t can_interrupts;          
00124     uint32_t can_overruns;            
00125     uint32_t can_errors;              
00126 };
00127 
00131 typedef struct _CANINFO CANINFO;
00132 
00139 struct ifcan {
00140     uint8_t can_type;            
00141     uint32_t can_baudrate;        
00142     uint8_t can_acc_mask[4];     
00143     uint8_t can_acc_code[4];     
00144     uint32_t can_rtimeout;        
00146     uint8_t (*can_rxavail) (NUTDEVICE *);           
00147     uint8_t (*can_txfree) (NUTDEVICE *);            
00148     uint8_t (*can_recv) (NUTDEVICE *, CANFRAME *);  
00149     void   (*can_send) (NUTDEVICE *, CANFRAME *);  
00150     void   (*can_set_ac) (NUTDEVICE *, uint8_t*);   
00151     void   (*can_set_am) (NUTDEVICE *, uint8_t*);   
00152     uint8_t (*can_set_baud) (NUTDEVICE *, uint32_t);  
00153 };
00154 
00158 typedef struct ifcan IFCAN;
00159 
00160 uint8_t CAN_SetSpeed(NUTDEVICE *dev, uint32_t baudrate);
00161 void   CAN_SetFilter(NUTDEVICE *dev, uint8_t *ac, uint8_t *am);
00162 
00163 void   CAN_TxFrame(NUTDEVICE *dev, CANFRAME *frame);
00164 uint8_t CAN_TryTxFrame(NUTDEVICE *dev, CANFRAME *frame);
00165 uint8_t CAN_TxFree(NUTDEVICE *dev);
00166 
00167 uint8_t CAN_RxFrame(NUTDEVICE *dev, CANFRAME *frame);
00168 uint8_t CAN_TryRxFrame(NUTDEVICE *dev, CANFRAME *frame);
00169 uint8_t CAN_RxAvail(NUTDEVICE *dev);
00170 void   CAN_SetRxTimeout(NUTDEVICE *dev, uint32_t timeout);
00171 
00174 #endif