Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * can_lib.h
  3.  *
  4.  * Created: 10/18/2013 11:38:16 AM
  5.  *  Author: Scott_Schmit
  6.  
  7.     The contents of this file were copy & pasted from the CAN Software Library
  8.     on Atmel.com. The library was written for AT90CANxx devices, but was modified
  9.     as an ATmegaxxM1 library.
  10.  
  11.  */
  12.  
  13.  
  14. //******************************************************************************
  15. //! @file $RCSfile: can_lib.h,v $
  16. //!
  17. //! Copyright (c) 2007 Atmel.
  18. //!
  19. //! Use of this program is subject to Atmel's End User License Agreement.
  20. //! Please read file license.txt for copyright notice.
  21. //!
  22. //! @brief This file contains the prototypes and the macros of the
  23. //!        library of functions of:
  24. //!             - CAN (Controller Array Network)
  25. //!             - for AT90CAN128/64/32.
  26. //!
  27. //! This file can be parsed by Doxygen for automatic documentation generation.
  28. //! This file has been validated with AVRStudio-413528/WinAVR-20070122.
  29. //!
  30. //! @version $Revision: 3.20 $ $Name: jtellier $
  31. //!
  32. //! @todo
  33. //! @bug
  34. //******************************************************************************
  35.  
  36. #ifndef _CAN_LIB_H_
  37. #define _CAN_LIB_H_
  38.  
  39. //_____ I N C L U D E S ________________________________________________________
  40. #include "can_drv.h"
  41.  
  42. //_____ D E F I N I T I O N S __________________________________________________
  43. #define Max(a, b)          ( (a)>(b) ? (a) : (b) )
  44. #define Min(a, b)          ( (a)<(b) ? (a) : (b) )
  45. typedef uint8_t Bool;
  46.  
  47. // ----------
  48. // @brief This constant is used as return value for "can_cmd" function.
  49. #define CAN_CMD_REFUSED  0xFF
  50. // ----------
  51. // @brief This constant is used as return value for "can_cmd" function.
  52. #define CAN_CMD_ACCEPTED         0x00
  53.  
  54. // ----------
  55. // @brief This constant is used as return value for "can_get_status" function.
  56. #define CAN_STATUS_COMPLETED     0x00
  57. // ----------
  58. // @brief This constant is used as return value for "can_get_status" function.
  59. #define CAN_STATUS_NOT_COMPLETED 0x01
  60. // ----------
  61. // @brief This constant is used as return value for "can_get_status" function.
  62. #define CAN_STATUS_ERROR         0x02
  63.  
  64. // ----------
  65. // @brief This enumeration is used to select an action for a specific message
  66. // declared in st_cmd_t structure.
  67. typedef enum {
  68.   CMD_NONE,
  69.   CMD_TX,
  70.   CMD_TX_DATA,
  71.   CMD_TX_REMOTE,
  72.   CMD_RX,
  73.   CMD_RX_DATA,
  74.   CMD_RX_REMOTE,
  75.   CMD_RX_MASKED,
  76.   CMD_RX_DATA_MASKED,
  77.   CMD_RX_REMOTE_MASKED,
  78.   CMD_REPLY,        
  79.   CMD_REPLY_MASKED,
  80.   CMD_ABORT
  81. } can_cmd_t;
  82.  
  83. // ----------
  84. // @brief This union defines a CAN identifier and allows to access it in mode
  85. // standard, extended or through a table.
  86. typedef union{
  87.   uint32_t ext;
  88.   uint16_t std;
  89.   uint8_t  tab[4];
  90. } can_id_t;
  91.  
  92. // ----------
  93. // @brief This structure defines some specific information as RTR bit and
  94. // IDE bit
  95. typedef struct{
  96.   Bool rtr;
  97.   Bool ide;
  98. } can_ctrl_t;
  99.  
  100. // ----------
  101. // @brief This structure allows to define a specific action on CAN network.
  102. // 1) handle: manage by the library.
  103. // 2) cmd   :  initialize by the application to select the operation.
  104. // 3) id    :  initialize by the application in transmission
  105. //             complete by the library in reception.
  106. // 4) dlc   :  initialize by the application to give the number of data to
  107. //             transmit complete by the library in reception.
  108. // 5) pt_data: pointer on the table which contains the data to send or
  109. //             received.
  110. // 6) status:  manage by the library.
  111. // 7) ctrl  :  field ide to signal a extended frame .
  112. typedef  struct{
  113.   uint8_t         handle;
  114.   can_cmd_t  cmd;
  115.   can_id_t   id;
  116.   uint8_t         dlc;  
  117.   uint8_t*        pt_data;
  118.   uint8_t         status;
  119.   can_ctrl_t ctrl;  
  120. } st_cmd_t;
  121.  
  122.  
  123. //_____ D E C L A R A T I O N S ________________________________________________
  124.  
  125. //------------------------------------------------------------------------------
  126. //  @fn can_init
  127. //!
  128. //! CAN macro initialization. Reset the CAN peripheral, initialize the bit
  129. //! timing, initialize all the registers mapped in SRAM to put MObs in
  130. //! inactive state and enable the CAN macro.
  131. //!
  132. //! @warning The CAN macro will be enable after seen on CAN bus a receceive
  133. //!          level as long as of an inter frame (hardware feature).
  134. //!
  135. //! @param  Mode (for "can_fixed_baudrate" param not used)
  136. //!         ==0: start CAN bit timing evaluation from faster baudrate
  137. //!         ==1: start CAN bit timing evaluation with CANBTx registers
  138. //!              contents
  139. //!
  140. //! @return Baudrate Status
  141. //!         ==0: research of bit timing configuration failed
  142. //!         ==1: baudrate performed
  143. //!
  144. extern uint8_t can_init(uint8_t mode);
  145.  
  146. //------------------------------------------------------------------------------
  147. //  @fn can_cmd
  148. //!
  149. //! This function takes a CAN descriptor, analyses the action to do:
  150. //! transmit, receive or abort.
  151. //! This function returns a status (CAN_CMD_ACCEPTED or CAN_CMD_REFUSED) if
  152. //! a MOb for Rx or Tx has been found. If no MOB has been found, the
  153. //! application must be retry at a later date.
  154. //! This function also updates the CAN descriptor status (MOB_PENDING or
  155. //! MOB_NOT_REACHED) if a MOb for Rx or Tx has been found. If aborting
  156. //! is performed, the CAN descriptor status will be set to STATUS_CLEARED.
  157. //!
  158. //! @param  st_cmd_t* - Can_descriptor pointer on CAN descriptor structure
  159. //!         to select the action to do.
  160. //!
  161. //! @return CAN_CMD_ACCEPTED - command is accepted
  162. //!         CAN_CMD_REFUSED  - command is refused
  163. //!
  164. extern uint8_t can_cmd (st_cmd_t *);
  165.  
  166. //------------------------------------------------------------------------------
  167. //  @fn can_get_status
  168. //!
  169. //! This function allows to return if the command has been performed or not.
  170. //! In an reception case, all the CAN message is stored in the structure.
  171. //! This function also updates the CAN descriptor status (MOB_TX_COMPLETED,    
  172. //!  MOB_RX_COMPLETED, MOB_RX_COMPLETED_DLCW or MOB_???_ERROR).        
  173. //!
  174. //! @param  st_cmd_t* pointer on CAN descriptor structure.
  175. //!
  176. //! @return CAN_STATUS_COMPLETED     - Rx or Tx is completed
  177. //!         CAN_STATUS_NOT_COMPLETED - Rx or Tx is not completed
  178. //!         CAN_STATUS_ERROR         - Error in configuration or in the
  179. //!                                    CAN communication
  180. //!
  181. extern uint8_t can_get_status (st_cmd_t *);
  182.  
  183. //______________________________________________________________________________
  184.  
  185. #endif // _CAN_LIB_H_
  186.