1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 
19 /********************************************************************************************************
20  * @file	mdec.c
21  *
22  * @brief	This is the source file for B91
23  *
24  * @author	Driver Group
25  *
26  *******************************************************************************************************/
27 #include"mdec.h"
28 #include "compiler.h"
29 #include "reg_include/mdec_reg.h"
30 #include "analog.h"
31 #include "clock.h"
32 
33 /**
34  * @brief		This function is used to initialize the MDEC module,include clock setting and input IO select.
35  * @param[in]	pin	- mdec pin.
36  * 					  In order to distinguish which pin the data is input from,only one input pin can be selected one time.
37  * @return		none.
38  */
mdec_init(mdec_pin_e pin)39 void mdec_init(mdec_pin_e pin)
40 {
41 	analog_write_reg8(mdec_rst_addr,(analog_read_reg8(mdec_rst_addr) & (~FLD_CLS_MDEC)) | pin);//A0/B7/C4/D0/E0
42 }
43 
44 /**
45  * @brief		This function is used to read the receive data of MDEC module's IO.
46  * @param[out]	dat		- The array to store date.
47  * @return		1 decode success,  0 decode failure.
48  */
mdec_read_dat(unsigned char * dat)49 unsigned char mdec_read_dat(unsigned char *dat)
50 {
51 	unsigned char m0,m1,m2,data_crc;
52 
53 	dat[0]=analog_read_reg8(0x6a);
54 	dat[1]=analog_read_reg8(0x6b);
55 	dat[2]=analog_read_reg8(0x6c);
56 	dat[3]=analog_read_reg8(0x6d);
57 	dat[4]=analog_read_reg8(0x6e);
58 
59 	m0= ((dat[0]>>5)<<4);
60 	m1= dat[0]&0x07;
61 	m2= m0+m1;
62 	data_crc=(((m2+dat[1])^dat[2])+dat[3])^0xa5;
63 
64 	if(data_crc==dat[4]){
65 		return 1;
66 	}
67 	return 0;
68 }
69 
70 
71