This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF52840 UART/UARTE not working

HI, all, 

       I am trying to transfer my previous project from nrf51822 to nrf52840, and at very the beginning ,I can not make the UART/UARTE of nrf52840  work. 

       0, I have programmed example hex file into 52840 to check the chip is OK and UART is OK, I can get data transmission from 52840 normally.

       1, I  tried to use nrf_uart.h to build the driver functions for UART,  since our previous project use it . However , when I watch the memory of the UART registers, I found that the function triggering STARTTX   nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STARTTX)  have no effect on the register TASKS_STARTTX which is located at 0x40002008, so data transmission did not happen. I have found when I operated other registers ,such as ENABLE, BAUDRATE and PSEL.TXD, data on the memory changed as  expected.

      2, I took reference from this link https://github.com/andenore/NordicSnippets/blob/master/examples/uart/main.c#L20 , trying to operate UARTE by setting register directly,  still STARTTX  can not be set.

       I have read about the STARTTX   in PS and searched in devzone,  and I have not found any restriction on setting that register, so I don`t know why that happened. 

   my code snap about UART is below, and I am using SDK16.0 ,the IDE is IAR8.22. the pin definition of my board is compatible with PCA10040 .   

   Any help and advice is appreciated! 

BOOL BSP_Uart_Init(void)
{
    if (s_tThisData.InitFlag) {
        return False;
    }
    
    if ((GPIO_UART_TX > CH47) && (GPIO_UART_RX > CH47)) {
        return False;
    }
    else {
        memset(&s_tThisData, 0, SIZE(s_tThisData));
        
        //nrf_uart_configure(NRF_UART0, NRF_UART_PARITY_EXCLUDED, NRF_UART_HWFC_DISABLED);
        //nrf_uart_txrx_pins_set(NRF_UART0, GPIO_UART_TX, GPIO_UART_RX);  // 配置端口
        
        //nrf_uart_int_enable(NRF_UART0, NRF_UART_INT_MASK_RXDRDY);   // 接收、错误
        
        
         NRF_UARTE0->CONFIG = (UART_CONFIG_HWFC_Disabled   << UART_CONFIG_HWFC_Pos) |
                       (NRF_UART_PARITY_EXCLUDED << UART_CONFIG_PARITY_Pos); 
        
        NRF_UARTE0->PSEL.TXD = GPIO_UART_TX;
        NRF_UARTE0->PSEL.RXD = GPIO_UART_RX;
        
        NRF_UARTE0->INTENSET = NRF_UARTE_INT_ENDRX_MASK |
                               NRF_UARTE_INT_ENDTX_MASK |
                               NRF_UARTE_INT_ERROR_MASK |
                               NRF_UARTE_INT_RXTO_MASK  |
                               NRF_UARTE_INT_TXSTOPPED_MASK;
        
        NVIC_ClearPendingIRQ(UARTE0_UART0_IRQn);
        NVIC_SetPriority(UARTE0_UART0_IRQn, 3);
        
        s_tThisData.InitFlag = True;
        BSP_Uart_Open(UART_BAUDRATE_115200);
    }
    
    return True;
}

BOOL BSP_Uart_Open(T_UART_Baudrate Baud)
{
    if (!s_tThisData.InitFlag) {
        return False;
    }
    
    if (NRF_UARTE0->ENABLE == 0) {
        //nrf_uart_baudrate_set(NRF_UART0, (nrf_uart_baudrate_t)Baud);    // 设置波特率
        
        NRF_UARTE0->BAUDRATE =(nrf_uart_baudrate_t)Baud;
        
        
       // uint32_t  TX_TASK =  nrf_uart_task_address_get(NRF_UART0,NRF_UART_TASK_STARTTX);
       // nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STARTTX);
       // uint32_t  RX_TASK =  nrf_uart_task_address_get(NRF_UART0,NRF_UART_TASK_STARTRX); 
       // nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STARTRX);
        NRF_UARTE0->ENABLE = UARTE_ENABLE_ENABLE_Enabled << UARTE_ENABLE_ENABLE_Pos;
        
      
        //nrf_uart_enable(NRF_UART0); 
       
          
        //nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_RXDRDY);        
        //nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_ERROR);
        
        NRF_UARTE0->EVENTS_ENDRX = 0x0UL;
        NRF_UARTE0->EVENTS_ERROR = 0x0UL;
        NRF_UARTE0->EVENTS_ENDTX = 0x0UL;
        NRF_UARTE0->EVENTS_RXTO  = 0x0UL;
        NRF_UARTE0->EVENTS_TXSTOPPED = 0x0UL;
        
        NVIC_EnableIRQ(UARTE0_UART0_IRQn);
        
        nrf_gpio_cfg_output(GPIO_UART_TX);
        nrf_gpio_cfg_input(GPIO_UART_RX, NRF_GPIO_PIN_PULLUP);  // 配置为上拉打印速度大大提升
        
        //NRF_UART0->TASKS_STARTRX = 0x1UL;
        //NRF_UART0->TASKS_STARTTX = 0x1UL;
      
        nrf_delay_us(1000);
    }
    
    return True;
}

BOOL BSP_Uart_Close(void)
{
    if (!s_tThisData.InitFlag) {
        return False;
    }
    
    if (NRF_UARTE0->ENABLE != 0) {
        NVIC_DisableIRQ(UARTE0_UART0_IRQn);
        
        //nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STOPTX);
        //nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STOPRX);
        //nrf_uart_disable(NRF_UARTE0);
        NRF_UARTE0->TASKS_STOPRX = 0x1UL;
        NRF_UARTE0->TASKS_STOPTX = 0x1UL;
 
        while (NRF_UARTE0->EVENTS_TXSTOPPED == 0);
        NRF_UARTE0->ENABLE = UARTE_ENABLE_ENABLE_Disabled << UARTE_ENABLE_ENABLE_Pos;
        //NRF_UARTE0->ENABLE = UARTE_ENABLE_ENABLE_Disabled;

        nrf_gpio_cfg_default(GPIO_UART_TX);
        nrf_gpio_cfg_default(GPIO_UART_RX);
    }
    return True;
}


void BSP_Uart_SendByte(U8 Byte)
{
   // U32 timer = 0;
    
    if (!s_tThisData.InitFlag) {
        return;
    }
    
    if (NRF_UARTE0->ENABLE == 0) {
        BSP_Uart_Open(UART_BAUDRATE_115200);
    }
    
    //nrf_uart_txd_set(NRF_UART0, Byte);
   // while (!nrf_uart_event_check(NRF_UART0, NRF_UART_EVENT_TXDRDY)) {
   //     if (Delay.Check(&timer, 10)) {
   //         break;
   //     }
   // }
    NRF_UARTE0->TXD.MAXCNT = 1;
    NRF_UARTE0->TXD.PTR = (uint32_t)&Byte;
    
    NRF_UARTE0->TASKS_STARTRX = true;
    NRF_UARTE0->TASKS_STARTTX = true;
    while(NRF_UARTE0->EVENTS_ENDTX == 0){ }
    
    //nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_TXDRDY);
    NRF_UARTE0->EVENTS_ENDTX = 0x0UL;
}

Related