Nordic stack and drivers for the mbed BLE API

Fork of nRF51822 by Nordic Semiconductor

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:39:43 2016 +0100
Revision:
640:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vincent Coubard 640:c90ae1400bf2 1 /*
Vincent Coubard 640:c90ae1400bf2 2 * Copyright (c) Nordic Semiconductor ASA
Vincent Coubard 640:c90ae1400bf2 3 * All rights reserved.
Vincent Coubard 640:c90ae1400bf2 4 *
Vincent Coubard 640:c90ae1400bf2 5 * Redistribution and use in source and binary forms, with or without modification,
Vincent Coubard 640:c90ae1400bf2 6 * are permitted provided that the following conditions are met:
Vincent Coubard 640:c90ae1400bf2 7 *
Vincent Coubard 640:c90ae1400bf2 8 * 1. Redistributions of source code must retain the above copyright notice, this
Vincent Coubard 640:c90ae1400bf2 9 * list of conditions and the following disclaimer.
Vincent Coubard 640:c90ae1400bf2 10 *
Vincent Coubard 640:c90ae1400bf2 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this
Vincent Coubard 640:c90ae1400bf2 12 * list of conditions and the following disclaimer in the documentation and/or
Vincent Coubard 640:c90ae1400bf2 13 * other materials provided with the distribution.
Vincent Coubard 640:c90ae1400bf2 14 *
Vincent Coubard 640:c90ae1400bf2 15 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
Vincent Coubard 640:c90ae1400bf2 16 * contributors to this software may be used to endorse or promote products
Vincent Coubard 640:c90ae1400bf2 17 * derived from this software without specific prior written permission.
Vincent Coubard 640:c90ae1400bf2 18 *
Vincent Coubard 640:c90ae1400bf2 19 *
Vincent Coubard 640:c90ae1400bf2 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
Vincent Coubard 640:c90ae1400bf2 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Vincent Coubard 640:c90ae1400bf2 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Vincent Coubard 640:c90ae1400bf2 23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
Vincent Coubard 640:c90ae1400bf2 24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Vincent Coubard 640:c90ae1400bf2 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Vincent Coubard 640:c90ae1400bf2 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Vincent Coubard 640:c90ae1400bf2 27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Vincent Coubard 640:c90ae1400bf2 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Vincent Coubard 640:c90ae1400bf2 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Vincent Coubard 640:c90ae1400bf2 30 *
Vincent Coubard 640:c90ae1400bf2 31 */
Vincent Coubard 640:c90ae1400bf2 32
Vincent Coubard 640:c90ae1400bf2 33 /**
Vincent Coubard 640:c90ae1400bf2 34 *@file
Vincent Coubard 640:c90ae1400bf2 35 *@brief NMVC driver implementation
Vincent Coubard 640:c90ae1400bf2 36 */
Vincent Coubard 640:c90ae1400bf2 37
Vincent Coubard 640:c90ae1400bf2 38 #include "stdbool.h"
Vincent Coubard 640:c90ae1400bf2 39 #include "nrf.h"
Vincent Coubard 640:c90ae1400bf2 40 #include "nrf_nvmc.h"
Vincent Coubard 640:c90ae1400bf2 41
Vincent Coubard 640:c90ae1400bf2 42
Vincent Coubard 640:c90ae1400bf2 43 void nrf_nvmc_page_erase(uint32_t address)
Vincent Coubard 640:c90ae1400bf2 44 {
Vincent Coubard 640:c90ae1400bf2 45 // Enable erase.
Vincent Coubard 640:c90ae1400bf2 46 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
Vincent Coubard 640:c90ae1400bf2 47 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 640:c90ae1400bf2 48 {
Vincent Coubard 640:c90ae1400bf2 49 }
Vincent Coubard 640:c90ae1400bf2 50
Vincent Coubard 640:c90ae1400bf2 51 // Erase the page
Vincent Coubard 640:c90ae1400bf2 52 NRF_NVMC->ERASEPAGE = address;
Vincent Coubard 640:c90ae1400bf2 53 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 640:c90ae1400bf2 54 {
Vincent Coubard 640:c90ae1400bf2 55 }
Vincent Coubard 640:c90ae1400bf2 56
Vincent Coubard 640:c90ae1400bf2 57 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
Vincent Coubard 640:c90ae1400bf2 58 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 640:c90ae1400bf2 59 {
Vincent Coubard 640:c90ae1400bf2 60 }
Vincent Coubard 640:c90ae1400bf2 61 }
Vincent Coubard 640:c90ae1400bf2 62
Vincent Coubard 640:c90ae1400bf2 63
Vincent Coubard 640:c90ae1400bf2 64 void nrf_nvmc_write_byte(uint32_t address, uint8_t value)
Vincent Coubard 640:c90ae1400bf2 65 {
Vincent Coubard 640:c90ae1400bf2 66 uint32_t byte_shift = address & (uint32_t)0x03;
Vincent Coubard 640:c90ae1400bf2 67 uint32_t address32 = address & ~byte_shift; // Address to the word this byte is in.
Vincent Coubard 640:c90ae1400bf2 68 uint32_t value32 = (*(uint32_t*)address32 & ~((uint32_t)0xFF << (byte_shift << (uint32_t)3)));
Vincent Coubard 640:c90ae1400bf2 69 value32 = value32 + ((uint32_t)value << (byte_shift << 3));
Vincent Coubard 640:c90ae1400bf2 70
Vincent Coubard 640:c90ae1400bf2 71 // Enable write.
Vincent Coubard 640:c90ae1400bf2 72 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
Vincent Coubard 640:c90ae1400bf2 73 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 640:c90ae1400bf2 74 {
Vincent Coubard 640:c90ae1400bf2 75 }
Vincent Coubard 640:c90ae1400bf2 76
Vincent Coubard 640:c90ae1400bf2 77 *(uint32_t*)address32 = value32;
Vincent Coubard 640:c90ae1400bf2 78 while(NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 640:c90ae1400bf2 79 {
Vincent Coubard 640:c90ae1400bf2 80 }
Vincent Coubard 640:c90ae1400bf2 81
Vincent Coubard 640:c90ae1400bf2 82 NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
Vincent Coubard 640:c90ae1400bf2 83 {
Vincent Coubard 640:c90ae1400bf2 84 }
Vincent Coubard 640:c90ae1400bf2 85 }
Vincent Coubard 640:c90ae1400bf2 86
Vincent Coubard 640:c90ae1400bf2 87 void nrf_nvmc_write_word(uint32_t address, uint32_t value)
Vincent Coubard 640:c90ae1400bf2 88 {
Vincent Coubard 640:c90ae1400bf2 89 // Enable write.
Vincent Coubard 640:c90ae1400bf2 90 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
Vincent Coubard 640:c90ae1400bf2 91 while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
Vincent Coubard 640:c90ae1400bf2 92 }
Vincent Coubard 640:c90ae1400bf2 93
Vincent Coubard 640:c90ae1400bf2 94 *(uint32_t*)address = value;
Vincent Coubard 640:c90ae1400bf2 95 while (NRF_NVMC->READY == NVMC_READY_READY_Busy){
Vincent Coubard 640:c90ae1400bf2 96 }
Vincent Coubard 640:c90ae1400bf2 97
Vincent Coubard 640:c90ae1400bf2 98 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
Vincent Coubard 640:c90ae1400bf2 99 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 640:c90ae1400bf2 100 {
Vincent Coubard 640:c90ae1400bf2 101 }
Vincent Coubard 640:c90ae1400bf2 102 }
Vincent Coubard 640:c90ae1400bf2 103
Vincent Coubard 640:c90ae1400bf2 104 void nrf_nvmc_write_bytes(uint32_t address, const uint8_t * src, uint32_t num_bytes)
Vincent Coubard 640:c90ae1400bf2 105 {
Vincent Coubard 640:c90ae1400bf2 106 uint32_t i;
Vincent Coubard 640:c90ae1400bf2 107 for(i=0;i<num_bytes;i++)
Vincent Coubard 640:c90ae1400bf2 108 {
Vincent Coubard 640:c90ae1400bf2 109 nrf_nvmc_write_byte(address+i,src[i]);
Vincent Coubard 640:c90ae1400bf2 110 }
Vincent Coubard 640:c90ae1400bf2 111 }
Vincent Coubard 640:c90ae1400bf2 112
Vincent Coubard 640:c90ae1400bf2 113 void nrf_nvmc_write_words(uint32_t address, const uint32_t * src, uint32_t num_words)
Vincent Coubard 640:c90ae1400bf2 114 {
Vincent Coubard 640:c90ae1400bf2 115 uint32_t i;
Vincent Coubard 640:c90ae1400bf2 116
Vincent Coubard 640:c90ae1400bf2 117 // Enable write.
Vincent Coubard 640:c90ae1400bf2 118 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
Vincent Coubard 640:c90ae1400bf2 119 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 640:c90ae1400bf2 120 {
Vincent Coubard 640:c90ae1400bf2 121 }
Vincent Coubard 640:c90ae1400bf2 122
Vincent Coubard 640:c90ae1400bf2 123 for(i=0;i<num_words;i++)
Vincent Coubard 640:c90ae1400bf2 124 {
Vincent Coubard 640:c90ae1400bf2 125 ((uint32_t*)address)[i] = src[i];
Vincent Coubard 640:c90ae1400bf2 126 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 640:c90ae1400bf2 127 {
Vincent Coubard 640:c90ae1400bf2 128 }
Vincent Coubard 640:c90ae1400bf2 129 }
Vincent Coubard 640:c90ae1400bf2 130
Vincent Coubard 640:c90ae1400bf2 131 NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
Vincent Coubard 640:c90ae1400bf2 132 while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
Vincent Coubard 640:c90ae1400bf2 133 {
Vincent Coubard 640:c90ae1400bf2 134 }
Vincent Coubard 640:c90ae1400bf2 135 }
Vincent Coubard 640:c90ae1400bf2 136