From 5fd72bef34ac5c937288d59158526a8c92cbe6df Mon Sep 17 00:00:00 2001 From: Kevin Cavailles Date: Mon, 12 Oct 2020 10:45:14 +0200 Subject: [PATCH] started incr encoder --- keil_project/MyDrivers/MyUart.c | 67 +++++++++++++++++++++++++++++ keil_project/MyDrivers/MyUart.h | 10 +++++ keil_project/Services/IncrEncoder.c | 33 ++++++++++++++ keil_project/Services/IncrEncoder.h | 11 +++++ 4 files changed, 121 insertions(+) create mode 100644 keil_project/MyDrivers/MyUart.c create mode 100644 keil_project/MyDrivers/MyUart.h create mode 100644 keil_project/Services/IncrEncoder.c create mode 100644 keil_project/Services/IncrEncoder.h diff --git a/keil_project/MyDrivers/MyUart.c b/keil_project/MyDrivers/MyUart.c new file mode 100644 index 0000000..b907c68 --- /dev/null +++ b/keil_project/MyDrivers/MyUart.c @@ -0,0 +1,67 @@ +#include "MyUart.h" +#include "stm32f1xx_ll_bus.h" +#include "stm32f1xx_ll_usart.h" +#include "stm32f1xx_ll_gpio.h" + +void MyUart_Conf(USART_TypeDef * uart_port, int baudrate){ + + LL_USART_InitTypeDef My_LL_Usart_Init_Struct; + + if (uart_port==USART1) { + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1); + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); + LL_GPIO_InitTypeDef tx; + tx.Mode = LL_GPIO_MODE_ALTERNATE; + tx.OutputType = LL_GPIO_OUTPUT_PUSHPULL; + tx.Speed = LL_GPIO_SPEED_FREQ_LOW; + tx.Pin = LL_GPIO_PIN_9; + LL_GPIO_Init(GPIOA, &tx); + } + if (uart_port==USART2){ + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2); + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); + LL_GPIO_InitTypeDef tx; + tx.Mode = LL_GPIO_MODE_ALTERNATE; + tx.OutputType = LL_GPIO_OUTPUT_PUSHPULL; + tx.Pull = LL_GPIO_PULL_UP; + tx.Speed = LL_GPIO_SPEED_FREQ_LOW; + tx.Pin = LL_GPIO_PIN_2; + LL_GPIO_Init(GPIOA, &tx); + } + if (uart_port==USART3){ + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART3); + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC); + LL_GPIO_InitTypeDef tx; + tx.Mode = LL_GPIO_MODE_ALTERNATE; + tx.OutputType = LL_GPIO_OUTPUT_PUSHPULL; + tx.Pull = LL_GPIO_PULL_UP; + tx.Speed = LL_GPIO_SPEED_FREQ_LOW; + tx.Pin = LL_GPIO_PIN_10; + LL_GPIO_Init(GPIOC, &tx); + } + + My_LL_Usart_Init_Struct.BaudRate = baudrate; + My_LL_Usart_Init_Struct.DataWidth = LL_USART_DATAWIDTH_8B ; + My_LL_Usart_Init_Struct.HardwareFlowControl = LL_USART_HWCONTROL_NONE; + My_LL_Usart_Init_Struct.OverSampling = LL_USART_OVERSAMPLING_16; + My_LL_Usart_Init_Struct.Parity = LL_USART_PARITY_NONE; + My_LL_Usart_Init_Struct.StopBits = LL_USART_STOPBITS_1; + My_LL_Usart_Init_Struct.TransferDirection = LL_USART_DIRECTION_TX_RX ; + + LL_USART_Init(uart_port,&My_LL_Usart_Init_Struct); + LL_USART_Enable(uart_port); + + /*int periph_speed; + if (uart_port==USART1) periph_speed = 36000000; + if (uart_port==USART2) periph_speed = 72000000; + if (uart_port==USART3) periph_speed = 72000000; + + LL_USART_SetBaudRate(uart_port, periph_speed, baudrate); +*/} + +void MyUart_send_bytes(USART_TypeDef * uart_port,char* buf, int len){ + for(int i = 0; i < len; i++){ + LL_USART_TransmitData8(uart_port, buf[i]); + while(!LL_USART_IsActiveFlag_TXE(uart_port)); + } +} diff --git a/keil_project/MyDrivers/MyUart.h b/keil_project/MyDrivers/MyUart.h new file mode 100644 index 0000000..6641c92 --- /dev/null +++ b/keil_project/MyDrivers/MyUart.h @@ -0,0 +1,10 @@ +#ifndef MYUART_H +#define MYUART_H + +#include "stm32f1xx_ll_usart.h" + +void MyUart_Conf(USART_TypeDef * uart_port, int baudrate); + +void MyUart_send_bytes(USART_TypeDef * uart_port,char* buf, int len); + +#endif \ No newline at end of file diff --git a/keil_project/Services/IncrEncoder.c b/keil_project/Services/IncrEncoder.c new file mode 100644 index 0000000..578d41d --- /dev/null +++ b/keil_project/Services/IncrEncoder.c @@ -0,0 +1,33 @@ +#include "IncrEncoder.h" +#include "stm32f1xx_ll_gpio.h" +#include "stm32f1xx_ll_bus.h" +#include "stm32f1xx_ll_exti.h" + +int index_passed = 0; + +void init(void){ +// use timer in encoder mode (14.3.16) + // attach interrupt to pa5 for i + // enable gpio clock + // configure pin + // attach interrupt? + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); + + LL_GPIO_InitTypeDef index_pin_conf; + index_pin_conf.Mode = LL_GPIO_MODE_FLOATING; + index_pin_conf.Pin = LL_GPIO_PIN_5; + LL_GPIO_Init(GPIOC, &index_pin_conf); + + NVIC_SetPriority(EXTI9_5_IRQn, 12); // prio?? + NVIC_EnableIRQ(EXTI9_5_IRQn); + +} + +void EXTI9_5_IRQHandler(void){ + index_passed = 1; + // TODO clear pending +} + +int isAbsolute(void); + +int getAngle(void); diff --git a/keil_project/Services/IncrEncoder.h b/keil_project/Services/IncrEncoder.h new file mode 100644 index 0000000..bf65dc6 --- /dev/null +++ b/keil_project/Services/IncrEncoder.h @@ -0,0 +1,11 @@ +#ifndef INCR_ENCODER + +/** + */ +void init(void); + +int isAbsolute(void); + +int getAngle(void); + +#endif