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 SPILAYERDRIVER_H_
20 #define SPILAYERDRIVER_H_
21 
22 #include <fcntl.h>
23 #include <linux/spi/spidev.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <sys/ioctl.h>
27 #include <unistd.h>
28 
29 #define ATP_FILE_PATH "/data/vendor/ese/atp.bin"
30 
31 #define MODE_TX 0
32 #define MODE_RX 1
33 #define MIN_TIME_BETWEEN_MODE_SWITCH 1
34 #define ST54J_SE_MAGIC 0xE5
35 #define ST54J_SE_PULSE_RESET _IOR(ST54J_SE_MAGIC, 0x01, unsigned int)
36 
37 /**
38  * Open the spi device driver.
39  *
40  * @return  -1 if an error occurred, file descriptor if success.
41  */
42 int SpiLayerDriver_open(char *spiDevPath);
43 
44 /**
45  * Close the spi device driver.
46  *
47  */
48 void SpiLayerDriver_close();
49 
50 /**
51  * Reads bytesToRead bytes from the SPI interface.
52  *
53  * @param rxBuffer The buffer where the received bytes are stored.
54  * @param bytesToRead The expected number of bytes to be read.
55  *
56  * @return The amount of bytes read from the slave, -1 if something failed.
57  */
58 int SpiLayerDriver_read(uint8_t *rxBuffer, unsigned int bytesToRead);
59 
60 /**
61  * Write txBufferLength bytes to the SPI interface.
62  *
63  * @param txBuffer The buffer where the bytes to write are placed.
64  * @param txBufferLength The number of bytes to be written.
65  *
66  * @return The amount of bytes written to the slave, -1 if something failed.
67  */
68 int SpiLayerDriver_write(uint8_t *writeBuffer, unsigned int bytesToWrite);
69 
70 /**
71  * Send a Reset pulse to the eSE.
72  *
73  * @return 0 if success, -1 if something failed.
74  */
75 int SpiLayerDriver_reset();
76 
77 #endif /* SPILAYERDRIVER_H_ */
78