学习目标

  • 掌握串口的代码编写和调试

    学习内容

    串口

    131.png136.png

  • P11为Uart2_TXD

  • P13为Uart2_RXD

    案例设计

  1. Hi3861的串口2(Uart2)和STC8的串口进行通讯
  2. HI3861的串口2定时发送消息给STC8
  3. HI3861的串口2收到消息后,通过printf输出到控制台。

    串口初始化

    ```c //初始化GPIO口 IoTGpioInit(IOT_IO_NAME_11); //设置IO口功能为GPIO IoTGpioSetFunc(IOT_IO_NAME_11, IOT_GPIO_FUNC_GPIO_11_UART2_TXD);

IoTGpioInit(IOT_IO_NAME_12); //设置IO口功能为GPIO IoTGpioSetFunc(IOT_IO_NAME_12, IOT_GPIO_FUNC_GPIO_12_UART2_RXD);

IotUartAttribute attr; attr.baudRate = 115200; //波特率 attr.dataBits = 8; //数据位 attr.stopBits = 1; // 停止位 attr.parity = IOT_UART_PARITY_NONE; //是否奇偶校验 attr.rxBlock = IOT_UART_BLOCK_STATE_NONE_BLOCK; // 是否为阻塞式 attr.txBlock = IOT_UART_BLOCK_STATE_NONE_BLOCK; // 是否为阻塞式 attr.pad = 0;//padding bit,占位空字符 IoTUartInit(IDX, &attr);

  1. <a name="lSmBq"></a>
  2. #### 开发流程
  3. 来到应用开发的**根目录。**<br />我们编写代码的**根目录**为`device/board/itcast/genkipi/app`。
  4. 1. **根目录**下新建`uart_demo`文件夹,此为**项目目录**。
  5. 2. 此**项目目录**下新建`main.c`文件
  6. 3. 此**项目目录**下新建`BUILD.gn`文件
  7. 4. 修改**根目录**下的`BUILD.gn`文件
  8. <a name="pbSC0"></a>
  9. #### 代码部分
  10. ```c
  11. /*
  12. * Copyright (C) 2020 Itcast Co., Ltd. All rights reserved.
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include "cmsis_os2.h"
  29. #include "ohos_init.h"
  30. #include "iot_gpio.h"
  31. #include "iot_gpio_ex.h"
  32. #include "iot_uart.h"
  33. #define IDX 2
  34. static void uart_write(void) {
  35. unsigned char data[256];
  36. int cnt = 0;
  37. while (1)
  38. {
  39. sprintf(data, "nice %d\r\n", cnt++);
  40. IoTUartWrite(IDX, data, strlen(data));
  41. usleep(2 * 1000 * 1000);
  42. }
  43. }
  44. static void uart_read(void) {
  45. unsigned char data[256];
  46. int len = 0;
  47. while (1)
  48. {
  49. len = IoTUartRead(IDX, data, 256);
  50. if(len <= 0) {
  51. usleep(20 * 1000);
  52. continue;
  53. }
  54. data[len] = '\0';
  55. printf("recv: %s\r\n", data);
  56. usleep(20 * 1000);
  57. }
  58. }
  59. static void start_read_task() {
  60. osThreadAttr_t attr;
  61. attr.name = "read";
  62. attr.attr_bits = 0U;
  63. attr.cb_mem = NULL;
  64. attr.cb_size = 0U;
  65. attr.stack_mem = NULL;
  66. attr.stack_size = 1024 * 2;
  67. attr.priority = 25;
  68. // Create the Thread1 task
  69. if (osThreadNew((osThreadFunc_t)uart_read, NULL, &attr) == NULL) {
  70. printf("Failed to create read Thread!\n");
  71. }
  72. }
  73. static void start_write_task() {
  74. osThreadAttr_t attr;
  75. attr.name = "write";
  76. attr.attr_bits = 0U;
  77. attr.cb_mem = NULL;
  78. attr.cb_size = 0U;
  79. attr.stack_mem = NULL;
  80. attr.stack_size = 1024 * 2;
  81. attr.priority = 25;
  82. // Create the Thread1 task
  83. if (osThreadNew((osThreadFunc_t)uart_write, NULL, &attr) == NULL) {
  84. printf("Failed to create read Thread!\n");
  85. }
  86. }
  87. static void uart(void)
  88. {
  89. printf("Hello World!Hello genkipi\n");
  90. //初始化GPIO口
  91. IoTGpioInit(IOT_IO_NAME_11);
  92. //设置IO口功能为GPIO
  93. IoTGpioSetFunc(IOT_IO_NAME_11, IOT_GPIO_FUNC_GPIO_11_UART2_TXD);
  94. IoTGpioInit(IOT_IO_NAME_12);
  95. //设置IO口功能为GPIO
  96. IoTGpioSetFunc(IOT_IO_NAME_12, IOT_GPIO_FUNC_GPIO_12_UART2_RXD);
  97. IotUartAttribute attr;
  98. attr.baudRate = 115200; //波特率
  99. attr.dataBits = 8; //数据位
  100. attr.stopBits = 1; // 停止位
  101. attr.parity = IOT_UART_PARITY_NONE; //是否奇偶校验
  102. attr.rxBlock = IOT_UART_BLOCK_STATE_NONE_BLOCK; // 是否为阻塞式
  103. attr.txBlock = IOT_UART_BLOCK_STATE_NONE_BLOCK; // 是否为阻塞式
  104. attr.pad = 0;//padding bit,占位空字符
  105. IoTUartInit(IDX, &attr);
  106. start_read_task();
  107. start_write_task();
  108. }
  109. static void main(void)
  110. {
  111. osThreadAttr_t attr;
  112. attr.name = "uart";
  113. attr.attr_bits = 0U;
  114. attr.cb_mem = NULL;
  115. attr.cb_size = 0U;
  116. attr.stack_mem = NULL;
  117. attr.stack_size = 1024;
  118. attr.priority = 25;
  119. // Create the Thread1 task
  120. if (osThreadNew((osThreadFunc_t)uart, NULL, &attr) == NULL) {
  121. printf("Failed to create say hello Thread!\n");
  122. }
  123. }
  124. APP_FEATURE_INIT(main);
  1. static_library("uart——demo") {
  2. sources = [ "main.c" ]
  3. include_dirs = [
  4. "//base/iothardware/peripheral/interfaces/inner_api",
  5. "../../iot_hardware_hals/include"
  6. ]
  7. }
  1. import("//build/lite/config/component/lite_component.gni")
  2. lite_component("app") {
  3. features = [
  4. "uart_demo"
  5. ]
  6. }

校验方式

接线:
137.png
通过串口调试工具,测试两端是否联通。

练习题

  • 调试不同芯片间的串口