Skip to content

Commit

Permalink
Minor changes in I2C. Fix formatting. GPIO enable IO fix.
Browse files Browse the repository at this point in the history
GPIO IOF register fix.
  • Loading branch information
NandkumarJoshi committed Mar 16, 2020
1 parent dffd969 commit e6fe403
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 23 deletions.
6 changes: 4 additions & 2 deletions metal/drivers/sifive_pwm0.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
#define METAL__DRIVERS__SIFIVE_PWM0_H

#include <metal/clock.h>
#include <metal/machine.h>
#include <metal/pwm.h>

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 {
struct metal_pwm pwm;
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;
};
Expand Down
2 changes: 1 addition & 1 deletion metal/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions metal/pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/drivers/sifive_gpio0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/drivers/sifive_i2c0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 11 additions & 9 deletions src/drivers/sifive_pwm0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 */
Expand All @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
12 changes: 6 additions & 6 deletions src/pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}

0 comments on commit e6fe403

Please sign in to comment.