ChibiOS/RT Architecture - Reference Manual - Guides |
00001 /* 00002 ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio. 00003 00004 This file is part of ChibiOS/RT. 00005 00006 ChibiOS/RT is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 ChibiOS/RT is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 00019 --- 00020 00021 A special exception to the GPL can be applied should you wish to distribute 00022 a combined work that includes ChibiOS/RT, without being obliged to provide 00023 the source code for any proprietary components. See the file exception.txt 00024 for full details of how and when the exception can be applied. 00025 */ 00026 00027 /** 00028 * @file templates/can_lld.h 00029 * @brief CAN Driver subsystem low level driver header template. 00030 * 00031 * @addtogroup CAN_LLD 00032 * @{ 00033 */ 00034 00035 #ifndef _CAN_LLD_H_ 00036 #define _CAN_LLD_H_ 00037 00038 #if CH_HAL_USE_CAN || defined(__DOXYGEN__) 00039 00040 /*===========================================================================*/ 00041 /* Driver constants. */ 00042 /*===========================================================================*/ 00043 00044 /** 00045 * @brief This switch defines whether the driver implementation supports 00046 * a low power switch mode with automatic an wakeup feature. 00047 */ 00048 #define CAN_SUPPORTS_SLEEP TRUE 00049 00050 /*===========================================================================*/ 00051 /* Driver pre-compile time settings. */ 00052 /*===========================================================================*/ 00053 00054 /** 00055 * @brief Sleep mode related APIs inclusion switch. 00056 * @note This switch is enforced to @p FALSE if the driver implementation 00057 * does not support the sleep mode. 00058 */ 00059 #if CAN_SUPPORTS_SLEEP || defined(__DOXYGEN__) 00060 #if !defined(CAN_USE_SLEEP_MODE) || defined(__DOXYGEN__) 00061 #define CAN_USE_SLEEP_MODE TRUE 00062 #endif 00063 #else /* !CAN_SUPPORTS_SLEEP */ 00064 #define CAN_USE_SLEEP_MODE FALSE 00065 #endif /* !CAN_SUPPORTS_SLEEP */ 00066 00067 /*===========================================================================*/ 00068 /* Derived constants and error checks. */ 00069 /*===========================================================================*/ 00070 00071 #if CAN_USE_SLEEP_MODE && !CAN_SUPPORTS_SLEEP 00072 #error "CAN sleep mode not supported in this architecture" 00073 #endif 00074 00075 /*===========================================================================*/ 00076 /* Driver data structures and types. */ 00077 /*===========================================================================*/ 00078 00079 /** 00080 * @brief CAN status flags. 00081 */ 00082 typedef uint32_t canstatus_t; 00083 00084 /** 00085 * @brief CAN transmission frame. 00086 * @note Accessing the frame data as word16 or word32 is not portable 00087 * because machine data endianness, it can be still useful for a 00088 * quick filling. 00089 */ 00090 typedef struct { 00091 struct { 00092 uint8_t cf_DLC:4; /**< @brief Data length. */ 00093 uint8_t cf_RTR:1; /**< @brief Frame type. */ 00094 uint8_t cf_IDE:1; /**< @brief Identifier type. */ 00095 }; 00096 union { 00097 struct { 00098 uint32_t cf_SID:11; /**< @brief Standard identifier.*/ 00099 }; 00100 struct { 00101 uint32_t cf_EID:29; /**< @brief Extended identifier.*/ 00102 }; 00103 }; 00104 union { 00105 uint8_t cf_data8[8]; /**< @brief Frame data. */ 00106 uint16_t cf_data16[4]; /**< @brief Frame data. */ 00107 uint32_t cf_data32[2]; /**< @brief Frame data. */ 00108 }; 00109 } CANTxFrame; 00110 00111 /** 00112 * @brief CAN received frame. 00113 * @note Accessing the frame data as word16 or word32 is not portable 00114 * because machine data endianness, it can be still useful for a 00115 * quick filling. 00116 */ 00117 typedef struct { 00118 struct { 00119 uint8_t cf_DLC:4; /**< @brief Data length. */ 00120 uint8_t cf_RTR:1; /**< @brief Frame type. */ 00121 uint8_t cf_IDE:1; /**< @brief Identifier type. */ 00122 }; 00123 union { 00124 struct { 00125 uint32_t cf_SID:11; /**< @brief Standard identifier.*/ 00126 }; 00127 struct { 00128 uint32_t cf_EID:29; /**< @brief Extended identifier.*/ 00129 }; 00130 }; 00131 union { 00132 uint8_t cf_data8[8]; /**< @brief Frame data. */ 00133 uint16_t cf_data16[4]; /**< @brief Frame data. */ 00134 uint32_t cf_data32[2]; /**< @brief Frame data. */ 00135 }; 00136 } CANRxFrame; 00137 00138 /** 00139 * @brief CAN filter. 00140 * @note It could not be present on some architectures. 00141 */ 00142 typedef struct { 00143 } CANFilter; 00144 00145 /** 00146 * @brief Driver configuration structure. 00147 * @note It could be empty on some architectures. 00148 */ 00149 typedef struct { 00150 } CANConfig; 00151 00152 /** 00153 * @brief Structure representing an CAN driver. 00154 */ 00155 typedef struct { 00156 /** 00157 * @brief Driver state. 00158 */ 00159 canstate_t cd_state; 00160 /** 00161 * @brief Current configuration data. 00162 */ 00163 const CANConfig *cd_config; 00164 /** 00165 * @brief Transmission queue semaphore. 00166 */ 00167 Semaphore cd_txsem; 00168 /** 00169 * @brief Receive queue semaphore. 00170 */ 00171 Semaphore cd_rxsem; 00172 /** 00173 * @brief One or more frames become available. 00174 * @note After broadcasting this event it will not be broadcasted again 00175 * until the received frames queue has been completely emptied. It 00176 * is <b>not</b> broadcasted for each received frame. It is 00177 * responsibility of the application to empty the queue by repeatedly 00178 * invoking @p chReceive() when listening to this event. This behavior 00179 * minimizes the interrupt served by the system because CAN traffic. 00180 */ 00181 EventSource cd_rxfull_event; 00182 /** 00183 * @brief One or more transmission slots become available. 00184 */ 00185 EventSource cd_txempty_event; 00186 /** 00187 * @brief A CAN bus error happened. 00188 */ 00189 EventSource cd_error_event; 00190 /** 00191 * @brief Error flags set when an error event is broadcasted. 00192 */ 00193 canstatus_t cd_status; 00194 #if CAN_USE_SLEEP_MODE || defined (__DOXYGEN__) 00195 /** 00196 * @brief Entering sleep state event. 00197 */ 00198 EventSource cd_sleep_event; 00199 /** 00200 * @brief Exiting sleep state event. 00201 */ 00202 EventSource cd_wakeup_event; 00203 #endif /* CAN_USE_SLEEP_MODE */ 00204 /* End of the mandatory fields.*/ 00205 } CANDriver; 00206 00207 /*===========================================================================*/ 00208 /* Driver macros. */ 00209 /*===========================================================================*/ 00210 00211 /*===========================================================================*/ 00212 /* External declarations. */ 00213 /*===========================================================================*/ 00214 00215 #ifdef __cplusplus 00216 extern "C" { 00217 #endif 00218 void can_lld_init(void); 00219 void can_lld_start(CANDriver *canp); 00220 void can_lld_stop(CANDriver *canp); 00221 bool_t can_lld_can_transmit(CANDriver *canp); 00222 void can_lld_transmit(CANDriver *canp, const CANTxFrame *crfp); 00223 bool_t can_lld_can_receive(CANDriver *canp); 00224 void can_lld_receive(CANDriver *canp, CANRxFrame *ctfp); 00225 #if CAN_USE_SLEEP_MODE 00226 void can_lld_sleep(CANDriver *canp); 00227 void can_lld_wakeup(CANDriver *canp); 00228 #endif /* CAN_USE_SLEEP_MODE */ 00229 #ifdef __cplusplus 00230 } 00231 #endif 00232 00233 #endif /* CH_HAL_USE_CAN */ 00234 00235 #endif /* _CAN_LLD_H_ */ 00236 00237 /** @} */