1.新增pwm。

2.优化其余设备。
This commit is contained in:
MacRsh
2023-12-25 15:48:49 +08:00
parent 0b29f94ebe
commit 868d7cc9ce
17 changed files with 637 additions and 180 deletions

View File

@@ -168,6 +168,12 @@ menu "Device configure"
help
"Use this option allows for the use of Pin devices."
config MR_USING_PWM
bool "Use PWM device"
default n
help
"Use this option allows for the use of PWM devices."
config MR_USING_SERIAL
bool "Use Serial device"
default n

View File

@@ -110,7 +110,7 @@ static int mr_adc_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_adc_config config = *((struct mr_adc_config *)args);
return adc_channel_set_state(adc, off, config.channel_state);
return adc_channel_set_state(adc, off, config.state);
}
return MR_EINVAL;
}
@@ -126,7 +126,7 @@ static int mr_adc_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
return ret;
}
config->channel_state = ret;
config->state = ret;
return MR_EOK;
}
}

View File

@@ -110,10 +110,11 @@ static int mr_dac_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_dac_config config = *((struct mr_dac_config *)args);
return dac_channel_set_state(dac, off, config.channel_state);
return dac_channel_set_state(dac, off, config.state);
}
return MR_EINVAL;
}
case MR_CTL_DAC_GET_CHANNEL_STATE:
{
if (args != MR_NULL)

View File

@@ -109,7 +109,7 @@ static ssize_t mr_pin_write(struct mr_dev *dev, int off, const void *buf, size_t
for (wr_size = 0; wr_size < size; wr_size += sizeof(*wr_buf))
{
ops->write(pin, off, (int)*wr_buf);
ops->write(pin, off, *wr_buf);
wr_buf++;
}
return wr_size;

324
device/pwm.c Normal file
View File

@@ -0,0 +1,324 @@
/*
* @copyright (c) 2023, MR Development Team
*
* @license SPDX-License-Identifier: Apache-2.0
*
* @date 2023-12-10 MacRsh First version
*/
#include "include/device/pwm.h"
#ifdef MR_USING_PWM
static int pwm_channel_set_configure(struct mr_pwm *pwm, int channel, struct mr_pwm_config config)
{
struct mr_pwm_ops *ops = (struct mr_pwm_ops *)pwm->dev.drv->ops;
/* Check channel is valid */
if (channel < 0 || channel >= 32)
{
return MR_EINVAL;
}
/* Configure the channel */
int ret = ops->channel_configure(pwm, channel, config.state, config.polarity);
if (ret != MR_EOK)
{
return ret;
}
/* Enable or disable the channel */
if (config.state == MR_PWM_ENABLE)
{
mr_bits_set(pwm->channel, (1 << channel));
/* Configure the polarity */
if (config.polarity == MR_PWM_POLARITY_NORMAL)
{
mr_bits_clr(pwm->channel_polarity, (1 << channel));
} else
{
mr_bits_set(pwm->channel_polarity, (1 << channel));
}
} else
{
mr_bits_clr(pwm->channel, (1 << channel));
mr_bits_clr(pwm->channel_polarity, (1 << channel));
}
return MR_EOK;
}
static int pwm_channel_get_configure(struct mr_pwm *pwm, int channel, struct mr_pwm_config *config)
{
/* Check channel is valid */
if (channel < 0 || channel >= 32)
{
return MR_EINVAL;
}
/* Get configure */
config->state = mr_bits_is_set(pwm->channel, (1 << channel));
config->polarity = mr_bits_is_set(pwm->channel_polarity, (1 << channel));
return config->state;
}
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;
/* Check the clock */
if (clk == 0 || freq == 0)
{
return MR_EINVAL;
}
/* Calculate the timeout */
timeout = (clk * 1000000) / freq;
/* Calculate the Least error period */
for (per = (timeout <= per_max) ? timeout : (timeout / (per_max + 1)); per > 0; per--)
{
psc = timeout / per;
/* Calculate the error */
error = (int)timeout - (int)(psc * per);
if (error == 0)
{
psc_best = psc;
per_best = per;
break;
}
if (error <= error_min)
{
error_min = error;
psc_best = psc;
per_best = per;
}
}
/* Optimize the prescaler and period */
uint32_t divisor = 0;
for (divisor = 9; divisor > 1; divisor--)
{
/* Check if reload value can be divided by current divisor */
while ((psc_best % divisor) == 0)
{
uint32_t per_temp = per_best * divisor;
/* Check if new period or prescaler is valid */
if (per_temp <= per_max)
{
per_best = per_temp;
psc_best /= divisor;
} else
{
break;
}
/* Check if reload can be used as period or prescaler */
if ((psc_best > per_best) && (psc_best < per_max))
{
mr_swap(per_best, psc_best);
}
}
}
/* Check prescaler is valid */
if (psc_best > psc_max)
{
return MR_EINVAL;
}
pwm->prescaler = psc_best;
pwm->period = per_best;
pwm->freq = clk / psc_best / per_best;
return MR_EOK;
}
static int mr_pwm_open(struct mr_dev *dev)
{
struct mr_pwm *pwm = (struct mr_pwm *)dev;
struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops;
return ops->configure(pwm, MR_ENABLE);
}
static int mr_pwm_close(struct mr_dev *dev)
{
struct mr_pwm *pwm = (struct mr_pwm *)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++)
{
if (mr_bits_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));
}
}
return ops->configure(pwm, MR_DISABLE);
}
static ssize_t mr_pwm_read(struct mr_dev *dev, int off, void *buf, size_t size, int async)
{
struct mr_pwm *pwm = (struct mr_pwm *)dev;
struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops;
uint32_t *rd_buf = (uint32_t *)buf;
ssize_t rd_size = 0;
/* Check if the channel is enabled */
if (mr_bits_is_set(pwm->channel, (1 << off)) == MR_DISABLE)
{
return MR_EINVAL;
}
mr_bits_clr(size, sizeof(*rd_buf) - 1);
for (rd_size = 0; rd_size < size; rd_size += sizeof(*rd_buf))
{
*rd_buf = ops->read(pwm, off);
rd_buf++;
}
return rd_size;
}
static ssize_t mr_pwm_write(struct mr_dev *dev, int off, const void *buf, size_t size, int async)
{
struct mr_pwm *pwm = (struct mr_pwm *)dev;
struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops;
uint32_t *wr_buf = (uint32_t *)buf;
ssize_t wr_size = 0;
/* Check if the channel is enabled */
if (mr_bits_is_set(pwm->channel, (1 << off)) == MR_DISABLE)
{
return MR_EINVAL;
}
mr_bits_clr(size, sizeof(*wr_buf) - 1);
for (wr_size = 0; wr_size < size; wr_size += sizeof(*wr_buf))
{
ops->write(pwm, off, *wr_buf);
wr_buf++;
}
return wr_size;
}
static int mr_pwm_ioctl(struct mr_dev *dev, int off, int cmd, void *args)
{
struct mr_pwm *pwm = (struct mr_pwm *)dev;
struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops;
switch (cmd)
{
case MR_CTL_PWM_SET_CHANNEL_CONFIG:
{
if (args != MR_NULL)
{
struct mr_pwm_config config = *((struct mr_pwm_config *)args);
return pwm_channel_set_configure(pwm, off, config);
}
return MR_EINVAL;
}
case MR_CTL_PWM_SET_FREQ:
{
if (args != MR_NULL)
{
uint32_t freq = *((uint32_t *)args);
/* Calculate prescaler and period */
int ret = pwm_calculate(pwm, freq);
if (ret != MR_EOK)
{
return ret;
}
/* Start pwm */
ops->start(pwm, pwm->prescaler, pwm->period);
return MR_EOK;
}
return MR_EINVAL;
}
case MR_CTL_PWM_GET_CHANNEL_CONFIG:
{
if (args != MR_NULL)
{
struct mr_pwm_config *config = ((struct mr_pwm_config *)args);
int ret = pwm_channel_get_configure(pwm, off, config);
if (ret < 0)
{
return ret;
}
return MR_EOK;
}
return MR_EINVAL;
}
case MR_CTL_PWM_GET_FREQ:
{
if (args != MR_NULL)
{
uint32_t *freq = (uint32_t *)args;
*freq = pwm->freq;
return MR_EOK;
}
return MR_EINVAL;
}
default:
{
return MR_ENOTSUP;
}
}
}
/**
* @brief This function registers a pwm.
*
* @param pwm The pwm.
* @param name The name of the pwm.
* @param drv The driver of the pwm.
* @param info The information of the pwm.
*
* @return MR_EOK on success, otherwise an error code.
*/
int mr_pwm_register(struct mr_pwm *pwm, const char *name, struct mr_drv *drv, struct mr_pwm_info *info)
{
static struct mr_dev_ops ops =
{
mr_pwm_open,
mr_pwm_close,
mr_pwm_read,
mr_pwm_write,
mr_pwm_ioctl,
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;
pwm->prescaler = 0;
pwm->period = 0;
pwm->channel = 0;
pwm->channel_polarity = 0;
pwm->info = info;
return mr_dev_register(&pwm->dev, name, Mr_Dev_Type_PWM, MR_SFLAG_RDWR, &ops, drv);
}
#endif /* MR_USING_PWM */

View File

@@ -35,7 +35,7 @@ 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 > 0; per--)
for (per = (timeout <= per_max) ? timeout : (timeout / (per_max + 1)); per > 1; per--)
{
reload = timeout / per;
@@ -59,7 +59,7 @@ static int timer_calculate(struct mr_timer *timer, uint32_t timeout)
/* Optimize the prescaler and period */
uint32_t divisor = 0;
for (divisor = 2; divisor <= 9; divisor++)
for (divisor = 9; divisor > 1; divisor--)
{
/* Check if reload value can be divided by current divisor */
while ((reload_best % divisor) == 0)

View File

@@ -106,7 +106,7 @@ mr_dev_ioctl(ds, MR_CTL_SERIAL_GET_CONFIG, &config);
- 停止位数:`MR_SERIAL_STOP_BITS_1`
- 校验位:`MR_SERIAL_PARITY_NONE`
- 数据传输顺序:`MR_SERIAL_BIT_ORDER_LSB`
- 极性反转:`MR_SERIAL_NRZ_NORMAL`
- 极性反转:`MR_SERIAL_POLARITY_NORMAL`
### 设置/获取读/写缓冲区大小

View File

@@ -28,7 +28,7 @@ extern "C" {
*/
struct mr_adc_config
{
uint32_t channel_state: 1; /**< Channel state */
uint32_t state: 1; /**< Channel state */
uint32_t reserved: 31; /**< Reserved */
};

View File

@@ -28,7 +28,7 @@ extern "C" {
*/
struct mr_dac_config
{
uint32_t channel_state: 1; /**< Channel state */
uint32_t state: 1; /**< Channel state */
uint32_t reserved: 31; /**< Reserved */
};

107
include/device/pwm.h Normal file
View File

@@ -0,0 +1,107 @@
/*
* @copyright (c) 2023, MR Development Team
*
* @license SPDX-License-Identifier: Apache-2.0
*
* @date 2023-12-10 MacRsh First version
*/
#ifndef _PWM_H_
#define _PWM_H_
#include "include/mr_api.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifdef MR_USING_PWM
/**
* @brief PWM channel state.
*/
#define MR_PWM_DISABLE MR_DISABLE /**< PWM disable */
#define MR_PWM_ENABLE MR_ENABLE /**< PWM enable */
/**
* @brief PWM channel polarity.
*/
#define MR_PWM_POLARITY_NORMAL (0) /**< PWM normal polarity */
#define MR_PWM_POLARITY_INVERTED (1) /**< PWM inverted polarity */
/**
* @brief PWM configuration structure.
*/
struct mr_pwm_config
{
uint32_t state: 1; /**< Channel state */
uint32_t polarity: 1; /**< Channel polarity */
};
/**
* @brief PWM control command.
*/
#define MR_CTL_PWM_SET_CHANNEL MR_CTL_SET_OFFSET /**< Set channel */
#define MR_CTL_PWM_SET_CHANNEL_CONFIG MR_CTL_SET_CONFIG /**< Set channel config */
#define MR_CTL_PWM_SET_FREQ (0x01 << 8) /**< Set frequency */
#define MR_CTL_PWM_GET_CHANNEL MR_CTL_GET_OFFSET /**< Get channel */
#define MR_CTL_PWM_GET_CHANNEL_CONFIG MR_CTL_GET_CONFIG /**< Get channel config */
#define MR_CTL_PWM_GET_FREQ (-(0x01 << 8)) /**< Get frequency */
/**
* @brief PWM data type.
*/
typedef uint32_t mr_pwm_data_t; /**< PWM read/write data type */
/**
* @brief PWM information structure.
*/
struct mr_pwm_info
{
uint32_t clk; /**< Clock(MHz) */
uint32_t prescaler_max; /**< Prescaler max */
uint32_t period_max; /**< Period max */
};
/**
* @brief PWM structure.
*/
struct mr_pwm
{
struct mr_dev dev; /**< Device */
uint32_t freq; /**< Frequency */
uint32_t prescaler; /**< Prescaler */
uint32_t period; /**< Period */
uint32_t channel; /**< Channel */
uint32_t channel_polarity; /**< Channel polarity */
struct mr_pwm_info *info; /**< Information */
};
/**
* @brief PWM operations structure.
*/
struct mr_pwm_ops
{
int (*configure)(struct mr_pwm *pwm, int state);
int (*channel_configure)(struct mr_pwm *pwm, int channel, int state, int polarity);
void (*start)(struct mr_pwm *pwm, uint32_t prescaler, uint32_t period);
void (*write)(struct mr_pwm *pwm, int channel, uint32_t duty);
uint32_t (*read)(struct mr_pwm *pwm, int channel);
};
/**
* @addtogroup PWM.
* @{
*/
int mr_pwm_register(struct mr_pwm *pwm, const char *name, struct mr_drv *drv, struct mr_pwm_info *info);
/** @} */
#endif /* MR_USING_PWM */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _PWM_H_ */

View File

@@ -49,8 +49,8 @@ extern "C" {
/**
* @brief SERIAL polarity.
*/
#define MR_SERIAL_NRZ_NORMAL (0) /**< Normal polarity */
#define MR_SERIAL_NRZ_INVERTED (1) /**< Inverted polarity */
#define MR_SERIAL_POLARITY_NORMAL (0) /**< Normal polarity */
#define MR_SERIAL_POLARITY_INVERTED (1) /**< Inverted polarity */
/**
* @brief SERIAL default configuration.
@@ -62,7 +62,7 @@ extern "C" {
MR_SERIAL_STOP_BITS_1, \
MR_SERIAL_PARITY_NONE, \
MR_SERIAL_BIT_ORDER_LSB, \
MR_SERIAL_NRZ_NORMAL, \
MR_SERIAL_POLARITY_NORMAL, \
}
/**
@@ -75,7 +75,7 @@ struct mr_serial_config
uint32_t stop_bits: 3; /**< Stop bits */
uint32_t parity: 2; /**< Parity */
uint32_t bit_order: 1; /**< Bit order */
uint32_t invert: 1; /**< Invert */
uint32_t polarity: 1; /**< Polarity */
uint32_t reserved: 21;
};

View File

@@ -24,12 +24,12 @@ extern "C" {
*/
struct mr_soft_i2c_bus
{
struct mr_i2c_bus i2c_bus; /* I2C-bus device */
struct mr_i2c_bus i2c_bus; /**< I2C-bus device */
uint32_t delay; /* Speed delay */
int scl_pin; /* SCL pin */
int sda_pin; /* SDA pin */
int desc; /* SCL-SDA descriptor */
uint32_t delay; /**< Speed delay */
int scl_pin; /**< SCL pin */
int sda_pin; /**< SDA pin */
int desc; /**< SCL-SDA descriptor */
};
/**

View File

@@ -41,8 +41,8 @@ extern "C" {
/**
* @brief SPI bit order.
*/
#define MR_SPI_BIT_ORDER_MSB (0) /**< MSB first */
#define MR_SPI_BIT_ORDER_LSB (1) /**< LSB first */
#define MR_SPI_BIT_ORDER_LSB (0) /**< LSB first */
#define MR_SPI_BIT_ORDER_MSB (1) /**< MSB first */
/**
* @brief SPI register bits.

View File

@@ -36,7 +36,7 @@ extern "C" {
*/
struct mr_timer_config
{
uint32_t mode: 1; /* Mode */
uint32_t mode: 1; /**< Mode */
uint32_t reserved: 31;
};
@@ -64,9 +64,9 @@ typedef uint32_t mr_timer_data_t; /**< Timer r
*/
struct mr_timer_info
{
uint32_t clk; /* Clock(MHz) */
uint32_t prescaler_max; /* Prescaler max */
uint32_t period_max; /* Period max */
uint32_t clk; /**< Clock(MHz) */
uint32_t prescaler_max; /**< Prescaler max */
uint32_t period_max; /**< Period max */
};
/**
@@ -74,16 +74,16 @@ struct mr_timer_info
*/
struct mr_timer
{
struct mr_dev dev; /* Device */
struct mr_dev dev; /**< Device */
struct mr_timer_config config; /* Config */
uint32_t reload; /* Reload */
uint32_t count; /* Count */
uint32_t timeout; /* Timeout */
uint32_t period; /* Period */
uint32_t prescaler; /* Prescaler */
struct mr_timer_config config; /**< Config */
uint32_t reload; /**< Reload */
uint32_t count; /**< Count */
uint32_t timeout; /**< Timeout */
uint32_t prescaler; /**< Prescaler */
uint32_t period; /**< Period */
struct mr_timer_info *info; /* Information */
struct mr_timer_info *info; /**< Information */
};
/**

View File

@@ -164,6 +164,7 @@ enum mr_drv_type
Mr_Drv_Type_Serial, /**< SERIAL */
Mr_Drv_Type_SPI, /**< SPI */
Mr_Drv_Type_Timer, /**< Timer */
Mr_Drv_Type_PWM, /**< PWM */
};
/**
@@ -190,6 +191,7 @@ enum mr_dev_type
Mr_Dev_Type_Serial = Mr_Drv_Type_Serial, /**< SERIAL */
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 */
};

View File

@@ -15,6 +15,157 @@
extern "C" {
#endif /* __cplusplus */
/**
* @brief This macro function concatenates two strings.
*
* @param a The first string.
* @param b The second string.
*
* @return The concatenated string.
*/
#define MR_CONCAT(a, b) a##b
/**
* @brief This macro function converts an integer to a string.
*
* @param a The integer to convert.
*
* @return The string representation of the integer.
*/
#define MR_STR(a) #a
/**
* @brief This macro function gets its structure from its member.
*
* @param pointer The pointer to the structure.
* @param type The type of the structure.
* @param member The member of the structure.
*
* @return A pointer to the structure.
*/
#define mr_container_of(pointer, type, member) \
((type *)((char *)(pointer) - (unsigned long)(&((type *)0)->member)))
/**
* @brief This macro function aligns the size up to a multiple of 4.
*
* @param size The size to align.
*/
#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))
/**
* @brief This macro function checks if a value is set.
*
* @param value The value to check.
* @param mask The mask to check.
*/
#define mr_bits_is_set(value, mask) (((value) & (mask)) == (mask))
/**
* @brief This macro function sets a value.
*
* @param value The value to set.
* @param mask The mask to set.
*/
#define mr_bits_set(value, mask) ((value) |= (mask))
/**
* @brief This macro function clears a value.
*
* @param value The value to clear.
* @param mask The mask to clear.
*/
#define mr_bits_clr(value, mask) ((value) &= ~(mask))
/**
* @brief This macro function creates a local variable.
*
* @param type The type of the variable.
* @param value The value of the variable.
*
* @return A pointer to the variable.
*
* @note The variable is local, please use it carefully.
*/
#define mr_make_local(type, ...) (&((type){__VA_ARGS__}))
/**
* @brief This macro function gets the number of elements in an array.
*
* @param array The array.
*
* @return The number of elements in the array.
*/
#define mr_array_num(array) (sizeof(array)/sizeof((array)[0]))
/**
* @brief This macro function gets the maximum of two values.
*
* @param a The first value.
* @param b The second value.
*
* @return The maximum of the two values.
*/
#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.
*
* @param a The first value.
* @param b The second value.
*
* @return The minimum of the two values.
*/
#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.
*
* @param value The value.
* @param min The minimum value.
* @param max The maximum value.
*
* @return The value within the specified range.
*/
#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));})
/**
* @brief This macro function ensures that a value is within a specified range.
*
* @param value The value.
* @param min The minimum value.
* @param max The maximum value.
*
* @return The value within the specified range.
*/
#define mr_limit(value, min, max) mr_bound(value, min, max)
/**
* @brief This macro function swaps two values.
*
* @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)
/**
* @brief This macro function converts a value to a boolean.
*
* @param value The value to convert.
*
* @return The boolean value.
*/
#define mr_to_bool(value) (!!(value))
/**
* @brief This macro function asserts a condition.
*
@@ -86,153 +237,16 @@ extern "C" {
#endif /* defined(MR_USING_LOG_SUCCESS) && defined(MR_USING_LOG) */
/**
* @brief This macro function gets its structure from its member.
*
* @param pointer The pointer to the structure.
* @param type The type of the structure.
* @param member The member of the structure.
*
* @return A pointer to the structure.
*/
#define mr_container_of(pointer, type, member) \
((type *)((char *)(pointer) - (unsigned long)(&((type *)0)->member)))
/**
* @brief This macro function checks if a value is set.
*
* @param value The value to check.
* @param mask The mask to check.
*/
#define mr_bits_is_set(value, mask) (((value) & (mask)) == (mask))
/**
* @brief This macro function sets a value.
*
* @param value The value to set.
* @param mask The mask to set.
*/
#define mr_bits_set(value, mask) ((value) |= (mask))
/**
* @brief This macro function clears a value.
*
* @param value The value to clear.
* @param mask The mask to clear.
*/
#define mr_bits_clr(value, mask) ((value) &= ~(mask))
/**
* @brief This macro function gets the number of elements in an array.
*
* @param array The array.
*
* @return The number of elements in the array.
*/
#define mr_array_num(array) (sizeof(array)/sizeof((array)[0]))
/**
* @brief This macro function creates a local variable.
*
* @param type The type of the variable.
* @param value The value of the variable.
*
* @return A pointer to the variable.
*/
#define mr_make_local(type, ...) (&((type){__VA_ARGS__}))
/**
* @brief This macro function gets the maximum of two values.
*
* @param a The first value.
* @param b The second value.
*
* @return The maximum of the two values.
*/
#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.
*
* @param a The first value.
* @param b The second value.
*
* @return The minimum of the two values.
*/
#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.
*
* @param value The value.
* @param min The minimum value.
* @param max The maximum value.
*
* @return The value within the specified range.
*/
#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));})
/**
* @brief This macro function ensures that a value is within a specified range.
*
* @param value The value.
* @param min The minimum value.
* @param max The maximum value.
*
* @return The value within the specified range.
*/
#define mr_limit(value, min, max) mr_bound(value, min, max)
/**
* @brief This macro function swaps two values.
*
* @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)
/**
* @brief This macro function aligns the size up to a multiple of 4.
*
* @param size The size to align.
*/
#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))
/**
* @brief This macro function concatenates two strings.
*
* @param a The first string.
* @param b The second string.
*
* @return The concatenated string.
*/
#define MR_CONCAT(a, b) a##b
/**
* @brief This macro function converts an integer to a string.
*
* @param a The integer to convert.
*
* @return The string representation of the integer.
*/
#define MR_STR(a) #a
/**
* @brief This macro function checks if a list is empty.
* @brief This function checks if a list is empty.
*
* @param list The list to check.
*
* @return True if the list is empty, mr_false otherwise.
*/
#define mr_list_is_empty(list) (((list)->next) == (list))
MR_INLINE int mr_list_is_empty(struct mr_list *list)
{
return list->next == list;
}
/**
* @brief This function initialize a double list.

View File

@@ -10,12 +10,15 @@
#define MR_ROOT_DEV_NAME "dev"
static struct mr_dev root_dev = {MR_MAGIC_NUMBER,
MR_ROOT_DEV_NAME,
Mr_Dev_Type_Root,
MR_NULL,
{&root_dev.list, &root_dev.list},
{&root_dev.clist, &root_dev.clist}};
static struct mr_dev root_dev =
{
MR_MAGIC_NUMBER,
MR_ROOT_DEV_NAME,
Mr_Dev_Type_Root,
MR_NULL,
{&root_dev.list, &root_dev.list},
{&root_dev.clist, &root_dev.clist}
};
static int dev_is_root(struct mr_dev *dev)
{