Skip to content

Commit

Permalink
Add generic 1-Wire interface, use camel case and clean DS18B20 driver
Browse files Browse the repository at this point in the history
  • Loading branch information
shellixyz committed Feb 7, 2019
1 parent d63ef61 commit 820a852
Show file tree
Hide file tree
Showing 11 changed files with 373 additions and 241 deletions.
19 changes: 12 additions & 7 deletions src/main/drivers/1-wire.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

#include <string.h>

#include "drivers/1-wire.h"
#include "drivers/1-wire/ds2482.h"

Expand All @@ -8,19 +10,22 @@
#ifdef USE_1WIRE

#ifdef USE_1WIRE_DS2482
bool ds2482_detected = false;
_1WireDev_t ds2482Dev;
static bool ds2482Detected = false;
static owDev_t ds2482Dev;
#endif

void _1WireInit(void)
void owInit(void)
{
memset(&ds2482Dev, 0, sizeof(ds2482Dev));
addBootlogEvent2(BOOT_EVENT_TEMP_SENSOR_DETECTION, BOOT_EVENT_FLAGS_NONE);
#ifdef USE_1WIRE_DS2482
if (ds2482Detect(&ds2482Dev)) {
ds2482_detected = true;
ds2482Init(&ds2482Dev);
}
if (ds2482Detect(&ds2482Dev)) ds2482Detected = true;
#endif
}

owDev_t *getOwDev(void)
{
return ds2482Detected ? &ds2482Dev : NULL;
}

#endif /* USE_1WIRE */
52 changes: 45 additions & 7 deletions src/main/drivers/1-wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,53 @@

#ifdef USE_1WIRE

typedef struct _1WireDev_s {
#define OW_TRIPLET_FIRST_BIT(tripletResult) (tripletResult & (1 << 0))
#define OW_TRIPLET_SECOND_BIT(tripletResult) (tripletResult & (1 << 1))
#define OW_TRIPLET_DIRECTION_BIT(tripletResult) (tripletResult & (1 << 2))

#define OW_SINGLE_BIT_WRITE0 0
#define OW_SINGLE_BIT_WRITE1_READ (1<<7)

typedef struct owDev_s {
busDevice_t *busDev;
} _1WireDev_t;

#if defined(USE_1WIRE) && defined(USE_1WIRE_DS2482)
extern bool ds2482_detected;
extern _1WireDev_t ds2482Dev;
#endif
uint8_t status;

bool (*reset)(struct owDev_s *owDev);
bool (*owResetCommand)(struct owDev_s *owDev);
bool (*owReset)(struct owDev_s *owDev);
bool (*waitForBus)(struct owDev_s *owDev);
bool (*readConfig)(struct owDev_s *owDev, uint8_t *config);
bool (*writeConfig)(struct owDev_s *owDev, uint8_t config);
bool (*readStatus)(struct owDev_s *owDev, uint8_t *status);
uint8_t (*getStatus)(struct owDev_s *owDev);
bool (*poll)(struct owDev_s *owDev, bool waitForBus, uint8_t *status);
bool (*owBusReady)(struct owDev_s *owDev);

// 1-Wire ROM
bool (*owSearchRom)(struct owDev_s *owDev, uint8_t familyCode, uint64_t *romTable, uint8_t *romTableLen);
bool (*owMatchRomCommand)(struct owDev_s *owDev);
bool (*owMatchRom)(struct owDev_s *owDev, uint64_t rom);
bool (*owSkipRomCommand)(struct owDev_s *owDev);
bool (*owSkipRom)(struct owDev_s *owDev);

// 1-Wire read/write
bool (*owWriteByteCommand)(struct owDev_s *owDev, uint8_t byte);
bool (*owWriteByte)(struct owDev_s *owDev, uint8_t byte);
bool (*owWriteBuf)(struct owDev_s *owDev, const uint8_t *buf, uint8_t len);
bool (*owReadByteCommand)(struct owDev_s *owDev);
bool (*owReadByteResult)(struct owDev_s *owDev, uint8_t *result);
bool (*owReadByte)(struct owDev_s *owDev, uint8_t *result);
bool (*owReadBuf)(struct owDev_s *owDev, uint8_t *buf, uint8_t len);
bool (*owSingleBitCommand)(struct owDev_s *owDev, uint8_t type);
bool (*owSingleBitResult)(struct owDev_s *owDev);
bool (*owSingleBit)(struct owDev_s *owDev, uint8_t type, bool *result);
bool (*owTripletCommand)(struct owDev_s *owDev, uint8_t direction);
uint8_t (*owTripletResult)(struct owDev_s *owDev);
bool (*owTriplet)(struct owDev_s *owDev, uint8_t direction, uint8_t *result);
} owDev_t;

void _1WireInit(void);
void owInit(void);
owDev_t *getOwDev(void);

#endif /* defined(USE_1WIRE) && defined(USE_1WIRE_DS2482) */
Loading

0 comments on commit 820a852

Please sign in to comment.