15、nRF52xx蓝牙学习(串口输入与回环)


uint32_t app_uart_get(uint8_t * p_byte)
{ASSERT(p_byte);bool rx_ovf = m_rx_ovf;ret_code_t err_code = app_fifo_get(&m_rx_fifo, p_byte);// If FIFO was full new request to receive one byte was not scheduled. Must be done here.if (rx_ovf){m_rx_ovf = false;uint32_t uart_err_code = nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);// RX resume should never fail.APP_ERROR_CHECK(uart_err_code);}return err_code;
}
(1)app_fifo_get函数
uint32_t app_fifo_get(app_fifo_t * p_fifo, uint8_t * p_byte)
{
if (FIFO_LENGTH() != 0)
{
fifo_get(p_fifo, p_byte);
return NRF_SUCCESS;
}
return NRF_ERROR_NOT_FOUND;
}
//其中:app_fifo_t结构体定义如下:
typedef struct
{
uint8_t * p_buf; /**< Pointer to FIFO buffer memory. */
uint16_t buf_size_mask; /**< Read/write index mask. Also used for size checking. */
volatile uint32_t read_pos; /**< Next read position in the FIFO buffer. */
volatile uint32_t write_pos; /**< Next write position in the FIFO buffer. */
} app_fifo_t;
//其中:有此宏:FIFO_LENGTH
#define FIFO_LENGTH() fifo_length(p_fifo) /**< Macro for calculating the FIFO length. */
//其中:fifo_length函数定义如下 :
static __INLINE uint32_t fifo_length(app_fifo_t * p_fifo)
{
uint32_t tmp = p_fifo->read_pos;
return p_fifo->write_pos - tmp;
}
//其中:fifo_get函数定义如下 :
static __INLINE void fifo_get(app_fifo_t * p_fifo, uint8_t * p_byte)
{
fifo_peek(p_fifo, 0, p_byte);
p_fifo->read_pos++;
}
//其中:fifo_peek函数定义如下:
static __INLINE void fifo_peek(app_fifo_t * p_fifo, uint16_t index, uint8_t * p_byte)
{
*p_byte = p_fifo->p_buf[(p_fifo->read_pos + index) & p_fifo->buf_size_mask];
}
这段代码定义了一个内联函数 fifo_peek,其功能是查看 FIFO(先进先出)缓冲区里指定位置的一个字节数据,且不会改变 FIFO 的读指针位置。
fifo_peek:函数名,代表查看 FIFO 中指定位置字节的操作。
• app_fifo_t * p_fifo:该参数是一个指向 app_fifo_t 类型结构体的指针,app_fifo_t 应该是自定义的结构体类型,用于表示 FIFO 缓冲区。
• uint16_t index:这是一个 16 位无符号整数,代表要查看的字节在 FIFO 缓冲区中的相对索引位置。
• uint8_t * p_byte:这是一个指向 8 位无符号整数的指针,用于存储从 FIFO 缓冲区中读取的字节数据。
p_fifo->p_buf:访问 app_fifo_t 结构体中的 p_buf 成员,该成员应该是一个指向 FIFO 缓冲区的指针。
• p_fifo->read_pos:访问 app_fifo_t 结构体中的 read_pos 成员,此成员表示当前 FIFO 缓冲区的读指针位置。
• (p_fifo->read_pos + index):将读指针位置与指定的相对索引相加,从而得到要查看的字节在缓冲区中的绝对索引。
• & p_fifo->buf_size_mask:使用按位与操作将绝对索引与 buf_size_mask 进行运算。buf_size_mask 通常是一个用于实现环形缓冲区的掩码,其作用是确保索引不会超出缓冲区的大小范围,当索引超出缓冲区大小时,会自动回绕到缓冲区的起始位置。
fifo_peek 函数能够在不改变 FIFO 缓冲区读指针位置的情况下,查看指定位置的一个字节数据。这种操作在某些场景下非常有用,例如在不影响后续读取操作的前提下,预先查看 FIFO 缓冲区中的数据。
(2)nrf_drv_uart_rx函数
__STATIC_INLINE
ret_code_t nrf_drv_uart_rx(nrf_drv_uart_t const * p_instance,
uint8_t * p_data,
uint8_t length)
{
uint32_t result = 0;
if (NRF_DRV_UART_USE_UARTE)
{
result = nrfx_uarte_rx(&p_instance->uarte,
p_data,
length);
}
else if (NRF_DRV_UART_USE_UART)
{
result = nrfx_uart_rx(&p_instance->uart,
p_data,
length);
}
return result;
}
//其中nrf_drv_uart_结构体如下:
typedef struct
{
uint8_t inst_idx;
#if defined(NRF_DRV_UART_WITH_UARTE)
nrfx_uarte_t uarte;
#endif
#if defined(NRF_DRV_UART_WITH_UART)
nrfx_uart_t uart;
#endif
} nrf_drv_uart_t;