[DemoCode下载] IO软件模拟串口

[复制链接]
677|4
 楼主| antusheng 发表于 2020-3-26 20:39 | 显示全部楼层 |阅读模式
EC_M051_SoftwareUART_V1.00.zip (2.58 MB, 下载次数: 17)


 楼主| antusheng 发表于 2020-3-26 20:49 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     SoftwareUARRT.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 Using GPIO emulation UART driver source file
  5. *
  6. * @note
  7. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  8. ******************************************************************************/
  9. #include "SoftwareUART.h"

  10. /*---------------------------------------------------------------------------------------------------------*/
  11. /* Global variables                                                                                        */
  12. /*---------------------------------------------------------------------------------------------------------*/
  13. sUART_Config  g_sSoftwareUART_Config;
  14. sUART_Config *g_psSoftwareUART_Config;
  15. uint16_t g_au16TxBuff[256];
  16. uint16_t g_au16RxBuff[256];

  17. /*---------------------------------------------------------------------------------------------------------*/
  18. /* Init SoftwareUART                                                                                       */
  19. /*---------------------------------------------------------------------------------------------------------*/
  20. void SoftwareUART_Init(sUART_Config *psConfig)
  21. {
  22.     /* Configure P2.6 as Input mode(Rx) */
  23.     GPIO_SetMode(P2, BIT6, GPIO_PMD_INPUT);
  24.     /* Configure P2.7 as Output mode(Tx) */
  25.     GPIO_SetMode(P2, BIT7, GPIO_PMD_OUTPUT);
  26.     /* Initial the SoftwareUART configuration setting */
  27.     SoftwareUART_ConfigInit(psConfig);
  28.        
  29.     /* Configure SoftwareUART and set SoftwareUART baudrate */
  30.     SoftwareUART_Open(psConfig,115200);
  31.        
  32. }

  33. /*---------------------------------------------------------------------------------------------------------*/
  34. /* Init SoftwareUART configuration                                                                         */
  35. /*---------------------------------------------------------------------------------------------------------*/
  36. void SoftwareUART_ConfigInit(sUART_Config *psConfig)
  37. {
  38.   
  39.         g_psSoftwareUART_Config = psConfig;
  40.         psConfig->u32BaudRate = 0;
  41.         psConfig->u8DataBits = 0;
  42.         psConfig->u8Parity = 0;
  43.         psConfig->u8StopBit = 0;
  44.         psConfig->u8RxStatus = eRxIdel;
  45.         psConfig->u8TxStatus = eTxIdel;
  46.         psConfig->u8CommunBitsLen = 0;
  47.         psConfig->pu16TxBuff = &g_au16TxBuff[0];
  48.         psConfig->pu16RxBuff = &g_au16RxBuff[0];
  49.         psConfig->u32SendLen = 0;
  50.         psConfig->u32ReceiveLen = 0;

  51. }

  52. /*---------------------------------------------------------------------------------------------------------*/
  53. /* Init SoftwareUART interface                                                                             */
  54. /*---------------------------------------------------------------------------------------------------------*/
  55. void SoftwareUART_Open(sUART_Config *psConfig,uint32_t u32BaudRate)
  56. {
  57.           uint32_t u32TimePeriodic;
  58.        
  59.           psConfig->u32BaudRate = u32BaudRate;
  60.        
  61.           /* Setting the UART Rx bit rate */
  62.           u32TimePeriodic = psConfig->u32BaudRate*2;
  63.           /* Open Timer0 frequency in periodic mode, and enable interrupt */
  64.           TIMER_Open(TIMER0,TIMER_PERIODIC_MODE,u32TimePeriodic);
  65.           /* Enable Timer0 INT */
  66.           TIMER_EnableInt(TIMER0);

  67.           /* Setting the UART Tx bit rate */
  68.           u32TimePeriodic  =  psConfig->u32BaudRate;  
  69.           /* Open Timer0 frequency in periodic mode, and enable interrupt */
  70.           TIMER_Open(TIMER1,TIMER_PERIODIC_MODE,u32TimePeriodic);
  71.           /* Enable Timer0 INT */
  72.           TIMER_EnableInt(TIMER1);

  73.           SoftwareUART_DataFormartConfig(psConfig);
  74.        
  75. }       

  76. /*---------------------------------------------------------------------------------------------------------*/
  77. /* SoftwareUART data format setting                                                                        */
  78. /*---------------------------------------------------------------------------------------------------------*/
  79. void SoftwareUART_DataFormartConfig(sUART_Config *psConfig)
  80. {
  81.        
  82.         psConfig->u8DataBits = 8;                /* Setting data length is 8 bits */
  83.         psConfig->u8Parity = PARITY_NONE;        /* No Parity */
  84.         psConfig->u8StopBit = STOP_BIT_1;        /* 1 Stop bit */
  85.         psConfig->u8TxLatency = 2;               /* Transmission interval is 2 bits rate */
  86.         psConfig->u32SendLen = 16;               /* Send 16 bytes(Tx) */
  87.         psConfig->u32ReceiveLen = 16;            /* Receive 16 bytes(Rx) */
  88.         /*Building UART Tx serial number*/
  89.         BuildTxDataPattern(psConfig);
  90.         /* Configure the parity bit */
  91.         SoftwareUART_ParityBitConfig(psConfig);
  92.         /* Configure the stop bit */
  93.         SoftwareUART_StopBitConfig(psConfig);

  94.         psConfig->u8CommunBitsLen = psConfig->u8DataBits;

  95.   if(psConfig->u8Parity != PARITY_NONE)
  96.     psConfig->u8CommunBitsLen += 1;     /* Parity bit */
  97.   if(psConfig->u8StopBit != STOP_BIT_1)  
  98.     psConfig->u8CommunBitsLen += 3;    /* Start bit + 2 Stop bits */
  99.   else
  100.     psConfig->u8CommunBitsLen += 2;    /* Start bit + 1 Stop bit*/

  101. }
  102. /*---------------------------------------------------------------------------------------------------------*/
  103. /* SoftwareUART Tx Send Data                                                                               */
  104. /*---------------------------------------------------------------------------------------------------------*/
  105. void SoftwareUART_SendData(sUART_Config *psConfig)
  106. {
  107.    uint32_t u32Index;
  108.          uint32_t u16SendData;
  109.    /* Enable Timer1 NVIC */
  110.    NVIC_EnableIRQ(TMR1_IRQn);
  111.    /* Start Timer1 counting */
  112.    TIMER_Start(TIMER1);

  113.          psConfig->u8TxStatus = eTxIdel;
  114.          psConfig->u8TxTiming =0;
  115.    for(u32Index=0 ;u32Index < psConfig->u32SendLen ;u32Index++)
  116.    {
  117.                 GPIO_UART_TX = 1;
  118.                 u16SendData = psConfig->pu16TxBuff[u32Index];
  119.                 while( psConfig->u8TxStatus != eTxActive){};
  120.                 psConfig->u8TxStatus = eTxSendData;
  121.           
  122.                 /* Use TIMER1 to emulate the bit rate of UART transmission */
  123.                 do{       
  124.         psConfig->u8TxTiming =0;

  125.         if((u16SendData & 0x01) == 0x01)
  126.            GPIO_UART_TX =1;
  127.         else
  128.            GPIO_UART_TX =0;
  129.         u16SendData = u16SendData >> 1;
  130.         while(psConfig->u8TxTiming == 0){};
  131.        /* Total bits Length(start bit(1) + data bits(5~8) + parity bit(0~1)+ stop bit(1~2)) */
  132.       }while(psConfig->u8TxSendCount <  (psConfig->u8CommunBitsLen+ psConfig->u8TxLatency ));
  133.                
  134.                  psConfig->u8TxStatus = eTxIdel;
  135.                  psConfig->u8TxSendCount =0;
  136.          
  137.          }               
  138.                        
  139.          /* Disable Timer1 NVIC */
  140.           NVIC_DisableIRQ(TMR1_IRQn);
  141.           /* Stop Timer1 counting */
  142.           TIMER_Stop(TIMER1);
  143. }       
  144. /*---------------------------------------------------------------------------------------------------------*/
  145. /* SoftwareUART Rx Receive Data                                                                            */
  146. /*---------------------------------------------------------------------------------------------------------*/
  147. void SoftwareUART_ReadData(sUART_Config *psConfig)
  148. {
  149.     uint32_t u32Index;
  150.     uint8_t u8RxCount;
  151.     /* Enable Timer0 NVIC */
  152.     NVIC_EnableIRQ(TMR0_IRQn);
  153.     /* Start Timer0 counting */
  154.     TIMER_Start(TIMER0);

  155.     for(u32Index=0 ;u32Index < psConfig->u32ReceiveLen ;u32Index++)
  156.     {
  157.       u8RxCount = 0;
  158.       /* Use TIMER0 to emulate the bit rate of UART reception  */
  159.       psConfig->u8RxTiming =0;

  160.       /* Wait to Receive the start bit */
  161.       while(GPIO_UART_RX == 1){};
  162.       psConfig->u8RxStatus = eRxRcvData;                                 
  163.         do{

  164.       while(psConfig->u8RxTiming == 0){};
  165.       if(GPIO_UART_RX == 0)
  166.         psConfig->pu16RxBuff[u32Index] |= (0 << u8RxCount);
  167.       else
  168.         psConfig->pu16RxBuff[u32Index] |= (1 << u8RxCount);
  169.       psConfig->u8RxTiming =0;
  170.       while(psConfig->u8RxTiming == 0){};
  171.       psConfig->u8RxTiming =0;
  172.       u8RxCount++;
  173.       /* Total bits length(start bit(1) + data bits(5~8) + parity bit(0-1)+ stop bit(1~2)) */
  174.      }while(u8RxCount < psConfig->u8CommunBitsLen);
  175.      u8RxCount =0;
  176.                  /* End of reception */
  177.      psConfig->u8RxStatus = eRxIdel;
  178.      psConfig->u8RxTiming =0;

  179.           }

  180.      /* Disable Timer0 NVIC */
  181.      NVIC_DisableIRQ(TMR0_IRQn);
  182.      /* Stop Timer0 counting */
  183.      TIMER_Stop(TIMER0);
  184.           
  185.          
  186.          for(u32Index=0 ;u32Index < psConfig->u32ReceiveLen ;u32Index++)
  187.          {
  188.          SoftwareUART_ReceiveDateParse(psConfig,u32Index);
  189.          psConfig->pu16RxBuff[u32Index] = 0;
  190.          }
  191.          
  192. }       

  193. /*---------------------------------------------------------------------------------------------------------*/
  194. /* Parse reduction receive data                                                                            */
  195. /*---------------------------------------------------------------------------------------------------------*/
  196. void SoftwareUART_ReceiveDateParse(sUART_Config *psConfig,uint32_t u32Index)
  197. {
  198.    uint8_t u8ParityCheck;
  199.          uint8_t u8TempData;
  200.          uint8_t u8DataMask;
  201.          uint8_t u8ParityBit;
  202.          uint8_t u8StopBit;

  203.     /*Get the Parity bits number*/
  204.     u8DataMask = GetDataMsk(psConfig);

  205.     /*Check start bit*/
  206.     if((psConfig->pu16RxBuff[u32Index] & 0x01) !=0)
  207.     {
  208.        printf("Start Bit Error\n");
  209.     }
  210.                  /*Remove start bit */
  211.     u8TempData = ((psConfig->pu16RxBuff[u32Index]>>1) & u8DataMask);

  212.     if(psConfig->u8Parity != PARITY_NONE)
  213.     {
  214.       /*Get the parity number */
  215.       u8ParityCheck = GetParityBitsNumber(psConfig,u8TempData);
  216.       /*Get Parity bit*/
  217.       u8ParityBit = ((psConfig->pu16RxBuff[u32Index] >> (psConfig->u8DataBits+1)) &0x1);
  218.     }               
  219.     /*Check EVEN or ODD Parity bit*/
  220.     if( (psConfig->u8Parity == PARITY_EVEN ) || (psConfig->u8Parity == PARITY_ODD ) )
  221.           {
  222.        if(psConfig->u8Parity == PARITY_ODD)
  223.        {
  224.           if((u8ParityCheck % 2) == 0)
  225.           {
  226.             if(u8ParityBit != 1) printf("ODD Error\n");
  227.           }
  228.           else
  229.           {
  230.             if(u8ParityBit == 1)  printf("ODD Error\n");
  231.           }
  232.        }
  233.        else
  234.        {
  235.          if((u8ParityCheck % 2) == 0)
  236.          {
  237.             if(u8ParityBit != 0) printf("Even Error\n");
  238.          }
  239.          else
  240.          {
  241.             if(u8ParityBit != 1) printf("Even Error\n");
  242.          }
  243.        }
  244.    
  245.    }/*Check MARK Parity bit*/
  246.    else if(psConfig->u8Parity == PARITY_MARK)
  247.    {
  248.       if(u8ParityBit != 1) printf("MARK Error\n");
  249.    }/*Check Space Parity bit*/
  250.    else if(psConfig->u8Parity == PARITY_SPACE)
  251.    {
  252.       if(u8ParityBit != 0) printf("SPACE Error\n");
  253.    }

  254.     /*Check Stop bit*/
  255.      if(psConfig->u8Parity == PARITY_NONE)
  256.         u8StopBit = (psConfig->pu16TxBuff[u32Index] >> (psConfig->u8DataBits + 1));
  257.      else
  258.         u8StopBit = (psConfig->pu16TxBuff[u32Index] >> (psConfig->u8DataBits + 2));

  259.     if(psConfig->u8StopBit == STOP_BIT_1 )
  260.     {
  261.       if(u8StopBit != 1)printf("STOP bit Error\n");
  262.     }
  263.     else
  264.     {
  265.       if(u8StopBit != 3)printf("STOP bits Error\n");
  266.     }
  267.    
  268.     psConfig->pu16RxBuff[u32Index] = u8TempData;
  269.     printf("Rx Data[%d] =0x%x\n",u32Index,psConfig->pu16RxBuff[u32Index]) ;                
  270.                          
  271. }       


  272. uint8_t GetDataMsk(sUART_Config *psConfig)
  273. {
  274.    uint8_t u8DataMask;
  275.        
  276.         if(psConfig->u8DataBits == 8) u8DataMask = 0xFF;            /* 8 bits (0x0~0xFF) */
  277.         else if(psConfig->u8DataBits == 7) u8DataMask = 0x7F;       /* 7 bits (0x0~0x7F) */
  278.         else if(psConfig->u8DataBits == 6) u8DataMask = 0x3F;       /* 6 bits (0x0~0x3F) */
  279.         else u8DataMask = 0x1F;                                     /* 5 bits (0x0~0x1F) */

  280.         return u8DataMask;
  281. }


  282. uint8_t GetParityBitsNumber(sUART_Config *psConfig,uint8_t u8Data)
  283. {
  284.     uint8_t u8Index,u8ParityCheck;
  285.           u8ParityCheck =0;
  286.     for(u8Index = 0;u8Index < psConfig->u8DataBits ; u8Index++)       
  287.     {
  288.        if((u8Data & (1 << u8Index)) != 0)
  289.            u8ParityCheck++;
  290.     }

  291.         return u8ParityCheck;         
  292.                
  293. }       


  294. void BuildTxDataPattern(sUART_Config *psConfig)
  295. {
  296.   uint32_t u32Index;
  297.         uint8_t u8DataMask;

  298.   u8DataMask = GetDataMsk(psConfig);
  299.   for(u32Index =0 ; u32Index  < psConfig->u32SendLen ;u32Index++)
  300.   {
  301.      psConfig->pu16TxBuff[u32Index] = (u32Index & u8DataMask);
  302.   }
  303. }
  304. /*---------------------------------------------------------------------------------------------------------*/
  305. /* SoftwareUART Stop bit configuration                                                                     */
  306. /*---------------------------------------------------------------------------------------------------------*/

  307. void SoftwareUART_StopBitConfig(sUART_Config *psConfig)
  308. {
  309.    uint32_t u32Index;
  310.    for(u32Index =0 ; u32Index < psConfig->u32SendLen ; u32Index++)
  311.    {
  312.      if(psConfig->u8StopBit == STOP_BIT_1 )
  313.      {       
  314.         /* No Parity bit,offset value(start bit + data bits) */
  315.         if(psConfig->u8Parity == PARITY_NONE)
  316.            psConfig->pu16TxBuff[u32Index] |= (0x01 << (psConfig->u8DataBits + 1));
  317.         else /* offset offset value(start bit + data bits + parity bit) */
  318.            psConfig->pu16TxBuff[u32Index] |= (0x01 << (psConfig->u8DataBits + 2));
  319.      }
  320.      else
  321.      {
  322.         /* No Parity bit,offset value(start bit + data bits) */
  323.         if(psConfig->u8Parity == PARITY_NONE)
  324.            psConfig->pu16TxBuff[u32Index] |= (0x03 << (psConfig->u8DataBits + 1));
  325.         else /* offset value (start bit + data bits + parity bit) */
  326.            psConfig->pu16TxBuff[u32Index] |= (0x03 << (psConfig->u8DataBits + 2));
  327.      }
  328.    }
  329.          
  330. }       

  331. /*---------------------------------------------------------------------------------------------------------*/
  332. /* SoftwareUART Parity bit configuration                                                                   */
  333. /*---------------------------------------------------------------------------------------------------------*/
  334. void SoftwareUART_ParityBitConfig(sUART_Config *psConfig)
  335. {
  336.   uint8_t u8ParityCheck;
  337.         uint32_t u32DatIndex;

  338.    for(u32DatIndex =0 ; u32DatIndex < psConfig->u32SendLen ; u32DatIndex++)
  339.    {
  340.      if((psConfig->u8Parity == PARITY_EVEN ) || (psConfig->u8Parity == PARITY_ODD ) )
  341.                  {
  342.       
  343.        u8ParityCheck = GetParityBitsNumber(psConfig,(uint8_t)psConfig->pu16TxBuff[u32DatIndex]);
  344.       
  345.        if(psConfig->u8Parity == PARITY_ODD)
  346.        {
  347.          if((u8ParityCheck % 2) == 0)
  348.              psConfig->pu16TxBuff[u32DatIndex] = (psConfig->pu16TxBuff[u32DatIndex]<< 1) | (0x01 << (psConfig->u8DataBits+1));
  349.          else
  350.              psConfig->pu16TxBuff[u32DatIndex] = (psConfig->pu16TxBuff[u32DatIndex]<< 1);
  351.        }
  352.        else
  353.        {
  354.          if((u8ParityCheck % 2) == 1)
  355.              psConfig->pu16TxBuff[u32DatIndex] = (psConfig->pu16TxBuff[u32DatIndex]<< 1)|(0x01 << (psConfig->u8DataBits+1));
  356.          else
  357.              psConfig->pu16TxBuff[u32DatIndex] = (psConfig->pu16TxBuff[u32DatIndex]<< 1);
  358.        }
  359.     }
  360.     else if(psConfig->u8Parity == PARITY_MARK)
  361.     {
  362.             psConfig->pu16TxBuff[u32DatIndex] = (psConfig->pu16TxBuff[u32DatIndex]<< 1)|(0x01 << (psConfig->u8DataBits+1));
  363.     }
  364.     else
  365.     {
  366.             psConfig->pu16TxBuff[u32DatIndex] = (psConfig->pu16TxBuff[u32DatIndex]<< 1);
  367.     }
  368.    
  369.        
  370. }

  371. }

  372. /**
  373. * @brief       Timer0 IRQ
  374. *
  375. * @param       None
  376. *
  377. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  378. *
  379. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The Timer0 default IRQ, declared in startup_M051Series.s.
  380. */
  381. void TMR0_IRQHandler(void)
  382. {
  383.     /* Clear Timer0 time-out interrupt flag */
  384.     TIMER_ClearIntFlag(TIMER0);
  385.     /* Receiving data */
  386.     if( g_psSoftwareUART_Config->u8RxStatus == eRxRcvData)
  387.         g_psSoftwareUART_Config->u8RxTiming = 1;
  388.   
  389.        
  390. }

  391. /**
  392. * @brief       Timer1 IRQ
  393. *
  394. * @param       None
  395. *
  396. * @return      None
  397. *
  398. * @details     The Timer1 default IRQ, declared in startup_M051Series.s.
  399. */
  400. void TMR1_IRQHandler(void)
  401. {
  402.     /* Clear Timer1 time-out interrupt flag */
  403.      TIMER_ClearIntFlag(TIMER1);

  404.      g_psSoftwareUART_Config->u8TxSendCount++;
  405.       /*Wait for the Tx transfer start bit to complete*/
  406.      if(g_psSoftwareUART_Config->u8TxStatus == eTxIdel)
  407.      {
  408.         if(g_psSoftwareUART_Config->u8TxSendCount == g_psSoftwareUART_Config->u8TxLatency)
  409.            g_psSoftwareUART_Config->u8TxStatus = eTxActive;
  410.      }/* Tx transfer timing */
  411.      else if(g_psSoftwareUART_Config->u8TxStatus == eTxSendData)
  412.                          g_psSoftwareUART_Config->u8TxTiming = 1;
  413.                  
  414. }

  415. /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/

 楼主| antusheng 发表于 2020-3-26 20:50 | 显示全部楼层
  1. #ifndef __SOFTWAREUART_H__
  2. #define __SOFTWAREUART_H__

  3. #include "stdio.h"
  4. #include "stdlib.h"
  5. #include "M051Series.h"
  6. #include "string.h"

  7. /*---------------------------------------------------------------------------------------------------------*/
  8. /* Define                                                                                                  */
  9. /*---------------------------------------------------------------------------------------------------------*/

  10. /* Set to use GPIO emulation UART transfer */
  11. #define GPIO_UART_TX       P27   
  12. #define GPIO_UART_RX       P26

  13. /*Define the UART Parity mode*/
  14. #define PARITY_NONE        0
  15. #define PARITY_ODD         1
  16. #define PARITY_EVEN        2
  17. #define PARITY_MARK        3
  18. #define PARITY_SPACE       4

  19. /*Define the UART STOP bit*/
  20. #define STOP_BIT_1         0
  21. #define STOP_BITS_2        1


  22. /*---------------------------------------------------------------------------------------------------------*/
  23. /* Global variables                                                                                        */
  24. /*---------------------------------------------------------------------------------------------------------*/

  25. /*SoftwareUART Rx status enumeration*/
  26. typedef enum
  27. {
  28. eRxIdel = 0, /*Wait receive start bit*/
  29. eRxActive,   /*Receive start bit */
  30. eRxRcvData,  /*Receiving data*/          
  31. }E_RX_STATUS;

  32. /*SoftwareUART Tx status enumeration*/
  33. typedef enum
  34. {
  35. eTxIdel = 0, /*Wait send start bit*/
  36. eTxActive,   /*Send start bit*/
  37. eTxSendData, /*Sending data*/        
  38. }E_TX_STATUS;

  39. /*SoftwareUART communication struct*/
  40. typedef  struct _Config{
  41. uint32_t u32BaudRate;      /* Setting the UART Bit Rate*/
  42. uint8_t  u8DataBits;       /* UART Data bits Length(5-8 bits) */
  43. uint8_t  u8Parity;         /* UART Parity(NONE,EVEN,ODD,SPACE,MARK)*/
  44. uint8_t  u8StopBit;        /* 1 bit,2 bits */
  45. uint8_t         u8CommunBitsLen;  /* for Software UART Tx/Rx Communication bits Length using */
  46. uint8_t  u8TxSendCount;          /* Tx Send Data Timing Counter */
  47. uint8_t  u8TxLatency;      /* Transmission interval*/
  48. uint8_t  u8TxStatus;       /* UART Tx Status*/
  49. uint8_t  u8TxTiming;              /* UART Tx Bit rate timing */
  50. uint8_t  u8RxStatus;              /* UART Rx Status*/
  51. uint8_t  u8RxTiming;              /* UART Rx Bit rate timing */
  52. uint16_t *pu16TxBuff;      /* UART Tx Buffer pointer */
  53. uint16_t *pu16RxBuff;      /* UART Rx Buffer pointer */
  54. uint32_t        u32SendLen;       /* UART Tx transfer total length*/
  55. uint32_t        u32ReceiveLen;          /* UART Tx receive total length*/
  56. }sUART_Config;

  57. extern sUART_Config  g_sSoftwareUART_Config;
  58. extern sUART_Config *g_psSoftwareUART_Config;

  59. /*---------------------------------------------------------------------------------------------------------*/
  60. /* SoftwareUART Function                                                                                   */
  61. /*---------------------------------------------------------------------------------------------------------*/
  62. void SoftwareUART_ParityBitConfig(sUART_Config *spConfig);
  63. void SoftwareUART_StopBitConfig(sUART_Config *spConfig);
  64. void SoftwareUART_DataFormartConfig(sUART_Config *psConfig);
  65. void BuildTxDataPattern(sUART_Config *psConfig);
  66. void SoftwareUART_ConfigInit(sUART_Config *psConfig);
  67. void SoftwareUART_Open(sUART_Config *psConfig,uint32_t u32BaudRate);
  68. void SoftwareUART_SendData(sUART_Config *psConfig) ;
  69. void SoftwareUART_ReadData(sUART_Config *psConfig) ;
  70. uint8_t GetParityBitsNumber(sUART_Config *psConfig,uint8_t u8Data);
  71. void SoftwareUART_ReceiveDateParse(sUART_Config *psConfig,uint32_t u32Index);
  72. uint8_t GetDataMsk(sUART_Config *psConfig);

  73. extern void SoftwareUART_Init(sUART_Config *psConfig);
  74. extern void UART_TX_TestFunctoion(sUART_Config *psConfig);
  75. extern void UART_RX_TestFunctoion(sUART_Config *psConfig);
  76. extern void SoftwareUART_SendData(sUART_Config *psConfig) ;
  77. extern void SoftwareUART_ReadData(sUART_Config *psConfig) ;


  78. #endif  /* __SOFTWAREUART_H__*/
 楼主| antusheng 发表于 2020-3-26 20:58 | 显示全部楼层
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V1.00
  4. * @brief    M051 Simulate UART transfers using GPIO Sample Code
  5. *
  6. * @note
  7. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  8. ******************************************************************************/
  9. #include <stdio.h>
  10. #include "M051Series.h"
  11. #include "SoftwareUART.h"

  12. #define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
  13. #define PLL_CLOCK           50000000


  14. void SYS_Init(void)
  15. {
  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Init System Clock                                                                                       */
  18.     /*---------------------------------------------------------------------------------------------------------*/
  19.    
  20.           /* Enable IRC22M clock */
  21.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
  22.        
  23.     /* Waiting for IRC22M clock ready */
  24.     CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk);

  25.     /* Enable external 12MHz XTAL clock ready */
  26.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  27.     /* Waiting for 12MHz XTAL clock ready */
  28.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  29.     /* Enable external IRC10K */
  30.     CLK_EnableXtalRC(CLK_PWRCON_OSC10K_EN_Msk);
  31.    
  32.     /* Waiting for IRC10K clock ready */
  33.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC10K_STB_Msk);
  34.                
  35.     /* Set core clock as PLL_CLOCK from PLL */
  36.     CLK_SetCoreClock(PLL_CLOCK);

  37.     /* Switch HCLK clock source to HIRC */
  38.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_PLL, CLK_CLKDIV_HCLK(1));

  39.     /* Enable peripheral clock */
  40.     /* Enable UART0 module clock */
  41.     CLK_EnableModuleClock(UART0_MODULE);
  42.     /* Enable TIMER0 module clock */
  43.     CLK_EnableModuleClock(TMR0_MODULE);
  44.     /* Enable TIMER1 module clock */
  45.     CLK_EnableModuleClock(TMR1_MODULE);
  46.                
  47.     /* Peripheral clock source */
  48.        
  49.     /* Select UART module clock source */
  50.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
  51.     /* Select TMR0 module clock source */
  52.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT,0);
  53.     /* Select TMR1 module clock source */
  54.     CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1_S_HXT,0);

  55.     /* Update System Core Clock */
  56.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  57.     SystemCoreClockUpdate();

  58.     /*---------------------------------------------------------------------------------------------------------*/
  59.     /* Init I/O Multi-function                                                                                 */
  60.     /*---------------------------------------------------------------------------------------------------------*/
  61.     /* Set P3 multi-function pins for UART0 RXD, TXD */
  62.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;
  63. }

  64. void SoftwareUART_TestItem(void)
  65. {

  66.     printf("+--------------------------------------------------------+\n");
  67.     printf("|           M051 SoftwareUART Transmission               |\n");
  68.     printf("+--------------------------------------------------------+\n");
  69.     printf("| [1] Software UART TX                                   |\n");
  70.     printf("| [2] Software UART RX                                   |\n");
  71.     printf("+--------------------------------------------------------+\n");
  72.     printf("Please Enter any Key to Start: ");
  73. }       
  74. /*---------------------------------------------------------------------------------------------------------*/
  75. /* UART0 Init Function                                                                                     */
  76. /*---------------------------------------------------------------------------------------------------------*/
  77. void UART0_Init(void)
  78. {
  79.     /* Reset IP */
  80.     SYS_ResetModule(UART0_RST);

  81.     /* Configure UART0 and set UART0 Baudrate */
  82.     UART_Open(UART0, 115200);
  83. }
  84. /*---------------------------------------------------------------------------------------------------------*/
  85. /* Simulate UART Tx transfers using GPIO                                                                   */
  86. /*---------------------------------------------------------------------------------------------------------*/
  87. void UART_TX_TestFunctoion(sUART_Config *psConfig)
  88. {
  89.    printf("Transfer %d-byte serial number\n",psConfig->u32SendLen);
  90.    SoftwareUART_SendData(psConfig);
  91. }       

  92. /*---------------------------------------------------------------------------------------------------------*/
  93. /*  Receive data using GPIO emulation UART Rx                                                              */
  94. /*---------------------------------------------------------------------------------------------------------*/
  95. void UART_RX_TestFunctoion(sUART_Config *psConfig)
  96. {       
  97.    printf("Waiting to receive %d-byte\n",psConfig->u32ReceiveLen);
  98.    SoftwareUART_ReadData(psConfig);
  99. }       


  100. /*---------------------------------------------------------------------------------------------------------*/
  101. /*  MAIN function                                                                                          */
  102. /*---------------------------------------------------------------------------------------------------------*/
  103. int main(void)
  104. {
  105.     uint8_t u8TestItem;
  106.     /* Unlock protected registers */
  107.     SYS_UnlockReg();

  108.     /* Init System, peripheral clock and multi-function I/O */
  109.     SYS_Init();

  110.     /* Lock protected registers */
  111.     SYS_LockReg();
  112.    
  113.     /* Init UART0 for printf */
  114.     UART0_Init();
  115.           /* Init SoftwareUART  */
  116.     SoftwareUART_Init(&g_sSoftwareUART_Config);
  117.     printf("\nSystem Clock = %d Hz\n",SystemCoreClock);
  118.    
  119.    while(1)
  120.    {

  121.     SoftwareUART_TestItem();
  122.     u8TestItem = getchar();
  123.     printf("%c\n",u8TestItem);
  124.    
  125.      switch(u8TestItem)
  126.      {
  127.        case '1':
  128.                UART_TX_TestFunctoion(g_psSoftwareUART_Config);
  129.               break;
  130.        case '2':
  131.                UART_RX_TestFunctoion(g_psSoftwareUART_Config);
  132.               break;
  133.        default:
  134.               break;                                  
  135.      }
  136.     }
  137. }

  138. /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/

huangcunxiake 发表于 2020-3-26 22:31 | 显示全部楼层

前来观摩学习。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

86

主题

1723

帖子

5

粉丝
快速回复 在线客服 返回列表 返回顶部