Mouse code for the MacroRat
mbed-dev/targets/TARGET_NUVOTON/TARGET_M451/device/StdDriver/m451_sys.c@46:b156ef445742, 2017-06-03 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sat Jun 03 00:22:44 2017 +0000
- Revision:
- 46:b156ef445742
- Parent:
- 18:6a4db94011d3
Final code for internal battlebot competition.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /**************************************************************************//** |
sahilmgandhi | 18:6a4db94011d3 | 2 | * @file sys.c |
sahilmgandhi | 18:6a4db94011d3 | 3 | * @version V3.00 |
sahilmgandhi | 18:6a4db94011d3 | 4 | * $Revision: 12 $ |
sahilmgandhi | 18:6a4db94011d3 | 5 | * $Date: 15/08/11 10:26a $ |
sahilmgandhi | 18:6a4db94011d3 | 6 | * @brief M451 series SYS driver source file |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * @note |
sahilmgandhi | 18:6a4db94011d3 | 9 | * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. |
sahilmgandhi | 18:6a4db94011d3 | 10 | *****************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 11 | |
sahilmgandhi | 18:6a4db94011d3 | 12 | #include "M451Series.h" |
sahilmgandhi | 18:6a4db94011d3 | 13 | |
sahilmgandhi | 18:6a4db94011d3 | 14 | #ifdef __cplusplus |
sahilmgandhi | 18:6a4db94011d3 | 15 | extern "C" |
sahilmgandhi | 18:6a4db94011d3 | 16 | { |
sahilmgandhi | 18:6a4db94011d3 | 17 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 18 | |
sahilmgandhi | 18:6a4db94011d3 | 19 | /** @addtogroup Standard_Driver Standard Driver |
sahilmgandhi | 18:6a4db94011d3 | 20 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 21 | */ |
sahilmgandhi | 18:6a4db94011d3 | 22 | |
sahilmgandhi | 18:6a4db94011d3 | 23 | /** @addtogroup SYS_Driver SYS Driver |
sahilmgandhi | 18:6a4db94011d3 | 24 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 25 | */ |
sahilmgandhi | 18:6a4db94011d3 | 26 | |
sahilmgandhi | 18:6a4db94011d3 | 27 | |
sahilmgandhi | 18:6a4db94011d3 | 28 | /** @addtogroup SYS_EXPORTED_FUNCTIONS SYS Exported Functions |
sahilmgandhi | 18:6a4db94011d3 | 29 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 30 | */ |
sahilmgandhi | 18:6a4db94011d3 | 31 | |
sahilmgandhi | 18:6a4db94011d3 | 32 | /** |
sahilmgandhi | 18:6a4db94011d3 | 33 | * @brief Clear reset source |
sahilmgandhi | 18:6a4db94011d3 | 34 | * @param[in] u32Src is system reset source. Including : |
sahilmgandhi | 18:6a4db94011d3 | 35 | * - \ref SYS_RSTSTS_CPURF_Msk |
sahilmgandhi | 18:6a4db94011d3 | 36 | * - \ref SYS_RSTSTS_SYSRF_Msk |
sahilmgandhi | 18:6a4db94011d3 | 37 | * - \ref SYS_RSTSTS_BODRF_Msk |
sahilmgandhi | 18:6a4db94011d3 | 38 | * - \ref SYS_RSTSTS_LVRF_Msk |
sahilmgandhi | 18:6a4db94011d3 | 39 | * - \ref SYS_RSTSTS_WDTRF_Msk |
sahilmgandhi | 18:6a4db94011d3 | 40 | * - \ref SYS_RSTSTS_PINRF_Msk |
sahilmgandhi | 18:6a4db94011d3 | 41 | * - \ref SYS_RSTSTS_PORF_Msk |
sahilmgandhi | 18:6a4db94011d3 | 42 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 43 | * @details This function clear the selected system reset source. |
sahilmgandhi | 18:6a4db94011d3 | 44 | */ |
sahilmgandhi | 18:6a4db94011d3 | 45 | void SYS_ClearResetSrc(uint32_t u32Src) |
sahilmgandhi | 18:6a4db94011d3 | 46 | { |
sahilmgandhi | 18:6a4db94011d3 | 47 | SYS->RSTSTS |= u32Src; |
sahilmgandhi | 18:6a4db94011d3 | 48 | } |
sahilmgandhi | 18:6a4db94011d3 | 49 | |
sahilmgandhi | 18:6a4db94011d3 | 50 | /** |
sahilmgandhi | 18:6a4db94011d3 | 51 | * @brief Get Brown-out detector output status |
sahilmgandhi | 18:6a4db94011d3 | 52 | * @param None |
sahilmgandhi | 18:6a4db94011d3 | 53 | * @retval 0 System voltage is higher than BOD_VL setting or BOD_EN is 0. |
sahilmgandhi | 18:6a4db94011d3 | 54 | * @retval 1 System voltage is lower than BOD_VL setting. |
sahilmgandhi | 18:6a4db94011d3 | 55 | * @details This function get Brown-out detector output status. |
sahilmgandhi | 18:6a4db94011d3 | 56 | */ |
sahilmgandhi | 18:6a4db94011d3 | 57 | uint32_t SYS_GetBODStatus(void) |
sahilmgandhi | 18:6a4db94011d3 | 58 | { |
sahilmgandhi | 18:6a4db94011d3 | 59 | return ((SYS->BODCTL & SYS_BODCTL_BODOUT_Msk) >> SYS_BODCTL_BODOUT_Pos); |
sahilmgandhi | 18:6a4db94011d3 | 60 | } |
sahilmgandhi | 18:6a4db94011d3 | 61 | |
sahilmgandhi | 18:6a4db94011d3 | 62 | /** |
sahilmgandhi | 18:6a4db94011d3 | 63 | * @brief Get reset status register value |
sahilmgandhi | 18:6a4db94011d3 | 64 | * @param None |
sahilmgandhi | 18:6a4db94011d3 | 65 | * @return Reset source |
sahilmgandhi | 18:6a4db94011d3 | 66 | * @details This function get the system reset status register value. |
sahilmgandhi | 18:6a4db94011d3 | 67 | */ |
sahilmgandhi | 18:6a4db94011d3 | 68 | uint32_t SYS_GetResetSrc(void) |
sahilmgandhi | 18:6a4db94011d3 | 69 | { |
sahilmgandhi | 18:6a4db94011d3 | 70 | return (SYS->RSTSTS); |
sahilmgandhi | 18:6a4db94011d3 | 71 | } |
sahilmgandhi | 18:6a4db94011d3 | 72 | |
sahilmgandhi | 18:6a4db94011d3 | 73 | /** |
sahilmgandhi | 18:6a4db94011d3 | 74 | * @brief Check if register is locked nor not |
sahilmgandhi | 18:6a4db94011d3 | 75 | * @param None |
sahilmgandhi | 18:6a4db94011d3 | 76 | * @retval 0 Write-protection function is disabled. |
sahilmgandhi | 18:6a4db94011d3 | 77 | * 1 Write-protection function is enabled. |
sahilmgandhi | 18:6a4db94011d3 | 78 | * @details This function check register write-protection bit setting. |
sahilmgandhi | 18:6a4db94011d3 | 79 | */ |
sahilmgandhi | 18:6a4db94011d3 | 80 | uint32_t SYS_IsRegLocked(void) |
sahilmgandhi | 18:6a4db94011d3 | 81 | { |
sahilmgandhi | 18:6a4db94011d3 | 82 | return !(SYS->REGLCTL & 0x1); |
sahilmgandhi | 18:6a4db94011d3 | 83 | } |
sahilmgandhi | 18:6a4db94011d3 | 84 | |
sahilmgandhi | 18:6a4db94011d3 | 85 | /** |
sahilmgandhi | 18:6a4db94011d3 | 86 | * @brief Get product ID |
sahilmgandhi | 18:6a4db94011d3 | 87 | * @param None |
sahilmgandhi | 18:6a4db94011d3 | 88 | * @return Product ID |
sahilmgandhi | 18:6a4db94011d3 | 89 | * @details This function get product ID. |
sahilmgandhi | 18:6a4db94011d3 | 90 | */ |
sahilmgandhi | 18:6a4db94011d3 | 91 | uint32_t SYS_ReadPDID(void) |
sahilmgandhi | 18:6a4db94011d3 | 92 | { |
sahilmgandhi | 18:6a4db94011d3 | 93 | return SYS->PDID; |
sahilmgandhi | 18:6a4db94011d3 | 94 | } |
sahilmgandhi | 18:6a4db94011d3 | 95 | |
sahilmgandhi | 18:6a4db94011d3 | 96 | /** |
sahilmgandhi | 18:6a4db94011d3 | 97 | * @brief Reset chip with chip reset |
sahilmgandhi | 18:6a4db94011d3 | 98 | * @param None |
sahilmgandhi | 18:6a4db94011d3 | 99 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 100 | * @details This function reset chip with chip reset. |
sahilmgandhi | 18:6a4db94011d3 | 101 | * The register write-protection function should be disabled before using this function. |
sahilmgandhi | 18:6a4db94011d3 | 102 | */ |
sahilmgandhi | 18:6a4db94011d3 | 103 | void SYS_ResetChip(void) |
sahilmgandhi | 18:6a4db94011d3 | 104 | { |
sahilmgandhi | 18:6a4db94011d3 | 105 | SYS->IPRST0 |= SYS_IPRST0_CHIPRST_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 106 | } |
sahilmgandhi | 18:6a4db94011d3 | 107 | |
sahilmgandhi | 18:6a4db94011d3 | 108 | /** |
sahilmgandhi | 18:6a4db94011d3 | 109 | * @brief Reset chip with CPU reset |
sahilmgandhi | 18:6a4db94011d3 | 110 | * @param None |
sahilmgandhi | 18:6a4db94011d3 | 111 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 112 | * @details This function reset CPU with CPU reset. |
sahilmgandhi | 18:6a4db94011d3 | 113 | * The register write-protection function should be disabled before using this function. |
sahilmgandhi | 18:6a4db94011d3 | 114 | */ |
sahilmgandhi | 18:6a4db94011d3 | 115 | void SYS_ResetCPU(void) |
sahilmgandhi | 18:6a4db94011d3 | 116 | { |
sahilmgandhi | 18:6a4db94011d3 | 117 | SYS->IPRST0 |= SYS_IPRST0_CPURST_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 118 | } |
sahilmgandhi | 18:6a4db94011d3 | 119 | |
sahilmgandhi | 18:6a4db94011d3 | 120 | /** |
sahilmgandhi | 18:6a4db94011d3 | 121 | * @brief Reset selected module |
sahilmgandhi | 18:6a4db94011d3 | 122 | * @param[in] u32ModuleIndex is module index. Including : |
sahilmgandhi | 18:6a4db94011d3 | 123 | * - \ref PDMA_RST |
sahilmgandhi | 18:6a4db94011d3 | 124 | * - \ref EBI_RST |
sahilmgandhi | 18:6a4db94011d3 | 125 | * - \ref USBH_RST |
sahilmgandhi | 18:6a4db94011d3 | 126 | * - \ref CRC_RST |
sahilmgandhi | 18:6a4db94011d3 | 127 | * - \ref GPIO_RST |
sahilmgandhi | 18:6a4db94011d3 | 128 | * - \ref TMR0_RST |
sahilmgandhi | 18:6a4db94011d3 | 129 | * - \ref TMR1_RST |
sahilmgandhi | 18:6a4db94011d3 | 130 | * - \ref TMR2_RST |
sahilmgandhi | 18:6a4db94011d3 | 131 | * - \ref TMR3_RST |
sahilmgandhi | 18:6a4db94011d3 | 132 | * - \ref ACMP01_RST |
sahilmgandhi | 18:6a4db94011d3 | 133 | * - \ref I2C0_RST |
sahilmgandhi | 18:6a4db94011d3 | 134 | * - \ref I2C1_RST |
sahilmgandhi | 18:6a4db94011d3 | 135 | * - \ref SPI0_RST |
sahilmgandhi | 18:6a4db94011d3 | 136 | * - \ref SPI1_RST |
sahilmgandhi | 18:6a4db94011d3 | 137 | * - \ref SPI2_RST |
sahilmgandhi | 18:6a4db94011d3 | 138 | * - \ref UART0_RST |
sahilmgandhi | 18:6a4db94011d3 | 139 | * - \ref UART1_RST |
sahilmgandhi | 18:6a4db94011d3 | 140 | * - \ref UART2_RST |
sahilmgandhi | 18:6a4db94011d3 | 141 | * - \ref UART3_RST |
sahilmgandhi | 18:6a4db94011d3 | 142 | * - \ref CAN0_RST |
sahilmgandhi | 18:6a4db94011d3 | 143 | * - \ref OTG_RST |
sahilmgandhi | 18:6a4db94011d3 | 144 | * - \ref USBD_RST |
sahilmgandhi | 18:6a4db94011d3 | 145 | * - \ref EADC_RST |
sahilmgandhi | 18:6a4db94011d3 | 146 | * - \ref SC0_RST |
sahilmgandhi | 18:6a4db94011d3 | 147 | * - \ref DAC_RST |
sahilmgandhi | 18:6a4db94011d3 | 148 | * - \ref PWM0_RST |
sahilmgandhi | 18:6a4db94011d3 | 149 | * - \ref PWM1_RST |
sahilmgandhi | 18:6a4db94011d3 | 150 | * - \ref TK_RST |
sahilmgandhi | 18:6a4db94011d3 | 151 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 152 | * @details This function reset selected module. |
sahilmgandhi | 18:6a4db94011d3 | 153 | */ |
sahilmgandhi | 18:6a4db94011d3 | 154 | void SYS_ResetModule(uint32_t u32ModuleIndex) |
sahilmgandhi | 18:6a4db94011d3 | 155 | { |
sahilmgandhi | 18:6a4db94011d3 | 156 | /* Generate reset signal to the corresponding module */ |
sahilmgandhi | 18:6a4db94011d3 | 157 | *(volatile uint32_t *)((uint32_t)&SYS->IPRST0 + (u32ModuleIndex >> 24)) |= 1 << (u32ModuleIndex & 0x00ffffff); |
sahilmgandhi | 18:6a4db94011d3 | 158 | |
sahilmgandhi | 18:6a4db94011d3 | 159 | /* Release corresponding module from reset state */ |
sahilmgandhi | 18:6a4db94011d3 | 160 | *(volatile uint32_t *)((uint32_t)&SYS->IPRST0 + (u32ModuleIndex >> 24)) &= ~(1 << (u32ModuleIndex & 0x00ffffff)); |
sahilmgandhi | 18:6a4db94011d3 | 161 | } |
sahilmgandhi | 18:6a4db94011d3 | 162 | |
sahilmgandhi | 18:6a4db94011d3 | 163 | /** |
sahilmgandhi | 18:6a4db94011d3 | 164 | * @brief Enable and configure Brown-out detector function |
sahilmgandhi | 18:6a4db94011d3 | 165 | * @param[in] i32Mode is reset or interrupt mode. Including : |
sahilmgandhi | 18:6a4db94011d3 | 166 | * - \ref SYS_BODCTL_BOD_RST_EN |
sahilmgandhi | 18:6a4db94011d3 | 167 | * - \ref SYS_BODCTL_BOD_INTERRUPT_EN |
sahilmgandhi | 18:6a4db94011d3 | 168 | * @param[in] u32BODLevel is Brown-out voltage level. Including : |
sahilmgandhi | 18:6a4db94011d3 | 169 | * - \ref SYS_BODCTL_BODVL_4_5V |
sahilmgandhi | 18:6a4db94011d3 | 170 | * - \ref SYS_BODCTL_BODVL_3_7V |
sahilmgandhi | 18:6a4db94011d3 | 171 | * - \ref SYS_BODCTL_BODVL_2_7V |
sahilmgandhi | 18:6a4db94011d3 | 172 | * - \ref SYS_BODCTL_BODVL_2_2V |
sahilmgandhi | 18:6a4db94011d3 | 173 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 174 | * @details This function configure Brown-out detector reset or interrupt mode, enable Brown-out function and set Brown-out voltage level. |
sahilmgandhi | 18:6a4db94011d3 | 175 | * The register write-protection function should be disabled before using this function. |
sahilmgandhi | 18:6a4db94011d3 | 176 | */ |
sahilmgandhi | 18:6a4db94011d3 | 177 | void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel) |
sahilmgandhi | 18:6a4db94011d3 | 178 | { |
sahilmgandhi | 18:6a4db94011d3 | 179 | /* Enable Brown-out Detector function */ |
sahilmgandhi | 18:6a4db94011d3 | 180 | SYS->BODCTL |= SYS_BODCTL_BODEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 181 | |
sahilmgandhi | 18:6a4db94011d3 | 182 | /* Enable Brown-out interrupt or reset function */ |
sahilmgandhi | 18:6a4db94011d3 | 183 | SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCTL_BODRSTEN_Msk) | i32Mode; |
sahilmgandhi | 18:6a4db94011d3 | 184 | |
sahilmgandhi | 18:6a4db94011d3 | 185 | /* Select Brown-out Detector threshold voltage */ |
sahilmgandhi | 18:6a4db94011d3 | 186 | SYS->BODCTL = (SYS->BODCTL & ~SYS_BODCTL_BODVL_Msk) | u32BODLevel; |
sahilmgandhi | 18:6a4db94011d3 | 187 | } |
sahilmgandhi | 18:6a4db94011d3 | 188 | |
sahilmgandhi | 18:6a4db94011d3 | 189 | /** |
sahilmgandhi | 18:6a4db94011d3 | 190 | * @brief Disable Brown-out detector function |
sahilmgandhi | 18:6a4db94011d3 | 191 | * @param None |
sahilmgandhi | 18:6a4db94011d3 | 192 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 193 | * @details This function disable Brown-out detector function. |
sahilmgandhi | 18:6a4db94011d3 | 194 | * The register write-protection function should be disabled before using this function. |
sahilmgandhi | 18:6a4db94011d3 | 195 | */ |
sahilmgandhi | 18:6a4db94011d3 | 196 | void SYS_DisableBOD(void) |
sahilmgandhi | 18:6a4db94011d3 | 197 | { |
sahilmgandhi | 18:6a4db94011d3 | 198 | SYS->BODCTL &= ~SYS_BODCTL_BODEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 199 | } |
sahilmgandhi | 18:6a4db94011d3 | 200 | |
sahilmgandhi | 18:6a4db94011d3 | 201 | |
sahilmgandhi | 18:6a4db94011d3 | 202 | |
sahilmgandhi | 18:6a4db94011d3 | 203 | /*@}*/ /* end of group SYS_EXPORTED_FUNCTIONS */ |
sahilmgandhi | 18:6a4db94011d3 | 204 | |
sahilmgandhi | 18:6a4db94011d3 | 205 | /*@}*/ /* end of group SYS_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 206 | |
sahilmgandhi | 18:6a4db94011d3 | 207 | /*@}*/ /* end of group Standard_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 208 | |
sahilmgandhi | 18:6a4db94011d3 | 209 | #ifdef __cplusplus |
sahilmgandhi | 18:6a4db94011d3 | 210 | } |
sahilmgandhi | 18:6a4db94011d3 | 211 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 212 | |
sahilmgandhi | 18:6a4db94011d3 | 213 | /*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ |