/
stm32h7xx_hal_dfsdm.c
executable file
·3798 lines (3417 loc) · 129 KB
/
stm32h7xx_hal_dfsdm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
******************************************************************************
* @file stm32h7xx_hal_dfsdm.c
* @author MCD Application Team
* @brief This file provides firmware functions to manage the following
* functionalities of the Digital Filter for Sigma-Delta Modulators
* (DFSDM) peripherals:
* + Initialization and configuration of channels and filters
* + Regular channels configuration
* + Injected channels configuration
* + Regular/Injected Channels DMA Configuration
* + Interrupts and flags management
* + Analog watchdog feature
* + Short-circuit detector feature
* + Extremes detector feature
* + Clock absence detector feature
* + Break generation on analog watchdog or short-circuit event
*
@verbatim
==============================================================================
##### How to use this driver #####
==============================================================================
[..]
*** Channel initialization ***
==============================
[..]
(#) User has first to initialize channels (before filters initialization).
(#) As prerequisite, fill in the HAL_DFSDM_ChannelMspInit() :
(++) Enable DFSDMz clock interface with __HAL_RCC_DFSDMz_CLK_ENABLE().
(++) Enable the clocks for the DFSDMz GPIOS with __HAL_RCC_GPIOx_CLK_ENABLE().
(++) Configure these DFSDMz pins in alternate mode using HAL_GPIO_Init().
(++) If interrupt mode is used, enable and configure DFSDMz_FLT0 global
interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
(#) Configure the output clock, input, serial interface, analog watchdog,
offset and data right bit shift parameters for this channel using the
HAL_DFSDM_ChannelInit() function.
*** Channel clock absence detector ***
======================================
[..]
(#) Start clock absence detector using HAL_DFSDM_ChannelCkabStart() or
HAL_DFSDM_ChannelCkabStart_IT().
(#) In polling mode, use HAL_DFSDM_ChannelPollForCkab() to detect the clock
absence.
(#) In interrupt mode, HAL_DFSDM_ChannelCkabCallback() will be called if
clock absence is detected.
(#) Stop clock absence detector using HAL_DFSDM_ChannelCkabStop() or
HAL_DFSDM_ChannelCkabStop_IT().
(#) Please note that the same mode (polling or interrupt) has to be used
for all channels because the channels are sharing the same interrupt.
(#) Please note also that in interrupt mode, if clock absence detector is
stopped for one channel, interrupt will be disabled for all channels.
*** Channel short circuit detector ***
======================================
[..]
(#) Start short circuit detector using HAL_DFSDM_ChannelScdStart() or
or HAL_DFSDM_ChannelScdStart_IT().
(#) In polling mode, use HAL_DFSDM_ChannelPollForScd() to detect short
circuit.
(#) In interrupt mode, HAL_DFSDM_ChannelScdCallback() will be called if
short circuit is detected.
(#) Stop short circuit detector using HAL_DFSDM_ChannelScdStop() or
or HAL_DFSDM_ChannelScdStop_IT().
(#) Please note that the same mode (polling or interrupt) has to be used
for all channels because the channels are sharing the same interrupt.
(#) Please note also that in interrupt mode, if short circuit detector is
stopped for one channel, interrupt will be disabled for all channels.
*** Channel analog watchdog value ***
=====================================
[..]
(#) Get analog watchdog filter value of a channel using
HAL_DFSDM_ChannelGetAwdValue().
*** Channel offset value ***
=====================================
[..]
(#) Modify offset value of a channel using HAL_DFSDM_ChannelModifyOffset().
*** Filter initialization ***
=============================
[..]
(#) After channel initialization, user has to init filters.
(#) As prerequisite, fill in the HAL_DFSDM_FilterMspInit() :
(++) If interrupt mode is used , enable and configure DFSDMz_FLTx global
interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
Please note that DFSDMz_FLT0 global interrupt could be already
enabled if interrupt is used for channel.
(++) If DMA mode is used, configure DMA with HAL_DMA_Init() and link it
with DFSDMz filter handle using __HAL_LINKDMA().
(#) Configure the regular conversion, injected conversion and filter
parameters for this filter using the HAL_DFSDM_FilterInit() function.
*** Filter regular channel conversion ***
=========================================
[..]
(#) Select regular channel and enable/disable continuous mode using
HAL_DFSDM_FilterConfigRegChannel().
(#) Start regular conversion using HAL_DFSDM_FilterRegularStart(),
HAL_DFSDM_FilterRegularStart_IT(), HAL_DFSDM_FilterRegularStart_DMA() or
HAL_DFSDM_FilterRegularMsbStart_DMA().
(#) In polling mode, use HAL_DFSDM_FilterPollForRegConversion() to detect
the end of regular conversion.
(#) In interrupt mode, HAL_DFSDM_FilterRegConvCpltCallback() will be called
at the end of regular conversion.
(#) Get value of regular conversion and corresponding channel using
HAL_DFSDM_FilterGetRegularValue().
(#) In DMA mode, HAL_DFSDM_FilterRegConvHalfCpltCallback() and
HAL_DFSDM_FilterRegConvCpltCallback() will be called respectively at the
half transfer and at the transfer complete. Please note that
HAL_DFSDM_FilterRegConvHalfCpltCallback() will be called only in DMA
circular mode.
(#) Stop regular conversion using HAL_DFSDM_FilterRegularStop(),
HAL_DFSDM_FilterRegularStop_IT() or HAL_DFSDM_FilterRegularStop_DMA().
*** Filter injected channels conversion ***
===========================================
[..]
(#) Select injected channels using HAL_DFSDM_FilterConfigInjChannel().
(#) Start injected conversion using HAL_DFSDM_FilterInjectedStart(),
HAL_DFSDM_FilterInjectedStart_IT(), HAL_DFSDM_FilterInjectedStart_DMA() or
HAL_DFSDM_FilterInjectedMsbStart_DMA().
(#) In polling mode, use HAL_DFSDM_FilterPollForInjConversion() to detect
the end of injected conversion.
(#) In interrupt mode, HAL_DFSDM_FilterInjConvCpltCallback() will be called
at the end of injected conversion.
(#) Get value of injected conversion and corresponding channel using
HAL_DFSDM_FilterGetInjectedValue().
(#) In DMA mode, HAL_DFSDM_FilterInjConvHalfCpltCallback() and
HAL_DFSDM_FilterInjConvCpltCallback() will be called respectively at the
half transfer and at the transfer complete. Please note that
HAL_DFSDM_FilterInjConvCpltCallback() will be called only in DMA
circular mode.
(#) Stop injected conversion using HAL_DFSDM_FilterInjectedStop(),
HAL_DFSDM_FilterInjectedStop_IT() or HAL_DFSDM_FilterInjectedStop_DMA().
*** Filter analog watchdog ***
==============================
[..]
(#) Start filter analog watchdog using HAL_DFSDM_FilterAwdStart_IT().
(#) HAL_DFSDM_FilterAwdCallback() will be called if analog watchdog occurs.
(#) Stop filter analog watchdog using HAL_DFSDM_FilterAwdStop_IT().
*** Filter extreme detector ***
===============================
[..]
(#) Start filter extreme detector using HAL_DFSDM_FilterExdStart().
(#) Get extreme detector maximum value using HAL_DFSDM_FilterGetExdMaxValue().
(#) Get extreme detector minimum value using HAL_DFSDM_FilterGetExdMinValue().
(#) Start filter extreme detector using HAL_DFSDM_FilterExdStop().
*** Filter conversion time ***
==============================
[..]
(#) Get conversion time value using HAL_DFSDM_FilterGetConvTimeValue().
*** Callback registration ***
=============================
[..]
The compilation define USE_HAL_DFSDM_REGISTER_CALLBACKS when set to 1
allows the user to configure dynamically the driver callbacks.
Use functions HAL_DFSDM_Channel_RegisterCallback(),
HAL_DFSDM_Filter_RegisterCallback() or
HAL_DFSDM_Filter_RegisterAwdCallback() to register a user callback.
[..]
Function HAL_DFSDM_Channel_RegisterCallback() allows to register
following callbacks:
(+) CkabCallback : DFSDM channel clock absence detection callback.
(+) ScdCallback : DFSDM channel short circuit detection callback.
(+) MspInitCallback : DFSDM channel MSP init callback.
(+) MspDeInitCallback : DFSDM channel MSP de-init callback.
[..]
This function takes as parameters the HAL peripheral handle, the Callback ID
and a pointer to the user callback function.
[..]
Function HAL_DFSDM_Filter_RegisterCallback() allows to register
following callbacks:
(+) RegConvCpltCallback : DFSDM filter regular conversion complete callback.
(+) RegConvHalfCpltCallback : DFSDM filter half regular conversion complete callback.
(+) InjConvCpltCallback : DFSDM filter injected conversion complete callback.
(+) InjConvHalfCpltCallback : DFSDM filter half injected conversion complete callback.
(+) ErrorCallback : DFSDM filter error callback.
(+) MspInitCallback : DFSDM filter MSP init callback.
(+) MspDeInitCallback : DFSDM filter MSP de-init callback.
[..]
This function takes as parameters the HAL peripheral handle, the Callback ID
and a pointer to the user callback function.
[..]
For specific DFSDM filter analog watchdog callback use dedicated register callback:
HAL_DFSDM_Filter_RegisterAwdCallback().
[..]
Use functions HAL_DFSDM_Channel_UnRegisterCallback() or
HAL_DFSDM_Filter_UnRegisterCallback() to reset a callback to the default
weak function.
[..]
HAL_DFSDM_Channel_UnRegisterCallback() takes as parameters the HAL peripheral handle,
and the Callback ID.
[..]
This function allows to reset following callbacks:
(+) CkabCallback : DFSDM channel clock absence detection callback.
(+) ScdCallback : DFSDM channel short circuit detection callback.
(+) MspInitCallback : DFSDM channel MSP init callback.
(+) MspDeInitCallback : DFSDM channel MSP de-init callback.
[..]
HAL_DFSDM_Filter_UnRegisterCallback() takes as parameters the HAL peripheral handle,
and the Callback ID.
[..]
This function allows to reset following callbacks:
(+) RegConvCpltCallback : DFSDM filter regular conversion complete callback.
(+) RegConvHalfCpltCallback : DFSDM filter half regular conversion complete callback.
(+) InjConvCpltCallback : DFSDM filter injected conversion complete callback.
(+) InjConvHalfCpltCallback : DFSDM filter half injected conversion complete callback.
(+) ErrorCallback : DFSDM filter error callback.
(+) MspInitCallback : DFSDM filter MSP init callback.
(+) MspDeInitCallback : DFSDM filter MSP de-init callback.
[..]
For specific DFSDM filter analog watchdog callback use dedicated unregister callback:
HAL_DFSDM_Filter_UnRegisterAwdCallback().
[..]
By default, after the call of init function and if the state is RESET
all callbacks are reset to the corresponding legacy weak functions:
examples HAL_DFSDM_ChannelScdCallback(), HAL_DFSDM_FilterErrorCallback().
Exception done for MspInit and MspDeInit callbacks that are respectively
reset to the legacy weak functions in the init and de-init only when these
callbacks are null (not registered beforehand).
If not, MspInit or MspDeInit are not null, the init and de-init keep and use
the user MspInit/MspDeInit callbacks (registered beforehand)
[..]
Callbacks can be registered/unregistered in READY state only.
Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
during the init/de-init.
In that case first register the MspInit/MspDeInit user callbacks using
HAL_DFSDM_Channel_RegisterCallback() or
HAL_DFSDM_Filter_RegisterCallback() before calling init or de-init function.
[..]
When The compilation define USE_HAL_DFSDM_REGISTER_CALLBACKS is set to 0 or
not defined, the callback registering feature is not available
and weak callbacks are used.
@endverbatim
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2017 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32h7xx_hal.h"
/** @addtogroup STM32H7xx_HAL_Driver
* @{
*/
#ifdef HAL_DFSDM_MODULE_ENABLED
/** @defgroup DFSDM DFSDM
* @brief DFSDM HAL driver module
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/** @defgroup DFSDM_Private_Define DFSDM Private Define
* @{
*/
#define DFSDM_FLTCR1_MSB_RCH_OFFSET 8
#define DFSDM_MSB_MASK 0xFFFF0000U
#define DFSDM_LSB_MASK 0x0000FFFFU
#define DFSDM_CKAB_TIMEOUT 5000U
#define DFSDM1_CHANNEL_NUMBER 8U
#if defined(DFSDM2_Channel0)
#define DFSDM2_CHANNEL_NUMBER 2U
#endif /* DFSDM2_Channel0 */
/**
* @}
*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/** @defgroup DFSDM_Private_Variables DFSDM Private Variables
* @{
*/
static __IO uint32_t v_dfsdm1ChannelCounter = 0;
static DFSDM_Channel_HandleTypeDef *a_dfsdm1ChannelHandle[DFSDM1_CHANNEL_NUMBER] = {NULL};
#if defined(DFSDM2_Channel0)
static __IO uint32_t v_dfsdm2ChannelCounter = 0;
static DFSDM_Channel_HandleTypeDef *a_dfsdm2ChannelHandle[DFSDM2_CHANNEL_NUMBER] = {NULL};
#endif /* DFSDM2_Channel0 */
/**
* @}
*/
/* Private function prototypes -----------------------------------------------*/
/** @defgroup DFSDM_Private_Functions DFSDM Private Functions
* @{
*/
static uint32_t DFSDM_GetInjChannelsNbr(uint32_t Channels);
static uint32_t DFSDM_GetChannelFromInstance(const DFSDM_Channel_TypeDef *Instance);
static void DFSDM_RegConvStart(DFSDM_Filter_HandleTypeDef *hdfsdm_filter);
static void DFSDM_RegConvStop(DFSDM_Filter_HandleTypeDef *hdfsdm_filter);
static void DFSDM_InjConvStart(DFSDM_Filter_HandleTypeDef *hdfsdm_filter);
static void DFSDM_InjConvStop(DFSDM_Filter_HandleTypeDef *hdfsdm_filter);
static void DFSDM_DMARegularHalfConvCplt(DMA_HandleTypeDef *hdma);
static void DFSDM_DMARegularConvCplt(DMA_HandleTypeDef *hdma);
static void DFSDM_DMAInjectedHalfConvCplt(DMA_HandleTypeDef *hdma);
static void DFSDM_DMAInjectedConvCplt(DMA_HandleTypeDef *hdma);
static void DFSDM_DMAError(DMA_HandleTypeDef *hdma);
/**
* @}
*/
/* Exported functions --------------------------------------------------------*/
/** @defgroup DFSDM_Exported_Functions DFSDM Exported Functions
* @{
*/
/** @defgroup DFSDM_Exported_Functions_Group1_Channel Channel initialization and de-initialization functions
* @brief Channel initialization and de-initialization functions
*
@verbatim
==============================================================================
##### Channel initialization and de-initialization functions #####
==============================================================================
[..] This section provides functions allowing to:
(+) Initialize the DFSDM channel.
(+) De-initialize the DFSDM channel.
@endverbatim
* @{
*/
/**
* @brief Initialize the DFSDM channel according to the specified parameters
* in the DFSDM_ChannelInitTypeDef structure and initialize the associated handle.
* @param hdfsdm_channel DFSDM channel handle.
* @retval HAL status.
*/
HAL_StatusTypeDef HAL_DFSDM_ChannelInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
{
__IO uint32_t *channelCounterPtr;
DFSDM_Channel_HandleTypeDef **channelHandleTable;
DFSDM_Channel_TypeDef *channel0Instance;
/* Check DFSDM Channel handle */
if(hdfsdm_channel == NULL)
{
return HAL_ERROR;
}
/* Check parameters */
assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
assert_param(IS_FUNCTIONAL_STATE(hdfsdm_channel->Init.OutputClock.Activation));
assert_param(IS_DFSDM_CHANNEL_INPUT(hdfsdm_channel->Init.Input.Multiplexer));
assert_param(IS_DFSDM_CHANNEL_DATA_PACKING(hdfsdm_channel->Init.Input.DataPacking));
assert_param(IS_DFSDM_CHANNEL_INPUT_PINS(hdfsdm_channel->Init.Input.Pins));
assert_param(IS_DFSDM_CHANNEL_SERIAL_INTERFACE_TYPE(hdfsdm_channel->Init.SerialInterface.Type));
assert_param(IS_DFSDM_CHANNEL_SPI_CLOCK(hdfsdm_channel->Init.SerialInterface.SpiClock));
assert_param(IS_DFSDM_CHANNEL_FILTER_ORDER(hdfsdm_channel->Init.Awd.FilterOrder));
assert_param(IS_DFSDM_CHANNEL_FILTER_OVS_RATIO(hdfsdm_channel->Init.Awd.Oversampling));
assert_param(IS_DFSDM_CHANNEL_OFFSET(hdfsdm_channel->Init.Offset));
assert_param(IS_DFSDM_CHANNEL_RIGHT_BIT_SHIFT(hdfsdm_channel->Init.RightBitShift));
#if defined(DFSDM2_Channel0)
if (IS_DFSDM1_CHANNEL_INSTANCE(hdfsdm_channel->Instance))
{
channelCounterPtr = &v_dfsdm1ChannelCounter;
channelHandleTable = a_dfsdm1ChannelHandle;
channel0Instance = DFSDM1_Channel0;
}
else
{
channelCounterPtr = &v_dfsdm2ChannelCounter;
channelHandleTable = a_dfsdm2ChannelHandle;
channel0Instance = DFSDM2_Channel0;
}
#else /* DFSDM2_Channel0 */
channelCounterPtr = &v_dfsdm1ChannelCounter;
channelHandleTable = a_dfsdm1ChannelHandle;
channel0Instance = DFSDM1_Channel0;
#endif /* DFSDM2_Channel0 */
/* Check that channel has not been already initialized */
if (channelHandleTable[DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance)] != NULL)
{
return HAL_ERROR;
}
#if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
/* Reset callback pointers to the weak predefined callbacks */
hdfsdm_channel->CkabCallback = HAL_DFSDM_ChannelCkabCallback;
hdfsdm_channel->ScdCallback = HAL_DFSDM_ChannelScdCallback;
/* Call MSP init function */
if(hdfsdm_channel->MspInitCallback == NULL)
{
hdfsdm_channel->MspInitCallback = HAL_DFSDM_ChannelMspInit;
}
hdfsdm_channel->MspInitCallback(hdfsdm_channel);
#else
/* Call MSP init function */
HAL_DFSDM_ChannelMspInit(hdfsdm_channel);
#endif
/* Update the channel counter */
(*channelCounterPtr)++;
/* Configure output serial clock and enable global DFSDM interface only for first channel */
if(*channelCounterPtr == 1U)
{
assert_param(IS_DFSDM_CHANNEL_OUTPUT_CLOCK(hdfsdm_channel->Init.OutputClock.Selection));
/* Set the output serial clock source */
channel0Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_CKOUTSRC);
channel0Instance->CHCFGR1 |= hdfsdm_channel->Init.OutputClock.Selection;
/* Reset clock divider */
channel0Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_CKOUTDIV);
if(hdfsdm_channel->Init.OutputClock.Activation == ENABLE)
{
assert_param(IS_DFSDM_CHANNEL_OUTPUT_CLOCK_DIVIDER(hdfsdm_channel->Init.OutputClock.Divider));
/* Set the output clock divider */
channel0Instance->CHCFGR1 |= (uint32_t)((hdfsdm_channel->Init.OutputClock.Divider - 1U) <<
DFSDM_CHCFGR1_CKOUTDIV_Pos);
}
/* enable the DFSDM global interface */
channel0Instance->CHCFGR1 |= DFSDM_CHCFGR1_DFSDMEN;
}
/* Set channel input parameters */
hdfsdm_channel->Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_DATPACK | DFSDM_CHCFGR1_DATMPX |
DFSDM_CHCFGR1_CHINSEL);
hdfsdm_channel->Instance->CHCFGR1 |= (hdfsdm_channel->Init.Input.Multiplexer |
hdfsdm_channel->Init.Input.DataPacking |
hdfsdm_channel->Init.Input.Pins);
/* Set serial interface parameters */
hdfsdm_channel->Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_SITP | DFSDM_CHCFGR1_SPICKSEL);
hdfsdm_channel->Instance->CHCFGR1 |= (hdfsdm_channel->Init.SerialInterface.Type |
hdfsdm_channel->Init.SerialInterface.SpiClock);
/* Set analog watchdog parameters */
hdfsdm_channel->Instance->CHAWSCDR &= ~(DFSDM_CHAWSCDR_AWFORD | DFSDM_CHAWSCDR_AWFOSR);
hdfsdm_channel->Instance->CHAWSCDR |= (hdfsdm_channel->Init.Awd.FilterOrder |
((hdfsdm_channel->Init.Awd.Oversampling - 1U) << DFSDM_CHAWSCDR_AWFOSR_Pos));
/* Set channel offset and right bit shift */
hdfsdm_channel->Instance->CHCFGR2 &= ~(DFSDM_CHCFGR2_OFFSET | DFSDM_CHCFGR2_DTRBS);
hdfsdm_channel->Instance->CHCFGR2 |= (((uint32_t) hdfsdm_channel->Init.Offset << DFSDM_CHCFGR2_OFFSET_Pos) |
(hdfsdm_channel->Init.RightBitShift << DFSDM_CHCFGR2_DTRBS_Pos));
/* Enable DFSDM channel */
hdfsdm_channel->Instance->CHCFGR1 |= DFSDM_CHCFGR1_CHEN;
/* Set DFSDM Channel to ready state */
hdfsdm_channel->State = HAL_DFSDM_CHANNEL_STATE_READY;
/* Store channel handle in DFSDM channel handle table */
channelHandleTable[DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance)] = hdfsdm_channel;
return HAL_OK;
}
/**
* @brief De-initialize the DFSDM channel.
* @param hdfsdm_channel DFSDM channel handle.
* @retval HAL status.
*/
HAL_StatusTypeDef HAL_DFSDM_ChannelDeInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
{
__IO uint32_t *channelCounterPtr;
DFSDM_Channel_HandleTypeDef **channelHandleTable;
DFSDM_Channel_TypeDef *channel0Instance;
/* Check DFSDM Channel handle */
if(hdfsdm_channel == NULL)
{
return HAL_ERROR;
}
/* Check parameters */
assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
#if defined(DFSDM2_Channel0)
if (IS_DFSDM1_CHANNEL_INSTANCE(hdfsdm_channel->Instance))
{
channelCounterPtr = &v_dfsdm1ChannelCounter;
channelHandleTable = a_dfsdm1ChannelHandle;
channel0Instance = DFSDM1_Channel0;
}
else
{
channelCounterPtr = &v_dfsdm2ChannelCounter;
channelHandleTable = a_dfsdm2ChannelHandle;
channel0Instance = DFSDM2_Channel0;
}
#else /* DFSDM2_Channel0 */
channelCounterPtr = &v_dfsdm1ChannelCounter;
channelHandleTable = a_dfsdm1ChannelHandle;
channel0Instance = DFSDM1_Channel0;
#endif /* DFSDM2_Channel0 */
/* Check that channel has not been already deinitialized */
if (channelHandleTable[DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance)] == NULL)
{
return HAL_ERROR;
}
/* Disable the DFSDM channel */
hdfsdm_channel->Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_CHEN);
/* Update the channel counter */
(*channelCounterPtr)--;
/* Disable global DFSDM at deinit of last channel */
if (*channelCounterPtr == 0U)
{
channel0Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_DFSDMEN);
}
/* Call MSP deinit function */
#if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
if(hdfsdm_channel->MspDeInitCallback == NULL)
{
hdfsdm_channel->MspDeInitCallback = HAL_DFSDM_ChannelMspDeInit;
}
hdfsdm_channel->MspDeInitCallback(hdfsdm_channel);
#else
HAL_DFSDM_ChannelMspDeInit(hdfsdm_channel);
#endif
/* Set DFSDM Channel in reset state */
hdfsdm_channel->State = HAL_DFSDM_CHANNEL_STATE_RESET;
/* Reset channel handle in DFSDM channel handle table */
channelHandleTable[DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance)] = (DFSDM_Channel_HandleTypeDef *) NULL;
return HAL_OK;
}
/**
* @brief Initialize the DFSDM channel MSP.
* @param hdfsdm_channel DFSDM channel handle.
* @retval None
*/
__weak void HAL_DFSDM_ChannelMspInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hdfsdm_channel);
/* NOTE : This function should not be modified, when the function is needed,
the HAL_DFSDM_ChannelMspInit could be implemented in the user file.
*/
}
/**
* @brief De-initialize the DFSDM channel MSP.
* @param hdfsdm_channel DFSDM channel handle.
* @retval None
*/
__weak void HAL_DFSDM_ChannelMspDeInit(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hdfsdm_channel);
/* NOTE : This function should not be modified, when the function is needed,
the HAL_DFSDM_ChannelMspDeInit could be implemented in the user file.
*/
}
#if (USE_HAL_DFSDM_REGISTER_CALLBACKS == 1)
/**
* @brief Register a user DFSDM channel callback
* to be used instead of the weak predefined callback.
* @param hdfsdm_channel DFSDM channel handle.
* @param CallbackID ID of the callback to be registered.
* This parameter can be one of the following values:
* @arg @ref HAL_DFSDM_CHANNEL_CKAB_CB_ID clock absence detection callback ID.
* @arg @ref HAL_DFSDM_CHANNEL_SCD_CB_ID short circuit detection callback ID.
* @arg @ref HAL_DFSDM_CHANNEL_MSPINIT_CB_ID MSP init callback ID.
* @arg @ref HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID MSP de-init callback ID.
* @param pCallback pointer to the callback function.
* @retval HAL status.
*/
HAL_StatusTypeDef HAL_DFSDM_Channel_RegisterCallback(DFSDM_Channel_HandleTypeDef *hdfsdm_channel,
HAL_DFSDM_Channel_CallbackIDTypeDef CallbackID,
pDFSDM_Channel_CallbackTypeDef pCallback)
{
HAL_StatusTypeDef status = HAL_OK;
if(pCallback == NULL)
{
/* update return status */
status = HAL_ERROR;
}
else
{
if(HAL_DFSDM_CHANNEL_STATE_READY == hdfsdm_channel->State)
{
switch (CallbackID)
{
case HAL_DFSDM_CHANNEL_CKAB_CB_ID :
hdfsdm_channel->CkabCallback = pCallback;
break;
case HAL_DFSDM_CHANNEL_SCD_CB_ID :
hdfsdm_channel->ScdCallback = pCallback;
break;
case HAL_DFSDM_CHANNEL_MSPINIT_CB_ID :
hdfsdm_channel->MspInitCallback = pCallback;
break;
case HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID :
hdfsdm_channel->MspDeInitCallback = pCallback;
break;
default :
/* update return status */
status = HAL_ERROR;
break;
}
}
else if(HAL_DFSDM_CHANNEL_STATE_RESET == hdfsdm_channel->State)
{
switch (CallbackID)
{
case HAL_DFSDM_CHANNEL_MSPINIT_CB_ID :
hdfsdm_channel->MspInitCallback = pCallback;
break;
case HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID :
hdfsdm_channel->MspDeInitCallback = pCallback;
break;
default :
/* update return status */
status = HAL_ERROR;
break;
}
}
else
{
/* update return status */
status = HAL_ERROR;
}
}
return status;
}
/**
* @brief Unregister a user DFSDM channel callback.
* DFSDM channel callback is redirected to the weak predefined callback.
* @param hdfsdm_channel DFSDM channel handle.
* @param CallbackID ID of the callback to be unregistered.
* This parameter can be one of the following values:
* @arg @ref HAL_DFSDM_CHANNEL_CKAB_CB_ID clock absence detection callback ID.
* @arg @ref HAL_DFSDM_CHANNEL_SCD_CB_ID short circuit detection callback ID.
* @arg @ref HAL_DFSDM_CHANNEL_MSPINIT_CB_ID MSP init callback ID.
* @arg @ref HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID MSP de-init callback ID.
* @retval HAL status.
*/
HAL_StatusTypeDef HAL_DFSDM_Channel_UnRegisterCallback(DFSDM_Channel_HandleTypeDef *hdfsdm_channel,
HAL_DFSDM_Channel_CallbackIDTypeDef CallbackID)
{
HAL_StatusTypeDef status = HAL_OK;
if(HAL_DFSDM_CHANNEL_STATE_READY == hdfsdm_channel->State)
{
switch (CallbackID)
{
case HAL_DFSDM_CHANNEL_CKAB_CB_ID :
hdfsdm_channel->CkabCallback = HAL_DFSDM_ChannelCkabCallback;
break;
case HAL_DFSDM_CHANNEL_SCD_CB_ID :
hdfsdm_channel->ScdCallback = HAL_DFSDM_ChannelScdCallback;
break;
case HAL_DFSDM_CHANNEL_MSPINIT_CB_ID :
hdfsdm_channel->MspInitCallback = HAL_DFSDM_ChannelMspInit;
break;
case HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID :
hdfsdm_channel->MspDeInitCallback = HAL_DFSDM_ChannelMspDeInit;
break;
default :
/* update return status */
status = HAL_ERROR;
break;
}
}
else if(HAL_DFSDM_CHANNEL_STATE_RESET == hdfsdm_channel->State)
{
switch (CallbackID)
{
case HAL_DFSDM_CHANNEL_MSPINIT_CB_ID :
hdfsdm_channel->MspInitCallback = HAL_DFSDM_ChannelMspInit;
break;
case HAL_DFSDM_CHANNEL_MSPDEINIT_CB_ID :
hdfsdm_channel->MspDeInitCallback = HAL_DFSDM_ChannelMspDeInit;
break;
default :
/* update return status */
status = HAL_ERROR;
break;
}
}
else
{
/* update return status */
status = HAL_ERROR;
}
return status;
}
#endif /* USE_HAL_DFSDM_REGISTER_CALLBACKS */
/**
* @}
*/
/** @defgroup DFSDM_Exported_Functions_Group2_Channel Channel operation functions
* @brief Channel operation functions
*
@verbatim
==============================================================================
##### Channel operation functions #####
==============================================================================
[..] This section provides functions allowing to:
(+) Manage clock absence detector feature.
(+) Manage short circuit detector feature.
(+) Get analog watchdog value.
(+) Modify offset value.
@endverbatim
* @{
*/
/**
* @brief This function allows to start clock absence detection in polling mode.
* @note Same mode has to be used for all channels.
* @note If clock is not available on this channel during 5 seconds,
* clock absence detection will not be activated and function
* will return HAL_TIMEOUT error.
* @param hdfsdm_channel DFSDM channel handle.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_DFSDM_ChannelCkabStart(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
{
HAL_StatusTypeDef status = HAL_OK;
uint32_t channel;
uint32_t tickstart;
DFSDM_Filter_TypeDef *filter0Instance;
/* Check parameters */
assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
#if defined(DFSDM2_Channel0)
if (IS_DFSDM1_CHANNEL_INSTANCE(hdfsdm_channel->Instance))
{
filter0Instance = DFSDM1_Filter0;
}
else
{
filter0Instance = DFSDM2_Filter0;
}
#else /* DFSDM2_Channel0 */
filter0Instance = DFSDM1_Filter0;
#endif /* DFSDM2_Channel0 */
/* Check DFSDM channel state */
if(hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
{
/* Return error status */
status = HAL_ERROR;
}
else
{
/* Get channel number from channel instance */
channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
/* Get timeout */
tickstart = HAL_GetTick();
/* Clear clock absence flag */
while ((((filter0Instance->FLTISR & DFSDM_FLTISR_CKABF) >> (DFSDM_FLTISR_CKABF_Pos + channel)) & 1U) != 0U)
{
filter0Instance->FLTICR = (1UL << (DFSDM_FLTICR_CLRCKABF_Pos + channel));
/* Check the Timeout */
if((HAL_GetTick()-tickstart) > DFSDM_CKAB_TIMEOUT)
{
/* Set timeout status */
status = HAL_TIMEOUT;
break;
}
}
if(status == HAL_OK)
{
/* Start clock absence detection */
hdfsdm_channel->Instance->CHCFGR1 |= DFSDM_CHCFGR1_CKABEN;
}
}
/* Return function status */
return status;
}
/**
* @brief This function allows to poll for the clock absence detection.
* @param hdfsdm_channel DFSDM channel handle.
* @param Timeout Timeout value in milliseconds.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_DFSDM_ChannelPollForCkab(DFSDM_Channel_HandleTypeDef *hdfsdm_channel,
uint32_t Timeout)
{
uint32_t tickstart;
uint32_t channel;
DFSDM_Filter_TypeDef *filter0Instance;
/* Check parameters */
assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
#if defined(DFSDM2_Channel0)
if (IS_DFSDM1_CHANNEL_INSTANCE(hdfsdm_channel->Instance))
{
filter0Instance = DFSDM1_Filter0;
}
else
{
filter0Instance = DFSDM2_Filter0;
}
#else /* DFSDM2_Channel0 */
filter0Instance = DFSDM1_Filter0;
#endif /* DFSDM2_Channel0 */
/* Check DFSDM channel state */
if(hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
{
/* Return error status */
return HAL_ERROR;
}
else
{
/* Get channel number from channel instance */
channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
/* Get timeout */
tickstart = HAL_GetTick();
/* Wait clock absence detection */
while ((((filter0Instance->FLTISR & DFSDM_FLTISR_CKABF) >> (DFSDM_FLTISR_CKABF_Pos + channel)) & 1U) == 0U)
{
/* Check the Timeout */
if(Timeout != HAL_MAX_DELAY)
{
if(((HAL_GetTick()-tickstart) > Timeout) || (Timeout == 0U))
{
/* Return timeout status */
return HAL_TIMEOUT;
}
}
}
/* Clear clock absence detection flag */
filter0Instance->FLTICR = (1UL << (DFSDM_FLTICR_CLRCKABF_Pos + channel));
/* Return function status */
return HAL_OK;
}
}
/**
* @brief This function allows to stop clock absence detection in polling mode.
* @param hdfsdm_channel DFSDM channel handle.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_DFSDM_ChannelCkabStop(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
{
HAL_StatusTypeDef status = HAL_OK;
uint32_t channel;
DFSDM_Filter_TypeDef *filter0Instance;
/* Check parameters */
assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
#if defined(DFSDM2_Channel0)
if (IS_DFSDM1_CHANNEL_INSTANCE(hdfsdm_channel->Instance))
{
filter0Instance = DFSDM1_Filter0;
}
else
{
filter0Instance = DFSDM2_Filter0;
}
#else /* DFSDM2_Channel0 */
filter0Instance = DFSDM1_Filter0;
#endif /* DFSDM2_Channel0 */
/* Check DFSDM channel state */
if(hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
{
/* Return error status */
status = HAL_ERROR;
}
else
{
/* Stop clock absence detection */
hdfsdm_channel->Instance->CHCFGR1 &= ~(DFSDM_CHCFGR1_CKABEN);
/* Clear clock absence flag */
channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
filter0Instance->FLTICR = (1UL << (DFSDM_FLTICR_CLRCKABF_Pos + channel));
}
/* Return function status */
return status;
}
/**
* @brief This function allows to start clock absence detection in interrupt mode.
* @note Same mode has to be used for all channels.
* @note If clock is not available on this channel during 5 seconds,
* clock absence detection will not be activated and function
* will return HAL_TIMEOUT error.
* @param hdfsdm_channel DFSDM channel handle.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_DFSDM_ChannelCkabStart_IT(DFSDM_Channel_HandleTypeDef *hdfsdm_channel)
{
HAL_StatusTypeDef status = HAL_OK;
uint32_t channel;
uint32_t tickstart;
DFSDM_Filter_TypeDef *filter0Instance;
/* Check parameters */
assert_param(IS_DFSDM_CHANNEL_ALL_INSTANCE(hdfsdm_channel->Instance));
#if defined(DFSDM2_Channel0)
if (IS_DFSDM1_CHANNEL_INSTANCE(hdfsdm_channel->Instance))
{
filter0Instance = DFSDM1_Filter0;
}
else
{
filter0Instance = DFSDM2_Filter0;
}
#else /* DFSDM2_Channel0 */
filter0Instance = DFSDM1_Filter0;
#endif /* DFSDM2_Channel0 */
/* Check DFSDM channel state */
if(hdfsdm_channel->State != HAL_DFSDM_CHANNEL_STATE_READY)
{
/* Return error status */
status = HAL_ERROR;
}
else
{
/* Get channel number from channel instance */
channel = DFSDM_GetChannelFromInstance(hdfsdm_channel->Instance);
/* Get timeout */
tickstart = HAL_GetTick();
/* Clear clock absence flag */
while ((((filter0Instance->FLTISR & DFSDM_FLTISR_CKABF) >> (DFSDM_FLTISR_CKABF_Pos + channel)) & 1U) != 0U)
{
filter0Instance->FLTICR = (1UL << (DFSDM_FLTICR_CLRCKABF_Pos + channel));
/* Check the Timeout */
if((HAL_GetTick()-tickstart) > DFSDM_CKAB_TIMEOUT)
{
/* Set timeout status */
status = HAL_TIMEOUT;
break;
}
}
if(status == HAL_OK)
{
/* Activate clock absence detection interrupt */
filter0Instance->FLTCR2 |= DFSDM_FLTCR2_CKABIE;
/* Start clock absence detection */
hdfsdm_channel->Instance->CHCFGR1 |= DFSDM_CHCFGR1_CKABEN;
}
}
/* Return function status */
return status;
}
/**