1 /******************************************************************************
2  *
3  *  Copyright (C) 2018 ST Microelectronics S.A.
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 #ifndef SPILAYERCOMM_H_
20 #define SPILAYERCOMM_H_
21 
22 #include "utils-lib/Tpdu.h"
23 
24 #define NAD_HOST_TO_SLAVE 0x21
25 #define NAD_SLAVE_TO_HOST 0x12
26 #define BWT_THRESHOlD 0xFFFF
27 #define DEFAULT_PWT 50
28 
29 // Global variables
30 
31 /**
32  * Starts the polling mechanism to read the length of the ATP.
33  *
34  * @returns 0 if everything is ok, -1 otherwise.
35  */
36 int SpiLayerComm_waitForAtpLength();
37 
38 /**
39  * Reads the ATP and stores it in the ATP parameter.
40  *
41  * @returns 0 if everything is ok, -1 otherwise.
42  */
43 int SpiLayerComm_readAtp();
44 
45 /**
46  * Writes the specified TPDU to the SPI interface.
47  *
48  * @param cmdTpdu The TPDU to be written.
49  *
50  * @return The number of bytes written if everything went well, -1 if an error
51  * 			occurred.
52  */
53 int SpiLayerComm_writeTpdu(Tpdu* cmdTpdu);
54 
55 /**
56  * Waits for a TPDU response to be available on the SPI interface.
57  *
58  * @param respTpdu The buffer where to store the TDPU.
59  * @param nBwt The maximum number of BWT to wait for the response.
60  *
61  * @return 0 if the response is available and the header could be read, -2
62  *           if no response received before the timeout, -1 otherwise.
63  */
64 int SpiLayerComm_waitForResponse(Tpdu* respTpdu, int nBwt);
65 
66 /**
67  * Reads the pending bytes of the response (data information and crc fields).
68  * Assumes respTpdu epilogue is initialized.
69  *
70  * @param respTpdu The buffer where to store the response. Should have the
71  *      epilogue initialized.
72  *
73  * @return 0 if everything went well, -1 otherwise.
74  */
75 int SpiLayerComm_readTpdu(Tpdu* respTpdu);
76 
77 void SpiLayerComm_readAtpFromFile();
78 
79 #endif /* SPILAYERCOMM_H_ */
80