dri_can.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. #include "dri_can.h"
  2. #include "dri_Flash.h"
  3. #include <rthw.h>
  4. #include <rtthread.h>
  5. #include "n32g45x_can.h"
  6. #include "n32g45x_gpio.h"
  7. #include "string.h"
  8. #include "application.h"
  9. #include "algorithm.h"
  10. uint16_t canBitrate = 6;
  11. #define DRONECAN_MEM_ARENA_SIZE 4096U
  12. #define DRONECAN_MEM_BLOCK_COUNT (DRONECAN_MEM_ARENA_SIZE / CANARD_MEM_BLOCK_SIZE)
  13. #define DRONECAN_RX_QUEUE_SIZE 64U
  14. #define DRONECAN_RX_THREAD_STACK_SIZE 2048U
  15. #define DRONECAN_RX_THREAD_PRIORITY 3U
  16. #define DRONECAN_RX_THREAD_TICK 1U
  17. #define DRONECAN_RX_POLL_TICKS ( RT_TICK_PER_SECOND / 20U )
  18. #define DRONECAN_UNIQUE_ID_LENGTH 16U
  19. #define DRONECAN_DNA_REQUEST_CHUNK_LENGTH 6U
  20. #define DRONECAN_DNA_FIRST_REQUEST_MIN_MS 600U
  21. #define DRONECAN_DNA_FIRST_REQUEST_JITTER_MS 400U
  22. #define DRONECAN_DNA_FOLLOWUP_DELAY_MS 50U
  23. #define DRONECAN_DNA_FOLLOWUP_TIMEOUT_MS UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_FOLLOWUP_TIMEOUT_MS
  24. #define DRONECAN_DNA_MAX_TIMEOUTS 5U
  25. #define DRONECAN_STATIC_NODE_ID 125U
  26. #define DRONECAN_NODE_INFO_NAME "com.vkradar.r4d"
  27. #define DRONECAN_NODE_INFO_NAME_LENGTH ( sizeof( DRONECAN_NODE_INFO_NAME ) - 1U )
  28. #define DRONECAN_SOFTWARE_VERSION_MAJOR 5U
  29. #define DRONECAN_SOFTWARE_VERSION_MINOR 0U
  30. #define DRONECAN_HARDWARE_VERSION_MAJOR 1U
  31. #define DRONECAN_HARDWARE_VERSION_MINOR 0U
  32. #define DRONECAN_BEGIN_FW_UPDATE_ERROR_MESSAGE "DroneCAN bootloader unavailable"
  33. #define DRONECAN_BEGIN_FW_UPDATE_STORAGE_ERROR_MESSAGE "Cannot store update context"
  34. #define DRONECAN_CAN_BITRATE 500000U
  35. #define FRAME_NUM 5
  36. #define FRAME_DATA_LEN 8
  37. #define VALID_PAYLOAD_LEN (FRAME_NUM * (FRAME_DATA_LEN - 1))
  38. typedef struct {
  39. uint8_t buf[VALID_PAYLOAD_LEN];
  40. uint8_t busy;
  41. uint8_t done;
  42. uint8_t expected_sequence;
  43. uint8_t rx_count;
  44. }can_rx_longframe;
  45. can_rx_longframe can_info_frame = {0};
  46. CanardInstance canard = {0};
  47. static CanardPoolAllocatorBlock canard_memory_pool[DRONECAN_MEM_BLOCK_COUNT] = {0};
  48. static uint8_t transfer_id_global_navigation_solution = 0;
  49. static uint8_t transfer_id_range_sensor_measurement = 0;
  50. static uint8_t transfer_id_node_status = 0;
  51. static uint8_t transfer_id_dynamic_node_id_allocation = 0;
  52. static volatile uint8_t dronecan_ready = 0;
  53. static struct rt_semaphore dronecan_rx_sem;
  54. static struct rt_thread dronecan_rx_thread;
  55. static struct rt_mutex dronecan_tx_lock;
  56. static volatile uint8_t dronecan_tx_lock_ready = 0;
  57. ALIGN( RT_ALIGN_SIZE )
  58. static rt_uint8_t dronecan_rx_stack[DRONECAN_RX_THREAD_STACK_SIZE];
  59. static CanRxMessage dronecan_rx_queue[DRONECAN_RX_QUEUE_SIZE];
  60. static volatile uint8_t dronecan_rx_head = 0;
  61. static volatile uint8_t dronecan_rx_tail = 0;
  62. static volatile uint8_t dronecan_rx_count = 0;
  63. static volatile uint8_t dronecan_rx_started = 0;
  64. static volatile uint32_t dronecan_rx_overflow = 0;
  65. static uint8_t dronecan_unique_id[DRONECAN_UNIQUE_ID_LENGTH] = {0};
  66. static uint8_t dronecan_dna_request_offset = 0;
  67. static uint8_t dronecan_dna_expected_uid_length = 0;
  68. static uint8_t dronecan_dna_waiting_for_response = 0;
  69. static uint8_t dronecan_dna_retry_count = 0;
  70. static uint8_t dronecan_dna_timeout_count = 0;
  71. static rt_tick_t dronecan_dna_next_request_tick = 0;
  72. static rt_tick_t dronecan_dna_response_deadline_tick = 0;
  73. extern float q_now[4];
  74. extern float p_now[3];
  75. extern uint8_t other[4];
  76. extern int16_t euler[3];
  77. extern uint8_t ParameterData[8];
  78. extern rt_sem_t parameter_sem_t;
  79. static uint64_t DroneCAN_GetTimeUsec( void )
  80. {
  81. return ( ( uint64_t )rt_tick_get() * 1000000ULL ) / ( uint64_t )RT_TICK_PER_SECOND;
  82. }
  83. void FLASH_LOG( void );
  84. static void DroneCAN_FlushTxQueue( void );
  85. static rt_tick_t DroneCAN_MillisecondsToTicks( uint32_t milliseconds )
  86. {
  87. return ( rt_tick_t )( ( milliseconds * RT_TICK_PER_SECOND + 999U ) / 1000U );
  88. }
  89. static bool DroneCAN_TickReached( rt_tick_t now, rt_tick_t deadline )
  90. {
  91. return ( ( int32_t )( now - deadline ) >= 0 );
  92. }
  93. static bool DroneCAN_TxLock( void )
  94. {
  95. return ( dronecan_tx_lock_ready != 0 &&
  96. rt_mutex_take( &dronecan_tx_lock, RT_WAITING_FOREVER ) == RT_EOK );
  97. }
  98. static void DroneCAN_TxUnlock( void )
  99. {
  100. rt_mutex_release( &dronecan_tx_lock );
  101. }
  102. bool DroneCAN_IsNodeIDAllocated( void )
  103. {
  104. return ( dronecan_ready != 0 &&
  105. canardGetLocalNodeID( &canard ) != CANARD_BROADCAST_NODE_ID );
  106. }
  107. static bool DroneCAN_ShouldAccept( const CanardInstance *ins, uint64_t *out_data_type_signature, uint16_t data_type_id, CanardTransferType transfer_type, uint8_t source_node_id )
  108. {
  109. ( void )source_node_id;
  110. if ( transfer_type == CanardTransferTypeBroadcast &&
  111. data_type_id == UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_ID &&
  112. canardGetLocalNodeID( ins ) == CANARD_BROADCAST_NODE_ID )
  113. {
  114. *out_data_type_signature = UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_SIGNATURE;
  115. return true;
  116. }
  117. if ( transfer_type == CanardTransferTypeBroadcast &&
  118. data_type_id == UAVCAN_NAVIGATION_GLOBALNAVIGATIONSOLUTION_ID )
  119. {
  120. *out_data_type_signature = UAVCAN_NAVIGATION_GLOBALNAVIGATIONSOLUTION_SIGNATURE;
  121. return true;
  122. }
  123. if ( transfer_type == CanardTransferTypeRequest &&
  124. data_type_id == UAVCAN_PROTOCOL_GETNODEINFO_ID &&
  125. canardGetLocalNodeID( ins ) != CANARD_BROADCAST_NODE_ID )
  126. {
  127. *out_data_type_signature = UAVCAN_PROTOCOL_GETNODEINFO_SIGNATURE;
  128. return true;
  129. }
  130. if ( transfer_type == CanardTransferTypeRequest &&
  131. data_type_id == UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_ID &&
  132. canardGetLocalNodeID( ins ) != CANARD_BROADCAST_NODE_ID )
  133. {
  134. *out_data_type_signature = UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_SIGNATURE;
  135. return true;
  136. }
  137. return false;
  138. }
  139. static bool DroneCAN_IsRelevantFrame( const CanRxMessage *rx_msg )
  140. {
  141. uint32_t ext_id;
  142. if ( rx_msg == RT_NULL ||
  143. rx_msg->IDE != CAN_Extended_Id ||
  144. rx_msg->RTR != CAN_RTRQ_Data ||
  145. rx_msg->DLC == 0 )
  146. {
  147. return false;
  148. }
  149. ext_id = rx_msg->ExtId;
  150. if ( ( ( ext_id >> 7U ) & 0x1U ) == 0U )
  151. {
  152. const uint16_t data_type_id = ( uint16_t )( ( ext_id >> 8U ) & 0xFFFFU );
  153. return ( data_type_id == UAVCAN_NAVIGATION_GLOBALNAVIGATIONSOLUTION_ID ||
  154. data_type_id == UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_ID );
  155. }
  156. return ( DroneCAN_IsNodeIDAllocated() &&
  157. ( ( ( uint8_t )( ( ext_id >> 16U ) & 0xFFU ) == UAVCAN_PROTOCOL_GETNODEINFO_ID ) ||
  158. ( ( uint8_t )( ( ext_id >> 16U ) & 0xFFU ) == UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_ID ) ) &&
  159. ( ( ( ext_id >> 15U ) & 0x1U ) != 0U ) &&
  160. ( ( uint8_t )( ( ext_id >> 8U ) & 0x7FU ) == canardGetLocalNodeID( &canard ) ) );
  161. }
  162. static void DroneCAN_DNA_Reset( rt_tick_t base_tick )
  163. {
  164. const uint32_t jitter = ( uint32_t )( dronecan_unique_id[0] +
  165. dronecan_unique_id[15] +
  166. dronecan_dna_retry_count * 37U ) %
  167. ( DRONECAN_DNA_FIRST_REQUEST_JITTER_MS + 1U );
  168. dronecan_dna_request_offset = 0;
  169. dronecan_dna_expected_uid_length = 0;
  170. dronecan_dna_waiting_for_response = 0;
  171. dronecan_dna_retry_count++;
  172. dronecan_dna_next_request_tick = base_tick +
  173. DroneCAN_MillisecondsToTicks( DRONECAN_DNA_FIRST_REQUEST_MIN_MS + jitter );
  174. }
  175. static bool DroneCAN_SendDynamicNodeIDRequest( uint8_t uid_offset )
  176. {
  177. struct uavcan_protocol_dynamic_node_id_Allocation request = {0};
  178. uint8_t buffer[UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_MAX_SIZE] = {0};
  179. uint8_t uid_length;
  180. uint32_t length;
  181. int16_t result;
  182. if ( uid_offset >= DRONECAN_UNIQUE_ID_LENGTH || DroneCAN_IsNodeIDAllocated() )
  183. {
  184. return false;
  185. }
  186. uid_length = DRONECAN_UNIQUE_ID_LENGTH - uid_offset;
  187. if ( uid_length > DRONECAN_DNA_REQUEST_CHUNK_LENGTH )
  188. {
  189. uid_length = DRONECAN_DNA_REQUEST_CHUNK_LENGTH;
  190. }
  191. request.node_id = CANARD_BROADCAST_NODE_ID;
  192. request.first_part_of_unique_id = ( uid_offset == 0U );
  193. request.unique_id.len = uid_length;
  194. memcpy( request.unique_id.data, &dronecan_unique_id[uid_offset], uid_length );
  195. length = uavcan_protocol_dynamic_node_id_Allocation_encode( &request, buffer );
  196. if ( DroneCAN_TxLock() == false )
  197. {
  198. return false;
  199. }
  200. result = canardBroadcast( &canard,
  201. UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_SIGNATURE,
  202. UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_ID,
  203. &transfer_id_dynamic_node_id_allocation,
  204. CANARD_TRANSFER_PRIORITY_LOW,
  205. buffer,
  206. ( uint16_t )length );
  207. if ( result > 0 )
  208. {
  209. DroneCAN_FlushTxQueue();
  210. }
  211. DroneCAN_TxUnlock();
  212. return ( result > 0 );
  213. }
  214. static void DroneCAN_SendInitialNodeStatus( void )
  215. {
  216. struct uavcan_protocol_NodeStatus status = {0};
  217. status.uptime_sec = rt_tick_get() / RT_TICK_PER_SECOND;
  218. status.health = UAVCAN_PROTOCOL_NODESTATUS_HEALTH_OK;
  219. status.mode = UAVCAN_PROTOCOL_NODESTATUS_MODE_OPERATIONAL;
  220. DroneCAN_SendNodeStatus( &status );
  221. }
  222. static void DroneCAN_UseStaticNodeID( void )
  223. {
  224. if ( DroneCAN_IsNodeIDAllocated() ||
  225. DRONECAN_STATIC_NODE_ID < CANARD_MIN_NODE_ID ||
  226. DRONECAN_STATIC_NODE_ID > CANARD_MAX_NODE_ID )
  227. {
  228. return;
  229. }
  230. canardSetLocalNodeID( &canard, DRONECAN_STATIC_NODE_ID );
  231. dronecan_dna_waiting_for_response = 0;
  232. dronecan_dna_request_offset = DRONECAN_UNIQUE_ID_LENGTH;
  233. dronecanUpdateInfo.local_node_id = DRONECAN_STATIC_NODE_ID;
  234. // write_dev_info();
  235. DroneCAN_SendInitialNodeStatus();
  236. }
  237. static void DroneCAN_HandleDynamicNodeIDAllocation( CanardRxTransfer *transfer )
  238. {
  239. struct uavcan_protocol_dynamic_node_id_Allocation response = {0};
  240. rt_tick_t now = rt_tick_get();
  241. if ( DroneCAN_IsNodeIDAllocated() ||
  242. uavcan_protocol_dynamic_node_id_Allocation_decode( transfer, &response ) != false )
  243. {
  244. return;
  245. }
  246. /* Another anonymous requester interrupts the allocator's UID session. */
  247. if ( transfer->source_node_id == CANARD_BROADCAST_NODE_ID )
  248. {
  249. DroneCAN_DNA_Reset( now );
  250. return;
  251. }
  252. if ( dronecan_dna_waiting_for_response == 0 ||
  253. response.unique_id.len != dronecan_dna_expected_uid_length ||
  254. memcmp( response.unique_id.data, dronecan_unique_id, response.unique_id.len ) != 0 )
  255. {
  256. DroneCAN_DNA_Reset( now );
  257. return;
  258. }
  259. if ( response.unique_id.len == DRONECAN_UNIQUE_ID_LENGTH )
  260. {
  261. if ( response.node_id < CANARD_MIN_NODE_ID || response.node_id > CANARD_MAX_NODE_ID )
  262. {
  263. DroneCAN_DNA_Reset( now );
  264. return;
  265. }
  266. canardSetLocalNodeID( &canard, response.node_id );
  267. dronecan_dna_waiting_for_response = 0;
  268. dronecan_dna_request_offset = DRONECAN_UNIQUE_ID_LENGTH;
  269. dronecan_dna_timeout_count = 0;
  270. // devInfo.nodeId = response.node_id;
  271. // write_dev_info();
  272. dronecanUpdateInfo.local_node_id = response.node_id;
  273. DroneCAN_SendInitialNodeStatus();
  274. return;
  275. }
  276. if ( response.node_id != CANARD_BROADCAST_NODE_ID )
  277. {
  278. DroneCAN_DNA_Reset( now );
  279. return;
  280. }
  281. dronecan_dna_request_offset = response.unique_id.len;
  282. dronecan_dna_waiting_for_response = 0;
  283. dronecan_dna_timeout_count = 0;
  284. dronecan_dna_next_request_tick = now + DroneCAN_MillisecondsToTicks( DRONECAN_DNA_FOLLOWUP_DELAY_MS );
  285. }
  286. static void DroneCAN_SendGetNodeInfoResponse( CanardRxTransfer *request )
  287. {
  288. struct uavcan_protocol_GetNodeInfoResponse response = {0};
  289. uint8_t buffer[UAVCAN_PROTOCOL_GETNODEINFO_RESPONSE_MAX_SIZE] = {0};
  290. uint32_t length;
  291. response.status.uptime_sec = rt_tick_get() / RT_TICK_PER_SECOND;
  292. response.status.health = UAVCAN_PROTOCOL_NODESTATUS_HEALTH_OK;
  293. response.status.mode = UAVCAN_PROTOCOL_NODESTATUS_MODE_OPERATIONAL;
  294. response.software_version.major = DRONECAN_SOFTWARE_VERSION_MAJOR;
  295. response.software_version.minor = DRONECAN_SOFTWARE_VERSION_MINOR;
  296. response.hardware_version.major = DRONECAN_HARDWARE_VERSION_MAJOR;
  297. response.hardware_version.minor = DRONECAN_HARDWARE_VERSION_MINOR;
  298. memcpy( response.hardware_version.unique_id, dronecan_unique_id, sizeof( dronecan_unique_id ) );
  299. response.name.len = DRONECAN_NODE_INFO_NAME_LENGTH;
  300. memcpy( response.name.data, DRONECAN_NODE_INFO_NAME, response.name.len );
  301. length = uavcan_protocol_GetNodeInfoResponse_encode( &response, buffer );
  302. if ( DroneCAN_TxLock() == false )
  303. {
  304. return;
  305. }
  306. canardReleaseRxTransferPayload( &canard, request );
  307. if ( canardRequestOrRespond( &canard,
  308. request->source_node_id,
  309. UAVCAN_PROTOCOL_GETNODEINFO_SIGNATURE,
  310. UAVCAN_PROTOCOL_GETNODEINFO_ID,
  311. &request->transfer_id,
  312. request->priority,
  313. CanardResponse,
  314. buffer,
  315. ( uint16_t )length ) > 0 )
  316. {
  317. DroneCAN_FlushTxQueue();
  318. }
  319. DroneCAN_TxUnlock();
  320. }
  321. static bool DroneCAN_SaveBeginFirmwareUpdateRequest(
  322. const struct uavcan_protocol_file_BeginFirmwareUpdateRequest *request )
  323. {
  324. const uint8_t path_length = request->image_file_remote_path.path.len;
  325. if ( request->source_node_id < CANARD_MIN_NODE_ID ||
  326. request->source_node_id > CANARD_MAX_NODE_ID ||
  327. path_length == 0U ||
  328. path_length > DRONECAN_FILE_PATH_MAX_LENGTH )
  329. {
  330. return false;
  331. }
  332. // memset( &dronecanUpdateInfo, 0, sizeof( dronecanUpdateInfo ) );
  333. dronecanUpdateInfo.magic = DRONECAN_UPDATE_CONTEXT_MAGIC;
  334. dronecanUpdateInfo.version = DRONECAN_UPDATE_CONTEXT_VERSION;
  335. dronecanUpdateInfo.size = sizeof( dronecanUpdateInfo );
  336. dronecanUpdateInfo.state = DRONECAN_UPDATE_STATE_PENDING;
  337. dronecanUpdateInfo.local_node_id = canardGetLocalNodeID( &canard );
  338. dronecanUpdateInfo.file_server_node_id = request->source_node_id;
  339. dronecanUpdateInfo.can_bitrate = DRONECAN_CAN_BITRATE;
  340. dronecanUpdateInfo.path_length = path_length;
  341. dronecanUpdateInfo.upgradeEable = 1;
  342. memcpy( dronecanUpdateInfo.path,
  343. request->image_file_remote_path.path.data,
  344. path_length );
  345. dronecanUpdateInfo.crc32 = DroneCAN_UpdateContextCalcCRC32( &dronecanUpdateInfo );
  346. return DroneCAN_UpdateContextWrite() == 0;
  347. }
  348. static void DroneCAN_SendBeginFirmwareUpdateResponse( CanardRxTransfer *request,
  349. uint8_t error,
  350. const char *error_message )
  351. {
  352. struct uavcan_protocol_file_BeginFirmwareUpdateResponse response = {0};
  353. uint8_t buffer[UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_RESPONSE_MAX_SIZE] = {0};
  354. size_t error_message_length = 0U;
  355. uint32_t length;
  356. response.error = error;
  357. if ( error_message != RT_NULL )
  358. {
  359. error_message_length = strlen( error_message );
  360. if ( error_message_length > sizeof( response.optional_error_message.data ) )
  361. {
  362. error_message_length = sizeof( response.optional_error_message.data );
  363. }
  364. response.optional_error_message.len = ( uint8_t )error_message_length;
  365. memcpy( response.optional_error_message.data, error_message, error_message_length );
  366. }
  367. length = uavcan_protocol_file_BeginFirmwareUpdateResponse_encode( &response, buffer );
  368. if ( DroneCAN_TxLock() == false )
  369. {
  370. return;
  371. }
  372. canardReleaseRxTransferPayload( &canard, request );
  373. if ( canardRequestOrRespond( &canard,
  374. request->source_node_id,
  375. UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_SIGNATURE,
  376. UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_ID,
  377. &request->transfer_id,
  378. request->priority,
  379. CanardResponse,
  380. buffer,
  381. ( uint16_t )length ) > 0 )
  382. {
  383. DroneCAN_FlushTxQueue();
  384. }
  385. DroneCAN_TxUnlock();
  386. }
  387. static void DroneCAN_OnReception( CanardInstance *ins, CanardRxTransfer *transfer )
  388. {
  389. ( void )ins;
  390. if ( transfer->transfer_type == CanardTransferTypeBroadcast &&
  391. transfer->data_type_id == UAVCAN_PROTOCOL_DYNAMIC_NODE_ID_ALLOCATION_ID )
  392. {
  393. DroneCAN_HandleDynamicNodeIDAllocation( transfer );
  394. return;
  395. }
  396. if ( transfer->transfer_type == CanardTransferTypeRequest &&
  397. transfer->data_type_id == UAVCAN_PROTOCOL_GETNODEINFO_ID &&
  398. DroneCAN_IsNodeIDAllocated() )
  399. {
  400. struct uavcan_protocol_GetNodeInfoRequest request;
  401. if ( uavcan_protocol_GetNodeInfoRequest_decode( transfer, &request ) == false )
  402. {
  403. DroneCAN_SendGetNodeInfoResponse( transfer );
  404. }
  405. return;
  406. }
  407. if ( transfer->transfer_type == CanardTransferTypeRequest &&
  408. transfer->data_type_id == UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_ID &&
  409. DroneCAN_IsNodeIDAllocated() )
  410. {
  411. struct uavcan_protocol_file_BeginFirmwareUpdateRequest request;
  412. if ( uavcan_protocol_file_BeginFirmwareUpdateRequest_decode( transfer, &request ) == false )
  413. {
  414. if ( DroneCAN_SaveBeginFirmwareUpdateRequest( &request ) )
  415. {
  416. /* Download/restart support is added with the DroneCAN bootloader. */
  417. DroneCAN_SendBeginFirmwareUpdateResponse(
  418. transfer,
  419. UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_RESPONSE_ERROR_OK,
  420. DRONECAN_BEGIN_FW_UPDATE_ERROR_MESSAGE );
  421. // FLASH_LOG();
  422. __set_PRIMASK( 1 );
  423. NVIC_SystemReset();
  424. }
  425. else
  426. {
  427. DroneCAN_SendBeginFirmwareUpdateResponse(
  428. transfer,
  429. UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_RESPONSE_ERROR_UNKNOWN,
  430. DRONECAN_BEGIN_FW_UPDATE_STORAGE_ERROR_MESSAGE );
  431. }
  432. }
  433. return;
  434. }
  435. if ( transfer->transfer_type == CanardTransferTypeBroadcast &&
  436. transfer->data_type_id == UAVCAN_NAVIGATION_GLOBALNAVIGATIONSOLUTION_ID )
  437. {
  438. static struct uavcan_navigation_GlobalNavigationSolution msg;
  439. if ( uavcan_navigation_GlobalNavigationSolution_decode( transfer, &msg ) == false )
  440. {
  441. rt_base_t level = rt_hw_interrupt_disable();
  442. q_now[0] = msg.orientation_xyzw[3];
  443. q_now[1] = msg.orientation_xyzw[0];
  444. q_now[2] = msg.orientation_xyzw[1];
  445. q_now[3] = msg.orientation_xyzw[2];
  446. p_now[0] = ( float )msg.longitude;
  447. p_now[1] = ( float )msg.latitude;
  448. p_now[2] = msg.height_msl;
  449. rt_hw_interrupt_enable( level );
  450. }
  451. }
  452. }
  453. static volatile int16_t dronecan_last_ret;
  454. static uint32_t dronecanCanardFalse = 0;
  455. static void DroneCAN_HandleRxMessage( const CanRxMessage *rx_msg )
  456. {
  457. CanardCANFrame rx_frame;
  458. uint8_t data_len;
  459. if ( dronecan_ready == 0 ||
  460. DroneCAN_IsRelevantFrame( rx_msg ) == false )
  461. {
  462. return;
  463. }
  464. data_len = rx_msg->DLC;
  465. if ( data_len > CANARD_CAN_FRAME_MAX_DATA_LEN )
  466. {
  467. data_len = CANARD_CAN_FRAME_MAX_DATA_LEN;
  468. }
  469. memset( &rx_frame, 0, sizeof( rx_frame ) );
  470. rx_frame.id = ( rx_msg->ExtId & CANARD_CAN_EXT_ID_MASK ) | CANARD_CAN_FRAME_EFF;
  471. rx_frame.data_len = data_len;
  472. rx_frame.iface_id = 0;
  473. memcpy( rx_frame.data, rx_msg->Data, data_len );
  474. dronecan_last_ret = canardHandleRxFrame( &canard, &rx_frame, DroneCAN_GetTimeUsec() );
  475. if (dronecan_last_ret != CANARD_OK)
  476. dronecanCanardFalse++;
  477. }
  478. static void DroneCAN_RxQueuePushFromIsr( const CanRxMessage *rx_msg )
  479. {
  480. if ( dronecan_rx_started == 0 ||
  481. DroneCAN_IsRelevantFrame( rx_msg ) == false )
  482. {
  483. return;
  484. }
  485. if ( dronecan_rx_count >= DRONECAN_RX_QUEUE_SIZE )
  486. {
  487. dronecan_rx_overflow++;
  488. return;
  489. }
  490. dronecan_rx_queue[dronecan_rx_head] = *rx_msg;
  491. dronecan_rx_head = ( uint8_t )( ( dronecan_rx_head + 1U ) % DRONECAN_RX_QUEUE_SIZE );
  492. if ( dronecan_rx_count++ == 0 )
  493. {
  494. rt_sem_release( &dronecan_rx_sem );
  495. }
  496. }
  497. static bool DroneCAN_RxQueuePop( CanRxMessage *rx_msg )
  498. {
  499. rt_base_t level;
  500. if ( rx_msg == RT_NULL )
  501. {
  502. return false;
  503. }
  504. level = rt_hw_interrupt_disable();
  505. if ( dronecan_rx_count == 0 )
  506. {
  507. rt_hw_interrupt_enable( level );
  508. return false;
  509. }
  510. *rx_msg = dronecan_rx_queue[dronecan_rx_tail];
  511. dronecan_rx_tail = ( uint8_t )( ( dronecan_rx_tail + 1U ) % DRONECAN_RX_QUEUE_SIZE );
  512. dronecan_rx_count--;
  513. rt_hw_interrupt_enable( level );
  514. return true;
  515. }
  516. static void DroneCAN_RxThreadEntry( void *parameter )
  517. {
  518. CanRxMessage rx_msg;
  519. rt_tick_t last_cleanup_tick = rt_tick_get();
  520. ( void )parameter;
  521. while ( 1 )
  522. {
  523. while ( DroneCAN_RxQueuePop( &rx_msg ) == true )
  524. {
  525. DroneCAN_HandleRxMessage( &rx_msg );
  526. }
  527. if ( DroneCAN_IsNodeIDAllocated() == false )
  528. {
  529. const rt_tick_t now = rt_tick_get();
  530. if ( dronecan_dna_waiting_for_response != 0 )
  531. {
  532. if ( DroneCAN_TickReached( now, dronecan_dna_response_deadline_tick ) )
  533. {
  534. dronecan_dna_timeout_count++;
  535. if ( dronecan_dna_timeout_count >= DRONECAN_DNA_MAX_TIMEOUTS )
  536. {
  537. DroneCAN_UseStaticNodeID();
  538. }
  539. else
  540. {
  541. DroneCAN_DNA_Reset( now );
  542. }
  543. }
  544. }
  545. else if ( DroneCAN_TickReached( now, dronecan_dna_next_request_tick ) &&
  546. DroneCAN_SendDynamicNodeIDRequest( dronecan_dna_request_offset ) )
  547. {
  548. uint8_t uid_length = DRONECAN_UNIQUE_ID_LENGTH - dronecan_dna_request_offset;
  549. if ( uid_length > DRONECAN_DNA_REQUEST_CHUNK_LENGTH )
  550. {
  551. uid_length = DRONECAN_DNA_REQUEST_CHUNK_LENGTH;
  552. }
  553. dronecan_dna_expected_uid_length = dronecan_dna_request_offset + uid_length;
  554. dronecan_dna_waiting_for_response = 1;
  555. dronecan_dna_response_deadline_tick = now +
  556. DroneCAN_MillisecondsToTicks( DRONECAN_DNA_FOLLOWUP_TIMEOUT_MS );
  557. }
  558. else if ( DroneCAN_TickReached( now, dronecan_dna_next_request_tick ) )
  559. {
  560. DroneCAN_DNA_Reset( now );
  561. }
  562. }
  563. if ( ( rt_tick_get() - last_cleanup_tick ) >= RT_TICK_PER_SECOND )
  564. {
  565. canardCleanupStaleTransfers( &canard, DroneCAN_GetTimeUsec() );
  566. last_cleanup_tick = rt_tick_get();
  567. }
  568. rt_sem_take( &dronecan_rx_sem, DRONECAN_RX_POLL_TICKS );
  569. }
  570. }
  571. const uint8_t auchCRCHi[] =
  572. {
  573. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  574. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  575. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  576. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  577. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  578. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  579. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  580. 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  581. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  582. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  583. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  584. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  585. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  586. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  587. 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  588. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  589. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  590. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  591. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  592. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  593. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  594. 0x00, 0xC1, 0x81, 0x40
  595. };
  596. const uint8_t auchCRCLo[] =
  597. {
  598. 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
  599. 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
  600. 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
  601. 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
  602. 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
  603. 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
  604. 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
  605. 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
  606. 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
  607. 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
  608. 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
  609. 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
  610. 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
  611. 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
  612. 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
  613. 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
  614. 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
  615. 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
  616. 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
  617. 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
  618. 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
  619. 0x41, 0x81, 0x80, 0x40
  620. };
  621. void can_init( void )
  622. {
  623. GPIO_InitType GPIO_InitStructure;
  624. CAN_InitType CAN_InitStructure;
  625. CAN_FilterInitType CAN_FilterInitStructure;
  626. RCC_EnableAPB2PeriphClk( RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_GPIOA, ENABLE );
  627. RCC_EnableAPB1PeriphClk( RCC_APB1_PERIPH_CAN1, ENABLE );
  628. GPIO_InitStructure.Pin = GPIO_PIN_11; //GPIO_PIN_8;
  629. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  630. GPIO_InitPeripheral( GPIOA, &GPIO_InitStructure );
  631. GPIO_InitStructure.Pin = GPIO_PIN_12;//GPIO_PIN_9;
  632. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  633. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  634. GPIO_InitPeripheral( GPIOA, &GPIO_InitStructure );
  635. GPIO_ConfigPinRemap( GPIO_RMP0_CAN1, ENABLE );
  636. CAN_DeInit( CAN1 );
  637. /* Struct init*/
  638. CAN_InitStruct( &CAN_InitStructure );
  639. CAN_InitStructure.ABOM = ENABLE;
  640. CAN_InitStructure.AWKUM = ENABLE;
  641. CAN_InitStructure.OperatingMode = CAN_Normal_Mode;
  642. CAN_InitStructure.NART = ENABLE;
  643. CAN_InitStructure.RFLM = ENABLE;
  644. CAN_InitStructure.TTCM = DISABLE;
  645. CAN_InitStructure.TXFP = ENABLE;
  646. CAN_InitStructure.TBS1 = CAN_TBS1_3tq;
  647. CAN_InitStructure.TBS2 = CAN_TBS2_2tq;
  648. CAN_InitStructure.RSJW = CAN_RSJW_1tq;
  649. CAN_InitStructure.BaudRatePrescaler = canBitrate;
  650. CAN_Init( CAN1, &CAN_InitStructure );
  651. CAN_FilterInitStructure.Filter_FIFOAssignment = CAN_Filter_FIFO0;
  652. CAN_FilterInitStructure.Filter_Mode = CAN_Filter_IdMaskMode;
  653. CAN_FilterInitStructure.Filter_Num = 0;
  654. CAN_FilterInitStructure.Filter_Scale = CAN_Filter_32bitScale;
  655. CAN_FilterInitStructure.FilterMask_HighId = 0X0000;
  656. CAN_FilterInitStructure.FilterMask_LowId = 0X0000;
  657. CAN_FilterInitStructure.Filter_HighId = 0X0000;
  658. CAN_FilterInitStructure.Filter_LowId = 0X0000;
  659. CAN_FilterInitStructure.Filter_Act = ENABLE;
  660. CAN1_InitFilter( &CAN_FilterInitStructure );
  661. CAN_INTConfig( CAN1, CAN_INT_FMP0, ENABLE );
  662. NVIC_InitType NVIC_InitStructure;
  663. NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  664. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  665. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  666. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  667. NVIC_Init( &NVIC_InitStructure );
  668. canardInit( &canard,
  669. canard_memory_pool,
  670. sizeof( canard_memory_pool ),
  671. DroneCAN_OnReception,
  672. DroneCAN_ShouldAccept,
  673. RT_NULL );
  674. read_uid( uid );
  675. memcpy( dronecan_unique_id, uid, sizeof( uid ) );
  676. dronecan_unique_id[12] = 'V';
  677. dronecan_unique_id[13] = 'K';
  678. dronecan_unique_id[14] = '4';
  679. dronecan_unique_id[15] = 'D';
  680. dronecan_ready = 1;
  681. }
  682. void DroneCAN_Start( void )
  683. {
  684. rt_err_t result;
  685. if ( dronecan_rx_started != 0 || dronecan_ready == 0 )
  686. {
  687. return;
  688. }
  689. rt_sem_init( &dronecan_rx_sem, "dc_rx", 0, RT_IPC_FLAG_FIFO );
  690. if ( rt_mutex_init( &dronecan_tx_lock, "dc_tx", RT_IPC_FLAG_PRIO ) != RT_EOK )
  691. {
  692. return;
  693. }
  694. dronecan_tx_lock_ready = 1;
  695. result = rt_thread_init( &dronecan_rx_thread,
  696. "dronecan",
  697. DroneCAN_RxThreadEntry,
  698. RT_NULL,
  699. dronecan_rx_stack,
  700. sizeof( dronecan_rx_stack ),
  701. DRONECAN_RX_THREAD_PRIORITY,
  702. DRONECAN_RX_THREAD_TICK );
  703. if ( result != RT_EOK )
  704. {
  705. return;
  706. }
  707. dronecan_rx_head = 0;
  708. dronecan_rx_tail = 0;
  709. dronecan_rx_count = 0;
  710. dronecan_rx_overflow = 0;
  711. dronecan_rx_started = 1;
  712. DroneCAN_DNA_Reset( rt_tick_get() );
  713. rt_thread_startup( &dronecan_rx_thread );
  714. }
  715. static void DroneCAN_FlushTxQueue( void )
  716. {
  717. CanardCANFrame *txf = RT_NULL;
  718. while ( ( txf = canardPeekTxQueue( &canard ) ) != RT_NULL )
  719. {
  720. MyCAN_Transmit( txf->id & CANARD_CAN_EXT_ID_MASK, txf->data_len, txf->data, 0 );
  721. canardPopTxQueue( &canard );
  722. }
  723. }
  724. void DroneCAN_SendRangeSensorMeasurement( struct uavcan_equipment_range_sensor_Measurement *msg )
  725. {
  726. uint8_t buffer[UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_MAX_SIZE];
  727. uint32_t len;
  728. if ( DroneCAN_IsNodeIDAllocated() == false || msg == RT_NULL || DroneCAN_TxLock() == false )
  729. {
  730. return;
  731. }
  732. #if CANARD_ENABLE_TAO_OPTION
  733. len = uavcan_equipment_range_sensor_Measurement_encode( msg, buffer, true );
  734. #else
  735. len = uavcan_equipment_range_sensor_Measurement_encode( msg, buffer );
  736. #endif
  737. if ( canardBroadcast( &canard,
  738. UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_SIGNATURE,
  739. UAVCAN_EQUIPMENT_RANGE_SENSOR_MEASUREMENT_ID,
  740. &transfer_id_range_sensor_measurement,
  741. CANARD_TRANSFER_PRIORITY_LOW,
  742. buffer,
  743. ( uint16_t )len ) > 0 )
  744. {
  745. DroneCAN_FlushTxQueue();
  746. }
  747. DroneCAN_TxUnlock();
  748. }
  749. void DroneCAN_SendNodeStatus(struct uavcan_protocol_NodeStatus *msg)
  750. {
  751. uint8_t buffer[UAVCAN_PROTOCOL_NODESTATUS_MAX_SIZE] = {0};
  752. uint32_t len;
  753. if ( DroneCAN_IsNodeIDAllocated() == false || msg == RT_NULL || DroneCAN_TxLock() == false )
  754. {
  755. return;
  756. }
  757. len = uavcan_protocol_NodeStatus_encode(msg, buffer);
  758. if (canardBroadcast(&canard,
  759. UAVCAN_PROTOCOL_NODESTATUS_SIGNATURE,
  760. UAVCAN_PROTOCOL_NODESTATUS_ID,
  761. &transfer_id_node_status,
  762. CANARD_TRANSFER_PRIORITY_LOW,
  763. buffer,
  764. len) > 0)
  765. {
  766. DroneCAN_FlushTxQueue();
  767. }
  768. DroneCAN_TxUnlock();
  769. }
  770. void myflash_erasepage( uint32_t addr ) //擦除
  771. {
  772. uint8_t retryCount = 0;
  773. FLASH_STS sts;
  774. rt_base_t level1;
  775. level1 = rt_hw_interrupt_disable();
  776. FLASH_Unlock();
  777. while ( 1 )
  778. {
  779. sts = FLASH_EraseOnePage( addr );
  780. if ( sts == FLASH_COMPL )
  781. {
  782. break;
  783. }
  784. else
  785. {
  786. if ( retryCount++ > 3 )
  787. {
  788. FLASH_Lock();
  789. rt_hw_interrupt_enable( level1 );
  790. return;
  791. }
  792. }
  793. }
  794. FLASH_Lock();
  795. rt_hw_interrupt_enable( level1 );
  796. return;
  797. }
  798. IAP_INFO iap_def =
  799. {
  800. .ValidFlag = IAP_VFLAG,
  801. .UpgradeFlag = IAP_FLAG
  802. };
  803. void FLASH_LOG( void )
  804. {
  805. myflash_erasepage( IAP_FLAG );
  806. rt_base_t level1;
  807. level1 = rt_hw_interrupt_disable();
  808. FLASH_Unlock();
  809. flash_write( IAP_FLAG_ADDR, ( uint8_t * )&iap_def, sizeof( iap_def ) );
  810. FLASH_Lock();
  811. rt_hw_interrupt_enable( level1 );
  812. }
  813. FlagUnion ReceiveFlag = {0};
  814. void USB_LP_CAN1_RX0_IRQHandler( void )
  815. {
  816. CanRxMessage RxMessage;
  817. rt_interrupt_enter();
  818. if ( CAN_GetIntStatus( CAN1, CAN_INT_FMP0 ) != RESET )
  819. {
  820. CAN_ReceiveMessage( CAN1, CAN_FIFO0, &RxMessage );
  821. if ( RxMessage.IDE == CAN_Extended_Id && RxMessage.RTR == CAN_RTRQ_Data )
  822. {
  823. if ( DroneCAN_IsRelevantFrame( &RxMessage ) == true )
  824. {
  825. DroneCAN_RxQueuePushFromIsr( &RxMessage );
  826. }
  827. // if (( ( uint16_t )( ( RxMessage.ExtId >> 8U ) & 0xFFFFU ) ==
  828. // UAVCAN_NAVIGATION_GLOBALNAVIGATIONSOLUTION_ID ))
  829. // {
  830. // DroneCAN_RxQueuePushFromIsr( &RxMessage );
  831. // }
  832. switch ( RxMessage.ExtId )
  833. {
  834. case 0x00002345: // q0 q1
  835. memcpy( &q_now[0], &RxMessage.Data[0], 4 );
  836. memcpy( &q_now[1], &RxMessage.Data[4], 4 );
  837. break;
  838. case 0x00002346: // q2 q3
  839. memcpy( &q_now[2], &RxMessage.Data[0], 4 );
  840. memcpy( &q_now[3], &RxMessage.Data[4], 4 );
  841. break;
  842. case 0x00002347: // p0 p1
  843. memcpy( &p_now[0], &RxMessage.Data[0], 4 );
  844. memcpy( &p_now[1], &RxMessage.Data[4], 4 );
  845. break;
  846. case 0x00002348: // p2 p3
  847. memcpy( &p_now[2], &RxMessage.Data[0], 4 );
  848. memcpy( &other[0], &RxMessage.Data[4], 4 );
  849. break;
  850. case 0xA01309: //行业协议
  851. ReceiveFlag.u8flag = RxMessage.Data[7];
  852. if(!can_info_frame.busy){
  853. if(ReceiveFlag.flags.seq != 1)
  854. break;
  855. can_info_frame.busy = 1;
  856. can_info_frame.expected_sequence = 1;
  857. can_info_frame.rx_count = 0;
  858. }
  859. if (ReceiveFlag.flags.seq != can_info_frame.expected_sequence){
  860. can_info_frame.busy = 0;
  861. can_info_frame.expected_sequence = 1;
  862. can_info_frame.rx_count = 0;
  863. break;
  864. }
  865. memcpy (&can_info_frame.buf[can_info_frame.rx_count * (FRAME_DATA_LEN - 1)]
  866. , &RxMessage.Data[0]
  867. , FRAME_DATA_LEN - 1);
  868. can_info_frame.expected_sequence++;
  869. can_info_frame.rx_count++;
  870. if(can_info_frame.rx_count >= FRAME_NUM)
  871. {
  872. can_info_frame.busy = 0;
  873. can_info_frame.rx_count = 0;
  874. memcpy(q_now, &can_info_frame.buf[0], 16);
  875. memcpy(p_now, &can_info_frame.buf[16], 12);
  876. memcpy(other, &can_info_frame.buf[28], 4);
  877. }
  878. break;
  879. case 0x381400:
  880. if ( confInfo.DeviceType == FRONT_OBSTACLE_RADAR )
  881. {
  882. if ( RxMessage.Data[5] == 0x44 && RxMessage.Data[6] == 0x34 && RxMessage.Data[7] == 0x46 )
  883. {
  884. FLASH_LOG();
  885. __set_PRIMASK( 1 );
  886. NVIC_SystemReset();
  887. }
  888. }
  889. if ( confInfo.DeviceType == REAR_OBSTACLE_RADAR )
  890. {
  891. if ( RxMessage.Data[5] == 0x44 && RxMessage.Data[6] == 0x34 && RxMessage.Data[7] == 0x42 )
  892. {
  893. FLASH_LOG();
  894. __set_PRIMASK( 1 );
  895. NVIC_SystemReset();
  896. }
  897. }
  898. if ( confInfo.DeviceType == LEFT_OBSTACLE_RADAR )
  899. {
  900. if ( RxMessage.Data[5] == 0x44 && RxMessage.Data[6] == 0x34 && RxMessage.Data[7] == 0x4C )
  901. {
  902. FLASH_LOG();
  903. __set_PRIMASK( 1 );
  904. NVIC_SystemReset();
  905. }
  906. }
  907. if ( confInfo.DeviceType == RIGHT_OBSTACLE_RADAR )
  908. {
  909. if ( RxMessage.Data[5] == 0x44 && RxMessage.Data[6] == 0x34 && RxMessage.Data[7] == 0x52 )
  910. {
  911. FLASH_LOG();
  912. __set_PRIMASK( 1 );
  913. NVIC_SystemReset();
  914. }
  915. }
  916. case 0xA81300:
  917. memcpy( &ParameterData[0], &RxMessage.Data[0], 8 );
  918. memcpy( &RceveiveCanID, &RxMessage.ExtId, sizeof(RxMessage.ExtId) );
  919. rt_sem_release(parameter_sem_t);
  920. break;
  921. case 0xB81300:
  922. memcpy( &ParameterData[0], &RxMessage.Data[0], 8 );
  923. memcpy( &RceveiveCanID, &RxMessage.ExtId, sizeof(RxMessage.ExtId) );
  924. rt_sem_release(parameter_sem_t);
  925. break;
  926. case 0xC81300:
  927. memcpy( &ParameterData[0], &RxMessage.Data[0], 8 );
  928. memcpy( &RceveiveCanID, &RxMessage.ExtId, sizeof(RxMessage.ExtId) );
  929. rt_sem_release(parameter_sem_t);
  930. break;
  931. case 0xD81300:
  932. memcpy( &ParameterData[0], &RxMessage.Data[0], 8 );
  933. memcpy( &RceveiveCanID, &RxMessage.ExtId, sizeof(RxMessage.ExtId) );
  934. rt_sem_release(parameter_sem_t);
  935. break;
  936. default:
  937. break;
  938. }
  939. }
  940. }
  941. rt_interrupt_leave();
  942. }
  943. void MyCAN_Transmit( uint32_t ID, uint8_t Length, uint8_t *Data, uint8_t sendbit )
  944. {
  945. CanTxMessage TxMessage;
  946. TxMessage.DLC = Length;
  947. TxMessage.ExtId = ID;
  948. TxMessage.IDE = CAN_Extended_Id;
  949. TxMessage.RTR = CAN_RTRQ_Data;
  950. TxMessage.StdId = ID;
  951. for ( uint8_t i = 0; i < Length; i++ )
  952. {
  953. TxMessage.Data[i] = Data[i];
  954. }
  955. uint8_t TransmitMailbox = CAN_TransmitMessage( CAN1, &TxMessage );
  956. uint32_t i = 0XFFF;
  957. while ( CAN_TxSTS_Ok != CAN_TransmitSTS( CAN1, TransmitMailbox ) && i > 0 )
  958. {
  959. uint8_t status = CAN_GetLastErrCode( CAN1 );
  960. i--;
  961. }
  962. }
  963. void MyCAN_Transmitbeg( uint32_t ID, uint8_t Length, uint8_t *Data )
  964. {
  965. // uint8_t a =0,b=0;
  966. CanTxMessage TxMessagebeg;
  967. TxMessagebeg.DLC = Length;
  968. TxMessagebeg.ExtId = ID;
  969. TxMessagebeg.IDE = CAN_Extended_Id;
  970. TxMessagebeg.RTR = CAN_RTRQ_Data;
  971. TxMessagebeg.StdId = ID;
  972. for ( uint8_t i = 0; i < Length; i++ )
  973. {
  974. TxMessagebeg.Data[i] = Data[i];
  975. }
  976. uint8_t TransmitMailbox = CAN_TransmitMessage( CAN1, &TxMessagebeg );
  977. // uint32_t send_time = 0;
  978. uint32_t i = 0XFFF;
  979. while ( CAN_TxSTS_Ok != CAN_TransmitSTS( CAN1, TransmitMailbox ) && i > 0 )
  980. {
  981. i--;
  982. }
  983. // while (CAN_TransmitSTS(CAN1, CAN_TransmitMessage(CAN1, &TxMessage)) != CAN_TxSTS_Ok);
  984. }
  985. void MyCAN_Transmitend( uint32_t ID, uint8_t Length, uint8_t *Data, uint8_t sendbit )
  986. {
  987. uint8_t z;
  988. CanTxMessage TxMessageend;
  989. TxMessageend.DLC = 8;
  990. TxMessageend.ExtId = ID;
  991. TxMessageend.IDE = CAN_Extended_Id;
  992. TxMessageend.RTR = CAN_RTRQ_Data;
  993. TxMessageend.StdId = ID;
  994. for ( z = 0; z < Length; z++ )
  995. {
  996. TxMessageend.Data[z] = Data[z];
  997. }
  998. for ( z = Length; z < 7; z++ )
  999. {
  1000. TxMessageend.Data[z] = 0x00;
  1001. }
  1002. TxMessageend.Data[z] = sendbit;
  1003. uint8_t TransmitMailbox = CAN_TransmitMessage( CAN1, &TxMessageend );
  1004. uint32_t i = 0XFFF;
  1005. while ( CAN_TxSTS_Ok != CAN_TransmitSTS( CAN1, TransmitMailbox ) && i > 0 )
  1006. {
  1007. i--;
  1008. }
  1009. }
  1010. uint16_t Get_Crc16( uint8_t *puchMsg, uint16_t usDataLen )
  1011. {
  1012. uint8_t uchCRCHi = 0xFF; // ��CRC �ֽڳ�ʼ��
  1013. uint8_t uchCRCLo = 0xFF; // ��CRC �ֽڳ�ʼ��
  1014. uint32_t uIndex; // CRC ѭ���е�����
  1015. while ( usDataLen-- ) // ������Ϣ������
  1016. {
  1017. uIndex = uchCRCLo ^ *puchMsg++; // ����CRC
  1018. uchCRCLo = uchCRCHi ^ auchCRCHi[uIndex];
  1019. uchCRCHi = auchCRCLo[uIndex];
  1020. }
  1021. return ( uchCRCHi << 8 | uchCRCLo );
  1022. }
  1023. uint8_t MyCAN_ReceiveFlag( void )
  1024. {
  1025. if ( CAN_PendingMessage( CAN1, CAN_FIFO0 ) > 0 )
  1026. {
  1027. return 1;
  1028. }
  1029. return 0;
  1030. }