Firmware for the remote control translator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Remote Control Translator Firmware
  3. * Copyright (C) 2017 Clayton G. Hobbs <clay@lakeserv.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* Portions subject to the following copyright notices */
  19. /*
  20. ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
  21. Licensed under the Apache License, Version 2.0 (the "License");
  22. you may not use this file except in compliance with the License.
  23. You may obtain a copy of the License at
  24. http://www.apache.org/licenses/LICENSE-2.0
  25. Unless required by applicable law or agreed to in writing, software
  26. distributed under the License is distributed on an "AS IS" BASIS,
  27. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. See the License for the specific language governing permissions and
  29. limitations under the License.
  30. */
  31. /*
  32. PLAY Embedded demos - Copyright (C) 2014-2017 Rocco Marco Guglielmi
  33. This file is part of PLAY Embedded demos.
  34. Licensed under the Apache License, Version 2.0 (the "License");
  35. you may not use this file except in compliance with the License.
  36. You may obtain a copy of the License at
  37. http://www.apache.org/licenses/LICENSE-2.0
  38. Unless required by applicable law or agreed to in writing, software
  39. distributed under the License is distributed on an "AS IS" BASIS,
  40. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  41. See the License for the specific language governing permissions and
  42. limitations under the License.
  43. */
  44. #include <stdbool.h>
  45. #include "ch.h"
  46. #include "hal.h"
  47. /*
  48. * RC5 timing macros (microseconds, unless otherwise specified)
  49. */
  50. #define RC5_PULSE_LENGTH 889
  51. #define RC5_CODE_SPACE_MS 90 /* Milliseconds */
  52. /*
  53. * NEC timing macros (tenths of a millisecond)
  54. */
  55. #define NEC_START_PULSE 45
  56. #define NEC_END_PULSE 405
  57. #define NEC_RPT_CMD_PULSE 962
  58. #define NEC_ZERO_PULSE 6
  59. #define NEC_ONE_PULSE 17
  60. #define NEC_COMMA_PULSE 23
  61. /* Tolerance */
  62. #define NEC_DELTA 5
  63. #define CLOSE_TO(var, target, error) (((var) > ((target) - (error))) && ((var) < ((target) + (error))))
  64. static event_listener_t el;
  65. static event_source_t IR_receiver;
  66. /* PWM configuration for IR output */
  67. static PWMConfig pwmcfg = {
  68. 380000, /* 380 kHz PWM clock frequency */
  69. 10, /* PWM period 10 clock cycles */
  70. NULL,
  71. {
  72. {PWM_OUTPUT_ACTIVE_HIGH, NULL},
  73. {PWM_OUTPUT_DISABLED, NULL},
  74. {PWM_OUTPUT_DISABLED, NULL},
  75. {PWM_OUTPUT_DISABLED, NULL}
  76. },
  77. 0,
  78. 0
  79. };
  80. /* Timer configuration for IR output */
  81. static const GPTConfig gpt4cfg = {
  82. 1000000, /* 1 MHz timer clock */
  83. NULL, /* No callback */
  84. 0, 0
  85. };
  86. /*
  87. * Variables for holding the most recently read information from nec_icuwidthcb
  88. */
  89. static uint8_t nec_addr, nec_data;
  90. static bool nec_repeat = false;
  91. /*
  92. * ICU pulse width callback for reading NEC remote control frames
  93. */
  94. static void nec_icuwidthcb(ICUDriver *icup)
  95. {
  96. static int32_t index = -1;
  97. static bool start_occured = false;
  98. static uint32_t tmp;
  99. icucnt_t cnt = icuGetWidthX(icup);
  100. if (CLOSE_TO(cnt, NEC_START_PULSE, NEC_DELTA)) {
  101. index = 0;
  102. start_occured = true;
  103. } else if (CLOSE_TO(cnt, NEC_ONE_PULSE, NEC_DELTA)) {
  104. if (index > -1) {
  105. tmp |= 1 << (31 - index);
  106. index++;
  107. }
  108. } else if (CLOSE_TO(cnt, NEC_ZERO_PULSE, NEC_DELTA)) {
  109. if (index > -1) {
  110. tmp &= ~(1 << (31 - index));
  111. index++;
  112. }
  113. } else if (CLOSE_TO(cnt, NEC_END_PULSE, 4*NEC_DELTA)) {
  114. /* Nothing to do here */
  115. } else if (CLOSE_TO(cnt, NEC_RPT_CMD_PULSE, 9*NEC_DELTA)) {
  116. /* Nothing to do here */
  117. } else if (CLOSE_TO(cnt, NEC_COMMA_PULSE, NEC_DELTA)) {
  118. nec_repeat = true;
  119. chEvtBroadcastFlags(&IR_receiver, 0);
  120. } else {
  121. /* When an unknown symbol is seen, forget what we've read */
  122. nec_addr = 0;
  123. nec_data = 0;
  124. nec_repeat = false;
  125. start_occured = false;
  126. index = -1;
  127. }
  128. /* If we've read a whole message, send it on */
  129. if (start_occured && index == 32) {
  130. nec_addr = (tmp >> 24) & 0xFF;
  131. nec_data = (tmp >> 8) & 0xFF;
  132. nec_repeat = false;
  133. start_occured = false;
  134. index = -1;
  135. chEvtBroadcastFlags(&IR_receiver, 0);
  136. }
  137. }
  138. /* Configuration for the ICU */
  139. static ICUConfig icucfg = {
  140. ICU_INPUT_ACTIVE_HIGH,
  141. 10000, /* 10kHz ICU clock frequency. */
  142. nec_icuwidthcb,
  143. NULL,
  144. NULL,
  145. ICU_CHANNEL_1,
  146. 0
  147. };
  148. /*
  149. * Synchronously send one bit for RC5
  150. */
  151. void rc5_bit(bool bit)
  152. {
  153. if (bit) {
  154. pwmDisableChannel(&PWMD1, 0);
  155. gptPolledDelay(&GPTD4, RC5_PULSE_LENGTH);
  156. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  157. gptPolledDelay(&GPTD4, RC5_PULSE_LENGTH);
  158. } else {
  159. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  160. gptPolledDelay(&GPTD4, RC5_PULSE_LENGTH);
  161. pwmDisableChannel(&PWMD1, 0);
  162. gptPolledDelay(&GPTD4, RC5_PULSE_LENGTH);
  163. }
  164. }
  165. /*
  166. * Synchronously send a whole RC5 frame
  167. */
  168. void rc5_frame(bool toggle, int addr, int data)
  169. {
  170. /* Start */
  171. rc5_bit(1);
  172. rc5_bit(1);
  173. /* Toggle */
  174. rc5_bit(toggle);
  175. /* Address */
  176. for (int b = 0x10; b != 0; b >>= 1) {
  177. rc5_bit(addr & b);
  178. }
  179. /* Data */
  180. for (int b = 0x20; b != 0; b >>= 1) {
  181. rc5_bit(data & b);
  182. }
  183. }
  184. /*
  185. * Application entry point.
  186. */
  187. int main(void)
  188. {
  189. /*
  190. * System initializations.
  191. * - HAL initialization, this also initializes the configured device drivers
  192. * and performs the board-specific initializations.
  193. * - Kernel initialization, the main() function becomes a thread and the
  194. * RTOS is active.
  195. */
  196. halInit();
  197. chSysInit();
  198. chEvtObjectInit(&IR_receiver);
  199. chEvtRegister(&IR_receiver, &el, 0);
  200. /*
  201. * Initializes the ICU driver 3.
  202. * GPIOC6 is the ICU input.
  203. * The two pins have to be externally connected together.
  204. */
  205. sdStart(&SD2, NULL);
  206. icuStart(&ICUD3, &icucfg);
  207. palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN);
  208. icuStartCapture(&ICUD3);
  209. icuEnableNotifications(&ICUD3);
  210. /* Configure PWM */
  211. pwmStart(&PWMD1, &pwmcfg);
  212. palSetPadMode(GPIOA, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  213. /* Configure GPT */
  214. gptStart(&GPTD4, &gpt4cfg);
  215. while (true) {
  216. /* Wait for a signal */
  217. chEvtWaitAny(ALL_EVENTS);
  218. /* If an NEC power signal was received */
  219. if (nec_addr == 0x80 && nec_data == 0x18) {
  220. /* Turn on the LED */
  221. palClearPad(GPIOC, GPIOC_LED);
  222. /* Wait until further codes stop arriving to avoid interfering */
  223. while (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(200)))
  224. ;
  225. /* Send the RC5 power signal */
  226. for (int i = 0; i < 3; i++) {
  227. rc5_frame(false, 0x00, 0x0C);
  228. chThdSleepMilliseconds(RC5_CODE_SPACE_MS);
  229. }
  230. /* Turn off the LED */
  231. palSetPad(GPIOC, GPIOC_LED);
  232. }
  233. }
  234. }