Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.

Dependents:   nRF51822 nRF51822

Committer:
vcoubard
Date:
Thu Apr 07 17:37:40 2016 +0100
Revision:
19:47192cb9def7
Parent:
10:233fefd8162b
Child:
20:a90c48eb1d30
Synchronized with git rev 9251259f
Author: Liyou Zhou
Copy over coresponding files from nordic-sdk 9.0.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 19:47192cb9def7 1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
vcoubard 19:47192cb9def7 2 *
vcoubard 19:47192cb9def7 3 * The information contained herein is property of Nordic Semiconductor ASA.
vcoubard 19:47192cb9def7 4 * Terms and conditions of usage are described in detail in NORDIC
vcoubard 19:47192cb9def7 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
vcoubard 19:47192cb9def7 6 *
vcoubard 19:47192cb9def7 7 * Licensees are granted free, non-transferable use of the information. NO
vcoubard 19:47192cb9def7 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
vcoubard 19:47192cb9def7 9 * the file.
vcoubard 19:47192cb9def7 10 *
vcoubard 19:47192cb9def7 11 * $LastChangedRevision: 17685 $
vcoubard 19:47192cb9def7 12 */
Vincent Coubard 0:f2542974c862 13
Vincent Coubard 0:f2542974c862 14 /**
Vincent Coubard 0:f2542974c862 15 *@file
Vincent Coubard 0:f2542974c862 16 *@brief NMVC driver implementation
Vincent Coubard 0:f2542974c862 17 */
Vincent Coubard 0:f2542974c862 18
Vincent Coubard 0:f2542974c862 19 #include "stdbool.h"
Vincent Coubard 0:f2542974c862 20 #include "nrf.h"
Vincent Coubard 0:f2542974c862 21 #include "nrf_nvmc.h"
Vincent Coubard 0:f2542974c862 22
Vincent Coubard 0:f2542974c862 23
Vincent Coubard 0:f2542974c862 24 void nrf_nvmc_page_erase(uint32_t address)
Vincent Coubard 0:f2542974c862 25 {
Vincent Coubard 0:f2542974c862 26 // Enable erase.
Vincent Coubard 0:f2542974c862 27 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
Vincent Coubard 0:f2542974c862 28 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 29 {
Vincent Coubard 0:f2542974c862 30 }
Vincent Coubard 0:f2542974c862 31
Vincent Coubard 0:f2542974c862 32 // Erase the page
Vincent Coubard 0:f2542974c862 33 NRF_NVMC->ERASEPAGE = address;
Vincent Coubard 0:f2542974c862 34 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 35 {
Vincent Coubard 0:f2542974c862 36 }
Vincent Coubard 0:f2542974c862 37
Vincent Coubard 0:f2542974c862 38 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
Vincent Coubard 0:f2542974c862 39 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 40 {
Vincent Coubard 0:f2542974c862 41 }
Vincent Coubard 0:f2542974c862 42 }
Vincent Coubard 0:f2542974c862 43
Vincent Coubard 0:f2542974c862 44
Vincent Coubard 0:f2542974c862 45 void nrf_nvmc_write_byte(uint32_t address, uint8_t value)
Vincent Coubard 0:f2542974c862 46 {
Vincent Coubard 0:f2542974c862 47 uint32_t byte_shift = address & (uint32_t)0x03;
Vincent Coubard 0:f2542974c862 48 uint32_t address32 = address & ~byte_shift; // Address to the word this byte is in.
Vincent Coubard 0:f2542974c862 49 uint32_t value32 = (*(uint32_t*)address32 & ~((uint32_t)0xFF << (byte_shift << (uint32_t)3)));
Vincent Coubard 0:f2542974c862 50 value32 = value32 + ((uint32_t)value << (byte_shift << 3));
Vincent Coubard 0:f2542974c862 51
Vincent Coubard 0:f2542974c862 52 // Enable write.
Vincent Coubard 0:f2542974c862 53 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
Vincent Coubard 0:f2542974c862 54 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 55 {
Vincent Coubard 0:f2542974c862 56 }
Vincent Coubard 0:f2542974c862 57
Vincent Coubard 0:f2542974c862 58 *(uint32_t*)address32 = value32;
Vincent Coubard 0:f2542974c862 59 while(NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 60 {
Vincent Coubard 0:f2542974c862 61 }
Vincent Coubard 0:f2542974c862 62
Vincent Coubard 0:f2542974c862 63 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
Vincent Coubard 0:f2542974c862 64 {
Vincent Coubard 0:f2542974c862 65 }
Vincent Coubard 0:f2542974c862 66 }
Vincent Coubard 0:f2542974c862 67
Vincent Coubard 0:f2542974c862 68 void nrf_nvmc_write_word(uint32_t address, uint32_t value)
Vincent Coubard 0:f2542974c862 69 {
Vincent Coubard 0:f2542974c862 70 // Enable write.
Vincent Coubard 0:f2542974c862 71 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
Vincent Coubard 0:f2542974c862 72 while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
Vincent Coubard 0:f2542974c862 73 }
Vincent Coubard 0:f2542974c862 74
Vincent Coubard 0:f2542974c862 75 *(uint32_t*)address = value;
Vincent Coubard 0:f2542974c862 76 while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
Vincent Coubard 0:f2542974c862 77 }
Vincent Coubard 0:f2542974c862 78
Vincent Coubard 0:f2542974c862 79 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
Vincent Coubard 0:f2542974c862 80 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 81 {
Vincent Coubard 0:f2542974c862 82 }
Vincent Coubard 0:f2542974c862 83 }
Vincent Coubard 0:f2542974c862 84
Vincent Coubard 0:f2542974c862 85 void nrf_nvmc_write_bytes(uint32_t address, const uint8_t * src, uint32_t num_bytes)
Vincent Coubard 0:f2542974c862 86 {
Vincent Coubard 0:f2542974c862 87 uint32_t i;
Vincent Coubard 0:f2542974c862 88 for(i=0;i<num_bytes;i++)
Vincent Coubard 0:f2542974c862 89 {
Vincent Coubard 0:f2542974c862 90 nrf_nvmc_write_byte(address+i,src[i]);
Vincent Coubard 0:f2542974c862 91 }
Vincent Coubard 0:f2542974c862 92 }
Vincent Coubard 0:f2542974c862 93
Vincent Coubard 0:f2542974c862 94 void nrf_nvmc_write_words(uint32_t address, const uint32_t * src, uint32_t num_words)
Vincent Coubard 0:f2542974c862 95 {
Vincent Coubard 0:f2542974c862 96 uint32_t i;
Vincent Coubard 0:f2542974c862 97
Vincent Coubard 0:f2542974c862 98 // Enable write.
Vincent Coubard 0:f2542974c862 99 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
Vincent Coubard 0:f2542974c862 100 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 101 {
Vincent Coubard 0:f2542974c862 102 }
Vincent Coubard 0:f2542974c862 103
Vincent Coubard 0:f2542974c862 104 for(i=0;i<num_words;i++)
Vincent Coubard 0:f2542974c862 105 {
Vincent Coubard 0:f2542974c862 106 ((uint32_t*)address)[i] = src[i];
Vincent Coubard 0:f2542974c862 107 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 108 {
Vincent Coubard 0:f2542974c862 109 }
Vincent Coubard 0:f2542974c862 110 }
Vincent Coubard 0:f2542974c862 111
Vincent Coubard 0:f2542974c862 112 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
Vincent Coubard 0:f2542974c862 113 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 0:f2542974c862 114 {
Vincent Coubard 0:f2542974c862 115 }
Vincent Coubard 0:f2542974c862 116 }
vcoubard 1:ebc0e0ef0a11 117