diff --git a/application_controller/serial_interface/serial_command.h b/application_controller/serial_interface/serial_command.h index c8460b3f..f2db2a42 100644 --- a/application_controller/serial_interface/serial_command.h +++ b/application_controller/serial_interface/serial_command.h @@ -29,7 +29,7 @@ typedef enum { SERIAL_CMD_OPCODE_ECHO = 0x02, SERIAL_CMD_OPCODE_RADIO_RESET = 0x0E, - + SERIAL_CMD_OPCODE_INIT = 0x70, SERIAL_CMD_OPCODE_VALUE_SET = 0x71, SERIAL_CMD_OPCODE_VALUE_ENABLE = 0x72, @@ -44,62 +44,68 @@ typedef enum SERIAL_CMD_OPCODE_ACCESS_ADDR_GET = 0x7C, SERIAL_CMD_OPCODE_CHANNEL_GET = 0x7D, SERIAL_CMD_OPCODE_INTERVAL_GET = 0x7F, + + SERIAL_CMD_OPCODE_VALUE_REFRESH = 0x6F, } __packed serial_cmd_opcode_t; /****** CMD PARAMS ******/ -typedef struct +typedef struct { uint8_t data[29]; } __packed serial_cmd_params_echo_t; -typedef struct +typedef struct { uint32_t access_addr; uint32_t int_min; uint8_t channel; } __packed serial_cmd_params_init_t; -typedef struct +typedef struct { uint16_t handle; - uint8_t flag; + uint8_t flag; uint8_t value; } __packed serial_cmd_params_flag_set_t; -typedef struct +typedef struct { uint16_t handle; - uint8_t flag; + uint8_t flag; } __packed serial_cmd_params_flag_get_t; -typedef struct +typedef struct { uint16_t handle; uint8_t value[RBC_MESH_VALUE_MAX_LEN]; } __packed serial_cmd_params_value_set_t; -typedef struct +typedef struct { uint16_t handle; } __packed serial_cmd_params_value_enable_t; -typedef struct +typedef struct { uint16_t handle; } __packed serial_cmd_params_value_disable_t; -typedef struct +typedef struct { uint16_t handle; } __packed serial_cmd_params_value_get_t; +typedef struct +{ + uint16_t handle; +} __packed serial_cmd_params_value_refresh_t -typedef struct +typedef struct { uint8_t length; serial_cmd_opcode_t opcode; - union + union { serial_cmd_params_echo_t echo; serial_cmd_params_init_t init; @@ -109,6 +115,7 @@ typedef struct serial_cmd_params_value_enable_t value_enable; serial_cmd_params_value_disable_t value_disable; serial_cmd_params_value_get_t value_get; + serial_cmd_params_value_refresh_t value_refresh; } __packed params; } __packed serial_cmd_t; diff --git a/nRF51/SDK/bsp/boards.h b/nRF51/SDK/bsp/boards.h index d10908d0..75f94fa7 100644 --- a/nRF51/SDK/bsp/boards.h +++ b/nRF51/SDK/bsp/boards.h @@ -53,6 +53,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "wt51822.h" #elif defined(BOARD_CUSTOM) #include "custom_board.h" +#elif defined(BOARD_CUSTOM_SERIAL) + #include "custom_board_serial.h" #else #error "Board is not defined" #endif diff --git a/nRF51/SDK/bsp/custom_board_serial.h b/nRF51/SDK/bsp/custom_board_serial.h new file mode 100644 index 00000000..f066decb --- /dev/null +++ b/nRF51/SDK/bsp/custom_board_serial.h @@ -0,0 +1,131 @@ +/*********************************************************************************** +Copyright (c) Nordic Semiconductor ASA +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + + 3. Neither the name of Nordic Semiconductor ASA nor the names of other + contributors to this software may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************************/ +#ifndef PCA10028_H +#define PCA10028_H + +// LEDs definitions for PCA10028 +#define LEDS_NUMBER 4 + +#define LED_START 21 +#define LED_1 21 +#define LED_2 22 +#define LED_3 23 +#define LED_4 24 +#define LED_STOP 24 + +#define LEDS_LIST { LED_1, LED_2, LED_3, LED_4 } + +#define BSP_LED_0 LED_1 +#define BSP_LED_1 LED_2 +#define BSP_LED_2 LED_3 +#define BSP_LED_3 LED_4 + +#define BSP_LED_0_MASK (1<*.txt; *.h; *.inc *.plm *.cpp + 0 @@ -31,6 +32,7 @@ 1 0 1 + 0 1 @@ -135,7 +137,7 @@ 0 JL2CM3 - -U681970993 -O78 -S0 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(0BB11477) -L00(0) -TO18 -TC10000000 -TP28 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO15 -FD20000000 -FC1000 -FN1 -FF0nrf51xxx.flm -FS00 -FL0200000 -FP0($$Device:nRF51822_xxAB$Flash\nrf51xxx.flm) + -U681083609 -O78 -S2 -ZTIFSpeedSel5000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(0BB11477) -L00(0) -TO18 -TC10000000 -TP28 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO15 -FD20000000 -FC1000 -FN1 -FF0nrf51xxx.flm -FS00 -FL0200000 -FP0($$Device:nRF51822_xxAB$Flash\nrf51xxx.flm) 0 @@ -249,6 +251,7 @@ + 0 System Viewer\TIMER0 @@ -269,6 +272,7 @@ 1 0 1 + 0 1 @@ -350,6 +354,11 @@ Segger\JL2CM3.dll + + 0 + ARMRTXEVENTFLAGS + -L70 -Z18 -C0 -M0 -T1 + 0 DLGUARM @@ -368,7 +377,7 @@ 0 JL2CM3 - -U681970993 -O78 -S0 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(0BB11477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO7 -FD20000000 -FC1000 -FN1 -FF0nrf51xxx.flm -FS00 -FL0200000 -FP0($$Device:nRF51822_xxAA$Flash\nrf51xxx.flm) + -U681578639 -O78 -S2 -ZTIFSpeedSel5000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(0BB11477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8004 -TDT0 -TDC1F -TIE1 -TIP8 -TB1 -TFE0 -FO7 -FD20000000 -FC1000 -FN1 -FF0nrf51xxx.flm -FS00 -FL0200000 -FP0($$Device:nRF51822_xxAA$Flash\nrf51xxx.flm) 0 @@ -430,7 +439,7 @@ 0 0 0 - 0 + 1 1 0 0 @@ -438,16 +447,7 @@ - - - System Viewer\POWER - 35904 - - - System Viewer\UICR - 35905 - - + 0 @@ -462,6 +462,7 @@ 1 0 1 + 0 1 @@ -658,6 +659,7 @@ + 0 System Viewer\GPIO @@ -669,7 +671,7 @@ app - 1 + 0 0 0 0 @@ -941,6 +943,32 @@ 0 0 + + 2 + 21 + 1 + 0 + 0 + 0 + 0 + ..\gpio_config.c + gpio_config.c + 0 + 0 + + + 2 + 22 + 1 + 0 + 0 + 0 + 0 + ..\..\..\rbc_mesh\src\simple_uart.c + simple_uart.c + 0 + 0 + @@ -951,7 +979,7 @@ 0 3 - 21 + 23 2 0 0 @@ -964,7 +992,7 @@ 3 - 22 + 24 1 0 0 @@ -979,13 +1007,13 @@ lib - 1 + 0 0 0 0 4 - 23 + 25 1 0 0 @@ -998,7 +1026,7 @@ 4 - 24 + 26 1 0 0 @@ -1011,7 +1039,7 @@ 4 - 25 + 27 1 0 0 diff --git a/nRF51/examples/BLE_Gateway/arm/rbc_mesh_BLE_Gateway.uvprojx b/nRF51/examples/BLE_Gateway/arm/rbc_mesh_BLE_Gateway.uvprojx index 241588b4..baa10d91 100644 --- a/nRF51/examples/BLE_Gateway/arm/rbc_mesh_BLE_Gateway.uvprojx +++ b/nRF51/examples/BLE_Gateway/arm/rbc_mesh_BLE_Gateway.uvprojx @@ -10,11 +10,12 @@ nRF51822 xxAA 0x4 ARM-ADS + 5060020::V5.06 (build 20)::ARMCC nRF51822_xxAB Nordic Semiconductor - NordicSemiconductor.nRF_DeviceFamilyPack.1.1.4 + NordicSemiconductor.nRF_DeviceFamilyPack.8.3.1 http://developer.nordicsemi.com/nRF51_SDK/pieces/nRF_DeviceFamilyPack/ IROM(0x00000000,0x20000) IRAM(0x20000000,0x4000) CPUTYPE("Cortex-M0") CLOCK(12000000) ELITTLE @@ -84,6 +85,8 @@ 0 0 + 0 + 0 0 @@ -227,6 +230,7 @@ 1 0 0 + 0 3 3 0 @@ -361,9 +365,13 @@ 0 0 0 + 0 + 0 + 0 + 0 --c99 - BOARD_PCA10028 NRF51 BLE_STACK_SUPPORT_REQD S110 SOFTDEVICE_PRESENT + BOARD_PCA10028 NRF51 BLE_STACK_SUPPORT_REQD S110 SOFTDEVICE_PRESENT DEBUG HVAC ..\include;..\..\..\rbc_mesh;..\..\..\rbc_mesh\include;..\..\..\SDK\bsp;..\..\..\..\..\..\components\softdevice\common\softdevice_handler;..\..\..\..\..\..\components\toolchain\gcc;..\..\..\..\..\..\components\libraries\util;..\..\..\..\..\..\components\ble\common;..\..\..\..\..\..\components\drivers_nrf\hal;..\..\..\..\..\..\components\drivers_nrf\spi_slave;..\..\..\..\..\..\components\softdevice\s110\headers @@ -501,6 +509,10 @@ 2 2 2 + 0 + 0 + 0 + 0 @@ -549,6 +561,10 @@ 2 2 2 + 0 + 0 + 0 + 0 @@ -599,6 +615,16 @@ 1 ..\..\..\rbc_mesh\src\bootloader_info_app.c + + gpio_config.c + 1 + ..\gpio_config.c + + + simple_uart.c + 1 + ..\..\..\rbc_mesh\src\simple_uart.c + @@ -662,6 +688,10 @@ 2 2 2 + 0 + 0 + 0 + 0 @@ -685,11 +715,12 @@ nRF51822 xxAA serial 0x4 ARM-ADS + 5060020::V5.06 (build 20)::ARMCC nRF51822_xxAC Nordic Semiconductor - NordicSemiconductor.nRF_DeviceFamilyPack.1.1.4 + NordicSemiconductor.nRF_DeviceFamilyPack.8.3.1 http://developer.nordicsemi.com/nRF51_SDK/pieces/nRF_DeviceFamilyPack/ IROM(0x00000000,0x40000) IRAM(0x20000000,0x8000) CPUTYPE("Cortex-M0") CLOCK(12000000) ELITTLE @@ -759,6 +790,8 @@ 0 0 + 0 + 0 0 @@ -902,6 +935,7 @@ 1 0 0 + 0 3 3 0 @@ -1036,9 +1070,13 @@ 0 0 0 + 0 + 0 + 0 + 0 --c99 - BOARD_PCA10028 NRF51 BLE_STACK_SUPPORT_REQD S110 SOFTDEVICE_PRESENT RBC_MESH_SERIAL + BOARD_PCA10028 NRF51 BLE_STACK_SUPPORT_REQD S110 SOFTDEVICE_PRESENT RBC_MESH_SERIAL DEBUG ..\include;..\..\..\rbc_mesh;..\..\..\rbc_mesh\include;..\..\..\SDK\bsp;..\..\..\..\..\..\components\softdevice\s110\headers;..\..\..\..\..\..\components\toolchain\gcc;..\..\..\..\..\..\components\libraries\util;..\..\..\..\..\..\components\ble\common;..\..\..\..\..\..\components\drivers_nrf\hal;..\..\..\..\..\..\components\drivers_nrf\spi_slave;..\..\..\..\..\..\components\softdevice\common\softdevice_handler @@ -1188,6 +1226,16 @@ 1 ..\..\..\rbc_mesh\src\bootloader_info_app.c + + gpio_config.c + 1 + ..\gpio_config.c + + + simple_uart.c + 1 + ..\..\..\rbc_mesh\src\simple_uart.c + @@ -1231,11 +1279,12 @@ nRF51822 xxAA buttons 0x4 ARM-ADS + 5060020::V5.06 (build 20)::ARMCC nRF51822_xxAC Nordic Semiconductor - NordicSemiconductor.nRF_DeviceFamilyPack.1.1.4 + NordicSemiconductor.nRF_DeviceFamilyPack.8.3.1 http://developer.nordicsemi.com/nRF51_SDK/pieces/nRF_DeviceFamilyPack/ IROM(0x00000000,0x40000) IRAM(0x20000000,0x8000) CPUTYPE("Cortex-M0") CLOCK(12000000) ELITTLE @@ -1305,6 +1354,8 @@ 0 0 + 0 + 0 0 @@ -1448,6 +1499,7 @@ 1 0 0 + 0 3 3 0 @@ -1582,6 +1634,10 @@ 0 0 0 + 0 + 0 + 0 + 0 --c99 BOARD_PCA10028 NRF51 BLE_STACK_SUPPORT_REQD S110 SOFTDEVICE_PRESENT BUTTONS @@ -1722,6 +1778,10 @@ 2 2 2 + 0 + 0 + 0 + 0 @@ -1770,6 +1830,10 @@ 2 2 2 + 0 + 0 + 0 + 0 @@ -1820,6 +1884,16 @@ 1 ..\..\..\rbc_mesh\src\bootloader_info_app.c + + gpio_config.c + 1 + ..\gpio_config.c + + + simple_uart.c + 1 + ..\..\..\rbc_mesh\src\simple_uart.c + @@ -1883,6 +1957,10 @@ 2 2 2 + 0 + 0 + 0 + 0 diff --git a/nRF51/examples/BLE_Gateway/gpio_config.c b/nRF51/examples/BLE_Gateway/gpio_config.c new file mode 100644 index 00000000..f93b7798 --- /dev/null +++ b/nRF51/examples/BLE_Gateway/gpio_config.c @@ -0,0 +1,59 @@ +/*********************************************************************************** +Copyright (c) Nordic Semiconductor ASA +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + + 3. Neither the name of Nordic Semiconductor ASA nor the names of other + contributors to this software may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************************/ + +#include "gpio_config.h" +#include "nrf_soc.h" +/** +* @brief configure LEDs for easily visible status check +*/ +void gpio_config(uint8_t pin, uint8_t conf) +{ +#if defined(BOARD_PCA10001) + if (conf) + { + NRF_GPIO->OUTSET = (1 << pin); + } + else + { + NRF_GPIO->OUTCLR = (1 << pin); + } +#else /* All other boards are the other way around */ + if (conf) + { + //LEDS_ON(BSP_LED_3_MASK); + NRF_GPIO->OUTSET = (1 << pin); + } + else + { + //LEDS_ON(BSP_LED_3_MASK); + NRF_GPIO->OUTCLR = (1 << pin); + } +#endif +} diff --git a/nRF51/examples/BLE_Gateway/include/gpio_config.h b/nRF51/examples/BLE_Gateway/include/gpio_config.h new file mode 100644 index 00000000..12e8646b --- /dev/null +++ b/nRF51/examples/BLE_Gateway/include/gpio_config.h @@ -0,0 +1,40 @@ +/*********************************************************************************** +Copyright (c) Nordic Semiconductor ASA +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + + 3. Neither the name of Nordic Semiconductor ASA nor the names of other + contributors to this software may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************************/ + +#ifndef _GPIO_CONFIG_H__ +#define _GPIO_CONFIG_H__ + +#include "boards.h" +#include + +void gpio_config(uint8_t gpio_pin, uint8_t conf); + + +#endif diff --git a/nRF51/examples/BLE_Gateway/led_config.c b/nRF51/examples/BLE_Gateway/led_config.c index d8915c0b..4a150802 100644 --- a/nRF51/examples/BLE_Gateway/led_config.c +++ b/nRF51/examples/BLE_Gateway/led_config.c @@ -47,13 +47,13 @@ void led_config(uint8_t led, uint8_t conf) #else /* All other boards are the other way around */ if (!conf) { + //LEDS_ON(BSP_LED_3_MASK); NRF_GPIO->OUTSET = (1 << (led + LED_START)); } else { + //LEDS_ON(BSP_LED_3_MASK); NRF_GPIO->OUTCLR = (1 << (led + LED_START)); } #endif -} - - +} diff --git a/nRF51/examples/BLE_Gateway/main.c b/nRF51/examples/BLE_Gateway/main.c index c3b59360..77724436 100644 --- a/nRF51/examples/BLE_Gateway/main.c +++ b/nRF51/examples/BLE_Gateway/main.c @@ -33,6 +33,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "nrf_adv_conn.h" #include "led_config.h" +#include "gpio_config.h" #include "mesh_aci.h" #include "softdevice_handler.h" @@ -44,6 +45,25 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include "simple_uart.h" + +struct __FILE { int handle; /* Add whatever you need here */ }; +FILE __stdout; +FILE __stdin; + +void retarget_init(void) +{ + simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC); +} + +int fputc(int ch, FILE * p_file) +{ + simple_uart_put((uint8_t)ch); + return 0; +} + +int analog_read(int pin_num); + /* Debug macros for debugging with logic analyzer */ #define SET_PIN(x) NRF_GPIO->OUTSET = (1 << (x)) #define CLEAR_PIN(x) NRF_GPIO->OUTCLR = (1 << (x)) @@ -59,6 +79,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ static void error_loop(void) { + #ifdef DEBUG + printf("error"); + #endif led_config(2, 1); led_config(3, 0); while (true) @@ -66,6 +89,8 @@ static void error_loop(void) } } + + /** * @brief Softdevice crash handler, never returns * @@ -75,6 +100,13 @@ static void error_loop(void) */ void sd_assert_handler(uint32_t pc, uint16_t line_num, const uint8_t* p_file_name) { + + #ifdef DEBUG + printf("sd_assert_handler"); + printf("error_code: %u\n", pc); + printf("line_number: %u\n", line_num); + printf("file_name: %s\n", (char *)p_file_name); + #endif error_loop(); } @@ -88,11 +120,26 @@ void sd_assert_handler(uint32_t pc, uint16_t line_num, const uint8_t* p_file_nam */ void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name) { + //FILE *fp; + //fp = fopen("error.log", "w+"); + //fprintf(fp, "error_code: %u\n", error_code); + //fprintf(fp, "line_number: %u\n", line_num); + //fprintf(fp, "file_name: %s\n", (char *)p_file_name); + + #ifdef DEBUG + printf("app_error_handler"); + printf("error_code: %u\n", error_code); + printf("line_number: %u\n", line_num); + printf("file_name: %s\n", (char *)p_file_name); + #endif error_loop(); } void HardFault_Handler(void) { + #ifdef DEBUG + printf("HardFault_Handler"); + #endif error_loop(); } @@ -112,16 +159,31 @@ void sd_ble_evt_handler(ble_evt_t* p_ble_evt) */ static void rbc_mesh_event_handler(rbc_mesh_event_t* evt) { + /*if(evt->event_type == RBC_MESH_EVENT_TYPE_UPDATE_VAL) { + evt->data -= 32; + printf("eve->data addr %p \n", &evt->data); + }*/ + +#ifdef DEBUG + printf("evt->event_type %x \n", evt->event_type); + printf("evt->value_handle %u \n", evt->value_handle); +#endif + TICK_PIN(28); switch (evt->event_type) { case RBC_MESH_EVENT_TYPE_CONFLICTING_VAL: case RBC_MESH_EVENT_TYPE_NEW_VAL: case RBC_MESH_EVENT_TYPE_UPDATE_VAL: - if (evt->value_handle > 1) + if (evt->value_handle > 32) break; - led_config(evt->value_handle, evt->data[0]); + #ifndef RBC_MESH_SERIAL + gpio_config(evt->value_handle, evt->data[0]); + #ifdef DEBUG + printf("gpio_config"); + #endif + #endif break; case RBC_MESH_EVENT_TYPE_TX: break; @@ -129,10 +191,21 @@ static void rbc_mesh_event_handler(rbc_mesh_event_t* evt) /* init BLE gateway softdevice application: */ nrf_adv_conn_init(); break; + case RBC_MESH_EVENT_TYPE_REFRESH_VAL: + if (evt->value_handle > 32) + break; + #ifdef HVAC + if(evt->value_handle == 5 || evt->value_handle == 6){ + uint8_t val = analog_read(evt->value_handle); //get analog value on pin + #ifdef DEBUG + printf("value returned is: %u \n", val); + #endif + rbc_mesh_value_set(evt->value_handle,&val,1); + } + #endif } } - /** * @brief Initialize GPIO pins, for LEDs and debugging */ @@ -146,7 +219,8 @@ void gpio_init(void) } #if defined(BOARD_PCA10001) || defined(BOARD_PCA10028) - nrf_gpio_range_cfg_output(0, 32); + //nrf_gpio_range_cfg_output(0, 32); + nrf_gpio_range_cfg_input(5, 6, NRF_GPIO_PIN_NOPULL); #endif #ifdef BOARD_PCA10028 @@ -160,9 +234,48 @@ void gpio_init(void) } +int analog_read(int pin_num) + +{ + uint16_t adc_result; + + NRF_ADC->INTENSET = (ADC_INTENSET_END_Disabled << ADC_INTENSET_END_Pos); /*!< Interrupt enabled. */ + + // config ADC + //*** pin_num= ADC_CONFIG_PSEL_AnalogInputX-1 + NRF_ADC->CONFIG = (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos) /*!< Analog external reference inputs disabled. */ + | ((pin_num) << ADC_CONFIG_PSEL_Pos) + | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) /*!< Use internal 1.2V bandgap voltage as reference for conversion. */ + | (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) /*!< Analog input specified by PSEL with 1/3 prescaling used as input for the conversion. */ + | (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos); /*!< 10bit ADC resolution. */ + //nrf_gpio_cfg_input(NRF_GPIO_PORT_SELECT_PORT0, GPIO_PIN_CNF_PULL_Disabled); + // enable ADC + NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; /* Bit 0 : ADC enable. */ + + // start ADC conversion + NRF_ADC->TASKS_START = 1; + + // wait for conversion to end + while (!NRF_ADC->EVENTS_END) + {} + NRF_ADC->EVENTS_END = 0; + + //Save your ADC result + adc_result = NRF_ADC->RESULT; + + //Use the STOP task to save current. Workaround for PAN_028 rev1.1 anomaly 1. + NRF_ADC->TASKS_STOP = 1; + + return adc_result; +} + /** @brief main function */ int main(void) { + #ifdef DEBUG + retarget_init(); + printf("Start...\r\n"); + #endif /* init leds and pins */ gpio_init(); NRF_GPIO->OUTSET = (1 << 4); @@ -176,6 +289,26 @@ int main(void) /* only want to enable serial interface, and let external host setup the framework */ mesh_aci_init(); + rbc_mesh_init_params_t init_params; + + init_params.access_addr = MESH_ACCESS_ADDR; + init_params.interval_min_ms = MESH_INTERVAL_MIN_MS; + init_params.channel = MESH_CHANNEL; + init_params.lfclksrc = MESH_CLOCK_SOURCE; + + uint32_t error_code = rbc_mesh_init(init_params); + APP_ERROR_CHECK(error_code); + + /* request values for both LEDs on the mesh */ + for (uint32_t i = 0; i < 32; ++i) + { + error_code = rbc_mesh_value_enable(i); + APP_ERROR_CHECK(error_code); + } + + /* init BLE gateway softdevice application: */ + nrf_adv_conn_init(); + #else rbc_mesh_init_params_t init_params; @@ -189,7 +322,7 @@ int main(void) APP_ERROR_CHECK(error_code); /* request values for both LEDs on the mesh */ - for (uint32_t i = 0; i < 2; ++i) + for (uint32_t i = 0; i < 32; ++i) { error_code = rbc_mesh_value_enable(i); APP_ERROR_CHECK(error_code); @@ -208,9 +341,58 @@ int main(void) { if (rbc_mesh_event_get(&evt) == NRF_SUCCESS) { + #ifdef DEBUG + printf("rbc_mesh_event_get(1): evt popped from g_rbc_event_fifo \n"); + #endif rbc_mesh_event_handler(&evt); rbc_mesh_packet_release(evt.data); + #ifdef DEBUG + printf("after packet_release \n"); + #endif + //#ifdef CUSTOM_BOARD_SERIAL + free(evt.data); + #ifdef DEBUG + printf("freed evt.data \n"); + #endif + memset(&evt, 0, sizeof(evt)); + //printf("rbc_mesh_event_get(1): evt popped from g_rbc_event_fifo \n"); } + //TODO: optional - wrap in ifdef for HVAC board + //update analog values + int analog_val = 0; + //Digital Variable Define + bool flag=0; + //int Dig_in_pin= Dig_PIN_1; + //int Dig_val_saver; + int Dig_out_1; + int Dig_out_2; + + //Variable Define + + //int Ana_output; + static uint8_t gas_read; + static uint8_t temp_read; + //TODO: get GPIO value on pin 5 to analog_val + /*******************Analog Read**********************/ + //C_ana_output_1=int2byte(analog_read(5)); + //C_ana_output_2=int2byte(analog_read(6)); + //gas_read=(uint8_t)analog_read(ADC_CONFIG_PSEL_AnalogInput6); + //temp_read = (uint8_t)analog_read(ADC_CONFIG_PSEL_AnalogInput7); + #ifdef DEBUG + //printf("analog read is: %d \n", gas_read); + //printf("temperature is: %d \n", temp_read); + #endif + + //TODO: convert data value to data + //uint8_t data1; + + // Pins 3 & 4 are digital read + //mesh_update(3, &data, 1); + //mesh_update(4, &data, 1); + + // Pins 5 & 6 are analog read + //mesh_update(5, &gas_read, 1); + //mesh_update(6, &temp_read, 1); sd_app_evt_wait(); } @@ -254,6 +436,9 @@ int main(void) if (rbc_mesh_event_get(&evt) == NRF_SUCCESS) { + #ifdef DEBUG + printf("rbc_mesh_event_get (2): evt popped from g_rbc_event_fifo \n"); + #endif rbc_mesh_event_handler(&evt); rbc_mesh_packet_release(evt.data); } @@ -261,4 +446,3 @@ int main(void) #endif } - diff --git a/nRF51/examples/Template project/arm/rbc_mesh_template.uvoptx b/nRF51/examples/Template project/arm/rbc_mesh_template.uvoptx index 2b441f36..68fce6ff 100644 --- a/nRF51/examples/Template project/arm/rbc_mesh_template.uvoptx +++ b/nRF51/examples/Template project/arm/rbc_mesh_template.uvoptx @@ -13,6 +13,7 @@ *.txt; *.h; *.inc *.plm *.cpp + 0 @@ -31,6 +32,7 @@ 1 0 1 + 0 1 @@ -73,7 +75,7 @@ 1 0 - 0 + 1 5 @@ -82,7 +84,7 @@ 1 1 1 - 1 + 0 1 1 1 @@ -112,6 +114,11 @@ Segger\JL2CM3.dll + + 0 + JL2CM3 + -U269306027 -O78 -S2 -ZTIFSpeedSel5000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(0BB11477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO7 -FD20000000 -FC1000 -FN1 -FF0nrf51xxx.flm -FS00 -FL0200000 -FP0($$Device:nRF51822_xxAA$Flash\nrf51xxx.flm) + 0 UL2CM3 @@ -150,6 +157,7 @@ + 0 @@ -164,6 +172,7 @@ 1 0 1 + 0 1 @@ -206,7 +215,7 @@ 1 0 - 1 + 0 5 @@ -245,6 +254,11 @@ Segger\JL2CM3.dll + + 0 + JL2CM3 + -U681083609 -O78 -S2 -ZTIFSpeedSel5000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST0 -N00("ARM CoreSight SW-DP") -D00(0BB11477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO7 -FD20000000 -FC1000 -FN1 -FF0nrf51xxx.flm -FS00 -FL0200000 -FP0($$Device:nRF51822_xxAA$Flash\nrf51xxx.flm) + 0 UL2CM3 @@ -283,6 +297,7 @@ + 0 @@ -305,6 +320,19 @@ 0 0 + + 1 + 2 + 1 + 0 + 0 + 0 + 0 + ..\nrf_adv_conn.c + nrf_adv_conn.c + 0 + 0 + @@ -315,7 +343,7 @@ 0 2 - 2 + 3 1 0 0 @@ -328,7 +356,7 @@ 2 - 3 + 4 1 0 0 @@ -341,7 +369,7 @@ 2 - 4 + 5 1 0 0 @@ -354,7 +382,7 @@ 2 - 5 + 6 1 0 0 @@ -367,7 +395,7 @@ 2 - 6 + 7 1 0 0 @@ -380,7 +408,7 @@ 2 - 7 + 8 1 0 0 @@ -393,7 +421,7 @@ 2 - 8 + 9 1 0 0 @@ -406,7 +434,7 @@ 2 - 9 + 10 1 0 0 @@ -419,7 +447,7 @@ 2 - 10 + 11 1 0 0 @@ -432,7 +460,7 @@ 2 - 11 + 12 1 0 0 @@ -445,7 +473,7 @@ 2 - 12 + 13 1 0 0 @@ -458,7 +486,7 @@ 2 - 13 + 14 1 0 0 @@ -471,7 +499,7 @@ 2 - 14 + 15 1 0 0 @@ -484,7 +512,7 @@ 2 - 15 + 16 1 0 0 @@ -497,7 +525,7 @@ 2 - 16 + 17 1 0 0 @@ -510,7 +538,7 @@ 2 - 17 + 18 1 0 0 @@ -523,7 +551,7 @@ 2 - 18 + 19 1 0 0 @@ -534,6 +562,19 @@ 0 0 + + 2 + 20 + 1 + 0 + 0 + 0 + 0 + ..\..\..\rbc_mesh\src\simple_uart.c + simple_uart.c + 0 + 0 + @@ -544,7 +585,7 @@ 0 3 - 19 + 21 1 0 0 @@ -557,7 +598,7 @@ 3 - 20 + 22 2 0 0 @@ -578,7 +619,7 @@ 0 4 - 21 + 23 1 0 0 @@ -591,7 +632,7 @@ 4 - 22 + 24 1 0 0 @@ -602,6 +643,19 @@ 0 0 + + 4 + 25 + 1 + 0 + 0 + 0 + 0 + ..\..\..\..\..\..\components\ble\common\ble_advdata.c + ble_advdata.c + 0 + 0 + diff --git a/nRF51/examples/Template project/arm/rbc_mesh_template.uvprojx b/nRF51/examples/Template project/arm/rbc_mesh_template.uvprojx index 7eea3850..ef69e365 100644 --- a/nRF51/examples/Template project/arm/rbc_mesh_template.uvprojx +++ b/nRF51/examples/Template project/arm/rbc_mesh_template.uvprojx @@ -10,11 +10,12 @@ nRF51822 xxAA 0x4 ARM-ADS + 5060020::V5.06 (build 20)::ARMCC nRF51822_xxAA Nordic Semiconductor - NordicSemiconductor.nRF_DeviceFamilyPack.1.1.4 + NordicSemiconductor.nRF_DeviceFamilyPack.8.3.1 http://developer.nordicsemi.com/nRF51_SDK/pieces/nRF_DeviceFamilyPack/ IROM(0x00000000,0x40000) IRAM(0x20000000,0x4000) CPUTYPE("Cortex-M0") CLOCK(12000000) ELITTLE @@ -84,6 +85,8 @@ 0 0 + 0 + 0 0 @@ -127,7 +130,7 @@ 1 1 1 - 1 + 0 1 1 1 @@ -227,6 +230,7 @@ 1 0 0 + 0 3 3 0 @@ -361,9 +365,13 @@ 0 0 0 + 0 + 0 + 0 + 0 --c99 - BOARD_PCA10028 NRF51 BLE_STACK_SUPPORT_REQD S110 SOFTDEVICE_PRESENT + BOARD_PCA10028 NRF51 BLE_STACK_SUPPORT_REQD S110 SOFTDEVICE_PRESENT SMART_VALVE ..\include;..\..\..\rbc_mesh;..\..\..\rbc_mesh\include;..\..\..\SDK\bsp;..\..\..\..\..\..\components\softdevice\s110\headers;..\..\..\..\..\..\components\toolchain\gcc;..\..\..\..\..\..\components\libraries\util;..\..\..\..\..\..\components\ble\common;..\..\..\..\..\..\components\drivers_nrf\hal;..\..\..\..\..\..\components\drivers_nrf\spi_slave;..\..\..\..\..\..\components\softdevice\common\softdevice_handler @@ -413,6 +421,11 @@ 1 ..\main.c + + nrf_adv_conn.c + 1 + ..\nrf_adv_conn.c + @@ -486,6 +499,10 @@ 2 2 2 + 0 + 0 + 0 + 0 @@ -534,6 +551,10 @@ 2 2 2 + 0 + 0 + 0 + 0 @@ -589,6 +610,11 @@ 1 ..\..\..\rbc_mesh\src\rand.c + + simple_uart.c + 1 + ..\..\..\rbc_mesh\src\simple_uart.c + @@ -647,6 +673,10 @@ 2 2 2 + 0 + 0 + 0 + 0 @@ -662,6 +692,11 @@ 1 ..\..\..\..\..\..\components\softdevice\common\softdevice_handler\softdevice_handler.c + + ble_advdata.c + 1 + ..\..\..\..\..\..\components\ble\common\ble_advdata.c + @@ -670,11 +705,12 @@ nRF51822 xxAA serial 0x4 ARM-ADS + 5060020::V5.06 (build 20)::ARMCC nRF51822_xxAA Nordic Semiconductor - NordicSemiconductor.nRF_DeviceFamilyPack.1.1.4 + NordicSemiconductor.nRF_DeviceFamilyPack.8.3.1 http://developer.nordicsemi.com/nRF51_SDK/pieces/nRF_DeviceFamilyPack/ IROM(0x00000000,0x40000) IRAM(0x20000000,0x4000) CPUTYPE("Cortex-M0") CLOCK(12000000) ELITTLE @@ -744,6 +780,8 @@ 0 0 + 0 + 0 0 @@ -887,6 +925,7 @@ 0 0 0 + 0 3 3 0 @@ -1021,6 +1060,10 @@ 0 1 0 + 0 + 0 + 0 + 0 BOARD_PCA10028 NRF51 BLE_STACK_SUPPORT_REQD S110 SOFTDEVICE_PRESENT RBC_MESH_SERIAL @@ -1073,6 +1116,11 @@ 1 ..\main.c + + nrf_adv_conn.c + 1 + ..\nrf_adv_conn.c + @@ -1163,6 +1211,11 @@ 1 ..\..\..\rbc_mesh\src\rand.c + + simple_uart.c + 1 + ..\..\..\rbc_mesh\src\simple_uart.c + @@ -1193,6 +1246,11 @@ 1 ..\..\..\..\..\..\components\softdevice\common\softdevice_handler\softdevice_handler.c + + ble_advdata.c + 1 + ..\..\..\..\..\..\components\ble\common\ble_advdata.c + diff --git a/nRF51/examples/Template project/include/nrf_adv_conn.h b/nRF51/examples/Template project/include/nrf_adv_conn.h new file mode 100644 index 00000000..a2958069 --- /dev/null +++ b/nRF51/examples/Template project/include/nrf_adv_conn.h @@ -0,0 +1,41 @@ +/*********************************************************************************** +Copyright (c) Nordic Semiconductor ASA +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + + 3. Neither the name of Nordic Semiconductor ASA nor the names of other + contributors to this software may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************************/ + +#ifndef __NRF_ADV_CONN_H__ +#define __NRF_ADV_CONN_H__ + +#include + +#include + +void nrf_adv_conn_init(void); +void nrf_adv_conn_evt_handler(ble_evt_t *evt); + +#endif /* __NRF_ADV_CONN_H__ */ diff --git a/nRF51/examples/Template project/main.c b/nRF51/examples/Template project/main.c index 6c95a08c..1e9e1350 100644 --- a/nRF51/examples/Template project/main.c +++ b/nRF51/examples/Template project/main.c @@ -29,6 +29,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ************************************************************************************/ #include "rbc_mesh.h" +#include "bootloader_app.h" +#include "nrf_adv_conn.h" #include "timeslot_handler.h" #include "mesh_aci.h" @@ -36,6 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "app_error.h" #include "nrf_gpio.h" #include "boards.h" +#include "simple_uart.h" #include #include #include @@ -51,48 +54,184 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define MESH_CHANNEL (38) #define MESH_CLOCK_SRC (NRF_CLOCK_LFCLKSRC_XTAL_75_PPM) +int pulse_val; + +//Available pin configurations +typedef enum{ + PIN_UNSET, //pin is not connected to device + PIN_OUTPUT, //pin is set to output + PIN_DINPUT, //pin is set to digital input + PIN_AINPUT, //pin is set to analog input +} pin_config; +//array of pin configurations +pin_config pin_configs[32]; + +struct __FILE { int handle; /* Add whatever you need here */ }; +FILE __stdout; +FILE __stdin; + +int fputc(int ch, FILE * p_file) +{ + simple_uart_put((uint8_t)ch); + return 0; +} + +void retarget_init(void) +{ + simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC); +} + +void sd_ble_evt_handler(ble_evt_t* p_ble_evt) { + rbc_mesh_ble_evt_handler(p_ble_evt); + nrf_adv_conn_evt_handler(p_ble_evt); +} + /** * @brief General error handler. */ static void error_loop(void) { + #ifdef DEBUG + printf("error \n"); + #endif SET_PIN(7); while (true) { __WFE(); } -} +} /** * @brief Softdevice crash handler, never returns -* +* * @param[in] pc Program counter at which the assert failed -* @param[in] line_num Line where the error check failed +* @param[in] line_num Line where the error check failed * @param[in] p_file_name File where the error check failed */ void sd_assert_handler(uint32_t pc, uint16_t line_num, const uint8_t* p_file_name) { + #ifdef DEBUG + printf("sd_assert_handler"); + printf("error_code: %u\n", pc); + printf("line_number: %u\n", line_num); + printf("file_name: %s\n", (char *)p_file_name); + #endif error_loop(); } /** * @brief App error handle callback. Called whenever an APP_ERROR_CHECK() fails. * Never returns. -* +* * @param[in] error_code The error code sent to APP_ERROR_CHECK() -* @param[in] line_num Line where the error check failed +* @param[in] line_num Line where the error check failed * @param[in] p_file_name File where the error check failed */ void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name) { + #ifdef DEBUG + printf("app_error_handler"); + printf("error_code: %u\n", error_code); + printf("line_number: %u\n", line_num); + printf("file_name: %s\n", (char *)p_file_name); + #endif error_loop(); } void HardFault_Handler(void) { + #ifdef DEBUG + printf("HardFault_Handler"); + #endif error_loop(); } +static void set_pin(int pin, int set){ + if(set) SET_PIN(pin); + else CLEAR_PIN(pin); + #ifdef DEBUG + printf("Pin %d set to %d", pin, set); + #endif +} + +int analog_read(int pin_num) + +{ + uint16_t adc_result; + NRF_ADC->INTENSET = (ADC_INTENSET_END_Disabled << ADC_INTENSET_END_Pos); /*!< Interrupt enabled. */ + // config ADC + //*** pin_num= ADC_CONFIG_PSEL_AnalogInputX-1 + NRF_ADC->CONFIG = (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos) /*!< Analog external reference inputs disabled. */ + | ((1 << pin_num) << ADC_CONFIG_PSEL_Pos) + | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) /*!< Use internal 1.2V bandgap voltage as reference for conversion. */ + | (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) /*!< Analog input specified by PSEL with 1/3 prescaling used as input for the conversion. */ + | (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos); /*!< 10bit ADC resolution. */ + //nrf_gpio_cfg_input(NRF_GPIO_PORT_SELECT_PORT0, GPIO_PIN_CNF_PULL_Disabled); + // enable ADC + NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; /* Bit 0 : ADC enable. */ + // start ADC conversion + NRF_ADC->TASKS_START = 1; + // wait for conversion to end + while (!NRF_ADC->EVENTS_END) + {} + NRF_ADC->EVENTS_END = 0; + //Save your ADC result + adc_result = NRF_ADC->RESULT; + //Use the STOP task to save current. Workaround for PAN_028 rev1.1 anomaly 1. + NRF_ADC->TASKS_STOP = 1; + return adc_result; +} + +int Pulsemeasure(int Pin_Num) +{ + + int count=0; + int output=0; + bool flag; + int pin_val= nrf_gpio_pin_read(Pin_Num); + + if(pin_val==1) + { + flag=1; + } + while(flag) + { + pin_val= nrf_gpio_pin_read(Pin_Num); + count++; + if(pin_val!=1) + { + flag=0; + output=count; + count=0; + } + } + + return output; +} + +static void ping_handle(uint16_t handle){ + uint16_t pin = handle - 32; + uint8_t val; + if(pin == 6 || pin == 5){ + val = analog_read(pin); + #ifdef DEBUG + printf("analog value is: %u\n", val); + #endif + } else if (pin ==3){ + #ifdef DEBUG + printf("pulse value \n"); + val = Pulsemeasure(pin); + #endif + } + #ifdef DEBUG + printf("Ping received: Updated pin %d with value = %d \n",pin,val); + #endif + + rbc_mesh_value_set(pin,&val, 1); + uint8_t tmp = 0; + rbc_mesh_value_set(pin+32,&tmp,1); +} + /** * @brief RBC_MESH framework event handler. Defined in rbc_mesh.h. Handles * events coming from the mesh. @@ -101,31 +240,120 @@ void HardFault_Handler(void) */ static void rbc_mesh_event_handler(rbc_mesh_event_t* evt) { + #ifdef DEBUG + printf("rbc_mesh_event_handler\n"); + #endif + + #ifdef RECEIVER + printf("Event type: "); + switch (evt->event_type) { + case RBC_MESH_EVENT_TYPE_CONFLICTING_VAL: + printf("CONFLICTING_VAL\n"); + break; + case RBC_MESH_EVENT_TYPE_NEW_VAL: + printf("NEW_VAL\n"); + break; + case RBC_MESH_EVENT_TYPE_UPDATE_VAL: + printf("UPDATE_VAL\n"); + break; + case RBC_MESH_EVENT_TYPE_TX: + printf("TX\n"); + break; + case RBC_MESH_EVENT_TYPE_INITIALIZED: + printf("INITIALIZED\n"); + break; + } + printf("Handle: %d; ",evt->value_handle); + printf("Data Len: %d\n",evt->data_len); + int i; + printf("Data (hex): "); + for(i = 0; i < evt->data_len; i++){ + printf("%x, ",evt->data[i]); + } + printf("\n\n"); + #endif + switch (evt->event_type) { - case RBC_MESH_EVENT_TYPE_CONFLICTING_VAL: + case RBC_MESH_EVENT_TYPE_CONFLICTING_VAL: case RBC_MESH_EVENT_TYPE_NEW_VAL: case RBC_MESH_EVENT_TYPE_UPDATE_VAL: + if (evt->value_handle >= 32){ + if(evt->value_handle < 64 && evt->data[0] == 1) { //else do nothing + #ifndef RBC_MESH_SERIAL + if(pin_configs[evt->value_handle - 32] == PIN_AINPUT || pin_configs[evt->value_handle - 32] == PIN_DINPUT) + ping_handle(evt->value_handle); + #endif + } + } else{ + if(pin_configs[evt->value_handle] == PIN_OUTPUT) + set_pin(evt->value_handle, evt->data[0]); + } + break; case RBC_MESH_EVENT_TYPE_TX: case RBC_MESH_EVENT_TYPE_INITIALIZED: - break; + break; } } +static void pin_init(int pin, pin_config cfg){ + pin_configs[pin] = cfg; + switch (cfg){ + case PIN_UNSET: + break; + case PIN_OUTPUT: + nrf_gpio_cfg_output(pin); + break; + case PIN_AINPUT: + nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL); + break; + case PIN_DINPUT: + nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_PULLDOWN); + } +} + + /** @brief main function */ int main(void) { + #ifdef DEBUG + retarget_init(); + printf("Start...\r\n"); + #endif + int i; + for(i = 0; i < 32; i++) pin_configs[i] = PIN_UNSET; + + /* init each device */ + #ifdef RBC_MESH_SERIAL + mesh_aci_init(); + #endif + #ifdef SMART_HVAC + pin_init(5,PIN_AINPUT); + pin_init(6,PIN_AINPUT); + pin_init(17,PIN_OUTPUT); + pin_init(18,PIN_OUTPUT); + pin_init(19,PIN_OUTPUT); + pin_init(20,PIN_OUTPUT); + #endif + #ifdef SMART_SWITCH + pin_init(21,PIN_OUTPUT); + pin_init(22,PIN_OUTPUT); + pin_init(23,PIN_OUTPUT); + pin_init(24,PIN_OUTPUT); + #endif + #ifdef SMART_VALVE + pin_init(30,PIN_OUTPUT); + pin_init(3, PIN_DINPUT); + #endif + + /* Enable Softdevice (including sd_ble before framework */ SOFTDEVICE_HANDLER_INIT(MESH_CLOCK_SRC, NULL); - softdevice_ble_evt_handler_set(rbc_mesh_ble_evt_handler); - -#ifdef RBC_MESH_SERIAL - - /* only want to enable serial interface, and let external host setup the framework */ - mesh_aci_init(); - -#else + softdevice_ble_evt_handler_set(sd_ble_evt_handler); + softdevice_sys_evt_handler_set(rbc_mesh_sd_evt_handler); + + rbc_mesh_init_params_t init_params; init_params.access_addr = MESH_ACCESS_ADDR; @@ -136,23 +364,37 @@ int main(void) uint32_t error_code; error_code = rbc_mesh_init(init_params); APP_ERROR_CHECK(error_code); - + error_code = rbc_mesh_value_enable(1); APP_ERROR_CHECK(error_code); error_code = rbc_mesh_value_enable(2); APP_ERROR_CHECK(error_code); -#endif - + + for (uint32_t i = 0; i < 64; ++i) + { + error_code = rbc_mesh_value_enable(i); + APP_ERROR_CHECK(error_code); + } + rbc_mesh_event_t evt; while (true) { + #ifdef SMART_VALVE + pulse_val = Pulsemeasure(3); + #endif + + #ifdef DEBUG + printf("pulse_val: %d\n", pulse_val); + #endif + if (rbc_mesh_event_get(&evt) == NRF_SUCCESS) { rbc_mesh_event_handler(&evt); rbc_mesh_packet_release(evt.data); + free(evt.data); + memset(&evt,0,sizeof(evt)); } - + sd_app_evt_wait(); } } - diff --git a/nRF51/examples/Template project/nrf_adv_conn.c b/nRF51/examples/Template project/nrf_adv_conn.c new file mode 100644 index 00000000..b8b2b0e4 --- /dev/null +++ b/nRF51/examples/Template project/nrf_adv_conn.c @@ -0,0 +1,192 @@ +/*********************************************************************************** +Copyright (c) Nordic Semiconductor ASA +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + + 3. Neither the name of Nordic Semiconductor ASA nor the names of other + contributors to this software may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************************/ + +#include "nrf_adv_conn.h" + +#include "rbc_mesh.h" + +#include "ble.h" +#include "ble_advdata.h" +#include "nrf_assert.h" +#include "nrf_gpio.h" +#include "app_error.h" + +#include +#include +#include + +#define BLE_ADV_INTERVAL_750MS (800) +#define BLE_ADV_INTERVAL_200MS (320) + +/***************************************************************************** +* Static Globals +*****************************************************************************/ + +/* Advertisement data */ +//static uint8_t ble_adv_man_data[] = {0x01 /* PDU_ID */, 0xA0, 0xA1, 0xA2, 0xA3}; + +/* BLE advertisement parameters */ +static ble_gap_adv_params_t ble_adv_params = { + BLE_GAP_ADV_TYPE_ADV_IND, /* Use ADV_IND advertisements */ + NULL, /* Not used for this type of advertisement */ + BLE_GAP_ADV_FP_ANY, /* Don't filter */ + NULL, /* Whitelist not in use */ + BLE_ADV_INTERVAL_200MS, /* Advertising interval set to intentionally disrupt the timeslot example */ + 0, /* Timeout in seconds */ + { + 0, 0, 0 + } +}; + +static ble_advdata_t ble_adv_data; +static ble_gap_sec_params_t ble_gap_bond_params = { + .bond = 0, /* Don't perform bonding */ + .mitm = 0, /* Man-in-the-middle protection not required */ + .io_caps = BLE_GAP_IO_CAPS_NONE, /* No I/O capabilities */ + .oob = 0, /* Out-of-band data not available */ + .min_key_size = 7, /* Minimum encryption key size */ + .max_key_size = 16 /* Maximum encryption key size */ +}; + +/***************************************************************************** +* Static Functions +*****************************************************************************/ + +static void ble_gatts_event_handler(ble_evt_t* evt) +{ + switch (evt->header.evt_id) + { + case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: + break; + + case BLE_GATTS_EVT_SYS_ATTR_MISSING: + sd_ble_gatts_sys_attr_set(evt->evt.gatts_evt.conn_handle, NULL, 0, 0); + break; + + case BLE_GATTS_EVT_WRITE: + break; + + default: + break; + } +} + +static void ble_gap_event_handler(ble_evt_t* evt) +{ + switch (evt->header.evt_id) + { + case BLE_GAP_EVT_CONNECTED: + break; + + case BLE_GAP_EVT_DISCONNECTED: + sd_ble_gap_adv_start(&ble_adv_params); + break; + + case BLE_GAP_EVT_SEC_PARAMS_REQUEST: + APP_ERROR_CHECK(sd_ble_gap_sec_params_reply(evt->evt.gap_evt.conn_handle, + BLE_GAP_SEC_STATUS_SUCCESS, &ble_gap_bond_params, NULL)); + break; + + case BLE_GAP_EVT_CONN_SEC_UPDATE: + break; + + case BLE_GAP_EVT_AUTH_STATUS: + break; + + default: + break; + } +} + +/***************************************************************************** +* Interface Functions +*****************************************************************************/ + +void nrf_adv_conn_init(void) +{ + uint32_t error_code; + ble_enable_params_t ble_enable; + ble_enable.gatts_enable_params.attr_tab_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT; + ble_enable.gatts_enable_params.service_changed = 0; + error_code = sd_ble_enable(&ble_enable); + if (error_code != NRF_SUCCESS && + error_code != NRF_ERROR_INVALID_STATE) + { + APP_ERROR_CHECK(error_code); + } + /* Fill advertisement data struct: */ + uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED | + BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE; + + memset(&ble_adv_data, 0, sizeof(ble_adv_data)); + + ble_adv_data.flags = flags; + ble_adv_data.name_type = BLE_ADVDATA_FULL_NAME; + //ble_adv_data.p_manuf_specific_data = &man_data; + + ble_gap_conn_sec_mode_t name_sec_mode = {1, 1}; + ble_gap_addr_t my_addr; + + error_code = sd_ble_gap_address_get(&my_addr); + APP_ERROR_CHECK(error_code); + + char name[64]; + sprintf(name, "rbc_mesh #%d", + ((uint16_t) my_addr.addr[4] << 8) | (my_addr.addr[5])); + + error_code = sd_ble_gap_device_name_set(&name_sec_mode, (uint8_t*) name, strlen(name)); + APP_ERROR_CHECK(error_code); + + /* Set advertisement data with ble_advdata-lib */ + error_code = ble_advdata_set(&ble_adv_data, NULL); + APP_ERROR_CHECK(error_code); + + /* Start advertising */ + error_code = sd_ble_gap_adv_start(&ble_adv_params); + APP_ERROR_CHECK(error_code); +} + + +void nrf_adv_conn_evt_handler(ble_evt_t* evt) +{ + switch (evt->header.evt_id & 0xF0) + { + case BLE_GAP_EVT_BASE: + ble_gap_event_handler(evt); + break; + + case BLE_GATTS_EVT_BASE: + ble_gatts_event_handler(evt); + break; + + default: + break; + } +} diff --git a/nRF51/rbc_mesh/include/gpio_config.h b/nRF51/rbc_mesh/include/gpio_config.h new file mode 100644 index 00000000..12e8646b --- /dev/null +++ b/nRF51/rbc_mesh/include/gpio_config.h @@ -0,0 +1,40 @@ +/*********************************************************************************** +Copyright (c) Nordic Semiconductor ASA +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + + 3. Neither the name of Nordic Semiconductor ASA nor the names of other + contributors to this software may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************************/ + +#ifndef _GPIO_CONFIG_H__ +#define _GPIO_CONFIG_H__ + +#include "boards.h" +#include + +void gpio_config(uint8_t gpio_pin, uint8_t conf); + + +#endif diff --git a/nRF51/rbc_mesh/include/serial_command.h b/nRF51/rbc_mesh/include/serial_command.h index 4d8a4eba..26af5ce0 100644 --- a/nRF51/rbc_mesh/include/serial_command.h +++ b/nRF51/rbc_mesh/include/serial_command.h @@ -40,7 +40,7 @@ typedef __packed_armcc enum { SERIAL_CMD_OPCODE_ECHO = 0x02, SERIAL_CMD_OPCODE_RADIO_RESET = 0x0E, - + SERIAL_CMD_OPCODE_INIT = 0x70, SERIAL_CMD_OPCODE_VALUE_SET = 0x71, SERIAL_CMD_OPCODE_VALUE_ENABLE = 0x72, @@ -55,70 +55,77 @@ typedef __packed_armcc enum SERIAL_CMD_OPCODE_BUILD_VERSION_GET = 0x7B, SERIAL_CMD_OPCODE_ACCESS_ADDR_GET = 0x7C, SERIAL_CMD_OPCODE_CHANNEL_GET = 0x7D, - SERIAL_CMD_OPCODE_INTERVAL_GET = 0x7F, + SERIAL_CMD_OPCODE_INTERVAL_GET = 0x7F, + + SERIAL_CMD_OPCODE_VALUE_REFRESH = 0x6F, } __packed_gcc serial_cmd_opcode_t; /****** CMD PARAMS ******/ -typedef __packed_armcc struct +typedef __packed_armcc struct { uint8_t data[29]; } __packed_gcc serial_cmd_params_echo_t; -typedef __packed_armcc struct +typedef __packed_armcc struct { uint32_t access_addr; uint32_t interval_min; uint8_t channel; } __packed_gcc serial_cmd_params_init_t; -typedef __packed_armcc struct +typedef __packed_armcc struct { rbc_mesh_value_handle_t handle; - uint8_t flag; + uint8_t flag; uint8_t value; } __packed_gcc serial_cmd_params_flag_set_t; -typedef __packed_armcc struct +typedef __packed_armcc struct { rbc_mesh_value_handle_t handle; - uint8_t flag; + uint8_t flag; } __packed_gcc serial_cmd_params_flag_get_t; -typedef __packed_armcc struct +typedef __packed_armcc struct { rbc_mesh_value_handle_t handle; uint8_t value[RBC_MESH_VALUE_MAX_LEN]; } __packed_gcc serial_cmd_params_value_set_t; -typedef __packed_armcc struct +typedef __packed_armcc struct { rbc_mesh_value_handle_t handle; } __packed_gcc serial_cmd_params_value_enable_t; -typedef __packed_armcc struct +typedef __packed_armcc struct { rbc_mesh_value_handle_t handle; } __packed_gcc serial_cmd_params_value_disable_t; -typedef __packed_armcc struct +typedef __packed_armcc struct { rbc_mesh_value_handle_t handle; } __packed_gcc serial_cmd_params_value_get_t; -typedef __packed_armcc struct +typedef __packed_armcc struct { dfu_packet_t packet; } __packed_gcc serial_cmd_params_dfu_t; +typedef __packed_armcc struct +{ + uint16_t handle; +} __packed_gcc serial_cmd_params_value_refresh_t; + -typedef __packed_armcc struct +typedef __packed_armcc struct { uint8_t length; serial_cmd_opcode_t opcode; - __packed_armcc union + __packed_armcc union { serial_cmd_params_echo_t echo; serial_cmd_params_init_t init; @@ -129,6 +136,7 @@ typedef __packed_armcc struct serial_cmd_params_value_disable_t value_disable; serial_cmd_params_value_get_t value_get; serial_cmd_params_dfu_t dfu; + serial_cmd_params_value_refresh_t value_refresh; } __packed_gcc params; } __packed_gcc serial_cmd_t; diff --git a/nRF51/rbc_mesh/include/serial_evt.h b/nRF51/rbc_mesh/include/serial_evt.h index b82de48f..8a6c1b4b 100644 --- a/nRF51/rbc_mesh/include/serial_evt.h +++ b/nRF51/rbc_mesh/include/serial_evt.h @@ -92,6 +92,7 @@ typedef __packed_armcc struct { rbc_mesh_value_handle_t handle; uint8_t data[RBC_MESH_VALUE_MAX_LEN]; + //uint8_t data; } __packed_gcc serial_evt_cmd_rsp_params_val_get_t; typedef __packed_armcc struct diff --git a/nRF51/rbc_mesh/include/simple_uart.h b/nRF51/rbc_mesh/include/simple_uart.h new file mode 100644 index 00000000..d64dd4c5 --- /dev/null +++ b/nRF51/rbc_mesh/include/simple_uart.h @@ -0,0 +1,72 @@ +/* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved. + * + * The information contained herein is property of Nordic Semiconductor ASA. + * Terms and conditions of usage are described in detail in NORDIC + * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. + * + * Licensees are granted free, non-transferable use of the information. NO + * WARRANTY of ANY KIND is provided. This heading must NOT be removed from + * the file. + * + */ + +#ifndef SIMPLE_UART_H +#define SIMPLE_UART_H + +/*lint ++flb "Enter library region" */ + +#include +#include + +/** @file +* @brief Simple UART driver +* +* +* @defgroup nrf_drivers_simple_uart Simple UART driver +* @{ +* @ingroup nrf_drivers +* @brief Simple UART driver +*/ + +/** @brief Function for reading a character from UART. +Execution is blocked until UART peripheral detects character has been received. +\return cr Received character. +*/ +uint8_t simple_uart_get(void); + +/** @brief Function for reading a character from UART with timeout on how long to wait for the byte to be received. +Execution is blocked until UART peripheral detects character has been received or until the timeout expires, which even occurs first +\return bool True, if byte is received before timeout, else returns False. +@param timeout_ms maximum time to wait for the data. +@param rx_data pointer to the memory where the received data is stored. +*/ +bool simple_uart_get_with_timeout(int32_t timeout_ms, uint8_t *rx_data); + +/** @brief Function for sending a character to UART. +Execution is blocked until UART peripheral reports character to have been send. +@param cr Character to send. +*/ +void simple_uart_put(uint8_t cr); + +/** @brief Function for sending a string to UART. +Execution is blocked until UART peripheral reports all characters to have been send. +Maximum string length is 254 characters including null character in the end. +@param str Null terminated string to send. +*/ +void simple_uart_putstring(const uint8_t *str); + +/** @brief Function for configuring UART to use 38400 baud rate. +@param rts_pin_number Chip pin number to be used for UART RTS +@param txd_pin_number Chip pin number to be used for UART TXD +@param cts_pin_number Chip pin number to be used for UART CTS +@param rxd_pin_number Chip pin number to be used for UART RXD +@param hwfc Enable hardware flow control +*/ +void simple_uart_config(uint8_t rts_pin_number, uint8_t txd_pin_number, uint8_t cts_pin_number, uint8_t rxd_pin_number, bool hwfc); + +/** + *@} + **/ + +/*lint --flb "Leave library region" */ +#endif //SIMPLE_UART_H diff --git a/nRF51/rbc_mesh/include/version_handler.h b/nRF51/rbc_mesh/include/version_handler.h index d9ffdb13..dd50b035 100644 --- a/nRF51/rbc_mesh/include/version_handler.h +++ b/nRF51/rbc_mesh/include/version_handler.h @@ -54,12 +54,14 @@ vh_data_status_t vh_rx_register(mesh_adv_data_t* p_adv_data, uint64_t timestamp) vh_data_status_t vh_local_update(rbc_mesh_value_handle_t handle, uint8_t* data, uint8_t length); + uint32_t vh_on_timeslot_begin(void); uint32_t vh_order_update(uint64_t time_now); /** @brief: Make copy of payload for given handle. */ uint32_t vh_value_get(rbc_mesh_value_handle_t handle, uint8_t* data, uint16_t* length); +//uint32_t vh_value_get(rbc_mesh_value_handle_t handle, uint8_t* data, uint16_t* length); uint32_t vh_tx_event_set(rbc_mesh_value_handle_t handle, bool do_tx_event); @@ -78,4 +80,3 @@ uint32_t vh_value_persistence_set(rbc_mesh_value_handle_t handle, bool persisten uint32_t vh_value_persistence_get(rbc_mesh_value_handle_t handle, bool* p_persistent); #endif /* _VERSION_HANDLER_H__ */ - diff --git a/nRF51/rbc_mesh/rbc_mesh.h b/nRF51/rbc_mesh/rbc_mesh.h index 967e213a..10add161 100644 --- a/nRF51/rbc_mesh/rbc_mesh.h +++ b/nRF51/rbc_mesh/rbc_mesh.h @@ -107,7 +107,8 @@ typedef enum RBC_MESH_EVENT_TYPE_CONFLICTING_VAL, /** Another node has a conflicting version of the value */ RBC_MESH_EVENT_TYPE_NEW_VAL, /** A previously unallocated value has been received and allocated */ RBC_MESH_EVENT_TYPE_INITIALIZED, /** The framework has been initialized internally (most likely via serial interface) */ - RBC_MESH_EVENT_TYPE_TX, /** The indicated handle was transmitted */ + RBC_MESH_EVENT_TYPE_TX, /** The indicated handle was transmitted */ + RBC_MESH_EVENT_TYPE_REFRESH_VAL, /** Another node is requesting value to be updated **/ } rbc_mesh_event_type_t; /** @brief Rebroadcast framework generated event. */ @@ -310,6 +311,10 @@ uint32_t rbc_mesh_value_get(rbc_mesh_value_handle_t handle, uint8_t* data, uint16_t* len); +//uint32_t rbc_mesh_value_get(rbc_mesh_value_handle_t handle, +// uint8_t data, +// uint16_t* len); + /** * @brief Get current mesh access address * @@ -447,6 +452,6 @@ uint32_t rbc_mesh_event_peek(rbc_mesh_event_t* p_evt); * @return NRF_ERROR_INVALID_STATE the framework has not been initialized. */ uint32_t rbc_mesh_packet_release(uint8_t* p_data); +//uint32_t rbc_mesh_packet_release(uint8_t p_data); #endif /* _RBC_MESH_H__ */ - diff --git a/nRF51/rbc_mesh/src/event_handler.c b/nRF51/rbc_mesh/src/event_handler.c index 4d1a2852..a8d94135 100644 --- a/nRF51/rbc_mesh/src/event_handler.c +++ b/nRF51/rbc_mesh/src/event_handler.c @@ -37,6 +37,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "nrf_soc.h" #include "toolchain.h" #include +#include #define EVENT_HANDLER_IRQ (QDEC_IRQn) diff --git a/nRF51/rbc_mesh/src/fifo.c b/nRF51/rbc_mesh/src/fifo.c index 02b0ff7e..56fe81b7 100644 --- a/nRF51/rbc_mesh/src/fifo.c +++ b/nRF51/rbc_mesh/src/fifo.c @@ -31,6 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "rbc_mesh_common.h" #include "nrf_error.h" #include +#include /***************************************************************************** * Static functions @@ -47,6 +48,7 @@ void fifo_init(fifo_t* p_fifo) uint32_t i = 32; while (!((p_fifo->array_len >> --i) & 0x01)); p_fifo->array_len = (1 << i); + //printf("p_fifo->array_len %u \n", p_fifo->array_len); p_fifo->head = 0; p_fifo->tail = 0; diff --git a/nRF51/rbc_mesh/src/gpio_config.c b/nRF51/rbc_mesh/src/gpio_config.c new file mode 100644 index 00000000..8b9c8928 --- /dev/null +++ b/nRF51/rbc_mesh/src/gpio_config.c @@ -0,0 +1,59 @@ +/*********************************************************************************** +Copyright (c) Nordic Semiconductor ASA +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + + 3. Neither the name of Nordic Semiconductor ASA nor the names of other + contributors to this software may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +************************************************************************************/ + +#include "gpio_config.h" +#include "nrf_soc.h" +/** +* @brief configure LEDs for easily visible status check +*/ +void gpio_config(uint8_t pin, uint8_t conf) +{ +#if defined(BOARD_PCA10001) + if (conf) + { + NRF_GPIO->OUTSET = (1 << pin); + } + else + { + NRF_GPIO->OUTCLR = (1 << pin); + } +#else /* All other boards are the other way around */ + if (!conf) + { + //LEDS_ON(BSP_LED_3_MASK); + NRF_GPIO->OUTSET = (1 << pin); + } + else + { + //LEDS_ON(BSP_LED_3_MASK); + NRF_GPIO->OUTCLR = (1 << pin); + } +#endif +} diff --git a/nRF51/rbc_mesh/src/mesh_aci.c b/nRF51/rbc_mesh/src/mesh_aci.c index 020acfae..03df5e6c 100644 --- a/nRF51/rbc_mesh/src/mesh_aci.c +++ b/nRF51/rbc_mesh/src/mesh_aci.c @@ -28,6 +28,8 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ************************************************************************************/ #include +#include +#include #include "mesh_aci.h" #include "rbc_mesh_common.h" @@ -36,6 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "timeslot_handler.h" #include "version.h" #include "mesh_packet.h" +#include "boards.h" #ifdef BOOTLOADER #include "bootloader_mesh.h" @@ -83,6 +86,7 @@ static aci_status_code_t error_code_translate(uint32_t nrf_error_code) */ static void serial_command_handler(serial_cmd_t* serial_cmd) { + //printf("Serial_cmd->opcode %x\n", serial_cmd->opcode); serial_evt_t serial_evt; uint32_t error_code; rbc_mesh_event_t app_evt; @@ -103,7 +107,7 @@ static void serial_command_handler(serial_cmd_t* serial_cmd) case SERIAL_CMD_OPCODE_RADIO_RESET: /* Host gets out of sync if we cut off in the middle of a TX */ serial_wait_for_completion(); - + /* kill ourself :) */ #ifdef SOFTDEVICE_PRESENT sd_power_reset_reason_clr(0x0F000F); @@ -113,7 +117,7 @@ static void serial_command_handler(serial_cmd_t* serial_cmd) NRF_POWER->RESETREAS = 0x0F000F; /* erase reset-reason to avoid wrongful state-readout on reboot */ NRF_POWER->GPREGRET = RBC_MESH_GPREGRET_CODE_FORCED_REBOOT; NVIC_SystemReset(); -#endif +#endif break; #ifndef BOOTLOADER case SERIAL_CMD_OPCODE_INIT: @@ -175,6 +179,7 @@ static void serial_command_handler(serial_cmd_t* serial_cmd) case SERIAL_CMD_OPCODE_VALUE_SET: { + serial_evt.opcode = SERIAL_EVT_OPCODE_CMD_RSP; serial_evt.params.cmd_rsp.command_opcode = serial_cmd->opcode; serial_evt.length = 3; @@ -183,7 +188,7 @@ static void serial_command_handler(serial_cmd_t* serial_cmd) if (mesh_packet_acquire(&p_packet)) { const uint8_t data_len = serial_cmd->length - 1 - sizeof(rbc_mesh_value_handle_t); - + if (serial_cmd->length > sizeof(serial_cmd_params_value_set_t) + 1) { serial_evt.params.cmd_rsp.status = ACI_STATUS_ERROR_INVALID_LENGTH; @@ -202,16 +207,36 @@ static void serial_command_handler(serial_cmd_t* serial_cmd) if (error_code == NRF_SUCCESS) { memcpy(p_packet->payload, serial_cmd->params.value_set.value, data_len); + //printf("Value copied into p_packet->payload: data_len = %u\n", data_len); memset(&app_evt, 0, sizeof(app_evt)); + //printf("Size of app_evt: %d \n", sizeof(app_evt)); + //printf("Size of rbc_mesh_event_t, uint8_t*, ble_gap_addr_t: %d %d %d \n ", sizeof(rbc_mesh_event_type_t), sizeof(uint8_t*), sizeof(ble_gap_addr_t)); app_evt.event_type = RBC_MESH_EVENT_TYPE_UPDATE_VAL; - app_evt.data = serial_cmd->params.value_set.value; + + //memcpy(app_evt.data, serial_cmd->params.value_set.value, data_len); + uint8_t * data_ = (uint8_t*) malloc(sizeof(uint8_t)*RBC_MESH_VALUE_MAX_LEN); + memcpy(data_,serial_cmd->params.value_set.value,RBC_MESH_VALUE_MAX_LEN); + app_evt.data = data_; + #ifdef DEBUG + printf("app_evt.data[0]: %u\n", app_evt.data[0]); + printf("app_evt.data[1]: %u\n", app_evt.data[1]); + printf("app_evt.data[2]: %u\n", app_evt.data[2]); + printf("app_evt.data[3]: %u\n", app_evt.data[3]); + printf("app_evt.data[4]: %u\n", app_evt.data[4]); + printf("app_evt.data %p \n", app_evt.data); + uint8_t* addr = (uint8_t*)0x20005178; + printf("app_evt.data %d \n", *addr); + printf("app_evt.data length %d \n", data_len); + #endif app_evt.data_len = data_len; app_evt.value_handle = serial_cmd->params.value_set.handle; error_code = rbc_mesh_event_push(&app_evt); mesh_packet_ref_count_dec(p_packet); - + //printf("rbc_mesh_event_push (1): app_evt onto g_rbc_event_fifo %x\n", error_code); serial_evt.params.cmd_rsp.status = error_code_translate(error_code); + //LEDS_ON(BSP_LED_1_MASK); + } } else @@ -326,7 +351,58 @@ static void serial_command_handler(serial_cmd_t* serial_cmd) serial_evt.params.cmd_rsp.status = error_code_translate(error_code); } break; + case SERIAL_CMD_OPCODE_VALUE_REFRESH: + #ifdef DEBUG + printf("SERIAL_CMD_OPCODE_VALUE_REFRESH \n"); + #endif + serial_evt.opcode = SERIAL_EVT_OPCODE_CMD_RSP; + serial_evt.params.cmd_rsp.command_opcode = serial_cmd->opcode; + serial_evt.length = 3; + + mesh_packet_t* p_packet; + if (mesh_packet_acquire(&p_packet)) + { + uint8_t* data_ = malloc(1); + const uint8_t data_len = serial_cmd->length - 1 - sizeof(rbc_mesh_value_handle_t); + + if (serial_cmd->length > sizeof(serial_cmd_params_value_set_t) + 1) + { + serial_evt.params.cmd_rsp.status = ACI_STATUS_ERROR_INVALID_LENGTH; + error_code = NRF_ERROR_INVALID_LENGTH; + } + else + { + error_code = rbc_mesh_value_set(serial_cmd->params.value_set.handle, + serial_cmd->params.value_set.value, + data_len); + serial_evt.params.cmd_rsp.status = error_code_translate(error_code); + } + + if (error_code == NRF_SUCCESS) { + //p_packet->payload[0] = &data_; + memcpy(p_packet->payload, data_, 1); + + memset(&app_evt, 0, sizeof(app_evt)); + app_evt.event_type = RBC_MESH_EVENT_TYPE_REFRESH_VAL; + app_evt.value_handle = serial_cmd->params.value_refresh.handle; + app_evt.data = data_; + app_evt.data_len = data_len; + error_code = rbc_mesh_event_push(&app_evt); + #ifdef DEBUG + printf("refresh error code is: %u \n", error_code); + #endif + mesh_packet_ref_count_dec(p_packet); + serial_evt.params.cmd_rsp.status = error_code_translate(error_code); + } + } + else + { + serial_evt.params.cmd_rsp.status = error_code_translate(NRF_ERROR_BUSY); + } + + serial_handler_event_send(&serial_evt); + break; case SERIAL_CMD_OPCODE_VALUE_GET: serial_evt.opcode = SERIAL_EVT_OPCODE_CMD_RSP; serial_evt.params.cmd_rsp.command_opcode = serial_cmd->opcode; @@ -341,6 +417,9 @@ static void serial_command_handler(serial_cmd_t* serial_cmd) uint32_t error_code = rbc_mesh_value_get(serial_cmd->params.value_get.handle, serial_evt.params.cmd_rsp.response.val_get.data, (uint16_t*) &serial_evt.length); + #ifdef DEBUG + printf("val_get.data = %u \n",serial_evt.params.cmd_rsp.response.val_get.data[0]); + #endif serial_evt.params.cmd_rsp.status = error_code_translate(error_code); @@ -496,7 +575,7 @@ static void serial_command_handler(serial_cmd_t* serial_cmd) } } break; - + } } else @@ -555,7 +634,16 @@ void mesh_aci_init(void) void mesh_aci_command_check(void) { + /*static serial_cmd_t* serial_cmd = NULL; + static bool cmd_init = 0; + if (!cmd_init) { + serial_cmd = realloc(serial_cmd, sizeof(serial_cmd_t)); + if (serial_cmd) { + cmd_init = 1; + } + }*/ serial_cmd_t serial_cmd; + /* poll queue */ while (serial_handler_command_get(&serial_cmd)) { @@ -595,7 +683,11 @@ void mesh_aci_rbc_event_handler(rbc_mesh_event_t* evt) /* all event parameter types are the same, just use event_update for all */ serial_evt.params.event_update.handle = evt->value_handle; memcpy(serial_evt.params.event_update.data, evt->data, evt->data_len); + + #ifdef DEBUG + printf("serial_evt.length: %u \n", serial_evt.length); + printf("serial_evt.opcode: %u \n", serial_evt.opcode); + #endif serial_handler_event_send(&serial_evt); } - diff --git a/nRF51/rbc_mesh/src/mesh_gatt.c b/nRF51/rbc_mesh/src/mesh_gatt.c index 442e2550..e29f8aae 100644 --- a/nRF51/rbc_mesh/src/mesh_gatt.c +++ b/nRF51/rbc_mesh/src/mesh_gatt.c @@ -42,6 +42,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "ble_err.h" #include +#include + extern uint32_t rbc_mesh_event_push(rbc_mesh_event_t* p_event); typedef struct @@ -432,6 +434,9 @@ void mesh_gatt_sd_ble_event_handle(ble_evt_t* p_ble_evt) break; case VH_DATA_STATUS_UPDATED: mesh_evt.event_type = RBC_MESH_EVENT_TYPE_UPDATE_VAL; + #ifdef DEBUG + printf("STATUS SET AS RBC_MESH_EVENT_TYPE_UPDATE_VAL \n"); + #endif break; default: mesh_gatt_cmd_rsp_push((mesh_gatt_evt_opcode_t) p_gatt_evt->opcode, MESH_GATT_RESULT_ERROR_BUSY); @@ -571,7 +576,8 @@ uint32_t mesh_gatt_init(uint32_t access_address, uint8_t channel, uint32_t inter return NRF_ERROR_SOFTDEVICE_NOT_ENABLED; } -uint32_t mesh_gatt_value_set(rbc_mesh_value_handle_t handle, uint8_t* data, uint8_t length) +//uint32_t mesh_gatt_value_set(rbc_mesh_value_handle_t handle, uint8_t* data, uint8_t length) +uint32_t mesh_gatt_value_set(rbc_mesh_value_handle_t handle, uint8_t data, uint8_t length) { return NRF_ERROR_SOFTDEVICE_NOT_ENABLED; } diff --git a/nRF51/rbc_mesh/src/mesh_packet.c b/nRF51/rbc_mesh/src/mesh_packet.c index f11f047f..e736e0c2 100644 --- a/nRF51/rbc_mesh/src/mesh_packet.c +++ b/nRF51/rbc_mesh/src/mesh_packet.c @@ -31,6 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mesh_packet.h" #include "app_error.h" #include +#include #define PACKET_INDEX(p_packet) ((((uint32_t) p_packet) - ((uint32_t) &g_packet_pool[0])) / sizeof(mesh_packet_t)) /****************************************************************************** @@ -187,6 +188,7 @@ uint32_t mesh_packet_build(mesh_packet_t* p_packet, if (length > 0 && data != NULL && length <= RBC_MESH_VALUE_MAX_LEN) { memcpy(p_mesh_adv_data->data, data, length); + } return NRF_SUCCESS; @@ -225,13 +227,15 @@ mesh_adv_data_t* mesh_packet_adv_data_get(mesh_packet_t* p_packet) { if (p_packet == NULL) { + #ifdef DEBUG + printf("p_packet == NULL \n"); + #endif return NULL; } mesh_adv_data_t* p_mesh_adv_data = (mesh_adv_data_t*) &p_packet->payload[0]; if (p_packet->header.length <= MESH_PACKET_BLE_OVERHEAD || - p_packet->header.length > MESH_PACKET_BLE_OVERHEAD + BLE_ADV_PACKET_PAYLOAD_MAX_LENGTH) - { + p_packet->header.length > MESH_PACKET_BLE_OVERHEAD + BLE_ADV_PACKET_PAYLOAD_MAX_LENGTH) { return NULL; } @@ -243,10 +247,16 @@ mesh_adv_data_t* mesh_packet_adv_data_get(mesh_packet_t* p_packet) > p_packet->header.length - MESH_PACKET_BLE_OVERHEAD) { /* invalid ad length */ + #ifdef DEBUG + //printf("invalid adv length \n"); + #endif return NULL; } p_mesh_adv_data += p_mesh_adv_data->adv_data_length + 1; /* length field in ad data is not considered */ } + #ifdef DEBUG + //printf("SUCCESS mesh_packet_adv_data_get \n"); + #endif /* The network packet overlaps with AD-data */ return (mesh_adv_data_t*) p_mesh_adv_data; diff --git a/nRF51/rbc_mesh/src/radio_control.c b/nRF51/rbc_mesh/src/radio_control.c index 6beace78..603bfd16 100644 --- a/nRF51/rbc_mesh/src/radio_control.c +++ b/nRF51/rbc_mesh/src/radio_control.c @@ -42,6 +42,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include #define LIGHTWEIGHT_RADIO (1) diff --git a/nRF51/rbc_mesh/src/rbc_mesh.c b/nRF51/rbc_mesh/src/rbc_mesh.c index 1eab75c3..c9e7a93f 100644 --- a/nRF51/rbc_mesh/src/rbc_mesh.c +++ b/nRF51/rbc_mesh/src/rbc_mesh.c @@ -27,6 +27,7 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ************************************************************************************/ +#include #include "rbc_mesh.h" #include "rbc_mesh_common.h" @@ -37,6 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "mesh_packet.h" #include "mesh_gatt.h" #include "fifo.h" +#include "boards.h" #include "nrf_error.h" #include "nrf_sdm.h" @@ -88,35 +90,35 @@ uint32_t rbc_mesh_init(rbc_mesh_init_params_t init_params) return NRF_ERROR_INVALID_PARAM; } - + event_handler_init(); mesh_packet_init(); tc_init(init_params.access_addr, init_params.channel); uint32_t error_code; /* multiply with 1024 instead of 1000 as the number will be easier to set accurately */ - error_code = vh_init(init_params.interval_min_ms * 1024); + error_code = vh_init(init_params.interval_min_ms * 1024); if (error_code != NRF_SUCCESS) { return error_code; } - + ble_enable_params_t ble_enable; ble_enable.gatts_enable_params.attr_tab_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT; ble_enable.gatts_enable_params.service_changed = 0; error_code = sd_ble_enable(&ble_enable); - if (error_code != NRF_SUCCESS && + if (error_code != NRF_SUCCESS && error_code != NRF_ERROR_INVALID_STATE) { return error_code; } - + error_code = mesh_gatt_init(init_params.access_addr, init_params.channel, init_params.interval_min_ms); if (error_code != NRF_SUCCESS) { return error_code; } - + timeslot_handler_init(init_params.lfclksrc); g_access_addr = init_params.access_addr; @@ -124,13 +126,13 @@ uint32_t rbc_mesh_init(rbc_mesh_init_params_t init_params) g_interval_min_ms = init_params.interval_min_ms; g_mesh_state = MESH_STATE_RUNNING; - + g_rbc_event_fifo.array_len = RBC_MESH_APP_EVENT_QUEUE_LENGTH; g_rbc_event_fifo.elem_array = g_rbc_event_buffer; g_rbc_event_fifo.elem_size = sizeof(rbc_mesh_event_t); g_rbc_event_fifo.memcpy_fptr = NULL; fifo_init(&g_rbc_event_fifo); - + return NRF_SUCCESS; } @@ -190,7 +192,7 @@ uint32_t rbc_mesh_persistence_set(rbc_mesh_value_handle_t handle, bool persisten { return NRF_ERROR_INVALID_ADDR; } - + return vh_value_persistence_set(handle, persistent); } @@ -220,16 +222,17 @@ uint32_t rbc_mesh_value_set(rbc_mesh_value_handle_t handle, uint8_t* data, uint1 { return NRF_ERROR_INVALID_ADDR; } - + /* no critical errors if this call fails, ignore return */ mesh_gatt_value_set(handle, data, len); - + if (vh_local_update(handle, data, len) == VH_DATA_STATUS_UNKNOWN) - return NRF_ERROR_INTERNAL; + return NRF_ERROR_INTERNAL; return NRF_SUCCESS; } uint32_t rbc_mesh_value_get(rbc_mesh_value_handle_t handle, uint8_t* data, uint16_t* len) +//uint32_t rbc_mesh_value_get(rbc_mesh_value_handle_t handle, uint8_t data, uint16_t* len) { if (handle > RBC_MESH_APP_MAX_HANDLE) { @@ -306,19 +309,29 @@ void rbc_mesh_sd_evt_handler(uint32_t sd_evt) { ts_sd_event_handler(sd_evt); } - + uint32_t rbc_mesh_event_push(rbc_mesh_event_t* p_event) { if (g_mesh_state == MESH_STATE_UNINITIALIZED) { + //LEDS_ON(BSP_LED_3_MASK); return NRF_ERROR_INVALID_STATE; } uint32_t error_code = fifo_push(&g_rbc_event_fifo, p_event); + #ifdef DEBUG + printf("event_push error code is: %u \n", error_code); + #endif + //printf("p_event pushed onto g_rbc_event_fifo\n"); + if (error_code == NRF_SUCCESS && p_event->data != NULL) { + #ifdef DEBUG + printf("p_event->data != NULL\n"); + #endif mesh_packet_ref_count_inc((mesh_packet_t*) p_event->data); /* will be aligned by packet manager */ + //printf("rbc_mesh: p_event->data AFTER %u \n", p_event->data[0]); } return error_code; } @@ -327,14 +340,21 @@ uint32_t rbc_mesh_event_get(rbc_mesh_event_t* p_evt) { if (g_mesh_state == MESH_STATE_UNINITIALIZED) { + /*#ifdef DEBUG + printf("MESH_STATE_UNINITIALIZED \n"); + #endif*/ return NRF_ERROR_INVALID_STATE; } if (fifo_pop(&g_rbc_event_fifo, p_evt) != NRF_SUCCESS) { + /*#ifdef DEBUG + printf("NRF_ERROR_NOT_FOUND \n"); + #endif*/ return NRF_ERROR_NOT_FOUND; } - - return NRF_SUCCESS; + //printf("fifo_pop p_evt from g_rbc_event_fifo \n"); + + return NRF_SUCCESS; } uint32_t rbc_mesh_event_peek(rbc_mesh_event_t* p_evt) @@ -347,16 +367,17 @@ uint32_t rbc_mesh_event_peek(rbc_mesh_event_t* p_evt) { return NRF_ERROR_NULL; } - + if (fifo_peek(&g_rbc_event_fifo, p_evt) != NRF_SUCCESS) { return NRF_ERROR_NOT_FOUND; } - + return NRF_SUCCESS; } uint32_t rbc_mesh_packet_release(uint8_t* p_data) +//uint32_t rbc_mesh_packet_release(uint8_t p_data) { if (g_mesh_state == MESH_STATE_UNINITIALIZED) { @@ -366,7 +387,6 @@ uint32_t rbc_mesh_packet_release(uint8_t* p_data) { mesh_packet_ref_count_dec((mesh_packet_t*) p_data); } - + return NRF_SUCCESS; } - diff --git a/nRF51/rbc_mesh/src/serial_handler_spi.c b/nRF51/rbc_mesh/src/serial_handler_spi.c index 35c75883..9dc7b626 100644 --- a/nRF51/rbc_mesh/src/serial_handler_spi.c +++ b/nRF51/rbc_mesh/src/serial_handler_spi.c @@ -36,6 +36,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "app_error.h" #include "app_util_platform.h" #include +#include /** @brief Pin configuration for the application */ #define PIN_MISO (28) #define PIN_MOSI (25) @@ -226,6 +227,9 @@ void spi_event_handler(spi_slave_evt_t evt) { if (fifo_push(&rx_fifo, &rx_buffer) == NRF_SUCCESS) { + #ifdef DEBUG + printf("spi_event_handler: rx_buffer pushed to rx_fifo\n"); + #endif /* notify ACI handler */ async_event_t async_evt; @@ -335,11 +339,11 @@ void serial_wait_for_completion(void) _DISABLE_IRQS(was_masked); suspend = true; - extern void SPI0_IRQHandler(void); + extern void SPI1_TWI1_IRQHandler(void); while (serial_state != SERIAL_STATE_IDLE) { - SPI0_IRQHandler(); + SPI1_TWI1_IRQHandler(); } suspend = false; @@ -359,6 +363,12 @@ bool serial_handler_event_send(serial_evt_t* evt) serial_data_t raw_data; raw_data.status_byte = 0; memcpy(raw_data.buffer, evt, evt->length + 1); + //printf("raw_data.buffer %s %u\n", raw_data.buffer, evt->length); + #ifdef DEBUG + for (int i = 0; i < evt->length + 1; i++) { + printf("raw_data.buffer %x \n", raw_data.buffer[i]); + } + #endif fifo_push(&tx_fifo, &raw_data); if (fifo_is_full(&rx_fifo)) @@ -391,6 +401,12 @@ bool serial_handler_command_get(serial_cmd_t* cmd) if (temp.buffer[SERIAL_LENGTH_POS] > 0) { memcpy(cmd, temp.buffer, temp.buffer[SERIAL_LENGTH_POS] + 1); + #ifdef DEBUG + printf("serial_handler_cmd_get: rx_fifo popped into cmd\n"); + for (int i = 0; i < temp.buffer[SERIAL_LENGTH_POS] + 1; i++) { + //printf("temp.buffer %x \n", temp.buffer[i]); + } + #endif } diff --git a/nRF51/rbc_mesh/src/simple_uart.c b/nRF51/rbc_mesh/src/simple_uart.c new file mode 100644 index 00000000..0ef78e4b --- /dev/null +++ b/nRF51/rbc_mesh/src/simple_uart.c @@ -0,0 +1,112 @@ +/* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved. + * + * The information contained herein is property of Nordic Semiconductor ASA. + * Terms and conditions of usage are described in detail in NORDIC + * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. + * + * Licensees are granted free, non-transferable use of the information. NO + * WARRANTY of ANY KIND is provided. This heading must NOT be removed from + * the file. + * + */ + +#include + +#include "nrf.h" +#include "simple_uart.h" +#include "nrf_delay.h" +#include "nrf_gpio.h" + +uint8_t simple_uart_get(void) +{ + while (NRF_UART0->EVENTS_RXDRDY != 1) + { + // Wait for RXD data to be received + } + + NRF_UART0->EVENTS_RXDRDY = 0; + return (uint8_t)NRF_UART0->RXD; +} + + +bool simple_uart_get_with_timeout(int32_t timeout_ms, uint8_t * rx_data) +{ + bool ret = true; + + while (NRF_UART0->EVENTS_RXDRDY != 1) + { + if (timeout_ms-- >= 0) + { + // wait in 1ms chunk before checking for status. + nrf_delay_us(1000); + } + else + { + ret = false; + break; + } + } // Wait for RXD data to be received. + + if (timeout_ms >= 0) + { + // clear the event and set rx_data with received byte. + NRF_UART0->EVENTS_RXDRDY = 0; + *rx_data = (uint8_t)NRF_UART0->RXD; + } + + return ret; +} + +void simple_uart_put(uint8_t cr) +{ + NRF_UART0->TXD = (uint8_t)cr; + + while (NRF_UART0->EVENTS_TXDRDY != 1) + { + // Wait for TXD data to be sent. + } + + NRF_UART0->EVENTS_TXDRDY = 0; +} + + +void simple_uart_putstring(const uint8_t * str) +{ + uint_fast8_t i = 0; + uint8_t ch = str[i++]; + + while (ch != '\0') + { + simple_uart_put(ch); + ch = str[i++]; + } +} + +void simple_uart_config(uint8_t rts_pin_number, + uint8_t txd_pin_number, + uint8_t cts_pin_number, + uint8_t rxd_pin_number, + bool hwfc) +{ +/** @snippet [Configure UART RX and TX pin] */ + nrf_gpio_cfg_output(txd_pin_number); + nrf_gpio_cfg_input(rxd_pin_number, NRF_GPIO_PIN_NOPULL); + + NRF_UART0->PSELTXD = txd_pin_number; + NRF_UART0->PSELRXD = rxd_pin_number; +/** @snippet [Configure UART RX and TX pin] */ + if (hwfc) + { + nrf_gpio_cfg_output(rts_pin_number); + nrf_gpio_cfg_input(cts_pin_number, NRF_GPIO_PIN_NOPULL); + NRF_UART0->PSELCTS = cts_pin_number; + NRF_UART0->PSELRTS = rts_pin_number; + NRF_UART0->CONFIG = (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos); + } + + NRF_UART0->BAUDRATE = (UART_BAUDRATE_BAUDRATE_Baud38400 << UART_BAUDRATE_BAUDRATE_Pos); + NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos); + NRF_UART0->TASKS_STARTTX = 1; + NRF_UART0->TASKS_STARTRX = 1; + NRF_UART0->EVENTS_RXDRDY = 0; +} diff --git a/nRF51/rbc_mesh/src/transport_control.c b/nRF51/rbc_mesh/src/transport_control.c index 73ac072a..06001a78 100644 --- a/nRF51/rbc_mesh/src/transport_control.c +++ b/nRF51/rbc_mesh/src/transport_control.c @@ -27,6 +27,7 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ************************************************************************************/ +#include #include "transport_control.h" @@ -46,6 +47,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "bootloader_app.h" #include +#include /* event push isn't present in the API header file. */ extern uint32_t rbc_mesh_event_push(rbc_mesh_event_t* p_evt); @@ -107,7 +109,9 @@ static void order_search(void) static void prepare_event(rbc_mesh_event_t* evt, mesh_adv_data_t* p_mesh_adv_data, uint8_t rssi, ble_gap_addr_t* p_addr) { evt->value_handle = p_mesh_adv_data->handle; - evt->data = &p_mesh_adv_data->data[0]; + //evt->data = &p_mesh_adv_data->data[0]; + evt->data = (uint8_t *) malloc(p_mesh_adv_data->adv_data_length); + memcpy(evt->data, p_mesh_adv_data->data, p_mesh_adv_data->adv_data_length); evt->data_len = p_mesh_adv_data->adv_data_length - MESH_PACKET_ADV_OVERHEAD; memcpy(&evt->ble_adv_addr, p_addr, sizeof(p_mesh_adv_data)); evt->rssi = -((int8_t) rssi); @@ -221,6 +225,10 @@ static void mesh_app_packet_handle(mesh_adv_data_t* p_mesh_adv_data, break; case VH_DATA_STATUS_UPDATED: + + #ifdef DEBUG + printf("STATUS SET AS RBC_MESH_EVENT_TYPE_UPDATE_VAL \n"); + #endif /* notify application */ prepare_event(&evt, p_mesh_adv_data, rssi, p_addr); diff --git a/nRF51/rbc_mesh/src/version_handler.c b/nRF51/rbc_mesh/src/version_handler.c index c5d1b65a..278b02c5 100644 --- a/nRF51/rbc_mesh/src/version_handler.c +++ b/nRF51/rbc_mesh/src/version_handler.c @@ -27,6 +27,7 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ************************************************************************************/ +#include #include "version_handler.h" @@ -153,7 +154,7 @@ static void data_entry_free(data_entry_t* p_data_entry) static uint16_t data_entry_allocate(void) { static uint16_t allocated = 0; - + for (uint32_t i = allocated; i < RBC_MESH_DATA_CACHE_ENTRIES; ++i) { if (m_data_cache[i].p_packet == NULL) @@ -170,7 +171,7 @@ static uint16_t data_entry_allocate(void) m_handle_cache[handle_index].persistent) { HANDLE_CACHE_ITERATE_BACK(handle_index); - + if (handle_index == HANDLE_CACHE_ENTRY_INVALID) { return DATA_CACHE_ENTRY_INVALID; @@ -179,10 +180,10 @@ static uint16_t data_entry_allocate(void) uint32_t data_index = m_handle_cache[handle_index].data_entry; APP_ERROR_CHECK_BOOL(data_index < RBC_MESH_DATA_CACHE_ENTRIES); - + /* cleanup */ m_handle_cache[handle_index].data_entry = DATA_CACHE_ENTRY_INVALID; - + data_entry_free(&m_data_cache[data_index]); return data_index; } @@ -203,7 +204,7 @@ static uint16_t handle_entry_get(rbc_mesh_value_handle_t handle) } HANDLE_CACHE_ITERATE(i); } - + event_handler_critical_section_end(); return i; } @@ -262,10 +263,10 @@ static uint16_t handle_entry_to_head(rbc_mesh_value_handle_t handle) m_handle_cache[m_handle_cache_head].index_prev = i; m_handle_cache_head = i; } - + m_handle_cache[m_handle_cache_head].index_prev = HANDLE_CACHE_ENTRY_INVALID; m_handle_cache[m_handle_cache_tail].index_next = HANDLE_CACHE_ENTRY_INVALID; - + return i; } @@ -289,7 +290,7 @@ static uint16_t handle_version_set(rbc_mesh_value_handle_t handle, { m_handle_cache[i].version = version; } - + return i; } @@ -330,7 +331,7 @@ static void order_next_transmission(uint64_t time_now) HANDLE_CACHE_ITERATE(handle_index); } while (handle_index != m_handle_cache_tail && handle_index != HANDLE_CACHE_ENTRY_INVALID); - + if (earliest < ts_end_time) { timer_order_cb(TIMER_INDEX_VH, earliest - ts_begin_time, transmit_all_instances); @@ -373,7 +374,7 @@ static void transmit_all_instances(uint64_t timestamp) } data_index++; } - + order_next_transmission(timestamp); } @@ -401,7 +402,7 @@ static void cache_task_handle_enable(rbc_mesh_value_handle_t handle, mesh_packet if (data_index == DATA_CACHE_ENTRY_INVALID) { data_index = data_entry_allocate(); - + APP_ERROR_CHECK_BOOL(data_index != DATA_CACHE_ENTRY_INVALID); m_handle_cache[handle_index].data_entry = data_index; @@ -418,7 +419,7 @@ static void cache_task_handle_enable(rbc_mesh_value_handle_t handle, mesh_packet { m_data_cache[data_index].p_packet = p_packet; } - + trickle_enable(&m_data_cache[data_index].trickle); } @@ -436,16 +437,16 @@ static void cache_task_handle_local_update(rbc_mesh_value_handle_t handle, mesh_ { data_index = data_entry_allocate(); APP_ERROR_CHECK_BOOL(data_index != DATA_CACHE_ENTRY_INVALID); - + m_handle_cache[handle_index].data_entry = data_index; } - + if (m_data_cache[data_index].p_packet != NULL) { mesh_packet_ref_count_dec(m_data_cache[data_index].p_packet); m_data_cache[data_index].p_packet = NULL; } - + /* set the version number in the packet, as we now know it. */ mesh_adv_data_t* p_adv_data = mesh_packet_adv_data_get(p_packet); APP_ERROR_CHECK_BOOL(p_adv_data != NULL); @@ -469,7 +470,7 @@ static void cache_task_handle_local_update(rbc_mesh_value_handle_t handle, mesh_ static void handle_task_queue(void) { m_handle_task_scheduled = false; - + cache_task_t task; while (fifo_pop(&m_task_fifo, &task) == NRF_SUCCESS) { @@ -478,7 +479,7 @@ static void handle_task_queue(void) case CACHE_TASK_TYPE_ENABLE: cache_task_handle_enable(task.handle, task.params.enable.p_packet); break; - + case CACHE_TASK_TYPE_LOCAL_UPDATE: cache_task_handle_local_update(task.handle, task.params.local_update.p_packet); break; @@ -493,7 +494,7 @@ static uint32_t cache_task_push(cache_task_t* p_task) { return error_code; } - + if (true || !m_handle_task_scheduled) { async_event_t evt; @@ -505,7 +506,7 @@ static uint32_t cache_task_push(cache_task_t* p_task) m_handle_task_scheduled = true; /* we may have a double-schedule here, but it doesn't really matter */ } } - + return error_code; } /****************************************************************************** @@ -535,12 +536,12 @@ uint32_t vh_init(uint32_t min_interval_us) m_handle_cache[i].index_prev = i - 1; m_handle_cache[i].index_next = i + 1; } - + m_handle_cache_head = 0; m_handle_cache_tail = RBC_MESH_HANDLE_CACHE_ENTRIES - 1; m_handle_cache[m_handle_cache_head].index_prev = HANDLE_CACHE_ENTRY_INVALID; m_handle_cache[m_handle_cache_tail].index_next = HANDLE_CACHE_ENTRY_INVALID; - + m_task_fifo.array_len = CACHE_TASK_FIFO_SIZE; m_task_fifo.elem_array = m_task_fifo_buffer; m_task_fifo.elem_size = sizeof(cache_task_t); @@ -588,12 +589,12 @@ vh_data_status_t vh_rx_register(mesh_adv_data_t* p_adv_data, uint64_t timestamp) trickle_rx_inconsistent(&m_data_cache[data_index].trickle, ts_start_time + timestamp); vh_order_update(timestamp); } - + return VH_DATA_STATUS_OLD; } else if (delta == 0) { - + /* the incoming packet has the same version as the current */ if (data_index == DATA_CACHE_ENTRY_INVALID) { @@ -610,7 +611,7 @@ vh_data_status_t vh_rx_register(mesh_adv_data_t* p_adv_data, uint64_t timestamp) return VH_DATA_STATUS_CONFLICTING; } else - { + { trickle_rx_consistent(&m_data_cache[data_index].trickle, ts_start_time + timestamp); return VH_DATA_STATUS_SAME; } @@ -640,7 +641,7 @@ vh_data_status_t vh_rx_register(mesh_adv_data_t* p_adv_data, uint64_t timestamp) m_data_cache[data_index].p_packet = p_packet; trickle_rx_inconsistent(&m_data_cache[data_index].trickle, ts_start_time + timestamp); vh_order_update(timestamp); - + if (cache_hit) { return VH_DATA_STATUS_UPDATED; @@ -650,25 +651,25 @@ vh_data_status_t vh_rx_register(mesh_adv_data_t* p_adv_data, uint64_t timestamp) return VH_DATA_STATUS_NEW; } } - + } vh_data_status_t vh_local_update(rbc_mesh_value_handle_t handle, uint8_t* data, uint8_t length) { if (!m_is_initialized) return VH_DATA_STATUS_UNKNOWN; - + cache_task_t task; task.type = CACHE_TASK_TYPE_LOCAL_UPDATE; task.handle = handle; - + uint32_t error_code; - + if (!mesh_packet_acquire(&task.params.local_update.p_packet)) { return VH_DATA_STATUS_UNKNOWN; } - + error_code = mesh_packet_build(task.params.local_update.p_packet, handle, 0, /* !!! TEMPORARY, as we can't know before the task is handled */ @@ -705,19 +706,20 @@ uint32_t vh_order_update(uint64_t time_now) } uint32_t vh_value_get(rbc_mesh_value_handle_t handle, uint8_t* data, uint16_t* length) +//uint32_t vh_value_get(rbc_mesh_value_handle_t handle, uint8_t* data, uint16_t* length) { if (!m_is_initialized) return NRF_ERROR_INVALID_STATE; event_handler_critical_section_begin(); - + uint16_t handle_index = handle_entry_get(handle); if (handle_index == HANDLE_CACHE_ENTRY_INVALID) { event_handler_critical_section_end(); return NRF_ERROR_NOT_FOUND; } - + uint16_t data_index = m_handle_cache[handle_index].data_entry; if (data_index == DATA_CACHE_ENTRY_INVALID || m_data_cache[data_index].p_packet == NULL) { @@ -746,7 +748,7 @@ uint32_t vh_tx_event_set(rbc_mesh_value_handle_t handle, bool do_tx_event) return NRF_ERROR_INVALID_STATE; event_handler_critical_section_begin(); - + uint16_t handle_index = handle_entry_get(handle); if (handle_index == HANDLE_CACHE_ENTRY_INVALID) @@ -772,7 +774,7 @@ uint32_t vh_tx_event_flag_get(rbc_mesh_value_handle_t handle, bool* is_doing_tx_ } event_handler_critical_section_begin(); - + uint16_t handle_index = handle_entry_get(handle); if (handle_index == HANDLE_CACHE_ENTRY_INVALID) { @@ -828,16 +830,16 @@ uint32_t vh_value_enable(rbc_mesh_value_handle_t handle) if (!m_is_initialized) return NRF_ERROR_INVALID_STATE; cache_task_t task; - + task.type = CACHE_TASK_TYPE_ENABLE; task.handle = handle; uint32_t error_code; - + if (!mesh_packet_acquire(&task.params.enable.p_packet)) { return NRF_ERROR_NO_MEM; } - + /* build a dummy packet to request data, as we don't know the contents of the packet */ error_code = mesh_packet_build(task.params.enable.p_packet, handle, @@ -849,15 +851,15 @@ uint32_t vh_value_enable(rbc_mesh_value_handle_t handle) mesh_packet_ref_count_dec(task.params.enable.p_packet); /* free the newly acquired packet */ return NRF_ERROR_INTERNAL; } - + error_code = cache_task_push(&task); - + if (error_code != NRF_SUCCESS) { APP_ERROR_CHECK(error_code); mesh_packet_ref_count_dec(task.params.enable.p_packet); /* free the newly acquired packet */ } - + return error_code; } @@ -867,7 +869,7 @@ uint32_t vh_value_disable(rbc_mesh_value_handle_t handle) return NRF_ERROR_INVALID_STATE; event_handler_critical_section_begin(); - + uint16_t handle_index = handle_entry_get(handle); if (handle_index == HANDLE_CACHE_ENTRY_INVALID) { @@ -897,7 +899,7 @@ uint32_t vh_value_is_enabled(rbc_mesh_value_handle_t handle, bool* p_is_enabled) } event_handler_critical_section_begin(); - + uint16_t handle_index = handle_entry_get(handle); if (handle_index == HANDLE_CACHE_ENTRY_INVALID) { @@ -915,7 +917,7 @@ uint32_t vh_value_is_enabled(rbc_mesh_value_handle_t handle, bool* p_is_enabled) } *p_is_enabled = trickle_is_enabled(&m_data_cache[data_index].trickle); - + event_handler_critical_section_end(); return NRF_SUCCESS; } @@ -924,23 +926,23 @@ uint32_t vh_value_persistence_set(rbc_mesh_value_handle_t handle, bool persisten { if (!m_is_initialized) return NRF_ERROR_INVALID_STATE; - + if (handle == RBC_MESH_INVALID_HANDLE) { return NRF_ERROR_INVALID_ADDR; } - + event_handler_critical_section_begin(); - + uint16_t handle_index = handle_entry_get(handle); if (handle_index == HANDLE_CACHE_ENTRY_INVALID) { event_handler_critical_section_end(); return NRF_ERROR_NOT_FOUND; } - + m_handle_cache[handle_index].persistent = persistent; - + event_handler_critical_section_end(); return NRF_SUCCESS; } @@ -954,9 +956,9 @@ uint32_t vh_value_persistence_get(rbc_mesh_value_handle_t handle, bool* p_persis { return NRF_ERROR_INVALID_ADDR; } - + event_handler_critical_section_begin(); - + uint16_t handle_index = handle_entry_get(handle); if (handle_index == HANDLE_CACHE_ENTRY_INVALID) { @@ -965,8 +967,7 @@ uint32_t vh_value_persistence_get(rbc_mesh_value_handle_t handle, bool* p_persis } *p_persistent = m_handle_cache[handle_index].persistent; - + event_handler_critical_section_end(); return NRF_SUCCESS; } -