<commit_before>/*----------------------------------------------------------------------------/
/  IR_CTRL - IR remote control module  R0.01                  (C)ChaN, 2008
/-----------------------------------------------------------------------------/
/ The IR_CTRL is a generic Transmisson/Reception control module for IR remote
/ control systems. This is a free software and is opened for education,
/ research and development under license policy of following trems.
/
/  Copyright (C) 2008, ChaN, all right reserved.
/
/ * The IR_CTRL module is a free software and there is no warranty.
/ * You can use, modify and/or redistribute it for personal, non-profit or
/   commercial use without restriction under your responsibility.
/ * Redistributions of source code must retain the above copyright notice.
/
/-----------------------------------------------------------------------------/
/ Aug 30,'08 R0.01  First release.
/----------------------------------------------------------------------------*/

#include "IrCtrl.h"

/*----------------------------------------------------------------------------/
/ Platform dependent definitions
/----------------------------------------------------------------------------*/
/* Define interrupt service functions */
#define ISR_COMPARE()   ISR(TIMER1_COMPA_vect)  /* Timer compare match ISR */
#define ISR_CAPTURE()   ISR(TIMER1_CAPT_vect)   /* Rx: Timer input capture ISR */

/* Define hardware control macros */
#define IR_INIT_TIMER()                         /* Initialize Timer (Timer1 for transmission/reception timing: Free running, clk/8) */ \
  TCCR1B = 0b10000010; \
  TCCR1A = 0b00000000
/*
  TCCR1B
    0bxxxxx001 = clk/   1
    0bxxxxx010 = clk/   8 *
    0bxxxxx011 = clk/  64
    0bxxxxx100 = clk/ 256
    0bxxxxx110 = clk/1024
*/

#define IR_INIT_XMIT()                          /* Tx: Initialize Transmitter (Timer2 for IR subcarrier: Fast PWM, clk/8) */ \
    OCR2B = 16; \
    TCCR2A = _BV(WGM21)|_BV(WGM20); \
    TCCR2B = _BV(WGM22)|0b010
/*
[sample(mega48 10MHz)]
  OCR2B  = 10
  TCCR2A = _BV(WGM21)|_BV(WGM20)
  TCCR2B = _BV(WGM22)|0b010
  IR_TX_38K() OCR2A = 32
  IR_TX_40K() OCR2A = 30

  WGM2[111] = fast PWM
  0b010     = clk/8
  Counting BOTTOM to TOP(=OCR2A), At BOTTOM set 1, At OCR2B set 0.
  Width of 1 is OCR2B=10, fixed. Width of 0 is OCR2A-OCR2B. Freq is defined by OCR2A.

   10MHz/8 = 1.25MHz
  OCR2A=33 : 1.25MHz/(33+1) =  36.76KHz
  OCR2A=32 : 1.25MHz/(32+1) =  37.87KHz * 38K
  OCR2A=31 : 1.25MHz/(31+1) =  39.06KHz
  OCR2A=30 : 1.25MHz/(30+1) =  40.32KHz * 40K
  OCR2A=29 : 1.25MHz/(29+1) =  41.66KHz

[Arduino2009+mega328 16MHz]
  10MHz/8  = 2.00MHz
  OCR2A=53 : 2.00MHz/(53+1) =  37.03KHz
  OCR2A=52 : 2.00MHz/(52+1) =  37.73KHz * 38K
  OCR2A=51 : 2.00MHz/(51+1) =  38.46KHz
  OCR2A=50 : 2.00MHz/(50+1) =  39.21KHz
  OCR2A=49 : 2.00MHz/(49+1) =  40.00KHz * 40K
  OCR2B = 16
*/

#define IR_TX_38K()     OCR2A = 52; TCNT2 = 0   /* Tx: Set IR burst frequency to 38kHz */
#define IR_TX_40K()     OCR2A = 49; TCNT2 = 0   /* Tx: Set IR burst frequency to 40kHz */
#define IR_TX_ON()      TCCR2A |=  _BV(COM2B1)  /* Tx: Start IR burst */
#define IR_TX_OFF()     TCCR2A &= ~_BV(COM2B1)  /* Tx: Stop IR burst */
#define IR_TX_TEST()    TCCR2A &   _BV(COM2B1)  /* Tx: Check if IR is being transmitted or not */

