1.编码风格修改,宏回到全大写。
This commit is contained in:
@@ -217,16 +217,16 @@ int main(void)
|
||||
/* 打开PIN设备 */
|
||||
int ds = mr_dev_open("pin", MR_OFLAG_RDWR);
|
||||
/* 设置到LED引脚 */
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, mr_make_local(int, LED_PIN_NUMBER));
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, LED_PIN_NUMBER));
|
||||
/* 设置LED引脚为推挽输出模式 */
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, mr_make_local(int, MR_PIN_MODE_OUTPUT));
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT));
|
||||
|
||||
while(1)
|
||||
{
|
||||
/* 点亮LED */
|
||||
mr_dev_write(ds, mr_make_local(uint8_t, MR_PIN_HIGH_LEVEL), sizeof(uint8_t));
|
||||
mr_dev_write(ds, MR_MAKE_LOCAL(uint8_t, MR_PIN_HIGH_LEVEL), sizeof(uint8_t));
|
||||
mr_delay_ms(500);
|
||||
mr_dev_write(ds, mr_make_local(uint8_t, MR_PIN_LOW_LEVEL), sizeof(uint8_t));
|
||||
mr_dev_write(ds, MR_MAKE_LOCAL(uint8_t, MR_PIN_LOW_LEVEL), sizeof(uint8_t));
|
||||
mr_delay_ms(500);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,16 +234,16 @@ int main(void)
|
||||
/* Open the PIN device */
|
||||
int ds = mr_dev_open("pin", MR_OFLAG_RDWR);
|
||||
/* Set to the LED pin */
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, mr_make_local(int, LED_PIN_NUMBER));
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, LED_PIN_NUMBER));
|
||||
/* Set the LED pin to push-pull output mode */
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, mr_make_local(int, MR_PIN_MODE_OUTPUT));
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT));
|
||||
|
||||
while(1)
|
||||
{
|
||||
/* Light up the LED */
|
||||
mr_dev_write(ds, mr_make_local(uint8_t, MR_PIN_HIGH_LEVEL), sizeof(uint8_t));
|
||||
mr_dev_write(ds, MR_MAKE_LOCAL(uint8_t, MR_PIN_HIGH_LEVEL), sizeof(uint8_t));
|
||||
mr_delay_ms(500);
|
||||
mr_dev_write(ds, mr_make_local(uint8_t, MR_PIN_LOW_LEVEL), sizeof(uint8_t));
|
||||
mr_dev_write(ds, MR_MAKE_LOCAL(uint8_t, MR_PIN_LOW_LEVEL), sizeof(uint8_t));
|
||||
mr_delay_ms(500);
|
||||
}
|
||||
}
|
||||
|
||||
22
device/adc.c
22
device/adc.c
@@ -30,10 +30,10 @@ static int adc_channel_set_state(struct mr_adc *adc, int channel, int state)
|
||||
/* Enable or disable the channel */
|
||||
if (state == MR_ADC_STATE_ENABLE)
|
||||
{
|
||||
mr_bits_set(adc->channel, (1 << channel));
|
||||
MR_BIT_SET(adc->channel, (1 << channel));
|
||||
} else
|
||||
{
|
||||
mr_bits_clr(adc->channel, (1 << channel));
|
||||
MR_BIT_CLR(adc->channel, (1 << channel));
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
@@ -47,7 +47,7 @@ static int adc_channel_get_state(struct mr_adc *adc, int channel)
|
||||
}
|
||||
|
||||
/* Check if the channel is enabled */
|
||||
return mr_bits_is_set(adc->channel, (1 << channel));
|
||||
return MR_BIT_IS_SET(adc->channel, (1 << channel));
|
||||
}
|
||||
|
||||
static int mr_adc_open(struct mr_dev *dev)
|
||||
@@ -66,10 +66,10 @@ static int mr_adc_close(struct mr_dev *dev)
|
||||
/* Disable all channels */
|
||||
for (int i = 0; i < 32; i++)
|
||||
{
|
||||
if (mr_bits_is_set(adc->channel, (1 << i)) == MR_ENABLE)
|
||||
if (MR_BIT_IS_SET(adc->channel, (1 << i)) == MR_ENABLE)
|
||||
{
|
||||
ops->channel_configure(adc, i, MR_DISABLE);
|
||||
mr_bits_clr(adc->channel, (1 << i));
|
||||
MR_BIT_CLR(adc->channel, (1 << i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,12 +84,12 @@ static ssize_t mr_adc_read(struct mr_dev *dev, int off, void *buf, size_t size,
|
||||
ssize_t rd_size = 0;
|
||||
|
||||
/* Check if the channel is enabled */
|
||||
if (mr_bits_is_set(adc->channel, (1 << off)) == MR_DISABLE)
|
||||
if (MR_BIT_IS_SET(adc->channel, (1 << off)) == MR_DISABLE)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
mr_bits_clr(size, sizeof(*rd_buf) - 1);
|
||||
MR_BIT_CLR(size, sizeof(*rd_buf) - 1);
|
||||
for (rd_size = 0; rd_size < size; rd_size += sizeof(*rd_buf))
|
||||
{
|
||||
*rd_buf = ops->read(adc, off);
|
||||
@@ -159,10 +159,10 @@ int mr_adc_register(struct mr_adc *adc, const char *name, struct mr_drv *drv)
|
||||
MR_NULL
|
||||
};
|
||||
|
||||
mr_assert(adc != MR_NULL);
|
||||
mr_assert(name != MR_NULL);
|
||||
mr_assert(drv != MR_NULL);
|
||||
mr_assert(drv->ops != MR_NULL);
|
||||
MR_ASSERT(adc != MR_NULL);
|
||||
MR_ASSERT(name != MR_NULL);
|
||||
MR_ASSERT(drv != MR_NULL);
|
||||
MR_ASSERT(drv->ops != MR_NULL);
|
||||
|
||||
/* Initialize the fields */
|
||||
adc->channel = 0;
|
||||
|
||||
18
device/can.c
18
device/can.c
@@ -57,7 +57,7 @@ static ssize_t mr_can_bus_isr(struct mr_dev *dev, int event, void *args)
|
||||
/* Search the matching device */
|
||||
for (list = dev->clist.next; list != &dev->clist; list = list->next)
|
||||
{
|
||||
struct mr_can_dev *can_dev = (struct mr_can_dev *)mr_container_of(list, struct mr_dev, list);
|
||||
struct mr_can_dev *can_dev = (struct mr_can_dev *)MR_CONTAINER_OF(list, struct mr_dev, list);
|
||||
|
||||
/* Check id is valid */
|
||||
if (can_dev->id == (id & ((1 << 29) - 1)))
|
||||
@@ -104,10 +104,10 @@ int mr_can_bus_register(struct mr_can_bus *can_bus, const char *name, struct mr_
|
||||
};
|
||||
struct mr_can_config default_config = MR_CAN_CONFIG_DEFAULT;
|
||||
|
||||
mr_assert(can_bus != MR_NULL);
|
||||
mr_assert(name != MR_NULL);
|
||||
mr_assert(drv != MR_NULL);
|
||||
mr_assert(drv->ops != MR_NULL);
|
||||
MR_ASSERT(can_bus != MR_NULL);
|
||||
MR_ASSERT(name != MR_NULL);
|
||||
MR_ASSERT(drv != MR_NULL);
|
||||
MR_ASSERT(drv->ops != MR_NULL);
|
||||
|
||||
/* Initialize the fields */
|
||||
can_bus->config = default_config;
|
||||
@@ -261,8 +261,8 @@ static ssize_t mr_can_dev_write(struct mr_dev *dev, int off, const void *buf, si
|
||||
|
||||
ret = can_dev_write(can_dev,
|
||||
(off & ((1 << 29) - 1)),
|
||||
mr_bits_is_set(off, MR_CAN_IDE_EXT),
|
||||
mr_bits_is_set(off, MR_CAN_RTR_REMOTE),
|
||||
MR_BIT_IS_SET(off, MR_CAN_IDE_EXT),
|
||||
MR_BIT_IS_SET(off, MR_CAN_RTR_REMOTE),
|
||||
(uint8_t *)buf,
|
||||
size);
|
||||
|
||||
@@ -363,8 +363,8 @@ int mr_can_dev_register(struct mr_can_dev *can_dev, const char *name, int id, in
|
||||
};
|
||||
struct mr_can_config default_config = MR_CAN_CONFIG_DEFAULT;
|
||||
|
||||
mr_assert(can_dev != MR_NULL);
|
||||
mr_assert(name != MR_NULL);
|
||||
MR_ASSERT(can_dev != MR_NULL);
|
||||
MR_ASSERT(name != MR_NULL);
|
||||
|
||||
/* Initialize the fields */
|
||||
can_dev->config = default_config;
|
||||
|
||||
22
device/dac.c
22
device/dac.c
@@ -30,10 +30,10 @@ static int dac_channel_set_state(struct mr_dac *dac, int channel, int state)
|
||||
/* Enable or disable the channel */
|
||||
if (state == MR_DAC_STATE_ENABLE)
|
||||
{
|
||||
mr_bits_set(dac->channel, (1 << channel));
|
||||
MR_BIT_SET(dac->channel, (1 << channel));
|
||||
} else
|
||||
{
|
||||
mr_bits_clr(dac->channel, (1 << channel));
|
||||
MR_BIT_CLR(dac->channel, (1 << channel));
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
@@ -47,7 +47,7 @@ static int dac_channel_get_state(struct mr_dac *dac, int channel)
|
||||
}
|
||||
|
||||
/* Check if the channel is enabled */
|
||||
return mr_bits_is_set(dac->channel, (1 << channel));
|
||||
return MR_BIT_IS_SET(dac->channel, (1 << channel));
|
||||
}
|
||||
|
||||
static int mr_dac_open(struct mr_dev *dev)
|
||||
@@ -66,10 +66,10 @@ static int mr_dac_close(struct mr_dev *dev)
|
||||
/* Disable all channels */
|
||||
for (int i = 0; i < 32; i++)
|
||||
{
|
||||
if (mr_bits_is_set(dac->channel, (1 << i)) == MR_ENABLE)
|
||||
if (MR_BIT_IS_SET(dac->channel, (1 << i)) == MR_ENABLE)
|
||||
{
|
||||
ops->channel_configure(dac, i, MR_DISABLE);
|
||||
mr_bits_clr(dac->channel, (1 << i));
|
||||
MR_BIT_CLR(dac->channel, (1 << i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,12 +84,12 @@ static ssize_t mr_dac_write(struct mr_dev *dev, int off, const void *buf, size_t
|
||||
ssize_t wr_size = 0;
|
||||
|
||||
/* Check if the channel is enabled */
|
||||
if (mr_bits_is_set(dac->channel, (1 << off)) == MR_DISABLE)
|
||||
if (MR_BIT_IS_SET(dac->channel, (1 << off)) == MR_DISABLE)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
mr_bits_clr(size, sizeof(*wr_buf) - 1);
|
||||
MR_BIT_CLR(size, sizeof(*wr_buf) - 1);
|
||||
for (wr_size = 0; wr_size < size; wr_size += sizeof(*wr_buf))
|
||||
{
|
||||
ops->write(dac, off, *wr_buf);
|
||||
@@ -160,10 +160,10 @@ int mr_dac_register(struct mr_dac *dac, const char *name, struct mr_drv *drv)
|
||||
MR_NULL
|
||||
};
|
||||
|
||||
mr_assert(dac != MR_NULL);
|
||||
mr_assert(name != MR_NULL);
|
||||
mr_assert(drv != MR_NULL);
|
||||
mr_assert(drv->ops != MR_NULL);
|
||||
MR_ASSERT(dac != MR_NULL);
|
||||
MR_ASSERT(name != MR_NULL);
|
||||
MR_ASSERT(drv != MR_NULL);
|
||||
MR_ASSERT(drv->ops != MR_NULL);
|
||||
|
||||
/* Initialize the fields */
|
||||
dac->channel = 0;
|
||||
|
||||
14
device/i2c.c
14
device/i2c.c
@@ -92,10 +92,10 @@ int mr_i2c_bus_register(struct mr_i2c_bus *i2c_bus, const char *name, struct mr_
|
||||
};
|
||||
struct mr_i2c_config default_config = MR_I2C_CONFIG_DEFAULT;
|
||||
|
||||
mr_assert(i2c_bus != MR_NULL);
|
||||
mr_assert(name != MR_NULL);
|
||||
mr_assert(drv != MR_NULL);
|
||||
mr_assert(drv->ops != MR_NULL);
|
||||
MR_ASSERT(i2c_bus != MR_NULL);
|
||||
MR_ASSERT(name != MR_NULL);
|
||||
MR_ASSERT(drv != MR_NULL);
|
||||
MR_ASSERT(drv->ops != MR_NULL);
|
||||
|
||||
/* Initialize the fields */
|
||||
i2c_bus->config = default_config;
|
||||
@@ -419,9 +419,9 @@ int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *name, int addr,
|
||||
};
|
||||
struct mr_i2c_config default_config = MR_I2C_CONFIG_DEFAULT;
|
||||
|
||||
mr_assert(i2c_dev != MR_NULL);
|
||||
mr_assert(name != MR_NULL);
|
||||
mr_assert((addr_bits == MR_I2C_ADDR_BITS_7) || (addr_bits == MR_I2C_ADDR_BITS_10));
|
||||
MR_ASSERT(i2c_dev != MR_NULL);
|
||||
MR_ASSERT(name != MR_NULL);
|
||||
MR_ASSERT((addr_bits == MR_I2C_ADDR_BITS_7) || (addr_bits == MR_I2C_ADDR_BITS_10));
|
||||
|
||||
/* Initialize the fields */
|
||||
i2c_dev->config = default_config;
|
||||
|
||||
44
device/pin.c
44
device/pin.c
@@ -10,12 +10,15 @@
|
||||
|
||||
#ifdef MR_USING_PIN
|
||||
|
||||
/**
|
||||
* @brief Pin irq structure.
|
||||
*/
|
||||
struct pin_irq
|
||||
{
|
||||
struct mr_list list;
|
||||
int number;
|
||||
int desc;
|
||||
int (*call)(int desc, void *args);
|
||||
int number; /**< Pin number */
|
||||
int desc; /**< Device descriptor */
|
||||
int (*call)(int desc, void *args); /**< Callback function */
|
||||
struct mr_list list; /**< List */
|
||||
};
|
||||
|
||||
static int pin_set_mode(struct mr_pin *pin, int number, int mode)
|
||||
@@ -41,7 +44,7 @@ static int pin_set_mode(struct mr_pin *pin, int number, int mode)
|
||||
/* If the irq exists, update it */
|
||||
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);
|
||||
struct pin_irq *irq = (struct pin_irq *)MR_CONTAINER_OF(list, struct pin_irq, list);
|
||||
if (irq->number == number)
|
||||
{
|
||||
if (mode < MR_PIN_MODE_IRQ_RISING)
|
||||
@@ -65,15 +68,22 @@ static int pin_set_mode(struct mr_pin *pin, int number, int mode)
|
||||
/* If not exist, allocate new irq */
|
||||
if (mode >= MR_PIN_MODE_IRQ_RISING)
|
||||
{
|
||||
/* Allocate irq */
|
||||
struct pin_irq *irq = (struct pin_irq *)mr_malloc(sizeof(struct pin_irq));
|
||||
if (irq != MR_NULL)
|
||||
if (irq == MR_NULL)
|
||||
{
|
||||
mr_list_init(&irq->list);
|
||||
irq->number = number;
|
||||
irq->desc = pin->dev.rd_call.desc;
|
||||
irq->call = pin->dev.rd_call.call;
|
||||
mr_list_insert_before(&pin->irq_list, &irq->list);
|
||||
/* Enable interrupt */
|
||||
mr_interrupt_enable();
|
||||
return MR_ENOMEM;
|
||||
}
|
||||
irq->number = number;
|
||||
irq->desc = pin->dev.rd_call.desc;
|
||||
irq->call = pin->dev.rd_call.call;
|
||||
mr_list_init(&irq->list);
|
||||
mr_list_insert_before(&pin->irq_list, &irq->list);
|
||||
|
||||
/* Clear call */
|
||||
pin->dev.rd_call.call = MR_NULL;
|
||||
}
|
||||
|
||||
/* Enable interrupt */
|
||||
@@ -161,14 +171,14 @@ static ssize_t mr_pin_isr(struct mr_dev *dev, int event, void *args)
|
||||
struct mr_list *list = MR_NULL;
|
||||
for (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);
|
||||
struct pin_irq *irq = (struct pin_irq *)MR_CONTAINER_OF(list, struct pin_irq, list);
|
||||
if (irq->number == number)
|
||||
{
|
||||
irq->call(irq->desc, &number);
|
||||
return MR_EEXIST;
|
||||
}
|
||||
}
|
||||
return number;
|
||||
return MR_ENOTFOUND;
|
||||
}
|
||||
|
||||
default:
|
||||
@@ -199,10 +209,10 @@ int mr_pin_register(struct mr_pin *pin, const char *name, struct mr_drv *drv)
|
||||
mr_pin_isr
|
||||
};
|
||||
|
||||
mr_assert(pin != MR_NULL);
|
||||
mr_assert(name != MR_NULL);
|
||||
mr_assert(drv != MR_NULL);
|
||||
mr_assert(drv->ops != MR_NULL);
|
||||
MR_ASSERT(pin != MR_NULL);
|
||||
MR_ASSERT(name != MR_NULL);
|
||||
MR_ASSERT(drv != MR_NULL);
|
||||
MR_ASSERT(drv->ops != MR_NULL);
|
||||
|
||||
/* Initialize the fields */
|
||||
mr_list_init(&pin->irq_list);
|
||||
|
||||
38
device/pwm.c
38
device/pwm.c
@@ -30,20 +30,20 @@ static int pwm_channel_set_configure(struct mr_pwm *pwm, int channel, struct mr_
|
||||
/* Enable or disable the channel */
|
||||
if (config.state == MR_PWM_ENABLE)
|
||||
{
|
||||
mr_bits_set(pwm->channel, (1 << channel));
|
||||
MR_BIT_SET(pwm->channel, (1 << channel));
|
||||
|
||||
/* Configure the polarity */
|
||||
if (config.polarity == MR_PWM_POLARITY_NORMAL)
|
||||
{
|
||||
mr_bits_clr(pwm->channel_polarity, (1 << channel));
|
||||
MR_BIT_CLR(pwm->channel_polarity, (1 << channel));
|
||||
} else
|
||||
{
|
||||
mr_bits_set(pwm->channel_polarity, (1 << channel));
|
||||
MR_BIT_SET(pwm->channel_polarity, (1 << channel));
|
||||
}
|
||||
} else
|
||||
{
|
||||
mr_bits_clr(pwm->channel, (1 << channel));
|
||||
mr_bits_clr(pwm->channel_polarity, (1 << channel));
|
||||
MR_BIT_CLR(pwm->channel, (1 << channel));
|
||||
MR_BIT_CLR(pwm->channel_polarity, (1 << channel));
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
@@ -57,8 +57,8 @@ static int pwm_channel_get_configure(struct mr_pwm *pwm, int channel, struct mr_
|
||||
}
|
||||
|
||||
/* Get configure */
|
||||
config->state = mr_bits_is_set(pwm->channel, (1 << channel));
|
||||
config->polarity = mr_bits_is_set(pwm->channel_polarity, (1 << channel));
|
||||
config->state = MR_BIT_IS_SET(pwm->channel, (1 << channel));
|
||||
config->polarity = MR_BIT_IS_SET(pwm->channel_polarity, (1 << channel));
|
||||
|
||||
return config->state;
|
||||
}
|
||||
@@ -121,7 +121,7 @@ static int pwm_calculate(struct mr_pwm *pwm, uint32_t freq)
|
||||
/* Check if prescaler can be used as period */
|
||||
if ((psc_best > per_best) && (psc_best < per_max))
|
||||
{
|
||||
mr_swap(per_best, psc_best);
|
||||
MR_SWAP(per_best, psc_best);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,10 +154,10 @@ static int mr_pwm_close(struct mr_dev *dev)
|
||||
/* Disable all channels */
|
||||
for (int i = 0; i < 32; i++)
|
||||
{
|
||||
if (mr_bits_is_set(pwm->channel, (1 << i)) == MR_ENABLE)
|
||||
if (MR_BIT_IS_SET(pwm->channel, (1 << i)) == MR_ENABLE)
|
||||
{
|
||||
ops->channel_configure(pwm, i, MR_DISABLE, MR_PWM_POLARITY_NORMAL);
|
||||
mr_bits_clr(pwm->channel, (1 << i));
|
||||
MR_BIT_CLR(pwm->channel, (1 << i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,12 +172,12 @@ static ssize_t mr_pwm_read(struct mr_dev *dev, int off, void *buf, size_t size,
|
||||
ssize_t rd_size = 0;
|
||||
|
||||
/* Check if the channel is enabled */
|
||||
if (mr_bits_is_set(pwm->channel, (1 << off)) == MR_DISABLE)
|
||||
if (MR_BIT_IS_SET(pwm->channel, (1 << off)) == MR_DISABLE)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
mr_bits_clr(size, sizeof(*rd_buf) - 1);
|
||||
MR_BIT_CLR(size, sizeof(*rd_buf) - 1);
|
||||
for (rd_size = 0; rd_size < size; rd_size += sizeof(*rd_buf))
|
||||
{
|
||||
*rd_buf = ops->read(pwm, off);
|
||||
@@ -194,12 +194,12 @@ static ssize_t mr_pwm_write(struct mr_dev *dev, int off, const void *buf, size_t
|
||||
ssize_t wr_size = 0;
|
||||
|
||||
/* Check if the channel is enabled */
|
||||
if (mr_bits_is_set(pwm->channel, (1 << off)) == MR_DISABLE)
|
||||
if (MR_BIT_IS_SET(pwm->channel, (1 << off)) == MR_DISABLE)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
mr_bits_clr(size, sizeof(*wr_buf) - 1);
|
||||
MR_BIT_CLR(size, sizeof(*wr_buf) - 1);
|
||||
for (wr_size = 0; wr_size < size; wr_size += sizeof(*wr_buf))
|
||||
{
|
||||
ops->write(pwm, off, *wr_buf);
|
||||
@@ -301,11 +301,11 @@ int mr_pwm_register(struct mr_pwm *pwm, const char *name, struct mr_drv *drv, st
|
||||
MR_NULL
|
||||
};
|
||||
|
||||
mr_assert(pwm != MR_NULL);
|
||||
mr_assert(name != MR_NULL);
|
||||
mr_assert(drv != MR_NULL);
|
||||
mr_assert(drv->ops != MR_NULL);
|
||||
mr_assert(info != MR_NULL);
|
||||
MR_ASSERT(pwm != MR_NULL);
|
||||
MR_ASSERT(name != MR_NULL);
|
||||
MR_ASSERT(drv != MR_NULL);
|
||||
MR_ASSERT(drv->ops != MR_NULL);
|
||||
MR_ASSERT(info != MR_NULL);
|
||||
|
||||
/* Initialize the fields */
|
||||
pwm->freq = 0;
|
||||
|
||||
@@ -188,8 +188,6 @@
|
||||
#define MR_CFG_OBJECT_NAME_SIZE 15
|
||||
```
|
||||
|
||||
2. 静态宏函数(无计算,无赋值等需要运行时才进行的操作)参考宏定义规则。动态宏函数(有任何需要运行过程中进行的操作),参考函数命名规则。
|
||||
|
||||
# 注释规范
|
||||
|
||||
1. 单行注释使用`/* */`,多行注释使用:
|
||||
|
||||
@@ -168,9 +168,6 @@
|
||||
#define MR_CFG_OBJECT_NAME_SIZE 15
|
||||
```
|
||||
|
||||
2. Static macro functions (no computation, assignment etc needed at runtime) follow macro naming. Dynamic macro
|
||||
functions (with runtime operations) follow function naming.
|
||||
|
||||
# Comment Conventions
|
||||
|
||||
1. Single line comments use `/* */`, multi-line comments:
|
||||
|
||||
@@ -77,7 +77,7 @@ int mr_dev_ioctl(int desc, int cmd, void *args);
|
||||
#define CHANNEL_NUMBER 5
|
||||
|
||||
/* 设置通道编号 */
|
||||
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL, mr_make_local(int, CHANNEL_NUMBER));
|
||||
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
|
||||
|
||||
/* 获取通道编号 */
|
||||
int number;
|
||||
@@ -93,7 +93,7 @@ mr_dev_ioctl(ds, MR_CTL_ADC_GET_CHANNEL, &number);
|
||||
|
||||
```c
|
||||
/* 设置通道状态 */
|
||||
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL_STATE, mr_make_local(int, MR_ADC_STATE_ENABLE));
|
||||
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_ADC_STATE_ENABLE));
|
||||
|
||||
/* 获取通道状态 */
|
||||
int state;
|
||||
@@ -153,9 +153,9 @@ int adc_init(void)
|
||||
/* 打印ADC描述符 */
|
||||
mr_printf("ADC1 desc: %d\r\n", adc_ds);
|
||||
/* 设置到通道5 */
|
||||
mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL, mr_make_local(int, CHANNEL_NUMBER));
|
||||
mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
|
||||
/* 设置通道使能 */
|
||||
ret = mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL_STATE, mr_make_local(int, MR_ADC_STATE_ENABLE));
|
||||
ret = mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_ADC_STATE_ENABLE));
|
||||
if (ret < 0)
|
||||
{
|
||||
mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret));
|
||||
@@ -164,7 +164,7 @@ int adc_init(void)
|
||||
return MR_EOK;
|
||||
}
|
||||
/* 导出到自动初始化(APP级) */
|
||||
MR_APP_EXPORT(adc_init);
|
||||
MR_INIT_APP_EXPORT(adc_init);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
@@ -77,7 +77,7 @@ Channel number range: `0` ~ `31`.
|
||||
#define CHANNEL_NUMBER 5
|
||||
|
||||
/* Set channel number */
|
||||
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL, mr_make_local(int, CHANNEL_NUMBER));
|
||||
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
|
||||
|
||||
/* Get channel number */
|
||||
int number;
|
||||
@@ -93,7 +93,7 @@ Channel status:
|
||||
|
||||
```c
|
||||
/* Set channel status */
|
||||
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL_STATE, mr_make_local(int, MR_ADC_STATE_ENABLE));
|
||||
mr_dev_ioctl(ds, MR_CTL_ADC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_ADC_STATE_ENABLE));
|
||||
|
||||
/* Get channel status */
|
||||
int state;
|
||||
@@ -154,9 +154,9 @@ int adc_init(void)
|
||||
/* Print ADC descriptor */
|
||||
mr_printf("ADC1 desc: %d\r\n", adc_ds);
|
||||
/* Set to channel 5 */
|
||||
mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL, mr_make_local(int, CHANNEL_NUMBER));
|
||||
mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
|
||||
/* Set channel enable */
|
||||
ret = mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL_STATE, mr_make_local(int, MR_ADC_STATE_ENABLE));
|
||||
ret = mr_dev_ioctl(adc_ds, MR_CTL_ADC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_ADC_STATE_ENABLE));
|
||||
if (ret < 0)
|
||||
{
|
||||
mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret));
|
||||
@@ -165,7 +165,7 @@ int adc_init(void)
|
||||
return MR_EOK;
|
||||
}
|
||||
/* Export to automatic initialization (APP level) */
|
||||
MR_APP_EXPORT(adc_init);
|
||||
MR_INIT_APP_EXPORT(adc_init);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
@@ -77,7 +77,7 @@ int mr_dev_ioctl(int desc, int cmd, void *args);
|
||||
#define CHANNEL_NUMBER 5
|
||||
|
||||
/* 设置通道编号 */
|
||||
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL, mr_make_local(int, CHANNEL_NUMBER));
|
||||
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
|
||||
|
||||
/* 获取通道编号 */
|
||||
int number;
|
||||
@@ -93,7 +93,7 @@ mr_dev_ioctl(ds, MR_CTL_DAC_GET_CHANNEL, &number);
|
||||
|
||||
```c
|
||||
/* 设置通道状态 */
|
||||
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL_STATE, mr_make_local(int, MR_DAC_STATE_ENABLE));
|
||||
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_DAC_STATE_ENABLE));
|
||||
|
||||
/* 获取通道状态 */
|
||||
int state;
|
||||
@@ -153,9 +153,9 @@ int dac_init(void)
|
||||
/* 打印DAC描述符 */
|
||||
mr_printf("DAC1 desc: %d\r\n", dac_ds);
|
||||
/* 设置到通道5 */
|
||||
mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL, mr_make_local(int, CHANNEL_NUMBER));
|
||||
mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
|
||||
/* 设置通道使能 */
|
||||
ret = mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL_STATE, mr_make_local(int, MR_DAC_STATE_ENABLE));
|
||||
ret = mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_DAC_STATE_ENABLE));
|
||||
if (ret < 0)
|
||||
{
|
||||
mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret));
|
||||
@@ -164,7 +164,7 @@ int dac_init(void)
|
||||
return MR_EOK;
|
||||
}
|
||||
/* 导出到自动初始化(APP级) */
|
||||
MR_APP_EXPORT(dac_init);
|
||||
MR_INIT_APP_EXPORT(dac_init);
|
||||
|
||||
/* 定义DAC数据最大值 */
|
||||
#define DAC_DATA_MAX 4095
|
||||
|
||||
@@ -77,7 +77,7 @@ Channel number range: `0` ~ `31`.
|
||||
#define CHANNEL_NUMBER 5
|
||||
|
||||
/* Set channel number */
|
||||
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL, mr_make_local(int, CHANNEL_NUMBER));
|
||||
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
|
||||
|
||||
/* Get channel number */
|
||||
int number;
|
||||
@@ -93,7 +93,7 @@ Channel status:
|
||||
|
||||
```c
|
||||
/* Set channel status */
|
||||
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL_STATE, mr_make_local(int, MR_DAC_STATE_ENABLE));
|
||||
mr_dev_ioctl(ds, MR_CTL_DAC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_DAC_STATE_ENABLE));
|
||||
|
||||
/* Get channel status */
|
||||
int state;
|
||||
@@ -153,9 +153,9 @@ int dac_init(void)
|
||||
/* Print DAC descriptor */
|
||||
mr_printf("DAC1 desc: %d\r\n", dac_ds);
|
||||
/* Set to channel 5 */
|
||||
mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL, mr_make_local(int, CHANNEL_NUMBER));
|
||||
mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL, MR_MAKE_LOCAL(int, CHANNEL_NUMBER));
|
||||
/* Set channel enable */
|
||||
ret = mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL_STATE, mr_make_local(int, MR_DAC_STATE_ENABLE));
|
||||
ret = mr_dev_ioctl(dac_ds, MR_CTL_DAC_SET_CHANNEL_STATE, MR_MAKE_LOCAL(int, MR_DAC_STATE_ENABLE));
|
||||
if (ret < 0)
|
||||
{
|
||||
mr_printf("Channel5 enable failed: %s\r\n", mr_strerror(ret));
|
||||
@@ -164,7 +164,7 @@ int dac_init(void)
|
||||
return MR_EOK;
|
||||
}
|
||||
/* Export to automatic initialization (APP level) */
|
||||
MR_APP_EXPORT(dac_init);
|
||||
MR_INIT_APP_EXPORT(dac_init);
|
||||
|
||||
/* Define DAC data maximum value */
|
||||
#define DAC_DATA_MAX 4095
|
||||
|
||||
@@ -134,7 +134,7 @@ mr_dev_ioctl(ds, MR_CTL_I2C_GET_CONFIG, &config);
|
||||
|
||||
```c
|
||||
/* 设置寄存器值 */
|
||||
mr_dev_ioctl(ds, MR_CTL_I2C_SET_REG, mr_make_local(int, 0x12));
|
||||
mr_dev_ioctl(ds, MR_CTL_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12));
|
||||
|
||||
/* 获取寄存器值 */
|
||||
uint8_t reg;
|
||||
@@ -296,7 +296,7 @@ int i2c_init(void)
|
||||
return ret;
|
||||
}
|
||||
/* 设置寄存器值 */
|
||||
mr_dev_ioctl(host_ds, MR_CTL_I2C_SET_REG, mr_make_local(int, 0x12));
|
||||
mr_dev_ioctl(host_ds, MR_CTL_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12));
|
||||
|
||||
/* 打开I2C-SLAVE设备 */
|
||||
slave_ds = mr_dev_open("i2c2/slave", MR_OFLAG_RDWR);
|
||||
@@ -317,7 +317,7 @@ int i2c_init(void)
|
||||
return MR_EOK;
|
||||
}
|
||||
/* 导出到自动初始化(APP级) */
|
||||
MR_APP_EXPORT(i2c_init);
|
||||
MR_INIT_APP_EXPORT(i2c_init);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
@@ -134,7 +134,7 @@ The register value will be written first (range: `0` ~ `INT32_MAX`) before readi
|
||||
|
||||
```c
|
||||
/* Set register value */
|
||||
mr_dev_ioctl(ds, MR_CTL_I2C_SET_REG, mr_make_local(int, 0x12));
|
||||
mr_dev_ioctl(ds, MR_CTL_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12));
|
||||
|
||||
/* Get register value */
|
||||
uint8_t reg;
|
||||
@@ -297,7 +297,7 @@ int i2c_init(void)
|
||||
return ret;
|
||||
}
|
||||
/* Set register value */
|
||||
mr_dev_ioctl(host_ds, MR_CTL_I2C_SET_REG, mr_make_local(int, 0x12));
|
||||
mr_dev_ioctl(host_ds, MR_CTL_I2C_SET_REG, MR_MAKE_LOCAL(int, 0x12));
|
||||
|
||||
/* Open I2C-SLAVE device */
|
||||
slave_ds = mr_dev_open("i2c2/slave", MR_OFLAG_RDWR);
|
||||
@@ -318,7 +318,7 @@ int i2c_init(void)
|
||||
return MR_EOK;
|
||||
}
|
||||
/* Export to auto init (APP level) */
|
||||
MR_APP_EXPORT(i2c_init);
|
||||
MR_INIT_APP_EXPORT(i2c_init);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
@@ -89,7 +89,7 @@ int mr_dev_ioctl(int desc, int cmd, void *args);
|
||||
#define PIN_NUMBER 45
|
||||
|
||||
/* 设置引脚编号 */
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, mr_make_local(int, PIN_NUMBER));
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, PIN_NUMBER));
|
||||
|
||||
/* 获取引脚编号 */
|
||||
int number;
|
||||
@@ -122,7 +122,7 @@ mr_dev_ioctl(ds, MR_CTL_PIN_GET_NUMBER, &number);
|
||||
#define PIN_MODE MR_PIN_MODE_OUTPUT
|
||||
|
||||
/* 设置引脚模式 */
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, mr_make_local(int, PIN_MODE));
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, PIN_MODE));
|
||||
```
|
||||
|
||||
### 设置/获取外部中断回调函数
|
||||
@@ -150,9 +150,7 @@ mr_dev_ioctl(ds, MR_CTL_PIN_GET_EXTI_CALL, &callback);
|
||||
|
||||
注:
|
||||
|
||||
-
|
||||
在设置回调函数之后,需要重新配置一次引脚模式为外部中断模式才能使回调函数绑定到对应引脚。因此在配置引脚模式之前,请先设置回调函数,且回调函数中一定要判断引脚源是否正确(未绑定的情况下,若同样有配置为外部中断模式的但未绑定引脚触发了外部中断,此时将调用未绑定的回调函数)。
|
||||
- 如果设置外部中断时没有重新配置回调函数,则默认使用上一次的结果,并且不会使用当前设备描述符。
|
||||
- 设置外部中断模式前需要先配置回调函数,否则将成为无回调中断。
|
||||
- 即使PIN设备被关闭,回调函数也不会失效,直至引脚被设置为普通模式(PIN设备关闭时,外部中断将被忽略)。
|
||||
|
||||
## 读取PIN设备引脚电平
|
||||
@@ -250,9 +248,9 @@ int led_key_init(void)
|
||||
/* 打印LED描述符 */
|
||||
mr_printf("LED desc: %d\r\n", led_ds);
|
||||
/* 设置到LED引脚 */
|
||||
mr_dev_ioctl(led_ds, MR_CTL_PIN_SET_NUMBER, mr_make_local(int, LED_PIN_NUMBER));
|
||||
mr_dev_ioctl(led_ds, MR_CTL_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, LED_PIN_NUMBER));
|
||||
/* 设置LED引脚为推挽输出模式 */
|
||||
ret = mr_dev_ioctl(led_ds, MR_CTL_PIN_SET_MODE, mr_make_local(int, MR_PIN_MODE_OUTPUT));
|
||||
ret = mr_dev_ioctl(led_ds, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT));
|
||||
if (ret < 0)
|
||||
{
|
||||
mr_printf("led set mode failed: %s\r\n", mr_strerror(ret));
|
||||
@@ -271,9 +269,9 @@ int led_key_init(void)
|
||||
/* 打印KEY描述符 */
|
||||
mr_printf("KEY desc: %d\r\n", key_ds);
|
||||
/* 设置到KEY引脚 */
|
||||
mr_dev_ioctl(key_ds, MR_CTL_PIN_SET_NUMBER, mr_make_local(int, KEY_PIN_NUMBER));
|
||||
mr_dev_ioctl(key_ds, MR_CTL_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, KEY_PIN_NUMBER));
|
||||
/* 设置KEY引脚为外部中断(下降沿)模式(未重新配置回调函数,则使用上一次的结果,即LED设置的回调函数) */
|
||||
ret = mr_dev_ioctl(key_ds, MR_CTL_PIN_SET_MODE, mr_make_local(int, MR_PIN_MODE_IRQ_FALLING));
|
||||
ret = mr_dev_ioctl(key_ds, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_IRQ_FALLING));
|
||||
if (ret < 0)
|
||||
{
|
||||
mr_printf("key set mode failed: %s\r\n", mr_strerror(ret));
|
||||
@@ -282,7 +280,7 @@ int led_key_init(void)
|
||||
return MR_EOK;
|
||||
}
|
||||
/* 导出到自动初始化(APP级) */
|
||||
MR_APP_EXPORT(led_key_init);
|
||||
MR_INIT_APP_EXPORT(led_key_init);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@@ -296,5 +294,4 @@ int main(void)
|
||||
}
|
||||
```
|
||||
|
||||
按下KEY后,LED将翻转。
|
||||
观察串口打印,可以看到LED和KEY的描述符。虽然KEY引脚的外部中断是KEY配置的,但未配置回调函数。故KEY引脚回调函数继承之前的配置,即LED配置的回调函数,所以KEY回调函数中打印的描述符为LED的描述符。
|
||||
按下KEY后,LED将翻转。观察串口打印,可以看到LED和KEY的描述符。虽然KEY引脚的外部中断是KEY配置的,但未重新配置回调函数。故KEY引脚回调函数继承之前的配置,即LED配置的回调函数,所以KEY回调函数中打印的描述符为LED的描述符。
|
||||
|
||||
@@ -89,7 +89,7 @@ Note: This rule may not apply to all MCUs. Special requirements need to check th
|
||||
#define PIN_NUMBER 45
|
||||
|
||||
/* Set pin number */
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, mr_make_local(int, PIN_NUMBER));
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, PIN_NUMBER));
|
||||
|
||||
/* Get pin number */
|
||||
int number;
|
||||
@@ -122,7 +122,7 @@ And 5 external interrupt modes:
|
||||
#define PIN_MODE MR_PIN_MODE_OUTPUT
|
||||
|
||||
/* Set pin mode */
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, mr_make_local(int, PIN_MODE));
|
||||
mr_dev_ioctl(ds, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, PIN_MODE));
|
||||
```
|
||||
|
||||
### Set/Get External Interrupt Callback Function
|
||||
@@ -150,8 +150,7 @@ mr_dev_ioctl(ds, MR_CTL_PIN_GET_EXTI_CALL, &callback);
|
||||
|
||||
Note:
|
||||
|
||||
- You need to reconfigure the pin mode to external interrupt mode after setting the callback function to bind the callback function to the corresponding pin. So please set the callback function first before configuring the pin mode. And the callback function must judge the pin source is correct (for the case of external interrupt triggered on pins without binding, it will call the unbound callback function).
|
||||
- If the external interrupt is set without reconfiguring the callback function, it will use the previous result by default, and will not use the current device descriptor.
|
||||
- Before setting the external interrupt mode, you need to configure the callback function, otherwise, it becomes a callback-free interrupt.
|
||||
- Even if the PIN device is closed, the callback function will not be invalid until the pin is set to a common mode (external interrupts will be ignored when the PIN device is closed).
|
||||
|
||||
## Read PIN Device Pin Level
|
||||
@@ -249,9 +248,9 @@ int led_key_init(void)
|
||||
/* Print LED descriptor */
|
||||
mr_printf("LED desc: %d\r\n", led_ds);
|
||||
/* Set to LED pin */
|
||||
mr_dev_ioctl(led_ds, MR_CTL_PIN_SET_NUMBER, mr_make_local(int, LED_PIN_NUMBER));
|
||||
mr_dev_ioctl(led_ds, MR_CTL_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, LED_PIN_NUMBER));
|
||||
/* Set LED pin to push-pull output mode */
|
||||
ret = mr_dev_ioctl(led_ds, MR_CTL_PIN_SET_MODE, mr_make_local(int, MR_PIN_MODE_OUTPUT));
|
||||
ret = mr_dev_ioctl(led_ds, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_OUTPUT));
|
||||
if (ret < 0)
|
||||
{
|
||||
mr_printf("led set mode failed: %s\r\n", mr_strerror(ret));
|
||||
@@ -270,9 +269,9 @@ int led_key_init(void)
|
||||
/* Print KEY descriptor */
|
||||
mr_printf("KEY desc: %d\r\n", key_ds);
|
||||
/* Set to KEY pin */
|
||||
mr_dev_ioctl(key_ds, MR_CTL_PIN_SET_NUMBER, mr_make_local(int, KEY_PIN_NUMBER));
|
||||
mr_dev_ioctl(key_ds, MR_CTL_PIN_SET_NUMBER, MR_MAKE_LOCAL(int, KEY_PIN_NUMBER));
|
||||
/* Set KEY pin to external interrupt (falling edge) mode (without reconfiguring the callback function, use the previous result, i.e. the callback function set by LED) */
|
||||
ret = mr_dev_ioctl(key_ds, MR_CTL_PIN_SET_MODE, mr_make_local(int, MR_PIN_MODE_IRQ_FALLING));
|
||||
ret = mr_dev_ioctl(key_ds, MR_CTL_PIN_SET_MODE, MR_MAKE_LOCAL(int, MR_PIN_MODE_IRQ_FALLING));
|
||||
if (ret < 0)
|
||||
{
|
||||
mr_printf("key set mode failed: %s\r\n", mr_strerror(ret));
|
||||
@@ -281,7 +280,7 @@ int led_key_init(void)
|
||||
return MR_EOK;
|
||||
}
|
||||
/* Export to auto initialization (APP level) */
|
||||
MR_APP_EXPORT(led_key_init);
|
||||
MR_INIT_APP_EXPORT(led_key_init);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
@@ -121,7 +121,7 @@ int mr_heap_init(void)
|
||||
}
|
||||
|
||||
/* 字节向上做4对齐 */
|
||||
size = mr_align4_up(size);
|
||||
size = MR_ALIGN4_UP(size);
|
||||
|
||||
/* 找到符合内存分配大小的内存块 */
|
||||
while (block->size < size)
|
||||
|
||||
@@ -130,7 +130,7 @@ void *mr_malloc(size_t size)
|
||||
}
|
||||
|
||||
/* Align the size to the next multiple of 4 bytes */
|
||||
size = mr_align4_up(size);
|
||||
size = MR_ALIGN4_UP(size);
|
||||
|
||||
/* Find a memory block that can accommodate the requested size */
|
||||
while (block->size < size)
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/**
|
||||
* @addtogroup Interrupt.
|
||||
@@ -114,6 +114,6 @@ const char *mr_dev_get_name(int desc);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* _MR_API_H_ */
|
||||
|
||||
@@ -63,7 +63,7 @@ typedef int ssize_t;
|
||||
typedef int (*mr_init_fn_t)(void);
|
||||
|
||||
/**
|
||||
* @brief Exports an auto initialization function.
|
||||
* @brief Exports an auto initialization function with level.
|
||||
*/
|
||||
#define MR_INIT_EXPORT(fn, level) \
|
||||
MR_USED const mr_init_fn_t _mr_auto_init_##fn MR_SECTION(".auto_init."level) = fn
|
||||
@@ -71,22 +71,22 @@ typedef int (*mr_init_fn_t)(void);
|
||||
/**
|
||||
* @brief Exports a board auto initialization function.
|
||||
*/
|
||||
#define MR_BOARD_EXPORT(fn) MR_INIT_EXPORT(fn, "1")
|
||||
#define MR_INIT_BOARD_EXPORT(fn) MR_INIT_EXPORT(fn, "1")
|
||||
|
||||
/**
|
||||
* @brief Exports a driver auto initialization function.
|
||||
*/
|
||||
#define MR_DRV_EXPORT(fn) MR_INIT_EXPORT(fn, "2")
|
||||
#define MR_INIT_DRV_EXPORT(fn) MR_INIT_EXPORT(fn, "2")
|
||||
|
||||
/**
|
||||
* @brief Exports a device auto initialization function.
|
||||
*/
|
||||
#define MR_DEV_EXPORT(fn) MR_INIT_EXPORT(fn, "3")
|
||||
#define MR_INIT_DEV_EXPORT(fn) MR_INIT_EXPORT(fn, "3")
|
||||
|
||||
/**
|
||||
* @brief Exports a app auto initialization function.
|
||||
*/
|
||||
#define MR_APP_EXPORT(fn) MR_INIT_EXPORT(fn, "4")
|
||||
#define MR_INIT_APP_EXPORT(fn) MR_INIT_EXPORT(fn, "4")
|
||||
|
||||
/**
|
||||
* @brief Error code.
|
||||
|
||||
@@ -43,7 +43,7 @@ extern "C" {
|
||||
*
|
||||
* @return A pointer to the structure.
|
||||
*/
|
||||
#define mr_container_of(pointer, type, member) \
|
||||
#define MR_CONTAINER_OF(pointer, type, member) \
|
||||
((type *)((char *)(pointer) - (unsigned long)(&((type *)0)->member)))
|
||||
|
||||
/**
|
||||
@@ -51,14 +51,14 @@ extern "C" {
|
||||
*
|
||||
* @param size The size to align.
|
||||
*/
|
||||
#define mr_align4_up(size) (((size) + 3) & (~3))
|
||||
#define MR_ALIGN4_UP(size) (((size) + 3) & (~3))
|
||||
|
||||
/**
|
||||
* @brief This macro function aligns a size down to a multiple of 4.
|
||||
*
|
||||
* @param size The size to align.
|
||||
*/
|
||||
#define mr_align4_down(size) ((size) & (~3))
|
||||
#define MR_ALIGN4_DOWN(size) ((size) & (~3))
|
||||
|
||||
/**
|
||||
* @brief This macro function checks if a value is set.
|
||||
@@ -66,7 +66,7 @@ extern "C" {
|
||||
* @param value The value to check.
|
||||
* @param mask The mask to check.
|
||||
*/
|
||||
#define mr_bits_is_set(value, mask) (((value) & (mask)) == (mask))
|
||||
#define MR_BIT_IS_SET(value, mask) (((value) & (mask)) == (mask))
|
||||
|
||||
/**
|
||||
* @brief This macro function sets a value.
|
||||
@@ -74,7 +74,7 @@ extern "C" {
|
||||
* @param value The value to set.
|
||||
* @param mask The mask to set.
|
||||
*/
|
||||
#define mr_bits_set(value, mask) ((value) |= (mask))
|
||||
#define MR_BIT_SET(value, mask) ((value) |= (mask))
|
||||
|
||||
/**
|
||||
* @brief This macro function clears a value.
|
||||
@@ -82,7 +82,7 @@ extern "C" {
|
||||
* @param value The value to clear.
|
||||
* @param mask The mask to clear.
|
||||
*/
|
||||
#define mr_bits_clr(value, mask) ((value) &= ~(mask))
|
||||
#define MR_BIT_CLR(value, mask) ((value) &= ~(mask))
|
||||
|
||||
/**
|
||||
* @brief This macro function creates a local variable.
|
||||
@@ -94,7 +94,7 @@ extern "C" {
|
||||
*
|
||||
* @note The variable is local, please use it carefully.
|
||||
*/
|
||||
#define mr_make_local(type, ...) (&((type){__VA_ARGS__}))
|
||||
#define MR_MAKE_LOCAL(type, ...) (&((type){__VA_ARGS__}))
|
||||
|
||||
/**
|
||||
* @brief This macro function gets the number of elements in an array.
|
||||
@@ -103,7 +103,7 @@ extern "C" {
|
||||
*
|
||||
* @return The number of elements in the array.
|
||||
*/
|
||||
#define mr_array_num(array) (sizeof(array)/sizeof((array)[0]))
|
||||
#define MR_ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))
|
||||
|
||||
/**
|
||||
* @brief This macro function gets the maximum of two values.
|
||||
@@ -113,7 +113,7 @@ extern "C" {
|
||||
*
|
||||
* @return The maximum of the two values.
|
||||
*/
|
||||
#define mr_max(a, b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
|
||||
#define MR_MAX(a, b) ({typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
|
||||
|
||||
/**
|
||||
* @brief This macro function gets the minimum of two values.
|
||||
@@ -123,7 +123,7 @@ extern "C" {
|
||||
*
|
||||
* @return The minimum of the two values.
|
||||
*/
|
||||
#define mr_min(a, b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
|
||||
#define MR_MIN(a, b) ({typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
|
||||
|
||||
/**
|
||||
* @brief This macro function ensures that a value is within a specified range.
|
||||
@@ -134,7 +134,7 @@ extern "C" {
|
||||
*
|
||||
* @return The value within the specified range.
|
||||
*/
|
||||
#define mr_bound(value, min, max) \
|
||||
#define MR_BOUND(value, min, max) \
|
||||
({__typeof__(value) _value = (value); __typeof__(min) _min = (min); __typeof__(max) _max = (max); \
|
||||
(_value) < (_min) ? (_min) : ((_value) > (_max) ? (_max) : (_value));})
|
||||
|
||||
@@ -147,7 +147,7 @@ extern "C" {
|
||||
*
|
||||
* @return The value within the specified range.
|
||||
*/
|
||||
#define mr_limit(value, min, max) mr_bound(value, min, max)
|
||||
#define MR_LIMIT(value, min, max) MR_BOUND(value, min, max)
|
||||
|
||||
/**
|
||||
* @brief This macro function swaps two values.
|
||||
@@ -155,7 +155,7 @@ extern "C" {
|
||||
* @param a The first value.
|
||||
* @param b The second value.
|
||||
*/
|
||||
#define mr_swap(a, b) do { typeof (a) temp = (a); (a) = (b); (b) = temp; } while (0)
|
||||
#define MR_SWAP(a, b) do { typeof (a) temp = (a); (a) = (b); (b) = temp; } while (0)
|
||||
|
||||
/**
|
||||
* @brief This macro function converts a value to a boolean.
|
||||
@@ -164,31 +164,7 @@ extern "C" {
|
||||
*
|
||||
* @return The boolean value.
|
||||
*/
|
||||
#define mr_to_bool(value) (!!(value))
|
||||
|
||||
/**
|
||||
* @brief This macro function asserts a condition.
|
||||
*
|
||||
* @param ex The condition to assert.
|
||||
*/
|
||||
#ifdef MR_USING_ASSERT
|
||||
#define mr_assert(ex) \
|
||||
do{ \
|
||||
if (!(ex)) \
|
||||
{ \
|
||||
mr_printf("assert > " \
|
||||
"failed: %s, " \
|
||||
"file: %s, " \
|
||||
"line: %d.\r\n", \
|
||||
#ex, \
|
||||
(__FUNCTION__), \
|
||||
(__LINE__)); \
|
||||
while(1); \
|
||||
} \
|
||||
} while(0)
|
||||
#else
|
||||
#define mr_assert(ex)
|
||||
#endif /* MR_USING_ASSERT */
|
||||
#define MR_TO_BOOL(value) (!!(value))
|
||||
|
||||
/**
|
||||
* @brief Log message with color.
|
||||
@@ -211,31 +187,56 @@ extern "C" {
|
||||
* @brief This macro function logs a error-warning-debug-info message.
|
||||
*/
|
||||
#if defined(MR_USING_LOG_ERROR) && defined(MR_USING_LOG)
|
||||
#define mr_log_error(fmt, ...) mr_printf("%s: %s\r\n", MR_LOG_COLOR_RED("ERROR", fmt), ##__VA_ARGS__)
|
||||
#define MR_LOG_ERROR(fmt, ...) mr_printf("%-8s %s\r\n", MR_LOG_COLOR_RED("ERROR:", fmt), ##__VA_ARGS__)
|
||||
#else
|
||||
#define mr_log_error(fmt, ...)
|
||||
#define MR_LOG_ERROR(fmt, ...)
|
||||
#endif /* defined(MR_USING_LOG_ERROR) && defined(MR_USING_LOG) */
|
||||
#if defined(MR_USING_LOG_WARN) && defined(MR_USING_LOG)
|
||||
#define mr_log_warn(fmt, ...) mr_printf("%s: %s\r\n", MR_LOG_COLOR_YELLOW("WARNING", fmt), ##__VA_ARGS__)
|
||||
#define MR_LOG_WARN(fmt, ...) mr_printf("%-8s %s\r\n", MR_LOG_COLOR_YELLOW("WARNING:", fmt), ##__VA_ARGS__)
|
||||
#else
|
||||
#define mr_log_warn(fmt, ...)
|
||||
#define MR_LOG_WARN(fmt, ...)
|
||||
#endif /* defined(MR_USING_LOG_WARN) && defined(MR_USING_LOG) */
|
||||
#if defined(MR_USING_LOG_INFO) && defined(MR_USING_LOG)
|
||||
#define mr_log_info(fmt, ...) mr_printf("%s: %s\r\n", MR_LOG_COLOR_BLUE("INFO", fmt), ##__VA_ARGS__)
|
||||
#define MR_LOG_INFO(fmt, ...) mr_printf("%-8s %s\r\n", MR_LOG_COLOR_BLUE("INFO:", fmt), ##__VA_ARGS__)
|
||||
#else
|
||||
#define mr_log_info(fmt, ...)
|
||||
#define MR_LOG_INFO(fmt, ...)
|
||||
#endif /* defined(MR_USING_LOG_INFO) && defined(MR_USING_LOG) */
|
||||
#if defined(MR_USING_LOG_DEBUG) && defined(MR_USING_LOG)
|
||||
#define mr_log_debug(fmt, ...) mr_printf("%s: %s\r\n", MR_LOG_COLOR_PURPLE("DEBUG", fmt), ##__VA_ARGS__)
|
||||
#define MR_LOG_DEBUG(fmt, ...) mr_printf("%-8s %s\r\n", MR_LOG_COLOR_PURPLE("DEBUG:", fmt), ##__VA_ARGS__)
|
||||
#else
|
||||
#define mr_log_debug(fmt, ...)
|
||||
#define MR_LOG_DEBUG(fmt, ...)
|
||||
#endif /* defined(MR_USING_LOG_DEBUG) && defined(MR_USING_LOG) */
|
||||
#if defined(MR_USING_LOG_SUCCESS) && defined(MR_USING_LOG)
|
||||
#define mr_log_success(fmt, ...) mr_printf("%s: %s\r\n", MR_LOG_COLOR_GREEN("SUCCESS", fmt), ##__VA_ARGS__)
|
||||
#define MR_LOG_SUCCESS(fmt, ...) mr_printf("%-8s %s\r\n", MR_LOG_COLOR_GREEN("SUCCESS:", fmt), ##__VA_ARGS__)
|
||||
#else
|
||||
#define mr_log_success(fmt, ...)
|
||||
#define MR_LOG_SUCCESS(fmt, ...)
|
||||
#endif /* defined(MR_USING_LOG_SUCCESS) && defined(MR_USING_LOG) */
|
||||
|
||||
/**
|
||||
* @brief This macro function asserts a condition.
|
||||
*
|
||||
* @param ex The condition to assert.
|
||||
*/
|
||||
#ifdef MR_USING_ASSERT
|
||||
#define MR_ASSERT(ex) \
|
||||
do{ \
|
||||
if (!(ex)) \
|
||||
{ \
|
||||
mr_printf("%-8s " \
|
||||
"failed: %s, " \
|
||||
"file: %s, " \
|
||||
"line: %d.\r\n", \
|
||||
"ASSERT:", \
|
||||
#ex, \
|
||||
(__FUNCTION__), \
|
||||
(__LINE__)); \
|
||||
while(1); \
|
||||
} \
|
||||
} while(0)
|
||||
#else
|
||||
#define mr_assert(ex)
|
||||
#endif /* MR_USING_ASSERT */
|
||||
|
||||
/**
|
||||
* @brief This function checks if a list is empty.
|
||||
*
|
||||
|
||||
177
source/device.c
177
source/device.c
@@ -27,23 +27,15 @@ MR_INLINE int dev_is_root(struct mr_dev *dev)
|
||||
|
||||
MR_INLINE struct mr_dev *dev_find_child(struct mr_dev *parent, const char *name)
|
||||
{
|
||||
/* Disable interrupt */
|
||||
mr_interrupt_disable();
|
||||
|
||||
/* Find the child device */
|
||||
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);
|
||||
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)
|
||||
{
|
||||
/* Enable interrupt */
|
||||
mr_interrupt_enable();
|
||||
return dev;
|
||||
}
|
||||
}
|
||||
|
||||
/* Enable interrupt */
|
||||
mr_interrupt_enable();
|
||||
return MR_NULL;
|
||||
}
|
||||
|
||||
@@ -55,17 +47,11 @@ MR_INLINE int dev_register_child(struct mr_dev *parent, struct mr_dev *child, co
|
||||
return MR_EEXIST;
|
||||
}
|
||||
|
||||
/* Disable interrupt */
|
||||
mr_interrupt_disable();
|
||||
|
||||
/* Insert the device into the child list */
|
||||
child->magic = MR_MAGIC_NUMBER;
|
||||
strncpy(child->name, name, MR_CFG_NAME_MAX);
|
||||
child->parent = parent;
|
||||
mr_list_insert_before(&parent->clist, &child->list);
|
||||
|
||||
/* Enable interrupt */
|
||||
mr_interrupt_enable();
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
@@ -87,30 +73,30 @@ MR_INLINE const char *dev_clear_path(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;
|
||||
char child_name[MR_CFG_NAME_MAX + 1] = {0};
|
||||
const char *child_path = MR_NULL;
|
||||
|
||||
/* Clear the path */
|
||||
path = dev_clear_path(path);
|
||||
|
||||
/* Check whether the parent exists */
|
||||
parent_path = strchr(path, '/');
|
||||
if (parent_path != MR_NULL)
|
||||
/* Check whether the child path exists */
|
||||
child_path = strchr(path, '/');
|
||||
if (child_path != MR_NULL)
|
||||
{
|
||||
/* Get the parent name */
|
||||
size_t len = (parent_path - path) > MR_CFG_NAME_MAX ? MR_CFG_NAME_MAX : (parent_path - path);
|
||||
strncpy(parent_name, path, len);
|
||||
parent_name[len] = '\0';
|
||||
/* Get the child name */
|
||||
size_t len = (child_path - path) > MR_CFG_NAME_MAX ? MR_CFG_NAME_MAX : (child_path - path);
|
||||
strncpy(child_name, path, len);
|
||||
child_name[len] = '\0';
|
||||
|
||||
/* Find the parent */
|
||||
parent = dev_find_child(parent, parent_name);
|
||||
if (parent == MR_NULL)
|
||||
/* Find the child */
|
||||
struct mr_dev *child = dev_find_child(parent, child_name);
|
||||
if (child == MR_NULL)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
|
||||
/* Continue iterating */
|
||||
return dev_register_by_path(parent, dev, parent_path);
|
||||
return dev_register_by_path(child, dev, child_path);
|
||||
} else
|
||||
{
|
||||
/* Register the child device to the parent */
|
||||
@@ -120,29 +106,33 @@ MR_INLINE int dev_register_by_path(struct mr_dev *parent, struct mr_dev *dev, co
|
||||
|
||||
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;
|
||||
char child_name[MR_CFG_NAME_MAX + 1] = {0};
|
||||
const char *child_path = MR_NULL;
|
||||
|
||||
/* Clear the path */
|
||||
path = dev_clear_path(path);
|
||||
|
||||
/* Check whether the parent exists */
|
||||
parent_path = strchr(path, '/');
|
||||
if (parent_path != MR_NULL)
|
||||
/* Check whether the child path exists */
|
||||
child_path = strchr(path, '/');
|
||||
if (child_path != MR_NULL)
|
||||
{
|
||||
/* Get the parent name */
|
||||
size_t len = (parent_path - path) > MR_CFG_NAME_MAX ? MR_CFG_NAME_MAX : (parent_path - path);
|
||||
strncpy(parent_name, path, len);
|
||||
parent_name[len] = '\0';
|
||||
/* Get the child name */
|
||||
size_t len = (child_path - path) > MR_CFG_NAME_MAX ? MR_CFG_NAME_MAX : (child_path - path);
|
||||
strncpy(child_name, path, len);
|
||||
child_name[len] = '\0';
|
||||
|
||||
parent = dev_find_child(parent, parent_name);
|
||||
if (parent == MR_NULL)
|
||||
/* Find the child */
|
||||
struct mr_dev *child = dev_find_child(parent, child_name);
|
||||
if (child == MR_NULL)
|
||||
{
|
||||
return MR_NULL;
|
||||
}
|
||||
return dev_find_by_path(parent, parent_path);
|
||||
|
||||
/* Continue iterating */
|
||||
return dev_find_by_path(child, child_path);
|
||||
} else
|
||||
{
|
||||
/* Find the child */
|
||||
return dev_find_child(parent, path);
|
||||
}
|
||||
}
|
||||
@@ -167,7 +157,7 @@ MR_INLINE int dev_lock_take(struct mr_dev *dev, int take, int set)
|
||||
}
|
||||
|
||||
/* Take the device */
|
||||
mr_bits_set(dev->lflags, set);
|
||||
MR_BIT_SET(dev->lflags, set);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
@@ -180,14 +170,21 @@ MR_INLINE void dev_lock_release(struct mr_dev *dev, int release)
|
||||
}
|
||||
|
||||
/* Release the device */
|
||||
mr_bits_clr(dev->lflags, release);
|
||||
MR_BIT_CLR(dev->lflags, release);
|
||||
}
|
||||
#endif /* MR_USING_RDWR_CTL */
|
||||
|
||||
MR_INLINE int dev_register(struct mr_dev *dev, const char *path)
|
||||
{
|
||||
/* Disable interrupt */
|
||||
mr_interrupt_disable();
|
||||
|
||||
/* Register the device to the root device */
|
||||
return dev_register_by_path(&root_dev, dev, path);
|
||||
int ret = dev_register_by_path(&root_dev, dev, path);
|
||||
|
||||
/* Enable interrupt */
|
||||
mr_interrupt_enable();
|
||||
return ret;
|
||||
}
|
||||
|
||||
MR_INLINE struct mr_dev *dev_find(const char *path)
|
||||
@@ -199,7 +196,7 @@ MR_INLINE struct mr_dev *dev_find(const char *path)
|
||||
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)
|
||||
if (MR_BIT_IS_SET(dev->sflags, oflags) != MR_ENABLE)
|
||||
{
|
||||
return MR_ENOTSUP;
|
||||
}
|
||||
@@ -229,7 +226,7 @@ MR_INLINE int dev_open(struct mr_dev *dev, int oflags)
|
||||
}
|
||||
}
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
else if (mr_bits_is_set(dev->sflags, MR_SFLAG_ONLY) == MR_ENABLE)
|
||||
else if (MR_BIT_IS_SET(dev->sflags, MR_SFLAG_ONLY) == MR_ENABLE)
|
||||
{
|
||||
return MR_EBUSY;
|
||||
}
|
||||
@@ -426,15 +423,15 @@ int mr_dev_register(struct mr_dev *dev,
|
||||
{
|
||||
static struct mr_dev_ops null_ops = {0};
|
||||
|
||||
mr_assert(dev != MR_NULL);
|
||||
mr_assert(dev->magic != MR_MAGIC_NUMBER);
|
||||
mr_assert(path != MR_NULL);
|
||||
mr_assert(type != Mr_Dev_Type_Root);
|
||||
mr_assert((ops != MR_NULL) || (sflags == MR_SFLAG_NONRDWR));
|
||||
mr_assert((ops->read != MR_NULL) || (mr_bits_is_set(sflags, MR_SFLAG_RDONLY) == MR_DISABLE));
|
||||
mr_assert((ops->write != MR_NULL) || (mr_bits_is_set(sflags, MR_SFLAG_WRONLY) == MR_DISABLE));
|
||||
mr_assert((drv != MR_NULL) || (sflags & MR_SFLAG_NONDRV));
|
||||
mr_assert((drv == MR_NULL) || (drv->type == type));
|
||||
MR_ASSERT(dev != MR_NULL);
|
||||
MR_ASSERT(dev->magic != MR_MAGIC_NUMBER);
|
||||
MR_ASSERT(path != MR_NULL);
|
||||
MR_ASSERT(type != Mr_Dev_Type_Root);
|
||||
MR_ASSERT((ops != MR_NULL) || (sflags == MR_SFLAG_NONRDWR));
|
||||
MR_ASSERT((ops->read != MR_NULL) || (MR_BIT_IS_SET(sflags, MR_SFLAG_RDONLY) == MR_DISABLE));
|
||||
MR_ASSERT((ops->write != MR_NULL) || (MR_BIT_IS_SET(sflags, MR_SFLAG_WRONLY) == MR_DISABLE));
|
||||
MR_ASSERT((drv != MR_NULL) || (sflags & MR_SFLAG_NONDRV));
|
||||
MR_ASSERT((drv == MR_NULL) || (drv->type == type));
|
||||
|
||||
/* Initialize the fields */
|
||||
dev->magic = 0;
|
||||
@@ -468,7 +465,7 @@ int mr_dev_register(struct mr_dev *dev,
|
||||
*/
|
||||
int mr_dev_isr(struct mr_dev *dev, int event, void *args)
|
||||
{
|
||||
mr_assert(dev != MR_NULL);
|
||||
MR_ASSERT(dev != MR_NULL);
|
||||
|
||||
/* Check whether the device is opened */
|
||||
if (dev->ref_count == 0)
|
||||
@@ -537,8 +534,8 @@ int mr_dev_get_path(struct mr_dev *dev, char *buf, size_t bufsz)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
mr_assert(dev != MR_NULL);
|
||||
mr_assert((buf != MR_NULL) || (bufsz == 0));
|
||||
MR_ASSERT(dev != MR_NULL);
|
||||
MR_ASSERT((buf != MR_NULL) || (bufsz == 0));
|
||||
|
||||
/* Continue to get the path of the parent device */
|
||||
if (dev->parent != MR_NULL)
|
||||
@@ -572,8 +569,8 @@ static struct mr_desc
|
||||
#endif /* MR_CFG_DESC_MAX */
|
||||
} desc_map[MR_CFG_DESC_MAX] = {0};
|
||||
|
||||
#define desc_of(desc) (desc_map[(desc)])
|
||||
#define desc_is_valid(desc) (((desc) >= 0 && (desc) < MR_CFG_DESC_MAX) && ((desc_of(desc).dev) != MR_NULL))
|
||||
#define DESC_OF(desc) (desc_map[(desc)])
|
||||
#define DESC_IS_VALID(desc) (((desc) >= 0 && (desc) < MR_CFG_DESC_MAX) && ((DESC_OF(desc).dev) != MR_NULL))
|
||||
|
||||
static int desc_allocate(const char *path)
|
||||
{
|
||||
@@ -582,7 +579,7 @@ static int desc_allocate(const char *path)
|
||||
/* Find a free descriptor */
|
||||
for (size_t i = 0; i < MR_CFG_DESC_MAX; i++)
|
||||
{
|
||||
if (desc_of(i).dev == MR_NULL)
|
||||
if (DESC_OF(i).dev == MR_NULL)
|
||||
{
|
||||
desc = (int)i;
|
||||
break;
|
||||
@@ -601,9 +598,9 @@ static int desc_allocate(const char *path)
|
||||
}
|
||||
|
||||
/* Initialize the fields */
|
||||
desc_of(desc).dev = dev;
|
||||
desc_of(desc).offset = -1;
|
||||
desc_of(desc).oflags = MR_OFLAG_CLOSED;
|
||||
DESC_OF(desc).dev = dev;
|
||||
DESC_OF(desc).offset = -1;
|
||||
DESC_OF(desc).oflags = MR_OFLAG_CLOSED;
|
||||
return desc;
|
||||
}
|
||||
|
||||
@@ -611,9 +608,9 @@ static void desc_free(int desc)
|
||||
{
|
||||
if (desc >= 0 && desc < MR_CFG_DESC_MAX)
|
||||
{
|
||||
desc_of(desc).dev = MR_NULL;
|
||||
desc_of(desc).oflags = MR_OFLAG_CLOSED;
|
||||
desc_of(desc).offset = -1;
|
||||
DESC_OF(desc).dev = MR_NULL;
|
||||
DESC_OF(desc).oflags = MR_OFLAG_CLOSED;
|
||||
DESC_OF(desc).offset = -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -627,8 +624,8 @@ static void desc_free(int desc)
|
||||
*/
|
||||
int mr_dev_open(const char *path, int oflags)
|
||||
{
|
||||
mr_assert(path != MR_NULL);
|
||||
mr_assert(oflags != MR_OFLAG_CLOSED);
|
||||
MR_ASSERT(path != MR_NULL);
|
||||
MR_ASSERT(oflags != MR_OFLAG_CLOSED);
|
||||
|
||||
/* Allocate a descriptor */
|
||||
int desc = desc_allocate(path);
|
||||
@@ -638,7 +635,7 @@ int mr_dev_open(const char *path, int oflags)
|
||||
}
|
||||
|
||||
/* Open the device */
|
||||
int ret = dev_open(desc_of(desc).dev, oflags);
|
||||
int ret = dev_open(DESC_OF(desc).dev, oflags);
|
||||
if (ret != MR_EOK)
|
||||
{
|
||||
desc_free(desc);
|
||||
@@ -646,7 +643,7 @@ int mr_dev_open(const char *path, int oflags)
|
||||
}
|
||||
|
||||
/* Initialize the open flags */
|
||||
desc_of(desc).oflags = oflags;
|
||||
DESC_OF(desc).oflags = oflags;
|
||||
return desc;
|
||||
}
|
||||
|
||||
@@ -659,10 +656,10 @@ int mr_dev_open(const char *path, int oflags)
|
||||
*/
|
||||
int mr_dev_close(int desc)
|
||||
{
|
||||
mr_assert(desc_is_valid(desc));
|
||||
MR_ASSERT(DESC_IS_VALID(desc));
|
||||
|
||||
/* Close the device */
|
||||
int ret = dev_close(desc_of(desc).dev);
|
||||
int ret = dev_close(DESC_OF(desc).dev);
|
||||
if (ret != MR_EOK)
|
||||
{
|
||||
return ret;
|
||||
@@ -684,22 +681,22 @@ int mr_dev_close(int desc)
|
||||
*/
|
||||
ssize_t mr_dev_read(int desc, void *buf, size_t size)
|
||||
{
|
||||
mr_assert(desc_is_valid(desc));
|
||||
mr_assert(buf != MR_NULL || size == 0);
|
||||
MR_ASSERT(DESC_IS_VALID(desc));
|
||||
MR_ASSERT(buf != MR_NULL || size == 0);
|
||||
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
if (mr_bits_is_set(desc_of(desc).oflags, MR_OFLAG_RDONLY) == MR_DISABLE)
|
||||
if (MR_BIT_IS_SET(DESC_OF(desc).oflags, MR_OFLAG_RDONLY) == MR_DISABLE)
|
||||
{
|
||||
return MR_ENOTSUP;
|
||||
}
|
||||
#endif /* MR_USING_RDWR_CTL */
|
||||
|
||||
/* Read buffer from the device */
|
||||
return dev_read(desc_of(desc).dev,
|
||||
desc_of(desc).offset,
|
||||
return dev_read(DESC_OF(desc).dev,
|
||||
DESC_OF(desc).offset,
|
||||
buf,
|
||||
size,
|
||||
(mr_bits_is_set(desc_of(desc).oflags, MR_OFLAG_NONBLOCK)));
|
||||
(MR_BIT_IS_SET(DESC_OF(desc).oflags, MR_OFLAG_NONBLOCK)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -713,22 +710,22 @@ ssize_t mr_dev_read(int desc, void *buf, size_t size)
|
||||
*/
|
||||
ssize_t mr_dev_write(int desc, const void *buf, size_t size)
|
||||
{
|
||||
mr_assert(desc_is_valid(desc));
|
||||
mr_assert(buf != MR_NULL || size == 0);
|
||||
MR_ASSERT(DESC_IS_VALID(desc));
|
||||
MR_ASSERT(buf != MR_NULL || size == 0);
|
||||
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
if (mr_bits_is_set(desc_of(desc).oflags, MR_OFLAG_WRONLY) == MR_DISABLE)
|
||||
if (MR_BIT_IS_SET(DESC_OF(desc).oflags, MR_OFLAG_WRONLY) == MR_DISABLE)
|
||||
{
|
||||
return MR_ENOTSUP;
|
||||
}
|
||||
#endif /* MR_USING_RDWR_CTL */
|
||||
|
||||
/* Write buffer to the device */
|
||||
return dev_write(desc_of(desc).dev,
|
||||
desc_of(desc).offset,
|
||||
return dev_write(DESC_OF(desc).dev,
|
||||
DESC_OF(desc).offset,
|
||||
buf,
|
||||
size,
|
||||
(mr_bits_is_set(desc_of(desc).oflags, MR_OFLAG_NONBLOCK)));
|
||||
(MR_BIT_IS_SET(DESC_OF(desc).oflags, MR_OFLAG_NONBLOCK)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -742,7 +739,7 @@ ssize_t mr_dev_write(int desc, const void *buf, size_t size)
|
||||
*/
|
||||
int mr_dev_ioctl(int desc, int cmd, void *args)
|
||||
{
|
||||
mr_assert(desc_is_valid(desc));
|
||||
MR_ASSERT(DESC_IS_VALID(desc));
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
@@ -750,7 +747,7 @@ int mr_dev_ioctl(int desc, int cmd, void *args)
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
desc_of(desc).offset = *(int *)args;
|
||||
DESC_OF(desc).offset = *(int *)args;
|
||||
return MR_EOK;
|
||||
}
|
||||
return MR_EINVAL;
|
||||
@@ -760,7 +757,7 @@ int mr_dev_ioctl(int desc, int cmd, void *args)
|
||||
{
|
||||
if (args != MR_NULL)
|
||||
{
|
||||
*(int *)args = desc_of(desc).offset;
|
||||
*(int *)args = DESC_OF(desc).offset;
|
||||
return MR_EOK;
|
||||
}
|
||||
return MR_EINVAL;
|
||||
@@ -769,7 +766,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);
|
||||
return dev_ioctl(DESC_OF(desc).dev, desc, DESC_OF(desc).offset, cmd, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -783,7 +780,7 @@ int mr_dev_ioctl(int desc, int cmd, void *args)
|
||||
*/
|
||||
const char *mr_dev_get_name(int desc)
|
||||
{
|
||||
mr_assert(desc_is_valid(desc));
|
||||
MR_ASSERT(DESC_IS_VALID(desc));
|
||||
|
||||
return desc_of(desc).dev->name;
|
||||
return DESC_OF(desc).dev->name;
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ int mr_heap_init(void)
|
||||
heap_start.next = first_block;
|
||||
return MR_EOK;
|
||||
}
|
||||
MR_BOARD_EXPORT(mr_heap_init);
|
||||
MR_INIT_BOARD_EXPORT(mr_heap_init);
|
||||
|
||||
static void heap_insert_block(struct mr_heap_block *block)
|
||||
{
|
||||
@@ -115,7 +115,7 @@ MR_WEAK void *mr_malloc(size_t size)
|
||||
}
|
||||
|
||||
/* Align the size up 4 bytes */
|
||||
size = mr_align4_up(size);
|
||||
size = MR_ALIGN4_UP(size);
|
||||
|
||||
/* Search for and take blocks that match the criteria */
|
||||
while (block->size < size)
|
||||
|
||||
Reference in New Issue
Block a user