Skip to content

Commit 1daff13

Browse files
quytranpzzKhiemNguyenT
authored andcommitted
hal: renesas: rx: Add r_bsp_locking file in the HAL Renesas RX
Add r_bsp_locking source and dirs for HAL Renesas RX Signed-off-by: Quy Tran <[email protected]>
1 parent bd9bedf commit 1daff13

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed

drivers/rx/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(srcs
1313
rdp/src/r_bsp/mcu/all/r_bsp_common.c
1414
rdp/src/r_bsp/mcu/all/r_bsp_cpu.c
1515
rdp/src/r_bsp/mcu/all/r_bsp_interrupts.c
16+
rdp/src/r_bsp/mcu/all/r_bsp_locking.c
1617
rdp/src/r_bsp/mcu/all/r_bsp_software_interrupt.c
1718
rdp/src/r_bsp/mcu/all/r_rx_intrinsic_functions.c
1819
rdp/src/r_bsp/mcu/all/mcu_locks.c
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* Copyright (c) 2011 Renesas Electronics Corporation and/or its affiliates
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
/***********************************************************************************************************************
7+
* File Name : r_bsp_locking.c
8+
* Description : This implements a locking mechanism that can be used by all code. The locking is done atomically so
9+
* common resources can be accessed safely.
10+
***********************************************************************************************************************/
11+
/**********************************************************************************************************************
12+
* History : DD.MM.YYYY Version Description
13+
* : 28.02.2019 2.00 Merged processing of all devices.
14+
* Added support for GNUC and ICCRX.
15+
* Fixed coding style.
16+
* : 26.07.2019 2.01 Modified comment of API function to Doxygen style.
17+
* : 10.12.2019 2.02 Modified comment.
18+
* : 31.05.2024 2.03 Fixed coding style.
19+
* : 26.02.2025 2.04 Changed the disclaimer.
20+
***********************************************************************************************************************/
21+
22+
/***********************************************************************************************************************
23+
Includes <System Includes> , "Project Includes"
24+
***********************************************************************************************************************/
25+
/* Platform configuration. */
26+
#include "platform.h"
27+
28+
/***********************************************************************************************************************
29+
Macro definitions
30+
***********************************************************************************************************************/
31+
32+
/***********************************************************************************************************************
33+
Typedef definitions
34+
***********************************************************************************************************************/
35+
36+
/***********************************************************************************************************************
37+
Exported global variables (to be accessed by other files)
38+
***********************************************************************************************************************/
39+
40+
/***********************************************************************************************************************
41+
Private global variables and functions
42+
***********************************************************************************************************************/
43+
44+
/**********************************************************************************************************************
45+
* Function Name: R_BSP_SoftwareLock
46+
******************************************************************************************************************//**
47+
* @brief Attempts to reserve a lock.
48+
* @param[out] plock Pointer to lock structure with lock to try and acquire.
49+
* @retval true Successful, lock was available and acquired.
50+
* @retval false Failure, lock was already acquired and is not available.
51+
* @details This function implements an atomic locking mechanism. Locks can be used in numerous ways. Two common uses
52+
* of locks are to protect critical sections of code and to protect against duplicate resource allocation.
53+
* For protecting critical sections of code the user would require that the code first obtain the critical section's
54+
* lock before executing. An example of protecting against duplicate resource allocation would be if the user had two
55+
* FIT modules that used the same peripheral. For example, the user may have one FIT module that uses the SCI
56+
* peripheral in UART mode and another FIT module that uses the SCI peripheral in I2C mode. To make sure that both
57+
* modules cannot use the same SCI channel, locks can be used.
58+
* Care should be taken when using locks as they do not provide advanced features one might expect from an RTOS
59+
* semaphore or mutex. If used improperly locks can lead to deadlock in the user's system.
60+
* Users can override the default locking mechanisms.
61+
*/
62+
bool R_BSP_SoftwareLock(BSP_CFG_USER_LOCKING_TYPE * const plock)
63+
{
64+
#if BSP_CFG_USER_LOCKING_ENABLED == 0
65+
bool ret = false;
66+
67+
/* Variable used in trying to acquire lock. Using the xchg instruction makes this atomic */
68+
int32_t is_locked = true;
69+
70+
/* This example uses the RX MCU's atomic xchg() instruction. plock->lock is the lock we are trying to reserve.
71+
The way this works is that 'is_locked' gets the value of the plock->lock and plock->lock gets the value of
72+
'is_locked' which we just set to 'true'. Basically this is an atomic 'swap' command. If the lock had not yet been
73+
reserved then its value would be 'false' and after the xchg() instruction finished 'is_locked' would have
74+
'false'. If it had already been reserved then 'is_locked' would have 'true' after the xchg() instruction. Since
75+
plock->lock was already 'true' and we just set it back to 'true' everything is ok. To see if we reserved the lock
76+
we just need to check the value of 'is_locked' after this instruction finishes. */
77+
78+
/* Try to acquire semaphore to obtain lock */
79+
R_BSP_EXCHANGE(&is_locked, &plock->lock);
80+
81+
/* Check to see if semaphore was successfully taken */
82+
if (false == is_locked)
83+
{
84+
/* Lock obtained, return success. */
85+
ret = true;
86+
}
87+
else
88+
{
89+
/* Lock was not obtained, another task already has it. */
90+
R_BSP_NOP();
91+
}
92+
93+
return ret;
94+
#else
95+
/* User is going to handle the locking themselves. */
96+
return BSP_CFG_USER_LOCKING_SW_LOCK_FUNCTION(plock);
97+
#endif /* BSP_CFG_USER_LOCKING_ENABLED == 0 */
98+
} /* End of function R_BSP_SoftwareLock() */
99+
100+
/**********************************************************************************************************************
101+
* Function Name: R_BSP_SoftwareUnlock
102+
******************************************************************************************************************//**
103+
* @brief Releases a lock.
104+
* @param[out] plock Pointer to lock structure with lock to release.
105+
* @retval true Successful, lock was released. Or the lock has been already released.
106+
* @retval false Failure, lock could not be released.
107+
* @details This function releases a lock that was previously acquired using the R_BSP_SoftwareLock() function.
108+
*/
109+
bool R_BSP_SoftwareUnlock(BSP_CFG_USER_LOCKING_TYPE * const plock)
110+
{
111+
#if BSP_CFG_USER_LOCKING_ENABLED == 0
112+
/* Set lock back to unlocked. */
113+
plock->lock = false;
114+
115+
return true;
116+
#else
117+
/* User is going to handle the locking themselves. */
118+
return BSP_CFG_USER_LOCKING_SW_UNLOCK_FUNCTION(plock);
119+
#endif
120+
} /* End of function R_BSP_SoftwareUnlock() */
121+
122+
123+
/**********************************************************************************************************************
124+
* Function Name: R_BSP_HardwareLock
125+
******************************************************************************************************************//**
126+
* @brief Attempts to reserve a hardware peripheral lock.
127+
* @param[in] hw_index Index of lock to acquire from the hardware lock array.
128+
* @retval true Successful, lock was available and acquired.
129+
* @retval false Failure, lock was already acquired and is not available.
130+
* @details This function attempts to acquire the lock for a hardware resource of the MCU. Instead of sending in a
131+
* pointer to a lock as with the R_BSP_SoftwareLock() function, the user sends in an index to an array that holds 1
132+
* lock per MCU hardware resource. This array is shared amongst all FIT modules and user code therefore allowing
133+
* multiple FIT modules (and user code) to use the same locks. The user can see the available hardware resources by
134+
* looking at the mcu_lock_t enum in mcu_locks.h. These enum values are also the index into the hardware lock array.
135+
* The same atomic locking mechanisms from the R_BSP_SoftwareLock() function are used with this function as well.
136+
* @note Each entry in the mcu_lock_t enum in mcu_locks.h will be allocated a lock. On RX MCUs, each lock is required
137+
* to be 4-bytes. If RAM space is an issue then the user can remove the entries from the mcu_lock_t enum they are not
138+
* using. For example, if the user is not using the CRC peripheral then they could delete the BSP_LOCK_CRC entry. The
139+
* user will save 4-bytes per deleted entry.
140+
*/
141+
bool R_BSP_HardwareLock(mcu_lock_t const hw_index)
142+
{
143+
#if BSP_CFG_USER_LOCKING_ENABLED == 0
144+
/* Pass actual lock to software lock function. */
145+
return R_BSP_SoftwareLock(&g_bsp_Locks[hw_index]);
146+
#else
147+
/* User is going to handle the locking themselves. */
148+
return BSP_CFG_USER_LOCKING_HW_LOCK_FUNCTION(hw_index);
149+
#endif
150+
} /* End of function R_BSP_HardwareLock() */
151+
152+
/**********************************************************************************************************************
153+
* Function Name: R_BSP_HardwareUnlock
154+
******************************************************************************************************************//**
155+
* @brief Releases a hardware peripheral lock.
156+
* @param[in] hw_index Index of lock to release from the hardware lock array.
157+
* @retval true Successful, lock was released.
158+
* @retval false Failure, lock could not be released.
159+
* @details This function attempts to release the lock for a hardware resource of the MCU that was previously acquired
160+
* using the R_BSP_HardwareLock() function.
161+
* @note Each entry in the mcu_lock_t enum in mcu_locks.h will be allocated a lock. On RX MCUs, each lock is required
162+
* to be 4-bytes. If RAM space is an issue then the user can remove the entries from the mcu_lock_t enum that they are
163+
* not using. For example, if the user is not using the CRC peripheral then they could delete the BSP_LOCK_CRC entry.
164+
* The user will save 4-bytes per deleted entry.
165+
*/
166+
bool R_BSP_HardwareUnlock(mcu_lock_t const hw_index)
167+
{
168+
#if BSP_CFG_USER_LOCKING_ENABLED == 0
169+
/* Pass actual lock to software unlock function. */
170+
return R_BSP_SoftwareUnlock(&g_bsp_Locks[hw_index]);
171+
#else
172+
/* User is going to handle the locking themselves. */
173+
return BSP_CFG_USER_LOCKING_HW_UNLOCK_FUNCTION(hw_index);
174+
#endif
175+
} /* End of function R_BSP_HardwareUnlock() */
176+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2011 Renesas Electronics Corporation and/or its affiliates
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
/***********************************************************************************************************************
7+
* File Name : r_bsp_locking.h
8+
* Description : This implements a locking mechanism that can be used by all code. The locking is done atomically so
9+
* common resources can be accessed safely.
10+
***********************************************************************************************************************/
11+
/**********************************************************************************************************************
12+
* History : DD.MM.YYYY Version Description
13+
* : 01.12.2015 1.00 First Release
14+
* : 28.02.2019 1.01 Fixed coding style.
15+
* : 26.02.2025 1.02 Changed the disclaimer.
16+
***********************************************************************************************************************/
17+
18+
/***********************************************************************************************************************
19+
Includes <System Includes> , "Project Includes"
20+
***********************************************************************************************************************/
21+
/* Lock types. */
22+
#include "mcu_locks.h"
23+
24+
/***********************************************************************************************************************
25+
Macro definitions
26+
***********************************************************************************************************************/
27+
/* Multiple inclusion prevention macro */
28+
#ifndef LOCKING_H
29+
#define LOCKING_H
30+
31+
/***********************************************************************************************************************
32+
Typedef definitions
33+
***********************************************************************************************************************/
34+
35+
/***********************************************************************************************************************
36+
Exported global variables
37+
***********************************************************************************************************************/
38+
39+
/***********************************************************************************************************************
40+
Exported global functions (to be accessed by other files)
41+
***********************************************************************************************************************/
42+
bool R_BSP_SoftwareLock(BSP_CFG_USER_LOCKING_TYPE * const plock);
43+
bool R_BSP_SoftwareUnlock(BSP_CFG_USER_LOCKING_TYPE * const plock);
44+
bool R_BSP_HardwareLock(mcu_lock_t const hw_index);
45+
bool R_BSP_HardwareUnlock(mcu_lock_t const hw_index);
46+
47+
#if BSP_CFG_USER_LOCKING_ENABLED != 0
48+
/* Is user is using their own lock functions then these are the prototypes. */
49+
bool BSP_CFG_USER_LOCKING_SW_LOCK_FUNCTION(BSP_CFG_USER_LOCKING_TYPE * const plock);
50+
bool BSP_CFG_USER_LOCKING_SW_UNLOCK_FUNCTION(BSP_CFG_USER_LOCKING_TYPE * const plock);
51+
bool BSP_CFG_USER_LOCKING_HW_LOCK_FUNCTION(mcu_lock_t const hw_index);
52+
bool BSP_CFG_USER_LOCKING_HW_UNLOCK_FUNCTION(mcu_lock_t const hw_index);
53+
#endif
54+
55+
#endif /* LOCKING_H */
56+

0 commit comments

Comments
 (0)