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