[活动专区] 【杰发科技AC7802x测评】linux下开发-实现串口空闲不定长数据收发

[复制链接]
676|0
 楼主| lulugl 发表于 2023-6-3 20:49 | 显示全部楼层 |阅读模式
【杰发科技AC7802x测评】linux下开发按键中断试验 - - 21ic电子技术开**坛
在这篇的基础之上,我们今天学习使用AC7802的串口收发。
1、把原来的工程复制一份到新的文件夹,并重命名为AC7802_UART工程。
2、新建Uart.h、Uart.c,内容如下:
  1. #ifndef __UART_H__
  2. #define __UART_H__
  3. #include "ac780x_uart.h"

  4. void UART_Cfg_Init(void);

  5. #endif
Uart.c
  1. #include <stdbool.h>
  2. #include "ac780x_gpio.h"
  3. #include "ac780x_uart_reg.h"
  4. #include "Uart.h"

  5. #define RX_BUF_LEN   100U

  6. uint8_t g_rxLen  = 0;                 /*!< 串口接收长度变量 */        
  7. uint8_t g_txLen  = 0;                 /*!< 串口发送长度变量 */
  8. uint8_t g_txCnt  = 0;
  9. uint8_t g_rxBuf[RX_BUF_LEN] = {0};    /*!< 串口接收数组 */     

  10. /*!
  11. * [url=home.php?mod=space&uid=247401]@brief[/url]   串口回调函数
  12. *
  13. * @param   void *device:UART_Type pointer
  14.             uint32_t wpara:UART lsr0 register
  15.             uint32_t lpara:UART lsr1 register
  16. * [url=home.php?mod=space&uid=266161]@return[/url]  none
  17. */
  18. void UART_Callback(void *device, uint32_t wpara, uint32_t lpara)
  19. {
  20.     UART_Type *Uart_Device = (UART_Type *)device;

  21.     /*! 串口接收中断 */
  22.     if(wpara & UART_LSR0_DR_Msk)
  23.     {
  24.         g_rxBuf[g_rxLen++] = UART_ReceiveData(Uart_Device);

  25.         if(g_rxLen > RX_BUF_LEN)
  26.         {
  27.             g_rxLen = 0;
  28.         }
  29.     }

  30.     /*!< 发送fifo空中断  */
  31.     if(Uart_Device->IER & UART_IER_ETXE_Msk)
  32.     {
  33.         UART_SendData(Uart_Device,g_rxBuf[g_txCnt++]);

  34.         if(g_txCnt >= g_txLen)
  35.         {
  36.             g_txCnt = 0;
  37.             UART_SetTXEInterrupt(Uart_Device, DISABLE);
  38.         }
  39.     }

  40.     /*!< 串口空闲中断 */
  41.     if(lpara & UART_LSR1_IDLE_Msk)
  42.     {
  43.         g_txLen = g_rxLen;   
  44.         g_rxLen = 0;

  45.         UART_SetTXEInterrupt(Uart_Device, ENABLE);
  46.     }
  47. }

  48. /*!
  49. * @brief   uart configuration init
  50. *
  51. * @param   none
  52. * @return  none
  53. */
  54. void UART_Cfg_Init(void)
  55. {
  56.     UART_ConfigType uart_config;

  57.     GPIO_SetFunc(GPIOA, GPIO_PIN4, GPIO_FUN3);    /*! uart tx */
  58.     GPIO_SetFunc(GPIOA, GPIO_PIN5, GPIO_FUN3);    /*! uart rx */

  59.     uart_config.baudrate   = 115200;              /*! 波特率115200 */
  60.     uart_config.dataBits   = UART_WORD_LEN_8BIT;  /*! 数据8bit */
  61.     uart_config.stopBits   = UART_STOP_1BIT;      /*! 停止位1bit */
  62.     uart_config.fifoByteEn = DISABLE;   
  63.     uart_config.sampleCnt  = UART_SMP_CNT0;       /*! 16倍采样 */
  64.     uart_config.callBack   = UART_Callback;       /*! 回调函数设置 */

  65.     UART_Init(UART1,&uart_config);                /*! 串口初始化 */

  66.     UART_SetRXNEInterrupt(UART1, ENABLE);         /*! 串口接收中断使能 */

  67.     UART_SetIdleFuncEn(UART1, ENABLE);
  68.     UART_SetIdleInterrupt(UART1, ENABLE);         /*! 使能串口空闲中断 */

  69.     NVIC_SetPriority(UART1_IRQn, 3);              /*! 串口中断优先级 */
  70.     NVIC_ClearPendingIRQ(UART1_IRQn);
  71.     NVIC_EnableIRQ(UART1_IRQn);
  72. }
