Advertisement
Mike057

system_S32K118.c

Jun 8th, 2025
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.48 KB | None | 0 0
  1. /*
  2.  * Copyright 2017-2023 NXP
  3.  *
  4.  * SPDX-License-Identifier: Apache-2.0
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the License); you may
  7.  * not use this file except in compliance with the License.
  8.  * You may obtain a copy of the License at
  9.  *
  10.  * www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14.  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License
  17.  */
  18.  
  19. /**
  20.  * @page misra_violations MISRA-C:2012 violations
  21.  *
  22.  * @section [global]
  23.  * Violates MISRA 2012 Advisory Rule 8.9, An object should be defined at block
  24.  * scope if its identifier only appears in a single function.
  25.  * An object with static storage duration declared at block scope cannot be
  26.  * accessed directly from outside the block.
  27.  *
  28.  * @section [global]
  29.  * Violates MISRA 2012 Advisory Rule 11.4, A conversion should not be performed
  30.  * between a pointer to object and an integer type.
  31.  * The cast is required to initialize a pointer with an unsigned int define,
  32.  * representing an address.
  33.  *
  34.  * @section [global]
  35.  * Violates MISRA 2012 Required Rule 11.6, A cast shall not be performed
  36.  * between pointer to void and an arithmetic type.
  37.  * The cast is required to initialize a pointer with an unsigned int define,
  38.  * representing an address.
  39.  *
  40.  * @section [global]
  41.  * Violates MISRA 2012 Advisory Rule 8.7, External could be made static.
  42.  * Function is defined for usage by application code.
  43.  *
  44.  */
  45.  
  46. #include "device_registers.h"
  47. #include "system_S32K118.h"
  48. #include "stdbool.h"
  49.  
  50. /* ----------------------------------------------------------------------------
  51.    -- Core clock
  52.    ---------------------------------------------------------------------------- */
  53.  
  54. uint32_t SystemCoreClock = DEFAULT_SYSTEM_CLOCK;
  55.  
  56. /*FUNCTION**********************************************************************
  57.  *
  58.  * Function Name : SystemInit
  59.  * Description   : This function disables the watchdog, enables FPU
  60.  * and the power mode protection if the corresponding feature macro
  61.  * is enabled. SystemInit is called from startup_device file.
  62.  *
  63.  * Implements    : SystemInit_Activity
  64.  *END**************************************************************************/
  65. void SystemInit(void)
  66. {
  67. /**************************************************************************/
  68. /* WDOG DISABLE*/
  69. /**************************************************************************/
  70.  
  71. #if (DISABLE_WDOG)
  72.     /* Write of the WDOG unlock key to CNT register, must be done in order to allow any modifications*/
  73.     IP_WDOG->CNT = (uint32_t ) FEATURE_WDOG_UNLOCK_VALUE;
  74.     /* The dummy read is used in order to make sure that the WDOG registers will be configured only
  75.      * after the write of the unlock value was completed. */
  76.     (void)IP_WDOG->CNT;
  77.  
  78.     /* Initial write of WDOG configuration register:
  79.      * enables support for 32-bit refresh/unlock command write words,
  80.      * clock select from LPO, update enable, watchdog disabled */
  81.    IP_WDOG->CS  = (uint32_t ) ( (1UL << WDOG_CS_CMD32EN_SHIFT)                       |
  82.                               (FEATURE_WDOG_CLK_FROM_LPO << WDOG_CS_CLK_SHIFT)     |
  83.                               (0U << WDOG_CS_EN_SHIFT)                             |
  84.                               (1U << WDOG_CS_UPDATE_SHIFT)                         );
  85.  
  86.     /* Configure timeout */
  87.     IP_WDOG->TOVAL = (uint32_t )0xFFFF;
  88. #endif /* (DISABLE_WDOG) */
  89. }
  90.  
  91. /*FUNCTION**********************************************************************
  92.  *
  93.  * Function Name : SystemCoreClockUpdate
  94.  * Description   : This function must be called whenever the core clock is changed
  95.  * during program execution. It evaluates the clock register settings and calculates
  96.  * the current core clock.
  97.  *
  98.  * Implements    : SystemCoreClockUpdate_Activity
  99.  *END**************************************************************************/
  100. void SystemCoreClockUpdate(void)
  101. {
  102.     uint32_t SCGOUTClock = 0U;      /* Variable to store output clock frequency of the SCG module */
  103.     uint32_t regValue;              /* Temporary variable */
  104.     uint32_t divider;
  105.     bool validSystemClockSource = true;
  106.  
  107.     divider = ((IP_SCG->CSR & SCG_CSR_DIVCORE_MASK) >> SCG_CSR_DIVCORE_SHIFT) + 1U;
  108.  
  109.     switch ((IP_SCG->CSR & SCG_CSR_SCS_MASK) >> SCG_CSR_SCS_SHIFT)
  110.     {
  111.         case 0x1:
  112.             /* System OSC */
  113.             SCGOUTClock = CPU_XTAL_CLK_HZ;
  114.             break;
  115.         case 0x2:
  116.             /* Slow IRC */
  117.             regValue = (IP_SCG->SIRCCFG & SCG_SIRCCFG_RANGE_MASK) >> SCG_SIRCCFG_RANGE_SHIFT;
  118.             if (regValue != 0UL)
  119.             {
  120.                 SCGOUTClock = FEATURE_SCG_SIRC_HIGH_RANGE_FREQ;
  121.             }
  122.             else
  123.             {
  124.                 validSystemClockSource = false;
  125.             }
  126.             break;
  127.         case 0x3:
  128.             /* Fast IRC */
  129.             regValue = (IP_SCG->FIRCCFG & SCG_FIRCCFG_RANGE_MASK) >> SCG_FIRCCFG_RANGE_SHIFT;
  130.             if (regValue == 0x0UL)
  131.             {
  132.                 SCGOUTClock = FEATURE_SCG_FIRC_FREQ0;
  133.             }
  134.             else
  135.             {
  136.                 validSystemClockSource = false;
  137.             }
  138.             break;
  139.         default:
  140.             validSystemClockSource = false;
  141.             break;
  142.     }
  143.  
  144.     if (validSystemClockSource == true)
  145.     {
  146.         SystemCoreClock = (SCGOUTClock / divider);
  147.     }
  148. }
  149.  
  150. /*FUNCTION**********************************************************************
  151.  *
  152.  * Function Name : SystemSoftwareReset
  153.  * Description   : This function is used to initiate a system reset
  154.  *
  155.  * Implements    : SystemSoftwareReset_Activity
  156.  *END**************************************************************************/
  157. void SystemSoftwareReset(void)
  158. {
  159.     uint32_t regValue;
  160.  
  161.     /* Read Application Interrupt and Reset Control Register */
  162.     regValue = S32_SCB->AIRCR;
  163.  
  164.     /* Clear register key */
  165.     regValue &= ~( S32_SCB_AIRCR_VECTKEY_MASK);
  166.  
  167.     /* Configure System reset request bit and Register Key */
  168.     regValue |= S32_SCB_AIRCR_VECTKEY(FEATURE_SCB_VECTKEY);
  169.     regValue |= S32_SCB_AIRCR_SYSRESETREQ(0x1u);
  170.  
  171.     /* Write computed register value */
  172.     S32_SCB->AIRCR = regValue;
  173. }
  174.  
  175. /*******************************************************************************
  176.  * EOF
  177.  ******************************************************************************/
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement