From cdb315da323c5fc3076394f320935fe24502f6d7 Mon Sep 17 00:00:00 2001 From: tkl Date: Wed, 27 Jul 2016 16:00:08 +0200 Subject: [PATCH 1/2] universal driver access module --- source/application/test/main.c | 15 +- .../include/bsp_stm32f4-discovery.h | 52 ++++--- .../stm32f4xx/driver/include/stm32f4_uart.h | 1 + source/firmware/kernel/driver/driver.c | 130 ++++++++++++++++++ .../firmware/kernel/driver/include/driver.h | 30 ++++ source/firmware/kernel/driver/uart.c | 1 + 6 files changed, 200 insertions(+), 29 deletions(-) create mode 100644 source/firmware/kernel/driver/driver.c create mode 100644 source/firmware/kernel/driver/include/driver.h diff --git a/source/application/test/main.c b/source/application/test/main.c index bd86951..f98855d 100644 --- a/source/application/test/main.c +++ b/source/application/test/main.c @@ -13,18 +13,19 @@ #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); + open(&led_4); + write(&led_4, 0, 1); while(1) { sleep_ms(1000); - gpio_toggle(&led_1); - uart_write(&uart_1, "Hello world\r\n", 13); + write(&uart_1, "Driver test\r\n", 13); } } @@ -32,9 +33,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 +46,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); diff --git a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/bsp_stm32f4-discovery.h b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/bsp_stm32f4-discovery.h index ce85bb1..df483d8 100755 --- a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/bsp_stm32f4-discovery.h +++ b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/bsp_stm32f4-discovery.h @@ -11,6 +11,7 @@ #include #include +#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, diff --git a/source/firmware/arch/stm32f4xx/driver/include/stm32f4_uart.h b/source/firmware/arch/stm32f4xx/driver/include/stm32f4_uart.h index 3dd611d..ac3aa1d 100644 --- a/source/firmware/arch/stm32f4xx/driver/include/stm32f4_uart.h +++ b/source/firmware/arch/stm32f4xx/driver/include/stm32f4_uart.h @@ -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. diff --git a/source/firmware/kernel/driver/driver.c b/source/firmware/kernel/driver/driver.c new file mode 100644 index 0000000..2203945 --- /dev/null +++ b/source/firmware/kernel/driver/driver.c @@ -0,0 +1,130 @@ +/* + * driver.c + * + * Created on: Jul 27, 2016 + * Author: tkl + */ + +#include + +#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]) + 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; +} diff --git a/source/firmware/kernel/driver/include/driver.h b/source/firmware/kernel/driver/include/driver.h new file mode 100644 index 0000000..ee82abf --- /dev/null +++ b/source/firmware/kernel/driver/include/driver.h @@ -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_ */ diff --git a/source/firmware/kernel/driver/uart.c b/source/firmware/kernel/driver/uart.c index 70266cd..196be50 100755 --- a/source/firmware/kernel/driver/uart.c +++ b/source/firmware/kernel/driver/uart.c @@ -6,6 +6,7 @@ #include #include +#include "driver.h" #include "ringbuffer.h" #include "irq.h" #include "stack.h" From f4bc9031720609c4f616d5e0091c864b8cc5e514 Mon Sep 17 00:00:00 2001 From: tkl Date: Wed, 27 Jul 2016 16:28:46 +0200 Subject: [PATCH 2/2] led driver --- source/application/test/main.c | 9 ++++++++- source/firmware/kernel/driver/driver.c | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/source/application/test/main.c b/source/application/test/main.c index f98855d..1079e84 100644 --- a/source/application/test/main.c +++ b/source/application/test/main.c @@ -21,10 +21,17 @@ struct thread_context tc_1; void task1(void *arg) { + char rd = '0'; open(&led_4); - write(&led_4, 0, 1); + write(&led_4, &rd, 1); while(1) { sleep_ms(1000); + 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); } } diff --git a/source/firmware/kernel/driver/driver.c b/source/firmware/kernel/driver/driver.c index 2203945..06b4747 100644 --- a/source/firmware/kernel/driver/driver.c +++ b/source/firmware/kernel/driver/driver.c @@ -110,7 +110,7 @@ int write(const struct driver *driver, const char *buffer, int len) case DRIVER_TYPE_GPIO: if(len > 0) { char send = 0; - if(buffer[0]) + if((buffer[0] - 0x30) > 0) send = 1; gpio_write((const struct gpio *)(driver->device_driver), send); ret = 1;