|
13 | 13 | * See the License for the specific language governing permissions and
|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 |
| -#include <stddef.h> |
17 | 16 | #include "us_ticker_api.h"
|
18 |
| -#include "PeripheralNames.h" |
19 |
| -#include "iodefine.h" |
20 |
| -#include "cmsis.h" |
| 17 | +#include "mbed_drv_cfg.h" |
21 | 18 |
|
22 |
| -#include "RZ_A1_Init.h" |
23 |
| -#include "vfp_neon_push_pop.h" |
24 |
| -#include "mbed_critical.h" |
| 19 | +#define SHIFT_NUM 5 /* P0/32 */ |
25 | 20 |
|
26 |
| -#define US_TICKER_TIMER_IRQn (OSTMI1TINT_IRQn) |
27 |
| -#define CPG_STBCR5_BIT_MSTP50 (0x01u) /* OSTM1 */ |
| 21 | +static int us_ticker_inited = 0; |
28 | 22 |
|
29 |
| -#define US_TICKER_CLOCK_US_DEV (1000000) |
| 23 | +void us_ticker_init(void) |
| 24 | +{ |
| 25 | + GIC_DisableIRQ(OSTMI1TINT_IRQn); |
| 26 | + GIC_ClearPendingIRQ(OSTMI1TINT_IRQn); |
30 | 27 |
|
31 |
| -int us_ticker_inited = 0; |
32 |
| -static double count_clock = 0; |
33 |
| -static uint32_t last_read = 0; |
34 |
| -static uint32_t wrap_arround = 0; |
35 |
| -static uint64_t ticker_us_last64 = 0; |
36 |
| -static uint64_t set_cmp_val64 = 0; |
37 |
| -static uint64_t timestamp64 = 0; |
38 |
| - |
39 |
| -void us_ticker_interrupt(void) { |
40 |
| - us_ticker_irq_handler(); |
41 |
| -} |
| 28 | + /* Power Control for Peripherals */ |
| 29 | + CPGSTBCR5 &= ~(CPG_STBCR5_BIT_MSTP50); /* enable OSTM1 clock */ |
42 | 30 |
|
43 |
| -void us_ticker_init(void) { |
44 | 31 | if (us_ticker_inited) return;
|
45 | 32 | us_ticker_inited = 1;
|
46 | 33 |
|
47 |
| - /* set Counter Clock(us) */ |
48 |
| - if (false == RZ_A1_IsClockMode0()) { |
49 |
| - count_clock = ((double)CM1_RENESAS_RZ_A1_P0_CLK / (double)US_TICKER_CLOCK_US_DEV); |
50 |
| - } else { |
51 |
| - count_clock = ((double)CM0_RENESAS_RZ_A1_P0_CLK / (double)US_TICKER_CLOCK_US_DEV); |
52 |
| - } |
53 |
| - |
54 |
| - /* Power Control for Peripherals */ |
55 |
| - CPGSTBCR5 &= ~(CPG_STBCR5_BIT_MSTP50); /* enable OSTM1 clock */ |
56 |
| - |
57 | 34 | // timer settings
|
58 | 35 | OSTM1TT = 0x01; /* Stop the counter and clears the OSTM1TE bit. */
|
59 | 36 | OSTM1CTL = 0x02; /* Free running timer mode. Interrupt disabled when star counter */
|
60 | 37 |
|
61 |
| - OSTM1TS = 0x1; /* Start the counter and sets the OSTM0TE bit. */ |
| 38 | + OSTM1TS = 0x1; /* Start the counter and sets the OSTM0TE bit. */ |
62 | 39 |
|
63 | 40 | // INTC settings
|
64 |
| - InterruptHandlerRegister(US_TICKER_TIMER_IRQn, (void (*)(uint32_t))us_ticker_interrupt); |
65 |
| - GIC_SetPriority(US_TICKER_TIMER_IRQn, 5); |
66 |
| - GIC_SetConfiguration(US_TICKER_TIMER_IRQn, 3); |
67 |
| - GIC_EnableIRQ(US_TICKER_TIMER_IRQn); |
| 41 | + InterruptHandlerRegister(OSTMI1TINT_IRQn, (void (*)(uint32_t))us_ticker_irq_handler); |
| 42 | + GIC_SetPriority(OSTMI1TINT_IRQn, 5); |
| 43 | + GIC_SetConfiguration(OSTMI1TINT_IRQn, 3); |
68 | 44 | }
|
69 | 45 |
|
70 |
| -static uint64_t ticker_read_counter64(void) { |
71 |
| - uint32_t cnt_val; |
72 |
| - uint64_t cnt_val64; |
73 |
| - |
74 |
| - if (!us_ticker_inited) |
75 |
| - us_ticker_init(); |
| 46 | +void us_ticker_free(void) |
| 47 | +{ |
| 48 | + GIC_DisableIRQ(OSTMI1TINT_IRQn); |
| 49 | + GIC_ClearPendingIRQ(OSTMI1TINT_IRQn); |
76 | 50 |
|
77 |
| - /* read counter */ |
78 |
| - cnt_val = OSTM1CNT; |
79 |
| - if (last_read > cnt_val) { |
80 |
| - wrap_arround++; |
81 |
| - } |
82 |
| - last_read = cnt_val; |
83 |
| - cnt_val64 = ((uint64_t)wrap_arround << 32) + cnt_val; |
84 |
| - |
85 |
| - return cnt_val64; |
| 51 | + /* Power Control for Peripherals */ |
| 52 | + CPGSTBCR5 |= (CPG_STBCR5_BIT_MSTP50); /* disable OSTM1 clock */ |
86 | 53 | }
|
87 | 54 |
|
88 |
| -static void us_ticker_read_last(void) { |
89 |
| - uint64_t cnt_val64; |
90 |
| - |
91 |
| - cnt_val64 = ticker_read_counter64(); |
92 |
| - |
93 |
| - ticker_us_last64 = (cnt_val64 / count_clock); |
| 55 | +uint32_t us_ticker_read() |
| 56 | +{ |
| 57 | + return (uint32_t)(OSTM1CNT >> SHIFT_NUM); |
94 | 58 | }
|
95 | 59 |
|
96 |
| -uint32_t us_ticker_read() { |
97 |
| - core_util_critical_section_enter(); |
98 |
| - |
99 |
| - __vfp_neon_push(); |
100 |
| - us_ticker_read_last(); |
101 |
| - __vfp_neon_pop(); |
102 |
| - |
103 |
| - core_util_critical_section_exit(); |
104 |
| - |
105 |
| - /* clock to us */ |
106 |
| - return (uint32_t)ticker_us_last64; |
| 60 | +void us_ticker_set_interrupt(timestamp_t timestamp) |
| 61 | +{ |
| 62 | + OSTM1CMP = (uint32_t)(timestamp << SHIFT_NUM); |
| 63 | + GIC_EnableIRQ(OSTMI1TINT_IRQn); |
107 | 64 | }
|
108 | 65 |
|
109 |
| -static void us_ticker_calc_compare_match(void) { |
110 |
| - set_cmp_val64 = timestamp64 * count_clock; |
| 66 | +void us_ticker_fire_interrupt(void) |
| 67 | +{ |
| 68 | + GIC_SetPendingIRQ(OSTMI1TINT_IRQn); |
| 69 | + GIC_EnableIRQ(OSTMI1TINT_IRQn); |
111 | 70 | }
|
112 | 71 |
|
113 |
| -void us_ticker_set_interrupt(timestamp_t timestamp) { |
114 |
| - // set match value |
115 |
| - volatile uint32_t set_cmp_val; |
116 |
| - uint64_t count_val_64; |
117 |
| - |
118 |
| - /* calc compare mach timestamp */ |
119 |
| - timestamp64 = (ticker_us_last64 & 0xFFFFFFFF00000000) + timestamp; |
120 |
| - if (timestamp < (ticker_us_last64 & 0x00000000FFFFFFFF)) { |
121 |
| - /* This event is wrap arround */ |
122 |
| - timestamp64 += 0x100000000; |
123 |
| - } |
124 |
| - |
125 |
| - /* calc compare mach timestamp */ |
126 |
| - __vfp_neon_push(); |
127 |
| - us_ticker_calc_compare_match(); |
128 |
| - __vfp_neon_pop(); |
129 |
| - |
130 |
| - set_cmp_val = (uint32_t)(set_cmp_val64 & 0x00000000FFFFFFFF); |
131 |
| - count_val_64 = ticker_read_counter64(); |
132 |
| - if (set_cmp_val64 <= (count_val_64 + 500)) { |
133 |
| - GIC_SetPendingIRQ(US_TICKER_TIMER_IRQn); |
134 |
| - GIC_EnableIRQ(US_TICKER_TIMER_IRQn); |
135 |
| - return; |
136 |
| - } |
137 |
| - OSTM1CMP = set_cmp_val; |
138 |
| - GIC_EnableIRQ(US_TICKER_TIMER_IRQn); |
| 72 | +void us_ticker_disable_interrupt(void) |
| 73 | +{ |
| 74 | + GIC_DisableIRQ(OSTMI1TINT_IRQn); |
139 | 75 | }
|
140 | 76 |
|
141 |
| -void us_ticker_fire_interrupt(void) { |
142 |
| - GIC_SetPendingIRQ(US_TICKER_TIMER_IRQn); |
| 77 | +void us_ticker_clear_interrupt(void) |
| 78 | +{ |
| 79 | + GIC_ClearPendingIRQ(OSTMI1TINT_IRQn); |
143 | 80 | }
|
144 | 81 |
|
145 |
| -void us_ticker_disable_interrupt(void) { |
146 |
| - GIC_DisableIRQ(US_TICKER_TIMER_IRQn); |
| 82 | +const ticker_info_t* us_ticker_get_info() |
| 83 | +{ |
| 84 | + static const ticker_info_t info = { |
| 85 | + (uint32_t)((float)RENESAS_RZ_A1_P0_CLK / (float)(1 << SHIFT_NUM) + 0.5f), |
| 86 | + (32 - SHIFT_NUM) |
| 87 | + }; |
| 88 | + return &info; |
147 | 89 | }
|
148 | 90 |
|
149 |
| -void us_ticker_clear_interrupt(void) { |
150 |
| - GIC_ClearPendingIRQ(US_TICKER_TIMER_IRQn); |
151 |
| -} |
0 commit comments