S32K1XX Reset Cause PROCFS: Add Kconfig option and cleanup

This commit is contained in:
Jari van Ewijk 2022-01-12 16:21:45 +01:00 committed by Xiang Xiao
parent e47a915f4c
commit 0fc613f0b3
5 changed files with 119 additions and 141 deletions

View File

@ -347,6 +347,15 @@ config S32K1XX_EEEPROM
endmenu # S32K1XX Peripheral Selection
menu "S32K1xx Reset Control Module (RCM)"
config S32K1XX_RESETCAUSE_PROCFS
bool "S32K1XX MCU Reset Cause PROCFS entry"
default n
depends on !DISABLE_MOUNTPOINT && FS_PROCFS && FS_PROCFS_REGISTER
endmenu # S32K1XX Reset Control Module (RCM)
menu "S32K1XX FTM PWM Configuration"
depends on S32K1XX_FTM

View File

@ -98,8 +98,8 @@ ifneq ($(CONFIG_ARCH_CUSTOM_PMINIT),y)
CHIP_CSRCS += s32k1xx_pminitialize.c
endif
ifeq ($(CONFIG_RESET_CAUSE_PROC_FS), y)
CHIP_CSRCS += s32k1xx_resetcause.c
ifeq ($(CONFIG_S32K1XX_RESETCAUSE_PROCFS), y)
CHIP_CSRCS += s32k1xx_resetcause_procfs.c
endif
# Source files specific to the ARM CPU family and to the S32K1xx chip family

View File

