linux驅動由淺入深系列:高通sensor架構實例分析之二(驅動代碼結構)

上一篇文章中我們瞭解了高通sensor的整體架構及對AP側的代碼進行了分析,這篇文章我們詳細分析一下aDSP側的代碼結構。

 

sensor數據流關鍵代碼概覽

下圖是sensor數據流程中的關鍵代碼部分:

 

實現sensor驅動最重要的一個結構體

結合上一篇的測試代碼,可以清楚的看到高通sensor的數據處理流程。圖中7位置指示了每個基於ADSP架構的傳感器需要實現的接口如下:


 
  1. typedef struct

  2. {

  3. /**

  4. * @brief Initializes the driver and sets up devices.

  5. *

  6. * Allocates a handle to a driver instance, opens a communication port to

  7. * associated devices, configures the driver and devices, and places

  8. * the devices in the default power state. Returns the instance handle along

  9. * with a list of supported sensors. This function will be called at init

  10. * time.

  11. *

  12. * @param[out] dd_handle_ptr Pointer that this function must malloc and

  13. * populate. This is a handle to the driver

  14. * instance that will be passed in to all other

  15. * functions. NB: Do not use @a memhandler to

  16. * allocate this memory.

  17. * @param[in] smgr_handle Handle used to identify this driver when it

  18. * calls into Sensors Manager functions.

  19. * @param[in] nv_params NV parameters retrieved for the driver.

  20. * @param[in] device_info Access info for physical devices controlled by

  21. * this driver. Used to configure the bus

  22. * and talk to the devices.

  23. * @param[in] num_devices Number of elements in @a device_info.

  24. * @param[in] memhandler Memory handler used to dynamically allocate

  25. * output parameters, if applicable. NB: Do not

  26. * use memhandler to allocate memory for

  27. * @a dd_handle_ptr.

  28. * @param[in/out] sensors List of supported sensors, allocated,

  29. * populated, and returned by this function.

  30. * @param[in/out] num_sensors Number of elements in @a sensors.

  31. *

  32. * @return Success if @a dd_handle_ptr was allocated and the driver was

  33. * configured properly. Otherwise a specific error code is returned.

  34. */

  35. sns_ddf_status_e (*init)(

  36. sns_ddf_handle_t* dd_handle_ptr,

  37. sns_ddf_handle_t smgr_handle,

  38. sns_ddf_nv_params_s* nv_params,

  39. sns_ddf_device_access_s device_info[],

  40. uint32_t num_devices,

  41. sns_ddf_memhandler_s* memhandler,

  42. sns_ddf_sensor_e** sensors,

  43. uint32_t* num_sensors);

  44.  
  45. /**

  46. * @brief Retrieves a single set of sensor data.

  47. *

  48. * Requests a single sample of sensor data from each of the specified

  49. * sensors. Data is returned in one of two ways: (1) immediately after being

  50. * read from the sensor, in which case data is populated in the same order

  51. * it was requested, or (2) in cases where the sensor requires several steps

  52. * to be read, this function will return with the status SNS_DDF_PENDING,

  53. * and provide the data asynchronously via @a sns_ddf_smgr_data_notify()

  54. * when it is ready. Note that @a sns_ddf_smgr_data_notify() must be called

  55. * even in the event of an error in order to report a failed status. An

  56. * asynchronous notification is also expected in the case of mixed data

  57. * (i.e. synchronous and asynchronous).

  58. *

  59. * @note In the case where multiple sensors are requested, the driver must

  60. * attempt to collect data from all requested sensors, meaning that

  61. * the time it takes to execute this function will be determined by

  62. * the number of sensors sampled, and their various delays. Drivers

  63. * must never return partial responses. If a sensor has failed or

  64. * isn't available, @a sns_ddf_sensor_data_s.status must be used to

  65. * reflect this status.

  66. *

  67. * @param[in] dd_handle Handle to a driver instance.

  68. * @param[in] sensors List of sensors for which data is requested.

  69. * @param[in] num_sensors Number of elements in @a sensors.

  70. * @param[in] memhandler Memory handler used to dynamically allocate

  71. * output parameters, if applicable.

  72. * @param[out] data Sampled sensor data. The number of elements must

  73. * match @a num_sensors.

  74. *

  75. * @return SNS_DDF_SUCCESS if data was populated successfully. If any of the

  76. * sensors queried are to be read asynchronously SNS_DDF_PENDING is

  77. * returned and data is via @a sns_ddf_smgr_data_notify() when

  78. * available. Otherwise a specific error code is returned.

  79. *

  80. * @see sns_ddf_data_notify()

  81. */

  82. sns_ddf_status_e (*get_data)(

  83. sns_ddf_handle_t dd_handle,

  84. sns_ddf_sensor_e sensors[],

  85. uint32_t num_sensors,

  86. sns_ddf_memhandler_s* memhandler,

  87. sns_ddf_sensor_data_s** data);

  88.  
  89. /**

  90. * @brief Sets a sensor attribute to a specific value.

  91. *

  92. * @param[in] dd_handle Handle to a driver instance.

  93. * @param[in] sensor Sensor for which this attribute is to be set. When

  94. * addressing an attribute that refers to the driver

  95. * this value is set to SNS_DDF_SENSOR__ALL.

  96. * @param[in] attrib Attribute to be set.

  97. * @param[in] value Value to set this attribute.

  98. *

  99. * @return Success if the value of the attribute was set properly. Otherwise

  100. * a specific error code is returned.

  101. */

  102. sns_ddf_status_e (*set_attrib)(

  103. sns_ddf_handle_t dd_handle,

  104. sns_ddf_sensor_e sensor,

  105. sns_ddf_attribute_e attrib,

  106. void* value);

  107.  
  108. /**

  109. * @brief Retrieves the value of an attribute for a sensor.

  110. *

  111. * @param[in] dd_handle Handle to a driver instance.

  112. * @param[in] sensor Sensor whose attribute is to be retrieved. When

  113. * addressing an attribute that refers to the driver

  114. * this value is set to SNS_DDF_SENSOR__ALL.

  115. * @param[in] attrib Attribute to be retrieved.

  116. * @param[in] memhandler Memory handler used to dynamically allocate

  117. * output parameters, if applicable.

  118. * @param[out] value Pointer that this function will allocate or set

  119. * to the attribute's value.

  120. * @param[out] num_elems Number of elements in @a value.

  121. *

  122. * @return Success if the attribute was retrieved and the buffer was

  123. * populated. Otherwise a specific error code is returned.

  124. */

  125. sns_ddf_status_e (*get_attrib)(

  126. sns_ddf_handle_t dd_handle,

  127. sns_ddf_sensor_e sensor,

  128. sns_ddf_attribute_e attrib,

  129. sns_ddf_memhandler_s* memhandler,

  130. void** value,

  131. uint32_t* num_elems);

  132.  
  133. /**

  134. * @brief Called when the timer set by this driver has expired. This must be

  135. * the callback function submitted when initializing a timer.

  136. *

  137. * @note This will be called within the context of the Sensors Manager task.

  138. *

  139. * @param[in] dd_handle Handle to a driver instance.

  140. * @param[in] arg The argument submitted when the timer was set.

  141. *

  142. * @see sns_ddf_set_timer()

  143. */

  144. void (*handle_timer)(sns_ddf_handle_t dd_handle, void* arg);

  145.  
  146. /**

  147. * @brief Called in response to an interrupt for this driver.

  148. *

  149. * @note This function will be called within the context of the SMGR task,

  150. * *not* the ISR.

  151. *

  152. * @param[in] dd_handle Handle to a driver instance.

  153. * @param[in] gpio_num GPIO number that triggered this interrupt.

  154. * @param[in] timestamp Time at which interrupt happened.

  155. */

  156. void (*handle_irq)(

  157. sns_ddf_handle_t dd_handle,

  158. uint32_t gpio_num,

  159. sns_ddf_time_t timestamp);

  160.  
  161. /**

  162. * @brief Resets the driver and device so they return to the state they were

  163. * in after init() was called.

  164. *

  165. * @param[in] dd_handle Handle to a driver instance.

  166. *

  167. * @return Success if the driver was able to reset its state and the device.

  168. * Otherwise a specific error code is returned.

  169. */

  170. sns_ddf_status_e (*reset)(sns_ddf_handle_t dd_handle);

  171.  
  172. /**

  173. * @brief Runs a factory test case.

  174. *

  175. * Tests may include embedded hardware tests in cases where the sensor

  176. * supports it, as well as driver based sensor tests. This is generally run

  177. * in a factory setting and must not be called while a device is streaming

  178. * data.

  179. *

  180. * @param[in] dd_handle Handle to a driver instance.

  181. * @param[in] sensor Sensor on which to run the test.

  182. * @param[in] test Test case to run.

  183. * @param[out] err Optional driver-specific error code.

  184. *

  185. * @return One of the following error codes:

  186. * SNS_DDF_SUCCESS - Test passed.

  187. * SNS_DDF_PENDING - Test result will be sent as an event.

  188. * SNS_DDF_EDEVICE_BUSY - Device is busy streaming, cannot run test.

  189. * SNS_DDF_EINVALID_TEST - Test is not defined for this sensor.

  190. * SNS_DDF_EINVALID_PARAM - One of the parameters is invalid.

  191. * SNS_DDF_EFAIL - Unknown error occurred.

  192. */

  193. sns_ddf_status_e (*run_test)(

  194. sns_ddf_handle_t dd_handle,

  195. sns_ddf_sensor_e sensor,

  196. sns_ddf_test_e test,

  197. uint32_t* err);

  198.  
  199. /**

  200. * @brief Begins device-scheduled sampling and enables notification via Data

  201. * Ready Interrupts (DRI).

  202. *

  203. * The driver commands the device to begin sampling at the configured

  204. * ODR (@a SNS_DDF_ATTRIB_ODR) and enables DRI. When data is ready, the

  205. * driver's handle_irq() function is called and the driver notifies

  206. * SMGR of the event via @a sns_ddf_smgr_notify_event() and @a

  207. * SNS_DDF_EVENT_DATAREADY.

  208. *

  209. * @param[in] handle Handle to the driver's instance.

  210. * @param[in] sensor Sensor to be sampled.

  211. * @param[in] enable True to enable or false to disable data stream.

  212. *

  213. * @return SNS_DDF_SUCCESS if sensor was successfully configured and

  214. * internal sampling has commenced or ceased. Otherwise an

  215. * appropriate error code.

  216. */

  217. sns_ddf_status_e (*enable_sched_data)(

  218. sns_ddf_handle_t handle,

  219. sns_ddf_sensor_e sensor,

  220. bool enable);

  221.  
  222. /**

  223. * @brief Probes for the device with a given configuration.

  224. *

  225. * This commands the driver to look for the device with the specified

  226. * configuration (ie, I2C address/bus defined in the sns_ddf_device_access_s

  227. * struct.

  228. *

  229. * @param[in] dev_info Access info for physical devices controlled by

  230. * this driver. Used to determine if the device is

  231. * physically present.

  232. * @param[in] memhandler Memory handler used to dynamically allocate

  233. * output parameters, if applicable.

  234. * @param[out] num_sensors Number of sensors supported. 0 if none.

  235. * @param[out] sensor_type Array of sensor types supported, with num_sensor

  236. * elements. Allocated by this function.

  237. *

  238. * @return SNS_DDF_SUCCESS if the part was probed function completed, even

  239. * if no device was found (in which case num_sensors will be set to

  240. * 0).

  241. */

  242. sns_ddf_status_e(*probe)(

  243. sns_ddf_device_access_s* device_info,

  244. sns_ddf_memhandler_s* memhandler,

  245. uint32_t* num_sensors,

  246. sns_ddf_sensor_e** sensors );

  247.  
  248.  
  249. /**

  250. * @brief Retrieves a set of sensor data. Asynchronous API

  251. *

  252. * Requests sample of sensor data from the specified sensor.

  253. *

  254. * @note If a sensor has failed or

  255. * isn't available, @a sns_ddf_sensor_data_s.status must be used to

  256. * reflect this status.

  257. *

  258. * @param[in] dd_handle Handle to a driver instance.

  259. * @param[in] sensor sensor for which data is requested.

  260. *

  261. * @param[in] num_samples number of samples to retrieve as available. Drain the FIFO if value is set to Zero.

  262. * @param[in] trigger now trigger notify fifo data now or

  263. * later when trigger_now is set to true.

  264. *

  265. *

  266. * @return SNS_DDF_SUCCESS if data was populated successfully.

  267. * via sns_ddf_smgr_data_notify() or if trigger_now is

  268. * set to false; Otherwise a specific error code is

  269. * returned.

  270. *

  271. * @see sns_ddf_data_notify_data() as this will be used to report the data.

  272. */

  273. sns_ddf_status_e (*trigger_fifo_data)(

  274. sns_ddf_handle_t dd_handle,

  275. sns_ddf_sensor_e sensor,

  276. uint16_t num_samples,

  277. bool trigger_now);

  278.  
  279.  
  280. /**

  281. * @brief Delivers a Driver Access Framework message to the driver.

  282. * Asynchronous/Synchronous API.

  283. *

  284. * @detail

  285. *

  286. * @param[in] dd_handle Handle to a driver instance.

  287. * @param[in] req_id Request identifier.

  288. * @param[in] req_msg Request message in the opaque payload. If no

  289. * payload is supplied, then this pointer will be

  290. * null.

  291. * @param[in] req_size Number of bytes in @req_msg. If req_msg is empty,

  292. * this value must be 0.

  293. * @param[in] memhandler Memory handler used to dynamically allocate

  294. * output parameters, if applicable.

  295. * @param[out] resp_msg Pointer to the output message pointer. The output

  296. * message must be allocated first using @memhandler.

  297. * @param[out] resp_size Pointer to number of bytes in @resp_msg. If there

  298. * is no DAF response message for the request, then

  299. * this must be 0 to show that the DAF response is

  300. * not present. Response messages are limited in

  301. * size to @SNS_SMGR_MAX_DAF_MESSAGE_SIZE_V01 bytes.

  302. * Any response message larger than

  303. * @SNS_SMGR_MAX_DAF_MESSAGE_SIZE_V01 bytes will be

  304. * truncated.

  305. * @param[in] trans_id_ptr Pointer to the optional transaction identifier.

  306. This will be null if a transaction ID was not

  307. provided.

  308. * @param[in] conn_handle The connection handle for the request message.

  309. * This value must be saved if the particular request

  310. * is expected to generate indications. Upon

  311. * notifying the SMGR of an indication, this value

  312. * must be provided to the SMGR.

  313. *

  314. * @return Success if the message was retrieved and the buffer was correctly

  315. * populated. Otherwise a specific error code is returned.

  316. */

  317. sns_ddf_status_e (*process_daf_req)(

  318. sns_ddf_handle_t dd_handle,

  319. uint32_t req_id,

  320. const void* req_msg,

  321. uint32_t req_size,

  322. sns_ddf_memhandler_s* memhandler,

  323. void** resp_msg,

  324. uint32_t* resp_size,

  325. const uint8_t* trans_id_ptr,

  326. void* conn_handle);

  327.  
  328.  
  329. /**

  330. * @brief Cancels all of the driver's current Driver Access Framework

  331. * asynchronous transactions for the provided connection handle.

  332. *

  333. * @note This does not have to cancel a response message in the process of

  334. * being created.

  335. * This function does not have to be implemented for drivers that do

  336. * not support or implement any asynchronous messages (these messages

  337. * require the usage of sns_ddf_smgr_notify_daf_ind).

  338. *

  339. * @param[in] dd_handle Handle to a driver instance.

  340. * @param[in] conn_handle The connection handle for the client that is

  341. * cancelling the Driver Access Framework

  342. * transaction.

  343. */

  344. void (*cancel_daf_trans)(

  345. sns_ddf_handle_t dd_handle,

  346. void* conn_handle);

  347.  
  348. } sns_ddf_driver_if_s;

 

 

