Skip to content

Commit

Permalink
#34 Disabling BLE when a Crazyradio packet is received. Note: BLE is …
Browse files Browse the repository at this point in the history
…not enabled again until after reboot
  • Loading branch information
krichardsson committed Feb 13, 2020
1 parent 0ee3b01 commit e11db79
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ endif

ifeq ($(strip $(BLE)), 1)
CFLAGS += -DBLE=1
endif

OBJS += src/ble/ble.o
OBJS += src/ble/ble_crazyflies.o
Expand All @@ -80,7 +81,6 @@ CFLAGS += -I$(NRF_S110)/Include/
CFLAGS += -I$(NRF51_SDK)/Include/app_common/
CFLAGS += -I$(NRF51_SDK)/Include/sd_common/
CFLAGS += -I$(NRF51_SDK)/Include/sdk/
endif

OBJS += src/main.o gcc_startup_nrf51.o system_nrf51.o src/uart.o \
src/syslink.o src/pm.o src/systick.o src/button.o src/swd.o src/ow.o \
Expand Down
3 changes: 0 additions & 3 deletions src/ble/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@

#include "ble_crazyflies.h"


#if BLE
#if S110==0
#error S110 shall be used when using BLE
#endif
#endif

#define DEVICE_NAME "Crazyflie" /**< Name of device. Will be included in the advertising data. */
#define MANUFACTURER_NAME "Bitcraze" /**< Manufacturer. Will be passed to Device Information Service. */
Expand Down
51 changes: 27 additions & 24 deletions src/esb.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@

#include <nrf.h>

#ifdef BLE
#include <ble_gap.h>
#include <nrf_soc.h>
#endif

#define RXQ_LEN 8
#define TXQ_LEN 8

extern int bleEnabled;

static bool isInit = true;

