초보의 아웅다웅 설계하기/Nordic_nRF

nRF5 Uart 사용하기(without FIFO)

로망와니 2020. 7. 30. 18:56

nRF5 예제의 FIFO 구조로 되어 있는 UART를 FIFO를 사용하지 않고 설정해 사용하려고 하였습니다. 그런데 믕?

사용은 되는데 app_uart_put 함수가 한바이트씩 밖에 동작하지 않습니다.

구글링 해보니 코드들을 조금 손대줘야 하나봅니다.(기존 예제를 사용하지 않으려고 하는 이유는 데이터를 계속 뿌리니 중간중간 데이터가 깨져서 연속으로 데이터를 계속 전송하는 경우 문제가 생겨서 입니다.)

일단 아쉬운대로 app_uart_put파일을 고쳐서 데이터만 확인하였습니다.(문제 여지가 있어 보이지만 임시로 썼습니다.)

    APP_UART_FIFO_INIT(&comm_params, 
                         UART_RX_BUF_SIZE, 
                         UART_TX_BUF_SIZE, 
                         uart_error_handle, 
                         APP_IRQ_PRIORITY_HIGHEST, 
                         err_code);

=>

APP_UART_INIT(&comm_params, uart_error_handle, APP_IRQ_PRIORITY_HIGHEST, err_code);


/******************************************************************************* 
* Function Name :  
* Parameters    : None 
* Return        : None 
* Description   :   
*******************************************************************************/ 
#define UART_TIMEOUT 0x800000
uint32_t app_uart_put(uint8_t byte)
{
uint32_t Timeout = 0; /* Variable used for timeout management */
tx_buffer[0] = byte;

Timeout = UART_TIMEOUT;

while (1){
ret_code_t ret =  nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
    if (ret == NRF_SUCCESS){
      return NRF_SUCCESS;
    }
if(Timeout-- == 0){
/* Timeasdfa-out occurred. */
return NRF_ERROR_INTERNAL;
}
}
return NRF_ERROR_INTERNAL;
}

/******************************************************************************* 
* Function Name :  
* Parameters    : None 
* Return        : None 
* Description   :   
*******************************************************************************/ 
void UART1_PutChar(uint8_t data)
{
app_uart_put(data);
}

/*******************************************************************************
* Function Name : 
* Parameters    : None
* Return        : None
* Description   :  
*******************************************************************************/
void UART_PutStr(char *string)
{
while(*string != '\0') UART1_PutChar(*string++);
}

/*******************************************************************************
* Function Name : 
* Parameters    : None
* Return        : None
* Description   :  
*******************************************************************************/
void UART1_printf(const char *fmt, ...)
{
  char gPrintBuf[256];
  va_list args;
   
  va_start(args,fmt);
  vsprintf(gPrintBuf, fmt, args);
  va_end(args);
  
  UART_PutStr(gPrintBuf);
}

 

------------------------------------------------------------------------------

fifo에서 딜레이를 강제로 넣어서 맞추는 방법도 있습니다.

void delay_uart(__IO uint32_t nCount)
{
nCount *= 4;
    for(; nCount!=0; nCount--);
}

uint32_t app_uart_put(uint8_t byte)
{
    uint32_t err_code;
    err_code = app_fifo_put(&m_tx_fifo, byte);
    if (err_code == NRF_SUCCESS)
    {
        // The new byte has been added to FIFO. It will be picked up from there
        // (in 'uart_event_handler') when all preceding bytes are transmitted.
        // But if UART is not transmitting anything at the moment, we must start
        // a new transmission here.
        if (!nrf_drv_uart_tx_in_progress(&app_uart_inst))
        {
            // This operation should be almost always successful, since we've
            // just added a byte to FIFO, but if some bigger delay occurred
            // (some heavy interrupt handler routine has been executed) since
            // that time, FIFO might be empty already.
            if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
            {
                err_code = nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
            }
        }
    }
delay_uart(100);
    return err_code;
}

 

 

 

'초보의 아웅다웅 설계하기 > Nordic_nRF' 카테고리의 다른 글

MAX30100 nRF52  (0) 2024.02.12
nRF52xxx Bluetooth사용 안하기  (0) 2022.12.18