1 /***************************************************************************//**
2 * \file cy_ble_hal_pvt.h
3 * \version 3.60
4 *
5 * \brief
6 *  Contains the function prototypes and constants for the HAL section
7 *  of the PSoC 6 BLE Middleware.
8 *
9 ********************************************************************************
10 * \copyright
11 * Copyright 2017-2021, Cypress Semiconductor Corporation.  All rights reserved.
12 * You may use this file only in accordance with the license, terms, conditions,
13 * disclaimers, and limitations in the end user license agreement accompanying
14 * the software package with which this file was provided.
15 *******************************************************************************/
16 
17 #ifndef CY_BLE_HALL_PVT_H
18 #define CY_BLE_HALL_PVT_H
19 
20 #include <stdlib.h>
21 
22 #include "cy_device.h"
23 #include "cy_ble_stack_pvt.h"
24 #include "cy_ble.h"
25 
26 /* system resources */
27 #include "cy_syspm.h"
28 #include "cy_flash.h"
29 #include "cy_ipc_pipe.h"
30 #include "cy_sysclk.h"
31 #include "cy_ipc_sema.h"
32 #include "cy_ble_clk.h"
33 
34 #if defined(CY_IP_MXBLESS)
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif /* __cplusplus */
39 
40 
41 /*******************************************************************************
42 *   HAL Constants
43 *******************************************************************************/
44 
45 #define CY_BLE_HCI_COMMAND_SUCCEEDED                                    (0x00u)
46 #define CY_BLE_UNKNOWN_HCI_COMMAND_ERROR                                (0x01u)
47 
48 #define CY_BLE_CYPIPE_MSG_COMPLETE_ID                                   (0u)
49 #define CY_BLE_CYPIPE_MSG_SEND_ID                                       (1u)
50 #define CY_BLE_CYPIPE_MSG_CMD_ID                                        (3u)
51 
52 /* Common IPC commands (used by middleware) */
53 #define CY_BLE_CYPIPE_INTR_NOTIFY_CMD                                   (0xFFu)
54 #define CY_BLE_CYPIPE_ENABLE_CONTR_CMD                                  (0x01u)
55 
56 /* Other */
57 #define CY_BLE_FLASH_OPR_TIMEOUT                                        (200u)
58 
59 #define CY_BLE_IPC_CONTROLLER_ADDR                                      (CY_IPC_EP_CYPIPE_CM0_ADDR)
60 #define CY_BLE_IPC_HOST_ADDR                                            (CY_IPC_EP_CYPIPE_CM4_ADDR)
61 
62 #define CY_BLE_SFLASH_BLE_RADIO_CALL_ADDRESS                            ((uint32_t)SFLASH_BLE_DEVICE_ADDRESS + sizeof(cy_stc_ble_bd_addr_t))
63 
64 /* Flash blocking mode(define for mode in Cy_BLE_HAL_NvramWrite function ) */
65 #define CY_BLE_FLASH_BLOCKING_MODE                                      (1u)
66 
67 /* Flash non blocking mode(define for mode in Cy_BLE_HAL_NvramWrite function )*/
68 #define CY_BLE_FLASH_NON_BLOCKING_MODE                                  (0u)
69 
70 /* Constants for Voltage value calculation  */
71 #define CY_BLE_RADIO_VOLTAGE_MONITOR_SLOPE                              (174u)
72 #define CY_BLE_RADIO_VOLTAGE_MONITOR_SLOPE_MULTIPLIER                   (10u)
73 #define CY_BLE_RADIO_VOLTAGE_MONITOR_OFFSET                             (879u)
74 
75 /* Constants for Temperature value calculation  */
76 #define CY_BLE_RADIO_TEMP_MONITOR_SLOPE                                 (10315)
77 #define CY_BLE_RADIO_TEMP_MONITOR_SLOPE_MULTIPLIER                      (10000)
78 #define CY_BLE_RADIO_TEMP_MONITOR_OFFSET                                (189)
79 
80 /* Default BLE ECO settings */
81 #define CY_BLE_DEFAULT_OSC_STARTUP_DELAY_LF                             (25u)
82 #define CY_BLE_DEFAULT_CAP_TRIM_VALUE                                   (32u)
83 #define CY_BLE_DEFAULT_BLESS_ECO_FREQ                                   (CY_BLE_BLESS_ECO_FREQ_32MHZ)
84 #define CY_BLE_DEFAULT_SYS_ECO_CLK_DIV                                  (CY_BLE_SYS_ECO_CLK_DIV_4)
85 #define CY_BLE_DEFAULT_CY_BLE_ECO_VOLTAGE_REG                           (CY_BLE_ECO_VOLTAGE_REG_AUTO)
86 
87 /* Package type */
88 #define CY_BLE_PACKAGE_QFN                                              (0u)
89 #define CY_BLE_PACKAGE_BGA                                              (1u)
90 #define CY_BLE_PACKAGE_CSP                                              (2u)
91 
92 /* Package HW Block */
93 #define CY_BLE_PACKAGE_HW_BLOCK_USB                                     (0x01UL << 1u)
94 
95 /* Constants for PILO */
96 /* Target frequency */
97 #define CY_BLE_PILO_TARGET_FREQ                                         (32768ul)
98 
99 /* Nominal trim step size */
100 #define CY_BLE_PILO_TRIM_STEP                                           (5ul)
101 
102 /* BLESS Syspm callback's order */
103 #if !defined(CY_BLE_LPM_SYSPM_CB_ORDER)
104     #define CY_BLE_LPM_SYSPM_CB_ORDER                                   (254u)
105 #endif  /* !defined(CY_BLE_LPM_SYSPM_CB_ORDER) */
106 
107 
108 
109 /*******************************************************************************
110 * Data Types
111 *******************************************************************************/
112 
113 /** \cond IGNORE */
114 typedef struct
115 {
116     uint8_t  clientID;
117     uint8_t  pktType;
118     uint16_t intrRelMask;
119     bool     controllerStarted;
120     uint32_t data;
121     uint16_t dataLen;
122 } cy_stc_ble_ipc_msg_t;
123 
124 typedef enum
125 {
126     CY_BLE_IC_PACKAGE_TYPE_BGA_116 = 0ul,
127     CY_BLE_IC_PACKAGE_TYPE_CSP_104,
128     CY_BLE_IC_PACKAGE_TYPE_BGA_124,
129     CY_BLE_IC_PACKAGE_TYPE_QFN_68,
130 }cy_en_ble_ic_package_type_t;
131 /** \endcond */
132 
133 
134 
135 /*******************************************************************************
136 * Function Prototypes
137 *******************************************************************************/
138 
139 void Cy_BLE_HAL_BlessStart(void);
140 cy_en_ble_api_result_t Cy_BLE_HAL_RegisterCommonPipeCallback(void);
141 
142 void Cy_BLE_HAL_DelayUs(uint16_t delayVal);
143 void Cy_BLE_HAL_DelayMs(uint32_t delayVal);
144 
145 void Cy_BLE_HAL_EnableBlessInterrupt(void);
146 void Cy_BLE_HAL_DisableBlessInterrupt(void);
147 
148 uint32_t Cy_BLE_HAL_GetIpBlockVersion(void);
149 uint32_t Cy_BLE_HAL_GetIcPackageType(void);
150 uint64_t Cy_BLE_HAL_GetUniqueId(void);
151 uint16_t Cy_BLE_HAL_GetSiliconId(void);
152 bool Cy_BLE_HAL_IsDefinedTx5dbm(void);
153 
154 uint32_t Cy_BLE_HAL_EnterCriticalSection(void);
155 void Cy_BLE_HAL_ExitCriticalSection(uint32_t interruptState);
156 cy_en_syspm_status_t Cy_BLE_HAL_SysPmSleep(cy_en_syspm_waitfor_t enWaitFor);
157 void Cy_BLE_HAL_SimoBuckSetVoltage2(cy_en_syspm_simo_buck_voltage2_t voltage);
158 
159 /* Stack Interface to clock */
160 uint8_t Cy_BLE_HAL_IsEcoCpuClockSrc(void);
161 uint8_t Cy_BLE_HAL_IsWcoLfclkSrc(void);
162 uint32_t Cy_BLE_HAL_EcoGetFrequency(void);
163 int32_t Cy_BLE_HAL_ClkMeasurementCountersDone(void);
164 uint32_t Cy_BLE_HAL_ClkMeasurementCountersGetFreq(bool measuredClock, uint32_t refClkFreq);
165 uint32_t Cy_BLE_HAL_StartClkMeasurementCounters(cy_en_meas_clks_t clock1, uint32_t count1, cy_en_meas_clks_t clock2);
166 int32_t Cy_BLE_HAL_PiloTrim(uint32_t piloFreq);
167 cy_en_clklf_in_sources_t Cy_BLE_HAL_LfClkGetSource(void);
168 #if ((CY_SYSCLK_DRV_VERSION_MAJOR == 2) && (CY_SYSCLK_DRV_VERSION_MINOR <= 10)) || \
169      (CY_SYSCLK_DRV_VERSION_MAJOR < 2)
170 void Cy_BLE_HAL_SetPiloTrimStep(uint32_t stepSize);
171 int32_t Cy_BLE_HAL_TryPiloTrim(uint32_t piloFreq, uint32_t targetFreq, uint32_t stepSize);
172 #endif /* (CY_SYSCLK_DRV_VERSION_MAJOR == 2) && (CY_SYSCLK_DRV_VERSION_MINOR <= 10) */
173 cy_en_ble_api_result_t Cy_BLE_HAL_EnableDefaultEco(void);
174 
175 /* Stack Interface to flash */
176 cy_en_ble_api_result_t Cy_BLE_HAL_NvramWrite(const uint8_t buffer[], const uint8_t varFlash[],
177                                              uint32_t length, uint32_t mode);
178 cy_en_ble_api_result_t Cy_BLE_HAL_StackNvramWrite(const uint8_t buffer[], const uint8_t varFlash[], uint32_t length);
179 bool Cy_BLE_HAL_NvramWriteIsBusy(void);
180 
181 /* CRC APIs */
182 uint8_t Cy_BLE_HAL_CalcCRC8(uint8_t data[], uint32_t length);
183 uint8_t Cy_BLE_HAL_BlockCalcCRC8(uint8_t data[], uint32_t length, uint8_t crcSeed);
184 
185 cy_en_ble_api_result_t Cy_BLE_HAL_RadioCalibrationRead(void *data, uint32_t length);
186 cy_en_ble_api_result_t Cy_BLE_HAL_RadioCalibrationWrite(const void *data, uint32_t length);
187 
188 /* API for calculating absolute values for Temperature and Voltage monitor */
189 void Cy_BLE_HAL_RadioGetAbsVbat(const uint16_t *raw, uint16_t *vbat);
190 void Cy_BLE_HAL_RadioGetAbsTemp(const uint16_t *raw, int16_t *temp);
191 
192 void Cy_BLE_HAL_SetConfigStructure(const cy_stc_ble_config_t *configPtr);
193 
194 
195 void Cy_BLE_HAL_MappingEccHeapInit(uint8_t *heapMem, uint8_t numOfConn);
196 void Cy_BLE_HAL_MappingEccHeapDeInit(void);
197 uint16_t Cy_BLE_HAL_MappingEccGetFeatureHeapReq(uint8_t numOfConn);
198 cy_en_ble_api_result_t Cy_BLE_HAL_MappingEccGenerateSecurityKeypair(uint8_t p_publicKey[],
199                                                                     uint8_t p_privateKey[],
200                                                                     uint8_t random[]);
201 cy_en_ble_api_result_t Cy_BLE_HAL_MappingEccGenerateDHKey(const uint8_t p_publicKey[],
202                                                           const uint8_t p_privateKey[],
203                                                           uint8_t p_secret[], uint8_t ci);
204 cy_en_ble_api_result_t Cy_BLE_HAL_MappingEccValidPublicKey(const uint8_t p_publicKey[]);
205 cy_en_ble_api_result_t Cy_BLE_HAL_MappingPairingLocalPublicKeyHandler(void *param);
206 cy_en_ble_api_result_t Cy_BLE_HAL_MappingPairingRemoteKeyHandler(void *param);
207 cy_en_ble_api_result_t Cy_BLE_HAL_MappingPairingDhkeyHandler(void *param);
208 cy_en_ble_api_result_t Cy_BLE_HAL_MappingPairingDhkeyCheckHandler(void *param);
209 cy_en_ble_api_result_t Cy_BLE_HAL_MappingPairingKeypressNotificationHandler(void *param);
210 cy_en_ble_api_result_t Cy_BLE_HAL_MappingPairingRandHandler(void * param);
211 cy_en_ble_api_result_t Cy_BLE_HAL_MappingPairingConfirmHandler(void *param);
212 cy_en_ble_api_result_t Cy_BLE_HAL_MappingPairingLrConfirmingHandler(void *param);
213 void Cy_BLE_HAL_MappingTbxDhkeyGenerateComplete(void *param);
214 void Cy_BLE_HAL_MappingTbxLocalPubkeyGenerateComplete(void *param);
215 cy_en_ble_api_result_t Cy_BLE_HAL_MappingTbxGenerateLocalP256PublicKey(uint8_t param);
216 cy_en_ble_api_result_t Cy_BLE_HAL_MappingTbxGenerateDHkey(void  *param1, void  *param2, uint8_t param3);
217 void Cy_BLE_HAL_MappingSmpScCmacComplete(uint8_t param);
218 cy_en_ble_api_result_t Cy_BLE_HAL_MappingSeSmpScUserPasskeyHandler(void *param, void *param2);
219 void Cy_BLE_HAL_MappingEccPointMult(uint8_t param);
220 
221 void Cy_BLE_HAL_UART_Start(void);
222 void Cy_BLE_HAL_UART_Stop(void);
223 void Cy_BLE_HAL_UART_Transmit(uint8_t *dataBuf, uint8_t length);
224 void Cy_BLE_HAL_UART_Interrupt(void);
225 void Cy_BLE_HAL_UART_EnableInterrupt(void);
226 void Cy_BLE_HAL_UART_DisableInterrupt(void);
227 
228 void Cy_BLE_HAL_EnableControllerIpcPipeInterrupt(void);
229 void Cy_BLE_HAL_DisableControllerIpcPipeInterrupt(void);
230 void Cy_BLE_HAL_EnableHostIpcPipeInterrupt(void);
231 void Cy_BLE_HAL_DisableHostIpcPipeInterrupt(void);
232 
233 cy_en_ble_api_result_t Cy_BLE_MappingIsControllerActive(cy_en_ble_controller_sleep_mode_t checkForMode);
234 bool Cy_BLE_HAL_MappingBlessInterruptHandler(void);
235 void Cy_BLE_MappingLlIsrExitLowPowerMode(void);
236 void Cy_BLE_HAL_MappingUartRxDataHandler(uint8_t byte);
237 void Cy_BLE_HAL_MappingUartTxCompltHandler(void);
238 void Cy_BLE_HAL_MappingSoftHciHostReceiveControllerPkt(cy_stc_ble_hci_tx_packet_info_t *hciPkt);
239 cy_en_ble_api_result_t Cy_BLE_HAL_MappingSoftHciControllerReceiveHostPkt(cy_stc_ble_hci_tx_packet_info_t *hciPkt);
240 cy_en_ble_lp_mode_t Cy_BLE_MappingStackEnterLPM(cy_en_ble_lp_mode_t pwrMode);
241 
242 
243 cy_en_ipc_pipe_status_t Cy_BLE_IPC_ControllerRegisterClientCallbacks(cy_ipc_pipe_callback_ptr_t ctrlMsgRecvCallBack,
244                                                                      cy_ipc_pipe_callback_ptr_t ctrlMsgFlushRecvCallBack);
245 cy_en_ipc_pipe_status_t Cy_BLE_IPC_SendMessageToHost(uint32_t *msg,
246                                                      cy_ipc_pipe_relcallback_ptr_t controllerIpcRelCallBack,
247                                                      cy_ipc_pipe_relcallback_ptr_t controllerPollCallBack);
248 
249 
250 cy_en_ipc_pipe_status_t Cy_BLE_IPC_HostRegisterClientCallbacks(cy_ipc_pipe_callback_ptr_t hostMsgRecvCallBack,
251                                                                cy_ipc_pipe_callback_ptr_t hostMsgFlushRecvCallBack);
252 cy_en_ipc_pipe_status_t Cy_BLE_IPC_SendMessageToController(uint32_t *msg,
253                                                            cy_ipc_pipe_relcallback_ptr_t hostIpcRelCallBack,
254                                                            cy_ipc_pipe_relcallback_ptr_t hostPollCallBack);
255 
256 void Cy_BLE_HAL_IntrNotifyIpcHandler(void);
257 extern cy_ble_intr_callback_t Cy_BLE_InterruptCallback;
258 extern cy_stc_ble_intr_notify_t *intrNotifyPtr;
259 
260 /* APIs to support Soft HCI mode  */
261 cy_en_ble_api_result_t Cy_BLE_HAL_SoftHciHostWritePkt(cy_stc_ble_hci_tx_packet_info_t *hciPktParams);
262 void Cy_BLE_HAL_SoftHciHostReceiveControllerPkt(cy_stc_ble_hci_tx_packet_info_t *hciPkt);
263 cy_en_ble_api_result_t Cy_BLE_HAL_SoftHciControllerReceiveHostPkt(cy_stc_ble_hci_tx_packet_info_t *hciPkt);
264 
265 
266 #ifdef __cplusplus
267 }
268 #endif /* __cplusplus */
269 #endif /* CY_IP_MXBLESS */
270 #endif /* CY_BLE_HALL_PVT_H  */
271 
272 /* [] END OF FILE */
273