This commit adds a 1wire subsystem.

Squashed commit of the following:

Author: Gregory Nutt <gnutt@nuttx.org>
    Some cosmetic changes from coding style review.
Author: Juha Niskanen <juha.niskanen@haltian.com>
    drivers/1wire: add 1-wire subsystem and ds28e17 driver
This commit is contained in:
Juha Niskanen
2018-04-04 10:57:36 -06:00
committed by Gregory Nutt
parent b4c1ac0659
commit 797d9b1822
11 changed files with 2092 additions and 11 deletions

640
drivers/1wire/1wire.c Normal file

File diff suppressed because it is too large Load Diff

159
drivers/1wire/1wire_crc.c Normal file
View File

@@ -0,0 +1,159 @@
/****************************************************************************
* drivers/1wire/1wire_crc.c
*
* Copyright (C) 2018 Haltian Ltd. All rights reserved.
* Author: Juha Niskanen <juha.niskanen@haltian.com>
*
* 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 NuttX nor the names of its contributors 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 OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "1wire_internal.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/* Compute a Dallas Semiconductor 8 bit CRC. */
uint8_t onewire_crc8(FAR const uint8_t *input, uint8_t len)
{
uint8_t crc = 0;
while (len-- > 0)
{
int i;
uint8_t inbyte = *input++;
for (i = 0; i < 8; i++)
{
uint8_t mix = (crc ^ inbyte) & 0x01;
crc >>= 1;
if (mix)
crc ^= 0x8c;
inbyte >>= 1;
}
}
return crc;
}
/* Compute a Dallas Semiconductor 16 bit CRC. This is used to check
* the integrity of received data from many 1-wire devices.
*
* Note: the CRC-16 computed here is not what you'll get from the 1-wire
* network, because:
* - The CRC-16 is transmitted bitwise inverted.
* - The binary representation of the return value may have a different
* byte order than the two bytes you get from 1-wire due to endian-ness.
*/
uint16_t onewire_crc16(FAR const uint8_t *input, uint16_t len,
uint16_t initial_crc)
{
uint16_t crc = initial_crc;
while (len-- > 0)
{
int i;
uint8_t inbyte = *input++;
for (i = 0; i < 8; i++)
{
uint8_t mix = ((crc & 0xff) ^ inbyte) & 0x01;
crc >>= 1;
if (mix)
{
crc ^= 0xa001;
}
inbyte >>= 1;
}
}
return crc;
}
/****************************************************************************
* Name: onewire_valid_rom
*
* Description:
* Check CRC-8 of received input
*
* Input Parameters:
* rom - 64-bit rom code
*
* Returned Value:
* true if CRC-8 of rom matches
*
****************************************************************************/
bool onewire_valid_rom(uint64_t rom)
{
uint8_t crc;
uint8_t buf[8];
memcpy(buf, &rom, sizeof(buf));
crc = onewire_crc8(buf, 7);
return crc == buf[7];
}
/****************************************************************************
* Name: onewire_check_crc16
*
* Description:
* Check CRC-16 of received input
*
* Input Parameters:
* input - Array of bytes to checksum
* len - Length of input
* inverted_crc - The two CRC16 bytes in the received data
*
* Returned Value:
* true if CRC-16 matches
*
****************************************************************************/
bool onewire_check_crc16(FAR const uint8_t *input, uint16_t len,
FAR const uint8_t *inverted_crc)
{
uint16_t crc;
crc = ~onewire_crc16(input, len, 0);
return (crc & 0xff) == inverted_crc[0] && (crc >> 8) == inverted_crc[1];
}

View File

