BTLE demo for MAXWSNENV.

Dependencies:   BLE_API BMP180 Si7020 mbed MaximBLE

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers btle_addr.c Source File

btle_addr.c

00001 /*******************************************************************************
00002  * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * Except as contained in this notice, the name of Maxim Integrated
00023  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024  * Products, Inc. Branding Policy.
00025  *
00026  * The mere transfer of this software does not imply any licenses
00027  * of trade secrets, proprietary technology, copyrights, patents,
00028  * trademarks, maskwork rights, or any other form of intellectual
00029  * property whatsoever. Maxim Integrated Products, Inc. retains all
00030  * ownership rights.
00031  *******************************************************************************
00032  */
00033 
00034 #include <stdint.h>
00035 #include <string.h>
00036 #include "cmsis.h"
00037 #include "clkman_regs.h"
00038 #include "tpu_regs.h"
00039 #include "flc_regs.h"
00040 
00041 #define BTLE_ADDR_ADDRESS       (MXC_FLASH_MEM_BASE + MXC_FLASH_MEM_SIZE - 8)
00042 #define FLC_CTRL_UNLOCK_VALUE   0x20000000UL
00043 
00044 static void getRandAddress(uint32_t address[2])
00045 {
00046     uint32_t clk_config_save = MXC_CLKMAN->clk_config;
00047     uint32_t clk_ctrl_10_prng_save = MXC_CLKMAN->clk_ctrl_10_prng;
00048     uint32_t crypt_clk_ctrl_2_prng_save = MXC_CLKMAN->crypt_clk_ctrl_2_prng;
00049     uint32_t clk_ctrl_save = MXC_CLKMAN->clk_ctrl;
00050     uint16_t *ptr = (uint16_t*)address;
00051 
00052     // Enable the crypto RO if necessary
00053     if (!(MXC_CLKMAN->clk_config & MXC_F_CLKMAN_CLK_CONFIG_CRYPTO_ENABLE)) {
00054         uint32_t temp = MXC_CLKMAN->clk_config;
00055         temp &= ~MXC_F_CLKMAN_CLK_CONFIG_CRYPTO_STABILITY_COUNT;
00056         temp |= (MXC_F_CLKMAN_CLK_CONFIG_CRYPTO_RESET_N |
00057                  MXC_F_CLKMAN_CLK_CONFIG_CRYPTO_ENABLE |
00058                 (MXC_E_CLKMAN_STABILITY_COUNT_2_8_CLKS << MXC_F_CLKMAN_CLK_CONFIG_CRYPTO_STABILITY_COUNT_POS));
00059         MXC_CLKMAN->clk_config = temp;
00060     }
00061 
00062     // Un-gate crypto RO if necessary
00063     if (!(MXC_CLKMAN->clk_ctrl & MXC_F_CLKMAN_CLK_CTRL_CRYPTO_GATE_N)) {
00064         MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_CRYPTO_GATE_N;
00065     }
00066 
00067     // Enable clock if necessary
00068     if (MXC_CLKMAN->clk_ctrl_10_prng == MXC_E_CLKMAN_CLK_SCALE_DISABLED) {
00069         MXC_CLKMAN->clk_ctrl_10_prng = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
00070     }
00071 
00072     // Enable clock if necessary
00073     if (MXC_CLKMAN->crypt_clk_ctrl_2_prng == MXC_E_CLKMAN_CLK_SCALE_DISABLED) {
00074         MXC_CLKMAN->crypt_clk_ctrl_2_prng = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
00075     }
00076 
00077     ptr[0] = MXC_TPU->prng_rnd_num;
00078     ptr[1] = MXC_TPU->prng_rnd_num;
00079     ptr[2] = MXC_TPU->prng_rnd_num;
00080     ptr[3] = MXC_TPU->prng_rnd_num;
00081 
00082     // Restore clock settings
00083     MXC_CLKMAN->crypt_clk_ctrl_2_prng = crypt_clk_ctrl_2_prng_save;
00084     MXC_CLKMAN->clk_ctrl_10_prng = clk_ctrl_10_prng_save;
00085     MXC_CLKMAN->clk_ctrl = clk_ctrl_save;
00086     MXC_CLKMAN->clk_config = clk_config_save;
00087 }
00088 
00089 void getBtleAddress(uint8_t *bdAddr)
00090 {
00091     uint32_t *ptr;
00092     uint32_t flash_value[2];
00093 
00094     ptr = (uint32_t*)BTLE_ADDR_ADDRESS;
00095 
00096     // Check if there is already an address written to flash
00097     if ((ptr[0] == 0xFFFFFFFF) && (ptr[1] == 0xFFFFFFFF)) {
00098 
00099         // Get a random numbers for the address
00100         getRandAddress(flash_value);
00101 
00102         // Set bits to indicate random static address
00103         flash_value[1] |= 0xFFFFC000;
00104 
00105         // Write the number to flash
00106         MXC_FLC->ctrl = (MXC_FLC->ctrl & ~MXC_F_FLC_CTRL_FLSH_UNLOCK) | FLC_CTRL_UNLOCK_VALUE;
00107 
00108         MXC_FLC->faddr = BTLE_ADDR_ADDRESS;
00109         MXC_FLC->fdata = flash_value[0];
00110         MXC_FLC->ctrl |= MXC_F_FLC_CTRL_WRITE;
00111         while (MXC_FLC->ctrl & MXC_F_FLC_CTRL_PENDING);
00112 
00113         MXC_FLC->faddr = BTLE_ADDR_ADDRESS + 4;
00114         MXC_FLC->fdata = flash_value[1];
00115         MXC_FLC->ctrl |= MXC_F_FLC_CTRL_WRITE;
00116         while (MXC_FLC->ctrl & MXC_F_FLC_CTRL_PENDING);
00117 
00118         MXC_FLC->ctrl &= ~MXC_F_FLC_CTRL_FLSH_UNLOCK;
00119     }
00120 
00121     memcpy(bdAddr, (uint32_t*)BTLE_ADDR_ADDRESS, 6);
00122 }