1 /***************************************************************************//**
2 * \file cy_ble_hal_pvt.c
3 * \version 3.60
4 *
5 * \brief
6 *  This file contains the source code for the HAL section of the PSoC 6 BLE Middleware.
7 *
8 ********************************************************************************
9 * \copyright
10 * Copyright 2017-2021, Cypress Semiconductor Corporation.  All rights reserved.
11 * You may use this file only in accordance with the license, terms, conditions,
12 * disclaimers, and limitations in the end user license agreement accompanying
13 * the software package with which this file was provided.
14 *******************************************************************************/
15 
16 #include "cy_device.h"
17 #include "cy_syspm.h"
18 #include "cy_sysclk.h"
19 #include "cy_ble_hal_pvt.h"
20 
21 #if defined(CY_IP_MXBLESS)
22 
23 
24 /*******************************************************************************
25 * Global Variables
26 *******************************************************************************/
27 
28 /* BLE application IPC notification handler */
29 cy_ble_app_notify_callback_t Cy_BLE_HostRegisteredCallback = NULL;
30 
31 /* Interrupt Notification Feature variables */
32 cy_ble_intr_callback_t Cy_BLE_InterruptCallback = NULL;
33 cy_stc_ble_intr_notify_t *intrNotifyPtr = NULL;
34 
35 /* Nominal trim step size */
36 #if ((CY_SYSCLK_DRV_VERSION_MAJOR == 2) && (CY_SYSCLK_DRV_VERSION_MINOR <= 10)) || \
37      (CY_SYSCLK_DRV_VERSION_MAJOR < 2)
38 static uint32 piloTrimStepSize = CY_BLE_PILO_TRIM_STEP;
39 #endif /* (CY_SYSCLK_DRV_VERSION_MAJOR == 2) && (CY_SYSCLK_DRV_VERSION_MINOR <= 10) */
40 
41 /* Pointer to BLE configuration structure (uses in hal only) */
42 static cy_stc_ble_config_t *cy_ble_halConfigPtr = NULL;
43 
44 /** BLE interrupt notification structure */
45 cy_stc_ble_intr_notify_t intrNotify =
46 {
47    /* clientID       */  CY_BLE_CYPIPE_MSG_CMD_ID,
48    /* pktType        */  CY_BLE_CYPIPE_INTR_NOTIFY_CMD,
49    /* intrRelMask    */  0x00u,
50    /* configFlag     */  false,
51    /* mask           */  0x00u,
52    /* data           */  0x00u,
53    /* userIpcChan    */  0x00u,
54    /* userIpcIntr    */  0x00u,
55    /* userIntrPrior  */  0x00u
56 };
57 
58 
59 /*******************************************************************************
60 * Function Name: Cy_BLE_HAL_BlessStart
61 ****************************************************************************//**
62 *
63 *  Starts Interrupt Controller.
64 *
65 *******************************************************************************/
Cy_BLE_HAL_BlessStart(void)66 void Cy_BLE_HAL_BlessStart(void)
67 {
68     /* Initialize HAL */
69     Cy_BLE_HAL_Init();
70 
71     /* Configures external power amplifier outputs. */
72     Cy_BLE_ConfigureExtPA(BLE_BLESS_EXT_PA_LNA_CTRL_ENABLE_EXT_PA_LNA_Msk);
73 
74     /* Enable BLESS ISR */
75     NVIC_EnableIRQ((IRQn_Type)cy_ble_halConfigPtr->hw->blessIsrConfig->intrSrc);
76 }
77 
78 
79 /*******************************************************************************
80 * Function Name: Cy_BLE_HAL_GetIpBlockVersion
81 ****************************************************************************//**
82 *
83 * This function returns the version of m0s8bless ip block.
84 *
85 * \return
86 *   uint32_t bits:
87 *     7:0 - ip version ( 1 - BLE_ver1, 2 - BLE_ver2, 3 - BLE_ver3, 4 - BLE_ver3 (*A) )
88 *     31:8 - reserved for future usage
89 *
90 *******************************************************************************/
Cy_BLE_HAL_GetIpBlockVersion(void)91 uint32_t Cy_BLE_HAL_GetIpBlockVersion(void)
92 {
93     return(CY_BLE_M0S8BLESS_VERSION);
94 }
95 
96 
97 /*******************************************************************************
98 * Function Name: Cy_BLE_HAL_CalcCRC8
99 ****************************************************************************//**
100 *
101 * Implements CRC-8 calculation
102 *
103 * \param data
104 * The data to be used to calculate the CRC.
105 *
106 * \param length
107 * The length of data.
108 *
109 * \return
110 * The calculated CRC value.
111 *
112 *******************************************************************************/
Cy_BLE_HAL_CalcCRC8(uint8_t data[],uint32_t length)113 uint8_t Cy_BLE_HAL_CalcCRC8(uint8_t data[], uint32_t length)
114 {
115     return (Cy_BLE_HAL_BlockCalcCRC8(data, length, 0u));
116 }
117 
118 
119 /*******************************************************************************
120 * Function Name: Cy_BLE_HAL_BlockCalcCRC8
121 ****************************************************************************//**
122 *
123 * Implements CRC-8 calculation
124 *
125 * \param data
126 * The data to be used to calculate the CRC.
127 *
128 * \param length
129 * The length of data.
130 *
131 * \param crcSeed
132 * Init value of CRC. Use in case if we need calculate large buffer size by block
133 *
134 * \return
135 * The calculated CRC value.
136 *
137 *******************************************************************************/
Cy_BLE_HAL_BlockCalcCRC8(uint8_t data[],uint32_t length,uint8_t crcSeed)138 uint8_t Cy_BLE_HAL_BlockCalcCRC8(uint8_t data[], uint32_t length, uint8_t crcSeed)
139 {
140     const uint8_t Crc8Table[256] = {
141     0x00u, 0x31u, 0x62u, 0x53u, 0xC4u, 0xF5u, 0xA6u, 0x97u,
142     0xB9u, 0x88u, 0xDBu, 0xEAu, 0x7Du, 0x4Cu, 0x1Fu, 0x2Eu,
143     0x43u, 0x72u, 0x21u, 0x10u, 0x87u, 0xB6u, 0xE5u, 0xD4u,
144     0xFAu, 0xCBu, 0x98u, 0xA9u, 0x3Eu, 0x0Fu, 0x5Cu, 0x6Du,
145     0x86u, 0xB7u, 0xE4u, 0xD5u, 0x42u, 0x73u, 0x20u, 0x11u,
146     0x3Fu, 0x0Eu, 0x5Du, 0x6Cu, 0xFBu, 0xCAu, 0x99u, 0xA8u,
147     0xC5u, 0xF4u, 0xA7u, 0x96u, 0x01u, 0x30u, 0x63u, 0x52u,
148     0x7Cu, 0x4Du, 0x1Eu, 0x2Fu, 0xB8u, 0x89u, 0xDAu, 0xEBu,
149     0x3Du, 0x0Cu, 0x5Fu, 0x6Eu, 0xF9u, 0xC8u, 0x9Bu, 0xAAu,
150     0x84u, 0xB5u, 0xE6u, 0xD7u, 0x40u, 0x71u, 0x22u, 0x13u,
151     0x7Eu, 0x4Fu, 0x1Cu, 0x2Du, 0xBAu, 0x8Bu, 0xD8u, 0xE9u,
152     0xC7u, 0xF6u, 0xA5u, 0x94u, 0x03u, 0x32u, 0x61u, 0x50u,
153     0xBBu, 0x8Au, 0xD9u, 0xE8u, 0x7Fu, 0x4Eu, 0x1Du, 0x2Cu,
154     0x02u, 0x33u, 0x60u, 0x51u, 0xC6u, 0xF7u, 0xA4u, 0x95u,
155     0xF8u, 0xC9u, 0x9Au, 0xABu, 0x3Cu, 0x0Du, 0x5Eu, 0x6Fu,
156     0x41u, 0x70u, 0x23u, 0x12u, 0x85u, 0xB4u, 0xE7u, 0xD6u,
157     0x7Au, 0x4Bu, 0x18u, 0x29u, 0xBEu, 0x8Fu, 0xDCu, 0xEDu,
158     0xC3u, 0xF2u, 0xA1u, 0x90u, 0x07u, 0x36u, 0x65u, 0x54u,
159     0x39u, 0x08u, 0x5Bu, 0x6Au, 0xFDu, 0xCCu, 0x9Fu, 0xAEu,
160     0x80u, 0xB1u, 0xE2u, 0xD3u, 0x44u, 0x75u, 0x26u, 0x17u,
161     0xFCu, 0xCDu, 0x9Eu, 0xAFu, 0x38u, 0x09u, 0x5Au, 0x6Bu,
162     0x45u, 0x74u, 0x27u, 0x16u, 0x81u, 0xB0u, 0xE3u, 0xD2u,
163     0xBFu, 0x8Eu, 0xDDu, 0xECu, 0x7Bu, 0x4Au, 0x19u, 0x28u,
164     0x06u, 0x37u, 0x64u, 0x55u, 0xC2u, 0xF3u, 0xA0u, 0x91u,
165     0x47u, 0x76u, 0x25u, 0x14u, 0x83u, 0xB2u, 0xE1u, 0xD0u,
166     0xFEu, 0xCFu, 0x9Cu, 0xADu, 0x3Au, 0x0Bu, 0x58u, 0x69u,
167     0x04u, 0x35u, 0x66u, 0x57u, 0xC0u, 0xF1u, 0xA2u, 0x93u,
168     0xBDu, 0x8Cu, 0xDFu, 0xEEu, 0x79u, 0x48u, 0x1Bu, 0x2Au,
169     0xC1u, 0xF0u, 0xA3u, 0x92u, 0x05u, 0x34u, 0x67u, 0x56u,
170     0x78u, 0x49u, 0x1Au, 0x2Bu, 0xBCu, 0x8Du, 0xDEu, 0xEFu,
171     0x82u, 0xB3u, 0xE0u, 0xD1u, 0x46u, 0x77u, 0x24u, 0x15u,
172     0x3Bu, 0x0Au, 0x59u, 0x68u, 0xFFu, 0xCEu, 0x9Du, 0xACu };
173 
174     uint8_t crc = crcSeed;
175 
176     while (length > 0u)
177     {
178         crc = Crc8Table[crc ^ *data];
179         data++;
180         length--;
181     }
182 
183     return(crc);
184 }
185 
186 
187 /*******************************************************************************
188 * Function Name: Cy_BLE_HAL_GetIcPackageType
189 ****************************************************************************//**
190 *
191 *  This function returns package type
192 *
193 *  return:
194 *   0 - BGA_116 package
195 *   1 - CSP_104 package
196 *   2 - BGA_124 package
197 *   3 - QFN_68 package
198 *
199 *******************************************************************************/
Cy_BLE_HAL_GetIcPackageType(void)200 uint32_t Cy_BLE_HAL_GetIcPackageType(void)
201 {
202     /* Constants used in this function */
203     const uint8_t PACKAGE_68_PINS  = 68u;
204     const uint8_t PACKAGE_116_PINS = 116u;
205     const uint8_t PACKAGE_104_PINS = 104u;
206     const uint8_t PACKAGE_124_PINS = 124u;
207 
208     /* Get package type */
209     uint8_t pType = (uint8_t)
210             (cy_ble_halConfigPtr->stackParam->packageType & CY_BLE_PACKAGE_TYPE_MASK);
211 
212     /* Get package pins number */
213     uint8_t pinCount = (uint8_t)
214             (cy_ble_halConfigPtr->stackParam->packageType >> CY_BLE_PACKAGE_PIN_NUM_OFFSET);
215 
216     cy_en_ble_ic_package_type_t ret;
217 
218     if( ((pType == CY_BLE_PACKAGE_BGA) && (pinCount == 0u)) ||
219         ((pType == CY_BLE_PACKAGE_BGA) && (pinCount == PACKAGE_116_PINS)) )
220     {
221         ret = CY_BLE_IC_PACKAGE_TYPE_BGA_116;
222     }
223     else if( ((pType == CY_BLE_PACKAGE_CSP) && (pinCount == 0u)) ||
224              ((pType == CY_BLE_PACKAGE_CSP) && (pinCount == PACKAGE_104_PINS)) )
225     {
226         ret = CY_BLE_IC_PACKAGE_TYPE_CSP_104;
227     }
228     else if((pType == CY_BLE_PACKAGE_BGA) && (pinCount == PACKAGE_124_PINS))
229     {
230         ret = CY_BLE_IC_PACKAGE_TYPE_BGA_124;
231     }
232     else if((pType == CY_BLE_PACKAGE_QFN) && (pinCount == PACKAGE_68_PINS))
233     {
234         ret = CY_BLE_IC_PACKAGE_TYPE_QFN_68;
235     }
236     else
237     {
238         ret = CY_BLE_IC_PACKAGE_TYPE_BGA_116;
239     }
240 
241     return((uint32_t)ret);
242 }
243 
244 
245 /*******************************************************************************
246 * Function Name: Cy_BLE_HAL_GetUniqueId
247 ****************************************************************************//**
248 * Interface to Cy_SysLib_GetUniqueId function.
249 *
250 * This function returns the silicon unique ID.
251 * The ID includes Die lot[3]#, Die Wafer#, Die X, Die Y, Die Sort#, Die Minor
252 * and Die Year.
253 *
254 * \return  A combined 64-bit unique ID.
255 *          [63:57] - DIE_YEAR
256 *          [56:56] - DIE_MINOR
257 *          [55:48] - DIE_SORT
258 *          [47:40] - DIE_Y
259 *          [39:32] - DIE_X
260 *          [31:24] - DIE_WAFER
261 *          [23:16] - DIE_LOT[2]
262 *          [15: 8] - DIE_LOT[1]
263 *          [ 7: 0] - DIE_LOT[0]
264 *
265 *******************************************************************************/
Cy_BLE_HAL_GetUniqueId(void)266 uint64_t Cy_BLE_HAL_GetUniqueId(void)
267 {
268     return(Cy_SysLib_GetUniqueId());
269 }
270 
271 
272 /*******************************************************************************
273 * Function Name: Cy_BLE_HAL_GetSiliconId
274 ****************************************************************************//**
275 *
276 * This function returns the Silicon ID of the device.
277 *
278 * \return  Silicon ID
279 *
280 *******************************************************************************/
Cy_BLE_HAL_GetSiliconId(void)281 uint16_t Cy_BLE_HAL_GetSiliconId(void)
282 {
283     return(SFLASH_SILICON_ID);
284 }
285 
286 
287 /*******************************************************************************
288 * Function Name: Cy_BLE_HAL_IsDefinedTx5dbm
289 ****************************************************************************//**
290 *
291 *  This function returns true if Tx 5dbm mode is defined
292 *
293 * return:
294 *  true  - Tx 5dbm mode is defined
295 *  false - Tx 5dbm mode is not defined
296 *
297 *******************************************************************************/
Cy_BLE_HAL_IsDefinedTx5dbm(void)298 bool Cy_BLE_HAL_IsDefinedTx5dbm(void)
299 {
300     bool ret = false;
301 
302     if((cy_ble_halConfigPtr != NULL) &&
303        (cy_ble_halConfigPtr->stackParam->tx5dbmModeEn))
304     {
305         ret = true;
306     }
307     return(ret);
308 }
309 
310 
311 /*******************************************************************************
312 * Function Name: Cy_BLE_HAL_SoftHciHostWritePkt
313 ****************************************************************************//**
314 *
315 * This function writes received package from host to controller queue for
316 * processing.
317 *
318 * \param hciPktParams
319 *  Pointer to the received package from host.
320 *
321 *  \return
322 *   CY_BLE_SUCCESS - A successful operation,
323 *   CY_BLE_ERROR_INSUFFICIENT_RESOURCES - BLE Controller queue is full.
324 *
325 *******************************************************************************/
Cy_BLE_HAL_SoftHciHostWritePkt(cy_stc_ble_hci_tx_packet_info_t * hciPktParams)326 cy_en_ble_api_result_t Cy_BLE_HAL_SoftHciHostWritePkt(cy_stc_ble_hci_tx_packet_info_t *hciPktParams)
327 {
328     return Cy_BLE_HAL_MappingSoftHciControllerReceiveHostPkt(hciPktParams);
329 }
330 
331 
332 /*******************************************************************************
333 ***
334 ****   BLE Stack Interface to flash
335 ***
336 *******************************************************************************/
337 
338 /*******************************************************************************
339 * Function Name: Cy_BLE_HAL_NvramWrite
340 ****************************************************************************//**
341 *
342 * This function writes the data to the NVRAM store. It will check the
343 * appropriate alignment of a start address and also perform an address range
344 * check based on the length before performing the write operation.
345 * This function performs memory compare and writes only row where there are new
346 * data to write.
347 *
348 * \param buffer:   Pointer to the buffer containing the data to be stored.
349 * \param varFlash: Pointer to the array or variable in the flash.
350 * \param length:   The length of the data in bytes.
351 * \param mode:     Flash operation mode:
352 *                     CY_BLE_FLASH_NON_BLOCKING_MODE - operate with non
353 *                     blocking flash API
354 *                     CY_BLE_FLASH_BLOCKING_MODE - operate with blocking
355 *                     flash API
356 *
357 * \return
358 *  CY_BLE_SUCCESS                         | A successful write
359 *  CY_BLE_ERROR_INVALID_PARAMETER         | At least one of the input parameters is invalid
360 *  CY_BLE_ERROR_FLASH_WRITE               | Error in flash write
361 *  CY_BLE_ERROR_FLASH_WRITE_NOT_PERMITED  | Flash operation is not permitted (see Note)
362 *
363 * \note: flash operation is not permitted with protection context (PC)
364 *        value > 0 and core voltage 0.9V, because of a preproduction
365 *        hardware limitation.
366 *
367 *******************************************************************************/
Cy_BLE_HAL_NvramWrite(const uint8_t buffer[],const uint8_t varFlash[],uint32_t length,uint32_t mode)368 cy_en_ble_api_result_t Cy_BLE_HAL_NvramWrite(const uint8_t buffer[],
369                                              const uint8_t varFlash[],
370                                              uint32_t length,
371                                              uint32_t mode)
372 {
373     cy_en_ble_api_result_t bleReturn;
374     cy_en_flashdrv_status_t flashReturn = CY_FLASH_DRV_SUCCESS;
375     cy_en_syspm_status_t syspmReturn = CY_SYSPM_SUCCESS;
376     uint32_t writeBuffer[CY_FLASH_SIZEOF_ROW / sizeof(uint32_t)];
377     uint32_t rowId;
378     uint32_t dstIndex;
379     uint32_t srcIndex = 0u;
380     uint32_t eeOffset;
381     uint32_t byteOffset;
382     bool rowsNotEqual;
383     uint8_t *writeBufferPointer;
384 
385     eeOffset = (uint32_t)varFlash;
386     writeBufferPointer = (uint8_t*)writeBuffer;
387 
388     /* Make sure, that varFlash[] points to application flash or auxiliary flash (AUXflash) Emulated EEPROM flash */
389     if( (eeOffset >= CY_FLASH_BASE) ||
390         ((eeOffset >= CY_EM_EEPROM_BASE) &&
391          ((eeOffset + length) <= (CY_EM_EEPROM_BASE + CY_EM_EEPROM_SIZE))) ||
392         ((eeOffset >= (uint32_t)SFLASH_BLE_DEVICE_ADDRESS) &&
393          ((eeOffset + length) <= ((uint32_t)SFLASH_BLE_DEVICE_ADDRESS + CY_FLASH_SIZEOF_ROW))) )
394     {
395         cy_en_syspm_simo_buck_voltage1_t simoVoltage = CY_SYSPM_SIMO_BUCK_OUT1_VOLTAGE_1_1V;
396         cy_en_syspm_ldo_voltage_t ldoVoltage = CY_SYSPM_LDO_VOLTAGE_1_1V;
397         bool writeFlag = false;
398 
399         /* Increase core voltage for write to flash operation */
400         if(Cy_SysPm_SimoBuckIsEnabled())
401         {
402             simoVoltage = Cy_SysPm_SimoBuckGetVoltage1();
403             if(simoVoltage == CY_SYSPM_SIMO_BUCK_OUT1_VOLTAGE_0_9V)
404             {   /* Increase core voltage (SIMO) for write to flash operation */
405                 do /* Polls whether syspm is not busy */
406                 {
407                     syspmReturn = Cy_SysPm_BuckSetVoltage1(CY_SYSPM_SIMO_BUCK_OUT1_VOLTAGE_1_1V);
408                 } while(syspmReturn == CY_SYSPM_CANCELED);
409             }
410         }
411         else
412         {
413             ldoVoltage = Cy_SysPm_LdoGetVoltage();
414             if(ldoVoltage == CY_SYSPM_LDO_VOLTAGE_0_9V)
415             {   /* Increase core voltage (LDO) for write to flash operation */
416                 do /* Polls whether syspm is not busy */
417                 {
418                     syspmReturn = Cy_SysPm_LdoSetVoltage(CY_SYSPM_LDO_VOLTAGE_1_1V);
419                 } while(syspmReturn == CY_SYSPM_CANCELED);
420             }
421         }
422 
423         /*
424          * Increase core voltage to 1.1V for Flash operation is not permitted
425          * with protection context (PC) value > 0, because of a preproduction
426          * hardware limitation.
427          */
428         CY_ASSERT(syspmReturn != CY_SYSPM_INVALID_STATE);
429 
430         eeOffset -= CY_FLASH_BASE;
431         rowId = eeOffset / CY_FLASH_SIZEOF_ROW;
432         byteOffset = CY_FLASH_SIZEOF_ROW * rowId;
433 
434         /* Inform Controller core about write operation */
435         while (CY_IPC_SEMA_SUCCESS != Cy_IPC_Sema_Set(CY_BLE_SEMA, false))
436         {
437             /* Polls whether sema is not set */
438         }
439 
440         while((syspmReturn == CY_SYSPM_SUCCESS) &&
441               (flashReturn == CY_FLASH_DRV_SUCCESS) && (srcIndex < length))
442         {
443             rowsNotEqual = false;
444             /* Copy data to the write buffer either from the source buffer or from the flash */
445             for(dstIndex = 0u; dstIndex < CY_FLASH_SIZEOF_ROW; dstIndex++)
446             {
447                 if((byteOffset >= eeOffset) && (srcIndex < length))
448                 {
449                     uint8_t bufferValue;
450 
451                     bufferValue = (buffer == NULL) ? 0u : buffer[srcIndex];
452 
453                     writeBufferPointer[dstIndex] = bufferValue;
454                     /* Detect that row programming is required */
455                     if((rowsNotEqual == false) && (CY_GET_REG8(CY_FLASH_BASE + byteOffset) != bufferValue))
456                     {
457                         rowsNotEqual = true;
458                     }
459                     srcIndex++;
460                 }
461                 else
462                 {
463                     writeBufferPointer[dstIndex] = CY_GET_REG8(CY_FLASH_BASE + byteOffset);
464                 }
465                 byteOffset++;
466             }
467 
468             if((rowsNotEqual) && (syspmReturn == CY_SYSPM_SUCCESS))
469             {
470                 uint32_t oprTimeout = CY_BLE_FLASH_OPR_TIMEOUT; /* us */
471                 if(mode == CY_BLE_FLASH_NON_BLOCKING_MODE) /* NON-BLOCKING FLASH OPERATION */
472                 {
473                     /* Perform Erase flash row operation */
474                     oprTimeout = CY_BLE_FLASH_OPR_TIMEOUT; /* us */
475                     do
476                     {
477                         flashReturn = Cy_Flash_StartErase((rowId * CY_FLASH_SIZEOF_ROW) + CY_FLASH_BASE);
478 
479                         /* Timeout if flash operation return error */
480                         oprTimeout--;
481                         Cy_SysLib_DelayUs(1u);
482 
483                     }while(((flashReturn & CY_FLASH_ID_ERROR) == CY_FLASH_ID_ERROR) && (oprTimeout != 0u));
484 
485                     /* Waiting completion of the Erase flash row operation */
486                     if(flashReturn == CY_FLASH_DRV_OPERATION_STARTED)
487                     {
488                         /* Polls whether the Flash operation is performed */
489                         do
490                         {
491                             flashReturn = Cy_Flash_IsEraseComplete();
492                         }
493                         while (flashReturn == CY_FLASH_DRV_OPCODE_BUSY);
494                     }
495 
496                     /* Perform Program flash row operation */
497                     if(flashReturn == CY_FLASH_DRV_SUCCESS)
498                     {
499                         oprTimeout = CY_BLE_FLASH_OPR_TIMEOUT; /* us */
500 
501                         /* Program flash row */
502                         do
503                         {
504                             flashReturn = Cy_Flash_StartProgram((rowId * CY_FLASH_SIZEOF_ROW) + CY_FLASH_BASE,
505                                                                  writeBuffer);
506 
507                             /* Timeout if flash operation return error */
508                             oprTimeout--;
509                             Cy_SysLib_DelayUs(1u);
510 
511                         }while(((flashReturn & CY_FLASH_ID_ERROR) == CY_FLASH_ID_ERROR) && (oprTimeout != 0u));
512                     }
513 
514                     /* Waiting completion of the Program flash row operation */
515                     if(flashReturn == CY_FLASH_DRV_OPERATION_STARTED)
516                     {
517                         /* Polls whether the Flash operation is performed */
518                         do
519                         {
520                             flashReturn = Cy_Flash_IsOperationComplete();
521                         }
522                         while (flashReturn == CY_FLASH_DRV_OPCODE_BUSY);
523                     }
524                 }
525                 else /* BLOCKING FLASH OPERATION */
526                 {
527                     /* Perform Write flash row operation (in blocking mode) */
528                     flashReturn = Cy_Flash_WriteRow((rowId * CY_FLASH_SIZEOF_ROW) + CY_FLASH_BASE,
529                                                      writeBuffer);
530                 }
531                 writeFlag = true;
532             }
533 
534             /* Go to the next row */
535             rowId++;
536         }
537 
538         /* Clear Flash Cache and Buffer after write operation */
539         if(writeFlag == true)
540         {
541             Cy_SysLib_ClearFlashCacheAndBuffer();
542         }
543 
544         /* Inform BLE syspm callback about complete of write operation */
545         while (CY_IPC_SEMA_SUCCESS != Cy_IPC_Sema_Clear(CY_BLE_SEMA, false))
546         {
547             /* Polls whether sema is not clear */
548         }
549 
550         /* Return core voltage */
551         if(syspmReturn == CY_SYSPM_SUCCESS)
552         {
553             /* Return core voltage (SIMO) */
554             if((Cy_SysPm_SimoBuckIsEnabled()) && (simoVoltage == CY_SYSPM_SIMO_BUCK_OUT1_VOLTAGE_0_9V))
555             {
556                 do /* Polls whether syspm is not busy */
557                 {
558                     syspmReturn = Cy_SysPm_BuckSetVoltage1(CY_SYSPM_SIMO_BUCK_OUT1_VOLTAGE_0_9V);
559                 } while(syspmReturn == CY_SYSPM_CANCELED);
560             }
561 
562             /* Return core voltage (LDO) */
563             if((Cy_SysPm_LdoIsEnabled()) && (ldoVoltage == CY_SYSPM_LDO_VOLTAGE_0_9V))
564             {
565                 do /* Polls whether syspm is not busy */
566                 {
567                     syspmReturn = Cy_SysPm_LdoSetVoltage(CY_SYSPM_LDO_VOLTAGE_0_9V);
568                 } while(syspmReturn == CY_SYSPM_CANCELED);
569             }
570         }
571     }
572     else
573     {
574         flashReturn = CY_FLASH_DRV_INVALID_INPUT_PARAMETERS;
575     }
576 
577     /* Return BLE error code */
578     if(syspmReturn != CY_SYSPM_SUCCESS)
579     {
580         bleReturn = CY_BLE_ERROR_FLASH_WRITE_NOT_PERMITED;
581     }
582     else
583     {
584         switch(flashReturn)
585         {
586             case CY_FLASH_DRV_SUCCESS:
587                 bleReturn = CY_BLE_SUCCESS;
588                 break;
589 
590             case CY_FLASH_DRV_INVALID_INPUT_PARAMETERS:
591             case CY_FLASH_DRV_INVALID_FLASH_ADDR:
592                 bleReturn = CY_BLE_ERROR_INVALID_PARAMETER;
593                 break;
594 
595             default:
596                 bleReturn = CY_BLE_ERROR_FLASH_WRITE;
597                 break;
598         }
599     }
600     return(bleReturn);
601 }
602 
603 
604 /*******************************************************************************
605 * Function Name: Cy_BLE_HAL_StackNvramWrite
606 ****************************************************************************//**
607 *
608 *  This function is used by stack to perform flash write
609 *
610 *  This function is designated for internal usage.
611 *
612 * \param buffer:   Pointer to the buffer containing the data to be stored.
613 * \param varFlash: Pointer to the array or variable in the flash.
614 * \param length:   The length of the data in bytes.
615 *
616 * \return
617 *  CY_BLE_SUCCESS                       A successful write
618 *  CY_BLE_INFO_FLASH_WRITE_IN_PROGRESS  Row writing in progress
619 *  CY_BLE_ERROR_INVALID_PARAMETER       At least one of the input parameters is invalid
620 *  CY_BLE_ERROR_FLASH_WRITE             Error in flash Write
621 *
622 *******************************************************************************/
Cy_BLE_HAL_StackNvramWrite(const uint8_t buffer[],const uint8_t varFlash[],uint32_t length)623 cy_en_ble_api_result_t Cy_BLE_HAL_StackNvramWrite(const uint8_t buffer[],
624                                                   const uint8_t varFlash[],
625                                                   uint32_t length)
626 {
627     return(Cy_BLE_HAL_NvramWrite(buffer, varFlash, length, CY_BLE_FLASH_NON_BLOCKING_MODE));
628 }
629 
630 
631 /*******************************************************************************
632 ***
633 ****   BLE Stack Interface to clock
634 ***
635 *******************************************************************************/
636 
637 /*******************************************************************************
638 * Function Name: Cy_BLE_HAL_IsEcoCpuClockSrc
639 ****************************************************************************//**
640 *
641 *  Return 1 if ECO from BLESS in CLK_PATH_SELECT register is selected.
642 *
643 *  \return
644 *  uint8_t: 0 - ECO from BLESS is not used in DWR
645 *           1 - ECO from BLESS is selected in CLK_PATH_SELECT register
646 *
647 *******************************************************************************/
Cy_BLE_HAL_IsEcoCpuClockSrc(void)648 uint8_t Cy_BLE_HAL_IsEcoCpuClockSrc(void)
649 {
650     uint8_t returnVal = 0U;
651     uint32_t i;
652 
653     for(i = 0U; i < CY_SRSS_NUM_CLKPATH; i++)
654     {
655         if(Cy_SysClk_ClkPathGetSource(i) == CY_SYSCLK_CLKPATH_IN_ALTHF)
656         {
657             returnVal = 1U;
658             break;
659         }
660     }
661 
662     return(returnVal);
663 }
664 
665 
666 /*******************************************************************************
667 * Function Name: Cy_BLE_HAL_EcoGetFrequency
668 ****************************************************************************//**
669 *
670 *  Return the frequency of the output of a BLE ECO clock.
671 *
672 *  \return The frequency, in Hz.
673 *
674 *******************************************************************************/
Cy_BLE_HAL_EcoGetFrequency(void)675 uint32_t Cy_BLE_HAL_EcoGetFrequency(void)
676 {
677     return (cy_BleEcoClockFreqHz);
678 }
679 
680 
681 /*******************************************************************************
682 * Function Name: Cy_BLE_HAL_IsWcoLfclkSrc
683 ****************************************************************************//**
684 *
685 *  Check whether WCO or PILO are the source for the low frequency clock (clkLf)
686 *
687 *  \return
688 *  uint8_t: 0 - WCO or PILO are not source for the low frequency clock
689 *           1 - WCO or PILO are source for the low frequency clock
690 *
691 *******************************************************************************/
Cy_BLE_HAL_IsWcoLfclkSrc(void)692 uint8_t Cy_BLE_HAL_IsWcoLfclkSrc(void)
693 {
694     cy_en_clklf_in_sources_t lfclkSrs = Cy_SysClk_ClkLfGetSource();
695     return(((lfclkSrs == CY_SYSCLK_CLKLF_IN_WCO) ||
696             (lfclkSrs == CY_SYSCLK_CLKLF_IN_PILO)) ? 1U : 0U);
697 }
698 
699 
700 /*******************************************************************************
701 * Function Name: Cy_BLE_HAL_LfClkGetSource
702 ****************************************************************************//**
703 *
704 *  Interface to Cy_SysClk_LfClkGetSource function.
705 *
706 *******************************************************************************/
Cy_BLE_HAL_LfClkGetSource(void)707 cy_en_clklf_in_sources_t Cy_BLE_HAL_LfClkGetSource(void)
708 {
709     return(Cy_SysClk_ClkLfGetSource());
710 }
711 
712 
713 /*******************************************************************************
714 * Function Name: Cy_BLE_HAL_ClkMeasurementCountersDone
715 ****************************************************************************//**
716 *
717 *  Interface to Cy_SysClk_ClkMeasurementCountersDone function.
718 *
719 *******************************************************************************/
Cy_BLE_HAL_ClkMeasurementCountersDone(void)720 int32_t Cy_BLE_HAL_ClkMeasurementCountersDone(void)
721 {
722     return((int32_t)Cy_SysClk_ClkMeasurementCountersDone());
723 }
724 
725 
726 /*******************************************************************************
727 * Function Name: Cy_BLE_HAL_ClkMeasurementCountersGetFreq
728 ****************************************************************************//**
729 *
730 *  Interface to Cy_SysClk_ClkMeasurementCountersGetFreq function.
731 *
732 *******************************************************************************/
Cy_BLE_HAL_ClkMeasurementCountersGetFreq(bool measuredClock,uint32_t refClkFreq)733 uint32_t Cy_BLE_HAL_ClkMeasurementCountersGetFreq(bool measuredClock,
734                                                   uint32_t refClkFreq)
735 {
736     return(Cy_SysClk_ClkMeasurementCountersGetFreq(measuredClock, refClkFreq));
737 }
738 
739 
740 /*******************************************************************************
741 * Function Name: Cy_BLE_HAL_StartClkMeasurementCounters
742 ****************************************************************************//**
743 *
744 *  Interface to Cy_SysClk_StartClkMeasurementCounters function.
745 *
746 *******************************************************************************/
Cy_BLE_HAL_StartClkMeasurementCounters(cy_en_meas_clks_t clock1,uint32_t count1,cy_en_meas_clks_t clock2)747 uint32_t Cy_BLE_HAL_StartClkMeasurementCounters(cy_en_meas_clks_t clock1,
748                                                 uint32_t count1,
749                                                 cy_en_meas_clks_t clock2)
750 {
751     cy_en_sysclk_status_t retVal = CY_SYSCLK_INVALID_STATE;
752     if(Cy_IPC_Sema_Status(CY_BLE_SEMA) != CY_IPC_SEMA_STATUS_LOCKED)
753     {
754         retVal = Cy_SysClk_StartClkMeasurementCounters(clock1, count1, clock2);
755     }
756     return((uint32_t) retVal);
757 }
758 
759 #if ((CY_SYSCLK_DRV_VERSION_MAJOR == 2) && (CY_SYSCLK_DRV_VERSION_MINOR <= 10)) || \
760      (CY_SYSCLK_DRV_VERSION_MAJOR < 2)
761 
762 /*******************************************************************************
763 * Function Name: Cy_BLE_HAL_SetPiloTrimStep
764 ****************************************************************************//**
765 *
766 * Set PILO trim step which will be used within BLE stack during periodical PILO
767 * calibration.
768 *
769 * \param stepSize: nominal trim step size.
770 *
771 *******************************************************************************/
Cy_BLE_HAL_SetPiloTrimStep(uint32_t stepSize)772 void Cy_BLE_HAL_SetPiloTrimStep(uint32_t stepSize)
773 {
774     piloTrimStepSize = stepSize;
775 }
776 
777 
778 /*******************************************************************************
779 * Function Name: Cy_BLE_HAL_TryPiloTrim
780 ****************************************************************************//**
781 *
782 * Trims the PILO to be as close to 32,768 Hz as possible.
783 *
784 * \param piloFreq:   current PILO frequency. Call Cy_SysClk_StartClkMeasurementCounters
785 *                    and other measurement functions to obtain the current frequency of
786 *                    the PILO.
787 * \param targetFreq: target frequency.
788 * \param stepSize:   nominal trim step size.
789 *
790 * \return Change in trim value; 0 if done, that is, no change in trim value.
791 *
792 *******************************************************************************/
Cy_BLE_HAL_TryPiloTrim(uint32_t piloFreq,uint32_t targetFreq,uint32_t stepSize)793 int32_t Cy_BLE_HAL_TryPiloTrim(uint32_t piloFreq, uint32_t targetFreq, uint32_t stepSize)
794 {
795     uint32_t newTrim = 0ul;
796     uint32_t curTrim = 0ul;
797 
798     /* Do nothing if piloFreq is already within one trim step from the target */
799     uint32_t diff = (uint32_t)abs((int32_t)piloFreq - (int32_t)targetFreq);
800     if (diff >= stepSize)
801     {
802         curTrim = Cy_SysClk_PiloGetTrim();
803         if (piloFreq > targetFreq)
804         { /* piloFreq too high. Decrease the trim value. */
805             newTrim = curTrim - CY_SYSCLK_DIV_ROUND(piloFreq - targetFreq, stepSize);
806             if ((int32_t)newTrim < 0) /* limit underflow */
807             {
808                 newTrim = 0u;
809             }
810         }
811         else
812         { /* piloFreq too low. Increase the trim value. */
813             newTrim = curTrim + CY_SYSCLK_DIV_ROUND(targetFreq - piloFreq, stepSize);
814             if (newTrim >= SRSS_CLK_PILO_CONFIG_PILO_FFREQ_Msk) /* limit overflow */
815             {
816                 newTrim = SRSS_CLK_PILO_CONFIG_PILO_FFREQ_Msk;
817             }
818         }
819         Cy_SysClk_PiloSetTrim(newTrim);
820     }
821 
822     return ((int32_t)curTrim -(int32_t) newTrim);
823 }
824 #endif /* (CY_SYSCLK_DRV_VERSION_MAJOR == 2) && (CY_SYSCLK_DRV_VERSION_MINOR <= 10) */
825 
826 
827 /*******************************************************************************
828 * Function Name: Cy_BLE_HAL_PiloTrim
829 ****************************************************************************//**
830 *
831 *  Interface to Cy_BLE_HAL_TryPiloTrim function.
832 *
833 *******************************************************************************/
Cy_BLE_HAL_PiloTrim(uint32_t piloFreq)834 int32_t Cy_BLE_HAL_PiloTrim(uint32_t piloFreq)
835 {
836 #if ((CY_SYSCLK_DRV_VERSION_MAJOR == 2) && (CY_SYSCLK_DRV_VERSION_MINOR <= 10)) || \
837      (CY_SYSCLK_DRV_VERSION_MAJOR < 2)
838     return(Cy_BLE_HAL_TryPiloTrim(piloFreq, CY_BLE_PILO_TARGET_FREQ,
839                                   piloTrimStepSize));
840 #else
841     return(Cy_SysClk_PiloTrim(piloFreq));
842 #endif /* (CY_SYSCLK_DRV_VERSION_MAJOR == 2) && (CY_SYSCLK_DRV_VERSION_MINOR <= 10) */
843 }
844 
845 /*******************************************************************************
846 * Function Name: Cy_BLE_HAL_EnableDefaultEco
847 ****************************************************************************//**
848 *  This function enable BLESS ECO clock on defauld configuration.
849 * \return
850 *  CY_BLE_SUCCESS                 - A successful operation
851 *  CY_BLE_ERROR_HARDWARE_FAILURE  - Error during BLE ECO clock configuration
852 *  CY_BLE_ERROR_INVALID_OPERATION - Operation is not permitted
853 *
854 *******************************************************************************/
855 #if 0
856 cy_en_ble_api_result_t Cy_BLE_HAL_EnableDefaultEco(void)
857 {
858     cy_en_ble_api_result_t apiResult = CY_BLE_ERROR_INVALID_OPERATION;
859     cy_en_ble_eco_status_t ecoStatus;
860 
861     if (Cy_BLE_GetState() == CY_BLE_STATE_STOPPED)
862     {
863         ecoStatus = Cy_BLE_EcoConfigure( CY_BLE_DEFAULT_BLESS_ECO_FREQ,
864                                          CY_BLE_DEFAULT_SYS_ECO_CLK_DIV,
865                                          CY_BLE_DEFAULT_CAP_TRIM_VALUE,
866                                          CY_BLE_DEFAULT_OSC_STARTUP_DELAY_LF,
867                                          CY_BLE_DEFAULT_CY_BLE_ECO_VOLTAGE_REG );
868 
869         if ((ecoStatus != CY_BLE_ECO_SUCCESS) &&
870             (ecoStatus != CY_BLE_ECO_ALREADY_STARTED))
871         {
872             /* CY_BLE_ERROR_HARDWARE_FAILURE */
873             apiResult = CY_BLE_ERROR_HARDWARE_FAILURE;
874         }
875         else
876         {
877             apiResult = CY_BLE_SUCCESS;
878         }
879     }
880 
881     return(apiResult);
882 }
883 #endif
884 
885 /*******************************************************************************
886 ***
887 ****   BLE Stack Interface to SysLib / SysPm
888 ***
889 *******************************************************************************/
890 
891 /*******************************************************************************
892 * Function Name: Cy_BLE_HAL_DelayUs
893 ****************************************************************************//**
894 *
895 *  Interface to Cy_SysLib_DelayUs function.
896 *
897 *******************************************************************************/
Cy_BLE_HAL_DelayUs(uint16_t delayVal)898 void Cy_BLE_HAL_DelayUs(uint16_t delayVal)
899 {
900     Cy_SysLib_DelayUs(delayVal);
901 }
902 
903 
904 /*******************************************************************************
905 * Function Name: Cy_BLE_HAL_DelayMs
906 ****************************************************************************//**
907 *
908 *  Interface to Cy_SysLib_Delay function.
909 *
910 *******************************************************************************/
Cy_BLE_HAL_DelayMs(uint32_t delayVal)911 void Cy_BLE_HAL_DelayMs(uint32_t delayVal)
912 {
913     Cy_SysLib_Delay(delayVal);
914 }
915 
916 
917 /*******************************************************************************
918 * Function Name: Cy_BLE_HAL_EnableBlessInterrupt
919 ****************************************************************************//**
920 *
921 *  This function enables BLESS interrupt.
922 *
923 *******************************************************************************/
Cy_BLE_HAL_EnableBlessInterrupt(void)924 void Cy_BLE_HAL_EnableBlessInterrupt(void)
925 {
926     if((cy_ble_halConfigPtr->hw != NULL) &&
927        (cy_ble_halConfigPtr->hw->blessIsrConfig != NULL))
928     {
929         NVIC_EnableIRQ((IRQn_Type)cy_ble_halConfigPtr->hw->blessIsrConfig->intrSrc);
930     }
931 }
932 
933 
934 /*******************************************************************************
935 * Function Name: Cy_BLE_HAL_DisableBlessInterrupt
936 ****************************************************************************//**
937 *
938 *  This function disable BLESS interrupt.
939 *
940 *******************************************************************************/
Cy_BLE_HAL_DisableBlessInterrupt(void)941 void Cy_BLE_HAL_DisableBlessInterrupt(void)
942 {
943     if((cy_ble_halConfigPtr->hw != NULL) &&
944        (cy_ble_halConfigPtr->hw->blessIsrConfig != NULL))
945     {
946         NVIC_DisableIRQ((IRQn_Type)cy_ble_halConfigPtr->hw->blessIsrConfig->intrSrc);
947     }
948 }
949 
950 
951 /*******************************************************************************
952 * Function Name: Cy_BLE_HAL_EnterCriticalSection
953 ****************************************************************************//**
954 *
955 *  Interface to Cy_SysLib_EnterCriticalSection function.
956 *
957 *******************************************************************************/
Cy_BLE_HAL_EnterCriticalSection(void)958 uint32_t Cy_BLE_HAL_EnterCriticalSection(void)
959 {
960     return(Cy_SysLib_EnterCriticalSection());
961 }
962 
963 
964 /*******************************************************************************
965 * Function Name: Cy_BLE_HAL_ExitCriticalSection
966 ****************************************************************************//**
967 *
968 *  Interface to Cy_SysLib_ExitCriticalSection function.
969 *
970 *******************************************************************************/
Cy_BLE_HAL_ExitCriticalSection(uint32_t interruptState)971 void Cy_BLE_HAL_ExitCriticalSection(uint32_t interruptState)
972 {
973     Cy_SysLib_ExitCriticalSection(interruptState);
974 }
975 
976 
977 /*******************************************************************************
978 * Function Name: Cy_BLE_HAL_SysPmSleep
979 ****************************************************************************//**
980 *
981 *  Interface to Cy_SysPm_Sleep function.
982 *
983 *******************************************************************************/
Cy_BLE_HAL_SysPmSleep(cy_en_syspm_waitfor_t enWaitFor)984 cy_en_syspm_status_t Cy_BLE_HAL_SysPmSleep(cy_en_syspm_waitfor_t enWaitFor)
985 {
986     return(Cy_SysPm_CpuEnterSleep(enWaitFor));
987 }
988 
989 
990 /*******************************************************************************
991 * Function Name: Cy_BLE_HAL_SimoBuckSetVoltage2
992 ****************************************************************************//**
993 *
994 *  Interface to Cy_SysPm_SimoBuckSetVoltage2 function with disable the 200 uS
995 *  delay after setting a higher voltage.
996 *
997 *******************************************************************************/
Cy_BLE_HAL_SimoBuckSetVoltage2(cy_en_syspm_simo_buck_voltage2_t voltage)998 void Cy_BLE_HAL_SimoBuckSetVoltage2(cy_en_syspm_simo_buck_voltage2_t voltage)
999 {
1000     Cy_SysPm_SimoBuckSetVoltage2(voltage, false);
1001 }
1002 
1003 
1004 
1005 /*******************************************************************************
1006 ***
1007 ****   Interrupt Notify Feature functions
1008 ***
1009 *******************************************************************************/
1010 
1011 /*******************************************************************************
1012 * Function Name: Cy_BLE_HAL_IntrNotifyIpcHandler
1013 ****************************************************************************//**
1014 *
1015 *  Handles the IPC notification for Interrupt Notification Feature.
1016 *
1017 *******************************************************************************/
Cy_BLE_HAL_IntrNotifyIpcHandler(void)1018 void Cy_BLE_HAL_IntrNotifyIpcHandler(void)
1019 {
1020     uint32_t interruptMasked;
1021     IPC_INTR_STRUCT_Type *intrTypePtr = Cy_IPC_Drv_GetIntrBaseAddr((uint32_t)intrNotify.userIpcIntr);
1022 
1023    /**
1024     * Check that there is really the IPC Notify interrupt,
1025     * because the same line can be used for the IPC Release interrupt.
1026     */
1027 
1028     interruptMasked = Cy_IPC_Drv_ExtractAcquireMask(Cy_IPC_Drv_GetInterruptStatusMasked(intrTypePtr));
1029 
1030     if ((1uL << ((uint32_t)intrNotify.userIpcChan)) == interruptMasked )
1031     {
1032         Cy_IPC_Drv_ClearInterrupt(intrTypePtr, CY_IPC_NO_NOTIFICATION, interruptMasked);
1033 
1034         if(Cy_BLE_InterruptCallback != NULL)
1035         {
1036             /* Call the BLE interrupt feature handler */
1037             Cy_BLE_InterruptCallback(intrNotify.data);
1038         }
1039 
1040         /* Release the IPC channel with the Release interrupt */
1041         (void)Cy_IPC_Drv_LockRelease(Cy_IPC_Drv_GetIpcBaseAddress((uint32_t)intrNotify.userIpcChan),
1042                                     (1uL << (uint32_t)intrNotify.userIpcIntr));
1043     }
1044 }
1045 
1046 
1047 /*******************************************************************************
1048 * Function Name: Cy_BLE_IPC_CommonCallBack
1049 ****************************************************************************//**
1050 *
1051 *  This a callback to be called when a message is received on a pipe from
1052 *  host side. This callback uses for:
1053 *   - Configure BLE Interrupt Notification Feature (pktType: 0xFF)
1054 *   - Initialize and enable BLE controller (pktType: 0x01)
1055 *
1056 
1057 *  \param msgPtr : Pointer to the callback message.
1058 *
1059 *******************************************************************************/
1060 static void Cy_BLE_IPC_CommonCallBack(uint32_t * msgPtr);
Cy_BLE_IPC_CommonCallBack(uint32_t * msgPtr)1061 static void Cy_BLE_IPC_CommonCallBack(uint32_t * msgPtr)
1062 {
1063     cy_stc_ble_ipc_msg_t * ipcMsgPtr = (cy_stc_ble_ipc_msg_t *) msgPtr;
1064 
1065     if((ipcMsgPtr != NULL) && (cy_ble_halConfigPtr != NULL))
1066     {
1067         switch (ipcMsgPtr->pktType)
1068         {
1069             /* Enable BLE controller (use only in BLE dual CPU mode) */
1070             case CY_BLE_CYPIPE_ENABLE_CONTR_CMD:
1071                 /* Store Stack param structure in Controller configuration structure */
1072                 cy_ble_halConfigPtr->stackParam = (cy_stc_ble_stack_params_t *)ipcMsgPtr->data;
1073 
1074                 /* Update System Core Clock values for correct Cy_SysLib_Delay
1075                  * functioning. Expected that System Core Clock is finally initialized
1076                  * on CM4 core */
1077                 SystemCoreClockUpdate();
1078 
1079                 /* Enable BLE controller. Return value Cy_BLE_EnableController store
1080                  * back in data field */
1081                 ipcMsgPtr->data = (uint32_t) Cy_BLE_EnableController();
1082 
1083                 /* Signals host side that operation completed */
1084                 ipcMsgPtr->controllerStarted = true;
1085                 break;
1086 
1087             /* Configure BLE Interrupt Notification Feature */
1088             case CY_BLE_CYPIPE_INTR_NOTIFY_CMD:
1089                 /* Set up Interrupt Notification Feature mask  */
1090                 intrNotifyPtr = (cy_stc_ble_intr_notify_t *)msgPtr;
1091                 intrNotifyPtr->configFlag = true;
1092                 break;
1093 
1094             default:
1095                 break;
1096         }
1097     }
1098 }
1099 
1100 
1101 /*******************************************************************************
1102 * Function Name: Cy_BLE_HAL_RegisterCommonPipeCallback
1103 ****************************************************************************//**
1104 *
1105 *  This function registers IPC pipe callback for PSoC 6 BLE Middleware (for internall
1106 *  usage).
1107 *
1108 *  The PSoC 6 BLE Middleware uses this callback for:
1109 *   - Configure BLE Interrupt Notification Feature,
1110 *   - Initialize and enable BLE controller, when PSoC 6 BLE Middleware operates in
1111 *     BLE dual CPU mode.
1112 *
1113 *  \return
1114 *   CY_BLE_SUCCESS                    A successful operation
1115 *   CY_BLE_ERROR_INVALID_OPERATION    Error during registering PIPE callback
1116 *
1117 *******************************************************************************/
Cy_BLE_HAL_RegisterCommonPipeCallback(void)1118 cy_en_ble_api_result_t Cy_BLE_HAL_RegisterCommonPipeCallback(void)
1119 {
1120     cy_en_ble_api_result_t rc = CY_BLE_SUCCESS;
1121 
1122     if(Cy_IPC_Pipe_RegisterCallback(CY_BLE_IPC_CONTROLLER_ADDR,
1123             &Cy_BLE_IPC_CommonCallBack, CY_BLE_CYPIPE_MSG_CMD_ID) == CY_IPC_PIPE_SUCCESS)
1124     {
1125         rc = CY_BLE_ERROR_INVALID_OPERATION;
1126     }
1127 
1128     return(rc);
1129 }
1130 
1131 
1132 /*******************************************************************************
1133 ***
1134 ****   HAL API for Radio Calibration feature
1135 ***
1136 *******************************************************************************/
1137 
1138 /*******************************************************************************
1139 * Function Name: Cy_BLE_HAL_RadioCalibrationWrite
1140 ****************************************************************************//**
1141 *
1142 *  Write radio calibration data to SFLASH
1143 *
1144 *  \param data:   Pointer to the buffer containing the data to be stored.
1145 *  \param length: The length of the data in bytes.
1146 *
1147 *  \return
1148 *   CY_BLE_SUCCESS                     A successful write
1149 *   CY_BLE_ERROR_INVALID_PARAMETER     At least one of the input parameters is invalid
1150 *   CY_BLE_ERROR_FLASH_WRITE           Error in flash Write
1151 *
1152 *******************************************************************************/
Cy_BLE_HAL_RadioCalibrationWrite(const void * data,uint32_t length)1153 cy_en_ble_api_result_t Cy_BLE_HAL_RadioCalibrationWrite(const void *data, uint32_t length)
1154 {
1155     cy_en_ble_api_result_t rc;
1156 
1157     if((data != NULL) &&
1158        (length <= (CY_FLASH_SIZEOF_ROW - sizeof(cy_stc_ble_bd_addr_t))))
1159     {
1160         rc = Cy_BLE_HAL_NvramWrite(data, (uint8_t *)CY_BLE_SFLASH_BLE_RADIO_CALL_ADDRESS,
1161                                    length, CY_BLE_FLASH_BLOCKING_MODE);
1162     }
1163     else
1164     {
1165         rc = CY_BLE_ERROR_INVALID_PARAMETER;
1166     }
1167     return(rc);
1168 }
1169 
1170 
1171 /*******************************************************************************
1172 * Function Name: Cy_BLE_HAL_RadioCalibrationRead
1173 ****************************************************************************//**
1174 *
1175 *  Read radio calibration data from SFLASH
1176 *
1177 *  \param data:   Pointer to the buffer containing the data to be read from SFASH.
1178 *  \param length: The length of the data in bytes.
1179 *
1180 *  \return
1181 *   CY_BLE_SUCCESS                     A successful read
1182 *   CY_BLE_ERROR_INVALID_PARAMETER     At least one of the input parameters is invalid
1183 *
1184 *******************************************************************************/
Cy_BLE_HAL_RadioCalibrationRead(void * data,uint32_t length)1185 cy_en_ble_api_result_t Cy_BLE_HAL_RadioCalibrationRead(void *data, uint32_t length)
1186 {
1187     cy_en_ble_api_result_t rc = CY_BLE_SUCCESS;
1188 
1189     if((data != NULL) &&
1190        (length <= (CY_FLASH_SIZEOF_ROW - sizeof(cy_stc_ble_bd_addr_t))))
1191     {
1192         (void) memcpy(data, (void *)CY_BLE_SFLASH_BLE_RADIO_CALL_ADDRESS, length);
1193     }
1194     else
1195     {
1196         rc = CY_BLE_ERROR_INVALID_PARAMETER;
1197     }
1198     return(rc);
1199 }
1200 
1201 
1202 /*******************************************************************************
1203 ***
1204 ****   BLE Stack Interface to IPC
1205 ***
1206 *******************************************************************************/
1207 
1208 /* PSoC 6 BLE Middleware IPC Controller handler */
1209 static cy_ipc_pipe_callback_ptr_t Cy_BLE_IPC_CtrlMsgFlushRecvStackHandle;
1210 static void Cy_BLE_IPC_CtrlMsgFlushRecvCallBack(uint32_t * msgPtr);
1211 
1212 /* BLE Stack IPC Host handler */
1213 static cy_ipc_pipe_callback_ptr_t Cy_BLE_IPC_HostMsgRecvStackHandle;
1214 static cy_ipc_pipe_callback_ptr_t Cy_BLE_IPC_HostMsgFlushRecvStackHandle;
1215 
1216 /* PSoC 6 BLE Middleware IPC Host handler */
1217 static void Cy_BLE_IPC_HostMsgFlushRecvCallBack(uint32_t * msgPtr);
1218 static void Cy_BLE_IPC_HostMsgRecvCallBack(uint32_t * msgPtr);
1219 
1220 
1221 /*******************************************************************************
1222 * Function Name: Cy_BLE_HAL_EnableControllerIpcPipeInterrupt
1223 ****************************************************************************//**
1224 *
1225 *   Enables the IPC pipe interrupt on controller side.
1226 *
1227 *******************************************************************************/
Cy_BLE_HAL_EnableControllerIpcPipeInterrupt(void)1228 void Cy_BLE_HAL_EnableControllerIpcPipeInterrupt(void)
1229 {
1230     (void)Cy_IPC_Pipe_EndpointResume(CY_BLE_IPC_CONTROLLER_ADDR);
1231 }
1232 
1233 
1234 /*******************************************************************************
1235 * Function Name: Cy_BLE_HAL_SetConfigStructure
1236 ****************************************************************************//**
1237 *
1238 *   Store BLE configuration structure in a internal HAL pointer.
1239 *
1240 *******************************************************************************/
Cy_BLE_HAL_SetConfigStructure(const cy_stc_ble_config_t * configPtr)1241 __WEAK void Cy_BLE_HAL_SetConfigStructure(const cy_stc_ble_config_t *configPtr)
1242 {
1243     cy_ble_halConfigPtr = (cy_stc_ble_config_t *) configPtr;
1244 }
1245 
1246 
1247 /*******************************************************************************
1248 * Function Name: Cy_BLE_HAL_DisableControllerIpcPipeInterrupt
1249 ****************************************************************************//**
1250 *
1251 *   Disable the IPC pipe interrupt on controller side.
1252 *
1253 *******************************************************************************/
Cy_BLE_HAL_DisableControllerIpcPipeInterrupt(void)1254 void Cy_BLE_HAL_DisableControllerIpcPipeInterrupt(void)
1255 {
1256     (void)Cy_IPC_Pipe_EndpointPause(CY_BLE_IPC_CONTROLLER_ADDR);
1257 }
1258 
1259 
1260 /*******************************************************************************
1261 * Function Name: Cy_BLE_HAL_EnableHostIpcPipeInterrupt
1262 ****************************************************************************//**
1263 *
1264 *   Enables the IPC pipe interrupt on host side.
1265 *
1266 *******************************************************************************/
Cy_BLE_HAL_EnableHostIpcPipeInterrupt(void)1267 void Cy_BLE_HAL_EnableHostIpcPipeInterrupt(void)
1268 {
1269     (void)Cy_IPC_Pipe_EndpointResume(CY_BLE_IPC_HOST_ADDR);
1270 }
1271 
1272 
1273 /*******************************************************************************
1274 * Function Name: Cy_BLE_HAL_DisableHostIpcPipeInterrupt
1275 ****************************************************************************//**
1276 *
1277 *   Disable the IPC pipe interrupt on host side.
1278 *
1279 *******************************************************************************/
Cy_BLE_HAL_DisableHostIpcPipeInterrupt(void)1280 void Cy_BLE_HAL_DisableHostIpcPipeInterrupt(void)
1281 {
1282     (void)Cy_IPC_Pipe_EndpointPause(CY_BLE_IPC_HOST_ADDR);
1283 }
1284 
1285 
1286 /*******************************************************************************
1287 * Function Name: Cy_BLE_IPC_CtrlMsgFlushRecvCallBack
1288 ****************************************************************************//**
1289 *
1290 * This a callback to be called when a message is received on a pipe.
1291 *
1292 * \param msgPtr
1293 * Pointer to the callback message.
1294 *
1295 *******************************************************************************/
Cy_BLE_IPC_CtrlMsgFlushRecvCallBack(uint32_t * msgPtr)1296 static void Cy_BLE_IPC_CtrlMsgFlushRecvCallBack(uint32_t * msgPtr)
1297 {
1298     /* Call BLE Stack IPC handler */
1299     Cy_BLE_IPC_CtrlMsgFlushRecvStackHandle(msgPtr);
1300 }
1301 
1302 
1303 /*******************************************************************************
1304 * Function Name: Cy_BLE_IPC_ControllerRegisterClientCallbacks
1305 ****************************************************************************//**
1306 *
1307 * This function registers a callbacks to be called when a message is received on a pipe.
1308 *
1309 * \param ctrlMsgRecvCallBack
1310 * Pointer to the callback to be called when the handle has received a message.
1311 *
1312 * \param ctrlMsgFlushRecvCallBack
1313 * Pointer to the callback to be called when the handle has received a flush message.
1314 *
1315 *  \return
1316 *    CY_IPC_PIPE_SUCCESS          - Callback registered successfully
1317 *    CY_IPC_PIPE_ERROR_BAD_CLIENT - Client ID out of range, callback not registered.
1318 *
1319 *******************************************************************************/
Cy_BLE_IPC_ControllerRegisterClientCallbacks(cy_ipc_pipe_callback_ptr_t ctrlMsgRecvCallBack,cy_ipc_pipe_callback_ptr_t ctrlMsgFlushRecvCallBack)1320 cy_en_ipc_pipe_status_t Cy_BLE_IPC_ControllerRegisterClientCallbacks(cy_ipc_pipe_callback_ptr_t ctrlMsgRecvCallBack,
1321                                                                      cy_ipc_pipe_callback_ptr_t ctrlMsgFlushRecvCallBack)
1322 {
1323     cy_en_ipc_pipe_status_t returnStatus;
1324 
1325     Cy_BLE_IPC_CtrlMsgFlushRecvStackHandle = ctrlMsgFlushRecvCallBack;
1326 
1327     returnStatus = Cy_IPC_Pipe_RegisterCallback(CY_BLE_IPC_CONTROLLER_ADDR, ctrlMsgRecvCallBack,
1328                                                 CY_BLE_CYPIPE_MSG_SEND_ID);
1329 
1330     if(returnStatus == CY_IPC_PIPE_SUCCESS)
1331     {
1332         returnStatus = Cy_IPC_Pipe_RegisterCallback(CY_BLE_IPC_CONTROLLER_ADDR, &Cy_BLE_IPC_CtrlMsgFlushRecvCallBack,
1333                                                     CY_BLE_CYPIPE_MSG_COMPLETE_ID);
1334     }
1335 
1336     return(returnStatus);
1337 }
1338 
1339 
1340 /*******************************************************************************
1341 * Function Name: Cy_BLE_IPC_SendMessageToHost
1342 ****************************************************************************//**
1343 *
1344 * This function sends a message from the Controller to Host.
1345 *
1346 * \param msg
1347 * Pointer to the message structure to be sent.
1348 *
1349 * \param controllerIpcRelCallBack
1350 * Pointer to the Release callback function.
1351 *
1352 * \param controllerPollCallBack
1353 * Pointer to the callback to be called when the handle has received a message.
1354 *
1355 *  \return
1356 *    CY_IPC_PIPE_SUCCESS    - message was sent to the other end of the pipe
1357 *    CY_IPC_PIPE_BAD_HANDLE - the handle provided for the pipe was not valid
1358 *    CY_IPC_PIPE_SEND_BUSY  - the pipe is already busy sending a message
1359 *    CY_IPC_PIPE_DIR_ERROR  - tried to send on the "to" end of a unidirectional pipe
1360 *
1361 *******************************************************************************/
Cy_BLE_IPC_SendMessageToHost(uint32_t * msg,cy_ipc_pipe_relcallback_ptr_t controllerIpcRelCallBack,cy_ipc_pipe_relcallback_ptr_t controllerPollCallBack)1362 cy_en_ipc_pipe_status_t Cy_BLE_IPC_SendMessageToHost(uint32_t *msg,
1363                                                      cy_ipc_pipe_relcallback_ptr_t controllerIpcRelCallBack,
1364                                                      cy_ipc_pipe_relcallback_ptr_t controllerPollCallBack)
1365 {
1366     cy_en_ipc_pipe_status_t returnStatus;
1367 
1368     returnStatus = Cy_IPC_Pipe_SendMessage(CY_BLE_IPC_HOST_ADDR, CY_BLE_IPC_CONTROLLER_ADDR, msg,
1369                                            controllerIpcRelCallBack);
1370 
1371     if(returnStatus != CY_IPC_PIPE_SUCCESS)
1372     {
1373         Cy_IPC_Pipe_RegisterCallbackRel(CY_BLE_IPC_CONTROLLER_ADDR, controllerPollCallBack);
1374     }
1375 
1376     return(returnStatus);
1377 }
1378 
1379 
1380 /*******************************************************************************
1381 * Function Name: Cy_BLE_IPC_HostMsgFlushRecvCallBack
1382 ****************************************************************************//**
1383 *
1384 * This a callback to be called when the handle has received a flush message.
1385 *
1386 * \param msgPtr: Pointer to the callback message.
1387 *
1388 *******************************************************************************/
Cy_BLE_IPC_HostMsgFlushRecvCallBack(uint32_t * msgPtr)1389 static void Cy_BLE_IPC_HostMsgFlushRecvCallBack(uint32_t *msgPtr)
1390 {
1391     /* Call the BLE Stack IPC handler */
1392     Cy_BLE_IPC_HostMsgFlushRecvStackHandle(msgPtr);
1393 
1394     /* Call the application notification handler */
1395     if(Cy_BLE_HostRegisteredCallback != NULL)
1396     {
1397         Cy_BLE_HostRegisteredCallback();
1398     }
1399 }
1400 
1401 
1402 /*******************************************************************************
1403 * Function Name: Cy_BLE_IPC_HostMsgRecvCallBack
1404 ****************************************************************************//**
1405 *
1406 * This a callback to be called when a message is received on a pipe.
1407 *
1408 * \param msgPtr
1409 * Pointer to the callback message.
1410 *
1411 *******************************************************************************/
Cy_BLE_IPC_HostMsgRecvCallBack(uint32_t * msgPtr)1412 static void Cy_BLE_IPC_HostMsgRecvCallBack(uint32_t *msgPtr)
1413 {
1414     if(msgPtr != NULL)
1415     {
1416         /* Call the BLE Stack IPC handler */
1417         Cy_BLE_IPC_HostMsgRecvStackHandle(msgPtr);
1418     }
1419 
1420     /* Call the application IPC notification handler */
1421     if(Cy_BLE_HostRegisteredCallback != NULL)
1422     {
1423         Cy_BLE_HostRegisteredCallback();
1424     }
1425 }
1426 
1427 
1428 /*******************************************************************************
1429 * Function Name: Cy_BLE_IPC_HostRegisterClientCallbacks
1430 ****************************************************************************//**
1431 *
1432 * This function registers a callbacks to be called when a message is received on a pipe.
1433 *
1434 * \param hostMsgRecvCallBack
1435 * Pointer to the callback to be called when the handle has received a message.
1436 *
1437 * \param hostMsgFlushRecvCallBack
1438 * Pointer to the callback to be called when the handle has received a flush message.
1439 *
1440 *  \return
1441 *    CY_IPC_PIPE_SUCCESS          - Callback registered successfully
1442 *    CY_IPC_PIPE_ERROR_BAD_CLIENT - Client ID out of range, callback not registered.
1443 *
1444 *******************************************************************************/
Cy_BLE_IPC_HostRegisterClientCallbacks(cy_ipc_pipe_callback_ptr_t hostMsgRecvCallBack,cy_ipc_pipe_callback_ptr_t hostMsgFlushRecvCallBack)1445 cy_en_ipc_pipe_status_t Cy_BLE_IPC_HostRegisterClientCallbacks(cy_ipc_pipe_callback_ptr_t hostMsgRecvCallBack,
1446                                                                cy_ipc_pipe_callback_ptr_t hostMsgFlushRecvCallBack)
1447 {
1448     cy_en_ipc_pipe_status_t returnStatus;
1449 
1450     Cy_BLE_IPC_HostMsgRecvStackHandle      = hostMsgRecvCallBack;
1451     Cy_BLE_IPC_HostMsgFlushRecvStackHandle = hostMsgFlushRecvCallBack;
1452 
1453     returnStatus = Cy_IPC_Pipe_RegisterCallback(CY_BLE_IPC_HOST_ADDR, &Cy_BLE_IPC_HostMsgRecvCallBack,
1454                                                 CY_BLE_CYPIPE_MSG_SEND_ID);
1455 
1456     if(returnStatus == CY_IPC_PIPE_SUCCESS)
1457     {
1458         returnStatus = Cy_IPC_Pipe_RegisterCallback(CY_BLE_IPC_HOST_ADDR, &Cy_BLE_IPC_HostMsgFlushRecvCallBack,
1459                                                     CY_BLE_CYPIPE_MSG_COMPLETE_ID);
1460     }
1461 
1462     /* Call the application notification handler */
1463     if(Cy_BLE_HostRegisteredCallback != NULL)
1464     {
1465         Cy_BLE_HostRegisteredCallback();
1466     }
1467     return(returnStatus);
1468 }
1469 
1470 
1471 /*******************************************************************************
1472 * Function Name: Cy_BLE_IPC_SendMessageToController
1473 ****************************************************************************//**
1474 *
1475 * This function sends a message from the Host to Controller.
1476 *
1477 * \param msg
1478 * Pointer to the message structure to be sent.
1479 *
1480 * \param hostIpcRelCallBack
1481 * Pointer to the Release callback function.
1482 *
1483 * \param hostPollCallBack
1484 * Pointer to the callback to be called when the handle has received a message.
1485 *
1486 *  \return
1487 *    CY_IPC_PIPE_SUCCESS - message was sent to the other end of the pipe
1488 *    CY_IPC_PIPE_BAD_HANDLE - the handle provided for the pipe was not valid
1489 *    CY_IPC_PIPE_SEND_BUSY - the pipe is already busy sending a message
1490 *    CY_IPC_PIPE_DIR_ERROR - tried to send on the "to" end of a unidirectional pipe
1491 *
1492 *******************************************************************************/
Cy_BLE_IPC_SendMessageToController(uint32_t * msg,cy_ipc_pipe_relcallback_ptr_t hostIpcRelCallBack,cy_ipc_pipe_relcallback_ptr_t hostPollCallBack)1493 cy_en_ipc_pipe_status_t Cy_BLE_IPC_SendMessageToController(uint32_t *msg,
1494                                                            cy_ipc_pipe_relcallback_ptr_t hostIpcRelCallBack,
1495                                                            cy_ipc_pipe_relcallback_ptr_t hostPollCallBack)
1496 {
1497     cy_en_ipc_pipe_status_t returnStatus;
1498 
1499     returnStatus = Cy_IPC_Pipe_SendMessage(CY_BLE_IPC_CONTROLLER_ADDR, CY_BLE_IPC_HOST_ADDR, msg, hostIpcRelCallBack);
1500 
1501     if(returnStatus != CY_IPC_PIPE_SUCCESS)
1502     {
1503         Cy_IPC_Pipe_RegisterCallbackRel(CY_BLE_IPC_HOST_ADDR, hostPollCallBack);
1504     }
1505 
1506     return(returnStatus);
1507 }
1508 
1509 
1510 /*******************************************************************************
1511 ***
1512 ****   Battery and Temperature Monitor  Functions
1513 ***
1514 *******************************************************************************/
1515 
1516 /*******************************************************************************
1517 * Function Name: Cy_BLE_HAL_RadioGetAbsVbat
1518 ****************************************************************************//**
1519 *
1520 * This function returns absolute value of voltage.
1521 *
1522 * input param: raw - pointer to the 16 bit result from MXD registers,
1523 * output param: vbat - pointer to the calculated voltage value in mV.
1524 *
1525 *******************************************************************************/
Cy_BLE_HAL_RadioGetAbsVbat(const uint16_t * raw,uint16_t * vbat)1526 void Cy_BLE_HAL_RadioGetAbsVbat(const uint16_t *raw, uint16_t *vbat)
1527 {
1528      *vbat = ((CY_BLE_RADIO_VOLTAGE_MONITOR_SLOPE * (*raw)) - CY_BLE_RADIO_VOLTAGE_MONITOR_OFFSET) /
1529                CY_BLE_RADIO_VOLTAGE_MONITOR_SLOPE_MULTIPLIER;
1530 }
1531 
1532 
1533 /*******************************************************************************
1534 * Function Name: Cy_BLE_HAL_RadioGetAbsTemp
1535 ****************************************************************************//**
1536 *
1537 * This function returns absolute temperature value.
1538 *
1539 * input param: raw - pointer to the the 16 bit result from MXD registers,
1540 * output param: temp - pointer to the int16_t calculated temperature value in C.
1541 *
1542 *******************************************************************************/
Cy_BLE_HAL_RadioGetAbsTemp(const uint16_t * raw,int16_t * temp)1543 void Cy_BLE_HAL_RadioGetAbsTemp(const uint16_t *raw, int16_t *temp)
1544 {
1545     *temp = CY_BLE_RADIO_TEMP_MONITOR_OFFSET -
1546             ((CY_BLE_RADIO_TEMP_MONITOR_SLOPE * (int16_t)(*raw)) / CY_BLE_RADIO_TEMP_MONITOR_SLOPE_MULTIPLIER);
1547 }
1548 
1549 
1550 /*******************************************************************************
1551 ***
1552 ****   HAL for Controller only (HCI) mode
1553 ***
1554 *******************************************************************************/
1555 
1556 /*******************************************************************************
1557 * Function Name: Cy_BLE_HAL_UART_Interrupt
1558 ****************************************************************************//**
1559 *
1560 *  Handles the Interrupt service Routine for the UART.
1561 *
1562 *******************************************************************************/
Cy_BLE_HAL_UART_Interrupt(void)1563 void Cy_BLE_HAL_UART_Interrupt(void)
1564 {
1565     CY_ASSERT_L1(NULL != cy_ble_halConfigPtr->hw);
1566 
1567     uint8_t uartTxStatus = CY_BLE_INTR_TX_SUCCESS;
1568     uint32_t srcInterrupt;
1569 
1570     uint8_t dLength = 0u;
1571     uint8_t srcCount = 0u;
1572     uint8_t uartRxStatus = CY_BLE_INTR_RX_SUCCESS;
1573     uint8_t receivedData;
1574 
1575     /* Evaluate RX interrupt event */
1576     srcInterrupt = Cy_SCB_GetRxInterruptStatusMasked(cy_ble_halConfigPtr->hw->uartHw);
1577 
1578     if(0u != (srcInterrupt & CY_SCB_RX_INTR_NOT_EMPTY))
1579     {
1580         if(0u != (srcInterrupt & CY_SCB_RX_INTR_UART_PARITY_ERROR))
1581         {
1582             uartRxStatus |= CY_BLE_INTR_RX_PARITY_ERROR;
1583         }
1584         if(0u != (srcInterrupt & CY_SCB_RX_INTR_UART_FRAME_ERROR))
1585         {
1586             uartRxStatus |= CY_BLE_INTR_RX_FRAME_ERROR;
1587         }
1588         if(0u != (srcInterrupt & CY_SCB_RX_INTR_OVERFLOW))
1589         {
1590             uartRxStatus |= CY_BLE_INTR_RX_OVERFLOW;
1591         }
1592         if(uartRxStatus == CY_BLE_INTR_RX_SUCCESS)
1593         {
1594             dLength = (uint8_t)Cy_SCB_GetNumInRxFifo(cy_ble_halConfigPtr->hw->uartHw);
1595             for(srcCount = 0u; srcCount < dLength; srcCount++)
1596             {
1597                 receivedData = (uint8_t)Cy_SCB_ReadRxFifo(cy_ble_halConfigPtr->hw->uartHw);
1598                 Cy_BLE_HAL_MappingUartRxDataHandler(receivedData);
1599             }
1600         }
1601         else
1602         {
1603             Cy_SCB_ClearRxFifo(cy_ble_halConfigPtr->hw->uartHw);
1604         }
1605         Cy_SCB_ClearRxInterrupt(cy_ble_halConfigPtr->hw->uartHw, srcInterrupt);
1606     }
1607     else
1608     {
1609         /* No RX interrupt. Do nothing. */
1610     }
1611 
1612     /* Evaluate TX interrupt event in sequence */
1613     srcInterrupt = Cy_SCB_GetTxInterruptStatusMasked(cy_ble_halConfigPtr->hw->uartHw);
1614 
1615     /* BLE Stack manager TX UART complete */
1616     if(0u != (srcInterrupt & CY_SCB_TX_INTR_UART_DONE))
1617     {
1618         if(0u != (srcInterrupt & CY_SCB_TX_INTR_OVERFLOW))
1619         {
1620             /*Stack manager TX UART error */
1621             uartTxStatus |= CY_BLE_INTR_TX_OVERFLOW;
1622         }
1623         Cy_BLE_HAL_MappingUartTxCompltHandler();
1624         Cy_SCB_ClearTxInterrupt(cy_ble_halConfigPtr->hw->uartHw, srcInterrupt);
1625     }
1626     else
1627     {
1628         /* No TX interrupt. Do nothing. */
1629     }
1630 }
1631 
1632 
1633 /*******************************************************************************
1634 * Function Name: Cy_BLE_HAL_UART_Start
1635 ****************************************************************************//**
1636 *
1637 *  Enables the platform UART TX and RX interrupts and then enables the UART.
1638 *
1639 *******************************************************************************/
Cy_BLE_HAL_UART_Start(void)1640 void Cy_BLE_HAL_UART_Start(void)
1641 {
1642     CY_ASSERT_L1(NULL != cy_ble_halConfigPtr->hw);
1643 
1644     /* Setup ISR */
1645     (void)Cy_SysInt_Init(cy_ble_halConfigPtr->hw->uartIsrConfig, &Cy_BLE_HAL_UART_Interrupt);
1646     NVIC_EnableIRQ((IRQn_Type)cy_ble_halConfigPtr->hw->uartIsrConfig->intrSrc);
1647 
1648     (void)Cy_SCB_UART_Init(cy_ble_halConfigPtr->hw->uartHw, cy_ble_halConfigPtr->hw->uartConfig, NULL);
1649 
1650     /* Configure interrupt sources. */
1651     Cy_SCB_SetRxInterruptMask(cy_ble_halConfigPtr->hw->uartHw, CY_SCB_RX_INTR_NOT_EMPTY |
1652                                 CY_SCB_RX_INTR_OVERFLOW | CY_SCB_RX_INTR_UNDERFLOW);
1653     Cy_SCB_SetTxInterruptMask(cy_ble_halConfigPtr->hw->uartHw, CY_SCB_TX_INTR_OVERFLOW |
1654                                                         CY_SCB_TX_INTR_UART_DONE);
1655 
1656     Cy_SCB_UART_Enable(cy_ble_halConfigPtr->hw->uartHw);
1657 }
1658 
1659 
1660 /*******************************************************************************
1661 * Function Name: Cy_BLE_HAL_UART_Stop
1662 ****************************************************************************//**
1663 *
1664 *  Disables the UART, clears all pending interrupts and disables the UART TX
1665 *  and RX interrupts. This will also empty out the FIFOs.
1666 *
1667 *******************************************************************************/
Cy_BLE_HAL_UART_Stop(void)1668 void Cy_BLE_HAL_UART_Stop(void)
1669 {
1670     CY_ASSERT_L1(NULL != cy_ble_halConfigPtr->hw);
1671 
1672     /* Stop interrupt and UART */
1673     NVIC_DisableIRQ((IRQn_Type)cy_ble_halConfigPtr->hw->uartIsrConfig->intrSrc);
1674 
1675     Cy_SCB_UART_Disable(cy_ble_halConfigPtr->hw->uartHw, NULL);
1676 }
1677 
1678 
1679 /*******************************************************************************
1680 * Function Name: Cy_BLE_HAL_UART_Transmit
1681 ****************************************************************************//**
1682 *
1683 *  Sends the data to UART TX FIFO. The function handles data length up to the
1684 *  supported TX FIFO length of the UART hardware module.
1685 *
1686 *  \param dataBuf: Pointer to the data to send through the UART
1687 *  \param length:  the length of data to transmit in bytes
1688 *
1689 *******************************************************************************/
Cy_BLE_HAL_UART_Transmit(uint8_t * dataBuf,uint8_t length)1690 void Cy_BLE_HAL_UART_Transmit(uint8_t *dataBuf,
1691                               uint8_t length)
1692 {
1693     CY_ASSERT_L1(NULL != cy_ble_halConfigPtr->hw);
1694 
1695     Cy_SCB_UART_PutArrayBlocking(cy_ble_halConfigPtr->hw->uartHw, dataBuf, (uint32_t)length);
1696 }
1697 
1698 
1699 /*******************************************************************************
1700 * Function Name: Cy_BLE_HAL_UART_EnableInterrupt
1701 ****************************************************************************//**
1702 *
1703 *   Enables the UART interrupt to the interrupt controller.
1704 *   Do not call this function unless Cy_BLE_HAL_UART_Start() has been called or the
1705 *   functionality of the Cy_SysInt_Init() function, which sets the vector and the
1706 *   priority, has been called.
1707 *
1708 *******************************************************************************/
Cy_BLE_HAL_UART_EnableInterrupt(void)1709 void Cy_BLE_HAL_UART_EnableInterrupt(void)
1710 {
1711     CY_ASSERT_L1(NULL != cy_ble_halConfigPtr->hw);
1712 
1713     NVIC_EnableIRQ((IRQn_Type)cy_ble_halConfigPtr->hw->uartIsrConfig->intrSrc);
1714 }
1715 
1716 
1717 /*******************************************************************************
1718 * Function Name: Cy_BLE_HAL_UART_DisableInterrupt
1719 ****************************************************************************//**
1720 *
1721 *   Disables the UART Interrupt in the interrupt controller.
1722 *
1723 *******************************************************************************/
Cy_BLE_HAL_UART_DisableInterrupt(void)1724 void Cy_BLE_HAL_UART_DisableInterrupt(void)
1725 {
1726     CY_ASSERT_L1(NULL != cy_ble_halConfigPtr->hw);
1727 
1728     NVIC_DisableIRQ((IRQn_Type)cy_ble_halConfigPtr->hw->uartIsrConfig->intrSrc);
1729 }
1730 
1731 #endif /* CY_IP_MXBLESS */
1732 
1733 /* [] END OF FILE */
1734