Nut/OS  5.0.5
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 THE COPYRIGHT HOLDERS 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 THE
00025  * COPYRIGHT OWNER 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 
00050 #include <sys/types.h>
00051 #include <sys/device.h>
00052 
00057 
00058 #define CAN_IF_2A    0x01
00059 #define CAN_IF_2B    0x02
00060 
00061 
00062 /*
00063  * CAN Baud rate constants
00064  */
00065 #define CAN_SPEED_10K      0   ///< 10 kbit/s, max. cable length 5000 m
00066 #define CAN_SPEED_20K      1   ///< 20 kbit/s, max. cable length 2500 m
00067 #define CAN_SPEED_50K      2   ///< 50 kbit/s, max. cable length 1000 m
00068 #define CAN_SPEED_100K     3   ///< 100 kbit/s, max. cable length 600 m
00069 #define CAN_SPEED_125K     4   ///< 125 kbit/s, max. cable length 500 m
00070 #define CAN_SPEED_250K     5   ///< 250 kbit/s, max. cable length 250 m
00071 #define CAN_SPEED_500K     6   ///< 500 kbit/s, max. cable length 100 m
00072 #define CAN_SPEED_800K     7   ///< 800 kbit/s, max. cable length 50 m
00073 #define CAN_SPEED_1M       8   ///< 1 Mbit/s, max. cable length 25 m
00074 #define CAN_SPEED_CUSTOM 255
00075 
00076 
00082 struct _CANFRAME {              // todo: Implement flags
00083     uint32_t id;                  // Identifier
00084     uint8_t len;                 // Length of frame, max = 8
00085     uint8_t byte[8];
00086     uint8_t ext;                 // Boolean, extendet frame
00087     uint8_t rtr;                 // Boolean, remote transmition bit
00088 };
00089 
00094 typedef struct _CANFRAME CANFRAME;
00095 
00100 struct _CANINFO {
00101     HANDLE volatile can_rx_rdy;     
00102     HANDLE volatile can_tx_rdy;     
00103     uint32_t can_rx_frames;         
00104     uint32_t can_tx_frames;         
00105     uint32_t can_interrupts;        
00106     uint32_t can_overruns;          
00107     uint32_t can_errors;            
00108 };
00109 
00113 typedef struct _CANINFO CANINFO;
00114 
00121 struct ifcan {
00122     uint8_t can_type;            
00123     uint32_t can_baudrate;        
00124     uint8_t can_acc_mask[4];     
00125     uint8_t can_acc_code[4];     
00126     uint32_t can_rtimeout;        
00128     uint8_t (*can_rxavail) (NUTDEVICE *);           
00129     uint8_t (*can_txfree) (NUTDEVICE *);            
00130     uint8_t (*can_recv) (NUTDEVICE *, CANFRAME *);  
00131     void   (*can_send) (NUTDEVICE *, CANFRAME *);  
00132     void   (*can_set_ac) (NUTDEVICE *, uint8_t*);   
00133     void   (*can_set_am) (NUTDEVICE *, uint8_t*);   
00134     uint8_t (*can_set_baud) (NUTDEVICE *, uint32_t);  
00135 };
00136 
00140 typedef struct ifcan IFCAN;
00141 
00142 uint8_t CAN_SetSpeed(NUTDEVICE *dev, uint32_t baudrate);
00143 void    CAN_SetFilter(NUTDEVICE *dev, uint8_t *ac, uint8_t *am);
00144 
00145 void    CAN_TxFrame(NUTDEVICE *dev, CANFRAME *frame);
00146 uint8_t CAN_TryTxFrame(NUTDEVICE *dev, CANFRAME *frame);
00147 uint8_t CAN_TxFree(NUTDEVICE *dev);
00148 
00149 uint8_t CAN_RxFrame(NUTDEVICE *dev, CANFRAME *frame);
00150 uint8_t CAN_TryRxFrame(NUTDEVICE *dev, CANFRAME *frame);
00151 uint8_t CAN_RxAvail(NUTDEVICE *dev);
00152 void    CAN_SetRxTimeout(NUTDEVICE *dev, uint32_t timeout);
00153 
00156 #endif