Debian Codegraph @

ports/renesas-ra/ra/ra_timer.c

raw
1 /*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2021-2023 Renesas Electronics Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdbool.h>
28 #include "hal_data.h"
29 #include "ra_config.h"
30 #include "ra_int.h"
31 #include "ra_utils.h"
32 #include "ra_timer.h"
33
34 #define AGT_CH_SIZE 2
35
36 enum AGT_SOURCE {
37 AGT_PCLKB = 0,
38 AGT_PCLKB8,
39 AGT_PCLKB2 = 3,
40 AGT_AGTKCLK,
41 AGT_AGT0UNDER,
42 AGT_AGTSCLK
43 };
44
45 static R_AGTX0_AGT16_Type *agt_regs[AGT_CH_SIZE] = {
46 (R_AGTX0_AGT16_Type *)R_AGTX0,
47 (R_AGTX0_AGT16_Type *)R_AGTX1
48 };
49
50 static uint8_t ch_to_irq[AGT_CH_SIZE] = {
51 #if defined(VECTOR_NUMBER_AGT0_INT)
52 VECTOR_NUMBER_AGT0_INT,
53 #else
54 0,
55 #endif
56 #if defined(VECTOR_NUMBER_AGT1_INT)
57 VECTOR_NUMBER_AGT1_INT,
58 #else
59 0,
60 #endif
61 };
62 static float ra_agt_freq[AGT_CH_SIZE];
63 static volatile uint32_t ra_agt_counter[AGT_CH_SIZE];
64 static AGT_TIMER_CB ra_agt_timer_cb[AGT_CH_SIZE];
65 static void *ra_agt_timer_param[AGT_CH_SIZE];
66
67 void ra_agt_timer_set_callback(uint32_t ch, AGT_TIMER_CB cb, void *param) {
68 ra_agt_timer_cb[ch] = cb;
69 ra_agt_timer_param[ch] = param;
70 }
71
72 static void ra_agt_timer_chk_callback(uint32_t ch) {
73 ra_agt_counter[ch] += 1;
74 if (ra_agt_timer_cb[ch]) {
75 (*ra_agt_timer_cb[ch])(ra_agt_timer_param[ch]);
76 }
77 }
78
79 void ra_agt_timer_start(uint32_t ch) {
80 agt_regs[ch]->CTRL.AGTCR_b.TSTART = 1; /* start counter */
81 }
82
83 void ra_agt_timer_stop(uint32_t ch) {
84 agt_regs[ch]->CTRL.AGTCR_b.TSTART = 0; /* stop counter */
85 }
86
87 void ra_agt_timer_set_freq(uint32_t ch, float freq) {
88 R_AGTX0_AGT16_Type *agt_reg = agt_regs[ch];
89 uint8_t source = 0;
90 uint16_t period = 0;
91 uint8_t cks = 0;
92 ra_agt_counter[ch] = 0;
93 if (freq > (float)(PCLK / 2)) {
94 return;
95 } else if (freq > 1000.0) {
96 source = AGT_PCLKB2;
97 period = (uint16_t)((float)(PCLK / 2) / freq);
98 } else if (freq > 1.0) {
99 source = AGT_AGTSCLK;
100 cks = 2;
101 period = (uint16_t)((float)(32768 / 4) / freq);
102 } else if (freq > 0.01) {
103 source = AGT_AGTSCLK;
104 period = (uint16_t)((float)(32768 / 128) / freq);
105 cks = 7;
106 } else {
107 return;
108 }
109 ra_agt_freq[ch] = freq;
110 agt_reg->CTRL.AGTCR_b.TSTART = 0; // stop counter
111 agt_reg->CTRL.AGTMR2 = cks;
112 agt_reg->CTRL.AGTMR1 = (uint8_t)(source << 4); // mode is timer mode
113 agt_reg->AGT = (uint16_t)period;
114 }
115
116 float ra_agt_timer_get_freq(uint32_t ch) {
117 return ra_agt_freq[ch];
118 }
119
120 void ra_agt_timer_init(uint32_t ch, float freq) {
121 R_AGTX0_AGT16_Type *agt_reg = agt_regs[ch];
122 if (ch == 0) {
123 ra_mstpcrd_start(R_MSTP_MSTPCRD_MSTPD3_Msk);
124 } else {
125 ra_mstpcrd_start(R_MSTP_MSTPCRD_MSTPD2_Msk);
126 }
127 ra_agt_timer_set_freq(ch, freq);
128 agt_reg->CTRL.AGTCR_b.TUNDF = 1; // underflow interrupt
129 R_BSP_IrqCfgEnable((IRQn_Type const)ch_to_irq[ch], RA_PRI_TIM5, (void *)NULL);
130 }
131
132 void ra_agt_timer_deinit(uint32_t ch) {
133 NVIC_DisableIRQ((IRQn_Type const)ch_to_irq[ch]);
134 ra_agt_timer_stop(ch);
135 ra_agt_timer_cb[ch] = (AGT_TIMER_CB)0;
136 ra_agt_timer_param[ch] = (void *)0;
137 if (ch == 0) {
138 ra_mstpcrd_stop(R_MSTP_MSTPCRD_MSTPD3_Msk);
139 } else {
140 ra_mstpcrd_stop(R_MSTP_MSTPCRD_MSTPD2_Msk);
141 }
142 }
143
144 __WEAK void agt_int_isr(void) {
145 IRQn_Type irq = R_FSP_CurrentIrqGet();
146 uint32_t ch = irq_to_ch[(uint32_t)irq];
147 R_BSP_IrqStatusClear(irq);
148 ra_agt_timer_chk_callback(ch);
149 }
150
151 extern uint32_t uwTick;
152
153 uint32_t mtick() {
154 return uwTick;
155 }