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日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值