/* * Remote Control Translator Firmware * Copyright (C) 2017 Clayton G. Hobbs * * 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 . */ /* 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 #include "ch.h" #include "hal.h" /* * RC5 timing macros (microseconds, unless otherwise specified) */ #define RC5_PULSE_LENGTH 889 #define RC5_CODE_SPACE_MS 90 /* Milliseconds */ /* * NEC timing macros (tenths of a millisecond) */ #define NEC_START_PULSE 45 #define NEC_END_PULSE 405 #define NEC_RPT_CMD_PULSE 962 #define NEC_ZERO_PULSE 6 #define NEC_ONE_PULSE 17 #define NEC_COMMA_PULSE 23 /* Tolerance */ #define NEC_DELTA 5 #define CLOSE_TO(var, target, error) (((var) > ((target) - (error))) && ((var) < ((target) + (error)))) static event_listener_t el; static event_source_t IR_receiver; /* 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 }; /* * Variables for holding the most recently read information from nec_icuwidthcb */ static uint8_t nec_addr, nec_data; static bool nec_repeat = false; /* * ICU pulse width callback for reading NEC remote control frames */ static void nec_icuwidthcb(ICUDriver *icup) { static int32_t index = -1; static bool start_occured = false; static uint32_t tmp; icucnt_t cnt = icuGetWidthX(icup); if (CLOSE_TO(cnt, NEC_START_PULSE, NEC_DELTA)) { index = 0; start_occured = true; } else if (CLOSE_TO(cnt, NEC_ONE_PULSE, NEC_DELTA)) { if (index > -1) { tmp |= 1 << (31 - index); index++; } } else if (CLOSE_TO(cnt, NEC_ZERO_PULSE, NEC_DELTA)) { if (index > -1) { tmp &= ~(1 << (31 - index)); index++; } } else if (CLOSE_TO(cnt, NEC_END_PULSE, 4*NEC_DELTA)) { /* Nothing to do here */ } else if (CLOSE_TO(cnt, NEC_RPT_CMD_PULSE, 9*NEC_DELTA)) { /* Nothing to do here */ } else if (CLOSE_TO(cnt, NEC_COMMA_PULSE, NEC_DELTA)) { nec_repeat = true; chEvtBroadcastFlags(&IR_receiver, 0); } else { /* When an unknown symbol is seen, forget what we've read */ nec_addr = 0; nec_data = 0; nec_repeat = false; start_occured = false; index = -1; } /* If we've read a whole message, send it on */ if (start_occured && index == 32) { nec_addr = (tmp >> 24) & 0xFF; nec_data = (tmp >> 8) & 0xFF; nec_repeat = false; start_occured = false; index = -1; chEvtBroadcastFlags(&IR_receiver, 0); } } /* Configuration for the ICU */ static ICUConfig icucfg = { ICU_INPUT_ACTIVE_HIGH, 10000, /* 10kHz ICU clock frequency. */ nec_icuwidthcb, NULL, NULL, ICU_CHANNEL_1, 0 }; /* * Synchronously send one bit for RC5 */ void rc5_bit(bool bit) { if (bit) { pwmDisableChannel(&PWMD1, 0); gptPolledDelay(&GPTD4, RC5_PULSE_LENGTH); pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000)); gptPolledDelay(&GPTD4, RC5_PULSE_LENGTH); } else { pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000)); gptPolledDelay(&GPTD4, RC5_PULSE_LENGTH); pwmDisableChannel(&PWMD1, 0); gptPolledDelay(&GPTD4, RC5_PULSE_LENGTH); } } /* * 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); /* Configure PWM */ pwmStart(&PWMD1, &pwmcfg); palSetPadMode(GPIOA, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL); /* Configure GPT */ gptStart(&GPTD4, &gpt4cfg); while (true) { /* Wait for a signal */ chEvtWaitAny(ALL_EVENTS); /* If an NEC power signal was received */ if (nec_addr == 0x80 && nec_data == 0x18) { /* Turn on the LED */ palClearPad(GPIOC, GPIOC_LED); /* Wait until further codes stop arriving to avoid interfering */ while (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(200))) ; /* Send the RC5 power signal */ for (int i = 0; i < 3; i++) { rc5_frame(false, 0x00, 0x0C); chThdSleepMilliseconds(RC5_CODE_SPACE_MS); } /* Turn off the LED */ palSetPad(GPIOC, GPIOC_LED); } } }