kosmos/source/firmware/kernel/driver/driver.c

172 lines
3.6 KiB
C
Raw Normal View History

2016-07-28 19:02:54 +00:00
/*
* driver.c
*
* Created on: Jul 27, 2016
* Author: tkl
*/
#include <stddef.h>
#include "driver.h"
#include "adc.h"
#include "gpio.h"
#include "i2c.h"
2016-08-09 10:52:38 +00:00
#include "pwm.h"
2016-07-28 19:02:54 +00:00
#include "rtc.h"
#include "spi.h"
#include "uart.h"
int open(const struct driver *driver)
{
int ret = -1;
if(NULL == driver)
return ret;
switch(driver->driver_type) {
case DRIVER_TYPE_ADC:
ret = adc_open((const struct adc *)(driver->device_driver));
break;
case DRIVER_TYPE_GPIO:
ret = gpio_open((const struct gpio *)(driver->device_driver));
break;
case DRIVER_TYPE_I2C:
ret = i2c_open((struct i2c *)(driver->device_driver));
break;
2016-08-09 10:52:38 +00:00
case DRIVER_TYPE_PWM:
ret = pwm_open((const struct pwm *)(driver->device_driver));
break;
2016-07-28 19:02:54 +00:00
case DRIVER_TYPE_RTC:
ret = rtc_open((const struct rtc *)(driver->device_driver));
break;
case DRIVER_TYPE_SPI:
ret = spi_open((const struct spi *)(driver->device_driver));
break;
case DRIVER_TYPE_UART:
ret = uart_open((const struct uart *)(driver->device_driver));
break;
}
return ret;
}
int close(const struct driver *driver)
{
int ret = -1;
if(NULL == driver)
return ret;
switch(driver->driver_type) {
case DRIVER_TYPE_ADC:
ret = adc_close((const struct adc *)(driver->device_driver));
break;
case DRIVER_TYPE_GPIO:
ret = gpio_close((const struct gpio *)(driver->device_driver));
break;
case DRIVER_TYPE_I2C:
ret = i2c_close((struct i2c *)(driver->device_driver));
break;
2016-08-09 10:52:38 +00:00
case DRIVER_TYPE_PWM:
ret = pwm_close((const struct pwm *)(driver->device_driver));
break;
2016-07-28 19:02:54 +00:00
case DRIVER_TYPE_RTC:
ret = rtc_close((const struct rtc *)(driver->device_driver));
break;
case DRIVER_TYPE_SPI:
ret = spi_close((const struct spi *)(driver->device_driver));
break;
case DRIVER_TYPE_UART:
ret = uart_close((const struct uart *)(driver->device_driver));
break;
}
return ret;
}
int read(const struct driver *driver, char *buffer, int len)
{
int ret = -1;
if(NULL == driver)
return ret;
switch(driver->driver_type) {
case DRIVER_TYPE_ADC:
break;
case DRIVER_TYPE_GPIO:
ret = gpio_read((const struct gpio *)(driver->device_driver));
if(len > 0) {
buffer[0] = ret + 0x30;
ret = 1;
}
break;
case DRIVER_TYPE_I2C:
break;
2016-08-09 10:52:38 +00:00
case DRIVER_TYPE_PWM:
ret = -1;
break;
2016-07-28 19:02:54 +00:00
case DRIVER_TYPE_RTC:
break;
case DRIVER_TYPE_SPI:
break;
case DRIVER_TYPE_UART:
ret = uart_read((const struct uart *)(driver->device_driver), buffer, len);
break;
}
return ret;
}
int write(const struct driver *driver, const char *buffer, int len)
{
int ret = -1;
if(NULL == driver)
return ret;
switch(driver->driver_type) {
case DRIVER_TYPE_ADC:
break;
case DRIVER_TYPE_GPIO:
if(len > 0) {
char send = 0;
if((buffer[0] - 0x30) > 0)
send = 1;
gpio_write((const struct gpio *)(driver->device_driver), send);
ret = 1;
}
break;
case DRIVER_TYPE_I2C:
break;
2016-08-09 10:52:38 +00:00
case DRIVER_TYPE_PWM:
ret = -1;
break;
2016-07-28 19:02:54 +00:00
case DRIVER_TYPE_RTC:
break;
case DRIVER_TYPE_SPI:
break;
case DRIVER_TYPE_UART:
ret = uart_write((const struct uart *)(driver->device_driver), buffer, len);
break;
}
return ret;
}
2016-08-09 10:52:38 +00:00
int ioctl(const struct driver *driver, unsigned int cmd, const void *data)
{
int ret = -1;
if(NULL == driver)
return ret;
switch(driver->driver_type) {
case DRIVER_TYPE_ADC:
break;
case DRIVER_TYPE_GPIO:
break;
case DRIVER_TYPE_I2C:
break;
case DRIVER_TYPE_PWM:
if(cmd == IOCTL_PWM_SET_DUTY_CYCLE) {
unsigned int *duty = (unsigned int *)data;
pwm_set_duty_cycle((const struct pwm *)(driver->device_driver), *duty);
}
break;
case DRIVER_TYPE_RTC:
break;
case DRIVER_TYPE_SPI:
break;
case DRIVER_TYPE_UART:
break;
}
return ret;
}