Mouse code for the MacroRat
mbed-dev/targets/TARGET_NUVOTON/TARGET_NUC472/device/StdDriver/nuc472_fmc.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /**************************************************************************//** |
sahilmgandhi | 18:6a4db94011d3 | 2 | * @file fmc.c |
sahilmgandhi | 18:6a4db94011d3 | 3 | * @version V1.10 |
sahilmgandhi | 18:6a4db94011d3 | 4 | * $Revision: 16 $ |
sahilmgandhi | 18:6a4db94011d3 | 5 | * $Date: 14/10/06 11:57a $ |
sahilmgandhi | 18:6a4db94011d3 | 6 | * @brief NUC472/NUC442 FMC driver source file |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * @note |
sahilmgandhi | 18:6a4db94011d3 | 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. |
sahilmgandhi | 18:6a4db94011d3 | 10 | *****************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 11 | |
sahilmgandhi | 18:6a4db94011d3 | 12 | //* Includes ------------------------------------------------------------------*/ |
sahilmgandhi | 18:6a4db94011d3 | 13 | #include <stdio.h> |
sahilmgandhi | 18:6a4db94011d3 | 14 | #include "NUC472_442.h" |
sahilmgandhi | 18:6a4db94011d3 | 15 | |
sahilmgandhi | 18:6a4db94011d3 | 16 | |
sahilmgandhi | 18:6a4db94011d3 | 17 | /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver |
sahilmgandhi | 18:6a4db94011d3 | 18 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 19 | */ |
sahilmgandhi | 18:6a4db94011d3 | 20 | |
sahilmgandhi | 18:6a4db94011d3 | 21 | /** @addtogroup NUC472_442_FMC_Driver FMC Driver |
sahilmgandhi | 18:6a4db94011d3 | 22 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 23 | */ |
sahilmgandhi | 18:6a4db94011d3 | 24 | |
sahilmgandhi | 18:6a4db94011d3 | 25 | |
sahilmgandhi | 18:6a4db94011d3 | 26 | /** @addtogroup NUC472_442_FMC_EXPORTED_FUNCTIONS FMC Exported Functions |
sahilmgandhi | 18:6a4db94011d3 | 27 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 28 | */ |
sahilmgandhi | 18:6a4db94011d3 | 29 | |
sahilmgandhi | 18:6a4db94011d3 | 30 | |
sahilmgandhi | 18:6a4db94011d3 | 31 | /** |
sahilmgandhi | 18:6a4db94011d3 | 32 | * @brief Set boot from APROM or LDROM of next software reset. |
sahilmgandhi | 18:6a4db94011d3 | 33 | * @param[in] i32BootSrc Next software boot selection. |
sahilmgandhi | 18:6a4db94011d3 | 34 | * - \ref IS_BOOT_FROM_LDROM |
sahilmgandhi | 18:6a4db94011d3 | 35 | * - \ref IS_BOOT_FROM_APROM |
sahilmgandhi | 18:6a4db94011d3 | 36 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 37 | */ |
sahilmgandhi | 18:6a4db94011d3 | 38 | void FMC_SetBootSource (int32_t i32BootSrc) |
sahilmgandhi | 18:6a4db94011d3 | 39 | { |
sahilmgandhi | 18:6a4db94011d3 | 40 | if (i32BootSrc == 1) |
sahilmgandhi | 18:6a4db94011d3 | 41 | FMC->ISPCTL |= FMC_ISPCTL_BS_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 42 | else |
sahilmgandhi | 18:6a4db94011d3 | 43 | FMC->ISPCTL &= ~FMC_ISPCTL_BS_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 44 | } |
sahilmgandhi | 18:6a4db94011d3 | 45 | |
sahilmgandhi | 18:6a4db94011d3 | 46 | |
sahilmgandhi | 18:6a4db94011d3 | 47 | /** |
sahilmgandhi | 18:6a4db94011d3 | 48 | * @brief Disable FMC ISP function. |
sahilmgandhi | 18:6a4db94011d3 | 49 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 50 | */ |
sahilmgandhi | 18:6a4db94011d3 | 51 | void FMC_Close(void) |
sahilmgandhi | 18:6a4db94011d3 | 52 | { |
sahilmgandhi | 18:6a4db94011d3 | 53 | FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 54 | } |
sahilmgandhi | 18:6a4db94011d3 | 55 | |
sahilmgandhi | 18:6a4db94011d3 | 56 | |
sahilmgandhi | 18:6a4db94011d3 | 57 | /** |
sahilmgandhi | 18:6a4db94011d3 | 58 | * @brief Disable ISP erase/program APROM function. |
sahilmgandhi | 18:6a4db94011d3 | 59 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 60 | */ |
sahilmgandhi | 18:6a4db94011d3 | 61 | void FMC_DisableAPUpdate(void) |
sahilmgandhi | 18:6a4db94011d3 | 62 | { |
sahilmgandhi | 18:6a4db94011d3 | 63 | FMC->ISPCTL &= ~FMC_ISPCTL_APUEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 64 | } |
sahilmgandhi | 18:6a4db94011d3 | 65 | |
sahilmgandhi | 18:6a4db94011d3 | 66 | |
sahilmgandhi | 18:6a4db94011d3 | 67 | /** |
sahilmgandhi | 18:6a4db94011d3 | 68 | * @brief Disable ISP erase/program User Configuration function. |
sahilmgandhi | 18:6a4db94011d3 | 69 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 70 | */ |
sahilmgandhi | 18:6a4db94011d3 | 71 | void FMC_DisableConfigUpdate(void) |
sahilmgandhi | 18:6a4db94011d3 | 72 | { |
sahilmgandhi | 18:6a4db94011d3 | 73 | FMC->ISPCTL &= ~FMC_ISPCTL_CFGUEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 74 | } |
sahilmgandhi | 18:6a4db94011d3 | 75 | |
sahilmgandhi | 18:6a4db94011d3 | 76 | |
sahilmgandhi | 18:6a4db94011d3 | 77 | /** |
sahilmgandhi | 18:6a4db94011d3 | 78 | * @brief Disable ISP erase/program LDROM function. |
sahilmgandhi | 18:6a4db94011d3 | 79 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 80 | */ |
sahilmgandhi | 18:6a4db94011d3 | 81 | void FMC_DisableLDUpdate(void) |
sahilmgandhi | 18:6a4db94011d3 | 82 | { |
sahilmgandhi | 18:6a4db94011d3 | 83 | FMC->ISPCTL &= ~FMC_ISPCTL_LDUEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 84 | } |
sahilmgandhi | 18:6a4db94011d3 | 85 | |
sahilmgandhi | 18:6a4db94011d3 | 86 | |
sahilmgandhi | 18:6a4db94011d3 | 87 | /** |
sahilmgandhi | 18:6a4db94011d3 | 88 | * @brief Enable ISP erase/program APROM function. |
sahilmgandhi | 18:6a4db94011d3 | 89 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 90 | */ |
sahilmgandhi | 18:6a4db94011d3 | 91 | void FMC_EnableAPUpdate(void) |
sahilmgandhi | 18:6a4db94011d3 | 92 | { |
sahilmgandhi | 18:6a4db94011d3 | 93 | FMC->ISPCTL |= FMC_ISPCTL_APUEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 94 | } |
sahilmgandhi | 18:6a4db94011d3 | 95 | |
sahilmgandhi | 18:6a4db94011d3 | 96 | |
sahilmgandhi | 18:6a4db94011d3 | 97 | /** |
sahilmgandhi | 18:6a4db94011d3 | 98 | * @brief Enable ISP erase/program User Configuration function. |
sahilmgandhi | 18:6a4db94011d3 | 99 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 100 | */ |
sahilmgandhi | 18:6a4db94011d3 | 101 | void FMC_EnableConfigUpdate(void) |
sahilmgandhi | 18:6a4db94011d3 | 102 | { |
sahilmgandhi | 18:6a4db94011d3 | 103 | FMC->ISPCTL |= FMC_ISPCTL_CFGUEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 104 | } |
sahilmgandhi | 18:6a4db94011d3 | 105 | |
sahilmgandhi | 18:6a4db94011d3 | 106 | |
sahilmgandhi | 18:6a4db94011d3 | 107 | /** |
sahilmgandhi | 18:6a4db94011d3 | 108 | * @brief Enable ISP erase/program LDROM function. |
sahilmgandhi | 18:6a4db94011d3 | 109 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 110 | */ |
sahilmgandhi | 18:6a4db94011d3 | 111 | void FMC_EnableLDUpdate(void) |
sahilmgandhi | 18:6a4db94011d3 | 112 | { |
sahilmgandhi | 18:6a4db94011d3 | 113 | FMC->ISPCTL |= FMC_ISPCTL_LDUEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 114 | } |
sahilmgandhi | 18:6a4db94011d3 | 115 | |
sahilmgandhi | 18:6a4db94011d3 | 116 | |
sahilmgandhi | 18:6a4db94011d3 | 117 | /** |
sahilmgandhi | 18:6a4db94011d3 | 118 | * @brief Erase a page. The page size is 2048 bytes. |
sahilmgandhi | 18:6a4db94011d3 | 119 | * @param[in] u32PageAddr Flash page address. Must be a 2048-byte aligned address. |
sahilmgandhi | 18:6a4db94011d3 | 120 | * @return Success or not. |
sahilmgandhi | 18:6a4db94011d3 | 121 | * @retval 0 Success |
sahilmgandhi | 18:6a4db94011d3 | 122 | * @retval -1 Erase failed |
sahilmgandhi | 18:6a4db94011d3 | 123 | */ |
sahilmgandhi | 18:6a4db94011d3 | 124 | int32_t FMC_Erase(uint32_t u32PageAddr) |
sahilmgandhi | 18:6a4db94011d3 | 125 | { |
sahilmgandhi | 18:6a4db94011d3 | 126 | FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE; |
sahilmgandhi | 18:6a4db94011d3 | 127 | FMC->ISPADDR = u32PageAddr; |
sahilmgandhi | 18:6a4db94011d3 | 128 | FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 129 | |
sahilmgandhi | 18:6a4db94011d3 | 130 | while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; |
sahilmgandhi | 18:6a4db94011d3 | 131 | |
sahilmgandhi | 18:6a4db94011d3 | 132 | if (FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk) { |
sahilmgandhi | 18:6a4db94011d3 | 133 | FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 134 | return -1; |
sahilmgandhi | 18:6a4db94011d3 | 135 | } |
sahilmgandhi | 18:6a4db94011d3 | 136 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 137 | } |
sahilmgandhi | 18:6a4db94011d3 | 138 | |
sahilmgandhi | 18:6a4db94011d3 | 139 | |
sahilmgandhi | 18:6a4db94011d3 | 140 | /** |
sahilmgandhi | 18:6a4db94011d3 | 141 | * @brief Get the current boot source. |
sahilmgandhi | 18:6a4db94011d3 | 142 | * @return The current boot source. |
sahilmgandhi | 18:6a4db94011d3 | 143 | * @retval 0 Is boot from APROM. |
sahilmgandhi | 18:6a4db94011d3 | 144 | * @retval 1 Is boot from LDROM. |
sahilmgandhi | 18:6a4db94011d3 | 145 | */ |
sahilmgandhi | 18:6a4db94011d3 | 146 | int32_t FMC_GetBootSource (void) |
sahilmgandhi | 18:6a4db94011d3 | 147 | { |
sahilmgandhi | 18:6a4db94011d3 | 148 | if (FMC->ISPCTL & FMC_ISPCTL_BS_Msk) |
sahilmgandhi | 18:6a4db94011d3 | 149 | return 1; |
sahilmgandhi | 18:6a4db94011d3 | 150 | else |
sahilmgandhi | 18:6a4db94011d3 | 151 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 152 | } |
sahilmgandhi | 18:6a4db94011d3 | 153 | |
sahilmgandhi | 18:6a4db94011d3 | 154 | |
sahilmgandhi | 18:6a4db94011d3 | 155 | /** |
sahilmgandhi | 18:6a4db94011d3 | 156 | * @brief Enable FMC ISP function |
sahilmgandhi | 18:6a4db94011d3 | 157 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 158 | */ |
sahilmgandhi | 18:6a4db94011d3 | 159 | void FMC_Open(void) |
sahilmgandhi | 18:6a4db94011d3 | 160 | { |
sahilmgandhi | 18:6a4db94011d3 | 161 | FMC->ISPCTL |= FMC_ISPCTL_ISPEN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 162 | } |
sahilmgandhi | 18:6a4db94011d3 | 163 | |
sahilmgandhi | 18:6a4db94011d3 | 164 | |
sahilmgandhi | 18:6a4db94011d3 | 165 | /** |
sahilmgandhi | 18:6a4db94011d3 | 166 | * @brief Execute ISP command to read a word from flash. |
sahilmgandhi | 18:6a4db94011d3 | 167 | * @param[in] u32Addr Address of the flash location to be read. |
sahilmgandhi | 18:6a4db94011d3 | 168 | * It must be a word aligned address. |
sahilmgandhi | 18:6a4db94011d3 | 169 | * @return The word data read from specified flash address. |
sahilmgandhi | 18:6a4db94011d3 | 170 | */ |
sahilmgandhi | 18:6a4db94011d3 | 171 | uint32_t FMC_Read(uint32_t u32Addr) |
sahilmgandhi | 18:6a4db94011d3 | 172 | { |
sahilmgandhi | 18:6a4db94011d3 | 173 | FMC->ISPCMD = FMC_ISPCMD_READ; |
sahilmgandhi | 18:6a4db94011d3 | 174 | FMC->ISPADDR = u32Addr; |
sahilmgandhi | 18:6a4db94011d3 | 175 | FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 176 | |
sahilmgandhi | 18:6a4db94011d3 | 177 | while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; |
sahilmgandhi | 18:6a4db94011d3 | 178 | |
sahilmgandhi | 18:6a4db94011d3 | 179 | return FMC->ISPDAT; |
sahilmgandhi | 18:6a4db94011d3 | 180 | } |
sahilmgandhi | 18:6a4db94011d3 | 181 | |
sahilmgandhi | 18:6a4db94011d3 | 182 | |
sahilmgandhi | 18:6a4db94011d3 | 183 | /** |
sahilmgandhi | 18:6a4db94011d3 | 184 | * @brief Execute ISP 64-bits read command to read two words from flash. |
sahilmgandhi | 18:6a4db94011d3 | 185 | * @param[in] u32Addr Flash word address. Must be a double word aligned address. |
sahilmgandhi | 18:6a4db94011d3 | 186 | * @param[out] u32Data0 The first word read from flash. |
sahilmgandhi | 18:6a4db94011d3 | 187 | * @param[out] u32Data1 The second word read from flash. |
sahilmgandhi | 18:6a4db94011d3 | 188 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 189 | */ |
sahilmgandhi | 18:6a4db94011d3 | 190 | void FMC_Read_64(uint32_t u32Addr, uint32_t *u32Data0, uint32_t *u32Data1) |
sahilmgandhi | 18:6a4db94011d3 | 191 | { |
sahilmgandhi | 18:6a4db94011d3 | 192 | FMC->ISPCMD = FMC_ISPCMD_READ_64; |
sahilmgandhi | 18:6a4db94011d3 | 193 | FMC->ISPADDR = u32Addr; |
sahilmgandhi | 18:6a4db94011d3 | 194 | FMC->ISPDAT = 0x0; |
sahilmgandhi | 18:6a4db94011d3 | 195 | FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 196 | |
sahilmgandhi | 18:6a4db94011d3 | 197 | while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; |
sahilmgandhi | 18:6a4db94011d3 | 198 | |
sahilmgandhi | 18:6a4db94011d3 | 199 | *u32Data0 = FMC->MPDAT0; |
sahilmgandhi | 18:6a4db94011d3 | 200 | *u32Data1 = FMC->MPDAT1; |
sahilmgandhi | 18:6a4db94011d3 | 201 | } |
sahilmgandhi | 18:6a4db94011d3 | 202 | |
sahilmgandhi | 18:6a4db94011d3 | 203 | |
sahilmgandhi | 18:6a4db94011d3 | 204 | /** |
sahilmgandhi | 18:6a4db94011d3 | 205 | * @brief Read company ID. |
sahilmgandhi | 18:6a4db94011d3 | 206 | * @return The company ID. |
sahilmgandhi | 18:6a4db94011d3 | 207 | */ |
sahilmgandhi | 18:6a4db94011d3 | 208 | uint32_t FMC_ReadCID(void) |
sahilmgandhi | 18:6a4db94011d3 | 209 | { |
sahilmgandhi | 18:6a4db94011d3 | 210 | FMC->ISPCMD = FMC_ISPCMD_READ_CID; |
sahilmgandhi | 18:6a4db94011d3 | 211 | FMC->ISPADDR = 0x0; |
sahilmgandhi | 18:6a4db94011d3 | 212 | FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 213 | while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; |
sahilmgandhi | 18:6a4db94011d3 | 214 | return FMC->ISPDAT; |
sahilmgandhi | 18:6a4db94011d3 | 215 | } |
sahilmgandhi | 18:6a4db94011d3 | 216 | |
sahilmgandhi | 18:6a4db94011d3 | 217 | |
sahilmgandhi | 18:6a4db94011d3 | 218 | /** |
sahilmgandhi | 18:6a4db94011d3 | 219 | * @brief Read device ID. |
sahilmgandhi | 18:6a4db94011d3 | 220 | * @return The device ID. |
sahilmgandhi | 18:6a4db94011d3 | 221 | */ |
sahilmgandhi | 18:6a4db94011d3 | 222 | uint32_t FMC_ReadDID(void) |
sahilmgandhi | 18:6a4db94011d3 | 223 | { |
sahilmgandhi | 18:6a4db94011d3 | 224 | FMC->ISPCMD = FMC_ISPCMD_READ_DID; |
sahilmgandhi | 18:6a4db94011d3 | 225 | FMC->ISPADDR = 0; |
sahilmgandhi | 18:6a4db94011d3 | 226 | FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 227 | while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; |
sahilmgandhi | 18:6a4db94011d3 | 228 | return FMC->ISPDAT; |
sahilmgandhi | 18:6a4db94011d3 | 229 | } |
sahilmgandhi | 18:6a4db94011d3 | 230 | |
sahilmgandhi | 18:6a4db94011d3 | 231 | |
sahilmgandhi | 18:6a4db94011d3 | 232 | /** |
sahilmgandhi | 18:6a4db94011d3 | 233 | * @brief Read product ID. |
sahilmgandhi | 18:6a4db94011d3 | 234 | * @return The product ID. |
sahilmgandhi | 18:6a4db94011d3 | 235 | */ |
sahilmgandhi | 18:6a4db94011d3 | 236 | uint32_t FMC_ReadPID(void) |
sahilmgandhi | 18:6a4db94011d3 | 237 | { |
sahilmgandhi | 18:6a4db94011d3 | 238 | FMC->ISPCMD = FMC_ISPCMD_READ_DID; |
sahilmgandhi | 18:6a4db94011d3 | 239 | FMC->ISPADDR = 0x04; |
sahilmgandhi | 18:6a4db94011d3 | 240 | FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 241 | while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; |
sahilmgandhi | 18:6a4db94011d3 | 242 | return FMC->ISPDAT; |
sahilmgandhi | 18:6a4db94011d3 | 243 | } |
sahilmgandhi | 18:6a4db94011d3 | 244 | |
sahilmgandhi | 18:6a4db94011d3 | 245 | |
sahilmgandhi | 18:6a4db94011d3 | 246 | /** |
sahilmgandhi | 18:6a4db94011d3 | 247 | * @brief This function reads one of the four UCID. |
sahilmgandhi | 18:6a4db94011d3 | 248 | * @param[in] u32Index Index of the UCID to read. u32Index must be 0, 1, 2, or 3. |
sahilmgandhi | 18:6a4db94011d3 | 249 | * @return The UCID. |
sahilmgandhi | 18:6a4db94011d3 | 250 | */ |
sahilmgandhi | 18:6a4db94011d3 | 251 | uint32_t FMC_ReadUCID(uint32_t u32Index) |
sahilmgandhi | 18:6a4db94011d3 | 252 | { |
sahilmgandhi | 18:6a4db94011d3 | 253 | FMC->ISPCMD = FMC_ISPCMD_READ_UID; |
sahilmgandhi | 18:6a4db94011d3 | 254 | FMC->ISPADDR = (0x04 * u32Index) + 0x10; |
sahilmgandhi | 18:6a4db94011d3 | 255 | FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 256 | |
sahilmgandhi | 18:6a4db94011d3 | 257 | while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; |
sahilmgandhi | 18:6a4db94011d3 | 258 | |
sahilmgandhi | 18:6a4db94011d3 | 259 | return FMC->ISPDAT; |
sahilmgandhi | 18:6a4db94011d3 | 260 | } |
sahilmgandhi | 18:6a4db94011d3 | 261 | |
sahilmgandhi | 18:6a4db94011d3 | 262 | |
sahilmgandhi | 18:6a4db94011d3 | 263 | /** |
sahilmgandhi | 18:6a4db94011d3 | 264 | * @brief This function reads one of the three UID. |
sahilmgandhi | 18:6a4db94011d3 | 265 | * @param[in] u32Index Index of the UID to read. u32Index must be 0, 1, or 2. |
sahilmgandhi | 18:6a4db94011d3 | 266 | * @return The UID. |
sahilmgandhi | 18:6a4db94011d3 | 267 | */ |
sahilmgandhi | 18:6a4db94011d3 | 268 | uint32_t FMC_ReadUID(uint32_t u32Index) |
sahilmgandhi | 18:6a4db94011d3 | 269 | { |
sahilmgandhi | 18:6a4db94011d3 | 270 | FMC->ISPCMD = FMC_ISPCMD_READ_UID; |
sahilmgandhi | 18:6a4db94011d3 | 271 | FMC->ISPADDR = 0x04 * u32Index; |
sahilmgandhi | 18:6a4db94011d3 | 272 | FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 273 | |
sahilmgandhi | 18:6a4db94011d3 | 274 | while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; |
sahilmgandhi | 18:6a4db94011d3 | 275 | |
sahilmgandhi | 18:6a4db94011d3 | 276 | return FMC->ISPDAT; |
sahilmgandhi | 18:6a4db94011d3 | 277 | } |
sahilmgandhi | 18:6a4db94011d3 | 278 | |
sahilmgandhi | 18:6a4db94011d3 | 279 | |
sahilmgandhi | 18:6a4db94011d3 | 280 | /** |
sahilmgandhi | 18:6a4db94011d3 | 281 | * @brief Get the base address of Data Flash if enabled. |
sahilmgandhi | 18:6a4db94011d3 | 282 | * @return Base address of Data Flash |
sahilmgandhi | 18:6a4db94011d3 | 283 | */ |
sahilmgandhi | 18:6a4db94011d3 | 284 | uint32_t FMC_ReadDataFlashBaseAddr(void) |
sahilmgandhi | 18:6a4db94011d3 | 285 | { |
sahilmgandhi | 18:6a4db94011d3 | 286 | return FMC->DFBA; |
sahilmgandhi | 18:6a4db94011d3 | 287 | } |
sahilmgandhi | 18:6a4db94011d3 | 288 | |
sahilmgandhi | 18:6a4db94011d3 | 289 | |
sahilmgandhi | 18:6a4db94011d3 | 290 | /** |
sahilmgandhi | 18:6a4db94011d3 | 291 | * @brief This function will force re-map assigned flash page to CPU address 0x0. |
sahilmgandhi | 18:6a4db94011d3 | 292 | * @param[in] u32PageAddr Address of the page to be mapped to CPU address 0x0. |
sahilmgandhi | 18:6a4db94011d3 | 293 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 294 | */ |
sahilmgandhi | 18:6a4db94011d3 | 295 | void FMC_SetVectorPageAddr(uint32_t u32PageAddr) |
sahilmgandhi | 18:6a4db94011d3 | 296 | { |
sahilmgandhi | 18:6a4db94011d3 | 297 | FMC->ISPCMD = FMC_ISPCMD_VECMAP; |
sahilmgandhi | 18:6a4db94011d3 | 298 | FMC->ISPADDR = u32PageAddr; |
sahilmgandhi | 18:6a4db94011d3 | 299 | FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 300 | while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; |
sahilmgandhi | 18:6a4db94011d3 | 301 | } |
sahilmgandhi | 18:6a4db94011d3 | 302 | |
sahilmgandhi | 18:6a4db94011d3 | 303 | |
sahilmgandhi | 18:6a4db94011d3 | 304 | /** |
sahilmgandhi | 18:6a4db94011d3 | 305 | * @brief Execute ISP command to program a word to flash. |
sahilmgandhi | 18:6a4db94011d3 | 306 | * @param[in] u32Addr Address of the flash location to be programmed. |
sahilmgandhi | 18:6a4db94011d3 | 307 | * It must be a word aligned address. |
sahilmgandhi | 18:6a4db94011d3 | 308 | * @param[out] u32Data The word data to be programmed. |
sahilmgandhi | 18:6a4db94011d3 | 309 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 310 | */ |
sahilmgandhi | 18:6a4db94011d3 | 311 | void FMC_Write(uint32_t u32Addr, uint32_t u32Data) |
sahilmgandhi | 18:6a4db94011d3 | 312 | { |
sahilmgandhi | 18:6a4db94011d3 | 313 | FMC->ISPCMD = FMC_ISPCMD_WRITE; |
sahilmgandhi | 18:6a4db94011d3 | 314 | FMC->ISPADDR = u32Addr; |
sahilmgandhi | 18:6a4db94011d3 | 315 | FMC->ISPDAT = u32Data; |
sahilmgandhi | 18:6a4db94011d3 | 316 | FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 317 | while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; |
sahilmgandhi | 18:6a4db94011d3 | 318 | } |
sahilmgandhi | 18:6a4db94011d3 | 319 | |
sahilmgandhi | 18:6a4db94011d3 | 320 | |
sahilmgandhi | 18:6a4db94011d3 | 321 | /** |
sahilmgandhi | 18:6a4db94011d3 | 322 | * @brief Execute ISP 64-bits write command to program two words to flash. |
sahilmgandhi | 18:6a4db94011d3 | 323 | * @param[in] u32Addr Destination address. It must be double word aligned. |
sahilmgandhi | 18:6a4db94011d3 | 324 | * @param[in] u32Data0 First word data to be written. |
sahilmgandhi | 18:6a4db94011d3 | 325 | * @param[in] u32Data1 Second word data to be written. |
sahilmgandhi | 18:6a4db94011d3 | 326 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 327 | */ |
sahilmgandhi | 18:6a4db94011d3 | 328 | void FMC_Write_64(uint32_t u32Addr, uint32_t u32Data0, uint32_t u32Data1) |
sahilmgandhi | 18:6a4db94011d3 | 329 | { |
sahilmgandhi | 18:6a4db94011d3 | 330 | FMC->ISPCMD = FMC_ISPCMD_WRITE_64; |
sahilmgandhi | 18:6a4db94011d3 | 331 | FMC->ISPADDR = u32Addr; |
sahilmgandhi | 18:6a4db94011d3 | 332 | FMC->MPDAT0 = u32Data0; |
sahilmgandhi | 18:6a4db94011d3 | 333 | FMC->MPDAT1 = u32Data0; |
sahilmgandhi | 18:6a4db94011d3 | 334 | FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 335 | while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; |
sahilmgandhi | 18:6a4db94011d3 | 336 | } |
sahilmgandhi | 18:6a4db94011d3 | 337 | |
sahilmgandhi | 18:6a4db94011d3 | 338 | |
sahilmgandhi | 18:6a4db94011d3 | 339 | /** |
sahilmgandhi | 18:6a4db94011d3 | 340 | * @brief CRC8 Calculator |
sahilmgandhi | 18:6a4db94011d3 | 341 | * @param[in] au32Data CRC8 input data words. |
sahilmgandhi | 18:6a4db94011d3 | 342 | * @param[in] i32Count Number of words in au32Data[]. |
sahilmgandhi | 18:6a4db94011d3 | 343 | * @return The CRC8 value. |
sahilmgandhi | 18:6a4db94011d3 | 344 | */ |
sahilmgandhi | 18:6a4db94011d3 | 345 | uint32_t FMC_CRC8(uint32_t au32Data[], int i32Count) |
sahilmgandhi | 18:6a4db94011d3 | 346 | { |
sahilmgandhi | 18:6a4db94011d3 | 347 | int i32ByteIdx; |
sahilmgandhi | 18:6a4db94011d3 | 348 | uint8_t i, u8Cnt, u8InData; |
sahilmgandhi | 18:6a4db94011d3 | 349 | uint8_t au8CRC[4] = { 0xff, 0xff, 0xff, 0xff }; |
sahilmgandhi | 18:6a4db94011d3 | 350 | |
sahilmgandhi | 18:6a4db94011d3 | 351 | for (i32ByteIdx = 0; i32ByteIdx < 4; i32ByteIdx++) { |
sahilmgandhi | 18:6a4db94011d3 | 352 | for (u8Cnt = 0; u8Cnt < i32Count; u8Cnt++) { |
sahilmgandhi | 18:6a4db94011d3 | 353 | for (i = 0x80; i != 0; i /= 2) { |
sahilmgandhi | 18:6a4db94011d3 | 354 | if ((au8CRC[i32ByteIdx] & 0x80)!=0) { |
sahilmgandhi | 18:6a4db94011d3 | 355 | au8CRC[i32ByteIdx] *= 2; |
sahilmgandhi | 18:6a4db94011d3 | 356 | au8CRC[i32ByteIdx] ^= 7; |
sahilmgandhi | 18:6a4db94011d3 | 357 | } else |
sahilmgandhi | 18:6a4db94011d3 | 358 | au8CRC[i32ByteIdx] *= 2; |
sahilmgandhi | 18:6a4db94011d3 | 359 | |
sahilmgandhi | 18:6a4db94011d3 | 360 | u8InData = (au32Data[u8Cnt] >> (i32ByteIdx * 8)) & 0xff; |
sahilmgandhi | 18:6a4db94011d3 | 361 | |
sahilmgandhi | 18:6a4db94011d3 | 362 | if ((u8InData & i) != 0) |
sahilmgandhi | 18:6a4db94011d3 | 363 | au8CRC[i32ByteIdx]^=0x7; |
sahilmgandhi | 18:6a4db94011d3 | 364 | } |
sahilmgandhi | 18:6a4db94011d3 | 365 | } |
sahilmgandhi | 18:6a4db94011d3 | 366 | } |
sahilmgandhi | 18:6a4db94011d3 | 367 | return (au8CRC[0] | au8CRC[1] << 8 | au8CRC[2] << 16 | au8CRC[3] << 24); |
sahilmgandhi | 18:6a4db94011d3 | 368 | } |
sahilmgandhi | 18:6a4db94011d3 | 369 | |
sahilmgandhi | 18:6a4db94011d3 | 370 | |
sahilmgandhi | 18:6a4db94011d3 | 371 | /** |
sahilmgandhi | 18:6a4db94011d3 | 372 | * @brief Read the User Configuration words. |
sahilmgandhi | 18:6a4db94011d3 | 373 | * @param[out] u32Config: The word array to store words read from flash. |
sahilmgandhi | 18:6a4db94011d3 | 374 | * @param[in] u32Count: Maximum length of u32Config. |
sahilmgandhi | 18:6a4db94011d3 | 375 | * @return Success or not. |
sahilmgandhi | 18:6a4db94011d3 | 376 | * @retval 0 Success |
sahilmgandhi | 18:6a4db94011d3 | 377 | * @retval -1 User Configuration CRC check error |
sahilmgandhi | 18:6a4db94011d3 | 378 | */ |
sahilmgandhi | 18:6a4db94011d3 | 379 | int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count) |
sahilmgandhi | 18:6a4db94011d3 | 380 | { |
sahilmgandhi | 18:6a4db94011d3 | 381 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 382 | |
sahilmgandhi | 18:6a4db94011d3 | 383 | for (i = 0; i < u32Count; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 384 | u32Config[i] = FMC_Read(FMC_CONFIG_BASE + i*4); |
sahilmgandhi | 18:6a4db94011d3 | 385 | } |
sahilmgandhi | 18:6a4db94011d3 | 386 | |
sahilmgandhi | 18:6a4db94011d3 | 387 | if (FMC->ISPSTS & FMC_ISPSTS_CFGCRCF_Msk) |
sahilmgandhi | 18:6a4db94011d3 | 388 | return -1; |
sahilmgandhi | 18:6a4db94011d3 | 389 | |
sahilmgandhi | 18:6a4db94011d3 | 390 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 391 | } |
sahilmgandhi | 18:6a4db94011d3 | 392 | |
sahilmgandhi | 18:6a4db94011d3 | 393 | |
sahilmgandhi | 18:6a4db94011d3 | 394 | /** |
sahilmgandhi | 18:6a4db94011d3 | 395 | * @brief Write User Configuration |
sahilmgandhi | 18:6a4db94011d3 | 396 | * @param[in] u32Config The word array to store data. MUST be a four word array. |
sahilmgandhi | 18:6a4db94011d3 | 397 | * @param[in] u32Count MUST be 4. |
sahilmgandhi | 18:6a4db94011d3 | 398 | * @return Success or not. |
sahilmgandhi | 18:6a4db94011d3 | 399 | * @retval 0 Success |
sahilmgandhi | 18:6a4db94011d3 | 400 | * @retval -1 Failed |
sahilmgandhi | 18:6a4db94011d3 | 401 | */ |
sahilmgandhi | 18:6a4db94011d3 | 402 | int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count) |
sahilmgandhi | 18:6a4db94011d3 | 403 | { |
sahilmgandhi | 18:6a4db94011d3 | 404 | uint32_t i, u32CRC, u32Data; |
sahilmgandhi | 18:6a4db94011d3 | 405 | |
sahilmgandhi | 18:6a4db94011d3 | 406 | FMC_Erase(FMC_CONFIG_BASE); |
sahilmgandhi | 18:6a4db94011d3 | 407 | |
sahilmgandhi | 18:6a4db94011d3 | 408 | u32CRC = FMC_CRC8(u32Config, 3); |
sahilmgandhi | 18:6a4db94011d3 | 409 | |
sahilmgandhi | 18:6a4db94011d3 | 410 | for (i = 0; i < 4; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 411 | FMC_Write(FMC_CONFIG_BASE + i * 4, (i < 3) ? u32Config[i] : u32CRC); |
sahilmgandhi | 18:6a4db94011d3 | 412 | } |
sahilmgandhi | 18:6a4db94011d3 | 413 | |
sahilmgandhi | 18:6a4db94011d3 | 414 | for (i = 0; i < 4; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 415 | u32Data = FMC_Read(FMC_CONFIG_BASE + i * 4); |
sahilmgandhi | 18:6a4db94011d3 | 416 | |
sahilmgandhi | 18:6a4db94011d3 | 417 | if (u32Data != ((i < 3) ? u32Config[i] : u32CRC)) |
sahilmgandhi | 18:6a4db94011d3 | 418 | return -1; |
sahilmgandhi | 18:6a4db94011d3 | 419 | } |
sahilmgandhi | 18:6a4db94011d3 | 420 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 421 | } |
sahilmgandhi | 18:6a4db94011d3 | 422 | |
sahilmgandhi | 18:6a4db94011d3 | 423 | |
sahilmgandhi | 18:6a4db94011d3 | 424 | /*@}*/ /* end of group NUC472_442_FMC_EXPORTED_FUNCTIONS */ |
sahilmgandhi | 18:6a4db94011d3 | 425 | |
sahilmgandhi | 18:6a4db94011d3 | 426 | /*@}*/ /* end of group NUC472_442_FMC_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 427 | |
sahilmgandhi | 18:6a4db94011d3 | 428 | /*@}*/ /* end of group NUC472_442_Device_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 429 | |
sahilmgandhi | 18:6a4db94011d3 | 430 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ |
sahilmgandhi | 18:6a4db94011d3 | 431 | |
sahilmgandhi | 18:6a4db94011d3 | 432 |