| NANO100_BSP V3.05.000 The Board Support Package for Nano100BN Series

NuEdu-Basic01_IrDA_NEC.c
Go to the documentation of this file.
1/**************************************************************************/
12#include <stdio.h>
13#include "Nano100Series.h"
14#include "NuEdu-Basic01_IrDA_NEC.h"
15
16
31 volatile uint8_t IR_State = 0; // IR State
32 volatile uint8_t IR_LDC_Ready = 0; // LeaDer Code is Ready
33 volatile uint8_t IR_CTC_Ready = 0; // CusTomer Code is Ready
34 volatile uint8_t IR_CTC0 = 0; // Received CusTomer Code 0
35 volatile uint8_t IR_CTC1 = 0; // Received CusTomer Code 1
36 volatile uint8_t IR_DAC = 0; // Received Data Code
37 volatile uint8_t IR_DAB = 0; // Received Data Bar code
38 volatile uint8_t IR_cnt = 0;
39 volatile uint8_t IR_CODE[4] = {0x00, 0x00, 0x00, 0x00};
40 IrDA_Code_Exe g_pfnIrDA_Code_Exe;
42
48 void IrDa_NEC_Rx(uint32_t u32Time)
49{
50 if(IR_State == 0)
51 {
52 IR_LDC_Ready = 0; // Clear LeaDer Code Ready
53 IR_CTC_Ready = 0; // Clear CusTomer Code Ready
54 IR_State++;
55 }
56 // Leader or Repeater code
57 else if(IR_State == 1)
58 {
59 // Leader code
60 if((u32Time >= IR_LDC_MIN) && (u32Time <= IR_LDC_MAX))
61 {
62 IR_LDC_Ready = 1; // Set LeaDer Code Ready
63 IR_State++;
64 }
65 else
66 {
67 IR_State = 1;
68 IR_LDC_Ready = 0; // Clear LeaDer Code Ready
69 IR_CTC_Ready = 0; // Clear CusTomer Code Ready
70 }
71 }
72 // Customer code 0
73 else if((IR_State >= 2 && IR_State < 10) && (IR_LDC_Ready == 1))
74 {
75 IR_State++;
76 IR_CTC0 = IR_CTC0 >> 1;
77 if((u32Time >= IR_BIT_0_MIN) && (u32Time <= IR_BIT_0_MAX)) // 1.12ms = 0
78 IR_CTC0 &= 0x7f;
79 else if((u32Time >= IR_BIT_1_MIN) && (u32Time <= IR_BIT_1_MAX)) // 2.25ms = 1
80 IR_CTC0 |= 0x80;
81 else
82 IR_State = 0;
83 }
84 // Customer code 1
85 else if((IR_State >= 10 && IR_State < 18) && (IR_LDC_Ready == 1))
86 {
87 IR_State++;
88 IR_CTC1 = IR_CTC1 >> 1;
89 if((u32Time >= IR_BIT_0_MIN) && (u32Time <= IR_BIT_0_MAX)) // 1.12ms = 0
90 IR_CTC1 &= 0x7f;
91 else if((u32Time >= IR_BIT_1_MIN) && (u32Time <= IR_BIT_1_MAX)) // 2.25ms = 1
92 IR_CTC1 |= 0x80;
93 else
94 IR_State = 0;
95 }
96 // Data
97 else if((IR_State >= 18 && IR_State < 26) && (IR_LDC_Ready == 1))
98 {
99 IR_State++;
100 IR_DAC = IR_DAC >> 1;
101 if((u32Time >= IR_BIT_0_MIN) && (u32Time <= IR_BIT_0_MAX)) // 1.12ms = 0
102 IR_DAC &= 0x7f;
103 else if((u32Time >= IR_BIT_1_MIN) && (u32Time <= IR_BIT_1_MAX)) // 2.25ms = 1
104 IR_DAC |= 0x80;
105 else
106 IR_State = 0;
107
108 }
109 // Data bar
110 else if((IR_State >= 26 && IR_State < 34) && (IR_LDC_Ready == 1))
111 {
112 IR_State++;
113 IR_DAB = IR_DAB >> 1;
114 if((u32Time >= IR_BIT_0_MIN) && (u32Time <= IR_BIT_0_MAX)) // 1.12ms = 0
115 IR_DAB &= 0x7f;
116 else if((u32Time >= IR_BIT_1_MIN) && (u32Time <= IR_BIT_1_MAX)) // 2.25ms = 1
117 IR_DAB |= 0x80;
118 else
119 IR_State = 0;
120
121 if(IR_State == 34)
122 {
123 if((IR_DAC ^ IR_DAB) == 0xff)
124 {
125 IR_LDC_Ready = 0; // Clear LeaDer Code Ready
126 IR_CODE[0] = IR_CTC0;
127 IR_CODE[1] = IR_CTC1;
128 IR_CODE[2] = IR_DAC;
129 IR_CODE[3] = IR_DAB;
130 IR_cnt++;
131 g_pfnIrDA_Code_Exe(IR_CODE);
132 printf("IR_cnt=%d, CTC0=%02x, CTC1=%02x, DAC=%02x, DAB=%02x\n", IR_cnt, IR_CTC0, IR_CTC1, IR_DAC, IR_DAB);
133 }
134 IR_State = 0;
135 }
136 }
137}
138
139#define NEC_LDC_MARK 16 // 16 x 560us = 8960us = 9ms
140#define NEC_LDC_SPACE 8 // 8 x 560us = 4480us = 4.5ms
141#define NEC_BIT_MARK 1 // 560us
142#define NEC_ONE_SPACE 3 // 3 x 560us = 1680us = 1690us
143#define NEC_ZERO_SPACE 1 // 560us
144#define NEC_BYTES 4
145
152 void Mark(uint8_t N)
153{
154 /* Switch to PWM output waveform */
155 PWM_EnableOutput(PWM1,PWM_CH_2_MASK);
156 CLK_SysTickDelay(560*N);
157 PWM_DisableOutput(PWM1,PWM_CH_2_MASK);
158}
164 void SPACE(uint8_t N)
165{
166 CLK_SysTickDelay(560*N);
167}
168
174 void SendNEC(uint8_t* data)
175{
176 uint8_t nbyte;
177 uint8_t nbit;
178
179 /* Send out Leader code */
180 Mark(NEC_LDC_MARK);
181 SPACE(NEC_LDC_SPACE);
182
183 /* Send out Customer code and Data code */
184 for (nbyte=0; nbyte < NEC_BYTES; nbyte++)
185 {
186 for (nbit=0; nbit < 8; nbit++)
187 {
188 Mark(NEC_BIT_MARK);
189 if (data[nbyte] & (1 << nbit)) // LSB first
190 SPACE(NEC_ONE_SPACE);
191 else
192 SPACE(NEC_ZERO_SPACE);
193 }
194 }
195
196 /* Send out Stop bit */
197 Mark(NEC_BIT_MARK);
198
199}
200
206 void IrDA_NEC_TxRx_Init(IrDA_Code_Exe pfnIrDA_Code_Exe)
207{
208 g_pfnIrDA_Code_Exe = pfnIrDA_Code_Exe;
209
210 CLK_SetModuleClock(PWM1_CH23_MODULE, CLK_CLKSEL2_PWM1_CH23_S_HIRC, 0);
211 CLK_EnableModuleClock(PWM1_CH23_MODULE);
212
213 SYS->PC_H_MFP |= ( SYS_PC_H_MFP_PC14_MFP_PWM1_CH3 | SYS_PC_H_MFP_PC15_MFP_PWM1_CH2);
214
215 PWM1->CTL |= (PWM_CTL_CH3MOD_Msk|PWM_CTL_CH2MOD_Msk); //Set PWM1_CH3 is Continuous Mode
216 PWM_SET_DIVIDER(PWM1, 2, PWM_CLK_DIV_1); //Set PWM1_CH2 Clock Source Divider
217 PWM_SET_DIVIDER(PWM1, 3, PWM_CLK_DIV_4); //Set PWM1_CH3 Clock Source Divider
218 PWM_SET_PRESCALER(PWM1, 3, 2); //Set PWM1_CH2 and PWM1_CH3 Prescaler
219 PWM_SET_CNR(PWM1, 3, MaxValue);
220 PWM_SET_CNR(PWM1, 2, ((120000000/3/38000+5)/10));
221 PWM_SET_CMR(PWM1, 2, ((120000000/3/38000+5)/10)/2);
222
223 //Start Capture
224 PWM_EnableCapture(PWM1, PWM_CH_3_MASK);
225 PWM1->CAPCTL |= PWM_CAPCTL_CAPRELOADFEN3_Msk;
226 PWM_EnableCaptureInt(PWM1, 3,PWM_FALLING_LATCH_INT_ENABLE);
227 NVIC_EnableIRQ(PWM1_IRQn);
228 PWM_Start(PWM1, PWM_CH_2_MASK|PWM_CH_3_MASK);
229}
230 /* end of group Nano130_Basic01_FUNCTIONS IrDA NEC Exported Functions */
232 /* end of group NuEdu-SDK-Nano130_Basic01 Nano130_Basic01 Library */
234 /* end of group NANO100_Library NANO100 Library */
236
237/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
Nano100Series.h
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
PWM_CTL_CH2MOD_Msk
#define PWM_CTL_CH2MOD_Msk
Definition: Nano100Series.h:7409
PWM_CTL_CH3MOD_Msk
#define PWM_CTL_CH3MOD_Msk
Definition: Nano100Series.h:7418
PWM_CAPCTL_CAPRELOADFEN3_Msk
#define PWM_CAPCTL_CAPRELOADFEN3_Msk
Definition: Nano100Series.h:7604
NuEdu-Basic01_IrDA_NEC.h
NuEdu-Basic01 IrDA NEC driver header file.
PWM1_CH23_MODULE
#define PWM1_CH23_MODULE
Definition: clk.h:307
CLK_CLKSEL2_PWM1_CH23_S_HIRC
#define CLK_CLKSEL2_PWM1_CH23_S_HIRC
Definition: clk.h:180
CLK_EnableModuleClock
void CLK_EnableModuleClock(uint32_t u32ModuleIdx)
This function enable module clock.
Definition: clk.c:436
CLK_SysTickDelay
int32_t CLK_SysTickDelay(uint32_t us)
This function execute delay function.
Definition: clk.c:560
CLK_SetModuleClock
void CLK_SetModuleClock(uint32_t u32ModuleIdx, uint32_t u32ClkSrc, uint32_t u32ClkDiv)
This function set selected module clock source and module clock divider.
Definition: clk.c:339
PWM1_IRQn
@ PWM1_IRQn
Definition: Nano100Series.h:97
PWM1
#define PWM1
Pointer to PWM1 register structure.
Definition: Nano100Series.h:11680
SYS
#define SYS
Pointer to SYS register structure.
Definition: Nano100Series.h:11692
PWM_CH_3_MASK
#define PWM_CH_3_MASK
Definition: pwm.h:40
PWM_CH_2_MASK
#define PWM_CH_2_MASK
Definition: pwm.h:39
PWM_FALLING_LATCH_INT_ENABLE
#define PWM_FALLING_LATCH_INT_ENABLE
Definition: pwm.h:49
PWM_CLK_DIV_1
#define PWM_CLK_DIV_1
Definition: pwm.h:41
PWM_CLK_DIV_4
#define PWM_CLK_DIV_4
Definition: pwm.h:43
PWM_EnableCapture
void PWM_EnableCapture(PWM_T *pwm, uint32_t u32ChannelMask)
This function enables PWM capture of selected channels.
Definition: pwm.c:285
PWM_SET_DIVIDER
#define PWM_SET_DIVIDER(pwm, u32ChannelNum, u32Divider)
This macro set the divider of the selected channel.
Definition: pwm.h:131
PWM_SET_CMR
#define PWM_SET_CMR(pwm, u32ChannelNum, u32CMR)
This macro set the duty of the selected channel.
Definition: pwm.h:143
PWM_SET_CNR
#define PWM_SET_CNR(pwm, u32ChannelNum, u32CNR)
This macro set the period of the selected channel.
Definition: pwm.h:159
PWM_DisableOutput
void PWM_DisableOutput(PWM_T *pwm, uint32_t u32ChannelMask)
This function disables PWM output generation of selected channels.
Definition: pwm.c:346
PWM_SET_PRESCALER
#define PWM_SET_PRESCALER(pwm, u32ChannelNum, u32Prescaler)
This macro set the prescaler of the selected channel.
Definition: pwm.h:115
PWM_Start
void PWM_Start(PWM_T *pwm, uint32_t u32ChannelMask)
This function start PWM module.
Definition: pwm.c:227
PWM_EnableOutput
void PWM_EnableOutput(PWM_T *pwm, uint32_t u32ChannelMask)
This function enables PWM output generation of selected channels.
Definition: pwm.c:334
PWM_EnableCaptureInt
void PWM_EnableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge)
This function enable capture interrupt of selected channel.
Definition: pwm.c:393
SYS_PC_H_MFP_PC15_MFP_PWM1_CH2
#define SYS_PC_H_MFP_PC15_MFP_PWM1_CH2
Definition: sys.h:375
SYS_PC_H_MFP_PC14_MFP_PWM1_CH3
#define SYS_PC_H_MFP_PC14_MFP_PWM1_CH3
Definition: sys.h:381
NEC_BIT_MARK
#define NEC_BIT_MARK
Definition: NuEdu-Basic01_IrDA_NEC.c:141
NEC_ONE_SPACE
#define NEC_ONE_SPACE
Definition: NuEdu-Basic01_IrDA_NEC.c:142
IrDa_NEC_Rx
void IrDa_NEC_Rx(uint32_t u32Time)
This function is used to detect NEC IR procotol.
Definition: NuEdu-Basic01_IrDA_NEC.c:48
NEC_ZERO_SPACE
#define NEC_ZERO_SPACE
Definition: NuEdu-Basic01_IrDA_NEC.c:143
NEC_LDC_SPACE
#define NEC_LDC_SPACE
Definition: NuEdu-Basic01_IrDA_NEC.c:140
IrDA_NEC_TxRx_Init
void IrDA_NEC_TxRx_Init(IrDA_Code_Exe pfnIrDA_Code_Exe)
This function is used to initiate PWM for IrDA NEC.
Definition: NuEdu-Basic01_IrDA_NEC.c:206
NEC_BYTES
#define NEC_BYTES
Definition: NuEdu-Basic01_IrDA_NEC.c:144
SendNEC
void SendNEC(uint8_t *data)
This function is used to transmit IrDA NEC waveform through PC 15 (PWM1_CH3)
Definition: NuEdu-Basic01_IrDA_NEC.c:174
SPACE
void SPACE(uint8_t N)
This function is used to transmit SPACE waveform.
Definition: NuEdu-Basic01_IrDA_NEC.c:164
IrDA_Code_Exe
void(* IrDA_Code_Exe)(volatile uint8_t *IR_CODE)
Definition: NuEdu-Basic01_IrDA_NEC.h:46
Mark
void Mark(uint8_t N)
This function is used to transmit MASK waveform Pulse = 1/3 duty @38KHz frequency.
Definition: NuEdu-Basic01_IrDA_NEC.c:152
NEC_LDC_MARK
#define NEC_LDC_MARK
Definition: NuEdu-Basic01_IrDA_NEC.c:139