CC2541增加特征值 CHAR6实现串口透传
一、實(shí)現(xiàn)串口
1.1、CC2541的串口簡(jiǎn)介
??CC2541有21個(gè)數(shù)字輸入/輸出引腳, 可以配置為通用數(shù)字I/O或外設(shè)I/O信號(hào), 配置為連接到ADC、 定時(shí)器或 USART外設(shè)。這些 I/O 口的用途可以通過(guò)一系列寄存器配置,由用戶軟件加以實(shí)現(xiàn)。
??對(duì)于 USART 和定時(shí)器 I/O,在一個(gè)數(shù)字 I/O 引腳上選擇外設(shè) I/O 功能,需要設(shè)置對(duì)應(yīng)的 PxSEL 位為 1。
??外設(shè) I/O 引腳映射如下圖所示:
1.2、CC2541串口的寄存器實(shí)現(xiàn)方法
??對(duì)于每個(gè) USART,有 5 個(gè)如下的寄存器(x 是 USART 的編號(hào),為 0 或者1):
??● UxCSR: USARTx 控制和狀態(tài);
??● UxUCR: USARTx UART 控制;
??● UxGCR: USARTx 通用控制
??● UxBUF: USART x 接收/發(fā)送數(shù)據(jù)緩沖
??● UxBAUD: USART x 波特率控
??CC2541 配置串口的一般步驟:
??1、 配置 IO,使用外部設(shè)備功能。
??2、 配置相應(yīng)串口的控制和狀態(tài)寄存器。
??3、 配置串口工作的波特率。
??具體代碼如下:
1.3、串口的API實(shí)現(xiàn)方法
1.3.1、IAR 設(shè)置中添加宏定義
??HAL_UART=TRUE
1.3.2初始化串口
??在npi.c的庫(kù)文件中有串口初始化函數(shù)如下:
/******************************************************************************** @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 UARTuartConfig.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; }為了適應(yīng)工程需求,可修改如下:
void My_NPI_InitTransport( npiCBack_t npiCBack, uint8 baudrate, uint8 parity, uint8 stopbit) {halUARTCfg_t uartConfig;// configure UARTuartConfig.configured = TRUE;uartConfig.baudRate = baudrate;//NPI_UART_BR;uartConfig.parity = parity;uartConfig.stopbit = stopbit;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; }1.3.3、定義串口回調(diào)函數(shù)
??在串口回調(diào)函數(shù)中,可以接收串口數(shù)據(jù)
static void NpiSerialCallback( uint8 port, uint8 events ) {(void)port;//加個(gè) (void),是未了避免編譯告警,明確告訴緩沖區(qū)不用理會(huì)這個(gè)變量if (events & (HAL_UART_RX_TIMEOUT | HAL_UART_RX_FULL)) //串口有數(shù)據(jù){uint8 numBytes = 0;numBytes = NPI_RxBufLen(); //讀出串口緩沖區(qū)有多少字節(jié)if(numBytes == 0){return;}else{uint8 *buffer = osal_mem_alloc(numBytes);//申請(qǐng)緩沖區(qū) bufferif(buffer){ NPI_ReadTransport(buffer,numBytes);//讀取讀取串口緩沖區(qū)數(shù)據(jù),釋放串口數(shù)據(jù)/*......// 數(shù)據(jù)處理 //藍(lán)牙串口透?jìng)鲿r(shí),可以在此處調(diào)用BLE發(fā)送數(shù)據(jù)的方法.......*/osal_mem_free(buffer);//釋放申請(qǐng)的緩沖區(qū)}}} }1.3.4、串口發(fā)送函數(shù)
??串口發(fā)送函數(shù)在npi.c文件內(nèi)定義:
/******************************************************************************** @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 ) ); }二、增加特征值char6
?? 本章節(jié)參考了【BLE】CC2541之添加特征值 要實(shí)現(xiàn)透?jìng)?#xff0c; 我們有個(gè)前提, 就是需要建立連接后,需要對(duì)某個(gè)特征值進(jìn)行寫(xiě)和通知。
2.1、添加 char6 的宏定義
??在simpleGATTprofile.h 文件中增加:
// Profile Parameters #define SIMPLEPROFILE_CHAR1 0 // RW uint8 - Profile Characteristic 1 value #define SIMPLEPROFILE_CHAR2 1 // RW uint8 - Profile Characteristic 2 value #define SIMPLEPROFILE_CHAR3 2 // RW uint8 - Profile Characteristic 3 value #define SIMPLEPROFILE_CHAR4 3 // RW uint8 - Profile Characteristic 4 value #define SIMPLEPROFILE_CHAR5 4 // RW uint8 - Profile Characteristic 4 value #define SIMPLEPROFILE_CHAR6 5 // RW uint8 - Profile Characteristic 5 value// Simple Profile Service UUID #define SIMPLEPROFILE_SERV_UUID 0xFFE0// Key Pressed UUID #define SIMPLEPROFILE_CHAR1_UUID 0xFFE6 #define SIMPLEPROFILE_CHAR2_UUID 0xFFE5 #define SIMPLEPROFILE_CHAR3_UUID 0xFFE4 #define SIMPLEPROFILE_CHAR4_UUID 0xFFE3 #define SIMPLEPROFILE_CHAR5_UUID 0xFFE2 #define SIMPLEPROFILE_CHAR6_UUID 0xFFE1// Simple Keys Profile Services bit fields #define SIMPLEPROFILE_SERVICE 0x00000001// Length of Characteristic 5 in bytes #define SIMPLEPROFILE_CHAR5_LEN 5 #define SIMPLEPROFILE_CHAR6_LEN 20??首先, 增加 CHAR6 的 profile 參數(shù)。
??然后, 增加該特征值的 UUID。
??最后, 增加該特征值的長(zhǎng)度
2.2、添加 char6 的 UUID
??在simpleGATTprofile.c 文件中增加:
// Characteristic 6 UUID: 0xFFE1 CONST uint8 simpleProfilechar6UUID[ATT_BT_UUID_SIZE] = { LO_UINT16(SIMPLEPROFILE_CHAR6_UUID), HI_UINT16(SIMPLEPROFILE_CHAR6_UUID) };??將 16 位的 UUID 拆成 2 個(gè)字節(jié)放到數(shù)組里
2.3、添加 char6 的 的配置屬性
??在simpleGATTprofile.c 文件中增加:
// Simple Profile Characteristic 6 Properties static uint8 simpleProfileChar6Props = GATT_PROP_READ | GATT_PROP_WRITE_NO_RSP | GATT_PROP_NOTIFY;// Characteristic 6 Value static uint8 simpleProfileChar6[SIMPLEPROFILE_CHAR6_LEN] = { 0, 0, 0, 0, 0 }; static uint8 simpleProfileChar6Len = 0;// Simple Profile Characteristic 6 Configuration Each client has its own // instantiation of the Client Characteristic Configuration. Reads of the // Client Characteristic Configuration only shows the configuration for // that client and writes only affect the configuration of that client. static gattCharCfg_t simpleProfileChar6Config[GATT_MAX_NUM_CONN];// Simple Profile Characteristic 6 User Description static uint8 simpleProfileChar6UserDesp[17] = "Characteristic 6\0";2.4、修改屬性表
2.4.1、修改屬性表的大小
??#define SERVAPP_NUM_ATTR_SUPPORTED 21
??增加上面定義的 char6 的 4 個(gè)屬性變量。
2.4.2、修改屬性表
??在simpleProfileAttrTbl數(shù)組中增加
// Characteristic 6 Declaration{ { ATT_BT_UUID_SIZE, characterUUID },GATT_PERMIT_READ, 0,&simpleProfileChar6Props },// Characteristic Value 6{ { ATT_BT_UUID_SIZE, simpleProfilechar6UUID },GATT_PERMIT_READ | GATT_PERMIT_WRITE,0, simpleProfileChar6 },// Characteristic 6 configuration{ { ATT_BT_UUID_SIZE, clientCharCfgUUID },GATT_PERMIT_READ | GATT_PERMIT_WRITE, 0, (uint8 *)simpleProfileChar6Config },// Characteristic 6 User Description{ { ATT_BT_UUID_SIZE, charUserDescUUID },GATT_PERMIT_READ, 0, simpleProfileChar6UserDesp },2.5、修改特征值的參數(shù)函數(shù)
2.5.1、增加 char6 的數(shù)值可設(shè)置的處理
??在simpleGATTprofile.c 的 SimpleProfile_SetParameter函數(shù)中
case SIMPLEPROFILE_CHAR6:if ( len <= SIMPLEPROFILE_CHAR6_LEN ) {VOID osal_memcpy( simpleProfileChar6, value, len );simpleProfileChar6Len = len;// See if Notification has been enabledGATTServApp_ProcessCharCfg( simpleProfileChar6Config, simpleProfileChar6, FALSE,simpleProfileAttrTbl, GATT_NUM_ATTRS( simpleProfileAttrTbl ),INVALID_TASK_ID );}else{ret = bleInvalidRange;}break;2.5.2、增加 char6 的數(shù)值可獲取的處理
??在simpleGATTprofile.c 文件的 SimpleProfile_GetParameter函數(shù)中
case SIMPLEPROFILE_CHAR6:VOID osal_memcpy( value, simpleProfileChar6, simpleProfileChar6Len );*returnBytes = simpleProfileChar6Len;break;2.6、修改特征值的讀寫(xiě)函數(shù)
2.6.1、增加 char6 的數(shù)值讀取的處理
??在simpleGATTprofile.c文件 的 simpleProfile_ReadAttrCB函數(shù)中
case SIMPLEPROFILE_CHAR6_UUID://*Len = SIMPLEPROFILE_CHAR6_LEN;//VOID osal_memcpy( pValue, pAttr->pValue, SIMPLEPROFILE_CHAR6_LEN );LCD_WRITE_STRING_VALUE( "ReadAttrCB 6 len=", simpleProfileChar6Len, 10, HAL_LCD_LINE_1 );*pLen = simpleProfileChar6Len;VOID osal_memcpy( pValue, pAttr->pValue, simpleProfileChar6Len );{// 這個(gè)變量用于表明上一次寫(xiě)數(shù)據(jù)到從機(jī)已經(jīng)成功, 可用于判斷寫(xiě)數(shù)據(jù)時(shí)的判斷, 以確保數(shù)據(jù)的完整性extern bool simpleBLEChar6DoWrite2;simpleBLEChar6DoWrite2 = TRUE;}break;2.6.2、增加 char6 的數(shù)值寫(xiě)入的處理
??在simpleGATTprofile.c文件 的 simpleProfile_WriteAttrCB函數(shù)中
case SIMPLEPROFILE_CHAR6_UUID://Validate the value// Make sure it's not a blob operLCD_WRITE_STRING_VALUE( "WriteAttrCB 6 len=", len, 10, HAL_LCD_LINE_1 );LCD_WRITE_STRING_VALUE( "WriteAttrCB 6 len2=", simpleProfileChar6Len, 10, HAL_LCD_LINE_1 );if ( offset == 0 ){//if ( len != SIMPLEPROFILE_CHAR6_LEN )if ( len > SIMPLEPROFILE_CHAR6_LEN ){status = ATT_ERR_INVALID_VALUE_SIZE;}}else{status = ATT_ERR_ATTR_NOT_LONG;}//Write the valueif ( status == SUCCESS ){//VOID osal_memcpy( pAttr->pValue, pValue, SIMPLEPROFILE_CHAR6_LEN );VOID osal_memcpy( pAttr->pValue, pValue, len );simpleProfileChar6Len = len;notifyApp = SIMPLEPROFILE_CHAR6;}break;2.6.3、增加通知開(kāi)關(guān)的處理
??替換 simpleGATTprofile.c文件 的 simpleProfile_WriteAttrCB 函數(shù)中的 GATT_CLIENT_CHAR_CFG_UUID 部分
case GATT_CLIENT_CHAR_CFG_UUID:status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,offset, GATT_CLIENT_CFG_NOTIFY );break;2.7、增加 char6 的通知開(kāi)關(guān)初始化
??替換 simpleGATTprofile.c文件中 的 SimpleProfile_AddService函數(shù)
/********************************************************************** @fn SimpleProfile_AddService** @brief Initializes the Simple Profile service by registering* GATT attributes with the GATT server.** @param services - services to add. This is a bit map and can* contain more than one service.** @return Success or Failure*/ bStatus_t SimpleProfile_AddService( uint32 services ) {uint8 status = SUCCESS;// Initialize Client Characteristic Configuration attributesGATTServApp_InitCharCfg( INVALID_CONNHANDLE, simpleProfileChar4Config );GATTServApp_InitCharCfg( INVALID_CONNHANDLE, simpleProfileChar6Config );//GATTServApp_InitCharCfg( INVALID_CONNHANDLE, simpleProfileChar7Config );// Register with Link DB to receive link status change callbackVOID linkDB_Register( simpleProfile_HandleConnStatusCB ); if ( services & SIMPLEPROFILE_SERVICE ){// Register GATT attribute list and CBs with GATT Server Appstatus = GATTServApp_RegisterService( simpleProfileAttrTbl, GATT_NUM_ATTRS( simpleProfileAttrTbl ),&simpleProfileCBs );}return ( status ); }2.8 、 增 加 char6 的 通 知 開(kāi) 關(guān) 初 始 化 的 實(shí) 時(shí) 更 新
?? 替 換 simpleGATTprofile.c文件中 的simpleProfile_HandleConnStatusCB 函數(shù)
/********************************************************************** @fn simpleProfile_HandleConnStatusCB** @brief Simple Profile link status change handler function.** @param connHandle - connection handle* @param changeType - type of change** @return none*/ static void simpleProfile_HandleConnStatusCB( uint16 connHandle, uint8 changeType ) { // Make sure this is not loopback connectionif ( connHandle != LOOPBACK_CONNHANDLE ){// Reset Client Char Config if connection has droppedif ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) && ( !linkDB_Up( connHandle ) ) ) ){ GATTServApp_InitCharCfg( connHandle/*INVALID_CONNHANDLE*/, simpleProfileChar4Config );GATTServApp_InitCharCfg( connHandle/*INVALID_CONNHANDLE*/, simpleProfileChar6Config );//GATTServApp_InitCharCfg( connHandle/*INVALID_CONNHANDLE*/, simpleProfileChar7Config );}} }2.9、應(yīng)用層修改
2.9.1、修改特征值初始化的數(shù)值
??在 simpleBLEPeripheral.c文件 的 SimpleBLEPeripheral_Init 函數(shù)
中添加
2.9.2、修改應(yīng)用層的回調(diào)函數(shù)
??在 simpleBLEPeripheral.c文件的 的 simpleProfileChangeCB 函數(shù)中
/********************************************************************** @fn simpleProfileChangeCB** @brief Callback from SimpleBLEProfile indicating a value change** @param paramID - parameter ID of the value that was changed.** @return none*/ static void simpleProfileChangeCB( uint8 paramID ) {uint8 newValue;uint8 newChar6Value[SIMPLEPROFILE_CHAR6_LEN];uint8 returnBytes;switch( paramID ){case SIMPLEPROFILE_CHAR1:SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &newValue, &returnBytes );#if (defined HAL_LCD) && (HAL_LCD == TRUE)HalLcdWriteStringValue( "Char 1:", (uint16)(newValue), 10, HAL_LCD_LINE_3 );#endif // (defined HAL_LCD) && (HAL_LCD == TRUE)break;case SIMPLEPROFILE_CHAR3:SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &newValue, &returnBytes );#if (defined HAL_LCD) && (HAL_LCD == TRUE)HalLcdWriteStringValue( "Char 3:", (uint16)(newValue), 10, HAL_LCD_LINE_3 );#endif // (defined HAL_LCD) && (HAL_LCD == TRUE)break;case SIMPLEPROFILE_CHAR6:SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR6, newChar6Value, &returnBytes );if(returnBytes > 0){/*//此處處理BLE接收的數(shù)據(jù)*/}break;default:// should not reach here!break;} }三、實(shí)現(xiàn)串口0和BLE數(shù)據(jù)透?jìng)?/h2>
3.1、串口接收數(shù)據(jù)并通過(guò)無(wú)線發(fā)送
??在1.3.3章節(jié)定義的串口回調(diào)函數(shù)NpiSerialCallback()中可以接收串口數(shù)據(jù),此時(shí)可以通過(guò)調(diào)用無(wú)線發(fā)送數(shù)據(jù)的函數(shù),將數(shù)據(jù)發(fā)送出去。數(shù)據(jù)發(fā)送函數(shù)如下:
// 處理: 串口發(fā)送過(guò)來(lái)的數(shù)據(jù), 通過(guò)無(wú)線發(fā)送出去 void simpleBLE_Send_Data(uint8 *buf, uint8 numBytes) {if(simpleBLEChar6DoWrite2) //寫(xiě)入成功后再寫(xiě)入{ static attHandleValueNoti_t pReport;pReport.len = numBytes;pReport.handle = 0x0035;osal_memcpy(pReport.value, buf, numBytes);GATT_Notification( 0, &pReport, FALSE ); } }3.2、無(wú)線接收數(shù)據(jù)并通過(guò)串口發(fā)送
??在2.9.2章節(jié)中修改的應(yīng)用層的回調(diào)函數(shù)simpleProfileChangeCB()可以接收無(wú)線數(shù)據(jù),在此處可以將無(wú)線數(shù)據(jù)通過(guò)串口發(fā)送出去。
四、實(shí)現(xiàn)串口1和BLE數(shù)據(jù)透?jìng)?/h2>
4.1、在IAR 設(shè)置中添加宏定義
??ZAPP_P2=TRUE
??如圖所示:
4.2、修改串口配置
??在npi.h文件內(nèi)修改串口配置如下:
/* UART port */ /* #if !defined NPI_UART_PORT #if ((defined HAL_UART_SPI) && (HAL_UART_SPI != 0)) #define NPI_UART_PORT HAL_UART_PORT_1 #else #define NPI_UART_PORT HAL_UART_PORT_0 #endif #endif */#if !defined NPI_UART_PORT #if ((defined HAL_UART_SPI) && (HAL_UART_SPI != 0)) #define NPI_UART_PORT HAL_UART_PORT_1 #else#if (defined ZAPP_P1) #define NPI_UART_PORT HAL_UART_PORT_0#elif (defined ZAPP_P2) #define NPI_UART_PORT HAL_UART_PORT_1#else #define NPI_UART_PORT HAL_UART_PORT_0#endif #endif #endif4.3、添加串口一的IO口初始化代碼
??在1.3.2章節(jié)的串口初始化函數(shù)調(diào)用前添加串口1的IO口初始化代碼,如下:
// 配置io 口功能作為uart功能 { //P1.6 as UART1_TXD,P1.7 as UART1_RXDP1SEL |= 0xC0;PERCFG |= 0x02;P2SEL |= 0x20;}總結(jié)
以上是生活随笔為你收集整理的CC2541增加特征值 CHAR6实现串口透传的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 小满Vue3第三十九章(Vue开发桌面程
- 下一篇: 《CentOS中使用sl(跑火车)命令》