cc2640调试经验之 串口添加

 由于项目需要,将cc2541平台移植到cc2640上,需要特别提醒的是,cc2640有三种封装,协议栈默认使用的是7*7的封装,因此如果换成其它封装需要对工程配置进行修改。


特别说明:此串口用来调试打印log ,可以在powersaving下工作。


一、串口驱动添加:

    串口对于调试来说,作用很大,因此第一步先调通串口,方便后期的调试工作。下面以SimpleBLECentral工程为例添加:

    1、在文件 C:\ti\tirtos_simplelink_2_11_01_09\packages\ti\drivers\UART.c 文件末尾添加串口初始化驱动:

//add by gyb


static UART_Handle SbpUartHandle;

static UART_Params SbpUartParams;

    void GYB_UART_OPEN()

    {

    #ifndef GYB_UART_DISABLE

      UART_Params_init(&SbpUartParams);

      SbpUartParams.baudRate      = 115200;

      SbpUartParams.writeDataMode = UART_DATA_BINARY;

      SbpUartParams.dataLength    = UART_LEN_8;

      SbpUartParams.stopBits      = UART_STOP_ONE;

      SbpUartHandle = UART_open(0,&SbpUartParams);

      if (SbpUartHandle) 

      {

      }

    #endif

    }


void printS(uint8 *buf)

{

  UART_write(SbpUartHandle, buf, osal_strlen((char*)buf));

  UART_write(SbpUartHandle, "\r\n",2); //把回车换行也加上,这样就方便多了

}

typedef unsigned long   uint32;


#include "osal.h"

void printV(uint8 *title, uint16 value, uint16 format)

{

  uint8 strLen;

  uint8 buf[256]; 


  osal_memset(buf,0, 256);

  strLen = (uint8)osal_strlen( (char*)title );

  osal_memcpy( buf, title, strLen );

  gyb_ltoa( (uint32)value, &buf[strLen], format );  //数字转字符串

 // strLen = (uint8)osal_strlen( (char*)buf);

 

 // osal_memcpy(&buf[strLen], "\r\n", 2);          //把回车换行也加上,这样就方便多了

  //HalUARTWrite(HAL_UART_PORT_0, buf, strLen+2);

   printS(buf);

}

 2、在文件 C:\ti\tirtos_simplelink_2_11_01_09\packages\ti\drivers\UART.h 中添加: 

    #include "hal_types.h"

    //add by gyb

    //typedef unsigned char   uint8;

    //typedef unsigned short  uint16;

    extern void GYB_UART_OPEN();

    extern void printS(uint8 *buf);

    extern void printV(uint8 *title, uint16 value, uint16 format);

 3、在文件 C:\ti\simplelink\ble_cc26xx_2_00_00_42893\Components\osal\common\OSAL.c 中添加:

    unsigned char * gyb_ltoa(unsigned long l, unsigned char *buf, uint16 radix)

    {

    #if defined( __GNUC__ )

      return ( (char*)ltoa( l, buf, radix ) );

    #else

      unsigned char tmp1[10] = "", tmp2[10] = "", tmp3[10] = "";

      unsigned short num1, num2, num3;

      unsigned char i;

    

      buf[0] = '\0';

    

      if ( radix == (uint8)10 )

      {

        num1 = l % 10000;

        num2 = (l / 10000) % 10000;

        num3 = (unsigned short)(l / 100000000);

    

        if (num3) _itoa(num3, tmp3, 10);

        if (num2) _itoa(num2, tmp2, 10);

        if (num1) _itoa(num1, tmp1, 10);

    

        if (num3)

        {

          strcpy((char*)buf, (char const*)tmp3);

          for (i = 0; i < 4 - strlen((char const*)tmp2); i++)

            strcat((char*)buf, "0");

        }

        strcat((char*)buf, (char const*)tmp2);

        if (num3 || num2)

        {

          for (i = 0; i < 4 - strlen((char const*)tmp1); i++)

            strcat((char*)buf, "0");

        }

        strcat((char*)buf, (char const*)tmp1);

        if (!num3 && !num2 && !num1)

          strcpy((char*)buf, "0");

      }

      else if ( radix == 16 )

      {

        num1 = l & 0x0000FFFF;

        num2 = l >> 16;

    

        if (num2) _itoa(num2, tmp2, 16);

        if (num1) _itoa(num1, tmp1, 16);

    

        if (num2)

        {

          strcpy((char*)buf,(char const*)tmp2);

          for (i = 0; i < 4 - strlen((char const*)tmp1); i++)

            strcat((char*)buf, "0");

        }

        strcat((char*)buf, (char const*)tmp1);

        if (!num2 && !num1)

          strcpy((char*)buf, "0");

      }

      else

        return NULL;

    

      return buf;

    #endif

    }

    4、在文件 C:\ti\simplelink\ble_cc26xx_2_00_00_42893\Components\osal\common\OSAL.h 中添加:

      //add by gyb

      unsigned char * gyb_ltoa(unsigned long l, unsigned char *buf, uint16 radix);

    5、在文件simpleBLECentral.c 中添加:

        #include

    

        #ifndef GYB_UART_DISABLE

           GYB_UART_OPEN();

        #endif

    6、在工程配置options-》c/c++ compiler-》preprocessor 中修改 Defined Symbols:

        TI_DRIVERS_LCD_INCLUDED-》xTI_DRIVERS_LCD_INCLUDED

        添加:

        NPI_USE_UART 

     7、在cc2640App工作空间中的Drivers组中新建UART组,往UART组中添加两个文件:

        C:\ti\tirtos_simplelink_2_11_01_09\packages\ti\drivers\UART.c

        C:\ti\simplelink\ble_cc26xx_2_00_00_42893\Components\osal\common\OSAL.c


    8、至此 uart可用了,另外需要特别说明两点:一是cc2640的每个DIO都可以配置成uart脚,因此需要根据原理图修改工程中Uart的管脚,根据芯片封装,在对应的Board.h中进行修改。二是,在调用GYB_UART_OPEN();初始化串口后,不要立即调用print函数打印,需有一定延时。

你可能感兴趣的:(低功耗蓝牙)