trgmux_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 - 2015, 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 
38 #include <stddef.h>
39 #include "trgmux_driver.h"
40 #include "trgmux_hw_access.h"
41 
42 /*******************************************************************************
43  * Variables
44  ******************************************************************************/
45 
48 
49 /*******************************************************************************
50  * Code
51  ******************************************************************************/
52 
53 /*FUNCTION**********************************************************************
54  *
55  * Function Name : TRGMUX_DRV_Init
56  * Description : This function first resets the source triggers of all TRGMUX target modules
57  * to their default values, then configures the TRGMUX with all the user defined in-out mappings.
58  * If at least one of the target modules is locked, the function will not change any of the
59  * TRGMUX target modules and return an error code.
60  * This example shows how to set up the trgmux_user_config_t parameters and how to call the
61  * TRGMUX_DRV_Init() function with the required parameters:
62  * trgmux_user_config_t trgmuxConfig;
63  * trgmux_inout_mapping_config_t trgmuxInoutMappingConfig[] =
64  * {
65  * {TRGMUX_TRIG_SOURCE_TRGMUX_IN9, TRGMUX_TARGET_MODULE_DMA_CH0, false},
66  * {TRGMUX_TRIG_SOURCE_FTM1_EXT_TRIG, TRGMUX_TARGET_MODULE_TRGMUX_OUT4, true}
67  * };
68  * trgmuxConfig.numInOutMappingConfigs = 2;
69  * trgmuxConfig.inOutMappingConfig = trgmuxInoutMappingConfig;
70  * TRGMUX_DRV_Init(instance, &trgmuxConfig);
71  *
72  * Implements : TRGMUX_DRV_Init_Activity
73  *END**************************************************************************/
74 status_t TRGMUX_DRV_Init(const uint32_t instance,
75  const trgmux_user_config_t * const trgmuxUserConfig)
76 {
78  DEV_ASSERT(trgmuxUserConfig != NULL);
79 
80  status_t status;
81  TRGMUX_Type * base = s_trgmuxBase[instance];
82  uint8_t count;
83 
84  /* Reset source triggers of all TRGMUX target modules to default. */
85  status = TRGMUX_Init(base);
86 
87  if (status == STATUS_SUCCESS)
88  {
89  /* Loop through all in-out mappings in the configuration and apply them in TRGMUX */
90  for (count = 0U; count < trgmuxUserConfig->numInOutMappingConfigs; count++)
91  {
92  TRGMUX_SetTrigSourceForTargetModule(base, trgmuxUserConfig->inOutMappingConfig[count].triggerSource,
93  trgmuxUserConfig->inOutMappingConfig[count].targetModule);
94  }
95 
96  /* Loop through all in-out mappings in the configuration and lock them if required */
97  for (count = 0U; count < trgmuxUserConfig->numInOutMappingConfigs; count++)
98  {
99  if (trgmuxUserConfig->inOutMappingConfig[count].lockTargetModuleReg)
100  {
101  TRGMUX_SetLockForTargetModule(base, trgmuxUserConfig->inOutMappingConfig[count].targetModule);
102  }
103  }
104  }
105 
106  return status;
107 }
108 
109 /*FUNCTION**********************************************************************
110  *
111  * Function Name : TRGMUX_DRV_Deinit
112  * Description : Reset to default values the source triggers corresponding to all target modules,
113  * if none of the target modules is locked.
114  *
115  * Implements : TRGMUX_DRV_Deinit_Activity
116  *END**************************************************************************/
117 status_t TRGMUX_DRV_Deinit(const uint32_t instance)
118 {
119  DEV_ASSERT(instance < TRGMUX_INSTANCE_COUNT);
120 
121  TRGMUX_Type * base = s_trgmuxBase[instance];
122  status_t status;
123 
124  /* Reset source triggers of all TRGMUX target modules to default. */
125  status = TRGMUX_Init(base);
126 
127  return status;
128 }
129 
130 /*FUNCTION**********************************************************************
131  *
132  * Function Name : TRGMUX_DRV_SetTrigSourceForTargetModule
133  * Description : This function configures a TRGMUX link between a source trigger and a target module,
134  * if the requested target module is not locked.
135  *
136  * Implements : TRGMUX_DRV_SetTrigSourceForTargetModule_Activity
137  *END**************************************************************************/
139  const trgmux_trigger_source_t triggerSource,
140  const trgmux_target_module_t targetModule)
141 {
142  DEV_ASSERT(instance < TRGMUX_INSTANCE_COUNT);
143 
144  TRGMUX_Type * base = s_trgmuxBase[instance];
145  status_t status;
146  bool lock;
147 
148  lock = TRGMUX_GetLockForTargetModule(base, targetModule);
149 
150  if (lock == true)
151  {
152  status = STATUS_ERROR;
153  }
154  else
155  {
156  /* Configure link between trigger source and target module. */
157  TRGMUX_SetTrigSourceForTargetModule(base, triggerSource, targetModule);
158  status = STATUS_SUCCESS;
159  }
160 
161  return status;
162 }
163 
164 /*FUNCTION**********************************************************************
165  *
166  * Function Name : TRGMUX_DRV_GetTrigSourceForTargetModule
167  * Description : This function returns the TRGMUX source trigger linked to a selected target module.
168  *
169  * Implements : TRGMUX_DRV_GetTrigSourceForTargetModule_Activity
170  *END**************************************************************************/
172  const trgmux_target_module_t targetModule)
173 {
174  DEV_ASSERT(instance < TRGMUX_INSTANCE_COUNT);
175 
176  const TRGMUX_Type * base = s_trgmuxBase[instance];
177 
178  return TRGMUX_GetTrigSourceForTargetModule(base, targetModule);
179 }
180 
181 /*FUNCTION**********************************************************************
182  *
183  * Function Name : TRGMUX_DRV_SetLockForTargetModule
184  * Description : This function locks the TRGMUX register of a selected target module.
185  *
186  * Implements : TRGMUX_DRV_SetLockForTargetModule_Activity
187  *END**************************************************************************/
188 void TRGMUX_DRV_SetLockForTargetModule(const uint32_t instance,
189  const trgmux_target_module_t targetModule)
190 {
191  DEV_ASSERT(instance < TRGMUX_INSTANCE_COUNT);
192 
193  TRGMUX_Type * base = s_trgmuxBase[instance];
194 
195  TRGMUX_SetLockForTargetModule(base, targetModule);
196 }
197 
198 /*FUNCTION**********************************************************************
199  *
200  * Function Name : TRGMUX_DRV_GetLockForTargetModule
201  * Description : This function gets the value of the LK bit from the TRGMUX register
202  * corresponding to the selected target module.
203  *
204  * Implements : TRGMUX_DRV_GetLockForTargetModule_Activity
205  *END**************************************************************************/
206 bool TRGMUX_DRV_GetLockForTargetModule(const uint32_t instance,
207  const trgmux_target_module_t targetModule)
208 {
209  DEV_ASSERT(instance < TRGMUX_INSTANCE_COUNT);
210 
211  const TRGMUX_Type * base = s_trgmuxBase[instance];
212 
213  return TRGMUX_GetLockForTargetModule(base, targetModule);
214 }
215 
216 /*FUNCTION**********************************************************************
217  *
218  * Function Name : TRGMUX_DRV_GenSWTrigger
219  * Description : This function uses a SIM register in order to generate software
220  * triggers to the target peripherals selected in TRGMUX
221  *
222  * Implements : TRGMUX_DRV_GenSWTrigger_Activity
223  *END**************************************************************************/
224 void TRGMUX_DRV_GenSWTrigger(const uint32_t instance)
225 {
226  DEV_ASSERT(instance < TRGMUX_INSTANCE_COUNT);
227  (void)instance;
228 
229  static SIM_Type * const s_simBase[SIM_INSTANCE_COUNT] = SIM_BASE_PTRS;
230 
231  /* The trigger is generated only when writing from 0 to 1*/
232  s_simBase[0U]->MISCTRL1 = 0U;
233  s_simBase[0U]->MISCTRL1 = SIM_MISCTRL1_SW_TRG(1U);
234 }
235 
236 /*******************************************************************************
237  * EOF
238  ******************************************************************************/
trgmux_trigger_source_t TRGMUX_DRV_GetTrigSourceForTargetModule(const uint32_t instance, const trgmux_target_module_t targetModule)
Get the source trigger configured for a target module.
User configuration structure for the TRGMUX driver.
status_t TRGMUX_DRV_SetTrigSourceForTargetModule(const uint32_t instance, const trgmux_trigger_source_t triggerSource, const trgmux_target_module_t targetModule)
Configure a source trigger for a selected target module.
status_t TRGMUX_DRV_Deinit(const uint32_t instance)
Reset to default values the source triggers corresponding to all target modules, if none of the targe...
static TRGMUX_Type *const s_trgmuxBase[TRGMUX_INSTANCE_COUNT]
Table of base addresses for TRGMUX instances.
Definition: trgmux_driver.c:47
void TRGMUX_DRV_GenSWTrigger(const uint32_t instance)
Generate software triggers.
volatile uint32_t MISCTRL1
Definition: S32K118.h:9618
bool TRGMUX_DRV_GetLockForTargetModule(const uint32_t instance, const trgmux_target_module_t targetModule)
Get the Lock bit status of the TRGMUX register of a target module.
#define DEV_ASSERT(x)
Definition: devassert.h:77
const trgmux_inout_mapping_config_t * inOutMappingConfig
uint8_t numInOutMappingConfigs
trgmux_trigger_source_t triggerSource
Definition: trgmux_driver.h:93
#define SIM_INSTANCE_COUNT
Definition: S32K118.h:9622
#define SIM_BASE_PTRS
Definition: S32K118.h:9633
enum trgmux_trigger_source_e trgmux_trigger_source_t
Enumeration for trigger source module of TRGMUX.
Definition: trgmux_driver.h:71
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:44
status_t TRGMUX_DRV_Init(const uint32_t instance, const trgmux_user_config_t *const trgmuxUserConfig)
Initialize a TRGMUX instance for operation.
Definition: trgmux_driver.c:74
#define SIM_MISCTRL1_SW_TRG(x)
Definition: S32K118.h:9914
#define TRGMUX_BASE_PTRS
Definition: S32K118.h:10070
trgmux_target_module_t targetModule
Definition: trgmux_driver.h:94
#define TRGMUX_INSTANCE_COUNT
Definition: S32K118.h:10059
enum trgmux_target_module_e trgmux_target_module_t
Enumeration for target module of TRGMUX.
Definition: trgmux_driver.h:81
void TRGMUX_DRV_SetLockForTargetModule(const uint32_t instance, const trgmux_target_module_t targetModule)
Locks the TRGMUX register of a target module.