Skip to content

Commit 52cbc33

Browse files
committed
Implementation of LPTICKER feature for Renesas mbed boards
Although other venders implement this feature by using RTC, in my H/W(RZ_A1), I cannot use RTC because it does not satisfy the spec of LP Ticker (ms order and low frequency between 8 KHz and 64 KHz). Therefore I implemented this feature by creating 1024 division by MTU2(Multi function Timer pulse Unit 2) in order to satisfy this spec. As a result of investigating, the most unaffected channel among MTU2 placed on GR-PEACH and GR-LYCHEE was channel 3, so I use channel 3 for this feature. - mbed_drv_cfg.h I added a macro of MTU2 channel to this file for commonalizing code for GR-PEACH and GR-LYCHEE, and referenced it's macro at us_ticker.c. - targets.json I added a macro for enabling LP Ticker. - mtu2.c mtu2.h I defined fuction of MTU2's clock supply and stop. Because MTU2 is utilized by pwm driver too, those function were referenced at lp_ticker driver and pwm driver. - lp_ticker.c lp_ticker_init() In order to satisfy the LP Ticker spec, I implemented by creating 1024 division by MTU2. When an interrupt is required, it will be set with ticker_set_interrupt(). - lp_ticker.c lp_ticker_free() This function stops the counting and powerdown the lp_ticker. - lp_ticker.c lp_read() This function returns the timer counter of MTU2. - lp_ticker.c lp_ticker_set_interrupt() In order to satisfy specifications, I implemented lp_ticker_set_interrupt() function. - lp_ticker.c lp_ticker_fire_interrupt() In order to satisfy spec, I implemented lp_ticker_fire_interrupt() function. Also I added GIC_EnableIRQ for allowing the interrupt at end of function. - lp_ticker.c lp_ticker_get_info() To satisfy the spec, I implemented lp_ticker_get_info() function. The value of freq includes rounding off.
1 parent dd6482b commit 52cbc33

File tree

10 files changed

+213
-12
lines changed

10 files changed

+213
-12
lines changed

targets/TARGET_RENESAS/TARGET_RZ_A1XX/TARGET_GR_LYCHEE/PeripheralPins.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,6 @@ const PinMap PinMap_PWM[] = {
250250
{P7_0 , PWM_TIOC2A, 5},
251251
{P9_4 , PWM_TIOC2A, 5}, /* for 208QFP */
252252
{P2_6 , PWM_TIOC2A, 3},
253-
{P6_7 , PWM_TIOC3A, 5},
254-
{P2_5 , PWM_TIOC3A, 3},
255-
{P3_11 , PWM_TIOC3A, 3},
256-
{P6_9 , PWM_TIOC3C, 5},
257-
{P3_12 , PWM_TIOC3C, 3},
258253
{P5_8 , PWM_TIOC4A, 3},
259254
{P2_4 , PWM_TIOC4A, 3},
260255
{P5_10 , PWM_TIOC4C, 3},

targets/TARGET_RENESAS/TARGET_RZ_A1XX/TARGET_GR_LYCHEE/device/os_tick_ostm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,9 @@ uint32_t OS_Tick_GetOverflow (void)
192192
return (IRQ_GetPending(OSTM_IRQn));
193193
}
194194

195+
// Get Cortex-A9 OS Timer interrupt number
196+
IRQn_ID_t mbed_get_a9_tick_irqn(){
197+
return OSTMI0TINT_IRQn;
198+
}
195199
#endif
196200

targets/TARGET_RENESAS/TARGET_RZ_A1XX/TARGET_GR_LYCHEE/mbed_drv_cfg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
#define RENESAS_RZ_A1_P0_CLK CM1_RENESAS_RZ_A1_P0_CLK
3636

37+
#define LP_TICKER_MTU2_CH 3
38+
3739
/* flash (W25Q64JV) */
3840
#define FLASH_BASE (0x18000000UL) /**< Flash Base Address */
3941
#define FLASH_SIZE (0x00800000UL) /**< Available Flash Memory */