@@ -0,0 +1,180 @@
/****************************************************************************
* drivers/1wire/1wire_internal.h
*
* Copyright (C) 2018 Haltian Ltd. All rights reserved.
* Author: Juha Niskanen <juha.niskanen@haltian.com>
*
* 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 NuttX nor the names of its contributors 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 OWNER 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 __DRIVERS_1WIRE_1WIRE_INTERNAL_H
#define __DRIVERS_1WIRE_1WIRE_INTERNAL_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdbool.h>
#include <semaphore.h>
/****************************************************************************
* Public Types
****************************************************************************/
struct onewire_dev_s;
struct onewire_sem_s
{
sem_t sem;
pid_t holder; /* The current holder of the semaphore */
int16_t count; /* Number of counts held */
};
struct onewire_master_s
{
FAR struct onewire_dev_s *dev; /* 1-Wire lower half */
struct onewire_sem_s devsem; /* Re-entrant semaphore */
int nslaves; /* Number of 1-wire slaves */
int maxslaves; /* Maximum number of 1-wire slaves */
bool insearch; /* If we are in middle of 1-wire search */
};
struct onewire_slave_s
{
FAR struct onewire_master_s *master; /* Slave's bus master */
uint64_t romcode; /* Unique ROM id in bus */
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* CRC helpers from 1wire_crc.c */
uint8_t onewire_crc8(FAR const uint8_t *input, uint8_t len);
uint16_t onewire_crc16(FAR const uint8_t *input, uint16_t len,
uint16_t initial_crc);
bool onewire_valid_rom(uint64_t rom);
bool onewire_check_crc16(FAR const uint8_t *input, uint16_t len,
FAR const uint8_t *inverted_crc);
/* Rest are from 1wire.c */
void onewire_sem_wait(FAR struct onewire_master_s *master);
void onewire_sem_post(FAR struct onewire_master_s *master);
int onewire_addslave(FAR struct onewire_master_s *master,
FAR struct onewire_slave_s *slave);
int onewire_removeslave(FAR struct onewire_master_s *master,
FAR struct onewire_slave_s *slave);
/****************************************************************************
* Name: onewire_reset_resume
*
* Description:
*
****************************************************************************/
int onewire_reset_resume(FAR struct onewire_master_s *master);
/****************************************************************************
* Name: onewire_reset_select
*
* Description:
*
****************************************************************************/
int onewire_reset_select(FAR struct onewire_slave_s *slave);
/****************************************************************************
* Name: onewire_triplet
*
* Description:
* Used by 1-wire search algorithm. Reads two bits and writes
* one based on comparison of read bits.
*
* Input Parameters:
* search_bit - Bit to write if both id_bit and cmp_id_bit match
*
* Output Parameters:
* taken_bit - Bit indicating the direction where the search is
* progressing.
*
* Return Value:
* Number of valid bits or negative on error.
*
****************************************************************************/
int onewire_triplet(FAR struct onewire_master_s *master,
uint8_t search_bit,
FAR uint8_t *taken_bit);
/****************************************************************************
* Name: onewire_search
*
* Description:
* Search all devices from a 1-wire network. This is the 1-wire search
* algorithm from Maxim Application Note 187.
*
* Input Parameters:
* master - Pointer to the allocated 1-wire interface
* family - Limit search to devices of matching family
* alarmonly - Limit search to devices on alarm state
* cb_search - Callback to call on each device found
* arg - Argument passed to cb_search
*
* Return Value:
* Number of slaves present and matching family.
*
****************************************************************************/
int onewire_search(FAR struct onewire_master_s *master,
int family,
bool alarmonly,
CODE void (*cb_search)(int family, uint64_t romcode, FAR void *arg),
FAR void *arg);
/****************************************************************************
* Name: onewire_initialize
*
* Description:
* Return 1-wire bus master from 1-wire lower half device.
*
* Input Parameters:
* dev - Pointer to the allocated 1-wire lower half
* maxslaves - Maximum number of 1-wire slave devices
*
****************************************************************************/
FAR struct onewire_master_s *
onewire_initialize(FAR struct onewire_dev_s *dev, int maxslaves);
#endif /* __DRIVERS_1WIRE_1WIRE_INTERNAL_H */

15
drivers/1wire/Kconfig Normal file
View File

@@ -0,0 +1,15 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
if 1WIRE
config 1WIRE_DS28E17
bool "DS28E17 1-wire to I2C converter"
default n
depends on I2C
---help---
Enable support for the Maxim DS28E17 1-wire to I2C converter
endif # 1WIRE

49
drivers/1wire/Make.defs Normal file
View File

@@ -0,0 +1,49 @@
############################################################################
# drivers/1wire/Make.defs
#
# Copyright (C) 2018 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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 NuttX nor the names of its contributors 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 OWNER 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.
#
############################################################################
ifeq ($(CONFIG_1WIRE),y)
CSRCS += 1wire.c 1wire_crc.c
ifeq ($(CONFIG_1WIRE_DS28E17),y)
CSRCS += ds28e17.c
endif
# Include 1wire device driver build support
DEPPATH += --dep-path 1wire
VPATH += :1wire
CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TOPDIR)$(DELIM)drivers$(DELIM)1wire}
endif

872
drivers/1wire/ds28e17.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -477,4 +477,12 @@ menuconfig DRIVERS_CONTACTLESS
source drivers/contactless/Kconfig
menuconfig 1WIRE
bool "1wire Device Support"
default n
---help---
Drivers for various 1wire devices.
source drivers/1wire/Kconfig
source drivers/syslog/Kconfig

View File

@@ -78,6 +78,7 @@ include usbmonitor$(DELIM)Make.defs
include video$(DELIM)Make.defs
include wireless$(DELIM)Make.defs
include contactless$(DELIM)Make.defs
include 1wire$(DELIM)Make.defs
ifneq ($(CONFIG_NFILE_DESCRIPTORS),0)
CSRCS += dev_null.c dev_zero.c

View File

