Skip to content

Commit

Permalink
Fixes #3: Allow OW write when BLE is active
Browse files Browse the repository at this point in the history
Disables BLE stack when accessing OW memory
  • Loading branch information
ataffanel committed Jun 25, 2019
1 parent 539ac3c commit 90ac428
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 44 deletions.
5 changes: 5 additions & 0 deletions src/ble/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ int ble_init(void)
timeslot_start();
}

void ble_deinit(void)
{
ble_sd_stop();
}

/**
* @}
*/
56 changes: 12 additions & 44 deletions src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,12 @@
#include "ds2431.h"
#include "ds28e05.h"

extern int ble_init(void);
extern void ble_deinit(void);

static bool isInit;
static int nMemory;

#ifdef BLE
#define OW_MAX_CACHED 4
static struct {unsigned char address[8]; unsigned char data[122];} owCache[OW_MAX_CACHED];

static void cacheMemory(int nMem)
{
if (nMem<OW_MAX_CACHED) {
owSerialNum(0, owCache[nMem].address, 1);
ds28e05ReadMemory(0, 0, owCache[nMem].data, 112);
}
}
#else
static bool selectMemory(int n)
{
if (owFirst(0, 1, 0))
Expand All @@ -69,7 +60,6 @@ static bool selectMemory(int n)

return false;
}
#endif

int owScan()
{
Expand All @@ -80,17 +70,9 @@ int owScan()

if (owFirst(0, 1, 0))
{
#ifdef BLE
cacheMemory(0);
#endif
nMem++;
while(owNext(0, 1, 0))
{
#ifdef BLE
cacheMemory(nMem);
if (nMem>=OW_MAX_CACHED)
break;
#endif
nMem++;
}
}
Expand All @@ -114,31 +96,27 @@ bool memorySyslink(struct syslinkPacket *pk) {
bool tx = false;
struct memoryCommand_s *command = (void*)pk->data;

#ifdef BLE
// Disable BLE stack to have real-time control during OW access
ble_deinit();
#endif

if (isInit == false)
return false;

switch (pk->type) {
case SYSLINK_OW_SCAN:
#ifndef BLE
nMemory = owScan();
#endif
pk->data[0] = nMemory;
pk->length = 1;
tx = true;
break;
case SYSLINK_OW_GETINFO:
#ifdef BLE
if (command->nmem<nMemory) {
memcpy(command->info.memId, owCache[command->nmem].address, 8);
pk->length = 1+8;
tx = true;
#else
if (selectMemory(command->nmem))
{
owSerialNum(0, command->info.memId, 1);
pk->length = 1+8;
tx = true;
#endif
} else {
//Cannot select the memory
pk->data[0] = -1;
Expand All @@ -148,18 +126,11 @@ bool memorySyslink(struct syslinkPacket *pk) {
break;

case SYSLINK_OW_READ:
#ifdef BLE
if (command->nmem<nMemory) {
memcpy(command->read.data, &owCache[command->nmem].data[command->read.address], 29);
pk->length = 32;
tx=true;
#else
if (selectMemory(command->nmem) &&
ds28e05ReadMemory(0, command->read.address, command->read.data, 29))
{
pk->length = 32;
tx=true;
#endif
} else {
//Cannot select the memory
pk->data[0] = -1;
Expand All @@ -169,12 +140,6 @@ bool memorySyslink(struct syslinkPacket *pk) {

break;
case SYSLINK_OW_WRITE:
#ifdef BLE
//Cannot currently write the memory when compiled with BLE
pk->data[0] = -2;
pk->length = 1;
tx=true;
#else
if (selectMemory(command->nmem) &&
ds28e05WriteMemory(0, command->write.address, command->write.data, command->write.length))
{
Expand All @@ -185,9 +150,12 @@ bool memorySyslink(struct syslinkPacket *pk) {
pk->length = 1;
tx=true;
}
#endif
break;
}

#ifdef ble
ble_init();
#endif

return tx;
}

0 comments on commit 90ac428

Please sign in to comment.