微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > TI无线射频设计 > the ninth week:sensortag原码剖析

the ninth week:sensortag原码剖析

时间:10-02 整理:3721RD 点击:


不说那些参数,因为这个参数是配对用的。也不说那些传感器怎么用地。我想就了解下LED是怎么控制地。

Sensor LED能控制么?答案是肯定地。我用usb dongle时发现用usb dongle可以控制LED。只要控制LED,就可以控制我的继电器。我的设计就完成了。

那么Sensortag又是怎么把LED点亮地呢?这又要看程序啦。

我的程序在:

C:\Texas Instruments\BLE-CC254x-1.4.0\Projects\ble\SensorTag\Source

SensorTag是靠广播联接地。也就是说不用什么密码。

首先传感器之类在回调函数中处理:


  1. /*********************************************************************
  2. * PROFILE CALLBACKS
  3. */

  4. // GAP Role Callbacks
  5. static gapRolesCBs_t sensorTag_PeripheralCBs =
  6. {
  7. peripheralStateNotificationCB, // Profile State Change Callbacks
  8. NULL // When a valid RSSI is read from controller (not used by application)
  9. };

  10. // GAP Bond Manager Callbacks
  11. static gapBondCBs_t sensorTag_BondMgrCBs =
  12. {
  13. NULL, // Passcode callback (not used by application)
  14. NULL // Pairing / Bonding state Callback (not used by application)
  15. };

  16. // Simple GATT Profile Callbacks
  17. static sensorCBs_t sensorTag_BarometerCBs =
  18. {
  19. barometerChangeCB, // Characteristic value change callback
  20. };

  21. static sensorCBs_t sensorTag_IrTempCBs =
  22. {
  23. irTempChangeCB, // Characteristic value change callback
  24. };

  25. static sensorCBs_t sensorTag_AccelCBs =
  26. {
  27. accelChangeCB, // Characteristic value change callback
  28. };

  29. static sensorCBs_t sensorTag_HumidCBs =
  30. {
  31. humidityChangeCB, // Characteristic value change callback
  32. };

  33. static sensorCBs_t sensorTag_MagnetometerCBs =
  34. {
  35. magnetometerChangeCB, // Characteristic value change callback
  36. };

  37. static sensorCBs_t sensorTag_GyroCBs =
  38. {
  39. gyroChangeCB, // Characteristic value change callback
  40. };

  41. static testCBs_t sensorTag_TestCBs =
  42. {
  43. testChangeCB, // Charactersitic value change callback
  44. };

  45. static ccCBs_t sensorTag_ccCBs =
  46. {
  47. ccChangeCB, // Charactersitic value change callback
  48. };

  49. static gapRolesParamUpdateCB_t paramUpdateCB =
  50. {
  51. gapRolesParamUpdateCB,
  52. };

复制代码


