• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef PHTMLUWB_H
18 #define PHTMLUWB_H
19 
20 #include <phUwbCommon.h>
21 
22 /*
23  * Message posted by Reader thread uponl
24  * completion of requested operation
25  */
26 #define PH_TMLUWB_READ_MESSAGE (0xAA)
27 
28 /*
29  * Message posted by Writer thread upon
30  * completion of requested operation
31  */
32 #define PH_TMLUWB_WRITE_MESSAGE (0x55)
33 
34 /*
35  * Value indicates to reset device
36  */
37 #define PH_TMLUWB_RESETDEVICE (0x00008001)
38 
39 /*
40 ***************************Globals,Structure and Enumeration ******************
41 */
42 
43 /*
44  * Transaction (Tx/Rx) completion information structure of TML
45  *
46  * This structure holds the completion callback information of the
47  * transaction passed from the TML layer to the Upper layer
48  * along with the completion callback.
49  *
50  * The value of field wStatus can be interpreted as:
51  *
52  *     - UWBSTATUS_SUCCESS                    Transaction performed
53  * successfully.
54  *     - UWBSTATUS_FAILED                     Failed to wait on Read/Write
55  * operation.
56  *     - UWBSTATUS_INSUFFICIENT_STORAGE       Not enough memory to store data in
57  * case of read.
58  *     - UWBSTATUS_BOARD_COMMUNICATION_ERROR  Failure to Read/Write from the
59  * file or timeout.
60  */
61 
62 typedef struct phTmlUwb_TransactInfo {
63   tHAL_UWB_STATUS wStatus;       /* Status of the Transaction Completion*/
64   uint8_t* pBuff;          /* Response Data of the Transaction*/
65   uint16_t wLength;        /* Data size of the Transaction*/
66 } phTmlUwb_TransactInfo_t; /* Instance of Transaction structure */
67 
68 /*
69  * TML transreceive completion callback to Upper Layer
70  *
71  * pContext - Context provided by upper layer
72  * pInfo    - Transaction info. See phTmlUwb_TransactInfo
73  */
74 typedef void (*pphTmlUwb_TransactCompletionCb_t)(
75     void* pContext, phTmlUwb_TransactInfo_t* pInfo);
76 
77 /*
78  * TML Deferred callback interface structure invoked by upper layer
79  *
80  * This could be used for read/write operations
81  *
82  * dwMsgPostedThread Message source identifier
83  * pParams Parameters for the deferred call processing
84  */
85 typedef void (*pphTmlUwb_DeferFuncPointer_t)(uint32_t dwMsgPostedThread,
86                                              void* pParams);
87 /*
88  * Structure containing details related to read and write operations
89  *
90  */
91 typedef struct phTmlUwb_ReadWriteInfo {
92   volatile uint8_t bEnable; /*This flag shall decide whether to perform
93                                Write/Read operation */
94   uint8_t
95       bThreadBusy; /*Flag to indicate thread is busy on respective operation */
96   /* Transaction completion Callback function */
97   pphTmlUwb_TransactCompletionCb_t pThread_Callback;
98   void* pContext;        /*Context passed while invocation of operation */
99   uint8_t* pBuffer;      /*Buffer passed while invocation of operation */
100   uint16_t wLength;      /*Length of data read/written */
101   tHAL_UWB_STATUS wWorkStatus; /*Status of the transaction performed */
102 } phTmlUwb_ReadWriteInfo_t;
103 
104 /*
105  *Base Context Structure containing members required for entire session
106  */
107 typedef struct phTmlUwb_Context {
108   pthread_t readerThread; /*Handle to the thread which handles write and read
109                              operations */
110   pthread_t writerThread;
111   volatile uint8_t
112       bThreadDone; /*Flag to decide whether to run or abort the thread */
113   phTmlUwb_ReadWriteInfo_t tReadInfo;  /*Pointer to Reader Thread Structure */
114   phTmlUwb_ReadWriteInfo_t tWriteInfo; /*Pointer to Writer Thread Structure */
115   void* pDevHandle;                    /* Pointer to Device Handle */
116   uintptr_t dwCallbackThreadId; /* Thread ID to which message to be posted */
117   uint8_t bEnableCrc;           /*Flag to validate/not CRC for input buffer */
118   sem_t rxSemaphore;
119   sem_t txSemaphore;      /* Lock/Aquire txRx Semaphore */
120   sem_t postMsgSemaphore; /* Semaphore to post message atomically by Reader &
121                              writer thread */
122   pthread_cond_t wait_busy_condition; /*Condition to wait reader thread*/
123   pthread_mutex_t wait_busy_lock;     /*Condition lock to wait reader thread*/
124   pthread_mutex_t read_abort_lock;    /*Condition lock to wait read abort*/
125   pthread_cond_t read_abort_condition;  /*Condition to wait read abort*/
126   volatile uint8_t wait_busy_flag;    /*Condition flag to wait reader thread*/
127   volatile uint8_t is_read_abort;    /*Condition flag for read abort*/
128   volatile uint8_t gWriterCbflag;    /* flag to indicate write callback message is pushed to
129                            queue*/
130 } phTmlUwb_Context_t;
131 
132 /*
133  * Enum definition contains  supported ioctl control codes.
134  *
135  * phTmlUwb_Spi_IoCtl
136  */
137 typedef enum {
138   phTmlUwb_Invalid = 0,
139   phTmlUwb_SetPower,
140   phTmlUwb_EnableFwdMode,
141   phTmlUwb_EnableThroughPut,
142   phTmlUwb_EseReset
143 } phTmlUwb_ControlCode_t;     /* Control code for IOCTL call */
144 
145 /*
146  * TML Configuration exposed to upper layer.
147  */
148 typedef struct phTmlUwb_Config {
149   /* Port name connected to SR100
150    *
151    * Platform specific canonical device name to which SR100 is connected.
152    *
153    * e.g. On Linux based systems this would be /dev/SR100
154    */
155   int8_t* pDevName;
156   /* Callback Thread ID
157    *
158    * This is the thread ID on which the Reader & Writer thread posts message. */
159   uintptr_t dwGetMsgThreadId;
160   /* Communication speed between DH and SR100
161    *
162    * This is the baudrate of the bus for communication between DH and SR100 */
163   uint32_t dwBaudRate;
164 } phTmlUwb_Config_t, *pphTmlUwb_Config_t; /* pointer to phTmlUwb_Config_t */
165 
166 /*
167  * TML Deferred Callback structure used to invoke Upper layer Callback function.
168  */
169 typedef struct {
170   /* Deferred callback function to be invoked */
171   pphTmlUwb_DeferFuncPointer_t pDef_call;
172   /* Source identifier
173    *
174    * Identifier of the source which posted the message
175    */
176   uint32_t dwMsgPostedThread;
177   /** Actual Message
178    *
179    * This is passed as a parameter passed to the deferred callback function
180    * pDef_call. */
181   void* pParams;
182 } phTmlUwb_DeferMsg_t; /* DeferMsg structure passed to User Thread */
183 
184 /* Function declarations */
185 tHAL_UWB_STATUS phTmlUwb_Init(pphTmlUwb_Config_t pConfig);
186 tHAL_UWB_STATUS phTmlUwb_Shutdown(void);
187 tHAL_UWB_STATUS phTmlUwb_Write(uint8_t* pBuffer, uint16_t wLength,
188                          pphTmlUwb_TransactCompletionCb_t pTmlWriteComplete,
189                          void* pContext);
190 tHAL_UWB_STATUS phTmlUwb_Read(uint8_t* pBuffer, uint16_t wLength,
191                         pphTmlUwb_TransactCompletionCb_t pTmlReadComplete,
192                         void* pContext);
193 tHAL_UWB_STATUS phTmlUwb_WriteAbort(void);
194 tHAL_UWB_STATUS phTmlUwb_ReadAbort(void);
195 void phTmlUwb_eSE_Reset(void);
196 void phTmlUwb_Spi_Reset(void);
197 void phTmlUwb_Chip_Reset(void);
198 void phTmlUwb_DeferredCall(uintptr_t dwThreadId,
199                            phLibUwb_Message_t* ptWorkerMsg);
200 #endif /*  PHTMLUWB_H  */
201