1 /*
2 * Copyright 2012-2020 NXP
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdlib.h>
19 #include <sys/ioctl.h>
20 #include <sys/select.h>
21 #include <termios.h>
22 #include <unistd.h>
23
24 #include <phUwbStatus.h>
25 #include <phNxpLog.h>
26 #include <phTmlUwb_spi.h>
27 #include <string.h>
28 #include "phNxpUciHal_utils.h"
29 #include "phNxpUciHal.h"
30 /*********************** Global Variables *************************************/
31 /* UCI HAL Control structure */
32 extern phNxpUciHal_Control_t nxpucihal_ctrl;
33
34 /*******************************************************************************
35 **
36 ** Function phTmlUwb_spi_open_and_configure
37 **
38 ** Description Open and configure SR100
39 **
40 ** Parameters pConfig - hardware information
41 ** pLinkHandle - device handle
42 **
43 ** Returns UWB status:
44 ** UWBSTATUS_SUCCESS - open_and_configure operation success
45 ** UWBSTATUS_INVALID_DEVICE - device open operation failure
46 **
47 *******************************************************************************/
phTmlUwb_spi_open_and_configure(pphTmlUwb_Config_t pConfig,void ** pLinkHandle)48 tHAL_UWB_STATUS phTmlUwb_spi_open_and_configure(pphTmlUwb_Config_t pConfig,
49 void** pLinkHandle) {
50 int nHandle;
51
52 NXPLOG_TML_D("Opening port=%s\n", pConfig->pDevName);
53 /* open port */
54 nHandle = open((const char*)pConfig->pDevName, O_RDWR);
55 if (nHandle < 0) {
56 NXPLOG_TML_E("_spi_open() Failed: retval %x", nHandle);
57 *pLinkHandle = NULL;
58 return UWBSTATUS_INVALID_DEVICE;
59 }
60
61 *pLinkHandle = (void*)((intptr_t)nHandle);
62
63 /*Reset SR100 */
64 phTmlUwb_Spi_Ioctl((void*)((intptr_t)nHandle), phTmlUwb_SetPower, 0);
65 usleep(1000);
66 phTmlUwb_Spi_Ioctl((void*)((intptr_t)nHandle), phTmlUwb_SetPower, 1);
67 usleep(10000);
68
69 return UWBSTATUS_SUCCESS;
70 }
71
72 /*******************************************************************************
73 **
74 ** Function phTmlUwb_spi_write
75 **
76 ** Description Writes requested number of bytes from given buffer into
77 ** SR100
78 **
79 ** Parameters pDevHandle - valid device handle
80 ** pBuffer - buffer for read data
81 ** nNbBytesToWrite - number of bytes requested to be written
82 **
83 ** Returns numWrote - number of successfully written bytes
84 ** -1 - write operation failure
85 **
86 *******************************************************************************/
phTmlUwb_spi_write(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToWrite)87 int phTmlUwb_spi_write(void* pDevHandle, uint8_t* pBuffer,
88 int nNbBytesToWrite) {
89 int ret;
90 int numWrote = 0;
91 int numByteWrite = 0;
92
93 if (NULL == pDevHandle) {
94 NXPLOG_TML_E("_spi_write() device is null");
95 return -1;
96 }
97 numByteWrite = NORMAL_MODE_HEADER_LEN;
98
99 ret = write((intptr_t)pDevHandle, pBuffer, nNbBytesToWrite);
100 if (ret > 0) {
101 NXPLOG_TML_D("_spi_write()_1 ret : %x", ret);
102 numWrote = ret;
103 } else {
104 NXPLOG_TML_D("_spi_write()_1 failed : %d", ret);
105 return -1;
106 }
107 return numWrote;
108 }
109
110 /*******************************************************************************
111 **
112 ** Function phTmlUwb_spi_read
113 **
114 ** Description Reads requested number of bytes from SR100 device into
115 ** given buffer
116 **
117 ** Parameters pDevHandle - valid device handle
118 ** pBuffer - buffer for read data
119 ** nNbBytesToRead - number of bytes requested to be read
120 **
121 ** Returns numRead - number of successfully read bytes
122 ** -1 - read operation failure
123 **
124 *******************************************************************************/
phTmlUwb_spi_read(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToRead)125 int phTmlUwb_spi_read(void* pDevHandle, uint8_t* pBuffer, int nNbBytesToRead) {
126 int ret_Read;
127 uint16_t totalBtyesToRead = 0;
128
129 UNUSED(nNbBytesToRead);
130 if (NULL == pDevHandle) {
131 NXPLOG_TML_E("_spi_read() error handle");
132 return -1;
133 }
134 totalBtyesToRead = NORMAL_MODE_HEADER_LEN;
135 /*Just requested 3 bytes header here but driver will get the header + payload and returns*/
136 ret_Read = read((intptr_t)pDevHandle, pBuffer, totalBtyesToRead);
137 if (ret_Read < 0) {
138 NXPLOG_TML_E("_spi_read() error: %d", ret_Read);
139 return -1;
140 } else if((nxpucihal_ctrl.fw_dwnld_mode) && ((0xFF == pBuffer[0]) || ((0x00 == pBuffer[0]) && (0x00 == pBuffer[3])))) {
141 NXPLOG_TML_E("_spi_read() error: Invalid UCI packet");
142 /* To Avoid spurious interrupt after FW download */
143 return 0;
144 }
145 //nxpucihal_ctrl.cir_dump_len = ret_Read - NORMAL_MODE_HEADER_LEN;
146 return ret_Read;
147 }
148
149 /*******************************************************************************
150 **
151 ** Function phTmlUwb_Spi_Ioctl
152 **
153 ** Description Reset SR100, using VEN pin
154 **
155 ** Parameters pDevHandle - valid device handle
156 ** level - reset level
157 **
158 ** Returns 0 - reset operation success
159 ** -1 - reset operation failure
160 **
161 *******************************************************************************/
phTmlUwb_Spi_Ioctl(void * pDevHandle,phTmlUwb_ControlCode_t eControlCode,long arg)162 int phTmlUwb_Spi_Ioctl(void* pDevHandle, phTmlUwb_ControlCode_t eControlCode , long arg) {
163 NXPLOG_TML_D("phTmlUwb_Spi_Ioctl(), cmd %d, arg %ld", eControlCode, arg);
164 int ret = 1;
165 if (NULL == pDevHandle) {
166 return -1;
167 }
168 switch(eControlCode){
169 case phTmlUwb_SetPower:
170 ioctl((intptr_t)pDevHandle, SRXXX_SET_PWR, arg);
171 break;
172 case phTmlUwb_EnableFwdMode:
173 ioctl((intptr_t)pDevHandle, SRXXX_SET_FWD, arg);
174 break;
175 case phTmlUwb_EnableThroughPut:
176 //ioctl((intptr_t)pDevHandle, SRXXX_GET_THROUGHPUT, arg);
177 break;
178 case phTmlUwb_EseReset:
179 ioctl((intptr_t)pDevHandle, SRXXX_ESE_RESET, arg);
180 break;
181 default:
182 NXPLOG_TML_D("phTmlUwb_Spi_Ioctl(), Invalid command");
183 ret = -1;
184 }
185 return ret;
186 }
187
188 /*******************************************************************************
189 **
190 ** Function phTmlUwb_spi_close
191 **
192 ** Description Closes SR100 device
193 **
194 ** Parameters pDevHandle - device handle
195 **
196 ** Returns None
197 **
198 *******************************************************************************/
phTmlUwb_spi_close(void * pDevHandle)199 void phTmlUwb_spi_close(void* pDevHandle) {
200 if (NULL != pDevHandle) {
201 close((intptr_t)pDevHandle);
202 }
203
204 return;
205 }
206