1818#define DBG_LVL DBG_INFO
1919#include <rtdbg.h>
2020
21- static const struct rt_ecap_config ecap1_config =
21+ enum
2222{
23- .name = "ecap1" ,
24- .instance = ECAP1 ,
25- .irq_type = ECAP1_IRQn ,
26- .input_xbar = XBAR_INPUT7 ,
27- .input_source = GPIO_PIN_16 ,
28- .gpio_port = GPIOA ,
29- .gpio_pin = GPIO_PIN_16 ,
30- .gpio_mux = ALT0_FUNCTION ,
23+ #ifdef BSP_USING_ECAP1
24+ ECAP1_INDEX ,
25+ #endif
26+ #ifdef BSP_USING_ECAP2
27+ ECAP2_INDEX ,
28+ #endif
29+ #ifdef BSP_USING_ECAP3
30+ ECAP3_INDEX ,
31+ #endif
32+ #ifdef BSP_USING_ECAP4
33+ ECAP4_INDEX ,
34+ #endif
35+ #ifdef BSP_USING_ECAP5
36+ ECAP5_INDEX ,
37+ #endif
38+ #ifdef BSP_USING_ECAP6
39+ ECAP6_INDEX ,
40+ #endif
41+ #ifdef BSP_USING_ECAP7
42+ ECAP7_INDEX ,
43+ #endif
3144};
3245
33- static struct rt_ecap_device ecap1_dev ;
34-
46+ #ifdef BSP_USING_ECAP1
3547void ECAP1_IRQHandler (void );
48+ #endif
49+ #ifdef BSP_USING_ECAP2
50+ void ECAP2_IRQHandler (void );
51+ #endif
52+ #ifdef BSP_USING_ECAP3
53+ void ECAP3_IRQHandler (void );
54+ #endif
55+ #ifdef BSP_USING_ECAP4
56+ void ECAP4_IRQHandler (void );
57+ #endif
58+ #ifdef BSP_USING_ECAP5
59+ void ECAP5_IRQHandler (void );
60+ #endif
61+ #ifdef BSP_USING_ECAP6
62+ void ECAP6_IRQHandler (void );
63+ #endif
64+ #ifdef BSP_USING_ECAP7
65+ void ECAP7_IRQHandler (void );
66+ #endif
67+
68+ static const struct rt_ecap_config ecap_config [] =
69+ {
70+ #ifdef BSP_USING_ECAP1
71+ {
72+ .name = "ecap1" ,
73+ .instance = ECAP1 ,
74+ .irq_type = ECAP1_IRQn ,
75+ .input_signal = ECAP_INPUT_XBAR_INPUT7 ,
76+ .input_xbar = XBAR_INPUT7 ,
77+ .input_source = GPIO_PIN_16 ,
78+ .pre_scaler = 30U ,
79+ .gpio_port = GPIOA ,
80+ .gpio_pin = GPIO_PIN_16 ,
81+ .gpio_mux = ALT0_FUNCTION ,
82+ .irq_handler = ECAP1_IRQHandler
83+ },
84+ #endif
85+ #ifdef BSP_USING_ECAP2
86+ {
87+ .name = "ecap2" ,
88+ .instance = ECAP2 ,
89+ .irq_type = ECAP2_IRQn ,
90+ .input_signal = ECAP_INPUT_XBAR_INPUT8 ,
91+ .input_xbar = XBAR_INPUT8 ,
92+ .input_source = GPIO_PIN_17 ,
93+ .pre_scaler = 30U ,
94+ .gpio_port = GPIOA ,
95+ .gpio_pin = GPIO_PIN_17 ,
96+ .gpio_mux = ALT0_FUNCTION ,
97+ .irq_handler = ECAP2_IRQHandler
98+ }
99+ #endif
100+ #ifdef BSP_USING_ECAP3
101+ {
102+ .name = "ecap3 ",
103+ .instance = ECAP3 ,
104+ .irq_type = ECAP3_IRQn ,
105+ .input_signal = ECAP_INPUT_XBAR_INPUT3 ,
106+ .input_xbar = XBAR_INPUT3 ,
107+ .input_source = GPIO_PIN_14 ,
108+ .pre_scaler = 30U ,
109+ .gpio_port = GPIOA ,
110+ .gpio_pin = GPIO_PIN_14 ,
111+ .gpio_mux = ALT0_FUNCTION ,
112+ .irq_handler = ECAP3_IRQHandler
113+ }
114+ #endif
115+ #ifdef BSP_USING_ECAP4
116+ {
117+ .name = "ecap4" ,
118+ .instance = ECAP4 ,
119+ .irq_type = ECAP4_IRQn ,
120+ .input_signal = ECAP_INPUT_XBAR_INPUT4 ,
121+ .input_xbar = XBAR_INPUT4 ,
122+ .input_source = GPIO_PIN_15 ,
123+ .pre_scaler = 30U ,
124+ .gpio_port = GPIOA ,
125+ .gpio_pin = GPIO_PIN_15 ,
126+ .gpio_mux = ALT0_FUNCTION ,
127+ .irq_handler = ECAP4_IRQHandler
128+ }
129+ #endif
130+ #ifdef BSP_USING_ECAP5
131+ {
132+ .name = "ecap5" ,
133+ .instance = ECAP5 ,
134+ .irq_type = ECAP5_IRQn ,
135+ .input_signal = ECAP_INPUT_XBAR_INPUT6 ,
136+ .input_xbar = XBAR_INPUT6 ,
137+ .input_source = GPIO_PIN_9 ,
138+ .pre_scaler = 30U ,
139+ .gpio_port = GPIOA ,
140+ .gpio_pin = GPIO_PIN_9 ,
141+ .gpio_mux = ALT0_FUNCTION ,
142+ .irq_handler = ECAP5_IRQHandler
143+ }
144+ #endif
145+ #ifdef BSP_USING_ECAP6
146+ {
147+ .name = "ecap6" ,
148+ .instance = ECAP6 ,
149+ .irq_type = ECAP6_IRQn ,
150+ .input_signal = ECAP_INPUT_XBAR_INPUT6 ,
151+ .input_xbar = XBAR_INPUT6 ,
152+ .input_source = GPIO_PIN_9 ,
153+ .pre_scaler = 30U ,
154+ .gpio_port = GPIOA ,
155+ .gpio_pin = GPIO_PIN_9 ,
156+ .gpio_mux = ALT0_FUNCTION ,
157+ .irq_handler = ECAP6_IRQHandler
158+ }
159+ #endif
160+ #ifdef BSP_USING_ECAP7
161+ {
162+ .name = "ecap7" ,
163+ .instance = ECAP7 ,
164+ .irq_type = ECAP7_IRQn ,
165+ .input_signal = ECAP_INPUT_XBAR_INPUT6 ,
166+ .input_xbar = XBAR_INPUT6 ,
167+ .input_source = GPIO_PIN_9 ,
168+ .pre_scaler = 30U ,
169+ .gpio_port = GPIOA ,
170+ .gpio_pin = GPIO_PIN_9 ,
171+ .gpio_mux = ALT0_FUNCTION ,
172+ .irq_handler = ECAP7_IRQHandler
173+ }
174+ #endif
175+ };
176+
177+ static struct rt_ecap_device ecap_dev [sizeof (ecap_config ) / sizeof (ecap_config [0 ])] = {0 };
36178
37179/*
38180 *
@@ -88,7 +230,7 @@ static void ecap_hw_init (const struct rt_ecap_config *config)
88230 */
89231 ECAP_enableCaptureMode (config -> instance );
90232 ECAP_setCaptureMode (config -> instance , ECAP_CONTINUOUS_CAPTURE_MODE , ECAP_EVENT_4 );
91- ECAP_setEventPrescaler (config -> instance , 0U );
233+ ECAP_setEventPrescaler (config -> instance , config -> pre_scaler );
92234
93235 ECAP_setEventPolarity (config -> instance , ECAP_EVENT_1 , ECAP_EVNT_FALLING_EDGE );
94236 ECAP_setEventPolarity (config -> instance , ECAP_EVENT_2 , ECAP_EVNT_RISING_EDGE );
@@ -100,7 +242,7 @@ static void ecap_hw_init (const struct rt_ecap_config *config)
100242 ECAP_enableCounterResetOnEvent (config -> instance , ECAP_EVENT_3 );
101243 ECAP_enableCounterResetOnEvent (config -> instance , ECAP_EVENT_4 );
102244
103- ECAP_selectECAPInput (config -> instance , ECAP_INPUT_XBAR_INPUT7 );
245+ ECAP_selectECAPInput (config -> instance , config -> input_signal );
104246
105247 ECAP_setPhaseShiftCount (config -> instance , 0U );
106248 ECAP_enableLoadCounter (config -> instance );
@@ -215,7 +357,7 @@ static rt_err_t rt_ecap_open (rt_device_t dev, rt_uint16_t oflag)
215357 ECAP_reArm (ecap -> config -> instance );
216358 ECAP_enableInterrupt (ecap -> config -> instance , ECAP_ECEINT_CEVT4_M );
217359
218- Interrupt_register (ecap -> config -> irq_type , & ECAP1_IRQHandler );
360+ Interrupt_register (ecap -> config -> irq_type , ecap -> config -> irq_handler );
219361 Interrupt_enable (ecap -> config -> irq_type );
220362 }
221363
@@ -273,7 +415,7 @@ static rt_ssize_t rt_ecap_read (rt_device_t dev, rt_off_t pos, void *buffer, rt_
273415 * 阻塞等待一次完整 capture。
274416 * 如果不希望阻塞,可以改成 RT_WAITING_NO。
275417 */
276- if (rt_sem_take (& ecap -> rx_sem , RT_WAITING_FOREVER ) != RT_EOK )
418+ if (rt_sem_take (& ecap -> rx_sem , RT_WAITING_NO ) != RT_EOK )
277419 {
278420 return 0 ;
279421 }
@@ -349,7 +491,7 @@ static rt_err_t rt_ecap_control (rt_device_t dev, int cmd, void *args)
349491
350492 case ECAP_CMD_ENABLE_IRQ :
351493 ECAP_enableInterrupt (ecap -> config -> instance , ECAP_ECEINT_CEVT4_M );
352- Interrupt_register (ecap -> config -> irq_type , & ECAP1_IRQHandler );
494+ Interrupt_register (ecap -> config -> irq_type , ecap -> config -> irq_handler );
353495 Interrupt_enable (ecap -> config -> irq_type );
354496 break ;
355497
@@ -413,34 +555,26 @@ static rt_err_t rt_hw_ecap_register (struct rt_ecap_device *ecap, const struct r
413555
414556int rt_hw_ecap_init (void )
415557{
416- rt_err_t ret ;
417-
418- ret = rt_hw_ecap_register (& ecap1_dev , & ecap1_config );
558+ rt_err_t result = RT_EOK ;
559+ rt_size_t i ;
419560
420- if ( ret != RT_EOK )
561+ for ( i = 0 ; i < ( sizeof ( ecap_config ) / sizeof ( ecap_config [ 0 ])); i ++ )
421562 {
422- LOG_E ( "register ecap1 failed, ret = %d" , ret );
423- return ret ;
563+ result = rt_hw_ecap_register ( & ecap_dev [ i ], & ecap_config [ i ] );
564+ RT_ASSERT ( result == RT_EOK ) ;
424565 }
425566
426- LOG_I ("ecap1 register done" );
427-
428- return RT_EOK ;
567+ return result ;
429568}
430569INIT_DEVICE_EXPORT (rt_hw_ecap_init );
431570
432571/*
433572 *
434573 */
435- void rt_hw_ecap1_isr ( void )
574+ void rt_hw_ecap_isr ( struct rt_ecap_device * ecap )
436575{
437- struct rt_ecap_device * ecap ;
438576 struct rt_ecap_capture cap ;
439577
440- ecap = & ecap1_dev ;
441-
442- rt_interrupt_enter ();
443-
444578 rt_memset (& cap , 0 , sizeof (cap ));
445579
446580 cap .cap1 = ECAP_getEventTimeStamp (ecap -> config -> instance , ECAP_EVENT_1 );
@@ -465,15 +599,70 @@ void rt_hw_ecap1_isr(void)
465599 ecap -> rx_callback ((struct rt_ecap_capture * )& ecap -> last_capture ,
466600 ecap -> rx_user_data );
467601 }
602+ }
468603
604+ #ifdef BSP_USING_ECAP1
605+ void ECAP1_IRQHandler (void )
606+ {
607+ rt_interrupt_enter ();
608+ rt_hw_ecap_isr (& ecap_dev [ECAP1_INDEX ]);
469609 rt_interrupt_leave ();
470610}
611+ #endif
471612
472- void ECAP1_IRQHandler (void )
613+ #ifdef BSP_USING_ECAP2
614+ void ECAP2_IRQHandler (void )
615+ {
616+ rt_interrupt_enter ();
617+ rt_hw_ecap_isr (& ecap_dev [ECAP2_INDEX ]);
618+ rt_interrupt_leave ();
619+ }
620+ #endif
621+
622+ #ifdef BSP_USING_ECAP3
623+ void ECAP3_IRQHandler (void )
624+ {
625+ rt_interrupt_enter ();
626+ rt_hw_ecap_isr (& ecap_dev [ECAP3_INDEX ]);
627+ rt_interrupt_leave ();
628+ }
629+ #endif
630+
631+ #ifdef BSP_USING_ECAP4
632+ void ECAP4_IRQHandler (void )
473633{
474- rt_hw_ecap1_isr ();
634+ rt_interrupt_enter ();
635+ rt_hw_ecap_isr (& ecap_dev [ECAP4_INDEX ]);
636+ rt_interrupt_leave ();
475637}
638+ #endif
476639
640+ #ifdef BSP_USING_ECAP5
641+ void ECAP5_IRQHandler (void )
642+ {
643+ rt_interrupt_enter ();
644+ rt_hw_ecap_isr (& ecap_dev [ECAP5_INDEX ]);
645+ rt_interrupt_leave ();
646+ }
647+ #endif
648+
649+ #ifdef BSP_USING_ECAP6
650+ void ECAP6_IRQHandler (void )
651+ {
652+ rt_interrupt_enter ();
653+ rt_hw_ecap_isr (& ecap_dev [ECAP6_INDEX ]);
654+ rt_interrupt_leave ();
655+ }
656+ #endif
657+
658+ #ifdef BSP_USING_ECAP7
659+ void ECAP7_IRQHandler (void )
660+ {
661+ rt_interrupt_enter ();
662+ rt_hw_ecap_isr (& ecap_dev [ECAP7_INDEX ]);
663+ rt_interrupt_leave ();
664+ }
665+ #endif
477666
478667#endif /* #if defined(BSP_USING_ECAP) */
479668
0 commit comments