2016年12月22日 星期四

nRF51 : Make the Printf to Work Well


NOTE :: It is suitable for nRF5X SDK BEFORE 12, for SDK 12, please refer to here.
     You may encounter a problem that when you follow the standard UART(Universal Asynchronous Receiver/Transmitter) step, but the printf would not work well. the Standard UART code in Nordic example be  (based on examples\ble_peripheral\ble_app_template ):


/**@brief Function for the Power manager.
 */
static void power_manage(void)
{
  uint32_t err_code = sd_app_evt_wait();
  APP_ERROR_CHECK(err_code);
}

#include "app_uart.h"

#define UART_TX_BUF_SIZE  (256) 
#define UART_RX_BUF_SIZE  (256) 

/**
 * @brief UART events handler.
 */
void uart_events_handler(app_uart_evt_t * p_event)
{
 switch (p_event->evt_type)
 {
 case APP_UART_DATA_READY:
   break;

 case APP_UART_COMMUNICATION_ERROR: 
   APP_ERROR_HANDLER(p_event->data.error_communication);
   break;

 case APP_UART_FIFO_ERROR:          
   APP_ERROR_HANDLER(p_event->data.error_code);
   break;

 case APP_UART_TX_EMPTY:
   break;

 case APP_UART_DATA:
   break;

 default: 
  break;
 }
}

/**
 * @brief UART initialization.
 */
void uart_init(void)
{
 uint32_t                     err_code;
  const app_uart_comm_params_t comm_params =
 {
  RX_PIN_NUMBER,
  TX_PIN_NUMBER,
  RTS_PIN_NUMBER,
  CTS_PIN_NUMBER,
  APP_UART_FLOW_CONTROL_DISABLED,
  false,
  UART_BAUDRATE_BAUDRATE_Baud38400
 };

 APP_UART_FIFO_INIT(&comm_params,
        UART_RX_BUF_SIZE,
        UART_TX_BUF_SIZE,
        uart_events_handler,
        APP_IRQ_PRIORITY_LOW,
        err_code);
  
 APP_ERROR_CHECK(err_code);
}


/**@brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;
    bool erase_bonds;
 
 uart_init();
 printf("Start...\r\n");

    // Initialize.


You will find that the output be :


S

,instead of full "Start". It prints the first byte always.


To solve this insufficient it very knack but simple :

Your default including libraries should be like this :


 

  Replace the  components\libraries\uart\app_uart.c as components\libraries\uart\app_uart_fifo.c and components\libraries\fifo\app_fifo.c



And do not forget to add the corresponding including path ..\..\..\..\..\..\components\libraries\fifo and ..\..\..\..\..\..\components\libraries\uart



And now your printf would work well:


Start...