arch/stm32: add 3-phase Hall effect sensor lower half

This commit is contained in:
raiden00pl
2021-08-18 14:01:01 +02:00
committed by Xiang Xiao
parent b30f5e146d
commit 78962165d8
3 changed files with 276 additions and 0 deletions
+4
View File
@@ -232,6 +232,10 @@ ifeq ($(CONFIG_SENSORS_QENCODER),y)
CHIP_CSRCS += stm32_qencoder.c
endif
ifeq ($(CONFIG_SENSORS_HALL3PHASE),y)
CHIP_CSRCS += stm32_hall3ph.c
endif
ifeq ($(CONFIG_STM32_CAN),y)
CHIP_CSRCS += stm32_can.c
endif
+209
View File
@@ -0,0 +1,209 @@
/****************************************************************************
* arch/arm/src/stm32/stm32_hall3ph.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <errno.h>
#include <debug.h>
#include <stdio.h>
#include <assert.h>
#include <nuttx/kmalloc.h>
#include <nuttx/sensors/hall3ph.h>
#include <arch/board/board.h>
#include "chip.h"
#include "arm_arch.h"
#include "stm32_hall3ph.h"
/****************************************************************************
* Private Types
****************************************************************************/
struct stm32_lowerhalf_s
{
/* The first field of this state structure must be a pointer to the lower-
* half callback structure:
*/
FAR const struct hall3_ops_s *ops; /* Lower half callback structure */
struct stm32_hall3ph_cfg_s cfg; /* Configuration */
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
int stm32_hall3ph_setup(FAR struct hall3_lowerhalf_s *lower);
int stm32_hall3ph_shutdown(FAR struct hall3_lowerhalf_s *lower);
int stm32_hall3ph_position(FAR struct hall3_lowerhalf_s *lower,
FAR uint8_t *pos);
/****************************************************************************
* Private Data
****************************************************************************/
struct hall3_ops_s g_hall3ph_ops =
{
.setup = stm32_hall3ph_setup,
.shutdown = stm32_hall3ph_shutdown,
.position = stm32_hall3ph_position
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: stm32_hall3ph_setup
*
* Description:
* Configure the 3-phase Hall effect sensor
*
****************************************************************************/
int stm32_hall3ph_setup(FAR struct hall3_lowerhalf_s *lower)
{
FAR struct stm32_lowerhalf_s *priv = (FAR struct stm32_lowerhalf_s *)lower;
DEBUGASSERT(priv);
/* Configure input pins */
stm32_configgpio(priv->cfg.gpio_pha);
stm32_configgpio(priv->cfg.gpio_phb);
stm32_configgpio(priv->cfg.gpio_phc);
return OK;
}
/****************************************************************************
* Name: stm32_hall3ph_shutdown
*
* Description:
* Disable the 3-phase Hall effect sensor
*
****************************************************************************/
int stm32_hall3ph_shutdown(FAR struct hall3_lowerhalf_s *lower)
{
FAR struct stm32_lowerhalf_s *priv = (FAR struct stm32_lowerhalf_s *)lower;
DEBUGASSERT(priv);
/* Unconfigure input pins */
stm32_unconfiggpio(priv->cfg.gpio_pha);
stm32_unconfiggpio(priv->cfg.gpio_phb);
stm32_unconfiggpio(priv->cfg.gpio_phc);
return OK;
}
/****************************************************************************
* Name: stm32_hall3ph_position
*
* Description:
* Return the current 3-phase Hall effect sensor position
*
****************************************************************************/
int stm32_hall3ph_position(FAR struct hall3_lowerhalf_s *lower,
FAR uint8_t *pos)
{
FAR struct stm32_lowerhalf_s *priv = (FAR struct stm32_lowerhalf_s *)lower;
uint8_t ha = 0;
uint8_t hb = 0;
uint8_t hc = 0;
uint8_t i = 0;
uint8_t thr = (priv->cfg.samples / 2);
DEBUGASSERT(priv);
/* Sample pins */
for (i = 0; i < priv->cfg.samples; i += 1)
{
ha += stm32_gpioread(priv->cfg.gpio_pha);
hb += stm32_gpioread(priv->cfg.gpio_phb);
hc += stm32_gpioread(priv->cfg.gpio_phc);
}
/* Get state based on threshold */
*pos = ((ha > thr) << 0) |
((hb > thr) << 1) |
((hc > thr) << 2);
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stm32_hall3ph_initialize
*
* Description:
* Initialize the 3-phase Hall effect driver
*
****************************************************************************/
int stm32_hall3ph_initialize(FAR const char *devpath,
FAR struct stm32_hall3ph_cfg_s *cfg)
{
FAR struct stm32_lowerhalf_s *priv = NULL;
int ret = OK;
/* Allocate the lower-half data structure */
priv = (FAR struct stm32_lowerhalf_s *)
kmm_zalloc(sizeof(struct stm32_lowerhalf_s));
if (priv == NULL)
{
snerr("ERROR: Allocation failed\n");
ret = -ENOMEM;
goto errout;
}
/* Copy configuration */
memcpy(&priv->cfg, cfg, sizeof(struct stm32_hall3ph_cfg_s));
/* Connect ops */
priv->ops = &g_hall3ph_ops;
/* Initialize a Hall effect sensor interface. */
ret = hall3_register(devpath, (FAR struct hall3_lowerhalf_s *)priv);
if (ret < 0)
{
snerr("ERROR: hall3ph_register failed: %d\n", ret);
}
errout:
return ret;
}
+63
View File
@@ -0,0 +1,63 @@
/****************************************************************************
* arch/arm/src/stm32/stm32_hall3ph.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
#ifndef __ARCH_ARM_SRC_STM32_STM32_HALL3PH_H
#define __ARCH_ARM_SRC_STM32_STM32_HALL3PH_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Public Types
****************************************************************************/
/* 3-phase Hall effect sensor configuration */
struct stm32_hall3ph_cfg_s
{
int gpio_pha; /* Hall phase A pincfg */
int gpio_phb; /* Hall phase B pincfg */
int gpio_phc; /* Hall phase C pincfg */
int samples; /* Number of samples taken from GPIO */
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: stm32_hall3ph_initialize
****************************************************************************/
/****************************************************************************
* Name: stm32_hall3ph_initialize
*
* Description:
* Initialize the 3-phase Hall effect driver
*
****************************************************************************/
int stm32_hall3ph_initialize(FAR const char *devpath,
FAR struct stm32_hall3ph_cfg_s *cfg);
#endif /* __ARCH_ARM_SRC_STM32_STM32_HALL3PH_H */