Driver_CAN.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c) 2015-2020 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. 0U, // Does not support 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. 0U, // Does not support bus monitoring mode
  29. 0U, // Does not support internal loopback mode
  30. 0U, // Does not support external loopback mode
  31. 0U // Reserved (must be zero)
  32. };
  33. // Object Capabilities
  34. static const ARM_CAN_OBJ_CAPABILITIES can_object_capabilities = {
  35. 1U, // Object supports transmission
  36. 1U, // Object supports reception
  37. 0U, // Object does not support RTR reception and automatic Data transmission
  38. 0U, // Object does not support RTR transmission and automatic Data reception
  39. 0U, // Object does not allow assignment of multiple filters to it
  40. 0U, // Object does not support exact identifier filtering
  41. 0U, // Object does not support range identifier filtering
  42. 0U, // Object does not support mask identifier filtering
  43. 0U, // Object can not buffer messages
  44. 0U // Reserved (must be zero)
  45. };
  46. static uint8_t can_driver_powered = 0U;
  47. static uint8_t can_driver_initialized = 0U;
  48. static ARM_CAN_SignalUnitEvent_t CAN_SignalUnitEvent = NULL;
  49. static ARM_CAN_SignalObjectEvent_t CAN_SignalObjectEvent = NULL;
  50. //
  51. // Functions
  52. //
  53. static ARM_DRIVER_VERSION ARM_CAN_GetVersion (void) {
  54. // Return driver version
  55. return can_driver_version;
  56. }
  57. static ARM_CAN_CAPABILITIES ARM_CAN_GetCapabilities (void) {
  58. // Return driver capabilities
  59. return can_driver_capabilities;
  60. }
  61. static int32_t ARM_CAN_Initialize (ARM_CAN_SignalUnitEvent_t cb_unit_event,
  62. ARM_CAN_SignalObjectEvent_t cb_object_event) {
  63. if (can_driver_initialized != 0U) { return ARM_DRIVER_OK; }
  64. CAN_SignalUnitEvent = cb_unit_event;
  65. CAN_SignalObjectEvent = cb_object_event;
  66. // Add code for pin, memory, RTX objects initialization
  67. // ..
  68. can_driver_initialized = 1U;
  69. return ARM_DRIVER_OK;
  70. }
  71. static int32_t ARM_CAN_Uninitialize (void) {
  72. // Add code for pin, memory, RTX objects de-initialization
  73. // ..
  74. can_driver_initialized = 0U;
  75. return ARM_DRIVER_OK;
  76. }
  77. static int32_t ARM_CAN_PowerControl (ARM_POWER_STATE state) {
  78. switch (state) {
  79. case ARM_POWER_OFF:
  80. can_driver_powered = 0U;
  81. // Add code to disable interrupts and put peripheral into reset mode,
  82. // and if possible disable clock
  83. // ..
  84. break;
  85. case ARM_POWER_FULL:
  86. if (can_driver_initialized == 0U) { return ARM_DRIVER_ERROR; }
  87. if (can_driver_powered != 0U) { return ARM_DRIVER_OK; }
  88. // Add code to enable clocks, reset variables enable interrupts
  89. // and put peripheral into operational
  90. // ..
  91. can_driver_powered = 1U;
  92. break;
  93. case ARM_POWER_LOW:
  94. return ARM_DRIVER_ERROR_UNSUPPORTED;
  95. }
  96. return ARM_DRIVER_OK;
  97. }
  98. static uint32_t ARM_CAN_GetClock (void) {
  99. // Add code to return peripheral clock frequency
  100. // ..
  101. }
  102. static int32_t ARM_CAN_SetBitrate (ARM_CAN_BITRATE_SELECT select, uint32_t bitrate, uint32_t bit_segments) {
  103. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  104. // Add code to setup peripheral parameters to generate specified bitrate
  105. // with specified bit segments
  106. // ..
  107. return ARM_DRIVER_OK;
  108. }
  109. static int32_t ARM_CAN_SetMode (ARM_CAN_MODE mode) {
  110. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  111. switch (mode) {
  112. case ARM_CAN_MODE_INITIALIZATION:
  113. // Add code to put peripheral into initialization mode
  114. // ..
  115. break;
  116. case ARM_CAN_MODE_NORMAL:
  117. // Add code to put peripheral into normal operation mode
  118. // ..
  119. break;
  120. case ARM_CAN_MODE_RESTRICTED:
  121. // Add code to put peripheral into restricted operation mode
  122. // ..
  123. break;
  124. case ARM_CAN_MODE_MONITOR:
  125. // Add code to put peripheral into bus monitoring mode
  126. // ..
  127. break;
  128. case ARM_CAN_MODE_LOOPBACK_INTERNAL:
  129. // Add code to put peripheral into internal loopback mode
  130. // ..
  131. break;
  132. case ARM_CAN_MODE_LOOPBACK_EXTERNAL:
  133. // Add code to put peripheral into external loopback mode
  134. // ..
  135. break;
  136. }
  137. return ARM_DRIVER_OK;
  138. }
  139. static ARM_CAN_OBJ_CAPABILITIES ARM_CAN_ObjectGetCapabilities (uint32_t obj_idx) {
  140. // Return object capabilities
  141. return can_object_capabilities;
  142. }
  143. static int32_t ARM_CAN_ObjectSetFilter (uint32_t obj_idx, ARM_CAN_FILTER_OPERATION operation, uint32_t id, uint32_t arg) {
  144. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  145. switch (operation) {
  146. case ARM_CAN_FILTER_ID_EXACT_ADD:
  147. // Add code to setup peripheral to receive messages with specified exact ID
  148. break;
  149. case ARM_CAN_FILTER_ID_MASKABLE_ADD:
  150. // Add code to setup peripheral to receive messages with specified maskable ID
  151. break;
  152. case ARM_CAN_FILTER_ID_RANGE_ADD:
  153. // Add code to setup peripheral to receive messages within specified range of IDs
  154. break;
  155. case ARM_CAN_FILTER_ID_EXACT_REMOVE:
  156. // Add code to remove specified exact ID from being received by peripheral
  157. break;
  158. case ARM_CAN_FILTER_ID_MASKABLE_REMOVE:
  159. // Add code to remove specified maskable ID from being received by peripheral
  160. break;
  161. case ARM_CAN_FILTER_ID_RANGE_REMOVE:
  162. // Add code to remove specified range of IDs from being received by peripheral
  163. break;
  164. }
  165. return ARM_DRIVER_OK;
  166. }
  167. static int32_t ARM_CAN_ObjectConfigure (uint32_t obj_idx, ARM_CAN_OBJ_CONFIG obj_cfg) {
  168. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  169. switch (obj_cfg) {
  170. case ARM_CAN_OBJ_INACTIVE:
  171. // Deactivate object
  172. // ..
  173. break;
  174. case ARM_CAN_OBJ_RX_RTR_TX_DATA:
  175. // Setup object to automatically return data when RTR with it's ID is received
  176. // ..
  177. break;
  178. case ARM_CAN_OBJ_TX_RTR_RX_DATA:
  179. // Setup object to send RTR and receive data response
  180. // ..
  181. break;
  182. case ARM_CAN_OBJ_TX:
  183. // Setup object to be used for sending messages
  184. // ..
  185. break;
  186. case ARM_CAN_OBJ_RX:
  187. // Setup object to be used for receiving messages
  188. // ..
  189. break;
  190. }
  191. return ARM_DRIVER_OK;
  192. }
  193. static int32_t ARM_CAN_MessageSend (uint32_t obj_idx, ARM_CAN_MSG_INFO *msg_info, const uint8_t *data, uint8_t size) {
  194. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  195. // Add code to send requested message
  196. // ..
  197. return ((int32_t)size);
  198. }
  199. static int32_t ARM_CAN_MessageRead (uint32_t obj_idx, ARM_CAN_MSG_INFO *msg_info, uint8_t *data, uint8_t size) {
  200. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  201. // Add code to read previously received message
  202. // (reception was started when object was configured for reception)
  203. // ..
  204. return ((int32_t)size);
  205. }
  206. static int32_t ARM_CAN_Control (uint32_t control, uint32_t arg) {
  207. if (can_driver_powered == 0U) { return ARM_DRIVER_ERROR; }
  208. switch (control & ARM_CAN_CONTROL_Msk) {
  209. case ARM_CAN_ABORT_MESSAGE_SEND:
  210. // Add code to abort message pending to be sent
  211. // ..
  212. break;
  213. case ARM_CAN_SET_FD_MODE:
  214. // Add code to enable Flexible Data-rate mode
  215. // ..
  216. break;
  217. case ARM_CAN_SET_TRANSCEIVER_DELAY:
  218. // Add code to set transceiver delay
  219. // ..
  220. break;
  221. default:
  222. // Handle unknown control code
  223. return ARM_DRIVER_ERROR_UNSUPPORTED;
  224. }
  225. return ARM_DRIVER_OK;
  226. }
  227. static ARM_CAN_STATUS ARM_CAN_GetStatus (void) {
  228. // Add code to return device bus and error status
  229. // ..
  230. }
  231. // IRQ handlers
  232. // Add interrupt routines to handle transmission, reception, error and status interrupts
  233. // ..
  234. // CAN driver functions structure
  235. extern \
  236. ARM_DRIVER_CAN Driver_CAN0;
  237. ARM_DRIVER_CAN Driver_CAN0 = {
  238. ARM_CAN_GetVersion,
  239. ARM_CAN_GetCapabilities,
  240. ARM_CAN_Initialize,
  241. ARM_CAN_Uninitialize,
  242. ARM_CAN_PowerControl,
  243. ARM_CAN_GetClock,
  244. ARM_CAN_SetBitrate,
  245. ARM_CAN_SetMode,
  246. ARM_CAN_ObjectGetCapabilities,
  247. ARM_CAN_ObjectSetFilter,
  248. ARM_CAN_ObjectConfigure,
  249. ARM_CAN_MessageSend,
  250. ARM_CAN_MessageRead,
  251. ARM_CAN_Control,
  252. ARM_CAN_GetStatus
  253. };