|
@@ -57,12 +57,10 @@
|
57
|
57
|
#include "ch.h"
|
58
|
58
|
#include "hal.h"
|
59
|
59
|
|
60
|
|
-static event_listener_t el;
|
61
|
|
-static event_source_t IR_receiver;
|
62
|
|
-/*===========================================================================*/
|
63
|
|
-/* IR remote related code. */
|
64
|
|
-/*===========================================================================*/
|
65
|
60
|
|
|
61
|
+/*
|
|
62
|
+ * NEC timing macros
|
|
63
|
+ */
|
66
|
64
|
#define DELTA 5
|
67
|
65
|
#define START_PULSE 45
|
68
|
66
|
#define END_PULSE 405
|
|
@@ -71,10 +69,14 @@ static event_source_t IR_receiver;
|
71
|
69
|
#define ONE_PULSE 17
|
72
|
70
|
#define COMMA_PULSE 23
|
73
|
71
|
|
|
72
|
+
|
|
73
|
+static event_listener_t el;
|
|
74
|
+static event_source_t IR_receiver;
|
|
75
|
+
|
74
|
76
|
/* PWM configuration for IR output */
|
75
|
77
|
static PWMConfig pwmcfg = {
|
76
|
|
- 380000, /* 380 kHz PWM clock frequency. */
|
77
|
|
- 10, /* PWM period 10 clock cycles. */
|
|
78
|
+ 380000, /* 380 kHz PWM clock frequency */
|
|
79
|
+ 10, /* PWM period 10 clock cycles */
|
78
|
80
|
NULL,
|
79
|
81
|
{
|
80
|
82
|
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
|
|
@@ -88,9 +90,9 @@ static PWMConfig pwmcfg = {
|
88
|
90
|
|
89
|
91
|
/* Timer configuration for IR output */
|
90
|
92
|
static const GPTConfig gpt4cfg = {
|
91
|
|
- 1000000, // 1 MHz timer clock.
|
92
|
|
- NULL, // No callback
|
93
|
|
- 0, 0
|
|
93
|
+ 1000000, /* 1 MHz timer clock */
|
|
94
|
+ NULL, /* No callback */
|
|
95
|
+ 0, 0
|
94
|
96
|
};
|
95
|
97
|
|
96
|
98
|
/*===========================================================================*/
|
|
@@ -99,67 +101,46 @@ static const GPTConfig gpt4cfg = {
|
99
|
101
|
static int32_t index = -1;
|
100
|
102
|
static bool START_OCCURED = FALSE, REPEAT_FLAG = FALSE;
|
101
|
103
|
static uint32_t tmp, command;
|
102
|
|
-static void icuwidthcb(ICUDriver *icup) {
|
103
|
104
|
|
104
|
|
- icucnt_t cnt = icuGetWidthX(icup);
|
105
|
|
- if((cnt > (START_PULSE - DELTA)) && (cnt < (START_PULSE + DELTA))){
|
106
|
|
- index = 0;
|
107
|
|
- START_OCCURED = TRUE;
|
108
|
|
- }
|
109
|
|
- else if((cnt > (ONE_PULSE - DELTA)) && (cnt < (ONE_PULSE + DELTA))){
|
110
|
|
- tmp |= 1 << (31 - index);
|
111
|
|
- index++;
|
112
|
|
- }
|
113
|
|
- else if((cnt > (ZERO_PULSE - DELTA)) && (cnt < (ZERO_PULSE + DELTA))){
|
114
|
|
- tmp &= ~(1 << (31 - index));
|
115
|
|
- index++;
|
116
|
|
- }
|
117
|
|
- else if((cnt > (END_PULSE - 4*DELTA)) && (cnt < (END_PULSE + 4*DELTA))){
|
118
|
|
- if((START_OCCURED) && (index == 32)) {
|
119
|
|
- command = tmp;
|
|
105
|
+static void icuwidthcb(ICUDriver *icup) {
|
|
106
|
+ icucnt_t cnt = icuGetWidthX(icup);
|
|
107
|
+ if ((cnt > (START_PULSE - DELTA)) && (cnt < (START_PULSE + DELTA))) {
|
|
108
|
+ index = 0;
|
|
109
|
+ START_OCCURED = TRUE;
|
|
110
|
+ } else if ((cnt > (ONE_PULSE - DELTA)) && (cnt < (ONE_PULSE + DELTA))) {
|
|
111
|
+ tmp |= 1 << (31 - index);
|
|
112
|
+ index++;
|
|
113
|
+ } else if ((cnt > (ZERO_PULSE - DELTA)) && (cnt < (ZERO_PULSE + DELTA))) {
|
|
114
|
+ tmp &= ~(1 << (31 - index));
|
|
115
|
+ index++;
|
|
116
|
+ } else if ((cnt > (END_PULSE - 4*DELTA)) && (cnt < (END_PULSE + 4*DELTA))) {
|
|
117
|
+ if ((START_OCCURED) && (index == 32)) {
|
|
118
|
+ command = tmp;
|
|
119
|
+ } else {
|
|
120
|
+ command = 0;
|
|
121
|
+ }
|
|
122
|
+ REPEAT_FLAG = FALSE;
|
|
123
|
+ START_OCCURED = FALSE;
|
|
124
|
+ index = -1;
|
|
125
|
+ } else if ((cnt > (RPT_CMD_PULSE - DELTA)) && (cnt < (RPT_CMD_PULSE + DELTA))) {
|
|
126
|
+ REPEAT_FLAG = TRUE;
|
|
127
|
+ } else if ((cnt > (COMMA_PULSE - DELTA)) && (cnt < (COMMA_PULSE + DELTA))) {
|
|
128
|
+ chEvtBroadcastFlags(&IR_receiver, 0);
|
120
|
129
|
} else {
|
121
|
|
- command = 0;
|
|
130
|
+ /* Long dead pulse nothing to do */
|
122
|
131
|
}
|
123
|
|
- REPEAT_FLAG = FALSE;
|
124
|
|
- START_OCCURED = FALSE;
|
125
|
|
- index = -1;
|
126
|
|
- }
|
127
|
|
- else if((cnt > (RPT_CMD_PULSE - DELTA)) && (cnt < (RPT_CMD_PULSE + DELTA))){
|
128
|
|
- REPEAT_FLAG = TRUE;
|
129
|
|
- }
|
130
|
|
- else if((cnt > (COMMA_PULSE - DELTA)) && (cnt < (COMMA_PULSE + DELTA))){
|
131
|
|
- chEvtBroadcastFlags(&IR_receiver, 0);
|
132
|
|
- }
|
133
|
|
- else{
|
134
|
|
- /* Long dead pulse nothing to do */
|
135
|
|
- }
|
136
|
132
|
}
|
137
|
133
|
|
138
|
134
|
static ICUConfig icucfg = {
|
139
|
|
- ICU_INPUT_ACTIVE_HIGH,
|
140
|
|
- 10000, /* 10kHz ICU clock frequency. */
|
141
|
|
- icuwidthcb,
|
142
|
|
- NULL,
|
143
|
|
- NULL,
|
144
|
|
- ICU_CHANNEL_1,
|
145
|
|
- 0
|
|
135
|
+ ICU_INPUT_ACTIVE_HIGH,
|
|
136
|
+ 10000, /* 10kHz ICU clock frequency. */
|
|
137
|
+ icuwidthcb,
|
|
138
|
+ NULL,
|
|
139
|
+ NULL,
|
|
140
|
+ ICU_CHANNEL_1,
|
|
141
|
+ 0
|
146
|
142
|
};
|
147
|
143
|
|
148
|
|
-/*
|
149
|
|
- * Green LED blinker thread, times are in milliseconds.
|
150
|
|
- */
|
151
|
|
-static THD_WORKING_AREA(waThread1, 128);
|
152
|
|
-static THD_FUNCTION(Thread1, arg) {
|
153
|
|
-
|
154
|
|
- (void)arg;
|
155
|
|
- while (true) {
|
156
|
|
- palClearPad(GPIOC, GPIOC_LED);
|
157
|
|
- chThdSleepMilliseconds(500);
|
158
|
|
- palSetPad(GPIOC, GPIOC_LED);
|
159
|
|
- chThdSleepMilliseconds(500);
|
160
|
|
- }
|
161
|
|
-}
|
162
|
|
-
|
163
|
144
|
/*
|
164
|
145
|
* Synchronously send one bit for RC5
|
165
|
146
|
*/
|
|
@@ -213,23 +194,18 @@ int main(void) {
|
213
|
194
|
halInit();
|
214
|
195
|
chSysInit();
|
215
|
196
|
|
216
|
|
- chEvtObjectInit(&IR_receiver);
|
217
|
|
- chEvtRegister(&IR_receiver, &el, 0);
|
218
|
|
-/*
|
219
|
|
- * Initializes the ICU driver 3.
|
220
|
|
- * GPIOC6 is the ICU input.
|
221
|
|
- * The two pins have to be externally connected together.
|
222
|
|
- */
|
223
|
|
- sdStart(&SD2, NULL);
|
224
|
|
- icuStart(&ICUD3, &icucfg);
|
225
|
|
- palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN);
|
226
|
|
- icuStartCapture(&ICUD3);
|
227
|
|
- icuEnableNotifications(&ICUD3);
|
228
|
|
-
|
|
197
|
+ chEvtObjectInit(&IR_receiver);
|
|
198
|
+ chEvtRegister(&IR_receiver, &el, 0);
|
229
|
199
|
/*
|
230
|
|
- * Creates the blinker thread.
|
|
200
|
+ * Initializes the ICU driver 3.
|
|
201
|
+ * GPIOC6 is the ICU input.
|
|
202
|
+ * The two pins have to be externally connected together.
|
231
|
203
|
*/
|
232
|
|
- //chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
|
|
204
|
+ sdStart(&SD2, NULL);
|
|
205
|
+ icuStart(&ICUD3, &icucfg);
|
|
206
|
+ palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN);
|
|
207
|
+ icuStartCapture(&ICUD3);
|
|
208
|
+ icuEnableNotifications(&ICUD3);
|
233
|
209
|
|
234
|
210
|
/* Configure PWM */
|
235
|
211
|
pwmStart(&PWMD1, &pwmcfg);
|