@@ -0,0 +1,139 @@
/****************************************************************************
* include/nuttx/1wire/ds28e17.h
*
* Copyright (C) 2018 Haltian Ltd. All rights reserved.
* Author: Juha Niskanen <juha.niskanen@haltian.com>
*
* 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 NuttX nor the names of its contributors 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 OWNER 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 __INCLUDE_NUTTX_1WIRE_DS28E17_H
#define __INCLUDE_NUTTX_1WIRE_DS28E17_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <nuttx/drivers/1wire.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Default how many converters in a bus. */
#define DS_DEFAULT_MAXSLAVES 10
/****************************************************************************
* Public Types
****************************************************************************/
struct ds28e17_dev_s;
struct i2c_master_s;
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: ds28e17_search
*
* Description:
* Search all DS28E17 devices from a 1-wire network.
*
* Input Parameters:
* priv - Pointer to the associated DS28E17
* cb_search - Callback to call on each device found
* arg - Argument passed to cb_search
*
* Return Value:
* Number of DS28E17 devices present.
*
****************************************************************************/
int ds28e17_search(FAR struct ds28e17_dev_s *priv,
CODE void (*cb_search)(int family, uint64_t romcode, FAR void *arg),
FAR oid *arg);
/****************************************************************************
* Name: ds28e17_lower_half
*
* Description:
* Initialize the lower half of the DS28E17 by creating a i2c_master_s
* for the virtual i2c master and link it to the associated DS28E17 and
* its port.
*
* Input Parameters:
* priv - Pointer to the associated DS28E17
* romcode - The unique 64-bit address in 1-wire network.
* Use zero for skip-ROM mode.
*
* Returned Value:
* i2c device instance; NULL on failure.
*
****************************************************************************/
FAR struct i2c_master_s *ds28e17_lower_half(FAR struct ds28e17_dev_s *priv,
uint64_t romcode);
/****************************************************************************
* Name: ds28e17_lower_half_unregister
*
* Description:
* Put back the lower half of the DS28E17.
*
* Input Parameters:
* priv - Pointer to the associated DS28E17
* i2cdev - i2c device instance from ds28e17_lower_half()
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
int ds28e17_lower_half_unregister(FAR struct ds28e17_dev_s *priv,
FAR struct i2c_master_s *i2cdev);
/****************************************************************************
* Name: ds28e17_initialize
*
* Description:
* Returns a common DS28E17 device from 1-wire lower half device
*
* Input Parameters:
* dev - The allocated 1-wire lower half
*
****************************************************************************/
FAR struct ds28e17_dev_s *ds28e17_initialize(FAR struct onewire_dev_s *dev);
#endif /* __INCLUDE_NUTTX_1WIRE_DS28E17_H */

View File

@@ -48,6 +48,18 @@
* Pre-processor Definitions
****************************************************************************/
/* Supported 1-Wire commands */
#define ONEWIRE_CMD_SEARCH 0xf0
#define ONEWIRE_CMD_ALARM_SEARCH 0xec
#define ONEWIRE_CMD_SKIP_ROM 0xcc
#define ONEWIRE_CMD_COPY_SCRATCHPAD 0x48
#define ONEWIRE_CMD_WRITE_SCRATCHPAD 0x4e
#define ONEWIRE_CMD_READ_SCRATCHPAD 0xbe
#define ONEWIRE_CMD_READ_ROM 0x33
#define ONEWIRE_CMD_MATCH_ROM 0x55
#define ONEWIRE_CMD_RESUME 0xa5
/****************************************************************************
* Name: ONEWIRE_RESET
*

View File

@@ -217,10 +217,15 @@ static int btnet_advertise(FAR struct net_driver_s *dev)
DEBUGASSERT(dev != NULL && dev->d_private != NULL);
/* Get the 6-byte local address from the device */
#warning Missing logic
/* Get the 6-byte local address from the device.
*
* REVISIT: The use of the g_btdev global restricts the implementation to
* a single Bluetooth device.
*/
/* Set the MAC address using that address */
addr = g_btdev.bdaddr.val
/* Set the MAC address using 6-byte local address from the device. */
BLUETOOTH_ADDRCOPY(dev->d_mac.radio.nv_addr, addr);
dev->d_mac.radio.nv_addrlen = BLUETOOTH_ADDRSIZE;
@@ -228,15 +233,16 @@ static int btnet_advertise(FAR struct net_driver_s *dev)
#ifdef CONFIG_NET_IPv6
/* Set the IP address based on the 6-byte address */
dev->d_ipv6addr[0] = HTONS(0xfe80);
dev->d_ipv6addr[1] = 0;
dev->d_ipv6addr[2] = 0;
dev->d_ipv6addr[3] = 0;
dev->d_ipv6addr[4] = HTONS(0x0200);
dev->d_ipv6addr[5] = (uint16_t)addr[0] << 8 | (uint16_t)addr[1];
dev->d_ipv6addr[6] = (uint16_t)addr[2] << 8 | (uint16_t)addr[3];
dev->d_ipv6addr[7] = (uint16_t)addr[4] << 8 | (uint16_t)addr[5];
dev->d_ipv6addr[0] = HTONS(0xfe80);
dev->d_ipv6addr[1] = 0;
dev->d_ipv6addr[2] = 0;
dev->d_ipv6addr[3] = 0;
dev->d_ipv6addr[4] = HTONS(0x0200);
dev->d_ipv6addr[5] = (uint16_t)addr[0] << 8 | (uint16_t)addr[1];
dev->d_ipv6addr[6] = (uint16_t)addr[2] << 8 | (uint16_t)addr[3];
dev->d_ipv6addr[7] = (uint16_t)addr[4] << 8 | (uint16_t)addr[5];
#endif
return OK;
}