• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "mdec.h"
19 #include "analog.h"
20 #include "clock.h"
21 #include "compiler.h"
22 #include "reg_include/mdec_reg.h"
23 
24 /**
25  * @brief		This function is used to initialize the MDEC module,include clock setting and input IO select.
26  * @param[in]	pin	- mdec pin.
27  * 					  In order to distinguish which pin the data is input from,
28  *                    only one input pin can be selected one time.
29  * @return		none.
30  */
mdec_init(mdec_pin_e pin)31 void mdec_init(mdec_pin_e pin)
32 {
33     analog_write_reg8(mdec_rst_addr, (analog_read_reg8(mdec_rst_addr) & (~FLD_CLS_MDEC)) | pin);  // A0/B7/C4/D0/E0
34 }
35 
36 /**
37  * @brief		This function is used to read the receive data of MDEC module's IO.
38  * @param[out]	dat		- The array to store date.
39  * @return		1 decode success,  0 decode failure.
40  */
mdec_read_dat(unsigned char * dat)41 unsigned char mdec_read_dat(unsigned char *dat)
42 {
43     unsigned char m0, m1, m2, data_crc;
44 
45     dat[0] = analog_read_reg8(0x6a);
46     dat[1] = analog_read_reg8(0x6b);
47     dat[2] = analog_read_reg8(0x6c);
48     dat[3] = analog_read_reg8(0x6d);
49     dat[4] = analog_read_reg8(0x6e);
50 
51     m0 = ((dat[0] >> 5) << 4);
52     m1 = dat[0] & 0x07;
53     m2 = m0 + m1;
54     data_crc = (((m2 + dat[1]) ^ dat[2]) + dat[3]) ^ 0xa5;
55 
56     if (data_crc == dat[4]) {
57         return 1;
58     }
59     return 0;
60 }
61