mbed official / mbed-dev

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
<>
Date:
Wed Apr 12 16:21:43 2017 +0100
Revision:
162:e13f6fdb2ac4
This updates the lib to the mbed lib v140

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 162:e13f6fdb2ac4 1 /*
<> 162:e13f6fdb2ac4 2 * Copyright (c) 2009 Nordic Semiconductor ASA
<> 162:e13f6fdb2ac4 3 * All rights reserved.
<> 162:e13f6fdb2ac4 4 *
<> 162:e13f6fdb2ac4 5 * Redistribution and use in source and binary forms, with or without modification,
<> 162:e13f6fdb2ac4 6 * are permitted provided that the following conditions are met:
<> 162:e13f6fdb2ac4 7 *
<> 162:e13f6fdb2ac4 8 * 1. Redistributions of source code must retain the above copyright notice, this list
<> 162:e13f6fdb2ac4 9 * of conditions and the following disclaimer.
<> 162:e13f6fdb2ac4 10 *
<> 162:e13f6fdb2ac4 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
<> 162:e13f6fdb2ac4 12 * integrated circuit in a product or a software update for such product, must reproduce
<> 162:e13f6fdb2ac4 13 * the above copyright notice, this list of conditions and the following disclaimer in
<> 162:e13f6fdb2ac4 14 * the documentation and/or other materials provided with the distribution.
<> 162:e13f6fdb2ac4 15 *
<> 162:e13f6fdb2ac4 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
<> 162:e13f6fdb2ac4 17 * used to endorse or promote products derived from this software without specific prior
<> 162:e13f6fdb2ac4 18 * written permission.
<> 162:e13f6fdb2ac4 19 *
<> 162:e13f6fdb2ac4 20 * 4. This software, with or without modification, must only be used with a
<> 162:e13f6fdb2ac4 21 * Nordic Semiconductor ASA integrated circuit.
<> 162:e13f6fdb2ac4 22 *
<> 162:e13f6fdb2ac4 23 * 5. Any software provided in binary or object form under this license must not be reverse
<> 162:e13f6fdb2ac4 24 * engineered, decompiled, modified and/or disassembled.
<> 162:e13f6fdb2ac4 25 *
<> 162:e13f6fdb2ac4 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
<> 162:e13f6fdb2ac4 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
<> 162:e13f6fdb2ac4 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
<> 162:e13f6fdb2ac4 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
<> 162:e13f6fdb2ac4 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
<> 162:e13f6fdb2ac4 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
<> 162:e13f6fdb2ac4 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
<> 162:e13f6fdb2ac4 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
<> 162:e13f6fdb2ac4 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
<> 162:e13f6fdb2ac4 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<> 162:e13f6fdb2ac4 36 *
<> 162:e13f6fdb2ac4 37 */
<> 162:e13f6fdb2ac4 38 /** @file
<> 162:e13f6fdb2ac4 39 * @addtogroup nrf_dev_radio_rx_example_main nrf_dev_radio_tx_example_main
<> 162:e13f6fdb2ac4 40 * @{
<> 162:e13f6fdb2ac4 41 */
<> 162:e13f6fdb2ac4 42
<> 162:e13f6fdb2ac4 43 #include "radio_config.h"
<> 162:e13f6fdb2ac4 44 #include "nrf_delay.h"
<> 162:e13f6fdb2ac4 45
<> 162:e13f6fdb2ac4 46 /* These are set to zero as ShockBurst packets don't have corresponding fields. */
<> 162:e13f6fdb2ac4 47 #define PACKET_S1_FIELD_SIZE (0UL) /**< Packet S1 field size in bits. */
<> 162:e13f6fdb2ac4 48 #define PACKET_S0_FIELD_SIZE (0UL) /**< Packet S0 field size in bits. */
<> 162:e13f6fdb2ac4 49 #define PACKET_LENGTH_FIELD_SIZE (0UL) /**< Packet length field size in bits. */
<> 162:e13f6fdb2ac4 50
<> 162:e13f6fdb2ac4 51 /**
<> 162:e13f6fdb2ac4 52 * @brief Function for swapping/mirroring bits in a byte.
<> 162:e13f6fdb2ac4 53 *
<> 162:e13f6fdb2ac4 54 *@verbatim
<> 162:e13f6fdb2ac4 55 * output_bit_7 = input_bit_0
<> 162:e13f6fdb2ac4 56 * output_bit_6 = input_bit_1
<> 162:e13f6fdb2ac4 57 * :
<> 162:e13f6fdb2ac4 58 * output_bit_0 = input_bit_7
<> 162:e13f6fdb2ac4 59 *@endverbatim
<> 162:e13f6fdb2ac4 60 *
<> 162:e13f6fdb2ac4 61 * @param[in] inp is the input byte to be swapped.
<> 162:e13f6fdb2ac4 62 *
<> 162:e13f6fdb2ac4 63 * @return
<> 162:e13f6fdb2ac4 64 * Returns the swapped/mirrored input byte.
<> 162:e13f6fdb2ac4 65 */
<> 162:e13f6fdb2ac4 66 static uint32_t swap_bits(uint32_t inp);
<> 162:e13f6fdb2ac4 67
<> 162:e13f6fdb2ac4 68 /**
<> 162:e13f6fdb2ac4 69 * @brief Function for swapping bits in a 32 bit word for each byte individually.
<> 162:e13f6fdb2ac4 70 *
<> 162:e13f6fdb2ac4 71 * The bits are swapped as follows:
<> 162:e13f6fdb2ac4 72 * @verbatim
<> 162:e13f6fdb2ac4 73 * output[31:24] = input[24:31]
<> 162:e13f6fdb2ac4 74 * output[23:16] = input[16:23]
<> 162:e13f6fdb2ac4 75 * output[15:8] = input[8:15]
<> 162:e13f6fdb2ac4 76 * output[7:0] = input[0:7]
<> 162:e13f6fdb2ac4 77 * @endverbatim
<> 162:e13f6fdb2ac4 78 * @param[in] input is the input word to be swapped.
<> 162:e13f6fdb2ac4 79 *
<> 162:e13f6fdb2ac4 80 * @return
<> 162:e13f6fdb2ac4 81 * Returns the swapped input byte.
<> 162:e13f6fdb2ac4 82 */
<> 162:e13f6fdb2ac4 83 static uint32_t bytewise_bitswap(uint32_t inp);
<> 162:e13f6fdb2ac4 84
<> 162:e13f6fdb2ac4 85 static uint32_t swap_bits(uint32_t inp)
<> 162:e13f6fdb2ac4 86 {
<> 162:e13f6fdb2ac4 87 uint32_t i;
<> 162:e13f6fdb2ac4 88 uint32_t retval = 0;
<> 162:e13f6fdb2ac4 89
<> 162:e13f6fdb2ac4 90 inp = (inp & 0x000000FFUL);
<> 162:e13f6fdb2ac4 91
<> 162:e13f6fdb2ac4 92 for (i = 0; i < 8; i++)
<> 162:e13f6fdb2ac4 93 {
<> 162:e13f6fdb2ac4 94 retval |= ((inp >> i) & 0x01) << (7 - i);
<> 162:e13f6fdb2ac4 95 }
<> 162:e13f6fdb2ac4 96
<> 162:e13f6fdb2ac4 97 return retval;
<> 162:e13f6fdb2ac4 98 }
<> 162:e13f6fdb2ac4 99
<> 162:e13f6fdb2ac4 100
<> 162:e13f6fdb2ac4 101 static uint32_t bytewise_bitswap(uint32_t inp)
<> 162:e13f6fdb2ac4 102 {
<> 162:e13f6fdb2ac4 103 return (swap_bits(inp >> 24) << 24)
<> 162:e13f6fdb2ac4 104 | (swap_bits(inp >> 16) << 16)
<> 162:e13f6fdb2ac4 105 | (swap_bits(inp >> 8) << 8)
<> 162:e13f6fdb2ac4 106 | (swap_bits(inp));
<> 162:e13f6fdb2ac4 107 }
<> 162:e13f6fdb2ac4 108
<> 162:e13f6fdb2ac4 109
<> 162:e13f6fdb2ac4 110 /**
<> 162:e13f6fdb2ac4 111 * @brief Function for configuring the radio to operate in ShockBurst compatible mode.
<> 162:e13f6fdb2ac4 112 *
<> 162:e13f6fdb2ac4 113 * To configure the application running on nRF24L series devices:
<> 162:e13f6fdb2ac4 114 *
<> 162:e13f6fdb2ac4 115 * @verbatim
<> 162:e13f6fdb2ac4 116 * uint8_t tx_address[5] = { 0xC0, 0x01, 0x23, 0x45, 0x67 };
<> 162:e13f6fdb2ac4 117 * hal_nrf_set_rf_channel(7);
<> 162:e13f6fdb2ac4 118 * hal_nrf_set_address_width(HAL_NRF_AW_5BYTES);
<> 162:e13f6fdb2ac4 119 * hal_nrf_set_address(HAL_NRF_TX, tx_address);
<> 162:e13f6fdb2ac4 120 * hal_nrf_set_address(HAL_NRF_PIPE0, tx_address);
<> 162:e13f6fdb2ac4 121 * hal_nrf_open_pipe(0, false);
<> 162:e13f6fdb2ac4 122 * hal_nrf_set_datarate(HAL_NRF_1MBPS);
<> 162:e13f6fdb2ac4 123 * hal_nrf_set_crc_mode(HAL_NRF_CRC_16BIT);
<> 162:e13f6fdb2ac4 124 * hal_nrf_setup_dynamic_payload(0xFF);
<> 162:e13f6fdb2ac4 125 * hal_nrf_enable_dynamic_payload(false);
<> 162:e13f6fdb2ac4 126 * @endverbatim
<> 162:e13f6fdb2ac4 127 *
<> 162:e13f6fdb2ac4 128 * When transmitting packets with hal_nrf_write_tx_payload(const uint8_t *tx_pload, uint8_t length),
<> 162:e13f6fdb2ac4 129 * match the length with PACKET_STATIC_LENGTH.
<> 162:e13f6fdb2ac4 130 * hal_nrf_write_tx_payload(payload, PACKET_STATIC_LENGTH);
<> 162:e13f6fdb2ac4 131 *
<> 162:e13f6fdb2ac4 132 */
<> 162:e13f6fdb2ac4 133 void radio_configure()
<> 162:e13f6fdb2ac4 134 {
<> 162:e13f6fdb2ac4 135 // Radio config
<> 162:e13f6fdb2ac4 136 NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
<> 162:e13f6fdb2ac4 137 NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz
<> 162:e13f6fdb2ac4 138 NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
<> 162:e13f6fdb2ac4 139
<> 162:e13f6fdb2ac4 140 // Radio address config
<> 162:e13f6fdb2ac4 141 NRF_RADIO->PREFIX0 =
<> 162:e13f6fdb2ac4 142 ((uint32_t)swap_bits(0xC3) << 24) // Prefix byte of address 3 converted to nRF24L series format
<> 162:e13f6fdb2ac4 143 | ((uint32_t)swap_bits(0xC2) << 16) // Prefix byte of address 2 converted to nRF24L series format
<> 162:e13f6fdb2ac4 144 | ((uint32_t)swap_bits(0xC1) << 8) // Prefix byte of address 1 converted to nRF24L series format
<> 162:e13f6fdb2ac4 145 | ((uint32_t)swap_bits(0xC0) << 0); // Prefix byte of address 0 converted to nRF24L series format
<> 162:e13f6fdb2ac4 146
<> 162:e13f6fdb2ac4 147 NRF_RADIO->PREFIX1 =
<> 162:e13f6fdb2ac4 148 ((uint32_t)swap_bits(0xC7) << 24) // Prefix byte of address 7 converted to nRF24L series format
<> 162:e13f6fdb2ac4 149 | ((uint32_t)swap_bits(0xC6) << 16) // Prefix byte of address 6 converted to nRF24L series format
<> 162:e13f6fdb2ac4 150 | ((uint32_t)swap_bits(0xC4) << 0); // Prefix byte of address 4 converted to nRF24L series format
<> 162:e13f6fdb2ac4 151
<> 162:e13f6fdb2ac4 152 NRF_RADIO->BASE0 = bytewise_bitswap(0x01234567UL); // Base address for prefix 0 converted to nRF24L series format
<> 162:e13f6fdb2ac4 153 NRF_RADIO->BASE1 = bytewise_bitswap(0x89ABCDEFUL); // Base address for prefix 1-7 converted to nRF24L series format
<> 162:e13f6fdb2ac4 154
<> 162:e13f6fdb2ac4 155 NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
<> 162:e13f6fdb2ac4 156 NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
<> 162:e13f6fdb2ac4 157
<> 162:e13f6fdb2ac4 158 // Packet configuration
<> 162:e13f6fdb2ac4 159 NRF_RADIO->PCNF0 = (PACKET_S1_FIELD_SIZE << RADIO_PCNF0_S1LEN_Pos) |
<> 162:e13f6fdb2ac4 160 (PACKET_S0_FIELD_SIZE << RADIO_PCNF0_S0LEN_Pos) |
<> 162:e13f6fdb2ac4 161 (PACKET_LENGTH_FIELD_SIZE << RADIO_PCNF0_LFLEN_Pos); //lint !e845 "The right argument to operator '|' is certain to be 0"
<> 162:e13f6fdb2ac4 162
<> 162:e13f6fdb2ac4 163 // Packet configuration
<> 162:e13f6fdb2ac4 164 NRF_RADIO->PCNF1 = (RADIO_PCNF1_WHITEEN_Disabled << RADIO_PCNF1_WHITEEN_Pos) |
<> 162:e13f6fdb2ac4 165 (RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) |
<> 162:e13f6fdb2ac4 166 (PACKET_BASE_ADDRESS_LENGTH << RADIO_PCNF1_BALEN_Pos) |
<> 162:e13f6fdb2ac4 167 (PACKET_STATIC_LENGTH << RADIO_PCNF1_STATLEN_Pos) |
<> 162:e13f6fdb2ac4 168 (PACKET_PAYLOAD_MAXSIZE << RADIO_PCNF1_MAXLEN_Pos); //lint !e845 "The right argument to operator '|' is certain to be 0"
<> 162:e13f6fdb2ac4 169
<> 162:e13f6fdb2ac4 170 // CRC Config
<> 162:e13f6fdb2ac4 171 NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits
<> 162:e13f6fdb2ac4 172 if ((NRF_RADIO->CRCCNF & RADIO_CRCCNF_LEN_Msk) == (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos))
<> 162:e13f6fdb2ac4 173 {
<> 162:e13f6fdb2ac4 174 NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
<> 162:e13f6fdb2ac4 175 NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16 + x^12^x^5 + 1
<> 162:e13f6fdb2ac4 176 }
<> 162:e13f6fdb2ac4 177 else if ((NRF_RADIO->CRCCNF & RADIO_CRCCNF_LEN_Msk) == (RADIO_CRCCNF_LEN_One << RADIO_CRCCNF_LEN_Pos))
<> 162:e13f6fdb2ac4 178 {
<> 162:e13f6fdb2ac4 179 NRF_RADIO->CRCINIT = 0xFFUL; // Initial value
<> 162:e13f6fdb2ac4 180 NRF_RADIO->CRCPOLY = 0x107UL; // CRC poly: x^8 + x^2^x^1 + 1
<> 162:e13f6fdb2ac4 181 }
<> 162:e13f6fdb2ac4 182 }
<> 162:e13f6fdb2ac4 183
<> 162:e13f6fdb2ac4 184 /**
<> 162:e13f6fdb2ac4 185 * @}
<> 162:e13f6fdb2ac4 186 */