Subversion Repositories HomeAutomation

Rev

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

  1. /*
  2.  * CAN_driver.c
  3.  *
  4.  * Created: 10/17/2013 2:53:10 PM
  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 driver.
  10.    
  11.  */
  12.  
  13. //******************************************************************************
  14. //! @file $RCSfile: can_drv.c,v $
  15. //!
  16. //! Copyright (c) 2007 Atmel.
  17. //!
  18. //! Use of this program is subject to Atmel's End User License Agreement.
  19. //! Please read file license.txt for copyright notice.
  20. //!
  21. //! @brief This file contains the low level functions (drivers) of:
  22. //!             - CAN (Controller Array Network)
  23. //!             - for AT90CAN128/64/32
  24. //!
  25. //! This file can be parsed by Doxygen for automatic documentation generation.
  26. //! This file has been validated with AVRStudio-413528/WinAVR-20070122.
  27. //!
  28. //! @version $Revision: 3.20 $ $Name: jtellier $
  29. //!
  30. //! @todo
  31. //! @bug
  32. //******************************************************************************
  33.  
  34. //_____ I N C L U D E S ________________________________________________________
  35. #include "config.h"
  36. #include "can_drv.h"
  37.  
  38. //_____ D E F I N I T I O N S __________________________________________________
  39.  
  40. //_____ F U N C T I O N S ______________________________________________________
  41.  
  42. //------------------------------------------------------------------------------
  43. //  @fn can_clear_all_mob
  44. //!
  45. //! This function clears the Mailbox content.
  46. //! It reset CANSTMOB, CANCDMOB, CANIDTx & CANIDMx and clears data FIFO of
  47. //! MOb[0] upto MOb[LAST_MOB_NB].
  48. //!
  49. //! @warning: This version doesn't clears the data FIFO
  50. //!
  51. //! @param none
  52. //!
  53. //! @return none
  54. //------------------------------------------------------------------------------
  55. void can_clear_all_mob(void)
  56. {
  57. uint8_t  mob_number;
  58. /*
  59.     uint8_t  data_index;
  60. */
  61.  
  62.     for (mob_number = 0; mob_number < NB_MOB; mob_number++)
  63.     {
  64.         CANPAGE = (mob_number << 4);    //! Page index
  65.         Can_clear_mob();                //! All MOb Registers=0
  66. /*
  67.         for (data_index = 0; data_index < NB_DATA_MAX; data_index++)
  68.         {
  69.             CANMSG = 0;                 //! MOb data FIFO
  70.         }
  71. */
  72.     }
  73. }
  74.  
  75. //------------------------------------------------------------------------------
  76. //  @fn can_get_mob_free
  77. //!
  78. //! This function returns the number of the first MOb available or 0xFF if
  79. //! no MOb is available.
  80. //!
  81. //! @warning none.
  82. //!
  83. //! @param  none.
  84. //!
  85. //! @return Handle of MOb.
  86. //!          - MOb[0] upto MOb[LAST_MOB_NB]
  87. //!          - 0xFF if no MOb
  88. //------------------------------------------------------------------------------
  89. uint8_t can_get_mob_free(void)
  90. {
  91.     uint8_t mob_number, page_saved;
  92.  
  93.     page_saved = CANPAGE;
  94.     for (mob_number = 0; mob_number < NB_MOB; mob_number++)
  95.     {
  96.         Can_set_mob(mob_number);
  97.         if ((CANCDMOB & 0xC0) == 0x00) //! Disable configuration
  98.         {
  99.             CANPAGE = page_saved;
  100.             return (mob_number);
  101.         }
  102.     }
  103.     CANPAGE = page_saved;
  104.     return (NO_MOB);
  105. }
  106.  
  107. //------------------------------------------------------------------------------
  108. //  @fn can_get_mob_status
  109. //!
  110. //! This function returns information "MOB completed its job"
  111. //! if one of the RXOK or TXOK Flag is set or "MOB not completed its job
  112. //! if no RXOK and TXOK flags are set.
  113. //! Previously, this function checks if the MOb is configured or not and in
  114. //!  case of the MOB not configured, the function returns "MOB_DISABLE".
  115. //!
  116. //! @warning none.
  117. //!
  118. //! @param none.
  119. //!
  120. //! @return MOb Status.
  121. //!          -  MOB_NOT_COMPLETED
  122. //!          -  MOB_TX_COMPLETED
  123. //!          -  MOB_RX_COMPLETED
  124. //!          -  MOB_RX_DLC_WARNING
  125. //!          -  MOB_DISABLE
  126. //!          or should be a combination of the following errors
  127. //!          -  MOB_ACK_ERROR
  128. //!          -  MOB_FORM_ERROR
  129. //!          -  MOB_CRC_ERROR
  130. //!          -  MOB_STUFF_ERROR
  131. //!          -  MOB_BIT_ERROR
  132. //------------------------------------------------------------------------------
  133. uint8_t can_get_mob_status(void)
  134. {
  135.     uint8_t mob_status, canstmob_copy;
  136.  
  137.     // Test if MOb ENABLE or DISABLE
  138.     if ((CANCDMOB & 0xC0) == 0x00) {return(MOB_DISABLE);}
  139.  
  140.     canstmob_copy = CANSTMOB; // Copy for test integrity
  141.  
  142.     // If MOb is ENABLE, test if MOb is COMPLETED
  143.     // - MOb Status = 0x20 then MOB_RX_COMPLETED
  144.     // - MOb Status = 0x40 then MOB_TX_COMPLETED
  145.     // - MOb Status = 0xA0 then MOB_RX_COMPLETED_DLCW
  146.     mob_status = canstmob_copy & ((1<<DLCW)|(1<<TXOK)|(1<<RXOK));
  147.     if ( (mob_status==MOB_RX_COMPLETED) ||   \
  148.          (mob_status==MOB_TX_COMPLETED) ||   \
  149.          (mob_status==MOB_RX_COMPLETED_DLCW) ) { return(mob_status); }
  150.  
  151.     // If MOb is ENABLE & NOT_COMPLETED, test if MOb is in ERROR
  152.     // - MOb Status bit_0 = MOB_ACK_ERROR
  153.     // - MOb Status bit_1 = MOB_FORM_ERROR
  154.     // - MOb Status bit_2 = MOB_CRC_ERROR
  155.     // - MOb Status bit_3 = MOB_STUFF_ERROR
  156.     // - MOb Status bit_4 = MOB_BIT_ERROR
  157.     mob_status = canstmob_copy & ERR_MOB_MSK;
  158.     if (mob_status != 0) { return(mob_status); }
  159.  
  160.     // If CANSTMOB = 0 then MOB_NOT_COMPLETED
  161.     return(MOB_NOT_COMPLETED);
  162. }
  163.  
  164. //------------------------------------------------------------------------------
  165. //  @fn can_get_data
  166. //!
  167. //! This function copy the data from the selected MOb to the address
  168. //! passed as parameter.
  169. //!
  170. //! @warning none.
  171. //!
  172. //! @param CAN message data address.
  173. //!
  174. //! @return none.
  175. //------------------------------------------------------------------------------
  176. void can_get_data(uint8_t* p_can_message_data)
  177. {
  178.     uint8_t data_index;
  179.  
  180.     for (data_index = 0; data_index < (Can_get_dlc()); data_index++)
  181.     {
  182.         *(p_can_message_data + data_index) = CANMSG;
  183.     }
  184. }
  185.  
  186. //------------------------------------------------------------------------------
  187. //  @fn can_fixed_baudrate
  188. //!
  189. //! This function programs the CANBTx registers with the predefined values
  190. //! CONF_CANBT1, CONF_CANBT2, CONF_CANBT3.
  191. //!
  192. //! @warning
  193. //!
  194. //! @param (unused!)
  195. //!
  196. //! @return Baudrate Status
  197. //!         fixed = 1: baudrate performed
  198. //------------------------------------------------------------------------------
  199. uint8_t can_fixed_baudrate(uint8_t mode)
  200. {
  201.     Can_reset();
  202.     Can_conf_bt();
  203.     return 1;
  204. }
  205.