处理过程:

  1. /*********************************************************************
  2. * @fn SensorTag_ProcessEvent
  3. *
  4. * @brief Simple BLE Peripheral Application Task event processor. This function
  5. * is called to process all events for the task. Events
  6. * include timers, messages and any other user defined events.
  7. *
  8. * @param task_id - The OSAL assigned task ID.
  9. * @param events - events to process. This is a bit map and can
  10. * contain more than one event.
  11. *
  12. * @return events not processed
  13. */
  14. uint16 SensorTag_ProcessEvent( uint8 task_id, uint16 events )
  15. {
  16. VOID task_id; // OSAL required parameter that isn't used in this function

  17. if ( events & SYS_EVENT_MSG )
  18. {
  19. uint8 *pMsg;

  20. if ( (pMsg = osal_msg_receive( sensorTag_TaskID )) != NULL )
  21. {
  22. sensorTag_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );

  23. // Release the OSAL message
  24. VOID osal_msg_deallocate( pMsg );
  25. }

  26. // return unprocessed events
  27. return (events ^ SYS_EVENT_MSG);
  28. }

  29. // Handle system reset (long press on side key)
  30. if ( events & ST_SYS_RESET_EVT )
  31. {
  32. if (sysResetRequest)
  33. {
  34. HAL_SYSTEM_RESET();
  35. }
  36. return ( events ^ ST_SYS_RESET_EVT );
  37. }

  38. if ( events & ST_START_DEVICE_EVT )
  39. {
  40. // Start the Device
  41. VOID GAPRole_StartDevice( &sensorTag_PeripheralCBs );

  42. // Start Bond Manager
  43. VOID GAPBondMgr_Register( &sensorTag_BondMgrCBs );

  44. return ( events ^ ST_START_DEVICE_EVT );
  45. }

  46. //////////////////////////
  47. // IR TEMPERATURE //
  48. //////////////////////////
  49. if ( events & ST_IRTEMPERATURE_READ_EVT )
  50. {
  51. if ( irTempEnabled )
  52. {
  53. if (HalIRTempStatus() == TMP006_DATA_READY)
  54. {
  55. readIrTempData();
  56. osal_start_timerEx( sensorTag_TaskID, ST_IRTEMPERATURE_READ_EVT, sensorTmpPeriod-TEMP_MEAS_DELAY );
  57. }
  58. else if (HalIRTempStatus() == TMP006_OFF)
  59. {
  60. HalIRTempTurnOn();
  61. osal_start_timerEx( sensorTag_TaskID, ST_IRTEMPERATURE_READ_EVT, TEMP_MEAS_DELAY );
  62. }
  63. }
  64. else
  65. {
  66. //Turn off Temperatur sensor
  67. VOID HalIRTempTurnOff();
  68. VOID resetCharacteristicValue(IRTEMPERATURE_SERV_UUID,SENSOR_DATA,0,IRTEMPERATURE_DATA_LEN);
  69. VOID resetCharacteristicValue(IRTEMPERATURE_SERV_UUID,SENSOR_CONF,ST_CFG_SENSOR_DISABLE,sizeof ( uint8 ));
  70. }

  71. return (events ^ ST_IRTEMPERATURE_READ_EVT);
  72. }

  73. //////////////////////////
  74. // Accelerometer //
  75. //////////////////////////
  76. if ( events & ST_ACCELEROMETER_SENSOR_EVT )
  77. {
  78. if(accConfig != ST_CFG_SENSOR_DISABLE)
  79. {
  80. readAccData();
  81. osal_start_timerEx( sensorTag_TaskID, ST_ACCELEROMETER_SENSOR_EVT, sensorAccPeriod );
  82. }
  83. else
  84. {
  85. VOID resetCharacteristicValue( ACCELEROMETER_SERV_UUID, SENSOR_DATA, 0, ACCELEROMETER_DATA_LEN );
  86. VOID resetCharacteristicValue( ACCELEROMETER_SERV_UUID, SENSOR_CONF, ST_CFG_SENSOR_DISABLE, sizeof ( uint8 ));
  87. }

  88. return (events ^ ST_ACCELEROMETER_SENSOR_EVT);
  89. }

  90. //////////////////////////
  91. // Humidity //
  92. //////////////////////////
  93. if ( events & ST_HUMIDITY_SENSOR_EVT )
  94. {
  95. if (humiEnabled)
  96. {
  97. HalHumiExecMeasurementStep(humiState);
  98. if (humiState == 2)
  99. {
  100. readHumData();
  101. humiState = 0;
  102. osal_start_timerEx( sensorTag_TaskID, ST_HUMIDITY_SENSOR_EVT, sensorHumPeriod );
  103. }
  104. else
  105. {
  106. humiState++;
  107. osal_start_timerEx( sensorTag_TaskID, ST_HUMIDITY_SENSOR_EVT, HUM_FSM_PERIOD );
  108. }
  109. }
  110. else
  111. {
  112. resetCharacteristicValue( HUMIDITY_SERV_UUID, SENSOR_DATA, 0, HUMIDITY_DATA_LEN);
  113. resetCharacteristicValue( HUMIDITY_SERV_UUID, SENSOR_CONF, ST_CFG_SENSOR_DISABLE, sizeof ( uint8 ));
  114. }

  115. return (events ^ ST_HUMIDITY_SENSOR_EVT);
  116. }

  117. //////////////////////////
  118. // Magnetometer //
  119. //////////////////////////
  120. if ( events & ST_MAGNETOMETER_SENSOR_EVT )
  121. {
  122. if(magEnabled)
  123. {
  124. if (HalMagStatus() == MAG3110_DATA_READY)
  125. {
  126. readMagData();
  127. }
  128. else if (HalMagStatus() == MAG3110_OFF)
  129. {
  130. HalMagTurnOn();
  131. }

  132. osal_start_timerEx( sensorTag_TaskID, ST_MAGNETOMETER_SENSOR_EVT, sensorMagPeriod );
  133. }
  134. else
  135. {
  136. HalMagTurnOff();
  137. resetCharacteristicValue( MAGNETOMETER_SERV_UUID, SENSOR_DATA, 0, MAGNETOMETER_DATA_LEN);
  138. resetCharacteristicValue( MAGNETOMETER_SERV_UUID, SENSOR_CONF, ST_CFG_SENSOR_DISABLE, sizeof ( uint8 ));
  139. }

  140. return (events ^ ST_MAGNETOMETER_SENSOR_EVT);
  141. }

  142. //////////////////////////
  143. // Barometer //
  144. //////////////////////////
  145. if ( events & ST_BAROMETER_SENSOR_EVT )
  146. {
  147. if (barEnabled)
  148. {
  149. if (barBusy)
  150. {
  151. barBusy = FALSE;
  152. readBarData();
  153. osal_start_timerEx( sensorTag_TaskID, ST_BAROMETER_SENSOR_EVT, sensorBarPeriod );
  154. }
  155. else
  156. {
  157. barBusy = TRUE;
  158. HalBarStartMeasurement();
  159. osal_start_timerEx( sensorTag_TaskID, ST_BAROMETER_SENSOR_EVT, BAR_FSM_PERIOD );
  160. }
  161. }
  162. else
  163. {
  164. resetCharacteristicValue( BAROMETER_SERV_UUID, SENSOR_DATA, 0, BAROMETER_DATA_LEN);
  165. resetCharacteristicValue( BAROMETER_SERV_UUID, SENSOR_CONF, ST_CFG_SENSOR_DISABLE, sizeof ( uint8 ));
  166. resetCharacteristicValue( BAROMETER_SERV_UUID, SENSOR_CALB, 0, BAROMETER_CALI_LEN);
  167. }

  168. return (events ^ ST_BAROMETER_SENSOR_EVT);
  169. }

  170. //////////////////////////
  171. // Gyroscope //
  172. //////////////////////////
  173. if ( events & ST_GYROSCOPE_SENSOR_EVT )
  174. {
  175. uint8 status;

  176. status = HalGyroStatus();

  177. if(gyroEnabled)
  178. {
  179. if (status == HAL_GYRO_STOPPED)
  180. {
  181. HalGyroSelectAxes(sensorGyroAxes);
  182. HalGyroTurnOn();
  183. osal_start_timerEx( sensorTag_TaskID, ST_GYROSCOPE_SENSOR_EVT, GYRO_STARTUP_TIME);
  184. }
  185. else
  186. {
  187. if(sensorGyroUpdateAxes)
  188. {
  189. HalGyroSelectAxes(sensorGyroAxes);
  190. sensorGyroUpdateAxes = FALSE;
  191. }

  192. if (status == HAL_GYRO_DATA_READY)
  193. {
  194. readGyroData();
  195. osal_start_timerEx( sensorTag_TaskID, ST_GYROSCOPE_SENSOR_EVT, sensorGyrPeriod - GYRO_STARTUP_TIME);
  196. }
  197. else
  198. {
  199. // Gyro needs to be activated;
  200. HalGyroWakeUp();
  201. osal_start_timerEx( sensorTag_TaskID, ST_GYROSCOPE_SENSOR_EVT, GYRO_STARTUP_TIME);
  202. }
  203. }
  204. }
  205. else
  206. {
  207. HalGyroTurnOff();
  208. resetCharacteristicValue( GYROSCOPE_SERV_UUID, SENSOR_DATA, 0, GYROSCOPE_DATA_LEN);
  209. resetCharacteristicValue( GYROSCOPE_SERV_UUID, SENSOR_CONF, ST_CFG_SENSOR_DISABLE, sizeof( uint8 ));
  210. }

  211. return (events ^ ST_GYROSCOPE_SENSOR_EVT);
  212. }

  213. #if defined ( PLUS_BROADCASTER )
  214. if ( events & ST_ADV_IN_CONNECTION_EVT )
  215. {
  216. uint8 turnOnAdv = TRUE;
  217. // Turn on advertising while in a connection
  218. GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &turnOnAdv );

  219. return (events ^ ST_ADV_IN_CONNECTION_EVT);
  220. }
  221. #endif // PLUS_BROADCASTER

  222. // Discard unknown events
  223. return 0;
  224. }