static int channel = 80;
Expand Down Expand Up @@ -312,12 +312,12 @@ void esbInit()
{
NRF_RADIO->POWER = 1;
// Enable Radio interrupts
#ifndef BLE
NVIC_SetPriority(RADIO_IRQn, 3);
NVIC_EnableIRQ(RADIO_IRQn);
#else
NVIC_EnableIRQ(RADIO_IRQn);
#endif
if (!bleEnabled) {
NVIC_SetPriority(RADIO_IRQn, 3);
NVIC_EnableIRQ(RADIO_IRQn);
} else {
NVIC_EnableIRQ(RADIO_IRQn);
}


NRF_RADIO->TXPOWER = (txpower << RADIO_TXPOWER_TXPOWER_Pos);
Expand Down Expand Up @@ -392,24 +392,27 @@ void esbInit()
void esbReset()
{
if (!isInit) return;
#ifndef BLE
__disable_irq();
#endif

if (!bleEnabled) {
__disable_irq();
}

NRF_RADIO->TASKS_DISABLE = 1;
NRF_RADIO->POWER = 0;

NVIC_GetPendingIRQ(RADIO_IRQn);
__enable_irq();
#ifndef BLE
esbInit();
#endif

if (!bleEnabled) {
esbInit();
}
}

void esbDeinit()
{
#ifndef BLE
NVIC_DisableIRQ(RADIO_IRQn);
#endif
if (!bleEnabled) {
NVIC_DisableIRQ(RADIO_IRQn);
}

NRF_RADIO->INTENCLR = RADIO_INTENSET_END_Msk;
NRF_RADIO->SHORTS = 0;
Expand Down Expand Up @@ -496,13 +499,13 @@ void esbSetContwave(bool enable)
{
contwave = enable;

#ifdef BLE
if (enable)
ble_advertising_stop();
else
advertising_start();
#endif

if (bleEnabled) {
if (enable) {
ble_advertising_stop();
} else {
advertising_start();
}
}

esbReset();
}
Expand Down
96 changes: 57 additions & 39 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
*/
#include <nrf.h>

#ifdef BLE
#include <nrf_soc.h>
#endif


#include <stdio.h>
#include <string.h>
Expand All @@ -41,13 +39,14 @@
#include "pm.h"
#include "pinout.h"
#include "systick.h"
#include "nrf_sdm.h"
#include "nrf_soc.h"

#include "memory.h"
#include "ownet.h"

#ifdef BLE
#include "ble_crazyflies.h"
#endif


extern void initialise_monitor_handles(void);
extern int ble_init(void);
Expand All @@ -69,10 +68,17 @@ static void mainloop(void);
#undef BLE
#endif

#ifdef BLE
int bleEnabled = 1;
#else
int bleEnabled = 0;
#endif

static bool boottedFromBootloader;

static void handleRadioCmd(struct esbPacket_s * packet);
static void handleBootloaderCmd(struct esbPacket_s *packet);
static void disableBle();

static int stmStartTime = 0;
int main()
Expand All @@ -86,12 +92,12 @@ int main()
systickInit();
memoryInit();

#ifdef BLE
ble_init();
#else
NRF_CLOCK->TASKS_HFCLKSTART = 1UL;
while(!NRF_CLOCK->EVENTS_HFCLKSTARTED);
#endif
if (bleEnabled) {
ble_init();
} else {
NRF_CLOCK->TASKS_HFCLKSTART = 1UL;
while(!NRF_CLOCK->EVENTS_HFCLKSTARTED);
}

#ifdef SEMIHOSTING
initialise_monitor_handles();
Expand Down Expand Up @@ -125,12 +131,12 @@ int main()

NRF_GPIO->PIN_CNF[RADIO_PAEN_PIN] |= GPIO_PIN_CNF_DIR_Output | (GPIO_PIN_CNF_DRIVE_S0H1<<GPIO_PIN_CNF_DRIVE_Pos);

#ifndef BLE
esbInit();
if (!bleEnabled) {
esbInit();

esbSetDatarate(DEFAULT_RADIO_RATE);
esbSetChannel(DEFAULT_RADIO_CHANNEL);
#endif
esbSetDatarate(DEFAULT_RADIO_RATE);
esbSetChannel(DEFAULT_RADIO_CHANNEL);
}

DEBUG_PRINT("Started\n");

Expand Down Expand Up @@ -159,16 +165,16 @@ void mainloop()
while(1)
{

#ifdef BLE
if ((esbReceived == false) && bleCrazyfliesIsPacketReceived()) {
EsbPacket* packet = bleCrazyfliesGetRxPacket();
memcpy(esbRxPacket.data, packet->data, packet->size);
esbRxPacket.size = packet->size;
esbReceived = true;
bleCrazyfliesReleaseRxPacket(packet);
if (bleEnabled) {
if ((esbReceived == false) && bleCrazyfliesIsPacketReceived()) {
EsbPacket* packet = bleCrazyfliesGetRxPacket();
memcpy(esbRxPacket.data, packet->data, packet->size);
esbRxPacket.size = packet->size;
esbReceived = true;
bleCrazyfliesReleaseRxPacket(packet);
}
}

#endif
#ifndef CONT_WAVE_TEST

if ((esbReceived == false) && esbIsRxPacket())
Expand All @@ -190,6 +196,8 @@ void mainloop()
esbRxPacket.size = packet->size;
esbReceived = true;
esbReleaseRxPacket(packet);

disableBle();
}

if (esbReceived)
Expand Down Expand Up @@ -249,14 +257,15 @@ void mainloop()
}
bzero(slRxPacket.data, SYSLINK_MTU);
}
#ifdef BLE
if (slRxPacket.length < SYSLINK_MTU) {
static EsbPacket pk;
memcpy(pk.data, slRxPacket.data, slRxPacket.length);
pk.size = slRxPacket.length;
bleCrazyfliesSendPacket(&pk);

if (bleEnabled) {
if (slRxPacket.length < SYSLINK_MTU) {
static EsbPacket pk;
memcpy(pk.data, slRxPacket.data, slRxPacket.length);
pk.size = slRxPacket.length;
bleCrazyfliesSendPacket(&pk);
}
}
#endif

break;
case SYSLINK_RADIO_CHANNEL:
Expand Down Expand Up @@ -464,9 +473,10 @@ static void handleBootloaderCmd(struct esbPacket_s *packet)
memcpy(&(txpk.data[3]), (uint32_t*)NRF_FICR->DEVICEADDR, 6);

txpk.size = 9;
#if BLE
bleCrazyfliesSendPacket(&txpk);
#endif
if (bleEnabled) {
bleCrazyfliesSendPacket(&txpk);
}

if (esbCanTxPacket()) {
struct esbPacket_s *pk = esbGetTxPacket();
memcpy(pk, &txpk, sizeof(struct esbPacket_s));
Expand All @@ -483,11 +493,11 @@ static void handleBootloaderCmd(struct esbPacket_s *packet)
//Set bit 0x20 forces boot to firmware
NRF_POWER->GPREGRET |= 0x20U;
}
#ifdef BLE
sd_nvic_SystemReset();
#else
NVIC_SystemReset();
#endif
if (bleEnabled) {
sd_nvic_SystemReset();
} else {
NVIC_SystemReset();
}
}
break;
case BOOTLOADER_CMD_ALLOFF:
Expand Down Expand Up @@ -528,3 +538,11 @@ static void handleBootloaderCmd(struct esbPacket_s *packet)
break;
}
}

static void disableBle() {
if (bleEnabled) {
sd_softdevice_disable();
bleEnabled = 0;
esbInit();
}
}
Loading

0 comments on commit e11db79

Please sign in to comment.