Nut/OS  5.0.5
API Reference
canbus.h
Go to the documentation of this file.
00001 #ifndef _CANBUS_H_
00002 #define _CANBUS_H_
00003 /*
00004  * Copyright (C) 2012 by Uwe Bonnes(bon@elektron.ikp.physik.tu-darmstadt.de)
00005  *
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. Neither the name of the copyright holders nor the names of
00017  *    contributors may be used to endorse or promote products derived
00018  *    from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00027  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00028  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00029  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00030  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  *
00033  * For additional information see http://www.ethernut.de/
00034  *
00035  */
00036 
00048 #include <cfg/arch.h>
00049 #include <sys/types.h>
00050 #include <sys/device.h>
00051 
00052 #include <dev/irqreg.h>
00053 
00058 
00062 #define CAN_SPEED_10K      0   ///< 10 kbit/s, max. cable length 5000 m
00063 #define CAN_SPEED_20K      1   ///< 20 kbit/s, max. cable length 2500 m
00064 #define CAN_SPEED_50K      2   ///< 50 kbit/s, max. cable length 1000 m
00065 #define CAN_SPEED_100K     3   ///< 100 kbit/s, max. cable length 600 m
00066 #define CAN_SPEED_125K     4   ///< 125 kbit/s, max. cable length 500 m
00067 #define CAN_SPEED_250K     5   ///< 250 kbit/s, max. cable length 250 m
00068 #define CAN_SPEED_500K     6   ///< 500 kbit/s, max. cable length 100 m
00069 #define CAN_SPEED_800K     7   ///< 800 kbit/s, max. cable length 50 m
00070 #define CAN_SPEED_1M       8   ///< 1 Mbit/s, max. cable length 25 m
00071 #define CAN_SPEED_CUSTOM 255
00072 
00076 enum CAN_RESULT
00077 {
00078    CAN_SUCCESS       =  0,   
00079    CAN_ERROR         = -1,   
00080    CAN_TXBUF_FULL    = -2,   
00081    CAN_RXBUF_EMPTY   = -3,   
00082    CAN_ILLEGAL_MOB   = -4,   
00083    CAN_INVALID_SPEED = -5,   
00084    CAN_PASSIVE       = -6,   
00085    CAN_BUS_OFF       = -7,   
00086    CAN_NO_COMPANION  = -8,   
00087    CAN_IS_COMPANION  = -9,   
00088 };
00089 
00093 enum CAN_COUNTERS
00094 {
00095     CAN_RX_FRAMES      =  0,   
00096     CAN_TX_FRAMES      =  1,   
00097     CAN_INTERRUPTS     =  2,   
00098     CAN_RX_INTERRUPTS  =  3,   
00099     CAN_TX_INTERRUPTS  =  4,   
00100     CAN_SCE_INTERRUPTS =  5,   
00101     CAN_OVERRUNS       =  6,   
00102     CAN_ERRORS         =  7,   
00103     CAN_NO_COUNTERS    =  8,
00104 };
00105 
00106 /* Enable / disable time triggered communication mode */
00107 #define CAN_TTCM    0x0001
00108 
00109 /* Enable / disable automatic bus-off management */
00110 #define CAN_ABOM    0x0002
00111 
00112 /* Set the automatic wake-up mode */
00113 #define CAN_AWUM    0x0004
00114 
00115 /* Enable / disable no automatic retransmission */
00116 #define CAN_NART    0x0008
00117 
00118 /* Enable / disable receive FIFO locked mode */
00119 #define CAN_RFLM    0x0010
00120 
00121 /* Enable / disable transmit FIFO priority */
00122 #define CAN_TXFP    0x0020
00123 
00127 typedef struct _NUTCANBUS NUTCANBUS;
00131 typedef struct _CANBUSINFO CANBUSINFO;
00132 
00136 typedef struct _CANBUFFER CANBUFFER;
00137 
00138 
00139 /* Include architecture specific CAN implementation */
00140 #if defined(MCU_STM32)
00141 #include <arch/cm3/stm/stm32_can.h>
00142 #endif
00143 
00149 struct _CANFRAME {              // todo: Implement flags
00150     uint32_t id;                  // Identifier
00151     uint8_t byte[8];
00152     uint8_t len;                 // Length of frame, max = 8
00153     uint8_t ext;                 // Boolean, extendet frame
00154     uint8_t rtr;                 // Boolean, remote transmition bit
00155 };
00156 
00161 typedef struct _CANFRAME CANFRAME;
00162 
00168 struct _CANFILTER{           //
00169     uint32_t id;             
00170     uint32_t mask;           
00171     uint8_t id_ext;          
00172     uint8_t id_rtr;          
00173     uint8_t mask_ext;        
00174     uint8_t mask_rtr;        
00175 };
00176 
00181 typedef struct _CANFILTER CANFILTER;
00182 
00183 #define FILTER_EXPLICIT 0xffffffff
00184 
00189 extern int NutRegisterCanBus( NUTCANBUS *bus, int8_t ln2_size );
00190 extern int CanAddFilter( NUTCANBUS *bus, CANFILTER *filter );
00191 extern int CanSetBaud( NUTCANBUS *bus, uint8_t baud, uint32_t alt_btr);
00192 extern void CANSetRxTimeout(NUTCANBUS *bus, uint32_t timeout);
00193 extern void CanEnableRx( NUTCANBUS *bus);
00194 extern int CanRxAvail( NUTCANBUS *bus);
00195 extern int CanInput(NUTCANBUS *bus, CANFRAME *output);
00196 extern int CanTxFree(NUTCANBUS *can);
00197 extern int CanOutput( NUTCANBUS *bus, CANFRAME *output);
00198 extern int CanGetCounter( NUTCANBUS *bus, enum CAN_COUNTERS index);
00199 
00200 #endif