1。代码优化。
This commit is contained in:
@@ -46,6 +46,7 @@ static int adc_channel_get_state(struct mr_adc *adc, int channel)
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
/* Check if the channel is enabled */
|
||||
return mr_bits_is_set(adc->channel, (1 << channel));
|
||||
}
|
||||
|
||||
@@ -63,8 +64,7 @@ static int mr_adc_close(struct mr_dev *dev)
|
||||
struct mr_adc_ops *ops = (struct mr_adc_ops *)dev->drv->ops;
|
||||
|
||||
/* Disable all channels */
|
||||
int i = 0;
|
||||
for (i = 0; i < 32; i++)
|
||||
for (int i = 0; i < 32; i++)
|
||||
{
|
||||
if (mr_bits_is_set(adc->channel, (1 << i)) == MR_ENABLE)
|
||||
{
|
||||
|
||||
@@ -376,7 +376,7 @@ int mr_can_dev_register(struct mr_can_dev *can_dev, const char *name, int id, in
|
||||
can_dev->id = id;
|
||||
can_dev->ide = ide;
|
||||
|
||||
/* Register the can-dev */
|
||||
/* Register the can-device */
|
||||
return mr_dev_register(&can_dev->dev, name, Mr_Dev_Type_CAN, MR_SFLAG_RDWR | MR_SFLAG_NONDRV, &ops, MR_NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ static int dac_channel_get_state(struct mr_dac *dac, int channel)
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
/* Check if the channel is enabled */
|
||||
return mr_bits_is_set(dac->channel, (1 << channel));
|
||||
}
|
||||
|
||||
@@ -63,8 +64,7 @@ static int mr_dac_close(struct mr_dev *dev)
|
||||
struct mr_dac_ops *ops = (struct mr_dac_ops *)dev->drv->ops;
|
||||
|
||||
/* Disable all channels */
|
||||
int i = 0;
|
||||
for (i = 0; i < 32; i++)
|
||||
for (int i = 0; i < 32; i++)
|
||||
{
|
||||
if (mr_bits_is_set(dac->channel, (1 << i)) == MR_ENABLE)
|
||||
{
|
||||
|
||||
13
device/i2c.c
13
device/i2c.c
@@ -18,6 +18,7 @@ static int mr_i2c_bus_open(struct mr_dev *dev)
|
||||
/* Reset the hold */
|
||||
i2c_bus->hold = MR_FALSE;
|
||||
|
||||
/* Default address is 0x00 and 7-bit */
|
||||
return ops->configure(i2c_bus, &i2c_bus->config, 0x00, MR_I2C_ADDR_BITS_7);
|
||||
}
|
||||
|
||||
@@ -32,12 +33,12 @@ static int mr_i2c_bus_close(struct mr_dev *dev)
|
||||
|
||||
static ssize_t mr_i2c_bus_read(struct mr_dev *dev, int off, void *buf, size_t size, int async)
|
||||
{
|
||||
return MR_EIO;
|
||||
return MR_ENOTSUP;
|
||||
}
|
||||
|
||||
static ssize_t mr_i2c_bus_write(struct mr_dev *dev, int off, const void *buf, size_t size, int async)
|
||||
{
|
||||
return MR_EIO;
|
||||
return MR_ENOTSUP;
|
||||
}
|
||||
|
||||
static ssize_t mr_i2c_bus_isr(struct mr_dev *dev, int event, void *args)
|
||||
@@ -116,6 +117,7 @@ MR_INLINE int i2c_dev_take_bus(struct mr_i2c_dev *i2c_dev)
|
||||
return MR_EBUSY;
|
||||
}
|
||||
|
||||
/* If the owner changes, recheck the configuration */
|
||||
if (i2c_dev != i2c_bus->owner)
|
||||
{
|
||||
/* Reconfigure the bus */
|
||||
@@ -221,7 +223,6 @@ static int mr_i2c_dev_open(struct mr_dev *dev)
|
||||
{
|
||||
struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev;
|
||||
|
||||
/* Allocate FIFO buffers */
|
||||
return mr_ringbuf_allocate(&i2c_dev->rd_fifo, i2c_dev->rd_bufsz);
|
||||
}
|
||||
|
||||
@@ -229,7 +230,6 @@ static int mr_i2c_dev_close(struct mr_dev *dev)
|
||||
{
|
||||
struct mr_i2c_dev *i2c_dev = (struct mr_i2c_dev *)dev;
|
||||
|
||||
/* Free FIFO buffers */
|
||||
mr_ringbuf_free(&i2c_dev->rd_fifo);
|
||||
return MR_EOK;
|
||||
}
|
||||
@@ -246,9 +246,9 @@ static ssize_t mr_i2c_dev_read(struct mr_dev *dev, int off, void *buf, size_t si
|
||||
|
||||
if (i2c_dev->config.host_slave == MR_I2C_HOST)
|
||||
{
|
||||
/* Send the address of the register that needs to be read */
|
||||
if (off >= 0)
|
||||
{
|
||||
/* Send the address of the register that needs to be read */
|
||||
i2c_dev_send_addr(i2c_dev, MR_I2C_WR);
|
||||
i2c_dev_write(i2c_dev, (uint8_t *)&off, (i2c_dev->config.reg_bits >> 3));
|
||||
}
|
||||
@@ -284,9 +284,10 @@ static ssize_t mr_i2c_dev_write(struct mr_dev *dev, int off, const void *buf, si
|
||||
if (i2c_dev->config.host_slave == MR_I2C_HOST)
|
||||
{
|
||||
i2c_dev_send_addr(i2c_dev, MR_I2C_WR);
|
||||
|
||||
/* Send the address of the register that needs to be written */
|
||||
if (off >= 0)
|
||||
{
|
||||
/* Send the address of the register that needs to be written */
|
||||
i2c_dev_write(i2c_dev, (uint8_t *)&off, (i2c_dev->config.reg_bits >> 3));
|
||||
}
|
||||
|
||||
|
||||
12
device/pin.c
12
device/pin.c
@@ -35,9 +35,11 @@ static int pin_set_mode(struct mr_pin *pin, int number, int mode)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Disable interrupt */
|
||||
mr_interrupt_disable();
|
||||
|
||||
/* If the irq exists, update it */
|
||||
struct mr_list *list = MR_NULL;
|
||||
for (list = pin->irq_list.next; list != &pin->irq_list; list = list->next)
|
||||
for (struct mr_list *list = pin->irq_list.next; list != &pin->irq_list; list = list->next)
|
||||
{
|
||||
struct pin_irq *irq = (struct pin_irq *)mr_container_of(list, struct pin_irq, list);
|
||||
if (irq->number == number)
|
||||
@@ -53,6 +55,9 @@ static int pin_set_mode(struct mr_pin *pin, int number, int mode)
|
||||
irq->desc = pin->dev.rd_call.desc;
|
||||
irq->call = pin->dev.rd_call.call;
|
||||
}
|
||||
|
||||
/* Enable interrupt */
|
||||
mr_interrupt_enable();
|
||||
return MR_EOK;
|
||||
}
|
||||
}
|
||||
@@ -70,6 +75,9 @@ static int pin_set_mode(struct mr_pin *pin, int number, int mode)
|
||||
mr_list_insert_before(&pin->irq_list, &irq->list);
|
||||
}
|
||||
}
|
||||
|
||||
/* Enable interrupt */
|
||||
mr_interrupt_enable();
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
|
||||
18
device/pwm.c
18
device/pwm.c
@@ -67,9 +67,7 @@ static int pwm_calculate(struct mr_pwm *pwm, uint32_t freq)
|
||||
{
|
||||
uint32_t clk = pwm->info->clk, psc_max = pwm->info->prescaler_max, per_max = pwm->info->period_max;
|
||||
uint32_t psc_best = 0, per_best = 0;
|
||||
uint32_t psc = 0, per = 0;
|
||||
uint32_t timeout = 0;
|
||||
int error = 0, error_min = INT32_MAX;
|
||||
int error_min = INT32_MAX;
|
||||
|
||||
/* Check the clock */
|
||||
if (clk == 0 || freq == 0)
|
||||
@@ -78,15 +76,15 @@ static int pwm_calculate(struct mr_pwm *pwm, uint32_t freq)
|
||||
}
|
||||
|
||||
/* Calculate the timeout */
|
||||
timeout = (clk * 1000000) / freq;
|
||||
uint32_t timeout = (clk * 1000000) / freq;
|
||||
|
||||
/* Calculate the Least error period */
|
||||
for (per = (timeout <= per_max) ? timeout : (timeout / (per_max + 1)); per > 0; per--)
|
||||
for (uint32_t per = (timeout <= per_max) ? timeout : (timeout / (per_max + 1)); per > 0; per--)
|
||||
{
|
||||
psc = timeout / per;
|
||||
uint32_t psc = timeout / per;
|
||||
|
||||
/* Calculate the error */
|
||||
error = (int)timeout - (int)(psc * per);
|
||||
int error = (int)timeout - (int)(psc * per);
|
||||
if (error == 0)
|
||||
{
|
||||
psc_best = psc;
|
||||
@@ -120,7 +118,7 @@ static int pwm_calculate(struct mr_pwm *pwm, uint32_t freq)
|
||||
break;
|
||||
}
|
||||
|
||||
/* Check if reload can be used as period or prescaler */
|
||||
/* Check if prescaler can be used as period */
|
||||
if ((psc_best > per_best) && (psc_best < per_max))
|
||||
{
|
||||
mr_swap(per_best, psc_best);
|
||||
@@ -154,8 +152,7 @@ static int mr_pwm_close(struct mr_dev *dev)
|
||||
struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops;
|
||||
|
||||
/* Disable all channels */
|
||||
int i = 0;
|
||||
for (i = 0; i < 32; i++)
|
||||
for (int i = 0; i < 32; i++)
|
||||
{
|
||||
if (mr_bits_is_set(pwm->channel, (1 << i)) == MR_ENABLE)
|
||||
{
|
||||
@@ -318,6 +315,7 @@ int mr_pwm_register(struct mr_pwm *pwm, const char *name, struct mr_drv *drv, st
|
||||
pwm->channel_polarity = 0;
|
||||
pwm->info = info;
|
||||
|
||||
/* Register the pwm */
|
||||
return mr_dev_register(&pwm->dev, name, Mr_Dev_Type_PWM, MR_SFLAG_RDWR, &ops, drv);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ static int mr_serial_open(struct mr_dev *dev)
|
||||
struct mr_serial *serial = (struct mr_serial *)dev;
|
||||
struct mr_serial_ops *ops = (struct mr_serial_ops *)dev->drv->ops;
|
||||
|
||||
/* Allocate FIFO buffers */
|
||||
int ret = mr_ringbuf_allocate(&serial->rd_fifo, serial->rd_bufsz);
|
||||
if (ret != MR_EOK)
|
||||
{
|
||||
@@ -36,7 +35,6 @@ static int mr_serial_close(struct mr_dev *dev)
|
||||
struct mr_serial_ops *ops = (struct mr_serial_ops *)dev->drv->ops;
|
||||
struct mr_serial_config close_config = {0};
|
||||
|
||||
/* Free FIFO buffers */
|
||||
mr_ringbuf_free(&serial->rd_fifo);
|
||||
mr_ringbuf_free(&serial->wr_fifo);
|
||||
|
||||
@@ -232,7 +230,7 @@ static ssize_t mr_serial_isr(struct mr_dev *dev, int event, void *args)
|
||||
|
||||
case MR_ISR_SERIAL_WR_INT:
|
||||
{
|
||||
/* Write data from FIFO */
|
||||
/* Write data from FIFO, if FIFO is empty, stop transmit */
|
||||
uint8_t data = 0;
|
||||
if (mr_ringbuf_pop(&serial->wr_fifo, &data) == sizeof(data))
|
||||
{
|
||||
|
||||
@@ -34,6 +34,7 @@ MR_INLINE void soft_i2c_bus_sda_set(struct mr_soft_i2c_bus *soft_i2c_bus, uint8_
|
||||
MR_INLINE uint8_t soft_i2c_sda_get(struct mr_soft_i2c_bus *soft_i2c_bus)
|
||||
{
|
||||
uint8_t value = 0;
|
||||
|
||||
mr_dev_ioctl(soft_i2c_bus->desc, MR_CTL_PIN_SET_NUMBER, &soft_i2c_bus->sda_pin);
|
||||
mr_dev_read(soft_i2c_bus->desc, &value, sizeof(value));
|
||||
return value;
|
||||
@@ -185,13 +186,12 @@ static uint8_t mr_soft_i2c_bus_read(struct mr_i2c_bus *i2c_bus, int ack_state)
|
||||
{
|
||||
struct mr_soft_i2c_bus *soft_i2c_bus = (struct mr_soft_i2c_bus *)i2c_bus;
|
||||
uint8_t data = 0;
|
||||
size_t bits = 0;
|
||||
|
||||
soft_i2c_scl_set(soft_i2c_bus, SOFT_I2C_LOW);
|
||||
mr_delay_us(soft_i2c_bus->delay);
|
||||
soft_i2c_bus_sda_set(soft_i2c_bus, SOFT_I2C_HIGH);
|
||||
|
||||
for (bits = 0; bits < 8; bits++)
|
||||
for (size_t bits = 0; bits < 8; bits++)
|
||||
{
|
||||
mr_delay_us(soft_i2c_bus->delay);
|
||||
soft_i2c_scl_set(soft_i2c_bus, SOFT_I2C_LOW);
|
||||
@@ -214,9 +214,8 @@ static uint8_t mr_soft_i2c_bus_read(struct mr_i2c_bus *i2c_bus, int ack_state)
|
||||
static void mr_soft_i2c_bus_write(struct mr_i2c_bus *i2c_bus, uint8_t data)
|
||||
{
|
||||
struct mr_soft_i2c_bus *soft_i2c_bus = (struct mr_soft_i2c_bus *)i2c_bus;
|
||||
size_t bits = 0;
|
||||
|
||||
for (bits = 0; bits < 8; bits++)
|
||||
for (size_t bits = 0; bits < 8; bits++)
|
||||
{
|
||||
if (data & 0x80)
|
||||
{
|
||||
@@ -272,6 +271,7 @@ int mr_soft_i2c_bus_register(struct mr_soft_i2c_bus *soft_i2c_bus, const char *n
|
||||
soft_i2c_bus->sda_pin = sda_pin;
|
||||
soft_i2c_bus->desc = -1;
|
||||
|
||||
/* Register the soft-i2c-bus */
|
||||
return mr_i2c_bus_register(&soft_i2c_bus->i2c_bus, name, &drv);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ static int timer_calculate(struct mr_timer *timer, uint32_t timeout)
|
||||
{
|
||||
uint32_t clk = timer->info->clk, psc_max = timer->info->prescaler_max, per_max = timer->info->period_max;
|
||||
uint32_t psc_best = 0, per_best = 0, reload_best = 0;
|
||||
uint32_t psc = 0, per = 0, reload = 0;
|
||||
int error = 0, error_min = INT32_MAX;
|
||||
uint32_t psc = 0;
|
||||
int error_min = INT32_MAX;
|
||||
|
||||
/* Check the clock */
|
||||
if (clk == 0)
|
||||
@@ -35,12 +35,12 @@ static int timer_calculate(struct mr_timer *timer, uint32_t timeout)
|
||||
}
|
||||
|
||||
/* Calculate the Least error reload */
|
||||
for (per = (timeout <= per_max) ? timeout : (timeout / (per_max + 1)); per > 1; per--)
|
||||
for (uint32_t per = (timeout <= per_max) ? timeout : (timeout / (per_max + 1)); per > 1; per--)
|
||||
{
|
||||
reload = timeout / per;
|
||||
uint32_t reload = timeout / per;
|
||||
|
||||
/* Calculate the error */
|
||||
error = (int)timeout - (int)(reload * per);
|
||||
int error = (int)timeout - (int)(reload * per);
|
||||
if (error == 0)
|
||||
{
|
||||
reload_best = reload;
|
||||
@@ -283,6 +283,7 @@ int mr_timer_register(struct mr_timer *timer, const char *name, struct mr_drv *d
|
||||
timer->prescaler = 0;
|
||||
timer->info = info;
|
||||
|
||||
/* Register the timer */
|
||||
return mr_dev_register(&timer->dev, name, Mr_Dev_Type_Timer, MR_SFLAG_RDWR, &ops, drv);
|
||||
}
|
||||
|
||||
|
||||
@@ -192,7 +192,7 @@ enum mr_dev_type
|
||||
Mr_Dev_Type_SPI = Mr_Drv_Type_SPI, /**< SPI */
|
||||
Mr_Dev_Type_Timer = Mr_Drv_Type_Timer, /**< Timer */
|
||||
Mr_Dev_Type_PWM = Mr_Drv_Type_PWM, /**< PWM */
|
||||
Mr_Dev_Type_Sensor, /**< Sensor */
|
||||
Mr_Dev_Type_Component, /**< Component */
|
||||
};
|
||||
|
||||
struct mr_dev;
|
||||
|
||||
@@ -20,19 +20,18 @@ static struct mr_dev root_dev =
|
||||
{&root_dev.clist, &root_dev.clist}
|
||||
};
|
||||
|
||||
static int dev_is_root(struct mr_dev *dev)
|
||||
MR_INLINE int dev_is_root(struct mr_dev *dev)
|
||||
{
|
||||
return (int)dev->type == Mr_Dev_Type_Root;
|
||||
}
|
||||
|
||||
static struct mr_dev *dev_find_child(struct mr_dev *parent, const char *name)
|
||||
MR_INLINE struct mr_dev *dev_find_child(struct mr_dev *parent, const char *name)
|
||||
{
|
||||
/* Disable interrupt */
|
||||
mr_interrupt_disable();
|
||||
|
||||
/* Find the child device */
|
||||
struct mr_list *list = MR_NULL;
|
||||
for (list = parent->clist.next; list != &parent->clist; list = list->next)
|
||||
for (struct mr_list *list = parent->clist.next; list != &parent->clist; list = list->next)
|
||||
{
|
||||
struct mr_dev *dev = (struct mr_dev *)mr_container_of(list, struct mr_dev, list);
|
||||
if (strncmp(name, dev->name, MR_CFG_NAME_MAX) == 0)
|
||||
@@ -48,7 +47,7 @@ static struct mr_dev *dev_find_child(struct mr_dev *parent, const char *name)
|
||||
return MR_NULL;
|
||||
}
|
||||
|
||||
static int dev_register_child(struct mr_dev *parent, struct mr_dev *child, const char *name)
|
||||
MR_INLINE int dev_register_child(struct mr_dev *parent, struct mr_dev *child, const char *name)
|
||||
{
|
||||
/* Check whether the device with the same name exists */
|
||||
if (dev_find_child(parent, name) != MR_NULL)
|
||||
@@ -70,7 +69,7 @@ static int dev_register_child(struct mr_dev *parent, struct mr_dev *child, const
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static const char *dev_clear_path(const char *path)
|
||||
MR_INLINE const char *dev_clear_path(const char *path)
|
||||
{
|
||||
/* Skip the leading '/' */
|
||||
if (*path == '/')
|
||||
@@ -86,7 +85,7 @@ static const char *dev_clear_path(const char *path)
|
||||
return path;
|
||||
}
|
||||
|
||||
static int dev_register_by_path(struct mr_dev *parent, struct mr_dev *dev, const char *path)
|
||||
MR_INLINE int dev_register_by_path(struct mr_dev *parent, struct mr_dev *dev, const char *path)
|
||||
{
|
||||
char parent_name[MR_CFG_NAME_MAX + 1] = {0};
|
||||
char *parent_path = MR_NULL;
|
||||
@@ -119,7 +118,7 @@ static int dev_register_by_path(struct mr_dev *parent, struct mr_dev *dev, const
|
||||
}
|
||||
}
|
||||
|
||||
static struct mr_dev *dev_find_by_path(struct mr_dev *parent, const char *path)
|
||||
MR_INLINE struct mr_dev *dev_find_by_path(struct mr_dev *parent, const char *path)
|
||||
{
|
||||
char parent_name[MR_CFG_NAME_MAX + 1] = {0};
|
||||
char *parent_path = MR_NULL;
|
||||
@@ -149,7 +148,7 @@ static struct mr_dev *dev_find_by_path(struct mr_dev *parent, const char *path)
|
||||
}
|
||||
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
static int dev_lock_take(struct mr_dev *dev, int take, int set)
|
||||
MR_INLINE int dev_lock_take(struct mr_dev *dev, int take, int set)
|
||||
{
|
||||
/* Continue iterating until reach the root device */
|
||||
if (dev_is_root(dev->parent) != MR_TRUE)
|
||||
@@ -172,7 +171,7 @@ static int dev_lock_take(struct mr_dev *dev, int take, int set)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void dev_lock_release(struct mr_dev *dev, int release)
|
||||
MR_INLINE void dev_lock_release(struct mr_dev *dev, int release)
|
||||
{
|
||||
/* Continue iterating until reach the root device */
|
||||
if (dev_is_root(dev->parent) != MR_TRUE)
|
||||
@@ -185,19 +184,19 @@ static void dev_lock_release(struct mr_dev *dev, int release)
|
||||
}
|
||||
#endif /* MR_USING_RDWR_CTL */
|
||||
|
||||
static int dev_register(struct mr_dev *dev, const char *path)
|
||||
MR_INLINE int dev_register(struct mr_dev *dev, const char *path)
|
||||
{
|
||||
/* Register the device to the root device */
|
||||
return dev_register_by_path(&root_dev, dev, path);
|
||||
}
|
||||
|
||||
static struct mr_dev *dev_find(const char *path)
|
||||
MR_INLINE struct mr_dev *dev_find(const char *path)
|
||||
{
|
||||
/* Find the device from the root device */
|
||||
return dev_find_by_path(&root_dev, path);
|
||||
}
|
||||
|
||||
static int dev_open(struct mr_dev *dev, int oflags)
|
||||
MR_INLINE int dev_open(struct mr_dev *dev, int oflags)
|
||||
{
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
if (mr_bits_is_set(dev->sflags, oflags) != MR_ENABLE)
|
||||
@@ -241,7 +240,7 @@ static int dev_open(struct mr_dev *dev, int oflags)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static int dev_close(struct mr_dev *dev)
|
||||
MR_INLINE int dev_close(struct mr_dev *dev)
|
||||
{
|
||||
/* Decrease the reference count */
|
||||
dev->ref_count--;
|
||||
@@ -268,7 +267,7 @@ static int dev_close(struct mr_dev *dev)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static ssize_t dev_read(struct mr_dev *dev, int off, void *buf, size_t size, int async)
|
||||
MR_INLINE ssize_t dev_read(struct mr_dev *dev, int off, void *buf, size_t size, int async)
|
||||
{
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
do
|
||||
@@ -296,7 +295,7 @@ static ssize_t dev_read(struct mr_dev *dev, int off, void *buf, size_t size, int
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t dev_write(struct mr_dev *dev, int offset, const void *buf, size_t size, int async)
|
||||
MR_INLINE ssize_t dev_write(struct mr_dev *dev, int offset, const void *buf, size_t size, int async)
|
||||
{
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
do
|
||||
@@ -334,7 +333,7 @@ static ssize_t dev_write(struct mr_dev *dev, int offset, const void *buf, size_t
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int dev_ioctl(struct mr_dev *dev, int desc, int off, int cmd, void *args)
|
||||
MR_INLINE int dev_ioctl(struct mr_dev *dev, int desc, int off, int cmd, void *args)
|
||||
{
|
||||
/* Check whether the device has an ioctl function */
|
||||
if (dev->ops->ioctl == MR_NULL)
|
||||
@@ -579,14 +578,13 @@ static struct mr_desc
|
||||
static int desc_allocate(const char *path)
|
||||
{
|
||||
int desc = -1;
|
||||
int i = 0;
|
||||
|
||||
/* Find a free descriptor */
|
||||
for (i = 0; i < MR_CFG_DESC_MAX; i++)
|
||||
for (size_t i = 0; i < MR_CFG_DESC_MAX; i++)
|
||||
{
|
||||
if (desc_of(i).dev == MR_NULL)
|
||||
{
|
||||
desc = i;
|
||||
desc = (int)i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -721,7 +719,6 @@ ssize_t mr_dev_write(int desc, const void *buf, size_t size)
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
if (mr_bits_is_set(desc_of(desc).oflags, MR_OFLAG_WRONLY) == MR_DISABLE)
|
||||
{
|
||||
|
||||
return MR_ENOTSUP;
|
||||
}
|
||||
#endif /* MR_USING_RDWR_CTL */
|
||||
@@ -771,6 +768,7 @@ int mr_dev_ioctl(int desc, int cmd, void *args)
|
||||
|
||||
default:
|
||||
{
|
||||
/* I/O control to the device */
|
||||
return dev_ioctl(desc_of(desc).dev, desc, desc_of(desc).offset, cmd, args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,8 +102,6 @@ MR_WEAK void *mr_malloc(size_t size)
|
||||
{
|
||||
struct mr_heap_block *block_prev = &heap_start;
|
||||
struct mr_heap_block *block = block_prev->next;
|
||||
void *memory = MR_NULL;
|
||||
size_t residual = 0;
|
||||
|
||||
/* Disable interrupt */
|
||||
mr_interrupt_disable();
|
||||
@@ -134,8 +132,8 @@ MR_WEAK void *mr_malloc(size_t size)
|
||||
block_prev->next = block->next;
|
||||
|
||||
/* Allocate memory */
|
||||
memory = (void *)((uint8_t *)block + sizeof(struct mr_heap_block));
|
||||
residual = block->size - size;
|
||||
void *memory = (void *)((uint8_t *)block + sizeof(struct mr_heap_block));
|
||||
size_t residual = block->size - size;
|
||||
|
||||
/* Set the block information */
|
||||
block->size = size;
|
||||
@@ -158,7 +156,6 @@ MR_WEAK void *mr_malloc(size_t size)
|
||||
|
||||
/* Enable interrupt */
|
||||
mr_interrupt_enable();
|
||||
|
||||
return memory;
|
||||
}
|
||||
|
||||
@@ -201,6 +198,7 @@ MR_WEAK size_t mr_malloc_usable_size(void *memory)
|
||||
{
|
||||
if (memory != MR_NULL)
|
||||
{
|
||||
/* Get the block information */
|
||||
struct mr_heap_block *block = (struct mr_heap_block *)((uint8_t *)memory - sizeof(struct mr_heap_block));
|
||||
return block->size;
|
||||
}
|
||||
@@ -218,9 +216,8 @@ MR_WEAK size_t mr_malloc_usable_size(void *memory)
|
||||
MR_WEAK void *mr_calloc(size_t num, size_t size)
|
||||
{
|
||||
size_t total = num * size;
|
||||
void *memory = MR_NULL;
|
||||
|
||||
memory = mr_malloc(total);
|
||||
void *memory = mr_malloc(total);
|
||||
if (memory != MR_NULL)
|
||||
{
|
||||
memset(memory, 0, total);
|
||||
@@ -239,9 +236,8 @@ MR_WEAK void *mr_calloc(size_t num, size_t size)
|
||||
MR_WEAK void *mr_realloc(void *memory, size_t size)
|
||||
{
|
||||
size_t old_size = mr_malloc_usable_size(memory);
|
||||
void *new_memory = MR_NULL;
|
||||
|
||||
new_memory = mr_malloc(size);
|
||||
void *new_memory = mr_malloc(size);
|
||||
if (new_memory != MR_NULL)
|
||||
{
|
||||
memcpy(new_memory, memory, old_size);
|
||||
|
||||
@@ -27,10 +27,8 @@ MR_INIT_EXPORT(end, "5.end");
|
||||
*/
|
||||
void mr_auto_init(void)
|
||||
{
|
||||
volatile const mr_init_fn_t *fn = MR_NULL;
|
||||
|
||||
/* Auto-initialization */
|
||||
for (fn = &_mr_auto_init_start; fn < &_mr_auto_init_end; fn++)
|
||||
for (volatile const mr_init_fn_t *fn = &_mr_auto_init_start; fn < &_mr_auto_init_end; fn++)
|
||||
{
|
||||
(*fn)();
|
||||
}
|
||||
@@ -59,8 +57,6 @@ MR_WEAK void mr_interrupt_enable(void)
|
||||
*/
|
||||
MR_WEAK void mr_delay_us(uint32_t us)
|
||||
{
|
||||
volatile uint32_t i = 0;
|
||||
|
||||
#ifndef MR_CFG_SYSCLK_FREQ
|
||||
#define MR_CFG_SYSCLK_FREQ (72000000)
|
||||
#endif /* MR_CFG_SYSCLK_FREQ */
|
||||
@@ -69,7 +65,7 @@ MR_WEAK void mr_delay_us(uint32_t us)
|
||||
#else
|
||||
#define MR_DELAY_COUNT (1)
|
||||
#endif /* (MR_CFG_SYSCLK_FREQ > 1000000) */
|
||||
for (i = 0; i < us * MR_DELAY_COUNT; i++)
|
||||
for (volatile uint32_t i = 0; i < us * MR_DELAY_COUNT; i++)
|
||||
{
|
||||
__asm__("nop");
|
||||
}
|
||||
@@ -84,9 +80,7 @@ MR_WEAK void mr_delay_us(uint32_t us)
|
||||
*/
|
||||
MR_WEAK void mr_delay_ms(uint32_t ms)
|
||||
{
|
||||
volatile uint32_t i = 0;
|
||||
|
||||
for (i = 0; i < ms; i++)
|
||||
for (volatile uint32_t i = 0; i < ms; i++)
|
||||
{
|
||||
mr_delay_us(1000);
|
||||
}
|
||||
@@ -209,8 +203,6 @@ void mr_ringbuf_init(struct mr_ringbuf *ringbuf, void *pool, size_t size)
|
||||
*/
|
||||
int mr_ringbuf_allocate(struct mr_ringbuf *ringbuf, size_t size)
|
||||
{
|
||||
void *pool = MR_NULL;
|
||||
|
||||
mr_assert(ringbuf != MR_NULL);
|
||||
|
||||
/* Check the buffer size */
|
||||
@@ -227,7 +219,7 @@ int mr_ringbuf_allocate(struct mr_ringbuf *ringbuf, size_t size)
|
||||
}
|
||||
|
||||
/* Allocate new buffer */
|
||||
pool = mr_malloc(size);
|
||||
void *pool = mr_malloc(size);
|
||||
if (pool == MR_NULL && size != 0)
|
||||
{
|
||||
return MR_ENOMEM;
|
||||
@@ -261,7 +253,6 @@ void mr_ringbuf_reset(struct mr_ringbuf *ringbuf)
|
||||
|
||||
ringbuf->read_index = 0;
|
||||
ringbuf->write_index = 0;
|
||||
|
||||
ringbuf->read_mirror = 0;
|
||||
ringbuf->write_mirror = 0;
|
||||
}
|
||||
@@ -370,13 +361,12 @@ size_t mr_ringbuf_pop(struct mr_ringbuf *ringbuf, uint8_t *data)
|
||||
size_t mr_ringbuf_read(struct mr_ringbuf *ringbuf, void *buffer, size_t size)
|
||||
{
|
||||
uint8_t *read_buffer = (uint8_t *)buffer;
|
||||
size_t data_size = 0;
|
||||
|
||||
mr_assert(ringbuf != MR_NULL);
|
||||
mr_assert((buffer != MR_NULL) || (size == 0));
|
||||
|
||||
/* Get the buf size */
|
||||
data_size = mr_ringbuf_get_data_size(ringbuf);
|
||||
size_t data_size = mr_ringbuf_get_data_size(ringbuf);
|
||||
if (data_size == 0)
|
||||
{
|
||||
return 0;
|
||||
@@ -498,13 +488,12 @@ size_t mr_ringbuf_push_force(struct mr_ringbuf *ringbuf, uint8_t data)
|
||||
size_t mr_ringbuf_write(struct mr_ringbuf *ringbuf, const void *buffer, size_t size)
|
||||
{
|
||||
uint8_t *write_buffer = (uint8_t *)buffer;
|
||||
size_t space_size = 0;
|
||||
|
||||
mr_assert(ringbuf != MR_NULL);
|
||||
mr_assert((buffer != MR_NULL) || (size == 0));
|
||||
|
||||
/* Get the space size */
|
||||
space_size = mr_ringbuf_get_space_size(ringbuf);
|
||||
size_t space_size = mr_ringbuf_get_space_size(ringbuf);
|
||||
if (space_size == 0)
|
||||
{
|
||||
return 0;
|
||||
@@ -547,7 +536,6 @@ size_t mr_ringbuf_write(struct mr_ringbuf *ringbuf, const void *buffer, size_t s
|
||||
size_t mr_ringbuf_write_force(struct mr_ringbuf *ringbuf, const void *buffer, size_t size)
|
||||
{
|
||||
uint8_t *write_buffer = (uint8_t *)buffer;
|
||||
size_t space_size = 0;
|
||||
|
||||
mr_assert(ringbuf != MR_NULL);
|
||||
mr_assert((buffer != MR_NULL) || (size == 0));
|
||||
@@ -558,7 +546,7 @@ size_t mr_ringbuf_write_force(struct mr_ringbuf *ringbuf, const void *buffer, si
|
||||
}
|
||||
|
||||
/* Get the space size */
|
||||
space_size = mr_ringbuf_get_space_size(ringbuf);
|
||||
size_t space_size = mr_ringbuf_get_space_size(ringbuf);
|
||||
|
||||
/* If the buf exceeds the buffer space_size, the front buf is discarded */
|
||||
if (size > ringbuf->size)
|
||||
@@ -626,8 +614,8 @@ static void mr_avl_left_rotate(struct mr_avl **node)
|
||||
right_child->left_child = (*node);
|
||||
|
||||
(*node)->height = mr_max(mr_avl_get_height((*node)->left_child), mr_avl_get_height((*node)->right_child)) + 1;
|
||||
right_child->height =
|
||||
mr_max(mr_avl_get_height(right_child->left_child), mr_avl_get_height(right_child->right_child)) + 1;
|
||||
right_child->height = mr_max(mr_avl_get_height(right_child->left_child),
|
||||
mr_avl_get_height(right_child->right_child)) + 1;
|
||||
|
||||
(*node) = right_child;
|
||||
}
|
||||
@@ -640,8 +628,8 @@ static void mr_avl_right_rotate(struct mr_avl **node)
|
||||
left_child->right_child = (*node);
|
||||
|
||||
(*node)->height = mr_max(mr_avl_get_height((*node)->left_child), mr_avl_get_height((*node)->right_child)) + 1;
|
||||
left_child->height =
|
||||
mr_max(mr_avl_get_height(left_child->left_child), mr_avl_get_height(left_child->right_child)) + 1;
|
||||
left_child->height = mr_max(mr_avl_get_height(left_child->left_child),
|
||||
mr_avl_get_height(left_child->right_child)) + 1;
|
||||
|
||||
(*node) = left_child;
|
||||
}
|
||||
@@ -670,8 +658,6 @@ void mr_avl_init(struct mr_avl *node, uint32_t value)
|
||||
*/
|
||||
void mr_avl_insert(struct mr_avl **tree, struct mr_avl *node)
|
||||
{
|
||||
int balance = 0;
|
||||
|
||||
mr_assert(tree != MR_NULL);
|
||||
mr_assert(node != MR_NULL);
|
||||
|
||||
@@ -693,7 +679,7 @@ void mr_avl_insert(struct mr_avl **tree, struct mr_avl *node)
|
||||
|
||||
(*tree)->height = mr_max(mr_avl_get_height((*tree)->left_child), mr_avl_get_height((*tree)->right_child)) + 1;
|
||||
|
||||
balance = mr_avl_get_balance((*tree));
|
||||
int balance = mr_avl_get_balance((*tree));
|
||||
if (balance > 1 && node->value < (*tree)->left_child->value)
|
||||
{
|
||||
mr_avl_right_rotate(&(*tree));
|
||||
|
||||
Reference in New Issue
Block a user