1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
|
#ifndef __UART_ATY_H #define __UART_ATY_H
#include "stm32f1xx_hal.h" #include "stdio.h" #include "string.h"
#define UART1_NIT_ATY
#define UART1_GET_SIZE 4 #define UART1_SEND_SIZE 8
#define UART2_NIT_ATY
#define UART2_GET_SIZE 11 #define UART2_SEND_SIZE 8
#define UART3_NIT_ATY
#define UART3_GET_SIZE 13 #define UART3_SEND_SIZE 255
#ifdef UART1_NIT_ATY uint8_t uart1GetByte=0; uint8_t uart1GetCount=0; uint8_t uart1GetStr[UART1_GET_SIZE]; uint8_t uart1SendStr[UART1_SEND_SIZE]; #endif
#ifdef UART2_NIT_ATY uint8_t uart2GetByte=0; uint8_t uart2GetCount=0; uint8_t uart2GetStr[UART2_GET_SIZE]; uint8_t uart2SendStr[UART2_SEND_SIZE]; #endif
#ifdef UART3_NIT_ATY uint8_t uart3GetByte=0; uint8_t uart3GetCount=0; uint8_t uart3GetStr[UART3_GET_SIZE]; uint8_t uart3SendStr[UART3_SEND_SIZE]; #endif
#ifdef UART4_NIT_ATY uint8_t uart4GetByte=0; uint8_t uart4GetCount=0; uint8_t uart4GetStr[UART4_GET_SIZE]; uint8_t uart4SendStr[UART4_SEND_SIZE]; #endif
#ifdef UART5_NIT_ATY uint8_t uart5GetByte=0; uint8_t uart5GetCount=0; uint8_t uart5GetStr[UART5_GET_SIZE]; uint8_t uart5SendStr[UART5_SEND_SIZE]; #endif
#ifdef UART1_DMA_ATY uint8_t uart1GetByte=0; uint8_t uart1GetCount=0; uint8_t uart1GetStr[UART1_GET_SIZE]; uint8_t uart1SendStr[UART1_SEND_SIZE]; uint16_t uart1DmaSendFlag=0; volatile uint8_t uart1RecvEndFlag=0; static __IO ITStatus uart1Ready=RESET; extern DMA_HandleTypeDef hdma_usart1_rx; #endif
#ifdef UART2_DMA_ATY uint8_t uart2GetByte=0; uint8_t uart2GetCount=0; uint8_t uart2GetStr[UART2_GET_SIZE]; uint8_t uart2SendStr[UART2_SEND_SIZE]; uint16_t uart2DmaSendFlag=0; volatile uint8_t uart2RecvEndFlag=0; static __IO ITStatus uart2Ready=RESET; extern DMA_HandleTypeDef hdma_usart2_rx; #endif
#ifdef UART3_DMA_ATY uint8_t uart3GetByte=0; uint8_t uart3GetCount=0; uint8_t uart3GetStr[UART3_GET_SIZE]; uint8_t uart3SendStr[UART3_SEND_SIZE]; uint16_t uart3DmaSendFlag=0; volatile uint8_t uart3RecvEndFlag=0; static __IO ITStatus uart3Ready=RESET; extern DMA_HandleTypeDef hdma_usart3_rx; #endif
#ifdef UART4_DMA_ATY uint8_t uart4GetByte=0; uint8_t uart4GetCount=0; uint8_t uart4GetStr[UART4_GET_SIZE]; uint8_t uart4SendStr[UART4_SEND_SIZE]; uint16_t uart4DmaSendFlag=0; volatile uint8_t uart4RecvEndFlag=0; static __IO ITStatus uart4Ready=RESET; extern DMA_HandleTypeDef hdma_usart4_rx; #endif
#ifdef UART5_DMA_ATY uint8_t uart5GetByte=0; uint8_t uart5GetCount=0; uint8_t uart5GetStr[UART5_GET_SIZE]; uint8_t uart5SendStr[UART5_SEND_SIZE]; uint16_t uart5DmaSendFlag=0; volatile uint8_t uart5RecvEndFlag=0; static __IO ITStatus uart5Ready=RESET; extern DMA_HandleTypeDef hdma_usart5_rx; #endif
void UartInit_ATY(void) { #ifdef UART1_NIT_ATY HAL_UART_Receive_IT(&huart1, &uart1GetByte, 1); #endif
#ifdef UART2_NIT_ATY HAL_UART_Receive_IT(&huart2, &uart2GetByte, 1); #endif
#ifdef UART3_NIT_ATY HAL_UART_Receive_IT(&huart3, &uart3GetByte, 1); #endif
#ifdef UART4_NIT_ATY HAL_UART_Receive_IT(&huart4, &uart4GetByte, 1); #endif
#ifdef UART5_NIT_ATY HAL_UART_Receive_IT(&huart5, &uart5GetByte, 1); #endif
#ifdef UART1_DMA_ATY __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); HAL_UART_Receive_DMA(&huart1, uart1GetStr, UART1_GET_SIZE); #endif
#ifdef UART2_DMA_ATY __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE); HAL_UART_Receive_DMA(&huart2, uart2GetStr, UART2_GET_SIZE); #endif
#ifdef UART3_DMA_ATY __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE); HAL_UART_Receive_DMA(&huart3, uart3GetStr, UART3_GET_SIZE); #endif
#ifdef UART4_DMA_ATY __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE); HAL_UART_Receive_DMA(&huart4, uart4GetStr, UART4_GET_SIZE); #endif
#ifdef UART5_DMA_ATY __HAL_UART_ENABLE_IT(&huart5, UART_IT_IDLE); HAL_UART_Receive_DMA(&huart5, uart5GetStr, UART5_GET_SIZE); #endif }
#ifdef UART1_NIT_ATY
void Uart1Send(uint8_t *pData, uint16_t Size) { HAL_UART_Transmit_IT(&huart1, (uint8_t*)pData, Size); }
void Uart1Recv(void) { if(uart1GetCount<UART1_GET_SIZE) { uart1GetStr[uart1GetCount++]=uart1GetByte; } else { uart1GetCount=0; } while(HAL_UART_Receive_IT(&huart1, &uart1GetByte, 1)==HAL_OK); } #endif
#ifdef UART2_NIT_ATY
void Uart2Send(uint8_t *pData, uint16_t Size) { HAL_UART_Transmit_IT(&huart2, (uint8_t*)pData, Size); }
void Uart2Recv(void) { if(uart2GetCount<UART2_GET_SIZE) { uart2GetStr[uart2GetCount++]=uart2GetByte; } else { uart2GetCount=0; } while(HAL_UART_Receive_IT(&huart2, &uart2GetByte, 1)==HAL_OK); } #endif
#ifdef UART3_NIT_ATY
void Uart3Send(uint8_t *pData, uint16_t Size) { HAL_UART_Transmit_IT(&huart3, (uint8_t*)pData, Size); }
void Uart3Recv(void) { if(uart3GetCount<UART3_GET_SIZE) { uart3GetStr[uart3GetCount++]=uart3GetByte; } else { uart3GetCount=0; } while(HAL_UART_Receive_IT(&huart3, &uart3GetByte, 1)==HAL_OK); } #endif
#ifdef UART4_NIT_ATY
void Uart4Send(uint8_t *pData, uint16_t Size) { HAL_UART_Transmit_IT(&huart4, (uint8_t*)pData, Size); }
void Uart4Recv(void) { if(uart4GetCount<UART4_GET_SIZE) { uart4GetStr[uart4GetCount++]=uart4GetByte; } else { uart4GetCount=0; } while(HAL_UART_Receive_IT(&huart4, &uart4GetByte, 1)==HAL_OK); } #endif
#ifdef UART5_NIT_ATY
void Uart5Send(uint8_t *pData, uint16_t Size) { HAL_UART_Transmit_IT(&huart5, (uint8_t*)pData, Size); }
void Uart5Recv(void) { if(uart5GetCount<UART5_GET_SIZE) { uart5GetStr[uart5GetCount++]=uart5GetByte; } else { uart5GetCount=0; } while(HAL_UART_Receive_IT(&huart5, &uart5GetByte, 1)==HAL_OK); } #endif
#if defined(UART1_NIT_ATY) \ || defined(UART2_NIT_ATY) \ || defined(UART3_NIT_ATY) \ || defined(UART4_NIT_ATY) \ || defined(UART5_NIT_ATY)
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { #ifdef UART1_NIT_ATY if(huart==&huart1) { Uart1Recv(); } #endif
#ifdef UART2_NIT_ATY if(huart==&huart2) { Uart2Recv(); } #endif
#ifdef UART3_NIT_ATY if(huart==&huart3) { Uart3Recv(); } #endif
#ifdef UART4_NIT_ATY if(huart==&huart4) { Uart4Recv(); } #endif
#ifdef UART5_NIT_ATY if(huart==&huart5) { Uart5Recv(); } #endif } #endif
#ifdef TEST_NIT_ATY
void UartControlTemplate(void) { HAL_UART_Receive_IT(&HUART_N, &uartByte, 1); if(uartBufCount>0) { if(memcmp(uartBuf, "Hello STM32F1!", 14)==0) { printf("Getted! - printf\n"); UartSend("Getted! - send\n", 15); uartBufCount=0; memset(uartBuf, 0, sizeof(uartBuf)); } } } #endif
#if defined(UART1_DMA_ATY) \ || defined(UART2_DMA_ATY) \ || defined(UART3_DMA_ATY) \ || defined(UART4_DMA_ATY) \ || defined(UART5_DMA_ATY)
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { #ifdef UART1_DMA_ATY if(huart==&huart1) uart1Ready=SET; #endif
#ifdef UART2_DMA_ATY if(huart==&huart2) uart2Ready=SET; #endif
#ifdef UART3_DMA_ATY if(huart==&huart3) uart3Ready=SET; #endif
#ifdef UART4_DMA_ATY if(huart==&huart4) uart4Ready=SET; #endif
#ifdef UART5_DMA_ATY if(huart==&huart5) uart5Ready=SET; #endif }
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { #ifdef UART1_DMA_ATY if(huart==&huart1) uart1Ready=SET; #endif
#ifdef UART2_DMA_ATY if(huart==&huart2) uart2Ready=SET; #endif
#ifdef UART3_DMA_ATY if(huart==&huart3) uart3Ready=SET; #endif
#ifdef UART4_DMA_ATY if(huart==&huart4) uart4Ready=SET; #endif
#ifdef UART5_DMA_ATY if(huart==&huart5) uart5Ready=SET; #endif } #endif
#ifdef UART1_DMA_ATY
void Uart1SendDMA(uint8_t *pData, uint16_t Size) { if(HAL_UART_Transmit_DMA(&huart1, pData, Size)!=HAL_OK) { Error_Handler(); } while(uart1Ready!=SET){} uart1Ready=RESET; uart1DmaSendFlag=0; }
void USART1_IRQHandler(void) { uint32_t tmpFlag=0; uint32_t temp; tmpFlag=__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE); if((tmpFlag!=RESET)) { __HAL_UART_CLEAR_IDLEFLAG(&huart1); temp=huart1.Instance->SR; temp=huart1.Instance->DR; HAL_UART_DMAStop(&huart1); temp=hdma_usart1_rx.Instance->CNDTR; uart1GetCount=UART1_GET_SIZE-temp; uart1RecvEndFlag=1; } HAL_UART_IRQHandler(&huart1); HAL_UART_Receive_DMA(&huart1, uart1GetStr, UART1_GET_SIZE); } #endif
#ifdef UART2_DMA_ATY
void Uart2SendDMA(uint8_t *pData, uint16_t Size) { if(HAL_UART_Transmit_DMA(&huart2, pData, Size)!=HAL_OK) { Error_Handler(); } while(uart2Ready!=SET){} uart2Ready=RESET; uart2DmaSendFlag=0; }
void USART2_IRQHandler(void) { uint32_t tmpFlag=0; uint32_t temp; tmpFlag=__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE); if((tmpFlag!=RESET)) { __HAL_UART_CLEAR_IDLEFLAG(&huart2); temp=huart2.Instance->SR; temp=huart2.Instance->DR; HAL_UART_DMAStop(&huart2); temp=hdma_usart2_rx.Instance->CNDTR; uart2GetCount=UART2_GET_SIZE-temp; uart2RecvEndFlag=1; } HAL_UART_IRQHandler(&huart2); HAL_UART_Receive_DMA(&huart2, uart2GetStr, UART2_GET_SIZE); } #endif
#ifdef UART3_DMA_ATY
void Uart3SendDMA(uint8_t *pData, uint16_t Size) { if(HAL_UART_Transmit_DMA(&huart3, pData, Size)!=HAL_OK) { Error_Handler(); } while(uart3Ready!=SET){} uart3Ready=RESET; uart3DmaSendFlag=0; }
void USART3_IRQHandler(void) { uint32_t tmpFlag=0; uint32_t temp; tmpFlag=__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE); if((tmpFlag!=RESET)) { __HAL_UART_CLEAR_IDLEFLAG(&huart3); temp=huart3.Instance->SR; temp=huart3.Instance->DR; HAL_UART_DMAStop(&huart3); temp=hdma_usart3_rx.Instance->CNDTR; uart3GetCount=UART3_GET_SIZE-temp; uart3RecvEndFlag=1; } HAL_UART_IRQHandler(&huart3); HAL_UART_Receive_DMA(&huart3, uart3GetStr, UART3_GET_SIZE); } #endif
#ifdef UART4_DMA_ATY
void Uart4SendDMA(uint8_t *pData, uint16_t Size) { if(HAL_UART_Transmit_DMA(&huart4, pData, Size)!=HAL_OK) { Error_Handler(); } while(uart4Ready!=SET){} uart4Ready=RESET; uart4DmaSendFlag=0; }
void USART4_IRQHandler(void) { uint32_t tmpFlag=0; uint32_t temp; tmpFlag=__HAL_UART_GET_FLAG(&huart4, UART_FLAG_IDLE); if((tmpFlag!=RESET)) { __HAL_UART_CLEAR_IDLEFLAG(&huart4); temp=huart4.Instance->SR; temp=huart4.Instance->DR; HAL_UART_DMAStop(&huart4); temp=hdma_usart4_rx.Instance->CNDTR; uart4GetCount=UART4_GET_SIZE-temp; uart4RecvEndFlag=1; } HAL_UART_IRQHandler(&huart4); HAL_UART_Receive_DMA(&huart4, uart4GetStr, UART4_GET_SIZE); } #endif
#ifdef UART5_DMA_ATY
void Uart5SendDMA(uint8_t *pData, uint16_t Size) { if(HAL_UART_Transmit_DMA(&huart5, pData, Size)!=HAL_OK) { Error_Handler(); } while(uart5Ready!=SET){} uart5Ready=RESET; uart5DmaSendFlag=0; }
void USART5_IRQHandler(void) { uint32_t tmpFlag=0; uint32_t temp; tmpFlag=__HAL_UART_GET_FLAG(&huart5, UART_FLAG_IDLE); if((tmpFlag!=RESET)) { __HAL_UART_CLEAR_IDLEFLAG(&huart5); temp=huart5.Instance->SR; temp=huart5.Instance->DR; HAL_UART_DMAStop(&huart5); temp=hdma_usart5_rx.Instance->CNDTR; uart5GetCount=UART5_GET_SIZE-temp; uart5RecvEndFlag=1; } HAL_UART_IRQHandler(&huart5); HAL_UART_Receive_DMA(&huart5, uart5GetStr, UART5_GET_SIZE); } #endif
#ifdef TEST_DMA_ATY
void UartControlTemplate(void) { if(uart1RecvEndFlag) { if(memcmp(uart1GetStr, "Hello STM32F1!", 14)==0) { printf("Getted! - printf\n"); Uart1SendDMA("Getted! - send\n", 15); memset(uart1GetStr, 0, sizeof(uart1GetStr)); uart1GetCount=0; } uart1RecvEndFlag=0; } } #endif
#endif
|