@ -1,5 +1,5 @@
/****************************************************************************
* arch/arm/src/s32k1xx/s32k1xx_resetcause.c
* arch/arm/src/s32k1xx/s32k1xx_resetcause_procfs.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -23,17 +23,15 @@
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/procfs.h>
#include <nuttx/fs/dirent.h>
#include <nuttx/kmalloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <debug.h>
@ -41,12 +39,9 @@
#include "arm_arch.h"
#include "hardware/s32k1xx_rcm.h"
#include "s32k1xx_resetcause_procfs.h"
#include "s32k1xx_resetcause.h"
#include "arm_internal.h"
#include <arch/board/board.h> /* Include last: has dependencies */
#ifdef CONFIG_S32K1XX_RESETCAUSE_PROCFS
/****************************************************************************
* Pre-processor Definitions
@ -55,7 +50,8 @@
/* Determines the size of an intermediate buffer that must be large enough
* to handle the longest line generated by this logic.
*/
#define RESETCAUSE_LINELEN 6
#define S32K1XX_RESETCAUSE_LINELEN 11
/****************************************************************************
* Private Types
@ -63,60 +59,50 @@
/* This structure describes one open "file" */
struct resetcause_file_s
struct s32k1xx_resetcause_procfs_file_s
{
struct procfs_file_s base; /* Base open file structure */
unsigned int linesize; /* Number of valid characters in line[] */
char line[RESETCAUSE_LINELEN]; /* Pre-allocated buffer for formatted lines */
unsigned int resetcause; /* Variable representing the MCU specific reset cause */
struct procfs_file_s base; /* Base open file structure */
unsigned int linesize; /* Number of valid characters in line[] */
char line[S32K1XX_RESETCAUSE_LINELEN]; /* Pre-allocated buffer for formatted lines */
};
static unsigned int g_reset_cause = 0;
#if defined(CONFIG_RESET_CAUSE_PROC_FS) && defined(CONFIG_FS_PROCFS)
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* File system methods */
static int resetcause_open(FAR struct file *filep,
FAR const char *relpath, int oflags, mode_t mode);
static int resetcause_close(FAR struct file *filep);
static ssize_t resetcause_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static int resetcause_dup(FAR const struct file *oldp,
FAR struct file *newp);
static int resetcause_stat(FAR const char *relpath,
FAR struct stat *buf);
static int s32k1xx_resetcause_procfs_open(FAR struct file *filep,
FAR const char *relpath,
int oflags, mode_t mode);
static int s32k1xx_resetcause_procfs_close(FAR struct file *filep);
static ssize_t s32k1xx_resetcause_procfs_read(FAR struct file *filep,
FAR char *buffer,
size_t buflen);
static int s32k1xx_resetcause_procfs_dup(FAR const struct file *oldp,
FAR struct file *newp);
static int s32k1xx_resetcause_procfs_stat(FAR const char *relpath,
FAR struct stat *buf);
/****************************************************************************
* Public Data
* Private Data
****************************************************************************/
const struct procfs_operations resetcause_operations =
const struct procfs_operations s32k1xx_resetcause_procfs_ops =
{
resetcause_open, /* open */
resetcause_close, /* close */
resetcause_read, /* read */
NULL, /* write */
resetcause_dup, /* dup */
NULL, /* opendir */
NULL, /* closedir */
NULL, /* readdir */
NULL, /* rewinddir */
resetcause_stat /* stat */
s32k1xx_resetcause_procfs_open, /* open */
s32k1xx_resetcause_procfs_close, /* close */
s32k1xx_resetcause_procfs_read, /* read */
NULL, /* write */
s32k1xx_resetcause_procfs_dup, /* dup */
NULL, /* opendir */
NULL, /* closedir */
NULL, /* readdir */
NULL, /* rewinddir */
s32k1xx_resetcause_procfs_stat, /* stat */
};
static const struct procfs_entry_s g_resetcause_procfs =
static const struct procfs_entry_s g_s32k1xx_resetcause_procfs =
{
"resetcause", &resetcause_operations
"resetcause", &s32k1xx_resetcause_procfs_ops
};
/****************************************************************************
@ -124,13 +110,14 @@ static const struct procfs_entry_s g_resetcause_procfs =
****************************************************************************/
/****************************************************************************
* Name: resetcause_open
* Name: s32k1xx_resetcause_procfs_open
****************************************************************************/
static int resetcause_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode)
static int s32k1xx_resetcause_procfs_open(FAR struct file *filep,
FAR const char *relpath,
int oflags, mode_t mode)
{
FAR struct resetcause_file_s *attr;
FAR struct s32k1xx_resetcause_procfs_file_s *attr;
finfo("Open '%s'\n", relpath);
@ -148,7 +135,7 @@ static int resetcause_open(FAR struct file *filep, FAR const char *relpath,
/* Allocate a container to hold the file attributes */
attr = kmm_zalloc(sizeof(struct resetcause_file_s));
attr = kmm_zalloc(sizeof(struct s32k1xx_resetcause_procfs_file_s));
if (!attr)
{
ferr("ERROR: Failed to allocate file attributes\n");
@ -162,16 +149,16 @@ static int resetcause_open(FAR struct file *filep, FAR const char *relpath,
}
/****************************************************************************
* Name: resetcause_close
* Name: s32k1xx_resetcause_procfs_close
****************************************************************************/
static int resetcause_close(FAR struct file *filep)
static int s32k1xx_resetcause_procfs_close(FAR struct file *filep)
{
FAR struct resetcause_file_s *attr;
FAR struct s32k1xx_resetcause_procfs_file_s *attr;
/* Recover our private data from the struct file instance */
attr = (FAR struct resetcause_file_s *)filep->f_priv;
attr = (FAR struct s32k1xx_resetcause_procfs_file_s *)filep->f_priv;
DEBUGASSERT(attr);
/* Release the file attributes structure */
@ -182,69 +169,66 @@ static int resetcause_close(FAR struct file *filep)
}
/****************************************************************************
* Name: resetcause_read
* Name: s32k1xx_resetcause_procfs_read
****************************************************************************/
static ssize_t resetcause_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
static ssize_t s32k1xx_resetcause_procfs_read(FAR struct file *filep,
FAR char *buffer,
size_t buflen)
{
FAR struct resetcause_file_s *attr;
size_t linesize;
FAR struct s32k1xx_resetcause_procfs_file_s *attr;
uint32_t resetcause;
off_t offset;
ssize_t ret;
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
/* Recover our private data from the struct file instance */
attr = (FAR struct resetcause_file_s *)filep->f_priv;
attr = (FAR struct s32k1xx_resetcause_procfs_file_s *)filep->f_priv;
DEBUGASSERT(attr);
/* Get the resetcause value and store it */
/* Retrieve the reset cause */
attr->resetcause = g_reset_cause;
resetcause = getreg32(S32K1XX_RCM_SRS);
/* Convert the resetcause to a string */
/* Convert the resetcause to a string and save the linesize in case we are
* re-entered with f_pos > 0
*/
linesize = snprintf(attr->line, RESETCAUSE_LINELEN,
"0x%x", attr->resetcause);
/* Save the linesize in case we are re-entered with f_pos > 0 */
attr->linesize = linesize;
attr->linesize = snprintf(attr->line, S32K1XX_RESETCAUSE_LINELEN,
"0x%08" PRIx32, resetcause);
/* Transfer the system reset cause to user receive buffer */
offset = filep->f_pos;
ret = procfs_memcpy(attr->line, attr->linesize, buffer, buflen, &offset);
return ret;
return procfs_memcpy(attr->line, attr->linesize, buffer, buflen, &offset);
}
/****************************************************************************
* Name: resetcause_dup
* Name: s32k1xx_resetcause_procfs_dup
*
* Description:
* Duplicate open file data in the new file structure.
*
****************************************************************************/
static int resetcause_dup(FAR const struct file *oldp, FAR struct file *newp)
static int s32k1xx_resetcause_procfs_dup(FAR const struct file *oldp,
FAR struct file *newp)
{
FAR struct resetcause_file_s *oldattr;
FAR struct resetcause_file_s *newattr;
FAR struct s32k1xx_resetcause_procfs_file_s *oldattr;
FAR struct s32k1xx_resetcause_procfs_file_s *newattr;
finfo("Dup %p->%p\n", oldp, newp);
/* Recover our private data from the old struct file instance */
oldattr = (FAR struct resetcause_file_s *)oldp->f_priv;
oldattr = (FAR struct s32k1xx_resetcause_procfs_file_s *)oldp->f_priv;
DEBUGASSERT(oldattr);
/* Allocate a new container to hold the task and attribute selection */
newattr = kmm_malloc(sizeof(struct resetcause_file_s));
newattr = kmm_malloc(sizeof(struct s32k1xx_resetcause_procfs_file_s));
if (!newattr)
{
ferr("ERROR: Failed to allocate file attributes\n");
@ -253,7 +237,7 @@ static int resetcause_dup(FAR const struct file *oldp, FAR struct file *newp)
/* The copy the file attributes from the old attributes to the new */
memcpy(newattr, oldattr, sizeof(struct resetcause_file_s));
memcpy(newattr, oldattr, sizeof(struct s32k1xx_resetcause_procfs_file_s));
/* Save the new attributes in the new file structure */
@ -262,13 +246,14 @@ static int resetcause_dup(FAR const struct file *oldp, FAR struct file *newp)
}
/****************************************************************************
* Name: resetcause_stat
* Name: s32k1xx_resetcause_procfs_stat
*
* Description: Return information about a file or directory
*
****************************************************************************/
static int resetcause_stat(FAR const char *relpath, FAR struct stat *buf)
static int s32k1xx_resetcause_procfs_stat(FAR const char *relpath,
FAR struct stat *buf)
{
/* "resetcause" is the name for a read-only file */
@ -282,37 +267,19 @@ static int resetcause_stat(FAR const char *relpath, FAR struct stat *buf)
****************************************************************************/
/****************************************************************************
* Name: s32k1xx_resetcause_init
* Name: s32k1xx_resetcause_procfs_register
*
* Description: This function initializes the resetcause
* It will get the resetcause and store it
* Description:
* Register the S32K1XX Reset Cause procfs file system entry
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure
*
****************************************************************************/
void s32k1xx_resetcause_init(void)
int s32k1xx_resetcause_procfs_register(void)
{
uint32_t reset_cause_register = 0;
/* get the reset cause */
reset_cause_register = getreg32(S32K1XX_RCM_SRS);
/* save it in the global variable */
g_reset_cause = (unsigned int) reset_cause_register;
return procfs_register(&g_s32k1xx_resetcause_procfs);
}
/****************************************************************************
* Name: s32k1xx_resetcause_initialize_procfs
*
* Description: This function registrates the reset cause as a proc fs
* Returns 0 if OK, error number otherwise
*
****************************************************************************/
int s32k1xx_resetcause_initialize_procfs(void)
{
return procfs_register(&g_resetcause_procfs);
}
#endif /* CONFIG_RESET_CAUSE_PROC_FS && CONFIG_FS_PROCFS */
#endif /* CONFIG_S32K1XX_RESETCAUSE_PROCFS */

View File

@ -1,5 +1,5 @@
/****************************************************************************
* arch/arm/src/s32k1xx/s32k1xx_resetcause.h
* arch/arm/src/s32k1xx/s32k1xx_resetcause_procfs.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@ -18,46 +18,33 @@
*
****************************************************************************/
#ifndef __ARCH_ARM_SRC_S32K1XX_RESETCAUSE_H
#define __ARCH_ARM_SRC_S32K1XX_RESETCAUSE_H
#ifndef __ARCH_ARM_SRC_S32K1XX_S32K1XX_RESETCAUSE_PROCFS_H
#define __ARCH_ARM_SRC_S32K1XX_S32K1XX_RESETCAUSE_PROCFS_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_S32K1XX_RESETCAUSE_PROCFS
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: s32k1xx_resetcause_init
* Name: s32k1xx_resetcause_procfs_register
*
* Description: This function initializes the resetcause
* It will get the resetcause and store it
* Description:
* Register the S32K1XX Reset Cause procfs file system entry
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure
*
****************************************************************************/
void s32k1xx_resetcause_init(void);
int s32k1xx_resetcause_procfs_register(void);
/****************************************************************************
* Name: s32k1xx_resetcause_initialize_procfs
*
* Description: This function registrates the reset cause as a proc fs
* Returns 0 if OK, error number otherwise
*
****************************************************************************/
int s32k1xx_resetcause_initialize_procfs(void);
#endif /* __ARCH_ARM_SRC_S32K1XX_RESETCAUSE_H */
#endif /* CONFIG_S32K1XX_RESETCAUSE_PROCFS */
#endif /* __ARCH_ARM_SRC_S32K1XX_S32K1XX_RESETCAUSE_PROCFS_H */

View File

@ -38,6 +38,10 @@
#ifdef CONFIG_FS_PROCFS
# include <nuttx/fs/fs.h>
# ifdef CONFIG_S32K1XX_RESETCAUSE_PROCFS
# include "s32k1xx_resetcause_procfs.h"
# endif
#endif
#ifdef CONFIG_S32K1XX_PROGMEM
@ -93,6 +97,17 @@ int s32k1xx_bringup(void)
#endif
#ifdef CONFIG_FS_PROCFS
/* Register procfs entries before mounting */
# ifdef CONFIG_S32K1XX_RESETCAUSE_PROCFS
ret = s32k1xx_resetcause_procfs_register();
if (ret < 0)
{
syslog(LOG_ERR,
"ERROR: Failed to register resetcause procfs entry: %d\n", ret);
}
# endif /* CONFIG_RESETCAUSE_PROCFS */
/* Mount the procfs file system */
ret = nx_mount(NULL, "/proc", "procfs", 0, NULL);