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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. static event_listener_t el;
  48. static event_source_t IR_receiver;
  49. /*===========================================================================*/
  50. /* IR remote related code. */
  51. /*===========================================================================*/
  52. #define DELTA 5
  53. #define START_PULSE 45
  54. #define END_PULSE 405
  55. #define RPT_CMD_PULSE 959
  56. #define ZERO_PULSE 6
  57. #define ONE_PULSE 17
  58. #define COMMA_PULSE 23
  59. /* PWM configuration for IR output */
  60. static PWMConfig pwmcfg = {
  61. 380000, /* 380 kHz PWM clock frequency. */
  62. 10, /* PWM period 10 clock cycles. */
  63. NULL,
  64. {
  65. {PWM_OUTPUT_ACTIVE_HIGH, NULL},
  66. {PWM_OUTPUT_DISABLED, NULL},
  67. {PWM_OUTPUT_DISABLED, NULL},
  68. {PWM_OUTPUT_DISABLED, NULL}
  69. },
  70. 0,
  71. 0
  72. };
  73. /* Timer configuration for IR output */
  74. static const GPTConfig gpt4cfg = {
  75. 1000000, // 1 MHz timer clock.
  76. NULL, // No callback
  77. 0, 0
  78. };
  79. /*===========================================================================*/
  80. /* ICU related code. */
  81. /*===========================================================================*/
  82. static int32_t index = -1;
  83. static bool START_OCCURED = FALSE, REPEAT_FLAG = FALSE;
  84. static uint32_t tmp, command;
  85. static void icuwidthcb(ICUDriver *icup) {
  86. icucnt_t cnt = icuGetWidthX(icup);
  87. if((cnt > (START_PULSE - DELTA)) && (cnt < (START_PULSE + DELTA))){
  88. index = 0;
  89. START_OCCURED = TRUE;
  90. }
  91. else if((cnt > (ONE_PULSE - DELTA)) && (cnt < (ONE_PULSE + DELTA))){
  92. tmp |= 1 << (31 - index);
  93. index++;
  94. }
  95. else if((cnt > (ZERO_PULSE - DELTA)) && (cnt < (ZERO_PULSE + DELTA))){
  96. tmp &= ~(1 << (31 - index));
  97. index++;
  98. }
  99. else if((cnt > (END_PULSE - 4*DELTA)) && (cnt < (END_PULSE + 4*DELTA))){
  100. if((START_OCCURED) && (index == 32)) {
  101. command = tmp;
  102. } else {
  103. command = 0;
  104. }
  105. REPEAT_FLAG = FALSE;
  106. START_OCCURED = FALSE;
  107. index = -1;
  108. }
  109. else if((cnt > (RPT_CMD_PULSE - DELTA)) && (cnt < (RPT_CMD_PULSE + DELTA))){
  110. REPEAT_FLAG = TRUE;
  111. }
  112. else if((cnt > (COMMA_PULSE - DELTA)) && (cnt < (COMMA_PULSE + DELTA))){
  113. chEvtBroadcastFlags(&IR_receiver, 0);
  114. }
  115. else{
  116. /* Long dead pulse nothing to do */
  117. }
  118. }
  119. static ICUConfig icucfg = {
  120. ICU_INPUT_ACTIVE_HIGH,
  121. 10000, /* 10kHz ICU clock frequency. */
  122. icuwidthcb,
  123. NULL,
  124. NULL,
  125. ICU_CHANNEL_1,
  126. 0
  127. };
  128. /*
  129. * Green LED blinker thread, times are in milliseconds.
  130. */
  131. static THD_WORKING_AREA(waThread1, 128);
  132. static THD_FUNCTION(Thread1, arg) {
  133. (void)arg;
  134. while (true) {
  135. palClearPad(GPIOC, GPIOC_LED);
  136. chThdSleepMilliseconds(500);
  137. palSetPad(GPIOC, GPIOC_LED);
  138. chThdSleepMilliseconds(500);
  139. }
  140. }
  141. /*
  142. * Synchronously send one bit for RC5
  143. */
  144. void rc5_bit(bool bit)
  145. {
  146. if (bit) {
  147. pwmDisableChannel(&PWMD1, 0);
  148. gptPolledDelay(&GPTD4, 889);
  149. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  150. gptPolledDelay(&GPTD4, 889);
  151. } else {
  152. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  153. gptPolledDelay(&GPTD4, 889);
  154. pwmDisableChannel(&PWMD1, 0);
  155. gptPolledDelay(&GPTD4, 889);
  156. }
  157. }
  158. /*
  159. * Synchronously send a whole RC5 frame
  160. */
  161. void rc5_frame(bool toggle, int addr, int data)
  162. {
  163. /* Start */
  164. rc5_bit(1);
  165. rc5_bit(1);
  166. /* Toggle */
  167. rc5_bit(toggle);
  168. /* Address */
  169. for (int b = 0x10; b != 0; b >>= 1) {
  170. rc5_bit(addr & b);
  171. }
  172. /* Data */
  173. for (int b = 0x20; b != 0; b >>= 1) {
  174. rc5_bit(data & b);
  175. }
  176. }
  177. /*
  178. * Application entry point.
  179. */
  180. int main(void) {
  181. /*
  182. * System initializations.
  183. * - HAL initialization, this also initializes the configured device drivers
  184. * and performs the board-specific initializations.
  185. * - Kernel initialization, the main() function becomes a thread and the
  186. * RTOS is active.
  187. */
  188. halInit();
  189. chSysInit();
  190. chEvtObjectInit(&IR_receiver);
  191. chEvtRegister(&IR_receiver, &el, 0);
  192. /*
  193. * Initializes the ICU driver 3.
  194. * GPIOC6 is the ICU input.
  195. * The two pins have to be externally connected together.
  196. */
  197. sdStart(&SD2, NULL);
  198. icuStart(&ICUD3, &icucfg);
  199. palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN);
  200. icuStartCapture(&ICUD3);
  201. icuEnableNotifications(&ICUD3);
  202. /*
  203. * Creates the blinker thread.
  204. */
  205. //chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  206. /* Configure PWM */
  207. pwmStart(&PWMD1, &pwmcfg);
  208. palSetPadMode(GPIOA, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  209. /* Configure GPT */
  210. gptStart(&GPTD4, &gpt4cfg);
  211. /* Send a continuous stream of power off signals to the TV */
  212. while (true) {
  213. if (command == 0x807F18E7) {
  214. if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(200))) {
  215. continue;
  216. }
  217. palClearPad(GPIOC, GPIOC_LED);
  218. for (int i = 0; i < 3; i++) {
  219. rc5_frame(false, 0x00, 0x0C);
  220. chThdSleepMilliseconds(90);
  221. }
  222. palSetPad(GPIOC, GPIOC_LED);
  223. }
  224. chEvtWaitAny(ALL_EVENTS);
  225. }
  226. }