1. 程式人生 > >CC2530增加printf輸出函式

CC2530增加printf輸出函式

printf在程式除錯中有很大幫助,實時列印log,非常方便。特別是在無線系統除錯,實時性很強,並不是單步除錯能夠做到的。
TICC2530,實現printf步驟:

1.新增log.c檔案

#include "OSAL.h"
#include "npi.h"
#include <stdarg.h>
#include <stdio.h>
#include "log.h"

#if (defined HAL_UART) && (HAL_UART == TRUE)

static void NpiSerialCallback( uint8 port, uint8
events ); void log_init(void) { // 串列埠初始化 波特率預設是115200, 形參是回撥函式 NPI_InitTransport(NpiSerialCallback); } // 串列埠回撥函式, static void NpiSerialCallback( uint8 port, uint8 events ) { (void)port;//加個 (void),是未了避免編譯告警,明確告訴緩衝區不用理會這個變數 if (events & (HAL_UART_RX_TIMEOUT | HAL_UART_RX_FULL)) //串列埠有資料
{ uint8 numBytes = 0; numBytes = NPI_RxBufLen(); //讀出串列埠緩衝區有多少位元組 if(numBytes == 0) { return; } else { //申請緩衝區buffer uint8 *buffer = osal_mem_alloc(numBytes); if(buffer) { //讀取讀取串列埠緩衝區資料,釋放串列埠資料
NPI_ReadTransport(buffer,numBytes); //把收到的資料傳送到串列埠-實現迴環 NPI_WriteTransport(buffer, numBytes); //釋放申請的緩衝區 osal_mem_free(buffer); } } } } #define PRINT_BUF_LEN 20 int SerialPrintf(const char*fmt, ...) { uint32 ulLen; va_list ap; char *pBuf = (char*)osal_mem_alloc(PRINT_BUF_LEN); // 開闢緩衝區 va_start(ap, fmt); ulLen = vsprintf(pBuf, fmt, ap); // 用虛擬列印函式實現 va_end(ap); HalUARTWrite(HAL_UART_PORT_0, (uint8*)pBuf, ulLen); // 從串列埠 0 輸出 osal_mem_free(pBuf); // 釋放記憶體空間 return ulLen; } #else int SerialPrintf(const char*fmt, ...) { return 0; } #endif // (defined HAL_UART) && (HAL_UART == TRUE)

2.新增log.h檔案


#ifndef LOG_H
#define LOG_H

#ifdef __cplusplus
extern "C"
{
#endif

void log_init(void);
int SerialPrintf(const char*fmt, ...);


#define log SerialPrintf


#ifdef __cplusplus
}
#endif

#endif /* LOG_H */

3.新增程式配置巨集
IAR配置巨集
4. 新增程式初始化呼叫(本例在是在Z-Stack Home 1.2.2a.44539\Projects\zstack\HomeAutomation\SampleLight例程上演示的,所以函式新增在zclSampleLight_Init()中)

#if (defined HAL_UART) && (HAL_UART == TRUE)
  log_init();
  log("[%s] %x\n", __FUNCTION__, 123);
#endif

5.檢視輸出結果
這裡寫圖片描述

6.特別說明,由於Z-stack程式碼中沒有新增npi.c和npi.h,所以此兩個檔案是在TI CC2540 BLE協議棧中copy的,具體程式碼如下:
7.npi.c:

/*******************************************************************************
 * INCLUDES
 */

#include "hal_types.h"
#include "hal_board.h"
#include "npi.h"

/*******************************************************************************
 * MACROS
 */

/*******************************************************************************
 * CONSTANTS
 */

/*******************************************************************************
 * TYPEDEFS
 */

/*******************************************************************************
 * LOCAL VARIABLES
 */

/*******************************************************************************
 * GLOBAL VARIABLES
 */

/*******************************************************************************
 * PROTOTYPES
 */

/*******************************************************************************
 * FUNCTIONS
 */

/*******************************************************************************
 * @fn          NPI_InitTransport
 *
 * @brief       This routine initializes the transport layer and opens the port
 *              of the device. Note that based on project defines, either the
 *              UART, USB (CDC), or SPI driver can be used.
 *
 * input parameters
 *
 * @param       npiCback - User callback function when data is available.
 *
 * output parameters
 *
 * @param       None.
 *
 * @return      None.
 */
void NPI_InitTransport( npiCBack_t npiCBack )
{
  halUARTCfg_t uartConfig;

  // configure UART
  uartConfig.configured           = TRUE;
  uartConfig.baudRate             = NPI_UART_BR;
  uartConfig.flowControl          = NPI_UART_FC;
  uartConfig.flowControlThreshold = NPI_UART_FC_THRESHOLD;
  uartConfig.rx.maxBufSize        = NPI_UART_RX_BUF_SIZE;
  uartConfig.tx.maxBufSize        = NPI_UART_TX_BUF_SIZE;
  uartConfig.idleTimeout          = NPI_UART_IDLE_TIMEOUT;
  uartConfig.intEnable            = NPI_UART_INT_ENABLE;
  uartConfig.callBackFunc         = (halUARTCBack_t)npiCBack;

  // start UART
  // Note: Assumes no issue opening UART port.
  (void)HalUARTOpen( NPI_UART_PORT, &uartConfig );

  return;
}


/*******************************************************************************
 * @fn          NPI_ReadTransport
 *
 * @brief       This routine reads data from the transport layer based on len,
 *              and places it into the buffer.
 *
 * input parameters
 *
 * @param       buf - Pointer to buffer to place read data.
 * @param       len - Number of bytes to read.
 *
 * output parameters
 *
 * @param       None.
 *
 * @return      Returns the number of bytes read from transport.
 */
uint16 NPI_ReadTransport( uint8 *buf, uint16 len )
{
  return( HalUARTRead( NPI_UART_PORT, buf, len ) );
}


/*******************************************************************************
 * @fn          NPI_WriteTransport
 *
 * @brief       This routine writes data from the buffer to the transport layer.
 *
 * input parameters
 *
 * @param       buf - Pointer to buffer to write data from.
 * @param       len - Number of bytes to write.
 *
 * output parameters
 *
 * @param       None.
 *
 * @return      Returns the number of bytes written to transport.
 */
uint16 NPI_WriteTransport( uint8 *buf, uint16 len )
{
  return( HalUARTWrite( NPI_UART_PORT, buf, len ) );
}


/*******************************************************************************
 * @fn          NPI_RxBufLen
 *
 * @brief       This routine returns the number of bytes in the receive buffer.
 *
 * input parameters
 *
 * @param       None.
 *
 * output parameters
 *
 * @param       None.
 *
 * @return      Returns the number of bytes in the receive buffer.
 */
uint16 NPI_RxBufLen( void )
{
  return( Hal_UART_RxBufLen( NPI_UART_PORT ) );
}


/*******************************************************************************
 * @fn          NPI_GetMaxRxBufSize
 *
 * @brief       This routine returns the max size receive buffer.
 *
 * input parameters
 *
 * @param       None.
 *
 * output parameters
 *
 * @param       None.
 *
 * @return      Returns the max size of the receive buffer.
 */
uint16 NPI_GetMaxRxBufSize( void )
{
  return( NPI_UART_RX_BUF_SIZE );
}


/*******************************************************************************
 * @fn          NPI_GetMaxTxBufSize
 *
 * @brief       This routine returns the max size transmit buffer.
 *
 * input parameters
 *
 * @param       None.
 *
 * output parameters
 *
 * @param       None.
 *
 * @return      Returns the max size of the transmit buffer.
 */
uint16 NPI_GetMaxTxBufSize( void )
{
  return( NPI_UART_TX_BUF_SIZE );
}


/*******************************************************************************
 ******************************************************************************/

8.npi.h

#ifndef NPI_H
#define NPI_H

#ifdef __cplusplus
extern "C"
{
#endif

/*******************************************************************************
 * INCLUDES
 */

#include "hal_types.h"
#include "hal_board.h"
#include "hal_uart.h"

/*******************************************************************************
 * MACROS
 */

/*******************************************************************************
 * CONSTANTS
 */

/* UART port */
#if !defined NPI_UART_PORT

#if ((defined HAL_UART_SPI) && (HAL_UART_SPI != 0)) // FOR SPI
#if (HAL_UART_SPI == 2)  
#define NPI_UART_PORT                  HAL_UART_PORT_1
#else
#define NPI_UART_PORT                  HAL_UART_PORT_0
#endif
#else // FOR UART
#if ((defined HAL_UART_DMA) && (HAL_UART_DMA  == 1))
#define NPI_UART_PORT                  HAL_UART_PORT_0
#elif ((defined HAL_UART_DMA) && (HAL_UART_DMA  == 2))
#define NPI_UART_PORT                  HAL_UART_PORT_1
#else
#define NPI_UART_PORT                  HAL_UART_PORT_0
#endif
#endif // Endif for HAL_UART_SPI/DMA 
#endif //Endif for NPI_UART_PORT

#if !defined( NPI_UART_FC )
#define NPI_UART_FC                    FALSE//TRUE  //change eric
#endif // !NPI_UART_FC

#define NPI_UART_FC_THRESHOLD          48
#define NPI_UART_RX_BUF_SIZE           128
#define NPI_UART_TX_BUF_SIZE           128
#define NPI_UART_IDLE_TIMEOUT          6
#define NPI_UART_INT_ENABLE            TRUE

#if !defined( NPI_UART_BR )
#define NPI_UART_BR                    HAL_UART_BR_19200//HAL_UART_BR_115200
#endif // !NPI_UART_BR

/*******************************************************************************
 * TYPEDEFS
 */

typedef void (*npiCBack_t) ( uint8 port, uint8 event );

/*******************************************************************************
 * LOCAL VARIABLES
 */

/*******************************************************************************
 * GLOBAL VARIABLES
 */

/*******************************************************************************
 * FUNCTIONS
 */

//
// Network Processor Interface APIs
//

extern void   NPI_InitTransport( npiCBack_t npiCBack );
extern uint16 NPI_ReadTransport( uint8 *buf, uint16 len );
extern uint16 NPI_WriteTransport( uint8 *, uint16 );
extern uint16 NPI_RxBufLen( void );
extern uint16 NPI_GetMaxRxBufSize( void );
extern uint16 NPI_GetMaxTxBufSize( void );

/*******************************************************************************
*/

#ifdef __cplusplus
}
#endif

#endif /* NPI_H */

9.將上面的npi檔案新增到工程,就可以了。

———-以此記錄,2016年8月22日