3、修改makefile内容中的TARGET 为:AC7802_UART。去掉原来gpio.c,新建Uart.c,增加ac78x_uart.c:
  1. # C sources
  2. C_SOURCES =  \
  3. Src/main.c \
  4. Src/AC7802x_irq_cb.c \
  5. Src/AC7802x_msp.c \
  6. Src/system_AC7802x.c \
  7. Drivers/ATC_Driver/Src/ac780x_gpio.c \
  8. Drivers/ATC_Driver/Src/ac780x_ckgen.c \
  9. Drivers/ATC_Driver/Src/ac780x_spm.c  \
  10. Drivers/ATC_Driver/Src/ac780x_uart.c \
  11. User/Src/Uart.c
4、make结果如下:
  1. lugl@lugl-virtual-machine:~/ac7802/AC7802_UART$ make
  2. mkdir build
  3. arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb    -IInc -IDrivers/ATC_Driver/Inc -IDrivers/Device/Include -IDrivers/Device -IDrivers/Device/Include/CMSIS -IUser/Inc -O0 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/main.d" -Wa,-a,-ad,-alms=build/main.lst Src/main.c -o build/main.o
  4. arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb    -IInc -IDrivers/ATC_Driver/Inc -IDrivers/Device/Include -IDrivers/Device -IDrivers/Device/Include/CMSIS -IUser/Inc -O0 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/AC7802x_irq_cb.d" -Wa,-a,-ad,-alms=build/AC7802x_irq_cb.lst Src/AC7802x_irq_cb.c -o build/AC7802x_irq_cb.o
  5. arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb    -IInc -IDrivers/ATC_Driver/Inc -IDrivers/Device/Include -IDrivers/Device -IDrivers/Device/Include/CMSIS -IUser/Inc -O0 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/AC7802x_msp.d" -Wa,-a,-ad,-alms=build/AC7802x_msp.lst Src/AC7802x_msp.c -o build/AC7802x_msp.o
  6. arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb    -IInc -IDrivers/ATC_Driver/Inc -IDrivers/Device/Include -IDrivers/Device -IDrivers/Device/Include/CMSIS -IUser/Inc -O0 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/system_AC7802x.d" -Wa,-a,-ad,-alms=build/system_AC7802x.lst Src/system_AC7802x.c -o build/system_AC7802x.o
  7. arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb    -IInc -IDrivers/ATC_Driver/Inc -IDrivers/Device/Include -IDrivers/Device -IDrivers/Device/Include/CMSIS -IUser/Inc -O0 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/ac780x_gpio.d" -Wa,-a,-ad,-alms=build/ac780x_gpio.lst Drivers/ATC_Driver/Src/ac780x_gpio.c -o build/ac780x_gpio.o
  8. arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb    -IInc -IDrivers/ATC_Driver/Inc -IDrivers/Device/Include -IDrivers/Device -IDrivers/Device/Include/CMSIS -IUser/Inc -O0 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/ac780x_ckgen.d" -Wa,-a,-ad,-alms=build/ac780x_ckgen.lst Drivers/ATC_Driver/Src/ac780x_ckgen.c -o build/ac780x_ckgen.o
  9. arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb    -IInc -IDrivers/ATC_Driver/Inc -IDrivers/Device/Include -IDrivers/Device -IDrivers/Device/Include/CMSIS -IUser/Inc -O0 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/ac780x_spm.d" -Wa,-a,-ad,-alms=build/ac780x_spm.lst Drivers/ATC_Driver/Src/ac780x_spm.c -o build/ac780x_spm.o
  10. arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb    -IInc -IDrivers/ATC_Driver/Inc -IDrivers/Device/Include -IDrivers/Device -IDrivers/Device/Include/CMSIS -IUser/Inc -O0 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/ac780x_uart.d" -Wa,-a,-ad,-alms=build/ac780x_uart.lst Drivers/ATC_Driver/Src/ac780x_uart.c -o build/ac780x_uart.o
  11. arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb    -IInc -IDrivers/ATC_Driver/Inc -IDrivers/Device/Include -IDrivers/Device -IDrivers/Device/Include/CMSIS -IUser/Inc -O0 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/Uart.d" -Wa,-a,-ad,-alms=build/Uart.lst User/Src/Uart.c -o build/Uart.o
  12. arm-none-eabi-gcc -x assembler-with-cpp -c -mcpu=cortex-m0plus -mthumb    -IInc -IDrivers/ATC_Driver/Inc -IDrivers/Device/Include -IDrivers/Device -IDrivers/Device/Include/CMSIS -IUser/Inc -O0 -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/startup_AC7802x.d" startup_AC7802x.s -o build/startup_AC7802x.o
  13. arm-none-eabi-gcc build/main.o build/AC7802x_irq_cb.o build/AC7802x_msp.o build/system_AC7802x.o build/ac780x_gpio.o build/ac780x_ckgen.o build/ac780x_spm.o build/ac780x_uart.o build/Uart.o build/startup_AC7802x.o -mcpu=cortex-m0plus -mthumb   -specs=nano.specs -TAC78022MBQA_FLASH.ld  -lc -lm -lnosys  -Wl,-Map=build/AC7802_UART.map,--cref -Wl,--gc-sections -o build/AC7802_UART.elf
  14. arm-none-eabi-size build/AC7802_UART.elf
  15.    text    data     bss     dec     hex filename
  16.   12016      12    2220   14248    37a8 build/AC7802_UART.elf
  17. arm-none-eabi-objcopy -O ihex build/AC7802_UART.elf build/AC7802_UART.hex
  18. arm-none-eabi-objcopy -O binary -S build/AC7802_UART.elf build/AC7802_UART.bin
