You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2020/09/21 11:54:05 UTC
[mynewt-nimble] branch master updated (4b6f331 -> d0d2ae0)
This is an automated email from the ASF dual-hosted git repository.
andk pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git.
from 4b6f331 apps/blestress: error correction Failed sending data via L2CAP in test 10 was always assumed to be BLE_HS_ESTALLED error, and it's not always the case.
new e69a66d nimble/phy: Add PHY for Dialog CMAC
new 8759947 nimble/transport: Add HCI transport for Dialog CMAC
new 3313d03 targets: Add sample target for controller on Dialog CMAC
new 3376c5f nimble/phy: Add README for Dialog CMAC PHY
new d0d2ae0 nimble/phy: Remove local copy of TinyCrypt
The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
nimble/drivers/dialog_cmac/README.md | 68 +
.../dialog_cmac/include/ble/xcvr.h} | 15 +-
.../{transport/ram => drivers/dialog_cmac}/pkg.yml | 15 +-
nimble/drivers/dialog_cmac/src/ble_hw.c | 340 ++++
.../drivers/dialog_cmac/src/ble_hw_priv.h | 13 +-
nimble/drivers/dialog_cmac/src/ble_phy.c | 1781 ++++++++++++++++++++
nimble/drivers/dialog_cmac/src/ble_rf.c | 729 ++++++++
.../drivers/dialog_cmac/src/ble_rf_priv.h | 31 +-
.../config => drivers/dialog_cmac}/syscfg.yml | 16 +-
.../transport/{da1469x => dialog_cmac}/.gitignore | 0
.../dialog_cmac/cmac_driver/diag}/pkg.yml | 14 +-
.../dialog_cmac/cmac_driver/diag/src/cmac_diag.c | 68 +
.../cmac_driver/include/cmac_driver/cmac_diag.h} | 9 +-
.../cmac_driver/include/cmac_driver/cmac_host.h} | 9 +-
.../cmac_driver/include/cmac_driver/cmac_shared.h | 164 ++
.../dialog_cmac/cmac_driver}/pkg.yml | 27 +-
.../cmac_driver/scripts/build_libcmac.sh | 34 +-
.../cmac_driver/scripts/create_cmac_bin.sh} | 25 +-
.../dialog_cmac/cmac_driver/src/cmac_host.c | 306 ++++
.../dialog_cmac/cmac_driver/src/cmac_mbox.c | 162 ++
.../dialog_cmac/cmac_driver/src/cmac_rand.c | 145 ++
.../dialog_cmac/cmac_driver/src/cmac_shared.c | 98 ++
.../transport/dialog_cmac/cmac_driver/syscfg.yml | 98 ++
nimble/transport/{ram => dialog_cmac}/pkg.yml | 10 +-
.../dialog_cmac/src/ble_hci_cmac_common.c | 194 +++
nimble/transport/dialog_cmac/src/ble_hci_cmac_hs.c | 153 ++
nimble/transport/dialog_cmac/src/ble_hci_cmac_ll.c | 141 ++
.../dialog_cmac/src/ble_hci_cmac_priv.h} | 16 +-
.../transport/dialog_cmac/src/ble_hci_trans_h4.c | 332 ++++
.../transport/dialog_cmac/src/ble_hci_trans_h4.h | 57 +-
.../transport/{da1469x => dialog_cmac}/syscfg.yml | 4 +-
nimble/transport/pkg.yml | 3 +
nimble/transport/syscfg.yml | 17 +-
targets/dialog_cmac/pkg.yml | 5 +
targets/dialog_cmac/syscfg.yml | 16 +
targets/dialog_cmac/target.yml | 3 +
36 files changed, 4951 insertions(+), 167 deletions(-)
create mode 100644 nimble/drivers/dialog_cmac/README.md
copy nimble/{host/services/ipss/include/services/ipss/ble_svc_ipss.h => drivers/dialog_cmac/include/ble/xcvr.h} (75%)
copy nimble/{transport/ram => drivers/dialog_cmac}/pkg.yml (82%)
create mode 100644 nimble/drivers/dialog_cmac/src/ble_hw.c
copy apps/mesh_badge/src/mesh_badge.h => nimble/drivers/dialog_cmac/src/ble_hw_priv.h (78%)
create mode 100644 nimble/drivers/dialog_cmac/src/ble_phy.c
create mode 100644 nimble/drivers/dialog_cmac/src/ble_rf.c
copy apps/bttester/src/bttester_pipe.h => nimble/drivers/dialog_cmac/src/ble_rf_priv.h (63%)
copy nimble/{host/store/config => drivers/dialog_cmac}/syscfg.yml (75%)
copy nimble/transport/{da1469x => dialog_cmac}/.gitignore (100%)
copy nimble/{drivers/native => transport/dialog_cmac/cmac_driver/diag}/pkg.yml (83%)
create mode 100644 nimble/transport/dialog_cmac/cmac_driver/diag/src/cmac_diag.c
copy nimble/{host/src/ble_hs_startup_priv.h => transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_diag.h} (85%)
copy nimble/{host/src/ble_hs_startup_priv.h => transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_host.h} (85%)
create mode 100644 nimble/transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_shared.h
copy nimble/{controller => transport/dialog_cmac/cmac_driver}/pkg.yml (70%)
copy nimble/transport/{da1469x => dialog_cmac}/cmac_driver/scripts/build_libcmac.sh (61%)
copy nimble/{host/services/ias/pkg.yml => transport/dialog_cmac/cmac_driver/scripts/create_cmac_bin.sh} (67%)
mode change 100644 => 100755
create mode 100644 nimble/transport/dialog_cmac/cmac_driver/src/cmac_host.c
create mode 100644 nimble/transport/dialog_cmac/cmac_driver/src/cmac_mbox.c
create mode 100644 nimble/transport/dialog_cmac/cmac_driver/src/cmac_rand.c
create mode 100644 nimble/transport/dialog_cmac/cmac_driver/src/cmac_shared.c
create mode 100644 nimble/transport/dialog_cmac/cmac_driver/syscfg.yml
copy nimble/transport/{ram => dialog_cmac}/pkg.yml (85%)
create mode 100644 nimble/transport/dialog_cmac/src/ble_hci_cmac_common.c
create mode 100644 nimble/transport/dialog_cmac/src/ble_hci_cmac_hs.c
create mode 100644 nimble/transport/dialog_cmac/src/ble_hci_cmac_ll.c
copy nimble/{host/src/ble_hs_startup_priv.h => transport/dialog_cmac/src/ble_hci_cmac_priv.h} (79%)
create mode 100644 nimble/transport/dialog_cmac/src/ble_hci_trans_h4.c
copy porting/npl/dummy/include/nimble/nimble_npl_os.h => nimble/transport/dialog_cmac/src/ble_hci_trans_h4.h (51%)
copy nimble/transport/{da1469x => dialog_cmac}/syscfg.yml (94%)
create mode 100644 targets/dialog_cmac/pkg.yml
create mode 100644 targets/dialog_cmac/syscfg.yml
create mode 100644 targets/dialog_cmac/target.yml
[mynewt-nimble] 05/05: nimble/phy: Remove local copy of TinyCrypt
Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git
commit d0d2ae010fe134e141748bf664c7c6e02938a69b
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Mon Sep 21 09:48:59 2020 +0200
nimble/phy: Remove local copy of TinyCrypt
Add explicit dependency to apache-mynewt-core, seems to work fine.
Also we do not support running CMAC controller on non-Mynewt RTOS so
mynewt-core will be always available.
---
nimble/drivers/dialog_cmac/pkg.yml | 1 +
nimble/drivers/dialog_cmac/src/aes/aes.h | 130 ---------------
nimble/drivers/dialog_cmac/src/aes/aes_encrypt.c | 193 -----------------------
nimble/drivers/dialog_cmac/src/aes/constants.h | 61 -------
nimble/drivers/dialog_cmac/src/aes/utils.c | 74 ---------
nimble/drivers/dialog_cmac/src/aes/utils.h | 95 -----------
nimble/drivers/dialog_cmac/src/ble_hw.c | 2 +-
7 files changed, 2 insertions(+), 554 deletions(-)
diff --git a/nimble/drivers/dialog_cmac/pkg.yml b/nimble/drivers/dialog_cmac/pkg.yml
index 6d158fc..9cf63ff 100644
--- a/nimble/drivers/dialog_cmac/pkg.yml
+++ b/nimble/drivers/dialog_cmac/pkg.yml
@@ -26,6 +26,7 @@ pkg.keywords:
- bluetooth
pkg.deps:
- "@apache-mynewt-nimble/nimble/controller"
+ - "@apache-mynewt-core/crypto/tinycrypt"
pkg.apis:
- ble_driver
pkg.req_apis:
diff --git a/nimble/drivers/dialog_cmac/src/aes/aes.h b/nimble/drivers/dialog_cmac/src/aes/aes.h
deleted file mode 100644
index b612213..0000000
--- a/nimble/drivers/dialog_cmac/src/aes/aes.h
+++ /dev/null
@@ -1,130 +0,0 @@
-/* aes.h - TinyCrypt interface to an AES-128 implementation */
-
-/*
- * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Intel Corporation nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * @file
- * @brief -- Interface to an AES-128 implementation.
- *
- * Overview: AES-128 is a NIST approved block cipher specified in
- * FIPS 197. Block ciphers are deterministic algorithms that
- * perform a transformation specified by a symmetric key in fixed-
- * length data sets, also called blocks.
- *
- * Security: AES-128 provides approximately 128 bits of security.
- *
- * Usage: 1) call tc_aes128_set_encrypt/decrypt_key to set the key.
- *
- * 2) call tc_aes_encrypt/decrypt to process the data.
- */
-
-#ifndef __TC_AES_H__
-#define __TC_AES_H__
-
-#include <stdint.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#define Nb (4) /* number of columns (32-bit words) comprising the state */
-#define Nk (4) /* number of 32-bit words comprising the key */
-#define Nr (10) /* number of rounds */
-#define TC_AES_BLOCK_SIZE (Nb*Nk)
-#define TC_AES_KEY_SIZE (Nb*Nk)
-
-typedef struct tc_aes_key_sched_struct {
- unsigned int words[Nb*(Nr+1)];
-} *TCAesKeySched_t;
-
-/**
- * @brief Set AES-128 encryption key
- * Uses key k to initialize s
- * @return returns TC_CRYPTO_SUCCESS (1)
- * returns TC_CRYPTO_FAIL (0) if: s == NULL or k == NULL
- * @note This implementation skips the additional steps required for keys
- * larger than 128 bits, and must not be used for AES-192 or
- * AES-256 key schedule -- see FIPS 197 for details
- * @param s IN/OUT -- initialized struct tc_aes_key_sched_struct
- * @param k IN -- points to the AES key
- */
-int tc_aes128_set_encrypt_key(TCAesKeySched_t s, const uint8_t *k);
-
-/**
- * @brief AES-128 Encryption procedure
- * Encrypts contents of in buffer into out buffer under key;
- * schedule s
- * @note Assumes s was initialized by aes_set_encrypt_key;
- * out and in point to 16 byte buffers
- * @return returns TC_CRYPTO_SUCCESS (1)
- * returns TC_CRYPTO_FAIL (0) if: out == NULL or in == NULL or s == NULL
- * @param out IN/OUT -- buffer to receive ciphertext block
- * @param in IN -- a plaintext block to encrypt
- * @param s IN -- initialized AES key schedule
- */
-int tc_aes_encrypt(uint8_t *out, const uint8_t *in,
- const TCAesKeySched_t s);
-
-/**
- * @brief Set the AES-128 decryption key
- * Uses key k to initialize s
- * @return returns TC_CRYPTO_SUCCESS (1)
- * returns TC_CRYPTO_FAIL (0) if: s == NULL or k == NULL
- * @note This is the implementation of the straightforward inverse cipher
- * using the cipher documented in FIPS-197 figure 12, not the
- * equivalent inverse cipher presented in Figure 15
- * @warning This routine skips the additional steps required for keys larger
- * than 128, and must not be used for AES-192 or AES-256 key
- * schedule -- see FIPS 197 for details
- * @param s IN/OUT -- initialized struct tc_aes_key_sched_struct
- * @param k IN -- points to the AES key
- */
-int tc_aes128_set_decrypt_key(TCAesKeySched_t s, const uint8_t *k);
-
-/**
- * @brief AES-128 Encryption procedure
- * Decrypts in buffer into out buffer under key schedule s
- * @return returns TC_CRYPTO_SUCCESS (1)
- * returns TC_CRYPTO_FAIL (0) if: out is NULL or in is NULL or s is NULL
- * @note Assumes s was initialized by aes_set_encrypt_key
- * out and in point to 16 byte buffers
- * @param out IN/OUT -- buffer to receive ciphertext block
- * @param in IN -- a plaintext block to encrypt
- * @param s IN -- initialized AES key schedule
- */
-int tc_aes_decrypt(uint8_t *out, const uint8_t *in,
- const TCAesKeySched_t s);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __TC_AES_H__ */
diff --git a/nimble/drivers/dialog_cmac/src/aes/aes_encrypt.c b/nimble/drivers/dialog_cmac/src/aes/aes_encrypt.c
deleted file mode 100644
index 4288c42..0000000
--- a/nimble/drivers/dialog_cmac/src/aes/aes_encrypt.c
+++ /dev/null
@@ -1,193 +0,0 @@
-/* aes_encrypt.c - TinyCrypt implementation of AES encryption procedure */
-
-/*
- * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Intel Corporation nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "aes.h"
-#include "utils.h"
-#include "constants.h"
-
-static const uint8_t sbox[256] = {
- 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
- 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
- 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26,
- 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
- 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
- 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
- 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed,
- 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
- 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
- 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
- 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec,
- 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
- 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
- 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
- 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
- 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
- 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
- 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
- 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11,
- 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
- 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
- 0xb0, 0x54, 0xbb, 0x16
-};
-
-static const unsigned int rconst[11] = {
- 0x00000000, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000,
- 0x20000000, 0x40000000, 0x80000000, 0x1b000000, 0x36000000
- };
-
-/* AES state and temp buffer */
-uint8_t g_aes_state[Nk*Nb];
-uint8_t g_t[Nb*Nk];
-
-static inline unsigned int rotword(unsigned int a)
-{
- return (((a) >> 24)|((a) << 8));
-}
-
-#define subbyte(a, o)(sbox[((a) >> (o))&0xff] << (o))
-#define subword(a)(subbyte(a, 24)|subbyte(a, 16)|subbyte(a, 8)|subbyte(a, 0))
-
-int tc_aes128_set_encrypt_key(TCAesKeySched_t s, const uint8_t *k)
-{
- unsigned int i;
- unsigned int t;
-
- if (s == (TCAesKeySched_t) 0) {
- return TC_CRYPTO_FAIL;
- } else if (k == (const uint8_t *) 0) {
- return TC_CRYPTO_FAIL;
- }
-
- for (i = 0; i < Nk; ++i) {
- s->words[i] = (k[Nb*i]<<24) | (k[Nb*i+1]<<16) |
- (k[Nb*i+2]<<8) | (k[Nb*i+3]);
- }
-
- for (; i < (Nb * (Nr + 1)); ++i) {
- t = s->words[i-1];
- if ((i % Nk) == 0) {
- t = subword(rotword(t)) ^ rconst[i/Nk];
- }
- s->words[i] = s->words[i-Nk] ^ t;
- }
-
- return TC_CRYPTO_SUCCESS;
-}
-
-static inline void add_round_key(uint8_t *s, const unsigned int *k)
-{
- s[0] ^= (uint8_t)(k[0] >> 24); s[1] ^= (uint8_t)(k[0] >> 16);
- s[2] ^= (uint8_t)(k[0] >> 8); s[3] ^= (uint8_t)(k[0]);
- s[4] ^= (uint8_t)(k[1] >> 24); s[5] ^= (uint8_t)(k[1] >> 16);
- s[6] ^= (uint8_t)(k[1] >> 8); s[7] ^= (uint8_t)(k[1]);
- s[8] ^= (uint8_t)(k[2] >> 24); s[9] ^= (uint8_t)(k[2] >> 16);
- s[10] ^= (uint8_t)(k[2] >> 8); s[11] ^= (uint8_t)(k[2]);
- s[12] ^= (uint8_t)(k[3] >> 24); s[13] ^= (uint8_t)(k[3] >> 16);
- s[14] ^= (uint8_t)(k[3] >> 8); s[15] ^= (uint8_t)(k[3]);
-}
-
-static inline void sub_bytes(uint8_t *s)
-{
- unsigned int i;
-
- for (i = 0; i < (Nb * Nk); ++i) {
- s[i] = sbox[s[i]];
- }
-}
-
-#define triple(a)(_double_byte(a)^(a))
-
-static inline void mult_row_column(uint8_t *out, const uint8_t *in)
-{
- out[0] = _double_byte(in[0]) ^ triple(in[1]) ^ in[2] ^ in[3];
- out[1] = in[0] ^ _double_byte(in[1]) ^ triple(in[2]) ^ in[3];
- out[2] = in[0] ^ in[1] ^ _double_byte(in[2]) ^ triple(in[3]);
- out[3] = triple(in[0]) ^ in[1] ^ in[2] ^ _double_byte(in[3]);
-}
-
-static inline void mix_columns(uint8_t *s)
-{
- mult_row_column(g_t, s);
- mult_row_column(&g_t[Nb], s+Nb);
- mult_row_column(&g_t[2 * Nb], s + (2 * Nb));
- mult_row_column(&g_t[3 * Nb], s + (3 * Nb));
- (void) _copy(s, sizeof(g_t), g_t, sizeof(g_t));
-}
-
-/*
- * This shift_rows also implements the matrix flip required for mix_columns, but
- * performs it here to reduce the number of memory operations.
- */
-static inline void shift_rows(uint8_t *s)
-{
- g_t[0] = s[0]; g_t[1] = s[5]; g_t[2] = s[10]; g_t[3] = s[15];
- g_t[4] = s[4]; g_t[5] = s[9]; g_t[6] = s[14]; g_t[7] = s[3];
- g_t[8] = s[8]; g_t[9] = s[13]; g_t[10] = s[2]; g_t[11] = s[7];
- g_t[12] = s[12]; g_t[13] = s[1]; g_t[14] = s[6]; g_t[15] = s[11];
- (void) _copy(s, sizeof(g_t), g_t, sizeof(g_t));
-}
-
-int tc_aes_encrypt(uint8_t *out, const uint8_t *in, const TCAesKeySched_t s)
-{
- unsigned int i;
-
- if (out == (uint8_t *) 0) {
- return TC_CRYPTO_FAIL;
- } else if (in == (const uint8_t *) 0) {
- return TC_CRYPTO_FAIL;
- } else if (s == (TCAesKeySched_t) 0) {
- return TC_CRYPTO_FAIL;
- }
-
- (void)_copy(g_aes_state, sizeof(g_aes_state), in,
- sizeof(g_aes_state));
- add_round_key(g_aes_state, s->words);
-
- for (i = 0; i < (Nr - 1); ++i) {
- sub_bytes(g_aes_state);
- shift_rows(g_aes_state);
- mix_columns(g_aes_state);
- add_round_key(g_aes_state, s->words + Nb*(i+1));
- }
-
- sub_bytes(g_aes_state);
- shift_rows(g_aes_state);
- add_round_key(g_aes_state, s->words + Nb*(i+1));
-
- (void)_copy(out, sizeof(g_aes_state), g_aes_state,
- sizeof(g_aes_state));
-
- /* zeroing out the state buffer */
- _set(g_aes_state, TC_ZERO_BYTE, sizeof(g_aes_state));
-
- return TC_CRYPTO_SUCCESS;
-}
diff --git a/nimble/drivers/dialog_cmac/src/aes/constants.h b/nimble/drivers/dialog_cmac/src/aes/constants.h
deleted file mode 100644
index 965490e..0000000
--- a/nimble/drivers/dialog_cmac/src/aes/constants.h
+++ /dev/null
@@ -1,61 +0,0 @@
-/* constants.h - TinyCrypt interface to constants */
-
-/*
- * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Intel Corporation nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * @file
- * @brief -- Interface to constants.
- *
- */
-
-#ifndef __TC_CONSTANTS_H__
-#define __TC_CONSTANTS_H__
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <stdbool.h>
-
-#ifndef NULL
-#define NULL ((void *)0)
-#endif
-
-#define TC_CRYPTO_SUCCESS 1
-#define TC_CRYPTO_FAIL 0
-
-#define TC_ZERO_BYTE 0x00
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __TC_CONSTANTS_H__ */
diff --git a/nimble/drivers/dialog_cmac/src/aes/utils.c b/nimble/drivers/dialog_cmac/src/aes/utils.c
deleted file mode 100644
index 61a567b..0000000
--- a/nimble/drivers/dialog_cmac/src/aes/utils.c
+++ /dev/null
@@ -1,74 +0,0 @@
-/* utils.c - TinyCrypt platform-dependent run-time operations */
-
-/*
- * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Intel Corporation nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "utils.h"
-#include "constants.h"
-
-#include <string.h>
-
-#define MASK_TWENTY_SEVEN 0x1b
-
-unsigned int _copy(uint8_t *to, unsigned int to_len,
- const uint8_t *from, unsigned int from_len)
-{
- if (from_len <= to_len) {
- (void)memcpy(to, from, from_len);
- return from_len;
- } else {
- return TC_CRYPTO_FAIL;
- }
-}
-
-void _set(void *to, uint8_t val, unsigned int len)
-{
- (void)memset(to, val, len);
-}
-
-/*
- * Doubles the value of a byte for values up to 127.
- */
-uint8_t _double_byte(uint8_t a)
-{
- return ((a<<1) ^ ((a>>7) * MASK_TWENTY_SEVEN));
-}
-
-int _compare(const uint8_t *a, const uint8_t *b, size_t size)
-{
- const uint8_t *tempa = a;
- const uint8_t *tempb = b;
- uint8_t result = 0;
-
- for (unsigned int i = 0; i < size; i++) {
- result |= tempa[i] ^ tempb[i];
- }
- return result;
-}
diff --git a/nimble/drivers/dialog_cmac/src/aes/utils.h b/nimble/drivers/dialog_cmac/src/aes/utils.h
deleted file mode 100644
index bab5c32..0000000
--- a/nimble/drivers/dialog_cmac/src/aes/utils.h
+++ /dev/null
@@ -1,95 +0,0 @@
-/* utils.h - TinyCrypt interface to platform-dependent run-time operations */
-
-/*
- * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Intel Corporation nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * @file
- * @brief Interface to platform-dependent run-time operations.
- *
- */
-
-#ifndef __TC_UTILS_H__
-#define __TC_UTILS_H__
-
-#include <stdint.h>
-#include <stddef.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- * @brief Copy the the buffer 'from' to the buffer 'to'.
- * @return returns TC_CRYPTO_SUCCESS (1)
- * returns TC_CRYPTO_FAIL (0) if:
- * from_len > to_len.
- *
- * @param to OUT -- destination buffer
- * @param to_len IN -- length of destination buffer
- * @param from IN -- origin buffer
- * @param from_len IN -- length of origin buffer
- */
-unsigned int _copy(uint8_t *to, unsigned int to_len,
- const uint8_t *from, unsigned int from_len);
-
-/**
- * @brief Set the value 'val' into the buffer 'to', 'len' times.
- *
- * @param to OUT -- destination buffer
- * @param val IN -- value to be set in 'to'
- * @param len IN -- number of times the value will be copied
- */
-void _set(void *to, uint8_t val, unsigned int len);
-
-/*
- * @brief AES specific doubling function, which utilizes
- * the finite field used by AES.
- * @return Returns a^2
- *
- * @param a IN/OUT -- value to be doubled
- */
-uint8_t _double_byte(uint8_t a);
-
-/*
- * @brief Constant-time algorithm to compare if two sequences of bytes are equal
- * @return Returns 0 if equal, and non-zero otherwise
- *
- * @param a IN -- sequence of bytes a
- * @param b IN -- sequence of bytes b
- * @param size IN -- size of sequences a and b
- */
-int _compare(const uint8_t *a, const uint8_t *b, size_t size);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __TC_UTILS_H__ */
diff --git a/nimble/drivers/dialog_cmac/src/ble_hw.c b/nimble/drivers/dialog_cmac/src/ble_hw.c
index d8ee544..98c8144 100644
--- a/nimble/drivers/dialog_cmac/src/ble_hw.c
+++ b/nimble/drivers/dialog_cmac/src/ble_hw.c
@@ -25,7 +25,7 @@
#include "CMAC.h"
#include "cmac_driver/cmac_shared.h"
#include "mcu/mcu.h"
-#include "aes/aes.h"
+#include "tinycrypt/aes.h"
static struct tc_aes_key_sched_struct g_ctx;
[mynewt-nimble] 03/05: targets: Add sample target for controller on
Dialog CMAC
Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git
commit 3313d03ba8e4ee377646b5fd7ecb2b891664ecd9
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Wed Sep 2 15:00:19 2020 +0200
targets: Add sample target for controller on Dialog CMAC
This adds sample target for building NimBLE controller (blehci app)
for Dialog CMAC. It is used by default when "dialog_cmac" transport
is selected on host side.
Sample target should be used as a base for creating customized targets.
The default settings for timigs are recommended values and should not
be changed unless required.
---
targets/dialog_cmac/pkg.yml | 5 +++++
targets/dialog_cmac/syscfg.yml | 16 ++++++++++++++++
targets/dialog_cmac/target.yml | 3 +++
3 files changed, 24 insertions(+)
diff --git a/targets/dialog_cmac/pkg.yml b/targets/dialog_cmac/pkg.yml
new file mode 100644
index 0000000..31242b2
--- /dev/null
+++ b/targets/dialog_cmac/pkg.yml
@@ -0,0 +1,5 @@
+pkg.name: targets/dialog_cmac
+pkg.type: target
+pkg.description: Sample target for BLE controller on Dialog CMAC
+pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
diff --git a/targets/dialog_cmac/syscfg.yml b/targets/dialog_cmac/syscfg.yml
new file mode 100644
index 0000000..2f4d334
--- /dev/null
+++ b/targets/dialog_cmac/syscfg.yml
@@ -0,0 +1,16 @@
+syscfg.vals:
+ MCU_DEEP_SLEEP: 1
+ MCU_SLP_TIMER: 1
+ MCU_SLP_TIMER_32K_ONLY: 1
+
+ BLE_HCI_TRANSPORT: dialog_cmac
+
+ # LL recommended settings (decreasing timing values is not recommended)
+ BLE_LL_CFG_FEAT_CTRL_TO_HOST_FLOW_CONTROL: 1
+ BLE_LL_CONN_INIT_MIN_WIN_OFFSET: 2
+ BLE_LL_RFMGMT_ENABLE_TIME: 20
+ BLE_LL_SCHED_AUX_MAFS_DELAY: 150
+ BLE_LL_SCHED_AUX_CHAIN_MAFS_DELAY: 150
+
+ # NOTE: set public address in target settings
+ # BLE_PUBLIC_DEV_ADDR: "(uint8_t[6]){0xff, 0xff, 0xff, 0xff, 0xff, 0xff}"
diff --git a/targets/dialog_cmac/target.yml b/targets/dialog_cmac/target.yml
new file mode 100644
index 0000000..e3ca85c
--- /dev/null
+++ b/targets/dialog_cmac/target.yml
@@ -0,0 +1,3 @@
+target.app: "@apache-mynewt-nimble/apps/blehci"
+target.bsp: "@apache-mynewt-core/hw/bsp/dialog_cmac"
+target.build_profile: speed
[mynewt-nimble] 04/05: nimble/phy: Add README for Dialog CMAC PHY
Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git
commit 3376c5ffeb220cbc13e0efd7316778f37b78c506
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Wed Sep 2 17:06:49 2020 +0200
nimble/phy: Add README for Dialog CMAC PHY
---
nimble/drivers/dialog_cmac/README.md | 68 ++++++++++++++++++++++++++++++++++++
1 file changed, 68 insertions(+)
diff --git a/nimble/drivers/dialog_cmac/README.md b/nimble/drivers/dialog_cmac/README.md
new file mode 100644
index 0000000..2e76e2c
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/README.md
@@ -0,0 +1,68 @@
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+-->
+
+## How to run NimBLE controller on Dialog DA1469x
+
+Dialog DA1469x has separate Cortex-M0+ core inside CMAC hw block which can run
+NimBLE controller. This means DA1469x can run full NimBLE stack: host is running
+on M33 core while controller is running on M0+ core. Both communicate using
+standard HCI H4 protocol exchanged over mailboxes located in shared memory.
+
+### Basic setup
+
+In order to run full NimBLE stack on DA1469x you will need two newt targets: one
+for M33 (e.g. `dialog_da1469x-dk-pro` BSP) and one for M0+ (`dialog_cmac` BSP).
+
+Once everything is configured properly, you only need to build target for M33.
+Target configured for M0+ will be build automatically and image is linked with
+M33 image so everything can be flashed at once just as if there is only single
+target used.
+
+Target for M33 should be set and configured as any other BLE application. In
+order to use NimBLE controller on CMAC, set proper HCI transport via syscfg:
+
+ BLE_HCI_TRANSPORT: dialog_cmac
+
+This will include proper transport, driver and add M0+ target to build process.
+
+For M0+, there is sample target provided in `targets/dialog_cmac` and it's used
+by default unless overrided by syscfg in M33 target:
+
+ CMAC_IMAGE_TARGET_NAME: "@apache-mynewt-nimble/targets/dialog_cmac"
+
+If you wish to create own target for M0+, make sure your target is set the same
+way (`app`, `bsp` and `build_profile`) as sample. Also it is recommended to use
+syscfg settings from sample target in new target.
+
+### NimBLE configuration
+
+Since host and controller are running on different cores, they both use separate
+configuration: host configuration is in M33 target, controller configuration is
+in M0+ target. There is currently no way to automatically synchronize both, so
+care needs to be taken when enabling features in either of targets.
+
+A possible workaround is to use separate `.yml` file with all the NimBLE syscfg
+values settings and include it in both targets using `$import` directive which
+is supported by recent versions of `newt` tool.
+
+### Advanced settings
+
+(tbd)
[mynewt-nimble] 02/05: nimble/transport: Add HCI transport for
Dialog CMAC
Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git
commit 87599473e0cbfd74afff194396b7644743a126f7
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Wed Sep 2 12:51:53 2020 +0200
nimble/transport: Add HCI transport for Dialog CMAC
This adds transport for Dialog CMAC. It uses HCI H4 protocol over shared
memory buffers. Included driver takes care of CMAC initialization.
Once host build is switched to "dialog_cmac" transport, proper image for
CMAC is automatically built and integrated with host image. A sample
target will be provided separatety.
---
nimble/transport/dialog_cmac/.gitignore | 2 +
.../{ => dialog_cmac/cmac_driver/diag}/pkg.yml | 32 +-
.../dialog_cmac/cmac_driver/diag/src/cmac_diag.c | 68 +++++
.../cmac_driver/include/cmac_driver/cmac_diag.h | 34 +++
.../cmac_driver/include/cmac_driver/cmac_host.h | 34 +++
.../cmac_driver/include/cmac_driver/cmac_shared.h | 164 ++++++++++
.../{ => dialog_cmac/cmac_driver}/pkg.yml | 35 +--
.../cmac_driver/scripts/build_libcmac.sh | 45 +++
.../cmac_driver/scripts/create_cmac_bin.sh} | 39 +--
.../dialog_cmac/cmac_driver/src/cmac_host.c | 306 +++++++++++++++++++
.../dialog_cmac/cmac_driver/src/cmac_mbox.c | 162 ++++++++++
.../dialog_cmac/cmac_driver/src/cmac_rand.c | 145 +++++++++
.../dialog_cmac/cmac_driver/src/cmac_shared.c | 98 ++++++
.../transport/dialog_cmac/cmac_driver/syscfg.yml | 98 ++++++
nimble/transport/{ => dialog_cmac}/pkg.yml | 32 +-
.../dialog_cmac/src/ble_hci_cmac_common.c | 194 ++++++++++++
nimble/transport/dialog_cmac/src/ble_hci_cmac_hs.c | 153 ++++++++++
nimble/transport/dialog_cmac/src/ble_hci_cmac_ll.c | 141 +++++++++
.../transport/dialog_cmac/src/ble_hci_cmac_priv.h | 29 ++
.../transport/dialog_cmac/src/ble_hci_trans_h4.c | 332 +++++++++++++++++++++
.../transport/dialog_cmac/src/ble_hci_trans_h4.h | 49 +++
.../transport/{pkg.yml => dialog_cmac/syscfg.yml} | 45 ++-
nimble/transport/pkg.yml | 3 +
nimble/transport/syscfg.yml | 17 +-
24 files changed, 2126 insertions(+), 131 deletions(-)
diff --git a/nimble/transport/dialog_cmac/.gitignore b/nimble/transport/dialog_cmac/.gitignore
new file mode 100644
index 0000000..bba9f99
--- /dev/null
+++ b/nimble/transport/dialog_cmac/.gitignore
@@ -0,0 +1,2 @@
+/src/libble_stack_da1469x.a
+
diff --git a/nimble/transport/pkg.yml b/nimble/transport/dialog_cmac/cmac_driver/diag/pkg.yml
similarity index 58%
copy from nimble/transport/pkg.yml
copy to nimble/transport/dialog_cmac/cmac_driver/diag/pkg.yml
index 3bfca88..9b9ee83 100644
--- a/nimble/transport/pkg.yml
+++ b/nimble/transport/dialog_cmac/cmac_driver/diag/pkg.yml
@@ -17,32 +17,12 @@
# under the License.
#
-pkg.name: nimble/transport
-pkg.description: Meta-package for NimBLE HCI transport
+pkg.name: nimble/transport/dialog_cmac/cmac_driver/diag
+pkg.description: Default diag configuration for CMAC
pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
pkg.homepage: "http://mynewt.apache.org/"
pkg.keywords:
- - ble
- - bluetooth
-
-pkg.deps.'BLE_HCI_TRANSPORT == "builtin"':
- - nimble/transport/ram
- - nimble/controller
-
-pkg.deps.'BLE_HCI_TRANSPORT == "emspi"':
- - nimble/transport/emspi
-
-pkg.deps.'BLE_HCI_TRANSPORT == "ram"':
- - nimble/transport/ram
-
-pkg.deps.'BLE_HCI_TRANSPORT == "socket"':
- - nimble/transport/socket
-
-pkg.deps.'BLE_HCI_TRANSPORT == "uart"':
- - nimble/transport/uart
-
-pkg.deps.'BLE_HCI_TRANSPORT == "da1469x"':
- - nimble/transport/da1469x
-
-pkg.deps.'BLE_HCI_TRANSPORT == "usb"':
- - nimble/transport/usb
+ - dialog
+ - da1469x
+ - cmac
+pkg.apis: dialog_cmac_diag
diff --git a/nimble/transport/dialog_cmac/cmac_driver/diag/src/cmac_diag.c b/nimble/transport/dialog_cmac/cmac_driver/diag/src/cmac_diag.c
new file mode 100644
index 0000000..4f75470
--- /dev/null
+++ b/nimble/transport/dialog_cmac/cmac_driver/diag/src/cmac_diag.c
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "syscfg/syscfg.h"
+#include "mcu/mcu.h"
+
+#if MYNEWT_VAL(BLE_HOST)
+void
+cmac_diag_setup_host(void)
+{
+ /* Setup pins for diagnostic signals */
+ mcu_gpio_set_pin_function(42, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAG0); /* DIAG_0 @ P1.10 */
+ mcu_gpio_set_pin_function(43, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAG1); /* DIAG_1 @ P1.11 */
+ mcu_gpio_set_pin_function(44, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAG2); /* DIAG_2 @ P1.12 */
+ mcu_gpio_set_pin_function(24, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_3 @ P0.24 */
+ mcu_gpio_set_pin_function(21, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_4 @ P0.21 */
+ mcu_gpio_set_pin_function(20, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_5 @ P0.20 */
+ mcu_gpio_set_pin_function(19, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_6 @ P0.19 */
+ mcu_gpio_set_pin_function(18, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_7 @ P0.18 */
+ mcu_gpio_set_pin_function(31, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_8 @ P0.31 */
+ mcu_gpio_set_pin_function(30, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_9 @ P0.30 */
+ mcu_gpio_set_pin_function(29, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_10 @ P0.29 */
+ mcu_gpio_set_pin_function(28, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_11 @ P0.28 */
+ mcu_gpio_set_pin_function(27, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_12 @ P0.27 */
+ mcu_gpio_set_pin_function(26, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_13 @ P0.26 */
+ mcu_gpio_set_pin_function(38, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_14 @ P1.06 */
+ mcu_gpio_set_pin_function(41, MCU_GPIO_MODE_OUTPUT, MCU_GPIO_FUNC_CMAC_DIAGX); /* DIAG_15 @ P1.09 */
+}
+#endif
+
+#if MYNEWT_VAL(BLE_CONTROLLER)
+void
+cmac_diag_setup_cmac(void)
+{
+ MCU_DIAG_MAP( 0, 4, DSER);
+ MCU_DIAG_MAP( 1, 6, CMAC_ON_ERROR);
+ MCU_DIAG_MAP( 2, 2, PHY_TX_EN);
+ MCU_DIAG_MAP( 3, 2, PHY_RX_EN);
+ MCU_DIAG_MAP( 4, 2, PHY_TXRX_DATA_COMB);
+ MCU_DIAG_MAP( 5, 2, PHY_TXRX_DATA_EN_COMB);
+ MCU_DIAG_MAP( 6, 5, EV1US_FRAME_START);
+ MCU_DIAG_MAP( 7, 5, EV_BS_START);
+ MCU_DIAG_MAP( 8, 5, EV1C_BS_STOP);
+ MCU_DIAG_MAP( 9, 5, EV1US_PHY_TO_IDLE);
+ MCU_DIAG_MAP(10, 9, CALLBACK_IRQ);
+ MCU_DIAG_MAP(11, 9, FIELD_IRQ);
+ MCU_DIAG_MAP(12, 9, FRAME_IRQ);
+ MCU_DIAG_MAP(13, 3, SLP_TIMER_ACTIVE);
+ MCU_DIAG_MAP(14, 4, SLEEPING);
+ MCU_DIAG_MAP(15, 8, LL_TIMER1_00);
+}
+#endif
diff --git a/nimble/transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_diag.h b/nimble/transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_diag.h
new file mode 100644
index 0000000..dc6ee90
--- /dev/null
+++ b/nimble/transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_diag.h
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __MCU_CMAC_DIAG_H_
+#define __MCU_CMAC_DIAG_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void cmac_diag_setup_host(void);
+void cmac_diag_setup_cmac(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __MCU_CMAC_DIAG_H_ */
diff --git a/nimble/transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_host.h b/nimble/transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_host.h
new file mode 100644
index 0000000..22193c7
--- /dev/null
+++ b/nimble/transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_host.h
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __DA1469X_CMAC_V2_H_
+#define __DA1469X_CMAC_V2_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void cmac_host_init(void);
+void cmac_host_signal2cmac(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __DA1469X_CMAC_V2_H_ */
diff --git a/nimble/transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_shared.h b/nimble/transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_shared.h
new file mode 100644
index 0000000..6bcc618
--- /dev/null
+++ b/nimble/transport/dialog_cmac/cmac_driver/include/cmac_driver/cmac_shared.h
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef __MCU_CMAC_SHARED_H_
+#define __MCU_CMAC_SHARED_H_
+
+#include <stdint.h>
+#include "syscfg/syscfg.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define CMAC_SHARED_MAGIC_CMAC (0x4C6C) /* "lL" */
+#define CMAC_SHARED_MAGIC_SYS (0x5368) /* "hS" */
+
+#define CMAC_SHARED_F_SYS_LPCLK_AVAILABLE (0x0001)
+
+/*
+ * Simple circular buffer for storing random numbers generated by M33
+ * Empty: cmr_in = cmr_out = 0;
+ * Full: cmr_in + 1 = cmr_out
+ *
+ * cmr_in: used by the M33 to add random numbers to the circular buffer.
+ * cmr_out: used by CMAC to retrieve random numbers
+ *
+ * NOTE: cmr_in and cmr_out are indices.
+ */
+#define CMAC_RAND_BUF_ELEMS (16)
+
+struct cmac_rand {
+ int cmr_active;
+ int cmr_in;
+ int cmr_out;
+ uint32_t cmr_buf[CMAC_RAND_BUF_ELEMS];
+};
+
+struct cmac_mbox {
+ uint16_t rd_off;
+ uint16_t wr_off;
+};
+
+struct cmac_dcdc {
+ uint8_t enabled;
+ uint32_t v18;
+ uint32_t v18p;
+ uint32_t vdd;
+ uint32_t v14;
+ uint32_t ctrl1;
+};
+
+struct cmac_trim {
+ uint8_t rfcu_len;
+ uint8_t rfcu_mode1_len;
+ uint8_t rfcu_mode2_len;
+ uint8_t synth_len;
+ uint32_t rfcu[ MYNEWT_VAL(CMAC_TRIM_SIZE_RFCU) ];
+ uint32_t rfcu_mode1[2];
+ uint32_t rfcu_mode2[2];
+ uint32_t synth[ MYNEWT_VAL(CMAC_TRIM_SIZE_SYNTH) ];
+};
+
+#if MYNEWT_VAL(CMAC_DEBUG_DATA_ENABLE)
+struct cmac_debug {
+ int8_t last_rx_rssi;
+ int8_t tx_power_override;
+
+ uint32_t cal_res_1;
+ uint32_t cal_res_2;
+ uint32_t trim_val1_tx_1;
+ uint32_t trim_val1_tx_2;
+ uint32_t trim_val2_tx;
+ uint32_t trim_val2_rx;
+};
+#endif
+
+#if MYNEWT_VAL(CMAC_DEBUG_COREDUMP_ENABLE)
+struct cmac_coredump {
+ uint32_t lr;
+ uint32_t pc;
+ uint32_t assert;
+ const char *assert_file;
+ uint32_t assert_line;
+
+ uint32_t CM_STAT_REG;
+ uint32_t CM_LL_TIMER1_36_10_REG;
+ uint32_t CM_LL_TIMER1_9_0_REG;
+ uint32_t CM_ERROR_REG;
+ uint32_t CM_EXC_STAT_REG;
+};
+#endif
+
+struct cmac_shared_data {
+ uint16_t magic_cmac;
+ uint16_t magic_sys;
+ uint32_t lp_clock_freq; /* LP clock frequency */
+ uint32_t xtal32m_settle_us;/* XTAL32M settling time */
+ struct cmac_mbox mbox_s2c; /* SYS2CMAC mailbox */
+ struct cmac_mbox mbox_c2s; /* CMAC2SYS mailbox */
+ struct cmac_dcdc dcdc; /* DCDC settings */
+ struct cmac_trim trim; /* Trim data */
+ struct cmac_rand rand; /* Random numbers */
+#if MYNEWT_VAL(CMAC_DEBUG_DATA_ENABLE)
+ struct cmac_debug debug; /* Extra debug data */
+#endif
+#if MYNEWT_VAL(CMAC_DEBUG_COREDUMP_ENABLE)
+ struct cmac_coredump coredump;
+#endif
+
+ uint8_t mbox_s2c_buf[ MYNEWT_VAL(CMAC_MBOX_SIZE_S2C) ];
+ uint8_t mbox_c2s_buf[ MYNEWT_VAL(CMAC_MBOX_SIZE_C2S) ];
+};
+
+#if MYNEWT_VAL(BLE_HOST)
+extern volatile struct cmac_shared_data *g_cmac_shared_data;
+#elif MYNEWT_VAL(BLE_CONTROLLER)
+extern volatile struct cmac_shared_data g_cmac_shared_data;
+#endif
+
+/* cmac_mbox */
+typedef int (cmac_mbox_read_cb)(const void *data, uint16_t len);
+typedef void (cmac_mbox_write_notif_cb)(void);
+void cmac_mbox_set_read_cb(cmac_mbox_read_cb *cb);
+void cmac_mbox_set_write_notif_cb(cmac_mbox_write_notif_cb *cb);
+int cmac_mbox_read(void);
+int cmac_mbox_write(const void *data, uint16_t len);
+
+/* cmac_rand */
+typedef void (*cmac_rand_isr_cb_t)(uint8_t rnum);
+void cmac_rand_start(void);
+void cmac_rand_stop(void);
+void cmac_rand_read(void);
+void cmac_rand_write(void);
+void cmac_rand_chk_fill(void);
+int cmac_rand_get_next(void);
+int cmac_rand_is_active(void);
+int cmac_rand_is_full(void);
+void cmac_rand_fill(uint32_t *buf, int num_words);
+void cmac_rand_set_isr_cb(cmac_rand_isr_cb_t cb);
+
+void cmac_shared_init(void);
+void cmac_shared_sync(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __MCU_CMAC_SHARED_H_ */
diff --git a/nimble/transport/pkg.yml b/nimble/transport/dialog_cmac/cmac_driver/pkg.yml
similarity index 59%
copy from nimble/transport/pkg.yml
copy to nimble/transport/dialog_cmac/cmac_driver/pkg.yml
index 3bfca88..5b4a212 100644
--- a/nimble/transport/pkg.yml
+++ b/nimble/transport/dialog_cmac/cmac_driver/pkg.yml
@@ -17,32 +17,23 @@
# under the License.
#
-pkg.name: nimble/transport
-pkg.description: Meta-package for NimBLE HCI transport
+pkg.name: nimble/transport/dialog_cmac/cmac_driver
+pkg.description: Driver for Dialog CMAC IPC
pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
pkg.homepage: "http://mynewt.apache.org/"
pkg.keywords:
- - ble
- - bluetooth
+ - dialog
+ - da1469x
+ - cmac
-pkg.deps.'BLE_HCI_TRANSPORT == "builtin"':
- - nimble/transport/ram
- - nimble/controller
+pkg.req_apis.CMAC_DEBUG_DIAG_ENABLE:
+ - dialog_cmac_diag
-pkg.deps.'BLE_HCI_TRANSPORT == "emspi"':
- - nimble/transport/emspi
+pkg.ign_files.BLE_CONTROLLER:
+ - cmac_host.c
-pkg.deps.'BLE_HCI_TRANSPORT == "ram"':
- - nimble/transport/ram
+pkg.post_link_cmds.BLE_CONTROLLER:
+ scripts/create_cmac_bin.sh: 100
-pkg.deps.'BLE_HCI_TRANSPORT == "socket"':
- - nimble/transport/socket
-
-pkg.deps.'BLE_HCI_TRANSPORT == "uart"':
- - nimble/transport/uart
-
-pkg.deps.'BLE_HCI_TRANSPORT == "da1469x"':
- - nimble/transport/da1469x
-
-pkg.deps.'BLE_HCI_TRANSPORT == "usb"':
- - nimble/transport/usb
+pkg.pre_link_cmds.BLE_HOST:
+ scripts/build_libcmac.sh: 100
diff --git a/nimble/transport/dialog_cmac/cmac_driver/scripts/build_libcmac.sh b/nimble/transport/dialog_cmac/cmac_driver/scripts/build_libcmac.sh
new file mode 100755
index 0000000..bbc822a
--- /dev/null
+++ b/nimble/transport/dialog_cmac/cmac_driver/scripts/build_libcmac.sh
@@ -0,0 +1,45 @@
+#!/bin/bash
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+NEWT=${MYNEWT_NEWT_PATH}
+OBJCOPY=${MYNEWT_OBJCOPY_PATH}
+AR=${MYNEWT_AR_PATH}
+LIBCMAC_A=${MYNEWT_USER_SRC_DIR}/libcmac.a
+
+export WORK_DIR=${MYNEWT_USER_WORK_DIR}
+export BASENAME_ROM=cmac.rom
+export BASENAME_RAM=cmac.ram
+
+if [ ${MYNEWT_VAL_CMAC_IMAGE_SINGLE} -eq 0 ]; then
+ # Create empty binary for ROM image (1 byte required for objcopy)
+ truncate -s 1 ${WORK_DIR}/${BASENAME_ROM}.bin
+ # Create fixed size RAM image
+ truncate -s ${MYNEWT_VAL_CMAC_IMAGE_RAM_SIZE} ${WORK_DIR}/${BASENAME_RAM}.bin
+else
+ ${NEWT} build ${MYNEWT_VAL_CMAC_IMAGE_TARGET_NAME}
+fi
+
+cd ${WORK_DIR}
+
+# Convert both binaries to objects and create archive to link
+${OBJCOPY} -I binary -O elf32-littlearm -B armv8-m.main \
+ --rename-section .data=.libcmac.rom ${BASENAME_ROM}.bin ${BASENAME_ROM}.o
+${OBJCOPY} -I binary -O elf32-littlearm -B armv8-m.main \
+ --rename-section .data=.libcmac.ram ${BASENAME_RAM}.bin ${BASENAME_RAM}.o
+${AR} -rcs ${LIBCMAC_A} ${BASENAME_ROM}.o ${BASENAME_RAM}.o
diff --git a/nimble/transport/pkg.yml b/nimble/transport/dialog_cmac/cmac_driver/scripts/create_cmac_bin.sh
old mode 100644
new mode 100755
similarity index 51%
copy from nimble/transport/pkg.yml
copy to nimble/transport/dialog_cmac/cmac_driver/scripts/create_cmac_bin.sh
index 3bfca88..954d086
--- a/nimble/transport/pkg.yml
+++ b/nimble/transport/dialog_cmac/cmac_driver/scripts/create_cmac_bin.sh
@@ -1,4 +1,5 @@
-#
+#!/bin/bash
+
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
@@ -15,34 +16,16 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
-#
-
-pkg.name: nimble/transport
-pkg.description: Meta-package for NimBLE HCI transport
-pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
-pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
- - ble
- - bluetooth
-
-pkg.deps.'BLE_HCI_TRANSPORT == "builtin"':
- - nimble/transport/ram
- - nimble/controller
-
-pkg.deps.'BLE_HCI_TRANSPORT == "emspi"':
- - nimble/transport/emspi
-
-pkg.deps.'BLE_HCI_TRANSPORT == "ram"':
- - nimble/transport/ram
-pkg.deps.'BLE_HCI_TRANSPORT == "socket"':
- - nimble/transport/socket
+OBJCOPY=${MYNEWT_OBJCOPY_PATH}
+ELF=${MYNEWT_APP_BIN_DIR}/blehci.elf
-pkg.deps.'BLE_HCI_TRANSPORT == "uart"':
- - nimble/transport/uart
+cd ${WORK_DIR}
-pkg.deps.'BLE_HCI_TRANSPORT == "da1469x"':
- - nimble/transport/da1469x
+# Strip .ram section from ROM image
+${OBJCOPY} -R .ram -O binary ${ELF} ${BASENAME_ROM}.bin
+# RAM image is the same as binary created by newt
+cp ${ELF}.bin ${BASENAME_RAM}.bin
-pkg.deps.'BLE_HCI_TRANSPORT == "usb"':
- - nimble/transport/usb
+# Create a copy of ROM image to flash to partition, if required
+cp ${BASENAME_ROM}.bin ${ELF}.rom.bin
diff --git a/nimble/transport/dialog_cmac/cmac_driver/src/cmac_host.c b/nimble/transport/dialog_cmac/cmac_driver/src/cmac_host.c
new file mode 100644
index 0000000..0bf2ce9
--- /dev/null
+++ b/nimble/transport/dialog_cmac/cmac_driver/src/cmac_host.c
@@ -0,0 +1,306 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+#include "syscfg/syscfg.h"
+#include "sysflash/sysflash.h"
+#include "os/os.h"
+#include "mcu/mcu.h"
+#include "mcu/cmsis_nvic.h"
+#include "mcu/da1469x_hal.h"
+#include "mcu/da1469x_lpclk.h"
+#include "mcu/da1469x_clock.h"
+#include "mcu/da1469x_trimv.h"
+#include "mcu/da1469x_pdc.h"
+#include "cmac_driver/cmac_host.h"
+#include "cmac_driver/cmac_shared.h"
+#include "cmac_driver/cmac_diag.h"
+#include "trng/trng.h"
+#if MYNEWT_VAL(CMAC_DEBUG_COREDUMP_ENABLE)
+#include "console/console.h"
+#endif
+
+/* CMAC data */
+extern char _binary_cmac_rom_bin_start[];
+extern char _binary_cmac_rom_bin_end;
+extern char _binary_cmac_ram_bin_start[];
+extern char _binary_cmac_ram_bin_end;
+
+struct cmac_image_info {
+ uint32_t magic;
+ uint32_t size_rom;
+ uint32_t size_ram;
+ uint32_t offset_data;
+ uint32_t offset_shared;
+};
+
+/* PDC entry for waking up CMAC */
+static int8_t g_cmac_host_pdc_sys2cmac;
+/* PDC entry for waking up M33 */
+static int8_t g_cmac_host_pdc_cmac2sys;
+
+static void cmac_host_rand_fill(struct os_event *ev);
+static struct os_event g_cmac_host_rand_ev = {
+ .ev_cb = cmac_host_rand_fill
+};
+
+static void cmac_host_rand_chk_fill(void);
+
+static void
+cmac2sys_isr(void)
+{
+#if MYNEWT_VAL(CMAC_DEBUG_COREDUMP_ENABLE)
+ volatile struct cmac_coredump *cd = &g_cmac_shared_data->coredump;
+ const char *assert_file;
+#endif
+
+ os_trace_isr_enter();
+
+ /* Clear CMAC2SYS interrupt */
+ *(volatile uint32_t *)0x40002000 = 2;
+
+ cmac_mbox_read();
+
+ if (*(volatile uint32_t *)0x40002000 & 0x1c00) {
+#if MYNEWT_VAL(CMAC_DEBUG_COREDUMP_ENABLE)
+ console_blocking_mode();
+ console_printf("CMAC error (0x%08lx)\n", *(volatile uint32_t *)0x40002000);
+ console_printf(" lr:0x%08lx pc:0x%08lx\n", cd->lr, cd->pc);
+ if (cd->assert) {
+ console_printf(" assert:0x%08lx\n", cd->assert);
+ if (cd->assert_file) {
+ /* Need to translate pointer from M0 code segment to M33 data */
+ assert_file = cd->assert_file + MCU_MEM_SYSRAM_START_ADDRESS +
+ MEMCTRL->CMI_CODE_BASE_REG;
+ console_printf(" %s:%d\n",
+ assert_file, (unsigned)cd->assert_line);
+ }
+ }
+ console_printf(" 0x%08lx CM_ERROR_REG\n", cd->CM_ERROR_REG);
+ console_printf(" 0x%08lx CM_EXC_STAT_REG\n", cd->CM_EXC_STAT_REG);
+ console_printf(" 0x%08lx CM_LL_TIMER1_36_10_REG\n", cd->CM_LL_TIMER1_36_10_REG);
+ console_printf(" 0x%08lx CM_LL_TIMER1_9_0_REG\n", cd->CM_LL_TIMER1_9_0_REG);
+
+ /* Spin if debugger is connected to CMAC to avoid resetting it */
+ if (cd->CM_STAT_REG & 0x20) {
+ for (;;);
+ }
+#endif
+ /* XXX CMAC is in error state, need to recover */
+ assert(0);
+ return;
+ }
+
+ cmac_host_rand_chk_fill();
+
+ os_trace_isr_exit();
+}
+
+static void
+cmac_host_rand_fill(struct os_event *ev)
+{
+ size_t num_bytes;
+ struct trng_dev *trng;
+ uint32_t *rnum;
+ uint32_t rnums[CMAC_RAND_BUF_ELEMS];
+
+ /* Check if full */
+ if (!cmac_rand_is_active() || cmac_rand_is_full()) {
+ return;
+ }
+
+ assert(ev->ev_arg != NULL);
+
+ /* Fill buffer with random numbers even though we may not use all of them */
+ trng = ev->ev_arg;
+ rnum = &rnums[0];
+ num_bytes = trng_read(trng, rnum, CMAC_RAND_BUF_ELEMS * sizeof(uint32_t));
+
+ cmac_rand_fill(rnum, num_bytes / 4);
+ cmac_host_signal2cmac();
+}
+
+static void
+cmac_host_rand_chk_fill(void)
+{
+ if (cmac_rand_is_active() && !cmac_rand_is_full()) {
+ os_eventq_put(os_eventq_dflt_get(), &g_cmac_host_rand_ev);
+ }
+}
+
+void
+cmac_host_signal2cmac(void)
+{
+ da1469x_pdc_set(g_cmac_host_pdc_sys2cmac);
+}
+
+static void
+cmac_host_lpclk_cb(uint32_t freq)
+{
+ g_cmac_shared_data->lp_clock_freq = freq;
+}
+
+#if MYNEWT_VAL(CMAC_DEBUG_HOST_PRINT_ENABLE)
+static void
+cmac_host_print_trim(const char *name, const uint32_t *tv, unsigned len)
+{
+ console_printf("[CMAC] Trim values for '%s'\n", name);
+
+ while (len) {
+ console_printf(" 0x%08x = 0x%08x\n", (unsigned)tv[0], (unsigned)tv[1]);
+ len -= 2;
+ tv += 2;
+ }
+}
+#endif
+
+void
+cmac_host_init(void)
+{
+ struct trng_dev *trng;
+ struct cmac_image_info ii;
+ uint32_t cmac_rom_size;
+ uint32_t cmac_ram_size;
+#if !MYNEWT_VAL(CMAC_IMAGE_SINGLE)
+ const struct flash_area *fa;
+ int rc;
+#endif
+ struct cmac_trim *trim;
+
+ /* Get trng os device */
+ trng = (struct trng_dev *) os_dev_open("trng", OS_TIMEOUT_NEVER, NULL);
+ assert(trng);
+ g_cmac_host_rand_ev.ev_arg = trng;
+
+#if MYNEWT_VAL(CMAC_DEBUG_DIAG_ENABLE)
+ cmac_diag_setup_host();
+#endif
+
+#if MYNEWT_VAL(CMAC_DEBUG_SWD_ENABLE)
+ /* Enable CMAC debugger */
+ CRG_TOP->SYS_CTRL_REG |= 0x40; /* CRG_TOP_SYS_CTRL_REG_CMAC_DEBUGGER_ENABLE_Msk */
+#endif
+
+ /*
+ * Add PDC entry to wake up CMAC from M33
+ *
+ * XXX if MCU_DEBUG_GPIO_DEEP_SLEEP is enabled on CMAC, this should also
+ * enable PD_COM so CMAC can access GPIOs after wake up
+ */
+ g_cmac_host_pdc_sys2cmac = da1469x_pdc_add(MCU_PDC_TRIGGER_MAC_TIMER,
+ MCU_PDC_MASTER_CMAC,
+ MCU_PDC_EN_XTAL);
+ da1469x_pdc_set(g_cmac_host_pdc_sys2cmac);
+ da1469x_pdc_ack(g_cmac_host_pdc_sys2cmac);
+
+ /* Add PDC entry to wake up M33 from CMAC, if does not exist yet */
+ g_cmac_host_pdc_cmac2sys = da1469x_pdc_find(MCU_PDC_TRIGGER_COMBO,
+ MCU_PDC_MASTER_M33, 0);
+ if (g_cmac_host_pdc_cmac2sys < 0) {
+ g_cmac_host_pdc_cmac2sys = da1469x_pdc_add(MCU_PDC_TRIGGER_COMBO,
+ MCU_PDC_MASTER_M33,
+ MCU_PDC_EN_XTAL);
+ da1469x_pdc_set(g_cmac_host_pdc_cmac2sys);
+ da1469x_pdc_ack(g_cmac_host_pdc_cmac2sys);
+ }
+
+ /* Setup CMAC2SYS interrupt */
+ NVIC_SetVector(CMAC2SYS_IRQn, (uint32_t)cmac2sys_isr);
+ NVIC_SetPriority(CMAC2SYS_IRQn, 0);
+ NVIC_DisableIRQ(CMAC2SYS_IRQn);
+
+ /* Enable Radio LDO */
+ CRG_TOP->POWER_CTRL_REG |= CRG_TOP_POWER_CTRL_REG_LDO_RADIO_ENABLE_Msk;
+
+ /* Enable CMAC, but keep it in reset */
+ CRG_TOP->CLK_RADIO_REG = (1 << CRG_TOP_CLK_RADIO_REG_RFCU_ENABLE_Pos) |
+ (1 << CRG_TOP_CLK_RADIO_REG_CMAC_SYNCH_RESET_Pos) |
+ (0 << CRG_TOP_CLK_RADIO_REG_CMAC_CLK_SEL_Pos) |
+ (1 << CRG_TOP_CLK_RADIO_REG_CMAC_CLK_ENABLE_Pos) |
+ (0 << CRG_TOP_CLK_RADIO_REG_CMAC_DIV_Pos);
+
+ /* Calculate size of ROM and RAM area */
+ cmac_rom_size = &_binary_cmac_rom_bin_end - &_binary_cmac_rom_bin_start[0];
+ cmac_ram_size = &_binary_cmac_ram_bin_end - &_binary_cmac_ram_bin_start[0];
+
+ /* Load image header and check if image can be loaded */
+#if MYNEWT_VAL(CMAC_IMAGE_SINGLE)
+ memcpy(&ii, &_binary_cmac_rom_bin_start[128], sizeof(ii));
+#else
+ rc = flash_area_open(FLASH_AREA_IMAGE_1, &fa);
+ assert(rc == 0);
+ rc = flash_area_read(fa, 128, &ii, sizeof(ii));
+ assert(rc == 0);
+#endif
+
+ assert(ii.magic == 0xC3ACC3AC);
+ assert(ii.size_rom == cmac_rom_size);
+ assert(ii.size_ram <= cmac_ram_size);
+
+ /* Copy CMAC image to RAM */
+#if MYNEWT_VAL(CMAC_IMAGE_SINGLE)
+ memset(&_binary_cmac_ram_bin_start, 0xaa, cmac_ram_size);
+ memcpy(&_binary_cmac_ram_bin_start, &_binary_cmac_rom_bin_start, ii.size_rom);
+#else
+ memset(&_binary_cmac_ram_bin_start, 0xaa, cmac_ram_size);
+ rc = flash_area_read(fa, 0, &_binary_cmac_ram_bin_start, ii.size_rom);
+ assert(rc == 0);
+#endif
+
+ /* Setup CMAC memory addresses */
+ MEMCTRL->CMI_CODE_BASE_REG = (uint32_t)&_binary_cmac_ram_bin_start;
+ MEMCTRL->CMI_DATA_BASE_REG = MEMCTRL->CMI_CODE_BASE_REG + ii.offset_data;
+ MEMCTRL->CMI_SHARED_BASE_REG = MEMCTRL->CMI_CODE_BASE_REG + ii.offset_shared;
+ MEMCTRL->CMI_END_REG = MEMCTRL->CMI_CODE_BASE_REG + ii.size_ram - 1;
+
+ /* Initialize shared memory */
+ cmac_shared_init();
+
+ trim = (struct cmac_trim *)&g_cmac_shared_data->trim;
+ trim->rfcu_len = da1469x_trimv_group_read(6, trim->rfcu, ARRAY_SIZE(trim->rfcu));
+ trim->rfcu_mode1_len = da1469x_trimv_group_read(8, trim->rfcu_mode1, ARRAY_SIZE(trim->rfcu_mode1));
+ trim->rfcu_mode2_len = da1469x_trimv_group_read(10, trim->rfcu_mode2, ARRAY_SIZE(trim->rfcu_mode2));
+ trim->synth_len = da1469x_trimv_group_read(7, trim->synth, ARRAY_SIZE(trim->synth));
+
+#if MYNEWT_VAL(CMAC_DEBUG_HOST_PRINT_ENABLE)
+ cmac_host_print_trim("rfcu", trim->rfcu, trim->rfcu_len);
+ cmac_host_print_trim("rfcu_mode1", trim->rfcu_mode1, trim->rfcu_mode1_len);
+ cmac_host_print_trim("rfcu_mode2", trim->rfcu_mode2, trim->rfcu_mode2_len);
+ cmac_host_print_trim("synth", trim->synth, trim->synth_len);
+#endif
+
+ /* Release CMAC from reset and sync */
+ CRG_TOP->CLK_RADIO_REG &= ~CRG_TOP_CLK_RADIO_REG_CMAC_SYNCH_RESET_Msk;
+ cmac_shared_sync();
+
+ da1469x_lpclk_register_cmac_cb(cmac_host_lpclk_cb);
+
+#if MYNEWT_VAL(CMAC_DEBUG_HOST_PRINT_ENABLE) && MYNEWT_VAL(CMAC_DEBUG_DATA_ENABLE)
+ /* Trim values are calculated on RF init, so are valid after synced with CMAC */
+ console_printf("[CMAC] Calculated trim_val1: 1=0x%08x 2=0x%08x\n",
+ (unsigned)g_cmac_shared_data->debug.trim_val1_tx_1,
+ (unsigned)g_cmac_shared_data->debug.trim_val1_tx_2);
+ console_printf("[CMAC] Calculated trim_val2: tx=0x%08x rx=0x%08x\n",
+ (unsigned)g_cmac_shared_data->debug.trim_val2_tx,
+ (unsigned)g_cmac_shared_data->debug.trim_val2_rx);
+#endif
+}
diff --git a/nimble/transport/dialog_cmac/cmac_driver/src/cmac_mbox.c b/nimble/transport/dialog_cmac/cmac_driver/src/cmac_mbox.c
new file mode 100644
index 0000000..9594dbe
--- /dev/null
+++ b/nimble/transport/dialog_cmac/cmac_driver/src/cmac_mbox.c
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <cmac_driver/cmac_shared.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include "syscfg/syscfg.h"
+#include "mcu/mcu.h"
+#include "os/os_arch.h"
+#include "os/os.h"
+
+#ifndef min
+#define min(_a, _b) ((_a) < (_b) ? (_a) : (_b))
+#endif
+
+static cmac_mbox_read_cb *g_cmac_mbox_read_cb;
+static cmac_mbox_write_notif_cb *g_cmac_mbox_write_notif_cb;
+
+void
+cmac_mbox_set_read_cb(cmac_mbox_read_cb *cb)
+{
+ g_cmac_mbox_read_cb = cb;
+}
+
+void
+cmac_mbox_set_write_notif_cb(cmac_mbox_write_notif_cb *cb)
+{
+ g_cmac_mbox_write_notif_cb = cb;
+}
+
+int
+cmac_mbox_read(void)
+{
+#if MYNEWT_VAL(BLE_HOST)
+ volatile struct cmac_mbox *mbox = &g_cmac_shared_data->mbox_c2s;
+ uint8_t *mbox_buf = (uint8_t *)&g_cmac_shared_data->mbox_c2s_buf;
+ const uint16_t mbox_size = MYNEWT_VAL(CMAC_MBOX_SIZE_C2S);
+#else
+ volatile struct cmac_mbox *mbox = &g_cmac_shared_data.mbox_s2c;
+ uint8_t *mbox_buf = (uint8_t *)&g_cmac_shared_data.mbox_s2c_buf;
+ const uint16_t mbox_size = MYNEWT_VAL(CMAC_MBOX_SIZE_S2C);
+#endif
+ uint16_t rd_off;
+ uint16_t wr_off;
+ uint16_t chunk;
+ int len = 0;
+
+ if (!g_cmac_mbox_read_cb) {
+ return 0;
+ }
+
+ do {
+ rd_off = mbox->rd_off;
+ wr_off = mbox->wr_off;
+
+ if (rd_off <= wr_off) {
+ chunk = wr_off - rd_off;
+ } else {
+ chunk = mbox_size - rd_off;
+ }
+
+ while (chunk) {
+ len = g_cmac_mbox_read_cb(&mbox_buf[rd_off], chunk);
+ if (len < 0) {
+ break;
+ }
+
+ rd_off += len;
+ chunk -= len;
+ }
+
+ mbox->rd_off = rd_off == mbox_size ? 0 : rd_off;
+ } while ((mbox->rd_off != mbox->wr_off) && (len >= 0));
+
+ return 0;
+}
+
+int
+cmac_mbox_write(const void *data, uint16_t len)
+{
+#if MYNEWT_VAL(BLE_HOST)
+ volatile struct cmac_mbox *mbox = &g_cmac_shared_data->mbox_s2c;
+ uint8_t *mbox_buf = (uint8_t *)&g_cmac_shared_data->mbox_s2c_buf;
+ const uint16_t mbox_size = MYNEWT_VAL(CMAC_MBOX_SIZE_S2C);
+#else
+ volatile struct cmac_mbox *mbox = &g_cmac_shared_data.mbox_c2s;
+ uint8_t *mbox_buf = (uint8_t *)&g_cmac_shared_data.mbox_c2s_buf;
+ const uint16_t mbox_size = MYNEWT_VAL(CMAC_MBOX_SIZE_C2S);
+#endif
+ uint16_t rd_off;
+ uint16_t wr_off;
+ uint16_t max_wr;
+ uint16_t chunk;
+
+ while (len) {
+ rd_off = mbox->rd_off;
+ wr_off = mbox->wr_off;
+
+ /*
+ * Calculate maximum length to write, i.e. up to end of buffer or stop
+ * before rd_off to be able to detect full queue.
+ */
+ if (rd_off > wr_off) {
+ /*
+ * |0|1|2|3|4|5|6|7|
+ * | | | |W| | |R| |
+ * `---^
+ */
+ max_wr = rd_off - wr_off - 1;
+ } else if (rd_off == 0) {
+ /*
+ * |0|1|2|3|4|5|6|7|
+ * |R| | |W| | | | |
+ * `-------^
+ */
+ max_wr = mbox_size - wr_off - 1;
+ } else {
+ /*
+ * |0|1|2|3|4|5|6|7|
+ * | |R| |W| | | | |
+ * `---------^
+ */
+ max_wr = mbox_size - wr_off;
+ }
+
+ chunk = min(len, max_wr);
+
+ if (chunk == 0) {
+ continue;
+ }
+
+ memcpy(&mbox_buf[wr_off], data, chunk);
+
+ wr_off += chunk;
+ mbox->wr_off = wr_off == mbox_size ? 0 : wr_off;
+
+ g_cmac_mbox_write_notif_cb();
+
+ len -= chunk;
+ data = (uint8_t *)data + chunk;
+ }
+
+ return 0;
+}
diff --git a/nimble/transport/dialog_cmac/cmac_driver/src/cmac_rand.c b/nimble/transport/dialog_cmac/cmac_driver/src/cmac_rand.c
new file mode 100644
index 0000000..67a315f
--- /dev/null
+++ b/nimble/transport/dialog_cmac/cmac_driver/src/cmac_rand.c
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <cmac_driver/cmac_shared.h>
+#include <string.h>
+#include <stdbool.h>
+#include "syscfg/syscfg.h"
+#include "mcu/mcu.h"
+#include "os/os_arch.h"
+#include "os/os.h"
+
+#if MYNEWT_VAL(BLE_HOST)
+int
+cmac_rand_is_active(void)
+{
+ return g_cmac_shared_data->rand.cmr_active;
+}
+
+int
+cmac_rand_is_full(void)
+{
+ int next;
+ bool rc;
+
+ next = cmac_rand_get_next();
+ if (next == g_cmac_shared_data->rand.cmr_out) {
+ rc = 1;
+ } else {
+ rc = 0;
+ }
+ return rc;
+}
+
+int
+cmac_rand_get_next(void)
+{
+ int next;
+
+ /* If active and not full, put event on queue to get random numbers */
+ next = g_cmac_shared_data->rand.cmr_in + 1;
+ if (next == CMAC_RAND_BUF_ELEMS) {
+ next = 0;
+ }
+ return next;
+}
+
+void
+cmac_rand_fill(uint32_t *buf, int num_words)
+{
+ int next;
+
+ /* XXX: if words is 0, is it possible we could get into a state
+ where we are waiting for random numbers but M33 does not know it
+ has to fill any? */
+
+ /* NOTE: we already know the buffer is not full first time through */
+ next = g_cmac_shared_data->rand.cmr_in;
+ while (num_words) {
+ g_cmac_shared_data->rand.cmr_buf[next] = buf[0];
+ next = cmac_rand_get_next();
+ g_cmac_shared_data->rand.cmr_in = next;
+ next = cmac_rand_get_next();
+ if (next == g_cmac_shared_data->rand.cmr_out) {
+ break;
+ }
+ --num_words;
+ ++buf;
+ }
+}
+#endif
+
+#if MYNEWT_VAL(BLE_CONTROLLER)
+static cmac_rand_isr_cb_t g_cmac_rand_isr_cb;
+
+void
+cmac_rand_set_isr_cb(cmac_rand_isr_cb_t cb)
+{
+ g_cmac_rand_isr_cb = cb;
+}
+
+void
+cmac_rand_start(void)
+{
+ g_cmac_shared_data.rand.cmr_active = 1;
+}
+
+void
+cmac_rand_stop(void)
+{
+ g_cmac_shared_data.rand.cmr_active = 0;
+}
+
+/**
+ * cmac rnum read
+ *
+ * Called during the system to cmac isr to take random numbers
+ * from shared memory into the BLE stack.
+ */
+void
+cmac_rand_read(void)
+{
+ uint8_t bytes_left;
+ uint32_t rnum;
+
+ /* Just leave if no callback. */
+ if (g_cmac_rand_isr_cb == NULL) {
+ return;
+ }
+
+ bytes_left = 0;
+ while (g_cmac_shared_data.rand.cmr_active) {
+ if (bytes_left) {
+ --bytes_left;
+ rnum >>= 8;
+ } else if (g_cmac_shared_data.rand.cmr_out != g_cmac_shared_data.rand.cmr_in) {
+ bytes_left = 3;
+ rnum = g_cmac_shared_data.rand.cmr_buf[g_cmac_shared_data.rand.cmr_out];
+ ++g_cmac_shared_data.rand.cmr_out;
+ if (g_cmac_shared_data.rand.cmr_out == CMAC_RAND_BUF_ELEMS) {
+ g_cmac_shared_data.rand.cmr_out = 0;
+ }
+ } else {
+ break;
+ }
+ (*g_cmac_rand_isr_cb)((uint8_t)rnum);
+ }
+}
+#endif
diff --git a/nimble/transport/dialog_cmac/cmac_driver/src/cmac_shared.c b/nimble/transport/dialog_cmac/cmac_driver/src/cmac_shared.c
new file mode 100644
index 0000000..24640ca
--- /dev/null
+++ b/nimble/transport/dialog_cmac/cmac_driver/src/cmac_shared.c
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "syscfg/syscfg.h"
+#if MYNEWT_VAL(BLE_CONTROLLER) && !MYNEWT_VAL(MCU_DEBUG_DSER_CMAC_SHARED)
+#define MCU_DIAG_SER_DISABLE
+#endif
+
+#include <assert.h>
+#include <string.h>
+#include <stdbool.h>
+#include "mcu/mcu.h"
+#include <cmac_driver/cmac_shared.h>
+#include "os/os_arch.h"
+#include "os/os.h"
+
+#ifndef min
+#define min(_a, _b) ((_a) < (_b) ? (_a) : (_b))
+#endif
+
+#if MYNEWT_VAL(BLE_HOST)
+volatile struct cmac_shared_data *g_cmac_shared_data;
+#include "mcu/da1469x_clock.h"
+#define MCU_DIAG_SER(_x)
+#elif MYNEWT_VAL(BLE_CONTROLLER)
+volatile struct cmac_shared_data g_cmac_shared_data __attribute__((section(".shdata")));
+#endif
+
+void
+cmac_shared_init(void)
+{
+#if MYNEWT_VAL(BLE_HOST)
+ g_cmac_shared_data = (void *)(MCU_MEM_SYSRAM_START_ADDRESS +
+ MEMCTRL->CMI_SHARED_BASE_REG);
+
+ memset((void *)g_cmac_shared_data, 0, sizeof(*g_cmac_shared_data));
+
+ g_cmac_shared_data->xtal32m_settle_us = MYNEWT_VAL(MCU_CLOCK_XTAL32M_SETTLE_TIME_US);
+
+ g_cmac_shared_data->dcdc.enabled = DCDC->DCDC_CTRL1_REG & DCDC_DCDC_CTRL1_REG_DCDC_ENABLE_Msk;
+ if (g_cmac_shared_data->dcdc.enabled) {
+ g_cmac_shared_data->dcdc.v18 = DCDC->DCDC_V18_REG;
+ g_cmac_shared_data->dcdc.v18p = DCDC->DCDC_V18P_REG;
+ g_cmac_shared_data->dcdc.vdd = DCDC->DCDC_VDD_REG;
+ g_cmac_shared_data->dcdc.v14 = DCDC->DCDC_V14_REG;
+ g_cmac_shared_data->dcdc.ctrl1 = DCDC->DCDC_CTRL1_REG;
+ }
+
+#if MYNEWT_VAL(CMAC_DEBUG_DATA_ENABLE)
+ g_cmac_shared_data->debug.tx_power_override = INT8_MAX;
+#endif
+#endif
+}
+
+
+void
+cmac_shared_sync(void)
+{
+ /*
+ * We need to guarantee proper order of initialization here, i.e. SYS has
+ * to wait until CMAC finished initialization as otherwise host may start
+ * sending HCI packets which will timeout as there is no one to read them.
+ */
+#if MYNEWT_VAL(BLE_HOST)
+ assert(g_cmac_shared_data->magic_sys == 0);
+
+ while (g_cmac_shared_data->magic_cmac != CMAC_SHARED_MAGIC_CMAC);
+ g_cmac_shared_data->magic_sys = CMAC_SHARED_MAGIC_SYS;
+
+ NVIC_EnableIRQ(CMAC2SYS_IRQn);
+#endif
+
+#if MYNEWT_VAL(BLE_CONTROLLER)
+ assert(g_cmac_shared_data.magic_cmac == 0);
+
+ g_cmac_shared_data.magic_cmac = CMAC_SHARED_MAGIC_CMAC;
+ while (g_cmac_shared_data.magic_sys != CMAC_SHARED_MAGIC_SYS);
+
+ NVIC_SetPriority(SYS2CMAC_IRQn, 3);
+ NVIC_EnableIRQ(SYS2CMAC_IRQn);
+#endif
+}
diff --git a/nimble/transport/dialog_cmac/cmac_driver/syscfg.yml b/nimble/transport/dialog_cmac/cmac_driver/syscfg.yml
new file mode 100644
index 0000000..e3f3a2d
--- /dev/null
+++ b/nimble/transport/dialog_cmac/cmac_driver/syscfg.yml
@@ -0,0 +1,98 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+syscfg.defs:
+ CMAC_MBOX_SIZE_S2C:
+ description: >
+ Size of mailbox for SYS to CMAC data. The size
+ value should be power of 2 to allow for better
+ code optimization.
+ value: 128
+ CMAC_MBOX_SIZE_C2S:
+ description: >
+ Size of mailbox for CMAC to SYS data. The size
+ value should be power of 2 to allow for better
+ code optimization.
+ value: 128
+
+ CMAC_TRIM_SIZE_RFCU:
+ description: >
+ Size of trim values for RFCU. This is maximum
+ number of trim values that can be read from
+ OTP and applied, all excessive values will be
+ discarded.
+ value: 10
+ CMAC_TRIM_SIZE_SYNTH:
+ description: >
+ Size of trim values for RFCU. This is maximum
+ number of trim values that can be read from
+ OTP and applied, all excessive values will be
+ discarded.
+ value: 10
+
+ CMAC_DEBUG_SWD_ENABLE:
+ description: >
+ Enable CMAC SWD interface.
+ value: 0
+ CMAC_DEBUG_DIAG_ENABLE:
+ description: >
+ Enable CMAC diagnostic lines.
+ value: 0
+ CMAC_DEBUG_DATA_ENABLE:
+ description: >
+ Enable extra debugging data in shared segment.
+ value: 0
+ CMAC_DEBUG_COREDUMP_ENABLE:
+ description: >
+ Enable dumping CMAC registers to shared segment
+ on fault.
+ value: 1
+ CMAC_DEBUG_HOST_PRINT_ENABLE:
+ description: >
+ Enable some debug printouts to console from host side.
+ This will dump some settings during startup, useful to
+ check what is loaded to CMAC via shared data.
+ value: 0
+
+ CMAC_IMAGE_SINGLE:
+ description: >
+ When enable, CMAC binary is linked with application image
+ creating a single image build. See CMAC_IMAGE_TARGET_NAME.
+ When disabled, CMAC binary is built and flashed separately
+ to flash partition. See CMAC_IMAGE_PARTITION.
+ value: 1
+ CMAC_IMAGE_TARGET_NAME:
+ description: >
+ Target name to build for CMAC binary for single image build.
+ value: "@apache-mynewt-nimble/targets/dialog_cmac"
+ CMAC_IMAGE_PARTITION:
+ description: >
+ Flash partition to load CMAC binary from if single image build
+ is disabled.
+ value: FLASH_AREA_IMAGE_1
+ CMAC_IMAGE_RAM_SIZE:
+ description: >
+ Size of RAM area in bytes reserved for CMAC if single image
+ build is disabled. Unit suffix (K, M) is allowed.
+ Note: for single image build this setting is not applicable
+ since proper RAM area size is automatically calculated from
+ CMAC binary.
+ value: 128K
+
+syscfg.restrictions.BLE_HOST:
+ - TRNG
diff --git a/nimble/transport/pkg.yml b/nimble/transport/dialog_cmac/pkg.yml
similarity index 60%
copy from nimble/transport/pkg.yml
copy to nimble/transport/dialog_cmac/pkg.yml
index 3bfca88..ef8fc1e 100644
--- a/nimble/transport/pkg.yml
+++ b/nimble/transport/dialog_cmac/pkg.yml
@@ -6,7 +6,7 @@
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
@@ -17,32 +17,20 @@
# under the License.
#
-pkg.name: nimble/transport
-pkg.description: Meta-package for NimBLE HCI transport
+pkg.name: nimble/transport/dialog_cmac
+pkg.description: HCI H4 transport for Dialog CMAC
pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
pkg.homepage: "http://mynewt.apache.org/"
pkg.keywords:
- ble
- bluetooth
-pkg.deps.'BLE_HCI_TRANSPORT == "builtin"':
- - nimble/transport/ram
- - nimble/controller
-
-pkg.deps.'BLE_HCI_TRANSPORT == "emspi"':
- - nimble/transport/emspi
-
-pkg.deps.'BLE_HCI_TRANSPORT == "ram"':
- - nimble/transport/ram
-
-pkg.deps.'BLE_HCI_TRANSPORT == "socket"':
- - nimble/transport/socket
-
-pkg.deps.'BLE_HCI_TRANSPORT == "uart"':
- - nimble/transport/uart
+pkg.deps:
+ - nimble
+ - nimble/transport/dialog_cmac/cmac_driver
-pkg.deps.'BLE_HCI_TRANSPORT == "da1469x"':
- - nimble/transport/da1469x
+pkg.apis:
+ - ble_transport
-pkg.deps.'BLE_HCI_TRANSPORT == "usb"':
- - nimble/transport/usb
+pkg.init:
+ ble_hci_cmac_init: 100
diff --git a/nimble/transport/dialog_cmac/src/ble_hci_cmac_common.c b/nimble/transport/dialog_cmac/src/ble_hci_cmac_common.c
new file mode 100644
index 0000000..665b621
--- /dev/null
+++ b/nimble/transport/dialog_cmac/src/ble_hci_cmac_common.c
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <string.h>
+
+#include "syscfg/syscfg.h"
+#include "os/mynewt.h"
+#include "nimble/ble.h"
+#include "nimble/ble_hci_trans.h"
+#include "nimble/hci_common.h"
+#include "ble_hci_cmac_priv.h"
+
+/*
+ * If controller-to-host flow control is enabled we need to hold an extra command
+ * buffer for HCI_Host_Number_Of_Completed_Packets which can be sent at any time.
+ */
+#if MYNEWT_VAL(BLE_HS_FLOW_CTRL) || MYNEWT_VAL(BLE_LL_CFG_FEAT_CTRL_TO_HOST_FLOW_CONTROL)
+#define HCI_CMD_COUNT 2
+#else
+#define HCI_CMD_COUNT 1
+#endif
+
+#define POOL_ACL_BLOCK_SIZE OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) + \
+ BLE_MBUF_MEMBLOCK_OVERHEAD + \
+ BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT)
+
+static uint8_t ble_hci_pool_cmd_mempool_buf[
+ OS_MEMPOOL_BYTES(HCI_CMD_COUNT, BLE_HCI_TRANS_CMD_SZ)];
+static struct os_mempool ble_hci_pool_cmd_mempool;
+
+static uint8_t ble_hci_pool_evt_hi_mempool_buf[
+ OS_MEMPOOL_BYTES(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
+ MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))];
+static struct os_mempool ble_hci_pool_evt_hi_mempool;
+
+static uint8_t ble_hci_pool_evt_lo_mempool_buf[
+ OS_MEMPOOL_BYTES(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
+ MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))];
+static struct os_mempool ble_hci_pool_evt_lo_mempool;
+
+static uint8_t ble_hci_pool_acl_mempool_buf[
+ OS_MEMPOOL_BYTES(MYNEWT_VAL(BLE_ACL_BUF_COUNT),
+ POOL_ACL_BLOCK_SIZE)];
+static struct os_mempool_ext ble_hci_pool_acl_mempool;
+static struct os_mbuf_pool ble_hci_pool_acl_mbuf_pool;
+
+__attribute__((weak)) void ble_hci_trans_notify_free(void);
+
+static os_mempool_put_fn *g_ble_hci_pool_acl_mempool_put_cb;
+static void *g_ble_hci_pool_acl_mempool_put_arg;
+
+int
+ble_hci_trans_reset(void)
+{
+ return 0;
+}
+
+uint8_t *
+ble_hci_trans_buf_alloc(int type)
+{
+ uint8_t *buf;
+
+ switch (type) {
+ case BLE_HCI_TRANS_BUF_CMD:
+ buf = os_memblock_get(&ble_hci_pool_cmd_mempool);
+ break;
+ case BLE_HCI_TRANS_BUF_EVT_HI:
+ buf = os_memblock_get(&ble_hci_pool_evt_hi_mempool);
+ if (buf) {
+ break;
+ }
+ /* no break */
+ case BLE_HCI_TRANS_BUF_EVT_LO:
+ buf = os_memblock_get(&ble_hci_pool_evt_lo_mempool);
+ break;
+ default:
+ assert(0);
+ buf = NULL;
+ }
+
+ return buf;
+}
+
+void
+ble_hci_trans_buf_free(uint8_t *buf)
+{
+ int rc;
+
+ if (os_memblock_from(&ble_hci_pool_cmd_mempool, buf)) {
+ rc = os_memblock_put(&ble_hci_pool_cmd_mempool, buf);
+ assert(rc == 0);
+ } else if (os_memblock_from(&ble_hci_pool_evt_hi_mempool, buf)) {
+ rc = os_memblock_put(&ble_hci_pool_evt_hi_mempool, buf);
+ assert(rc == 0);
+ } else {
+ assert(os_memblock_from(&ble_hci_pool_evt_lo_mempool, buf));
+ rc = os_memblock_put(&ble_hci_pool_evt_lo_mempool, buf);
+ assert(rc == 0);
+ }
+
+ ble_hci_trans_notify_free();
+}
+
+struct os_mbuf *
+ble_hci_cmac_alloc_acl_mbuf(void)
+{
+ return os_mbuf_get_pkthdr(&ble_hci_pool_acl_mbuf_pool,
+ sizeof(struct ble_mbuf_hdr));
+}
+
+static os_error_t
+ble_hci_cmac_free_acl_cb(struct os_mempool_ext *mpe, void *data, void *arg)
+{
+ int rc;
+
+ if (g_ble_hci_pool_acl_mempool_put_cb) {
+ rc = g_ble_hci_pool_acl_mempool_put_cb(mpe, data,
+ g_ble_hci_pool_acl_mempool_put_arg);
+ } else {
+ rc = os_memblock_put_from_cb(&mpe->mpe_mp, data);
+ }
+
+ if (rc != 0) {
+ return rc;
+ }
+
+ ble_hci_trans_notify_free();
+
+ return 0;
+}
+
+
+int
+ble_hci_trans_set_acl_free_cb(os_mempool_put_fn *cb, void *arg)
+{
+ g_ble_hci_pool_acl_mempool_put_cb = cb;
+ g_ble_hci_pool_acl_mempool_put_arg = arg;
+
+ return 0;
+}
+
+void
+ble_hci_cmac_init(void)
+{
+ int rc;
+
+ SYSINIT_ASSERT_ACTIVE();
+
+ rc = os_mempool_init(&ble_hci_pool_cmd_mempool,
+ HCI_CMD_COUNT, BLE_HCI_TRANS_CMD_SZ,
+ ble_hci_pool_cmd_mempool_buf, "ble_hci_cmd");
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ rc = os_mempool_init(&ble_hci_pool_evt_hi_mempool,
+ MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
+ MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE),
+ ble_hci_pool_evt_hi_mempool_buf, "ble_hci_evt_hi");
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ rc = os_mempool_init(&ble_hci_pool_evt_lo_mempool,
+ MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
+ MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE),
+ ble_hci_pool_evt_lo_mempool_buf, "ble_hci_evt_lo");
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ rc = os_mempool_ext_init(&ble_hci_pool_acl_mempool,
+ MYNEWT_VAL(BLE_ACL_BUF_COUNT), POOL_ACL_BLOCK_SIZE,
+ ble_hci_pool_acl_mempool_buf, "ble_hci_acl");
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ rc = os_mbuf_pool_init(&ble_hci_pool_acl_mbuf_pool,
+ &ble_hci_pool_acl_mempool.mpe_mp, POOL_ACL_BLOCK_SIZE,
+ MYNEWT_VAL(BLE_ACL_BUF_COUNT));
+ SYSINIT_PANIC_ASSERT(rc == 0);
+
+ ble_hci_pool_acl_mempool.mpe_put_cb = ble_hci_cmac_free_acl_cb;
+}
diff --git a/nimble/transport/dialog_cmac/src/ble_hci_cmac_hs.c b/nimble/transport/dialog_cmac/src/ble_hci_cmac_hs.c
new file mode 100644
index 0000000..1164fe7
--- /dev/null
+++ b/nimble/transport/dialog_cmac/src/ble_hci_cmac_hs.c
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <string.h>
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(BLE_HOST)
+
+#include "cmac_driver/cmac_shared.h"
+#include "cmac_driver/cmac_host.h"
+#include "nimble/ble_hci_trans.h"
+#include "os/os_mbuf.h"
+#include "ble_hci_trans_h4.h"
+#include "ble_hci_cmac_priv.h"
+
+struct ble_hci_cmac_hs_api {
+ ble_hci_trans_rx_cmd_fn *evt_cb;
+ void *evt_arg;
+ ble_hci_trans_rx_acl_fn *acl_cb;
+ void *acl_arg;
+};
+
+static struct ble_hci_cmac_hs_api g_ble_hci_cmac_hs_api;
+static struct ble_hci_trans_h4_rx_state g_ble_hci_cmac_hs_rx_state;
+static bool g_ble_hci_cmac_hs_read_err;
+
+static int
+ble_hci_cmac_hs_frame_cb(uint8_t pkt_type, void *data)
+{
+ int rc;
+
+ switch (pkt_type) {
+ case BLE_HCI_TRANS_H4_PKT_TYPE_ACL:
+ rc = g_ble_hci_cmac_hs_api.acl_cb(data, g_ble_hci_cmac_hs_api.acl_arg);
+ break;
+ case BLE_HCI_TRANS_H4_PKT_TYPE_EVT:
+ rc = g_ble_hci_cmac_hs_api.evt_cb(data, g_ble_hci_cmac_hs_api.evt_arg);
+ break;
+ default:
+ assert(0);
+ break;
+ }
+
+ return rc;
+}
+
+static int
+ble_hci_cmac_hs_mbox_read_cb(const void *data, uint16_t len)
+{
+ int rlen;
+ os_sr_t sr;
+
+ rlen = ble_hci_trans_h4_rx(&g_ble_hci_cmac_hs_rx_state, data, len,
+ ble_hci_cmac_hs_frame_cb);
+ if (rlen < 0) {
+ /*
+ * There was oom error, we need to wait for buffer to be freed and
+ * trigger another read.
+ */
+ OS_ENTER_CRITICAL(sr);
+ g_ble_hci_cmac_hs_read_err = true;
+ OS_EXIT_CRITICAL(sr);
+ }
+
+ return rlen;
+}
+
+static void
+ble_hci_cmac_hs_mbox_write_notif_cb(void)
+{
+ cmac_host_signal2cmac();
+}
+
+int
+ble_hci_trans_hs_cmd_tx(uint8_t *cmd)
+{
+ uint8_t pkt_type = BLE_HCI_TRANS_H4_PKT_TYPE_CMD;
+
+ cmac_mbox_write(&pkt_type, sizeof(pkt_type));
+ cmac_mbox_write(cmd, cmd[2] + 3);
+
+ ble_hci_trans_buf_free(cmd);
+
+ return 0;
+}
+
+int
+ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
+{
+ uint8_t pkt_type = BLE_HCI_TRANS_H4_PKT_TYPE_ACL;
+ struct os_mbuf *om_next;
+
+ cmac_mbox_write(&pkt_type, sizeof(pkt_type));
+
+ while (om) {
+ om_next = SLIST_NEXT(om, om_next);
+
+ cmac_mbox_write(om->om_data, om->om_len);
+
+ os_mbuf_free(om);
+ om = om_next;
+ }
+
+ return 0;
+}
+
+void
+ble_hci_trans_notify_free(void)
+{
+ os_sr_t sr;
+
+ OS_ENTER_CRITICAL(sr);
+ if (g_ble_hci_cmac_hs_read_err) {
+ g_ble_hci_cmac_hs_read_err = false;
+ /* Just trigger an interrupt, it will trigger read */
+ NVIC_SetPendingIRQ(CMAC2SYS_IRQn);
+ }
+ OS_EXIT_CRITICAL(sr);
+}
+
+void
+ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn *evt_cb, void *evt_arg,
+ ble_hci_trans_rx_acl_fn *acl_cb, void *acl_arg)
+{
+ g_ble_hci_cmac_hs_api.evt_cb = evt_cb;
+ g_ble_hci_cmac_hs_api.evt_arg = evt_arg;
+ g_ble_hci_cmac_hs_api.acl_cb = acl_cb;
+ g_ble_hci_cmac_hs_api.acl_arg = acl_arg;
+
+ /* We can now handle data from CMAC, initialize it */
+ cmac_mbox_set_read_cb(ble_hci_cmac_hs_mbox_read_cb);
+ cmac_mbox_set_write_notif_cb(ble_hci_cmac_hs_mbox_write_notif_cb);
+ cmac_host_init();
+}
+
+#endif
diff --git a/nimble/transport/dialog_cmac/src/ble_hci_cmac_ll.c b/nimble/transport/dialog_cmac/src/ble_hci_cmac_ll.c
new file mode 100644
index 0000000..3531529
--- /dev/null
+++ b/nimble/transport/dialog_cmac/src/ble_hci_cmac_ll.c
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(BLE_CONTROLLER)
+
+#if !MYNEWT_VAL(MCU_DEBUG_DSER_BLE_HCI_CMAC_LL)
+#define MCU_DIAG_SER_DISABLE
+#endif
+
+#include <assert.h>
+#include <string.h>
+#include "mcu/mcu.h"
+#include "cmac_driver/cmac_shared.h"
+#include "nimble/ble_hci_trans.h"
+#include "os/os_mbuf.h"
+#include "ble_hci_trans_h4.h"
+#include "ble_hci_cmac_priv.h"
+
+struct ble_hci_cmac_ll_api {
+ ble_hci_trans_rx_cmd_fn *cmd_cb;
+ void *cmd_arg;
+ ble_hci_trans_rx_acl_fn *acl_cb;
+ void *acl_arg;
+};
+
+static struct ble_hci_cmac_ll_api g_ble_hci_cmac_ll_api;
+static struct ble_hci_trans_h4_rx_state g_ble_hci_cmac_ll_rx_state;
+
+static int
+ble_hci_cmac_ll_frame_cb(uint8_t pkt_type, void *data)
+{
+ int rc;
+
+ MCU_DIAG_SER('F');
+
+ switch (pkt_type) {
+ case BLE_HCI_TRANS_H4_PKT_TYPE_CMD:
+ rc = g_ble_hci_cmac_ll_api.cmd_cb(data, g_ble_hci_cmac_ll_api.cmd_arg);
+ break;
+ case BLE_HCI_TRANS_H4_PKT_TYPE_ACL:
+ rc = g_ble_hci_cmac_ll_api.acl_cb(data, g_ble_hci_cmac_ll_api.acl_arg);
+ break;
+ default:
+ assert(0);
+ break;
+ }
+
+ return rc;
+}
+
+static int
+ble_hci_cmac_ll_mbox_read_cb(const void *data, uint16_t len)
+{
+ int rlen;
+
+ MCU_DIAG_SER('R');
+ rlen = ble_hci_trans_h4_rx(&g_ble_hci_cmac_ll_rx_state, data, len,
+ ble_hci_cmac_ll_frame_cb);
+
+ /* There should be no oom on LL side due to flow control used */
+ assert(rlen >= 0);
+
+ return rlen;
+}
+
+static void
+ble_hci_cmac_ll_mbox_write_notif_cb(void)
+{
+ MCU_DIAG_SER('W');
+ CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV1C_CMAC2SYS_IRQ_SET_Msk;
+}
+
+int
+ble_hci_trans_ll_evt_tx(uint8_t *evt)
+{
+ uint8_t pkt_type = BLE_HCI_TRANS_H4_PKT_TYPE_EVT;
+
+ cmac_mbox_write(&pkt_type, sizeof(pkt_type));
+ cmac_mbox_write(evt, evt[1] + 2);
+
+ ble_hci_trans_buf_free(evt);
+
+ return 0;
+}
+
+int
+ble_hci_trans_ll_acl_tx(struct os_mbuf *om)
+{
+ uint8_t pkt_type = BLE_HCI_TRANS_H4_PKT_TYPE_ACL;
+ struct os_mbuf *om_next;
+
+ cmac_mbox_write(&pkt_type, sizeof(pkt_type));
+
+ while (om) {
+ om_next = SLIST_NEXT(om, om_next);
+
+ cmac_mbox_write(om->om_data, om->om_len);
+
+ os_mbuf_free(om);
+ om = om_next;
+ }
+
+ return 0;
+}
+
+void
+ble_hci_trans_cfg_ll(ble_hci_trans_rx_cmd_fn *cmd_cb, void *cmd_arg,
+ ble_hci_trans_rx_acl_fn *acl_cb, void *acl_arg)
+{
+ g_ble_hci_cmac_ll_api.cmd_cb = cmd_cb;
+ g_ble_hci_cmac_ll_api.cmd_arg = cmd_arg;
+ g_ble_hci_cmac_ll_api.acl_cb = acl_cb;
+ g_ble_hci_cmac_ll_api.acl_arg = acl_arg;
+
+ /* Setup callbacks for mailboxes */
+ cmac_mbox_set_read_cb(ble_hci_cmac_ll_mbox_read_cb);
+ cmac_mbox_set_write_notif_cb(ble_hci_cmac_ll_mbox_write_notif_cb);
+
+ /* Synchronize with SYS */
+ cmac_shared_sync();
+}
+
+#endif
diff --git a/nimble/transport/dialog_cmac/src/ble_hci_cmac_priv.h b/nimble/transport/dialog_cmac/src/ble_hci_cmac_priv.h
new file mode 100644
index 0000000..451053c
--- /dev/null
+++ b/nimble/transport/dialog_cmac/src/ble_hci_cmac_priv.h
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef _BLE_HCI_CMAC_PRIV_H_
+#define _BLE_HCI_CMAC_PRIV_H_
+
+#include "os/os_mbuf.h"
+
+struct os_mbuf *ble_hci_cmac_alloc_acl_mbuf(void);
+
+void ble_hci_trans_notify_free(void);
+
+#endif /* _BLE_HCI_CMAC_PRIV_H_ */
diff --git a/nimble/transport/dialog_cmac/src/ble_hci_trans_h4.c b/nimble/transport/dialog_cmac/src/ble_hci_trans_h4.c
new file mode 100644
index 0000000..74da391
--- /dev/null
+++ b/nimble/transport/dialog_cmac/src/ble_hci_trans_h4.c
@@ -0,0 +1,332 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <stdbool.h>
+#include <string.h>
+#include "syscfg/syscfg.h"
+#include "os/os_mbuf.h"
+#include "nimble/ble_hci_trans.h"
+#include "nimble/hci_common.h"
+#include "ble_hci_trans_h4.h"
+#include "ble_hci_cmac_priv.h"
+
+#define RXS_STATE_W4_PKT_TYPE 0
+#define RXS_STATE_W4_HEADER 1
+#define RXS_STATE_W4_PAYLOAD 2
+#define RXS_STATE_COMPLETED 3
+
+struct input_buffer {
+ const uint8_t *buf;
+ uint16_t len;
+};
+
+static int
+ble_hci_trans_h4_ib_adjust(struct input_buffer *ib, uint16_t len)
+{
+ assert(ib->len >= len);
+
+ ib->buf += len;
+ ib->len -= len;
+
+ return len;
+}
+
+static void
+ble_hci_trans_h4_rxs_start(struct ble_hci_trans_h4_rx_state *rxs, uint8_t pkt_type)
+{
+ rxs->pkt_type = pkt_type;
+ rxs->len = 0;
+ rxs->expected_len = 0;
+
+ switch (rxs->pkt_type) {
+#if MYNEWT_VAL(BLE_CONTROLLER)
+ case BLE_HCI_TRANS_H4_PKT_TYPE_CMD:
+ rxs->min_len = 3;
+ break;
+#endif
+ case BLE_HCI_TRANS_H4_PKT_TYPE_ACL:
+ rxs->min_len = 4;
+ break;
+#if MYNEWT_VAL(BLE_HOST)
+ case BLE_HCI_TRANS_H4_PKT_TYPE_EVT:
+ rxs->min_len = 2;
+ break;
+#endif
+ default:
+ /* XXX sync loss */
+ assert(0);
+ break;
+ }
+}
+
+static int
+ble_hci_trans_h4_pull_min_len(struct ble_hci_trans_h4_rx_state *rxs,
+ struct input_buffer *ib)
+{
+ uint16_t len;
+
+ len = min(ib->len, rxs->min_len - rxs->len);
+ memcpy(&rxs->hdr[rxs->len], ib->buf, len);
+
+ rxs->len += len;
+ ble_hci_trans_h4_ib_adjust(ib, len);
+
+ return rxs->len != rxs->min_len;
+}
+
+static int
+ble_hci_trans_h4_rx_state_w4_header(struct ble_hci_trans_h4_rx_state *rxs,
+ struct input_buffer *ib)
+{
+#if MYNEWT_VAL(BLE_HOST)
+ int pool;
+#endif
+ int rc;
+
+ rc = ble_hci_trans_h4_pull_min_len(rxs, ib);
+ if (rc) {
+ /* need more data */
+ return 1;
+ }
+
+ switch (rxs->pkt_type) {
+#if MYNEWT_VAL(BLE_CONTROLLER)
+ case BLE_HCI_TRANS_H4_PKT_TYPE_CMD:
+ rxs->buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD);
+ if (!rxs->buf) {
+ return -1;
+ }
+
+ memcpy(rxs->buf, rxs->hdr, rxs->len);
+ rxs->expected_len = rxs->hdr[2] + 3;
+ break;
+#endif
+ case BLE_HCI_TRANS_H4_PKT_TYPE_ACL:
+ rxs->om = ble_hci_cmac_alloc_acl_mbuf();
+ if (!rxs->om) {
+ return -1;
+ }
+
+ os_mbuf_append(rxs->om, rxs->hdr, rxs->len);
+ rxs->expected_len = get_le16(&rxs->hdr[2]) + 4;
+ break;
+#if MYNEWT_VAL(BLE_HOST)
+ case BLE_HCI_TRANS_H4_PKT_TYPE_EVT:
+ pool = BLE_HCI_TRANS_BUF_EVT_HI;
+ if (rxs->hdr[0] == BLE_HCI_EVCODE_LE_META) {
+ /* For LE Meta event we need 3 bytes to parse header */
+ rxs->min_len = 3;
+ rc = ble_hci_trans_h4_pull_min_len(rxs, ib);
+ if (rc) {
+ /* need more data */
+ return 1;
+ }
+
+ /* Advertising reports shall be allocated from low-prio pool */
+ if ((rxs->hdr[2] == BLE_HCI_LE_SUBEV_ADV_RPT) ||
+ (rxs->hdr[2] == BLE_HCI_LE_SUBEV_EXT_ADV_RPT)) {
+ pool = BLE_HCI_TRANS_BUF_EVT_LO;
+ }
+ }
+
+ /*
+ * XXX Events originally allocated from hi-pool can use lo-pool as
+ * fallback and cannot be dropped. Events allocated from lo-pool
+ * can be dropped to avoid oom while scanning which means that
+ * any advertising or extended advertising report can be silently
+ * discarded by transport. While this is perfectly fine for legacy
+ * advertising, for extended advertising it means we can drop start
+ * or end of chain report and host won't be able to reassemble
+ * chain properly... so just need to make sure pool on host side is
+ * large enough to catch up with controller.
+ */
+ rxs->buf = ble_hci_trans_buf_alloc(pool);
+ if (!rxs->buf && pool == BLE_HCI_TRANS_BUF_EVT_HI) {
+ rxs->buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_LO);
+ if (!rxs->buf) {
+ return -1;
+ }
+ }
+
+ if (rxs->buf) {
+ memcpy(rxs->buf, rxs->hdr, rxs->len);
+ }
+
+ rxs->expected_len = rxs->hdr[1] + 2;
+ break;
+#endif
+ default:
+ assert(0);
+ break;
+ }
+
+ return 0;
+}
+
+static int
+ble_hci_trans_h4_rx_state_w4_payload(struct ble_hci_trans_h4_rx_state *rxs,
+ struct input_buffer *ib)
+{
+ uint16_t mbuf_len;
+ uint16_t len;
+ int rc;
+
+ len = min(ib->len, rxs->expected_len - rxs->len);
+
+ switch (rxs->pkt_type) {
+#if MYNEWT_VAL(BLE_CONTROLLER)
+ case BLE_HCI_TRANS_H4_PKT_TYPE_CMD:
+#endif
+#if MYNEWT_VAL(BLE_HOST)
+ case BLE_HCI_TRANS_H4_PKT_TYPE_EVT:
+#endif
+ if (rxs->buf) {
+ memcpy(&rxs->buf[rxs->len], ib->buf, len);
+ }
+ break;
+ case BLE_HCI_TRANS_H4_PKT_TYPE_ACL:
+ assert(rxs->om);
+
+ mbuf_len = OS_MBUF_PKTLEN(rxs->om);
+ rc = os_mbuf_append(rxs->om, ib->buf, len);
+ if (rc) {
+ /*
+ * Some data may already be appended so need to adjust rxs only by
+ * the size of appended data.
+ */
+ len = OS_MBUF_PKTLEN(rxs->om) - mbuf_len;
+ rxs->len += len;
+ ble_hci_trans_h4_ib_adjust(ib, len);
+
+ return -1;
+ }
+ break;
+ default:
+ assert(0);
+ break;
+ }
+
+ rxs->len += len;
+ ble_hci_trans_h4_ib_adjust(ib, len);
+
+ /* return 1 if need more data */
+ return rxs->len != rxs->expected_len;
+}
+
+static void
+ble_hci_trans_h4_rx_state_completed(struct ble_hci_trans_h4_rx_state *rxs,
+ ble_hci_trans_h4_frame_cb *frame_cb)
+{
+ int rc;
+
+ switch (rxs->pkt_type) {
+#if MYNEWT_VAL(BLE_CONTROLLER)
+ case BLE_HCI_TRANS_H4_PKT_TYPE_CMD:
+#endif
+#if MYNEWT_VAL(BLE_HOST)
+ case BLE_HCI_TRANS_H4_PKT_TYPE_EVT:
+#endif
+ if (rxs->buf) {
+ rc = frame_cb(rxs->pkt_type, rxs->buf);
+ if (rc != 0) {
+ ble_hci_trans_buf_free(rxs->buf);
+ }
+ rxs->buf = NULL;
+ }
+ break;
+ case BLE_HCI_TRANS_H4_PKT_TYPE_ACL:
+ if (rxs->om) {
+ rc = frame_cb(rxs->pkt_type, rxs->om);
+ if (rc != 0) {
+ os_mbuf_free_chain(rxs->om);
+ }
+ rxs->om = NULL;
+ }
+ break;
+ default:
+ assert(0);
+ break;
+ }
+}
+
+int
+ble_hci_trans_h4_rx(struct ble_hci_trans_h4_rx_state *rxs, const uint8_t *buf,
+ uint16_t len, ble_hci_trans_h4_frame_cb *frame_cb)
+{
+ struct input_buffer ib = {
+ .buf = buf,
+ .len = len,
+ };
+ int rc = 0;
+
+ while (ib.len && (rc >= 0)) {
+ rc = 0;
+ switch (rxs->state) {
+ case RXS_STATE_W4_PKT_TYPE:
+ ble_hci_trans_h4_rxs_start(rxs, ib.buf[0]);
+ ble_hci_trans_h4_ib_adjust(&ib, 1);
+ rxs->state = RXS_STATE_W4_HEADER;
+ /* no break */
+
+ case RXS_STATE_W4_HEADER:
+ rc = ble_hci_trans_h4_rx_state_w4_header(rxs, &ib);
+ if (rc) {
+ break;
+ }
+ rxs->state = RXS_STATE_W4_PAYLOAD;
+ /* no break */
+
+ case RXS_STATE_W4_PAYLOAD:
+ rc = ble_hci_trans_h4_rx_state_w4_payload(rxs, &ib);
+ if (rc) {
+ break;
+ }
+ rxs->state = RXS_STATE_COMPLETED;
+ /* no break */
+
+ case RXS_STATE_COMPLETED:
+ ble_hci_trans_h4_rx_state_completed(rxs, frame_cb);
+ rxs->state = RXS_STATE_W4_PKT_TYPE;
+ break;
+
+ default:
+ assert(0);
+ /* consume all remaining data */
+ ble_hci_trans_h4_ib_adjust(&ib, ib.len);
+ break;
+ }
+ }
+
+ /*
+ * Calculate consumed bytes
+ *
+ * Note: we should always consume some bytes unless there is an oom error.
+ * It's also possible that we have an oom error but already consumed some
+ * data, in such case just return success and error will be returned on next
+ * pass.
+ */
+ len = len - ib.len;
+ if (len == 0) {
+ assert(rc < 0);
+ return -1;
+ }
+
+ return len;
+}
diff --git a/nimble/transport/dialog_cmac/src/ble_hci_trans_h4.h b/nimble/transport/dialog_cmac/src/ble_hci_trans_h4.h
new file mode 100644
index 0000000..5b83f6b
--- /dev/null
+++ b/nimble/transport/dialog_cmac/src/ble_hci_trans_h4.h
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef _BLE_HCI_TRANS_H4_H_
+#define _BLE_HCI_TRANS_H4_H_
+
+#include <stdint.h>
+
+#define BLE_HCI_TRANS_H4_PKT_TYPE_NONE 0x00
+#define BLE_HCI_TRANS_H4_PKT_TYPE_CMD 0x01
+#define BLE_HCI_TRANS_H4_PKT_TYPE_ACL 0x02
+#define BLE_HCI_TRANS_H4_PKT_TYPE_EVT 0x04
+
+struct ble_hci_trans_h4_rx_state {
+ uint8_t state;
+ uint8_t pkt_type;
+ uint8_t min_len;
+ uint16_t len;
+ uint16_t expected_len;
+ uint8_t hdr[4];
+ union {
+ uint8_t *buf;
+ struct os_mbuf *om;
+ };
+};
+
+typedef int (ble_hci_trans_h4_frame_cb)(uint8_t pkt_type, void *data);
+
+int ble_hci_trans_h4_rx(struct ble_hci_trans_h4_rx_state *rxs,
+ const uint8_t *buf, uint16_t len,
+ ble_hci_trans_h4_frame_cb *frame_cb);
+
+#endif /* _BLE_HCI_TRANS_H4_H_ */
diff --git a/nimble/transport/pkg.yml b/nimble/transport/dialog_cmac/syscfg.yml
similarity index 52%
copy from nimble/transport/pkg.yml
copy to nimble/transport/dialog_cmac/syscfg.yml
index 3bfca88..c44773e 100644
--- a/nimble/transport/pkg.yml
+++ b/nimble/transport/dialog_cmac/syscfg.yml
@@ -1,4 +1,3 @@
-#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
@@ -17,32 +16,28 @@
# under the License.
#
-pkg.name: nimble/transport
-pkg.description: Meta-package for NimBLE HCI transport
-pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
-pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
- - ble
- - bluetooth
-
-pkg.deps.'BLE_HCI_TRANSPORT == "builtin"':
- - nimble/transport/ram
- - nimble/controller
-
-pkg.deps.'BLE_HCI_TRANSPORT == "emspi"':
- - nimble/transport/emspi
+syscfg.defs:
+ BLE_HCI_EVT_HI_BUF_COUNT:
+ description: 'Number of high-priority event buffers.'
+ value: 2
-pkg.deps.'BLE_HCI_TRANSPORT == "ram"':
- - nimble/transport/ram
+ BLE_HCI_EVT_LO_BUF_COUNT:
+ description: 'Number of low-priority event buffers.'
+ value: 8
-pkg.deps.'BLE_HCI_TRANSPORT == "socket"':
- - nimble/transport/socket
+ BLE_HCI_EVT_BUF_SIZE:
+ description: 'Size of each event buffer, in bytes.'
+ value: 70
-pkg.deps.'BLE_HCI_TRANSPORT == "uart"':
- - nimble/transport/uart
+ BLE_ACL_BUF_COUNT:
+ description: 'The number of ACL data buffers'
+ value: 4
-pkg.deps.'BLE_HCI_TRANSPORT == "da1469x"':
- - nimble/transport/da1469x
+ BLE_ACL_BUF_SIZE:
+ description: >
+ This is the maximum size of the data portion of HCI ACL data
+ packets. It does not include the HCI data header (of 4 bytes).
+ value: 255
-pkg.deps.'BLE_HCI_TRANSPORT == "usb"':
- - nimble/transport/usb
+syscfg.vals.'BLE_EXT_ADV || BLE_LL_CFG_FEAT_LL_EXT_ADV':
+ BLE_HCI_EVT_BUF_SIZE: 257
diff --git a/nimble/transport/pkg.yml b/nimble/transport/pkg.yml
index 3bfca88..a172258 100644
--- a/nimble/transport/pkg.yml
+++ b/nimble/transport/pkg.yml
@@ -44,5 +44,8 @@ pkg.deps.'BLE_HCI_TRANSPORT == "uart"':
pkg.deps.'BLE_HCI_TRANSPORT == "da1469x"':
- nimble/transport/da1469x
+pkg.deps.'BLE_HCI_TRANSPORT == "dialog_cmac"':
+ - nimble/transport/dialog_cmac
+
pkg.deps.'BLE_HCI_TRANSPORT == "usb"':
- nimble/transport/usb
diff --git a/nimble/transport/syscfg.yml b/nimble/transport/syscfg.yml
index 96ea2c2..edf30ca 100644
--- a/nimble/transport/syscfg.yml
+++ b/nimble/transport/syscfg.yml
@@ -26,14 +26,15 @@ syscfg.defs:
value: builtin
restrictions: $notnull
choices:
- - builtin # Built-in NimBLE controller and RAM transport
- - custom # Custom transport, has to be included manually by user
- - ram # RAM transport
- - uart # UART HCI H4 transport
- - socket # Socket transport (for native builds)
- - emspi # SPI transport for EM Microelectionic controllers
- - da1469x # Dialog DA1469x integrated controller
- - usb # USB
+ - builtin # Built-in NimBLE controller and RAM transport
+ - custom # Custom transport, has to be included manually by user
+ - ram # RAM transport
+ - uart # UART HCI H4 transport
+ - socket # Socket transport (for native builds)
+ - emspi # SPI transport for EM Microelectionic controllers
+ - da1469x # Dialog DA1469x integrated controller
+ - dialog_cmac # Dialog CMAC via shared memory
+ - usb # USB
# Deprecated settings
BLE_HCI_TRANSPORT_NIMBLE_BUILTIN:
[mynewt-nimble] 01/05: nimble/phy: Add PHY for Dialog CMAC
Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git
commit e69a66d715687ef923950cfd2f0f17fb88efc71f
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Wed Sep 2 15:33:12 2020 +0200
nimble/phy: Add PHY for Dialog CMAC
This adds PHY for Dialog CMAC.
This allows to run complete NimBLE controller on Dialog DA1469x MCU
family. 1M and 2M PHYs are supported, Coded support is missing in
hardware at the moment.
Some parts of code that access RF block are obfuscated, but there
are no proprietary binaries used so fixes are possible if necessary.
Co-authored-by: Will San Filippo <wi...@juul.com>
---
nimble/drivers/dialog_cmac/include/ble/xcvr.h | 39 +
nimble/drivers/dialog_cmac/pkg.yml | 32 +
nimble/drivers/dialog_cmac/src/aes/aes.h | 130 ++
nimble/drivers/dialog_cmac/src/aes/aes_encrypt.c | 193 +++
nimble/drivers/dialog_cmac/src/aes/constants.h | 61 +
nimble/drivers/dialog_cmac/src/aes/utils.c | 74 +
nimble/drivers/dialog_cmac/src/aes/utils.h | 95 ++
nimble/drivers/dialog_cmac/src/ble_hw.c | 340 +++++
nimble/drivers/dialog_cmac/src/ble_hw_priv.h | 29 +
nimble/drivers/dialog_cmac/src/ble_phy.c | 1781 ++++++++++++++++++++++
nimble/drivers/dialog_cmac/src/ble_rf.c | 729 +++++++++
nimble/drivers/dialog_cmac/src/ble_rf_priv.h | 39 +
nimble/drivers/dialog_cmac/syscfg.yml | 29 +
13 files changed, 3571 insertions(+)
diff --git a/nimble/drivers/dialog_cmac/include/ble/xcvr.h b/nimble/drivers/dialog_cmac/include/ble/xcvr.h
new file mode 100644
index 0000000..e4e741c
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/include/ble/xcvr.h
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_BLE_XCVR_
+#define H_BLE_XCVR_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define XCVR_TX_SCHED_DELAY_USECS (250)
+
+/*
+ * Define HW whitelist size. This is the total possible whitelist size;
+ * not necessarily the size that will be used (may be smaller)
+ */
+#define BLE_HW_WHITE_LIST_SIZE (8)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_BLE_XCVR_ */
diff --git a/nimble/drivers/dialog_cmac/pkg.yml b/nimble/drivers/dialog_cmac/pkg.yml
new file mode 100644
index 0000000..6d158fc
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/pkg.yml
@@ -0,0 +1,32 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: nimble/drivers/dialog_cmac
+pkg.description: BLE driver for Dialog CMAC
+pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+ - ble
+ - bluetooth
+pkg.deps:
+ - "@apache-mynewt-nimble/nimble/controller"
+pkg.apis:
+ - ble_driver
+pkg.req_apis:
+ - ble_transport
diff --git a/nimble/drivers/dialog_cmac/src/aes/aes.h b/nimble/drivers/dialog_cmac/src/aes/aes.h
new file mode 100644
index 0000000..b612213
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/src/aes/aes.h
@@ -0,0 +1,130 @@
+/* aes.h - TinyCrypt interface to an AES-128 implementation */
+
+/*
+ * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Intel Corporation nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @file
+ * @brief -- Interface to an AES-128 implementation.
+ *
+ * Overview: AES-128 is a NIST approved block cipher specified in
+ * FIPS 197. Block ciphers are deterministic algorithms that
+ * perform a transformation specified by a symmetric key in fixed-
+ * length data sets, also called blocks.
+ *
+ * Security: AES-128 provides approximately 128 bits of security.
+ *
+ * Usage: 1) call tc_aes128_set_encrypt/decrypt_key to set the key.
+ *
+ * 2) call tc_aes_encrypt/decrypt to process the data.
+ */
+
+#ifndef __TC_AES_H__
+#define __TC_AES_H__
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define Nb (4) /* number of columns (32-bit words) comprising the state */
+#define Nk (4) /* number of 32-bit words comprising the key */
+#define Nr (10) /* number of rounds */
+#define TC_AES_BLOCK_SIZE (Nb*Nk)
+#define TC_AES_KEY_SIZE (Nb*Nk)
+
+typedef struct tc_aes_key_sched_struct {
+ unsigned int words[Nb*(Nr+1)];
+} *TCAesKeySched_t;
+
+/**
+ * @brief Set AES-128 encryption key
+ * Uses key k to initialize s
+ * @return returns TC_CRYPTO_SUCCESS (1)
+ * returns TC_CRYPTO_FAIL (0) if: s == NULL or k == NULL
+ * @note This implementation skips the additional steps required for keys
+ * larger than 128 bits, and must not be used for AES-192 or
+ * AES-256 key schedule -- see FIPS 197 for details
+ * @param s IN/OUT -- initialized struct tc_aes_key_sched_struct
+ * @param k IN -- points to the AES key
+ */
+int tc_aes128_set_encrypt_key(TCAesKeySched_t s, const uint8_t *k);
+
+/**
+ * @brief AES-128 Encryption procedure
+ * Encrypts contents of in buffer into out buffer under key;
+ * schedule s
+ * @note Assumes s was initialized by aes_set_encrypt_key;
+ * out and in point to 16 byte buffers
+ * @return returns TC_CRYPTO_SUCCESS (1)
+ * returns TC_CRYPTO_FAIL (0) if: out == NULL or in == NULL or s == NULL
+ * @param out IN/OUT -- buffer to receive ciphertext block
+ * @param in IN -- a plaintext block to encrypt
+ * @param s IN -- initialized AES key schedule
+ */
+int tc_aes_encrypt(uint8_t *out, const uint8_t *in,
+ const TCAesKeySched_t s);
+
+/**
+ * @brief Set the AES-128 decryption key
+ * Uses key k to initialize s
+ * @return returns TC_CRYPTO_SUCCESS (1)
+ * returns TC_CRYPTO_FAIL (0) if: s == NULL or k == NULL
+ * @note This is the implementation of the straightforward inverse cipher
+ * using the cipher documented in FIPS-197 figure 12, not the
+ * equivalent inverse cipher presented in Figure 15
+ * @warning This routine skips the additional steps required for keys larger
+ * than 128, and must not be used for AES-192 or AES-256 key
+ * schedule -- see FIPS 197 for details
+ * @param s IN/OUT -- initialized struct tc_aes_key_sched_struct
+ * @param k IN -- points to the AES key
+ */
+int tc_aes128_set_decrypt_key(TCAesKeySched_t s, const uint8_t *k);
+
+/**
+ * @brief AES-128 Encryption procedure
+ * Decrypts in buffer into out buffer under key schedule s
+ * @return returns TC_CRYPTO_SUCCESS (1)
+ * returns TC_CRYPTO_FAIL (0) if: out is NULL or in is NULL or s is NULL
+ * @note Assumes s was initialized by aes_set_encrypt_key
+ * out and in point to 16 byte buffers
+ * @param out IN/OUT -- buffer to receive ciphertext block
+ * @param in IN -- a plaintext block to encrypt
+ * @param s IN -- initialized AES key schedule
+ */
+int tc_aes_decrypt(uint8_t *out, const uint8_t *in,
+ const TCAesKeySched_t s);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TC_AES_H__ */
diff --git a/nimble/drivers/dialog_cmac/src/aes/aes_encrypt.c b/nimble/drivers/dialog_cmac/src/aes/aes_encrypt.c
new file mode 100644
index 0000000..4288c42
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/src/aes/aes_encrypt.c
@@ -0,0 +1,193 @@
+/* aes_encrypt.c - TinyCrypt implementation of AES encryption procedure */
+
+/*
+ * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Intel Corporation nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "aes.h"
+#include "utils.h"
+#include "constants.h"
+
+static const uint8_t sbox[256] = {
+ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
+ 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
+ 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26,
+ 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
+ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
+ 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
+ 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed,
+ 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
+ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
+ 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
+ 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec,
+ 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
+ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
+ 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
+ 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
+ 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
+ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
+ 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
+ 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11,
+ 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
+ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
+ 0xb0, 0x54, 0xbb, 0x16
+};
+
+static const unsigned int rconst[11] = {
+ 0x00000000, 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000,
+ 0x20000000, 0x40000000, 0x80000000, 0x1b000000, 0x36000000
+ };
+
+/* AES state and temp buffer */
+uint8_t g_aes_state[Nk*Nb];
+uint8_t g_t[Nb*Nk];
+
+static inline unsigned int rotword(unsigned int a)
+{
+ return (((a) >> 24)|((a) << 8));
+}
+
+#define subbyte(a, o)(sbox[((a) >> (o))&0xff] << (o))
+#define subword(a)(subbyte(a, 24)|subbyte(a, 16)|subbyte(a, 8)|subbyte(a, 0))
+
+int tc_aes128_set_encrypt_key(TCAesKeySched_t s, const uint8_t *k)
+{
+ unsigned int i;
+ unsigned int t;
+
+ if (s == (TCAesKeySched_t) 0) {
+ return TC_CRYPTO_FAIL;
+ } else if (k == (const uint8_t *) 0) {
+ return TC_CRYPTO_FAIL;
+ }
+
+ for (i = 0; i < Nk; ++i) {
+ s->words[i] = (k[Nb*i]<<24) | (k[Nb*i+1]<<16) |
+ (k[Nb*i+2]<<8) | (k[Nb*i+3]);
+ }
+
+ for (; i < (Nb * (Nr + 1)); ++i) {
+ t = s->words[i-1];
+ if ((i % Nk) == 0) {
+ t = subword(rotword(t)) ^ rconst[i/Nk];
+ }
+ s->words[i] = s->words[i-Nk] ^ t;
+ }
+
+ return TC_CRYPTO_SUCCESS;
+}
+
+static inline void add_round_key(uint8_t *s, const unsigned int *k)
+{
+ s[0] ^= (uint8_t)(k[0] >> 24); s[1] ^= (uint8_t)(k[0] >> 16);
+ s[2] ^= (uint8_t)(k[0] >> 8); s[3] ^= (uint8_t)(k[0]);
+ s[4] ^= (uint8_t)(k[1] >> 24); s[5] ^= (uint8_t)(k[1] >> 16);
+ s[6] ^= (uint8_t)(k[1] >> 8); s[7] ^= (uint8_t)(k[1]);
+ s[8] ^= (uint8_t)(k[2] >> 24); s[9] ^= (uint8_t)(k[2] >> 16);
+ s[10] ^= (uint8_t)(k[2] >> 8); s[11] ^= (uint8_t)(k[2]);
+ s[12] ^= (uint8_t)(k[3] >> 24); s[13] ^= (uint8_t)(k[3] >> 16);
+ s[14] ^= (uint8_t)(k[3] >> 8); s[15] ^= (uint8_t)(k[3]);
+}
+
+static inline void sub_bytes(uint8_t *s)
+{
+ unsigned int i;
+
+ for (i = 0; i < (Nb * Nk); ++i) {
+ s[i] = sbox[s[i]];
+ }
+}
+
+#define triple(a)(_double_byte(a)^(a))
+
+static inline void mult_row_column(uint8_t *out, const uint8_t *in)
+{
+ out[0] = _double_byte(in[0]) ^ triple(in[1]) ^ in[2] ^ in[3];
+ out[1] = in[0] ^ _double_byte(in[1]) ^ triple(in[2]) ^ in[3];
+ out[2] = in[0] ^ in[1] ^ _double_byte(in[2]) ^ triple(in[3]);
+ out[3] = triple(in[0]) ^ in[1] ^ in[2] ^ _double_byte(in[3]);
+}
+
+static inline void mix_columns(uint8_t *s)
+{
+ mult_row_column(g_t, s);
+ mult_row_column(&g_t[Nb], s+Nb);
+ mult_row_column(&g_t[2 * Nb], s + (2 * Nb));
+ mult_row_column(&g_t[3 * Nb], s + (3 * Nb));
+ (void) _copy(s, sizeof(g_t), g_t, sizeof(g_t));
+}
+
+/*
+ * This shift_rows also implements the matrix flip required for mix_columns, but
+ * performs it here to reduce the number of memory operations.
+ */
+static inline void shift_rows(uint8_t *s)
+{
+ g_t[0] = s[0]; g_t[1] = s[5]; g_t[2] = s[10]; g_t[3] = s[15];
+ g_t[4] = s[4]; g_t[5] = s[9]; g_t[6] = s[14]; g_t[7] = s[3];
+ g_t[8] = s[8]; g_t[9] = s[13]; g_t[10] = s[2]; g_t[11] = s[7];
+ g_t[12] = s[12]; g_t[13] = s[1]; g_t[14] = s[6]; g_t[15] = s[11];
+ (void) _copy(s, sizeof(g_t), g_t, sizeof(g_t));
+}
+
+int tc_aes_encrypt(uint8_t *out, const uint8_t *in, const TCAesKeySched_t s)
+{
+ unsigned int i;
+
+ if (out == (uint8_t *) 0) {
+ return TC_CRYPTO_FAIL;
+ } else if (in == (const uint8_t *) 0) {
+ return TC_CRYPTO_FAIL;
+ } else if (s == (TCAesKeySched_t) 0) {
+ return TC_CRYPTO_FAIL;
+ }
+
+ (void)_copy(g_aes_state, sizeof(g_aes_state), in,
+ sizeof(g_aes_state));
+ add_round_key(g_aes_state, s->words);
+
+ for (i = 0; i < (Nr - 1); ++i) {
+ sub_bytes(g_aes_state);
+ shift_rows(g_aes_state);
+ mix_columns(g_aes_state);
+ add_round_key(g_aes_state, s->words + Nb*(i+1));
+ }
+
+ sub_bytes(g_aes_state);
+ shift_rows(g_aes_state);
+ add_round_key(g_aes_state, s->words + Nb*(i+1));
+
+ (void)_copy(out, sizeof(g_aes_state), g_aes_state,
+ sizeof(g_aes_state));
+
+ /* zeroing out the state buffer */
+ _set(g_aes_state, TC_ZERO_BYTE, sizeof(g_aes_state));
+
+ return TC_CRYPTO_SUCCESS;
+}
diff --git a/nimble/drivers/dialog_cmac/src/aes/constants.h b/nimble/drivers/dialog_cmac/src/aes/constants.h
new file mode 100644
index 0000000..965490e
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/src/aes/constants.h
@@ -0,0 +1,61 @@
+/* constants.h - TinyCrypt interface to constants */
+
+/*
+ * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Intel Corporation nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @file
+ * @brief -- Interface to constants.
+ *
+ */
+
+#ifndef __TC_CONSTANTS_H__
+#define __TC_CONSTANTS_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdbool.h>
+
+#ifndef NULL
+#define NULL ((void *)0)
+#endif
+
+#define TC_CRYPTO_SUCCESS 1
+#define TC_CRYPTO_FAIL 0
+
+#define TC_ZERO_BYTE 0x00
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TC_CONSTANTS_H__ */
diff --git a/nimble/drivers/dialog_cmac/src/aes/utils.c b/nimble/drivers/dialog_cmac/src/aes/utils.c
new file mode 100644
index 0000000..61a567b
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/src/aes/utils.c
@@ -0,0 +1,74 @@
+/* utils.c - TinyCrypt platform-dependent run-time operations */
+
+/*
+ * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Intel Corporation nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "utils.h"
+#include "constants.h"
+
+#include <string.h>
+
+#define MASK_TWENTY_SEVEN 0x1b
+
+unsigned int _copy(uint8_t *to, unsigned int to_len,
+ const uint8_t *from, unsigned int from_len)
+{
+ if (from_len <= to_len) {
+ (void)memcpy(to, from, from_len);
+ return from_len;
+ } else {
+ return TC_CRYPTO_FAIL;
+ }
+}
+
+void _set(void *to, uint8_t val, unsigned int len)
+{
+ (void)memset(to, val, len);
+}
+
+/*
+ * Doubles the value of a byte for values up to 127.
+ */
+uint8_t _double_byte(uint8_t a)
+{
+ return ((a<<1) ^ ((a>>7) * MASK_TWENTY_SEVEN));
+}
+
+int _compare(const uint8_t *a, const uint8_t *b, size_t size)
+{
+ const uint8_t *tempa = a;
+ const uint8_t *tempb = b;
+ uint8_t result = 0;
+
+ for (unsigned int i = 0; i < size; i++) {
+ result |= tempa[i] ^ tempb[i];
+ }
+ return result;
+}
diff --git a/nimble/drivers/dialog_cmac/src/aes/utils.h b/nimble/drivers/dialog_cmac/src/aes/utils.h
new file mode 100644
index 0000000..bab5c32
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/src/aes/utils.h
@@ -0,0 +1,95 @@
+/* utils.h - TinyCrypt interface to platform-dependent run-time operations */
+
+/*
+ * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * - Neither the name of Intel Corporation nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @file
+ * @brief Interface to platform-dependent run-time operations.
+ *
+ */
+
+#ifndef __TC_UTILS_H__
+#define __TC_UTILS_H__
+
+#include <stdint.h>
+#include <stddef.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @brief Copy the the buffer 'from' to the buffer 'to'.
+ * @return returns TC_CRYPTO_SUCCESS (1)
+ * returns TC_CRYPTO_FAIL (0) if:
+ * from_len > to_len.
+ *
+ * @param to OUT -- destination buffer
+ * @param to_len IN -- length of destination buffer
+ * @param from IN -- origin buffer
+ * @param from_len IN -- length of origin buffer
+ */
+unsigned int _copy(uint8_t *to, unsigned int to_len,
+ const uint8_t *from, unsigned int from_len);
+
+/**
+ * @brief Set the value 'val' into the buffer 'to', 'len' times.
+ *
+ * @param to OUT -- destination buffer
+ * @param val IN -- value to be set in 'to'
+ * @param len IN -- number of times the value will be copied
+ */
+void _set(void *to, uint8_t val, unsigned int len);
+
+/*
+ * @brief AES specific doubling function, which utilizes
+ * the finite field used by AES.
+ * @return Returns a^2
+ *
+ * @param a IN/OUT -- value to be doubled
+ */
+uint8_t _double_byte(uint8_t a);
+
+/*
+ * @brief Constant-time algorithm to compare if two sequences of bytes are equal
+ * @return Returns 0 if equal, and non-zero otherwise
+ *
+ * @param a IN -- sequence of bytes a
+ * @param b IN -- sequence of bytes b
+ * @param size IN -- size of sequences a and b
+ */
+int _compare(const uint8_t *a, const uint8_t *b, size_t size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __TC_UTILS_H__ */
diff --git a/nimble/drivers/dialog_cmac/src/ble_hw.c b/nimble/drivers/dialog_cmac/src/ble_hw.c
new file mode 100644
index 0000000..d8ee544
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/src/ble_hw.c
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <stdint.h>
+#include "mcu/mcu.h"
+#include "nimble/ble.h"
+#include "controller/ble_hw.h"
+#include "CMAC.h"
+#include "cmac_driver/cmac_shared.h"
+#include "mcu/mcu.h"
+#include "aes/aes.h"
+
+static struct tc_aes_key_sched_struct g_ctx;
+
+int
+ble_hw_rng_init(ble_rng_isr_cb_t cb, int bias)
+{
+ cmac_rand_set_isr_cb(cb);
+ return 0;
+}
+
+int
+ble_hw_rng_start(void)
+{
+ /* Chime the M33 in case we need random numbers generated */
+ cmac_rand_start();
+ CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV1C_CMAC2SYS_IRQ_SET_Msk;
+ return 0;
+}
+
+int
+ble_hw_rng_stop(void)
+{
+ cmac_rand_stop();
+ return 0;
+}
+
+#define BLE_HW_RESOLV_LIST_SIZE (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE))
+
+struct ble_hw_resolv_irk {
+ uint32_t key[4];
+};
+
+struct ble_hw_resolv_list {
+ uint8_t count;
+ struct ble_hw_resolv_irk irk[BLE_HW_RESOLV_LIST_SIZE];
+};
+
+struct ble_hw_resolv_proc {
+ uint32_t hash;
+ uint8_t f_configured;
+ uint8_t f_active;
+ uint8_t f_match;
+ uint8_t f_done;
+ struct ble_hw_resolv_irk *irk;
+ struct ble_hw_resolv_irk *irk_end;
+ uint32_t crypto_prand_in[4];
+ uint32_t crypto_e_out[4];
+};
+
+static struct ble_hw_resolv_list g_ble_hw_resolv_list;
+static struct ble_hw_resolv_proc g_ble_hw_resolv_proc;
+
+int
+ble_hw_get_public_addr(ble_addr_t *addr)
+{
+ return -1;
+}
+
+int
+ble_hw_get_static_addr(ble_addr_t *addr)
+{
+ return -1;
+}
+
+void
+ble_hw_whitelist_clear(void)
+{
+}
+
+int
+ble_hw_whitelist_add(const uint8_t *addr, uint8_t addr_type)
+{
+ return 0;
+}
+
+void
+ble_hw_whitelist_rmv(const uint8_t *addr, uint8_t addr_type)
+{
+}
+
+uint8_t
+ble_hw_whitelist_size(void)
+{
+ return 0;
+}
+
+void
+ble_hw_whitelist_enable(void)
+{
+}
+
+
+void
+ble_hw_whitelist_disable(void)
+{
+}
+
+int
+ble_hw_whitelist_match(void)
+{
+ return 0;
+}
+
+int
+ble_hw_encrypt_block(struct ble_encryption_block *ecb)
+{
+ uint32_t in_addr;
+ uint32_t out_addr;
+
+ /*
+ * The following code bears some explanation. This function is called by
+ * the LL task to encrypt blocks and calculate session keys. Address
+ * resolution also calls this function. Furthermore, during connections,
+ * the M0 crypto accelerator is used but this function is not called when
+ * using it. During the entire connection event, the M0 crypto block cannot
+ * be used as the crypto state (some of it) needs to remain un-changed.
+ * Note that this is also true when address resolution is enabled: the
+ * HW crypto block is set up and cannot be modified.
+ *
+ * Rather than attempt to share the M0 crypto block between the various
+ * controller features which require it, we decided to use software to
+ * perform the encryption task for anything being done at the link-layer
+ * (outside of an ISR). If this function is called inside an ISR, and it
+ * is when resolving addresses, the crypto accelerator is not being used
+ * by a connection event. Thus, we check to see if we are inside of an ISR.
+ * If so, we use the M0 crypto block. If outside of an ISR, we use the M33
+ */
+ if (!os_arch_in_isr()) {
+ tc_aes128_set_encrypt_key(&g_ctx, ecb->key);
+ tc_aes_encrypt(ecb->cipher_text, ecb->plain_text, &g_ctx);
+ return 0;
+ }
+
+ /* Need to retain state of in/out pointers */
+ in_addr = CMAC->CM_CRYPTO_IN_ADR2_REG;
+ out_addr = CMAC->CM_CRYPTO_OUT_ADR_REG;
+
+ while (CMAC->CM_CRYPTO_STAT_REG & CMAC_CM_CRYPTO_STAT_REG_CM_CRYPTO_BUSY_Msk);
+
+ /* RECB, memory in/out, encryption */
+ CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_ECB_ENC_EN_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_IN_SEL_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_OUT_SEL_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_ENC_DECN_Msk;
+
+ CMAC->CM_CRYPTO_KEY_31_0_REG = get_le32(&ecb->key[0]);
+ CMAC->CM_CRYPTO_KEY_63_32_REG = get_le32(&ecb->key[4]);
+ CMAC->CM_CRYPTO_KEY_95_64_REG = get_le32(&ecb->key[8]);
+ CMAC->CM_CRYPTO_KEY_127_96_REG = get_le32(&ecb->key[12]);
+ CMAC->CM_CRYPTO_IN_ADR2_REG = (uint32_t)ecb->plain_text;
+ CMAC->CM_CRYPTO_OUT_ADR_REG = (uint32_t)ecb->cipher_text;
+
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_CRYPTO_Msk;
+ CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV_CRYPTO_START_Msk;
+ while (!(CMAC->CM_EXC_STAT_REG & CMAC_CM_EXC_STAT_REG_EXC_CRYPTO_Msk));
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_CRYPTO_Msk;
+
+ CMAC->CM_CRYPTO_IN_ADR2_REG = in_addr;
+ CMAC->CM_CRYPTO_OUT_ADR_REG = out_addr;
+
+ return 0;
+}
+
+void
+ble_hw_resolv_list_clear(void)
+{
+ g_ble_hw_resolv_list.count = 0;
+}
+
+int
+ble_hw_resolv_list_add(uint8_t *irk)
+{
+ struct ble_hw_resolv_irk *e;
+
+ if (g_ble_hw_resolv_list.count == BLE_HW_RESOLV_LIST_SIZE) {
+ return BLE_ERR_MEM_CAPACITY;
+ }
+
+ e = &g_ble_hw_resolv_list.irk[g_ble_hw_resolv_list.count];
+ /* Prepare key here so we do not need to do it during resolving */
+ e->key[0] = get_le32(&irk[0]);
+ e->key[1] = get_le32(&irk[4]);
+ e->key[2] = get_le32(&irk[8]);
+ e->key[3] = get_le32(&irk[12]);
+
+ g_ble_hw_resolv_list.count++;
+
+ return BLE_ERR_SUCCESS;
+}
+
+void
+ble_hw_resolv_list_rmv(int index)
+{
+ struct ble_hw_resolv_irk *e;
+
+ if (index < g_ble_hw_resolv_list.count) {
+ g_ble_hw_resolv_list.count--;
+
+ e = &g_ble_hw_resolv_list.irk[index];
+ memmove(e, e + 1, (g_ble_hw_resolv_list.count - index) * sizeof(e->key));
+ }
+}
+
+uint8_t
+ble_hw_resolv_list_size(void)
+{
+ return BLE_HW_RESOLV_LIST_SIZE;
+}
+
+int
+ble_hw_resolv_list_match(void)
+{
+ return g_ble_hw_resolv_proc.f_match ?
+ g_ble_hw_resolv_proc.irk - g_ble_hw_resolv_list.irk : -1;
+}
+
+static void
+ble_hw_resolv_proc_next(void)
+{
+ void *src = &g_ble_hw_resolv_proc.irk->key;
+
+ if (g_ble_hw_resolv_proc.irk == g_ble_hw_resolv_proc.irk_end) {
+ g_ble_hw_resolv_proc.f_done = 1;
+ g_ble_hw_resolv_proc.f_active = 0;
+ } else {
+ __asm__ volatile (".syntax unified \n"
+ " ldm %[ptr]!, {r1, r2, r3, r4} \n"
+ " ldr %[ptr], =%[reg] \n"
+ " stm %[ptr]!, {r1, r2, r3, r4} \n"
+ : [ptr] "+l" (src)
+ : [reg] "i" (&CMAC->CM_CRYPTO_KEY_31_0_REG)
+ : "r1", "r2", "r3", "r4", "memory");
+
+ CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV_CRYPTO_START_Msk;
+ }
+}
+
+void
+ble_hw_resolv_proc_enable(void)
+{
+ assert(!g_ble_hw_resolv_proc.f_active);
+
+ CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_SW_REQ_ABORT_Msk;
+
+ CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_ECB_ENC_EN_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_IN_SEL_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_OUT_SEL_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_ENC_DECN_Msk;
+
+ CMAC->CM_CRYPTO_IN_ADR2_REG = (uint32_t)g_ble_hw_resolv_proc.crypto_prand_in;
+ CMAC->CM_CRYPTO_OUT_ADR_REG = (uint32_t)g_ble_hw_resolv_proc.crypto_e_out;
+
+ g_ble_hw_resolv_proc.irk = g_ble_hw_resolv_list.irk;
+ g_ble_hw_resolv_proc.irk_end = g_ble_hw_resolv_list.irk +
+ g_ble_hw_resolv_list.count;
+ g_ble_hw_resolv_proc.f_configured = 1;
+ g_ble_hw_resolv_proc.f_active = 0;
+
+ /*
+ * It would be better to enable IRQ in ble_hw_resolv_proc_start, but this
+ * would introduce a bit of latency when starting resolving procedure and
+ * we need to save every us possible there in order to be able to resolve
+ * RPA on time.
+ */
+ NVIC_ClearPendingIRQ(CRYPTO_IRQn);
+ NVIC_EnableIRQ(CRYPTO_IRQn);
+}
+
+void
+ble_hw_resolv_proc_disable(void)
+{
+ g_ble_hw_resolv_proc.f_configured = 0;
+ g_ble_hw_resolv_proc.f_active = 0;
+ g_ble_hw_resolv_proc.f_match = 0;
+ g_ble_hw_resolv_proc.f_done = 1;
+
+ NVIC_DisableIRQ(CRYPTO_IRQn);
+}
+
+void
+ble_hw_resolv_proc_start(const uint8_t *addr)
+{
+ assert(g_ble_hw_resolv_proc.f_configured);
+
+ /* crypto_prand_in is already zeroed so prand is properly padded */
+ g_ble_hw_resolv_proc.crypto_prand_in[3] = get_be24(&addr[3]) << 8;
+ g_ble_hw_resolv_proc.hash = get_be24(&addr[0]);
+
+ g_ble_hw_resolv_proc.f_match = 0;
+ g_ble_hw_resolv_proc.f_done = 0;
+ g_ble_hw_resolv_proc.f_active = 1;
+
+ ble_hw_resolv_proc_next();
+}
+
+void
+CRYPTO_IRQHandler(void)
+{
+ uint32_t hash;
+
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_CRYPTO_Msk;
+
+ hash = g_ble_hw_resolv_proc.crypto_e_out[3] >> 8;
+ if (g_ble_hw_resolv_proc.hash == hash) {
+ g_ble_hw_resolv_proc.f_active = 0;
+ g_ble_hw_resolv_proc.f_match = 1;
+ g_ble_hw_resolv_proc.f_done = 1;
+ } else {
+ g_ble_hw_resolv_proc.irk++;
+ ble_hw_resolv_proc_next();
+ }
+}
diff --git a/nimble/drivers/dialog_cmac/src/ble_hw_priv.h b/nimble/drivers/dialog_cmac/src/ble_hw_priv.h
new file mode 100644
index 0000000..627994f
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/src/ble_hw_priv.h
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef _BLE_HW_PRIV_H_
+#define _BLE_HW_PRIV_H_
+
+#include <stdint.h>
+
+void ble_hw_resolv_proc_enable(void);
+void ble_hw_resolv_proc_disable(void);
+void ble_hw_resolv_proc_start(const uint8_t *addr);
+
+#endif /* _BLE_HW_PRIV_H_ */
diff --git a/nimble/drivers/dialog_cmac/src/ble_phy.c b/nimble/drivers/dialog_cmac/src/ble_phy.c
new file mode 100644
index 0000000..20c6409
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/src/ble_phy.c
@@ -0,0 +1,1781 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "syscfg/syscfg.h"
+#if !MYNEWT_VAL(BLE_PHY_DEBUG_DSER)
+#define MCU_DIAG_SER_DISABLE
+#endif
+
+#include <assert.h>
+#include <stdint.h>
+#include <assert.h>
+#include "nimble/ble.h"
+#include "mcu/mcu.h"
+#include "mcu/cmac_timer.h"
+#include "cmac_driver/cmac_shared.h"
+#include "controller/ble_phy.h"
+#include "controller/ble_ll.h"
+#include "stats/stats.h"
+#include "CMAC.h"
+#include "ble_hw_priv.h"
+#include "ble_rf_priv.h"
+
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
+#error LE Coded PHY cannot be enabled on DA1469x
+#endif
+
+/* Statistics */
+STATS_SECT_START(ble_phy_stats)
+ STATS_SECT_ENTRY(phy_isrs)
+ STATS_SECT_ENTRY(tx_good)
+ STATS_SECT_ENTRY(tx_fail)
+ STATS_SECT_ENTRY(tx_late)
+ STATS_SECT_ENTRY(tx_bytes)
+ STATS_SECT_ENTRY(rx_starts)
+ STATS_SECT_ENTRY(rx_aborts)
+ STATS_SECT_ENTRY(rx_valid)
+ STATS_SECT_ENTRY(rx_crc_err)
+ STATS_SECT_ENTRY(rx_late)
+ STATS_SECT_ENTRY(radio_state_errs)
+ STATS_SECT_ENTRY(rx_hw_err)
+ STATS_SECT_ENTRY(tx_hw_err)
+STATS_SECT_END
+STATS_SECT_DECL(ble_phy_stats) ble_phy_stats;
+
+STATS_NAME_START(ble_phy_stats)
+ STATS_NAME(ble_phy_stats, phy_isrs)
+ STATS_NAME(ble_phy_stats, tx_good)
+ STATS_NAME(ble_phy_stats, tx_fail)
+ STATS_NAME(ble_phy_stats, tx_late)
+ STATS_NAME(ble_phy_stats, tx_bytes)
+ STATS_NAME(ble_phy_stats, rx_starts)
+ STATS_NAME(ble_phy_stats, rx_aborts)
+ STATS_NAME(ble_phy_stats, rx_valid)
+ STATS_NAME(ble_phy_stats, rx_crc_err)
+ STATS_NAME(ble_phy_stats, rx_late)
+ STATS_NAME(ble_phy_stats, radio_state_errs)
+ STATS_NAME(ble_phy_stats, rx_hw_err)
+ STATS_NAME(ble_phy_stats, tx_hw_err)
+STATS_NAME_END(ble_phy_stats)
+
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY)
+#error LE Coded PHY is not supported
+#endif
+
+/* An easy way to get and set bit field value in CMAC registers */
+#define CMAC_SETREGF(_reg, _field, _val) \
+ CMAC->_reg = (CMAC->_reg & ~(CMAC_ ## _reg ## _ ## _field ## _Msk)) | \
+ ((_val) << (CMAC_ ## _reg ## _ ## _field ## _Pos));
+#define CMAC_GETREGF(_reg, _field) \
+ (CMAC->_reg & (CMAC_ ## _reg ## _ ## _field ## _Msk)) >> \
+ (CMAC_ ## _reg ## _ ## _field ## _Pos)
+
+/* Definitions for fields queue */
+#define FIELD_DATA_REG_DMA_TX(_offset, _len) \
+ ((uint32_t)&g_ble_phy_tx_buf[(_offset)] & 0x3ffff) | ((_len) << 20)
+#define FIELD_DATA_REG_DMA_RX(_offset, _len) \
+ ((uint32_t)&g_ble_phy_rx_buf[(_offset)] & 0x3ffff) | ((_len) << 20)
+
+#if MYNEWT_VAL(BLE_LL_DTM)
+#define PHY_WHITENING (g_ble_phy_data.phy_whitening)
+#else
+#define PHY_WHITENING (1)
+#endif
+
+#define FIELD_CTRL_REG_TX_PREAMBLE \
+ (0 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CRC_Pos) | \
+ (0 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_WHITENING_Pos) | \
+ (0 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_TX_DATA_SRC_Pos) | \
+ (g_ble_phy_data.phy_mode_evpsym << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EVPSYMBOL_LUT_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_VALID_Pos) | \
+ (g_ble_phy_data.phy_mode_pre_len << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_SIZE_M1_Pos)
+#define FIELD_CTRL_REG_TX_ACCESS_ADDR \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EXC_ON_EXP_Pos) | \
+ (0 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CRC_Pos) | \
+ (0 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_WHITENING_Pos) | \
+ (0 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_TX_DATA_SRC_Pos) | \
+ (g_ble_phy_data.phy_mode_evpsym << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EVPSYMBOL_LUT_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_VALID_Pos) | \
+ (31 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_SIZE_M1_Pos)
+#define FIELD_CTRL_REG_TX_PAYLOAD \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CRC_Pos) | \
+ (PHY_WHITENING << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_WHITENING_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_DMA_MEM_Pos) | \
+ (g_ble_phy_data.phy_mode_evpsym << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EVPSYMBOL_LUT_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_VALID_Pos);
+#define FIELD_CTRL_REG_TX_ENC_PAYLOAD \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CRC_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_WHITENING_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_DMA_CRYPTO_Pos) | \
+ (g_ble_phy_data.phy_mode_evpsym << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EVPSYMBOL_LUT_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_VALID_Pos)
+#define FIELD_CTRL_REG_TX_MIC \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CRC_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_WHITENING_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_DMA_CRYPTO_Pos) | \
+ (g_ble_phy_data.phy_mode_evpsym << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EVPSYMBOL_LUT_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_VALID_Pos)
+#define FIELD_CTRL_REG_TX_CRC \
+ (0 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CRC_Pos) | \
+ (PHY_WHITENING << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_WHITENING_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_TX_DATA_SRC_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_LAST_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_MSB_FIRST_Pos) | \
+ (g_ble_phy_data.phy_mode_evpsym << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EVPSYMBOL_LUT_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_VALID_Pos) | \
+ (23 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_SIZE_M1_Pos)
+#define FIELD_CTRL_REG_RX_ACCESS_ADDR \
+ (0 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CRC_Pos) | \
+ (0 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_WHITENING_Pos) | \
+ (0 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_TX_DATA_SRC_Pos) | \
+ (g_ble_phy_data.phy_mode_evpsym << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EVPSYMBOL_LUT_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CORR_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_VALID_Pos) | \
+ (31 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_SIZE_M1_Pos)
+#define FIELD_CTRL_REG_RX_HEADER \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EXC_ON_EXP_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CRC_Pos) | \
+ (PHY_WHITENING << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_WHITENING_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_DMA_MEM_Pos) | \
+ (g_ble_phy_data.phy_mode_evpsym << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EVPSYMBOL_LUT_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_VALID_Pos)
+#define FIELD_CTRL_REG_RX_CRC \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CRC_Pos) | \
+ (PHY_WHITENING << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_WHITENING_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_LAST_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_MSB_FIRST_Pos) | \
+ (g_ble_phy_data.phy_mode_evpsym << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EVPSYMBOL_LUT_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_VALID_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_DMA_MEM_Pos)
+#define FIELD_CTRL_REG_RX_PAYLOAD \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CRC_Pos) | \
+ (PHY_WHITENING << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_WHITENING_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_DMA_MEM_Pos) | \
+ (g_ble_phy_data.phy_mode_evpsym << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EVPSYMBOL_LUT_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_VALID_Pos)
+#define FIELD_CTRL_REG_RX_PAYLOAD_WITH_EXC \
+ FIELD_CTRL_REG_RX_PAYLOAD | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EXC_ON_EXP_Pos)
+#define FIELD_CTRL_REG_RX_ENC_PAYLOAD \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_CRC_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_WHITENING_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_XL_DMA_CRYPTO_Pos) | \
+ (g_ble_phy_data.phy_mode_evpsym << \
+ CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_EVPSYMBOL_LUT_Pos) | \
+ (1 << CMAC_CM_FIELD_PUSH_CTRL_REG_FIELD_VALID_Pos)
+
+/* RF power up/down delays */
+#define PHY_DELAY_POWER_DN_RX (23)
+#define PHY_DELAY_POWER_DN_TX (23)
+#define PHY_DELAY_POWER_UP_RX (90)
+#define PHY_DELAY_POWER_UP_TX (75)
+#define PHY_DELAY_TX_RX ((PHY_DELAY_POWER_DN_TX) + (PHY_DELAY_POWER_UP_RX))
+#define PHY_DELAY_RX_TX ((PHY_DELAY_POWER_DN_RX) + (PHY_DELAY_POWER_UP_TX))
+
+/* RF TX/RX path delays */
+static const uint8_t g_ble_phy_path_delay_tx[2] = {
+ 4, /* 1M = 3.8us */
+ 0, /* 2M = 0.2us */
+};
+static const uint8_t g_ble_phy_path_delay_rx[2] = {
+ 2, /* 1M = 2.2us */
+ 1, /* 2M = 0.8us */
+};
+
+/* Measured and pre-calculated offsets for transitions */
+static const uint8_t g_ble_phy_frame_offset_txrx[4] = {
+ ((BLE_LL_IFS) - (PHY_DELAY_TX_RX) + (4)), /* 2M/1M */
+ ((BLE_LL_IFS) - (PHY_DELAY_TX_RX) + (5)), /* 1M/1M */
+ ((BLE_LL_IFS) - (PHY_DELAY_TX_RX) + (4)), /* 2M/2M */
+ ((BLE_LL_IFS) - (PHY_DELAY_TX_RX) + (5)), /* 1M/2M */
+};
+static const uint8_t g_ble_phy_frame_offset_rxtx[4] = {
+ ((BLE_LL_IFS) - (PHY_DELAY_RX_TX) - (5)), /* 2M/1M */
+ ((BLE_LL_IFS) - (PHY_DELAY_RX_TX) - (6)), /* 1M/1M */
+ ((BLE_LL_IFS) - (PHY_DELAY_RX_TX) - (3)), /* 2M/2M */
+ ((BLE_LL_IFS) - (PHY_DELAY_RX_TX) - (5)), /* 1M/2M */
+};
+
+/* packet start offsets (in usecs) */
+static const uint16_t g_ble_phy_mode_pkt_start_off[BLE_PHY_NUM_MODE] = { 376, 40, 24, 376 };
+
+struct ble_phy_data {
+ uint8_t phy_state; /* Current state */
+ uint8_t channel; /* Current PHY channel */
+ uint8_t phy_mode_cur; /* Current PHY mode */
+ uint8_t phy_mode_tx; /* TX PHY mode */
+ uint8_t phy_mode_rx; /* RX PHY mode */
+ uint8_t phy_mode_pre_len; /* Preamble length - 1 */
+ uint8_t phy_mode_evpsym; /* EVPSYMBOL_LUT value for fields */
+ uint8_t end_transition; /* Scheduled transition */
+ uint8_t path_delay_tx;
+ uint8_t path_delay_rx;
+ uint8_t frame_offset_txrx;
+ uint8_t frame_offset_rxtx;
+ uint8_t phy_rx_started;
+ uint8_t phy_encrypted;
+ uint8_t phy_privacy;
+#if MYNEWT_VAL(BLE_LL_DTM)
+ uint8_t phy_whitening; /* Whitening state (disabled for DTM) */
+#endif
+ uint32_t access_addr; /* Current access address */
+ uint32_t crc_init;
+ uint32_t llt_at_cputime;
+ uint32_t cputime_at_llt;
+ uint64_t start_llt;
+ struct ble_mbuf_hdr rxhdr;
+ ble_phy_tx_end_func txend_cb;
+ void *txend_arg;
+};
+
+static struct ble_phy_data g_ble_phy_data;
+
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
+/* Encryption related variables */
+struct ble_phy_encrypt_obj {
+ uint8_t key[16];
+ uint8_t b0[16];
+ uint8_t b1[16];
+ uint8_t ai[16];
+};
+
+struct ble_phy_encrypt_obj g_ble_phy_encrypt_data;
+
+static void ble_phy_tx_enc_start(void);
+static void ble_phy_rx_enc_start(uint8_t len);
+#endif
+
+#define SW_MAC_EXC_NONE (0)
+#define SW_MAC_EXC_LL_RX_END (1)
+#define SW_MAC_EXC_TXEND_CB (2)
+#define SW_MAC_EXC_LL_RX_START (3)
+#define SW_MAC_EXC_WFR_TIMER_EXP (4)
+
+static volatile uint8_t g_sw_mac_exc;
+
+/* Channel index to RF channel mapping */
+static const uint8_t g_ble_phy_chan_to_rf[BLE_PHY_NUM_CHANS] = {
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, /* 0-9 */
+ 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, /* 10-19 */
+ 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 20-29 */
+ 32, 33, 34, 35, 36, 37, 38, 0, 12, 39, /* 30-39 */
+};
+
+__attribute__((aligned(4)))
+static uint8_t g_ble_phy_tx_buf[BLE_PHY_MAX_PDU_LEN + 3];
+__attribute__((aligned(4)))
+static uint8_t g_ble_phy_rx_buf[BLE_PHY_MAX_PDU_LEN + 3];
+
+static void ble_phy_irq_field_tx(void);
+static void ble_phy_irq_field_rx(void);
+static void ble_phy_irq_frame_tx(void);
+static void ble_phy_irq_frame_rx(void);
+static bool ble_phy_rx_start_isr(void);
+static void ble_phy_rx_setup_fields(void);
+static void ble_phy_rx_setup_xcvr(void);
+static void ble_phy_mode_apply(uint8_t phy_mode);
+
+void
+FIELD_IRQHandler(void)
+{
+ MCU_DIAG_SER('E');
+
+ switch (g_ble_phy_data.phy_state) {
+ case BLE_PHY_STATE_TX:
+ ble_phy_irq_field_tx();
+ break;
+ case BLE_PHY_STATE_RX:
+ ble_phy_irq_field_rx();
+ break;
+ default:
+ STATS_INC(ble_phy_stats, radio_state_errs);
+ CMAC->CM_EXC_STAT_REG = 0xfffffffe;
+ break;
+ }
+
+ MCU_DIAG_SER('e');
+}
+
+void
+CALLBACK_IRQHandler(void)
+{
+ MCU_DIAG_SER('C');
+
+ /* XXX: clear these for now. */
+ (void)CMAC->CM_BS_SMPL_D_REG;
+
+ /* Clear IRQ*/
+ CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV1C_CALLBACK_VALID_CLR_Msk;
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_FIELD_ON_THR_EXP_Msk;
+
+ /*
+ * Program next frame for transition to TX. CM_EV_LINKUP_REG register to
+ * enable actual transition can be set later, we just need to make sure 2nd
+ * frame is already set before current frame is finished - this guarantees
+ * that frame for transition will be moved to 1st frame once current frame
+ * is popped off the queue.
+ */
+ CMAC->CM_FRAME_2_REG = CMAC_CM_FRAME_2_REG_FRAME_VALID_Msk |
+ CMAC_CM_FRAME_2_REG_FRAME_TX_Msk |
+ CMAC_CM_FRAME_2_REG_FRAME_EXC_ON_BS_START_Msk |
+ ((g_ble_phy_data.frame_offset_rxtx) <<
+ CMAC_CM_FRAME_2_REG_FRAME_START_OFFSET_Pos);
+
+ /*
+ * We just got an access address match so do this as early as possible
+ * to save time in the field rx isr.
+ */
+ ble_phy_rx_start_isr();
+
+ MCU_DIAG_SER('c');
+}
+
+void
+FRAME_IRQHandler(void)
+{
+ MCU_DIAG_SER('F');
+
+ switch (g_ble_phy_data.phy_state) {
+ case BLE_PHY_STATE_TX:
+ ble_phy_irq_frame_tx();
+ break;
+ case BLE_PHY_STATE_RX:
+ ble_phy_irq_frame_rx();
+ break;
+ default:
+ STATS_INC(ble_phy_stats, radio_state_errs);
+ CMAC->CM_EXC_STAT_REG = 0xfffffffe;
+ break;
+ }
+
+ MCU_DIAG_SER('f');
+}
+
+void
+SW_MAC_IRQHandler(void)
+{
+ uint8_t exc;
+ int rc;
+
+ MCU_DIAG_SER('S');
+
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_SW_MAC_Msk;
+ assert(g_sw_mac_exc);
+
+ exc = g_sw_mac_exc;
+ g_sw_mac_exc = 0;
+
+ MCU_DIAG_SER('0' + exc);
+
+ /* Next SW_MAC handover can now be queued */
+ os_arch_cmac_bs_ctrl_irq_unblock();
+
+ switch (exc) {
+ case SW_MAC_EXC_TXEND_CB:
+ assert(g_ble_phy_data.txend_cb);
+ g_ble_phy_data.txend_cb(g_ble_phy_data.txend_arg);
+ break;
+ case SW_MAC_EXC_WFR_TIMER_EXP:
+ ble_ll_wfr_timer_exp(NULL);
+ break;
+ case SW_MAC_EXC_LL_RX_START:
+ /* Call Link Layer receive start function */
+ rc = ble_ll_rx_start(&g_ble_phy_rx_buf[0], g_ble_phy_data.channel,
+ &g_ble_phy_data.rxhdr);
+ if (rc == 0) {
+ /* Set rx started flag and enable rx end ISR */
+ g_ble_phy_data.phy_rx_started = 1;
+
+ /* No transition */
+ CMAC->CM_EV_LINKUP_REG = CMAC_CM_EV_LINKUP_REG_LU_PHY_TO_IDLE_2_NONE_Msk |
+ CMAC_CM_EV_LINKUP_REG_LU_FRAME_START_2_NONE_Msk;
+ } else if (rc > 0) {
+ g_ble_phy_data.phy_rx_started = 1;
+
+ /* Setup transition */
+ CMAC->CM_EV_LINKUP_REG = CMAC_CM_EV_LINKUP_REG_LU_PHY_TO_IDLE_2_EXC_Msk |
+ CMAC_CM_EV_LINKUP_REG_LU_FRAME_START_2_PHY_TO_IDLE_Msk;
+ } else {
+ /* Disable PHY */
+ ble_phy_disable();
+ STATS_INC(ble_phy_stats, rx_aborts);
+ }
+ break;
+ case SW_MAC_EXC_LL_RX_END:
+ /* Call LL end processing */
+ rc = ble_ll_rx_end(&g_ble_phy_rx_buf[0], &g_ble_phy_data.rxhdr);
+ if (rc < 0) {
+ ble_phy_disable();
+ }
+ break;
+ default:
+ assert(0);
+ break;
+ }
+
+ MCU_DIAG_SER('s');
+}
+
+static inline uint32_t
+ble_phy_convert_and_record_start_time(uint32_t cputime, uint8_t rem_usecs)
+{
+ uint64_t ll_val;
+
+ ll_val = cmac_timer_convert_hal2llt(cputime);
+
+ /*
+ * Since we just converted cputime to the LL timer, record both these
+ * values as they will be used to calculate packet reception start time.
+ */
+ g_ble_phy_data.cputime_at_llt = cputime;
+ g_ble_phy_data.llt_at_cputime = ll_val;
+ g_ble_phy_data.start_llt = ll_val + rem_usecs;
+
+ return ll_val;
+}
+
+static inline void
+ble_phy_sw_mac_handover(uint8_t exc)
+{
+ assert(!g_sw_mac_exc);
+
+ g_sw_mac_exc = exc;
+
+ CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV1C_SW_MAC_Msk;
+
+ /*
+ * We want SW_MAC to be fired just after BS_CTRL interrupt so we block
+ * BS_CTRL temporarily and SW_MAC is next in order of interrupts priority.
+ */
+ os_arch_cmac_bs_ctrl_irq_block();
+}
+
+static void
+ble_phy_rx_end_isr(void)
+{
+ struct ble_mbuf_hdr *ble_hdr;
+
+ /* XXX just clear captured timer for now. Handle rx end time */
+ (void)CMAC->CM_TS1_REG;
+
+ /* Set RSSI and CRC status flag in header */
+ ble_hdr = &g_ble_phy_data.rxhdr;
+
+ /* Count PHY crc errors and valid packets */
+ if (CMAC->CM_CRC_REG != 0) {
+ STATS_INC(ble_phy_stats, rx_crc_err);
+ } else {
+ STATS_INC(ble_phy_stats, rx_valid);
+ ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_CRC_OK;
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
+ if (g_ble_phy_data.phy_encrypted) {
+ /* Only set MIC failure flag if frame is not zero length */
+ if (g_ble_phy_rx_buf[1] != 0) {
+ if (CMAC->CM_CRYPTO_STAT_REG != 0) {
+ ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_MIC_FAILURE;
+ } else {
+ g_ble_phy_rx_buf[1] = g_ble_phy_rx_buf[1] - 4;
+ }
+ }
+ }
+#endif
+ }
+
+ ble_phy_sw_mac_handover(SW_MAC_EXC_LL_RX_END);
+}
+
+static bool
+ble_phy_rx_start_isr(void)
+{
+ uint32_t llt32;
+ uint32_t llt_10_0;
+ uint32_t llt_10_0_mask;
+ uint32_t timestamp;
+ uint32_t ticks;
+ uint32_t usecs;
+ struct ble_mbuf_hdr *ble_hdr;
+
+ /* Initialize the ble mbuf header */
+ ble_hdr = &g_ble_phy_data.rxhdr;
+ ble_hdr->rxinfo.flags = ble_ll_state_get();
+ ble_hdr->rxinfo.channel = g_ble_phy_data.channel;
+ ble_hdr->rxinfo.handle = 0;
+ ble_hdr->rxinfo.phy = ble_phy_get_cur_phy();
+ ble_hdr->rxinfo.phy_mode = g_ble_phy_data.phy_mode_rx;
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
+ ble_hdr->rxinfo.user_data = NULL;
+#endif
+
+ /* Read the latched RSSI value */
+ ble_hdr->rxinfo.rssi = ble_rf_get_rssi();
+#if MYNEWT_VAL(CMAC_DEBUG_DATA_ENABLE)
+ g_cmac_shared_data.debug.last_rx_rssi = ble_hdr->rxinfo.rssi;
+#endif
+
+ /* Count rx starts */
+ STATS_INC(ble_phy_stats, rx_starts);
+
+ /*
+ * Calculate packet start time. Note that we have only received the
+ * access address at this point but we should have the 1st symbol and
+ * thus the timestamp should be set (this is based on looking at the diag
+ * signals). For now, lets make sure that the dirty bit is set. The
+ * dirty bit means that the timestamp was set since the last time cleared.
+ * Note that we need to read the timestamp first to guarantee it was set
+ * before reading the LL timer.
+ */
+ timestamp = CMAC->CM_TS1_REG;
+ assert((timestamp & CMAC_CM_TS1_REG_TS1_DIRTY_Msk) != 0);
+
+ /* Get the LL timer (only need 32 bits) */
+ llt32 = cmac_timer_read32();
+
+ /*
+ * We assume that the timestamp was set within 11 bits, or 2047 usecs, of
+ * when we read the ll timer. We assume this because we need to calculate
+ * the LL timer value at the timestamp. If the low 11 bits of the LL timer
+ * are greater than the timestamp, it means that the upper bits of the
+ * timestamp are correct. If the timestamp value is greater, it means the
+ * timer wrapped the 11 bits and we need to adjust the LL timer value.
+ */
+ llt_10_0_mask = (CMAC_CM_TS1_REG_TS1_TIMER1_9_0_Msk |
+ CMAC_CM_TS1_REG_TS1_TIMER1_10_Msk);
+ timestamp &= llt_10_0_mask;
+ llt_10_0 = llt32 & llt_10_0_mask;
+ llt32 &= ~llt_10_0_mask;
+ if (timestamp > llt_10_0) {
+ llt32 -= 2048;
+ }
+ llt32 |= timestamp;
+
+ /* Actual RX start time needs to account for preamble and access address */
+ llt32 -= g_ble_phy_mode_pkt_start_off[g_ble_phy_data.phy_mode_rx] +
+ g_ble_phy_data.path_delay_rx;
+
+ if (llt32 < g_ble_phy_data.llt_at_cputime) {
+ g_ble_phy_data.llt_at_cputime -= 31;
+ g_ble_phy_data.cputime_at_llt--;
+ }
+
+ /*
+ * We now have the LL timer when the packet was received. Get the cputime
+ * and the leftover usecs.
+ */
+ usecs = llt32 - g_ble_phy_data.llt_at_cputime;
+ ticks = os_cputime_usecs_to_ticks(usecs);
+ ble_hdr->beg_cputime = g_ble_phy_data.cputime_at_llt + ticks;
+ ble_hdr->rem_usecs = usecs - os_cputime_ticks_to_usecs(ticks);
+
+ return true;
+}
+
+static void
+ble_phy_irq_field_tx_exc_bs_start_4this(void)
+{
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_BS_START_4THIS_Msk;
+
+ if (g_ble_phy_data.end_transition == BLE_PHY_TRANSITION_TX_RX) {
+ /*
+ * Setup 2nd frame that will start after current one.
+ * -2us offset to adjust for allowed active clock accuracy.
+ */
+ CMAC->CM_FRAME_2_REG = CMAC_CM_FRAME_2_REG_FRAME_VALID_Msk |
+ (((g_ble_phy_data.frame_offset_txrx) - 2) <<
+ CMAC_CM_FRAME_2_REG_FRAME_START_OFFSET_Pos);
+
+ /* Next frame starts automatically on phy2idle */
+ CMAC->CM_EV_LINKUP_REG = CMAC_CM_EV_LINKUP_REG_LU_PHY_TO_IDLE_2_EXC_Msk |
+ CMAC_CM_EV_LINKUP_REG_LU_FRAME_START_2_PHY_TO_IDLE_Msk;
+
+ ble_phy_wfr_enable(BLE_PHY_WFR_ENABLE_TXRX, g_ble_phy_data.phy_mode_rx, 0);
+ } else {
+ CMAC->CM_EV_LINKUP_REG = CMAC_CM_EV_LINKUP_REG_LU_PHY_TO_IDLE_2_EXC_Msk |
+ CMAC_CM_EV_LINKUP_REG_LU_FRAME_START_2_NONE_Msk;
+ }
+
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
+ if (g_ble_phy_data.phy_encrypted && (g_ble_phy_tx_buf[1] != 0)) {
+ ble_phy_tx_enc_start();
+ }
+#endif
+}
+
+static void
+ble_phy_irq_field_tx_exc_field_on_thr_exp(void)
+{
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_FIELD_ON_THR_EXP_Msk;
+ (void)CMAC->CM_TS1_REG;
+
+ /* Set up remaining field (CRC) */
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_TX_CRC;
+}
+
+static void
+ble_phy_irq_field_tx(void)
+{
+ uint32_t stat;
+
+ stat = CMAC->CM_EXC_STAT_REG;
+
+ if (stat & CMAC_CM_EXC_STAT_REG_EXC_BS_START_4THIS_Msk) {
+ MCU_DIAG_SER('6');
+ ble_phy_irq_field_tx_exc_bs_start_4this();
+ }
+
+ if (stat & CMAC_CM_EXC_STAT_REG_EXC_FIELD_ON_THR_EXP_Msk) {
+ MCU_DIAG_SER('7');
+ ble_phy_irq_field_tx_exc_field_on_thr_exp();
+ }
+}
+
+static void
+ble_phy_irq_frame_tx_exc_bs_stop(void)
+{
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_BS_STOP_Msk;
+
+ /* Clear latched timestamp so we do not have error on next frame */
+ (void)CMAC->CM_TS1_REG;
+
+ if (g_ble_phy_data.end_transition == BLE_PHY_TRANSITION_TX_RX) {
+#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
+ ble_phy_mode_apply(g_ble_phy_data.phy_mode_rx);
+#endif
+ ble_phy_rx_setup_fields();
+ } else {
+ CMAC->CM_EV_LINKUP_REG = CMAC_CM_EV_LINKUP_REG_LU_PHY_TO_IDLE_2_EXC_Msk |
+ CMAC_CM_EV_LINKUP_REG_LU_FRAME_START_2_NONE_Msk;
+ }
+
+ if (g_ble_phy_data.txend_cb) {
+ ble_phy_sw_mac_handover(SW_MAC_EXC_TXEND_CB);
+ return;
+ }
+}
+
+static void
+ble_phy_irq_frame_tx_exc_phy_to_idle_4this(void)
+{
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_PHY_TO_IDLE_4THIS_Msk;
+
+ if (g_ble_phy_data.end_transition == BLE_PHY_TRANSITION_TX_RX) {
+ ble_phy_rx_setup_xcvr();
+
+ g_ble_phy_data.phy_state = BLE_PHY_STATE_RX;
+ } else {
+ /*
+ * Disable explicitly in case RX-TX was done (we cannot setup for auto
+ * disable in such case) */
+ ble_rf_stop();
+
+ g_ble_phy_data.phy_state = BLE_PHY_STATE_IDLE;
+ }
+
+ g_ble_phy_data.end_transition = BLE_PHY_TRANSITION_NONE;
+}
+
+static void
+ble_phy_irq_frame_tx(void)
+{
+ uint32_t stat;
+
+ stat = CMAC->CM_EXC_STAT_REG;
+
+ /*
+ * In case of phy2idle this should be first and only exception we handle
+ * here. This is because in case of TX-RX transition frame_start will occur
+ * at the same as phy2idle so we will have 2 exceptions here. To handle this
+ * properly we first need to handle phy2idle in TX state and keep frame_start
+ * pending so it will be called again in RX state.
+ */
+ if (stat & CMAC_CM_EXC_STAT_REG_EXC_PHY_TO_IDLE_4THIS_Msk) {
+ MCU_DIAG_SER('6');
+ ble_phy_irq_frame_tx_exc_phy_to_idle_4this();
+ return;
+ }
+
+ if (stat & CMAC_CM_EXC_STAT_REG_EXC_FRAME_START_Msk) {
+ MCU_DIAG_SER('7');
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_FRAME_START_Msk;
+ }
+
+ if (stat & CMAC_CM_EXC_STAT_REG_EXC_BS_STOP_Msk) {
+ MCU_DIAG_SER('8');
+ ble_phy_irq_frame_tx_exc_bs_stop();
+ }
+}
+
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
+static void
+ble_phy_field_rx_encrypted(uint32_t len)
+{
+ if (len) {
+ /*
+ * An encrypted frame should have a minimum length of 5
+ * bytes (at least one for payload and 4 for MIC). If the
+ * length is less than 5 this frame is bogus and will most
+ * likely fail CRC. We still need to process this frame
+ * though as we need to call the handover function with
+ * the frame. If this happens we will not bother to
+ * run the remaining bytes through the accelerator; just
+ * process them like normal and generate (a hopefully
+ * incorrect) CRC.
+ */
+ if (len >= 5) {
+ /* Start the crypto accelerator */
+ ble_phy_rx_enc_start(len);
+
+ /*
+ * We have already processed one byte; process remaining
+ * payload and MIC. Note: length contains MIC.
+ */
+ len -= 2;
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(4, len);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_ENC_PAYLOAD;
+
+ /* CRC */
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(4 + len, 3);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_CRC;
+ } else {
+ /* We have processed one byte so far. Send remaining
+ payload bytes to normal rx payload processing */
+ len -= 2;
+ if (len) {
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(4, len);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_PAYLOAD;
+ }
+
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(4 + len, 3);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_CRC;
+
+ /* Clear crypto pre-buffer */
+ CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_SW_REQ_PBUF_CLR_Msk;
+ }
+ } else {
+ /* We programmed one byte, so get next two bytes for CRC */
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(4, 1);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_CRC;
+ }
+}
+#endif
+
+static void
+ble_phy_field_rx_unencrypted(uint32_t len)
+{
+ uint8_t pduhdr;
+ uint8_t adva_thr;
+
+ if (len) {
+ pduhdr = g_ble_phy_rx_buf[0];
+ adva_thr = 0;
+
+ /*
+ * Setup interrupt after AdvA to start address resolving if
+ * privacy is enabled and TxAdd bit is set.
+ */
+ if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) &&
+ g_ble_phy_data.phy_privacy && (pduhdr & 0x40)) {
+
+ /*
+ * For legacy advertising AdvA ends at 6th byte.
+ * For extended advertising AdvA ends at 8th byte.
+ * We already programmed 2 bytes of payload so need
+ * to adjust threshold accordingly or just reset it
+ * in case there is not enough bytes in PDU to fit AdvA.
+ */
+ adva_thr = (pduhdr & 0x0f) == 0x07 ? 6 : 4;
+ if (len >= adva_thr + 2) {
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(4, adva_thr);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_PAYLOAD_WITH_EXC;
+ } else {
+ adva_thr = 0;
+ }
+ }
+
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(4 + adva_thr,
+ len - adva_thr);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_PAYLOAD;
+ }
+
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(4 + len, 1);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_CRC;
+}
+
+static void
+ble_phy_irq_field_rx_exc_field_on_thr_exp(void)
+{
+ uint32_t len;
+ uint32_t smpl;
+
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_FIELD_ON_THR_EXP_Msk;
+
+ smpl = CMAC->CM_BS_SMPL_ST_REG;
+
+ if ((smpl & CMAC_CM_BS_SMPL_ST_REG_FIELD_CNT_LATCHED_Msk) == 1) {
+ assert(g_ble_phy_data.phy_rx_started == 0);
+
+ /* Clear this */
+ (void)CMAC->CM_TS1_REG;
+
+ /* Read length of frame */
+ len = CMAC->CM_BS_SMPL_D_REG;
+ len = len & 0xFF;
+
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
+ if (g_ble_phy_data.phy_encrypted) {
+ ble_phy_field_rx_encrypted(len);
+ } else {
+ ble_phy_field_rx_unencrypted(len);
+ }
+#else
+ ble_phy_field_rx_unencrypted(len);
+#endif
+
+ ble_phy_sw_mac_handover(SW_MAC_EXC_LL_RX_START);
+ } else if ((smpl & CMAC_CM_BS_SMPL_ST_REG_FIELD_CNT_LATCHED_Msk) == 3) {
+ (void)CMAC->CM_BS_SMPL_D_REG;
+
+ assert(g_ble_phy_data.phy_privacy);
+
+ /*
+ * Resolve only if RPA is received. AdvA is at different offset
+ * in ExtAdv PDU. TxAdd was already checked before programming
+ * field threshold.
+ */
+ if ((g_ble_phy_rx_buf[0] & 0x0f) == 0x07) {
+ if ((g_ble_phy_rx_buf[9] & 0xc0) == 0x40) {
+ ble_hw_resolv_proc_start(&g_ble_phy_rx_buf[4]);
+ }
+ } else {
+ if ((g_ble_phy_rx_buf[7] & 0xc0) == 0x40) {
+ ble_hw_resolv_proc_start(&g_ble_phy_rx_buf[2]);
+ }
+ }
+ } else {
+ assert(0);
+ }
+}
+
+static void
+ble_phy_irq_field_rx_exc_stat_reg_exc_corr_timeout(void)
+{
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_CORR_TIMEOUT_Msk;
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_BS_STOP_Msk;
+ CMAC->CM_EV_LINKUP_REG = CMAC_CM_EV_LINKUP_REG_LU_FRAME_START_2_NONE_Msk;
+
+ ble_phy_sw_mac_handover(SW_MAC_EXC_WFR_TIMER_EXP);
+}
+
+static void
+ble_phy_irq_field_rx(void)
+{
+ uint32_t stat;
+
+ stat = CMAC->CM_EXC_STAT_REG;
+
+ if (stat & CMAC_CM_EXC_STAT_REG_EXC_FIELD_ON_THR_EXP_Msk) {
+ MCU_DIAG_SER('1');
+ ble_phy_irq_field_rx_exc_field_on_thr_exp();
+ }
+
+ if (stat & CMAC_CM_EXC_STAT_REG_EXC_CORR_TIMEOUT_Msk) {
+ MCU_DIAG_SER('2');
+ ble_phy_irq_field_rx_exc_stat_reg_exc_corr_timeout();
+ }
+}
+
+static void
+ble_phy_irq_frame_rx_exc_phy_to_idle_4this(void)
+{
+ uint8_t rf_chan;
+
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_PHY_TO_IDLE_4THIS_Msk;
+
+ /* We are here only on transition, so switch to TX */
+#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
+ ble_phy_mode_apply(g_ble_phy_data.phy_mode_tx);
+#endif
+ rf_chan = g_ble_phy_chan_to_rf[g_ble_phy_data.channel];
+ ble_rf_setup_tx(rf_chan, g_ble_phy_data.phy_mode_tx);
+ g_ble_phy_data.phy_state = BLE_PHY_STATE_TX;
+}
+
+static void
+ble_phy_irq_frame_rx_exc_frame_start(void)
+{
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_FRAME_START_Msk;
+}
+
+static void
+ble_phy_irq_frame_rx_exc_bs_stop(void)
+{
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_BS_STOP_Msk;
+ ble_phy_rx_end_isr();
+}
+
+static void
+ble_phy_irq_frame_rx(void)
+{
+ uint32_t stat;
+
+ stat = CMAC->CM_EXC_STAT_REG;
+
+ if (stat & CMAC_CM_EXC_STAT_REG_EXC_PHY_TO_IDLE_4THIS_Msk) {
+ MCU_DIAG_SER('3');
+ ble_phy_irq_frame_rx_exc_phy_to_idle_4this();
+ return;
+ }
+
+ if (stat & CMAC_CM_EXC_STAT_REG_EXC_FRAME_START_Msk) {
+ MCU_DIAG_SER('1');
+ ble_phy_irq_frame_rx_exc_frame_start();
+ }
+
+ if (stat & CMAC_CM_EXC_STAT_REG_EXC_BS_STOP_Msk) {
+ MCU_DIAG_SER('2');
+ ble_phy_irq_frame_rx_exc_bs_stop();
+ }
+}
+
+static void
+ble_phy_mode_apply(uint8_t phy_mode)
+{
+ if (phy_mode == g_ble_phy_data.phy_mode_cur) {
+ return;
+ }
+
+ switch (phy_mode) {
+ case BLE_PHY_MODE_1M:
+#if MYNEWT_VAL(BLE_PHY_RF_HP_MODE)
+ CMAC_SETREGF(CM_PHY_CTRL2_REG, CORR_CLK_MODE, 3);
+#endif
+ CMAC_SETREGF(CM_PHY_CTRL2_REG, PHY_MODE, 1);
+ g_ble_phy_data.phy_mode_evpsym = 1; /* 1000 ns per symbol */
+ g_ble_phy_data.phy_mode_pre_len = 7;
+ break;
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY)
+ case BLE_PHY_MODE_2M:
+#if MYNEWT_VAL(BLE_PHY_RF_HP_MODE)
+ CMAC_SETREGF(CM_PHY_CTRL2_REG, CORR_CLK_MODE, 2);
+#endif
+ CMAC_SETREGF(CM_PHY_CTRL2_REG, PHY_MODE, 0);
+ g_ble_phy_data.phy_mode_evpsym = 0; /* 500 ns per symbol */
+ g_ble_phy_data.phy_mode_pre_len = 15;
+ break;
+#endif
+ default:
+ assert(0);
+ return;
+ }
+
+ g_ble_phy_data.phy_mode_cur = phy_mode;
+}
+
+void
+ble_phy_mode_set(uint8_t tx_phy_mode, uint8_t rx_phy_mode)
+{
+ uint8_t txrx;
+ uint8_t rxtx;
+
+ g_ble_phy_data.phy_mode_tx = tx_phy_mode;
+ g_ble_phy_data.phy_mode_rx = rx_phy_mode;
+
+ g_ble_phy_data.path_delay_tx = g_ble_phy_path_delay_tx[tx_phy_mode - 1];
+ g_ble_phy_data.path_delay_rx = g_ble_phy_path_delay_rx[rx_phy_mode - 1];
+
+ /*
+ * Calculate index of transition in frame offset array without tons of
+ * branches. Note that transitions have to be in specific order in array.
+ *
+ * phy_mode 1M = 01b
+ * phy_mode 2M = 10b
+ *
+ * 1M/1M = 01b | 00b = 01b
+ * 1M/2M = 01b | 10b = 11b
+ * 2M/1M = 00b | 00b = 00b
+ * 2M/2M = 00b | 10b = 10b
+ */
+ txrx = (tx_phy_mode & 0x01) | (rx_phy_mode & 0x02);
+ rxtx = (rx_phy_mode & 0x01) | (tx_phy_mode & 0x02);
+ g_ble_phy_data.frame_offset_txrx = g_ble_phy_frame_offset_txrx[txrx];
+ g_ble_phy_data.frame_offset_rxtx = g_ble_phy_frame_offset_rxtx[rxtx];
+}
+
+int
+ble_phy_get_cur_phy(void)
+{
+#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
+ switch (g_ble_phy_data.phy_mode_cur) {
+ case BLE_PHY_MODE_1M:
+ return BLE_PHY_1M;
+ case BLE_PHY_MODE_2M:
+ return BLE_PHY_2M;
+ default:
+ assert(0);
+ return -1;
+ }
+#else
+ return BLE_PHY_1M;
+#endif
+}
+
+/**
+ * Copies the data from the phy receive buffer into a mbuf chain.
+ *
+ * @param dptr Pointer to receive buffer
+ * @param rxpdu Pointer to already allocated mbuf chain
+ *
+ * NOTE: the packet header already has the total mbuf length in it. The
+ * lengths of the individual mbufs are not set prior to calling.
+ *
+ */
+void
+ble_phy_rxpdu_copy(uint8_t *dptr, struct os_mbuf *rxpdu)
+{
+ uint32_t rem_len;
+ uint32_t copy_len;
+ uint32_t block_len;
+ uint32_t block_rem_len;
+ void *dst;
+ void *src;
+ struct os_mbuf * om;
+
+ /* Better be aligned */
+ assert(((uint32_t)dptr & 3) == 0);
+
+ block_len = rxpdu->om_omp->omp_databuf_len;
+ rem_len = OS_MBUF_PKTHDR(rxpdu)->omp_len;
+ src = dptr;
+
+ /*
+ * Setup for copying from first mbuf which is shorter due to packet header
+ * and extra leading space
+ */
+ copy_len = block_len - rxpdu->om_pkthdr_len - 4;
+ om = rxpdu;
+ dst = om->om_data;
+
+ while (om) {
+ /*
+ * Always copy blocks of length aligned to word size, only last mbuf
+ * will have remaining non-word size bytes appended.
+ */
+ block_rem_len = copy_len;
+ copy_len = min(copy_len, rem_len);
+ copy_len &= ~3;
+
+ dst = om->om_data;
+ om->om_len = copy_len;
+ rem_len -= copy_len;
+ block_rem_len -= copy_len;
+
+ __asm__ volatile (".syntax unified \n"
+ " mov r4, %[len] \n"
+ " b 2f \n"
+ "1: ldr r3, [%[src], %[len]] \n"
+ " str r3, [%[dst], %[len]] \n"
+ "2: subs %[len], #4 \n"
+ " bpl 1b \n"
+ " adds %[src], %[src], r4 \n"
+ " adds %[dst], %[dst], r4 \n"
+ : [dst] "+l" (dst), [src] "+l" (src),
+ [len] "+l" (copy_len)
+ :
+ : "r3", "r4", "memory");
+
+ if ((rem_len < 4) && (block_rem_len >= rem_len)) {
+ break;
+ }
+
+ /* Move to next mbuf */
+ om = SLIST_NEXT(om, om_next);
+ copy_len = block_len;
+ }
+
+ /* Copy remaining bytes, if any, to last mbuf */
+ om->om_len += rem_len;
+ __asm__ volatile (".syntax unified \n"
+ " b 2f \n"
+ "1: ldrb r3, [%[src], %[len]] \n"
+ " strb r3, [%[dst], %[len]] \n"
+ "2: subs %[len], #1 \n"
+ " bpl 1b \n"
+ : [len] "+l" (rem_len)
+ : [dst] "l" (dst), [src] "l" (src)
+ : "r3", "memory");
+
+ /* Copy header */
+ memcpy(BLE_MBUF_HDR_PTR(rxpdu), &g_ble_phy_data.rxhdr,
+ sizeof(struct ble_mbuf_hdr));
+}
+
+void
+ble_phy_wfr_enable(int txrx, uint8_t tx_phy_mode, uint32_t wfr_usecs)
+{
+ uint64_t llt;
+ uint32_t corr_window;
+ uint32_t llt_z_ticks;
+ uint32_t aa_time;
+
+ /*
+ * RX is started 2us earlier due to allowed clock accuracy and it should end
+ * 2us later for the same reason. Preamble is always 8us (8 symbols on 1M,
+ * 16 symbols on 2M) and Access Address is 32us on 1M and 16us on 2M. Add
+ * 1us just in case...
+ */
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_2M_PHY)
+ aa_time = 2 + (g_ble_phy_data.phy_mode_rx == BLE_PHY_MODE_1M ? 40 : 24) + 2 + 1;
+#else
+ aa_time = 45;
+#endif
+
+ if (txrx == BLE_PHY_WFR_ENABLE_TXRX) {
+ CMAC_SETREGF(CM_PHY_CTRL2_REG, CORR_WINDOW, aa_time);
+ CMAC->CM_EV_LINKUP_REG = CMAC_CM_EV_LINKUP_REG_LU_CORR_TMR_LD_2_CORR_START_Msk;
+ } else if (wfr_usecs < 16384) {
+ CMAC_SETREGF(CM_PHY_CTRL2_REG, CORR_WINDOW, wfr_usecs + aa_time);
+ CMAC->CM_EV_LINKUP_REG = CMAC_CM_EV_LINKUP_REG_LU_CORR_TMR_LD_2_CORR_START_Msk;
+ } else {
+ wfr_usecs += aa_time;
+ llt = g_ble_phy_data.start_llt;
+
+ /*
+ * wfr is outside range of CORR_WINDOW so we need to use LLT to start
+ * correlator timeout with some delay. Let's use ~10ms as new CORR_WINDOW
+ * value (does not really matter, just had to pick something) so need to
+ * calculate how many hi-Z ticks of delay we need.
+ */
+ llt_z_ticks = (wfr_usecs - 10000) / 1024;
+
+ /* New CORR_WINDOW is wfr adjusted by hi-Z ticks and remainder of 1st tick. */
+ corr_window = wfr_usecs;
+ corr_window -= llt_z_ticks * 1024;
+ corr_window -= 1024 - (llt & 0x3ff);
+
+ CMAC->CM_LL_TIMER1_36_10_EQ_Z_REG = (llt >> 10) + llt_z_ticks;
+ CMAC_SETREGF(CM_PHY_CTRL2_REG, CORR_WINDOW, corr_window);
+ CMAC->CM_EV_LINKUP_REG = CMAC_CM_EV_LINKUP_REG_LU_CORR_TMR_LD_2_TMR1_36_10_EQ_Z_Msk;
+ }
+}
+
+int
+ble_phy_init(void)
+{
+ g_ble_phy_data.phy_state = BLE_PHY_STATE_IDLE;
+#if MYNEWT_VAL(BLE_LL_DTM)
+ g_ble_phy_data.phy_whitening = 1;
+#endif
+
+ ble_rf_init();
+ ble_rf_calibrate_once();
+
+ /*
+ * 9_0_EQ_X can be linked to start RX/TX so we'll use this one for
+ * scheduling TX/RX start - make sure it's not linked to LL_TIMER2LLC
+ */
+ CMAC->CM_LL_INT_SEL_REG &= ~CMAC_CM_LL_INT_SEL_REG_LL_TIMER1_9_0_EQ_X_SEL_Msk;
+
+ CMAC->CM_PHY_CTRL_REG = ((PHY_DELAY_POWER_DN_RX - 1) << 24) |
+ ((PHY_DELAY_POWER_DN_TX - 1) << 16) |
+ ((PHY_DELAY_POWER_UP_RX - 1) << 8) |
+ ((PHY_DELAY_POWER_UP_TX - 1));
+
+#if MYNEWT_VAL(BLE_PHY_RF_HP_MODE)
+ CMAC->CM_PHY_CTRL2_REG = CMAC_CM_PHY_CTRL2_REG_PHY_MODE_Msk |
+ (3 << CMAC_CM_PHY_CTRL2_REG_CORR_CLK_MODE_Pos);
+#else
+ CMAC->CM_PHY_CTRL2_REG = CMAC_CM_PHY_CTRL2_REG_PHY_MODE_Msk |
+ (2 << CMAC_CM_PHY_CTRL2_REG_CORR_CLK_MODE_Pos);
+#endif
+
+ CMAC_SETREGF(CM_CTRL2_REG, WHITENING_MODE, 0);
+ CMAC_SETREGF(CM_CTRL2_REG, CRC_MODE, 0);
+
+ /* Setup for 1M by default */
+ ble_phy_mode_set(BLE_PHY_MODE_1M, BLE_PHY_MODE_1M);
+ ble_phy_mode_apply(BLE_PHY_MODE_1M);
+
+ NVIC_SetPriority(FIELD_IRQn, 0);
+ NVIC_SetPriority(CALLBACK_IRQn, 0);
+ NVIC_SetPriority(FRAME_IRQn, 0);
+ NVIC_SetPriority(CRYPTO_IRQn, 1);
+ NVIC_SetPriority(SW_MAC_IRQn, 1);
+ NVIC_EnableIRQ(FIELD_IRQn);
+ NVIC_EnableIRQ(CALLBACK_IRQn);
+ NVIC_EnableIRQ(FRAME_IRQn);
+ NVIC_EnableIRQ(SW_MAC_IRQn);
+
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
+ /* Initialize non-zero fixed values in CCM blocks */
+ g_ble_phy_encrypt_data.b0[0] = 0x49;
+ g_ble_phy_encrypt_data.b1[1] = 0x01;
+ g_ble_phy_encrypt_data.ai[0] = 0x01;
+#endif
+
+ return 0;
+}
+
+void
+ble_phy_disable(void)
+{
+ MCU_DIAG_SER('D');
+
+ __disable_irq();
+
+ CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV1C_BS_CLEAR_Msk;
+
+ __NOP();
+ __NOP();
+ __NOP();
+ __NOP();
+ __NOP();
+
+ CMAC->CM_EXC_STAT_REG = CMAC_CM_EXC_STAT_REG_EXC_FIELD_ON_THR_EXP_Msk |
+ CMAC_CM_EXC_STAT_REG_EXC_BS_START_4THIS_Msk |
+ CMAC_CM_EXC_STAT_REG_EXC_CORR_TIMEOUT_Msk |
+ CMAC_CM_EXC_STAT_REG_EXC_BS_STOP_Msk |
+ CMAC_CM_EXC_STAT_REG_EXC_FRAME_START_Msk |
+ CMAC_CM_EXC_STAT_REG_EXC_PHY_TO_IDLE_4THIS_Msk |
+ CMAC_CM_EXC_STAT_REG_EXC_SW_MAC_Msk;
+
+ NVIC->ICPR[0] = (1 << FIELD_IRQn) | (1 << CALLBACK_IRQn) |
+ (1 << FRAME_IRQn) | (1 << SW_MAC_IRQn);
+
+ os_arch_cmac_bs_ctrl_irq_unblock();
+ g_sw_mac_exc = 0;
+
+ CMAC->CM_ERROR_DIS_REG = 0;
+
+ ble_rf_stop();
+
+ /*
+ * If ble_phy_disable is called precisely when access address was matched,
+ * ts1_dirty may not be cleared properly. This is because bs_clear will
+ * cause bitstream controller to be stopped and we won't get callback_irq,
+ * but seems like demodulator is still active for a while and will trigger
+ * ev1c_ts1_trigger on 1st symbol which will set ts1_dirty. We do not expect
+ * ts1_dirty to be set after bs_clear so we won't clear it. To workaround
+ * his, we can just clear it explicitly here after everything is already
+ * disabled.
+ */
+ (void)CMAC->CM_TS1_REG;
+
+ __enable_irq();
+
+ g_ble_phy_data.phy_state = BLE_PHY_STATE_IDLE;
+}
+
+static void
+ble_phy_rx_setup_fields(void)
+{
+ /* Make sure CRC LFSR initial value is set */
+ CMAC_SETREGF(CM_CRC_REG, CRC_INIT_VAL, g_ble_phy_data.crc_init);
+
+ CMAC->CM_FIELD_PUSH_DATA_REG = g_ble_phy_data.access_addr;
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_ACCESS_ADDR;
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(0, 2);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_HEADER;
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
+ if (g_ble_phy_data.phy_encrypted) {
+ /* Only program one byte for encrypted payloads */
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(2, 2);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_ENC_PAYLOAD;
+ } else {
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(2, 2);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_PAYLOAD;
+ }
+#else
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_RX(2, 2);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_RX_PAYLOAD;
+#endif
+}
+
+static void
+ble_phy_rx_setup_xcvr(void)
+{
+ uint8_t rf_chan = g_ble_phy_chan_to_rf[g_ble_phy_data.channel];
+
+ CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV1C_CALLBACK_VALID_SET_Msk;
+
+ ble_rf_setup_rx(rf_chan, g_ble_phy_data.phy_mode_rx);
+
+ g_ble_phy_data.phy_rx_started = 0;
+}
+
+int
+ble_phy_rx(void)
+{
+ MCU_DIAG_SER('R');
+
+ ble_rf_configure();
+ ble_phy_rx_setup_xcvr();
+
+ CMAC->CM_FRAME_1_REG = CMAC_CM_FRAME_1_REG_FRAME_VALID_Msk;
+
+ CMAC_SETREGF(CM_PHY_CTRL2_REG, CORR_WINDOW, 0);
+
+ ble_phy_rx_setup_fields();
+
+ g_ble_phy_data.phy_state = BLE_PHY_STATE_RX;
+
+ return 0;
+}
+
+int
+ble_phy_rx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
+{
+ uint32_t ll_val32;
+ int32_t time_till_start;
+
+ MCU_DIAG_SER('r');
+
+#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
+ ble_phy_mode_apply(g_ble_phy_data.phy_mode_rx);
+#endif
+
+ assert(ble_rf_is_enabled());
+
+ ble_phy_rx();
+
+ /* Get LL timer at cputime */
+ ll_val32 = ble_phy_convert_and_record_start_time(cputime, rem_usecs);
+
+ /* Add remaining usecs to get exact receive start time */
+ ll_val32 += rem_usecs;
+
+ /* Adjust start time for rx delays */
+ ll_val32 -= PHY_DELAY_POWER_UP_RX - g_ble_phy_data.path_delay_rx;
+
+ __disable_irq();
+ CMAC->CM_LL_TIMER1_9_0_EQ_X_REG = ll_val32;
+ CMAC->CM_EV_LINKUP_REG =
+ CMAC_CM_EV_LINKUP_REG_LU_FRAME_START_2_TMR1_9_0_EQ_X_Msk;
+
+ time_till_start = (int32_t)(ll_val32 - cmac_timer_read32());
+ if (time_till_start <= 0) {
+ /*
+ * Possible we missed the frame start! If we have, we need to start
+ * ASAP.
+ */
+ if ((CMAC->CM_EXC_STAT_REG & CMAC_CM_EXC_STAT_REG_EXC_FRAME_START_Msk) == 0) {
+ /* We missed start. Start now */
+ CMAC->CM_EV_LINKUP_REG =
+ CMAC_CM_EV_LINKUP_REG_LU_FRAME_START_2_ASAP_Msk;
+ }
+ }
+ __enable_irq();
+
+ return 0;
+}
+
+int
+ble_phy_tx(ble_phy_tx_pducb_t pducb, void *pducb_arg, uint8_t end_trans)
+{
+ uint8_t *txbuf = g_ble_phy_tx_buf;
+ int rc;
+
+ MCU_DIAG_SER('T');
+
+ assert(CMAC->CM_FRAME_1_REG & CMAC_CM_FRAME_1_REG_FRAME_TX_Msk);
+
+ g_ble_phy_data.end_transition = end_trans;
+
+ /*
+ * Program required fields now so in worst case TX can continue while we
+ * are still preparing header and payload.
+ */
+ CMAC->CM_FIELD_PUSH_DATA_REG = g_ble_phy_data.access_addr & 1 ? 0x5555 : 0xaaaa;
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_TX_PREAMBLE;
+ CMAC->CM_FIELD_PUSH_DATA_REG = g_ble_phy_data.access_addr;
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_TX_ACCESS_ADDR;
+
+ /* Make sure CRC LFSR initial value is set */
+ CMAC_SETREGF(CM_CRC_REG, CRC_INIT_VAL, g_ble_phy_data.crc_init);
+
+ /* txbuf[0] is hdr_byte, txbuf[1] is pkt_len */
+ txbuf[1] = pducb(&txbuf[2], pducb_arg, &txbuf[0]);
+
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
+ if (g_ble_phy_data.phy_encrypted && (txbuf[1] != 0)) {
+ /* We have to add the MIC to the length */
+ txbuf[1] += BLE_LL_DATA_MIC_LEN;
+
+ /* Program header field */
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_TX(0, 2);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_TX_PAYLOAD;
+
+ /* Program payload (and MIC) */
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_TX(2, txbuf[1]);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_TX_ENC_PAYLOAD;
+ } else {
+ /* Program header and payload fields */
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_TX(0, txbuf[1] + 2);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_TX_PAYLOAD;
+ }
+#else
+ /* Program header and payload fields */
+ CMAC->CM_FIELD_PUSH_DATA_REG = FIELD_DATA_REG_DMA_TX(0, txbuf[1] + 2);
+ CMAC->CM_FIELD_PUSH_CTRL_REG = FIELD_CTRL_REG_TX_PAYLOAD;
+#endif
+
+ /*
+ * If there was FIELD_ON_THR exception it means access address was already
+ * sent and we are likely too late here - abort.
+ */
+ if (CMAC->CM_EXC_STAT_REG & CMAC_CM_EXC_STAT_REG_EXC_FIELD_ON_THR_EXP_Msk) {
+ ble_phy_disable();
+ g_ble_phy_data.end_transition = BLE_PHY_TRANSITION_NONE;
+ STATS_INC(ble_phy_stats, tx_late);
+ rc = BLE_PHY_ERR_RADIO_STATE;
+ } else {
+ if (g_ble_phy_data.phy_state == BLE_PHY_STATE_IDLE) {
+ g_ble_phy_data.phy_state = BLE_PHY_STATE_TX;
+ }
+ STATS_INC(ble_phy_stats, tx_good);
+ STATS_INCN(ble_phy_stats, tx_bytes, txbuf[1] + 2);
+ rc = BLE_ERR_SUCCESS;
+ }
+
+ /* Now we can handle BS_CTRL */
+ CMAC->CM_ERROR_DIS_REG &= ~CMAC_CM_ERROR_DIS_REG_CM_FIELD1_ERR_Msk;
+ NVIC_EnableIRQ(FRAME_IRQn);
+ NVIC_EnableIRQ(FIELD_IRQn);
+
+ return rc;
+}
+
+int
+ble_phy_tx_set_start_time(uint32_t cputime, uint8_t rem_usecs)
+{
+ uint8_t rf_chan = g_ble_phy_chan_to_rf[g_ble_phy_data.channel];
+ uint32_t ll_val32;
+ int rc;
+
+ MCU_DIAG_SER('t');
+
+#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
+ ble_phy_mode_apply(g_ble_phy_data.phy_mode_tx);
+#endif
+
+ assert(ble_rf_is_enabled());
+
+ ble_rf_configure();
+ ble_rf_setup_tx(rf_chan, g_ble_phy_data.phy_mode_tx);
+
+ ll_val32 = ble_phy_convert_and_record_start_time(cputime, rem_usecs);
+ ll_val32 += rem_usecs;
+ ll_val32 -= PHY_DELAY_POWER_UP_TX + g_ble_phy_data.path_delay_tx;
+ /* we can schedule TX only up to 1023us in advance */
+ assert((int32_t)(ll_val32 - cmac_timer_read32()) < 1024);
+
+ /*
+ * We do not want FIELD/FRAME interrupts or FIELD1_ERR until ble_phy_tx()
+ * has finished pushing all the fields. If we won't make it on time, we
+ * assume tx_late and abort TX.
+ */
+ NVIC_DisableIRQ(FRAME_IRQn);
+ NVIC_DisableIRQ(FIELD_IRQn);
+ CMAC->CM_ERROR_DIS_REG |= CMAC_CM_ERROR_DIS_REG_CM_FIELD1_ERR_Msk;
+
+ CMAC->CM_LL_TIMER1_9_0_EQ_X_REG = ll_val32;
+ CMAC->CM_EV_LINKUP_REG = CMAC_CM_EV_LINKUP_REG_LU_FRAME_START_2_TMR1_9_0_EQ_X_Msk;
+
+ if ((int32_t)(ll_val32 - cmac_timer_read32()) < 0) {
+ STATS_INC(ble_phy_stats, tx_late);
+ ble_phy_disable();
+ NVIC_EnableIRQ(FRAME_IRQn);
+ NVIC_EnableIRQ(FIELD_IRQn);
+ rc = BLE_PHY_ERR_TX_LATE;
+ } else {
+ /*
+ * Program frame now since it needs to be ready for FRAME_START, we can
+ * push fields later
+ */
+ CMAC->CM_FRAME_1_REG = CMAC_CM_FRAME_1_REG_FRAME_VALID_Msk |
+ CMAC_CM_FRAME_1_REG_FRAME_TX_Msk |
+ CMAC_CM_FRAME_2_REG_FRAME_EXC_ON_BS_START_Msk;
+ rc = 0;
+ }
+
+ return rc;
+}
+
+void
+ble_phy_set_txend_cb(ble_phy_tx_end_func txend_cb, void *arg)
+{
+ g_ble_phy_data.txend_cb = txend_cb;
+ g_ble_phy_data.txend_arg = arg;
+}
+
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
+void
+ble_phy_tx_enc_start(void)
+{
+ struct ble_phy_encrypt_obj *enc;
+
+ enc = &g_ble_phy_encrypt_data;
+ enc->b0[15] = g_ble_phy_tx_buf[1] - 4;
+ enc->b1[2] = g_ble_phy_tx_buf[0] & BLE_LL_DATA_HDR_LLID_MASK;
+
+ /* XXX: should we check for busy? */
+ /* XXX: might not be needed, but for now terminate any crypto operations. */
+ CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_SW_REQ_ABORT_Msk;
+
+ CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_IN_SEL_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_CTR_MAC_EN_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_CTR_PLD_EN_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_AUTH_EN_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_ENC_DECN_Msk;
+
+ /* Start crypto */
+ CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV_CRYPTO_START_Msk;
+}
+
+void
+ble_phy_rx_enc_start(uint8_t len)
+{
+ struct ble_phy_encrypt_obj *enc;
+
+ enc = &g_ble_phy_encrypt_data;
+ enc->b0[15] = len - 4; /* length without MIC as length includes MIC */
+ enc->b1[2] = g_ble_phy_rx_buf[0] & BLE_LL_DATA_HDR_LLID_MASK;
+
+ /* XXX: should we check for busy? */
+ /* XXX: might not be needed, but for now terminate any crypto operations. */
+ //CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_SW_REQ_ABORT_Msk;
+
+ CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_OUT_SEL_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_CTR_MAC_EN_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_CTR_PLD_EN_Msk |
+ CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_AUTH_EN_Msk;
+
+ /* Start crypto */
+ CMAC->CM_EV_SET_REG = CMAC_CM_EV_SET_REG_EV_CRYPTO_START_Msk;
+}
+
+void
+ble_phy_encrypt_enable(uint64_t pkt_counter, uint8_t *iv, uint8_t *key,
+ uint8_t is_master)
+{
+ struct ble_phy_encrypt_obj *enc;
+
+ enc = &g_ble_phy_encrypt_data;
+ memcpy(enc->key, key, 16);
+ memcpy(&enc->b0[6], iv, 8);
+ put_le32(&enc->b0[1], pkt_counter);
+ enc->b0[5] = is_master ? 0x80 : 0;
+ memcpy(&enc->ai[6], iv, 8);
+ put_le32(&enc->ai[1], pkt_counter);
+ enc->ai[5] = enc->b0[5];
+
+ g_ble_phy_data.phy_encrypted = 1;
+
+ /* Program key registers */
+ CMAC->CM_CRYPTO_KEY_31_0_REG = get_le32(&enc->key[0]);
+ CMAC->CM_CRYPTO_KEY_63_32_REG = get_le32(&enc->key[4]);
+ CMAC->CM_CRYPTO_KEY_95_64_REG = get_le32(&enc->key[8]);
+ CMAC->CM_CRYPTO_KEY_127_96_REG = get_le32(&enc->key[12]);
+
+ /* Program ADRx registers */
+ CMAC->CM_CRYPTO_IN_ADR0_REG = (uint32_t)enc->b1;
+ CMAC->CM_CRYPTO_IN_ADR1_REG = (uint32_t)enc->b0;
+ CMAC->CM_CRYPTO_IN_ADR2_REG = (uint32_t)&g_ble_phy_tx_buf[2];
+ CMAC->CM_CRYPTO_IN_ADR3_REG = (uint32_t)enc->ai;
+ CMAC->CM_CRYPTO_OUT_ADR_REG = (uint32_t)&g_ble_phy_rx_buf[2];
+
+ CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_SW_REQ_PBUF_CLR_Msk;
+}
+
+void
+ble_phy_encrypt_set_pkt_cntr(uint64_t pkt_counter, int dir)
+{
+ struct ble_phy_encrypt_obj *enc;
+
+ enc = &g_ble_phy_encrypt_data;
+ put_le32(&enc->b0[1], pkt_counter);
+ enc->b0[5] = dir ? 0x80 : 0;
+ put_le32(&enc->ai[1], pkt_counter);
+ enc->ai[5] = enc->b0[5];
+
+ CMAC->CM_CRYPTO_CTRL_REG = CMAC_CM_CRYPTO_CTRL_REG_CM_CRYPTO_SW_REQ_PBUF_CLR_Msk;
+}
+
+void
+ble_phy_encrypt_disable(void)
+{
+ g_ble_phy_data.phy_encrypted = 0;
+}
+#endif
+
+int
+ble_phy_txpwr_set(int dbm)
+{
+#if MYNEWT_VAL(CMAC_DEBUG_DATA_ENABLE)
+ if (g_cmac_shared_data.debug.tx_power_override != INT8_MAX) {
+ ble_rf_set_tx_power(g_cmac_shared_data.debug.tx_power_override);
+ } else {
+ ble_rf_set_tx_power(dbm);
+ }
+#else
+ ble_rf_set_tx_power(dbm);
+#endif
+
+ return 0;
+}
+
+int
+ble_phy_txpower_round(int dbm)
+{
+ return 0;
+}
+
+void
+ble_phy_set_rx_pwr_compensation(int8_t compensation)
+{
+}
+
+int
+ble_phy_setchan(uint8_t chan, uint32_t access_addr, uint32_t crc_init)
+{
+ uint8_t rf_chan = g_ble_phy_chan_to_rf[chan];
+
+ assert(chan < BLE_PHY_NUM_CHANS);
+
+ if (chan >= BLE_PHY_NUM_CHANS) {
+ return BLE_PHY_ERR_INV_PARAM;
+ }
+
+ g_ble_phy_data.channel = chan;
+ g_ble_phy_data.access_addr = access_addr;
+ g_ble_phy_data.crc_init = crc_init;
+
+ CMAC_SETREGF(CM_PHY_CTRL2_REG, PHY_RF_CHANNEL, rf_chan);
+
+ return 0;
+}
+
+void
+ble_phy_restart_rx(void)
+{
+ /* XXX: for now, we will disable the phy using ble_phy_disable and then
+ re-enable it
+ */
+ ble_phy_disable();
+
+ /* Apply mode before starting RX */
+#if (BLE_LL_BT5_PHY_SUPPORTED == 1)
+ ble_phy_mode_apply(g_ble_phy_data.phy_mode_rx);
+#endif
+
+ /* Setup phy to rx again */
+ ble_phy_rx();
+
+ /* Start reception now */
+ CMAC->CM_EV_LINKUP_REG = CMAC_CM_EV_LINKUP_REG_LU_FRAME_START_2_ASAP_Msk;
+}
+
+uint32_t
+ble_phy_access_addr_get(void)
+{
+ return g_ble_phy_data.access_addr;
+}
+
+int
+ble_phy_state_get(void)
+{
+ return g_ble_phy_data.phy_state;
+}
+
+int
+ble_phy_rx_started(void)
+{
+ return g_ble_phy_data.phy_rx_started;
+}
+
+uint8_t
+ble_phy_xcvr_state_get(void)
+{
+ return ble_rf_is_enabled();
+}
+
+uint8_t
+ble_phy_max_data_pdu_pyld(void)
+{
+ return BLE_LL_DATA_PDU_MAX_PYLD;
+}
+
+void
+ble_phy_resolv_list_enable(void)
+{
+ g_ble_phy_data.phy_privacy = 1;
+
+ ble_hw_resolv_proc_enable();
+}
+
+void
+ble_phy_resolv_list_disable(void)
+{
+ ble_hw_resolv_proc_disable();
+
+ g_ble_phy_data.phy_privacy = 0;
+}
+
+void
+ble_phy_rfclk_enable(void)
+{
+ ble_rf_enable();
+}
+
+void
+ble_phy_rfclk_disable(void)
+{
+ /* XXX We can't disable RF while PHY_BUSY is asserted so let's wait a bit */
+ while (CMAC->CM_DIAG_WORD2_REG & CMAC_CM_DIAG_WORD2_REG_DIAG2_PHY_BUSY_BUF_Msk);
+
+ ble_rf_disable();
+}
+
+#if MYNEWT_VAL(BLE_LL_DTM)
+void
+ble_phy_enable_dtm(void)
+{
+ g_ble_phy_data.phy_whitening = 0;
+}
+
+void
+ble_phy_disable_dtm(void)
+{
+ g_ble_phy_data.phy_whitening = 1;
+}
+#endif
diff --git a/nimble/drivers/dialog_cmac/src/ble_rf.c b/nimble/drivers/dialog_cmac/src/ble_rf.c
new file mode 100644
index 0000000..6197c45
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/src/ble_rf.c
@@ -0,0 +1,729 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include "mcu/mcu.h"
+#include "mcu/cmac_timer.h"
+#include "controller/ble_phy.h"
+#include "cmac_driver/cmac_shared.h"
+#include "ble_rf_priv.h"
+
+#define RF_CALIBRATION_0 (0x01)
+#define RF_CALIBRATION_1 (0x02)
+#define RF_CALIBRATION_2 (0x04)
+
+static const int8_t g_ble_rf_power_lvls[] = {
+ -18, -12, -8, -6, -3, -2, -1, 0, 1, 2, 3, 4, 4, 5, 6
+};
+
+struct ble_phy_rf_data {
+ uint8_t tx_power_cfg0;
+ uint8_t tx_power_cfg1;
+ uint8_t tx_power_cfg2;
+ uint8_t tx_power_cfg3;
+ uint32_t cal_res_1;
+ uint32_t cal_res_2;
+ uint32_t trim_val1_tx_1;
+ uint32_t trim_val1_tx_2;
+ uint32_t trim_val2_tx;
+ uint32_t trim_val2_rx;
+};
+
+static struct ble_phy_rf_data g_ble_phy_rf_data;
+
+static inline uint32_t
+get_reg32(uint32_t addr)
+{
+ volatile uint32_t *reg = (volatile uint32_t *)addr;
+
+ return *reg;
+}
+
+static inline uint32_t
+get_reg32_bits(uint32_t addr, uint32_t mask)
+{
+ volatile uint32_t *reg = (volatile uint32_t *)addr;
+
+ return (*reg & mask) >> __builtin_ctz(mask);
+}
+
+static inline void
+set_reg8(uint32_t addr, uint8_t val)
+{
+ volatile uint8_t *reg = (volatile uint8_t *)addr;
+
+ *reg = val;
+}
+
+static inline void
+set_reg16(uint32_t addr, uint16_t val)
+{
+ volatile uint16_t *reg = (volatile uint16_t *)addr;
+
+ *reg = val;
+}
+
+static inline void
+set_reg32(uint32_t addr, uint32_t val)
+{
+ volatile uint32_t *reg = (volatile uint32_t *)addr;
+
+ *reg = val;
+}
+
+static inline void
+set_reg32_bits(uint32_t addr, uint32_t mask, uint32_t val)
+{
+ volatile uint32_t *reg = (volatile uint32_t *)addr;
+
+ *reg = (*reg & (~mask)) | (val << __builtin_ctz(mask));
+}
+
+static inline void
+set_reg32_mask(uint32_t addr, uint32_t mask, uint32_t val)
+{
+ volatile uint32_t *reg = (volatile uint32_t *)addr;
+
+ *reg = (*reg & (~mask)) | (val & mask);
+}
+
+static inline void
+set_reg16_mask(uint32_t addr, uint16_t mask, uint16_t val)
+{
+ volatile uint16_t *reg = (volatile uint16_t *)addr;
+
+ *reg = (*reg & (~mask)) | (val & mask);
+}
+
+static void
+delay_us(uint32_t delay_us)
+{
+ while (delay_us--) {
+ __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
+ __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
+ __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
+ __NOP(); __NOP(); __NOP(); __NOP();
+ }
+}
+
+static void
+ble_rf_apply_trim(volatile uint32_t *tv, unsigned len)
+{
+ while (len) {
+ *(volatile uint32_t *)tv[0] = tv[1];
+ len -= 2;
+ tv += 2;
+ }
+}
+
+static void
+ble_rf_apply_calibration(void)
+{
+ set_reg32(0x40020094, g_ble_phy_rf_data.cal_res_1);
+ if (g_ble_phy_rf_data.cal_res_2) {
+ set_reg32_bits(0x40022018, 0xff800000, g_ble_phy_rf_data.cal_res_2);
+ set_reg32_bits(0x40022018, 0x00007fc0, g_ble_phy_rf_data.cal_res_2);
+ }
+}
+
+static inline void
+ble_rf_ldo_on(void)
+{
+ set_reg8(0x40020004, 9);
+}
+
+static inline void
+ble_rf_ldo_off(void)
+{
+ set_reg8(0x40020004, 0);
+}
+
+static inline void
+ble_rf_rfcu_enable(void)
+{
+ set_reg32_bits(0x50000010, 0x00000020, 1);
+}
+
+static inline void
+ble_rf_rfcu_disable(void)
+{
+ set_reg32_bits(0x50000010, 0x00000020, 0);
+}
+
+static void
+ble_rf_rfcu_apply_recommended_settings(void)
+{
+ set_reg16_mask(0x400200a0, 0x0001, 0x0001);
+ set_reg16_mask(0x40021020, 0x03f0, 0x02f5);
+ set_reg32_mask(0x40021018, 0x001fffff, 0x005a5809);
+ set_reg32_mask(0x4002101c, 0x00001e01, 0x0040128c);
+ set_reg32_mask(0x40021004, 0xffffff1f, 0x64442404);
+ set_reg32_mask(0x40021008, 0xfcfcffff, 0x6b676665);
+ set_reg32_mask(0x4002100c, 0x00fcfcfc, 0x9793736f);
+ set_reg32_mask(0x40021010, 0x1f1f1c1f, 0x04072646);
+ set_reg32_mask(0x40020000, 0x001ff000, 0x0f099820);
+ set_reg16_mask(0x40020348, 0x00ff, 0x0855);
+ set_reg16(0x40020350, 0x0234);
+ set_reg16(0x40020354, 0x0a34);
+ set_reg16(0x40020358, 0x0851);
+ set_reg16(0x4002035c, 0x0a26);
+ set_reg16(0x40020360, 0x0858);
+ set_reg16(0x4002102c, 0xdfe7);
+ set_reg32_mask(0x4002103c, 0x00c00000, 0x0024a19f);
+ set_reg16_mask(0x40021000, 0x0008, 0x000b);
+ set_reg16_mask(0x40020238, 0x03e0, 0x02c0);
+ set_reg16_mask(0x4002023c, 0x03e0, 0x02c0);
+ set_reg16_mask(0x40020244, 0x03e0, 0x0250);
+ set_reg16_mask(0x40020248, 0x03e0, 0x02a0);
+ set_reg16_mask(0x4002024c, 0x03e0, 0x02c0);
+ set_reg16_mask(0x40020288, 0x03e0, 0x0300);
+ set_reg16_mask(0x4002029c, 0x001f, 0x0019);
+ set_reg16_mask(0x4002003c, 0x6000, 0x0788);
+ set_reg16_mask(0x40020074, 0x7f00, 0x2007);
+ set_reg32_mask(0x40020080, 0x00333330, 0x00222224);
+ set_reg32_mask(0x40020068, 0x00000f0f, 0x00000f0d);
+}
+
+static void
+ble_rf_rfcu_apply_settings(void)
+{
+ ble_rf_apply_trim(g_cmac_shared_data.trim.rfcu,
+ g_cmac_shared_data.trim.rfcu_len);
+ ble_rf_rfcu_apply_recommended_settings();
+}
+
+static inline void
+ble_rf_synth_enable(void)
+{
+ set_reg8(0x40020005, 3);
+}
+
+static inline void
+ble_rf_synth_disable(void)
+{
+ set_reg8(0x40020005, 0);
+ __NOP();
+ __NOP();
+}
+
+static bool
+ble_rf_synth_is_enabled(void)
+{
+ return get_reg32_bits(0x40020004, 256);
+}
+
+static void
+ble_rf_synth_apply_recommended_settings(void)
+{
+ set_reg32_mask(0x40022048, 0x0000000c, 0x000000d5);
+ set_reg32_mask(0x40022050, 0x00000300, 0x00000300);
+ set_reg16_mask(0x40022024, 0x0001, 0x0001);
+}
+
+static void
+ble_rf_synth_apply_settings(void)
+{
+ ble_rf_apply_trim(g_cmac_shared_data.trim.synth,
+ g_cmac_shared_data.trim.synth_len);
+ ble_rf_synth_apply_recommended_settings();
+}
+
+static void
+ble_rf_calibration_0(void)
+{
+ uint32_t bkp[10];
+
+ bkp[0] = get_reg32(0x40020208);
+ bkp[1] = get_reg32(0x40020250);
+ bkp[2] = get_reg32(0x40020254);
+ bkp[3] = get_reg32(0x40021028);
+ bkp[4] = get_reg32(0x40020020);
+ bkp[5] = get_reg32(0x40020294);
+ bkp[6] = get_reg32(0x4002103C);
+ bkp[7] = get_reg32(0x400200A8);
+ bkp[8] = get_reg32(0x40020000);
+ bkp[9] = get_reg32(0x40022000);
+
+ set_reg32_bits(0x40020000, 0x00000002, 0);
+ set_reg32_bits(0x40022000, 0x00000001, 0);
+ set_reg32_mask(0x4002103C, 0x00201c00, 0x00001c00);
+ set_reg32_bits(0x400200A8, 0x00000001, 1);
+ set_reg8(0x40020006, 1);
+ set_reg32(0x40020208, 0);
+ set_reg32(0x40020250, 0);
+ set_reg32(0x40020254, 0);
+ set_reg32(0x40021028, 0x00F8A494);
+ set_reg32(0x40020020, 8);
+ set_reg32(0x40020294, 0);
+ set_reg32(0x40020024, 0);
+
+ delay_us(5);
+ if (get_reg32_bits(0x40020020, 0x00000002)) {
+ goto done;
+ }
+
+ set_reg32_bits(0x40020020, 0x00000001, 1);
+ delay_us(15);
+ if (!get_reg32_bits(0x40020020, 0x00000001)) {
+ goto done;
+ }
+
+ delay_us(300);
+ if (get_reg32_bits(0x40020020, 0x00000001)) {
+ goto done;
+ }
+
+done:
+ set_reg32(0x40020024, 0);
+ set_reg32(0x40020208, bkp[0]);
+ set_reg32(0x40020250, bkp[1]);
+ set_reg32(0x40020254, bkp[2]);
+ set_reg32(0x40021028, bkp[3]);
+ set_reg32(0x40020020, bkp[4]);
+ set_reg32(0x40020294, bkp[5]);
+ set_reg32(0x4002103C, bkp[6]);
+ set_reg32(0x400200A8, bkp[7]);
+ set_reg32(0x40020000, bkp[8]);
+ set_reg32(0x40022000, bkp[9]);
+}
+
+static void
+ble_rf_calibration_1(void)
+{
+ uint32_t bkp[12];
+ uint32_t val;
+
+ bkp[0] = get_reg32(0x40020020);
+ bkp[1] = get_reg32(0x40020208);
+ bkp[2] = get_reg32(0x40020250);
+ bkp[3] = get_reg32(0x40020254);
+ bkp[4] = get_reg32(0x40020218);
+ bkp[5] = get_reg32(0x4002021c);
+ bkp[6] = get_reg32(0x40020220);
+ bkp[7] = get_reg32(0x40020270);
+ bkp[8] = get_reg32(0x4002027c);
+ bkp[9] = get_reg32(0x4002101c);
+ bkp[10] = get_reg32(0x40020000);
+ bkp[11] = get_reg32(0x40022000);
+
+ set_reg32(0x40020208, 0);
+ set_reg32(0x40020250, 0);
+ set_reg32(0x40020254, 0);
+ set_reg32(0x40020218, 0);
+ set_reg32(0x4002021c, 0);
+ set_reg32(0x40020220, 0);
+ set_reg32(0x40020270, 0);
+ set_reg32(0x4002027c, 0);
+ set_reg32(0x40020000, 0x0f168820);
+ set_reg32_bits(0x40022000, 0x00000001, 0);
+ set_reg32_bits(0x4002101c, 0x00001e00, 0);
+ set_reg32_bits(0x4002001c, 0x0000003f, 47);
+ set_reg8(0x40020006, 1);
+ set_reg32(0x40020020, 16);
+ set_reg32_bits(0x4002003c, 0x00000800, 1);
+ set_reg32(0x40020024, 0);
+
+ delay_us(5);
+ if (get_reg32_bits(0x40020020, 0x00000002)) {
+ goto done;
+ }
+
+ set_reg32_bits(0x40020020, 0x00000001, 1);
+ delay_us(15);
+ if (!get_reg32_bits(0x40020020, 0x00000001)) {
+ goto done;
+ }
+
+ delay_us(300);
+ if (get_reg32_bits(0x40020020, 0x00000001)) {
+ goto done;
+ }
+
+ val = get_reg32(0x40020090);
+ set_reg32_bits(0x40020094, 0x0000000f, val);
+ set_reg32_bits(0x40020094, 0x00000f00, val);
+ set_reg32_bits(0x40020094, 0x000f0000, val);
+ set_reg32_bits(0x40020094, 0x0f000000, val);
+ g_ble_phy_rf_data.cal_res_1 = get_reg32(0x40020094);
+
+done:
+ set_reg32(0x40020024, 0);
+ set_reg32(0x40020020, bkp[0]);
+ set_reg32(0x40020208, bkp[1]);
+ set_reg32(0x40020250, bkp[2]);
+ set_reg32(0x40020254, bkp[3]);
+ set_reg32(0x40020218, bkp[4]);
+ set_reg32(0x4002021c, bkp[5]);
+ set_reg32(0x40020220, bkp[6]);
+ set_reg32(0x40020270, bkp[7]);
+ set_reg32(0x4002027c, bkp[8]);
+ set_reg32(0x4002101c, bkp[9]);
+ set_reg32(0x40020000, bkp[10]);
+ set_reg32(0x40022000, bkp[11]);
+ set_reg32_bits(0x4002003c, 0x00000800, 0);
+}
+
+static void
+ble_rf_calibration_2(void)
+{
+ uint32_t bkp[2];
+ uint32_t k1;
+
+ set_reg8(0x40020005, 3);
+ set_reg32_bits(0x40022004, 0x0000007f, 20);
+ bkp[0] = get_reg32(0x40022040);
+ set_reg32(0x40022040, 0xffffffff);
+ set_reg32_bits(0x40022018, 0x0000003f, 0);
+ set_reg32_bits(0x40022018, 0x00008000, 0);
+ set_reg32_bits(0x4002201c, 0x00000600, 2);
+ set_reg32_bits(0x4002201c, 0x00000070, 4);
+ set_reg32_bits(0x40022030, 0x3f000000, 22);
+ set_reg32_bits(0x40022030, 0x00000fc0, 24);
+ set_reg32_bits(0x40022030, 0x0000003f, 24);
+ set_reg8(0x4002201c, 0x43);
+ set_reg8(0x40020006, 2);
+ delay_us(2);
+ bkp[1] = get_reg32_bits(0x4002024c, 0x000003e0);
+ set_reg32_bits(0x4002024c, 0x000003e0, 0);
+ set_reg8(0x40020006, 1);
+ set_reg32_bits(0x400200ac, 0x00000003, 3);
+ delay_us(30);
+ delay_us(100);
+ set_reg8(0x40020005, 3);
+ k1 = get_reg32_bits(0x40022088, 0x000001ff);
+ set_reg32(0x400200ac, 0);
+ delay_us(20);
+ set_reg32_bits(0x4002024c, 0x000003e0, bkp[1]);
+ delay_us(10);
+
+ set_reg32_bits(0x40022018, 0xff800000, k1);
+ set_reg32_bits(0x40022018, 0x00007fc0, k1);
+ set_reg8(0x4002201c, 0x41);
+ set_reg32_bits(0x4002201c, 0x00000600, 2);
+ set_reg8(0x40020006, 2);
+ delay_us(2);
+ bkp[1] = get_reg32_bits(0x4002024c, 0x000003e0);
+ set_reg32_bits(0x4002024c, 0x000003e0, 0);
+ set_reg8(0x40020006, 1);
+ set_reg32_bits(0x400200ac, 0x00000003, 3);
+ delay_us(30);
+ delay_us(100);
+ set_reg8(0x40020005, 3);
+ k1 = get_reg32_bits(0x40022088, 0x1ff);
+ set_reg32(0x400200ac, 0);
+ delay_us(20);
+ set_reg32_bits(0x4002024c, 0x000003e0, bkp[1]);
+ delay_us(10);
+
+ set_reg32_bits(0x40022018, 0xff800000, k1);
+ set_reg32_bits(0x40022018, 0x00007fc0, k1);
+ set_reg8(0x4002201c, 0x41);
+ set_reg32_bits(0x4002201c, 0x00000600, 2);
+ set_reg8(0x40020006, 2);
+ delay_us(2);
+ bkp[1] = get_reg32_bits(0x4002024c, 0x000003e0);
+ set_reg32_bits(0x4002024c, 0x000003e0, 0);
+ set_reg8(0x40020006, 1);
+ set_reg32_bits(0x400200ac, 0x00000003, 3);
+ delay_us(30);
+ delay_us(100);
+ set_reg8(0x40020005, 3);
+ k1 = get_reg32_bits(0x40022088, 0x000001ff);
+ set_reg32_bits(0x40022018, 0xff800000, k1);
+ set_reg32_bits(0x40022018, 0x00007fc0, k1);
+ set_reg32_bits(0x4002201c, 0x00000001, 0);
+ set_reg32(0x40022040, bkp[0]);
+ set_reg32_bits(0x40022018, 0x0000003f, 0x1c);
+ set_reg32_bits(0x40022018, 0x00008000, 0);
+ set_reg32_bits(0x40022030, 0x3f000000, 28);
+ set_reg32_bits(0x40022030, 0x00000fc0, 30);
+ set_reg32_bits(0x40022030, 0x0000003f, 30);
+ set_reg32(0x400200ac, 0);
+ delay_us(20);
+ set_reg32_bits(0x4002024c, 0x000003e0, bkp[1]);
+ delay_us(10);
+
+ g_ble_phy_rf_data.cal_res_2 = k1;
+}
+
+static uint32_t
+ble_rf_find_trim_reg(volatile uint32_t *tv, unsigned len, uint32_t reg)
+{
+ while (len) {
+ if (tv[0] == reg) {
+ return tv[1];
+ }
+ len -= 2;
+ tv += 2;
+ }
+
+ return 0;
+}
+
+void
+ble_rf_init(void)
+{
+ uint32_t val;
+
+ val = ble_rf_find_trim_reg(g_cmac_shared_data.trim.rfcu_mode1,
+ g_cmac_shared_data.trim.rfcu_mode1_len,
+ 0x4002004c);
+ g_ble_phy_rf_data.trim_val1_tx_1 = val;
+
+ val = ble_rf_find_trim_reg(g_cmac_shared_data.trim.rfcu_mode2,
+ g_cmac_shared_data.trim.rfcu_mode2_len,
+ 0x4002004c);
+ g_ble_phy_rf_data.trim_val1_tx_2 = val;
+
+ if (!g_ble_phy_rf_data.trim_val1_tx_1 || !g_ble_phy_rf_data.trim_val1_tx_2) {
+ val = ble_rf_find_trim_reg(g_cmac_shared_data.trim.rfcu,
+ g_cmac_shared_data.trim.rfcu_len,
+ 0x4002004c);
+ if (!val) {
+ val = 0x0300;
+ }
+ g_ble_phy_rf_data.trim_val1_tx_1 = val;
+ g_ble_phy_rf_data.trim_val1_tx_2 = val;
+ }
+
+ val = ble_rf_find_trim_reg(g_cmac_shared_data.trim.synth,
+ g_cmac_shared_data.trim.synth_len,
+ 0x40022038);
+ if (!val) {
+ val = 0x0198ff03;
+ }
+ g_ble_phy_rf_data.trim_val2_rx = val;
+ g_ble_phy_rf_data.trim_val2_tx = val;
+ set_reg32_bits((uint32_t)&g_ble_phy_rf_data.trim_val2_tx, 0x0001ff00, 0x87);
+
+#if MYNEWT_VAL(CMAC_DEBUG_DATA_ENABLE)
+ g_cmac_shared_data.debug.trim_val1_tx_1 = g_ble_phy_rf_data.trim_val1_tx_1;
+ g_cmac_shared_data.debug.trim_val1_tx_2 = g_ble_phy_rf_data.trim_val1_tx_2;
+ g_cmac_shared_data.debug.trim_val2_tx = g_ble_phy_rf_data.trim_val2_tx;
+ g_cmac_shared_data.debug.trim_val2_rx = g_ble_phy_rf_data.trim_val2_rx;
+#endif
+}
+
+void
+ble_rf_enable(void)
+{
+ if (ble_rf_is_enabled()) {
+ return;
+ }
+
+ ble_rf_rfcu_enable();
+ ble_rf_rfcu_apply_settings();
+ ble_rf_ldo_on();
+}
+
+void
+ble_rf_configure(void)
+{
+ if (ble_rf_synth_is_enabled()) {
+ return;
+ }
+
+ ble_rf_synth_enable();
+ ble_rf_synth_apply_settings();
+}
+
+void
+ble_rf_stop(void)
+{
+ ble_rf_synth_disable();
+ set_reg8(0x40020006, 0);
+}
+
+void
+ble_rf_disable(void)
+{
+ ble_rf_stop();
+ ble_rf_ldo_off();
+ ble_rf_rfcu_disable();
+}
+
+bool
+ble_rf_is_enabled(void)
+{
+ return get_reg32_bits(0x40020008, 5) == 5;
+}
+
+static void
+ble_rf_calibrate_int(uint8_t mask)
+{
+ ble_rf_rfcu_enable();
+ ble_rf_rfcu_apply_settings();
+ ble_rf_ldo_on();
+ delay_us(20);
+
+ ble_rf_synth_disable();
+ ble_rf_synth_enable();
+ ble_rf_synth_apply_settings();
+ set_reg8(0x40020005, 1);
+
+ if (mask & RF_CALIBRATION_0) {
+ ble_rf_calibration_0();
+ }
+ if (mask & RF_CALIBRATION_1) {
+ ble_rf_calibration_1();
+ }
+ if (mask & RF_CALIBRATION_2) {
+ ble_rf_calibration_2();
+ }
+
+ ble_rf_ldo_off();
+ ble_rf_rfcu_disable();
+
+#if MYNEWT_VAL(CMAC_DEBUG_DATA_ENABLE)
+ g_cmac_shared_data.debug.cal_res_1 = g_ble_phy_rf_data.cal_res_1;
+ g_cmac_shared_data.debug.cal_res_2 = g_ble_phy_rf_data.cal_res_2;
+#endif
+}
+
+void
+ble_rf_calibrate(void)
+{
+ ble_rf_calibrate_int(RF_CALIBRATION_1 | RF_CALIBRATION_2);
+}
+
+void
+ble_rf_calibrate_once(void)
+{
+ static bool calibrated = false;
+
+ if (calibrated) {
+ return;
+ }
+
+ ble_rf_rfcu_enable();
+ ble_rf_rfcu_apply_settings();
+
+ g_ble_phy_rf_data.tx_power_cfg1 = get_reg32_bits(0x500000a4, 0xf0);
+ g_ble_phy_rf_data.tx_power_cfg2 = get_reg32_bits(0x40020238, 0x000003e0);
+ g_ble_phy_rf_data.tx_power_cfg3 = 0;
+
+ ble_rf_rfcu_disable();
+
+ ble_rf_calibrate_int(RF_CALIBRATION_0 | RF_CALIBRATION_1 | RF_CALIBRATION_2);
+
+ calibrated = true;
+}
+
+void
+ble_rf_setup_tx(uint8_t rf_chan, uint8_t phy_mode)
+{
+ set_reg32_bits(0x40020000, 0x0f000000, g_ble_phy_rf_data.tx_power_cfg0);
+ set_reg32_bits(0x500000a4, 0x000000f0, g_ble_phy_rf_data.tx_power_cfg1);
+ set_reg32_bits(0x40020238, 0x000003e0, g_ble_phy_rf_data.tx_power_cfg2);
+ set_reg32_bits(0x40020234, 0x000003e0, g_ble_phy_rf_data.tx_power_cfg3);
+
+ if (g_ble_phy_rf_data.tx_power_cfg0 < 13) {
+ set_reg32(0x4002004c, g_ble_phy_rf_data.trim_val1_tx_1);
+ } else {
+ set_reg32(0x4002004c, g_ble_phy_rf_data.trim_val1_tx_2);
+ }
+ set_reg8(0x40020005, 3);
+ set_reg8(0x40022004, rf_chan);
+ if (phy_mode == BLE_PHY_MODE_2M) {
+#if MYNEWT_VAL(BLE_PHY_RF_HP_MODE)
+ set_reg32(0x40022000, 0x00000303);
+#else
+ set_reg32(0x40022000, 0x00000003);
+#endif
+ } else {
+#if MYNEWT_VAL(BLE_PHY_RF_HP_MODE)
+ set_reg32(0x40022000, 0x00000300);
+#else
+ set_reg32(0x40022000, 0x00000000);
+#endif
+ }
+
+ ble_rf_apply_calibration();
+
+ set_reg32_bits(0x40022050, 0x00000200, 1);
+ set_reg32_bits(0x40022050, 0x00000100, 0);
+ set_reg32_bits(0x40022048, 0x01ffff00, 0x7700);
+ set_reg32(0x40022038, g_ble_phy_rf_data.trim_val2_tx);
+
+ set_reg8(0x40020006, 3);
+}
+
+void
+ble_rf_setup_rx(uint8_t rf_chan, uint8_t phy_mode)
+{
+ set_reg32_bits(0x500000a4, 0x000000f0, g_ble_phy_rf_data.tx_power_cfg1);
+ set_reg8(0x40020005, 3);
+ set_reg8(0x40022004, rf_chan);
+ if (phy_mode == BLE_PHY_MODE_2M) {
+#if MYNEWT_VAL(BLE_PHY_RF_HP_MODE)
+ set_reg32(0x40022000, 0x00000303);
+ set_reg32(0x40020000, 0x0f11b823);
+ set_reg32(0x4002103c, 0x0125261b);
+#else
+ set_reg32(0x40022000, 0x00000003);
+ set_reg32(0x40020000, 0x0f0c2803);
+ set_reg32(0x4002103c, 0x0125a61b);
+#endif
+ set_reg32(0x40021020, 0x000002f5);
+ set_reg32(0x4002102c, 0x0000d1d5);
+ } else {
+#if MYNEWT_VAL(BLE_PHY_RF_HP_MODE)
+ set_reg32(0x40022000, 0x00000300);
+ set_reg32(0x40020000, 0x0f099820);
+ set_reg32(0x4002103c, 0x0124a21f);
+#else
+ set_reg32(0x40022000, 0x00000000);
+ set_reg32(0x40020000, 0x0f062800);
+ set_reg32(0x4002103c, 0x01051e1f);
+#endif
+ set_reg32(0x40021020, 0x000002f5);
+ set_reg32(0x4002102c, 0x0000dfe7);
+ }
+
+ ble_rf_apply_calibration();
+
+ set_reg32_bits(0x40022050, 0x00000200, 1);
+ set_reg32_bits(0x40022050, 0x00000100, 1);
+ set_reg32_bits(0x40022048, 0x01ffff00, 0);
+ set_reg32(0x40022038, g_ble_phy_rf_data.trim_val2_rx);
+
+ set_reg8(0x40020006, 3);
+}
+
+void
+ble_rf_set_tx_power(int dbm)
+{
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(g_ble_rf_power_lvls); i++) {
+ if (g_ble_rf_power_lvls[i] >= dbm) {
+ break;
+ }
+ }
+
+ g_ble_phy_rf_data.tx_power_cfg0 = i + 1;
+}
+
+int8_t
+ble_rf_get_rssi(void)
+{
+ return (501 * get_reg32_bits(0x40021038, 0x000003ff) - 493000) / 4096;
+}
diff --git a/nimble/drivers/dialog_cmac/src/ble_rf_priv.h b/nimble/drivers/dialog_cmac/src/ble_rf_priv.h
new file mode 100644
index 0000000..358300d
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/src/ble_rf_priv.h
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef _BLE_RF_PRIV_H_
+#define _BLE_RF_PRIV_H_
+
+void ble_rf_init(void);
+void ble_rf_enable(void);
+void ble_rf_stop(void);
+void ble_rf_disable(void);
+bool ble_rf_is_enabled(void);
+void ble_rf_configure(void);
+
+void ble_rf_calibrate(void);
+void ble_rf_calibrate_once(void);
+
+void ble_rf_setup_tx(uint8_t rf_chan, uint8_t mode);
+void ble_rf_setup_rx(uint8_t rf_chan, uint8_t mode);
+
+void ble_rf_set_tx_power(int dbm);
+int8_t ble_rf_get_rssi(void);
+
+#endif /* _BLE_RF_PRIV_H_ */
diff --git a/nimble/drivers/dialog_cmac/syscfg.yml b/nimble/drivers/dialog_cmac/syscfg.yml
new file mode 100644
index 0000000..a82b62e
--- /dev/null
+++ b/nimble/drivers/dialog_cmac/syscfg.yml
@@ -0,0 +1,29 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+syscfg.defs:
+ BLE_PHY_RF_HP_MODE:
+ description: Enable high-performance RF mode.
+ value: 1
+
+ BLE_PHY_DEBUG_DSER:
+ description: Enable DSER output from PHY
+ value: 0
+
+syscfg.restrictions:
+ - BLE_LL_RFMGMT_ENABLE_TIME == 0 || BLE_LL_RFMGMT_ENABLE_TIME >= 20