-
Notifications
You must be signed in to change notification settings - Fork 10
/
ir_transmitter.c
executable file
·215 lines (159 loc) · 6.86 KB
/
ir_transmitter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*MIT License
Copyright (c) 2019 Raees Kattali
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/
#include <stdbool.h>
#include <stdint.h>
#include "ir_transmitter.h"
#include "nrfx_timer.h"
#include "nrfx_gpiote.h"
#include "nrf_delay.h"
#include "nrfx_ppi.h"
#include "nrf_drv_ppi.h"
#include "app_error.h"
#include "nrf_log.h"
#include "boards.h"
//IR LED Input
#define IR_LED 18
const nrfx_timer_t IR_CARRIER_TIMER = NRFX_TIMER_INSTANCE(1);
const nrfx_timer_t IR_PWM_TIMER = NRFX_TIMER_INSTANCE(2);
static nrf_ppi_channel_t ppi_channel_1;
static nrf_ppi_channel_t ppi_channel_2;
void ir_carrier_init(void);
void ir_pwm_timer_init(void);
void pwm_timer_event_handler(nrf_timer_event_t event_type, void* p_context);
void dummy_timer_event_handler(nrf_timer_event_t event_type, void* p_context);
static uint32_t ir_bit_index = 0;
static uint32_t ir_pulse_count = 0;
ir_data_t *ir_signal_burst_to_send;
ir_transmit_complete_task *c_task;
bool is_sending = false;
bool send_ir_burst(ir_data_t *ir_data, uint32_t pulse_count, ir_transmit_complete_task t) {
ret_code_t err_code;
if(is_sending) {
t();
return false;
}
c_task = t;
ir_pulse_count = pulse_count;
ir_signal_burst_to_send = ir_data;
if(!nrfx_gpiote_is_init()) {
err_code = nrfx_gpiote_init();
APP_ERROR_CHECK(err_code);
}
ir_carrier_init();
ir_pwm_timer_init();
ir_bit_index = 0;
is_sending = true;
return true;
}
//Timer 1 will toggle IR LED via PPI
void ir_carrier_init(void) {
uint32_t time_us = 14; //Carrier frequancy
uint32_t time_ticks;
uint32_t compare_evt_addr;
uint32_t gpiote_task_addr;
ret_code_t err_code;
nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;
err_code = nrfx_timer_init(&IR_CARRIER_TIMER, &timer_cfg, dummy_timer_event_handler);
APP_ERROR_CHECK(err_code);
nrfx_gpiote_out_config_t config = NRFX_GPIOTE_CONFIG_OUT_TASK_TOGGLE(false);
err_code = nrfx_gpiote_out_init(IR_LED, &config);
APP_ERROR_CHECK(err_code);
time_ticks = nrfx_timer_us_to_ticks(&IR_CARRIER_TIMER, time_us);
nrfx_timer_extended_compare(&IR_CARRIER_TIMER, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);
err_code = nrfx_ppi_channel_alloc(&ppi_channel_2);
APP_ERROR_CHECK(err_code);
compare_evt_addr = nrfx_timer_event_address_get(&IR_CARRIER_TIMER, NRF_TIMER_EVENT_COMPARE0);
gpiote_task_addr = nrfx_gpiote_out_task_addr_get(IR_LED);
err_code = nrfx_ppi_channel_assign(ppi_channel_2, compare_evt_addr, gpiote_task_addr);
APP_ERROR_CHECK(err_code);
err_code = nrfx_ppi_channel_enable(ppi_channel_2);
APP_ERROR_CHECK(err_code);
nrfx_gpiote_out_task_enable(IR_LED);
nrfx_timer_enable(&IR_CARRIER_TIMER);
nrfx_timer_pause(&IR_CARRIER_TIMER);
}
//This timir will control the IR Carrier out ON/OFF based on IR Data
void ir_pwm_timer_init(void) {
uint32_t timer_stop_task_addr;
uint32_t timer_event_addr;
ret_code_t err_code;
nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;
timer_cfg.frequency = 4;
err_code = nrfx_timer_init(&IR_PWM_TIMER, &timer_cfg, pwm_timer_event_handler);
APP_ERROR_CHECK(err_code);
nrfx_timer_extended_compare(&IR_PWM_TIMER, NRF_TIMER_CC_CHANNEL0, 20000, NRF_TIMER_SHORT_COMPARE0_STOP_MASK, true);
//PPI from PWM TIMER to IR_CARRIER TIMER Pause
timer_stop_task_addr = nrfx_timer_task_address_get(&IR_CARRIER_TIMER, NRF_TIMER_TASK_STOP);
timer_event_addr = nrfx_timer_event_address_get(&IR_PWM_TIMER, NRF_TIMER_EVENT_COMPARE0);
err_code = nrfx_ppi_channel_alloc(&ppi_channel_1);
APP_ERROR_CHECK(err_code);
err_code = nrfx_ppi_channel_assign(ppi_channel_1, timer_event_addr, timer_stop_task_addr);
APP_ERROR_CHECK(err_code);
err_code = nrfx_ppi_channel_enable(ppi_channel_1);
APP_ERROR_CHECK(err_code);
nrfx_timer_enable(&IR_PWM_TIMER);
}
void pwm_timer_event_handler(nrf_timer_event_t event_type, void* p_context) {
switch (event_type)
{
case NRF_TIMER_EVENT_COMPARE0:
{
nrf_gpio_pin_clear(IR_LED);
//DWT->CYCCNT = 0;
ir_data_t *x = &ir_signal_burst_to_send[ir_bit_index];
uint32_t width_us = x->duty_cycle_us;
uint32_t state = x->duty_cycle_state;
//Timing calibrations considering processor instruction cycles delay
if(state) {
width_us = width_us - 7;
}
else
{
width_us = width_us - 40;
}
//----
nrfx_timer_disable(&IR_PWM_TIMER);
nrfx_timer_extended_compare(&IR_PWM_TIMER, NRF_TIMER_CC_CHANNEL0, width_us, NRF_TIMER_SHORT_COMPARE0_STOP_MASK, true);
nrfx_timer_enable(&IR_PWM_TIMER);
if(state) {
nrfx_timer_resume(&IR_CARRIER_TIMER);
}
ir_bit_index++;
if(ir_bit_index > ir_pulse_count) {
nrfx_timer_pause(&IR_CARRIER_TIMER);
nrfx_timer_pause(&IR_PWM_TIMER);
if(nrfx_timer_is_enabled(&IR_PWM_TIMER)) nrfx_timer_disable(&IR_PWM_TIMER);
if(nrfx_timer_is_enabled(&IR_CARRIER_TIMER)) nrfx_timer_disable(&IR_CARRIER_TIMER);
nrfx_timer_uninit(&IR_PWM_TIMER);
nrfx_timer_uninit(&IR_CARRIER_TIMER);
nrfx_ppi_channel_disable(ppi_channel_1);
nrfx_ppi_channel_free(ppi_channel_1);
nrfx_ppi_channel_disable(ppi_channel_2);
nrfx_ppi_channel_free(ppi_channel_2);
nrfx_gpiote_out_task_disable(IR_LED);
nrfx_gpiote_out_uninit(IR_LED);
is_sending = false;
c_task();
}
break;
}
default:
break;
}
}
void dummy_timer_event_handler(nrf_timer_event_t event_type, void* p_context) {}