Adding UART
authorThomas Pietrzak <thomas.pietrzak@gmail.com>
Sat, 19 Oct 2013 15:08:01 +0000 (15:08 +0000)
committerThomas Pietrzak <thomas.pietrzak@gmail.com>
Sat, 19 Oct 2013 15:08:01 +0000 (15:08 +0000)
git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@120 47cf9a05-e0a8-4ed5-9e9b-101a649bc004

Makefile
lib/Makefile
lib/spi.c
lib/spi.h
lib/uart.c [new file with mode: 0644]
lib/uart.h [new file with mode: 0644]
multitouchglove.c

index 92a4610a3b37aef9f0028dd4027bbab1c470c54c..dd278f7be20766cc4b10c7627296c1b98852542f 100644 (file)
--- 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
index 1e755976f324b3b0214dc102d131ff1eef5cb714..a2c591572f73cae6798abfbef18f56f4d00f48f8 100644 (file)
@@ -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
index a7ccb5e3f3227c856f494c885c5ecb4bbbef0d84..242de71da59e12b5b148bca813a861f971a4f289 100644 (file)
--- 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)
index 7ca8806e36c564920ad82882067126a53bc1d48a..58ff45098f3b791589ec94c97b41fe9d3d7fe747 100644 (file)
--- 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 (file)
index 0000000..a97b51d
--- /dev/null
@@ -0,0 +1,119 @@
+#include "spi.h"
+#include "task.h"
+#include <stm32f10x.h>
+#include <stm32f10x_gpio.h>
+#include <stm32f10x_rcc.h>
+#include <stm32f10x_usart.h>
+
+#include <stdio.h>
+
+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 (file)
index 0000000..d83e586
--- /dev/null
@@ -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
index 48a7bb77a96eb55df403789764824122e2975446..d5bcf38546c59e4a7de8becca7bc6a4ab3556624 100644 (file)
@@ -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);
     }
 }