From e6fe403b033ebda2bb47f0eeba4d36fa41cd3ba7 Mon Sep 17 00:00:00 2001 From: Nandkumar Joshi Date: Fri, 13 Mar 2020 01:17:10 +0530 Subject: [PATCH] Minor changes in I2C. Fix formatting. GPIO enable IO fix. GPIO IOF register fix. --- metal/drivers/sifive_pwm0.h | 6 ++++-- metal/i2c.h | 2 +- metal/pwm.h | 6 ++++-- src/drivers/sifive_gpio0.c | 2 +- src/drivers/sifive_i2c0.c | 2 +- src/drivers/sifive_pwm0.c | 20 +++++++++++--------- src/i2c.c | 2 +- src/pwm.c | 12 ++++++------ 8 files changed, 29 insertions(+), 23 deletions(-) diff --git a/metal/drivers/sifive_pwm0.h b/metal/drivers/sifive_pwm0.h index b8efc70f..e3874270 100644 --- a/metal/drivers/sifive_pwm0.h +++ b/metal/drivers/sifive_pwm0.h @@ -5,13 +5,15 @@ #define METAL__DRIVERS__SIFIVE_PWM0_H #include -#include #include struct __metal_driver_vtable_sifive_pwm0 { const struct metal_pwm_vtable pwm; }; +/* Max possible PWM channel count */ +#define METAL_MAX_PWM_CHANNELS 16 + __METAL_DECLARE_VTABLE(__metal_driver_vtable_sifive_pwm0) struct __metal_driver_sifive_pwm0 { @@ -19,7 +21,7 @@ struct __metal_driver_sifive_pwm0 { unsigned int max_count; unsigned int count_val; unsigned int freq; - unsigned int duty[METAL_MAX_PWM0_NCMP]; + unsigned int duty[METAL_MAX_PWM_CHANNELS]; metal_clock_callback pre_rate_change_callback; metal_clock_callback post_rate_change_callback; }; diff --git a/metal/i2c.h b/metal/i2c.h index ec45573f..baf62e5d 100644 --- a/metal/i2c.h +++ b/metal/i2c.h @@ -37,7 +37,7 @@ struct metal_i2c { /*! @brief Get a handle for a I2C device. * @param device_num The index of the desired I2C device. * @return A handle to the I2C device, or NULL if the device does not exist.*/ -struct metal_i2c *metal_i2c_get_device(int device_num); +struct metal_i2c *metal_i2c_get_device(unsigned int device_num); /*! @brief Initialize a I2C device with a certain baud rate. * @param i2c The handle for the I2C device to initialize. diff --git a/metal/pwm.h b/metal/pwm.h index a6d37a4d..8096635f 100644 --- a/metal/pwm.h +++ b/metal/pwm.h @@ -33,7 +33,8 @@ struct metal_pwm_vtable { metal_pwm_phase_correct_t phase_corr); unsigned int (*get_duty)(struct metal_pwm *pwm, unsigned int idx); unsigned int (*get_freq)(struct metal_pwm *pwm, unsigned int idx); - int (*trigger)(struct metal_pwm *pwm, unsigned int idx, metal_pwm_run_mode_t mode); + int (*trigger)(struct metal_pwm *pwm, unsigned int idx, + metal_pwm_run_mode_t mode); int (*stop)(struct metal_pwm *pwm, unsigned int idx); int (*cfg_interrupt)(struct metal_pwm *pwm, metal_pwm_interrupt_t flag); int (*clr_interrupt)(struct metal_pwm *pwm, unsigned int idx); @@ -110,7 +111,8 @@ inline unsigned int metal_pwm_get_freq(struct metal_pwm *pwm, * @param pwm PWM device handle. * @param idx PWM channel id. * @return 0 If no error.*/ -inline int metal_pwm_trigger(struct metal_pwm *pwm, unsigned int idx, metal_pwm_run_mode_t mode) { +inline int metal_pwm_trigger(struct metal_pwm *pwm, unsigned int idx, + metal_pwm_run_mode_t mode) { return pwm->vtable->trigger(pwm, idx, mode); } diff --git a/src/drivers/sifive_gpio0.c b/src/drivers/sifive_gpio0.c index c536c324..1324eb6e 100644 --- a/src/drivers/sifive_gpio0.c +++ b/src/drivers/sifive_gpio0.c @@ -100,7 +100,7 @@ int __metal_driver_sifive_gpio0_enable_io(struct metal_gpio *ggpio, long source, long base = __metal_driver_sifive_gpio0_base(ggpio); __METAL_ACCESS_ONCE( - (__metal_io_u32 *)(base + METAL_SIFIVE_GPIO0_IOF_SEL)) &= ~source; + (__metal_io_u32 *)(base + METAL_SIFIVE_GPIO0_IOF_SEL)) |= source; __METAL_ACCESS_ONCE((__metal_io_u32 *)(base + METAL_SIFIVE_GPIO0_IOF_EN)) |= dest; diff --git a/src/drivers/sifive_i2c0.c b/src/drivers/sifive_i2c0.c index e8ecc584..ec79bbc3 100644 --- a/src/drivers/sifive_i2c0.c +++ b/src/drivers/sifive_i2c0.c @@ -260,7 +260,7 @@ static int __metal_driver_sifive_i2c0_write(struct metal_i2c *i2c, } } else { - /* Write address failed */ + /* I2C device not initialized, return error */ METAL_I2C_LOG("I2C device not initialized.\n"); ret = METAL_I2C_RET_ERR; } diff --git a/src/drivers/sifive_pwm0.c b/src/drivers/sifive_pwm0.c index a602fcdc..087efd4b 100644 --- a/src/drivers/sifive_pwm0.c +++ b/src/drivers/sifive_pwm0.c @@ -40,12 +40,14 @@ #error *** Unsupported endianess *** #endif +#if (METAL_MAX_PWM0_NCMP > METAL_MAX_PWM_CHANNELS) +#error *** METAL_MAX_PWM_CHANNELS exceeded *** +#endif + /* Return values */ #define METAL_PWM_RET_OK 0 #define METAL_PWM_RET_ERR -1 -#define METAL_PWM_DEBUG - static void pre_rate_change_callback(void *priv) { struct metal_pwm *gpwm = priv; /* Disable active PWM instance. */ @@ -172,8 +174,8 @@ static int __metal_driver_sifive_pwm0_set_freq(struct metal_pwm *gpwm, #if defined(METAL_PWM_DEBUG) printf("PWM requested freq:%u set freq:%u \n", freq, pwm->freq); - printf("CPU Clk:%u Prescale:%u Count:%u \n", clock_rate, - prescale, count); + printf("CPU Clk:%u Prescale:%u Count:%u \n", clock_rate, prescale, + count); #endif } return ret; @@ -222,12 +224,12 @@ static unsigned int __metal_driver_sifive_pwm0_get_duty(struct metal_pwm *gpwm, return duty; } -static unsigned int -__metal_driver_sifive_pwm0_get_freq(struct metal_pwm *gpwm, unsigned int idx) { +static unsigned int __metal_driver_sifive_pwm0_get_freq(struct metal_pwm *gpwm, + unsigned int idx) { struct __metal_driver_sifive_pwm0 *pwm = (void *)gpwm; unsigned int freq = 0; - (void)idx;/* Unused parameter, no support for per channel frequency */ + (void)idx; /* Unused parameter, no support for per channel frequency */ /* Check for valid parameters and get configured PWM frequency value */ if (pwm != NULL) { @@ -242,7 +244,7 @@ static int __metal_driver_sifive_pwm0_trigger(struct metal_pwm *gpwm, unsigned long base = __metal_driver_sifive_pwm0_control_base(gpwm); int ret = METAL_PWM_RET_ERR; - (void)idx;/* Unused parameter,for later use */ + (void)idx; /* Unused parameter,for later use */ if (base != 0) { /* Configure for requested PWM run mode */ @@ -265,7 +267,7 @@ static int __metal_driver_sifive_pwm0_stop(struct metal_pwm *gpwm, unsigned long base = __metal_driver_sifive_pwm0_control_base(gpwm); int ret = METAL_PWM_RET_ERR; - (void)idx;/* Unused parameter,for later use */ + (void)idx; /* Unused parameter,for later use */ if (base != 0) { /* Disable always running mode */ diff --git a/src/i2c.c b/src/i2c.c index f9e77225..6c342aa3 100644 --- a/src/i2c.c +++ b/src/i2c.c @@ -18,7 +18,7 @@ extern inline int metal_i2c_transfer(struct metal_i2c *i2c, unsigned int addr, extern inline int metal_i2c_get_baud_rate(struct metal_i2c *i2c); extern inline int metal_i2c_set_baud_rate(struct metal_i2c *i2c, int baud_rate); -struct metal_i2c *metal_i2c_get_device(int device_num) { +struct metal_i2c *metal_i2c_get_device(unsigned int device_num) { #if __METAL_DT_MAX_I2CS > 0 if (device_num < __METAL_DT_MAX_I2CS) { return (struct metal_i2c *)__metal_i2c_table[device_num]; diff --git a/src/pwm.c b/src/pwm.c index e65880c7..c263270f 100644 --- a/src/pwm.c +++ b/src/pwm.c @@ -15,8 +15,7 @@ extern inline unsigned int metal_pwm_get_duty(struct metal_pwm *pwm, unsigned int idx); extern inline unsigned int metal_pwm_get_freq(struct metal_pwm *pwm, unsigned int idx); -extern inline int metal_pwm_trigger(struct metal_pwm *pwm, - unsigned int idx, +extern inline int metal_pwm_trigger(struct metal_pwm *pwm, unsigned int idx, metal_pwm_run_mode_t mode); extern inline int metal_pwm_stop(struct metal_pwm *pwm, unsigned int idx); extern inline int metal_pwm_cfg_interrupt(struct metal_pwm *pwm, @@ -28,9 +27,10 @@ metal_pwm_interrupt_controller(struct metal_pwm *pwm); extern int metal_pwm_get_interrupt_id(struct metal_pwm *pwm, unsigned int idx); struct metal_pwm *metal_pwm_get_device(unsigned int device_num) { - if (device_num >= __METAL_DT_MAX_PWMS) { - return NULL; +#if __METAL_DT_MAX_PWMS > 0 + if (device_num < __METAL_DT_MAX_PWMS) { + return (struct metal_pwm *)__metal_pwm_table[device_num]; } - - return (struct metal_pwm *)__metal_pwm_table[device_num]; +#endif + return NULL; }