nRF52 UART SDKv14.2.0

UART

The UART driver includes two layers: the hardware access layer (HAL) and the driver layer (DRV).

UART驱动程序包括两层:硬件访问层(HAL)和驱动程序层(DRV)。

The hardware access layer provides basic APIs for accessing the registers of the UART and UARTE peripherals. See the API documentation for the UART HAL and UARTE HAL for details.

硬件访问层提供了访问UART和UARTE外设寄存器的基本API。 有关详细信息,请参阅UART HAL和UARTE HAL的API文档。

The driver layer provides APIs on a higher level than the HAL. See the API documentation for the UART driver for details.

驱动程序层提供比HAL更高级别的API。 有关详细信息,请参阅UART驱动程序的API文档。

Key features include:

主要功能包括:

  • Multi-instance support.
  • 多实例支持。
  • Common API for two types of peripherals: legacy peripherals (UART) and peripherals using EasyDMA (UARTE). The type of peripheral that is used can be configured either during build time or at runtime during initialization.
  • 两种外设的通用API:传统外设(UART)和使用EasyDMA(UARTE)的外设。 使用的外设类型可以在构建时或在初始化期间的运行时进行配置。
  • Two modes of data transfer: blocking and non-blocking (with end notification done via an event handler that is provided during initialization).
  • 两种数据传输模式:阻塞和非阻塞(通过初始化期间提供的事件处理程序完成结束通知)。
  • Driver supports aborting ongoing RX or TX transfers.
  • 驱动程序支持中止正在进行的RX或TX传输。
  • Supported modes: with and without hardware flow control and with and without parity bit.
  • 支持的模式:有和没有硬件流量控制,有和没有奇偶校验位。
  • Double RX buffering allowing to receive data continuously.
  • 双RX缓冲允许连续接收数据。

Note that peripherals using EasyDMA can work only with buffers that are placed in the Data RAM region. Under certain circumstances, compilers might choose to use a different region for data placement and, for example, place a constant buffer in the code FLASH. In such a case, the UARTE peripheral cannot be used to transfer data from the buffer.

请注意,使用EasyDMA的外设只能使用放置在数据RAM区域中的缓冲区。 在某些情况下,编译器可能会选择使用不同的区域来放置数据,例如,在代码FLASH中放置一个常量缓冲区。 在这种情况下,UARTE外设不能用于从缓冲区传输数据。

Driver configuration

驱动配置

The UART driver can use multiple instances of the UART/UARTE peripherals, and it provides a common API for both peripheral types. The instances of the peripherals that are to be assigned to the driver must be selected statically in sdk_config.h. In the same way, you configure whether a given instance uses EasyDMA.

UART驱动程序可以使用多个UART / UARTE外设实例,并为两种外设类型提供了一个通用API。 必须在sdk_config.h中静态选择要分配给驱动程序的外设实例。 以同样的方式,您可以配置给定实例是否使用EasyDMA。

The UART default configuration is located in sdk_config.h. If UARTE is present on the chip, the driver can be configured at runtime to support UART mode, UARTE mode, or both. The following example shows how to configure the driver to support both modes in runtime and have legacy mode as the default:

UART默认配置位于sdk_config.h中。 如果芯片上存在UARTE,则可以在运行时配置驱动程序以支持UART模式,UARTE模式或两者。 以下示例显示如何将驱动程序配置为在运行时支持两种模式,并将legacy模式设置为默认模式:

#ifdef NRF52
#define UART0_USE_EASY_DMA false
#define UART_EASY_DMA_SUPPORT 1
#define UART_LEGACY_SUPPORT 1
#endif //NRF52

If only one mode is used in the application, disable the second mode in sdk_config.h to achieve a lower memory footprint and better performance.

如果在应用程序中仅使用一种模式,请禁用sdk_config.h中的第二种模式以实现更低的内存占用量和更好的性能。

Call nrf_drv_uart_init with the p_config argument set to NULL to use the default configuration. To use a custom configuration, provide a user configuration structure, for example:

使用设置为NULL的p_config参数调用nrf_drv_uart_init以使用默认配置。 要使用自定义配置,请提供用户配置结构,例如:

