235 lines
7.4 KiB
C
235 lines
7.4 KiB
C
/*
|
|
ChibiOS/RT - Copyright (C) 2014 Uladzimir Pylinsky aka barthess
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
* @file onewire.h
|
|
* @brief 1-wire Driver macros and structures.
|
|
*
|
|
* @addtogroup onewire
|
|
* @{
|
|
*/
|
|
|
|
#ifndef _ONEWIRE_H_
|
|
#define _ONEWIRE_H_
|
|
|
|
#include "hal.h" //FIXME: delete this line when integration done
|
|
|
|
#if HAL_USE_ONEWIRE || defined(__DOXYGEN__)
|
|
|
|
/*===========================================================================*/
|
|
/* Driver constants. */
|
|
/*===========================================================================*/
|
|
/**
|
|
* @brief Enable synthetic test for 'search ROM' procedure.
|
|
* @note Only for debugging/testing.
|
|
*/
|
|
#define ONEWIRE_SYNTH_SEARCH_TEST FALSE
|
|
|
|
/**
|
|
* @brief Aliases for 1-wire protocol.
|
|
*/
|
|
#define ONEWIRE_CMD_READ_ROM 0x33
|
|
#define ONEWIRE_CMD_SEARCH_ROM 0xF0
|
|
#define ONEWIRE_CMD_MATCH_ROM 0x55
|
|
#define ONEWIRE_CMD_SKIP_ROM 0xCC
|
|
#define ONEWIRE_CMD_CONVERT_TEMP 0x44
|
|
#define ONEWIRE_CMD_READ_SCRATCHPAD 0xBE
|
|
|
|
/*===========================================================================*/
|
|
/* Driver pre-compile time settings. */
|
|
/*===========================================================================*/
|
|
|
|
/*===========================================================================*/
|
|
/* Derived constants and error checks. */
|
|
/*===========================================================================*/
|
|
|
|
/*===========================================================================*/
|
|
/* Driver data structures and types. */
|
|
/*===========================================================================*/
|
|
|
|
/**
|
|
* @brief 1-wire strong pull up assert callback type.
|
|
*/
|
|
typedef void (*onewire_pullup_assert_t)(void);
|
|
|
|
/**
|
|
* @brief 1-wire strong pull up release callback type.
|
|
*/
|
|
typedef void (*onewire_pullup_release_t)(void);
|
|
|
|
/**
|
|
* @brief 1-wire read bit callback type.
|
|
*
|
|
* @return Bit acquired directly from pin (0 or 1)
|
|
*/
|
|
typedef uint_fast8_t (*onewire_read_bit_t)(void);
|
|
|
|
/**
|
|
* @brief Driver state machine possible states.
|
|
*/
|
|
typedef enum {
|
|
ONEWIRE_UNINIT = 0,
|
|
ONEWIRE_STOP = 1,
|
|
ONEWIRE_READY = 2,
|
|
#if ONEWIRE_USE_STRONG_PULLUP
|
|
ONEWIRE_PULL_UP
|
|
#endif
|
|
} onewire_state_t;
|
|
|
|
/**
|
|
* @brief Search ROM procedure possible state.
|
|
*/
|
|
typedef enum {
|
|
ONEWIRE_SEARCH_ROM_SUCCESS = 0,
|
|
ONEWIRE_SEARCH_ROM_LAST,
|
|
ONEWIRE_SEARCH_ROM_ERROR
|
|
} search_rom_result_t;
|
|
|
|
/**
|
|
* @brief Search ROM procedure iteration enum.
|
|
*/
|
|
typedef enum {
|
|
ONEWIRE_SEARCH_ROM_FIRST = 0,
|
|
ONEWIRE_SEARCH_ROM_NEXT
|
|
} search_iteration_t;
|
|
|
|
/**
|
|
* @brief Driver configuration structure.
|
|
*/
|
|
typedef struct {
|
|
PWMDriver *pwmd;
|
|
size_t master_channel;
|
|
size_t sample_channel;
|
|
onewire_read_bit_t readBitX;
|
|
#if ONEWIRE_USE_STRONG_PULLUP
|
|
onewire_pullup_assert_t pullup_assert;
|
|
onewire_pullup_release_t pullup_release;
|
|
#endif
|
|
} onewireConfig;
|
|
|
|
/**
|
|
* @brief Some small variable used in 'search ROM' procedure combined
|
|
* in single machine word to save RAM.
|
|
*/
|
|
typedef struct {
|
|
uint32_t single_device: 1; /**< @brief Bool flag */
|
|
uint32_t search_iter: 1; /**< @brief 0 - first, 1 - next */
|
|
uint32_t result: 2; /**< @brief 0 - success, 1 - last, 2 - error.*/
|
|
uint32_t bit_step: 2; /**< @brief 0 - direct, 1 - complemented, 2 - generated by master. */
|
|
uint32_t bit_buf: 2; /**< @brief Acquired bits. 0s - direct, 1st - complement */
|
|
uint32_t rombit: 7; /**< @brief Currently processing ROM bit. Must be big enough to store number 64.*/
|
|
uint32_t devices_found: 8; /**< @brief Devices count discovered on bus .*/
|
|
} search_rom_reg_t;
|
|
|
|
/**
|
|
* @brief Helper structure for 'search ROM' procedure
|
|
*/
|
|
typedef struct {
|
|
search_rom_reg_t reg;
|
|
uint8_t *retbuf; /* buffer for currently discovering ROM */
|
|
uint8_t prev_path[8];
|
|
int8_t last_zero_branch; /* negative values uses to point out of tree root */
|
|
int8_t prev_zero_branch; /* negative values uses to point out of tree root */
|
|
} onewire_search_rom_t;
|
|
|
|
/**
|
|
* @brief Some small variable used in driver combined
|
|
* in single machine word to save RAM.
|
|
*/
|
|
typedef struct {
|
|
#if ONEWIRE_USE_STRONG_PULLUP
|
|
/**
|
|
* @brief This flag will be asserted by driver to signalize
|
|
* ISR part when strong pullup needed.
|
|
*/
|
|
uint32_t need_pullup: 1;
|
|
#endif
|
|
uint32_t slave_present: 1; /**< @brief Bool flag */
|
|
uint32_t state: 2; /**< @brief Driver state */
|
|
uint32_t bit: 4; /**< @brief Bit number in currently receiving byte. Must be big enough to store 8 */
|
|
uint32_t final_timeslot: 1; /**< @brief bool flag for premature timer stop prevention */
|
|
uint32_t bytes: 16; /**< @brief Bytes number to be processing in current transaction. */
|
|
} onewire_reg_t;
|
|
|
|
/**
|
|
* @brief Structure representing an 1-wire driver.
|
|
*/
|
|
typedef struct {
|
|
onewire_reg_t reg;
|
|
const onewireConfig *config;
|
|
PWMConfig pwmcfg;
|
|
uint8_t *buf;
|
|
onewire_search_rom_t search_rom;
|
|
|
|
/**
|
|
* @brief Thread waiting for I/O completion.
|
|
*/
|
|
thread_reference_t thread;
|
|
} onewireDriver;
|
|
|
|
/*===========================================================================*/
|
|
/* Driver macros. */
|
|
/*===========================================================================*/
|
|
|
|
/*===========================================================================*/
|
|
/* External declarations. */
|
|
/*===========================================================================*/
|
|
|
|
extern onewireDriver OWD1;
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
void onewireInit(void);
|
|
void onewireObjectInit(onewireDriver *owp);
|
|
void onewireStart(onewireDriver *owp, const onewireConfig *config);
|
|
void onewireStop(onewireDriver *owp);
|
|
bool onewireReset(onewireDriver *owp);
|
|
void onewireRead(onewireDriver *owp, uint8_t *rxbuf, size_t rxbytes);
|
|
void onewireWrite(onewireDriver *owp,
|
|
uint8_t *txbuf,
|
|
size_t txbytes,
|
|
systime_t pullup_time);
|
|
size_t onewireSearchRom(onewireDriver *owp,
|
|
uint8_t *result,
|
|
size_t max_rom_cnt);
|
|
uint8_t onewireCRC(const uint8_t *buf, size_t len);
|
|
#if ONEWIRE_SYNTH_SEARCH_TEST
|
|
void _synth_ow_write_bit(onewireDriver *owp, uint8_t bit);
|
|
uint_fast8_t _synth_ow_read_bit(void);
|
|
void synthSearchRomTest(onewireDriver *owp);
|
|
#endif /* ONEWIRE_SYNTH_SEARCH_TEST */
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* HAL_USE_ONEWIRE */
|
|
|
|
#endif /* _ONEWIRE_H_ */
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|