test
Fork of nrf51-sdk by
Embed:
(wiki syntax)
Show/hide line numbers
nrf_nvmc.c
Go to the documentation of this file.
00001 /* 00002 * Copyright (c) Nordic Semiconductor ASA 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without modification, 00006 * are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, this 00009 * list of conditions and the following disclaimer. 00010 * 00011 * 2. Redistributions in binary form must reproduce the above copyright notice, this 00012 * list of conditions and the following disclaimer in the documentation and/or 00013 * other materials provided with the distribution. 00014 * 00015 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other 00016 * contributors to this software may be used to endorse or promote products 00017 * derived from this software without specific prior written permission. 00018 * 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00021 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00022 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00023 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00024 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00025 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00026 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 00027 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 * 00031 */ 00032 00033 /** 00034 *@file 00035 *@brief NMVC driver implementation 00036 */ 00037 00038 #include "stdbool.h" 00039 #include "nrf.h" 00040 #include "nrf_nvmc.h" 00041 00042 00043 void nrf_nvmc_page_erase(uint32_t address) 00044 { 00045 // Enable erase. 00046 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een; 00047 while (NRF_NVMC->READY == NVMC_READY_READY_Busy) 00048 { 00049 } 00050 00051 // Erase the page 00052 NRF_NVMC->ERASEPAGE = address; 00053 while (NRF_NVMC->READY == NVMC_READY_READY_Busy) 00054 { 00055 } 00056 00057 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; 00058 while (NRF_NVMC->READY == NVMC_READY_READY_Busy) 00059 { 00060 } 00061 } 00062 00063 00064 void nrf_nvmc_write_byte(uint32_t address, uint8_t value) 00065 { 00066 uint32_t byte_shift = address & (uint32_t)0x03; 00067 uint32_t address32 = address & ~byte_shift; // Address to the word this byte is in. 00068 uint32_t value32 = (*(uint32_t*)address32 & ~((uint32_t)0xFF << (byte_shift << (uint32_t)3))); 00069 value32 = value32 + ((uint32_t)value << (byte_shift << 3)); 00070 00071 // Enable write. 00072 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos); 00073 while (NRF_NVMC->READY == NVMC_READY_READY_Busy) 00074 { 00075 } 00076 00077 *(uint32_t*)address32 = value32; 00078 while(NRF_NVMC->READY == NVMC_READY_READY_Busy) 00079 { 00080 } 00081 00082 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos); 00083 { 00084 } 00085 } 00086 00087 void nrf_nvmc_write_word(uint32_t address, uint32_t value) 00088 { 00089 // Enable write. 00090 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen; 00091 while (NRF_NVMC->READY == NVMC_READY_READY_Busy){ 00092 } 00093 00094 *(uint32_t*)address = value; 00095 while (NRF_NVMC->READY == NVMC_READY_READY_Busy){ 00096 } 00097 00098 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; 00099 while (NRF_NVMC->READY == NVMC_READY_READY_Busy) 00100 { 00101 } 00102 } 00103 00104 void nrf_nvmc_write_bytes(uint32_t address, const uint8_t * src, uint32_t num_bytes) 00105 { 00106 uint32_t i; 00107 for(i=0;i<num_bytes;i++) 00108 { 00109 nrf_nvmc_write_byte(address+i,src[i]); 00110 } 00111 } 00112 00113 void nrf_nvmc_write_words(uint32_t address, const uint32_t * src, uint32_t num_words) 00114 { 00115 uint32_t i; 00116 00117 // Enable write. 00118 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen; 00119 while (NRF_NVMC->READY == NVMC_READY_READY_Busy) 00120 { 00121 } 00122 00123 for(i=0;i<num_words;i++) 00124 { 00125 ((uint32_t*)address)[i] = src[i]; 00126 while (NRF_NVMC->READY == NVMC_READY_READY_Busy) 00127 { 00128 } 00129 } 00130 00131 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; 00132 while (NRF_NVMC->READY == NVMC_READY_READY_Busy) 00133 { 00134 } 00135 } 00136
Generated on Tue Jul 12 2022 15:51:27 by 1.7.2