You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
12 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "zcanreceiver.hpp"
  2. #ifdef HAL_CAN_MODULE_ENABLED
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. using namespace iflytop;
  7. using namespace zcr;
  8. #define TAG "ZCanCmder"
  9. #define OVER_TIME_MS 5
  10. ZCanCmder::CFG *ZCanCmder::createCFG(uint8_t deviceId) {
  11. CFG *cfg = new CFG();
  12. ZASSERT(cfg != NULL);
  13. cfg->deviceId = deviceId;
  14. #ifdef STM32F103xB
  15. cfg->canHandle = &hcan;
  16. #else
  17. cfg->canHandle = &hcan1;
  18. #endif
  19. cfg->canFilterIndex0 = 0;
  20. cfg->maxFilterNum = 7;
  21. cfg->rxfifoNum = CAN_RX_FIFO0;
  22. return cfg;
  23. }
  24. void ZCanCmder::init(CFG *cfg) {
  25. HAL_StatusTypeDef hal_status;
  26. m_config = cfg;
  27. m_lock.init();
  28. /**
  29. * @brief ʼCAN
  30. */
  31. /**
  32. * @brief ʼϢbuf
  33. */
  34. m_canPacketRxBuffer[0].dataIsReady = false;
  35. m_canPacketRxBuffer[0].id = 0; // ֻ����������������Ϣ
  36. m_canPacketRxBuffer[0].m_canPacketNum = 0;
  37. /**
  38. * @brief ʼ
  39. */
  40. hal_status = initializeFilter();
  41. if (hal_status != HAL_OK) {
  42. ZLOGE(TAG, "start can initializeFilter fail\r\n");
  43. return;
  44. }
  45. /**
  46. * @brief CAN
  47. */
  48. hal_status = HAL_CAN_Start(m_config->canHandle); // ����CAN
  49. if (hal_status != HAL_OK) {
  50. ZLOGE(TAG, "start can fail\r\n");
  51. return;
  52. }
  53. /**
  54. * @brief ص
  55. */
  56. ZCanIRQDispatcher::instance().regListener(this);
  57. HAL_StatusTypeDef status = activateRxIT();
  58. if (status != HAL_OK) {
  59. ZLOGE(TAG, "activateRxIT fail\r\n");
  60. return;
  61. }
  62. // ZHALCORE::getInstance()->regPeriodJob([this](ZHALCORE::Context &context) { loop(); }, 0);
  63. }
  64. HAL_StatusTypeDef ZCanCmder::initializeFilter() {
  65. /**
  66. * @brief ID֡ʽ
  67. * [ 27:0 ]
  68. * [ STDID ] [ EXTID ]
  69. * [11 :9] [8:6] [5:0] [17:16] [15:8] [7:0]
  70. * ȼ ֡ ĿID ԴID
  71. */
  72. HAL_StatusTypeDef HAL_Status;
  73. CAN_FilterTypeDef sFilterConfig;
  74. uint32_t filterId;
  75. uint32_t mask;
  76. memset(&sFilterConfig, 0, sizeof(sFilterConfig));
  77. sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK; // ��ΪMASKģʽ
  78. sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT; // CAN_FILTERSCALE_16BIT
  79. sFilterConfig.FilterFIFOAssignment = m_config->rxfifoNum; // ������������rxfifoNum
  80. sFilterConfig.FilterActivation = ENABLE; // ����������
  81. sFilterConfig.SlaveStartFilterBank = m_config->maxFilterNum; // slave filter start index
  82. /*******************************************************************************
  83. * Ϣ *
  84. *******************************************************************************/
  85. filterId = (0); //
  86. mask = (0); //
  87. sFilterConfig.FilterBank = m_config->canFilterIndex0; //
  88. sFilterConfig.FilterMaskIdLow = mask & 0xffff; //
  89. sFilterConfig.FilterMaskIdHigh = (mask & 0xffff0000) >> 16; //
  90. sFilterConfig.FilterIdLow = filterId & 0xffff; //
  91. sFilterConfig.FilterIdHigh = (filterId & 0xffff0000) >> 16; //
  92. HAL_Status = HAL_CAN_ConfigFilter(m_config->canHandle, &sFilterConfig);
  93. if (HAL_Status != HAL_OK) {
  94. ZLOGE(TAG, "HAL_CAN_ConfigFilter filter0 fail");
  95. return HAL_Status;
  96. }
  97. ZLOGI(TAG, "HAL_CAN_ConfigFilter filterID1 %08x", filterId >> 3);
  98. return HAL_Status;
  99. }
  100. void ZCanCmder::registerListener(IZcanCmderListener *listener) { m_listenerList.push_back(listener); }
  101. void ZCanCmder::sendPacket(uint8_t *packet, size_t len) {
  102. /**
  103. * @brief
  104. */
  105. int npacket = len / 8 + (len % 8 == 0 ? 0 : 1);
  106. if (npacket > 255) {
  107. ZLOGE(TAG, "sendPacket fail, len:%d", len);
  108. return;
  109. }
  110. int finalpacketlen = len % 8 == 0 ? 8 : len % 8;
  111. for (uint8_t i = 0; i < npacket; i++) {
  112. bool suc = false;
  113. if (i == npacket - 1) {
  114. suc = sendPacketSub(npacket, i, packet + i * 8, finalpacketlen, OVER_TIME_MS);
  115. } else {
  116. suc = sendPacketSub(npacket, i, packet + i * 8, 8, OVER_TIME_MS);
  117. }
  118. if (!suc) {
  119. ZLOGE(TAG, "sendPacket fail, packet(%d:%d)", npacket, i);
  120. return;
  121. }
  122. }
  123. }
  124. #if 0
  125. void ZCanCmder::sendAck(zcr_cmd_header_t *cmdheader, uint8_t *data, size_t len) {
  126. zlock_guard l(m_lock);
  127. zcr_cmd_header_t *txheader = (zcr_cmd_header_t *)txbuff;
  128. memcpy(txheader, cmdheader, sizeof(zcr_cmd_header_t));
  129. txheader->packetType = kptv2_ack;
  130. memcpy(txheader->data, data, len);
  131. sendPacket(txbuff, sizeof(zcr_cmd_header_t) + len);
  132. }
  133. void ZCanCmder::sendErrorAck(zcr_cmd_header_t *cmdheader, uint16_t id, uint32_t errcode) {
  134. zlock_guard l(m_lock);
  135. zcr_cmd_header_t *txheader = (zcr_cmd_header_t *)txbuff;
  136. memcpy(txheader, cmdheader, sizeof(zcr_cmd_header_t));
  137. txheader->packetType = kptv2_error_ack;
  138. zcan_cmder_error_ack_t *error_ack = (zcan_cmder_error_ack_t *)txheader->data;
  139. error_ack->id = id;
  140. error_ack->errorcode = errcode;
  141. sendPacket(txbuff, sizeof(zcr_cmd_header_t) + sizeof(zcan_cmder_error_ack_t));
  142. }
  143. void ZCanCmder::sendExecStatusReport(zcr_cmd_header_t *rxcmdheader, uint8_t *data, size_t len) {
  144. zlock_guard l(m_lock);
  145. zcr_cmd_header_t *txheader = (zcr_cmd_header_t *)txbuff;
  146. memcpy(txheader, rxcmdheader, sizeof(zcr_cmd_header_t));
  147. txheader->packetType = kptv2_cmd_exec_status_report;
  148. memcpy(txheader->data, data, len);
  149. sendPacket(txbuff, sizeof(zcr_cmd_header_t) + len);
  150. }
  151. void ZCanCmder::sendStatusReport(zcr_cmd_header_t *rxcmdheader, uint8_t *data, size_t len) {
  152. zlock_guard l(m_lock);
  153. zcr_cmd_header_t *txheader = (zcr_cmd_header_t *)txbuff;
  154. memcpy(txheader, rxcmdheader, sizeof(zcr_cmd_header_t));
  155. txheader->packetType = kptv2_report;
  156. memcpy(txheader->data, data, len);
  157. sendPacket(txbuff, sizeof(zcr_cmd_header_t) + len);
  158. }
  159. #endif
  160. int32_t ZCanCmder::sendBufAck(zcr_cmd_header_t *rx_cmd_header, uint8_t *data, int32_t len) {
  161. zlock_guard l(m_lock);
  162. ZASSERT(sizeof(txbuff) > sizeof(zcr_cmd_header_t) + len);
  163. zcr_cmd_header_t *txheader = (zcr_cmd_header_t *)txbuff;
  164. memcpy(txheader, rx_cmd_header, sizeof(zcr_cmd_header_t));
  165. txheader->packetType = kptv2_ack;
  166. memcpy(txheader->data, data, len);
  167. sendPacket(txbuff, sizeof(zcr_cmd_header_t) + len);
  168. return 0;
  169. }
  170. int32_t ZCanCmder::triggerEvent(zcr_cmd_header_t *cmd_header, uint8_t *data, int32_t len) {
  171. zlock_guard l(m_lock);
  172. m_reportIndex++;
  173. ZASSERT(sizeof(txbuff) > sizeof(zcr_cmd_header_t) + len);
  174. zcr_cmd_header_t *txheader = (zcr_cmd_header_t *)txbuff;
  175. memcpy(txheader, cmd_header, sizeof(zcr_cmd_header_t));
  176. txheader->packetType = kptv2_event;
  177. txheader->packetindex = m_reportIndex;
  178. memcpy(txheader->data, data, len);
  179. sendPacket(txbuff, sizeof(zcr_cmd_header_t) + len);
  180. return 0;
  181. }
  182. int32_t ZCanCmder::sendAck(zcr_cmd_header_t *rx_cmd_header, int32_t *ackvar, int32_t nack) {
  183. zlock_guard l(m_lock);
  184. zcr_cmd_header_t *txheader = (zcr_cmd_header_t *)txbuff;
  185. memcpy(txheader, rx_cmd_header, sizeof(zcr_cmd_header_t));
  186. txheader->packetType = kptv2_ack;
  187. int32_t *txackcache = (int32_t *)txheader->data;
  188. for (int i = 0; i < nack; i++) {
  189. txackcache[i] = ackvar[i];
  190. }
  191. sendPacket(txbuff, sizeof(zcr_cmd_header_t) + sizeof(int32_t) * nack);
  192. return 0;
  193. }
  194. int32_t ZCanCmder::sendErrorAck(zcr_cmd_header_t *rx_cmd_header, int32_t errorcode) {
  195. zlock_guard l(m_lock);
  196. zcr_cmd_header_t *txheader = (zcr_cmd_header_t *)txbuff;
  197. memcpy(txheader, rx_cmd_header, sizeof(zcr_cmd_header_t));
  198. txheader->packetType = kptv2_error_ack;
  199. int32_t *txackcache = (int32_t *)txheader->data;
  200. txackcache[0] = errorcode;
  201. sendPacket(txbuff, sizeof(zcr_cmd_header_t) + sizeof(int32_t));
  202. return 0;
  203. }
  204. bool ZCanCmder::sendPacketSub(int npacket, int packetIndex, uint8_t *packet, size_t len, int overtimems) {
  205. zlock_guard l(m_lock);
  206. // ZLOGI(TAG, "sendPacketSub(%d:%d)", npacket, packetIndex);
  207. CAN_TxHeaderTypeDef pHeader;
  208. uint8_t aData[8] /*8byte table*/;
  209. uint32_t txMailBox = 0;
  210. uint32_t enterticket = zos_get_tick();
  211. memset(&pHeader, 0, sizeof(pHeader));
  212. memset(aData, 0, sizeof(aData));
  213. pHeader.StdId = 0x00;
  214. pHeader.ExtId = (m_config->deviceId << 16) | (npacket << 8) | packetIndex;
  215. pHeader.IDE = CAN_ID_EXT;
  216. pHeader.RTR = CAN_RTR_DATA;
  217. pHeader.DLC = len;
  218. pHeader.TransmitGlobalTime = DISABLE;
  219. memcpy(aData, packet, len);
  220. m_lastTransmitStatus = HAL_CAN_AddTxMessage(m_config->canHandle, &pHeader, aData, &txMailBox);
  221. if (m_lastTransmitStatus != HAL_OK) {
  222. ZLOGE(TAG, "HAL_CAN_AddTxMessage fail");
  223. return false;
  224. }
  225. while (HAL_CAN_IsTxMessagePending(m_config->canHandle, txMailBox)) {
  226. if (zos_haspassedms(enterticket) > (uint32_t)overtimems) {
  227. m_lastTransmitStatus = HAL_TIMEOUT;
  228. HAL_CAN_AbortTxRequest(m_config->canHandle, txMailBox);
  229. return false;
  230. }
  231. // m_os->sleepMS(1);
  232. }
  233. if (txPacketInterval_ms > 0) {
  234. osDelay(txPacketInterval_ms);
  235. }
  236. return true;
  237. }
  238. bool ZCanCmder::getRxMessage(CAN_RxHeaderTypeDef *pHeader, uint8_t aData[] /*8byte table*/) {
  239. /**
  240. * @brief ȡǰFIFOл˶֡
  241. */
  242. uint32_t level = HAL_CAN_GetRxFifoFillLevel(m_config->canHandle, m_config->rxfifoNum);
  243. if (level == 0) {
  244. return false;
  245. }
  246. HAL_StatusTypeDef HAL_RetVal;
  247. HAL_RetVal = HAL_CAN_GetRxMessage(m_config->canHandle, m_config->rxfifoNum, pHeader, aData);
  248. if (HAL_OK == HAL_RetVal) {
  249. // �������յ���can��������
  250. return true;
  251. }
  252. return false;
  253. }
  254. void ZCanCmder::STM32_HAL_onCAN_RxFifo0MsgPending(CAN_HandleTypeDef *canHandle) {
  255. /**
  256. * @brief ж
  257. */
  258. // ZLOG_INFO("%s\n", __FUNCTION__);
  259. // printf("------------------%s\n", __FUNCTION__);
  260. if (canHandle != m_config->canHandle) {
  261. return;
  262. }
  263. /**
  264. * @brief canյϢ
  265. */
  266. CAN_RxHeaderTypeDef pHeader;
  267. uint8_t aData[8] /*8byte table*/;
  268. while (getRxMessage(&pHeader, aData)) {
  269. /**
  270. * @brief Ϣʽ
  271. *
  272. * [2] [3bit] [8bit] [8bit] [8bit]
  273. * , from frameNum frameId
  274. */
  275. uint8_t from = (pHeader.ExtId >> 16 & 0xFF);
  276. uint8_t nframe = (pHeader.ExtId & 0xFF00) >> 8;
  277. uint8_t frameId = (pHeader.ExtId & 0x00FF);
  278. CanPacketRxBuffer *rxbuf = &m_canPacketRxBuffer[0];
  279. if (from != rxbuf->id) {
  280. // Ŀǰֻ����������������Ϣ
  281. continue;
  282. }
  283. if (rxbuf->dataIsReady) {
  284. // �ϴν��յ�����Ϣ��û�����ļ�����
  285. continue;
  286. }
  287. /**
  288. * @TODO:жǷ񶪰
  289. */
  290. if (frameId == 0) {
  291. rxbuf->m_canPacketNum = 0;
  292. }
  293. if (rxbuf->m_canPacketNum < 255) {
  294. rxbuf->m_canPacket[rxbuf->m_canPacketNum].dlc = pHeader.DLC;
  295. memcpy(rxbuf->m_canPacket[rxbuf->m_canPacketNum].aData, aData, 8);
  296. rxbuf->m_canPacketNum++;
  297. }
  298. if (nframe == frameId + 1) {
  299. rxbuf->dataIsReady = true;
  300. }
  301. }
  302. // deactivateRxIT();
  303. }
  304. void ZCanCmder::STM32_HAL_onCAN_Error(CAN_HandleTypeDef *canHandle) {
  305. if (canHandle != m_config->canHandle) {
  306. return;
  307. }
  308. ZLOGE(TAG, "onCAN_Error\r\n");
  309. }
  310. void ZCanCmder::loop() {
  311. CanPacketRxBuffer *rxbuf = &m_canPacketRxBuffer[0];
  312. if (rxbuf->dataIsReady) {
  313. int dataoff = 0;
  314. for (size_t i = 0; i < rxbuf->m_canPacketNum; i++) {
  315. memcpy(rxbuf->rxdata + dataoff, rxbuf->m_canPacket[i].aData, rxbuf->m_canPacket[i].dlc);
  316. dataoff += rxbuf->m_canPacket[i].dlc;
  317. }
  318. rxbuf->rxdataSize = dataoff;
  319. for (auto &var : m_listenerList) {
  320. if (var) var->onRceivePacket(rxbuf->get_cmdheader(), rxbuf->get_data(), rxbuf->get_datalen());
  321. }
  322. rxbuf->dataIsReady = false;
  323. }
  324. }
  325. HAL_StatusTypeDef ZCanCmder::activateRxIT() {
  326. HAL_StatusTypeDef hal_status = HAL_ERROR;
  327. if (m_config->rxfifoNum == CAN_RX_FIFO0) {
  328. hal_status = HAL_CAN_ActivateNotification(m_config->canHandle, CAN_IT_RX_FIFO0_MSG_PENDING);
  329. } else if (m_config->rxfifoNum == CAN_RX_FIFO1) {
  330. hal_status = HAL_CAN_ActivateNotification(m_config->canHandle, CAN_IT_RX_FIFO1_MSG_PENDING);
  331. } else {
  332. ZLOGE(TAG, "start can HAL_CAN_ActivateNotification CAN_IT_RX_FIFO0_MSG_PENDING fail\r\n");
  333. return hal_status;
  334. }
  335. return hal_status;
  336. }
  337. HAL_StatusTypeDef ZCanCmder::deactivateRxIT() {
  338. HAL_StatusTypeDef hal_status = HAL_ERROR;
  339. if (m_config->rxfifoNum == CAN_RX_FIFO0) {
  340. hal_status = HAL_CAN_DeactivateNotification(m_config->canHandle, CAN_IT_RX_FIFO0_MSG_PENDING);
  341. } else if (m_config->rxfifoNum == CAN_RX_FIFO1) {
  342. hal_status = HAL_CAN_DeactivateNotification(m_config->canHandle, CAN_IT_RX_FIFO1_MSG_PENDING);
  343. } else {
  344. ZLOGE(TAG, "start can HAL_CAN_ActivateNotification CAN_IT_RX_FIFO0_MSG_PENDING fail\r\n");
  345. return hal_status;
  346. }
  347. return hal_status;
  348. }
  349. #endif