├── README.md ├── include ├── debug │ ├── rtt_log.h │ ├── DhDebug.h │ ├── SEGGER_RTT_Conf.h │ └── SEGGER_RTT.h ├── Crypto │ └── SwAes │ │ ├── aes_locl.h │ │ └── aes.h ├── DhTypes.h ├── BleStack │ ├── BleGap.h │ ├── BleL2cap.h │ ├── BleLink │ │ ├── BleLinkControl.h │ │ ├── BleLinkAdvertising.h │ │ ├── BleLinkConnect.h │ │ └── BleLink.h │ ├── BleAdvertising.h │ ├── DhBleEventNtf.h │ ├── BleAtt.h │ └── BleGatt.h ├── ChipDrv │ ├── NrfDrv │ │ ├── NrfUart.h │ │ ├── NrfClockDrv.h │ │ ├── system_nrf51.h │ │ ├── NrfTimerDrv.h │ │ ├── NrfRtcDrv.h │ │ └── nrf52840_delay.h │ └── CommDrv │ │ └── HardwareUart.h ├── DhConfig.h ├── DhBleAux.h ├── HardwareHead.h ├── Common │ ├── DhBuffManage.h │ └── DhQueue.h ├── BleDrv │ ├── BleHAccuracyTimer.h │ ├── BleLPowerTimer.h │ └── BleRadioDrv.h ├── DhGlobalHead.h ├── CMSIS │ ├── core_cmFunc.h │ ├── core_cmInstr.h │ ├── core_cmSimd.h │ └── arm_const_structs.h ├── DhError.h └── DhBleDefine.h ├── .gitignore ├── source ├── project_conf.h ├── Crypto │ └── SwAes │ │ └── Aes.c ├── Common │ ├── DhBuffManage.c │ └── DhQueue.c ├── ChipDrv │ ├── CommDrv │ │ └── HardwareUart.c │ └── NrfDrv │ │ ├── nrf51822 │ │ ├── NrfUart.c │ │ ├── NrfClockDrv.c │ │ ├── NrfTimer0Drv.c │ │ ├── system_nrf51.c │ │ └── NrfRtcDrv.c │ │ └── nrf52840 │ │ ├── NrfUart.c │ │ ├── NrfClockDrv.c │ │ ├── NrfTimer0Drv.c │ │ └── NrfRtcDrv.c ├── debug │ └── rtt_log.c ├── BleStack │ ├── DhBleEventNtf.c │ ├── BleGap.c │ ├── BleL2cap.c │ └── BleAdvertising.c ├── BleDrv │ └── BleHAccuracyTimer.c └── DhBleAux.c ├── test ├── test_rtc0_timer0.c └── test_radio.c └── debug_use_systemview ├── Config ├── SEGGER_SYSVIEW_Conf.h └── Global.h └── SEGGER ├── SEGGER_SYSVIEW_Int.h └── Syscalls ├── SEGGER_RTT_Syscalls_IAR.c └── SEGGER_RTT_Syscalls_GCC.c /README.md: -------------------------------------------------------------------------------- 1 | # dh_ble 2 | 基于nrf51的开源ble协议栈——已不再维护(没有相关硬件可以测试了) 3 | 4 | # 目前正在 dev分支 开发基于 nordic nrf52840 的BLE 协议栈,基于最新规范新增一些必要功能,以及修改一些之前不合理的地方。实现和手机 BLE 可连接,可传输数据。 5 | 6 | # 相关协议栈细节解析,和本协议栈实现细节会陆续发布在博客中: 7 | https://fengxun2017.github.io/categories/BLE%E5%8D%8F%E8%AE%AE%E6%A0%88/ 8 | 9 | 许可协议: 10 | 工程文件中除部分CMSIS相关文件,一些nordic官方文件,其他源码文件许可协议均为 MIT许可协议,详见各文件。 11 | -------------------------------------------------------------------------------- /include/debug/rtt_log.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef RTT_LOG_H__ 3 | #define RTT_LOG_H__ 4 | 5 | #ifdef DEBUG_LOG 6 | #include "../DhGlobalHead.h" 7 | // Function Declarations 8 | void rtt_log_general_print(int terminal, const char* color, const char *file, int line, const char *sFormat, ...); 9 | 10 | 11 | #define loge(...) rtt_log_general_print(0,RTT_CTRL_TEXT_BRIGHT_RED,__FILE__,__LINE__,__VA_ARGS__) 12 | #define logw(...) rtt_log_general_print(0,RTT_CTRL_TEXT_BRIGHT_YELLOW,__FILE__,__LINE__,__VA_ARGS__) 13 | #define logi(...) rtt_log_general_print(0,RTT_CTRL_TEXT_WHITE,__FILE__,__LINE__,__VA_ARGS__) 14 | 15 | #else 16 | 17 | #define loge(...) 18 | #define logw(...) 19 | #define logi(...) 20 | #define log_init() 21 | #endif 22 | 23 | #endif // RTT_LOG_H__ 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /si/* 2 | /project/* 3 | !/project/DH_BLE.uvprojx 4 | 5 | # Prerequisites 6 | *.d 7 | 8 | # Object files 9 | *.o 10 | *.ko 11 | *.obj 12 | *.elf 13 | 14 | # Linker output 15 | *.ilk 16 | *.map 17 | *.exp 18 | 19 | # Precompiled Headers 20 | *.gch 21 | *.pch 22 | 23 | # Libraries 24 | *.lib 25 | *.a 26 | *.la 27 | *.lo 28 | 29 | # Shared objects (inc. Windows DLLs) 30 | *.dll 31 | *.so 32 | *.so.* 33 | *.dylib 34 | 35 | # Executables 36 | *.exe 37 | *.out 38 | *.app 39 | *.i*86 40 | *.x86_64 41 | *.hex 42 | 43 | # Debug files 44 | *.dSYM/ 45 | *.su 46 | *.idb 47 | *.pdb 48 | 49 | # Kernel Module Compile Results 50 | *.mod* 51 | *.cmd 52 | .tmp_versions/ 53 | .vscode 54 | *.uvoptx* 55 | *.uvguix* 56 | *.uvgui* 57 | *.uvmpw 58 | *.scvd 59 | JLink* 60 | /test/Listings 61 | /test/Objects 62 | -------------------------------------------------------------------------------- /source/project_conf.h: -------------------------------------------------------------------------------- 1 | #ifndef PROJECT_CONF_H_ 2 | #define PROJECT_CONF_H_ 3 | 4 | 5 | #define HARDWARE_NRF52840 6 | 7 | // 硬件中断优先级 8 | #define DH_IRQ_PRIORITY_0 (0) // 最高优先级 9 | #define DH_IRQ_PRIORITY_1 (1) 10 | #define DH_IRQ_PRIORITY_2 (2) 11 | #define DH_IRQ_PRIORITY_3 (3) 12 | 13 | #define DEBUG_LOG 14 | #define nDEBUG_LOG_USE_UART /* 使用串口打印信息时,只供应用程显示使用。不要再协议栈内部打印信息,串口打印太慢影响时序 */ 15 | #define nDEBUG_LOG_USE_RTT /* 协议栈内部可以使用RTT打印调试信息,但是打印的位置不能影响时序 */ 16 | 17 | 18 | #ifdef DEBUG_LOG 19 | #include "../debug_use_systemview/SEGGER/SEGGER_SYSVIEW.h" 20 | #endif 21 | 22 | #ifdef HARDWARE_NRF51 23 | #define UART_RX_PIN (8) 24 | #define UART_TX_PIN (9) 25 | #define LITTLE_ENDIA 26 | #endif 27 | 28 | #ifdef HARDWARE_NRF52840 29 | #define CONFIG_GPIO_AS_PINRESET // nordic52840 P0.18引脚作为 reset 功能 30 | #endif 31 | 32 | 33 | #endif // PROJECT_CONF_H_ 34 | -------------------------------------------------------------------------------- /include/Crypto/SwAes/aes_locl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved. 3 | * 4 | * Licensed under the OpenSSL license (the "License"). You may not use 5 | * this file except in compliance with the License. You can obtain a copy 6 | * in the file LICENSE in the source distribution or at 7 | * https://www.openssl.org/source/license.html 8 | */ 9 | 10 | #ifndef HEADER_AES_LOCL_H 11 | # define HEADER_AES_LOCL_H 12 | 13 | # include "../../DhGlobalHead.h" 14 | 15 | 16 | 17 | # if defined(LITTLE_ENDIA) 18 | # define SWAP(x) (DhIrol(x, 8) & 0x00ff00ff | DhIror(x, 8) & 0xff00ff00) 19 | # define GETU32(p) SWAP(*((u32 *)(p))) 20 | # define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); } 21 | # else 22 | # define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3])) 23 | # define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); } 24 | # endif 25 | 26 | # ifdef AES_LONG 27 | typedef unsigned long u32; 28 | # else 29 | typedef unsigned int u32; 30 | # endif 31 | typedef unsigned short u16; 32 | typedef unsigned char u8; 33 | 34 | # define MAXKC (256/32) 35 | # define MAXKB (256/8) 36 | # define MAXNR 14 37 | 38 | /* This controls loop-unrolling in aes_core.c */ 39 | # undef FULL_UNROLL 40 | 41 | #endif /* !HEADER_AES_LOCL_H */ 42 | -------------------------------------------------------------------------------- /include/DhTypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __DHTYPES_H__ 28 | #define __DHTYPES_H__ 29 | 30 | typedef unsigned char u1; 31 | typedef unsigned short u2; 32 | typedef unsigned int u4; 33 | 34 | 35 | typedef signed char s1; 36 | typedef signed short int s2; 37 | typedef signed int s4; 38 | 39 | 40 | #endif /* __DHTYPES_H__ */ 41 | -------------------------------------------------------------------------------- /source/Crypto/SwAes/Aes.c: -------------------------------------------------------------------------------- 1 | 2 | #include "../../../include/DhGlobalHead.h" 3 | 4 | 5 | 6 | void SwAesEncryptData(u1 *pu1Key, u1* pu1InData, u4 u4InLen, u1 *pu1OutData) 7 | { 8 | u4 u4Index = 0; 9 | AES_KEY aesKey; 10 | 11 | if( (NULL==pu1InData) || (NULL==pu1OutData) ) 12 | { 13 | return ; 14 | } 15 | 16 | AES_set_encrypt_key(pu1Key, 128, &aesKey); 17 | while ( u4InLen > 0 ) 18 | { 19 | AES_encrypt(pu1InData+u4Index, pu1OutData+u4Index, &aesKey); 20 | u4Index += 16; 21 | u4InLen -= 16; 22 | } 23 | 24 | } 25 | 26 | void SwAesDecryptData(u1 *pu1Key, u1* pu1InData, u4 u4InLen, u1 *pu1OutData) 27 | { 28 | u4 u4Index = 0; 29 | AES_KEY aesKey; 30 | 31 | if( (NULL==pu1InData) || (NULL==pu1OutData) ) 32 | { 33 | return ; 34 | } 35 | 36 | AES_set_decrypt_key(pu1Key, 128, &aesKey); 37 | while ( u4InLen > 0 ) 38 | { 39 | AES_decrypt(pu1InData+u4Index, pu1OutData+u4Index, &aesKey); 40 | u4Index += 16; 41 | u4InLen -= 16; 42 | } 43 | 44 | } 45 | 46 | unsigned int DhIrol(unsigned int u4Value, unsigned char b) 47 | { 48 | unsigned int temp; 49 | unsigned int highBit; 50 | unsigned char i; 51 | temp = u4Value; 52 | for(i = 0; i < b; i++) 53 | { 54 | highBit = temp&0x80000000; 55 | temp = temp<<1; 56 | if(highBit) 57 | temp |= 1; 58 | } 59 | 60 | return temp; 61 | } 62 | 63 | 64 | unsigned int DhIror(unsigned int u4Value, unsigned char b) 65 | { 66 | unsigned int temp; 67 | unsigned char bit; 68 | unsigned char i; 69 | 70 | temp = u4Value; 71 | for(i = 0; i < b; i++) 72 | { 73 | bit = temp&0x01; 74 | temp = temp>>1; 75 | if(bit) 76 | temp |= 0x80000000; 77 | } 78 | 79 | return temp; 80 | } 81 | -------------------------------------------------------------------------------- /include/BleStack/BleGap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __BLEGAP_H__ 28 | #define __BLEGAP_H__ 29 | #include "../DhGlobalHead.h" 30 | 31 | #ifdef __cplusplus 32 | #if __cplusplus 33 | extern "C"{ 34 | #endif 35 | #endif /* __cplusplus */ 36 | 37 | extern u4 BleGapAddrCfg(BlkBleAddrInfo addr); 38 | extern u4 BleGapDeviceNameCfg(char *pu1Name, u1 len); 39 | extern u4 BleGapDeviceNameGet(char *pu1Name, u1 *pu1BuffSize); 40 | 41 | #ifdef __cplusplus 42 | #if __cplusplus 43 | } 44 | #endif 45 | #endif /* __cplusplus */ 46 | 47 | 48 | #endif /* __BLEGAP_H__ */ 49 | -------------------------------------------------------------------------------- /include/ChipDrv/NrfDrv/NrfUart.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __NRFUART_H__ 28 | #define __NRFUART_H__ 29 | #include "../../DhGlobalHead.h" 30 | 31 | 32 | 33 | 34 | #ifdef __cplusplus 35 | #if __cplusplus 36 | extern "C"{ 37 | #endif 38 | #endif /* __cplusplus */ 39 | 40 | extern u4 NrfUartBaudGet(EnUartBaudrate baud); 41 | extern void NrfUartSimpleInit(u1 rxPin, u1 txpin, u4 baud); 42 | extern void NrfUartSimpleTxByte(u1 byte); 43 | 44 | 45 | #ifdef __cplusplus 46 | #if __cplusplus 47 | } 48 | #endif 49 | #endif /* __cplusplus */ 50 | 51 | 52 | #endif /* __NRFUART_H__ */ 53 | -------------------------------------------------------------------------------- /include/ChipDrv/NrfDrv/NrfClockDrv.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __NRFCLOCKDRV_H__ 28 | #define __NRFCLOCKDRV_H__ 29 | 30 | typedef enum 31 | { 32 | LFCLK_SRC_RC = 0, 33 | LFCLK_SRC_XTAL, 34 | LFCLK_SRC_SYNTH, // 从高频时钟同步而来 35 | }EnNrfLFClockSrc; 36 | 37 | #ifdef __cplusplus 38 | #if __cplusplus 39 | extern "C"{ 40 | #endif 41 | #endif /* __cplusplus */ 42 | 43 | extern void NrfLFClkStart( EnNrfLFClockSrc src , u4 u4CalTimeoutMs, u1 enableCal); 44 | extern void NrfHFClkSrcSetXtal(void); 45 | 46 | #ifdef __cplusplus 47 | #if __cplusplus 48 | } 49 | #endif 50 | #endif /* __cplusplus */ 51 | 52 | 53 | #endif /* __NRFCLOCKDRV_H__ */ 54 | -------------------------------------------------------------------------------- /include/DhConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __DH_CONFIG_H__ 28 | #define __DH_CONFIG_H__ 29 | 30 | #define DH_IRQ_PRIORITY_0 (0) // 最高优先级 31 | #define DH_IRQ_PRIORITY_1 (1) 32 | #define DH_IRQ_PRIORITY_2 (2) 33 | #define DH_IRQ_PRIORITY_3 (3) 34 | 35 | 36 | 37 | #define HARDWARE_NRF51 38 | 39 | #define DEBUG_LOG 40 | #define DEBUG_LOG_USE_UART /* 使用串口打印信息时,只供应用程显示使用。不要再协议栈内部打印信息,串口打印太慢影响时序 */ 41 | #define nDEBUG_LOG_USE_RTT /* 协议栈内部可以使用RTT打印调试信息,但是打印的位置不能影响时序 */ 42 | 43 | 44 | #ifdef HARDWARE_NRF51 45 | #define UART_RX_PIN (8) 46 | #define UART_TX_PIN (9) 47 | #define LITTLE_ENDIA 48 | #endif 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /source/Common/DhBuffManage.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../include/DhGlobalHead.h" 28 | /* 29 | 实现简单的内存管理,只能静态申请,只能使用在申请后就不会释放的场景中使用,协议栈中需要该模块管理内存 30 | */ 31 | 32 | u1 *DhMemoryAlloc(BlkDhMemoryManage *pblkMemory, u4 size) 33 | { 34 | u1 *pu1Buff = NULL; 35 | 36 | if( NULL == pblkMemory ) 37 | { 38 | return pu1Buff; 39 | } 40 | if( (pblkMemory->m_u4BuffSize-pblkMemory->m_u4CurrentIndex) < size ) 41 | { 42 | /* 内存不够申请的了 */ 43 | return pu1Buff; 44 | } 45 | 46 | pu1Buff = &pblkMemory->m_pu1Buff[pblkMemory->m_u4CurrentIndex]; 47 | pblkMemory->m_u4CurrentIndex += size; 48 | 49 | return pu1Buff; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /include/BleStack/BleL2cap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __BLEL2CAP_H__ 28 | #define __BLEL2CAP_H__ 29 | #include "../DhGlobalHead.h" 30 | 31 | #define BLE_L2CAP_HEADER_LEN 0x04 32 | 33 | /* BLE只支持3个通道 */ 34 | #define BLE_L2CAP_ATT_CHANNEL_ID 0x0004 35 | #define BLE_L2CAP_SM_CHANNEL_ID 0x0006 36 | #define BLE_L2CAP_SIGNAL_CHANNEL_ID 0x0005 37 | 38 | #ifdef __cplusplus 39 | #if __cplusplus 40 | extern "C"{ 41 | #endif 42 | #endif /* __cplusplus */ 43 | 44 | extern u4 BleL2capHandle(u1 *pu1Data, u2 u2length); 45 | extern u4 BleL2capDataSend(u2 u2ChannelId, u1 *pu1Data, u2 len); 46 | 47 | #ifdef __cplusplus 48 | #if __cplusplus 49 | } 50 | #endif 51 | #endif /* __cplusplus */ 52 | 53 | 54 | #endif /* __BLEL2CAP_H__ */ 55 | -------------------------------------------------------------------------------- /include/BleStack/BleLink/BleLinkControl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __BLELINKCONTROL_H__ 28 | #define __BLELINKCONTROL_H__ 29 | #include "../../DhGlobalHead.h" 30 | 31 | 32 | 33 | 34 | 35 | #ifdef __cplusplus 36 | #if __cplusplus 37 | extern "C"{ 38 | #endif 39 | #endif /* __cplusplus */ 40 | extern u4 BleLinkControlHandle(u1 *pu1Data, u2 len); 41 | extern u4 CheckLinkChannelMapUpdateReq(u1 *pu1Data, u1 *pu1NewChannelMap, u2 *u2Instant); 42 | extern u4 CheckLinkConnUpdateReq(u1 *pu1PDU, u1 *u1WinSize, u2 *u2WinOffset, u2 *u2Interval, u2 *u2Latency, u2 *u2Timeout, u2 *u2Instant); 43 | 44 | 45 | #ifdef __cplusplus 46 | #if __cplusplus 47 | } 48 | #endif 49 | #endif /* __cplusplus */ 50 | 51 | 52 | #endif /* __BLELINKCONTROL_H__ */ 53 | -------------------------------------------------------------------------------- /include/Crypto/SwAes/aes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved. 3 | * 4 | * Licensed under the OpenSSL license (the "License"). You may not use 5 | * this file except in compliance with the License. You can obtain a copy 6 | * in the file LICENSE in the source distribution or at 7 | * https://www.openssl.org/source/license.html 8 | */ 9 | 10 | #ifndef HEADER_AES_H 11 | # define HEADER_AES_H 12 | 13 | # include "../../DhGlobalHead.h" 14 | 15 | 16 | # include 17 | # ifdef __cplusplus 18 | extern "C" { 19 | # endif 20 | 21 | # define AES_ENCRYPT 1 22 | # define AES_DECRYPT 0 23 | 24 | /* 25 | * Because array size can't be a const in C, the following two are macros. 26 | * Both sizes are in bytes. 27 | */ 28 | # define AES_MAXNR 14 29 | # define AES_BLOCK_SIZE 16 30 | 31 | /* This should be a hidden type, but EVP requires that the size be known */ 32 | struct aes_key_st { 33 | # ifdef AES_LONG 34 | unsigned long rd_key[4 * (AES_MAXNR + 1)]; 35 | # else 36 | unsigned int rd_key[4 * (AES_MAXNR + 1)]; 37 | # endif 38 | int rounds; 39 | }; 40 | typedef struct aes_key_st AES_KEY; 41 | 42 | 43 | unsigned int DhIrol(u4 u4Value, u1 b); 44 | unsigned int DhIror(u4 u4Value, u1 b); 45 | 46 | void SwAesEncryptData(u1 *pu1Key, u1* pu1InData, u4 u4InLen, u1 *pu1OutData); 47 | void SwAesDecryptData(u1 *pu1Key, u1* pu1InData, u4 u4InLen, u1 *pu1OutData); 48 | 49 | int AES_set_encrypt_key(const unsigned char *userKey, const int bits, 50 | AES_KEY *key); 51 | int AES_set_decrypt_key(const unsigned char *userKey, const int bits, 52 | AES_KEY *key); 53 | 54 | void AES_encrypt(const unsigned char *in, unsigned char *out, 55 | const AES_KEY *key); 56 | void AES_decrypt(const unsigned char *in, unsigned char *out, 57 | const AES_KEY *key); 58 | 59 | # ifdef __cplusplus 60 | } 61 | # endif 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /include/DhBleAux.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __DHBLEAUX_H__ 28 | #define __DHBLEAUX_H__ 29 | 30 | #include "./DhGlobalHead.h" 31 | 32 | #define DH_MIN(a,b) ((a)<(b)?(a):(b)) 33 | #define DH_MAX(a,b) ((a)>(b)?(a):(b)) 34 | 35 | #ifdef __cplusplus 36 | #if __cplusplus 37 | extern "C"{ 38 | #endif 39 | #endif /* __cplusplus */ 40 | 41 | extern u2 BleGetChannelFreq(u1 u1Channel); 42 | 43 | extern u1 GetChannelWhiteIv(u1 u1Channel ); 44 | 45 | extern void DhMemxor(u1 *pu1Data1, u1 *pu1Data2, u2 u2Len); 46 | 47 | extern u4 DhAesEnc(u1 *pu1In, u1 *pu1Key, u1 *pu1Out); 48 | 49 | extern u4 DhAesEnc2(u1 *pu1In, u1 *pu1Key, u1 *pu1Out); 50 | 51 | extern void DhGetRand(u1 *pu1Data, u2 len); 52 | 53 | #ifdef __cplusplus 54 | #if __cplusplus 55 | } 56 | #endif 57 | #endif /* __cplusplus */ 58 | 59 | 60 | #endif /* __DHBLEAUX_H__ */ 61 | -------------------------------------------------------------------------------- /include/BleStack/BleAdvertising.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __BLEADVERTISING_H__ 28 | #define __BLEADVERTISING_H__ 29 | #include "../DhGlobalHead.h" 30 | 31 | typedef struct 32 | { 33 | u1 m_u1AddrType; /* 0-public 1-random */ 34 | u1 m_pu1Addr[BLE_ADDR_LEN]; /* 设备地址,LSB */ 35 | }BlkBleAddrInfo; 36 | 37 | typedef struct 38 | { 39 | u1 m_ChannelOn_37:1; 40 | u1 m_ChannelOn_38:1; 41 | u1 m_ChannelOn_39:1; 42 | }BlkAdvChannelOn; 43 | 44 | 45 | #ifdef __cplusplus 46 | #if __cplusplus 47 | extern "C"{ 48 | #endif 49 | #endif /* __cplusplus */ 50 | 51 | extern u4 BleAdvDataCfg(u1 *pu1Data, u2 len); 52 | extern u4 BleAdvStart(BlkAdvChannelOn channels, u2 IntervalMs); 53 | extern u4 BleScanRspDataCfg(u1 *pu1Data, u2 len); 54 | 55 | #ifdef __cplusplus 56 | #if __cplusplus 57 | } 58 | #endif 59 | #endif /* __cplusplus */ 60 | 61 | 62 | #endif /* __BLEADVERTISING_H__ */ 63 | -------------------------------------------------------------------------------- /include/HardwareHead.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __HARDWAREHEAD_H__ 28 | #define __HARDWAREHEAD_H__ 29 | 30 | 31 | #ifdef __cplusplus 32 | #if __cplusplus 33 | extern "C"{ 34 | #endif 35 | #endif /* __cplusplus */ 36 | 37 | 38 | #ifdef __cplusplus 39 | #if __cplusplus 40 | } 41 | #endif 42 | #endif /* __cplusplus */ 43 | 44 | #include "./ChipDrv/CommDrv/HardwareUart.h" 45 | 46 | #ifdef HARDWARE_NRF52840 47 | #include "./ChipDrv/NrfDrv/nrf52840.h" 48 | #include "./ChipDrv/NrfDrv/nrf52840_delay.h" 49 | #include "./ChipDrv/NrfDrv/nrf52840_bitfields.h" 50 | #endif 51 | 52 | #include "./ChipDrv/NrfDrv/NrfRtcDrv.h" 53 | #include "./ChipDrv/NrfDrv/NrfRadioDrv.h" 54 | #include "./ChipDrv/NrfDrv/NrfTimerDrv.h" 55 | //#include "./ChipDrv/NrfDrv/nrf_gpio.h" 56 | #include "./ChipDrv/NrfDrv/NrfClockDrv.h" 57 | #include "./ChipDrv/NrfDrv/NrfUart.h" 58 | 59 | 60 | #endif /* __HARDWAREHEAD_H__ */ 61 | -------------------------------------------------------------------------------- /include/Common/DhBuffManage.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __DHBUFFMANAGE_H__ 28 | #define __DHBUFFMANAGE_H__ 29 | #include "../DhGlobalHead.h" 30 | 31 | typedef struct 32 | { 33 | u4 m_u4CurrentIndex; // 当前可以使用的缓存的起始地址 34 | u4 m_u4BuffSize; // 可供申请缓存总量 35 | u1 *m_pu1Buff; 36 | }BlkDhMemoryManage; 37 | 38 | #define CREATE_MEMORY_INSTANCE(BUFF_NAME, BUFF_SIZE) \ 39 | static u1 BUFF_NAME##_BUFF[BUFF_SIZE]; \ 40 | static BlkDhMemoryManage BUFF_NAME##_INSTANCE = {0, BUFF_SIZE, BUFF_NAME##_BUFF}; \ 41 | static BlkDhMemoryManage *BUFF_NAME = &BUFF_NAME##_INSTANCE 42 | 43 | 44 | #ifdef __cplusplus 45 | #if __cplusplus 46 | extern "C"{ 47 | #endif 48 | #endif /* __cplusplus */ 49 | 50 | extern u1 *DhMemoryAlloc(BlkDhMemoryManage *pblkMemory, u4 size); 51 | #ifdef __cplusplus 52 | #if __cplusplus 53 | } 54 | #endif 55 | #endif /* __cplusplus */ 56 | 57 | 58 | #endif /* __DHBUFFMANAGE_H__ */ 59 | -------------------------------------------------------------------------------- /include/BleDrv/BleHAccuracyTimer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __BLEHACCURACYTIMER_H__ 28 | #define __BLEHACCURACYTIMER_H__ 29 | 30 | /**@brief 协议栈使用的高精度时钟 31 | 使用的nrf的timer0,目前好像只需要一个高精度的短时间定时器。就先做一个吧 32 | PS:timer虽然有4个比较寄存器但是都是用的同一个内部COUNTER计数的。无法做到完全独立。 33 | */ 34 | typedef enum 35 | { 36 | BLE_HA_TIMER0 = 0, 37 | BLE_HA_MAX, 38 | BLE_HA_TIMER_INVALID, 39 | }EnBleHAccuracyTimer; 40 | 41 | typedef void (*BleHAccuracyTimeroutHandler)(void * pvalue); 42 | 43 | #ifdef __cplusplus 44 | #if __cplusplus 45 | extern "C"{ 46 | #endif 47 | #endif /* __cplusplus */ 48 | 49 | extern void BleHAccuracyTimerInit( void ); 50 | extern void BleHAccuracyTimerStart( EnBleHAccuracyTimer timerId, u4 timeoutUs, BleHAccuracyTimeroutHandler cb, void *pvalue); 51 | extern void BleHAccuracyTimerStop(EnBleHAccuracyTimer timerId); 52 | 53 | 54 | #ifdef __cplusplus 55 | #if __cplusplus 56 | } 57 | #endif 58 | #endif /* __cplusplus */ 59 | 60 | 61 | #endif /* __BLEHACCURACYTIMER_H__ */ 62 | -------------------------------------------------------------------------------- /include/debug/DhDebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __DH_DEBUG_H__ 28 | #define __DH_DEBUG_H__ 29 | 30 | #ifdef DEBUG_LOG 31 | //#include "SEGGER_RTT_Conf.h" 32 | //#include "SEGGER_RTT.h" 33 | //#include "rtt_log.h" 34 | //#include "DhDebug.h" 35 | /* 36 | 不要在协议栈的内部时序部分代码中添加日志打印,调试信息的处理比较耗时可能需要一百多us,这已经可以影响 37 | BLE协议栈底层的时序精度要求了。 38 | */ 39 | 40 | #if defined(DEBUG_LOG_USE_RTT) 41 | #define DEBUG_INFO logi 42 | #define DEBUG_WARNING logw 43 | #define DEBUG_ERROR loge 44 | #elif defined(DEBUG_LOG_USE_UART) 45 | #define DEBUG_INFO DhPrintWithfLinefeed 46 | #define DEBUG_WARNING DhPrintWithfLinefeed 47 | #define DEBUG_ERROR DhPrintWithfLinefeed 48 | #endif 49 | 50 | 51 | #define DEBUG_DATA DebugData 52 | #define DEBUG_ASCII DebugAscii 53 | 54 | 55 | void DebugData(u1 *pu1Data, u2 len); 56 | void DebugAscii(u1 *pu1Data, u2 len); 57 | #else 58 | #define DEBUG_INFO(...) 59 | #define DEBUG_WARNING(...) 60 | #define DEBUG_ERROR(...) 61 | #define DEBUG_DATA(...) 62 | #define DEBUG_ASCII(...) 63 | 64 | #endif 65 | 66 | 67 | #endif /* __DEBUG_H__ */ 68 | 69 | -------------------------------------------------------------------------------- /include/ChipDrv/CommDrv/HardwareUart.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __HARWAREUART_H__ 28 | #define __HARWAREUART_H__ 29 | #include "../../DhGlobalHead.h" 30 | 31 | 32 | typedef enum 33 | { 34 | UART_BAUDRATE_1200 = 0, 35 | UART_BAUDRATE_2400, 36 | UART_BAUDRATE_4800, 37 | UART_BAUDRATE_9600, 38 | UART_BAUDRATE_14400, 39 | UART_BAUDRATE_19200, 40 | UART_BAUDRATE_28800, 41 | UART_BAUDRATE_38400, 42 | UART_BAUDRATE_57600, 43 | UART_BAUDRATE_76800, 44 | UART_BAUDRATE_115200, 45 | UART_BAUDRATE_230400, 46 | UART_BAUDRATE_250000, 47 | UART_BAUDRATE_460800, 48 | UART_BAUDRATE_921600, 49 | UART_BAUDRATE_1M, 50 | }EnUartBaudrate; 51 | 52 | #ifdef __cplusplus 53 | #if __cplusplus 54 | extern "C"{ 55 | #endif 56 | #endif /* __cplusplus */ 57 | 58 | extern void HwUartSimpleInit(u1 txPin, u1 rxPin, EnUartBaudrate enBaud); 59 | extern void HwUartSimpleTxData(u1 *pu1Data, u2 len); 60 | extern void DhPrintf(const char *fmt,...); 61 | extern void DhPrintWithfLinefeed(const char *fmt,...); 62 | #ifdef __cplusplus 63 | #if __cplusplus 64 | } 65 | #endif 66 | #endif /* __cplusplus */ 67 | 68 | 69 | #endif /* __HARWAREUART_H__ */ 70 | -------------------------------------------------------------------------------- /include/BleDrv/BleLPowerTimer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __BLELPOWERTIMER_H__ 28 | #define __BLELPOWERTIMER_H__ 29 | #include "../DhGlobalHead.h" 30 | 31 | 32 | /* 33 | * @brief 利用RTC实现3个ble需要使用的低功耗时钟,定时器每次只作用一次。 34 | LP_TIMER0: 用来定时长时间的定时器(几十ms-几十s),使用前会重启底层硬件RTC,使用RTC0的 compare0 寄存器实现 35 | LP_TIMER1: 用来定时短时间的定时器(几百us-几ms),在长定时器启动的情况下才能使用,使用RTC0的 compare1 寄存器实现 36 | LP_TIMER2: 同LP_TIMER1,使用RTC0的 compare2 寄存器实现 37 | */ 38 | 39 | typedef enum 40 | { 41 | BLE_LP_TIMER0 = 0, 42 | BLE_LP_TIMER1, 43 | BLE_LP_TIMER2, 44 | BLE_LP_MAX, 45 | BLE_LP_TIMER_INVALID, 46 | }EnBleLPowerTimer; 47 | 48 | typedef enum 49 | { 50 | LP_CLK_SRC_RC_250_PPM = 0, // 目前只实现使用内部RC的方式 51 | }EnBleLPowerClkSrc; 52 | typedef void (*BleLPowerTimeroutHandler)(void * pvalue); 53 | 54 | #ifdef __cplusplus 55 | #if __cplusplus 56 | extern "C"{ 57 | #endif 58 | #endif /* __cplusplus */ 59 | 60 | extern void BleLPowerTimerInit( EnBleLPowerClkSrc src, u4 u4CalTimeoutMs ); 61 | extern u4 BleLPowerTimerStart( EnBleLPowerTimer timerId, u4 timeoutUs, BleLPowerTimeroutHandler cb, void *pvalue); 62 | extern u4 BleLPowerTimerStop( EnBleLPowerTimer timerId); 63 | extern u4 BleLPowerTimerTickGet(void); 64 | 65 | 66 | 67 | #ifdef __cplusplus 68 | #if __cplusplus 69 | } 70 | #endif 71 | #endif /* __cplusplus */ 72 | 73 | 74 | #endif /* __BLELPOWERTIMER_H__ */ 75 | -------------------------------------------------------------------------------- /include/Common/DhQueue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __DHQUEUE_H__ 28 | #define __DHQUEUE_H__ 29 | #include "../DhGlobalHead.h" 30 | typedef struct 31 | { 32 | void *m_pValue; // 指向实际数据 33 | u2 m_u2QueueSize; // 队列大小,需要为2^n 34 | u2 m_u2ElemCount; // 当年队列元素个数 35 | u2 m_u2IRead; // 读指针 36 | u2 m_u2IWrite; // 写指针 37 | }BlkDhQueue; 38 | 39 | /* 40 | 队列创建 41 | !!!!!!!!!!!!! 队列大小需要时2^n !!!!!!!!!!!!!!!!!! 42 | */ 43 | #define CREATE_QUEUE_INSTANCE(QUEUE_NAME, QUEUE_ELEM_TYPE, QUEUE_SIZE) \ 44 | static QUEUE_ELEM_TYPE QUEUE_NAME##_BUFF[QUEUE_SIZE]; \ 45 | static BlkDhQueue QUEUE_NAME##_queue = {QUEUE_NAME##_BUFF,QUEUE_SIZE,0,0,0}; \ 46 | static BlkDhQueue *QUEUE_NAME = &QUEUE_NAME##_queue 47 | 48 | 49 | 50 | 51 | #ifdef __cplusplus 52 | #if __cplusplus 53 | extern "C"{ 54 | #endif 55 | #endif /* __cplusplus */ 56 | 57 | extern void *QueueEmptyElemGet( BlkDhQueue *queue ,u4 u4ElemSize); 58 | extern void *QueueValidElemGet(BlkDhQueue *queue, u4 u4ElemSize); 59 | extern u1 IsQueueEmpty( BlkDhQueue *queue ); 60 | extern u1 IsQueueFull( BlkDhQueue *queue ); 61 | extern u4 QueueInit( BlkDhQueue * queue, u2 size, void *elemBuff ); 62 | extern void QueuePop(BlkDhQueue *queue); 63 | 64 | #ifdef __cplusplus 65 | #if __cplusplus 66 | } 67 | #endif 68 | #endif /* __cplusplus */ 69 | 70 | 71 | #endif /* __DHQUEUE_H__ */ 72 | -------------------------------------------------------------------------------- /source/ChipDrv/CommDrv/HardwareUart.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../../include/DhGlobalHead.h" 28 | 29 | void HwUartSimpleInit(u1 txPin, u1 rxPin, EnUartBaudrate enBaud) 30 | { 31 | #ifdef HARDWARE_NRF51 32 | u4 baud; 33 | 34 | baud = NrfUartBaudGet(enBaud); 35 | NrfUartSimpleInit(rxPin, txPin, baud); 36 | #endif 37 | } 38 | 39 | void HwUartSimpleTxData(u1 *pu1Data, u2 len) 40 | { 41 | u2 index; 42 | #ifdef HARDWARE_NRF51 43 | for ( index = 0; index < len; index++ ) 44 | { 45 | NrfUartSimpleTxByte(pu1Data[index]); 46 | } 47 | #endif 48 | } 49 | 50 | 51 | /* 带换行的打印*/ 52 | void DhPrintWithfLinefeed(const char *fmt,...) 53 | { 54 | u2 outLen; 55 | u1 buff[1024]; 56 | va_list args; 57 | 58 | va_start(args,fmt); 59 | outLen = vsnprintf((char *)buff, sizeof(buff), fmt, args); 60 | if ( outLen < (sizeof(buff)-2) ) 61 | { 62 | buff[outLen] = '\r'; 63 | buff[outLen+1] = '\n'; 64 | } 65 | outLen += 2; 66 | va_end(args); 67 | 68 | HwUartSimpleTxData(buff, outLen); 69 | 70 | } 71 | 72 | void DhPrintf(const char *fmt,...) 73 | { 74 | u2 outLen; 75 | u1 buff[1024]; 76 | va_list args; 77 | 78 | va_start(args,fmt); 79 | outLen = vsnprintf((char *)buff, sizeof(buff), fmt, args); 80 | va_end(args); 81 | 82 | HwUartSimpleTxData(buff, outLen); 83 | 84 | } 85 | -------------------------------------------------------------------------------- /include/ChipDrv/NrfDrv/system_nrf51.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2015, Nordic Semiconductor ASA 2 | * All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * * Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 10 | * * Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * * Neither the name of Nordic Semiconductor ASA nor the names of its 15 | * contributors may be used to endorse or promote products derived from 16 | * this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | * 29 | */ 30 | 31 | #ifndef SYSTEM_NRF51_H 32 | #define SYSTEM_NRF51_H 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | #include 39 | 40 | 41 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ 42 | 43 | /** 44 | * Initialize the system 45 | * 46 | * @param none 47 | * @return none 48 | * 49 | * @brief Setup the microcontroller system. 50 | * Initialize the System and update the SystemCoreClock variable. 51 | */ 52 | extern void SystemInit (void); 53 | 54 | /** 55 | * Update SystemCoreClock variable 56 | * 57 | * @param none 58 | * @return none 59 | * 60 | * @brief Updates the SystemCoreClock with current core Clock 61 | * retrieved from cpu registers. 62 | */ 63 | extern void SystemCoreClockUpdate (void); 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | #endif /* SYSTEM_NRF51_H */ 70 | -------------------------------------------------------------------------------- /source/debug/rtt_log.c: -------------------------------------------------------------------------------- 1 | 2 | #include "../../include/DhGlobalHead.h" 3 | 4 | #ifdef DEBUG_LOG 5 | 6 | 7 | // Forward declartion of Segger vprintf. 8 | int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList); 9 | 10 | #define LOG_BUFF 0 11 | #define BUFF_SIZE 1024 12 | #define NULL 0 13 | 14 | // Spaces and length for maximum file names 15 | #define MAX_FILE_LEN 25 16 | 17 | static char buffer[BUFF_SIZE]; // The buffer 18 | 19 | void rtt_log_general_print(int terminal, const char* color, const char *file, int line, const char * sFormat, ...) { 20 | static int initialized = 0; 21 | //01234567890123456789012345 22 | //static const char *spaces = " "; 23 | // const char *lastSlash = file; 24 | // const char *current = file; 25 | // int nameLen; 26 | va_list ParamList; 27 | 28 | if(!initialized) { 29 | SEGGER_RTT_ConfigUpBuffer(LOG_BUFF, NULL, buffer, BUFF_SIZE, SEGGER_RTT_MODE_NO_BLOCK_SKIP); 30 | initialized = 1; 31 | } 32 | 33 | // Set Terminal 34 | SEGGER_RTT_SetTerminal(terminal); 35 | 36 | // Set Color 37 | SEGGER_RTT_WriteString(LOG_BUFF, color); 38 | 39 | // Print file name 40 | 41 | // while(*current!=0) { 42 | // if(*current == '\\' || *current == '/') 43 | // lastSlash = current+1; 44 | // current++; 45 | // } 46 | // Print name/line number 47 | // SEGGER_RTT_printf(LOG_BUFF, "%s:%d",lastSlash,line); 48 | // 49 | // nameLen = (current-lastSlash)+1+(line>999?4:((line>99)?3:((line>9)?2:1))); 50 | // 51 | // // Print justification spacing 52 | // if(nameLen 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | #include "../source/project_conf.h" 40 | 41 | #include "DhTypes.h" 42 | #include "DhBleDefine.h" 43 | #include "DhBleAux.h" 44 | #include "./Common/DhQueue.h" 45 | #include "./Common/DhBuffManage.h" 46 | #include "./debug/DhDebug.h" 47 | #include "./DhError.h" 48 | 49 | #include "./Crypto/SwAes/aes.h" 50 | #include "./Crypto/SwAes/aes_locl.h" 51 | 52 | #define CRITICAL_REGION_ENTER() __disable_irq() 53 | #define CRITICAL_REGION_EXIT() __enable_irq() 54 | 55 | #ifdef __cplusplus 56 | #if __cplusplus 57 | extern "C"{ 58 | #endif 59 | #endif /* __cplusplus */ 60 | 61 | 62 | #ifdef __cplusplus 63 | #if __cplusplus 64 | } 65 | #endif 66 | #endif /* __cplusplus */ 67 | 68 | #include "HardwareHead.h" 69 | #include "./BleDrv/BleLPowerTimer.h" 70 | #include "./BleDrv/BleHAccuracyTimer.h" 71 | #include "./BleDrv/BleRadioDrv.h" 72 | #include "./BleStack/BleAdvertising.h" 73 | #include "./BleStack/BleLink/BleLink.h" 74 | #include "./BleStack/BleLink/BleLinkAdvertising.h" 75 | #include "./BleStack/BleLink/BleLinkConnect.h" 76 | #include "./BleStack/BleLink/BleLinkControl.h" 77 | #include "./BleStack/BleL2cap.h" 78 | #include "./BleStack/BleAtt.h" 79 | #include "./BleStack/BleGatt.h" 80 | #include "./BleStack/BleGap.h" 81 | #include "./BleStack/BleSecurityManager.h" 82 | #include "./BleStack/DhBleEventNtf.h" 83 | 84 | #endif /* __DHGLOBALHEAD_H__ */ 85 | -------------------------------------------------------------------------------- /include/ChipDrv/NrfDrv/NrfTimerDrv.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __NRFTIMERDRV_H__ 28 | #define __NRFTIMERDRV_H__ 29 | #include "../../DhGlobalHead.h" 30 | 31 | typedef enum 32 | { 33 | NRF_TIMER_CMPREG_0 = 0, 34 | NRF_TIMER_CMPREG_1, 35 | NRF_TIMER_CMPREG_2, 36 | NRF_TIMER_CMPREG_3, 37 | NRF_TIMER_CMPREG_INVALID 38 | }EnNrfTimerCmpReg; 39 | 40 | typedef enum 41 | { 42 | NRF_TIMER0_EVT_COMPARE0 = 0, 43 | NRF_TIMER0_EVT_COMPARE1, 44 | NRF_TIMER0_EVT_COMPARE2, 45 | NRF_TIMER0_EVT_COMPARE3, 46 | }EnNrfTimer0Evt; 47 | 48 | 49 | /* RTC0 的中断回调函数 */ 50 | typedef void (*Timer0IntHandler)(EnNrfTimer0Evt event); 51 | 52 | #ifdef __cplusplus 53 | #if __cplusplus 54 | extern "C"{ 55 | #endif 56 | #endif /* __cplusplus */ 57 | 58 | /** 59 | *@brief: NrfTimer0Clear 60 | *@details: 清空Timer0计数 61 | *@param[in] void 62 | *@retval: void 63 | */ 64 | __INLINE void NrfTimer0Clear(void){NRF_TIMER0->TASKS_CLEAR = 1;} 65 | 66 | 67 | /** 68 | *@brief: NrfTimer0Start 69 | *@details: 启动Timer0开始计数 70 | *@param[in] void 71 | *@retval: void 72 | */ 73 | __INLINE void NrfTimer0Start(void){NRF_TIMER0->TASKS_START = 1;} 74 | 75 | /** 76 | *@brief: NrfTimer0Stop 77 | *@details: 停止Timer0时钟计数 78 | *@param[in] void 79 | *@retval: void 80 | */ 81 | __INLINE void NrfTimer0Stop(void){NRF_TIMER0->TASKS_STOP = 1;} 82 | 83 | extern void NrfTimer0Init(void); 84 | extern void NrfTimer0HandlerRegister(Timer0IntHandler IntHandler); 85 | extern void NrfTimer0SetCmpReg(EnNrfTimerCmpReg reg, u4 u4TimeoutUs); 86 | 87 | #ifdef __cplusplus 88 | #if __cplusplus 89 | } 90 | #endif 91 | #endif /* __cplusplus */ 92 | 93 | 94 | #endif /* __NRFDRVRTC_H__ */ 95 | -------------------------------------------------------------------------------- /source/BleStack/DhBleEventNtf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../include/DhGlobalHead.h" 28 | 29 | #define BLE_EVENT_NTF_IRQ SWI0_IRQn 30 | #define BLE_EVENT_NTF_IRQ_HANDLER SWI0_IRQHandler 31 | static BleEventHandler s_bleEventHandler = NULL; 32 | 33 | #define BLE_EVENT_QUEUE_SIZE (16) /* 必须是2的n次幂*/ 34 | 35 | /* 创建事件通知队列 */ 36 | CREATE_QUEUE_INSTANCE(BLE_EVENT_NTF_QUEUE, BlkBleEvent, BLE_EVENT_QUEUE_SIZE); 37 | 38 | void DhBleEventNtfInit(void) 39 | { 40 | 41 | // 事件通知使用最低优先级 42 | NVIC_ClearPendingIRQ(BLE_EVENT_NTF_IRQ); 43 | NVIC_SetPriority(BLE_EVENT_NTF_IRQ, DH_IRQ_PRIORITY_3); 44 | NVIC_EnableIRQ(BLE_EVENT_NTF_IRQ); 45 | } 46 | 47 | void DhBleEventHandlerReg(BleEventHandler handler) 48 | { 49 | s_bleEventHandler = handler; 50 | } 51 | 52 | 53 | /** 54 | *@brief: BleEventPush 55 | *@details: BLE事件入队,通知上层应用 56 | *@param[in] BlkBleEvent event 57 | *@retval: 58 | */ 59 | u4 BleEventPush(BlkBleEvent event) 60 | { 61 | BlkBleEvent *pBlkEvent; 62 | pBlkEvent = (BlkBleEvent *)QueueEmptyElemGet(BLE_EVENT_NTF_QUEUE, sizeof(BlkBleEvent)); 63 | if( NULL == pBlkEvent) 64 | { 65 | return ERR_DH_QUEUE_FULL; 66 | } 67 | *pBlkEvent = event; 68 | 69 | /* 触发中断,执行后通知上层动作 */ 70 | NVIC_SetPendingIRQ(BLE_EVENT_NTF_IRQ); 71 | 72 | return DH_SUCCESS; 73 | } 74 | 75 | void BLE_EVENT_NTF_IRQ_HANDLER(void) 76 | { 77 | static BlkBleEvent bleEvent; 78 | BlkBleEvent *pBleEvent; 79 | 80 | do{ 81 | /* 依次提取队列中的事件调用上层回调 82 | 对 BLE_EVENT_NTF_QUEUE队列的出队操作只在这里 83 | 没有临界区问题 84 | */ 85 | pBleEvent = (BlkBleEvent *)QueueValidElemGet(BLE_EVENT_NTF_QUEUE, sizeof(BlkBleEvent)); 86 | if( NULL != pBleEvent) 87 | { 88 | bleEvent = *pBleEvent; 89 | QueuePop(BLE_EVENT_NTF_QUEUE); 90 | if( NULL != s_bleEventHandler ) 91 | { 92 | s_bleEventHandler(&bleEvent); 93 | } 94 | } 95 | }while(NULL != pBleEvent ); 96 | } 97 | -------------------------------------------------------------------------------- /include/ChipDrv/NrfDrv/NrfRtcDrv.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __NRFDRVRTC_H__ 28 | #define __NRFDRVRTC_H__ 29 | #include "../../DhGlobalHead.h" 30 | 31 | 32 | #define SET_RTC_CC_ABSOLUTE (1) 33 | #define SET_RTC_CC_RELATIVE (0) 34 | 35 | typedef enum 36 | { 37 | NRF_RTC_CMPREG_0 = 0, 38 | NRF_RTC_CMPREG_1, 39 | NRF_RTC_CMPREG_2, 40 | NRF_RTC_CMPREG_INVALID 41 | }EnNrfRtcCmpReg; 42 | 43 | typedef enum 44 | { 45 | NRF_RTC0_EVT_COMPARE0 = 0, 46 | NRF_RTC0_EVT_COMPARE1, 47 | NRF_RTC0_EVT_COMPARE2, 48 | }EnNrfRtc0Evt; 49 | 50 | 51 | /* RTC0 的中断回调函数 */ 52 | typedef void (*Rtc0IntHandler)(EnNrfRtc0Evt event); 53 | 54 | #ifdef __cplusplus 55 | #if __cplusplus 56 | extern "C"{ 57 | #endif 58 | #endif /* __cplusplus */ 59 | 60 | /** 61 | *@brief: NrfRtc0Clear 62 | *@details: 清空RTC0计数 63 | *@param[in] void 64 | *@retval: void 65 | */ 66 | __INLINE void NrfRtc0Clear(void){NRF_RTC0->TASKS_CLEAR = 1;} 67 | 68 | 69 | /** 70 | *@brief: NrfRtc0Start 71 | *@details: 启动RTC0开始计数 72 | *@param[in] void 73 | *@retval: void 74 | */ 75 | __INLINE void NrfRtc0Start(void){NRF_RTC0->TASKS_START = 1;} 76 | 77 | /** 78 | *@brief: NrfRtc0Stop 79 | *@details: 停止RTC0时钟计数 80 | *@param[in] void 81 | *@retval: void 82 | */ 83 | __INLINE void NrfRtc0Stop(void){NRF_RTC0->TASKS_STOP = 1;} 84 | 85 | extern u4 NrfRtc0CounterGet(void); 86 | extern void NrfRtc0Init(void); 87 | extern void NrfRtc0HandlerRegister(Rtc0IntHandler IntHandler); 88 | extern void NrfRtc0SetCmpReg(EnNrfRtcCmpReg reg, u4 u4TimeoutUs, u1 isAbsolute); 89 | extern void NrfRtc0DisableCmpReg(EnNrfRtcCmpReg reg); 90 | 91 | extern void NrfRtc0HandlerRegister ( Rtc0IntHandler IntHandler); 92 | 93 | #ifdef __cplusplus 94 | #if __cplusplus 95 | } 96 | #endif 97 | #endif /* __cplusplus */ 98 | 99 | 100 | #endif /* __NRFDRVRTC_H__ */ 101 | -------------------------------------------------------------------------------- /test/test_rtc0_timer0.c: -------------------------------------------------------------------------------- 1 | 2 | #include "../include/DhGlobalHead.h" 3 | #define LEDS_NUMBER 4 4 | #define LED_ACTIVE 0 5 | static const uint32_t m_board_led_list[LEDS_NUMBER] = {13, 14, 15, 16}; 6 | 7 | static void nrf_gpio_cfg_output(uint32_t pin_number) { 8 | NRF_P0->PIN_CNF[pin_number] = ((1<<0) // output 9 | | (1<<1) 10 | | (3<<2)); 11 | } 12 | 13 | 14 | static void nrf_gpio_cfg_interrupt(uint32_t pin_number) { 15 | NRF_P0->PIN_CNF[pin_number] = ((0<<0) // input 16 | | (0<<1) 17 | | (3<<2)) 18 | | (3<<16); 19 | } 20 | 21 | static void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value) 22 | { 23 | if (value == 0) 24 | { 25 | NRF_P0->OUTCLR = (1<OUTSET = (1<OUT; 37 | 38 | reg->OUTSET = (~pins_state & (1UL << pin_number)); 39 | reg->OUTCLR = (pins_state & (1UL << pin_number)); 40 | } 41 | 42 | static void bsp_board_leds_init(void) 43 | { 44 | uint32_t i; 45 | for(i = 0; i < LEDS_NUMBER; ++i) 46 | { 47 | nrf_gpio_cfg_output(m_board_led_list[i]); 48 | nrf_gpio_pin_write(m_board_led_list[i], ( LED_ACTIVE)); 49 | } 50 | } 51 | 52 | 53 | #define LED_1 (13) 54 | #define LED_2 (14) 55 | 56 | 57 | void rtc0_timeout_callback(EnNrfRtc0Evt event) { 58 | SEGGER_SYSVIEW_RecordEnterISR(); 59 | 60 | NrfRtc0Clear(); 61 | NrfRtc0SetCmpReg(NRF_RTC_CMPREG_0, 500000, 1); 62 | SEGGER_SYSVIEW_RecordExitISR(); 63 | 64 | } 65 | 66 | void timer0_timeout_callback(EnNrfTimer0Evt event) { 67 | SEGGER_SYSVIEW_RecordEnterISR(); 68 | NrfTimer0Clear(); 69 | NrfTimer0SetCmpReg(NRF_TIMER_CMPREG_0, 400000); 70 | SEGGER_SYSVIEW_RecordExitISR(); 71 | 72 | } 73 | 74 | int main(void) { 75 | uint32_t v; 76 | bsp_board_leds_init(); 77 | SEGGER_SYSVIEW_Conf(); 78 | SEGGER_SYSVIEW_OnIdle(); 79 | 80 | // 启动HFCLK,LFCLK。LFCLK每秒校准一次(校准源为HFCLK) 81 | NrfHFClkSrcSetXtal(); 82 | NrfLFClkStart(LFCLK_SRC_RC, 1000, 1); 83 | 84 | // 设置rtc0中断回调,初始化rtc0, 设置 1 秒中断一次。 85 | NrfRtc0HandlerRegister(rtc0_timeout_callback); 86 | NrfRtc0Init(); 87 | NrfRtc0SetCmpReg(NRF_RTC_CMPREG_0, 1000000, 1); 88 | 89 | // 初始化timer,2秒中断一次 90 | NrfTimer0Init(); 91 | NrfTimer0SetCmpReg(NRF_TIMER_CMPREG_0, 2000000); 92 | NrfTimer0HandlerRegister(timer0_timeout_callback); 93 | 94 | NrfRtc0Start(); 95 | NrfTimer0Start(); 96 | for( ; ; ) { 97 | nrf_gpio_pin_toggle(LED_2); 98 | nrf_delay_ms(1000); 99 | //SEGGER_SYSVIEW_PrintfHost("toggle\n"); 100 | } 101 | 102 | return 0; 103 | } 104 | 105 | 106 | -------------------------------------------------------------------------------- /include/debug/SEGGER_RTT_Conf.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * SEGGER MICROCONTROLLER SYSTEME GmbH * 3 | * Solutions for real time microcontroller applications * 4 | ********************************************************************** 5 | * * 6 | * (c) 1996-2014 SEGGER Microcontroller Systeme GmbH * 7 | * * 8 | * Internet: www.segger.com Support: support@segger.com * 9 | * * 10 | ********************************************************************** 11 | ---------------------------------------------------------------------- 12 | File : SEGGER_RTT_Conf.h 13 | Date : 17 Dec 2014 14 | Purpose : Implementation of SEGGER real-time terminal which allows 15 | real-time terminal communication on targets which support 16 | debugger memory accesses while the CPU is running. 17 | ---------------------------END-OF-HEADER------------------------------ 18 | */ 19 | 20 | /********************************************************************* 21 | * 22 | * Defines, configurable 23 | * 24 | ********************************************************************** 25 | */ 26 | 27 | #define SEGGER_RTT_MAX_NUM_UP_BUFFERS (2) // Max. number of up-buffers (T->H) available on this target (Default: 2) 28 | #define SEGGER_RTT_MAX_NUM_DOWN_BUFFERS (2) // Max. number of down-buffers (H->T) available on this target (Default: 2) 29 | 30 | #define BUFFER_SIZE_UP (350) // Size of the buffer for terminal output of target, up to host (Default: 1k) 31 | #define BUFFER_SIZE_DOWN (16) // Size of the buffer for terminal input to target from host (Usually keyboard input) (Default: 16) 32 | 33 | #define SEGGER_RTT_PRINTF_BUFFER_SIZE (64) // Size of buffer for RTT printf to bulk-send chars via RTT (Default: 64) 34 | 35 | // 36 | // Target is not allowed to perform other RTT operations while string still has not been stored completely. 37 | // Otherwise we would probably end up with a mixed string in the buffer. 38 | // If using RTT from within interrupts, multiple tasks or multi processors, define the SEGGER_RTT_LOCK() and SEGGER_RTT_UNLOCK() function here. 39 | // 40 | #define SEGGER_RTT_LOCK() 41 | #define SEGGER_RTT_UNLOCK() 42 | 43 | // 44 | // Define SEGGER_RTT_IN_RAM as 1 45 | // when using RTT in RAM targets (init and data section both in RAM). 46 | // This prevents the host to falsly identify the RTT Callback Structure 47 | // in the init segment as the used Callback Structure. 48 | // 49 | // When defined as 1, 50 | // the first call to an RTT function will modify the ID of the RTT Callback Structure. 51 | // To speed up identifying on the host, 52 | // especially when RTT functions are not called at the beginning of execution, 53 | // SEGGER_RTT_Init() should be called at the start of the application. 54 | // 55 | #define SEGGER_RTT_IN_RAM (0) 56 | 57 | /*************************** End of file ****************************/ 58 | -------------------------------------------------------------------------------- /include/BleStack/DhBleEventNtf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __DHBLEEVENTNTF_H__ 28 | #define __DHBLEEVENTNTF_H__ 29 | 30 | #include "../DhGlobalHead.h" 31 | 32 | #define DH_BLE_CONNECTED // 蓝牙连接 33 | #define DH_BLE_DISCONNECTED // 蓝牙断开 34 | 35 | typedef enum 36 | { 37 | BLE_EVENT_CONNECTED = 0x0001, 38 | BLE_EVENT_DISCONNECTED, 39 | BLE_EVENT_CONN_UPDATE, 40 | BLE_EVENT_RECV_WRITE, 41 | BlE_EVENT_RECV_HVC, // handle value confirm 对indication的响应 42 | 43 | BLE_EVENT_SM_DISP_KEY, 44 | BLE_EVENT_SM_INPUT_KEY, 45 | BLE_EVENT_SM_PAIRING_REQ, 46 | BLE_EVENT_SM_ENC_COMPLETE, // 链路加密过程完成。 47 | BLE_EVENT_SM_BONDING_COMPLETE, 48 | BLE_EVENT_SM_LTK_REQ, 49 | BLE_EVENT_SM_FAILED, 50 | }EnBleEvtType; 51 | 52 | 53 | 54 | 55 | typedef struct 56 | { 57 | EnBleEvtType m_u2EvtType; // 事件类型 58 | union 59 | { 60 | BlkBleRecvWriteEvent m_blkWriteInfo; 61 | BlkBleConnectedEvent m_blkConnInfo; 62 | BlkBleDisconnectedEvent m_blkDisconnInfo; 63 | BlkBleConnUpdateEvent m_blkConnUpdateInfo; 64 | 65 | BlkBleSmKeyDispEvent m_blkSmKeyDisp; 66 | BlkBleSmKeyInputEvent m_blkSmKeyInput; 67 | BlkBleSmPairingReqEvent m_blkSmPairingReq; 68 | BlkBleSmFailedEvent m_blkSmFailed; 69 | BlkBleSmEncCompleteEvent m_blkSmEncComplete; 70 | BlKBleSmBondingCompleteEvent m_blkSmBondingComplete; 71 | BlkBleSmLtkReqEvent m_blkSmLtkReq; 72 | }m_event; 73 | }BlkBleEvent; 74 | 75 | typedef void (*BleEventHandler)(BlkBleEvent *event); 76 | 77 | #ifdef __cplusplus 78 | #if __cplusplus 79 | extern "C"{ 80 | #endif 81 | #endif /* __cplusplus */ 82 | 83 | extern u4 BleEventPush(BlkBleEvent event); 84 | extern void DhBleEventNtfInit(void); 85 | extern void DhBleEventHandlerReg(BleEventHandler handler); 86 | 87 | 88 | #ifdef __cplusplus 89 | #if __cplusplus 90 | } 91 | #endif 92 | #endif /* __cplusplus */ 93 | 94 | 95 | #endif /* __DHBLEEVENTNTF_H__ */ 96 | -------------------------------------------------------------------------------- /source/BleStack/BleGap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../include/DhGlobalHead.h" 28 | 29 | u4 BleGapAddrCfg(BlkBleAddrInfo addr) 30 | { 31 | LinkAdvAddrInfoCfg(addr); 32 | 33 | return DH_SUCCESS; 34 | } 35 | 36 | u4 BleGapDeviceNameCfg(char *pu1Name, u1 len) 37 | { 38 | u2 handle; 39 | BlkBleAttribute *pblkAtt; 40 | 41 | if( NULL==pu1Name || len>BLE_DEV_NAME_MAX_SIZE ) 42 | { 43 | return ERR_GAP_INVALID_PARAMS; 44 | } 45 | handle = BleDeviceNameAttHandleGet(); 46 | if( BLE_ATT_INVALID_HANDLE == handle ) 47 | { 48 | return ERR_GAP_INTERNAL; 49 | } 50 | BleGattFindAttByHandle(handle, &pblkAtt); 51 | if( NULL == pblkAtt ) 52 | { 53 | return ERR_GAP_INTERNAL; 54 | } 55 | if( len > pblkAtt->m_blkAttValue.m_u2MaxSize ) 56 | { 57 | len = pblkAtt->m_blkAttValue.m_u2MaxSize; 58 | } 59 | memcpy(pblkAtt->m_blkAttValue.m_pu1AttValue, pu1Name, len); 60 | pblkAtt->m_blkAttValue.m_u2CurrentLen = len; 61 | 62 | return DH_SUCCESS; 63 | } 64 | 65 | /** 66 | *@brief: BleGapDeviceNameGet 67 | *@details: 获取设备名 68 | *@param[out] pu1Name 返回设备名的Buff 69 | *@param[in|out] pu1BuffSize buff大小,返回实际设备名长度 70 | *@retval: 71 | */ 72 | u4 BleGapDeviceNameGet(char *pu1Name, u1 *pu1BuffSize) 73 | { 74 | u2 handle; 75 | BlkBleAttribute *pblkAtt; 76 | 77 | if( NULL==pu1Name ) 78 | { 79 | return ERR_GAP_INVALID_PARAMS; 80 | } 81 | handle = BleDeviceNameAttHandleGet(); 82 | if( BLE_ATT_INVALID_HANDLE == handle ) 83 | { 84 | return ERR_GAP_INTERNAL; 85 | } 86 | BleGattFindAttByHandle(handle, &pblkAtt); 87 | if( NULL == pblkAtt ) 88 | { 89 | return ERR_GAP_INTERNAL; 90 | } 91 | if( *pu1BuffSize < pblkAtt->m_blkAttValue.m_u2CurrentLen) 92 | { 93 | return ERR_GAP_BUFF_LEN; 94 | } 95 | memcpy(pu1Name, pblkAtt->m_blkAttValue.m_pu1AttValue, pblkAtt->m_blkAttValue.m_u2CurrentLen); 96 | *pu1BuffSize = pblkAtt->m_blkAttValue.m_u2CurrentLen; 97 | 98 | return DH_SUCCESS; 99 | } 100 | -------------------------------------------------------------------------------- /include/BleStack/BleAtt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __BLEATT_H__ 28 | #define __BLEATT_H__ 29 | #include "../DhGlobalHead.h" 30 | typedef struct 31 | { 32 | u1 m_u1UuidType; /* 当前只支持16/128bit长度的UUID */ 33 | u1 *m_pu1Uuid; /* 指向实际存放uuid的buff,UUID 按LSB格式存放 */ 34 | }BlkAttributeType; 35 | 36 | typedef struct 37 | { 38 | u2 m_u2MaxSize; /* 属性的大小*/ 39 | u2 m_u2CurrentLen; /* 属性值的当前长度*/ 40 | u1 *m_pu1AttValue; /* 具体属性值*/ 41 | }BlkAttributeValue; 42 | 43 | typedef struct 44 | { 45 | BlkAttributeType m_blkAttType; 46 | BlkAttributeValue m_blkAttValue; 47 | u2 m_u2AttPermission; /* 许可条件,上层自定义实现*/ 48 | u2 m_u2CCCDHandle; 49 | }BlkBleAttribute; 50 | 51 | 52 | 53 | #define ATT_OPERATE_READ 0x01 54 | #define ATT_OPERATE_WRITE 0x02 55 | 56 | #define ATT_PERMISSION_READ 0x01 /* 允许读 */ 57 | #define ATT_PERMISSION_WRITE 0x02 /* 允许写 */ 58 | #define ATT_PERMISSION_READ_AUTHENTICATION 0x04 /* 读需要链路认证加密过 */ 59 | #define ATT_PERMISSION_WRITE_AUTHENTICATION 0x08 /* 写需要链路认证加密过 */ 60 | //#define ATT_PERMISSION_READ_AUTHORIZATION 0x10 /* 读需要需要授权,暂不实现 */ 61 | //#define ATT_PERMISSION_WRITE_AUTHORIZATION 0x20 /* 写需要需要授权,暂不实现 */ 62 | #define ATT_PERMISSION_READ_ENCRYPTION 0x40 /* 读需要链路加密--仅加密就可以 */ 63 | #define ATT_PERMISSION_WRITE_ENCRYPTION 0x80 /* 写需要链路加密--仅加密就可以 */ 64 | 65 | #define ATT_PROPERTIES_BROADCAST 0x01 66 | #define ATT_PROPERTIES_READ 0x02 67 | #define ATT_PROPERTIES_WRITE_WITHOUT_RSP 0x04 68 | #define ATT_PROPERTIES_WRITE 0x08 69 | #define ATT_PROPERTIES_NOTIFY 0x10 70 | #define ATT_PROPERTIES_INDICATE 0x20 71 | #define ATT_PROPERTIES_AUTH_SIGN_WRITE 0x40 72 | #define ATT_PROPERTIES_EXTEND_PEOPERTIES 0x80 73 | 74 | #define UUID_TYPE_16BIT 2 75 | #define UUID_TYPE_128BIT 16 76 | 77 | #define BLE_ATT_INVALID_HANDLE 0x0000 78 | 79 | #ifdef __cplusplus 80 | #if __cplusplus 81 | extern "C"{ 82 | #endif 83 | #endif /* __cplusplus */ 84 | 85 | 86 | extern u4 BleAttReqHandle( u1 *pu1Data, u2 len ); 87 | extern u4 BleAttSendIndication(u2 u2AttHandle, u1 *pu1AttValue, u2 len); 88 | extern u4 BleAttSendNotify(u2 u2AttHandle, u1 *pu1AttValue, u2 len); 89 | 90 | #ifdef __cplusplus 91 | #if __cplusplus 92 | } 93 | #endif 94 | #endif /* __cplusplus */ 95 | 96 | 97 | #endif /* __BLEATT_H__ */ 98 | -------------------------------------------------------------------------------- /source/BleStack/BleL2cap.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../include/DhGlobalHead.h" 28 | 29 | 30 | /** 31 | *@brief: BleL2capPackage 32 | *@details: l2cap数据封包并发送 33 | *@param[in] u2ChannelId l2cap channel id 34 | *@param[in] pu1Data 待封包数据 35 | *@param[in] len 待封包数据长度 36 | 37 | *@retval: DH_SUCCESS 38 | */ 39 | 40 | u4 BleL2capDataSend(u2 u2ChannelId, u1 *pu1Data, u2 len) 41 | { 42 | BlkHostToLinkData hostData; 43 | u2 index = 0; 44 | 45 | if ( NULL == pu1Data ) 46 | { 47 | return ERR_L2CAP_INVALID_PARAM; 48 | } 49 | if ( (len+BLE_L2CAP_HEADER_LEN) > (BLE_PDU_LENGTH-BLE_PDU_HEADER_LENGTH) ) 50 | { 51 | return ERR_L2CAP_INVALID_LENGTH; 52 | } 53 | 54 | hostData.m_pu1HostData[index++] = len&0xff; 55 | hostData.m_pu1HostData[index++] = len>>8; 56 | hostData.m_pu1HostData[index++] = u2ChannelId&0xff; 57 | hostData.m_pu1HostData[index++] = u2ChannelId>>8; 58 | memcpy(hostData.m_pu1HostData+index, pu1Data, len); 59 | hostData.m_u2Length = index+len; 60 | hostData.m_u1PacketFlag = DATA_PACKET; 61 | 62 | if(BleHostDataToLinkPush(hostData) != DH_SUCCESS ) 63 | { 64 | return ERR_L2CAP_INSUFFICIENT_RESOURCE; 65 | } 66 | 67 | return DH_SUCCESS; 68 | } 69 | 70 | /** 71 | *@brief: BleL2capHandle 72 | *@details: 处理L2cap层的数据包 73 | *@param[in] pu1Data L2cap层数据包 74 | *@param[in] u2length 数据长度 75 | 76 | *@retval: DH_SUCCESS 77 | */ 78 | 79 | u4 BleL2capHandle(u1 *pu1Data, u2 u2length) 80 | { 81 | u2 u2PayloadLen,u2ChannelId; 82 | 83 | if( NULL==pu1Data ) 84 | { 85 | return ERR_L2CAP_INVALID_PARAM; 86 | } 87 | u2PayloadLen = pu1Data[0]+((pu1Data[1]<<8)&0xFF00); 88 | if( (u2PayloadLen+0x04) != u2length ) 89 | { 90 | return ERR_L2CAP_INVALID_LENGTH; 91 | } 92 | 93 | u2ChannelId = pu1Data[2]+ ((pu1Data[3]<<8)&0xFF00); 94 | switch(u2ChannelId) 95 | { 96 | case BLE_L2CAP_ATT_CHANNEL_ID: 97 | BleAttReqHandle(pu1Data+BLE_L2CAP_HEADER_LEN, u2PayloadLen); 98 | break; 99 | 100 | case BLE_L2CAP_SM_CHANNEL_ID: 101 | BleSecurityReqHandle(pu1Data+BLE_L2CAP_HEADER_LEN, u2PayloadLen); 102 | break; 103 | 104 | case BLE_L2CAP_SIGNAL_CHANNEL_ID: 105 | break; 106 | 107 | 108 | default: 109 | break; 110 | } 111 | 112 | 113 | return DH_SUCCESS; 114 | } 115 | -------------------------------------------------------------------------------- /test/test_radio.c: -------------------------------------------------------------------------------- 1 | 2 | #include "../include/DhGlobalHead.h" 3 | #define LEDS_NUMBER 4 4 | #define LED_ACTIVE 0 5 | static const uint32_t m_board_led_list[LEDS_NUMBER] = {13, 14, 15, 16}; 6 | 7 | static void nrf_gpio_cfg_output(uint32_t pin_number) { 8 | NRF_P0->PIN_CNF[pin_number] = ((1<<0) // output 9 | | (1<<1) 10 | | (3<<2)); 11 | } 12 | 13 | 14 | static void nrf_gpio_cfg_interrupt(uint32_t pin_number) { 15 | NRF_P0->PIN_CNF[pin_number] = ((0<<0) // input 16 | | (0<<1) 17 | | (3<<2)) 18 | | (3<<16); 19 | } 20 | 21 | static void nrf_gpio_pin_write(uint32_t pin_number, uint32_t value) 22 | { 23 | if (value == 0) 24 | { 25 | NRF_P0->OUTCLR = (1<OUTSET = (1<OUT; 37 | 38 | reg->OUTSET = (~pins_state & (1UL << pin_number)); 39 | reg->OUTCLR = (pins_state & (1UL << pin_number)); 40 | } 41 | 42 | static void bsp_board_leds_init(void) 43 | { 44 | uint32_t i; 45 | for(i = 0; i < LEDS_NUMBER; ++i) 46 | { 47 | nrf_gpio_cfg_output(m_board_led_list[i]); 48 | nrf_gpio_pin_write(m_board_led_list[i], ( LED_ACTIVE)); 49 | } 50 | } 51 | 52 | 53 | #define LED_1 (13) 54 | #define LED_2 (14) 55 | 56 | 57 | void rtc0_timeout_callback(EnNrfRtc0Evt event) { 58 | SEGGER_SYSVIEW_RecordEnterISR(); 59 | 60 | // 广播周期到了,再次发送之前设置的广播 61 | NrfRadioStartTx(); 62 | 63 | // 重启定时器 64 | NrfRtc0Clear(); 65 | NrfRtc0SetCmpReg(NRF_RTC_CMPREG_0, 100000, 1); 66 | NrfRtc0Start(); 67 | 68 | SEGGER_SYSVIEW_RecordExitISR(); 69 | } 70 | 71 | int main(void) { 72 | uint32_t v; 73 | bsp_board_leds_init(); 74 | SEGGER_SYSVIEW_Conf(); 75 | SEGGER_SYSVIEW_OnIdle(); 76 | 77 | // 启动HFCLK,LFCLK。LFCLK每秒校准一次(校准源为HFCLK) 78 | NrfHFClkSrcSetXtal(); 79 | NrfLFClkStart(LFCLK_SRC_RC, 1000, 1); 80 | 81 | // 设置rtc0中断回调,初始化rtc0, 设置 1 秒中断一次。 82 | NrfRtc0HandlerRegister(rtc0_timeout_callback); 83 | NrfRtc0Init(); 84 | NrfRtc0SetCmpReg(NRF_RTC_CMPREG_0, 100000, 1); 85 | 86 | // 初始化 radio,发射功率设置为-4,dBm 87 | NrfRadioInit(); 88 | NrfRadioTxPowerSet( -4 ); 89 | 90 | // 广播通道的CRC初始值为固定的 0x555555 91 | NrfRadioCrcInitValueCfg(BLE_ADV_CHANNEL_CRC_INIT); 92 | // 都使用逻辑0地址,关联的物理接入地址为广播使用的0x8E89BED6 93 | NrfLogicAddrCfg( 0, BLE_ADV_ACCESS_ADDR ); 94 | NrfRadioTxAddrCfg( 0 ); 95 | NrfRadioRxAddrEnable( 0 ); 96 | 97 | // 配置Tx接收后(disabled),自动切换到Rx 98 | //NrfRadioAutoToRxEnable() 99 | 100 | // 白化初始值为数据所在BLE逻辑通道号(0-39),这里设置37,测试就在37上广播 101 | // 37广播通道,物理频率为2402 102 | NrfRadioDataWhiteIvCfg(37); 103 | NrfRadioFreqCfg(2402); 104 | 105 | u1 adv_data[] = {0x02,0x12,0xFF,0x01,0x02,0x03,0x05,0xff, 0x02,0x01,0x06, 0x08,0x09,0x66,0x65,0x6e,0x67,0x78,0x75,0x6e}; 106 | NrfRadioPacketPtrCfg( adv_data ); 107 | NrfRadioStartTx(); 108 | 109 | NrfRtc0Start(); 110 | 111 | for( ; ; ) { 112 | nrf_gpio_pin_toggle(LED_2); 113 | nrf_delay_ms(1000); 114 | //SEGGER_SYSVIEW_PrintfHost("toggle\n"); 115 | } 116 | 117 | return 0; 118 | } 119 | 120 | 121 | -------------------------------------------------------------------------------- /include/BleStack/BleLink/BleLinkConnect.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __BLELINKCONNECT_H__ 28 | #define __BLELINKCONNECT_H__ 29 | #include "../../DhGlobalHead.h" 30 | 31 | #define LINK_ENCRYPTED (1) 32 | #define LINK_NO_ENCRYPTED (0) 33 | 34 | typedef struct 35 | { 36 | u1 m_u1PeerBleAddrType; 37 | u1 m_pu1PeerBleAddr[BLE_ADDR_LEN]; /* 对端设备地址,LSB */ 38 | u2 m_u2ConnInterval; /* 1.25ms为单位 */ 39 | u2 m_u2SlaveLatency; 40 | u2 m_u2ConnTimeout; /* 10ms为单位 */ 41 | }BlkBleConnectedEvent; 42 | 43 | typedef struct 44 | { 45 | u2 m_u2ConnInterval; 46 | u2 m_u2SlaveLatency; 47 | u2 m_u2ConnTimeout; 48 | }BlkBleConnUpdateEvent; 49 | 50 | typedef struct 51 | { 52 | u1 m_u1ErrCode; 53 | }BlkBleDisconnectedEvent; 54 | 55 | typedef enum 56 | { 57 | BLE_DATA_PACKET_CONTINUATION = 0x01, // 高层报文延续包或空包 58 | BLE_DATA_PACKET_START, 59 | BLE_CONTROL_PACKET, 60 | }EnBlePacketType; 61 | 62 | typedef enum 63 | { 64 | CONN_IDLE = 0, 65 | CONN_CONNING_RX = 0x12, // 连接建立中,收到连接请求后等待主机发过来的第一个数据包的过程 66 | CONN_CONNECTED_TX = 0x21, // 连接后的发送数据状态 67 | CONN_CONNECTED_RX = 0x22, // 连接后的等待数据状态 68 | CONN_CONNECTED_RXTIMEOUT=0x30, // 接收超时 69 | CONN_CONNING_RXTIMEOUT = 0x40, 70 | }EnConnSubState; // 连接态的子状态 71 | 72 | #ifdef __cplusplus 73 | #if __cplusplus 74 | extern "C"{ 75 | #endif 76 | #endif /* __cplusplus */ 77 | 78 | extern void LinkConnReqHandle(u1 addrType, u1 *pu1Addr, u1* pu1LLData); 79 | extern void LinkConnStateInit(u1 sca); 80 | extern void LinkConnStateReset(void); 81 | extern void LinkConnSubStateSwitch(EnConnSubState enConnSubState); 82 | extern void LinkPeerAddrInfo(u1 *pu1Buff, u1 size); 83 | extern void LinkEncInfoCfg(u1 *pu1SK, u1 *pu1IV, u1 *pu1SKD); 84 | extern void LinkEncSkdGet(u1 *pu1SKD); 85 | extern void UpdateRecvPacketCounter(void); 86 | extern void UpdateSendPacketCounter(void); 87 | extern u1 GetRecvPacketCounter(void); 88 | extern u1 GetSendPacketCounter(void); 89 | 90 | extern u4 LinkEncKeyGet(u1 *pu1SK); 91 | extern u4 LinkDataDec(u1 LLID, u1 dir, u1 *pu1Key, u1 *pu1In, u2 InLen, u1 *pu1Out, u2 *outLen ); 92 | extern u4 LinkDataEnc(u1 LLID, u1 dir, u1 *pu1Key, u1 *pu1In, u2 InLen, u1 *pu1Out, u2 *outLen ); 93 | 94 | 95 | 96 | #ifdef __cplusplus 97 | #if __cplusplus 98 | } 99 | #endif 100 | #endif /* __cplusplus */ 101 | 102 | 103 | #endif /* __BLELINKCONNECT_H__ */ 104 | -------------------------------------------------------------------------------- /source/ChipDrv/NrfDrv/nrf51822/NrfUart.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../../../include/DhGlobalHead.h" 28 | 29 | u4 NrfUartBaudGet(EnUartBaudrate enBaud) 30 | { 31 | u4 baud; 32 | 33 | switch(enBaud) 34 | { 35 | case UART_BAUDRATE_115200: 36 | baud = 0x01D7E000; 37 | break; 38 | case UART_BAUDRATE_1200: 39 | baud = 0x0004F000; 40 | break; 41 | case UART_BAUDRATE_14400: 42 | baud = 0x003B0000; 43 | break; 44 | case UART_BAUDRATE_19200: 45 | baud = 0x004EA000; 46 | break; 47 | case UART_BAUDRATE_1M: 48 | baud = 0x10000000; 49 | break; 50 | case UART_BAUDRATE_230400: 51 | baud = 0x03AFB000; 52 | break; 53 | case UART_BAUDRATE_2400: 54 | baud = 0x0009D000; 55 | break; 56 | case UART_BAUDRATE_250000: 57 | baud = 0x04000000; 58 | break; 59 | case UART_BAUDRATE_28800: 60 | baud = 0x0075F000; 61 | break; 62 | case UART_BAUDRATE_38400: 63 | baud = 0x009D5000; 64 | break; 65 | case UART_BAUDRATE_460800: 66 | baud = 0x075F7000; 67 | break; 68 | case UART_BAUDRATE_4800: 69 | baud = 0x0013B000; 70 | break; 71 | case UART_BAUDRATE_57600: 72 | baud = 0x00EBF000; 73 | break; 74 | case UART_BAUDRATE_76800: 75 | baud = 0x013A9000; 76 | break; 77 | case UART_BAUDRATE_921600: 78 | baud = 0x0EBEDFA4; 79 | break; 80 | case UART_BAUDRATE_9600: 81 | baud = 0x00275000; 82 | break; 83 | default: 84 | baud = 0x00275000; 85 | break; 86 | } 87 | 88 | return baud; 89 | } 90 | 91 | void NrfUartSimpleInit(u1 rxPin, u1 txpin, u4 baud) 92 | { 93 | NRF_UART0->PSELTXD = txpin; 94 | NRF_UART0->PSELRXD = rxPin; 95 | NRF_UART0->PSELCTS = 0xFFFFFFFF; 96 | NRF_UART0->PSELRTS = 0xFFFFFFFF; 97 | NRF_UART0->CONFIG = 0x00; 98 | 99 | NRF_UART0->BAUDRATE = baud; 100 | NRF_UART0->ENABLE = 0x04; 101 | } 102 | 103 | void NrfUartSimpleTxByte(u1 byte) 104 | { 105 | u2 count = 20000; 106 | NRF_UART0->EVENTS_TXDRDY = 0; 107 | NRF_UART0->TXD = byte; 108 | NRF_UART0->TASKS_STARTTX = 1; 109 | while( 0==NRF_UART0->EVENTS_TXDRDY && (count--) ) 110 | { 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /source/ChipDrv/NrfDrv/nrf52840/NrfUart.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../../../include/DhGlobalHead.h" 28 | 29 | u4 NrfUartBaudGet(EnUartBaudrate enBaud) 30 | { 31 | u4 baud; 32 | 33 | switch(enBaud) 34 | { 35 | case UART_BAUDRATE_115200: 36 | baud = 0x01D7E000; 37 | break; 38 | case UART_BAUDRATE_1200: 39 | baud = 0x0004F000; 40 | break; 41 | case UART_BAUDRATE_14400: 42 | baud = 0x003B0000; 43 | break; 44 | case UART_BAUDRATE_19200: 45 | baud = 0x004EA000; 46 | break; 47 | case UART_BAUDRATE_1M: 48 | baud = 0x10000000; 49 | break; 50 | case UART_BAUDRATE_230400: 51 | baud = 0x03AFB000; 52 | break; 53 | case UART_BAUDRATE_2400: 54 | baud = 0x0009D000; 55 | break; 56 | case UART_BAUDRATE_250000: 57 | baud = 0x04000000; 58 | break; 59 | case UART_BAUDRATE_28800: 60 | baud = 0x0075F000; 61 | break; 62 | case UART_BAUDRATE_38400: 63 | baud = 0x009D5000; 64 | break; 65 | case UART_BAUDRATE_460800: 66 | baud = 0x075F7000; 67 | break; 68 | case UART_BAUDRATE_4800: 69 | baud = 0x0013B000; 70 | break; 71 | case UART_BAUDRATE_57600: 72 | baud = 0x00EBF000; 73 | break; 74 | case UART_BAUDRATE_76800: 75 | baud = 0x013A9000; 76 | break; 77 | case UART_BAUDRATE_921600: 78 | baud = 0x0EBEDFA4; 79 | break; 80 | case UART_BAUDRATE_9600: 81 | baud = 0x00275000; 82 | break; 83 | default: 84 | baud = 0x00275000; 85 | break; 86 | } 87 | 88 | return baud; 89 | } 90 | 91 | void NrfUartSimpleInit(u1 rxPin, u1 txpin, u4 baud) 92 | { 93 | NRF_UART0->PSELTXD = txpin; 94 | NRF_UART0->PSELRXD = rxPin; 95 | NRF_UART0->PSELCTS = 0xFFFFFFFF; 96 | NRF_UART0->PSELRTS = 0xFFFFFFFF; 97 | NRF_UART0->CONFIG = 0x00; 98 | 99 | NRF_UART0->BAUDRATE = baud; 100 | NRF_UART0->ENABLE = 0x04; 101 | } 102 | 103 | void NrfUartSimpleTxByte(u1 byte) 104 | { 105 | u2 count = 20000; 106 | NRF_UART0->EVENTS_TXDRDY = 0; 107 | NRF_UART0->TXD = byte; 108 | NRF_UART0->TASKS_STARTTX = 1; 109 | while( 0==NRF_UART0->EVENTS_TXDRDY && (count--) ) 110 | { 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /include/CMSIS/core_cmFunc.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file core_cmFunc.h 3 | * @brief CMSIS Cortex-M Core Function Access Header File 4 | * @version V4.30 5 | * @date 20. October 2015 6 | ******************************************************************************/ 7 | /* Copyright (c) 2009 - 2015 ARM LIMITED 8 | 9 | All rights reserved. 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | - Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | - Redistributions in binary form must reproduce the above copyright 15 | notice, this list of conditions and the following disclaimer in the 16 | documentation and/or other materials provided with the distribution. 17 | - Neither the name of ARM nor the names of its contributors may be used 18 | to endorse or promote products derived from this software without 19 | specific prior written permission. 20 | * 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | POSSIBILITY OF SUCH DAMAGE. 32 | ---------------------------------------------------------------------------*/ 33 | 34 | 35 | #if defined ( __ICCARM__ ) 36 | #pragma system_include /* treat file as system include file for MISRA check */ 37 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 38 | #pragma clang system_header /* treat file as system include file */ 39 | #endif 40 | 41 | #ifndef __CORE_CMFUNC_H 42 | #define __CORE_CMFUNC_H 43 | 44 | 45 | /* ########################### Core Function Access ########################### */ 46 | /** \ingroup CMSIS_Core_FunctionInterface 47 | \defgroup CMSIS_Core_RegAccFunctions CMSIS Core Register Access Functions 48 | @{ 49 | */ 50 | 51 | /*------------------ RealView Compiler -----------------*/ 52 | #if defined ( __CC_ARM ) 53 | #include "cmsis_armcc.h" 54 | 55 | /*------------------ ARM Compiler V6 -------------------*/ 56 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 57 | #include "cmsis_armcc_V6.h" 58 | 59 | /*------------------ GNU Compiler ----------------------*/ 60 | #elif defined ( __GNUC__ ) 61 | #include "cmsis_gcc.h" 62 | 63 | /*------------------ ICC Compiler ----------------------*/ 64 | #elif defined ( __ICCARM__ ) 65 | #include 66 | 67 | /*------------------ TI CCS Compiler -------------------*/ 68 | #elif defined ( __TMS470__ ) 69 | #include 70 | 71 | /*------------------ TASKING Compiler ------------------*/ 72 | #elif defined ( __TASKING__ ) 73 | /* 74 | * The CMSIS functions have been implemented as intrinsics in the compiler. 75 | * Please use "carm -?i" to get an up to date list of all intrinsics, 76 | * Including the CMSIS ones. 77 | */ 78 | 79 | /*------------------ COSMIC Compiler -------------------*/ 80 | #elif defined ( __CSMC__ ) 81 | #include 82 | 83 | #endif 84 | 85 | /*@} end of CMSIS_Core_RegAccFunctions */ 86 | 87 | #endif /* __CORE_CMFUNC_H */ 88 | -------------------------------------------------------------------------------- /include/CMSIS/core_cmInstr.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file core_cmInstr.h 3 | * @brief CMSIS Cortex-M Core Instruction Access Header File 4 | * @version V4.30 5 | * @date 20. October 2015 6 | ******************************************************************************/ 7 | /* Copyright (c) 2009 - 2015 ARM LIMITED 8 | 9 | All rights reserved. 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | - Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | - Redistributions in binary form must reproduce the above copyright 15 | notice, this list of conditions and the following disclaimer in the 16 | documentation and/or other materials provided with the distribution. 17 | - Neither the name of ARM nor the names of its contributors may be used 18 | to endorse or promote products derived from this software without 19 | specific prior written permission. 20 | * 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | POSSIBILITY OF SUCH DAMAGE. 32 | ---------------------------------------------------------------------------*/ 33 | 34 | 35 | #if defined ( __ICCARM__ ) 36 | #pragma system_include /* treat file as system include file for MISRA check */ 37 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 38 | #pragma clang system_header /* treat file as system include file */ 39 | #endif 40 | 41 | #ifndef __CORE_CMINSTR_H 42 | #define __CORE_CMINSTR_H 43 | 44 | 45 | /* ########################## Core Instruction Access ######################### */ 46 | /** \defgroup CMSIS_Core_InstructionInterface CMSIS Core Instruction Interface 47 | Access to dedicated instructions 48 | @{ 49 | */ 50 | 51 | /*------------------ RealView Compiler -----------------*/ 52 | #if defined ( __CC_ARM ) 53 | #include "cmsis_armcc.h" 54 | 55 | /*------------------ ARM Compiler V6 -------------------*/ 56 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 57 | #include "cmsis_armcc_V6.h" 58 | 59 | /*------------------ GNU Compiler ----------------------*/ 60 | #elif defined ( __GNUC__ ) 61 | #include "cmsis_gcc.h" 62 | 63 | /*------------------ ICC Compiler ----------------------*/ 64 | #elif defined ( __ICCARM__ ) 65 | #include 66 | 67 | /*------------------ TI CCS Compiler -------------------*/ 68 | #elif defined ( __TMS470__ ) 69 | #include 70 | 71 | /*------------------ TASKING Compiler ------------------*/ 72 | #elif defined ( __TASKING__ ) 73 | /* 74 | * The CMSIS functions have been implemented as intrinsics in the compiler. 75 | * Please use "carm -?i" to get an up to date list of all intrinsics, 76 | * Including the CMSIS ones. 77 | */ 78 | 79 | /*------------------ COSMIC Compiler -------------------*/ 80 | #elif defined ( __CSMC__ ) 81 | #include 82 | 83 | #endif 84 | 85 | /*@}*/ /* end of group CMSIS_Core_InstructionInterface */ 86 | 87 | #endif /* __CORE_CMINSTR_H */ 88 | -------------------------------------------------------------------------------- /include/CMSIS/core_cmSimd.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file core_cmSimd.h 3 | * @brief CMSIS Cortex-M SIMD Header File 4 | * @version V4.30 5 | * @date 20. October 2015 6 | ******************************************************************************/ 7 | /* Copyright (c) 2009 - 2015 ARM LIMITED 8 | 9 | All rights reserved. 10 | Redistribution and use in source and binary forms, with or without 11 | modification, are permitted provided that the following conditions are met: 12 | - Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | - Redistributions in binary form must reproduce the above copyright 15 | notice, this list of conditions and the following disclaimer in the 16 | documentation and/or other materials provided with the distribution. 17 | - Neither the name of ARM nor the names of its contributors may be used 18 | to endorse or promote products derived from this software without 19 | specific prior written permission. 20 | * 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 | POSSIBILITY OF SUCH DAMAGE. 32 | ---------------------------------------------------------------------------*/ 33 | 34 | 35 | #if defined ( __ICCARM__ ) 36 | #pragma system_include /* treat file as system include file for MISRA check */ 37 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 38 | #pragma clang system_header /* treat file as system include file */ 39 | #endif 40 | 41 | #ifndef __CORE_CMSIMD_H 42 | #define __CORE_CMSIMD_H 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | 49 | /* ################### Compiler specific Intrinsics ########################### */ 50 | /** \defgroup CMSIS_SIMD_intrinsics CMSIS SIMD Intrinsics 51 | Access to dedicated SIMD instructions 52 | @{ 53 | */ 54 | 55 | /*------------------ RealView Compiler -----------------*/ 56 | #if defined ( __CC_ARM ) 57 | #include "cmsis_armcc.h" 58 | 59 | /*------------------ ARM Compiler V6 -------------------*/ 60 | #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) 61 | #include "cmsis_armcc_V6.h" 62 | 63 | /*------------------ GNU Compiler ----------------------*/ 64 | #elif defined ( __GNUC__ ) 65 | #include "cmsis_gcc.h" 66 | 67 | /*------------------ ICC Compiler ----------------------*/ 68 | #elif defined ( __ICCARM__ ) 69 | #include 70 | 71 | /*------------------ TI CCS Compiler -------------------*/ 72 | #elif defined ( __TMS470__ ) 73 | #include 74 | 75 | /*------------------ TASKING Compiler ------------------*/ 76 | #elif defined ( __TASKING__ ) 77 | /* 78 | * The CMSIS functions have been implemented as intrinsics in the compiler. 79 | * Please use "carm -?i" to get an up to date list of all intrinsics, 80 | * Including the CMSIS ones. 81 | */ 82 | 83 | /*------------------ COSMIC Compiler -------------------*/ 84 | #elif defined ( __CSMC__ ) 85 | #include 86 | 87 | #endif 88 | 89 | /*@} end of group CMSIS_SIMD_intrinsics */ 90 | 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #endif /* __CORE_CMSIMD_H */ 97 | -------------------------------------------------------------------------------- /include/BleStack/BleGatt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __BLEGATT_H__ 28 | #define __BLEGATT_H__ 29 | #include "../DhGlobalHead.h" 30 | 31 | 32 | #define BLE_PRIMARY_SERVICE_UUID 0x2800 33 | #define BLE_INCLUDE_UUID 0x2802 34 | #define BLE_CHARACTERISTIC_DECLARATION_UUID 0x2803 35 | #define BLE_CCCD_UUID 0x2902 36 | #define BLE_GENERIC_ACCESS_SERVICE_UUID 0x1800 37 | #define BLE_GENERIC_ATTRIBUTE_SERVICE_UUID 0x1801 38 | #define BLE_DEVICE_NAME_UUID 0x2A00 39 | #define BLE_APPEARANCE_UUID 0x2A01 40 | #define BLE_PERIPHERAL_PRIVACY_FLAG_UUID 0x2A02 41 | #define BLE_PPCP_UUID 0x2A04 42 | #define BLE_RECONNECTION_ADDRESS_UUID 0x2A03 43 | #define BLE_SERVICE_CHANGED_UUID 0x2A05 44 | 45 | /* 返回特性句柄相关的信息,包括特性值的句柄,描述符的句柄 */ 46 | typedef struct 47 | { 48 | u2 m_u2ValueHandle; /* 特性值句柄 */ 49 | u2 m_u2CccdHandle; /* Client Characteristic Configuration 句柄 */ 50 | }BlkBleAttHandleInfo; 51 | 52 | typedef struct 53 | { 54 | u2 m_u2AttHandle; 55 | u2 m_u2WriteLen; 56 | u1 m_pu1AttValue[BLE_ATT_MTU_MAX_SIZE]; 57 | }BlkBleRecvWriteEvent; 58 | 59 | 60 | /* 特征值具有的特性 */ 61 | typedef struct 62 | { 63 | u1 m_u1BroadcastEnable:1; 64 | u1 m_u1ReadEnable:1; 65 | u1 m_u1WriteCmdEnable:1; 66 | u1 m_u1WriteReqEnable:1; 67 | u1 m_u1NotifyEnable:1; 68 | u1 m_u1IndicateEnable:1; 69 | u1 m_u1SignedWriteEnable:1; 70 | u1 m_u1ExtendedProps:1; 71 | }BlkCharProperties; 72 | 73 | /* 特性配置,当前只支持CCCD描述符 */ 74 | typedef struct 75 | { 76 | BlkAttributeType m_blkUuid; // 特性值的UUID 77 | BlkCharProperties m_BlkCharProps; // 是否支持读写等特性 78 | u2 m_u2ValuePermission; // 特性值的许可条件,若未设置默认配置成可读写 79 | u2 m_u2CCCDPermission; // 客户端特性配置描述符的许可条件,若未设置默认配置为可读写 80 | }BlkGattCharCfg; 81 | 82 | #ifdef __cplusplus 83 | #if __cplusplus 84 | extern "C"{ 85 | #endif 86 | #endif /* __cplusplus */ 87 | 88 | extern u4 BleGattFindAttByHandle(u2 u2Handle, BlkBleAttribute **ppblkAtt); 89 | extern u4 BleGattFindAttByType(u2 u2StartHandle, u2 u2EndHandle, u1 *pu1UUID, u1 UUIDType, BlkBleAttribute **ppblkAtt); 90 | extern u4 BleGattInfoInit(void); 91 | extern u4 BleGattServiceDeclAdd(u1 *pu1ServiceUuid, u1 uuidType); 92 | extern u4 BleGattCharacteristicAdd( BlkGattCharCfg charaCfg, u1 *pu1CharValueBuff, u2 u2BuffSize, BlkBleAttHandleInfo *pu2ValueHandle ); 93 | extern u2 BleDeviceNameAttHandleGet(void); 94 | extern u4 BleGattSendNotify(u2 u2AttHandle, u1 *pu1AttValue, u2 len); 95 | extern u4 BleGattSendIndication(u2 u2AttHandle, u1 *pu1AttValue, u2 len); 96 | 97 | 98 | #ifdef __cplusplus 99 | #if __cplusplus 100 | } 101 | #endif 102 | #endif /* __cplusplus */ 103 | 104 | 105 | #endif /* __BLEGATT_H__ */ 106 | -------------------------------------------------------------------------------- /include/CMSIS/arm_const_structs.h: -------------------------------------------------------------------------------- 1 | /* ---------------------------------------------------------------------- 2 | * Copyright (C) 2010-2014 ARM Limited. All rights reserved. 3 | * 4 | * $Date: 19. March 2015 5 | * $Revision: V.1.4.5 6 | * 7 | * Project: CMSIS DSP Library 8 | * Title: arm_const_structs.h 9 | * 10 | * Description: This file has constant structs that are initialized for 11 | * user convenience. For example, some can be given as 12 | * arguments to the arm_cfft_f32() function. 13 | * 14 | * Target Processor: Cortex-M4/Cortex-M3 15 | * 16 | * Redistribution and use in source and binary forms, with or without 17 | * modification, are permitted provided that the following conditions 18 | * are met: 19 | * - Redistributions of source code must retain the above copyright 20 | * notice, this list of conditions and the following disclaimer. 21 | * - Redistributions in binary form must reproduce the above copyright 22 | * notice, this list of conditions and the following disclaimer in 23 | * the documentation and/or other materials provided with the 24 | * distribution. 25 | * - Neither the name of ARM LIMITED nor the names of its contributors 26 | * may be used to endorse or promote products derived from this 27 | * software without specific prior written permission. 28 | * 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 32 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 33 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 34 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 35 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 36 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 37 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 39 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 40 | * POSSIBILITY OF SUCH DAMAGE. 41 | * -------------------------------------------------------------------- */ 42 | 43 | #ifndef _ARM_CONST_STRUCTS_H 44 | #define _ARM_CONST_STRUCTS_H 45 | 46 | #include "arm_math.h" 47 | #include "arm_common_tables.h" 48 | 49 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len16; 50 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len32; 51 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len64; 52 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len128; 53 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len256; 54 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len512; 55 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len1024; 56 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len2048; 57 | extern const arm_cfft_instance_f32 arm_cfft_sR_f32_len4096; 58 | 59 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len16; 60 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len32; 61 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len64; 62 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len128; 63 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len256; 64 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len512; 65 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len1024; 66 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len2048; 67 | extern const arm_cfft_instance_q31 arm_cfft_sR_q31_len4096; 68 | 69 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len16; 70 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len32; 71 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len64; 72 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len128; 73 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len256; 74 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len512; 75 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len1024; 76 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len2048; 77 | extern const arm_cfft_instance_q15 arm_cfft_sR_q15_len4096; 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /include/DhError.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | #ifndef __DH_ERROR_H__ 27 | #define __DH_ERROR_H__ 28 | #define DH_SUCCESS (0x00) 29 | 30 | #define ERR_LINK_BASE (0xFF000000) 31 | #define ERR_LINK_INVALID_PARAMS (ERR_LINK_BASE+0x00000001) 32 | #define ERR_LINK_INVALID_LEN (ERR_LINK_BASE+0x00000003) 33 | #define ERR_LINK_START_ERROR (ERR_LINK_BASE+0x00000004) 34 | #define ERR_LINK_NOT_CHANNEL_MAP_REQ (ERR_LINK_BASE+0x00000005) 35 | #define ERR_LINK_NOT_CONN_UPDATE_REQ (ERR_LINK_BASE+0x00000006) 36 | #define ERR_LINK_STATE_INVALID (ERR_LINK_BASE+0x00000007) 37 | #define ERR_LINK_MIC_FAILED (ERR_LINK_BASE+0x00000008) 38 | 39 | #define ERR_HA_TIMER_BASE (0xFE000000) 40 | #define ERR_LP_TIMER_BASE (0xFD000000) 41 | 42 | #define ERR_DH_QUEUE_BASE (0xFC000000) 43 | #define ERR_DH_QUEUE_SIZE_INVALID (ERR_DH_QUEUE_BASE + 0x00000001) 44 | #define ERR_DH_QUEUE_FULL (ERR_DH_QUEUE_BASE + 0x00000002) 45 | #define ERR_DH_QEUEUE_EMPTY (ERR_DH_QUEUE_BASE + 0x00000003) 46 | 47 | #define ERR_L2CAP_BASE (0xFB000000) 48 | #define ERR_L2CAP_INVALID_PARAM (ERR_L2CAP_BASE+0x00000001) 49 | #define ERR_L2CAP_INVALID_CHANNEL (ERR_L2CAP_BASE+0x00000002) 50 | #define ERR_L2CAP_INVALID_LENGTH (ERR_L2CAP_BASE+0x00000003) 51 | #define ERR_L2CAP_INSUFFICIENT_RESOURCE (ERR_L2CAP_BASE+0x00000004) 52 | 53 | #define ERR_GATT_BASE (0xFA000000) 54 | #define ERR_GATT_INVALID_HANDLE (ERR_GATT_BASE+0x00000001) 55 | #define ERR_GATT_NOT_FIND_ATT (ERR_GATT_BASE+0x00000002) 56 | #define ERR_GATT_RESOURCE_INSUFFICCIENT (ERR_GATT_BASE+0x00000003) 57 | #define ERR_GATT_INVALID_PARAMS (ERR_GATT_BASE+0x00000004) 58 | #define ERR_GATT_NOT_FIND_CCCD (ERR_GATT_BASE+0x00000005) 59 | #define ERR_GATT_ATT_STATE_INVALID (ERR_GATT_BASE+0x00000006) 60 | #define ERR_GATT_VALUE_LEN (ERR_GATT_BASE+0x00000007) 61 | 62 | #define ERR_ATT_BASE (0xF9000000) 63 | #define ERR_ATT_SEND_RSP_FAILED (ERR_ATT_BASE+0x00000001) 64 | #define ERR_ATT_INVALID_PARAMS (ERR_ATT_BASE+0x00000002) 65 | #define ERR_ATT_NOT_FIND (ERR_ATT_BASE+0x00000003) 66 | 67 | #define ERR_DH_MEMORY_BASE (0xF8000000) 68 | #define ERR_DH_MEMORY_PARAMS_IINVALID (ERR_DH_MEMORY_BASE+0x00000001) 69 | #define ERR_DH_MEMORY_INSUFFICIENT (ERR_DH_MEMORY_BASE+0x00000002) 70 | 71 | #define ERR_GAP_BASE (0xF7000000) 72 | #define ERR_GAP_ADV_INVALID_PARAMS (ERR_GAP_BASE+0x00000001) 73 | #define ERR_GAP_INTERNAL (ERR_GAP_BASE+0x00000002) 74 | #define ERR_GAP_INVALID_PARAMS (ERR_GAP_BASE+0x00000003) 75 | #define ERR_GAP_BUFF_LEN (ERR_GAP_BASE+0x00000004) 76 | 77 | #define ERR_SM_BASE (0xF6000000) 78 | #define ERR_SM_INVALID_PARAMS (ERR_SM_BASE+0x00000001) 79 | 80 | #define ERR_COMM_BASE (0xF5000000) 81 | #define ERR_COMM_INVALID_PARAMS (ERR_COMM_BASE+0x00000001) 82 | #endif 83 | -------------------------------------------------------------------------------- /source/BleStack/BleAdvertising.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | #include "../../include/DhGlobalHead.h" 27 | 28 | #define BLE_GAP_ADV_FLAG_LEN (0x03) // L(0x02) T(0x01) V(flag) 29 | 30 | #define BLE_ADVDATA_FLAG (0x01) 31 | 32 | 33 | 34 | /** 35 | *@brief: BleAdvDataCfg 36 | *@details: 设置广播数据,广播名字不需要设置,通过BleGapDeviceNameCfg函数配置 37 | *@param[in] pu1Data L T V 格式的数据 38 | len 数据长度 39 | 40 | *@retval: DH_SUCCESS 41 | */ 42 | u4 BleAdvDataCfg(u1 *pu1Data, u2 len) 43 | { 44 | u1 tmp[BLE_PDU_LENGTH-BLE_PDU_HEADER_LENGTH]; 45 | char pu1DeviceName[BLE_DEV_NAME_MAX_SIZE]; 46 | u1 nameLen = BLE_DEV_NAME_MAX_SIZE; 47 | u2 index = 0; 48 | BlkBleAddrInfo addr; 49 | 50 | BleGapDeviceNameGet(pu1DeviceName, &nameLen); 51 | if( len>(BLE_PDU_LENGTH-BLE_PDU_HEADER_LENGTH-BLE_ADDR_LEN-BLE_GAP_ADV_FLAG_LEN-nameLen-2) ) // 2为 L T占用的字节 52 | { 53 | return ERR_GAP_ADV_INVALID_PARAMS; 54 | } 55 | addr = LinkAdvAddrInfoGet(); 56 | memcpy(tmp+index, addr.m_pu1Addr, BLE_ADDR_LEN); 57 | index += BLE_ADDR_LEN; 58 | 59 | tmp[index++] = 0x02; 60 | tmp[index++] = BLE_ADVDATA_FLAG; 61 | tmp[index++] = 0x06; // General Discoverable Mode & BR/EDR Not Supported 62 | 63 | tmp[index++] = nameLen+1; 64 | tmp[index++] = 0x09; 65 | memcpy(tmp+index, pu1DeviceName, nameLen); 66 | index += nameLen; 67 | 68 | if( NULL != pu1Data ) 69 | { 70 | memcpy(tmp+index, pu1Data, len); 71 | index += len; 72 | } 73 | LinkAdvDataCfg(tmp, BLE_PDU_HEADER_LENGTH, index); 74 | 75 | return DH_SUCCESS; 76 | } 77 | 78 | u4 BleScanRspDataCfg(u1 *pu1Data, u2 len) 79 | { 80 | u1 tmp[BLE_PDU_LENGTH-BLE_PDU_HEADER_LENGTH]; 81 | u2 index = 0; 82 | BlkBleAddrInfo addr; 83 | 84 | if( len>(BLE_PDU_LENGTH-BLE_PDU_HEADER_LENGTH-BLE_ADDR_LEN) ) 85 | { 86 | return ERR_GAP_ADV_INVALID_PARAMS; 87 | } 88 | addr = LinkAdvAddrInfoGet(); 89 | memcpy(tmp+index, addr.m_pu1Addr, BLE_ADDR_LEN); 90 | index += BLE_ADDR_LEN; 91 | if( NULL != pu1Data ) 92 | { 93 | memcpy(tmp+index, pu1Data, len); 94 | index += len; 95 | } 96 | LinkScanRspCfg(tmp, BLE_PDU_HEADER_LENGTH, index); 97 | 98 | return DH_SUCCESS; 99 | } 100 | 101 | u4 BleAdvStart(BlkAdvChannelOn channels, u2 IntervalMs) 102 | { 103 | u2 len; 104 | u1 pu1Header[BLE_PDU_HEADER_LENGTH]; 105 | BlkBleAddrInfo addr; 106 | 107 | LinkAdvParamsCfg(channels, IntervalMs); 108 | addr = LinkAdvAddrInfoGet(); 109 | len = LinkAdvDataLenGet(); 110 | pu1Header[0] = (addr.m_u1AddrType<<6) | PDU_TYPE_ADV; 111 | pu1Header[1] = len&0x3F; 112 | LinkAdvDataCfg(pu1Header, 0, BLE_PDU_HEADER_LENGTH); 113 | 114 | len = LinkScanRspLenGet(); 115 | pu1Header[0] = addr.m_u1AddrType<<6 | PDU_TYPE_SCAN_RSP; 116 | pu1Header[1] = len&0x3F; 117 | LinkScanRspCfg(pu1Header, 0, BLE_PDU_HEADER_LENGTH); 118 | 119 | return LinkAdvStart(); 120 | } 121 | 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /source/Common/DhQueue.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../include/DhGlobalHead.h" 28 | /** 29 | *@brief: DhQueueInit 30 | *@details: 队列初始化 31 | *@param[in] queue 32 | *@param[in] size 队列大小 33 | *@param[in] elemBuff 实际存放队列元素的buff 34 | 35 | *@retval: void 36 | */ 37 | u4 QueueInit( BlkDhQueue * queue, u2 size, void *elemBuff ) 38 | { 39 | if( size!=0 && (size&(size-1))==0 ) 40 | { 41 | return ERR_DH_QUEUE_SIZE_INVALID; 42 | } 43 | queue->m_pValue = elemBuff; 44 | queue->m_u2IRead = 0; 45 | queue->m_u2IWrite = 0; 46 | queue->m_u2QueueSize = size; 47 | queue->m_u2ElemCount = 0; 48 | 49 | return DH_SUCCESS; 50 | } 51 | 52 | 53 | /** 54 | *@brief: IsQueueEmpty 55 | *@details: 队列是否为空 56 | *@param[in] queue 57 | *@retval: 1-为空 0-非空 58 | */ 59 | u1 IsQueueEmpty( BlkDhQueue *queue ) 60 | { 61 | if ( 0 == queue->m_u2ElemCount ) 62 | { 63 | return 1; 64 | } 65 | 66 | return 0; 67 | } 68 | 69 | u1 IsQueueFull( BlkDhQueue *queue ) 70 | { 71 | if ( queue->m_u2ElemCount == queue->m_u2QueueSize ) 72 | { 73 | return 1; 74 | } 75 | 76 | return 0; 77 | } 78 | 79 | /** 80 | *@brief: QueueEmptyElemGet 81 | *@details: 获取队列中的一个无效内容元素,队里现有元素加1,赋值在外部进行。 82 | 等于将入队操作的赋值操作提取到外部进行,先入队实际的队列元素赋值在外部 83 | *@param[in] queue 84 | *@param[in] u4ElemSize 队列中的元素大小 85 | 86 | *@retval: pvalue 内容为空的元素指针 87 | */ 88 | void *QueueEmptyElemGet( BlkDhQueue *queue ,u4 u4ElemSize) 89 | { 90 | void *pValue; 91 | if( IsQueueFull(queue) ) 92 | { 93 | /* 队列已满,没有空元素了 */ 94 | return NULL; 95 | } 96 | 97 | /* 98 | 判断队列满不放在临界区里了,就算放到临界区buff真放满了,再有新数据也是丢弃或者覆盖队头数据 99 | 这里放在临界区外面出现冲突的时候也就是覆盖掉队列头的数据而已。 100 | */ 101 | CRITICAL_REGION_ENTER(); 102 | 103 | pValue = (void *)((u4)(queue->m_pValue)+u4ElemSize*(queue->m_u2IWrite)); 104 | queue->m_u2IWrite = (queue->m_u2IWrite+1)&(queue->m_u2QueueSize-1); // 先做入队操作了 105 | queue->m_u2ElemCount++; 106 | 107 | CRITICAL_REGION_EXIT(); 108 | 109 | return pValue; 110 | } 111 | 112 | /** 113 | *@brief: QueueValidElemGet 114 | *@details: 获取队列头元素,没有出队.该函数之后需要调用QueuePop执行出队操作。 115 | 将队列元素的提取和出队操作分离是因为这里提取获取到的只是元素的指针,外部还需要获取元素的实际内容 116 | 如果这里就直接出队,如果这个时候有很多入队操作,会导致覆盖点当前刚出队的这个元素的内容 117 | 提取数据和出队操纵分开,可能出现多个地方出队操纵提取的是同一个数据。 118 | 不过现在队列是协议栈内部使用的。对同一个队列的出队操纵都是在同一个地方,不会出现多线程的问题。 119 | *@param[in] queue 120 | *@param[in] u4ElemSize 队列中的元素大小 121 | 122 | *@retval: 队列头元素 123 | */ 124 | void *QueueValidElemGet(BlkDhQueue *queue, u4 u4ElemSize) 125 | { 126 | void *pValue; 127 | 128 | if( IsQueueEmpty(queue) ) 129 | { 130 | return NULL; 131 | } 132 | pValue = (void *)((u4)(queue->m_pValue)+u4ElemSize*(queue->m_u2IRead)); 133 | 134 | return pValue; 135 | } 136 | 137 | /** 138 | *@brief: QueuePop 139 | *@details: 出队操作,剔除队列头元素 140 | *@param[in] queue 141 | 142 | *@retval: void 143 | */ 144 | void QueuePop(BlkDhQueue *queue) 145 | { 146 | CRITICAL_REGION_ENTER(); 147 | queue->m_u2IRead = (queue->m_u2IRead+1)&(queue->m_u2QueueSize-1); // 出队操作 148 | queue->m_u2ElemCount--; 149 | CRITICAL_REGION_EXIT(); 150 | 151 | } 152 | 153 | -------------------------------------------------------------------------------- /source/BleDrv/BleHAccuracyTimer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | 28 | #include "../../include/DhGlobalHead.h" 29 | 30 | /* 31 | * @brief 高精度时钟内部维护信息 32 | */ 33 | typedef struct 34 | { 35 | BleHAccuracyTimeroutHandler m_cb; /* 回调函数 */ 36 | void *m_pValue; /* 传给回调函数的参数 */ 37 | } BlkHATimerCb; 38 | 39 | typedef struct 40 | { 41 | BlkHATimerCb m_pblkHATimer[BLE_HA_MAX]; 42 | } BlkHATimerManager; 43 | 44 | BlkHATimerManager s_blkBleHATimer; 45 | 46 | /** 47 | *@brief: Timer0Evt2HATimerId 48 | *@details: 由timer0的比较事件获取到是哪个高精度timer产生了超时 49 | *@param[in] evtId 50 | *@retval: timerId 51 | */ 52 | static EnBleHAccuracyTimer Evt2HATimerId( EnNrfTimer0Evt evtId ) 53 | { 54 | if( NRF_TIMER0_EVT_COMPARE0 == evtId ) 55 | { 56 | return BLE_HA_TIMER0; 57 | } 58 | 59 | return BLE_HA_TIMER_INVALID; 60 | } 61 | 62 | /** 63 | *@brief: Timerid2CmpReg 64 | *@details: 由timerid获取对应的比较寄存器 65 | *@param[in] timerId 66 | *@retval: CmpReg 67 | */ 68 | static EnNrfTimerCmpReg Timerid2CmpReg(EnBleHAccuracyTimer timerId) 69 | { 70 | if( BLE_HA_TIMER0 == timerId ) 71 | { 72 | return NRF_TIMER_CMPREG_0; 73 | } 74 | 75 | return NRF_TIMER_CMPREG_INVALID; 76 | } 77 | 78 | /** 79 | *@brief: TImer0IntCb 80 | *@details: 注册的Timer0的中断回调函数,中断发生后,timer0中断处理调- 81 | 用该回调函数,参数为中断事件 82 | *@param[in] evt 83 | *@retval: void 84 | */ 85 | static void Timer0IntCb( EnNrfTimer0Evt evt ) 86 | { 87 | BleHAccuracyTimeroutHandler cb = NULL; 88 | void *pvalue; 89 | u1 timerId; 90 | 91 | timerId = Evt2HATimerId(evt); 92 | 93 | if ( BLE_HA_TIMER_INVALID != timerId ) 94 | { 95 | cb = s_blkBleHATimer.m_pblkHATimer[timerId].m_cb; 96 | pvalue = s_blkBleHATimer.m_pblkHATimer[timerId].m_pValue; 97 | } 98 | if ( NULL != cb ) 99 | { 100 | cb(pvalue); 101 | } 102 | 103 | } 104 | 105 | 106 | /** 107 | *@brief: BleHAccuracyTimerInit 108 | *@details: 初始化BLE使用的低功耗时钟 109 | *@param[in] void 110 | *@retval: void 111 | */ 112 | void BleHAccuracyTimerInit( void ) 113 | { 114 | memset( &s_blkBleHATimer, 0x00, sizeof( s_blkBleHATimer ) ); 115 | NrfTimer0HandlerRegister( Timer0IntCb ); 116 | NrfTimer0Init(); 117 | } 118 | 119 | 120 | 121 | /** 122 | *@brief: BleHAccuracyTimerStart 123 | *@details: 启动ble的高精度定时器 124 | *@param[in] timerId 需要启动的ble低功耗时钟id,目前就一个 BLE_HA_TIMER0 125 | *@param[in] timeoutUs 定时器超时时间 126 | *@param[in] cb 定时器超时后的回调函数 127 | *@param[in] pvalue 回调函数触发时会获取到该值,该值为传递的是地址,所以需要保证运行期间传递的变量的有效性。 128 | 129 | *@retval: void 130 | */ 131 | void BleHAccuracyTimerStart( EnBleHAccuracyTimer timerId, u4 timeoutUs, BleHAccuracyTimeroutHandler cb, void *pvalue) 132 | { 133 | EnNrfTimerCmpReg reg; 134 | 135 | s_blkBleHATimer.m_pblkHATimer[timerId].m_cb = cb; 136 | s_blkBleHATimer.m_pblkHATimer[timerId].m_pValue = pvalue; 137 | 138 | NrfTimer0Stop(); 139 | NrfTimer0Clear(); 140 | reg = Timerid2CmpReg(timerId); 141 | NrfTimer0SetCmpReg(reg, timeoutUs); 142 | NrfTimer0Start(); 143 | } 144 | 145 | 146 | void BleHAccuracyTimerStop(EnBleHAccuracyTimer timerId) 147 | { 148 | /* 目前高频定时器协议栈只需要用到一个就可以了,所以直接关timer0定时器就行了 */ 149 | NrfTimer0Stop(); 150 | } 151 | -------------------------------------------------------------------------------- /source/ChipDrv/NrfDrv/nrf51822/NrfClockDrv.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../../../include/DhGlobalHead.h" 28 | 29 | #define nDEBUG_CLOCK_ENABLE 30 | 31 | #if !defined(DEBUG_CLOCK_ENABLE) 32 | #undef DEBUG_INFO 33 | #define DEBUG_INFO(...) 34 | #endif 35 | 36 | #define XTALFREQ_16MHZ (0xff) 37 | #define TASK_START (0x01) 38 | #define LFCLK_STAT_POS (16) 39 | #define HFCLK_SRC_POS (0) 40 | #define HFCLK_SRC_XTAL (1) 41 | #define HFCLK_SRC_RC (0) 42 | 43 | #define INTENSET_CTTO (1<<4) 44 | #define INTENSET_DONE (1<<3) 45 | 46 | #define CAL_TIME_UNIT_MS (250) 47 | 48 | static u4 g_CalTimeout = 0; 49 | 50 | 51 | static u1 IsHFCLKSrcXtal(void) 52 | { 53 | if( NRF_CLOCK->HFCLKSTAT&(1<INTENSET = INTENSET_CTTO | INTENSET_DONE; 76 | 77 | NRF_CLOCK->CTIV = timeoutMs/CAL_TIME_UNIT_MS; 78 | 79 | // 我们使用了LFCLK校准,相对而言不那么重要,硬件中断优先级设为3(比radio低)。 80 | NVIC_SetPriority(POWER_CLOCK_IRQn, 3); 81 | NVIC_ClearPendingIRQ(POWER_CLOCK_IRQn); 82 | NVIC_EnableIRQ(POWER_CLOCK_IRQn); 83 | 84 | NRF_CLOCK->TASKS_CTSTART = TASK_START; 85 | 86 | g_CalTimeout = timeoutMs; 87 | } 88 | 89 | /** 90 | *@brief: NrfLFClkStart 91 | *@details: 启动低频时钟 92 | *@param[in] src 低频时钟源 93 | *@retval: void 94 | */ 95 | void NrfLFClkStart( EnNrfLFClockSrc src , u4 u4CalTimeoutMs, u1 enableCal) 96 | { 97 | NRF_CLOCK->LFCLKSRC = src; 98 | NRF_CLOCK->TASKS_LFCLKSTART = TASK_START; 99 | while( !(NRF_CLOCK->LFCLKSTAT&(1<XTALFREQ = XTALFREQ_16MHZ; 116 | NRF_CLOCK->EVENTS_HFCLKSTARTED = 0; 117 | NRF_CLOCK->TASKS_HFCLKSTART = TASK_START; 118 | // 等待启振完成 119 | while(1) 120 | { 121 | if(1 == NRF_CLOCK->EVENTS_HFCLKSTARTED) 122 | { 123 | break; 124 | } 125 | } 126 | } 127 | 128 | 129 | void POWER_CLOCK_IRQHandler(void) 130 | { 131 | if( NRF_CLOCK->EVENTS_CTTO ) 132 | { 133 | NRF_CLOCK->EVENTS_CTTO = 0; 134 | if( HFCLK_SRC_XTAL == IsHFCLKSrcXtal() ) 135 | { 136 | /* 使用了外部高频晶振,高频时钟才是比较精确的,此时才能校准才有意义 */ 137 | NRF_CLOCK->TASKS_CAL = TASK_START; 138 | DEBUG_INFO("cal...."); 139 | } 140 | else 141 | { 142 | /* 重启校准定时器 */ 143 | NRF_CLOCK->CTIV = g_CalTimeout/CAL_TIME_UNIT_MS; 144 | NRF_CLOCK->TASKS_CTSTART = TASK_START; 145 | } 146 | } 147 | if( NRF_CLOCK->EVENTS_DONE ) 148 | { 149 | NRF_CLOCK->EVENTS_DONE = 0; 150 | /* 重启校准定时器,实现周期校准 */ 151 | NRF_CLOCK->CTIV = g_CalTimeout/CAL_TIME_UNIT_MS; 152 | NRF_CLOCK->TASKS_CTSTART = TASK_START; 153 | DEBUG_INFO("cal done"); 154 | DEBUG_INFO("start cal time"); 155 | } 156 | } -------------------------------------------------------------------------------- /include/DhBleDefine.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __BLECONFIG_H__ 28 | #define __BLECONFIG_H__ 29 | 30 | 31 | #define DH_BLE_VERSION 0x0001 //2018-2-9 0.01版本 32 | 33 | #define BLE_VERSION 0x40 34 | 35 | #if (BLE_VERSION == 0x40) 36 | #define BLE_PDU_LENGTH (39) /* 协议数据单元长度为39字节,2字节header+37字节payload*/ 37 | #define BLE_PDU_HEADER_LENGTH (2) 38 | #define BLE_PREAMBLE_LENGTH (1) 39 | #define BLE_ACC_ADDR_LENGTH (4) 40 | #define BLE_CRC_LENGTH (3) 41 | #define BLE_PACKET_LENGTH (BLE_PREAMBLE_LENGTH+BLE_ACC_ADDR_LENGTH+BLE_PDU_LENGTH+BLE_CRC_LENGTH) 42 | #define BLE_VERSION_NUMBER (0x06) /* 6-4.0 7-4.1 8-4.2 9-5.0*/ 43 | 44 | #define BLE_ENC_IV_SIZE (8) 45 | #define BLE_ENC_SKD_SIZE (16) 46 | #define BLE_ENC_PACKETCOUNTER_SIZE (5) 47 | #define BLE_ENC_KEY_SIZE (16) 48 | 49 | #define BLE_ATT_MTU_DEFALUT_SIZE (23) 50 | #endif 51 | 52 | /* 通道使用表为5字节 */ 53 | #define BLE_CHANNEL_MAP_LEN (5) 54 | 55 | /* 目前只支持默认包,不支持协议自动分包 */ 56 | #define BLE_ATT_MTU_SIZE BLE_ATT_MTU_DEFALUT_SIZE 57 | 58 | /* 当前资源下允许的最大ATT MTU */ 59 | #define BLE_ATT_MTU_MAX_SIZE BLE_ATT_MTU_DEFALUT_SIZE 60 | 61 | 62 | /* 按一个广播包来看 37,去掉6字节地址,去掉3字节广播flag, 去掉 L T 2字节还剩可以放名字26字节 */ 63 | #define BLE_DEV_NAME_MAX_SIZE (26) 64 | 65 | /* 广播的接入地址是固定的*/ 66 | #define BLE_ADV_ACCESS_ADDR (0x8E89BED6) 67 | 68 | /* BLE CRC多项式为x24 + x10 + x9 + x6 + x4 + x3 + x + 1 */ 69 | #define BLE_CRC_POLY (0x100065B) 70 | 71 | /* BLE广播通道下CRC初始值 */ 72 | #define BLE_ADV_CHANNEL_CRC_INIT (0x555555) 73 | 74 | /* BLE帧间间隔为150us */ 75 | #define BLE_TIFS_VALUE (150) 76 | 77 | /* BLE地址长度 */ 78 | #define BLE_ADDR_LEN (6) 79 | 80 | #define BLE_ADV_CHANNEL_NUMBER (3) 81 | #define BLE_ADV_CHANNEL_37 (37) 82 | #define BLE_ADV_CHANNEL_38 (38) 83 | #define BLE_ADV_CHANNEL_39 (39) 84 | 85 | #define BLE_DATA_CHANNEL_NUMBER (37) 86 | #define BLE_DATA_CHANNEL_0 (0) 87 | #define BLE_DATA_CHANNEL_1 (1) 88 | #define BLE_DATA_CHANNEL_2 (2) 89 | #define BLE_DATA_CHANNEL_3 (3) 90 | #define BLE_DATA_CHANNEL_4 (4) 91 | #define BLE_DATA_CHANNEL_5 (5) 92 | #define BLE_DATA_CHANNEL_6 (6) 93 | #define BLE_DATA_CHANNEL_7 (7) 94 | #define BLE_DATA_CHANNEL_8 (8) 95 | #define BLE_DATA_CHANNEL_9 (9) 96 | #define BLE_DATA_CHANNEL_10 (10) 97 | #define BLE_DATA_CHANNEL_11 (11) 98 | #define BLE_DATA_CHANNEL_12 (12) 99 | #define BLE_DATA_CHANNEL_13 (13) 100 | #define BLE_DATA_CHANNEL_14 (14) 101 | #define BLE_DATA_CHANNEL_15 (15) 102 | #define BLE_DATA_CHANNEL_16 (16) 103 | #define BLE_DATA_CHANNEL_17 (17) 104 | #define BLE_DATA_CHANNEL_18 (18) 105 | #define BLE_DATA_CHANNEL_19 (19) 106 | #define BLE_DATA_CHANNEL_20 (20) 107 | #define BLE_DATA_CHANNEL_21 (21) 108 | #define BLE_DATA_CHANNEL_22 (22) 109 | #define BLE_DATA_CHANNEL_23 (23) 110 | #define BLE_DATA_CHANNEL_24 (24) 111 | #define BLE_DATA_CHANNEL_25 (25) 112 | #define BLE_DATA_CHANNEL_26 (26) 113 | #define BLE_DATA_CHANNEL_27 (27) 114 | #define BLE_DATA_CHANNEL_28 (28) 115 | #define BLE_DATA_CHANNEL_29 (29) 116 | #define BLE_DATA_CHANNEL_30 (30) 117 | #define BLE_DATA_CHANNEL_31 (31) 118 | #define BLE_DATA_CHANNEL_32 (32) 119 | #define BLE_DATA_CHANNEL_33 (33) 120 | #define BLE_DATA_CHANNEL_34 (34) 121 | #define BLE_DATA_CHANNEL_35 (35) 122 | #define BLE_DATA_CHANNEL_36 (36) 123 | #define BLE_INVALID_CHANNEL (0xFF) 124 | 125 | 126 | #endif /* __BLECONFIG_H__ */ 127 | -------------------------------------------------------------------------------- /source/ChipDrv/NrfDrv/nrf51822/NrfTimer0Drv.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../../../include/DhGlobalHead.h" 28 | 29 | 30 | 31 | #define INTEN_COMPARE0 (1<<16) 32 | #define INTEN_COMPARE1 (1<<17) 33 | #define INTEN_COMPARE2 (1<<18) 34 | #define INTEN_COMPARE3 (1<<19) 35 | 36 | #define TIMER_MODE (0) 37 | #define COUNTER_MODE (1) 38 | 39 | #define BIT_MODE_8 (1) 40 | #define BIT_MODE_16 (0) 41 | #define BIT_MODE_24 (2) 42 | #define BIT_MODE_32 (3) 43 | 44 | #define PRESCALER_VALUE (4) // fTIMER = 16 MHz / (2^PRESCALER) 1us为计时单位 45 | 46 | #define TIMER0_RESOLUTION (1) /* 1μs TIMER0的分辨率*/ 47 | #define RTC_US_TO_COUNT(US) (US)//((US)/TIMER0_RESOLUTION) 48 | 49 | static u4 GET_CMP_OFFSET[] = {INTEN_COMPARE0,INTEN_COMPARE1,INTEN_COMPARE2,INTEN_COMPARE3}; 50 | 51 | static Timer0IntHandler S_NrfTimer0IntCb = NULL; 52 | 53 | /** 54 | *@brief: NrfTimer0Init 55 | *@details: nrf timer0初始化 56 | *@param[in] void 57 | */ 58 | void NrfTimer0Init(void) 59 | { 60 | NRF_TIMER0->TASKS_STOP = 1; 61 | NRF_TIMER0->TASKS_CLEAR = 1; 62 | NRF_TIMER0->INTENCLR = INTEN_COMPARE0|INTEN_COMPARE1|INTEN_COMPARE2; 63 | NRF_TIMER0->EVENTS_COMPARE[0] = 0; 64 | NRF_TIMER0->EVENTS_COMPARE[1] = 0; 65 | NRF_TIMER0->EVENTS_COMPARE[2] = 0; 66 | NRF_TIMER0->MODE = TIMER_MODE; 67 | NRF_TIMER0->BITMODE = BIT_MODE_32; 68 | NRF_TIMER0->PRESCALER = PRESCALER_VALUE; 69 | 70 | /* 协议栈的高精度定时器,目前先设置最高中断优先级吧 */ 71 | NVIC_SetPriority(TIMER0_IRQn, DH_IRQ_PRIORITY_0); 72 | NVIC_ClearPendingIRQ(TIMER0_IRQn); 73 | NVIC_EnableIRQ(TIMER0_IRQn); 74 | } 75 | 76 | /** 77 | *@brief: NrfTimer0HandlerRegister 78 | *@details: 设置TIMER0的中断事件处理函数 79 | *@param[in] IntHandler 中断事件处理函数 80 | *@retval: void 81 | */ 82 | void NrfTimer0HandlerRegister ( Timer0IntHandler IntHandler) 83 | { 84 | S_NrfTimer0IntCb = IntHandler; 85 | } 86 | 87 | /** 88 | *@brief: NrfTimer0SetCmpReg 89 | *@details: 设置TIMER0的比较寄存器的值 90 | *@param[in] reg 需要配置的TIMER0的比较寄存器 91 | *@param[in] u4TimeoutUs 超时时间us,0-0xFFFFFFFF 92 | 93 | *@retval: void 94 | */ 95 | void NrfTimer0SetCmpReg(EnNrfTimerCmpReg reg, u4 u4TimeoutUs) 96 | { 97 | u1 regNum; 98 | 99 | regNum = reg; 100 | NRF_TIMER0->CC[regNum] = RTC_US_TO_COUNT(u4TimeoutUs); 101 | NRF_TIMER0->INTENSET = GET_CMP_OFFSET[reg]; 102 | } 103 | 104 | 105 | void TIMER0_IRQHandler(void) 106 | { 107 | if ( NRF_TIMER0->EVENTS_COMPARE[0] ) 108 | { 109 | NRF_TIMER0->EVENTS_COMPARE[0] = 0; 110 | NRF_TIMER0->INTENCLR |= INTEN_COMPARE0; // 产生事件后就除能产生中断,定时器只作用一次。 111 | 112 | if ( NULL!=S_NrfTimer0IntCb ) 113 | { 114 | S_NrfTimer0IntCb(NRF_TIMER0_EVT_COMPARE0); 115 | } 116 | } 117 | if ( NRF_TIMER0->EVENTS_COMPARE[1] ) 118 | { 119 | NRF_TIMER0->EVENTS_COMPARE[1] = 0; 120 | NRF_TIMER0->INTENCLR |= INTEN_COMPARE1; 121 | if ( NULL != S_NrfTimer0IntCb ) 122 | { 123 | S_NrfTimer0IntCb(NRF_TIMER0_EVT_COMPARE1); 124 | } 125 | 126 | } 127 | if ( NRF_TIMER0->EVENTS_COMPARE[2] ) 128 | { 129 | NRF_TIMER0->EVENTS_COMPARE[2] = 0; 130 | NRF_TIMER0->INTENCLR |= INTEN_COMPARE2; 131 | if ( NULL != S_NrfTimer0IntCb ) 132 | { 133 | S_NrfTimer0IntCb(NRF_TIMER0_EVT_COMPARE2); 134 | } 135 | } 136 | if ( NRF_TIMER0->EVENTS_COMPARE[3] ) 137 | { 138 | NRF_TIMER0->EVENTS_COMPARE[3] = 0; 139 | NRF_TIMER0->INTENCLR |= INTEN_COMPARE3; 140 | if ( NULL != S_NrfTimer0IntCb ) 141 | { 142 | S_NrfTimer0IntCb(NRF_TIMER0_EVT_COMPARE3); 143 | } 144 | } 145 | } 146 | 147 | 148 | 149 | -------------------------------------------------------------------------------- /source/ChipDrv/NrfDrv/nrf52840/NrfClockDrv.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * (The MIT License) 4 | 5 | * Copyright (c) 2018 Feng Xun 6 | 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * 'Software'), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | 18 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "../../../../include/DhGlobalHead.h" 29 | 30 | #define nDEBUG_CLOCK_ENABLE 31 | 32 | #if !defined(DEBUG_CLOCK_ENABLE) 33 | #undef DEBUG_INFO 34 | #define DEBUG_INFO(...) 35 | #endif 36 | 37 | #define TASK_START (0x01) 38 | #define LFCLK_STAT_POS (16) 39 | #define HFCLK_SRC_POS (0) 40 | #define HFCLK_SRC_XTAL (1) 41 | #define HFCLK_SRC_RC (0) 42 | 43 | #define INTENSET_CTTO (1<<4) 44 | #define INTENSET_DONE (1<<3) 45 | 46 | #define CAL_TIME_UNIT_MS (250) 47 | 48 | u4 g_CalTimeout = 0; 49 | 50 | 51 | static u1 IsHFCLKSrcXtal(void) 52 | { 53 | if( NRF_CLOCK->HFCLKSTAT&(1<HFCLKSTAT & 0xff) == 1) { 63 | return 1; 64 | } 65 | return 0; 66 | } 67 | 68 | /** 69 | *@brief: NrfRcOscCalStart 70 | *@details: 启动内部rc震荡器的周期校准 71 | *@param[in] timeoutMs 校准周期 0.25s-31.75s 72 | *@retval: void 73 | */ 74 | static void NrfRcOscCalStart(u4 timeoutMs ) 75 | { 76 | DEBUG_INFO("start rc cal"); 77 | NRF_CLOCK->INTENSET = INTENSET_CTTO | INTENSET_DONE; 78 | NRF_CLOCK->CTIV = timeoutMs/CAL_TIME_UNIT_MS; 79 | 80 | // 校准定时器,设置个低优先级就好了 81 | NVIC_SetPriority(POWER_CLOCK_IRQn, 3); 82 | NVIC_ClearPendingIRQ(POWER_CLOCK_IRQn); 83 | NVIC_EnableIRQ(POWER_CLOCK_IRQn); 84 | 85 | NRF_CLOCK->TASKS_CTSTART = TASK_START; 86 | 87 | g_CalTimeout = timeoutMs; 88 | } 89 | 90 | /** 91 | *@brief: NrfLFClkStart 92 | *@details: 启动低频时钟 93 | *@param[in] src 低频时钟源 94 | *@retval: void 95 | */ 96 | void NrfLFClkStart( EnNrfLFClockSrc src , u4 u4CalTimeoutMs, u1 enableCal) 97 | { 98 | NRF_CLOCK->LFCLKSRC = src; 99 | 100 | NRF_CLOCK->TASKS_LFCLKSTART = TASK_START; 101 | while( !(NRF_CLOCK->LFCLKSTAT&(1<EVENTS_HFCLKSTARTED = 0; 121 | 122 | // 52840 内部高频时钟(64M HFCLK)来源有内部RC振荡器(64M HFINT)和晶体振荡器(64M HFXO,实际由外部32M物理晶振驱动获得) 123 | // 上电后,不做配置,即默认使用内部RC振荡器(HFINT) 124 | // 设置 TASKS_HFCLKSTART 寄存器来启动HFCLK,即表示使用HFXO(硬件上需要外接32M晶振) 125 | NRF_CLOCK->TASKS_HFCLKSTART = TASK_START; 126 | 127 | // 等待晶振启振完成,HFCLK运行 128 | while(1) 129 | { 130 | if ((1 == NRF_CLOCK->EVENTS_HFCLKSTARTED) 131 | && IsHFCLKRunning() == 1 ) 132 | { 133 | break; 134 | } 135 | } 136 | } 137 | 138 | 139 | void POWER_CLOCK_IRQHandler(void) 140 | { 141 | if( NRF_CLOCK->EVENTS_CTTO ) 142 | { 143 | NRF_CLOCK->EVENTS_CTTO = 0; 144 | if( HFCLK_SRC_XTAL == IsHFCLKSrcXtal() ) 145 | { 146 | /* 使用了外部高频晶振时才能校准 */ 147 | NRF_CLOCK->TASKS_CAL = TASK_START; 148 | DEBUG_INFO("cal...."); 149 | } 150 | else 151 | { 152 | /* 重启校准定时器 */ 153 | NRF_CLOCK->CTIV = g_CalTimeout/CAL_TIME_UNIT_MS; 154 | NRF_CLOCK->TASKS_CTSTART = TASK_START; 155 | } 156 | } 157 | if( NRF_CLOCK->EVENTS_DONE ) 158 | { 159 | NRF_CLOCK->EVENTS_DONE = 0; 160 | /* 重启校准定时器 */ 161 | NRF_CLOCK->CTIV = g_CalTimeout/CAL_TIME_UNIT_MS; 162 | NRF_CLOCK->TASKS_CTSTART = TASK_START; 163 | DEBUG_INFO("cal done"); 164 | DEBUG_INFO("start cal time"); 165 | } 166 | } -------------------------------------------------------------------------------- /debug_use_systemview/Config/SEGGER_SYSVIEW_Conf.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * SEGGER Microcontroller GmbH * 3 | * The Embedded Experts * 4 | ********************************************************************** 5 | * * 6 | * (c) 1995 - 2021 SEGGER Microcontroller GmbH * 7 | * * 8 | * www.segger.com Support: support@segger.com * 9 | * * 10 | ********************************************************************** 11 | * * 12 | * SEGGER SystemView * Real-time application analysis * 13 | * * 14 | ********************************************************************** 15 | * * 16 | * All rights reserved. * 17 | * * 18 | * SEGGER strongly recommends to not make any changes * 19 | * to or modify the source code of this software in order to stay * 20 | * compatible with the SystemView and RTT protocol, and J-Link. * 21 | * * 22 | * Redistribution and use in source and binary forms, with or * 23 | * without modification, are permitted provided that the following * 24 | * condition is met: * 25 | * * 26 | * o Redistributions of source code must retain the above copyright * 27 | * notice, this condition and the following disclaimer. * 28 | * * 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * 30 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * 31 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * 32 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * 33 | * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR * 34 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * 35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * 36 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * 37 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * 38 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * 40 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * 41 | * DAMAGE. * 42 | * * 43 | ********************************************************************** 44 | * * 45 | * SystemView version: 3.42 * 46 | * * 47 | ********************************************************************** 48 | -------------------------- END-OF-HEADER ----------------------------- 49 | 50 | File : SEGGER_SYSVIEW_Conf.h 51 | Purpose : SEGGER SystemView configuration file. 52 | Set defines which deviate from the defaults (see SEGGER_SYSVIEW_ConfDefaults.h) here. 53 | Revision: $Rev: 21292 $ 54 | 55 | Additional information: 56 | Required defines which must be set are: 57 | SEGGER_SYSVIEW_GET_TIMESTAMP 58 | SEGGER_SYSVIEW_GET_INTERRUPT_ID 59 | For known compilers and cores, these might be set to good defaults 60 | in SEGGER_SYSVIEW_ConfDefaults.h. 61 | 62 | SystemView needs a (nestable) locking mechanism. 63 | If not defined, the RTT locking mechanism is used, 64 | which then needs to be properly configured. 65 | */ 66 | 67 | #ifndef SEGGER_SYSVIEW_CONF_H 68 | #define SEGGER_SYSVIEW_CONF_H 69 | 70 | /********************************************************************* 71 | * 72 | * Defines, configurable 73 | * 74 | ********************************************************************** 75 | */ 76 | 77 | /********************************************************************* 78 | * TODO: Add your defines here. * 79 | ********************************************************************** 80 | */ 81 | 82 | 83 | #endif // SEGGER_SYSVIEW_CONF_H 84 | 85 | /*************************** End of file ****************************/ 86 | -------------------------------------------------------------------------------- /include/BleStack/BleLink/BleLink.h: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef __BLELINK_H__ 28 | #define __BLELINK_H__ 29 | #include "../../DhGlobalHead.h" 30 | 31 | #define BLE_SCA_20_PPM (7) 32 | #define BLE_SCA_30_PPM (6) 33 | #define BLE_SCA_50_PPM (5) 34 | #define BLE_SCA_75_PPM (4) 35 | #define BLE_SCA_100_PPM (3) 36 | #define BLE_SCA_150_PPM (2) 37 | #define BLE_SCA_250_PPM (1) 38 | #define BLE_SCA_500_PPM (0) 39 | #define BLE_SCA_GRADE_NUMBER (8) 40 | 41 | #define LLID_START (0x02) 42 | #define LLID_CONTINUATION (0x01) 43 | #define LLID_EMPTY_PACKET (0x01) // 空包的LLID需要设置成0x01 44 | #define LLID_CONTROL (0x03) 45 | 46 | #define CONTROL_PACKET (0x03) 47 | #define DATA_PACKET (0x02) 48 | 49 | #define LINK_DATA_ENC_FLAG (1) 50 | #define LINK_DATA_NO_ENC (0) 51 | 52 | /* The Connection Timeout error code indicates that the link supervision timeout has expired for a given connection */ 53 | #define LINK_DISCONN_REASON_TIMEOUT (0x08) 54 | /* The Remote User Terminated Connection error code indicates that the user on the remote device terminated the connection.*/ 55 | #define LINK_DISCONN_REASON_REMOTE_TERMINATED (0x13) 56 | /* The Connection Terminated Due to MIC Failure error code indicates that the connection was terminated because the Message Integrity Check (MIC) failed on a received packet */ 57 | #define LINK_DISCONN_REASON_MIC_FAILED (0x3D) 58 | /*The Instant Passed error code indicates that an LMP PDU or LL PDU that includes an instant cannot be performed because the instant when this would have occurred has passed.*/ 59 | #define LINK_DISCONN_REASON_INSTANT_PASSED (0x28) 60 | 61 | typedef struct 62 | { 63 | u1 m_u1Header1; 64 | u1 m_u1Header2; 65 | }BlkBlePduHeader; /* PDU header有2字节*/ 66 | 67 | /* 68 | 69 | ******link层以上都当做host,部分link control命令可以延迟处理则也当做host层数据******** 70 | */ 71 | 72 | 73 | /* 链路中断只会处理紧急事件,其他事情通过将数据延后到下半部处理,部分链路控制命令也是当做host层数据推送到下半部*/ 74 | typedef struct 75 | { 76 | u1 m_pu1LinkData[BLE_PDU_LENGTH]; 77 | }BlkLinkToHostData; 78 | 79 | /* host层数据推送到一个数据队列中,链路层进入连接事件后再取出数据,部分链路控制返回的响应也当做host层数据通过这个数据结构放到数据队列中 */ 80 | typedef struct 81 | { 82 | u1 m_u2Length; // buff中数据长度 83 | u1 m_u1PacketFlag; 84 | u1 m_pu1HostData[BLE_PDU_LENGTH-BLE_PDU_HEADER_LENGTH]; 85 | }BlkHostToLinkData; 86 | 87 | 88 | 89 | typedef enum 90 | { 91 | BLE_LINK_ADVERTISING = 0, /* 广播中 */ 92 | BLE_LINK_CONNECTED, /* 连接态,里面分了连接中子状态 ,连接上子状态 */ 93 | BLE_LINK_PRE_CONNING, /* 一个连接建立前的特殊状态 94 | 以前链路细分状态转换是 广播-->连接中--->连接上 95 | 现在链路细分状态转换是 广播-->准备连接前---->连接中--->连接上 */ 96 | BLE_LINK_STANDBY, /* 空闲待命状态*/ 97 | 98 | BLE_LINK_STATE_END, 99 | }EnBleLinkState; /* 链路状态*/ 100 | 101 | #ifdef __cplusplus 102 | #if __cplusplus 103 | extern "C"{ 104 | #endif 105 | #endif /* __cplusplus */ 106 | 107 | extern void BleLinkInit(void); 108 | extern void BleLinkReset(void); 109 | extern void BleLinkStateHandlerReg(EnBleLinkState state, BleRadioEvtHandler evtHandler); 110 | extern void BleLinkStateSwitch(EnBleLinkState state); 111 | extern void BleLinkPeerAddrInfoGet(BlkBleAddrInfo *addr); 112 | extern void BleDisconnCommHandle(u1 DisconnReason); 113 | extern void LinkEncStartFlagCfg(u1 flag); 114 | 115 | extern u4 BleHostDataToLinkPop(BlkHostToLinkData *pblkData); 116 | extern u4 BleHostDataToLinkPush(BlkHostToLinkData blkData); 117 | extern u4 BleLinkDataToHostPush(BlkLinkToHostData blkData); 118 | extern u4 NotifyHostDisconn(u1 reason); 119 | 120 | 121 | #ifdef __cplusplus 122 | #if __cplusplus 123 | } 124 | #endif 125 | #endif /* __cplusplus */ 126 | 127 | 128 | #endif /* __BLELINK_H__ */ 129 | -------------------------------------------------------------------------------- /source/ChipDrv/NrfDrv/nrf52840/NrfTimer0Drv.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../../../include/DhGlobalHead.h" 28 | 29 | #define nDEBUG_NRF_TIMER0_ENABLE 30 | 31 | #ifndef DEBUG_NRF_TIMER0_ENABLE 32 | #undef SEGGER_SYSVIEW_PrintfHost 33 | #undef SEGGER_SYSVIEW_RecordEnterISR 34 | #undef SEGGER_SYSVIEW_RecordExitISR 35 | 36 | #define SEGGER_SYSVIEW_PrintfHost(...) 37 | #define SEGGER_SYSVIEW_RecordEnterISR(...) 38 | #define SEGGER_SYSVIEW_RecordExitISR(...) 39 | #endif 40 | 41 | #define INTEN_COMPARE0 (1<<16) 42 | #define INTEN_COMPARE1 (1<<17) 43 | #define INTEN_COMPARE2 (1<<18) 44 | #define INTEN_COMPARE3 (1<<19) 45 | 46 | #define TIMER_MODE (0) 47 | #define COUNTER_MODE (1) 48 | 49 | #define BIT_MODE_8 (1) 50 | #define BIT_MODE_16 (0) 51 | #define BIT_MODE_24 (2) 52 | #define BIT_MODE_32 (3) 53 | 54 | #define PRESCALER_VALUE (4) // fTIMER = 16 MHz / (2^PRESCALER) 1us为计时单位 55 | 56 | #define TIMER0_RESOLUTION (1) /* 1μs TIMER0的分辨率*/ 57 | #define RTC_US_TO_COUNT(US) (US)//((US)/TIMER0_RESOLUTION) 58 | 59 | static u4 GET_CMP_OFFSET[] = {INTEN_COMPARE0,INTEN_COMPARE1,INTEN_COMPARE2,INTEN_COMPARE3}; 60 | 61 | static Timer0IntHandler S_NrfTimer0IntCb = NULL; 62 | 63 | /** 64 | *@brief: NrfTimer0Init 65 | *@details: nrf timer0初始化 66 | *@param[in] void 67 | */ 68 | void NrfTimer0Init(void) 69 | { 70 | NRF_TIMER0->TASKS_STOP = 1; 71 | NRF_TIMER0->TASKS_CLEAR = 1; 72 | NRF_TIMER0->INTENCLR = INTEN_COMPARE0|INTEN_COMPARE1|INTEN_COMPARE2; 73 | NRF_TIMER0->EVENTS_COMPARE[0] = 0; 74 | NRF_TIMER0->EVENTS_COMPARE[1] = 0; 75 | NRF_TIMER0->EVENTS_COMPARE[2] = 0; 76 | NRF_TIMER0->MODE = TIMER_MODE; 77 | NRF_TIMER0->BITMODE = BIT_MODE_32; 78 | NRF_TIMER0->PRESCALER = PRESCALER_VALUE; 79 | 80 | /* 协议栈的高精度定时器,设置最高中断优先级 */ 81 | NVIC_SetPriority(TIMER0_IRQn, 0); 82 | NVIC_ClearPendingIRQ(TIMER0_IRQn); 83 | NVIC_EnableIRQ(TIMER0_IRQn); 84 | } 85 | 86 | /** 87 | *@brief: NrfTimer0HandlerRegister 88 | *@details: 设置TIMER0的中断事件处理函数 89 | *@param[in] IntHandler 中断事件处理函数 90 | *@retval: void 91 | */ 92 | void NrfTimer0HandlerRegister ( Timer0IntHandler IntHandler) 93 | { 94 | S_NrfTimer0IntCb = IntHandler; 95 | } 96 | 97 | /** 98 | *@brief: NrfTimer0SetCmpReg 99 | *@details: 设置TIMER0的比较寄存器的值 100 | *@param[in] reg 需要配置的TIMER0的比较寄存器 101 | *@param[in] u4TimeoutUs 超时时间us,0-0xFFFFFFFF 102 | 103 | *@retval: void 104 | */ 105 | void NrfTimer0SetCmpReg(EnNrfTimerCmpReg reg, u4 u4TimeoutUs) 106 | { 107 | u1 regNum; 108 | 109 | regNum = reg; 110 | NRF_TIMER0->CC[regNum] = RTC_US_TO_COUNT(u4TimeoutUs); 111 | NRF_TIMER0->INTENSET = GET_CMP_OFFSET[reg]; 112 | } 113 | 114 | 115 | void TIMER0_IRQHandler(void) 116 | { 117 | if ( NRF_TIMER0->EVENTS_COMPARE[0] ) 118 | { 119 | NRF_TIMER0->EVENTS_COMPARE[0] = 0; 120 | NRF_TIMER0->INTENCLR |= INTEN_COMPARE0; // 产生事件后就除能产生中断,定时器只作用一次。 121 | 122 | if ( NULL!=S_NrfTimer0IntCb ) 123 | { 124 | S_NrfTimer0IntCb(NRF_TIMER0_EVT_COMPARE0); 125 | } 126 | } 127 | if ( NRF_TIMER0->EVENTS_COMPARE[1] ) 128 | { 129 | NRF_TIMER0->EVENTS_COMPARE[1] = 0; 130 | NRF_TIMER0->INTENCLR |= INTEN_COMPARE1; 131 | if ( NULL != S_NrfTimer0IntCb ) 132 | { 133 | S_NrfTimer0IntCb(NRF_TIMER0_EVT_COMPARE1); 134 | } 135 | 136 | } 137 | if ( NRF_TIMER0->EVENTS_COMPARE[2] ) 138 | { 139 | NRF_TIMER0->EVENTS_COMPARE[2] = 0; 140 | NRF_TIMER0->INTENCLR |= INTEN_COMPARE2; 141 | if ( NULL != S_NrfTimer0IntCb ) 142 | { 143 | S_NrfTimer0IntCb(NRF_TIMER0_EVT_COMPARE2); 144 | } 145 | } 146 | if ( NRF_TIMER0->EVENTS_COMPARE[3] ) 147 | { 148 | NRF_TIMER0->EVENTS_COMPARE[3] = 0; 149 | NRF_TIMER0->INTENCLR |= INTEN_COMPARE3; 150 | if ( NULL != S_NrfTimer0IntCb ) 151 | { 152 | S_NrfTimer0IntCb(NRF_TIMER0_EVT_COMPARE3); 153 | } 154 | } 155 | } 156 | 157 | 158 | 159 | -------------------------------------------------------------------------------- /include/ChipDrv/NrfDrv/nrf52840_delay.h: -------------------------------------------------------------------------------- 1 | #ifndef _NRF_DELAY_H 2 | #define _NRF_DELAY_H 3 | 4 | #include "nrf52840.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ 10 | 11 | 12 | #define CLOCK_FREQ_16MHz (16000000UL) 13 | 14 | /** 15 | * @brief Function for delaying execution for number of microseconds. 16 | * 17 | * @note NRF52 has instruction cache and because of that delay is not precise. 18 | * 19 | * @param number_of_us 20 | * 21 | */ 22 | /*lint --e{438, 522, 40, 10, 563} "Variable not used" "Function lacks side-effects" */ 23 | __STATIC_INLINE void nrf_delay_us(uint32_t number_of_us); 24 | 25 | 26 | /** 27 | * @brief Function for delaying execution for number of miliseconds. 28 | * 29 | * @note NRF52 has instruction cache and because of that delay is not precise. 30 | * 31 | * @note Function internally calls @ref nrf_delay_us so the maximum delay is the 32 | * same as in case of @ref nrf_delay_us, approx. 71 minutes. 33 | * 34 | * @param number_of_ms 35 | * 36 | */ 37 | 38 | /*lint --e{438, 522, 40, 10, 563} "Variable not used" "Function lacks side-effects" */ 39 | __STATIC_INLINE void nrf_delay_ms(uint32_t number_of_ms); 40 | 41 | #if defined ( __CC_ARM ) 42 | __STATIC_INLINE void nrf_delay_us(uint32_t number_of_us) 43 | { 44 | if(!number_of_us) 45 | return; 46 | __asm 47 | { 48 | loop: 49 | NOP 50 | NOP 51 | NOP 52 | NOP 53 | NOP 54 | NOP 55 | NOP 56 | NOP 57 | CMP SystemCoreClock, CLOCK_FREQ_16MHz 58 | BEQ cond 59 | NOP 60 | #if defined(NRF52) || defined(NRF52840_XXAA) || defined(NRF52832) 61 | NOP 62 | NOP 63 | NOP 64 | NOP 65 | NOP 66 | NOP 67 | NOP 68 | NOP 69 | NOP 70 | NOP 71 | NOP 72 | NOP 73 | NOP 74 | NOP 75 | NOP 76 | NOP 77 | NOP 78 | NOP 79 | NOP 80 | NOP 81 | NOP 82 | NOP 83 | NOP 84 | NOP 85 | NOP 86 | NOP 87 | NOP 88 | NOP 89 | NOP 90 | NOP 91 | NOP 92 | NOP 93 | NOP 94 | NOP 95 | NOP 96 | NOP 97 | NOP 98 | NOP 99 | NOP 100 | NOP 101 | NOP 102 | NOP 103 | NOP 104 | NOP 105 | NOP 106 | NOP 107 | NOP 108 | #endif 109 | cond: 110 | SUBS number_of_us,number_of_us, #1 111 | BNE loop 112 | } 113 | } 114 | 115 | #elif defined ( _WIN32 ) || defined ( __unix ) || defined( __APPLE__ ) 116 | 117 | 118 | #ifndef CUSTOM_NRF_DELAY_US 119 | __STATIC_INLINE void nrf_delay_us(uint32_t number_of_us) 120 | {} 121 | #endif 122 | 123 | #elif defined ( __GNUC__ ) || ( __ICCARM__ ) 124 | 125 | __STATIC_INLINE void nrf_delay_us(uint32_t number_of_us) 126 | { 127 | const uint32_t clock16MHz = CLOCK_FREQ_16MHz; 128 | if (number_of_us) 129 | { 130 | __ASM volatile ( 131 | #if ( defined(__GNUC__) && (__CORTEX_M == (0x00U) ) ) 132 | ".syntax unified\n" 133 | #endif 134 | "1:\n" 135 | " NOP\n" 136 | " NOP\n" 137 | " NOP\n" 138 | " NOP\n" 139 | " NOP\n" 140 | " NOP\n" 141 | " NOP\n" 142 | " NOP\n" 143 | " CMP %[SystemCoreClock],%[clock16MHz]\n" 144 | " BEQ.n 2f\n" 145 | " NOP\n" 146 | #if defined(NRF52) || defined(NRF52840_XXAA) || defined(NRF52832) 147 | " NOP\n" 148 | " NOP\n" 149 | " NOP\n" 150 | " NOP\n" 151 | " NOP\n" 152 | " NOP\n" 153 | " NOP\n" 154 | " NOP\n" 155 | " NOP\n" 156 | " NOP\n" 157 | " NOP\n" 158 | " NOP\n" 159 | " NOP\n" 160 | " NOP\n" 161 | " NOP\n" 162 | " NOP\n" 163 | " NOP\n" 164 | " NOP\n" 165 | " NOP\n" 166 | " NOP\n" 167 | " NOP\n" 168 | " NOP\n" 169 | " NOP\n" 170 | " NOP\n" 171 | " NOP\n" 172 | " NOP\n" 173 | " NOP\n" 174 | " NOP\n" 175 | " NOP\n" 176 | " NOP\n" 177 | " NOP\n" 178 | " NOP\n" 179 | " NOP\n" 180 | " NOP\n" 181 | " NOP\n" 182 | " NOP\n" 183 | " NOP\n" 184 | " NOP\n" 185 | " NOP\n" 186 | " NOP\n" 187 | " NOP\n" 188 | " NOP\n" 189 | " NOP\n" 190 | " NOP\n" 191 | " NOP\n" 192 | " NOP\n" 193 | " NOP\n" 194 | #endif 195 | "2:\n" 196 | " SUBS %0, %0, #1\n" 197 | " BNE.n 1b\n" 198 | #if __CORTEX_M == (0x00U) 199 | #ifdef __GNUC__ 200 | ".syntax divided\n" 201 | #endif 202 | :"+l" (number_of_us) : 203 | #else 204 | :"+r" (number_of_us) : 205 | #endif 206 | [SystemCoreClock] "r" (SystemCoreClock), 207 | [clock16MHz] "r" (clock16MHz) 208 | ); 209 | #ifdef __ICCARM__ 210 | __DMB(); 211 | #endif 212 | } 213 | } 214 | #endif 215 | 216 | __STATIC_INLINE void nrf_delay_ms(uint32_t number_of_ms) 217 | { 218 | nrf_delay_us(1000*number_of_ms); 219 | } 220 | 221 | 222 | #ifdef __cplusplus 223 | } 224 | #endif 225 | 226 | #endif 227 | -------------------------------------------------------------------------------- /debug_use_systemview/SEGGER/SEGGER_SYSVIEW_Int.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * SEGGER Microcontroller GmbH * 3 | * The Embedded Experts * 4 | ********************************************************************** 5 | * * 6 | * (c) 1995 - 2021 SEGGER Microcontroller GmbH * 7 | * * 8 | * www.segger.com Support: support@segger.com * 9 | * * 10 | ********************************************************************** 11 | * * 12 | * SEGGER SystemView * Real-time application analysis * 13 | * * 14 | ********************************************************************** 15 | * * 16 | * All rights reserved. * 17 | * * 18 | * SEGGER strongly recommends to not make any changes * 19 | * to or modify the source code of this software in order to stay * 20 | * compatible with the SystemView and RTT protocol, and J-Link. * 21 | * * 22 | * Redistribution and use in source and binary forms, with or * 23 | * without modification, are permitted provided that the following * 24 | * condition is met: * 25 | * * 26 | * o Redistributions of source code must retain the above copyright * 27 | * notice, this condition and the following disclaimer. * 28 | * * 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * 30 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * 31 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * 32 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * 33 | * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR * 34 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * 35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * 36 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * 37 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * 38 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * 40 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * 41 | * DAMAGE. * 42 | * * 43 | ********************************************************************** 44 | * * 45 | * SystemView version: 3.42 * 46 | * * 47 | ********************************************************************** 48 | -------------------------- END-OF-HEADER ----------------------------- 49 | File : SEGGER_SYSVIEW_Int.h 50 | Purpose : SEGGER SystemView internal header. 51 | Revision: $Rev: 21281 $ 52 | */ 53 | 54 | #ifndef SEGGER_SYSVIEW_INT_H 55 | #define SEGGER_SYSVIEW_INT_H 56 | 57 | /********************************************************************* 58 | * 59 | * #include Section 60 | * 61 | ********************************************************************** 62 | */ 63 | 64 | #include "SEGGER_SYSVIEW.h" 65 | 66 | #ifdef __cplusplus 67 | extern "C" { 68 | #endif 69 | 70 | 71 | /********************************************************************* 72 | * 73 | * Private data types 74 | * 75 | ********************************************************************** 76 | */ 77 | // 78 | // Commands that Host can send to target 79 | // 80 | typedef enum { 81 | SEGGER_SYSVIEW_COMMAND_ID_START = 1, 82 | SEGGER_SYSVIEW_COMMAND_ID_STOP, 83 | SEGGER_SYSVIEW_COMMAND_ID_GET_SYSTIME, 84 | SEGGER_SYSVIEW_COMMAND_ID_GET_TASKLIST, 85 | SEGGER_SYSVIEW_COMMAND_ID_GET_SYSDESC, 86 | SEGGER_SYSVIEW_COMMAND_ID_GET_NUMMODULES, 87 | SEGGER_SYSVIEW_COMMAND_ID_GET_MODULEDESC, 88 | SEGGER_SYSVIEW_COMMAND_ID_HEARTBEAT = 127, 89 | // Extended commands: Commands >= 128 have a second parameter 90 | SEGGER_SYSVIEW_COMMAND_ID_GET_MODULE = 128 91 | } SEGGER_SYSVIEW_COMMAND_ID; 92 | 93 | #ifdef __cplusplus 94 | } 95 | #endif 96 | 97 | #endif 98 | 99 | /*************************** End of file ****************************/ 100 | -------------------------------------------------------------------------------- /include/debug/SEGGER_RTT.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * SEGGER MICROCONTROLLER SYSTEME GmbH * 3 | * Solutions for real time microcontroller applications * 4 | ********************************************************************** 5 | * * 6 | * (c) 1996-2014 SEGGER Microcontroller Systeme GmbH * 7 | * * 8 | * Internet: www.segger.com Support: support@segger.com * 9 | * * 10 | ********************************************************************** 11 | ---------------------------------------------------------------------- 12 | File : SEGGER_RTT.h 13 | Date : 17 Dec 2014 14 | Purpose : Implementation of SEGGER real-time terminal which allows 15 | real-time terminal communication on targets which support 16 | debugger memory accesses while the CPU is running. 17 | ---------------------------END-OF-HEADER------------------------------ 18 | */ 19 | 20 | /********************************************************************* 21 | * 22 | * Defines 23 | * 24 | ********************************************************************** 25 | */ 26 | #define SEGGER_RTT_MODE_MASK (3 << 0) 27 | 28 | #define SEGGER_RTT_MODE_NO_BLOCK_SKIP (0) 29 | #define SEGGER_RTT_MODE_NO_BLOCK_TRIM (1 << 0) 30 | #define SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL (1 << 1) 31 | 32 | #define RTT_CTRL_RESET "" 33 | 34 | #define RTT_CTRL_CLEAR "" 35 | 36 | #define RTT_CTRL_TEXT_BLACK "" 37 | #define RTT_CTRL_TEXT_RED "" 38 | #define RTT_CTRL_TEXT_GREEN "" 39 | #define RTT_CTRL_TEXT_YELLOW "" 40 | #define RTT_CTRL_TEXT_BLUE "" 41 | #define RTT_CTRL_TEXT_MAGENTA "" 42 | #define RTT_CTRL_TEXT_CYAN "" 43 | #define RTT_CTRL_TEXT_WHITE "" 44 | 45 | #define RTT_CTRL_TEXT_BRIGHT_BLACK "" 46 | #define RTT_CTRL_TEXT_BRIGHT_RED "" 47 | #define RTT_CTRL_TEXT_BRIGHT_GREEN "" 48 | #define RTT_CTRL_TEXT_BRIGHT_YELLOW "" 49 | #define RTT_CTRL_TEXT_BRIGHT_BLUE "" 50 | #define RTT_CTRL_TEXT_BRIGHT_MAGENTA "" 51 | #define RTT_CTRL_TEXT_BRIGHT_CYAN "" 52 | #define RTT_CTRL_TEXT_BRIGHT_WHITE "" 53 | 54 | #define RTT_CTRL_BG_BLACK "" 55 | #define RTT_CTRL_BG_RED "" 56 | #define RTT_CTRL_BG_GREEN "" 57 | #define RTT_CTRL_BG_YELLOW "" 58 | #define RTT_CTRL_BG_BLUE "" 59 | #define RTT_CTRL_BG_MAGENTA "" 60 | #define RTT_CTRL_BG_CYAN "" 61 | #define RTT_CTRL_BG_WHITE "" 62 | 63 | #define RTT_CTRL_BG_BRIGHT_BLACK "" 64 | #define RTT_CTRL_BG_BRIGHT_RED "" 65 | #define RTT_CTRL_BG_BRIGHT_GREEN "" 66 | #define RTT_CTRL_BG_BRIGHT_YELLOW "" 67 | #define RTT_CTRL_BG_BRIGHT_BLUE "" 68 | #define RTT_CTRL_BG_BRIGHT_MAGENTA "" 69 | #define RTT_CTRL_BG_BRIGHT_CYAN "" 70 | #define RTT_CTRL_BG_BRIGHT_WHITE "" 71 | 72 | 73 | /********************************************************************* 74 | * 75 | * RTT API functions 76 | * 77 | ********************************************************************** 78 | */ 79 | 80 | int SEGGER_RTT_Read (unsigned BufferIndex, char* pBuffer, unsigned BufferSize); 81 | int SEGGER_RTT_Write (unsigned BufferIndex, const char* pBuffer, unsigned NumBytes); 82 | int SEGGER_RTT_WriteString (unsigned BufferIndex, const char* s); 83 | 84 | int SEGGER_RTT_GetKey (void); 85 | int SEGGER_RTT_WaitKey (void); 86 | int SEGGER_RTT_HasKey (void); 87 | 88 | int SEGGER_RTT_ConfigUpBuffer (unsigned BufferIndex, const char* sName, char* pBuffer, int BufferSize, int Flags); 89 | int SEGGER_RTT_ConfigDownBuffer (unsigned BufferIndex, const char* sName, char* pBuffer, int BufferSize, int Flags); 90 | 91 | void SEGGER_RTT_Init (void); 92 | 93 | /********************************************************************* 94 | * 95 | * RTT "Terminal" API functions 96 | * 97 | ********************************************************************** 98 | */ 99 | void SEGGER_RTT_SetTerminal (char TerminalId); 100 | int SEGGER_RTT_TerminalOut (char TerminalId, const char* s); 101 | 102 | /********************************************************************* 103 | * 104 | * RTT printf functions (require SEGGER_RTT_printf.c) 105 | * 106 | ********************************************************************** 107 | */ 108 | int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...); 109 | 110 | /*************************** End of file ****************************/ 111 | -------------------------------------------------------------------------------- /source/ChipDrv/NrfDrv/nrf51822/system_nrf51.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2015, Nordic Semiconductor ASA 2 | * All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * * Redistributions of source code must retain the above copyright notice, this 8 | * list of conditions and the following disclaimer. 9 | * 10 | * * Redistributions in binary form must reproduce the above copyright notice, 11 | * this list of conditions and the following disclaimer in the documentation 12 | * and/or other materials provided with the distribution. 13 | * 14 | * * Neither the name of Nordic Semiconductor ASA nor the names of its 15 | * contributors may be used to endorse or promote products derived from 16 | * this software without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | * 29 | */ 30 | 31 | /* NOTE: Template files (including this one) are application specific and therefore expected to 32 | be copied into the application project folder prior to its use! */ 33 | 34 | #include "../../../../include/DhGlobalHead.h" 35 | 36 | /*lint ++flb "Enter library region" */ 37 | 38 | 39 | #define __SYSTEM_CLOCK (16000000UL) /*!< nRF51 devices use a fixed System Clock Frequency of 16MHz */ 40 | 41 | static bool is_manual_peripheral_setup_needed(void); 42 | static bool is_disabled_in_debug_needed(void); 43 | 44 | 45 | #if defined ( __CC_ARM ) 46 | uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK; 47 | #elif defined ( __ICCARM__ ) 48 | __root uint32_t SystemCoreClock = __SYSTEM_CLOCK; 49 | #elif defined ( __GNUC__ ) 50 | uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK; 51 | #endif 52 | 53 | void SystemCoreClockUpdate(void) 54 | { 55 | SystemCoreClock = __SYSTEM_CLOCK; 56 | } 57 | 58 | void SystemInit(void) 59 | { 60 | /* If desired, switch off the unused RAM to lower consumption by the use of RAMON register. 61 | It can also be done in the application main() function. */ 62 | 63 | /* Prepare the peripherals for use as indicated by the PAN 26 "System: Manual setup is required 64 | to enable the use of peripherals" found at Product Anomaly document for your device found at 65 | https://www.nordicsemi.com/. The side effect of executing these instructions in the devices 66 | that do not need it is that the new peripherals in the second generation devices (LPCOMP for 67 | example) will not be available. */ 68 | if (is_manual_peripheral_setup_needed()) 69 | { 70 | *(uint32_t volatile *)0x40000504 = 0xC007FFDF; 71 | *(uint32_t volatile *)0x40006C18 = 0x00008000; 72 | } 73 | 74 | /* Disable PROTENSET registers under debug, as indicated by PAN 59 "MPU: Reset value of DISABLEINDEBUG 75 | register is incorrect" found at Product Anomaly document four your device found at 76 | https://www.nordicsemi.com/. There is no side effect of using these instruction if not needed. */ 77 | if (is_disabled_in_debug_needed()) 78 | { 79 | NRF_MPU->DISABLEINDEBUG = MPU_DISABLEINDEBUG_DISABLEINDEBUG_Disabled << MPU_DISABLEINDEBUG_DISABLEINDEBUG_Pos; 80 | } 81 | } 82 | 83 | 84 | static bool is_manual_peripheral_setup_needed(void) 85 | { 86 | if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)) 87 | { 88 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x00) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) 89 | { 90 | return true; 91 | } 92 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x10) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) 93 | { 94 | return true; 95 | } 96 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x30) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) 97 | { 98 | return true; 99 | } 100 | } 101 | 102 | return false; 103 | } 104 | 105 | static bool is_disabled_in_debug_needed(void) 106 | { 107 | if ((((*(uint32_t *)0xF0000FE0) & 0x000000FF) == 0x1) && (((*(uint32_t *)0xF0000FE4) & 0x0000000F) == 0x0)) 108 | { 109 | if ((((*(uint32_t *)0xF0000FE8) & 0x000000F0) == 0x40) && (((*(uint32_t *)0xF0000FEC) & 0x000000F0) == 0x0)) 110 | { 111 | return true; 112 | } 113 | } 114 | 115 | return false; 116 | } 117 | 118 | /*lint --flb "Leave library region" */ 119 | -------------------------------------------------------------------------------- /source/ChipDrv/NrfDrv/nrf51822/NrfRtcDrv.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../../../../include/DhGlobalHead.h" 28 | 29 | #define INTEN_COMPARE0 (1<<16) 30 | #define INTEN_COMPARE1 (1<<17) 31 | #define INTEN_COMPARE2 (1<<18) 32 | #define INTEN_COMPARE3 (1<<19) 33 | 34 | #define EVTEN_COMPARE0 (1<<16) 35 | #define EVTEN_COMPARE1 (1<<17) 36 | #define EVTEN_COMPARE2 (1<<18) 37 | #define EVTEN_COMPARE3 (1<<19) 38 | 39 | #define PRESCALER_VALUE (0) // The COUNTER resolution will therefore be 30.517 μs. 40 | 41 | #define RTC0_RESOLUTION (30.517) /* 30.517 μs RTC0的分辨率*/ 42 | #define RTC0_JITTER (30) /* 根据手册 RTC的CLEAR STOP START有平均30us的延迟*/ 43 | #define RTC_US_TO_COUNT(US) (((US)-RTC0_JITTER+RTC0_RESOLUTION/2)/RTC0_RESOLUTION) /*减去RTC的延迟,并做四舍五入*/ 44 | 45 | static u4 GET_CMP_OFFSET[] = {EVTEN_COMPARE0,EVTEN_COMPARE1,EVTEN_COMPARE2}; 46 | static Rtc0IntHandler S_NrfRtc0IntCb = NULL; 47 | 48 | /** 49 | *@brief: NrfRtc0Init 50 | *@details: nrf rt0初始化 51 | *@param[in] void 52 | */ 53 | void NrfRtc0Init(void) 54 | { 55 | NRF_RTC0->TASKS_STOP = 1; 56 | NRF_RTC0->TASKS_CLEAR = 1; 57 | NRF_RTC0->INTENSET = INTEN_COMPARE0|INTEN_COMPARE1|INTEN_COMPARE2; 58 | NRF_RTC0->EVTEN = 0;//EVTEN_COMPARE0|EVTEN_COMPARE1|EVTEN_COMPARE2; 59 | NRF_RTC0->EVENTS_COMPARE[0] = 0; 60 | NRF_RTC0->EVENTS_COMPARE[1] = 0; 61 | NRF_RTC0->EVENTS_COMPARE[2] = 0; 62 | 63 | NRF_RTC0->PRESCALER = PRESCALER_VALUE; 64 | 65 | /* 协议栈的低功耗唤醒定时器,目前先设置最高中断优先级吧 */ 66 | NVIC_SetPriority(RTC0_IRQn, DH_IRQ_PRIORITY_0); 67 | NVIC_ClearPendingIRQ(RTC0_IRQn); 68 | NVIC_EnableIRQ(RTC0_IRQn); 69 | } 70 | 71 | /** 72 | *@brief: NrfRtc0HandlerRegister 73 | *@details: 设置RTC0的中断事件处理函数 74 | *@param[in] IntHandler 中断事件处理函数 75 | *@retval: void 76 | */ 77 | void NrfRtc0HandlerRegister ( Rtc0IntHandler IntHandler) 78 | { 79 | S_NrfRtc0IntCb = IntHandler; 80 | } 81 | 82 | /** 83 | *@brief: NrfRtc0SetCmpReg 84 | *@details: 设置RTC0的比较寄存器的值 85 | *@param[in] reg 需要配置的RTC0的比较寄存器 86 | *@param[in] u4TimeoutUs 不应该超过500s 87 | *@param[in] isAbsolute 是否是设置绝对值,绝对值则直接设置cc寄存器,否则为相对值,需要先获取当前的COUNT值相加后设置cc寄存器的值 88 | *@retval: void 89 | */ 90 | void NrfRtc0SetCmpReg(EnNrfRtcCmpReg reg, u4 u4TimeoutUs, u1 isAbsolute) 91 | { 92 | u1 regNum; 93 | u4 curCounter = 0; 94 | 95 | regNum = reg; 96 | NRF_RTC0->EVTEN |= GET_CMP_OFFSET[reg]; 97 | if( SET_RTC_CC_RELATIVE == isAbsolute ) 98 | { 99 | curCounter = NRF_RTC0->COUNTER; 100 | } 101 | NRF_RTC0->CC[regNum] =curCounter + RTC_US_TO_COUNT(u4TimeoutUs); 102 | } 103 | 104 | /** 105 | *@brief: NrfRtc0DisableCmpReg 106 | *@details: 关闭某个比较器的中断功能 107 | *@param[in] reg 比较器寄存器 108 | 109 | *@retval: void 110 | */ 111 | void NrfRtc0DisableCmpReg(EnNrfRtcCmpReg reg) 112 | { 113 | NRF_RTC0->EVTEN &= ~GET_CMP_OFFSET[reg]; 114 | } 115 | 116 | /** 117 | *@brief: NrfRtc0CounterGet 118 | *@details: 获取当前的Rtc0 counter值 119 | *@retval: void 120 | */ 121 | u4 NrfRtc0CounterGet(void) 122 | { 123 | return NRF_RTC0->COUNTER; 124 | } 125 | 126 | 127 | void RTC0_IRQHandler(void) 128 | { 129 | u4 regBackup; 130 | 131 | regBackup = NRF_RTC0->EVTEN; 132 | if ( NRF_RTC0->EVENTS_COMPARE[0] ) 133 | { 134 | NRF_RTC0->EVENTS_COMPARE[0] = 0; 135 | NRF_RTC0->EVTEN &= ~EVTEN_COMPARE0; // 产生事件后就除能产生事件,使定时器只作用一次。 136 | if ( NULL!=S_NrfRtc0IntCb && regBackup&EVTEN_COMPARE0 ) 137 | { 138 | S_NrfRtc0IntCb(NRF_RTC0_EVT_COMPARE0); 139 | } 140 | } 141 | if ( NRF_RTC0->EVENTS_COMPARE[1] ) 142 | { 143 | NRF_RTC0->EVENTS_COMPARE[1] = 0; 144 | NRF_RTC0->EVTEN &= ~EVTEN_COMPARE1; // 产生事件后就除能产生事件,使定时器只作用一次。 145 | 146 | if ( NULL != S_NrfRtc0IntCb && regBackup&EVTEN_COMPARE1 ) 147 | { 148 | S_NrfRtc0IntCb(NRF_RTC0_EVT_COMPARE1); 149 | } 150 | 151 | } 152 | if ( NRF_RTC0->EVENTS_COMPARE[2] ) 153 | { 154 | NRF_RTC0->EVENTS_COMPARE[2] = 0; 155 | NRF_RTC0->EVTEN &= ~EVTEN_COMPARE2; 156 | if ( NULL != S_NrfRtc0IntCb && regBackup&EVTEN_COMPARE2 ) 157 | { 158 | S_NrfRtc0IntCb(NRF_RTC0_EVT_COMPARE2); 159 | } 160 | 161 | } 162 | if ( NRF_RTC0->EVENTS_COMPARE[3] ) /* 手册说RTC0只有3个比较寄存器,应该不会有这个中断的 */ 163 | { 164 | NRF_RTC0->EVENTS_COMPARE[3] = 0; 165 | NRF_RTC0->EVTEN &= ~EVTEN_COMPARE3; 166 | 167 | } 168 | } -------------------------------------------------------------------------------- /source/DhBleAux.c: -------------------------------------------------------------------------------- 1 | /* 2 | * (The MIT License) 3 | 4 | * Copyright (c) 2018 Feng Xun 5 | 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * 'Software'), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | 17 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 21 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #include "../include/DhGlobalHead.h" 28 | 29 | #define RF_CHANNEL0_FREQ (2402) 30 | #define RF_CHANNEL1_FREQ (2404) 31 | #define RF_CHANNEL12_FREQ (2426) 32 | #define RF_CHANNEL13_FREQ (2428) 33 | #define RF_CHANNEL39_FREQ (2480) 34 | /** 35 | *@brief: GetRfChannelFreq 36 | *@details: 获据通道对应的频率 37 | *@param[in] u1 u1DataChannel 0-39 38 | 39 | *@retval: 具体频率 40 | */ 41 | u2 BleGetChannelFreq(u1 u1Channel) 42 | { 43 | if ( u1Channel <=BLE_DATA_CHANNEL_10 ) 44 | { 45 | return RF_CHANNEL1_FREQ+u1Channel*2; 46 | } 47 | else if ( u1Channel>=BLE_DATA_CHANNEL_11 && u1Channel<=BLE_DATA_CHANNEL_36 ) 48 | { 49 | return RF_CHANNEL13_FREQ+(u1Channel-BLE_DATA_CHANNEL_11)*2; 50 | } 51 | else if ( BLE_ADV_CHANNEL_37 == u1Channel ) 52 | { 53 | return RF_CHANNEL0_FREQ; 54 | } 55 | else if ( BLE_ADV_CHANNEL_38 == u1Channel ) 56 | { 57 | return RF_CHANNEL12_FREQ; 58 | } 59 | else if ( BLE_ADV_CHANNEL_39 == u1Channel ) 60 | { 61 | return RF_CHANNEL39_FREQ; 62 | } 63 | return 0xFFFF; 64 | } 65 | 66 | /** 67 | *@brief: GetChannelWhiteIv 68 | *@details: 获得该通道的白化序列初值 69 | *@param[in] channel 通道号 70 | 71 | *@retval: whiteIv 白化初值 72 | */ 73 | u1 GetChannelWhiteIv(u1 channel ) 74 | { 75 | return channel; 76 | } 77 | 78 | /** 79 | *@brief: DhMemxor 80 | *@details: 数组异或运算 81 | *@param[in|out] pu1Data1 原数据1,运算后输出 82 | *@param[in] pu1Data2 原数据2 83 | *@param[in] u2Len 数据长度 84 | *@retval: void 85 | */ 86 | void DhMemxor(u1 *pu1Data1, u1 *pu1Data2, u2 u2Len) 87 | { 88 | u2 u2Index; 89 | 90 | if( NULL==pu1Data1 || NULL==pu1Data2 ) 91 | { 92 | return ; 93 | } 94 | 95 | for( u2Index = 0; u2Index < u2Len; u2Index++ ) 96 | { 97 | pu1Data1[u2Index] = pu1Data1[u2Index]^pu1Data2[u2Index]; 98 | } 99 | 100 | } 101 | 102 | /** 103 | *@brief: DhAesEnc 104 | *@details: 加密16字节数据 105 | *@param[in] pu1In 数据Buff,LSB输入,内部加密前转换成MSB格式 106 | *@param[in] pu1Key 秘钥,LSB输入,内部加密前转换成MSB。 107 | *@param[out] pu1Out 输出密文,LSB输出 108 | 109 | *@retval: DH_SUCCESS 110 | */ 111 | u4 DhAesEnc(u1 *pu1In, u1 *pu1Key, u1 *pu1Out) 112 | { 113 | /* AES 算法内部有 U4指针转换过程,M0处理器要求地址访问对齐,所以这里确保一下地址是4字节对齐的 */ 114 | u1 tmpIn[16] __attribute__((aligned(4))); 115 | u1 tmpKey[16] __attribute__((aligned(4))); 116 | u1 tmpOut[16] __attribute__((aligned(4))); 117 | u1 i; 118 | if ( NULL==pu1In || NULL==pu1Key || NULL==pu1Out ) 119 | { 120 | return ERR_COMM_INVALID_PARAMS; 121 | } 122 | 123 | for(i = 0; i < 16; i++ ) 124 | { 125 | tmpIn[i] = pu1In[16-i-1]; 126 | } 127 | for(i = 0; i < 16; i++ ) 128 | { 129 | tmpKey[i] = pu1Key[16-i-1]; 130 | } 131 | SwAesEncryptData(tmpKey, tmpIn, 16, tmpOut); 132 | 133 | for(i = 0; i < 16; i++ ) 134 | { 135 | pu1Out[i] = tmpOut[16-i-1]; 136 | } 137 | 138 | return DH_SUCCESS; 139 | } 140 | 141 | 142 | /** 143 | *@brief: DhAesEnc2 144 | *@details: 加密16字节数据 145 | *@param[in] pu1In 数据Buff,MSB,内部不需要转换 146 | *@param[in] pu1Key 秘钥,LSB输入,内部加密前转换成MSB。 147 | *@param[out] pu1Out 输出密文,MSB 输出 148 | 149 | *@retval: DH_SUCCESS 150 | */ 151 | u4 DhAesEnc2(u1 *pu1In, u1 *pu1Key, u1 *pu1Out) 152 | { 153 | /* AES 算法内部有 U4指针转换过程,M0处理器要求地址访问对齐,所以这里确保一下地址是4字节对齐的 */ 154 | u1 tmpKey[16] __attribute__((aligned(4))); 155 | u1 i; 156 | 157 | if ( NULL==pu1In || NULL==pu1Key || NULL==pu1Out ) 158 | { 159 | return ERR_COMM_INVALID_PARAMS; 160 | } 161 | for(i = 0; i < 16; i++ ) 162 | { 163 | tmpKey[i] = pu1Key[16-i-1]; 164 | } 165 | SwAesEncryptData(tmpKey, pu1In, 16, pu1Out); 166 | 167 | return DH_SUCCESS; 168 | } 169 | 170 | 171 | /** 172 | *@brief: DhGetRand 173 | *@details: 获取随机数 174 | *@param[out] pu1Data 175 | *@param[int] len 要获取的长度 176 | 177 | *@retval: void 178 | */ 179 | void DhGetRand(u1 *pu1Data, u2 len) 180 | { 181 | u2 i; 182 | 183 | if( NULL== pu1Data ) 184 | { 185 | return ; 186 | } 187 | for( i = 0; i < len; i++ ) 188 | { 189 | pu1Data[i] = i; 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /debug_use_systemview/Config/Global.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * SEGGER Microcontroller GmbH * 3 | * The Embedded Experts * 4 | ********************************************************************** 5 | * * 6 | * (c) 1995 - 2021 SEGGER Microcontroller GmbH * 7 | * * 8 | * www.segger.com Support: support@segger.com * 9 | * * 10 | ********************************************************************** 11 | * * 12 | * SEGGER SystemView * Real-time application analysis * 13 | * * 14 | ********************************************************************** 15 | * * 16 | * All rights reserved. * 17 | * * 18 | * SEGGER strongly recommends to not make any changes * 19 | * to or modify the source code of this software in order to stay * 20 | * compatible with the SystemView and RTT protocol, and J-Link. * 21 | * * 22 | * Redistribution and use in source and binary forms, with or * 23 | * without modification, are permitted provided that the following * 24 | * condition is met: * 25 | * * 26 | * o Redistributions of source code must retain the above copyright * 27 | * notice, this condition and the following disclaimer. * 28 | * * 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * 30 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * 31 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * 32 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * 33 | * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR * 34 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * 35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * 36 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * 37 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * 38 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * 40 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * 41 | * DAMAGE. * 42 | * * 43 | ********************************************************************** 44 | * * 45 | * SystemView version: 3.42 * 46 | * * 47 | ********************************************************************** 48 | ---------------------------------------------------------------------- 49 | File : Global.h 50 | Purpose : Global types 51 | In case your application already has a Global.h, you should 52 | merge the files. In order to use Segger code, the types 53 | U8, U16, U32, I8, I16, I32 need to be defined in Global.h; 54 | additional definitions do not hurt. 55 | Revision: $Rev: 12501 $ 56 | ---------------------------END-OF-HEADER------------------------------ 57 | */ 58 | 59 | #ifndef GLOBAL_H // Guard against multiple inclusion 60 | #define GLOBAL_H 61 | 62 | #define U8 unsigned char 63 | #define I8 signed char 64 | #define U16 unsigned short 65 | #define I16 signed short 66 | #ifdef __x86_64__ 67 | #define U32 unsigned 68 | #define I32 int 69 | #else 70 | #define U32 unsigned long 71 | #define I32 signed long 72 | #endif 73 | 74 | // 75 | // CC_NO_LONG_SUPPORT can be defined to compile test 76 | // without long support for compilers that do not 77 | // support C99 and its long type. 78 | // 79 | #ifdef CC_NO_LONG_SUPPORT 80 | #define PTR_ADDR U32 81 | #else // Supports long type. 82 | #if defined(_WIN32) && !defined(__clang__) && !defined(__MINGW32__) 83 | // 84 | // Microsoft VC6 compiler related 85 | // 86 | #define U64 unsigned __int64 87 | #define U128 unsigned __int128 88 | #define I64 __int64 89 | #define I128 __int128 90 | #if _MSC_VER <= 1200 91 | #define U64_C(x) x##UI64 92 | #else 93 | #define U64_C(x) x##ULL 94 | #endif 95 | #else 96 | // 97 | // C99 compliant compiler 98 | // 99 | #define U64 unsigned long long 100 | #define I64 signed long long 101 | #define U64_C(x) x##ULL 102 | #endif 103 | 104 | #if (defined(_WIN64) || defined(__LP64__)) // 64-bit symbols used by Visual Studio and GCC, maybe others as well. 105 | #define PTR_ADDR U64 106 | #else 107 | #define PTR_ADDR U32 108 | #endif 109 | #endif // Supports long type. 110 | 111 | #endif // Avoid multiple inclusion 112 | 113 | /*************************** End of file ****************************/ 114 | -------------------------------------------------------------------------------- /source/ChipDrv/NrfDrv/nrf52840/NrfRtcDrv.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * (The MIT License) 4 | 5 | * Copyright (c) 2018 Feng Xun 6 | 7 | * Permission is hereby granted, free of charge, to any person obtaining 8 | * a copy of this software and associated documentation files (the 9 | * 'Software'), to deal in the Software without restriction, including 10 | * without limitation the rights to use, copy, modify, merge, publish, 11 | * distribute, sublicense, and/or sell copies of the Software, and to 12 | * permit persons to whom the Software is furnished to do so, subject to 13 | * the following conditions: 14 | 15 | * The above copyright notice and this permission notice shall be 16 | * included in all copies or substantial portions of the Software. 17 | 18 | * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | * 26 | */ 27 | 28 | #include "../../../../include/DhGlobalHead.h" 29 | 30 | 31 | #define nDEBUG_NRF_RTC0_ENABLE 32 | 33 | #ifndef DEBUG_NRF_RTC0_ENABLE 34 | #undef SEGGER_SYSVIEW_PrintfHost 35 | #undef SEGGER_SYSVIEW_RecordEnterISR 36 | #undef SEGGER_SYSVIEW_RecordExitISR 37 | 38 | #define SEGGER_SYSVIEW_PrintfHost(...) 39 | #define SEGGER_SYSVIEW_RecordEnterISR(...) 40 | #define SEGGER_SYSVIEW_RecordExitISR(...) 41 | #endif 42 | 43 | 44 | 45 | #define INTEN_COMPARE0 (1<<16) 46 | #define INTEN_COMPARE1 (1<<17) 47 | #define INTEN_COMPARE2 (1<<18) 48 | #define INTEN_COMPARE3 (1<<19) 49 | 50 | #define EVTEN_COMPARE0 (1<<16) 51 | #define EVTEN_COMPARE1 (1<<17) 52 | #define EVTEN_COMPARE2 (1<<18) 53 | #define EVTEN_COMPARE3 (1<<19) 54 | 55 | #define PRESCALER_VALUE (0) // The COUNTER resolution will therefore be 30.517 μs. 56 | 57 | #define RTC0_RESOLUTION (30.517) /* 30.517 μs RTC0的分辨率*/ 58 | 59 | /* 根据手册 RTC的CLEAR STOP START命令的实际生效最大可能有46us的延迟, 60 | * 如果RTC 在LFCLK启动之前启动,则抖动最大有250us,所以实际使用需要先明确启动LFCLK 61 | */ 62 | #define RTC0_JITTER (46) 63 | 64 | #define RTC_US_TO_COUNT(US) (((US)+RTC0_RESOLUTION/2)/RTC0_RESOLUTION) /* 四舍五入 */ 65 | 66 | static u4 GET_CMP_OFFSET[] = {EVTEN_COMPARE0,EVTEN_COMPARE1,EVTEN_COMPARE2}; 67 | static Rtc0IntHandler S_NrfRtc0IntCb = NULL; 68 | 69 | /** 70 | *@brief: NrfRtc0Init 71 | *@details: nrf rt0初始化 72 | *@param[in] void 73 | */ 74 | void NrfRtc0Init(void) 75 | { 76 | NRF_RTC0->TASKS_STOP = 1; 77 | NRF_RTC0->TASKS_CLEAR = 1; 78 | 79 | // 使能COMPARE事件发生时触发对应中断 80 | NRF_RTC0->INTENSET = INTEN_COMPARE0|INTEN_COMPARE1|INTEN_COMPARE2; 81 | 82 | // COMPARE事件,先全部屏蔽,后续使用哪个再开启哪个 83 | NRF_RTC0->EVTEN = 0; 84 | 85 | // 启动时清除事件标记 86 | NRF_RTC0->EVENTS_COMPARE[0] = 0; 87 | NRF_RTC0->EVENTS_COMPARE[1] = 0; 88 | NRF_RTC0->EVENTS_COMPARE[2] = 0; 89 | NRF_RTC0->EVENTS_COMPARE[3] = 0; 90 | 91 | // 不分频,RTC 时间精度为1/32768 秒 92 | NRF_RTC0->PRESCALER = PRESCALER_VALUE; 93 | 94 | /* 协议栈的低功耗唤醒定时器,设置最高中断优先级吧 */ 95 | NVIC_SetPriority(RTC0_IRQn, 0); 96 | NVIC_ClearPendingIRQ(RTC0_IRQn); 97 | NVIC_EnableIRQ(RTC0_IRQn); 98 | } 99 | 100 | /** 101 | *@brief: NrfRtc0HandlerRegister 102 | *@details: 设置RTC0的中断事件处理函数 103 | *@param[in] IntHandler 中断事件处理函数 104 | *@retval: void 105 | */ 106 | void NrfRtc0HandlerRegister ( Rtc0IntHandler IntHandler) 107 | { 108 | S_NrfRtc0IntCb = IntHandler; 109 | } 110 | 111 | /** 112 | *@brief: NrfRtc0SetCmpReg 113 | *@details: 设置RTC0的比较寄存器的值 114 | *@param[in] reg 需要配置的RTC0的比较寄存器 115 | *@param[in] u4TimeoutUs 不应该超过500s 116 | *@param[in] isAbsolute 是否是设置绝对值,绝对值则直接设置cc寄存器, 117 | 否则为相对值,需要先获取当前的COUNT值相加后设置cc寄存器的值 118 | 上层调用自己保证,相对值不会溢出计数器的范围 119 | *@retval: void 120 | */ 121 | void NrfRtc0SetCmpReg(EnNrfRtcCmpReg reg, u4 u4TimeoutUs, u1 isAbsolute) 122 | { 123 | u1 regNum; 124 | u4 curCounter = 0; 125 | 126 | regNum = reg; 127 | NRF_RTC0->EVTEN |= GET_CMP_OFFSET[reg]; 128 | if( SET_RTC_CC_RELATIVE == isAbsolute ) 129 | { 130 | curCounter = NRF_RTC0->COUNTER; 131 | } 132 | NRF_RTC0->CC[regNum] =curCounter + RTC_US_TO_COUNT(u4TimeoutUs); 133 | } 134 | 135 | /** 136 | *@brief: NrfRtc0DisableCmpReg 137 | *@details: 关闭某个比较器的中断功能 138 | *@param[in] reg 比较器寄存器 139 | 140 | *@retval: void 141 | */ 142 | void NrfRtc0DisableCmpReg(EnNrfRtcCmpReg reg) 143 | { 144 | NRF_RTC0->EVTEN &= ~GET_CMP_OFFSET[reg]; 145 | } 146 | 147 | /** 148 | *@brief: NrfRtc0CounterGet 149 | *@details: 获取当前的Rtc0 counter值 150 | *@retval: void 151 | */ 152 | u4 NrfRtc0CounterGet(void) 153 | { 154 | return NRF_RTC0->COUNTER; 155 | } 156 | 157 | 158 | void RTC0_IRQHandler(void) 159 | { 160 | u4 regBackup; 161 | 162 | regBackup = NRF_RTC0->EVTEN; 163 | if ( NRF_RTC0->EVENTS_COMPARE[0] ) 164 | { 165 | NRF_RTC0->EVENTS_COMPARE[0] = 0; 166 | NRF_RTC0->EVTEN &= ~EVTEN_COMPARE0; // 产生事件后就除能产生事件,使定时器只作用一次。 167 | if ( NULL!=S_NrfRtc0IntCb && regBackup&EVTEN_COMPARE0 ) 168 | { 169 | S_NrfRtc0IntCb(NRF_RTC0_EVT_COMPARE0); 170 | } 171 | } 172 | if ( NRF_RTC0->EVENTS_COMPARE[1] ) 173 | { 174 | NRF_RTC0->EVENTS_COMPARE[1] = 0; 175 | NRF_RTC0->EVTEN &= ~EVTEN_COMPARE1; // 产生事件后就除能产生事件,使定时器只作用一次。 176 | 177 | if ( NULL != S_NrfRtc0IntCb && regBackup&EVTEN_COMPARE1 ) 178 | { 179 | S_NrfRtc0IntCb(NRF_RTC0_EVT_COMPARE1); 180 | } 181 | 182 | } 183 | if ( NRF_RTC0->EVENTS_COMPARE[2] ) 184 | { 185 | NRF_RTC0->EVENTS_COMPARE[2] = 0; 186 | NRF_RTC0->EVTEN &= ~EVTEN_COMPARE2; 187 | if ( NULL != S_NrfRtc0IntCb && regBackup&EVTEN_COMPARE2 ) 188 | { 189 | S_NrfRtc0IntCb(NRF_RTC0_EVT_COMPARE2); 190 | } 191 | 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /debug_use_systemview/SEGGER/Syscalls/SEGGER_RTT_Syscalls_IAR.c: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * SEGGER Microcontroller GmbH * 3 | * The Embedded Experts * 4 | ********************************************************************** 5 | * * 6 | * (c) 1995 - 2021 SEGGER Microcontroller GmbH * 7 | * * 8 | * www.segger.com Support: support@segger.com * 9 | * * 10 | ********************************************************************** 11 | * * 12 | * SEGGER SystemView * Real-time application analysis * 13 | * * 14 | ********************************************************************** 15 | * * 16 | * All rights reserved. * 17 | * * 18 | * SEGGER strongly recommends to not make any changes * 19 | * to or modify the source code of this software in order to stay * 20 | * compatible with the SystemView and RTT protocol, and J-Link. * 21 | * * 22 | * Redistribution and use in source and binary forms, with or * 23 | * without modification, are permitted provided that the following * 24 | * condition is met: * 25 | * * 26 | * o Redistributions of source code must retain the above copyright * 27 | * notice, this condition and the following disclaimer. * 28 | * * 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * 30 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * 31 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * 32 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * 33 | * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR * 34 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * 35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * 36 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * 37 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * 38 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * 40 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * 41 | * DAMAGE. * 42 | * * 43 | ********************************************************************** 44 | * * 45 | * SystemView version: 3.42 * 46 | * * 47 | ********************************************************************** 48 | ---------------------------END-OF-HEADER------------------------------ 49 | File : SEGGER_RTT_Syscalls_IAR.c 50 | Purpose : Low-level functions for using printf() via RTT in IAR. 51 | To use RTT for printf output, include this file in your 52 | application and set the Library Configuration to Normal. 53 | Revision: $Rev: 24316 $ 54 | ---------------------------------------------------------------------- 55 | */ 56 | #ifdef __IAR_SYSTEMS_ICC__ 57 | 58 | // 59 | // Since IAR EWARM V8 and EWRX V4, yfuns.h is considered as deprecated and LowLevelIOInterface.h 60 | // shall be used instead. To not break any compatibility with older compiler versions, we have a 61 | // version check in here. 62 | // 63 | #if ((defined __ICCARM__) && (__VER__ >= 8000000)) || ((defined __ICCRX__) && (__VER__ >= 400)) 64 | #include 65 | #else 66 | #include 67 | #endif 68 | 69 | #include "SEGGER_RTT.h" 70 | #pragma module_name = "?__write" 71 | 72 | /********************************************************************* 73 | * 74 | * Function prototypes 75 | * 76 | ********************************************************************** 77 | */ 78 | size_t __write(int handle, const unsigned char * buffer, size_t size); 79 | 80 | /********************************************************************* 81 | * 82 | * Global functions 83 | * 84 | ********************************************************************** 85 | */ 86 | /********************************************************************* 87 | * 88 | * __write() 89 | * 90 | * Function description 91 | * Low-level write function. 92 | * Standard library subroutines will use this system routine 93 | * for output to all files, including stdout. 94 | * Write data via RTT. 95 | */ 96 | size_t __write(int handle, const unsigned char * buffer, size_t size) { 97 | (void) handle; /* Not used, avoid warning */ 98 | SEGGER_RTT_Write(0, (const char*)buffer, size); 99 | return size; 100 | } 101 | 102 | /********************************************************************* 103 | * 104 | * __write_buffered() 105 | * 106 | * Function description 107 | * Low-level write function. 108 | * Standard library subroutines will use this system routine 109 | * for output to all files, including stdout. 110 | * Write data via RTT. 111 | */ 112 | size_t __write_buffered(int handle, const unsigned char * buffer, size_t size) { 113 | (void) handle; /* Not used, avoid warning */ 114 | SEGGER_RTT_Write(0, (const char*)buffer, size); 115 | return size; 116 | } 117 | 118 | #endif 119 | /****** End Of File *************************************************/ 120 | -------------------------------------------------------------------------------- /debug_use_systemview/SEGGER/Syscalls/SEGGER_RTT_Syscalls_GCC.c: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * SEGGER Microcontroller GmbH * 3 | * The Embedded Experts * 4 | ********************************************************************** 5 | * * 6 | * (c) 1995 - 2021 SEGGER Microcontroller GmbH * 7 | * * 8 | * www.segger.com Support: support@segger.com * 9 | * * 10 | ********************************************************************** 11 | * * 12 | * SEGGER SystemView * Real-time application analysis * 13 | * * 14 | ********************************************************************** 15 | * * 16 | * All rights reserved. * 17 | * * 18 | * SEGGER strongly recommends to not make any changes * 19 | * to or modify the source code of this software in order to stay * 20 | * compatible with the SystemView and RTT protocol, and J-Link. * 21 | * * 22 | * Redistribution and use in source and binary forms, with or * 23 | * without modification, are permitted provided that the following * 24 | * condition is met: * 25 | * * 26 | * o Redistributions of source code must retain the above copyright * 27 | * notice, this condition and the following disclaimer. * 28 | * * 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * 30 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * 31 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * 32 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * 33 | * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR * 34 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * 35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * 36 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * 37 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * 38 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 39 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * 40 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * 41 | * DAMAGE. * 42 | * * 43 | ********************************************************************** 44 | * * 45 | * SystemView version: 3.42 * 46 | * * 47 | ********************************************************************** 48 | ---------------------------END-OF-HEADER------------------------------ 49 | File : SEGGER_RTT_Syscalls_GCC.c 50 | Purpose : Low-level functions for using printf() via RTT in GCC. 51 | To use RTT for printf output, include this file in your 52 | application. 53 | Revision: $Rev: 24316 $ 54 | ---------------------------------------------------------------------- 55 | */ 56 | #if (defined __GNUC__) && !(defined __SES_ARM) && !(defined __CROSSWORKS_ARM) && !(defined __ARMCC_VERSION) && !(defined __CC_ARM) 57 | 58 | #include // required for _write_r 59 | #include "SEGGER_RTT.h" 60 | 61 | 62 | /********************************************************************* 63 | * 64 | * Types 65 | * 66 | ********************************************************************** 67 | */ 68 | // 69 | // If necessary define the _reent struct 70 | // to match the one passed by the used standard library. 71 | // 72 | struct _reent; 73 | 74 | /********************************************************************* 75 | * 76 | * Function prototypes 77 | * 78 | ********************************************************************** 79 | */ 80 | _ssize_t _write (int file, const void *ptr, size_t len); 81 | _ssize_t _write_r(struct _reent *r, int file, const void *ptr, size_t len); 82 | 83 | /********************************************************************* 84 | * 85 | * Global functions 86 | * 87 | ********************************************************************** 88 | */ 89 | 90 | /********************************************************************* 91 | * 92 | * _write() 93 | * 94 | * Function description 95 | * Low-level write function. 96 | * libc subroutines will use this system routine for output to all files, 97 | * including stdout. 98 | * Write data via RTT. 99 | */ 100 | _ssize_t _write(int file, const void *ptr, size_t len) { 101 | (void) file; /* Not used, avoid warning */ 102 | SEGGER_RTT_Write(0, ptr, len); 103 | return len; 104 | } 105 | 106 | /********************************************************************* 107 | * 108 | * _write_r() 109 | * 110 | * Function description 111 | * Low-level reentrant write function. 112 | * libc subroutines will use this system routine for output to all files, 113 | * including stdout. 114 | * Write data via RTT. 115 | */ 116 | _ssize_t _write_r(struct _reent *r, int file, const void *ptr, size_t len) { 117 | (void) file; /* Not used, avoid warning */ 118 | (void) r; /* Not used, avoid warning */ 119 | SEGGER_RTT_Write(0, ptr, len); 120 | return len; 121 | } 122 | 123 | #endif 124 | /****** End Of File *************************************************/ 125 | --------------------------------------------------------------------------------