6、CC2541修改按键调节广播发送功率例程为持续发送4DB的蓝牙基站
一、目的
在 OSAL操作系统-实验31 从机广播功率修改-(20141029更新).zip 基础上进行修改,该工程是通过5向按键的上下按键来控制广播功率的加减,总共有4个档位。我们的目的是直接用最高功率进行广播。
二、涉及文件
下面几个文件是每个CC2541工程的主要文件(基本的修改都会在此):
其中:
- xxx_Main.c只有main函数,
- OSAL_SimpleBLEPeripheral.c中只有任务初始化函数,
- simpleBLEPeripheral.c才是重点
三、如何修改
整个工程是采用默认发射功率,然后通过有向按键控制gTxPower大小并通过HCI_EXT_SetTxPowerCmd(gTxPower);重新设置发射功率大小:
1 if ( keys & HAL_KEY_UP ) 2 { 3 HalLcdWriteString( "HAL_KEY_UP", HAL_LCD_LINE_5 ); 4 5 /* 6 #define LL_EXT_TX_POWER_MINUS_23_DBM 0 // -23dbm 功率 最小 7 #define LL_EXT_TX_POWER_MINUS_6_DBM 1 // -6dbm 8 #define LL_EXT_TX_POWER_0_DBM 2 // 0dbm 9 #define LL_EXT_TX_POWER_4_DBM 3 // +dbm 功率 最大 10 */ 11 if(gTxPower < LL_EXT_TX_POWER_4_DBM) 12 { 13 gTxPower++; //功率提高一档 14 HCI_EXT_SetTxPowerCmd(gTxPower); 15 16 HalLcdWriteStringValue( "TxPower: ", gTxPower, 10, HAL_LCD_LINE_7 ); 17 } 18 }
因此,我们只要在GAPROLE_ADVERTISING事件下调用HCI_EXT_SetTxPowerCmd(gTxPower);将默认广播发送功率设置为4db即可(同时也要设置默认广播发送功率为4db:uint8 gTxPower = LL_EXT_TX_POWER_4_DBM;)!
1 case GAPROLE_CONNECTED: 2 { 3 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 4 HalLcdWriteString( "Connected", HAL_LCD_LINE_3 ); 5 HCI_EXT_SetTxPowerCmd(gTxPower); 6 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 7 } 8 break;
修改后simpleBLEPeripheral.c为:
1 /************************************************************************************************** 2 Filename: simpleBLEPeripheral.c 3 Revised: $Date: 2010-08-06 08:56:11 -0700 (Fri, 06 Aug 2010) $ 4 Revision: $Revision: 23333 $ 5 6 Description: This file contains the Simple BLE Peripheral sample application 7 for use with the CC2540 Bluetooth Low Energy Protocol Stack. 8 9 Copyright 2010 - 2013 Texas Instruments Incorporated. All rights reserved. 10 11 IMPORTANT: Your use of this Software is limited to those specific rights 12 granted under the terms of a software license agreement between the user 13 who downloaded the software, his/her employer (which must be your employer) 14 and Texas Instruments Incorporated (the "License"). You may not use this 15 Software unless you agree to abide by the terms of the License. The License 16 limits your use, and you acknowledge, that the Software may not be modified, 17 copied or distributed unless embedded on a Texas Instruments microcontroller 18 or used solely and exclusively in conjunction with a Texas Instruments radio 19 frequency transceiver, which is integrated into your product. Other than for 20 the foregoing purpose, you may not use, reproduce, copy, prepare derivative 21 works of, modify, distribute, perform, display or sell this Software and/or 22 its documentation for any purpose. 23 24 YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE 25 PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, 26 INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, 27 NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL 28 TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, 29 NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER 30 LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES 31 INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE 32 OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT 33 OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES 34 (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. 35 36 Should you have any questions regarding your right to use this Software, 37 contact Texas Instruments Incorporated at www.TI.com. 38 **************************************************************************************************/ 39 40 /********************************************************************* 41 * INCLUDES 42 */ 43 44 #include "bcomdef.h" 45 #include "OSAL.h" 46 #include "OSAL_PwrMgr.h" 47 48 #include "OnBoard.h" 49 #include "hal_adc.h" 50 #include "hal_led.h" 51 #include "hal_key.h" 52 #include "hal_lcd.h" 53 54 #include "gatt.h" 55 56 #include "hci.h" 57 58 #include "gapgattserver.h" 59 #include "gattservapp.h" 60 #include "devinfoservice.h" 61 #include "simpleGATTprofile.h" 62 63 #if defined( CC2540_MINIDK ) 64 #include "simplekeys.h" 65 #endif 66 67 #if defined ( PLUS_BROADCASTER ) 68 #include "peripheralBroadcaster.h" 69 #else 70 #include "peripheral.h" 71 #endif 72 73 #include "gapbondmgr.h" 74 75 #include "simpleBLEPeripheral.h" 76 77 #if defined FEATURE_OAD 78 #include "oad.h" 79 #include "oad_target.h" 80 #endif 81 82 /********************************************************************* 83 * MACROS 84 */ 85 86 /********************************************************************* 87 * CONSTANTS 88 */ 89 90 // How often to perform periodic event 91 #define SBP_PERIODIC_EVT_PERIOD 1000 92 93 // What is the advertising interval when device is discoverable (units of 625us, 160=100ms) 94 #define DEFAULT_ADVERTISING_INTERVAL 160 95 96 // Limited discoverable mode advertises for 30.72s, and then stops 97 // General discoverable mode advertises indefinitely 98 99 #if defined ( CC2540_MINIDK ) 100 #define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_LIMITED 101 #else 102 #define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_GENERAL 103 #endif // defined ( CC2540_MINIDK ) 104 105 // Minimum connection interval (units of 1.25ms, 80=100ms) if automatic parameter update request is enabled 106 #define DEFAULT_DESIRED_MIN_CONN_INTERVAL 80 107 108 // Maximum connection interval (units of 1.25ms, 800=1000ms) if automatic parameter update request is enabled 109 #define DEFAULT_DESIRED_MAX_CONN_INTERVAL 800 110 111 // Slave latency to use if automatic parameter update request is enabled 112 #define DEFAULT_DESIRED_SLAVE_LATENCY 0 113 114 // Supervision timeout value (units of 10ms, 1000=10s) if automatic parameter update request is enabled 115 #define DEFAULT_DESIRED_CONN_TIMEOUT 1000 116 117 // Whether to enable automatic parameter update request when a connection is formed 118 #define DEFAULT_ENABLE_UPDATE_REQUEST TRUE 119 120 // Connection Pause Peripheral time value (in seconds) 121 #define DEFAULT_CONN_PAUSE_PERIPHERAL 6 122 123 // Company Identifier: Texas Instruments Inc. (13) 124 #define TI_COMPANY_ID 0x000D 125 126 #define INVALID_CONNHANDLE 0xFFFF 127 128 // Length of bd addr as a string 129 #define B_ADDR_STR_LEN 15 130 131 #if defined ( PLUS_BROADCASTER ) 132 #define ADV_IN_CONN_WAIT 500 // delay 500 ms 133 #endif 134 135 /********************************************************************* 136 * TYPEDEFS 137 */ 138 139 /********************************************************************* 140 * GLOBAL VARIABLES 141 */ 142 143 /********************************************************************* 144 * EXTERNAL VARIABLES 145 */ 146 147 /********************************************************************* 148 * EXTERNAL FUNCTIONS 149 */ 150 151 /********************************************************************* 152 * LOCAL VARIABLES 153 */ 154 static uint8 simpleBLEPeripheral_TaskID; // Task ID for internal task/event processing 155 156 static gaprole_States_t gapProfileState = GAPROLE_INIT; 157 158 // GAP - SCAN RSP data (max size = 31 bytes) 159 static uint8 scanRspData[] = 160 { 161 // complete name 162 0x14, // length of this data 163 GAP_ADTYPE_LOCAL_NAME_COMPLETE, 164 0x53, // 'S' 165 0x69, // 'i' 166 0x6d, // 'm' 167 0x70, // 'p' 168 0x6c, // 'l' 169 0x65, // 'e' 170 0x42, // 'B' 171 0x4c, // 'L' 172 0x45, // 'E' 173 0x50, // 'P' 174 0x65, // 'e' 175 0x72, // 'r' 176 0x69, // 'i' 177 0x70, // 'p' 178 0x68, // 'h' 179 0x65, // 'e' 180 0x72, // 'r' 181 0x61, // 'a' 182 0x6c, // 'l' 183 184 // connection interval range 185 0x05, // length of this data 186 GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE, 187 LO_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ), // 100ms 188 HI_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ), 189 LO_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ), // 1s 190 HI_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ), 191 192 // Tx power level 193 0x02, // length of this data 194 GAP_ADTYPE_POWER_LEVEL, 195 0 // 0dBm 196 }; 197 198 // GAP - Advertisement data (max size = 31 bytes, though this is 199 // best kept short to conserve power while advertisting) 200 static uint8 advertData[] = 201 { 202 // Flags; this sets the device to use limited discoverable 203 // mode (advertises for 30 seconds at a time) instead of general 204 // discoverable mode (advertises indefinitely) 205 0x02, // length of this data 206 GAP_ADTYPE_FLAGS, 207 DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED, 208 209 // service UUID, to notify central devices what services are included 210 // in this peripheral 211 0x03, // length of this data 212 GAP_ADTYPE_16BIT_MORE, // some of the UUID's, but not all 213 LO_UINT16( SIMPLEPROFILE_SERV_UUID ), 214 HI_UINT16( SIMPLEPROFILE_SERV_UUID ), 215 216 }; 217 218 // GAP - Advertisement data (max size = 31 bytes, though this is 219 // best kept short to conserve power while advertisting) 220 uint8 advertData_Ex[] = 221 { 222 // Flags; this sets the device to use limited discoverable 223 // mode (advertises for 30 seconds at a time) instead of general 224 // discoverable mode (advertises indefinitely) 225 0x02, // length of this data 226 GAP_ADTYPE_FLAGS, 227 DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED, 228 229 // service UUID, to notify central devices what services are included 230 // in this peripheral 231 0x0B, // length of this data 232 //下面总共自定义 8个字节 233 0x00, // 自定义 234 0x00, // 自定义 235 0x00, // 自定义 236 0x00, // 自定义 237 0x00, // 自定义 238 0x00, // 自定义 239 0x00, // 自定义 240 0x00, // 自定义 241 242 GAP_ADTYPE_16BIT_MORE, // some of the UUID's, but not all 243 LO_UINT16( SIMPLEPROFILE_SERV_UUID ), 244 HI_UINT16( SIMPLEPROFILE_SERV_UUID ), 245 246 }; 247 248 249 // GAP GATT Attributes 250 static uint8 attDeviceName[GAP_DEVICE_NAME_LEN] = "Simple BLE Peripheral"; 251 252 /********************************************************************* 253 * LOCAL FUNCTIONS 254 */ 255 static void simpleBLEPeripheral_ProcessOSALMsg( osal_event_hdr_t *pMsg ); 256 static void peripheralStateNotificationCB( gaprole_States_t newState ); 257 static void performPeriodicTask( void ); 258 static void simpleProfileChangeCB( uint8 paramID ); 259 260 //#if defined( CC2540_MINIDK ) 261 static void simpleBLEPeripheral_HandleKeys( uint8 shift, uint8 keys ); 262 //#endif 263 264 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 265 static char *bdAddr2Str ( uint8 *pAddr ); 266 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 267 268 269 270 /********************************************************************* 271 * PROFILE CALLBACKS 272 */ 273 274 // GAP Role Callbacks 275 static gapRolesCBs_t simpleBLEPeripheral_PeripheralCBs = 276 { 277 peripheralStateNotificationCB, // Profile State Change Callbacks 278 NULL // When a valid RSSI is read from controller (not used by application) 279 }; 280 281 // GAP Bond Manager Callbacks 282 static gapBondCBs_t simpleBLEPeripheral_BondMgrCBs = 283 { 284 NULL, // Passcode callback (not used by application) 285 NULL // Pairing / Bonding state Callback (not used by application) 286 }; 287 288 // Simple GATT Profile Callbacks 289 static simpleProfileCBs_t simpleBLEPeripheral_SimpleProfileCBs = 290 { 291 simpleProfileChangeCB // Charactersitic value change callback 292 }; 293 294 /********************************************************************* 295 * PUBLIC FUNCTIONS 296 */ 297 298 /********************************************************************* 299 * @fn SimpleBLEPeripheral_Init 300 * 301 * @brief Initialization function for the Simple BLE Peripheral App Task. 302 * This is called during initialization and should contain 303 * any application specific initialization (ie. hardware 304 * initialization/setup, table initialization, power up 305 * notificaiton ... ). 306 * 307 * @param task_id - the ID assigned by OSAL. This ID should be 308 * used to send messages and set timers. 309 * 310 * @return none 311 */ 312 void SimpleBLEPeripheral_Init( uint8 task_id ) 313 { 314 simpleBLEPeripheral_TaskID = task_id; 315 316 // Setup the GAP 317 VOID GAP_SetParamValue( TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL ); 318 319 // Setup the GAP Peripheral Role Profile 320 { 321 #if defined( CC2540_MINIDK ) 322 // For the CC2540DK-MINI keyfob, device doesn't start advertising until button is pressed 323 uint8 initial_advertising_enable = FALSE; 324 #else 325 // For other hardware platforms, device starts advertising upon initialization 326 uint8 initial_advertising_enable = TRUE; 327 #endif 328 329 // By setting this to zero, the device will go into the waiting state after 330 // being discoverable for 30.72 second, and will not being advertising again 331 // until the enabler is set back to TRUE 332 uint16 gapRole_AdvertOffTime = 0; 333 334 uint8 enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST; 335 uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL; 336 uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL; 337 uint16 desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY; 338 uint16 desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT; 339 340 // Set the GAP Role Parameters 341 GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable ); 342 GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime ); 343 344 GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData ); 345 GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData ); 346 347 GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request ); 348 GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval ); 349 GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval ); 350 GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency ); 351 GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout ); 352 } 353 354 // Set the GAP Characteristics 355 GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName ); 356 357 // Set advertising interval 358 { 359 uint16 advInt = DEFAULT_ADVERTISING_INTERVAL; 360 361 GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, advInt ); 362 GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, advInt ); 363 GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, advInt ); 364 GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, advInt ); 365 } 366 367 // Setup the GAP Bond Manager 368 { 369 uint32 passkey = 0; // passkey "000000" 370 uint8 pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ; 371 uint8 mitm = TRUE; 372 uint8 ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY; 373 uint8 bonding = TRUE; 374 GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof ( uint32 ), &passkey ); 375 GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof ( uint8 ), &pairMode ); 376 GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof ( uint8 ), &mitm ); 377 GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof ( uint8 ), &ioCap ); 378 GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof ( uint8 ), &bonding ); 379 } 380 381 // Initialize GATT attributes 382 GGS_AddService( GATT_ALL_SERVICES ); // GAP 383 GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes 384 DevInfo_AddService(); // Device Information Service 385 SimpleProfile_AddService( GATT_ALL_SERVICES ); // Simple GATT Profile 386 #if defined FEATURE_OAD 387 VOID OADTarget_AddService(); // OAD Profile 388 #endif 389 390 // Setup the SimpleProfile Characteristic Values 391 { 392 uint8 charValue1 = 1; 393 uint8 charValue2 = 2; 394 uint8 charValue3 = 3; 395 uint8 charValue4 = 4; 396 uint8 charValue5[SIMPLEPROFILE_CHAR5_LEN] = { 1, 2, 3, 4, 5 }; 397 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR1, sizeof ( uint8 ), &charValue1 ); 398 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR2, sizeof ( uint8 ), &charValue2 ); 399 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR3, sizeof ( uint8 ), &charValue3 ); 400 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof ( uint8 ), &charValue4 ); 401 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN, charValue5 ); 402 } 403 404 405 #if defined( CC2540_MINIDK ) 406 407 SK_AddService( GATT_ALL_SERVICES ); // Simple Keys Profile 408 409 // Register for all key events - This app will handle all key events 410 RegisterForKeys( simpleBLEPeripheral_TaskID ); 411 412 // makes sure LEDs are off 413 HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF ); 414 415 // For keyfob board set GPIO pins into a power-optimized state 416 // Note that there is still some leakage current from the buzzer, 417 // accelerometer, LEDs, and buttons on the PCB. 418 419 P0SEL = 0; // Configure Port 0 as GPIO 420 P1SEL = 0; // Configure Port 1 as GPIO 421 P2SEL = 0; // Configure Port 2 as GPIO 422 423 P0DIR = 0xFC; // Port 0 pins P0.0 and P0.1 as input (buttons), 424 // all others (P0.2-P0.7) as output 425 P1DIR = 0xFF; // All port 1 pins (P1.0-P1.7) as output 426 P2DIR = 0x1F; // All port 1 pins (P2.0-P2.4) as output 427 428 P0 = 0x03; // All pins on port 0 to low except for P0.0 and P0.1 (buttons) 429 P1 = 0; // All pins on port 1 to low 430 P2 = 0; // All pins on port 2 to low 431 #else 432 // Register for all key events - This app will handle all key events 433 RegisterForKeys( simpleBLEPeripheral_TaskID ); 434 #endif // #if defined( CC2540_MINIDK ) 435 436 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 437 438 #if defined FEATURE_OAD 439 #if defined (HAL_IMAGE_A) 440 HalLcdWriteStringValue( "BLE Peri-A", OAD_VER_NUM( _imgHdr.ver ), 16, HAL_LCD_LINE_1 ); 441 #else 442 HalLcdWriteStringValue( "BLE Peri-B", OAD_VER_NUM( _imgHdr.ver ), 16, HAL_LCD_LINE_1 ); 443 #endif // HAL_IMAGE_A 444 #else 445 HalLcdWriteString( "BLE Peripheral", HAL_LCD_LINE_1 ); 446 #endif // FEATURE_OAD 447 448 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 449 450 // Register callback with SimpleGATTprofile 451 VOID SimpleProfile_RegisterAppCBs( &simpleBLEPeripheral_SimpleProfileCBs ); 452 453 // Enable clock divide on halt 454 // This reduces active current while radio is active and CC254x MCU 455 // is halted 456 HCI_EXT_ClkDivOnHaltCmd( HCI_EXT_ENABLE_CLK_DIVIDE_ON_HALT ); 457 458 #if defined ( DC_DC_P0_7 ) 459 460 // Enable stack to toggle bypass control on TPS62730 (DC/DC converter) 461 HCI_EXT_MapPmIoPortCmd( HCI_EXT_PM_IO_PORT_P0, HCI_EXT_PM_IO_PORT_PIN7 ); 462 463 #endif // defined ( DC_DC_P0_7 ) 464 465 // Setup a delayed profile startup 466 osal_set_event( simpleBLEPeripheral_TaskID, SBP_START_DEVICE_EVT ); 467 } 468 469 /********************************************************************* 470 * @fn SimpleBLEPeripheral_ProcessEvent 471 * 472 * @brief Simple BLE Peripheral Application Task event processor. This function 473 * is called to process all events for the task. Events 474 * include timers, messages and any other user defined events. 475 * 476 * @param task_id - The OSAL assigned task ID. 477 * @param events - events to process. This is a bit map and can 478 * contain more than one event. 479 * 480 * @return events not processed 481 */ 482 uint16 SimpleBLEPeripheral_ProcessEvent( uint8 task_id, uint16 events ) 483 { 484 485 VOID task_id; // OSAL required parameter that isn't used in this function 486 487 if ( events & SYS_EVENT_MSG ) 488 { 489 uint8 *pMsg; 490 491 if ( (pMsg = osal_msg_receive( simpleBLEPeripheral_TaskID )) != NULL ) 492 { 493 simpleBLEPeripheral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg ); 494 495 // Release the OSAL message 496 VOID osal_msg_deallocate( pMsg ); 497 } 498 499 // return unprocessed events 500 return (events ^ SYS_EVENT_MSG); 501 } 502 503 if ( events & SBP_START_DEVICE_EVT ) 504 { 505 // Start the Device 506 VOID GAPRole_StartDevice( &simpleBLEPeripheral_PeripheralCBs ); 507 508 // Start Bond Manager 509 VOID GAPBondMgr_Register( &simpleBLEPeripheral_BondMgrCBs ); 510 511 // Set timer for first periodic event 512 osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD ); 513 514 return ( events ^ SBP_START_DEVICE_EVT ); 515 } 516 517 if ( events & SBP_PERIODIC_EVT ) 518 { 519 // Restart timer 520 if ( SBP_PERIODIC_EVT_PERIOD ) 521 { 522 osal_start_timerEx( simpleBLEPeripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD ); 523 } 524 525 // Perform periodic application task 526 // performPeriodicTask(); 527 528 return (events ^ SBP_PERIODIC_EVT); 529 } 530 531 #if defined ( PLUS_BROADCASTER ) 532 if ( events & SBP_ADV_IN_CONNECTION_EVT ) 533 { 534 uint8 turnOnAdv = TRUE; 535 // Turn on advertising while in a connection 536 GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &turnOnAdv ); 537 538 return (events ^ SBP_ADV_IN_CONNECTION_EVT); 539 } 540 #endif // PLUS_BROADCASTER 541 542 // Discard unknown events 543 return 0; 544 } 545 546 /********************************************************************* 547 * @fn simpleBLEPeripheral_ProcessOSALMsg 548 * 549 * @brief Process an incoming task message. 550 * 551 * @param pMsg - message to process 552 * 553 * @return none 554 */ 555 static void simpleBLEPeripheral_ProcessOSALMsg( osal_event_hdr_t *pMsg ) 556 { 557 switch ( pMsg->event ) 558 { 559 //#if defined( CC2540_MINIDK ) 560 case KEY_CHANGE: 561 simpleBLEPeripheral_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys ); 562 break; 563 //#endif // #if defined( CC2540_MINIDK ) 564 565 default: 566 // do nothing 567 break; 568 } 569 } 570 571 //#if defined( CC2540_MINIDK ) 572 /********************************************************************* 573 * @fn simpleBLEPeripheral_HandleKeys 574 * 575 * @brief Handles all key events for this device. 576 * 577 * @param shift - true if in shift/alt. 578 * @param keys - bit field for key events. Valid entries: 579 * HAL_KEY_SW_2 580 * HAL_KEY_SW_1 581 * 582 * @return none 583 */ 584 585 uint8 gTxPower = LL_EXT_TX_POWER_4_DBM; 586 587 static void simpleBLEPeripheral_HandleKeys( uint8 shift, uint8 keys ) 588 { 589 (void)shift; // Intentionally unreferenced parameter 590 591 HalLcdWriteStringValue( "key = 0x", keys, 16, HAL_LCD_LINE_4 ); 592 593 // smartRF开发板上的S1 对应我们源码上的HAL_KEY_SW_6 594 if ( keys & HAL_KEY_SW_6 ) 595 { 596 HalLcdWriteString( "HAL_KEY_SW_6", HAL_LCD_LINE_5 ); 597 } 598 599 if ( keys & HAL_KEY_UP ) 600 { 601 HalLcdWriteString( "HAL_KEY_UP", HAL_LCD_LINE_5 ); 602 603 /* 604 #define LL_EXT_TX_POWER_MINUS_23_DBM 0 // -23dbm 功率 最小 605 #define LL_EXT_TX_POWER_MINUS_6_DBM 1 // -6dbm 606 #define LL_EXT_TX_POWER_0_DBM 2 // 0dbm 607 #define LL_EXT_TX_POWER_4_DBM 3 // +dbm 功率 最大 608 */ 609 if(gTxPower < LL_EXT_TX_POWER_4_DBM) 610 { 611 gTxPower++; //功率提高一档 612 HCI_EXT_SetTxPowerCmd(gTxPower); 613 614 HalLcdWriteStringValue( "TxPower: ", gTxPower, 10, HAL_LCD_LINE_7 ); 615 } 616 } 617 618 if ( keys & HAL_KEY_LEFT ) 619 { 620 HalLcdWriteString( "HAL_KEY_LEFT", HAL_LCD_LINE_5 ); 621 } 622 623 if ( keys & HAL_KEY_RIGHT ) 624 { 625 HalLcdWriteString( "HAL_KEY_RIGHT", HAL_LCD_LINE_5 ); 626 } 627 628 if ( keys & HAL_KEY_CENTER ) 629 { 630 HalLcdWriteString( "HAL_KEY_CENTER", HAL_LCD_LINE_5 ); 631 } 632 633 if ( keys & HAL_KEY_DOWN ) 634 { 635 HalLcdWriteString( "HAL_KEY_DOWN", HAL_LCD_LINE_5 ); 636 /* 637 #define LL_EXT_TX_POWER_MINUS_23_DBM 0 // -23dbm 功率 最小 638 #define LL_EXT_TX_POWER_MINUS_6_DBM 1 // -6dbm 639 #define LL_EXT_TX_POWER_0_DBM 2 // 0dbm 640 #define LL_EXT_TX_POWER_4_DBM 3 // +dbm 功率 最大 641 */ 642 if(gTxPower > LL_EXT_TX_POWER_MINUS_23_DBM) 643 { 644 gTxPower--; //功率降低一档 645 HCI_EXT_SetTxPowerCmd(gTxPower); 646 647 HalLcdWriteStringValue( "TxPower: ", gTxPower, 10, HAL_LCD_LINE_7 ); 648 } 649 } 650 } 651 //#endif // #if defined( CC2540_MINIDK ) 652 653 /********************************************************************* 654 * @fn peripheralStateNotificationCB 655 * 656 * @brief Notification from the profile of a state change. 657 * 658 * @param newState - new state 659 * 660 * @return none 661 */ 662 static void peripheralStateNotificationCB( gaprole_States_t newState ) 663 { 664 switch ( newState ) 665 { 666 case GAPROLE_STARTED: 667 { 668 uint8 ownAddress[B_ADDR_LEN]; 669 uint8 systemId[DEVINFO_SYSTEM_ID_LEN]; 670 671 GAPRole_GetParameter(GAPROLE_BD_ADDR, ownAddress); 672 673 // use 6 bytes of device address for 8 bytes of system ID value 674 systemId[0] = ownAddress[0]; 675 systemId[1] = ownAddress[1]; 676 systemId[2] = ownAddress[2]; 677 678 // set middle bytes to zero 679 systemId[4] = 0x00; 680 systemId[3] = 0x00; 681 682 // shift three bytes up 683 systemId[7] = ownAddress[5]; 684 systemId[6] = ownAddress[4]; 685 systemId[5] = ownAddress[3]; 686 687 DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId); 688 689 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 690 // Display device address 691 HalLcdWriteString( bdAddr2Str( ownAddress ), HAL_LCD_LINE_2 ); 692 HalLcdWriteString( "Initialized", HAL_LCD_LINE_3 ); 693 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 694 } 695 break; 696 697 case GAPROLE_ADVERTISING: 698 { 699 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 700 HalLcdWriteString( "Advertising", HAL_LCD_LINE_3 ); 701 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 702 } 703 break; 704 705 case GAPROLE_CONNECTED: 706 { 707 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 708 HalLcdWriteString( "Connected", HAL_LCD_LINE_3 ); 709 HCI_EXT_SetTxPowerCmd(gTxPower); 710 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 711 } 712 break; 713 714 case GAPROLE_WAITING: 715 { 716 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 717 HalLcdWriteString( "Disconnected", HAL_LCD_LINE_3 ); 718 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 719 } 720 break; 721 722 case GAPROLE_WAITING_AFTER_TIMEOUT: 723 { 724 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 725 HalLcdWriteString( "Timed Out", HAL_LCD_LINE_3 ); 726 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 727 } 728 break; 729 730 case GAPROLE_ERROR: 731 { 732 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 733 HalLcdWriteString( "Error", HAL_LCD_LINE_3 ); 734 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 735 } 736 break; 737 738 default: 739 { 740 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 741 HalLcdWriteString( "", HAL_LCD_LINE_3 ); 742 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 743 } 744 break; 745 746 } 747 748 gapProfileState = newState; 749 750 #if !defined( CC2540_MINIDK ) 751 VOID gapProfileState; // added to prevent compiler warning with 752 // "CC2540 Slave" configurations 753 #endif 754 755 756 } 757 758 /********************************************************************* 759 * @fn performPeriodicTask 760 * 761 * @brief Perform a periodic application task. This function gets 762 * called every five seconds as a result of the SBP_PERIODIC_EVT 763 * OSAL event. In this example, the value of the third 764 * characteristic in the SimpleGATTProfile service is retrieved 765 * from the profile, and then copied into the value of the 766 * the fourth characteristic. 767 * 768 * @param none 769 * 770 * @return none 771 */ 772 static void performPeriodicTask( void ) 773 { 774 uint8 valueToCopy; 775 uint8 stat; 776 777 // Call to retrieve the value of the third characteristic in the profile 778 stat = SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &valueToCopy); 779 780 if( stat == SUCCESS ) 781 { 782 /* 783 * Call to set that value of the fourth characteristic in the profile. Note 784 * that if notifications of the fourth characteristic have been enabled by 785 * a GATT client device, then a notification will be sent every time this 786 * function is called. 787 */ 788 SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &valueToCopy); 789 } 790 } 791 792 /********************************************************************* 793 * @fn simpleProfileChangeCB 794 * 795 * @brief Callback from SimpleBLEProfile indicating a value change 796 * 797 * @param paramID - parameter ID of the value that was changed. 798 * 799 * @return none 800 */ 801 static void simpleProfileChangeCB( uint8 paramID ) 802 { 803 uint8 newValue; 804 805 switch( paramID ) 806 { 807 case SIMPLEPROFILE_CHAR1: 808 SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &newValue ); 809 810 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 811 HalLcdWriteStringValue( "Char 1:", (uint16)(newValue), 10, HAL_LCD_LINE_3 ); 812 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 813 814 break; 815 816 case SIMPLEPROFILE_CHAR3: 817 SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &newValue ); 818 819 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 820 HalLcdWriteStringValue( "Char 3:", (uint16)(newValue), 10, HAL_LCD_LINE_3 ); 821 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 822 823 break; 824 825 default: 826 // should not reach here! 827 break; 828 } 829 } 830 831 #if (defined HAL_LCD) && (HAL_LCD == TRUE) 832 /********************************************************************* 833 * @fn bdAddr2Str 834 * 835 * @brief Convert Bluetooth address to string. Only needed when 836 * LCD display is used. 837 * 838 * @return none 839 */ 840 char *bdAddr2Str( uint8 *pAddr ) 841 { 842 uint8 i; 843 char hex[] = "0123456789ABCDEF"; 844 static char str[B_ADDR_STR_LEN]; 845 char *pStr = str; 846 847 *pStr++ = '0'; 848 *pStr++ = 'x'; 849 850 // Start from end of addr 851 pAddr += B_ADDR_LEN; 852 853 for ( i = B_ADDR_LEN; i > 0; i-- ) 854 { 855 *pStr++ = hex[*--pAddr >> 4]; 856 *pStr++ = hex[*pAddr & 0x0F]; 857 } 858 859 *pStr = 0; 860 861 return str; 862 } 863 #endif // (defined HAL_LCD) && (HAL_LCD == TRUE) 864 865 /********************************************************************* 866 *********************************************************************/
>>更好的方式应该是寻找默认广播发送功率的原始设置方式
相关链接:
2、利用蓝牙定位及姿态识别实现一个智能篮球场套件(二)——CC2540/CC2541基于广播的RSSI获得
1、利用蓝牙定位及姿态识别实现一个智能篮球场套件(一)——用重写CC2541透传模块做成智能手环
5、CC2541芯片中级教程-OSAL操作系统(PWM+看门狗)
4、CC2541芯片中级教程-OSAL操作系统(简单AT指令实现+IIC软件和硬件实现驱动MPU6050)
3、CC2541芯片中级教程-OSAL操作系统(ADC光敏电阻和修改串口波特率)
2、CC2541芯片中级教程-OSAL操作系统(进一步了解-OLED && 普通按键和5方向按键-中断!!!)这个系统驱动层和应用层不一样~
1、CC2541蓝牙4.0芯片中级教程——基于OSAL操作系统的运行流程了解+定时器和串口例程了解
:: 如果您觉得不错,请推荐给更多人,帮助他们更快地解决实际问题中的坑~
@beautifulzzzz
智能硬件、物联网,热爱技术,关注产品
博客:http://blog.beautifulzzzz.com
园友交流群:414948975