1.更新驱动,适配设备新接口。
This commit is contained in:
@@ -105,17 +105,19 @@ static int drv_adc_channel_configure(struct mr_adc *adc, int channel, int state)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static uint32_t drv_adc_read(struct mr_adc *adc, int channel)
|
||||
static int drv_adc_read(struct mr_adc *adc, int channel, uint32_t *data)
|
||||
{
|
||||
struct drv_adc_data *adc_data = (struct drv_adc_data *)adc->dev.drv->data;
|
||||
struct drv_adc_channel_data *adc_channel_data = drv_adc_get_channel_data(channel);
|
||||
ADC_ChannelConfTypeDef sConfig = {0};
|
||||
|
||||
#ifdef MR_USING_ADC_CHANNEL_CHECK
|
||||
/* Check channel is valid */
|
||||
if (adc_channel_data == NULL)
|
||||
{
|
||||
return 0;
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_ADC_CHANNEL_CHECK */
|
||||
|
||||
/* Read data */
|
||||
sConfig.Channel = adc_channel_data->channel;
|
||||
@@ -141,9 +143,14 @@ static uint32_t drv_adc_read(struct mr_adc *adc, int channel)
|
||||
#endif /* defined(ADC_SAMPLETIME_55CYCLES_5) */
|
||||
HAL_ADC_ConfigChannel(&adc_data->handle, &sConfig);
|
||||
HAL_ADC_Start(&adc_data->handle);
|
||||
HAL_ADC_PollForConversion(&adc_data->handle, UINT16_MAX);
|
||||
if (HAL_ADC_PollForConversion(&adc_data->handle, UINT16_MAX) == HAL_OK)
|
||||
{
|
||||
*data = HAL_ADC_GetValue(&adc_data->handle);
|
||||
HAL_ADC_Stop(&adc_data->handle);
|
||||
return HAL_ADC_GetValue(&adc_data->handle);
|
||||
return MR_EOK;
|
||||
}
|
||||
HAL_ADC_Stop(&adc_data->handle);
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
|
||||
static struct mr_adc_ops adc_drv_ops =
|
||||
|
||||
@@ -90,11 +90,13 @@ static int drv_dac_channel_configure(struct mr_dac *dac, int channel, int state)
|
||||
struct drv_dac_channel_data *dac_channel_data = drv_dac_get_channel_data(channel);
|
||||
DAC_ChannelConfTypeDef sConfig = {0};
|
||||
|
||||
#ifdef MR_USING_DAC_CHANNEL_CHECK
|
||||
/* Check channel is valid */
|
||||
if (dac_channel_data == NULL)
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_DAC_CHANNEL_CHECK */
|
||||
|
||||
/* Configure Channel */
|
||||
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
|
||||
@@ -111,16 +113,18 @@ static int drv_dac_channel_configure(struct mr_dac *dac, int channel, int state)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_dac_write(struct mr_dac *dac, int channel, uint32_t data)
|
||||
static int drv_dac_write(struct mr_dac *dac, int channel, uint32_t data)
|
||||
{
|
||||
struct drv_dac_data *dac_data = (struct drv_dac_data *)dac->dev.drv->data;
|
||||
struct drv_dac_channel_data *dac_channel_data = drv_dac_get_channel_data(channel);
|
||||
|
||||
#ifdef MR_USING_DAC_CHANNEL_CHECK
|
||||
/* Check channel is valid */
|
||||
if (dac_channel_data == NULL)
|
||||
{
|
||||
return;
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_DAC_CHANNEL_CHECK */
|
||||
|
||||
/* Write data */
|
||||
HAL_DAC_SetValue(&dac_data->handle, dac_channel_data->channel, DAC_ALIGN_12B_R, (data & 0xFFF));
|
||||
|
||||
@@ -214,7 +214,7 @@ static int drv_pin_configure(struct mr_pin *pin, int number, int mode)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static uint8_t drv_pin_read(struct mr_pin *pin, int number)
|
||||
static int drv_pin_read(struct mr_pin *pin, int number, uint8_t *value)
|
||||
{
|
||||
struct drv_pin_port_data *pin_port_data = drv_pin_get_port_data(number);
|
||||
struct drv_pin_data *pin_data = drv_pin_get_data(number);
|
||||
@@ -223,13 +223,14 @@ static uint8_t drv_pin_read(struct mr_pin *pin, int number)
|
||||
/* Check pin is valid */
|
||||
if (pin_port_data == NULL || pin_data == NULL)
|
||||
{
|
||||
return 0;
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_PIN_CHECK */
|
||||
return (int)HAL_GPIO_ReadPin(pin_port_data->port, pin_data->pin);
|
||||
*value = HAL_GPIO_ReadPin(pin_port_data->port, pin_data->pin);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_pin_write(struct mr_pin *pin, int number, uint8_t value)
|
||||
static int drv_pin_write(struct mr_pin *pin, int number, uint8_t value)
|
||||
{
|
||||
struct drv_pin_port_data *pin_port_data = drv_pin_get_port_data(number);
|
||||
struct drv_pin_data *pin_data = drv_pin_get_data(number);
|
||||
@@ -238,10 +239,11 @@ static void drv_pin_write(struct mr_pin *pin, int number, uint8_t value)
|
||||
/* Check pin is valid */
|
||||
if (pin_port_data == NULL || pin_data == NULL)
|
||||
{
|
||||
return;
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_PIN_CHECK */
|
||||
HAL_GPIO_WritePin(pin_port_data->port, pin_data->pin, (GPIO_PinState)value);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
void EXTI0_IRQHandler(void)
|
||||
|
||||
@@ -297,20 +297,36 @@ static void drv_pwm_start(struct mr_pwm *pwm, uint32_t prescaler, uint32_t perio
|
||||
__HAL_TIM_ENABLE(&pwm_data->handle);
|
||||
}
|
||||
|
||||
static void drv_pwm_write(struct mr_pwm *pwm, int channel, uint32_t compare_value)
|
||||
static int drv_pwm_read(struct mr_pwm *pwm, int channel, uint32_t *compare_value)
|
||||
{
|
||||
struct drv_pwm_data *pwm_data = (struct drv_pwm_data *)pwm->dev.drv->data;
|
||||
uint32_t Channel = (channel - 1) << 2;
|
||||
|
||||
__HAL_TIM_SET_COMPARE(&pwm_data->handle, Channel, compare_value);
|
||||
#ifdef MR_USING_PWM_CHANNEL_CHECK
|
||||
if ((Channel & TIM_CHANNEL_ALL) || (Channel == 0))
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_PWM_CHANNEL_CHECK */
|
||||
|
||||
*compare_value = __HAL_TIM_GET_COMPARE(&pwm_data->handle, Channel);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static uint32_t drv_pwm_read(struct mr_pwm *pwm, int channel)
|
||||
static int drv_pwm_write(struct mr_pwm *pwm, int channel, uint32_t compare_value)
|
||||
{
|
||||
struct drv_pwm_data *pwm_data = (struct drv_pwm_data *)pwm->dev.drv->data;
|
||||
uint32_t Channel = (channel - 1) << 2;
|
||||
|
||||
return __HAL_TIM_GET_COMPARE(&pwm_data->handle, Channel);
|
||||
#ifdef MR_USING_PWM_CHANNEL_CHECK
|
||||
if ((Channel & TIM_CHANNEL_ALL) || (Channel == 0))
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_PWM_CHANNEL_CHECK */
|
||||
|
||||
__HAL_TIM_SET_COMPARE(&pwm_data->handle, Channel, compare_value);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static struct mr_pwm_ops pwm_drv_ops =
|
||||
@@ -318,8 +334,8 @@ static struct mr_pwm_ops pwm_drv_ops =
|
||||
drv_pwm_configure,
|
||||
drv_pwm_channel_configure,
|
||||
drv_pwm_start,
|
||||
drv_pwm_write,
|
||||
drv_pwm_read
|
||||
drv_pwm_read,
|
||||
drv_pwm_write
|
||||
};
|
||||
|
||||
static struct mr_drv pwm_drv[] =
|
||||
|
||||
@@ -215,7 +215,7 @@ static int drv_serial_configure(struct mr_serial *serial, struct mr_serial_confi
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static uint8_t drv_serial_read(struct mr_serial *serial)
|
||||
static int drv_serial_read(struct mr_serial *serial, uint8_t *data)
|
||||
{
|
||||
struct drv_serial_data *serial_data = (struct drv_serial_data *)serial->dev.drv->data;
|
||||
size_t i = 0;
|
||||
@@ -226,20 +226,21 @@ static uint8_t drv_serial_read(struct mr_serial *serial)
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return 0;
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
#if defined(STM32L4) || defined(STM32WL) || defined(STM32F7) || defined(STM32F0) \
|
||||
|| defined(STM32L0) || defined(STM32G0) || defined(STM32H7) || defined(STM32L5) \
|
||||
|| defined(STM32G4) || defined(STM32MP1) || defined(STM32WB) || defined(STM32F3)\
|
||||
|| defined(STM32U5) || defined(STM32H5)
|
||||
return (uint8_t)serial_data->handle.Instance->RDR & 0xff;
|
||||
*data = (uint8_t)serial_data->handle.Instance->RDR & 0xff;
|
||||
#else
|
||||
return (uint8_t)serial_data->handle.Instance->DR & 0xff;
|
||||
*data = (uint8_t)serial_data->handle.Instance->DR & 0xff;
|
||||
#endif
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_serial_write(struct mr_serial *serial, uint8_t data)
|
||||
static int drv_serial_write(struct mr_serial *serial, uint8_t data)
|
||||
{
|
||||
struct drv_serial_data *serial_data = (struct drv_serial_data *)serial->dev.drv->data;
|
||||
size_t i = 0;
|
||||
@@ -258,9 +259,10 @@ static void drv_serial_write(struct mr_serial *serial, uint8_t data)
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return;
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_serial_start_tx(struct mr_serial *serial)
|
||||
|
||||
@@ -182,24 +182,6 @@ static int drv_spi_bus_configure(struct mr_spi_bus *spi_bus, struct mr_spi_confi
|
||||
}
|
||||
}
|
||||
|
||||
switch (config->data_bits)
|
||||
{
|
||||
case MR_SPI_DATA_BITS_8:
|
||||
{
|
||||
spi_bus_data->handle.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
break;
|
||||
}
|
||||
case MR_SPI_DATA_BITS_16:
|
||||
{
|
||||
spi_bus_data->handle.Init.DataSize = SPI_DATASIZE_16BIT;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
switch (config->bit_order)
|
||||
{
|
||||
case MR_SPI_BIT_ORDER_LSB:
|
||||
@@ -219,6 +201,7 @@ static int drv_spi_bus_configure(struct mr_spi_bus *spi_bus, struct mr_spi_confi
|
||||
}
|
||||
|
||||
/* Configure SPI */
|
||||
spi_bus_data->handle.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
spi_bus_data->handle.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
spi_bus_data->handle.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
spi_bus_data->handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
@@ -250,7 +233,7 @@ static int drv_spi_bus_configure(struct mr_spi_bus *spi_bus, struct mr_spi_confi
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static uint32_t drv_spi_bus_read(struct mr_spi_bus *spi_bus)
|
||||
static int drv_spi_bus_read(struct mr_spi_bus *spi_bus, uint8_t *data)
|
||||
{
|
||||
struct drv_spi_bus_data *spi_bus_data = (struct drv_spi_bus_data *)spi_bus->dev.drv->data;
|
||||
size_t i = 0;
|
||||
@@ -260,13 +243,14 @@ static uint32_t drv_spi_bus_read(struct mr_spi_bus *spi_bus)
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return 0;
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
return (uint32_t)spi_bus_data->handle.Instance->DR;
|
||||
*data = (uint8_t)(spi_bus_data->handle.Instance->DR & 0xff);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_spi_bus_write(struct mr_spi_bus *spi_bus, uint32_t data)
|
||||
static int drv_spi_bus_write(struct mr_spi_bus *spi_bus, uint8_t data)
|
||||
{
|
||||
struct drv_spi_bus_data *spi_bus_data = (struct drv_spi_bus_data *)spi_bus->dev.drv->data;
|
||||
size_t i = 0;
|
||||
@@ -277,7 +261,7 @@ static void drv_spi_bus_write(struct mr_spi_bus *spi_bus, uint32_t data)
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return;
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,17 +115,19 @@ static int drv_adc_channel_configure(struct mr_adc *adc, int channel, int state)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static uint32_t drv_adc_read(struct mr_adc *adc, int channel)
|
||||
static int drv_adc_read(struct mr_adc *adc, int channel, uint32_t *data)
|
||||
{
|
||||
struct drv_adc_data *adc_data = (struct drv_adc_data *)adc->dev.drv->data;
|
||||
struct drv_adc_channel_data *adc_channel_data = drv_adc_get_channel_data(channel);
|
||||
size_t i = 0;
|
||||
|
||||
#ifdef MR_USING_ADC_CHANNEL_CHECK
|
||||
/* Check channel is valid */
|
||||
if (adc_channel_data == NULL)
|
||||
{
|
||||
return 0;
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_ADC_CHANNEL_CHECK */
|
||||
|
||||
/* Read data */
|
||||
#ifdef MR_USING_CH32V00X
|
||||
@@ -139,11 +141,12 @@ static uint32_t drv_adc_read(struct mr_adc *adc, int channel)
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return 0;
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
ADC_ClearFlag(adc_data->instance, ADC_FLAG_EOC);
|
||||
return ADC_GetConversionValue(adc_data->instance);
|
||||
*data = ADC_GetConversionValue(adc_data->instance);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static struct mr_adc_ops adc_drv_ops =
|
||||
|
||||
@@ -101,15 +101,17 @@ static int drv_dac_channel_configure(struct mr_dac *dac, int channel, int state)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_dac_write(struct mr_dac *dac, int channel, uint32_t data)
|
||||
static int drv_dac_write(struct mr_dac *dac, int channel, uint32_t data)
|
||||
{
|
||||
struct drv_dac_channel_data *dac_channel_data = drv_dac_get_channel_data(channel);
|
||||
|
||||
#ifdef MR_USING_DAC_CHANNEL_CHECK
|
||||
/* Check channel is valid */
|
||||
if (dac_channel_data == NULL)
|
||||
{
|
||||
return;
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_DAC_CHANNEL_CHECK */
|
||||
|
||||
/* Write data */
|
||||
switch (dac_channel_data->channel)
|
||||
@@ -130,7 +132,7 @@ static void drv_dac_write(struct mr_dac *dac, int channel, uint32_t data)
|
||||
#endif /* DAC_Channel_2 */
|
||||
default:
|
||||
{
|
||||
return;
|
||||
return MR_EINVAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,34 +149,47 @@ static void drv_i2c_bus_start(struct mr_i2c_bus *i2c_bus)
|
||||
}
|
||||
}
|
||||
|
||||
static void drv_i2c_bus_send_addr(struct mr_i2c_bus *i2c_bus, int addr, int addr_bits)
|
||||
static int drv_i2c_bus_send_addr(struct mr_i2c_bus *i2c_bus, int addr, int addr_bits)
|
||||
{
|
||||
struct drv_i2c_bus_data *i2c_bus_data = (struct drv_i2c_bus_data *)i2c_bus->dev.drv->data;
|
||||
size_t i = 0;
|
||||
|
||||
I2C_SendData(i2c_bus_data->instance, addr);
|
||||
/* Check start condition */
|
||||
while (I2C_CheckEvent(i2c_bus_data->instance, I2C_EVENT_MASTER_MODE_SELECT) == RESET)
|
||||
{
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
/* Send address with 10-bit */
|
||||
if (addr_bits == MR_I2C_ADDR_BITS_10)
|
||||
{
|
||||
i = 0;
|
||||
I2C_SendData(i2c_bus_data->instance, addr >> 8);
|
||||
while (I2C_CheckEvent(i2c_bus_data->instance, I2C_EVENT_MASTER_MODE_ADDRESS10) == RESET)
|
||||
{
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Send address */
|
||||
i = 0;
|
||||
while (I2C_CheckEvent(i2c_bus_data->instance, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == RESET)
|
||||
{
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (addr_bits == MR_I2C_ADDR_BITS_10)
|
||||
{
|
||||
I2C_SendData(i2c_bus_data->instance, (addr >> 8));
|
||||
i = 0;
|
||||
while (I2C_CheckEvent(i2c_bus_data->instance, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == RESET)
|
||||
{
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return;
|
||||
}
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_i2c_bus_stop(struct mr_i2c_bus *i2c_bus)
|
||||
@@ -186,7 +199,7 @@ static void drv_i2c_bus_stop(struct mr_i2c_bus *i2c_bus)
|
||||
I2C_GenerateSTOP(i2c_bus_data->instance, ENABLE);
|
||||
}
|
||||
|
||||
static uint8_t drv_i2c_bus_read(struct mr_i2c_bus *i2c_bus, int ack_state)
|
||||
static int drv_i2c_bus_read(struct mr_i2c_bus *i2c_bus, uint8_t *data, int ack_state)
|
||||
{
|
||||
struct drv_i2c_bus_data *i2c_bus_data = (struct drv_i2c_bus_data *)i2c_bus->dev.drv->data;
|
||||
size_t i = 0;
|
||||
@@ -200,13 +213,14 @@ static uint8_t drv_i2c_bus_read(struct mr_i2c_bus *i2c_bus, int ack_state)
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return 0;
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
return (uint8_t)I2C_ReceiveData(i2c_bus_data->instance);
|
||||
*data = (uint8_t)I2C_ReceiveData(i2c_bus_data->instance);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_i2c_bus_write(struct mr_i2c_bus *i2c_bus, uint8_t data)
|
||||
static int drv_i2c_bus_write(struct mr_i2c_bus *i2c_bus, uint8_t data)
|
||||
{
|
||||
struct drv_i2c_bus_data *i2c_bus_data = (struct drv_i2c_bus_data *)i2c_bus->dev.drv->data;
|
||||
size_t i = 0;
|
||||
@@ -218,9 +232,10 @@ static void drv_i2c_bus_write(struct mr_i2c_bus *i2c_bus, uint8_t data)
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return;
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_i2c_bus_isr(struct mr_i2c_bus *i2c_bus)
|
||||
|
||||
@@ -251,7 +251,7 @@ static int drv_pin_configure(struct mr_pin *pin, int number, int mode)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static uint8_t drv_pin_read(struct mr_pin *pin, int number)
|
||||
static int drv_pin_read(struct mr_pin *pin, int number, uint8_t *value)
|
||||
{
|
||||
struct drv_pin_port_data *pin_port_data = drv_pin_get_port_data(number);
|
||||
struct drv_pin_data *pin_data = drv_pin_get_data(number);
|
||||
@@ -260,13 +260,14 @@ static uint8_t drv_pin_read(struct mr_pin *pin, int number)
|
||||
/* Check pin is valid */
|
||||
if (pin_port_data == NULL || pin_data == NULL)
|
||||
{
|
||||
return 0;
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_PIN_CHECK */
|
||||
return (int)GPIO_ReadInputDataBit(pin_port_data->port, pin_data->pin);
|
||||
*value = GPIO_ReadInputDataBit(pin_port_data->port, pin_data->pin);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_pin_write(struct mr_pin *pin, int number, uint8_t value)
|
||||
static int drv_pin_write(struct mr_pin *pin, int number, uint8_t value)
|
||||
{
|
||||
struct drv_pin_port_data *pin_port_data = drv_pin_get_port_data(number);
|
||||
struct drv_pin_data *pin_data = drv_pin_get_data(number);
|
||||
@@ -275,10 +276,11 @@ static void drv_pin_write(struct mr_pin *pin, int number, uint8_t value)
|
||||
/* Check pin is valid */
|
||||
if (pin_port_data == NULL || pin_data == NULL)
|
||||
{
|
||||
return;
|
||||
return MR_EINVAL;
|
||||
}
|
||||
#endif /* MR_USING_PIN_CHECK */
|
||||
GPIO_WriteBit(pin_port_data->port, pin_data->pin, value);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
#ifdef MR_USING_CH32V00X
|
||||
|
||||
@@ -200,7 +200,7 @@ static int drv_pwm_channel_configure(struct mr_pwm *pwm, int channel, int state,
|
||||
GPIO_Init(pwm_data->ch1_port, &GPIO_InitStructure);
|
||||
TIM_OC1Init(pwm_data->instance, &TIM_OCInitStructure);
|
||||
TIM_OC1PreloadConfig(pwm_data->instance, OCPreload);
|
||||
return MR_EOK;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
@@ -208,7 +208,7 @@ static int drv_pwm_channel_configure(struct mr_pwm *pwm, int channel, int state,
|
||||
GPIO_Init(pwm_data->ch2_port, &GPIO_InitStructure);
|
||||
TIM_OC2Init(pwm_data->instance, &TIM_OCInitStructure);
|
||||
TIM_OC2PreloadConfig(pwm_data->instance, OCPreload);
|
||||
return MR_EOK;
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
@@ -216,7 +216,7 @@ static int drv_pwm_channel_configure(struct mr_pwm *pwm, int channel, int state,
|
||||
GPIO_Init(pwm_data->ch3_port, &GPIO_InitStructure);
|
||||
TIM_OC3Init(pwm_data->instance, &TIM_OCInitStructure);
|
||||
TIM_OC3PreloadConfig(pwm_data->instance, OCPreload);
|
||||
return MR_EOK;
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
@@ -224,13 +224,14 @@ static int drv_pwm_channel_configure(struct mr_pwm *pwm, int channel, int state,
|
||||
GPIO_Init(pwm_data->ch4_port, &GPIO_InitStructure);
|
||||
TIM_OC4Init(pwm_data->instance, &TIM_OCInitStructure);
|
||||
TIM_OC4PreloadConfig(pwm_data->instance, OCPreload);
|
||||
return MR_EOK;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_pwm_start(struct mr_pwm *pwm, uint32_t prescaler, uint32_t period)
|
||||
@@ -245,7 +246,7 @@ static void drv_pwm_start(struct mr_pwm *pwm, uint32_t prescaler, uint32_t perio
|
||||
TIM_Cmd(pwm_data->instance, ENABLE);
|
||||
}
|
||||
|
||||
static void drv_pwm_write(struct mr_pwm *pwm, int channel, uint32_t compare_value)
|
||||
static int drv_pwm_write(struct mr_pwm *pwm, int channel, uint32_t compare_value)
|
||||
{
|
||||
struct drv_pwm_data *pwm_data = (struct drv_pwm_data *)pwm->dev.drv->data;
|
||||
|
||||
@@ -254,31 +255,32 @@ static void drv_pwm_write(struct mr_pwm *pwm, int channel, uint32_t compare_valu
|
||||
case 1:
|
||||
{
|
||||
TIM_SetCompare1(pwm_data->instance, compare_value);
|
||||
return;
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
TIM_SetCompare2(pwm_data->instance, compare_value);
|
||||
return;
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
TIM_SetCompare3(pwm_data->instance, compare_value);
|
||||
return;
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
TIM_SetCompare4(pwm_data->instance, compare_value);
|
||||
return;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
return;
|
||||
return MR_EINVAL;
|
||||
}
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static uint32_t drv_pwm_read(struct mr_pwm *pwm, int channel)
|
||||
static int drv_pwm_read(struct mr_pwm *pwm, int channel, uint32_t *compare_value)
|
||||
{
|
||||
struct drv_pwm_data *pwm_data = (struct drv_pwm_data *)pwm->dev.drv->data;
|
||||
|
||||
@@ -286,25 +288,30 @@ static uint32_t drv_pwm_read(struct mr_pwm *pwm, int channel)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
return TIM_GetCapture1(pwm_data->instance);
|
||||
*compare_value = TIM_GetCapture1(pwm_data->instance);
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
return TIM_GetCapture2(pwm_data->instance);
|
||||
*compare_value = TIM_GetCapture2(pwm_data->instance);
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
return TIM_GetCapture3(pwm_data->instance);
|
||||
*compare_value = TIM_GetCapture3(pwm_data->instance);
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
return TIM_GetCapture4(pwm_data->instance);
|
||||
*compare_value = TIM_GetCapture4(pwm_data->instance);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
return 0;
|
||||
return MR_EINVAL;
|
||||
}
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static struct mr_pwm_ops pwm_drv_ops =
|
||||
@@ -312,8 +319,8 @@ static struct mr_pwm_ops pwm_drv_ops =
|
||||
drv_pwm_configure,
|
||||
drv_pwm_channel_configure,
|
||||
drv_pwm_start,
|
||||
drv_pwm_write,
|
||||
drv_pwm_read
|
||||
drv_pwm_read,
|
||||
drv_pwm_write
|
||||
};
|
||||
|
||||
static struct mr_drv pwm_drv[] =
|
||||
|
||||
@@ -257,7 +257,7 @@ static int drv_serial_configure(struct mr_serial *serial, struct mr_serial_confi
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static uint8_t drv_serial_read(struct mr_serial *serial)
|
||||
static int drv_serial_read(struct mr_serial *serial, uint8_t *data)
|
||||
{
|
||||
struct drv_serial_data *serial_data = (struct drv_serial_data *)serial->dev.drv->data;
|
||||
size_t i = 0;
|
||||
@@ -268,13 +268,14 @@ static uint8_t drv_serial_read(struct mr_serial *serial)
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return 0;
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
return (uint8_t)USART_ReceiveData(serial_data->instance);
|
||||
*data = (uint8_t)USART_ReceiveData(serial_data->instance);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_serial_write(struct mr_serial *serial, uint8_t data)
|
||||
static int drv_serial_write(struct mr_serial *serial, uint8_t data)
|
||||
{
|
||||
struct drv_serial_data *serial_data = (struct drv_serial_data *)serial->dev.drv->data;
|
||||
size_t i = 0;
|
||||
@@ -286,9 +287,10 @@ static void drv_serial_write(struct mr_serial *serial, uint8_t data)
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return;
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_serial_start_tx(struct mr_serial *serial)
|
||||
|
||||
@@ -198,24 +198,6 @@ static int drv_spi_bus_configure(struct mr_spi_bus *spi_bus, struct mr_spi_confi
|
||||
}
|
||||
}
|
||||
|
||||
switch (config->data_bits)
|
||||
{
|
||||
case MR_SPI_DATA_BITS_8:
|
||||
{
|
||||
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
||||
break;
|
||||
}
|
||||
case MR_SPI_DATA_BITS_16:
|
||||
{
|
||||
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
return MR_EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
switch (config->bit_order)
|
||||
{
|
||||
case MR_SPI_BIT_ORDER_LSB:
|
||||
@@ -256,6 +238,7 @@ static int drv_spi_bus_configure(struct mr_spi_bus *spi_bus, struct mr_spi_confi
|
||||
}
|
||||
|
||||
/* Configure SPI */
|
||||
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
||||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
||||
SPI_Init(spi_bus_data->instance, &SPI_InitStructure);
|
||||
@@ -280,7 +263,7 @@ static int drv_spi_bus_configure(struct mr_spi_bus *spi_bus, struct mr_spi_confi
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static uint32_t drv_spi_bus_read(struct mr_spi_bus *spi_bus)
|
||||
static int drv_spi_bus_read(struct mr_spi_bus *spi_bus, uint8_t *data)
|
||||
{
|
||||
struct drv_spi_bus_data *spi_bus_data = (struct drv_spi_bus_data *)spi_bus->dev.drv->data;
|
||||
size_t i = 0;
|
||||
@@ -290,13 +273,14 @@ static uint32_t drv_spi_bus_read(struct mr_spi_bus *spi_bus)
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return 0;
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
return SPI_I2S_ReceiveData(spi_bus_data->instance);
|
||||
*data = (uint8_t)(SPI_I2S_ReceiveData(spi_bus_data->instance) & 0xFF);
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void drv_spi_bus_write(struct mr_spi_bus *spi_bus, uint32_t data)
|
||||
static int drv_spi_bus_write(struct mr_spi_bus *spi_bus, uint8_t data)
|
||||
{
|
||||
struct drv_spi_bus_data *spi_bus_data = (struct drv_spi_bus_data *)spi_bus->dev.drv->data;
|
||||
size_t i = 0;
|
||||
@@ -307,7 +291,7 @@ static void drv_spi_bus_write(struct mr_spi_bus *spi_bus, uint32_t data)
|
||||
i++;
|
||||
if (i > UINT16_MAX)
|
||||
{
|
||||
return;
|
||||
return MR_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user