Review of PR 135

This commit is contained in:
Gregory Nutt 2016-09-21 09:36:39 -06:00
parent 28b4082020
commit 54eee5b303
2 changed files with 102 additions and 97 deletions

View File

@ -60,28 +60,6 @@
#include "chip/tiva_pinmap.h"
#include "chip/tm4c_memorymap.h"
/************************************************************************************
* Pre-processor Definitions
************************************************************************************/
#define qeierr(fmt, args...) printf("%s(%d): " fmt, __FUNCTION__, __LINE__, ##args);
#ifndef CONFIG_DEBUG
# undef CONFIG_DEBUG_QE
#endif
#ifdef CONFIG_DEBUG_QEI
# define qeidbg dbg
# ifdef CONFIG_DEBUG_VERBOSE
# define qeivdbg vdbg
# else
# define qeivdbg(x...)
# endif
#else
# define qeidbg(x...)
# define qeivdbg(x...)
#endif
/************************************************************************************
* Private Types
************************************************************************************/
@ -101,14 +79,18 @@ struct tiva_qe_s
* Private Function Prototypes
************************************************************************************/
static inline void tiva_qe_putreg(struct tiva_qe_s *qe, unsigned int offset, uint32_t regval);
static inline uint32_t tiva_qe_getreg(struct tiva_qe_s *qe, unsigned int offset);
static inline void tiva_qe_putreg(struct tiva_qe_s *qe, unsigned int offset,
uint32_t regval);
static inline uint32_t tiva_qe_getreg(struct tiva_qe_s *qe,
unsigned int offset);
static int tiva_qe_setup(FAR struct qe_lowerhalf_s *lower);
static int tiva_qe_shutdown(FAR struct qe_lowerhalf_s *lower);
static int tiva_qe_position(FAR struct qe_lowerhalf_s *lower, FAR int32_t *pos);
static int tiva_qe_position(FAR struct qe_lowerhalf_s *lower,
FAR int32_t * pos);
static int tiva_qe_reset(FAR struct qe_lowerhalf_s *lower);
static int tiva_qe_ioctl(FAR struct qe_lowerhalf_s *lower, int cmd, unsigned long arg);
static int tiva_qe_ioctl(FAR struct qe_lowerhalf_s *lower, int cmd,
unsigned long arg);
static int tiva_qe_direction(struct tiva_qe_s *qe, unsigned long *dir);
static int tiva_qe_velocity(struct tiva_qe_s *qe, unsigned long *vel);
@ -119,26 +101,26 @@ static int tiva_qe_velocity(struct tiva_qe_s *qe, unsigned long *vel);
static const struct qe_ops_s g_qe_ops =
{
.setup = tiva_qe_setup,
.setup = tiva_qe_setup,
.shutdown = tiva_qe_shutdown,
.position = tiva_qe_position,
.reset = tiva_qe_reset,
.ioctl = tiva_qe_ioctl,
.reset = tiva_qe_reset,
.ioctl = tiva_qe_ioctl,
};
#ifdef CONFIG_TIVA_QEI0
static struct tiva_qe_s g_qe0 =
{
.ops = &g_qe_ops,
.id = 0,
.base = TIVA_QEI0_BASE,
.idx = GPIO_QEI0_IDX,
.pha = GPIO_QEI0_PHA,
.phb = GPIO_QEI0_PHB,
.ops = &g_qe_ops,
.id = 0,
.base = TIVA_QEI0_BASE,
.idx = GPIO_QEI0_IDX,
.pha = GPIO_QEI0_PHA,
.phb = GPIO_QEI0_PHB,
# ifdef CONFIG_TIVA_QEI0_PULSES
.pulses = CONFIG_TIVA_QEI0_PULSES,
.pulses = CONFIG_TIVA_QEI0_PULSES,
# else
.pulses = 0,
.pulses = 0,
# endif
};
#endif
@ -146,16 +128,16 @@ static struct tiva_qe_s g_qe0 =
#ifdef CONFIG_TIVA_QEI1
static struct tiva_qe_s g_qe1 =
{
.ops = &g_qe_ops,
.id = 1,
.base = TIVA_QEI1_BASE,
.idx = GPIO_QEI1_IDX,
.pha = GPIO_QEI1_PHA,
.phb = GPIO_QEI1_PHB,
.ops = &g_qe_ops,
.id = 1,
.base = TIVA_QEI1_BASE,
.idx = GPIO_QEI1_IDX,
.pha = GPIO_QEI1_PHA,
.phb = GPIO_QEI1_PHB,
# ifdef CONFIG_TIVA_QEI1_PULSES
.pulses = CONFIG_TIVA_QEI1_PUSLSE,
.pulses = CONFIG_TIVA_QEI1_PUSLSE,
# else
.pulses = 0,
.pulses = 0,
# endif
};
#endif
@ -186,7 +168,8 @@ static inline uint32_t tiva_qe_getreg(struct tiva_qe_s *qe, unsigned int offset)
*
************************************************************************************/
static inline void tiva_qe_putreg(struct tiva_qe_s *qe, unsigned int offset, uint32_t regval)
static inline void tiva_qe_putreg(struct tiva_qe_s *qe, unsigned int offset,
uint32_t regval)
{
uintptr_t regaddr = qe->base + offset;
putreg32(regval, regaddr);
@ -213,57 +196,68 @@ static int tiva_qe_setup(FAR struct qe_lowerhalf_s *lower)
{
uint32_t ctlreg = 0;
FAR struct tiva_qe_s *qe = (FAR struct tiva_qe_s *)lower;
qeidbg("setup QEI %d\n", qe->id);
int ret;
sninfo("setup QEI %d\n", qe->id);
/* Enable GPIO port, GPIO pin type and GPIO alternate function */
/* (refer to TM4C1294NC 24.4.2-4) */
int ret;
ret = tiva_configgpio(qe->idx);
if (ret < 0)
{
qeierr("tiva_configgpio failed (%x)\n", qe->idx);
snerr("ERROR: tiva_configgpio failed (%x)\n", qe->idx);
return -1;
}
ret = tiva_configgpio(qe->pha);
if (ret < 0)
{
qeierr("tiva_configgpio failed (%x)\n", qe->pha);
snerr("ERROR: tiva_configgpio failed (%x)\n", qe->pha);
return -1;
}
ret = tiva_configgpio(qe->phb);
if (ret < 0)
{
qeierr("tiva_configgpio failed (%x)\n", qe->phb);
snerr("ERROR: tiva_configgpio failed (%x)\n", qe->phb);
return -1;
}
/* Set reset mode */
/* (refer to TM4C1294NC 24.4.5.1) */
if (qe->pulses == 0) {
ctlreg = RESMODE_BY_INDEX_PULSE << TIVA_QEI_CTL_RESMODE;
} else {
ctlreg = RESMODE_BY_MAXPOS << TIVA_QEI_CTL_RESMODE;
}
if (qe->pulses == 0)
{
ctlreg = RESMODE_BY_INDEX_PULSE << TIVA_QEI_CTL_RESMODE;
}
else
{
ctlreg = RESMODE_BY_MAXPOS << TIVA_QEI_CTL_RESMODE;
}
tiva_qe_putreg(qe, TIVA_QEI_CTL_OFFSET, ctlreg);
/* Set capture mode (PHA_AND_PHB) */
/* (refer to TM4C1294NC 24.4.5.1) */
ctlreg = tiva_qe_getreg(qe, TIVA_QEI_CTL_OFFSET);
ctlreg |= CAPMODE_PHA_AND_PHB << TIVA_QEI_CTL_CAPMODE;
tiva_qe_putreg(qe, TIVA_QEI_CTL_OFFSET, ctlreg);
/* Set maxpos */
/* (refer to TM4C1294NC 24.4.5.2) */
tiva_qe_putreg(qe, TIVA_QEI_MAXPOS_OFFSET, qe->pulses * 4);
/* Enable velocity capture */
ctlreg = tiva_qe_getreg(qe, TIVA_QEI_CTL_OFFSET);
ctlreg |= VELEN_ENABLE << TIVA_QEI_CTL_VELEN;
tiva_qe_putreg(qe, TIVA_QEI_CTL_OFFSET, ctlreg);
/* Set prediv (1) */
ctlreg = tiva_qe_getreg(qe, TIVA_QEI_CTL_OFFSET);
ctlreg |= VELDIV_1 << TIVA_QEI_CTL_VELDIV;
tiva_qe_putreg(qe, TIVA_QEI_CTL_OFFSET, ctlreg);
@ -273,6 +267,7 @@ static int tiva_qe_setup(FAR struct qe_lowerhalf_s *lower)
/* Enable the QEI */
/* (refer to TM4C1294NC 24.4.6) */
ctlreg = tiva_qe_getreg(qe, TIVA_QEI_CTL_OFFSET);
ctlreg |= QEI_ENABLE << TIVA_QEI_CTL_ENABLE;
tiva_qe_putreg(qe, TIVA_QEI_CTL_OFFSET, ctlreg);
@ -298,13 +293,15 @@ static int tiva_qe_setup(FAR struct qe_lowerhalf_s *lower)
static int tiva_qe_shutdown(FAR struct qe_lowerhalf_s *lower)
{
FAR struct tiva_qe_s *qe = (FAR struct tiva_qe_s *)lower;
qeidbg("shutdown QEI %d\n", qe->id);
FAR struct tiva_qe_s *qe = (FAR struct tiva_qe_s *)lower;
/* Disable the QEI */
tiva_qe_putreg(qe, TIVA_SYSCON_SRQEI_OFFSET, SYSCON_SRQEI(qe->id));
sninfo("shutdown QEI %d\n", qe->id);
return OK;
/* Disable the QEI */
tiva_qe_putreg(qe, TIVA_SYSCON_SRQEI_OFFSET, SYSCON_SRQEI(qe->id));
return OK;
}
/****************************************************************************
@ -323,12 +320,13 @@ static int tiva_qe_shutdown(FAR struct qe_lowerhalf_s *lower)
static int tiva_qe_reset(FAR struct qe_lowerhalf_s *lower)
{
FAR struct tiva_qe_s *qe = (FAR struct tiva_qe_s *)lower;
qeidbg("reset QEI %d\n", qe->id);
FAR struct tiva_qe_s *qe = (FAR struct tiva_qe_s *)lower;
tiva_qe_putreg(qe, TIVA_QEI_POS_OFFSET, 0);
sninfo("reset QEI %d\n", qe->id);
return OK;
tiva_qe_putreg(qe, TIVA_QEI_POS_OFFSET, 0);
return OK;
}
/****************************************************************************
@ -346,15 +344,16 @@ static int tiva_qe_reset(FAR struct qe_lowerhalf_s *lower)
*
****************************************************************************/
static int tiva_qe_position(FAR struct qe_lowerhalf_s *lower, FAR int32_t *pos)
static int tiva_qe_position(FAR struct qe_lowerhalf_s *lower, FAR int32_t * pos)
{
FAR struct tiva_qe_s *qe = (FAR struct tiva_qe_s *)lower;
qeidbg("get position of QEI %d\n", qe->id);
FAR struct tiva_qe_s *qe = (FAR struct tiva_qe_s *)lower;
/* (refer to TM4C1294NC 24.4.8) */
*pos = (int32_t)tiva_qe_getreg(qe, TIVA_QEI_POS_OFFSET);
sninfo("get position of QEI %d\n", qe->id);
return OK;
/* (refer to TM4C1294NC 24.4.8) */
*pos = (int32_t) tiva_qe_getreg(qe, TIVA_QEI_POS_OFFSET);
return OK;
}
/****************************************************************************
@ -373,22 +372,26 @@ static int tiva_qe_position(FAR struct qe_lowerhalf_s *lower, FAR int32_t *pos)
*
****************************************************************************/
static int tiva_qe_ioctl(FAR struct qe_lowerhalf_s *lower, int cmd, unsigned long arg)
static int tiva_qe_ioctl(FAR struct qe_lowerhalf_s *lower, int cmd,
unsigned long arg)
{
FAR struct tiva_qe_s *qe = (FAR struct tiva_qe_s *)lower;
qeidbg("ioctl QEI %d\n", qe->id);
sninfo("ioctl QEI %d\n", qe->id);
switch (cmd)
{
case QEIOC_DIRECTION:
tiva_qe_direction(qe, (unsigned long *)arg);
break;
case QEIOC_VELOCITY:
tiva_qe_velocity(qe, (unsigned long *)arg);
break;
default:
qeierr("invalid cmd %x\n", cmd);
break;
case QEIOC_DIRECTION:
tiva_qe_direction(qe, (unsigned long *)arg);
break;
case QEIOC_VELOCITY:
tiva_qe_velocity(qe, (unsigned long *)arg);
break;
default:
snerr("ERROR: invalid cmd %x\n", cmd);
break;
}
return OK;
@ -411,17 +414,18 @@ static int tiva_qe_ioctl(FAR struct qe_lowerhalf_s *lower, int cmd, unsigned lon
static int tiva_qe_direction(FAR struct tiva_qe_s *qe, unsigned long *dir)
{
qeidbg("get direction of QEI %d\n", qe->id);
sninfo("get direction of QEI %d\n", qe->id);
uint32_t statreg;
statreg = tiva_qe_getreg(qe, TIVA_QEI_STAT_OFFSET);
uint32_t statreg;
statreg = tiva_qe_getreg(qe, TIVA_QEI_STAT_OFFSET);
int32_t dirbit;
dirbit = (statreg & (1 << TIVA_QEI_STAT_DIRECTION)) == DIRECTION_FORWARD ? 1 : -1;
int32_t dirbit;
dirbit =
(statreg & (1 << TIVA_QEI_STAT_DIRECTION)) == DIRECTION_FORWARD ? 1 : -1;
*dir = dirbit;
*dir = dirbit;
return OK;
return OK;
}
/****************************************************************************
@ -440,11 +444,11 @@ static int tiva_qe_direction(FAR struct tiva_qe_s *qe, unsigned long *dir)
static int tiva_qe_velocity(FAR struct tiva_qe_s *qe, unsigned long *vel)
{
qeidbg("get direction of QEI %d\n", qe->id);
sninfo("get direction of QEI %d\n", qe->id);
*vel = (int32_t)tiva_qe_getreg(qe, TIVA_QEI_SPEED_OFFSET);
*vel = (int32_t) tiva_qe_getreg(qe, TIVA_QEI_SPEED_OFFSET);
return OK;
return OK;
}
/************************************************************************************
@ -465,7 +469,6 @@ static int tiva_qe_velocity(FAR struct tiva_qe_s *qe, unsigned long *vel)
FAR struct qe_lowerhalf_s *tiva_qei_initialize(int id)
{
assert(id >= 0);
FAR struct tiva_qe_s *qe;
FAR struct qe_lowerhalf_s *lower;
@ -484,15 +487,17 @@ FAR struct qe_lowerhalf_s *tiva_qei_initialize(int id)
#endif
default:
qeierr("ERROR: invalid QEI %d\n", id);
snerr("ERROR: invalid QEI %d\n", id);
return NULL;
}
/* Enable QEI clock (refer to TM4C1294NC 24.4.1) */
tiva_qei_enablepwr(qe->id);
tiva_qei_enableclk(qe->id);
/* Make sure that the QEI enable bit has been cleared */
lower = (FAR struct qe_lowerhalf_s *)qe;
tiva_qe_shutdown(lower);

View File

@ -171,11 +171,11 @@ static void tm4c_i2ctool(void)
****************************************************************************/
#ifdef HAVE_PWM
void tm4c_pwm_register(int channel)
static void tm4c_pwm_register(int channel)
{
FAR struct pwm_lowerhalf_s *dev;
int ret;
char pwm_path[PWM_PATH_FMTLEN];
int ret;
dev = tiva_pwm_initialize(channel);
if (dev == NULL)
@ -247,7 +247,7 @@ static void tm4c_pwm(void)
*
****************************************************************************/
void tm4c_qei_register(int id)
static void tm4c_qei_register(int id)
{
FAR struct qe_lowerhalf_s *dev;
int ret;