From de8cd6c870db763c77a4d5cd0da3769b64a4ca1d Mon Sep 17 00:00:00 2001 From: raiden00pl Date: Sun, 11 Jun 2017 20:51:23 +0200 Subject: [PATCH 1/3] stm32_hrtim: add character driver --- arch/arm/src/stm32/stm32_hrtim.c | 149 ++++++++++++++++++++++++------- arch/arm/src/stm32/stm32_hrtim.h | 20 +++++ 2 files changed, 135 insertions(+), 34 deletions(-) diff --git a/arch/arm/src/stm32/stm32_hrtim.c b/arch/arm/src/stm32/stm32_hrtim.c index c0cc3e48af..aef00eaba6 100644 --- a/arch/arm/src/stm32/stm32_hrtim.c +++ b/arch/arm/src/stm32/stm32_hrtim.c @@ -45,7 +45,6 @@ #include #include -#include #include "chip.h" #include "stm32.h" @@ -248,7 +247,6 @@ struct stm32_hrtim_slave_priv_s struct stm32_hrtim_s { uint32_t base; /* Base adress of HRTIM block */ - FAR const struct hrtim_ops_s *ops; /* */ struct stm32_hrtim_tim_s *master; /* Master Timer */ #ifdef CONFIG_STM32_HRTIM_TIMA struct stm32_hrtim_tim_s *tima; /* HRTIM Timer A */ @@ -271,6 +269,12 @@ struct stm32_hrtim_s * Private Function Prototypes ****************************************************************************/ +/* HRTIM Driver Methods */ + +static int stm32_hrtim_open(FAR struct file *filep); +static int stm32_hrtim_close(FAR struct file *filep); +static int stm32_hrtim_ioctl(FAR struct file *filep, int cmd, unsigned long arg); + /* HRTIM Register access */ #ifdef HRTIM_HAVE_CLK_FROM_PLL @@ -294,10 +298,6 @@ static uint32_t hrtim_tim_getreg(FAR struct stm32_hrtim_s *priv, uint8_t index, static FAR struct stm32_hrtim_tim_s *hrtim_tim_get(FAR struct stm32_hrtim_s *priv, uint8_t index); -/* HRTIM Driver Methods */ - -static int hrtim_ioctl(FAR struct hrtim_dev_s *dev, int cmd, unsigned long arg); - /* Configuration */ static int hrtim_dll_cal(FAR struct stm32_hrtim_s *priv); @@ -338,11 +338,20 @@ static int stm32_hrtimconfig(FAR struct stm32_hrtim_s *priv); * Private Data ****************************************************************************/ -/* HRTIM interface operations */ - -static const struct hrtim_ops_s g_hrtimops = +static const struct file_operations hrtim_fops = { - .ho_ioctl = hrtim_ioctl, + stm32_hrtim_open, /* open */ + stm32_hrtim_close, /* close */ + NULL, /* read */ + NULL, /* write */ + NULL, /* seek */ + stm32_hrtim_ioctl /* ioctl */ +#ifndef CONFIG_DISABLE_POLL + , NULL /* poll */ +#endif +#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS + , NULL /* unlink */ +#endif }; /* Master Timer data */ @@ -439,7 +448,6 @@ static struct stm32_hrtim_s g_hrtim1priv = struct hrtim_dev_s g_hrtim1dev = { - .hd_ops = &g_hrtimops, .hd_priv = &g_hrtim1priv, }; @@ -447,6 +455,73 @@ struct hrtim_dev_s g_hrtim1dev = * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: stm32_hrtim_open + * + * Description: + * This function is called whenever the HRTIM device is opened. + * + ****************************************************************************/ + +static int stm32_hrtim_open(FAR struct file *filep) +{ +#warning "stm32_hrtim_open: missing logic" + return OK; +} + +/**************************************************************************** + * Name: stm32_hrtim_close + * + * Description: + * This function is called when the HRTIM device is closed. + * + ****************************************************************************/ + +static int stm32_hrtim_close(FAR struct file *filep) +{ +#warning "smt32_hrtim_close: missing logic" + return OK; +} + +/**************************************************************************** + * Name: stm32_hrtim_ioctl + * + * Description: + * The standard ioctl method. This is where ALL of the HRTIM work is done. + * + ****************************************************************************/ + +static int stm32_hrtim_ioctl(FAR struct file *filep, int cmd, unsigned long arg) +{ + FAR struct inode *inode = filep->f_inode; + FAR struct hrtim_dev_s *dev; + FAR struct stm32_hrtim_s *hrtim; + int ret; + + tmrinfo("cmd: %d arg: %ld\n", cmd, arg); + dev = inode->i_private; + DEBUGASSERT(dev != NULL); + hrtim = dev->hd_priv; + + UNUSED(hrtim); + +#warning "smt32_hrtim_ioctl: missing logic" + + /* Handle HRTIM ioctl commands */ + + switch (cmd) + { + + default: + { + ret = -ENOSYS; + break; + } + } + + return ret; +} + /**************************************************************************** * Name: stm32_modifyreg32 * @@ -463,12 +538,13 @@ struct hrtim_dev_s g_hrtim1dev = * ****************************************************************************/ +#ifdef HRTIM_HAVE_CLK_FROM_PLL static void stm32_modifyreg32(unsigned int addr, uint32_t clrbits, uint32_t setbits) { putreg32((getreg32(addr) & ~clrbits) | setbits, addr); } - +#endif /**************************************************************************** * Name: hrtim_getreg @@ -730,28 +806,6 @@ static void hrtim_tim_modifyreg(FAR struct stm32_hrtim_s *priv, uint8_t index, (hrtim_tim_getreg(priv, index, offset) & ~clrbits) | setbits); } -/**************************************************************************** - * Name: hrtim_ioctl - * - * Description: - * All ioctl calls will be routed through this method. - * - * Input Parameters: - * dev - pointer to device structure used by the driver - * cmd - command - * arg - arguments passed with command - * - * Returned Value: - * Zero on success; a negated errno value on failure. - * - ****************************************************************************/ - -static int hrtim_ioctl(FAR struct hrtim_dev_s *dev, int cmd, unsigned long arg) -{ -#warning "hrtim_ioctl: missing logic" - return -ENOTTY; -} - /**************************************************************************** * Name: stm32_dll_cal * @@ -1514,6 +1568,33 @@ FAR struct hrtim_dev_s* stm32_hrtiminitialize(void) return dev; } +/**************************************************************************** + * Name: hrtim_register + ****************************************************************************/ + +int hrtim_register(FAR const char *path, FAR struct hrtim_dev_s *dev) +{ + int ret ; + + /* Initialize the HRTIM device structure */ + + dev->hd_ocount = 0; + + /* Initialize semaphores */ + + sem_init(&dev->hd_closesem, 0, 1); + + /* Register the HRTIM character driver */ + + ret = register_driver(path, &hrtim_fops, 0444, dev); + if (ret < 0) + { + sem_destroy(&dev->hd_closesem); + } + + return ret; +} + #endif /* CONFIG_STM32_STM32F33XX */ #endif /* CONFIG_STM32_HRTIM1 */ diff --git a/arch/arm/src/stm32/stm32_hrtim.h b/arch/arm/src/stm32/stm32_hrtim.h index 1fa9a4971a..e4fe9be912 100644 --- a/arch/arm/src/stm32/stm32_hrtim.h +++ b/arch/arm/src/stm32/stm32_hrtim.h @@ -222,6 +222,20 @@ enum stm32_hrtim_tim_prescaler_e HRTIM_PRESCALER_128, }; +struct hrtim_dev_s +{ +#ifdef CONFIG_HRTIM + /* Fields managed by common upper half HRTIM logic */ + + uint8_t hd_ocount; /* The number of times the device has been opened */ + sem_t hd_closesem; /* Locks out new opens while close is in progress */ +#endif + + /* Fields provided by lower half HRTIM logic */ + + FAR void *hd_priv; /* Used by the arch-specific logic */ +}; + /************************************************************************************ * Public Function Prototypes ************************************************************************************/ @@ -255,6 +269,12 @@ extern "C" FAR struct hrtim_dev_s* stm32_hrtiminitialize(void); +/**************************************************************************** + * Name: hrtim_register + ****************************************************************************/ + +int hrtim_register(FAR const char *path, FAR struct hrtim_dev_s *dev); + #undef EXTERN #ifdef __cplusplus } From 43e0c28acbeeed764f54fac07f4d69f9e123e911 Mon Sep 17 00:00:00 2001 From: raiden00pl Date: Sun, 11 Jun 2017 20:52:43 +0200 Subject: [PATCH 2/3] nucleo-f334r8: add HRTIM initialization --- configs/nucleo-f334r8/src/stm32_hrtim.c | 96 +++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 configs/nucleo-f334r8/src/stm32_hrtim.c diff --git a/configs/nucleo-f334r8/src/stm32_hrtim.c b/configs/nucleo-f334r8/src/stm32_hrtim.c new file mode 100644 index 0000000000..d5af56f757 --- /dev/null +++ b/configs/nucleo-f334r8/src/stm32_hrtim.c @@ -0,0 +1,96 @@ +/**************************************************************************** + * configs/nucleo-f334r8/src/stm32_hrtim.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Mateusz Szafoni + * + * 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 + +#include +#include +#include + +#include + +#include "stm32.h" + +#if defined(CONFIG_HRTIM) && defined(CONFIG_STM32_HRTIM1) + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_hrtim_setup + * + * Description: + * Initialize HRTIM + * + ****************************************************************************/ + +int stm32_hrtim_setup(void) +{ + static bool initialized = false; + struct hrtim_dev_s* hrtim = NULL; + int ret; + + if (!initialized) + { + /* Get the HRTIM interface */ + + hrtim = stm32_hrtiminitialize(); + if (hrtim == NULL) + { + aerr("ERROR: Failed to get HRTIM1interface\n"); + return -ENODEV; + } + + /* Register the HRTIM character driver at /dev/hrtim0 */ + + ret = hrtim_register("/dev/hrtim0", hrtim); + if (ret < 0) + { + aerr("ERROR: hrtim_register failed: %d\n", ret); + return ret; + } + + initialized = true; + } + + return OK; +} + +#endif /* CONFIG_HRTIM && CONFIG_STM32_HRTIM1 */ From fe7148dff0561f62f00d46b84e0395a8e5feb544 Mon Sep 17 00:00:00 2001 From: raiden00pl Date: Sun, 11 Jun 2017 21:14:48 +0200 Subject: [PATCH 3/3] nucleo-f334r8: cosmetics --- configs/nucleo-f334r8/src/stm32_hrtim.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/configs/nucleo-f334r8/src/stm32_hrtim.c b/configs/nucleo-f334r8/src/stm32_hrtim.c index d5af56f757..591e4c6fda 100644 --- a/configs/nucleo-f334r8/src/stm32_hrtim.c +++ b/configs/nucleo-f334r8/src/stm32_hrtim.c @@ -57,7 +57,10 @@ * Name: stm32_hrtim_setup * * Description: - * Initialize HRTIM + * Initialize HRTIM driver + * + * Returned Value: + * 0 on success, a negated errno value on failure * ****************************************************************************/ @@ -74,7 +77,7 @@ int stm32_hrtim_setup(void) hrtim = stm32_hrtiminitialize(); if (hrtim == NULL) { - aerr("ERROR: Failed to get HRTIM1interface\n"); + tmrerr("ERROR: Failed to get HRTIM1 interface\n"); return -ENODEV; } @@ -83,7 +86,7 @@ int stm32_hrtim_setup(void) ret = hrtim_register("/dev/hrtim0", hrtim); if (ret < 0) { - aerr("ERROR: hrtim_register failed: %d\n", ret); + tmrerr("ERROR: hrtim_register failed: %d\n", ret); return ret; }