From 30e6778231f25c9ab1e18a9a02700aaf532ff408 Mon Sep 17 00:00:00 2001 From: Tadeusz Magura-Witkowski Date: Thu, 14 Mar 2019 23:01:58 +0100 Subject: [PATCH] More wiegand stuff --- Core/Inc/logger.h | 3 +++ Core/Inc/wiegand.h | 18 ++++++++++++++ Core/Src/main.c | 2 +- Core/Src/stm32f1xx_it.c | 13 ++++++++++ Core/Src/wiegand.c | 54 ++++++++++++++++++++++++++++++++++++----- Makefile | 1 + canpybara.ioc | 6 +++-- 7 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 Core/Inc/wiegand.h diff --git a/Core/Inc/logger.h b/Core/Inc/logger.h index 37c74db..c24cbe9 100644 --- a/Core/Inc/logger.h +++ b/Core/Inc/logger.h @@ -1,7 +1,10 @@ #ifndef _LOGGER_H #define _LOGGER_H +#include "stm32f1xx_hal.h" + #include +#include #define __LOG__(format, loglevel, ...) printf("%-10"PRIu32" [%s:%d] " format "\n", HAL_GetTick(), __FILE__, __LINE__, ## __VA_ARGS__) #define __LOG(format, ...) __LOG__(format, "DEBUG", ## __VA_ARGS__) diff --git a/Core/Inc/wiegand.h b/Core/Inc/wiegand.h new file mode 100644 index 0000000..64826a1 --- /dev/null +++ b/Core/Inc/wiegand.h @@ -0,0 +1,18 @@ +#ifndef WIEGAND_H +#define WIEGAND_H + +#define WIEGAND_MAX_LENGTH 30 +#define WIEGAND_BUFFER_BIT_LENGTH sizeof(wiegand_buffer)*8 +#define WIEGAND_TIMEOUT 100 + +#define WIEGAND_CARD_LENGTH 26 +#define WIEGAND_KEYPRESS_LENGTH 6 + +#define WIEGAND_ENABLED + +void canpybara_wiegand_reset(void); + +void canpybara_wiegand_pin_pulse_interrupt(int bit); +void canpybara_wiegand_systick_interrupt(void); + +#endif diff --git a/Core/Src/main.c b/Core/Src/main.c index 8b65122..2378de7 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -340,7 +340,7 @@ static void MX_GPIO_Init(void) /*Configure GPIO pins : IN0_Pin IN1_Pin */ GPIO_InitStruct.Pin = IN0_Pin|IN1_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; + GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); diff --git a/Core/Src/stm32f1xx_it.c b/Core/Src/stm32f1xx_it.c index 9a54c9e..c2cc2db 100644 --- a/Core/Src/stm32f1xx_it.c +++ b/Core/Src/stm32f1xx_it.c @@ -40,6 +40,7 @@ #include "logger.h" #include "gpio.h" #include "can.h" +#include "wiegand.h" /* USER CODE END 0 */ @@ -360,7 +361,19 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { + #ifdef WIEGAND_ENABLED + canpybara_wiegand_pin_pulse_interrupt(GPIO_Pin == IN1_Pin ? 1 : 0); + #warning Wiegand enabled: GPIO interrupt will be disabled + #else canpybara_gpio_interrupt(GPIO_Pin); + #endif +} + +void HAL_SYSTICK_Callback(void) +{ + #ifdef WIEGAND_ENABLED + canpybara_wiegand_systick_interrupt(); + #endif } /* USER CODE END 1 */ diff --git a/Core/Src/wiegand.c b/Core/Src/wiegand.c index e0c92d6..795e4ec 100644 --- a/Core/Src/wiegand.c +++ b/Core/Src/wiegand.c @@ -1,9 +1,11 @@ #include -int wiegand_position; -uint32_t wiegand_buffer; +#include "logger.h" +#include "wiegand.h" -#define WIEGAND_BUFFER_BIT_LENGTH sizeof(wiegand_buffer)*8 +unsigned int wiegand_position; +uint32_t wiegand_buffer; +uint32_t wiegand_timeout; void canpybara_wiegand_reset(void) { @@ -11,11 +13,53 @@ void canpybara_wiegand_reset(void) wiegand_buffer = 0; } +void canpybara_wiegand_process_card(void) +{ + +} + +void canpybara_wiegand_process_keypress(void) +{ + +} + +void canpybara_wiegand_process_scan(void) +{ + switch(wiegand_position) + { + case WIEGAND_CARD_LENGTH: + canpybara_wiegand_process_card(); + break; + case WIEGAND_KEYPRESS_LENGTH: + canpybara_wiegand_process_keypress(); + break; + default: + LOG("Unknown WIEGAND message"); + } +} -void canpybara_wiegand_pin_pulse(int bit) +void canpybara_wiegand_pin_pulse_interrupt(int bit) { wiegand_buffer |= bit << (WIEGAND_BUFFER_BIT_LENGTH -1 - wiegand_position); wiegand_position ++; + + wiegand_timeout = 0; +} + +void canpybara_wiegand_systick_interrupt(void) +{ + if(wiegand_timeout > WIEGAND_TIMEOUT) + { + if(wiegand_position) + { + canpybara_wiegand_process_scan(); + } + canpybara_wiegand_reset(); + } + else + { + wiegand_timeout ++; + } } @@ -67,5 +111,3 @@ uint8_t canpybara_wiegand_is_valid(void) return 1; } - - diff --git a/Makefile b/Makefile index be98b7b..326a08d 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,7 @@ Core/Src/test.c \ Core/Src/can.c \ Core/Src/gpio.c \ Core/Src/version.c \ +Core/Src/wiegand.c \ # ASM sources ASM_SOURCES = \ diff --git a/canpybara.ioc b/canpybara.ioc index f6e36ac..d568ab3 100644 --- a/canpybara.ioc +++ b/canpybara.ioc @@ -128,12 +128,14 @@ PA6.Signal=GPIO_Input PA9.Locked=true PA9.Mode=Asynchronous PA9.Signal=USART1_TX -PB0.GPIOParameters=GPIO_Label +PB0.GPIOParameters=GPIO_Label,GPIO_ModeDefaultEXTI PB0.GPIO_Label=IN0 +PB0.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING PB0.Locked=true PB0.Signal=GPXTI0 -PB1.GPIOParameters=GPIO_Label +PB1.GPIOParameters=GPIO_Label,GPIO_ModeDefaultEXTI PB1.GPIO_Label=IN1 +PB1.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING PB1.Locked=true PB1.Signal=GPXTI1 PB10.GPIOParameters=GPIO_Label