1 /***************************************************************************
2  *
3  * Copyright 2015-2019 BES.
4  * All rights reserved. All unpublished rights reserved.
5  *
6  * No part of this work may be used or reproduced in any form or by any
7  * means, or stored in a database or retrieval system, without prior written
8  * permission of BES.
9  *
10  * Use of this work is governed by a license granted by BES.
11  * This work contains confidential and proprietary information of
12  * BES. which is protected by copyright, trade secret,
13  * trademark and other intellectual property rights.
14  *
15  ****************************************************************************/
16 #ifndef __BES_IRLIB_H__
17 #define __BES_IRLIB_H__
18 
19 //#include "net_defs.h"
20 #ifdef RTOS
21 #include "cmsis_os.h"
22 #endif
23 #include "cmsis.h"
24 #include "hal_timer.h"
25 #include "hal_cmu.h"
26 #include "hal_pwm.h"
27 #include "hal_gpio.h"
28 
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #if 0
35 #define ir_printf(s,...) TRACE(s, ##__VA_ARGS__)
36 #define ir_printf_debug(s,...) TRACE(s, ##__VA_ARGS__)
37 #define ir_printf_info(s,...) TRACE(s, ##__VA_ARGS__)
38 #define ir_printf_warn(s,...) TRACE(s, ##__VA_ARGS__)
39 #else
40 #define ir_printf(s,...) printf(s, ##__VA_ARGS__)
41 #define ir_printf_debug(...)
42 #define ir_printf_info(s,...) printf(s, ##__VA_ARGS__)
43 #define ir_printf_warn(s,...) printf(s, ##__VA_ARGS__)
44 #endif
45 
46 #define MAX_IR_TX_BUF        512
47 
48 
49 //FUNCTION DEFILE
50 #define CONFIG_IR_CHECK_ADDR_AND_CODE   1
51 #define CONFIG_IR_TIMER2                1
52 
53 /*
54  *  NEC protocal Show:
55  *  8 bits addr, 8 bits cmd code.
56  *  send addr and cmd code twice every time to improve reliable.
57  * -----------------------------------------------------------------------------
58  * |         Logical '1'         |      Logical '0'     |    Logical 'x'     |
59  * -----------------------------------------------------------------------------
60  * | 1(carrier)|         0       | 1(carrier)|    0     | 1(carrier)|    -   |
61  * -----------------------------------------------------------------------------
62  * |   560uS   |      1680uS     |   560uS   |   560uS  |   560uS   |        |
63  * |           2.25mS            |        1.12mS        |           |        |
64  * -----------------------------------------------------------------------------
65  *  1(pulses): f=38KHz, T=26.3us, N=560/26.3=21
66  *  38KHz carrier frequency.
67  *  bit time is 1.12ms or 2.25ms.
68  *  send time is 68ms, send cycle is 110ms.
69 
70  * example: send 0x89(1001 0001 LSB first) addr, 0x82(0100 0001 LSB first) cmd code.
71  * ---------------------------------------------------------------------------------------------------------------------
72  * |  S1   | S2  |L1|L0|L0|L1|L0|L0|L0|L1|L0|L1|L1|L0|L1|L1|L1|L0|L0|L1|L0|L0|L0|L0|L0|L1|L1|L1|L0|L1|L0|L1|L1|L1| E1  |
73  * |  9mS  |4.5mS|LSB                 MSB|LSB                 MSB|LSB                 MSB|LSB                 MSB|560uS|
74  * |    Start    |     0x89 Address      |     ~0x89 Address     |     0x82 Command      |    ~0x82 Command      | End |
75  * ---------------------------------------------------------------------------------------------------------------------
76  * the repeat code S2 is 2.25mS.
77  *
78  * code dealt:
79  *  0x89 = b 1000 1001 right shift >> dealt bit,so will get: b 1001 0001 (LSB First)
80  * ~0x89 = b 0111 0110 right shift >> dealt bit,so will get: b 0110 1110 (LSB First)
81  *  0x82 = b 1000 0010 right shift >> dealt bit,so will get: b 0100 0001
82  * ~0x82 = b 0111 1101 right shift >> dealt bit,so will get: b 1011 1110
83  *
84  * 0x0b = b 0000 1011 right shift >> dealt bit,so will get: b 1101 0000 (LSB First)
85  *
86 */
87 
88 
89 
90 /*****************************************************************************
91  * struct or unit
92 *****************************************************************************/
93 
94 /**
95  * Media r05d cmd.
96  */
97 typedef enum
98 {
99 	DRV_CMD_R05D_POWEROFF   = 2,     /* R05d power off command */
100     DRV_CMD_R05D_NORMAL     = 4,
101 } IR_R05D_T;
102 
103 #if 1
104 /* IR return parameter definition */
105 typedef enum
106 {
107 	IR_OK		            = 0x00, /* IR OK,SUCCESS*/
108 	IR_BUSY		            = 0x01, /* IR is busy state */
109 	IR_PARAM_ERROR          = 0x02, /* IR parameters error */
110 	IR_MEM_ERROR            = 0X03, /* IR memory error */
111 	IR_UNKNOW        	    = 0x04  /* IR UNKNOW state */
112 } IR_RET_T;
113 #endif
114 
115 /* IR cmd definition */
116 typedef enum
117 {
118 	IR_CMD_TX               = 0x00, /* IR TX */
119 	IR_CMD_RX               = 0x01, /* IR RX */
120 	IR_CMD_SLEEP            = 0x02, /* IR SLEEP */
121 	IR_CMD_EXIT             = 0x03, /* EXIT */
122 	IR_CMD_ERROR            = 0x04, /* Error */
123 } IR_CMD_T;
124 
125 /* IR FINISH EVENT definition */
126 typedef enum
127 {
128     IR_TX_FINISH_EVENT      = 0x00,  /* IR ONE CODE TRANSMIT FINISHED */
129     IR_TX_TIMEOUT_EVENT     = 0x01,  /* IR TX TIMEOUT EVENT */
130     IR_TX_ERROR_EVENT       = 0x02,  /* IR TX ERROR EVENT */
131 } IR_EVENT_T;
132 
133 /* IR STATE */
134 typedef enum
135 {
136 	IR_IDLE_STATE			= 0x00, /* IDLE MODE*/
137 	IR_TX_STATE             = 0x01, /* BUSY! TX Doing */
138 	IR_RX_STATE				= 0X02, /* BUSY! RX Doing */
139 } IR_STATE_T;
140 
141 /* IR STUDY STATE */
142 typedef enum
143 {
144 	IR_STU_INIT			    = 0x00, /* Study initial state */
145 	IR_STU_DOING            = 0x01, /* Study doing */
146 	IR_STU_EXIT		        = 0X02, /* Study exit */
147 } IR_STUDY_T;
148 
149 
150 /*
151  * ir protocal.
152  */
153 typedef enum
154 {
155 	IR_STUDY_PROTO         = 0,     /* 学习到的码,这里简称学习协议 */
156 	IR_NEC_PROTO,                   /* NEC protocol */
157 	IR_ITT_PROTO,                   /* ITT protocol */
158 	IR_MEDIA_R05D_PROTO,            /* Media R05d protocol */
159 	IR_GREE_YB0F2_PROTO,            /* GREE YB0F2 protocol*/
160 	IR_PROTO_MAX
161 } IR_PROTO_T;
162 
163 
164 /*****************************************************************************
165  * functions
166 *****************************************************************************/
167 typedef void (* IR_HW_FUNC)(void);
168 typedef void (* IR_HW_START_FUNC)(uint32_t time);
169 typedef void (* IR_CARRIER_INIT_FUNC)(enum HAL_IOMUX_PIN_T gpio);
170 typedef void (* IR_CARRIER_FUNC)(void);
171 typedef int (* IR_CARRIER_START_FUNC)(uint32_t carrier_freq, uint8_t freq_ratio);
172 typedef void (* IR_TX_CB)(int type);
173 typedef void (* IR_RX_CB)(IR_PROTO_T proto,uint16_t *frame,uint16_t num,uint8_t repeat);
174 typedef void (* INPUT_CB)(uint32_t tm, enum HAL_GPIO_IRQ_POLARITY_T polarity);
175 
176 typedef struct {
177     IR_HW_FUNC          rtx_timer_init;         /* 红外定时器初始化*/
178     IR_HW_START_FUNC    txr_timer_start;        /* 红外定时器设置周期并开启 */
179     IR_HW_FUNC          txr_timer_stop;         /* 红外定时器暂停 */
180     IR_HW_FUNC          trx_timer_free;         /* 释放红外定时*/
181 } IR_FUNC_S;
182 
183 typedef struct {
184    IR_CARRIER_INIT_FUNC     carrier_init;      /* 红外载波初始化 */
185    IR_CARRIER_START_FUNC    carrier_start;     /* 红外载波发送开始 */
186    IR_CARRIER_FUNC          carrier_stop;      /* 红外载波发送停止 */
187 } IR_CARRI_F_S;
188 
189 #if 0
190 typedef struct {
191     osMutexId   mutex_id;
192 #if (osCMSIS >= 0x20000U)
193     osMutexAttr_t mutex_attr;
194 #else
195     uint32_t    os_mutex_cb[3];
196 #endif
197 }IR_MUTEX_T;
198 #endif
199 
200 typedef struct ir_finish_event {
201     uint8_t event_id;      /**< even ID */
202 } IR_EVENT_S;
203 
204 
205 typedef struct {
206     uint8_t     ircode[8];              /* useing bytes to code*/
207     uint16_t    nums;                   /* ir code/frame number */
208     uint8_t     cmd;                    /* IR RX OR IR TX cmd*/
209     uint16_t    *frame;                 /* ir frame time buf */
210     IR_PROTO_T  proto;                  /* ir protocal */
211     uint8_t     repeat;                 /* ir repeat: 1 true,0: flase ; 像空调这种没有repeat的,可以直接复用代码命令*/
212     /*定义
213     bit0 for repeat
214     bit1~4: other cmd
215     */
216 } IR_MESG_S;
217 
218 
219 typedef struct {
220     uint8_t             mode;           /* repeat mode,<1>: repeat mode,<0>: no repeat mode*/
221     uint8_t             repeat_sending; /* repeat sending <1>,doing repeat,<0>,normal*/
222     uint8_t             framenum;       /* ir repeat frame numbers*/
223     uint16_t            frametime[4];   /* ir repeat time buffer */
224     //uint32_t          repeat_interval; /* ir repeat interval*/
225 }IR_REPEAT_S;
226 
227 
228 
229 /**********************
230 * 红外载波参数结构体
231 **********************/
232 typedef struct {
233     enum HAL_PWM_ID_T   id;             /* 载波pwd id */
234     uint8_t             ratio;          /* 频率占空比 */
235     uint16_t            freq;            /* 载波频率 */
236     uint16_t            *txbuf;         /* ir tx time buffer */
237     uint16_t            txnum;          /* txbuf time numbers */
238     uint8_t             txfinish;       /* 标志红外码是否发送完 */
239     IR_REPEAT_S         repeat;
240     IR_PROTO_T          prototal;       /* IR protocal */
241 //    IR_MUTEX_T          tx_mutex;       /* tx mutex */
242     IR_CARRI_F_S        *funcs;         /* tx functions */
243 } IR_CARRI_S;
244 
245 typedef struct {
246     IR_TX_CB            app_tx_cb;      /* ir tx app cb func */
247     IR_RX_CB            app_rx_cb;      /* ir rx app cb func */
248     INPUT_CB            app_input_cb;   /* input app cb func */
249 }IR_CB_FUNCS;
250 
251 
252 /**********************
253 * 红外接收参数结构体
254 **********************/
255 typedef struct {
256     uint8_t             rx_mode;        //接收模式:
257     uint16_t            *rx_frame;      //每帧时间
258     uint16_t            rx_num;
259     //uint8_t           rx_triger;      //用于标志收到第一个中断
260     IR_STUDY_T          study_state;    //红外学习状态。
261     enum HAL_GPIO_PIN_T io;             //GPIO
262 } IR_RX_S;
263 
264 
265 /**********************
266 * 红外参数结构体
267 **********************/
268 typedef struct {
269     IR_FUNC_S 		*ir_funcs;
270 	IR_STATE_T      ir_state;
271 } IR_PARAM_S;
272 
273 
274 extern IR_CARRI_F_S ir_init_tx_funcs;
275 extern IR_PARAM_S   ir_pub_parm;
276 extern IR_CARRI_S   *ir_tx_param;
277 extern IR_RX_S      ir_rx_parm;
278 extern IR_FUNC_S    ir_init_cb;
279 
280 
281 /***************************************************************
282  * Function     : ir_get_status
283  * Description  : 获取红外工作状态
284  * Input        : None
285  * Output       : None
286  * Return       : IR_STATE_T
287 ****************************************************************/
288 IR_STATE_T ir_get_status(void);
289 
290 /***************************************************************
291  * Function     : ir_set_status
292  * Description  : 设置红外工作状态
293  * Input        : IR_STATE_T
294  * Output       : None
295  * Return       : None
296 ****************************************************************/
297 void ir_set_status(const IR_STATE_T state);
298 
299 /***************************************************************
300  * Function     : ir_get_status
301  * Description  : 获取红外学习状态
302  * Input        : None
303  * Output       : None
304  * Return       : IR_RX_S
305 ****************************************************************/
306 IR_STUDY_T ir_rx_get_status(void);
307 
308 /***************************************************************
309  * Function     : ir_rx_set_status
310  * Description  : 设置红外学习状态
311  * Input        : IR_STUDY_T
312  * Output       : None
313  * Return       : None
314 ****************************************************************/
315 void ir_rx_set_status(const IR_STUDY_T state);
316 
317 
318 /***************************************************************
319  * Function     : ir_rx_gpio_irq_enable
320  * Description  :
321  * Input        : None
322  * Output       : None
323  * Return       : 0: success; others: failed
324 ****************************************************************/
325 uint8_t ir_rx_gpio_irq_enable(void);
326 
327 
328 /***************************************************************
329  * Function     : ir_rx_gpio_irq_disable
330  * Description  :
331  * Input        : None
332  * Output       : None
333  * Return       : 0: success; others: failed
334 ****************************************************************/
335 uint8_t ir_rx_gpio_irq_disable(void);
336 
337 
338 /***************************************************************
339  * Function 	: ir_mailbox_put
340  * Description	:
341  * Input		: IR_MESG_S *
342  * Output		: None
343  * Return		: 0: success; others: failed
344 ****************************************************************/
345 int ir_mailbox_put(IR_MESG_S *input);
346 
347 /***************************************************************
348  * Function     : ir_mailbox_init
349  * Description  :
350  * Input        : None
351  * Output       : None
352  * Return       : 0: success; others: failed
353 ****************************************************************/
354 int ir_mailbox_init(void);
355 
356 /***************************************************************
357  * Function     : ir_tx_feedback_mailbox_init
358  * Description  :
359  * Input        : None
360  * Output       : None
361  * Return       : 0: success; others: failed
362 ****************************************************************/
363 //int ir_tx_feedback_mailbox_init(void);
364 
365 /***************************************************************
366  * Function     : ir_repeat_timer_init
367  * Description  :
368  * Input        : None
369  * Output       : None
370  * Return       : None
371 ****************************************************************/
372 void ir_repeat_timer_init(void);
373 
374 /***************************************************************
375  * Function     : ir_repeat_timer_del
376  * Description  :
377  * Input        : None
378  * Output       : None
379  * Return       : None
380 ****************************************************************/
381 void ir_repeat_timer_del(void);
382 
383 /******************************************************************************
384  * Function     : ir_create_thread
385  * Description  : 创建IR线程处理函数
386  * Input        : None
387  * Output       : None
388  * Return       : 0: success,others: failed!
389 ******************************************************************************/
390 int ir_create_thread(void);
391 
392 
393 #ifdef __cplusplus
394 }
395 #endif
396 
397 #endif
398