S32 SDK
trgmux_hal.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 - 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
18 
36 #include <stddef.h>
37 #include "trgmux_hal.h"
38 
39 /*******************************************************************************
40  * Definitions
41  *******************************************************************************/
42 /* Number of possible outputs (target module) for TRGMUX IP */
43 #define TRGMUX_NUM_TARGET_MODULES ((uint8_t)(sizeof(s_trgmuxTargetModule)/sizeof(trgmux_target_module_t)))
44 /* Number of SEL bitfields in one TRGMUX register */
45 #define TRGMUX_NUM_SEL_BITFIELDS_PER_REG (4U)
46 
47 /*******************************************************************************
48  * Code
49  ******************************************************************************/
50 
51 /*FUNCTION**********************************************************************
52  *
53  * Function Name : TRGMUX_HAL_Init
54  * Description : This function restores the TRGMUX module to reset value.
55  *
56  * Implements : TRGMUX_HAL_Init_Activity
57  *END**************************************************************************/
59 {
60  DEV_ASSERT(base != NULL);
61 
62  /* Constant array storing the value of all TRGMUX output(target module) identifiers */
63  static const trgmux_target_module_t s_trgmuxTargetModule[] =
64  {
118  };
119  uint8_t count;
120  uint8_t idxTrgmuxRegister;
121  uint8_t idxSelBitfield;
122  bool lock = false;
123  status_t status;
124 
125  /* Check if any of the TRGMUX registers is locked */
126  count = 0U;
127  while((count < TRGMUX_NUM_TARGET_MODULES) && (lock != true))
128  {
129  lock = TRGMUX_HAL_GetLockForTargetModule(base, s_trgmuxTargetModule[count]);
130 
131  count++;
132  }
133 
134  /* Abort operations if at least one of the target module is locked. */
135  if(lock == true)
136  {
137  status = STATUS_ERROR;
138  }
139  else
140  {
141  /* Set all SEL bitfields of all TRGMUX registers to default value */
142  for(count = 0U; count < TRGMUX_NUM_TARGET_MODULES; count++)
143  {
144  /* Get index of TRGMUX register to update */
145  idxTrgmuxRegister = (uint8_t)((uint8_t)s_trgmuxTargetModule[count] / TRGMUX_NUM_SEL_BITFIELDS_PER_REG);
146  /* Get index of SEL bitfield inside TRGMUX register to update */
147  idxSelBitfield = (uint8_t)((uint8_t)s_trgmuxTargetModule[count] % TRGMUX_NUM_SEL_BITFIELDS_PER_REG);
148  /* Write the TRGMUX register */
149  base->TRGMUXn[idxTrgmuxRegister] &= ~((uint32_t)TRGMUX_TRGMUXn_SEL0_MASK << (TRGMUX_TRGMUXn_SEL1_SHIFT * idxSelBitfield));
150  }
151  status = STATUS_SUCCESS;
152  }
153 
154  return status;
155 }
156 
157 
158 /*FUNCTION**********************************************************************
159  *
160  * Function Name : TRGMUX_HAL_SetTrigSourceForTargetModule
161  * Description : This function configures a TRGMUX link between a source trigger
162  * and a target module, if the requested target module is not locked.
163  *
164  * Implements : TRGMUX_HAL_SetTrigSourceForTargetModule_Activity
165  *END**************************************************************************/
167  const trgmux_trigger_source_t triggerSource,
168  const trgmux_target_module_t targetModule)
169 {
170  DEV_ASSERT(base != NULL);
171 
172  uint8_t idxTrgmuxRegister, idxSelBitfield;
173  uint32_t tmpReg;
174  /* Get the index of the TRGMUX register that should be updated */
175  idxTrgmuxRegister = (uint8_t)((uint8_t)targetModule / TRGMUX_NUM_SEL_BITFIELDS_PER_REG);
176  /* Get the index of the SEL bitfield inside TRGMUX register that should be updated */
177  idxSelBitfield = (uint8_t)((uint8_t)targetModule % TRGMUX_NUM_SEL_BITFIELDS_PER_REG);
178  /* Read value of entire TRGMUX register in a temp variable */
179  tmpReg = base->TRGMUXn[idxTrgmuxRegister];
180  /* Clear first the SEL bitfield inside the TRGMUX register */
181  tmpReg &= ~((uint32_t)TRGMUX_TRGMUXn_SEL0_MASK << (TRGMUX_TRGMUXn_SEL1_SHIFT * idxSelBitfield));
182  /* Configure the SEL bitfield to the desired value */
183  tmpReg |= ((uint32_t)triggerSource) << ((uint8_t)(TRGMUX_TRGMUXn_SEL1_SHIFT * idxSelBitfield));
184  /* Write back the TRGMUX register */
185  base->TRGMUXn[idxTrgmuxRegister] = tmpReg;
186 }
187 
188 
189 /*FUNCTION**********************************************************************
190  *
191  * Function Name : TRGMUX_HAL_GetTrigSourceForTargetModule
192  * Description : This function returns the TRGMUX source trigger linked to
193  * a selected target module.
194  *
195  * Implements : TRGMUX_HAL_GetTrigSourceForTargetModule_Activity
196  *END**************************************************************************/
198  const trgmux_target_module_t targetModule)
199 {
200  DEV_ASSERT(base != NULL);
201 
202  uint8_t idxTrgmuxRegister, idxSelBitfield;
203  uint32_t trigSource;
204  /* Get the index of the TRGMUX register that should be updated */
205  idxTrgmuxRegister = (uint8_t)((uint8_t)targetModule / TRGMUX_NUM_SEL_BITFIELDS_PER_REG);
206  /* Get the index of the SEL bitfield inside TRGMUX register that should be updated */
207  idxSelBitfield = (uint8_t)((uint8_t)targetModule % TRGMUX_NUM_SEL_BITFIELDS_PER_REG);
208  /* Perform the update operation */
209  trigSource = ((base->TRGMUXn[idxTrgmuxRegister] >> (TRGMUX_TRGMUXn_SEL1_SHIFT * idxSelBitfield)) & TRGMUX_TRGMUXn_SEL0_MASK);
210  return (trgmux_trigger_source_t)(trigSource);
211 }
212 
213 
214 /*FUNCTION**********************************************************************
215  *
216  * Function Name : TRGMUX_HAL_SetLockForTargetModule
217  * Description : This function sets the LK bit of the TRGMUX register corresponding
218  * to the selected target module.
219  *
220  * Implements : TRGMUX_HAL_SetLockForTargetModule_Activity
221  *END**************************************************************************/
223  const trgmux_target_module_t targetModule)
224 {
225  DEV_ASSERT(base != NULL);
226 
227  uint8_t idxTrgmuxRegister;
228  /* Get the index of the TRGMUX register that should be updated */
229  idxTrgmuxRegister = (uint8_t)((uint8_t)targetModule / TRGMUX_NUM_SEL_BITFIELDS_PER_REG);
230  /* Perform the update operation */
231  base->TRGMUXn[idxTrgmuxRegister] |= (((uint32_t)1U) << TRGMUX_TRGMUXn_LK_SHIFT);
232 }
233 
234 /*FUNCTION**********************************************************************
235  *
236  * Function Name : TRGMUX_HAL_GetLockForTargetModule
237  * Description : Get the Lock bit status of the TRGMUX register of a target module.
238  *
239  * Implements : TRGMUX_HAL_GetLockForTargetModule_Activity
240  *END**************************************************************************/
242  const trgmux_target_module_t targetModule)
243 {
244  DEV_ASSERT(base != NULL);
245 
246  uint8_t idxTrgmuxRegister;
247  uint32_t lockVal;
248  bool lock;
249 
250  /* Get the index of the TRGMUX register that should be updated */
251  idxTrgmuxRegister = (uint8_t)((uint8_t)targetModule / TRGMUX_NUM_SEL_BITFIELDS_PER_REG);
252 
253  /* Get the lock bit value */
254  lockVal = ((base->TRGMUXn[idxTrgmuxRegister] & TRGMUX_TRGMUXn_LK_MASK) >> TRGMUX_TRGMUXn_LK_SHIFT);
255 
256  lock = (lockVal == 0U) ? false : true;
257 
258  return lock;
259 }
260 
261 /*******************************************************************************
262  * EOF
263  *******************************************************************************/
#define TRGMUX_TRGMUXn_LK_MASK
Definition: S32K144.h:11234
__IO uint32_t TRGMUXn[TRGMUX_TRGMUXn_COUNT]
Definition: S32K144.h:11169
status_t TRGMUX_HAL_Init(TRGMUX_Type *const base)
Restore the TRGMUX module to reset value.
Definition: trgmux_hal.c:58
#define TRGMUX_NUM_TARGET_MODULES
Definition: trgmux_hal.c:43
#define TRGMUX_TRGMUXn_SEL1_SHIFT
Definition: S32K144.h:11223
bool TRGMUX_HAL_GetLockForTargetModule(const TRGMUX_Type *const base, const trgmux_target_module_t targetModule)
Get the Lock bit status of the TRGMUX register of a target module.
Definition: trgmux_hal.c:241
void TRGMUX_HAL_SetLockForTargetModule(TRGMUX_Type *const base, const trgmux_target_module_t targetModule)
Lock the TRGMUX register of a target module.
Definition: trgmux_hal.c:222
#define TRGMUX_NUM_SEL_BITFIELDS_PER_REG
Definition: trgmux_hal.c:45
#define DEV_ASSERT(x)
Definition: devassert.h:78
trgmux_target_module_t
Describes all possible outputs (target modules) of the TRGMUX IP.
Definition: trgmux_hal.h:125
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:31
#define TRGMUX_TRGMUXn_SEL0_MASK
Definition: S32K144.h:11218
void TRGMUX_HAL_SetTrigSourceForTargetModule(TRGMUX_Type *const base, const trgmux_trigger_source_t triggerSource, const trgmux_target_module_t targetModule)
Configures a source trigger for a target module.
Definition: trgmux_hal.c:166
trgmux_trigger_source_t
Describes all possible inputs (trigger sources) of the TRGMUX IP.
Definition: trgmux_hal.h:62
trgmux_trigger_source_t TRGMUX_HAL_GetTrigSourceForTargetModule(const TRGMUX_Type *const base, const trgmux_target_module_t targetModule)
Get the source trigger configured for a target module.
Definition: trgmux_hal.c:197
#define TRGMUX_TRGMUXn_LK_SHIFT
Definition: S32K144.h:11235