1。代码优化。

This commit is contained in:
MacRsh
2023-12-27 23:47:57 +08:00
parent f4cfebefe1
commit b2c2ff1532
13 changed files with 78 additions and 92 deletions

View File

@@ -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)
{

View File

@@ -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);
}

View File

@@ -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)
{

View File

@@ -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));
}

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -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))
{

View File

@@ -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);
}

View File

@@ -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);
}