#define IR_CAPT_TEST()  TCCR1B &   _BV(ICES1)   /* Rx: Check which edge generated the capture interrupt */
#define IR_CAPT_RISE()  TCCR1B |=  _BV(ICES1)   /* Rx: Set captureing is triggered on rising edge */
#define IR_CAPT_FALL()  TCCR1B &= ~_BV(ICES1)   /* Rx: Set captureing is triggered on falling edge */
#define IR_CAPT_ENA()   TIFR1 = _BV(ICF1); TIMSK1 = _BV(ICIE1)  /* Rx: Enable captureing interrupt */
#define IR_CAPT_REG()   ICR1                    /* Rx: Returns the value in capture register */
#define IR_CAPT_DIS()   TIMSK1 &= ~_BV(ICIE1)   /* Tx && Rx: Disable captureing interrupt */

#define IR_COMP_ENA(n)  OCR1A = TCNT1 + (n); TIFR1 = _BV(OCF1A); TIMSK1 |= _BV(OCIE1A) /* Enable compare interrupt n count after now */
#define IR_COMP_DIS()   TIMSK1 &= ~_BV(OCIE1A)  /* Disable compare interrupt */
#define IR_COMP_NEXT(n) OCR1A += (n)            /* Tx: Increase compare register by n count */

/* Counter clock rate and register width */
#define T_CLK           500                     /* Timer tick period [ns] */
/*
[sample 10MHz]
  T_CLK = 800
  1s = 1000ms = 1000000us = 1000000000ns
  1M = 1000K  = 1000000
  10MHz   => 1000000000ns/ 10000000    = 100ns
  10MHz/8 => 1000000000ns/(10000000/8) = 800ns
[Arduino 16MHz]
  16MHz/8 => 1000000000ns/(16000000/8) = 500ns
*/
#define _tmr_t          uint16_t                /* Integer type of timer register */
/*---------------------------------------------------------------------------*/

/* IR control timings */<commit_msg>make T_CLK valid for 8MHz system<commit_after>/*----------------------------------------------------------------------------/
/  IR_CTRL - IR remote control module  R0.01                  (C)ChaN, 2008
/-----------------------------------------------------------------------------/
/ The IR_CTRL is a generic Transmisson/Reception control module for IR remote
/ control systems. This is a free software and is opened for education,
/ research and development under license policy of following trems.
/
/  Copyright (C) 2008, ChaN, all right reserved.
/
/ * The IR_CTRL module is a free software and there is no warranty.
/ * You can use, modify and/or redistribute it for personal, non-profit or
/   commercial use without restriction under your responsibility.
/ * Redistributions of source code must retain the above copyright notice.
/
/-----------------------------------------------------------------------------/
/ Aug 30,'08 R0.01  First release.
/----------------------------------------------------------------------------*/

#include "IrCtrl.h"

/*----------------------------------------------------------------------------/
/ Platform dependent definitions
/----------------------------------------------------------------------------*/
/* Define interrupt service functions */
#define ISR_COMPARE()   ISR(TIMER1_COMPA_vect)  /* Timer compare match ISR */
#define ISR_CAPTURE()   ISR(TIMER1_CAPT_vect)   /* Rx: Timer input capture ISR */

/* Define hardware control macros */
#define IR_INIT_TIMER()                         /* Initialize Timer (Timer1 for transmission/reception timing: Free running, clk/8) */ \
  TCCR1B = 0b10000010; \
  TCCR1A = 0b00000000
/*
  TCCR1B
    0bxxxxx001 = clk/   1
    0bxxxxx010 = clk/   8 *
    0bxxxxx011 = clk/  64
    0bxxxxx100 = clk/ 256
    0bxxxxx110 = clk/1024
*/

#define IR_INIT_XMIT()                          /* Tx: Initialize Transmitter (Timer2 for IR subcarrier: Fast PWM, clk/8) */ \
    OCR2B = 16; \
    TCCR2A = _BV(WGM21)|_BV(WGM20); \
    TCCR2B = _BV(WGM22)|0b010
