123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- /*
- * 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/>.
- */
-
- /* Portions subject to the following copyright notices */
-
- /*
- 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.
- */
-
- /*
- PLAY Embedded demos - Copyright (C) 2014-2017 Rocco Marco Guglielmi
-
- This file is part of PLAY Embedded demos.
-
- 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"
-
- static event_listener_t el;
- static event_source_t IR_receiver;
- /*===========================================================================*/
- /* IR remote related code. */
- /*===========================================================================*/
-
- #define DELTA 5
- #define START_PULSE 45
- #define END_PULSE 405
- #define RPT_CMD_PULSE 959
- #define ZERO_PULSE 6
- #define ONE_PULSE 17
- #define COMMA_PULSE 23
-
- /* 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
- };
-
- /*===========================================================================*/
- /* ICU related code. */
- /*===========================================================================*/
- static int32_t index = -1;
- static bool START_OCCURED = FALSE, REPEAT_FLAG = FALSE;
- static uint32_t tmp, command;
- static void icuwidthcb(ICUDriver *icup) {
-
- icucnt_t cnt = icuGetWidthX(icup);
- if((cnt > (START_PULSE - DELTA)) && (cnt < (START_PULSE + DELTA))){
- index = 0;
- START_OCCURED = TRUE;
- }
- else if((cnt > (ONE_PULSE - DELTA)) && (cnt < (ONE_PULSE + DELTA))){
- tmp |= 1 << (31 - index);
- index++;
- }
- else if((cnt > (ZERO_PULSE - DELTA)) && (cnt < (ZERO_PULSE + DELTA))){
- tmp &= ~(1 << (31 - index));
- index++;
- }
- else if((cnt > (END_PULSE - 4*DELTA)) && (cnt < (END_PULSE + 4*DELTA))){
- if((START_OCCURED) && (index == 32)) {
- command = tmp;
- } else {
- command = 0;
- }
- REPEAT_FLAG = FALSE;
- START_OCCURED = FALSE;
- index = -1;
- }
- else if((cnt > (RPT_CMD_PULSE - DELTA)) && (cnt < (RPT_CMD_PULSE + DELTA))){
- REPEAT_FLAG = TRUE;
- }
- else if((cnt > (COMMA_PULSE - DELTA)) && (cnt < (COMMA_PULSE + DELTA))){
- chEvtBroadcastFlags(&IR_receiver, 0);
- }
- else{
- /* Long dead pulse nothing to do */
- }
- }
-
- static ICUConfig icucfg = {
- ICU_INPUT_ACTIVE_HIGH,
- 10000, /* 10kHz ICU clock frequency. */
- icuwidthcb,
- NULL,
- NULL,
- ICU_CHANNEL_1,
- 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();
-
- chEvtObjectInit(&IR_receiver);
- chEvtRegister(&IR_receiver, &el, 0);
- /*
- * Initializes the ICU driver 3.
- * GPIOC6 is the ICU input.
- * The two pins have to be externally connected together.
- */
- sdStart(&SD2, NULL);
- icuStart(&ICUD3, &icucfg);
- palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN);
- icuStartCapture(&ICUD3);
- icuEnableNotifications(&ICUD3);
-
- /*
- * 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) {
- if (command == 0x807F18E7) {
- if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(200))) {
- continue;
- }
- palClearPad(GPIOC, GPIOC_LED);
- for (int i = 0; i < 3; i++) {
- rc5_frame(false, 0x00, 0x0C);
- chThdSleepMilliseconds(90);
- }
- palSetPad(GPIOC, GPIOC_LED);
- }
- chEvtWaitAny(ALL_EVENTS);
- }
- }
|