复制代码


我找了很久也没找到LED方面的,又往下盾发现了这个:

  1. /*********************************************************************
  2. * @fn testChangeCB
  3. *
  4. * @brief Callback from Test indicating a value change
  5. *
  6. * @param paramID - parameter ID of the value that was changed.
  7. *
  8. * @return none
  9. */
  10. static void testChangeCB( uint8 paramID )
  11. {
  12. if( paramID == TEST_CONF_ATTR )
  13. {
  14. uint8 newValue;

  15. Test_GetParameter( TEST_CONF_ATTR, &newValue );

  16. if (newValue & TEST_MODE_ENABLE)
  17. {
  18. testMode = TRUE;
  19. }
  20. else
  21. {
  22. testMode = FALSE;
  23. }

  24. if (testMode)
  25. {
  26. // Test mode: possible to operate LEDs. Key hits will cause notifications,
  27. // side key does not influence connection state
  28. if (newValue & 0x01)
  29. {
  30. HalLedSet(HAL_LED_1,HAL_LED_MODE_ON);
  31. }
  32. else
  33. {
  34. HalLedSet(HAL_LED_1,HAL_LED_MODE_OFF);
  35. }

  36. if (newValue & 0x02)
  37. {
  38. HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);
  39. }
  40. else
  41. {
  42. HalLedSet(HAL_LED_2,HAL_LED_MODE_OFF);
  43. }
  44. }
  45. else
  46. {
  47. // Normal mode; make sure LEDs are reset and attribute cleared
  48. HalLedSet(HAL_LED_1,HAL_LED_MODE_OFF);
  49. HalLedSet(HAL_LED_2,HAL_LED_MODE_OFF);
  50. newValue = 0x00;
  51. Test_SetParameter( TEST_CONF_ATTR, 1, &newValue );
  52. }
  53. }
  54. }

复制代码


这个就是LED的原码。从上边可以分析出只要每一比特为1且后边是1的就是第一个LED亮,是2为第二个LED亮,是0为两个全灭。

恩,看来我只要找到一种方法能发出一种广播,并且UUID发对就可以控制LED,把LED换成控制信号,就可控制我的继电器从而控制开关。

感觉离结束不远了。欲知后事如何且听下文分解。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top