1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: #include <sys/machine.h>
15: #ifdef CPU_STM32L4
16: 17: 18: 19: 20:
21: #include <tk/tkernel.h>
22: #include <tk/syslib.h>
23:
24: #if USE_PTMR
25:
26: typedef struct {
27: UW baddr;
28: UINT mode;
29: UW limit;
30: FP ptmrhdr;
31: PRI intpri;
32: UW psc;
33: BOOL tim32;
34: void *exinf;
35: } T_PTMRCB;
36:
37: T_PTMRCB ptmrcb[TK_MAX_PTIMER] = {
38: { TIM2_BASE, -1, 0, (FP)NULL, INTPRI_TIM2, TIM2PSC_PSC_INIT, TRUE, 0 },
39: { TIM3_BASE, -1, 0, (FP)NULL, INTPRI_TIM3, TIM3PSC_PSC_INIT, FALSE, 0 },
40: { TIM4_BASE, -1, 0, (FP)NULL, INTPRI_TIM4, TIM4PSC_PSC_INIT, FALSE, 0 },
41: { TIM5_BASE, -1, 0, (FP)NULL, INTPRI_TIM5, TIM5PSC_PSC_INIT, TRUE, 0 },
42: };
43:
44: #define TIM_CR1(n) (ptmrcb[n].baddr + TIMxCR1)
45: #define TIM_DIER(n) (ptmrcb[n].baddr + TIMxDIER)
46: #define TIM_SR(n) (ptmrcb[n].baddr + TIMxSR)
47: #define TIM_EGR(n) (ptmrcb[n].baddr + TIMxEGR)
48: #define TIM_CNT(n) (ptmrcb[n].baddr + TIMxCNT)
49: #define TIM_PSC(n) (ptmrcb[n].baddr + TIMxPSC)
50: #define TIM_ARR(n) (ptmrcb[n].baddr + TIMxARR)
51:
52: 53: 54:
55: LOCAL void ptmr_int_main( UINT intno, T_PTMRCB *p_cb)
56: {
57: out_h(p_cb->baddr + TIMxSR, 0);
58: ClearInt( intno);
59:
60: if( p_cb->ptmrhdr != NULL) {
61: (*p_cb->ptmrhdr)( p_cb->exinf);
62: }
63:
64: if( p_cb->mode == TA_ALM_PTMR) {
65: DisableInt( intno);
66: }
67: }
68:
69: LOCAL void ptmr1_inthdr( UINT intno ) { ptmr_int_main( intno, &ptmrcb[0]); }
70: LOCAL void ptmr2_inthdr( UINT intno ) { ptmr_int_main( intno, &ptmrcb[1]); }
71: LOCAL void ptmr3_inthdr( UINT intno ) { ptmr_int_main( intno, &ptmrcb[2]); }
72: LOCAL void ptmr4_inthdr( UINT intno ) { ptmr_int_main( intno, &ptmrcb[3]); }
73:
74: LOCAL void (* const inthdr_tbl[])() = {
75: ptmr1_inthdr, ptmr2_inthdr, ptmr3_inthdr, ptmr4_inthdr
76: };
77:
78: LOCAL const UINT intno_tbl[] = {
79: INTNO_TIM2, INTNO_TIM3, INTNO_TIM4, INTNO_TIM5
80: };
81:
82: 83: 84:
85: EXPORT ER StartPhysicalTimer( UINT ptmrno, UW limit, UINT mode)
86: {
87: T_DINT dint;
88: UINT intno;
89: UW limit_max;
90: ER err;
91:
92:
93: if(( ptmrno == 0 || ptmrno > TK_MAX_PTIMER )
94: || ( limit == 0 ) || ( mode > TA_CYC_PTMR )) return E_PAR;
95:
96: ptmrno--;
97: if(ptmrcb[ptmrno].tim32) {
98: limit_max = PTMR_MAX_CNT32;
99: } else {
100: limit_max = PTMR_MAX_CNT16;
101: }
102: if( limit > limit_max) {
103: return E_PAR;
104: }
105:
106: ptmrcb[ptmrno].limit = limit;
107: ptmrcb[ptmrno].mode = mode;
108:
109:
110: out_h( TIM_CR1(ptmrno), 0);
111: out_h( TIM_PSC(ptmrno), ptmrcb[ptmrno].psc);
112: out_w( TIM_ARR(ptmrno), limit);
113:
114:
115: intno = intno_tbl[ptmrno];
116: dint.intatr = TA_HLNG;
117: dint.inthdr = inthdr_tbl[ptmrno];
118: err = tk_def_int( intno, &dint);
119: if(err != E_OK) {
120: return err;
121: }
122: out_h( TIM_SR(ptmrno), 0);
123: out_h( TIM_DIER(ptmrno), TIMxDIER_UIE);
124: EnableInt( intno, ptmrcb[ptmrno].intpri);
125:
126:
127: out_h( TIM_CR1(ptmrno) , TIMxCR1_CEN | ((mode==TA_ALM_PTMR)?TIMxCR1_OPM:0));
128:
129: return E_OK;
130: }
131:
132: EXPORT ER StopPhysicalTimer( UINT ptmrno )
133: {
134:
135: if( ptmrno == 0 || ptmrno > TK_MAX_PTIMER ) return E_PAR;
136:
137: ptmrno--;
138:
139:
140: DisableInt( intno_tbl[ptmrno]);
141: out_h( TIM_CR1(ptmrno), 0);
142:
143: return E_OK;
144: }
145:
146:
147: IMPORT ER GetPhysicalTimerCount( UINT ptmrno, UW *p_count )
148: {
149:
150: if( ptmrno == 0 || ptmrno > TK_MAX_PTIMER ) return E_PAR;
151:
152: ptmrno--;
153:
154:
155: if(ptmrcb[ptmrno].tim32) {
156: *p_count = in_w( TIM_CNT(ptmrno)) ;
157: } else {
158: *p_count = in_w( TIM_CNT(ptmrno)) & PTMR_MAX_CNT16;
159: }
160:
161: return E_OK;
162: }
163:
164:
165: EXPORT ER DefinePhysicalTimerHandler( UINT ptmrno, CONST T_DPTMR *pk_dptmr )
166: {
167:
168: if( ptmrno == 0 || ptmrno > TK_MAX_PTIMER ) return E_PAR;
169:
170: ptmrno--;
171:
172:
173: if(pk_dptmr != NULL) {
174: ptmrcb[ptmrno].ptmrhdr = pk_dptmr->ptmrhdr;
175: ptmrcb[ptmrno].exinf = pk_dptmr->exinf;
176: } else {
177: ptmrcb[ptmrno].ptmrhdr = NULL;
178: }
179:
180: return E_OK;
181: }
182:
183:
184: EXPORT ER GetPhysicalTimerConfig(UINT ptmrno, T_RPTMR *pk_rptmr)
185: {
186: UW ptmrclk;
187:
188:
189: if( ptmrno == 0 || ptmrno > TK_MAX_PTIMER ) return E_PAR;
190:
191: ptmrclk = (PCLK1 / (ptmrcb[--ptmrno].psc + 1));
192:
193: pk_rptmr->ptmrclk = ptmrclk;
194: pk_rptmr->maxcount = (ptmrcb[--ptmrno].tim32)?PTMR_MAX_CNT32:PTMR_MAX_CNT16;
195: pk_rptmr->defhdr = TRUE;
196:
197: return E_OK;
198: }
199:
200: #endif
201: #endif