5、输入下载命令:
  1. lugl@lugl-virtual-machine:~/ac7802/AC7802_UART$ pyocd flash ./build/AC7802_UART.elf --target ac78022mbqa
  2. 0000426 I Loading /home/lugl/ac7802/AC7802_UART/build/AC7802_UART.elf [load_cmd]
  3. [==================================================] 100%
  4. 0001139 I Erased 0 bytes (0 sectors), programmed 0 bytes (0 pages), skipped 12288 bytes (24 pages) at 16.84 kB/s [loader]
6、同时为了方便下载,我们在makefile后面增加 makefile flash命令实现下载:
  1. flash:
  2.         pyocd flash ./build/AC7802_UART.elf --target ac78022mbqa
我们在执行make falsh后就可以实现下载功能了:
  1. lugl@lugl-virtual-machine:~/ac7802/AC7802_UART$ make flash
  2. pyocd flash ./build/AC7802_UART.elf --target ac78022mbqa
  3. 0000445 I Loading /home/lugl/ac7802/AC7802_UART/build/AC7802_UART.elf [load_cmd]
  4. [==================================================] 100%
  5. 0001146 I Erased 0 bytes (0 sectors), programmed 0 bytes (0 pages), skipped 12288 bytes (24 pages) at 17.16 kB/s [loader]
  6. l
7、实现效果:下载后,我们用typec线接到开发板的typec接口,打开串口助手,我们就可以实现不定义发送,同时AC7802把接收到的数据回显回来:
  1. [20:21:10.657]发→◇AT1313123112123123


  2. [20:21:10.661]收←◆AT1313123112123123


  3. [20:47:13.157]发→◇AT13131231



  4. [20:47:13.160]收←◆AT13131231



  5. [20:47:22.561]发→◇你好



  6. [20:47:22.563]收←◆你好



  7. [20:47:30.890]发→◇你好 AC7802X



  8. [20:47:30.893]收←◆你好 AC7802X
【总结】杰发的库函数做得非常好,给的示例也非常好,使得学习起来非常快捷。同时在linux环境下,用vscode也开发,也是非常之方便。体验了编程之快乐!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

188

主题

843

帖子

12

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