Driver_CAN.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (c) 2015-2018 Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "Driver_CAN.h"
  19. #define ARM_CAN_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(1,0) // CAN driver version
  20. // Driver Version
  21. static const ARM_DRIVER_VERSION can_driver_version = { ARM_CAN_API_VERSION, ARM_CAN_DRV_VERSION };
  22. // Driver Capabilities
  23. static const ARM_CAN_CAPABILITIES can_driver_capabilities = {
  24. 32U, // Number of CAN Objects available
  25. 1U, // Supports reentrant calls to ARM_CAN_MessageSend, ARM_CAN_MessageRead, ARM_CAN_ObjectConfigure and abort message sending used by ARM_CAN_Control.
  26. 0U, // Does not support CAN with Flexible Data-rate mode (CAN_FD)
  27. 0U, // Does not support restricted operation mode
  28. 1U, // Supports bus monitoring mode
  29. 1U, // Supports internal loopback mode
  30. 1U, // Supports external loopback mode
  31. };
  32. // Object Capabilities
  33. static const ARM_CAN_OBJ_CAPABILITIES can_object_capabilities = {
  34. 1U, // Object supports transmission
  35. 1U, // Object supports reception
  36. 0U, // Object does not support RTR reception and automatic Data transmission
  37. 0U, // Object does not support RTR transmission and automatic Data reception
  38. 1U, // Object allows assignment of multiple filters to it
  39. 1U, // Object supports exact identifier filtering
  40. 0U, // Object does not support range identifier filtering
  41. 1U, // Object supports mask identifier filtering
  42. 3U // Object can buffer 3 messages
  43. };
  44. static uint8_t can_driver_powered = 0U;
  45. static uint8_t can_driver_initialized = 0U;
  46. static ARM_CAN_SignalUnitEvent_t CAN_SignalUnitEvent = NULL;
  47. static ARM_CAN_SignalObjectEvent_t CAN_SignalObjectEvent = NULL;
  48. //
  49. // Functions
  50. //
  51. static ARM_DRIVER_VERSION CAN_GetVersion (void) {
  52. // Return driver version
  53. return can_driver_version;
  54. }
  55. static ARM_CAN_CAPABILITIES CAN_GetCapabilities (void) {
  56. // Return driver capabilities
  57. return can_driver_capabilities;
  58. }
  59. static int32_t CAN_Initialize (ARM_CAN_SignalUnitEvent_t cb_unit_event,
  60. ARM_CAN_SignalObjectEvent_t cb_object_event) {
  61. if (can_driver_initialized != 0U) { return ARM_DRIVER_OK; }
  62. CAN_SignalUnitEvent = cb_unit_event;
  63. CAN_SignalObjectEvent = cb_object_event;
  64. // Add code for pin, memory, RTX objects initialization
  65. // ..
  66. can_driver_initialized = 1U;
  67. return ARM_DRIVER_OK;
  68. }
  69. static int32_t CAN_Uninitialize (void) {
  70. // Add code for pin, memory, RTX objects de-initialization
  71. // ..
  72. can_driver_initialized = 0U;
  73. return ARM_DRIVER_OK;
  74. }
  75. static int32_t CAN_PowerControl (ARM_POWER_STATE state) {
  76. switch (state) {
  77. case ARM_POWER_OFF:
  78. can_driver_powered = 0U;
  79. // Add code to disable interrupts and put peripheral into reset mode,
  80. // and if possible disable clock
  81. // ..
  82. case ARM_POWER_FULL:
  83. if (can_driver_initialized == 0U) { return ARM_DRIVER_ERROR; }
  84. if (can_driver_powered != 0U) { return ARM_DRIVER_OK; }
  85. // Add code to enable clocks, reset variables enable interrupts
  86. // and put peripheral into operational
  87. // ..
  88. can_driver_powered = 1U;
  89. break;
  90. default:
  91. // Other states are not supported
  92. return ARM_DRIVER_ERROR_UNSUPPORTED;
  93. }
  94. return ARM_DRIVER_OK;
  95. }
  96. uint32_t CAN_GetClock (void) {
  97. // Add code to return peripheral clock frequency
  98. // ..
  99. }
  100. static int32_t CAN_SetBitrate (ARM_CAN_BITRATE_SELECT select, uint32_t bitrate, uint32_t bit_segments) {
  101. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  102. // Add code to setup peripheral parameters to generate specified bitrate
  103. // with specified bit segments
  104. // ..
  105. return ARM_DRIVER_OK;
  106. }
  107. static int32_t CAN_SetMode (ARM_CAN_MODE mode) {
  108. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  109. switch (mode) {
  110. case ARM_CAN_MODE_INITIALIZATION:
  111. // Add code to put peripheral into initialization mode
  112. // ..
  113. break;
  114. case ARM_CAN_MODE_NORMAL:
  115. // Add code to put peripheral into normal operation mode
  116. // ..
  117. break;
  118. case ARM_CAN_MODE_RESTRICTED:
  119. // Add code to put peripheral into restricted operation mode
  120. // ..
  121. break;
  122. case ARM_CAN_MODE_MONITOR:
  123. // Add code to put peripheral into bus monitoring mode
  124. // ..
  125. break;
  126. case ARM_CAN_MODE_LOOPBACK_INTERNAL:
  127. // Add code to put peripheral into internal loopback mode
  128. // ..
  129. break;
  130. case ARM_CAN_MODE_LOOPBACK_EXTERNAL:
  131. // Add code to put peripheral into external loopback mode
  132. // ..
  133. break;
  134. default:
  135. // Handle unknown mode code
  136. return ARM_DRIVER_ERROR_UNSUPPORTED;
  137. }
  138. return ARM_DRIVER_OK;
  139. }
  140. ARM_CAN_OBJ_CAPABILITIES CAN_ObjectGetCapabilities (uint32_t obj_idx) {
  141. // Return object capabilities
  142. return can_object_capabilities;
  143. }
  144. static int32_t CAN_ObjectSetFilter (uint32_t obj_idx, ARM_CAN_FILTER_OPERATION operation, uint32_t id, uint32_t arg) {
  145. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  146. switch (operation) {
  147. case ARM_CAN_FILTER_ID_EXACT_ADD:
  148. // Add code to setup peripheral to receive messages with specified exact ID
  149. break;
  150. case ARM_CAN_FILTER_ID_MASKABLE_ADD:
  151. // Add code to setup peripheral to receive messages with specified maskable ID
  152. break;
  153. case ARM_CAN_FILTER_ID_RANGE_ADD:
  154. // Add code to setup peripheral to receive messages within specified range of IDs
  155. break;
  156. case ARM_CAN_FILTER_ID_EXACT_REMOVE:
  157. // Add code to remove specified exact ID from being received by peripheral
  158. break;
  159. case ARM_CAN_FILTER_ID_MASKABLE_REMOVE:
  160. // Add code to remove specified maskable ID from being received by peripheral
  161. break;
  162. case ARM_CAN_FILTER_ID_RANGE_REMOVE:
  163. // Add code to remove specified range of IDs from being received by peripheral
  164. break;
  165. default:
  166. // Handle unknown operation code
  167. return ARM_DRIVER_ERROR_UNSUPPORTED;
  168. }
  169. return ARM_DRIVER_OK;
  170. }
  171. static int32_t CAN_ObjectConfigure (uint32_t obj_idx, ARM_CAN_OBJ_CONFIG obj_cfg) {
  172. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  173. switch (obj_cfg) {
  174. case ARM_CAN_OBJ_INACTIVE:
  175. // Deactivate object
  176. // ..
  177. break;
  178. case ARM_CAN_OBJ_RX_RTR_TX_DATA:
  179. // Setup object to automatically return data when RTR with it's ID is received
  180. // ..
  181. break;
  182. case ARM_CAN_OBJ_TX_RTR_RX_DATA:
  183. // Setup object to send RTR and receive data response
  184. // ..
  185. break;
  186. case ARM_CAN_OBJ_TX:
  187. // Setup object to be used for sending messages
  188. // ..
  189. break;
  190. case ARM_CAN_OBJ_RX:
  191. // Setup object to be used for receiving messages
  192. // ..
  193. break;
  194. default:
  195. // Handle unknown object configuration code
  196. return ARM_DRIVER_ERROR_UNSUPPORTED;
  197. }
  198. return ARM_DRIVER_OK;
  199. }
  200. static int32_t CAN_MessageSend (uint32_t obj_idx, ARM_CAN_MSG_INFO *msg_info, const uint8_t *data, uint8_t size) {
  201. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  202. // Add code to send requested message
  203. // ..
  204. return ((int32_t)size);
  205. }
  206. static int32_t CAN_MessageRead (uint32_t obj_idx, ARM_CAN_MSG_INFO *msg_info, uint8_t *data, uint8_t size) {
  207. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  208. // Add code to read previously received message
  209. // (reception was started when object was configured for reception)
  210. // ..
  211. return ((int32_t)size);
  212. }
  213. static int32_t CAN_Control (uint32_t control, uint32_t arg) {
  214. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  215. switch (control & ARM_CAN_CONTROL_Msk) {
  216. case ARM_CAN_ABORT_MESSAGE_SEND:
  217. // Add code to abort message pending to be sent
  218. // ..
  219. break;
  220. case ARM_CAN_SET_FD_MODE:
  221. // Add code to enable Flexible Data-rate mode
  222. // ..
  223. break;
  224. case ARM_CAN_SET_TRANSCEIVER_DELAY:
  225. // Add code to set transceiver delay
  226. // ..
  227. break;
  228. default:
  229. // Handle unknown control code
  230. return ARM_DRIVER_ERROR_UNSUPPORTED;
  231. }
  232. return ARM_DRIVER_OK;
  233. }
  234. static ARM_CAN_STATUS CAN_GetStatus (void) {
  235. // Add code to return device bus and error status
  236. // ..
  237. }
  238. // IRQ handlers
  239. // Add interrupt routines to handle transmission, reception, error and status interrupts
  240. // ..
  241. // CAN driver functions structure
  242. ARM_DRIVER_CAN Driver_CAN = {
  243. CAN_GetVersion,
  244. CAN_GetCapabilities,
  245. CAN_Initialize,
  246. CAN_Uninitialize,
  247. CAN_PowerControl,
  248. CAN_GetClock,
  249. CAN_SetBitrate,
  250. CAN_SetMode,
  251. CAN_ObjectGetCapabilities,
  252. CAN_ObjectSetFilter,
  253. CAN_ObjectConfigure,
  254. CAN_MessageSend,
  255. CAN_MessageRead,
  256. CAN_Control,
  257. CAN_GetStatus
  258. };