i15765.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. #include "MCU_CAN.h"
  2. #include "i15765.h"
  3. #include "bits.h"
  4. /* FC types */ //Flow Control
  5. #define I15765_FS_CTS 0
  6. #define I15765_FS_WT 1
  7. #define I15765_FS_OVFLW 2
  8. /* PDU types */
  9. #define I15765_PDU_SF 0 //Single Frame
  10. #define I15765_PDU_FF 1 //First Frame
  11. #define I15765_PDU_CF 2 //Consecutive Frame
  12. #define I15765_PDU_FC 3 //Flow Control N_PCI
  13. /* source address */
  14. #define I15765CFG_SA 0xf1
  15. /* multi-frame TX buffer size */
  16. #define I15765CFG_MF_TX_BUF_NUM 1
  17. #define I15765CFG_MF_TX_BUF_SIZE 1024
  18. /* multi-frame RX buffer size */
  19. #define I15765CFG_MF_RX_BUF_NUM 1
  20. #define I15765CFG_MF_RX_BUF_SIZE 2048
  21. extern uint8_t can_rx(uint8_t p, can_t *frame);
  22. extern void CAN_SendData(uint8_t canCom, uint32_t mailbox, uint32_t messageId, uint8_t * data, uint32_t len);
  23. extern void i15765app_process(i15765_t *msg);
  24. /* misc */
  25. #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
  26. #define ADDR_FUNC(tat) (tat == I15765_TAT_NF11 || tat == I15765_TAT_NF29)
  27. enum
  28. {
  29. STATE_IDLE = 0,
  30. STATE_TX_FF,
  31. STATE_TX_FC,
  32. STATE_TX_CF,
  33. STATE_WT_FC,
  34. STATE_WT_CF,
  35. STATE_TX_CTS,
  36. STATE_TX_OVFLW
  37. };
  38. /* multiframe rx type */
  39. typedef struct
  40. {
  41. uint8_t sn; //Sequence Number
  42. i15765_t msg;
  43. uint8_t state;
  44. uint16_t timeout;
  45. uint8_t buf[I15765CFG_MF_RX_BUF_SIZE];
  46. } i15765_mfr_t;
  47. /* multiframe tx type */
  48. typedef struct
  49. {
  50. uint8_t bs;
  51. uint8_t bs_cnt;
  52. uint16_t st;
  53. uint16_t st_cnt;
  54. uint8_t sn;
  55. uint8_t wt_cnt;
  56. i15765_t msg;
  57. uint8_t state;
  58. uint8_t *status;
  59. uint16_t timeout;
  60. uint8_t buf[I15765CFG_MF_TX_BUF_SIZE];
  61. } i15765_mft_t;
  62. /* multiframe buffers */
  63. i15765_mfr_t i15765_mfrb[I15765CFG_MF_RX_BUF_NUM];
  64. i15765_mft_t i15765_mftb[I15765CFG_MF_TX_BUF_NUM];
  65. /* used instead of NULL pointers */
  66. uint8_t i15765_tmp;
  67. /* source address */
  68. uint8_t i15765_sa = I15765CFG_SA;
  69. /*
  70. ** Compare address information of two messages.
  71. **
  72. ** INPUT: msg1 - Pointer to first message
  73. ** msg2 - Pointer to second message
  74. **
  75. ** RETURN: 1: if the ai's match
  76. ** 0: otherwise
  77. */
  78. uint8_t i15765_ai_cmp(i15765_t *msg1, i15765_t *msg2)
  79. {
  80. if((msg1->sa != msg2->sa)
  81. || (msg1->ta != msg2->ta)
  82. || (msg1->tat != msg2->tat))
  83. return 0;
  84. return 1;
  85. }
  86. /*
  87. ** Initialize the receive and transmit buffers.
  88. */
  89. void i15765_init(void)
  90. {
  91. uint8_t i;
  92. /* clear the rx buffers */
  93. for(i = 0; i < I15765CFG_MF_RX_BUF_NUM; i++)
  94. i15765_mfrb[i].state = STATE_IDLE;
  95. /* clear the tx buffers */
  96. for(i = 0; i < I15765CFG_MF_TX_BUF_NUM; i++)
  97. {
  98. i15765_mftb[i].state = STATE_IDLE;
  99. i15765_mftb[i].status = 0;//&i15765_tmp;
  100. }
  101. // i15765app_init();
  102. }
  103. /*
  104. ** Sets the pointer to the first available index.
  105. **
  106. ** INPUT: mfrb - pointer to available index
  107. **
  108. ** RETURN: 0: success, index found
  109. ** 1: failure, buf is full
  110. */
  111. uint8_t i15765_mfrb_get(i15765_mfr_t **mfrb)
  112. {
  113. uint8_t i;
  114. /* find first available index */
  115. for(i = 0; i < I15765CFG_MF_RX_BUF_NUM; i++)
  116. {
  117. if(i15765_mfrb[i].state == STATE_IDLE)
  118. {
  119. *mfrb = &i15765_mfrb[i];
  120. return 0;
  121. }
  122. }
  123. return 1;
  124. }
  125. /*
  126. ** Sets the pointer to the first available index.
  127. **
  128. ** INPUT: mftb - pointer to available index
  129. **
  130. ** RETURN: 0: success, index found
  131. ** 1: failure, buf is full
  132. */
  133. uint8_t i15765_mftb_get(i15765_mft_t **mftb)
  134. {
  135. uint8_t i;
  136. /* find first available index */
  137. for(i = 0; i < I15765CFG_MF_TX_BUF_NUM; i++)
  138. {
  139. if(i15765_mftb[i].state == STATE_IDLE)
  140. {
  141. *mftb = &i15765_mftb[i];
  142. return 0;
  143. }
  144. }
  145. return 1;
  146. }
  147. /*
  148. ** Delete all receive mf buffers with matching AE info
  149. */
  150. void i15765_mfrb_del(i15765_t *msg)
  151. {
  152. uint8_t i;
  153. for(i = 0; i < I15765CFG_MF_RX_BUF_NUM; i++)
  154. {
  155. if(i15765_ai_cmp(&i15765_mfrb[i].msg, msg))
  156. {
  157. i15765_mfrb[i].state = STATE_IDLE;
  158. }
  159. }
  160. return;
  161. }
  162. /*
  163. ** Delete all transmit mf buffers with matching AE info
  164. */
  165. void i15765_mftb_del(i15765_t *msg)
  166. {
  167. uint8_t i;
  168. for(i = 0; i < I15765CFG_MF_TX_BUF_NUM; i++)
  169. {
  170. if(i15765_ai_cmp(&i15765_mftb[i].msg, msg)
  171. && (i15765_mftb[i].state != STATE_IDLE))
  172. {
  173. i15765_mftb[i].state = STATE_IDLE;
  174. *i15765_mftb[i].status = I15765_FAILED;
  175. i15765_mftb[i].status = &i15765_tmp;
  176. }
  177. }
  178. return;
  179. }
  180. /*
  181. ** Sets the ptr to point to the first index in buf whose
  182. ** address information matches the current message.
  183. **
  184. ** INPUT: msg - the address information to match
  185. ** ptr - double pointer to matching index
  186. **
  187. ** RETURN: 0 - match found
  188. ** 1 - no match
  189. */
  190. uint8_t i15765_mftb_seek(i15765_t *msg, i15765_mft_t **ptr)
  191. {
  192. uint8_t i;
  193. for(i = 0; i < I15765CFG_MF_TX_BUF_NUM; i++)
  194. {
  195. if(i15765_mftb[i].state != STATE_IDLE)
  196. {
  197. *ptr = &i15765_mftb[i];
  198. return 0;
  199. }
  200. }
  201. return 1;
  202. }
  203. /*
  204. ** Sets the ptr to point to the first index in buf whose
  205. ** address information matches the current message.
  206. **
  207. ** INPUT: msg - the address information to match
  208. ** ptr - double pointer to matching index
  209. **
  210. ** RETURN: 0 - match found
  211. ** 1 - no match
  212. */
  213. uint8_t i15765_mfrb_seek(i15765_t *msg, i15765_mfr_t **ptr)
  214. {
  215. uint8_t i;
  216. for(i = 0; i < I15765CFG_MF_RX_BUF_NUM; i++)
  217. {
  218. if(i15765_mfrb[i].state != STATE_IDLE)
  219. {
  220. *ptr = &i15765_mfrb[i];
  221. return 0;
  222. }
  223. }
  224. return 1;
  225. }
  226. /*
  227. ** Convert an i15765 message to a CAN frame and transmit it
  228. **
  229. ** INPUT: msg - the message to transmit
  230. **
  231. ** RETURN: 0 - success
  232. ** 1 - failure
  233. **
  234. */
  235. uint8_t i15765_tx(i15765_t *msg)
  236. {
  237. can_t can;
  238. uint8_t i = 0;
  239. // 11bit ��׼֡ ֱ�Ӹ�ֵID��
  240. /* physical response */
  241. can.id = UDS_TX_ID;
  242. // can.id = msg->ID;
  243. /* pack the data, garbage will be packed when we overflow so it is the
  244. receiver's responsibility to only read as much as was valid */
  245. for(i = 0; i < 8; i++)
  246. can.buf[i] = (i < msg->buf_len) ? msg->buf[i] : BUFFER_DATA_LAST;
  247. /* fix all packets to 8 bytes per 15765-4 */
  248. can.buf_len = 8;
  249. /* Send message */
  250. // return can_tx(0, &can);
  251. CAN_SendData(UDS_CAN_COM, UDS_TX_MAILBOX, can.id, can.buf, can.buf_len);
  252. return 0;
  253. }
  254. /*
  255. ** Initialize and attempt transmission of a single frame message.
  256. ** INPUT: frame - pointer to the i15765 frame to be transmitted
  257. ** RETURN: 0 - success
  258. ** 1 - failure
  259. */
  260. uint8_t i15765_tx_sf(i15765_t *msg)
  261. {
  262. uint8_t i;
  263. uint8_t cnt;
  264. i15765_t sf;
  265. uint8_t buf[8];
  266. /* copy over the old message */
  267. sf = *msg;
  268. /* start writing at the beginning */
  269. i = 0;
  270. /* construct SF N_PDU and pack it into a CAN frame */
  271. buf[i++] = (I15765_PDU_SF << 4) | (uint8_t)msg->buf_len;
  272. for(cnt = 0; cnt < msg->buf_len; cnt++)
  273. buf[i++] = msg->buf[cnt];
  274. sf.buf = buf;
  275. sf.buf_len = i;
  276. /* attempt to buffer the CAN frame */
  277. return i15765_tx(&sf);
  278. }
  279. /*
  280. ** Transmit a first frame.
  281. ** INPUT: frame - pointer to the i15765 multiframe buffer
  282. */
  283. void i15765_tx_ff(i15765_mft_t *mftb)
  284. {
  285. uint8_t i = 0;
  286. uint8_t *ptr;
  287. uint8_t buf[8];
  288. i15765_t frame;
  289. /* copy over message */
  290. frame = mftb->msg;
  291. frame.buf = buf;
  292. ptr = mftb->buf;
  293. /* construct FF N_PDU and pack it into a CAN frame */
  294. frame.buf[i++] = (I15765_PDU_FF << 4) | (mftb->msg.buf_len >> 8);
  295. frame.buf[i++] = (uint8_t)mftb->msg.buf_len;
  296. /* copy over the data */
  297. while(i < 8)
  298. frame.buf[i++] = *ptr++;
  299. /* FF's are always 8 bytes long */
  300. frame.buf_len = 8;
  301. /* transmit the CAN frame, testing to see if it was sent correctly */
  302. if(i15765_tx(&frame) == 0)
  303. {
  304. mftb->state = STATE_WT_FC;
  305. mftb->timeout = TIMEOUT_FC_S;
  306. mftb->msg.buf = ptr;
  307. }
  308. return;
  309. }
  310. /*
  311. ** Transmit a consecutive frame.
  312. ** INPUT: mftb - pointer to the i15765 multiframe buffer
  313. */
  314. void i15765_tx_cf(i15765_mft_t *mftb)
  315. {
  316. uint8_t min;
  317. uint8_t i = 0;
  318. uint8_t *ptr;
  319. uint16_t rem;
  320. uint8_t buf[8];
  321. i15765_t frame;
  322. /* copy over message */
  323. frame = mftb->msg;
  324. frame.buf = buf;
  325. ptr = mftb->msg.buf;
  326. /* update separation time */
  327. if(mftb->st_cnt)
  328. {
  329. /* delay */
  330. mftb->st_cnt--;
  331. return;
  332. }
  333. else
  334. {
  335. /* transmit */
  336. mftb->st_cnt = mftb->st;
  337. }
  338. /* load PDU type */
  339. frame.buf[i++] = (I15765_PDU_CF << 4) | (mftb->sn++ & 0xf);
  340. /* how much data can we (or need we) copy over? */
  341. rem = mftb->msg.buf_len - (mftb->msg.buf - mftb->buf);
  342. min = (uint8_t)MIN(rem, 8 - i);
  343. /* add in the PDU field (and maybe AE) */
  344. min += i;
  345. /* copy over the data */
  346. while(i < min)
  347. frame.buf[i++] = *ptr++;
  348. /* FF's are always 8 bytes long */
  349. frame.buf_len = min;
  350. /* transmit the CAN frame, testing to see if it was sent correctly */
  351. if(i15765_tx(&frame) == 0)
  352. {
  353. mftb->timeout = TIMEOUT_CF_S;
  354. mftb->msg.buf = ptr;
  355. /* if bs is zero, then transmit away, else check how many we have left */
  356. mftb->bs_cnt--;
  357. if(mftb->bs && (mftb->bs_cnt == 0))
  358. {
  359. // mftb->state = STATE_WT_FC;//qiaoxu Ϊ���ó����ķ��ʹ���8֡
  360. }
  361. }
  362. rem = mftb->msg.buf_len - (mftb->msg.buf - mftb->buf);
  363. if(rem == 0)
  364. {
  365. *mftb->status = I15765_SENT;
  366. mftb->status = &i15765_tmp;
  367. mftb->state = STATE_IDLE;
  368. }
  369. return;
  370. }
  371. /*
  372. ** Initialize and attempt transmit a multi-frame message.
  373. ** INPUT: msg - pointer to the i15765 message to be transmitted
  374. ** status - pointer to user RAM location for status feedback
  375. **
  376. */
  377. void i15765_tx_mf(i15765_t *msg, uint8_t *status)
  378. {
  379. uint16_t i;
  380. i15765_mft_t *mftb;
  381. /* delete any mf buffers with matching address information */
  382. // i15765_mftb_del(msg);
  383. /* assume failure */
  384. *status = I15765_FAILED;
  385. /* find an available spot for this message */
  386. if(i15765_mftb_get(&mftb))
  387. return;
  388. /* there was room, so update status */
  389. *status = I15765_SENDING;
  390. /* copy over the data */
  391. for(i = 0; i < msg->buf_len; i++)
  392. mftb->buf[i] = msg->buf[i];
  393. /* copy over message */
  394. mftb->msg = *msg;
  395. mftb->msg.buf = mftb->buf;
  396. /* set state to wait for a flow control PDU */
  397. mftb->state = STATE_TX_FF;
  398. mftb->sn = 1;
  399. mftb->status = status;
  400. /* transmit the CAN frame, testing to see if it was sent correctly */
  401. mftb->timeout = TIMEOUT_TX_S;
  402. i15765_tx_ff(mftb);
  403. }
  404. /*
  405. ** Transmit a flow control frame and updates the status of mfrb. Reception
  406. ** will be paused until the FC has been transmitted.
  407. ** INPUT: mfrb - pointer multiframe receive buffer
  408. */
  409. void i15765_tx_fc(i15765_mfr_t *mfrb)
  410. {
  411. uint8_t fs;
  412. i15765_t msg;
  413. uint8_t buf[8];
  414. /* get the FF_DL and check for potential buffer overflow */
  415. if(mfrb->msg.buf_len > I15765CFG_MF_RX_BUF_SIZE)
  416. {
  417. fs = I15765_FS_OVFLW;
  418. mfrb->state = STATE_TX_OVFLW;
  419. }
  420. else
  421. {
  422. fs = I15765_FS_CTS; //
  423. // fs = I15765_FS_WT; //wait
  424. mfrb->state = STATE_TX_CTS;
  425. }
  426. /* pack the pci */
  427. msg.buf = buf;
  428. msg.buf[0] = (I15765_PDU_FC << 4) | fs;
  429. msg.buf[1] = BS; //qiaoxu modify
  430. msg.buf[2] = STmin; //qiaoxu modify
  431. msg.buf_len = 3;
  432. msg.sa = i15765_sa;
  433. msg.ta = mfrb->msg.sa;
  434. msg.tat = mfrb->msg.tat;
  435. /* set pri same as received FF */
  436. msg.pri = mfrb->msg.pri;
  437. /* send the message */
  438. if(i15765_tx(&msg) == 0)
  439. {
  440. /* transmission successful */
  441. mfrb->timeout = TIMEOUT_CF_R;
  442. mfrb->state = (mfrb->state == STATE_TX_CTS) ? STATE_WT_CF : STATE_IDLE;
  443. }
  444. }
  445. /*
  446. ** Handles a recieved single frame
  447. ** INPUT: msg - pointer to the received message
  448. */
  449. void i15765_rx_sf(i15765_t *msg)
  450. {
  451. /* ignore message if errors are present */
  452. if((msg->buf[0] == 0) || (msg->buf[0] > (msg->buf_len - 1)))
  453. {
  454. return;
  455. }
  456. /* message length */
  457. msg->buf_len = msg->buf[0];
  458. /* skip PCI field */
  459. msg->buf++;
  460. i15765app_process(msg);
  461. }
  462. /*
  463. ** Handles a recieved first frame.
  464. ** INPUT: msg - pointer to the received message segment
  465. */
  466. void i15765_rx_ff(i15765_t *msg)
  467. {
  468. uint8_t i;
  469. uint16_t ff_dl;
  470. i15765_mfr_t *mfrb;
  471. //qiaoxu ɾ��
  472. /* delete any mf buffers with matching address information */
  473. i15765_mfrb_del(msg);
  474. /* find an available spot for this message */
  475. if(i15765_mfrb_get(&mfrb))
  476. return;
  477. /* first frame data length */
  478. ff_dl = ((msg->buf[0] & 0xf) << 8) | msg->buf[1];
  479. /* if the total size could fit in an SF PDU, ignore */
  480. if(ff_dl <= 7)
  481. return;
  482. /* skip PCI field */
  483. msg->buf += 2;
  484. msg->buf_len -= 2;
  485. /* buffer the necessary info */
  486. mfrb->sn = 0;
  487. mfrb->msg = *msg;
  488. mfrb->msg.buf_len = ff_dl;
  489. /* reset buf pointer and store data that was recieved with the FF */
  490. mfrb->msg.buf = mfrb->buf;
  491. for(i = 0; i < msg->buf_len; i++)
  492. *mfrb->msg.buf++ = msg->buf[i];
  493. /* transmit a flow control frame (the status will be updated based on FS) */
  494. mfrb->timeout = TIMEOUT_TX_R;
  495. i15765_tx_fc(mfrb);
  496. }
  497. /*
  498. ** Handles a recieved consecutive frame
  499. ** INPUT: msg - pointer to the received message
  500. */
  501. void i15765_rx_cf(i15765_t *msg)
  502. {
  503. uint8_t i;
  504. uint8_t min;
  505. uint16_t rem;
  506. i15765_mfr_t *mfrb;
  507. /* find the matching index */ //qiaoxu
  508. if(i15765_mfrb_seek(msg, &mfrb))
  509. return;
  510. /* if the CF frame is not expected, ignore it */
  511. if(mfrb->state != STATE_WT_CF)
  512. return;
  513. /* increment the sequence number and compare */
  514. if((++mfrb->sn & 0xf) == (msg->buf[0] & 0xf))
  515. {
  516. /* skip PCI field */
  517. msg->buf++;
  518. msg->buf_len--;
  519. /* if we are at the end of the message, stop early */
  520. rem = (mfrb->buf + mfrb->msg.buf_len) - mfrb->msg.buf;
  521. min = (uint8_t) MIN(msg->buf_len, rem);
  522. /* add the data to the buffer */
  523. for(i = 0; i < min; i++)
  524. *mfrb->msg.buf++ = msg->buf[i];
  525. rem = (mfrb->buf + mfrb->msg.buf_len) - mfrb->msg.buf;
  526. /* if we stopped early, the reception is complete */
  527. if(rem == 0)
  528. {
  529. mfrb->msg.buf = mfrb->buf;
  530. i15765app_process(&mfrb->msg);
  531. mfrb->state = STATE_IDLE;
  532. }
  533. else
  534. {
  535. mfrb->timeout = TIMEOUT_CF_R;
  536. }
  537. }
  538. else
  539. {
  540. /* abort the reception */
  541. mfrb->state = STATE_IDLE;
  542. }
  543. }
  544. /*
  545. ** Handle a recieved flow control frame
  546. ** INPUT: msg - pointer to the received message
  547. */
  548. void i15765_rx_fc(i15765_t *msg)
  549. {
  550. i15765_mft_t *mftb;
  551. /* find matching multiframe transmit buffer */
  552. if(i15765_mftb_seek(msg, &mftb))//qiaoxu
  553. return;
  554. /* if the FC frame is not expected, ignore it */
  555. if(mftb->state != STATE_WT_FC)
  556. return;
  557. //qiaoxu add
  558. if(msg->buf_len<3)
  559. {
  560. return;
  561. }
  562. //////////////////////////////////////////////////////////////
  563. /* determine what type of FC this is and act appropriately */
  564. switch(msg->buf[0] & 0xf)
  565. {
  566. /* continue to send */
  567. case 0:
  568. mftb->state = STATE_TX_CF;
  569. mftb->bs = msg->buf[1];
  570. mftb->bs_cnt = mftb->bs;
  571. if(msg->buf[2] <= 0x7f)
  572. mftb->st = msg->buf[2];
  573. else if((msg->buf[2] >= 0xf1) && (msg->buf[2] <= 0xf9))
  574. mftb->st = 0x01;
  575. else
  576. mftb->st = 0x7f;
  577. /* convert to units of 100 microseconds (1ms) */
  578. mftb->st *= ((uint16_t)1);
  579. /* convert to ticks */
  580. mftb->st /= ((uint8_t)I15765CFG_TICK_PERIOD);
  581. mftb->st_cnt = mftb->st;
  582. mftb->timeout = TIMEOUT_CF_S;
  583. mftb->wt_cnt = 0;
  584. break;
  585. /* wait (NOTE - must be above "default" case) */
  586. case 1:
  587. mftb->state = STATE_WT_FC;
  588. mftb->timeout = TIMEOUT_FC_S;
  589. /* if too many waits, bail */
  590. if(++mftb->wt_cnt < 5)
  591. break;
  592. /* all others */
  593. default:
  594. mftb->state = STATE_IDLE;
  595. *mftb->status = I15765_FAILED;
  596. mftb->status = &i15765_tmp;
  597. break;
  598. }
  599. }
  600. /*
  601. ** Translates the CAN frame into an i15765 message
  602. ** INPUT: can - pointer to the received CAN frame
  603. */
  604. void i15765_rx_post(can_t *can)
  605. {
  606. i15765_t msg;
  607. uint8_t pdu_type;
  608. if(!(can->id & B31))
  609. {
  610. msg.buf = &can->buf[0];
  611. msg.buf_len = can->buf_len;
  612. msg.pri = 0;
  613. if(can->id == 0x7DF)
  614. {
  615. /* support incoming functional requests */
  616. msg.tat = I15765_TAT_NF11;
  617. //msg.sa = 0xf1;
  618. msg.sa = I15765CFG_SA;
  619. }
  620. else if(can->id == UDS_RX_ID)//
  621. {
  622. /* support incoming requests (0xf1 is external test equipment) */
  623. msg.tat = I15765_TAT_NP11;
  624. //msg.sa = 0xf1;
  625. msg.sa = I15765CFG_SA; //
  626. msg.ta = (can->id - 0x7e0) + 1;
  627. }
  628. }
  629. /* SF, FF, CF, or FC? */
  630. pdu_type = msg.buf[0] >> 4;
  631. /* if this message is not for us or not functional, discard it */
  632. if((ADDR_FUNC(msg.tat) && pdu_type != I15765_PDU_SF))
  633. {
  634. return;
  635. }
  636. if(pdu_type <= 3)
  637. {
  638. switch(pdu_type)
  639. {
  640. case I15765_PDU_SF:
  641. i15765_rx_sf(&msg);
  642. break;
  643. case I15765_PDU_FF:
  644. i15765_rx_ff(&msg);
  645. break;
  646. case I15765_PDU_CF:
  647. i15765_rx_cf(&msg);
  648. break;
  649. case I15765_PDU_FC:
  650. i15765_rx_fc(&msg);
  651. break;
  652. }
  653. }
  654. }
  655. /*
  656. ** Requests a message to be transmitted, either single or multi-frame
  657. ** INPUT: msg - the message to transmit
  658. ** status - pointer to user RAM location for status feedback
  659. */
  660. void i15765_tx_app(i15765_t *msg, uint8_t *status)
  661. {
  662. /* status always has to point somewhere */
  663. if(status == 0)
  664. status = &i15765_tmp;
  665. /* load in source address into out going message */
  666. msg->sa = i15765_sa;
  667. /* if its small enough to send in a SF, pack and transmit */
  668. if(msg->buf_len <= 7)
  669. {
  670. *status = (i15765_tx_sf(msg) == 0) ? I15765_SENT : I15765_FAILED;
  671. }
  672. else if(msg->buf_len <= I15765CFG_MF_TX_BUF_SIZE)
  673. {
  674. /* multiframe - only support physical addressing */
  675. i15765_tx_mf(msg, status);
  676. }
  677. else
  678. {
  679. /* too big or no available buffers */
  680. *status = I15765_FAILED;
  681. }
  682. }
  683. /*
  684. ** Cycle through our active messages and attempt to continue
  685. ** transmitting. Also, check for timeouts as we go.
  686. */
  687. void i15765_tx_update(void)
  688. {
  689. uint8_t i;
  690. /* for each currently transmitting message, continue */
  691. for(i = 0; i < I15765CFG_MF_TX_BUF_NUM; i++)
  692. {
  693. /* if we are waiting on a transmission to complete, try it again */
  694. switch(i15765_mftb[i].state)
  695. {
  696. case STATE_TX_FF:
  697. i15765_tx_ff(&i15765_mftb[i]);
  698. break;
  699. case STATE_TX_CF:
  700. i15765_tx_cf(&i15765_mftb[i]);
  701. break;
  702. }
  703. /* update timeout */
  704. if(i15765_mftb[i].timeout)
  705. {
  706. i15765_mftb[i].timeout--;
  707. }
  708. else
  709. {
  710. i15765_mftb[i].state = STATE_IDLE;
  711. *i15765_mftb[i].status = I15765_FAILED;
  712. i15765_mftb[i].status = &i15765_tmp;
  713. // i15765_mftb[i].status = 0;
  714. }
  715. }
  716. }
  717. /*
  718. ** Cycle through our active messages and attempt to continue
  719. ** reception checking for timeouts as we go.
  720. */
  721. void i15765_rx_update(void)
  722. {
  723. can_t can;
  724. uint8_t i;
  725. /* read all the CAN frames and pass them up */
  726. while(can_rx(0, &can) == 0)
  727. i15765_rx_post(&can);
  728. /* for each active message, check for expiration and cancel if needed */
  729. for(i = 0; i < I15765CFG_MF_RX_BUF_NUM; i++)
  730. {
  731. /* if we are waiting to send an FC frame, try it again */
  732. if((i15765_mfrb[i].state == STATE_TX_CTS)
  733. || (i15765_mfrb[i].state == STATE_TX_OVFLW))
  734. i15765_tx_fc(&i15765_mfrb[i]);
  735. /* update timeout */
  736. if(i15765_mfrb[i].timeout)
  737. i15765_mfrb[i].timeout--;
  738. else
  739. i15765_mfrb[i].state = STATE_IDLE;
  740. }
  741. }
  742. /*
  743. ** This function is the time base for the entire i15765 module.
  744. */
  745. void i15765_update(void)
  746. {
  747. i15765_rx_update();
  748. i15765_tx_update();
  749. }