diff --git a/applications/irda/irda.c b/applications/irda/irda.c index a63d72cb..31a1a7f2 100644 --- a/applications/irda/irda.c +++ b/applications/irda/irda.c @@ -7,12 +7,13 @@ typedef enum { EventTypeTick, EventTypeKey, - EventTypeLed, + EventTypeRX, } EventType; typedef struct { union { InputEvent input; + bool rx_edge; } value; EventType type; } AppEvent; @@ -227,8 +228,11 @@ static void input_callback(InputEvent* input_event, void* ctx) { osMessageQueuePut(event_queue, &event, 0, 0); } +osMessageQueueId_t irda_event_queue; + void irda(void* p) { - osMessageQueueId_t event_queue = osMessageQueueNew(1, sizeof(AppEvent), NULL); + osMessageQueueId_t event_queue = osMessageQueueNew(32, sizeof(AppEvent), NULL); + irda_event_queue = event_queue; State _state; uint8_t mode_count = sizeof(modes) / sizeof(modes[0]); @@ -259,6 +263,19 @@ void irda(void* p) { } gui->add_widget(gui, widget, GuiLayerFullscreen); + // Red LED + // create pin + GpioPin led = led_gpio[0]; + + // TODO open record + GpioPin* led_record = &led; + + // configure pin + gpio_init(led_record, GpioModeOutputOpenDrain); + + // setup irda rx timer + tim_irda_rx_init(); + AppEvent event; while(1) { osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, osWaitForever); @@ -287,7 +304,10 @@ void irda(void* p) { } modes[state->mode_id].input(&event, state); + } else if(event.type == EventTypeRX) { + gpio_write(led_record, event.value.rx_edge); } + } else { // event timeout } @@ -295,4 +315,23 @@ void irda(void* p) { release_mutex(&state_mutex, state); widget_update(widget); } +} + +void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim) { + if(htim->Instance == TIM2) { + if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) { + // falling event + AppEvent event; + event.type = EventTypeRX; + event.value.rx_edge = false; + osMessageQueuePut(irda_event_queue, &event, 0, 0); + } else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) { + // rising event + //uint32_t period_in_us = HAL_TIM_ReadCapturedValue(); + AppEvent event; + event.type = EventTypeRX; + event.value.rx_edge = true; + osMessageQueuePut(irda_event_queue, &event, 0, 0); + } + } } \ No newline at end of file diff --git a/firmware/targets/f2/Inc/main.h b/firmware/targets/f2/Inc/main.h index 7ea00148..0e8ffcea 100644 --- a/firmware/targets/f2/Inc/main.h +++ b/firmware/targets/f2/Inc/main.h @@ -146,8 +146,8 @@ extern TIM_HandleTypeDef htim15; #define LFRFID_TIM htim15 #define LFRFID_CH TIM_CHANNEL_1 -#define IRDA_TIM htim2 -#define IRDA_CH TIM_CHANNEL_4 +#define IRDA_TX_TIM htim2 +#define IRDA_TX_CH TIM_CHANNEL_3 #define NFC_IRQ_Pin RFID_PULL_Pin #define NFC_IRQ_GPIO_Port RFID_PULL_GPIO_Port diff --git a/firmware/targets/f2/api-hal/api-hal-pwm.c b/firmware/targets/f2/api-hal/api-hal-pwm.c index ee8bf879..2a7f4a6f 100644 --- a/firmware/targets/f2/api-hal/api-hal-pwm.c +++ b/firmware/targets/f2/api-hal/api-hal-pwm.c @@ -43,9 +43,9 @@ void hal_pwmn_stop(TIM_HandleTypeDef* tim, uint32_t channel) { } void irda_pwm_set(float value, float freq){ - hal_pwm_set(value, freq, &IRDA_TIM, IRDA_CH); + hal_pwm_set(value, freq, &IRDA_TX_TIM, IRDA_TX_CH); } void irda_pwm_stop(){ - hal_pwm_stop(&IRDA_TIM, IRDA_CH); + hal_pwm_stop(&IRDA_TX_TIM, IRDA_TX_CH); } \ No newline at end of file diff --git a/firmware/targets/f2/api-hal/api-hal-tim.c b/firmware/targets/f2/api-hal/api-hal-tim.c new file mode 100644 index 00000000..2c61ce8e --- /dev/null +++ b/firmware/targets/f2/api-hal/api-hal-tim.c @@ -0,0 +1,5 @@ +#include "cmsis_os.h" +#include "api-hal-tim.h" + +void tim_irda_rx_init(void) { +} \ No newline at end of file diff --git a/firmware/targets/f2/api-hal/api-hal-tim.h b/firmware/targets/f2/api-hal/api-hal-tim.h new file mode 100644 index 00000000..3a7ee438 --- /dev/null +++ b/firmware/targets/f2/api-hal/api-hal-tim.h @@ -0,0 +1,4 @@ +#pragma once +#include "main.h" + +void tim_irda_rx_init(void); diff --git a/firmware/targets/f2/api-hal/api-hal.h b/firmware/targets/f2/api-hal/api-hal.h index 68fb0bfb..556f617b 100644 --- a/firmware/targets/f2/api-hal/api-hal.h +++ b/firmware/targets/f2/api-hal/api-hal.h @@ -4,3 +4,4 @@ #include "api-hal-delay.h" #include "api-hal-pwm.h" #include "api-hal-task.h" +#include "api-hal-tim.h" diff --git a/firmware/targets/f3/Inc/main.h b/firmware/targets/f3/Inc/main.h index 40550057..070ff06e 100644 --- a/firmware/targets/f3/Inc/main.h +++ b/firmware/targets/f3/Inc/main.h @@ -173,8 +173,15 @@ extern TIM_HandleTypeDef htim16; #define LFRFID_TIM htim1 #define LFRFID_CH TIM_CHANNEL_1 -#define IRDA_TIM htim1 -#define IRDA_CH TIM_CHANNEL_3 +#define IRDA_TX_TIM htim1 +#define IRDA_TX_CH TIM_CHANNEL_3 + +// only for reference +// IRDA RX timer dont exist in F2 +// and timer need more data to init (NVIC IRQn to set priority) +#define IRDA_RX_TIM htim2 +#define IRDA_RX_FALLING_CH TIM_CHANNEL_1 +#define IRDA_RX_RISING_CH TIM_CHANNEL_2 #define NFC_IRQ_Pin RFID_PULL_Pin #define NFC_IRQ_GPIO_Port RFID_PULL_GPIO_Port diff --git a/firmware/targets/f3/api-hal/api-hal-pwm.c b/firmware/targets/f3/api-hal/api-hal-pwm.c index d198e552..76bf21a6 100644 --- a/firmware/targets/f3/api-hal/api-hal-pwm.c +++ b/firmware/targets/f3/api-hal/api-hal-pwm.c @@ -49,9 +49,9 @@ void hal_pwmn_stop(TIM_HandleTypeDef* tim, uint32_t channel) { } void irda_pwm_set(float value, float freq){ - hal_pwmn_set(value, freq, &IRDA_TIM, IRDA_CH); + hal_pwmn_set(value, freq, &IRDA_TX_TIM, IRDA_TX_CH); } void irda_pwm_stop(){ - hal_pwmn_stop(&IRDA_TIM, IRDA_CH); + hal_pwmn_stop(&IRDA_TX_TIM, IRDA_TX_CH); } \ No newline at end of file diff --git a/firmware/targets/f3/api-hal/api-hal-tim.c b/firmware/targets/f3/api-hal/api-hal-tim.c new file mode 100644 index 00000000..a423dd0b --- /dev/null +++ b/firmware/targets/f3/api-hal/api-hal-tim.c @@ -0,0 +1,8 @@ +#include "cmsis_os.h" +#include "api-hal-tim.h" + +void tim_irda_rx_init(void) { + HAL_NVIC_SetPriority(TIM2_IRQn, 5, 0); + HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1); + HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2); +} \ No newline at end of file diff --git a/firmware/targets/f3/api-hal/api-hal-tim.h b/firmware/targets/f3/api-hal/api-hal-tim.h new file mode 100644 index 00000000..3a7ee438 --- /dev/null +++ b/firmware/targets/f3/api-hal/api-hal-tim.h @@ -0,0 +1,4 @@ +#pragma once +#include "main.h" + +void tim_irda_rx_init(void); diff --git a/firmware/targets/f3/api-hal/api-hal.h b/firmware/targets/f3/api-hal/api-hal.h index 68fb0bfb..556f617b 100644 --- a/firmware/targets/f3/api-hal/api-hal.h +++ b/firmware/targets/f3/api-hal/api-hal.h @@ -4,3 +4,4 @@ #include "api-hal-delay.h" #include "api-hal-pwm.h" #include "api-hal-task.h" +#include "api-hal-tim.h"