aDSP初始化流程

 

aDSP的初始化工作從[Sns_init_dsps.c]文件中的  sns_init()函數開始,其中調用  ->    sns_init_once(); -> SNS_INIT_FUNCTIONS存在一個各個模塊的初始化函數指針列表,依次調用各個模塊的初始化函數init_ptrs[i]()          -> 其中我們關注傳感器相關的[sns_smgr_main_uimg.c]sns_smgr_init() ->        創建了 [sns_smgr_main.c]sns_smgr_task() 進程 ->sns_smgr_hw_init(); ->sns_smgr_process_msg(); ->sns_smgr_process_reg_resp_msg();         ->sns_smgr_process_reg_data() ->sns_smgr_process_reg_devinfo() ->sns_smgr_parse_reg_devinfo_resp() -> 通過drv_fn_ptr->probe()指針,調用相應傳感器實現的probe函數。如果某傳感器沒有實現probe函數,則調用sns_smgr_populate_cfg_from_devinfo()。

 

aDSP上報傳感器數據

 

 

Sensor上報數據的三種方式:

1,  (Polling)0x00調用一次get_data後啓動timer,等到timer到時間後調用sns_ddf_driver_if_s中指定的handle_timer()函數上報一組傳感器數據

2,  (DRI)0x80調用enable_sched_data()啓用DRI(Data ReadyInterrupt,數據完成中斷),按照set_cycle_time指定的ODR(Output Data Rate,數據輸出速率)進行數據採集,採集完成後調用sns_ddf_driver_if_s中指定的handle_irq()函數上報傳感器數據。

3,  (FIFO)0xD0調用trigger_fifo_data()函數啓動FIFO模式,當數據量到達指定的閾值,觸發sns_ddf_smgr_data_notify()函數上報一批數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章