/*
[sample(mega48 10MHz)]
  OCR2B  = 10
  TCCR2A = _BV(WGM21)|_BV(WGM20)
  TCCR2B = _BV(WGM22)|0b010
  IR_TX_38K() OCR2A = 32
  IR_TX_40K() OCR2A = 30

  WGM2[111] = fast PWM
  0b010     = clk/8
  Counting BOTTOM to TOP(=OCR2A), At BOTTOM set 1, At OCR2B set 0.
  Width of 1 is OCR2B=10, fixed. Width of 0 is OCR2A-OCR2B. Freq is defined by OCR2A.

   10MHz/8 = 1.25MHz
  OCR2A=33 : 1.25MHz/(33+1) =  36.76KHz
  OCR2A=32 : 1.25MHz/(32+1) =  37.87KHz * 38K
  OCR2A=31 : 1.25MHz/(31+1) =  39.06KHz
  OCR2A=30 : 1.25MHz/(30+1) =  40.32KHz * 40K
  OCR2A=29 : 1.25MHz/(29+1) =  41.66KHz

[Arduino2009+mega328 16MHz]
  10MHz/8  = 2.00MHz
  OCR2A=53 : 2.00MHz/(53+1) =  37.03KHz
  OCR2A=52 : 2.00MHz/(52+1) =  37.73KHz * 38K
  OCR2A=51 : 2.00MHz/(51+1) =  38.46KHz
  OCR2A=50 : 2.00MHz/(50+1) =  39.21KHz
  OCR2A=49 : 2.00MHz/(49+1) =  40.00KHz * 40K
  OCR2B = 16
*/

#define IR_TX_38K()     OCR2A = 52; TCNT2 = 0   /* Tx: Set IR burst frequency to 38kHz */
#define IR_TX_40K()     OCR2A = 49; TCNT2 = 0   /* Tx: Set IR burst frequency to 40kHz */
#define IR_TX_ON()      TCCR2A |=  _BV(COM2B1)  /* Tx: Start IR burst */
#define IR_TX_OFF()     TCCR2A &= ~_BV(COM2B1)  /* Tx: Stop IR burst */
#define IR_TX_TEST()    TCCR2A &   _BV(COM2B1)  /* Tx: Check if IR is being transmitted or not */

#define IR_CAPT_TEST()  TCCR1B &   _BV(ICES1)   /* Rx: Check which edge generated the capture interrupt */
#define IR_CAPT_RISE()  TCCR1B |=  _BV(ICES1)   /* Rx: Set captureing is triggered on rising edge */
#define IR_CAPT_FALL()  TCCR1B &= ~_BV(ICES1)   /* Rx: Set captureing is triggered on falling edge */
#define IR_CAPT_ENA()   TIFR1 = _BV(ICF1); TIMSK1 = _BV(ICIE1)  /* Rx: Enable captureing interrupt */
#define IR_CAPT_REG()   ICR1                    /* Rx: Returns the value in capture register */
#define IR_CAPT_DIS()   TIMSK1 &= ~_BV(ICIE1)   /* Tx && Rx: Disable captureing interrupt */

#define IR_COMP_ENA(n)  OCR1A = TCNT1 + (n); TIFR1 = _BV(OCF1A); TIMSK1 |= _BV(OCIE1A) /* Enable compare interrupt n count after now */
#define IR_COMP_DIS()   TIMSK1 &= ~_BV(OCIE1A)  /* Disable compare interrupt */
#define IR_COMP_NEXT(n) OCR1A += (n)            /* Tx: Increase compare register by n count */

/*
 * 16MHz -> 500
 *  8MHz -> 1000
 */
#define T_CLK           (1000000000 / (F_CPU / 8))

/* Counter clock rate and register width */
/* #define T_CLK           500 */                    /* Timer tick period [ns] */
/*
[sample 10MHz]
  T_CLK = 800
  1s = 1000ms = 1000000us = 1000000000ns
  1M = 1000K  = 1000000
  10MHz   => 1000000000ns/ 10000000    = 100ns
  10MHz/8 => 1000000000ns/(10000000/8) = 800ns
[Arduino 16MHz]
  16MHz/8 => 1000000000ns/(16000000/8) = 500ns
*/
#define _tmr_t          uint16_t                /* Integer type of timer register */
/*---------------------------------------------------------------------------*/

/* IR control timings */
{'fraction_non_alphanumeric': 0.22266997313494524, 'fraction_numerical': 0.09960735689191982, 'mean_word_length': 1.876374442793462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 15, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3709280', 'n_tokens_mistral': 4157, 'n_tokens_neox': 3379, 'n_words': 932}