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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /*
  19. ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
  20. Licensed under the Apache License, Version 2.0 (the "License");
  21. you may not use this file except in compliance with the License.
  22. You may obtain a copy of the License at
  23. http://www.apache.org/licenses/LICENSE-2.0
  24. Unless required by applicable law or agreed to in writing, software
  25. distributed under the License is distributed on an "AS IS" BASIS,
  26. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. See the License for the specific language governing permissions and
  28. limitations under the License.
  29. */
  30. #include <stdbool.h>
  31. #include "ch.h"
  32. #include "hal.h"
  33. /* PWM configuration for IR output */
  34. static PWMConfig pwmcfg = {
  35. 380000, /* 380 kHz PWM clock frequency. */
  36. 10, /* PWM period 10 clock cycles. */
  37. NULL,
  38. {
  39. {PWM_OUTPUT_ACTIVE_HIGH, NULL},
  40. {PWM_OUTPUT_DISABLED, NULL},
  41. {PWM_OUTPUT_DISABLED, NULL},
  42. {PWM_OUTPUT_DISABLED, NULL}
  43. },
  44. 0,
  45. 0
  46. };
  47. /* Timer configuration for IR output */
  48. static const GPTConfig gpt4cfg = {
  49. 1000000, // 1 MHz timer clock.
  50. NULL, // No callback
  51. 0, 0
  52. };
  53. /*
  54. * Green LED blinker thread, times are in milliseconds.
  55. */
  56. static THD_WORKING_AREA(waThread1, 128);
  57. static THD_FUNCTION(Thread1, arg) {
  58. (void)arg;
  59. while (true) {
  60. palClearPad(GPIOC, GPIOC_LED);
  61. chThdSleepMilliseconds(500);
  62. palSetPad(GPIOC, GPIOC_LED);
  63. chThdSleepMilliseconds(500);
  64. }
  65. }
  66. /*
  67. * Synchronously send one bit for RC5
  68. */
  69. void rc5_bit(bool bit)
  70. {
  71. if (bit) {
  72. pwmDisableChannel(&PWMD1, 0);
  73. gptPolledDelay(&GPTD4, 889);
  74. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  75. gptPolledDelay(&GPTD4, 889);
  76. } else {
  77. pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
  78. gptPolledDelay(&GPTD4, 889);
  79. pwmDisableChannel(&PWMD1, 0);
  80. gptPolledDelay(&GPTD4, 889);
  81. }
  82. }
  83. /*
  84. * Synchronously send a whole RC5 frame
  85. */
  86. void rc5_frame(bool toggle, int addr, int data)
  87. {
  88. /* Start */
  89. rc5_bit(1);
  90. rc5_bit(1);
  91. /* Toggle */
  92. rc5_bit(toggle);
  93. /* Address */
  94. for (int b = 0x10; b != 0; b >>= 1) {
  95. rc5_bit(addr & b);
  96. }
  97. /* Data */
  98. for (int b = 0x20; b != 0; b >>= 1) {
  99. rc5_bit(data & b);
  100. }
  101. }
  102. /*
  103. * Application entry point.
  104. */
  105. int main(void) {
  106. /*
  107. * System initializations.
  108. * - HAL initialization, this also initializes the configured device drivers
  109. * and performs the board-specific initializations.
  110. * - Kernel initialization, the main() function becomes a thread and the
  111. * RTOS is active.
  112. */
  113. halInit();
  114. chSysInit();
  115. /*
  116. * Creates the blinker thread.
  117. */
  118. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  119. /* Configure PWM */
  120. pwmStart(&PWMD1, &pwmcfg);
  121. palSetPadMode(GPIOA, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  122. /* Configure GPT */
  123. gptStart(&GPTD4, &gpt4cfg);
  124. /* Send a continuous stream of power off signals to the TV */
  125. while (true) {
  126. rc5_frame(false, 0x00, 0x0C);
  127. chThdSleepMilliseconds(90);
  128. }
  129. }