-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathir_send.c
247 lines (194 loc) · 6.23 KB
/
ir_send.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#ifndef IR_SEND_C_
#define IR_SEND_C_
#include "app/framework/include/af.h"
#include "em_device.h"
#include "em_cmu.h"
#include "em_emu.h"
#include "em_chip.h"
#include "em_gpio.h"
#include "em_prs.h"
#include "em_timer.h"
#include "dmadrv.h"
#define SEND_PORT_BRD4182A gpioPortB
#define SEND_PIN_BRD4182A 1
#define DUTY_CYCLE_STEPS 0.335
#define CARRIER_FREQUENCY 38000
#define PRS_CH 2
#define IR_SEND_DEBUG
#define DUMMY_VALUE 1000
uint8_t count = 0;
uint32_t timer1Freq = 0;
unsigned int channel;
int IR_frame[] = {DUMMY_VALUE, 560, 200, 560, 1600, 400, 150, 800, 3000, 40, DUMMY_VALUE};
const int length_IR_frame = sizeof (IR_frame) / sizeof (int);
void ir_frame_setup()
{
if ((length_IR_frame % 2) == 0)
{
emberAfCorePrintln("Number of input symbol should be a odd number");
abort();
}
uint64_t v;
for (uint32_t i = 0; i < length_IR_frame; i++)
{
v = ((uint64_t)IR_frame[i] * (uint64_t)timer1Freq)/1000000;
#ifdef IR_SEND_DEBUG
emberAfCorePrintln("v[%d] = %ld", i, v);
#endif
if ((i != length_IR_frame - 1) && (v == 0 || (v >> 16)))
{
emberAfCorePrintln("Bad value %ld, %d", v, i);
abort();
}
IR_frame[i] = v & 0xFFFF;
}
/* Set last dummy value to maximum */
IR_frame[length_IR_frame - 1] = 0xFFFF;
}
void IR_generate_STOP(void)
{
DMADRV_FreeChannel(&channel);
TIMER_Enable(TIMER1,false);
TIMER_Enable(TIMER2,false);
}
void ir_init_topB(void)
{
TIMER_TopSet(TIMER1, IR_frame[0]);
TIMER_TopBufSet(TIMER1, IR_frame[1]);
}
/**************************************************************************//**
* @brief
* TIMER 1 handler
*****************************************************************************/
void TIMER1_IRQHandler(void)
{
/*Check the interrupt flag */
uint32_t flags = TIMER_IntGet(TIMER1);
TIMER_IntClear(TIMER1, flags);
if ((flags & TIMER_IF_OF) == 0)
return;
if ((TIMER1->STATUS & TIMER_STATUS_RUNNING) == 0)
return;
if (++count == length_IR_frame - 1)
/* End of transfer */
{
IR_generate_STOP();
GPIO_PinOutClear(SEND_PORT_BRD4182A, SEND_PIN_BRD4182A);
}
}
/**************************************************************************//**
* @brief
* IR generate 38kHz with a certain duty cycle with TIMER 2 CC0
*****************************************************************************/
static volatile float dutyCycle = 0;
void IR_generate_freq(void)
{
uint32_t topValue;
uint32_t timer2Freq = 0;
CMU_ClockEnable(cmuClock_TIMER2, true);
/*Initialize the timer*/
TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;
TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT;
// Use PWM mode, which sets output on overflow and clears on compare events
timerInit.prescale = timerPrescale1;
timerInit.enable = false;
timerCCInit.mode = timerCCModePWM;
timerCCInit.prsOutput = timerPrsOutputLevel;
// configure, but do not start timer
TIMER_Init(TIMER2, &timerInit);
// Configure CC Channel 0
TIMER_InitCC(TIMER2, 0, &timerCCInit);
dutyCycle = DUTY_CYCLE_STEPS;
// set PWM period
timer2Freq = CMU_ClockFreqGet(cmuClock_TIMER2) / (timerInit.prescale + 1);
topValue = (timer2Freq / CARRIER_FREQUENCY);
// Set top value to overflow at the desired PWM_FREQ frequency
TIMER_TopSet (TIMER2, topValue);
/*Set compare value for initial duty cycle with the CCV register*/
TIMER_CompareSet(TIMER2, 0, (uint32_t)(topValue * dutyCycle));
TIMER_Enable(TIMER2, true);
}
/**************************************************************************//**
* @brief
* IR generate time base
*****************************************************************************/
void IR_generate_timebase(void)
{
CMU_ClockEnable(cmuClock_TIMER1, true);
count = 0;
/*Initialize the timer*/
TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT;
timerInit.prescale = timerPrescale8;
timerInit.enable = false;
timerInit.clkSel = timerClkSelHFPerClk;
// configure, but do not start timer
TIMER_Init(TIMER1, &timerInit);
timer1Freq = CMU_ClockFreqGet(cmuClock_TIMER1)/(timerInit.prescale + 1);
emberAfCorePrintln("%timer1 frequency : %d", timer1Freq);
ir_frame_setup();
/* CC0 */
TIMER_InitCC_TypeDef timerCC0Init = TIMER_INITCC_DEFAULT;
timerCC0Init.mode = timerCCModeCompare;
timerCC0Init.prsOutput = timerPrsOutputLevel;
timerCC0Init.cofoa = timerOutputActionToggle;
TIMER_InitCC(TIMER1, 0, &timerCC0Init);
#ifdef IR_SEND_DEBUG
GPIO->TIMERROUTE[1].ROUTEEN = GPIO_TIMER_ROUTEEN_CC0PEN;
GPIO->TIMERROUTE[1].CC0ROUTE = (SEND_PORT_BRD4182A << _GPIO_TIMER_CC0ROUTE_PORT_SHIFT)
| (0 << _GPIO_TIMER_CC0ROUTE_PIN_SHIFT);
#endif
/* Load two first values in TIMER1 */
ir_init_topB();
/* Initialize DMA */
DMADRV_Init();
/* Request a DMA channel */
DMADRV_AllocateChannel( &channel, NULL );
DMADRV_MemoryPeripheral(channel,
dmadrvPeripheralSignal_TIMER1_UFOF,
&(TIMER1->TOPB),
&IR_frame[2],
1,
length_IR_frame - 2,
dmadrvDataSize4,
NULL,
NULL);
/* Clear and enable OF interrupt */
TIMER_IntClear(TIMER1, TIMER_IF_OF);
TIMER_IntEnable(TIMER1, TIMER_IF_OF);
NVIC_EnableIRQ(TIMER1_IRQn);
/* Start timer */
TIMER_Enable(TIMER1, true);
}
void initPrs_And(void)
{
/*init CMU*/
CMU_ClockEnable(cmuClock_PRS, true);
// Route PRS TIMER1 CC0 on PRS_CH + 1
PRS_SourceAsyncSignalSet(
PRS_CH + 1,
PRS_ASYNC_CH_CTRL_SOURCESEL_TIMER1,
_PRS_ASYNC_CH_CTRL_SIGSEL_TIMER1CC0);
// Route TIMER2 CC0 on PRS_CH
PRS_SourceAsyncSignalSet(
PRS_CH,
PRS_ASYNC_CH_CTRL_SOURCESEL_TIMER2,
_PRS_ASYNC_CH_CTRL_SIGSEL_TIMER2CC0);
// Configure PRS logic
PRS_Combine(PRS_CH+1, PRS_CH, prsLogic_A_AND_B);
// Route output
PRS_PinOutput(PRS_CH+1, prsTypeAsync, SEND_PORT_BRD4182A , 1);
}
void IR_init_send()
{
/*init CMU*/
CMU_ClockEnable(cmuClock_GPIO, true);
/*init Send PORT*/
GPIO_PinModeSet(SEND_PORT_BRD4182A, SEND_PIN_BRD4182A, gpioModePushPull, 0);
GPIO_PinModeSet(SEND_PORT_BRD4182A, 1, gpioModePushPull, 0);
initPrs_And();
//init 38kHZ
IR_generate_freq();
//send trame
IR_generate_timebase();
}
#endif /* IR_SEND_C_ */