1.更新驱动,适配设备新接口。

This commit is contained in:
MacRsh
2024-02-02 01:47:16 +08:00
parent 7930b355aa
commit 59276f5e05
13 changed files with 156 additions and 126 deletions

View File

@@ -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 MR_EOK;
}
HAL_ADC_Stop(&adc_data->handle);
return HAL_ADC_GetValue(&adc_data->handle);
return MR_ETIMEOUT;
}
static struct mr_adc_ops adc_drv_ops =

View File

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

View File

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

View File

@@ -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[] =

View File

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

View File

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