/********************************************************************* * @fn SimpleBLECentral_Init * * @brief Initialization function for the Simple BLE Central App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notification). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void SimpleBLECentral_Init( uint8 task_id ) { simpleBLETaskId = task_id; // 串口初始化 NPI_InitTransport(uart_NpiSerialCallback); //NPI_WriteTransport("SimpleBLECentral_Init\r\n", 23); Get_IMEI(); //halSleep(100); //Get_Test(); // Setup Central Profile { uint8 scanRes = DEFAULT_MAX_SCAN_RES; GAPCentralRole_SetParameter ( GAPCENTRALROLE_MAX_SCAN_RES, sizeof( uint8 ), &scanRes ); } // Setup GAP GAP_SetParamValue( TGAP_GEN_DISC_SCAN, DEFAULT_SCAN_DURATION ); GAP_SetParamValue( TGAP_LIM_DISC_SCAN, DEFAULT_SCAN_DURATION ); GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, (uint8 *) simpleBLEDeviceName ); // Setup the GAP Bond Manager { uint32 passkey = DEFAULT_PASSCODE; uint8 pairMode = DEFAULT_PAIRING_MODE; uint8 mitm = DEFAULT_MITM_MODE; uint8 ioCap = DEFAULT_IO_CAPABILITIES; uint8 bonding = DEFAULT_BONDING_MODE; GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof( uint32 ), &passkey ); GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof( uint8 ), &pairMode ); GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof( uint8 ), &mitm ); GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof( uint8 ), &ioCap ); GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof( uint8 ), &bonding ); } // Initialize GATT Client VOID GATT_InitClient(); // Register to receive incoming ATT Indications/Notifications GATT_RegisterForInd( simpleBLETaskId ); // Initialize GATT attributes GGS_AddService( GATT_ALL_SERVICES ); // GAP GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes // Register for all key events - This app will handle all key events RegisterForKeys( simpleBLETaskId ); // makes sure LEDs are off HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF ); // Setup a delayed profile startup osal_set_event( simpleBLETaskId, START_DEVICE_EVT ); }
/********************************************************************* * @fn SimpleBLEPeripheral_Init * * @brief Initialization function for the Simple BLE Peripheral App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notificaiton ... ). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void Biscuit_Init( uint8 task_id ) { biscuit_TaskID = task_id; // Setup the GAP VOID GAP_SetParamValue( TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL ); // Setup the GAP Peripheral Role Profile { // Device starts advertising upon initialization uint8 initial_advertising_enable = TRUE; // By setting this to zero, the device will go into the waiting state after // being discoverable for 30.72 second, and will not being advertising again // until the enabler is set back to TRUE uint16 gapRole_AdvertOffTime = 0; uint8 enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST; uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL; uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL; uint16 desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY; uint16 desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT; // Set the GAP Role Parametersuint8 initial_advertising_enable = TRUE; GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable ); GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime ); GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData ); GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request ); GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval ); GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval ); GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency ); GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout ); } i2c_init(); // Set the GAP Characteristics uint8 nameFlag = eeprom_read(4); uint8 nameLen = eeprom_read(5); if( (nameFlag!=1) || (nameLen>20) ) // First time power up after burning firmware { GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName ); uint8 len = strlen( (char const *)attDeviceName ); TXRX_SetParameter( DEV_NAME_CHAR, len, attDeviceName ); eeprom_write(4, 1); eeprom_write(5, len); for(uint8 i=0; i<len; i++) { eeprom_write(i+8, attDeviceName[i]); } } else { uint8 devName[GAP_DEVICE_NAME_LEN]; for(uint8 i=0; i<nameLen; i++) { devName[i] = eeprom_read(i+8); } devName[nameLen] = '\0'; GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, devName ); TXRX_SetParameter( DEV_NAME_CHAR, nameLen, devName ); } uint8 LocalName[GAP_DEVICE_NAME_LEN]; nameLen = eeprom_read(5); for(uint8 i=0; i<nameLen; i++) { LocalName[i] = eeprom_read(i+8); } advertData[3] = nameLen + 1; osal_memcpy(&advertData[5], LocalName, nameLen); osal_memset(&advertData[nameLen+5], 0, 31-5-nameLen); GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData ); // Set advertising interval { uint16 advInt = DEFAULT_ADVERTISING_INTERVAL; GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, advInt ); GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, advInt ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, advInt ); GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, advInt ); } // Setup the GAP Bond Manager { uint32 passkey = 0; // passkey "000000" uint8 pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ; uint8 mitm = TRUE; uint8 ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY; uint8 bonding = TRUE; GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof ( uint32 ), &passkey ); GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof ( uint8 ), &pairMode ); GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof ( uint8 ), &mitm ); GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof ( uint8 ), &ioCap ); GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof ( uint8 ), &bonding ); } // Initialize GATT attributes GGS_AddService( GATT_ALL_SERVICES ); // GAP GATTServApp_AddService( GATT_ALL_SERVICES ); // GATT attributes //DevInfo_AddService(); // Device Information Service TXRX_AddService( GATT_ALL_SERVICES ); // Simple GATT Profile #if defined FEATURE_OAD VOID OADTarget_AddService(); // OAD Profile #endif #if defined( CC2540_MINIDK ) // Register for all key events - This app will handle all key events RegisterForKeys( biscuit_TaskID ); // makes sure LEDs are off HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF ); // For keyfob board set GPIO pins into a power-optimized state // Note that there is still some leakage current from the buzzer, // accelerometer, LEDs, and buttons on the PCB. P0SEL = 0; // Configure Port 0 as GPIO P1SEL = 0; // Configure Port 1 as GPIO P2SEL = 0; // Configure Port 2 as GPIO P0DIR = 0xFC; // Port 0 pins P0.0 and P0.1 as input (buttons), // all others (P0.2-P0.7) as output P1DIR = 0xFF; // All port 1 pins (P1.0-P1.7) as output P2DIR = 0x1F; // All port 1 pins (P2.0-P2.4) as output P0 = 0x03; // All pins on port 0 to low except for P0.0 and P0.1 (buttons) P1 = 0; // All pins on port 1 to low P2 = 0; // All pins on port 2 to low #endif // #if defined( CC2540_MINIDK ) // Register callback with TXRXService VOID TXRX_RegisterAppCBs( &biscuit_TXRXServiceCBs ); // Enable clock divide on halt // This reduces active current while radio is active and CC254x MCU // is halted // HCI_EXT_ClkDivOnHaltCmd( HCI_EXT_ENABLE_CLK_DIVIDE_ON_HALT ); // Initialize serial interface P1SEL = 0x30; P1DIR |= 0x02; P1_1 = 1; PERCFG |= 1; NPI_InitTransport(dataHandler); uint8 flag, baud; uint8 value; flag = eeprom_read(0); baud = eeprom_read(1); if( flag!=1 || baud>4 ) // First time power up after burning firmware { U0GCR &= 0xE0; // Default baudrate 57600 U0GCR |= 0x0A; U0BAUD = 216; value = 3; eeprom_write(0, 1); eeprom_write(1, 3); } else { switch(baud) { case 0: //9600 { U0GCR &= 0xE0; U0GCR |= 0x08; U0BAUD = 59; value = 0; break; } case 1: //19200 { U0GCR &= 0xE0; U0GCR |= 0x09; U0BAUD = 59; value = 1; break; } case 2: //38400 { U0GCR &= 0xE0; U0GCR |= 0x0A; U0BAUD = 59; value = 2; break; } case 3: //57600 { U0GCR &= 0xE0; U0GCR |= 0x0A; U0BAUD = 216; value = 3; break; } case 4: //115200 { U0GCR &= 0xE0; U0GCR |= 0x0B; U0BAUD = 216; value = 4; break; } default: break; } } TXRX_SetParameter( BAUDRATE_CHAR, 1, &value ); uint8 flag2, txpwr; flag2 = eeprom_read(2); txpwr = eeprom_read(3); if( flag2!=1 || txpwr>3 ) // First time power up after burning firmware { HCI_EXT_SetTxPowerCmd( HCI_EXT_TX_POWER_0_DBM ); txpwr = HCI_EXT_TX_POWER_0_DBM; eeprom_write(2, 1); eeprom_write(3, HCI_EXT_TX_POWER_0_DBM); } else { HCI_EXT_SetTxPowerCmd( txpwr ); } TXRX_SetParameter( TX_POWER_CHAR, 1, &txpwr ); // Setup a delayed profile startup osal_set_event( biscuit_TaskID, SBP_START_DEVICE_EVT ); }
void SerialInterface_Init( uint8 task_id ) { serialInterface_TaskID = task_id; NPI_InitTransport(cSerialPacketParser); }