Merge branch 'feature/unified_driver_access' into 'master'
Feature/unified driver access See merge request !6
This commit is contained in:
commit
cf7455020f
@ -13,18 +13,26 @@
|
||||
#include "isr.h"
|
||||
#include "sys_tick.h"
|
||||
|
||||
#include "driver.h"
|
||||
|
||||
#define STACK_SIZE 256
|
||||
stack_t tc_1_stack[STACK_SIZE];
|
||||
struct thread_context tc_1;
|
||||
|
||||
void task1(void *arg)
|
||||
{
|
||||
gpio_open(&led_1);
|
||||
gpio_write(&led_1, 0);
|
||||
char rd = '0';
|
||||
open(&led_4);
|
||||
write(&led_4, &rd, 1);
|
||||
while(1) {
|
||||
sleep_ms(1000);
|
||||
gpio_toggle(&led_1);
|
||||
uart_write(&uart_1, "Hello world\r\n", 13);
|
||||
read(&led_4, &rd, 1);
|
||||
if(rd == '0')
|
||||
rd = '1';
|
||||
else
|
||||
rd = '0';
|
||||
write(&led_4, &rd, 1);
|
||||
write(&uart_1, "Driver test\r\n", 13);
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,9 +40,9 @@ char rx_buf[80];
|
||||
void task2(void *arg)
|
||||
{
|
||||
while(1) {
|
||||
int i = uart_read(&uart_1, rx_buf, 80);
|
||||
int i = read(&uart_1, rx_buf, 80);
|
||||
if(i > 0) {
|
||||
uart_write(&uart_1, rx_buf, i);
|
||||
write(&uart_1, rx_buf, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -45,7 +53,7 @@ int main(void)
|
||||
{
|
||||
board_init();
|
||||
sys_tick_init(&timer_1);
|
||||
uart_open(&uart_1);
|
||||
open(&uart_1);
|
||||
thread_create(&tc_1, tc_1_stack, STACK_SIZE, task1, NULL, THREAD_PRIO_LOW);
|
||||
thread_create(&tc_2, tc_2_stack, STACK_SIZE, task2, NULL, THREAD_PRIO_LOW);
|
||||
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "driver.h"
|
||||
#include "gpio.h"
|
||||
#include "uart.h"
|
||||
#include "ringbuffer.h"
|
||||
@ -19,10 +20,6 @@
|
||||
#include "stm32f4_uart.h"
|
||||
#include "stm32_sys_tick.h"
|
||||
|
||||
#if 0
|
||||
#include "usb_vport.h"
|
||||
#endif
|
||||
|
||||
// SYSTEM TICK
|
||||
static const enum stm32_sys_tick_time_base stm23_sys_tick_time_base =
|
||||
STM32_SYS_TICK_TIME_BASE_MS;
|
||||
@ -37,16 +34,6 @@ static const struct loki_timer timer_1 = {
|
||||
&timer_fp
|
||||
};
|
||||
|
||||
#if 0
|
||||
// USB
|
||||
static const stm32_usb_vport_t stm32_usb_vport;
|
||||
static const struct uart uart_0 = {
|
||||
&stm32_usb_vport,
|
||||
&usb_vport_fp,
|
||||
&console_buffer
|
||||
};
|
||||
#endif
|
||||
|
||||
static char console_linear_buffer[80];
|
||||
static struct ringbuffer console_buffer = {
|
||||
console_linear_buffer,
|
||||
@ -91,15 +78,16 @@ static const struct stm32f4_uart stm32f4_uart1 = {
|
||||
|
||||
};
|
||||
|
||||
static const struct uart uart_1 = {
|
||||
static const struct uart __uart_1 = {
|
||||
&stm32f4_uart1,
|
||||
&stm32f4_uart_fp,
|
||||
&console_buffer,
|
||||
};
|
||||
|
||||
// STATUS LED
|
||||
//! \brief Status Led is forwarded to led 4.
|
||||
#define led_1 led_4
|
||||
static const struct driver uart_1 = {
|
||||
DRIVER_TYPE_UART,
|
||||
&__uart_1,
|
||||
};
|
||||
|
||||
// LED 3
|
||||
static const GPIO_InitTypeDef stm32_f4_discovery_led_3_gpio = {
|
||||
@ -119,11 +107,16 @@ static const struct stm32f4_gpio stm32_f4_discovery_led_3 = {
|
||||
NULL
|
||||
};
|
||||
|
||||
static const struct gpio led_3 = {
|
||||
static const struct gpio __led_3 = {
|
||||
(void*)&stm32_f4_discovery_led_3,
|
||||
&gpio_fp
|
||||
};
|
||||
|
||||
static const struct driver led_3 = {
|
||||
DRIVER_TYPE_GPIO,
|
||||
&__led_3,
|
||||
};
|
||||
|
||||
// LED 4
|
||||
static const GPIO_InitTypeDef stm32_f4_discovery_led_4_gpio = {
|
||||
GPIO_Pin_13,
|
||||
@ -142,11 +135,16 @@ static const struct stm32f4_gpio stm32_f4_discovery_led_4 = {
|
||||
NULL
|
||||
};
|
||||
|
||||
static const struct gpio led_4 = {
|
||||
static const struct gpio __led_4 = {
|
||||
(void*)&stm32_f4_discovery_led_4,
|
||||
&gpio_fp
|
||||
};
|
||||
|
||||
static const struct driver led_4 = {
|
||||
DRIVER_TYPE_GPIO,
|
||||
&__led_4,
|
||||
};
|
||||
|
||||
// LED 5
|
||||
static const GPIO_InitTypeDef stm32_f4_discovery_led_5_gpio = {
|
||||
GPIO_Pin_14,
|
||||
@ -165,11 +163,16 @@ static const struct stm32f4_gpio stm32_f4_discovery_led_5 = {
|
||||
NULL
|
||||
};
|
||||
|
||||
static const struct gpio led_5 = {
|
||||
static const struct gpio __led_5 = {
|
||||
(void*)&stm32_f4_discovery_led_5,
|
||||
&gpio_fp
|
||||
};
|
||||
|
||||
static const struct driver led_5 = {
|
||||
DRIVER_TYPE_GPIO,
|
||||
&__led_5,
|
||||
};
|
||||
|
||||
// LED 6
|
||||
static const GPIO_InitTypeDef stm32_f4_discovery_led_6_gpio = {
|
||||
GPIO_Pin_15,
|
||||
@ -188,11 +191,16 @@ static const struct stm32f4_gpio stm32_f4_discovery_led_6 = {
|
||||
NULL
|
||||
};
|
||||
|
||||
static const struct gpio led_6 = {
|
||||
static const struct gpio __led_6 = {
|
||||
(void*)&stm32_f4_discovery_led_6,
|
||||
&gpio_fp
|
||||
};
|
||||
|
||||
static const struct driver led_6 = {
|
||||
DRIVER_TYPE_GPIO,
|
||||
&__led_6,
|
||||
};
|
||||
|
||||
// BUTTON
|
||||
static const GPIO_InitTypeDef stm32_f4_discovery_user_button_gpio = {
|
||||
GPIO_Pin_0,
|
||||
|
@ -8,6 +8,7 @@
|
||||
#ifndef SOURCE_FIRMWARE_ARCH_STM32F4XX_DRIVER_UART_STM32_UART_H_
|
||||
#define SOURCE_FIRMWARE_ARCH_STM32F4XX_DRIVER_UART_STM32_UART_H_
|
||||
|
||||
#include "driver.h"
|
||||
#include "uart.h"
|
||||
|
||||
//! \brief Stm32f4 uart device.
|
||||
|
130
source/firmware/kernel/driver/driver.c
Normal file
130
source/firmware/kernel/driver/driver.c
Normal file
@ -0,0 +1,130 @@
|
||||
/*
|
||||
* driver.c
|
||||
*
|
||||
* Created on: Jul 27, 2016
|
||||
* Author: tkl
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "driver.h"
|
||||
#include "adc.h"
|
||||
#include "gpio.h"
|
||||
#include "i2c.h"
|
||||
#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;
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
}
|
30
source/firmware/kernel/driver/include/driver.h
Normal file
30
source/firmware/kernel/driver/include/driver.h
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* driver.h
|
||||
*
|
||||
* Created on: Jul 27, 2016
|
||||
* Author: tkl
|
||||
*/
|
||||
|
||||
#ifndef SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_
|
||||
#define SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_
|
||||
|
||||
enum driver_type {
|
||||
DRIVER_TYPE_ADC,
|
||||
DRIVER_TYPE_GPIO,
|
||||
DRIVER_TYPE_I2C,
|
||||
DRIVER_TYPE_RTC,
|
||||
DRIVER_TYPE_SPI,
|
||||
DRIVER_TYPE_UART
|
||||
};
|
||||
|
||||
struct driver {
|
||||
enum driver_type driver_type;
|
||||
const void *device_driver;
|
||||
};
|
||||
|
||||
int open(const struct driver *driver);
|
||||
int close(const struct driver *driver);
|
||||
int read(const struct driver *driver, char *buffer, int len);
|
||||
int write(const struct driver *driver, const char *buffer, int len);
|
||||
|
||||
#endif /* SOURCE_FIRMWARE_KERNEL_DRIVER_INCLUDE_DRIVER_H_ */
|
@ -6,6 +6,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "driver.h"
|
||||
#include "ringbuffer.h"
|
||||
#include "irq.h"
|
||||
#include "stack.h"
|
||||
|
Loading…
Reference in New Issue
Block a user