From e7d3eb88d4167194cd9b517b2a82267361aa3c9b Mon Sep 17 00:00:00 2001 From: Thomas Pietrzak Date: Sat, 19 Oct 2013 15:08:01 +0000 Subject: [PATCH] Adding UART git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@120 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- Makefile | 20 ++++++-- lib/Makefile | 1 + lib/spi.c | 2 +- lib/spi.h | 4 ++ lib/uart.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++ lib/uart.h | 14 ++++++ multitouchglove.c | 9 ++-- 7 files changed, 159 insertions(+), 10 deletions(-) create mode 100644 lib/uart.c create mode 100644 lib/uart.h diff --git a/Makefile b/Makefile index 92a4610..dd278f7 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,26 @@ -NODEMO = true +export NODEMO = true ROOTDIR = $(CURDIR)/uC-sdk -BOARD = inemo +export BOARD = inemo -TARGET = test.bin -TARGET_OBJS = +TARGET = multitouchglove.bin +TARGET_OBJS = lib/spi.o lib/uart.o lib/HAL_L3Gx.o lib/L3Gx/src/L3Gx.o LIBDEPS = uC-sdk/FreeRTOS/libFreeRTOS.a uC-sdk/arch/libarch.a uC-sdk/os/libos.a uC-sdk/libc/libc.a uC-sdk/libm/libm.a uC-sdk/acorn/libacorn.a LIBS = -Wl,--start-group $(LIBDEPS) -Wl,--end-group TARGET_INCLUDES = include +export MAINDIR = $(CURDIR) +export ROOTDIR = $(MAINDIR)/uC-sdk/ + +include $(MAINDIR)/lib/config.mk include $(ROOTDIR)/common.mk -all: uC-sdk $(TARGET) +all: uC-sdk lib $(TARGET) clean: clean-generic $(Q)rm -f $(TARGET) + $(Q)$(MAKE) $(MAKE_OPTS) -C uC-sdk clean + $(Q)$(MAKE) $(MAKE_OPTS) -C lib clean .PHONY: uC-sdk @@ -25,6 +31,10 @@ uC-sdk/libc/libc.a: uC-sdk uC-sdk/libm/libm.a: uC-sdk uC-sdk/acorn/libacorn.a: uC-sdk +lib: + $(E) "[MAKE] Entering lib" + $(Q)$(MAKE) $(MAKE_OPTS) -C lib + uC-sdk: $(E) "[MAKE] Entering uC-sdk" $(Q)$(MAKE) $(MAKE_OPTS) -C uC-sdk diff --git a/lib/Makefile b/lib/Makefile index 1e75597..a2c5915 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -12,6 +12,7 @@ include $(ROOTDIR)/FreeRTOS/config.mk #include ../uC-sdk/arch/config.mk TARGET_SRCS += spi.c +TARGET_SRCS += uart.c TARGET_SRCS += HAL_L3Gx.c #TARGET_SRCS += i2c.c #TARGET_SRCS = iNEMO_Compass/src/iNEMO_Compass.c diff --git a/lib/spi.c b/lib/spi.c index a7ccb5e..242de71 100644 --- a/lib/spi.c +++ b/lib/spi.c @@ -35,7 +35,7 @@ static struct spiInitDef_t spiInitDefs[3] = { { GPIO_Pin_5, GPIO_Speed_50MHz, GPIO_Mode_AF_PP }, // MOSI { GPIO_Pin_4, GPIO_Speed_50MHz, GPIO_Mode_IN_FLOATING }, // MISO { GPIO_Pin_15, GPIO_Speed_50MHz, GPIO_Mode_Out_PP }, // CS - }, {&RCC->APB1ENR, &RCC->APB2ENR}, {RCC_APB1Periph_SPI3, RCC_APB2Periph_GPIOC} }, + }, {&RCC->APB1ENR, &RCC->APB2ENR}, {RCC_APB1Periph_SPI3, RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB} }, }; void spi_init(uint8_t id) diff --git a/lib/spi.h b/lib/spi.h index 7ca8806..58ff450 100644 --- a/lib/spi.h +++ b/lib/spi.h @@ -1,3 +1,6 @@ +#ifndef __SPI__ +#define __SPI__ + #include "FreeRTOS.h" //setup spi @@ -13,3 +16,4 @@ void spi_read(uint8_t id, uint8_t *buffer, uint8_t nb); //write data to SPI void spi_write(uint8_t id, uint8_t *address, uint8_t nb); +#endif diff --git a/lib/uart.c b/lib/uart.c new file mode 100644 index 0000000..a97b51d --- /dev/null +++ b/lib/uart.c @@ -0,0 +1,119 @@ +#include "spi.h" +#include "task.h" +#include +#include +#include +#include + +#include + +struct uartInitDef_t { + // tx / rx + USART_TypeDef * id; + GPIO_TypeDef * tdef[2]; + GPIO_InitTypeDef gpiodef[2]; + // uart / gpio + volatile uint32_t * bridge[2]; + uint32_t peripheral[2]; +}; + +static struct uartInitDef_t uartInitDefs[5] = { + { USART1, { GPIOA, GPIOA}, { // UART1 + { GPIO_Pin_9 , GPIO_Speed_50MHz, GPIO_Mode_AF_PP }, // TX + { GPIO_Pin_10, GPIO_Speed_50MHz, GPIO_Mode_IN_FLOATING }, // RX + }, {&RCC->APB2ENR, &RCC->APB2ENR}, {RCC_APB2Periph_USART1, RCC_APB2Periph_GPIOA } }, + { USART2, { GPIOA, GPIOA }, { // UART2 + { GPIO_Pin_2 , GPIO_Speed_50MHz, GPIO_Mode_AF_PP }, // TX + { GPIO_Pin_3, GPIO_Speed_50MHz, GPIO_Mode_IN_FLOATING }, // RX + }, {&RCC->APB1ENR, &RCC->APB2ENR}, {RCC_APB1Periph_USART2, RCC_APB2Periph_GPIOA } }, + { USART3, { GPIOB, GPIOB }, { // UART3 + { GPIO_Pin_10 , GPIO_Speed_50MHz, GPIO_Mode_AF_PP }, // TX + { GPIO_Pin_11, GPIO_Speed_50MHz, GPIO_Mode_IN_FLOATING }, // RX + }, {&RCC->APB1ENR, &RCC->APB2ENR}, {RCC_APB1Periph_USART3, RCC_APB2Periph_GPIOB } }, + { UART4, { GPIOC, GPIOC }, { // UART4 + { GPIO_Pin_10 , GPIO_Speed_50MHz, GPIO_Mode_AF_PP }, // TX + { GPIO_Pin_11, GPIO_Speed_50MHz, GPIO_Mode_IN_FLOATING }, // RX + }, {&RCC->APB1ENR, &RCC->APB2ENR}, {RCC_APB1Periph_UART4, RCC_APB2Periph_GPIOC } }, + { UART5, { GPIOC, GPIOD }, { // UART5 + { GPIO_Pin_12 , GPIO_Speed_50MHz, GPIO_Mode_AF_PP }, // TX + { GPIO_Pin_2, GPIO_Speed_50MHz, GPIO_Mode_IN_FLOATING }, // RX + }, {&RCC->APB1ENR, &RCC->APB2ENR}, {RCC_APB1Periph_UART5, RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD } }, +}; + +void uart_init(uint8_t id) +{ + if (!((id >= 1) && (id <= 3))) + return; + + struct uartInitDef_t * uartInitDef = uartInitDefs + id - 1; + + //clock AFIO + RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); + + int i; + for (i = 0; i < 2; i++) + *(uartInitDef->bridge[i]) |= uartInitDef->peripheral[i]; + + for (i = 0; i < 2; i++) + GPIO_Init(uartInitDef->tdef[i], &uartInitDef->gpiodef[i]); + + USART_InitTypeDef usartdef; + + usartdef.USART_BaudRate = 115200; + usartdef.USART_HardwareFlowControl = USART_HardwareFlowControl_None; + usartdef.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; + usartdef.USART_WordLength = USART_WordLength_8b; + usartdef.USART_StopBits = USART_StopBits_1; + usartdef.USART_Parity = USART_Parity_No; + + USART_Init(uartInitDef->id, &usartdef); + + USART_ClockInitTypeDef clockdef; + USART_ClockStructInit(&clockdef); + USART_ClockInit(uartInitDef->id, &clockdef); + + USART_Cmd(uartInitDef->id, ENABLE); +} + +void uart_send_char(uint8_t id, uint8_t c) +{ + if (!((id >= 1) && (id <= 3))) + return; + + struct uartInitDef_t * uartInitDef = uartInitDefs + id - 1; + + while (USART_GetFlagStatus(uartInitDef->id, USART_FLAG_TXE) == RESET); + USART_SendData(uartInitDef->id, c); +} + +uint8_t uart_receive_char(uint8_t id) +{ + if (!((id >= 1) && (id <= 3))) + return 0; + + struct uartInitDef_t * uartInitDef = uartInitDefs + id - 1; + + while (USART_GetFlagStatus(uartInitDef->id, USART_FLAG_RXNE) == RESET); + return (uint8_t) USART_ReceiveData(uartInitDef->id); +} + +void uart_read(uint8_t id, uint8_t *buffer, uint8_t nb) +{ + while (nb > 0) + { + *buffer = uart_receive_char(id); + buffer++; + nb--; + } +} + +void uart_write(uint8_t id, uint8_t *buffer, uint8_t nb) +{ + while (nb > 0) + { + uart_send_char(id, (uint8_t) (*buffer)); + buffer++; + nb--; + } +} + diff --git a/lib/uart.h b/lib/uart.h new file mode 100644 index 0000000..d83e586 --- /dev/null +++ b/lib/uart.h @@ -0,0 +1,14 @@ +#ifndef __UART__ +#define __UART__ + +#include "FreeRTOS.h" + +void uart_init(uint8_t id); + +void uart_send_char(uint8_t id, uint8_t c); +uint8_t uart_receive_char(uint8_t id); + +void uart_read(uint8_t id, uint8_t *buffer, uint8_t nb); +void uart_write(uint8_t id, uint8_t *buffer, uint8_t nb); + +#endif diff --git a/multitouchglove.c b/multitouchglove.c index 48a7bb7..d5bcf38 100644 --- a/multitouchglove.c +++ b/multitouchglove.c @@ -65,13 +65,14 @@ void resetLed() static void blinkerTask(void *p) { + uint8_t buffer[5]; printf("Start blinker task\n"); while (1) { - setLed(); - vTaskDelay(1357); - resetLed(); - vTaskDelay(1357); + setLed(); + vTaskDelay(1357); + resetLed(); + vTaskDelay(1357); } } -- 2.30.2