LibDriver MMA7660FC
Loading...
Searching...
No Matches
driver_mma7660fc_motion_test.c
Go to the documentation of this file.
1
36
38
39static mma7660fc_handle_t gs_handle;
40static volatile uint16_t gs_flag = 0;
41
47static void a_receive_callback(uint8_t type)
48{
49 switch (type)
50 {
52 {
53 mma7660fc_interface_debug_print("mma7660fc: irq front.\n");
54 gs_flag |= 1 << 0;
55
56 break;
57 }
59 {
60 mma7660fc_interface_debug_print("mma7660fc: irq back.\n");
61 gs_flag |= 1 << 1;
62
63 break;
64 }
66 {
67 mma7660fc_interface_debug_print("mma7660fc: irq left.\n");
68 gs_flag |= 1 << 2;
69
70 break;
71 }
73 {
74 mma7660fc_interface_debug_print("mma7660fc: irq right.\n");
75 gs_flag |= 1 << 3;
76
77 break;
78 }
80 {
81 mma7660fc_interface_debug_print("mma7660fc: irq down.\n");
82 gs_flag |= 1 << 4;
83
84 break;
85 }
87 {
88 mma7660fc_interface_debug_print("mma7660fc: irq up.\n");
89 gs_flag |= 1 << 5;
90
91 break;
92 }
94 {
95 mma7660fc_interface_debug_print("mma7660fc: irq tap.\n");
96 gs_flag |= 1 << 6;
97
98 break;
99 }
101 {
102 mma7660fc_interface_debug_print("mma7660fc: irq update.\n");
103
104 break;
105 }
107 {
108 mma7660fc_interface_debug_print("mma7660fc: irq shake.\n");
109 gs_flag |= 1 << 7;
110
111 break;
112 }
114 {
115 mma7660fc_interface_debug_print("mma7660fc: irq auto sleep.\n");
116 gs_flag |= 1 << 8;
117
118 break;
119 }
121 {
122 mma7660fc_interface_debug_print("mma7660fc: irq auto wake up.\n");
123 gs_flag |= 1 << 9;
124
125 break;
126 }
127 default :
128 {
129 mma7660fc_interface_debug_print("mma7660fc: unknown code.\n");
130
131 break;
132 }
133 }
134}
135
144{
145 if (mma7660fc_irq_handler(&gs_handle) != 0)
146 {
147 return 1;
148 }
149
150 return 0;
151}
152
161uint8_t mma7660fc_motion_test(uint32_t timeout)
162{
163 uint8_t res;
164 uint8_t count;
165 uint32_t i;
166 mma7660fc_info_t info;
167
168 /* link interface function */
176 DRIVER_MMA7660FC_LINK_RECEIVE_CALLBACK(&gs_handle, a_receive_callback);
177
178 /* get mma7660fc info */
179 res = mma7660fc_info(&info);
180 if (res != 0)
181 {
182 mma7660fc_interface_debug_print("mma7660fc: get info failed.\n");
183
184 return 1;
185 }
186 else
187 {
188 /* print mma7660fc */
189 mma7660fc_interface_debug_print("mma7660fc: chip is %s.\n", info.chip_name);
190 mma7660fc_interface_debug_print("mma7660fc: manufacturer is %s.\n", info.manufacturer_name);
191 mma7660fc_interface_debug_print("mma7660fc: interface is %s.\n", info.interface);
192 mma7660fc_interface_debug_print("mma7660fc: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
193 mma7660fc_interface_debug_print("mma7660fc: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
194 mma7660fc_interface_debug_print("mma7660fc: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
195 mma7660fc_interface_debug_print("mma7660fc: max current is %0.2fmA.\n", info.max_current_ma);
196 mma7660fc_interface_debug_print("mma7660fc: max temperature is %0.1fC.\n", info.temperature_max);
197 mma7660fc_interface_debug_print("mma7660fc: min temperature is %0.1fC.\n", info.temperature_min);
198 }
199
200 /* start motion test */
201 mma7660fc_interface_debug_print("mma7660fc: start motion test.\n");
202
203 /* init */
204 res = mma7660fc_init(&gs_handle);
205 if (res != 0)
206 {
207 mma7660fc_interface_debug_print("mma7660fc: init failed.\n");
208
209 return 1;
210 }
211
212 /* enable front back interrupt */
214 if (res != 0)
215 {
216 mma7660fc_interface_debug_print("mma7660fc: set front back interrupt failed.\n");
217 (void)mma7660fc_deinit(&gs_handle);
218
219 return 1;
220 }
221
222 /* enable up down right left interrupt */
224 if (res != 0)
225 {
226 mma7660fc_interface_debug_print("mma7660fc: set up down right left interrupt failed.\n");
227 (void)mma7660fc_deinit(&gs_handle);
228
229 return 1;
230 }
231
232 /* enable tap interrupt */
234 if (res != 0)
235 {
236 mma7660fc_interface_debug_print("mma7660fc: set tap interrupt failed.\n");
237 (void)mma7660fc_deinit(&gs_handle);
238
239 return 1;
240 }
241
242 /* enable auto sleep interrupt */
244 if (res != 0)
245 {
246 mma7660fc_interface_debug_print("mma7660fc: set auto sleep interrupt failed.\n");
247 (void)mma7660fc_deinit(&gs_handle);
248
249 return 1;
250 }
251
252 /* disable update interrupt */
254 if (res != 0)
255 {
256 mma7660fc_interface_debug_print("mma7660fc: set update interrupt failed.\n");
257 (void)mma7660fc_deinit(&gs_handle);
258
259 return 1;
260 }
261
262 /* enable shake x interrupt */
264 if (res != 0)
265 {
266 mma7660fc_interface_debug_print("mma7660fc: set shake x interrupt failed.\n");
267 (void)mma7660fc_deinit(&gs_handle);
268
269 return 1;
270 }
271
272 /* enable shake y interrupt */
274 if (res != 0)
275 {
276 mma7660fc_interface_debug_print("mma7660fc: set shake y interrupt failed.\n");
277 (void)mma7660fc_deinit(&gs_handle);
278
279 return 1;
280 }
281
282 /* enable shake z interrupt */
284 if (res != 0)
285 {
286 mma7660fc_interface_debug_print("mma7660fc: set shake z interrupt failed.\n");
287 (void)mma7660fc_deinit(&gs_handle);
288
289 return 1;
290 }
291
292 /* set sleep counter prescaler 1 */
294 if (res != 0)
295 {
296 mma7660fc_interface_debug_print("mma7660fc: set sleep counter prescaler failed.\n");
297 (void)mma7660fc_deinit(&gs_handle);
298
299 return 1;
300 }
301
302 /* set tap detection rate 120 */
304 if (res != 0)
305 {
306 mma7660fc_interface_debug_print("mma7660fc: set tap detection rate failed.\n");
307 (void)mma7660fc_deinit(&gs_handle);
308
309 return 1;
310 }
311
312 /* set auto wake rate 32 */
314 if (res != 0)
315 {
316 mma7660fc_interface_debug_print("mma7660fc: set auto wake rate failed.\n");
317 (void)mma7660fc_deinit(&gs_handle);
318
319 return 1;
320 }
321
322 /* set sleep count 127 */
323 res = mma7660fc_set_sleep_count(&gs_handle, 127);
324 if (res != 0)
325 {
326 mma7660fc_interface_debug_print("mma7660fc: set sleep count failed.\n");
327 (void)mma7660fc_deinit(&gs_handle);
328
329 return 1;
330 }
331
332 /* enable auto wake up */
334 if (res != 0)
335 {
336 mma7660fc_interface_debug_print("mma7660fc: set auto wake up failed.\n");
337 (void)mma7660fc_deinit(&gs_handle);
338
339 return 1;
340 }
341
342 /* enable auto sleep */
344 if (res != 0)
345 {
346 mma7660fc_interface_debug_print("mma7660fc: set auto sleep failed.\n");
347 (void)mma7660fc_deinit(&gs_handle);
348
349 return 1;
350 }
351
352 /* interrupt pin type push-pull */
354 if (res != 0)
355 {
356 mma7660fc_interface_debug_print("mma7660fc: set interrupt pin type failed.\n");
357 (void)mma7660fc_deinit(&gs_handle);
358
359 return 1;
360 }
361
362 /* set interrupt active level low */
364 if (res != 0)
365 {
366 mma7660fc_interface_debug_print("mma7660fc: set interrupt active level failed.\n");
367 (void)mma7660fc_deinit(&gs_handle);
368
369 return 1;
370 }
371
372 /* set tilt debounce filter 8 */
374 if (res != 0)
375 {
376 mma7660fc_interface_debug_print("mma7660fc: set tilt debounce filter failed.\n");
377 (void)mma7660fc_deinit(&gs_handle);
378
379 return 1;
380 }
381
382 /* set tap detection threshold 2 */
383 res = mma7660fc_set_tap_detection_threshold(&gs_handle, 2);
384 if (res != 0)
385 {
386 mma7660fc_interface_debug_print("mma7660fc: set tap detection threshold failed.\n");
387 (void)mma7660fc_deinit(&gs_handle);
388
389 return 1;
390 }
391
392 /* enable tap x detection */
394 if (res != 0)
395 {
396 mma7660fc_interface_debug_print("mma7660fc: set tap x detection failed.\n");
397 (void)mma7660fc_deinit(&gs_handle);
398
399 return 1;
400 }
401
402 /* enable tap y detection */
404 if (res != 0)
405 {
406 mma7660fc_interface_debug_print("mma7660fc: set tap y detection failed.\n");
407 (void)mma7660fc_deinit(&gs_handle);
408
409 return 1;
410 }
411
412 /* enable tap z detection */
414 if (res != 0)
415 {
416 mma7660fc_interface_debug_print("mma7660fc: set tap z detection failed.\n");
417 (void)mma7660fc_deinit(&gs_handle);
418
419 return 1;
420 }
421
422 /* tap pulse debounce 10ms */
423 res = mma7660fc_tap_pulse_debounce_convert_to_register(&gs_handle, 10.0f, &count);
424 if (res != 0)
425 {
426 mma7660fc_interface_debug_print("mma7660fc: tap pulse debounce convert to register failed.\n");
427 (void)mma7660fc_deinit(&gs_handle);
428
429 return 1;
430 }
431
432 /* set tap pulse debounce count */
433 res = mma7660fc_set_tap_pulse_debounce_count(&gs_handle, count);
434 if (res != 0)
435 {
436 mma7660fc_interface_debug_print("mma7660fc: set tap pulse debounce count failed.\n");
437 (void)mma7660fc_deinit(&gs_handle);
438
439 return 1;
440 }
441
442 /* set active mode */
443 res = mma7660fc_set_mode(&gs_handle, MMA7660FC_MODE_ACTIVE);
444 if (res != 0)
445 {
446 mma7660fc_interface_debug_print("mma7660fc: set mode failed.\n");
447 (void)mma7660fc_deinit(&gs_handle);
448
449 return 1;
450 }
451
452 /* init 0 */
453 gs_flag = 0;
454
455 /* loop */
456 for (i = 0; i < timeout; i++)
457 {
458 if (gs_flag == 0x3FF)
459 {
460 break;
461 }
462
463 /* delay 1ms */
465 }
466
467 /* finish motion test */
468 mma7660fc_interface_debug_print("mma7660fc: finish motion test.\n");
469 (void)mma7660fc_deinit(&gs_handle);
470
471 return 0;
472}
driver mma7660fc motion test header file
uint8_t mma7660fc_set_auto_wake_up(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable auto wake up
uint8_t mma7660fc_init(mma7660fc_handle_t *handle)
initialize the chip
uint8_t mma7660fc_set_shake_x_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable shake x interrupt
uint8_t mma7660fc_set_sleep_counter_prescaler(mma7660fc_handle_t *handle, mma7660fc_sleep_counter_prescaler_t prescaler)
set sleep counter prescaler
uint8_t mma7660fc_set_interrupt_pin_type(mma7660fc_handle_t *handle, mma7660fc_interrupt_pin_type_t type)
set interrupt pin type
uint8_t mma7660fc_set_tap_detection_threshold(mma7660fc_handle_t *handle, uint8_t threshold)
set tap detection threshold
uint8_t mma7660fc_deinit(mma7660fc_handle_t *handle)
close the chip
uint8_t mma7660fc_set_shake_z_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable shake z interrupt
uint8_t mma7660fc_tap_pulse_debounce_convert_to_register(mma7660fc_handle_t *handle, float ms, uint8_t *reg)
convert tap pulse debounce to the register raw data
uint8_t mma7660fc_info(mma7660fc_info_t *info)
get chip's information
uint8_t mma7660fc_set_tilt_debounce_filter(mma7660fc_handle_t *handle, mma7660fc_tilt_debounce_filter_t filter)
set tilt debounce filter
uint8_t mma7660fc_set_auto_wake_rate(mma7660fc_handle_t *handle, mma7660fc_auto_wake_rate_t rate)
set auto wake rate
struct mma7660fc_info_s mma7660fc_info_t
mma7660fc information structure definition
uint8_t mma7660fc_set_mode(mma7660fc_handle_t *handle, mma7660fc_mode_t mode)
set mode
uint8_t mma7660fc_set_auto_sleep_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable auto sleep interrupt
struct mma7660fc_handle_s mma7660fc_handle_t
mma7660fc handle structure definition
uint8_t mma7660fc_set_update_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable update interrupt status
uint8_t mma7660fc_set_front_back_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable front back interrupt
uint8_t mma7660fc_set_tap_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable tap interrupt
uint8_t mma7660fc_set_tap_x_detection(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable tap x detection
uint8_t mma7660fc_set_tap_y_detection(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable tap y detection
uint8_t mma7660fc_set_auto_sleep(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable auto sleep
uint8_t mma7660fc_set_interrupt_active_level(mma7660fc_handle_t *handle, mma7660fc_interrupt_active_level_t level)
set interrupt active level
uint8_t mma7660fc_set_tap_pulse_debounce_count(mma7660fc_handle_t *handle, uint8_t count)
set tap pulse debounce count
uint8_t mma7660fc_irq_handler(mma7660fc_handle_t *handle)
irq handler
uint8_t mma7660fc_set_tap_detection_rate(mma7660fc_handle_t *handle, mma7660fc_auto_sleep_rate_t rate)
set tap detection rate
uint8_t mma7660fc_set_shake_y_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable shake y interrupt
uint8_t mma7660fc_set_sleep_count(mma7660fc_handle_t *handle, uint8_t count)
set sleep count
uint8_t mma7660fc_set_tap_z_detection(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable tap z detection
uint8_t mma7660fc_set_up_down_right_left_interrupt(mma7660fc_handle_t *handle, mma7660fc_bool_t enable)
enable or disable up down right left interrupt
@ MMA7660FC_STATUS_LEFT
@ MMA7660FC_STATUS_TAP
@ MMA7660FC_STATUS_RIGHT
@ MMA7660FC_STATUS_FRONT
@ MMA7660FC_STATUS_SHAKE
@ MMA7660FC_STATUS_AUTO_SLEEP
@ MMA7660FC_STATUS_UP
@ MMA7660FC_STATUS_BACK
@ MMA7660FC_STATUS_AUTO_WAKE_UP
@ MMA7660FC_STATUS_DOWN
@ MMA7660FC_STATUS_UPDATE
@ MMA7660FC_INTERRUPT_PIN_TYPE_PUSH_PULL
@ MMA7660FC_BOOL_FALSE
@ MMA7660FC_BOOL_TRUE
@ MMA7660FC_INTERRUPT_ACTIVE_LEVEL_LOW
@ MMA7660FC_SLEEP_COUNTER_PRESCALER_1
@ MMA7660FC_AUTO_SLEEP_RATE_120
@ MMA7660FC_TILT_DEBOUNCE_FILTER_8
@ MMA7660FC_MODE_ACTIVE
@ MMA7660FC_AUTO_WAKE_RATE_32
uint8_t mma7660fc_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
void mma7660fc_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t mma7660fc_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t mma7660fc_interface_iic_init(void)
interface iic bus init
void mma7660fc_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t mma7660fc_interface_iic_deinit(void)
interface iic bus deinit
uint8_t mma7660fc_motion_test(uint32_t timeout)
motion test
uint8_t mma7660fc_motion_test_irq_handler(void)
motion test irq