123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- /*
- * Remote Control Translator Firmware
- * Copyright (C) 2017 Clayton G. Hobbs <clay@lakeserv.net>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
- /*
- ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
-
- #include <stdbool.h>
-
- #include "ch.h"
- #include "hal.h"
-
- /* PWM configuration for IR output */
- static PWMConfig pwmcfg = {
- 380000, /* 380 kHz PWM clock frequency. */
- 10, /* PWM period 10 clock cycles. */
- NULL,
- {
- {PWM_OUTPUT_ACTIVE_HIGH, NULL},
- {PWM_OUTPUT_DISABLED, NULL},
- {PWM_OUTPUT_DISABLED, NULL},
- {PWM_OUTPUT_DISABLED, NULL}
- },
- 0,
- 0
- };
-
- /* Timer configuration for IR output */
- static const GPTConfig gpt4cfg = {
- 1000000, // 1 MHz timer clock.
- NULL, // No callback
- 0, 0
- };
-
- /*
- * Green LED blinker thread, times are in milliseconds.
- */
- static THD_WORKING_AREA(waThread1, 128);
- static THD_FUNCTION(Thread1, arg) {
-
- (void)arg;
- while (true) {
- palClearPad(GPIOC, GPIOC_LED);
- chThdSleepMilliseconds(500);
- palSetPad(GPIOC, GPIOC_LED);
- chThdSleepMilliseconds(500);
- }
- }
-
- /*
- * Synchronously send one bit for RC5
- */
- void rc5_bit(bool bit)
- {
- if (bit) {
- pwmDisableChannel(&PWMD1, 0);
- gptPolledDelay(&GPTD4, 889);
- pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
- gptPolledDelay(&GPTD4, 889);
- } else {
- pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
- gptPolledDelay(&GPTD4, 889);
- pwmDisableChannel(&PWMD1, 0);
- gptPolledDelay(&GPTD4, 889);
- }
- }
-
- /*
- * Synchronously send a whole RC5 frame
- */
- void rc5_frame(bool toggle, int addr, int data)
- {
- /* Start */
- rc5_bit(1);
- rc5_bit(1);
- /* Toggle */
- rc5_bit(toggle);
- /* Address */
- for (int b = 0x10; b != 0; b >>= 1) {
- rc5_bit(addr & b);
- }
- /* Data */
- for (int b = 0x20; b != 0; b >>= 1) {
- rc5_bit(data & b);
- }
- }
-
- /*
- * Application entry point.
- */
- int main(void) {
-
- /*
- * System initializations.
- * - HAL initialization, this also initializes the configured device drivers
- * and performs the board-specific initializations.
- * - Kernel initialization, the main() function becomes a thread and the
- * RTOS is active.
- */
- halInit();
- chSysInit();
-
- /*
- * Creates the blinker thread.
- */
- chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
-
- /* Configure PWM */
- pwmStart(&PWMD1, &pwmcfg);
- palSetPadMode(GPIOA, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
-
- /* Configure GPT */
- gptStart(&GPTD4, &gpt4cfg);
-
- /* Send a continuous stream of power off signals to the TV */
- while (true) {
- rc5_frame(false, 0x00, 0x0C);
-
- chThdSleepMilliseconds(90);
- }
- }
|