UART.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * Copyright (c) 2021 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. * ----------------------------------------------------------------------
  19. *
  20. * $Date: 1. March 2021
  21. * $Revision: V1.0.0
  22. *
  23. * Project: CMSIS-DAP Source
  24. * Title: UART.c CMSIS-DAP UART
  25. *
  26. *---------------------------------------------------------------------------*/
  27. #include "DAP_config.h"
  28. #include "DAP.h"
  29. #if (DAP_UART != 0)
  30. #ifdef DAP_FW_V1
  31. #error "UART Communication Port not supported in DAP V1!"
  32. #endif
  33. #include "Driver_USART.h"
  34. #include "cmsis_os2.h"
  35. #include <string.h>
  36. #define UART_RX_BLOCK_SIZE 32U /* Uart Rx Block Size (must be 2^n) */
  37. // USART Driver
  38. #define _USART_Driver_(n) Driver_USART##n
  39. #define USART_Driver_(n) _USART_Driver_(n)
  40. extern ARM_DRIVER_USART USART_Driver_(DAP_UART_DRIVER);
  41. #define pUSART (&USART_Driver_(DAP_UART_DRIVER))
  42. // UART Configuration
  43. #if (DAP_UART_USB_COM_PORT != 0)
  44. static uint8_t UartTransport = DAP_UART_TRANSPORT_USB_COM_PORT;
  45. #else
  46. static uint8_t UartTransport = DAP_UART_TRANSPORT_NONE;
  47. #endif
  48. // UART Flags
  49. static uint8_t UartConfigured = 0U;
  50. static uint8_t UartReceiveEnabled = 0U;
  51. static uint8_t UartTransmitEnabled = 0U;
  52. static uint8_t UartTransmitActive = 0U;
  53. // UART TX Buffer
  54. static uint8_t UartTxBuf[DAP_UART_TX_BUFFER_SIZE];
  55. static volatile uint32_t UartTxIndexI = 0U;
  56. static volatile uint32_t UartTxIndexO = 0U;
  57. // UART RX Buffer
  58. static uint8_t UartRxBuf[DAP_UART_RX_BUFFER_SIZE];
  59. static volatile uint32_t UartRxIndexI = 0U;
  60. static volatile uint32_t UartRxIndexO = 0U;
  61. // Uart Errors
  62. static volatile uint8_t UartErrorRxDataLost = 0U;
  63. static volatile uint8_t UartErrorFraming = 0U;
  64. static volatile uint8_t UartErrorParity = 0U;
  65. // UART Transmit
  66. static uint32_t UartTxNum = 0U;
  67. // Function prototypes
  68. static uint8_t UART_Init (void);
  69. static void UART_Uninit (void);
  70. static uint8_t UART_Get_Status (void);
  71. static uint8_t UART_Receive_Enable (void);
  72. static uint8_t UART_Transmit_Enable (void);
  73. static void UART_Receive_Disable (void);
  74. static void UART_Transmit_Disable (void);
  75. static void UART_Receive_Flush (void);
  76. static void UART_Transmit_Flush (void);
  77. static void UART_Receive (void);
  78. static void UART_Transmit (void);
  79. // USART Driver Callback function
  80. // event: event mask
  81. static void USART_Callback (uint32_t event) {
  82. if (event & ARM_USART_EVENT_SEND_COMPLETE) {
  83. UartTxIndexO += UartTxNum;
  84. UartTransmitActive = 0U;
  85. UART_Transmit();
  86. }
  87. if (event & ARM_USART_EVENT_RECEIVE_COMPLETE) {
  88. UartRxIndexI += UART_RX_BLOCK_SIZE;
  89. UART_Receive();
  90. }
  91. if (event & ARM_USART_EVENT_RX_OVERFLOW) {
  92. UartErrorRxDataLost = 1U;
  93. }
  94. if (event & ARM_USART_EVENT_RX_FRAMING_ERROR) {
  95. UartErrorFraming = 1U;
  96. }
  97. if (event & ARM_USART_EVENT_RX_PARITY_ERROR) {
  98. UartErrorParity = 1U;
  99. }
  100. }
  101. // Init UART
  102. // return: DAP_OK or DAP_ERROR
  103. static uint8_t UART_Init (void) {
  104. int32_t status;
  105. uint8_t ret = DAP_ERROR;
  106. UartConfigured = 0U;
  107. UartReceiveEnabled = 0U;
  108. UartTransmitEnabled = 0U;
  109. UartTransmitActive = 0U;
  110. UartErrorRxDataLost = 0U;
  111. UartErrorFraming = 0U;
  112. UartErrorParity = 0U;
  113. UartTxIndexI = 0U;
  114. UartTxIndexO = 0U;
  115. UartRxIndexI = 0U;
  116. UartRxIndexO = 0U;
  117. UartTxNum = 0U;
  118. status = pUSART->Initialize(USART_Callback);
  119. if (status == ARM_DRIVER_OK) {
  120. status = pUSART->PowerControl(ARM_POWER_FULL);
  121. }
  122. if (status == ARM_DRIVER_OK) {
  123. ret = DAP_OK;
  124. }
  125. return (ret);
  126. }
  127. // Un-Init UART
  128. static void UART_Uninit (void) {
  129. UartConfigured = 0U;
  130. pUSART->PowerControl(ARM_POWER_OFF);
  131. pUSART->Uninitialize();
  132. }
  133. // Get UART Status
  134. // return: status
  135. static uint8_t UART_Get_Status (void) {
  136. uint8_t status = 0U;
  137. if (UartReceiveEnabled != 0U) {
  138. status |= DAP_UART_STATUS_RX_ENABLED;
  139. }
  140. if (UartErrorRxDataLost != 0U) {
  141. UartErrorRxDataLost = 0U;
  142. status |= DAP_UART_STATUS_RX_DATA_LOST;
  143. }
  144. if (UartErrorFraming != 0U) {
  145. UartErrorFraming = 0U;
  146. status |= DAP_UART_STATUS_FRAMING_ERROR;
  147. }
  148. if (UartErrorParity != 0U) {
  149. UartErrorParity = 0U;
  150. status |= DAP_UART_STATUS_PARITY_ERROR;
  151. }
  152. if (UartTransmitEnabled != 0U) {
  153. status |= DAP_UART_STATUS_TX_ENABLED;
  154. }
  155. return (status);
  156. }
  157. // Enable UART Receive
  158. // return: DAP_OK or DAP_ERROR
  159. static uint8_t UART_Receive_Enable (void) {
  160. int32_t status;
  161. uint8_t ret = DAP_ERROR;
  162. if (UartReceiveEnabled == 0U) {
  163. // Flush Buffers
  164. UartRxIndexI = 0U;
  165. UartRxIndexO = 0U;
  166. UART_Receive();
  167. status = pUSART->Control(ARM_USART_CONTROL_RX, 1U);
  168. if (status == ARM_DRIVER_OK) {
  169. UartReceiveEnabled = 1U;
  170. ret = DAP_OK;
  171. }
  172. } else {
  173. ret = DAP_OK;
  174. }
  175. return (ret);
  176. }
  177. // Enable UART Transmit
  178. // return: DAP_OK or DAP_ERROR
  179. static uint8_t UART_Transmit_Enable (void) {
  180. int32_t status;
  181. uint8_t ret = DAP_ERROR;
  182. if (UartTransmitEnabled == 0U) {
  183. // Flush Buffers
  184. UartTransmitActive = 0U;
  185. UartTxIndexI = 0U;
  186. UartTxIndexO = 0U;
  187. UartTxNum = 0U;
  188. status = pUSART->Control(ARM_USART_CONTROL_TX, 1U);
  189. if (status == ARM_DRIVER_OK) {
  190. UartTransmitEnabled = 1U;
  191. ret = DAP_OK;
  192. }
  193. } else {
  194. ret = DAP_OK;
  195. }
  196. return (ret);
  197. }
  198. // Disable UART Receive
  199. static void UART_Receive_Disable (void) {
  200. if (UartReceiveEnabled != 0U) {
  201. pUSART->Control(ARM_USART_CONTROL_RX, 0U);
  202. pUSART->Control(ARM_USART_ABORT_RECEIVE, 0U);
  203. UartReceiveEnabled = 0U;
  204. }
  205. }
  206. // Disable UART Transmit
  207. static void UART_Transmit_Disable (void) {
  208. if (UartTransmitEnabled != 0U) {
  209. pUSART->Control(ARM_USART_ABORT_SEND, 0U);
  210. pUSART->Control(ARM_USART_CONTROL_TX, 0U);
  211. UartTransmitActive = 0U;
  212. UartTransmitEnabled = 0U;
  213. }
  214. }
  215. // Flush UART Receive buffer
  216. static void UART_Receive_Flush (void) {
  217. pUSART->Control(ARM_USART_ABORT_RECEIVE, 0U);
  218. UartRxIndexI = 0U;
  219. UartRxIndexO = 0U;
  220. if (UartReceiveEnabled != 0U) {
  221. UART_Receive();
  222. }
  223. }
  224. // Flush UART Transmit buffer
  225. static void UART_Transmit_Flush (void) {
  226. pUSART->Control(ARM_USART_ABORT_SEND, 0U);
  227. UartTransmitActive = 0U;
  228. UartTxIndexI = 0U;
  229. UartTxIndexO = 0U;
  230. UartTxNum = 0U;
  231. }
  232. // Receive data from target via UART
  233. static void UART_Receive (void) {
  234. uint32_t index;
  235. index = UartRxIndexI & (DAP_UART_RX_BUFFER_SIZE - 1U);
  236. pUSART->Receive(&UartRxBuf[index], UART_RX_BLOCK_SIZE);
  237. }
  238. // Transmit available data to target via UART
  239. static void UART_Transmit (void) {
  240. uint32_t count;
  241. uint32_t index;
  242. count = UartTxIndexI - UartTxIndexO;
  243. index = UartTxIndexO & (DAP_UART_TX_BUFFER_SIZE - 1U);
  244. if (count != 0U) {
  245. if ((index + count) <= DAP_UART_TX_BUFFER_SIZE) {
  246. UartTxNum = count;
  247. } else {
  248. UartTxNum = DAP_UART_TX_BUFFER_SIZE - index;
  249. }
  250. UartTransmitActive = 1U;
  251. pUSART->Send(&UartTxBuf[index], UartTxNum);
  252. }
  253. }
  254. // Process UART Transport command and prepare response
  255. // request: pointer to request data
  256. // response: pointer to response data
  257. // return: number of bytes in response (lower 16 bits)
  258. // number of bytes in request (upper 16 bits)
  259. uint32_t UART_Transport (const uint8_t *request, uint8_t *response) {
  260. uint8_t transport;
  261. uint8_t ret = DAP_ERROR;
  262. transport = *request;
  263. switch (transport) {
  264. case DAP_UART_TRANSPORT_NONE:
  265. switch (UartTransport) {
  266. case DAP_UART_TRANSPORT_NONE:
  267. ret = DAP_OK;
  268. break;
  269. case DAP_UART_TRANSPORT_USB_COM_PORT:
  270. #if (DAP_UART_USB_COM_PORT != 0)
  271. USB_COM_PORT_Activate(0U);
  272. UartTransport = DAP_UART_TRANSPORT_NONE;
  273. ret = DAP_OK;
  274. #endif
  275. break;
  276. case DAP_UART_TRANSPORT_DAP_COMMAND:
  277. UART_Receive_Disable();
  278. UART_Transmit_Disable();
  279. UART_Uninit();
  280. UartTransport = DAP_UART_TRANSPORT_NONE;
  281. ret= DAP_OK;
  282. break;
  283. }
  284. break;
  285. case DAP_UART_TRANSPORT_USB_COM_PORT:
  286. switch (UartTransport) {
  287. case DAP_UART_TRANSPORT_NONE:
  288. #if (DAP_UART_USB_COM_PORT != 0)
  289. if (USB_COM_PORT_Activate(1U) == 0U) {
  290. UartTransport = DAP_UART_TRANSPORT_USB_COM_PORT;
  291. ret = DAP_OK;
  292. }
  293. #endif
  294. break;
  295. case DAP_UART_TRANSPORT_USB_COM_PORT:
  296. ret = DAP_OK;
  297. break;
  298. case DAP_UART_TRANSPORT_DAP_COMMAND:
  299. UART_Receive_Disable();
  300. UART_Transmit_Disable();
  301. UART_Uninit();
  302. UartTransport = DAP_UART_TRANSPORT_NONE;
  303. #if (DAP_UART_USB_COM_PORT != 0)
  304. if (USB_COM_PORT_Activate(1U) == 0U) {
  305. UartTransport = DAP_UART_TRANSPORT_USB_COM_PORT;
  306. ret = DAP_OK;
  307. }
  308. #endif
  309. break;
  310. }
  311. break;
  312. case DAP_UART_TRANSPORT_DAP_COMMAND:
  313. switch (UartTransport) {
  314. case DAP_UART_TRANSPORT_NONE:
  315. ret = UART_Init();
  316. if (ret == DAP_OK) {
  317. UartTransport = DAP_UART_TRANSPORT_DAP_COMMAND;
  318. }
  319. break;
  320. case DAP_UART_TRANSPORT_USB_COM_PORT:
  321. #if (DAP_UART_USB_COM_PORT != 0)
  322. USB_COM_PORT_Activate(0U);
  323. UartTransport = DAP_UART_TRANSPORT_NONE;
  324. #endif
  325. ret = UART_Init();
  326. if (ret == DAP_OK) {
  327. UartTransport = DAP_UART_TRANSPORT_DAP_COMMAND;
  328. }
  329. break;
  330. case DAP_UART_TRANSPORT_DAP_COMMAND:
  331. ret = DAP_OK;
  332. break;
  333. }
  334. break;
  335. default:
  336. break;
  337. }
  338. *response = ret;
  339. return ((1U << 16) | 1U);
  340. }
  341. // Process UART Configure command and prepare response
  342. // request: pointer to request data
  343. // response: pointer to response data
  344. // return: number of bytes in response (lower 16 bits)
  345. // number of bytes in request (upper 16 bits)
  346. uint32_t UART_Configure (const uint8_t *request, uint8_t *response) {
  347. uint8_t control, status;
  348. uint32_t baudrate;
  349. int32_t result;
  350. if (UartTransport != DAP_UART_TRANSPORT_DAP_COMMAND) {
  351. status = DAP_UART_CFG_ERROR_DATA_BITS |
  352. DAP_UART_CFG_ERROR_PARITY |
  353. DAP_UART_CFG_ERROR_STOP_BITS;
  354. baudrate = 0U; // baudrate error
  355. } else {
  356. status = 0U;
  357. control = *request;
  358. baudrate = (uint32_t)(*(request+1) << 0) |
  359. (uint32_t)(*(request+2) << 8) |
  360. (uint32_t)(*(request+3) << 16) |
  361. (uint32_t)(*(request+4) << 24);
  362. result = pUSART->Control(control |
  363. ARM_USART_MODE_ASYNCHRONOUS |
  364. ARM_USART_FLOW_CONTROL_NONE,
  365. baudrate);
  366. if (result == ARM_DRIVER_OK) {
  367. UartConfigured = 1U;
  368. } else {
  369. UartConfigured = 0U;
  370. switch (result) {
  371. case ARM_USART_ERROR_BAUDRATE:
  372. status = 0U;
  373. baudrate = 0U;
  374. break;
  375. case ARM_USART_ERROR_DATA_BITS:
  376. status = DAP_UART_CFG_ERROR_DATA_BITS;
  377. break;
  378. case ARM_USART_ERROR_PARITY:
  379. status = DAP_UART_CFG_ERROR_PARITY;
  380. break;
  381. case ARM_USART_ERROR_STOP_BITS:
  382. status = DAP_UART_CFG_ERROR_STOP_BITS;
  383. break;
  384. default:
  385. status = DAP_UART_CFG_ERROR_DATA_BITS |
  386. DAP_UART_CFG_ERROR_PARITY |
  387. DAP_UART_CFG_ERROR_STOP_BITS;
  388. baudrate = 0U;
  389. break;
  390. }
  391. }
  392. }
  393. *response++ = status;
  394. *response++ = (uint8_t)(baudrate >> 0);
  395. *response++ = (uint8_t)(baudrate >> 8);
  396. *response++ = (uint8_t)(baudrate >> 16);
  397. *response = (uint8_t)(baudrate >> 24);
  398. return ((5U << 16) | 5U);
  399. }
  400. // Process UART Control command and prepare response
  401. // request: pointer to request data
  402. // response: pointer to response data
  403. // return: number of bytes in response (lower 16 bits)
  404. // number of bytes in request (upper 16 bits)
  405. uint32_t UART_Control (const uint8_t *request, uint8_t *response) {
  406. uint8_t control;
  407. uint8_t result;
  408. uint8_t ret = DAP_OK;
  409. if (UartTransport != DAP_UART_TRANSPORT_DAP_COMMAND) {
  410. ret = DAP_ERROR;
  411. } else {
  412. control = *request;
  413. if ((control & DAP_UART_CONTROL_RX_DISABLE) != 0U) {
  414. // Receive disable
  415. UART_Receive_Disable();
  416. } else if ((control & DAP_UART_CONTROL_RX_ENABLE) != 0U) {
  417. // Receive enable
  418. if (UartConfigured != 0U) {
  419. result = UART_Receive_Enable();
  420. if (result != DAP_OK) {
  421. ret = DAP_ERROR;
  422. }
  423. } else {
  424. ret = DAP_ERROR;
  425. }
  426. }
  427. if ((control & DAP_UART_CONTROL_RX_BUF_FLUSH) != 0U) {
  428. UART_Receive_Flush();
  429. }
  430. if ((control & DAP_UART_CONTROL_TX_DISABLE) != 0U) {
  431. // Transmit disable
  432. UART_Transmit_Disable();
  433. } else if ((control & DAP_UART_CONTROL_TX_ENABLE) != 0U) {
  434. // Transmit enable
  435. if (UartConfigured != 0U) {
  436. result = UART_Transmit_Enable();
  437. if (result != DAP_OK) {
  438. ret = DAP_ERROR;
  439. }
  440. } else {
  441. ret = DAP_ERROR;
  442. }
  443. }
  444. if ((control & DAP_UART_CONTROL_TX_BUF_FLUSH) != 0U) {
  445. UART_Transmit_Flush();
  446. }
  447. }
  448. *response = ret;
  449. return ((1U << 16) | 1U);
  450. }
  451. // Process UART Status command and prepare response
  452. // response: pointer to response data
  453. // return: number of bytes in response (lower 16 bits)
  454. // number of bytes in request (upper 16 bits)
  455. uint32_t UART_Status (uint8_t *response) {
  456. uint32_t rx_cnt, tx_cnt;
  457. uint32_t cnt;
  458. uint8_t status;
  459. if ((UartTransport != DAP_UART_TRANSPORT_DAP_COMMAND) ||
  460. (UartConfigured == 0U)) {
  461. rx_cnt = 0U;
  462. tx_cnt = 0U;
  463. status = 0U;
  464. } else {
  465. rx_cnt = UartRxIndexI - UartRxIndexO;
  466. rx_cnt += pUSART->GetRxCount();
  467. if (rx_cnt > (DAP_UART_RX_BUFFER_SIZE - (UART_RX_BLOCK_SIZE*2))) {
  468. // Overflow
  469. UartErrorRxDataLost = 1U;
  470. rx_cnt = (DAP_UART_RX_BUFFER_SIZE - (UART_RX_BLOCK_SIZE*2));
  471. UartRxIndexO = UartRxIndexI - rx_cnt;
  472. }
  473. tx_cnt = UartTxIndexI - UartTxIndexO;
  474. cnt = pUSART->GetTxCount();
  475. if (UartTransmitActive != 0U) {
  476. tx_cnt -= cnt;
  477. }
  478. status = UART_Get_Status();
  479. }
  480. *response++ = status;
  481. *response++ = (uint8_t)(rx_cnt >> 0);
  482. *response++ = (uint8_t)(rx_cnt >> 8);
  483. *response++ = (uint8_t)(rx_cnt >> 16);
  484. *response++ = (uint8_t)(rx_cnt >> 24);
  485. *response++ = (uint8_t)(tx_cnt >> 0);
  486. *response++ = (uint8_t)(tx_cnt >> 8);
  487. *response++ = (uint8_t)(tx_cnt >> 16);
  488. *response = (uint8_t)(tx_cnt >> 24);
  489. return ((0U << 16) | 9U);
  490. }
  491. // Process UART Transfer command and prepare response
  492. // request: pointer to request data
  493. // response: pointer to response data
  494. // return: number of bytes in response (lower 16 bits)
  495. // number of bytes in request (upper 16 bits)
  496. uint32_t UART_Transfer (const uint8_t *request, uint8_t *response) {
  497. uint32_t rx_cnt, tx_cnt;
  498. uint32_t rx_num, tx_num;
  499. uint8_t *rx_data;
  500. const
  501. uint8_t *tx_data;
  502. uint32_t num;
  503. uint32_t index;
  504. uint8_t status;
  505. if (UartTransport != DAP_UART_TRANSPORT_DAP_COMMAND) {
  506. status = 0U;
  507. rx_cnt = 0U;
  508. tx_cnt = 0U;
  509. } else {
  510. // RX Data
  511. rx_cnt = ((uint32_t)(*(request+0) << 0) |
  512. (uint32_t)(*(request+1) << 8));
  513. if (rx_cnt > (DAP_PACKET_SIZE - 6U)) {
  514. rx_cnt = (DAP_PACKET_SIZE - 6U);
  515. }
  516. rx_num = UartRxIndexI - UartRxIndexO;
  517. rx_num += pUSART->GetRxCount();
  518. if (rx_num > (DAP_UART_RX_BUFFER_SIZE - (UART_RX_BLOCK_SIZE*2))) {
  519. // Overflow
  520. UartErrorRxDataLost = 1U;
  521. rx_num = (DAP_UART_RX_BUFFER_SIZE - (UART_RX_BLOCK_SIZE*2));
  522. UartRxIndexO = UartRxIndexI - rx_num;
  523. }
  524. if (rx_cnt > rx_num) {
  525. rx_cnt = rx_num;
  526. }
  527. rx_data = (response+5);
  528. index = UartRxIndexO & (DAP_UART_RX_BUFFER_SIZE - 1U);
  529. if ((index + rx_cnt) <= DAP_UART_RX_BUFFER_SIZE) {
  530. memcpy( rx_data, &UartRxBuf[index], rx_cnt);
  531. } else {
  532. num = DAP_UART_RX_BUFFER_SIZE - index;
  533. memcpy( rx_data, &UartRxBuf[index], num);
  534. memcpy(&rx_data[num], &UartRxBuf[0], rx_cnt - num);
  535. }
  536. UartRxIndexO += rx_cnt;
  537. // TX Data
  538. tx_cnt = ((uint32_t)(*(request+2) << 0) |
  539. (uint32_t)(*(request+3) << 8));
  540. tx_data = (request+4);
  541. if (tx_cnt > (DAP_PACKET_SIZE - 5U)) {
  542. tx_cnt = (DAP_PACKET_SIZE - 5U);
  543. }
  544. tx_num = UartTxIndexI - UartTxIndexO;
  545. num = pUSART->GetTxCount();
  546. if (UartTransmitActive != 0U) {
  547. tx_num -= num;
  548. }
  549. if (tx_cnt > (DAP_UART_TX_BUFFER_SIZE - tx_num)) {
  550. tx_cnt = (DAP_UART_TX_BUFFER_SIZE - tx_num);
  551. }
  552. index = UartTxIndexI & (DAP_UART_TX_BUFFER_SIZE - 1U);
  553. if ((index + tx_cnt) <= DAP_UART_TX_BUFFER_SIZE) {
  554. memcpy(&UartTxBuf[index], tx_data, tx_cnt);
  555. } else {
  556. num = DAP_UART_TX_BUFFER_SIZE - index;
  557. memcpy(&UartTxBuf[index], tx_data, num);
  558. memcpy(&UartTxBuf[0], &tx_data[num], tx_cnt - num);
  559. }
  560. UartTxIndexI += tx_cnt;
  561. if (UartTransmitActive == 0U) {
  562. UART_Transmit();
  563. }
  564. status = UART_Get_Status();
  565. }
  566. *response++ = status;
  567. *response++ = (uint8_t)(tx_cnt >> 0);
  568. *response++ = (uint8_t)(tx_cnt >> 8);
  569. *response++ = (uint8_t)(rx_cnt >> 0);
  570. *response = (uint8_t)(rx_cnt >> 8);
  571. return (((4U + tx_cnt) << 16) | (5U + rx_cnt));
  572. }
  573. #endif /* DAP_UART */