nrf_drv_uart_t uart_driver_instance = NRF_DRV_UART_INSTANCE(UART0_INSTANCE_INDEX);
config.use_easy_dma = true;
ret_code = nrf_drv_uart_init(&uart_driver_instance, &config, NULL);

Using the UART driver

Blocking mode

阻止模式

If no event handler is provided during the initialization of the driver, the driver will operate in blocking mode. In this case, nrf_drv_uart_tx and nrf_drv_uart_rx will not return until the requested transfer is completed, nrf_drv_uart_rx_abort or nrf_drv_uart_tx_abort is called from a different context, or an error is detected. When an abort function is called, nrf_drv_uart_tx or nrf_drv_uart_rx returns with an error code. When an error is reported by the peripheral,nrf_drv_uart_rxreturns with an error code.

如果在驱动程序初始化期间没有提供事件处理程序,则驱动程序将以阻塞模式运行。 在这种情况下,直到请求的传输完成,nrf_drv_uart_rx_abort或nrf_drv_uart_tx_abort从不同的上下文被调用,或者检测到错误,nrf_drv_uart_tx和nrf_drv_uart_rx才会返回。 当调用中止函数时,nrf_drv_uart_tx或nrf_drv_uart_rx返回一个错误代码。 当外设报告错误时,nrf_drv_uart_rx返回错误代码。

In blocking mode, the driver does not use a peripheral interrupt, and there is no context switching inside the driver.

在阻塞模式下,驱动程序不使用外设中断,驱动程序内部没有上下文切换。

Non-blocking mode

If an event handler (nrf_uart_event_handler_t) is provided during the initialization of the driver, the driver will operate in non-blocking mode. In this case, nrf_drv_uart_tx and nrf_drv_uart_rx will return immediately after transfer is started. Completion will be notified to the user by the event handler that was provided during initialization. The event handler is called in the context of the UART/UARTE interrupt. If an error is reported by the peripheral, it is also reported by the event handler. The error event contains information about the amount of data that was transferred before the error occurred.

如果在驱动程序初始化期间提供事件处理程序(nrf_uart_event_handler_t),则驱动程序将以非阻塞模式运行。 在这种情况下,传输开始后,nrf_drv_uart_tx和nrf_drv_uart_rx将立即返回。 完成将通过初始化期间提供的事件处理程序通知给用户。 事件处理程序在UART / UARTE中断的上下文中调用。 如果外设报告错误,则也由事件处理程序报告。 错误事件包含有关在发生错误之前传输的数据量的信息。

If nrf_drv_uart_rx_abort or nrf_drv_uart_tx_abort is called, the event handler notifies that the transfer is completed, but the length field is set to the amount of data that was transferred before the abort function was called.

如果调用nrf_drv_uart_rx_abort或nrf_drv_uart_tx_abort,则事件处理程序通知传输已完成,但长度字段已设置为调用中止函数之前传输的数据量。

Enabling RX without providing data buffer

无需提供数据缓冲区即可启用RX

You can enable RX without providing a buffer for incoming data. In this case, incoming data (up to 6 bytes) stays in the hardware FIFO. If more than 6 bytes are received, an overrun error condition occurs. This condition will be reported by the event handler or at the next nrf_drv_uart_rx call.

您可以在不为传入数据提供缓冲区的情况下启用接收。 在这种情况下,输入数据(最多6个字节)保留在硬件FIFO中。 如果收到超过6个字节,则会发生溢出错误情况。 此情况将由事件处理程序或下一次nrf_drv_uart_rx调用报告。

Note
This functionality is provided only for UART mode.该功能仅适用于UART模式。
nrf_drv_uart_t uart_driver_instance = NRF_DRV_UART_INSTANCE(UART0_INSTANCE_INDEX);
ret_code = nrf_drv_uart_init(&uart_driver_instance, NULL, NULL);
nrf_drv_uart_rx_enable(&uart_driver_instance);
//UART RX is enabled
nrf_drv_uart_rx(&uart_driver_instance, buffer, 2);
//UART RX is still enabled.
nrf_drv_uart_rx_disable(&uart_driver_instance);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值