targets/TARGET_RENESAS/TARGET_RZ_A1XX/TARGET_RZ_A1H/PeripheralPins.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,17 +276,13 @@ const PinMap PinMap_PWM[] = {
276276
{P4_4 , PWM_TIOC4A, 3},
277277
{P4_6 , PWM_TIOC4C, 3},
278278
{P5_0 , PWM_TIOC0A, 6},
279-
{P5_3 , PWM_TIOC3C, 6},
280279
{P5_5 , PWM_TIOC0C, 6},
281280
{P7_2 , PWM_TIOC0C, 7},
282281
{P7_4 , PWM_TIOC1A, 7},
283282
{P7_6 , PWM_TIOC2A, 7},
284-
{P7_10 , PWM_TIOC3C, 7},
285283
{P7_12 , PWM_TIOC4A, 7},
286284
{P7_14 , PWM_TIOC4C, 7},
287285
{P8_8 , PWM_TIOC1A, 5},
288-
{P8_10 , PWM_TIOC3A, 4},
289-
{P8_12 , PWM_TIOC3C, 4},
290286
{P8_14 , PWM_TIOC2A, 4},
291287
{P11_0 , PWM_TIOC4A, 2},
292288
{P11_2 , PWM_TIOC4C, 2},

targets/TARGET_RENESAS/TARGET_RZ_A1XX/TARGET_RZ_A1H/mbed_drv_cfg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
#define RENESAS_RZ_A1_P0_CLK CM0_RENESAS_RZ_A1_P0_CLK
3636

37+
#define LP_TICKER_MTU2_CH 3
38+
3739
/* flash (MX25L6433FM2I) */
3840
#define FLASH_BASE (0x18000000UL) /**< Flash Base Address */
3941
#define FLASH_SIZE (0x00800000UL) /**< Available Flash Memory */
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#if DEVICE_LPTICKER
17+
#include "lp_ticker_api.h"
18+
#include "mtu2_iobitmask.h"
19+
#include "mbed_drv_cfg.h"
20+
#include "mtu2.h"
21+
22+
#if (LP_TICKER_MTU2_CH == 2)
23+
#define LP_TICKER_TIMER_IRQn (TGI2A_IRQn)
24+
#define MTU2TCR (MTU2TCR_2)
25+
#define MTU2TCNT (MTU2TCNT_2)
26+
#define MTU2TIER (MTU2TIER_2)
27+
#define MTU2TGRA (MTU2TGRA_2)
28+
#define MTU2TSR (MTU2TSR_2)
29+
#define MTU2_TSTR_CST MTU2_TSTR_CST2
30+
#define MTU2_TCR_TPSC (0x07)
31+
#elif (LP_TICKER_MTU2_CH == 3)
32+
#define LP_TICKER_TIMER_IRQn (TGI3A_IRQn)
33+
#define MTU2TCR (MTU2TCR_3)
34+
#define MTU2TCNT (MTU2TCNT_3)
35+
#define MTU2TIER (MTU2TIER_3)
36+
#define MTU2TGRA (MTU2TGRA_3)
37+
#define MTU2TSR (MTU2TSR_3)
38+
#define MTU2_TSTR_CST MTU2_TSTR_CST3
39+
#define MTU2_TCR_TPSC (0x05)
40+
#elif (LP_TICKER_MTU2_CH == 4)
41+
#define LP_TICKER_TIMER_IRQn (TGI4A_IRQn)
42+
#define MTU2TCR (MTU2TCR_4)
43+
#define MTU2TCNT (MTU2TCNT_4)
44+
#define MTU2TIER (MTU2TIER_4)
45+
#define MTU2TGRA (MTU2TGRA_4)
46+
#define MTU2TSR (MTU2TSR_4)
47+
#define MTU2_TSTR_CST MTU2_TSTR_CST4
48+
#define MTU2_TCR_TPSC (0x05)
49+
#else
50+
#error "Invalid number : LP_TICKER_MTU2_CH (2-4)"
51+
#endif
52+
53+
static int lp_ticker_inited = 0;
54+
55+
void lp_ticker_init(void)
56+
{
57+
GIC_DisableIRQ(LP_TICKER_TIMER_IRQn);
58+
GIC_ClearPendingIRQ(LP_TICKER_TIMER_IRQn);
59+
60+
/* Power Control for Peripherals */
61+
mtu2_init();
62+
63+
if (lp_ticker_inited) return;
64+
lp_ticker_inited = 1;
65+
66+
MTU2TCR = MTU2_TCR_TPSC;
67+
MTU2TSTR |= MTU2_TSTR_CST;
68+
MTU2TIER |= MTU2_TIER_n_TGIEA;
69+
70+
// INTC settings
71+
InterruptHandlerRegister(LP_TICKER_TIMER_IRQn, (void (*)(uint32_t))lp_ticker_irq_handler);
72+
GIC_SetPriority(LP_TICKER_TIMER_IRQn, 5);
73+
GIC_SetConfiguration(LP_TICKER_TIMER_IRQn, 3);
74+
}
75+
76+
void lp_ticker_free(void)
77+
{
78+
GIC_DisableIRQ(LP_TICKER_TIMER_IRQn);
79+
GIC_ClearPendingIRQ(LP_TICKER_TIMER_IRQn);
80+
81+
MTU2TIER &= ~MTU2_TIER_n_TGIEA;
82+
mtu2_free();
83+
}
84+
85+
uint32_t lp_ticker_read()
86+
{
87+
return (uint32_t)MTU2TCNT;
88+
}
89+
90+
void lp_ticker_set_interrupt(timestamp_t timestamp)
91+
{
92+
MTU2TSR = (MTU2TSR & 0xFE);
93+
MTU2TGRA = (uint16_t)timestamp;
94+
GIC_EnableIRQ(LP_TICKER_TIMER_IRQn);
95+
}
96+
97+
void lp_ticker_fire_interrupt(void)
98+
{
99+
GIC_SetPendingIRQ(LP_TICKER_TIMER_IRQn);
100+
GIC_EnableIRQ(LP_TICKER_TIMER_IRQn);
101+
}
102+
103+
void lp_ticker_disable_interrupt(void)
104+
{
105+
GIC_DisableIRQ(LP_TICKER_TIMER_IRQn);
106+
}
107+
108+
void lp_ticker_clear_interrupt(void)
109+
{
110+
MTU2TSR = (MTU2TSR & 0xFE);
111+
GIC_ClearPendingIRQ(LP_TICKER_TIMER_IRQn);
112+
}
113+
114+
const ticker_info_t* lp_ticker_get_info()
115+
{
116+
static const ticker_info_t info = {
117+
(uint32_t)((float)RENESAS_RZ_A1_P0_CLK / 1024.0f + 0.5f),
118+
16
119+
};
120+
return &info;
121+
}
122+
123+
#endif // DEVICE_LPTICKER
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "mbed_drv_cfg.h"
18+
19+
#if (defined(FUMC_MTU2_PWM) || defined(DEVICE_LPTICKER))
20+
#include "mtu2.h"
21+
22+
static int mtu2_used_cnt = 0;
23+
24+
void mtu2_init(void)
25+
{
26+
if (mtu2_used_cnt == 0) {
27+
CPGSTBCR3 &= ~(CPG_STBCR3_BIT_MSTP33);
28+
}
29+
if (mtu2_used_cnt < 256) {
30+
mtu2_used_cnt++;
31+
}
32+
}
33+
34+
void mtu2_free(void)
35+
{
36+
if (mtu2_used_cnt > 0) {
37+
mtu2_used_cnt--;
38+
}
39+
if (mtu2_used_cnt == 0) {
40+
CPGSTBCR3 |= (CPG_STBCR3_BIT_MSTP33);
41+
}
42+
}
43+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MTU2_H
18+
#define MTU2_H
19+
20+
#include "device.h"
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
void mtu2_init(void);
27+
void mtu2_free(void);
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif
32+
33+
#endif
34+

targets/TARGET_RENESAS/TARGET_RZ_A1XX/pwmout_api.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "iodefine.h"
2222
#include "gpio_addrdefine.h"
2323
#include "mbed_drv_cfg.h"
24+
#include "mtu2.h"
2425

2526
#define MTU2_PWM_OFFSET 0x20
2627

@@ -208,7 +209,7 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
208209
int tmp_pwm;
209210

210211
// power on
211-
CPGSTBCR3 &= ~(CPG_STBCR3_BIT_MSTP33);
212+
mtu2_init();
212213

213214
obj->pwm = pwm;
214215
tmp_pwm = (int)(obj->pwm - MTU2_PWM_OFFSET);
@@ -274,6 +275,7 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
274275

275276
void pwmout_free(pwmout_t* obj) {
276277
pwmout_write(obj, 0);
278+
mtu2_free();
277279
}
278280

279281
void pwmout_write(pwmout_t* obj, float value) {

targets/targets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,7 +2841,7 @@
28412841
"inherits": ["RZ_A1XX"],
28422842
"supported_form_factors": ["ARDUINO"],
28432843
"extra_labels_add": ["RZA1H", "MBRZA1H", "RZ_A1_EMAC"],
2844-
"device_has_add": ["EMAC", "FLASH"],
2844+
"device_has_add": ["EMAC", "FLASH", "LPTICKER"],
28452845
"release_versions": ["2", "5"],
28462846
"device_name": "R7S72100",
28472847
"bootloader_supported": true
@@ -2856,7 +2856,7 @@
28562856
"inherits": ["RZ_A1XX"],
28572857
"supported_form_factors": ["ARDUINO"],
28582858
"extra_labels_add": ["RZA1UL", "MBRZA1LU"],
2859-
"device_has_add": ["TRNG", "FLASH"],
2859+
"device_has_add": ["TRNG", "FLASH", "LPTICKER"],
28602860
"device_has_remove": ["ETHERNET"],
28612861
"release_versions": ["2", "5"],
28622862
"device_name": "R7S72103",

0 commit comments

Comments
 (0)