forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstm32_pulsecount.c
More file actions
1790 lines (1450 loc) · 48.4 KB
/
Copy pathstm32_pulsecount.c
File metadata and controls
1790 lines (1450 loc) · 48.4 KB
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
/****************************************************************************
* arch/arm/src/common/stm32/stm32_pulsecount.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/debug.h>
#include <nuttx/arch.h>
#include <arch/board/board.h>
#include "arm_internal.h"
#include "chip.h"
#include "stm32_pulsecount.h"
#include "stm32.h"
/* Generalized pulse count support for all STM32 families */
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Timer Definitions ********************************************************/
/* Pulsecount is supported by advanced timers only. */
#define TIMTYPE_ADVANCED 5
#define TIMTYPE_TIM1 TIMTYPE_ADVANCED
#define TIMTYPE_TIM8 TIMTYPE_ADVANCED
/* Advanced timer clock source, RCC EN offset, enable bit,
* RCC RST offset, reset bit to use
*/
#define TIMCLK_TIM1 STM32_TIM1_CLKIN
#define TIMRCCEN_TIM1 STM32_RCC_APB2ENR
#define TIMEN_TIM1 RCC_APB2ENR_TIM1EN
#define TIMRCCRST_TIM1 STM32_RCC_APB2RSTR
#define TIMRST_TIM1 RCC_APB2RSTR_TIM1RST
#define TIMCLK_TIM8 STM32_TIM8_CLKIN
#define TIMRCCEN_TIM8 STM32_RCC_APB2ENR
#define TIMEN_TIM8 RCC_APB2ENR_TIM8EN
#define TIMRCCRST_TIM8 STM32_RCC_APB2RSTR
#define TIMRST_TIM8 RCC_APB2RSTR_TIM8RST
/* The TIM1/TIM8 update-event interrupt vector is named differently across
* families. This will be unified later.
*/
#if defined(STM32_IRQ_TIM1UP)
# define PULSECOUNT_TIM1_IRQ STM32_IRQ_TIM1UP
#elif defined(STM32_IRQ_TIM1_UP)
# define PULSECOUNT_TIM1_IRQ STM32_IRQ_TIM1_UP
#elif defined(STM32_IRQ_TIM1_BRK)
# define PULSECOUNT_TIM1_IRQ STM32_IRQ_TIM1_BRK
#endif
#if defined(STM32_IRQ_TIM8UP)
# define PULSECOUNT_TIM8_IRQ STM32_IRQ_TIM8UP
#elif defined(STM32_IRQ_TIM8_UP)
# define PULSECOUNT_TIM8_IRQ STM32_IRQ_TIM8_UP
#endif
/* Default GPIO pins state */
#if defined(CONFIG_STM32_STM32F10XX)
# define PINCFG_DEFAULT (GPIO_INPUT | GPIO_CNF_INFLOAT | GPIO_MODE_INPUT)
#else
# define PINCFG_DEFAULT (GPIO_INPUT | GPIO_FLOAT)
#endif
#define PULSECOUNT_POL_NEG 1
#define PULSECOUNT_IDLE_ACTIVE 1
/* Debug ********************************************************************/
#ifdef CONFIG_DEBUG_TIMER_INFO
# define pulsecount_dumpgpio(p,m) stm32_dumpgpio(p,m)
#else
# define pulsecount_dumpgpio(p,m)
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/* Pulsecount output configuration */
struct stm32_out_s
{
uint8_t in_use:1;
uint8_t pol:1;
uint8_t idle:1;
uint8_t _res:5;
uint32_t pincfg;
};
/* Pulsecount channel configuration */
struct stm32_chan_s
{
uint8_t channel;
struct stm32_out_s out1;
};
/* This structure represents the state of one pulsecount timer */
struct stm32_tim_s
{
struct stm32_chan_s channel;
uint8_t timid:5;
uint8_t timtype:3;
uint8_t t_dts:3;
uint8_t _res:5;
uint8_t irq;
uint8_t prev;
uint8_t curr;
uint32_t count;
uint32_t frequency;
uint32_t base;
uint32_t pclk;
void *handle;
};
struct stm32_pulsecount_s
{
const struct pulsecount_ops_s *ops;
struct stm32_tim_s *timer;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Register access */
static uint32_t pulsecount_getreg(struct stm32_tim_s *priv, int offset);
static void pulsecount_putreg(struct stm32_tim_s *priv, int offset,
uint32_t value);
static void pulsecount_modifyreg(struct stm32_tim_s *priv, uint32_t offset,
uint32_t clearbits, uint32_t setbits);
#ifdef CONFIG_DEBUG_TIMER_INFO
static void pulsecount_dumpregs(struct pulsecount_lowerhalf_s *dev,
const char *msg);
#else
# define pulsecount_dumpregs(priv,msg)
#endif
/* Timer management */
static int pulsecount_ccr_update(struct pulsecount_lowerhalf_s *dev,
uint8_t index, uint32_t ccr);
static int pulsecount_duty_update(struct pulsecount_lowerhalf_s *dev,
uint8_t channel, ub16_t duty);
static int pulsecount_frequency_update(struct pulsecount_lowerhalf_s *dev,
uint32_t frequency);
static int pulsecount_timer_configure(struct stm32_tim_s *priv);
static int pulsecount_channel_configure(struct pulsecount_lowerhalf_s *dev,
uint8_t channel);
static int pulsecount_output_configure(struct stm32_tim_s *priv,
struct stm32_chan_s *chan);
static int pulsecount_outputs_enable(struct pulsecount_lowerhalf_s *dev,
uint16_t outputs, bool state);
static void pulsecount_moe_enable(struct pulsecount_lowerhalf_s *dev,
bool enable);
static int pulsecount_configure(struct pulsecount_lowerhalf_s *dev);
static int pulsecount_timer(struct pulsecount_lowerhalf_s *dev,
const struct pulsecount_info_s *info);
static int pulsecount_interrupt(struct pulsecount_lowerhalf_s *dev);
# ifdef CONFIG_STM32_TIM1_PULSECOUNT
static int pulsecount_tim1interrupt(int irq, void *context, void *arg);
# endif
# ifdef CONFIG_STM32_TIM8_PULSECOUNT
static int pulsecount_tim8interrupt(int irq, void *context, void *arg);
# endif
static uint8_t pulsecount_count(uint32_t count);
/* Pulsecount driver methods */
static int pulsecount_ll_setup(struct pulsecount_lowerhalf_s *dev);
static int pulsecount_ll_shutdown(struct pulsecount_lowerhalf_s *dev);
static int pulsecount_ll_stop(struct pulsecount_lowerhalf_s *dev);
static int pulsecount_ll_ioctl(struct pulsecount_lowerhalf_s *dev,
int cmd, unsigned long arg);
static int pulsecount_setup(struct pulsecount_lowerhalf_s *dev);
static int pulsecount_shutdown(struct pulsecount_lowerhalf_s *dev);
static int pulsecount_start(struct pulsecount_lowerhalf_s *dev,
const struct pulsecount_info_s *info,
void *handle);
static int pulsecount_stop(struct pulsecount_lowerhalf_s *dev);
static int pulsecount_ioctl(struct pulsecount_lowerhalf_s *dev,
int cmd, unsigned long arg);
/****************************************************************************
* Private Data
****************************************************************************/
#ifdef CONFIG_STM32_TIM1_PULSECOUNT
static struct stm32_tim_s g_pulsecount1dev =
{
.channel =
{
.channel = CONFIG_STM32_TIM1_PULSECOUNT_CHANNEL,
#if CONFIG_STM32_TIM1_PULSECOUNT_CHANNEL == 1
.out1 =
{
.in_use = 1,
.pol = CONFIG_STM32_TIM1_PULSECOUNT_POL,
.idle = CONFIG_STM32_TIM1_PULSECOUNT_IDLE,
.pincfg = GPIO_TIM1_CH1OUT,
},
#elif CONFIG_STM32_TIM1_PULSECOUNT_CHANNEL == 2
.out1 =
{
.in_use = 1,
.pol = CONFIG_STM32_TIM1_PULSECOUNT_POL,
.idle = CONFIG_STM32_TIM1_PULSECOUNT_IDLE,
.pincfg = GPIO_TIM1_CH2OUT,
},
#elif CONFIG_STM32_TIM1_PULSECOUNT_CHANNEL == 3
.out1 =
{
.in_use = 1,
.pol = CONFIG_STM32_TIM1_PULSECOUNT_POL,
.idle = CONFIG_STM32_TIM1_PULSECOUNT_IDLE,
.pincfg = GPIO_TIM1_CH3OUT,
},
#elif CONFIG_STM32_TIM1_PULSECOUNT_CHANNEL == 4
.out1 =
{
.in_use = 1,
.pol = CONFIG_STM32_TIM1_PULSECOUNT_POL,
.idle = CONFIG_STM32_TIM1_PULSECOUNT_IDLE,
.pincfg = GPIO_TIM1_CH4OUT,
},
#endif
},
.timid = 1,
.timtype = TIMTYPE_TIM1,
.t_dts = CONFIG_STM32_TIM1_PULSECOUNT_TDTS,
.irq = PULSECOUNT_TIM1_IRQ,
.base = STM32_TIM1_BASE,
.pclk = TIMCLK_TIM1,
};
#endif /* CONFIG_STM32_TIM1_PULSECOUNT */
#ifdef CONFIG_STM32_TIM8_PULSECOUNT
static struct stm32_tim_s g_pulsecount8dev =
{
.channel =
{
.channel = CONFIG_STM32_TIM8_PULSECOUNT_CHANNEL,
#if CONFIG_STM32_TIM8_PULSECOUNT_CHANNEL == 1
.out1 =
{
.in_use = 1,
.pol = CONFIG_STM32_TIM8_PULSECOUNT_POL,
.idle = CONFIG_STM32_TIM8_PULSECOUNT_IDLE,
.pincfg = GPIO_TIM8_CH1OUT,
},
#elif CONFIG_STM32_TIM8_PULSECOUNT_CHANNEL == 2
.out1 =
{
.in_use = 1,
.pol = CONFIG_STM32_TIM8_PULSECOUNT_POL,
.idle = CONFIG_STM32_TIM8_PULSECOUNT_IDLE,
.pincfg = GPIO_TIM8_CH2OUT,
},
#elif CONFIG_STM32_TIM8_PULSECOUNT_CHANNEL == 3
.out1 =
{
.in_use = 1,
.pol = CONFIG_STM32_TIM8_PULSECOUNT_POL,
.idle = CONFIG_STM32_TIM8_PULSECOUNT_IDLE,
.pincfg = GPIO_TIM8_CH3OUT,
},
#elif CONFIG_STM32_TIM8_PULSECOUNT_CHANNEL == 4
.out1 =
{
.in_use = 1,
.pol = CONFIG_STM32_TIM8_PULSECOUNT_POL,
.idle = CONFIG_STM32_TIM8_PULSECOUNT_IDLE,
.pincfg = GPIO_TIM8_CH4OUT,
},
#endif
},
.timid = 8,
.timtype = TIMTYPE_TIM8,
.t_dts = CONFIG_STM32_TIM8_PULSECOUNT_TDTS,
.irq = PULSECOUNT_TIM8_IRQ,
.base = STM32_TIM8_BASE,
.pclk = TIMCLK_TIM8,
};
#endif /* CONFIG_STM32_TIM8_PULSECOUNT */
static const struct pulsecount_ops_s g_pulsecountops =
{
.setup = pulsecount_setup,
.shutdown = pulsecount_shutdown,
.start = pulsecount_start,
.stop = pulsecount_stop,
.ioctl = pulsecount_ioctl,
};
#ifdef CONFIG_STM32_TIM1_PULSECOUNT
static struct stm32_pulsecount_s g_pulsecount1lower =
{
.ops = &g_pulsecountops,
.timer = &g_pulsecount1dev,
};
#endif
#ifdef CONFIG_STM32_TIM8_PULSECOUNT
static struct stm32_pulsecount_s g_pulsecount8lower =
{
.ops = &g_pulsecountops,
.timer = &g_pulsecount8dev,
};
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: pulsecount_reg_is_32bit
****************************************************************************/
static bool pulsecount_reg_is_32bit(uint8_t timtype, uint32_t offset)
{
bool ret = false;
/* TODO: If all the advanced-timer registers are safely word-accessible
* then this helper could be dropped and every access made 32-bit.
* This is NOT verified yet.
*/
if (timtype == TIMTYPE_ADVANCED)
{
if (offset == STM32_ATIM_CR2_OFFSET ||
offset == STM32_ATIM_CCMR1_OFFSET ||
offset == STM32_ATIM_CCMR2_OFFSET ||
offset == STM32_ATIM_CCER_OFFSET ||
offset == STM32_ATIM_BDTR_OFFSET)
{
ret = true;
}
}
return ret;
}
/****************************************************************************
* Name: pulsecount_getreg
*
* Description:
* Read the value of an pulsecount timer register
*
* Input Parameters:
* priv - A reference to the pulsecount block status
* offset - The offset to the register to read
*
* Returned Value:
* The current contents of the specified register
*
****************************************************************************/
static uint32_t pulsecount_getreg(struct stm32_tim_s *priv, int offset)
{
uint32_t retval = 0;
if (pulsecount_reg_is_32bit(priv->timtype, offset) == true)
{
/* 32-bit register */
retval = getreg32(priv->base + offset);
}
else
{
/* 16-bit register */
retval = getreg16(priv->base + offset);
}
/* Return 32-bit value */
return retval;
}
/****************************************************************************
* Name: pulsecount_putreg
*
* Description:
* Read the value of an pulsecount timer register
*
* Input Parameters:
* priv - A reference to the pulsecount block status
* offset - The offset to the register to read
*
* Returned Value:
* None
*
****************************************************************************/
static void pulsecount_putreg(struct stm32_tim_s *priv, int offset,
uint32_t value)
{
if (pulsecount_reg_is_32bit(priv->timtype, offset) == true)
{
/* 32-bit register */
putreg32(value, priv->base + offset);
}
else
{
/* 16-bit register */
putreg16((uint16_t)value, priv->base + offset);
}
}
/****************************************************************************
* Name: pulsecount_modifyreg
*
* Description:
* Modify timer register (32-bit or 16-bit)
*
* Input Parameters:
* priv - A reference to the pulsecount block status
* offset - The offset to the register to read
* clrbits - The bits to clear
* setbits - The bits to set
*
* Returned Value:
* None
*
****************************************************************************/
static void pulsecount_modifyreg(struct stm32_tim_s *priv, uint32_t offset,
uint32_t clearbits, uint32_t setbits)
{
if (pulsecount_reg_is_32bit(priv->timtype, offset) == true)
{
/* 32-bit register */
modifyreg32(priv->base + offset, clearbits, setbits);
}
else
{
/* 16-bit register */
modifyreg16(priv->base + offset, (uint16_t)clearbits,
(uint16_t)setbits);
}
}
/****************************************************************************
* Name: pulsecount_dumpregs
*
* Description:
* Dump all timer registers.
*
* Input Parameters:
* dev - A reference to the lower half pulsecount driver state structure
*
* Returned Value:
* None
*
****************************************************************************/
#ifdef CONFIG_DEBUG_TIMER_INFO
static void pulsecount_dumpregs(struct pulsecount_lowerhalf_s *dev,
const char *msg)
{
struct stm32_tim_s *priv = (struct stm32_tim_s *)dev;
_info("%s:\n", msg);
_info(" CR1: %04x CR2: %04x SMCR: %04x DIER: %04x\n",
pulsecount_getreg(priv, STM32_GTIM_CR1_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_CR2_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_SMCR_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_DIER_OFFSET));
_info(" SR: %04x EGR: %04x CCMR1: %04x CCMR2: %04x\n",
pulsecount_getreg(priv, STM32_GTIM_SR_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_EGR_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_CCMR1_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_CCMR2_OFFSET));
_info(" CCER: %04x CNT: %04x PSC: %04x ARR: %04x\n",
pulsecount_getreg(priv, STM32_GTIM_CCER_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_CNT_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_PSC_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_ARR_OFFSET));
if (priv->timid == 1 || priv->timid == 8)
{
_info(" RCR: %04x BDTR: %04x\n",
pulsecount_getreg(priv, STM32_ATIM_RCR_OFFSET),
pulsecount_getreg(priv, STM32_ATIM_BDTR_OFFSET));
}
_info(" CCR1: %04x CCR2: %04x CCR3: %04x CCR4: %04x\n",
pulsecount_getreg(priv, STM32_GTIM_CCR1_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_CCR2_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_CCR3_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_CCR4_OFFSET));
_info(" DCR: %04x DMAR: %04x\n",
pulsecount_getreg(priv, STM32_GTIM_DCR_OFFSET),
pulsecount_getreg(priv, STM32_GTIM_DMAR_OFFSET));
}
#endif
/****************************************************************************
* Name: pulsecount_ccr_update
****************************************************************************/
static int pulsecount_ccr_update(struct pulsecount_lowerhalf_s *dev,
uint8_t index, uint32_t ccr)
{
struct stm32_tim_s *priv = (struct stm32_tim_s *)dev;
uint32_t offset = 0;
/* CCR channel indices are one-based to match timer channel numbers. */
switch (index)
{
case 1:
{
offset = STM32_GTIM_CCR1_OFFSET;
break;
}
case 2:
{
offset = STM32_GTIM_CCR2_OFFSET;
break;
}
case 3:
{
offset = STM32_GTIM_CCR3_OFFSET;
break;
}
case 4:
{
offset = STM32_GTIM_CCR4_OFFSET;
break;
}
default:
{
_err("ERROR: No such CCR: %u\n", index);
return -EINVAL;
}
}
/* Update CCR register */
pulsecount_putreg(priv, offset, ccr);
return OK;
}
/****************************************************************************
* Name: pulsecount_duty_update
*
* Description:
* Try to change only channel duty
*
* Input Parameters:
* dev - A reference to the lower half driver state structure
* channel - Channel to by updated
* duty - New duty
*
* Returned Value:
* Zero on success; a negated errno value on failure
*
****************************************************************************/
static int pulsecount_duty_update(struct pulsecount_lowerhalf_s *dev,
uint8_t channel, ub16_t duty)
{
struct stm32_tim_s *priv = (struct stm32_tim_s *)dev;
uint32_t reload = 0;
uint32_t ccr = 0;
/* We don't want compilation warnings if no DEBUGASSERT */
UNUSED(priv);
DEBUGASSERT(priv != NULL);
_info("TIM%u channel: %u duty: %08" PRIx32 "\n",
priv->timid, channel, duty);
/* Get the reload values */
reload = pulsecount_getreg(priv, STM32_GTIM_ARR_OFFSET);
/* Duty cycle:
*
* duty cycle = ccr / reload (fractional value)
*/
ccr = b16toi(duty * reload + b16HALF);
_info("ccr: %" PRIu32 "\n", ccr);
/* Write corresponding CCR register */
return pulsecount_ccr_update(dev, channel, ccr);
}
/****************************************************************************
* Name: pulsecount_frequency_update
*
* Description:
* Update a pulsecount timer frequency
*
****************************************************************************/
static int pulsecount_frequency_update(struct pulsecount_lowerhalf_s *dev,
uint32_t frequency)
{
struct stm32_tim_s *priv = (struct stm32_tim_s *)dev;
uint32_t reload = 0;
uint32_t timclk = 0;
uint32_t prescaler = 0;
/* Calculate optimal values for the timer prescaler and for the timer
* reload register. If 'frequency' is the desired frequency, then
*
* reload = timclk / frequency
* timclk = pclk / presc
*
* Or,
*
* reload = pclk / presc / frequency
*
* There are many solutions to this, but the best solution will be the one
* that has the largest reload value and the smallest prescaler value.
* That is the solution that should give us the most accuracy in the timer
* control. Subject to:
*
* 0 <= presc <= 65536
* 1 <= reload <= 65535
*
* So presc = pclk / 65535 / frequency would be optimal.
*
* Example:
*
* pclk = 42 MHz
* frequency = 100 Hz
*
* prescaler = 42,000,000 / 65,535 / 100
* = 6.4 (or 7 -- taking the ceiling always)
* timclk = 42,000,000 / 7
* = 6,000,000
* reload = 6,000,000 / 100
* = 60,000
*/
prescaler = (priv->pclk / frequency + 65534) / 65535;
if (prescaler < 1)
{
prescaler = 1;
}
else if (prescaler > 65536)
{
prescaler = 65536;
}
timclk = priv->pclk / prescaler;
reload = timclk / frequency;
if (reload < 2)
{
reload = 1;
}
else if (reload > 65535)
{
reload = 65535;
}
else
{
reload--;
}
_info("TIM%u PCLK: %" PRIu32" frequency: %" PRIu32
" TIMCLK: %" PRIu32 " "
"prescaler: %" PRIu32 " reload: %" PRIu32 "\n",
priv->timid, priv->pclk, frequency, timclk, prescaler, reload);
/* Set the reload and prescaler values */
pulsecount_putreg(priv, STM32_GTIM_ARR_OFFSET, reload);
pulsecount_putreg(priv, STM32_GTIM_PSC_OFFSET, (uint16_t)(prescaler - 1));
return OK;
}
/****************************************************************************
* Name: pulsecount_timer_configure
*
* Description:
* Initial configuration for pulsecount timer
*
****************************************************************************/
static int pulsecount_timer_configure(struct stm32_tim_s *priv)
{
uint16_t cr1 = 0;
/* Set up the advanced timer CR1 register. */
cr1 = pulsecount_getreg(priv, STM32_GTIM_CR1_OFFSET);
/* Pulsecount always uses edge-aligned up-counting mode. */
cr1 &= ~(GTIM_CR1_DIR | GTIM_CR1_CMS_MASK);
cr1 |= GTIM_CR1_EDGE;
cr1 &= ~GTIM_CR1_CKD_MASK;
cr1 |= priv->t_dts << GTIM_CR1_CKD_SHIFT;
/* Enable ARR preload to preserve the previous pulsecount behavior. */
cr1 |= GTIM_CR1_ARPE;
/* Write CR1 */
pulsecount_putreg(priv, STM32_GTIM_CR1_OFFSET, cr1);
return OK;
}
/****************************************************************************
* Name: pulsecount_channel_configure
*
* Description:
* Configure pulsecount output compare for a channel
*
****************************************************************************/
static int pulsecount_channel_configure(struct pulsecount_lowerhalf_s *dev,
uint8_t channel)
{
struct stm32_tim_s *priv = (struct stm32_tim_s *)dev;
uint32_t chanmode = 0;
uint32_t ocmode = 0;
uint32_t ccmr = 0;
uint32_t offset = 0;
int ret = OK;
/* Configure output compare mode */
chanmode = GTIM_CCMR_MODE_PWM1;
/* Get CCMR offset */
switch (channel)
{
case 1:
case 2:
{
offset = STM32_GTIM_CCMR1_OFFSET;
break;
}
case 3:
case 4:
{
offset = STM32_GTIM_CCMR2_OFFSET;
break;
}
default:
{
_err("ERROR: No such channel: %u\n", channel);
ret = -EINVAL;
goto errout;
}
}
/* Get current registers */
ccmr = pulsecount_getreg(priv, offset);
/* output compare configuration.
* NOTE: The CCMRx registers are identical if the channels are outputs.
*/
switch (channel)
{
/* Configure channel 1/3 */
case 1:
case 3:
{
ccmr &= ~(ATIM_CCMR1_CC1S_MASK | ATIM_CCMR1_OC1M_MASK |
ATIM_CCMR1_OC1PE);
ocmode |= (ATIM_CCMR_CCS_CCOUT << ATIM_CCMR1_CC1S_SHIFT);
ocmode |= (chanmode << ATIM_CCMR1_OC1M_SHIFT);
ocmode |= ATIM_CCMR1_OC1PE;
#ifdef ATIM_CCMR1_OC1M
/* Clear the extended (bit 3) output-compare-mode bit on the timer
* IP versions that have it.
*/
ccmr &= ~(ATIM_CCMR1_OC1M);
#endif
break;
}
/* Configure channel 2/4 */
case 2:
case 4:
{
ccmr &= ~(ATIM_CCMR1_CC2S_MASK | ATIM_CCMR1_OC2M_MASK |
ATIM_CCMR1_OC2PE);
ocmode |= (ATIM_CCMR_CCS_CCOUT << ATIM_CCMR1_CC2S_SHIFT);
ocmode |= (chanmode << ATIM_CCMR1_OC2M_SHIFT);
ocmode |= ATIM_CCMR1_OC2PE;
#ifdef ATIM_CCMR1_OC2M
/* Clear the extended (bit 3) output-compare-mode bit on the timer
* IP versions that have it.
*/
ccmr &= ~(ATIM_CCMR1_OC2M);
#endif
break;
}
}
/* Set the selected output compare configuration */
ccmr |= ocmode;
/* Write CCMRx registers */
pulsecount_putreg(priv, offset, ccmr);
errout:
return ret;
}
/****************************************************************************
* Name: pulsecount_output_configure
*
* Description:
* Configure pulsecount output for given channel
*
****************************************************************************/
static int pulsecount_output_configure(struct stm32_tim_s *priv,
struct stm32_chan_s *chan)
{
uint32_t cr2 = 0;
uint32_t ccer = 0;
uint8_t channel = 0;
/* Get channel */
channel = chan->channel;
/* Get current registers state */
cr2 = pulsecount_getreg(priv, STM32_GTIM_CR2_OFFSET);
ccer = pulsecount_getreg(priv, STM32_GTIM_CCER_OFFSET);
/* | OISx | IDLE | advanced timers | CR2 register
* | CCxP | POL | all pulsecount timers | CCER register
*/
/* Configure output polarity (all pulsecount timers) */
if (chan->out1.pol == PULSECOUNT_POL_NEG)
{
ccer |= (GTIM_CCER_CC1P << ((channel - 1) * 4));
}
else
{
ccer &= ~(GTIM_CCER_CC1P << ((channel - 1) * 4));
}
if (priv->timtype == TIMTYPE_ADVANCED)
{
/* Configure output IDLE State */
if (chan->out1.idle == PULSECOUNT_IDLE_ACTIVE)
{
cr2 |= (ATIM_CR2_OIS1 << ((channel - 1) * 2));
}
else
{
cr2 &= ~(ATIM_CR2_OIS1 << ((channel - 1) * 2));
}
}
/* Write registers */
pulsecount_modifyreg(priv, STM32_GTIM_CR2_OFFSET, 0, cr2);
pulsecount_modifyreg(priv, STM32_GTIM_CCER_OFFSET, 0, ccer);
return OK;
}
/****************************************************************************
* Name: pulsecount_outputs_enable
*
* Description:
* Enable/disable given timer pulsecount outputs.
*
* NOTE: This is bulk operation - we can enable/disable many outputs
* at one time
*
* Input Parameters:
* dev - A reference to the lower half driver state structure
* outputs - outputs to set (look at enum stm32_pulsecount_chan_e)
* state - Enable/disable operation
*
****************************************************************************/
static int pulsecount_outputs_enable(struct pulsecount_lowerhalf_s *dev,
uint16_t outputs, bool state)
{
struct stm32_tim_s *priv = (struct stm32_tim_s *)dev;
uint32_t ccer = 0;
uint32_t regval = 0;
/* Get current register state */
ccer = pulsecount_getreg(priv, STM32_GTIM_CCER_OFFSET);
/* Get outputs configuration */
regval |= ((outputs & (1 << 0)) ? GTIM_CCER_CC1E : 0);
regval |= ((outputs & (1 << 2)) ? GTIM_CCER_CC2E : 0);
regval |= ((outputs & (1 << 4)) ? GTIM_CCER_CC3E : 0);
regval |= ((outputs & (1 << 6)) ? GTIM_CCER_CC4E : 0);
if (state == true)
{
/* Enable outputs - set bits */
ccer |= regval;
}
else
{
/* Disable outputs - reset bits */
ccer &= ~regval;
}
/* Write register */
pulsecount_putreg(priv, STM32_GTIM_CCER_OFFSET, ccer);
return OK;
}
/****************************************************************************
* Name: pulsecount_moe_enable