/* Main loop */ int main() { CYBLE_API_RESULT_T apiResult; CyGlobalIntEnable; Initialization(); for(;;) { /* Delayed start of advertisement */ if(initCounter == 6) { initCounter = 7; WDT_DisableWcoEcoCounters(); apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_CUSTOM); if(apiResult != CYBLE_ERROR_OK) { CYASSERT(0); } } CyBle_ProcessEvents(); /* BLE stack processing state machine interface */ LowPower(); } }
/******************************************************************************* * Function Name: `$INSTANCE_NAME`_SetOffset ******************************************************************************** * * Summary: * Description: Sets the ADC offset which is used by the functions * ADC_CountsTo_uVolts, ADC_CountsTo_mVolts and ADC_CountsTo_Volts * to substract the offset from the given reading * before calculating the voltage conversion. * * Parameters: * chan: ADC channel number. * offset: This value is a measured value when the * inputs are shorted or connected to the same input voltage. * * Return: * None. * * Global variables: * `$INSTANCE_NAME`_Offset: Modified to set the user provided offset. * *******************************************************************************/ void `$INSTANCE_NAME`_SetOffset(uint32 chan, int16 offset) { /* Halt CPU in debug mode if channel is out of valid range */ CYASSERT(chan < `$INSTANCE_NAME`_TOTAL_CHANNELS_NUM); `$INSTANCE_NAME`_offset[chan] = offset; }
/******************************************************************************* * Function Name: Midi_UpdateBufferPointers ******************************************************************************** * * Summary: * This function updates the MIDI buffer pointers * * Parameters: * void * * Return: * None * *******************************************************************************/ static void Midi_UpdateBufferPointers(void) { if(midiBuffer.midiPacket[midiBuffer.packetIndex].packetStatus == MIDI_PACKET_VALID && midiBuffer.midiPacket[midiBuffer.packetIndex].dataIndex >= (CYBLE_GATT_MTU - MAX_MIDI_PACKET_LENGTH + 1)) { midiBuffer.packetIndex++; if(midiBuffer.packetIndex == MAX_NUMBER_OF_MIDI_PACKETS) { midiBuffer.packetIndex = 0; } if(midiBuffer.midiPacket[midiBuffer.packetIndex].packetStatus == MIDI_PACKET_VALID) { /* The updated packet is still valid and is not sent over BLE. Flag this as an overflow condition */ CYASSERT(0); } } if(midiBuffer.midiPacket[midiBuffer.packetIndex].dataIndex == 0) { /* Insert the header byte to the packet structure */ midiBuffer.midiPacket[midiBuffer.packetIndex].midiNotificationPacket\ [midiBuffer.midiPacket[midiBuffer.packetIndex].dataIndex++] = MIDI_PACKET_HEADER; midiBuffer.midiPacket[midiBuffer.packetIndex].packetStatus = MIDI_PACKET_VALID; } }
/******************************************************************************* * Function Name: adc_SetOffset ******************************************************************************** * * Summary: * Description: Sets the ADC offset which is used by the functions * ADC_CountsTo_uVolts, ADC_CountsTo_mVolts and ADC_CountsTo_Volts * to substract the offset from the given reading * before calculating the voltage conversion. * * Parameters: * chan: ADC channel number. * offset: This value is a measured value when the * inputs are shorted or connected to the same input voltage. * * Return: * None. * * Global variables: * adc_Offset: Modified to set the user provided offset. * *******************************************************************************/ void adc_SetOffset(uint32 chan, int16 offset) { /* Halt CPU in debug mode if channel is out of valid range */ CYASSERT(chan < adc_TOTAL_CHANNELS_NUM); adc_offset[chan] = offset; }
/******************************************************************************* * Function Name: ADC_SAR_Seq_0_SetOffset ******************************************************************************** * * Summary: * Description: Sets the ADC offset which is used by the functions * ADC_CountsTo_uVolts, ADC_CountsTo_mVolts and ADC_CountsTo_Volts * to substract the offset from the given reading * before calculating the voltage conversion. * * Parameters: * chan: ADC channel number. * offset: This value is a measured value when the * inputs are shorted or connected to the same input voltage. * * Return: * None. * * Global variables: * ADC_SAR_Seq_0_Offset: Modified to set the user provided offset. * *******************************************************************************/ void ADC_SAR_Seq_0_SetOffset(uint32 chan, int16 offset) { /* Halt CPU in debug mode if channel is out of valid range */ CYASSERT(chan < ADC_SAR_Seq_0_TOTAL_CHANNELS_NUM); ADC_SAR_Seq_0_offset[chan] = offset; }
/******************************************************************************* * Function Name: `$INSTANCE_NAME`_SetGain ******************************************************************************** * * Summary: * Description: Sets the ADC gain in counts per 10 volt for the voltage * conversion functions below. This value is set by default by the * reference and input range settings. It should only be used to further * calibrate the ADC with a known input or if an external reference is * used. Affects the ADC_CountsTo_uVolts, ADC_CountsTo_mVolts * and ADC_CountsTo_Volts functions by supplying the correct * conversion between ADC counts and voltage. * * Parameters: * chan: ADC channel number. * adcGain: ADC gain in counts per 10 volts. * * Return: * None. * * Global variables: * `$INSTANCE_NAME`_CountsPer10Volt: modified to set the ADC gain in counts * per 10 volt. * *******************************************************************************/ void `$INSTANCE_NAME`_SetGain(uint32 chan, int32 adcGain) { /* Halt CPU in debug mode if channel is out of valid range */ CYASSERT(chan < `$INSTANCE_NAME`_TOTAL_CHANNELS_NUM); `$INSTANCE_NAME`_countsPer10Volt[chan] = adcGain; }
/******************************************************************************* * Function Name: TPS_ADC_SAR_SetResolution ******************************************************************************** * * Summary: * Sets the Relution of the SAR. * * Parameters: * resolution: * 12 -> RES12 * 10 -> RES10 * 8 -> RES8 * * Return: * None. * * Side Effects: * The ADC resolution cannot be changed during a conversion cycle. The * recommended best practice is to stop conversions with * ADC_StopConvert(), change the resolution, then restart the * conversions with ADC_StartConvert(). * If you decide not to stop conversions before calling this API, you * should use ADC_IsEndConversion() to wait until conversion is complete * before changing the resolution. * If you call ADC_SetResolution() during a conversion, the resolution will * not be changed until the current conversion is complete. Data will not be * available in the new resolution for another 6 + "New Resolution(in bits)" * clock cycles. * You may need add a delay of this number of clock cycles after * ADC_SetResolution() is called before data is valid again. * Affects ADC_CountsTo_Volts(), ADC_CountsTo_mVolts(), and * ADC_CountsTo_uVolts() by calculating the correct conversion between ADC * counts and the applied input voltage. Calculation depends on resolution, * input range, and voltage reference. * *******************************************************************************/ void TPS_ADC_SAR_SetResolution(uint8 resolution) { uint8 tmpReg; /* Set SAR ADC resolution and sample width: 18 conversion cycles at 12bits + 1 gap */ switch (resolution) { case (uint8)TPS_ADC_SAR__BITS_12: tmpReg = TPS_ADC_SAR_SAR_RESOLUTION_12BIT | TPS_ADC_SAR_SAR_SAMPLE_WIDTH; break; case (uint8)TPS_ADC_SAR__BITS_10: tmpReg = TPS_ADC_SAR_SAR_RESOLUTION_10BIT | TPS_ADC_SAR_SAR_SAMPLE_WIDTH; break; case (uint8)TPS_ADC_SAR__BITS_8: tmpReg = TPS_ADC_SAR_SAR_RESOLUTION_8BIT | TPS_ADC_SAR_SAR_SAMPLE_WIDTH; break; default: tmpReg = TPS_ADC_SAR_SAR_RESOLUTION_12BIT | TPS_ADC_SAR_SAR_SAMPLE_WIDTH; /* Halt CPU in debug mode if resolution is out of valid range */ CYASSERT(0u != 0u); break; } TPS_ADC_SAR_SAR_CSR2_REG = tmpReg; /* Calculate gain for convert counts to volts */ TPS_ADC_SAR_CalcGain(resolution); }
/******************************************************************************* * Function Name: ADC_SAR_Seq_0_SetGain ******************************************************************************** * * Summary: * Description: Sets the ADC gain in counts per 10 volt for the voltage * conversion functions below. This value is set by default by the * reference and input range settings. It should only be used to further * calibrate the ADC with a known input or if an external reference is * used. Affects the ADC_CountsTo_uVolts, ADC_CountsTo_mVolts * and ADC_CountsTo_Volts functions by supplying the correct * conversion between ADC counts and voltage. * * Parameters: * chan: ADC channel number. * adcGain: ADC gain in counts per 10 volts. * * Return: * None. * * Global variables: * ADC_SAR_Seq_0_CountsPer10Volt: modified to set the ADC gain in counts * per 10 volt. * *******************************************************************************/ void ADC_SAR_Seq_0_SetGain(uint32 chan, int32 adcGain) { /* Halt CPU in debug mode if channel is out of valid range */ CYASSERT(chan < ADC_SAR_Seq_0_TOTAL_CHANNELS_NUM); ADC_SAR_Seq_0_countsPer10Volt[chan] = adcGain; }
/******************************************************************************* * Function Name: BLE_StackEventHandler ******************************************************************************** * * Summary: * BLE stack generic event handler routine for handling connection, discovery, * security etc. events. * * Parameters: * event - event that triggered this callback * eventParam - parameters for the event. * * Return: * None *******************************************************************************/ void BLE_StackEventHandler(uint32 event, void* eventParam) { CYBLE_API_RESULT_T apiResult; (void) eventParam; switch(event) { case CYBLE_EVT_GAP_DEVICE_DISCONNECTED: break; case CYBLE_EVT_STACK_ON: /* Put the device into discoverable mode so that remote can search it. */ apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); if(apiResult != CYBLE_ERROR_OK) { CYASSERT(0); } break; /* ADD YOUR CODE TO HANDLE OTHER GENERIC BLE STACK EVENTS */ /* All the BLE stack events can be found in BLE_Stack.h (see CYBLE_EVENT_T enum) and BLE_eventHandler.h (see * CYBLE_EVT_T enum ) */ default: break; } }
/******************************************************************************* * Function Name: TPS_ADC_SAR_CalcGain ******************************************************************************** * * Summary: * This function calculates the ADC gain in counts per 10 volt. * * Parameters: * uint8: resolution * * Return: * None. * * Global Variables: * TPS_ADC_SAR_shift variable initialized. This variable is used to * convert the ADC counts to the 2s compliment form. * TPS_ADC_SAR_countsPer10Volt variable initialized. This variable is used * for gain calibration purpose. * *******************************************************************************/ static void TPS_ADC_SAR_CalcGain( uint8 resolution ) { int32 counts; #if(!((TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSS_TO_VREF) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDDA) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDAC)) ) uint16 diff_zero; #endif /* End TPS_ADC_SAR_DEFAULT_RANGE */ switch (resolution) { case (uint8)TPS_ADC_SAR__BITS_12: counts = (int32)TPS_ADC_SAR_SAR_WRK_MAX_12BIT; #if(!((TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSS_TO_VREF) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDDA) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDAC)) ) diff_zero = TPS_ADC_SAR_SAR_DIFF_SHIFT; #endif /* End TPS_ADC_SAR_DEFAULT_RANGE */ break; case (uint8)TPS_ADC_SAR__BITS_10: counts = (int32)TPS_ADC_SAR_SAR_WRK_MAX_10BIT; #if(!((TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSS_TO_VREF) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDDA) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDAC)) ) diff_zero = TPS_ADC_SAR_SAR_DIFF_SHIFT >> 2u; #endif /* End TPS_ADC_SAR_DEFAULT_RANGE */ break; case (uint8)TPS_ADC_SAR__BITS_8: counts = (int32)TPS_ADC_SAR_SAR_WRK_MAX_8BIT; #if(!((TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSS_TO_VREF) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDDA) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDAC)) ) diff_zero = TPS_ADC_SAR_SAR_DIFF_SHIFT >> 4u; #endif /* End TPS_ADC_SAR_DEFAULT_RANGE */ break; default: /* Halt CPU in debug mode if resolution is out of valid range */ counts = 0; #if(!((TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSS_TO_VREF) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDDA) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDAC)) ) diff_zero = 0u; #endif /* End TPS_ADC_SAR_DEFAULT_RANGE */ CYASSERT(0u != 0u); break; } TPS_ADC_SAR_countsPerVolt = 0; /* Clear obsolete variable */ /* Calculate gain in counts per 10 volts with rounding */ TPS_ADC_SAR_countsPer10Volt = (((counts * TPS_ADC_SAR_10MV_COUNTS) + TPS_ADC_SAR_DEFAULT_REF_VOLTAGE_MV) / (TPS_ADC_SAR_DEFAULT_REF_VOLTAGE_MV * 2)); #if( (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSS_TO_VREF) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDDA) || \ (TPS_ADC_SAR_DEFAULT_RANGE == TPS_ADC_SAR__VSSA_TO_VDAC) ) TPS_ADC_SAR_shift = 0; #else TPS_ADC_SAR_shift = diff_zero; #endif /* End TPS_ADC_SAR_DEFAULT_RANGE */ }
/******************************************************************************* * Function Name: WaveDAC8_1_BuffAmp_SetPower ******************************************************************************** * * Summary: * Sets power level of Analog buffer. * * Parameters: * power: PSoC3: Sets power level between low (1) and high power (3). * PSoC5: Sets power level High (0) * * Return: * void * **********************************************************************************/ void WaveDAC8_1_BuffAmp_SetPower(uint8 power) { #if (CY_PSOC3 || CY_PSOC5LP) WaveDAC8_1_BuffAmp_CR_REG &= (uint8)(~WaveDAC8_1_BuffAmp_PWR_MASK); WaveDAC8_1_BuffAmp_CR_REG |= power & WaveDAC8_1_BuffAmp_PWR_MASK; /* Set device power */ #else CYASSERT(WaveDAC8_1_BuffAmp_HIGHPOWER == power); #endif /* CY_PSOC3 || CY_PSOC5LP */ }
/******************************************************************************* * Function Name: Opamp_L_SetPower ******************************************************************************** * * Summary: * Sets power level of Analog buffer. * * Parameters: * power: PSoC3: Sets power level between low (1) and high power (3). * PSoC5: Sets power level High (0) * * Return: * void * **********************************************************************************/ void Opamp_L_SetPower(uint8 power) { #if (CY_PSOC3 || CY_PSOC5LP) Opamp_L_CR_REG &= (uint8)(~Opamp_L_PWR_MASK); Opamp_L_CR_REG |= power & Opamp_L_PWR_MASK; /* Set device power */ #else CYASSERT(Opamp_L_HIGHPOWER == power); #endif /* CY_PSOC3 || CY_PSOC5LP */ }
int main() { CYBLE_API_RESULT_T apiResult; uint32 count = 0; uint8 triggerNotification = 0; // Enable global interrupts CyGlobalIntEnable; // Initialize the watchdog timer CySysWdtSetIsrCallback(CY_SYS_WDT_COUNTER0, Watchdog0_cb); // Initialize the BLE device. apiResult = CyBle_Start(StackEventHandler); // Validate BLE stack initialization successed CYASSERT(apiResult == CYBLE_ERROR_OK); for (;;) { // Service all the BLE stack events. // Must be called at least once in a BLE connection interval CyBle_ProcessEvents(); if (deviceConnected) { if (counterCccDescriptor.dirty) { // Update Counter CCCD updateCounterCccDescriptor(); } else if (triggerNotification) { // Send notification if required if (enableCounterNotification) { sendCounterNotification(count); } triggerNotification = 0; } else if (triggerUpdateCounter) { // Update counter value count++; updateCounter(count); triggerNotification = ((count & 0x0000000F) == 0); triggerUpdateCounter = 0; } } // Scan update queue if (rgbDescriptor.dirty) { // Update RGB Descriptor updateRgbDescriptor(); } // Enter to deep sleep mode { CYBLE_LP_MODE_T state; state = CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP); if (state == CYBLE_BLESS_DEEPSLEEP) { CySysPmDeepSleep(); } } } }
/******************************************************************************* * Function Name: ADC_SAR_1_CalcGain ******************************************************************************** * * Summary: * This function calculates the ADC gain in counts per volt. * * Parameters: * uint8: resolution * * Return: * None. * * Global Variables: * ADC_SAR_1_shift variable initialized. This variable is used to * convert the ADC counts to the 2's compliment form. * ADC_SAR_1_countsPerVolt variable initialized. This variable is used * for gain calibration purpose. * *******************************************************************************/ static void ADC_SAR_1_CalcGain( uint8 resolution ) { int32 counts; #if(!((ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSS_TO_VREF) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDDA) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDAC)) ) uint16 diff_zero; #endif /* End ADC_SAR_1_DEFAULT_RANGE */ switch (resolution) { case (uint8)ADC_SAR_1__BITS_12: counts = (int32)ADC_SAR_1_SAR_WRK_MAX; #if(!((ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSS_TO_VREF) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDDA) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDAC)) ) diff_zero = ADC_SAR_1_SAR_DIFF_SHIFT; #endif /* End ADC_SAR_1_DEFAULT_RANGE */ break; case (uint8)ADC_SAR_1__BITS_10: counts = (int32)(ADC_SAR_1_SAR_WRK_MAX >> 2u); #if(!((ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSS_TO_VREF) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDDA) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDAC)) ) diff_zero = ADC_SAR_1_SAR_DIFF_SHIFT >> 2u; #endif /* End ADC_SAR_1_DEFAULT_RANGE */ break; case (uint8)ADC_SAR_1__BITS_8: counts = (int32)(ADC_SAR_1_SAR_WRK_MAX >> 4u); #if(!((ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSS_TO_VREF) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDDA) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDAC)) ) diff_zero = ADC_SAR_1_SAR_DIFF_SHIFT >> 4u; #endif /* End ADC_SAR_1_DEFAULT_RANGE */ break; default: /* Halt CPU in debug mode if resolution is out of valid range */ counts = 0; #if(!((ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSS_TO_VREF) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDDA) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDAC)) ) diff_zero = 0u; #endif /* End ADC_SAR_1_DEFAULT_RANGE */ CYASSERT(0u != 0u); break; } counts *= 1000; /* To avoid float point arithmetic*/ ADC_SAR_1_countsPerVolt = (int16)(counts / ADC_SAR_1_DEFAULT_REF_VOLTAGE_MV / 2); #if( (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSS_TO_VREF) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDDA) || \ (ADC_SAR_1_DEFAULT_RANGE == ADC_SAR_1__VSSA_TO_VDAC) ) ADC_SAR_1_shift = 0; #else ADC_SAR_1_shift = diff_zero; #endif /* End ADC_SAR_1_DEFAULT_RANGE */ }
/******************************************************************************* * Function Name: USBUART_1_PutData ******************************************************************************** * * Summary: * This function sends a specified number of bytes from the location specified * by a pointer to the PC. The USBUART_1_CDCIsReady() function should be * called before sending new data, to be sure that the previous data has * finished sending. * If the last sent packet is less than maximum packet size the USB transfer * of this short packet will identify the end of the segment. If the last sent * packet is exactly maximum packet size, it shall be followed by a zero-length * packet (which is a short packet) to assure the end of segment is properly * identified. To send zero-length packet, use USBUART_1_PutData() API * with length parameter set to zero. * * Parameters: * pData: pointer to the buffer containing data to be sent. * length: Specifies the number of bytes to send from the pData * buffer. Maximum length will be limited by the maximum packet * size for the endpoint. Data will be lost if length is greater than Max * Packet Size. * * Return: * None. * * Global variables: * USBUART_1_cdc_data_in_ep: CDC IN endpoint number used for sending * data. * * Reentrant: * No. * *******************************************************************************/ void USBUART_1_PutData(const uint8* pData, uint16 length) { /* Limits length to maximum packet size for the EP */ if(length > USBUART_1_EP[USBUART_1_cdc_data_in_ep].bufferSize) { /* Caution: Data will be lost if length is greater than Max Packet Length */ length = USBUART_1_EP[USBUART_1_cdc_data_in_ep].bufferSize; /* Halt CPU in debug mode */ CYASSERT(0u != 0u); } USBUART_1_LoadInEP(USBUART_1_cdc_data_in_ep, pData, length); }
/******************************************************************************* * Function Name: hallTickCounter_WriteCounter ******************************************************************************** * Summary: * This funtion is used to set the counter to a specific value * * Parameters: * counter: New counter value. * * Return: * void * *******************************************************************************/ void hallTickCounter_WriteCounter(uint8 counter) \ { #if(hallTickCounter_UsingFixedFunction) /* assert if block is already enabled */ CYASSERT (0u == (hallTickCounter_GLOBAL_ENABLE & hallTickCounter_BLOCK_EN_MASK)); /* If block is disabled, enable it and then write the counter */ hallTickCounter_GLOBAL_ENABLE |= hallTickCounter_BLOCK_EN_MASK; CY_SET_REG16(hallTickCounter_COUNTER_LSB_PTR, (uint16)counter); hallTickCounter_GLOBAL_ENABLE &= ((uint8)(~hallTickCounter_BLOCK_EN_MASK)); #else CY_SET_REG8(hallTickCounter_COUNTER_LSB_PTR, counter); #endif /* (hallTickCounter_UsingFixedFunction) */ }
/******************************************************************************* * Function Name: QuadDecoder_Cnt16_WriteCounter ******************************************************************************** * Summary: * This funtion is used to set the counter to a specific value * * Parameters: * counter: New counter value. * * Return: * void * *******************************************************************************/ void QuadDecoder_Cnt16_WriteCounter(uint16 counter) \ { #if(QuadDecoder_Cnt16_UsingFixedFunction) /* assert if block is already enabled */ CYASSERT (0u == (QuadDecoder_Cnt16_GLOBAL_ENABLE & QuadDecoder_Cnt16_BLOCK_EN_MASK)); /* If block is disabled, enable it and then write the counter */ QuadDecoder_Cnt16_GLOBAL_ENABLE |= QuadDecoder_Cnt16_BLOCK_EN_MASK; CY_SET_REG16(QuadDecoder_Cnt16_COUNTER_LSB_PTR, (uint16)counter); QuadDecoder_Cnt16_GLOBAL_ENABLE &= ((uint8)(~QuadDecoder_Cnt16_BLOCK_EN_MASK)); #else CY_SET_REG16(QuadDecoder_Cnt16_COUNTER_LSB_PTR, counter); #endif /* (QuadDecoder_Cnt16_UsingFixedFunction) */ }
/******************************************************************************* * Function Name: Counter_1_WriteCounter ******************************************************************************** * Summary: * This funtion is used to set the counter to a specific value * * Parameters: * counter: New counter value. * * Return: * void * *******************************************************************************/ void Counter_1_WriteCounter(uint16 counter) \ { #if(Counter_1_UsingFixedFunction) /* assert if block is already enabled */ CYASSERT (0u == (Counter_1_GLOBAL_ENABLE & Counter_1_BLOCK_EN_MASK)); /* If block is disabled, enable it and then write the counter */ Counter_1_GLOBAL_ENABLE |= Counter_1_BLOCK_EN_MASK; CY_SET_REG16(Counter_1_COUNTER_LSB_PTR, (uint16)counter); Counter_1_GLOBAL_ENABLE &= ((uint8)(~Counter_1_BLOCK_EN_MASK)); #else CY_SET_REG16(Counter_1_COUNTER_LSB_PTR, counter); #endif /* (Counter_1_UsingFixedFunction) */ }
/******************************************************************************* * Function Name: Phase_Counter_WriteCounter ******************************************************************************** * Summary: * This funtion is used to set the counter to a specific value * * Parameters: * counter: New counter value. * * Return: * void * *******************************************************************************/ void Phase_Counter_WriteCounter(uint32 counter) \ { #if(Phase_Counter_UsingFixedFunction) /* assert if block is already enabled */ CYASSERT (0u == (Phase_Counter_GLOBAL_ENABLE & Phase_Counter_BLOCK_EN_MASK)); /* If block is disabled, enable it and then write the counter */ Phase_Counter_GLOBAL_ENABLE |= Phase_Counter_BLOCK_EN_MASK; CY_SET_REG16(Phase_Counter_COUNTER_LSB_PTR, (uint16)counter); Phase_Counter_GLOBAL_ENABLE &= ((uint8)(~Phase_Counter_BLOCK_EN_MASK)); #else CY_SET_REG32(Phase_Counter_COUNTER_LSB_PTR, counter); #endif /* (Phase_Counter_UsingFixedFunction) */ }
/******************************************************************************* * Function Name: BLE_AppEventHandler ******************************************************************************** * * Summary: * BLE stack generic event handler routine for handling connection, discovery, * security etc. events. * * Parameters: * event - event that triggered this callback * eventParam - parameters for the event. * * Return: * None *******************************************************************************/ void BLE_AppEventHandler(uint32 event, void* eventParam) { CYBLE_API_RESULT_T apiResult; (void)eventParam; switch (event) { /********************************************************** * General Events ***********************************************************/ case CYBLE_EVT_STACK_ON: /* This event is received when component is Started */ /* Enter in to discoverable mode so that remote can search it. */ apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); if(apiResult != CYBLE_ERROR_OK) { CYASSERT(0); } break; case CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP: if(CyBle_GetState() == CYBLE_STATE_DISCONNECTED) { /* On advertisement timeout, restart advertisement */ apiResult = CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST); if(apiResult != CYBLE_ERROR_OK) { CYASSERT(0); } } break; default: break; } }
/******************************************************************************* * Function Name: CyFlash_SetWaitCycles ******************************************************************************** * * Summary: * Sets the number of clock cycles the cache will wait before it samples data * coming back from the Flash. This function must be called before increasing * the CPU clock frequency. It can optionally be called after lowering the CPU * clock frequency in order to improve the CPU performance. * * Parameters: * uint8 freq: * Frequency of operation in Megahertz. * * Return: * None * *******************************************************************************/ void CyFlash_SetWaitCycles(uint8 freq) { uint8 interruptState; /* Save current global interrupt enable and disable it */ interruptState = CyEnterCriticalSection(); /*************************************************************************** * The number of clock cycles the cache will wait before it samples data * coming back from the Flash must be equal or greater to to the CPU frequency * outlined in clock cycles. ***************************************************************************/ if (freq < CY_FLASH_CACHE_WS_1_FREQ_MAX) { CY_FLASH_CONTROL_REG = (CY_FLASH_CONTROL_REG & (uint8)(~CY_FLASH_CACHE_WS_VALUE_MASK)) | CY_FLASH_CACHE_WS_1_VALUE_MASK; } else if (freq < CY_FLASH_CACHE_WS_2_FREQ_MAX) { CY_FLASH_CONTROL_REG = (CY_FLASH_CONTROL_REG & (uint8)(~CY_FLASH_CACHE_WS_VALUE_MASK)) | CY_FLASH_CACHE_WS_2_VALUE_MASK; } else if (freq < CY_FLASH_CACHE_WS_3_FREQ_MAX) { CY_FLASH_CONTROL_REG = (CY_FLASH_CONTROL_REG & (uint8)(~CY_FLASH_CACHE_WS_VALUE_MASK)) | CY_FLASH_CACHE_WS_3_VALUE_MASK; } #if (CY_PSOC5) else if (freq < CY_FLASH_CACHE_WS_4_FREQ_MAX) { CY_FLASH_CONTROL_REG = (CY_FLASH_CONTROL_REG & (uint8)(~CY_FLASH_CACHE_WS_VALUE_MASK)) | CY_FLASH_CACHE_WS_4_VALUE_MASK; } else if (freq <= CY_FLASH_CACHE_WS_5_FREQ_MAX) { CY_FLASH_CONTROL_REG = (CY_FLASH_CONTROL_REG & (uint8)(~CY_FLASH_CACHE_WS_VALUE_MASK)) | CY_FLASH_CACHE_WS_5_VALUE_MASK; } #endif /* (CY_PSOC5) */ else { /* Halt CPU in debug mode if frequency is invalid */ CYASSERT(0u != 0u); } /* Restore global interrupt enable state */ CyExitCriticalSection(interruptState); }
/******************************************************************************* * Function Name: BLE_Engine_Start ******************************************************************************** * * Summary: * Application level API for starting the BLE interface. The API internally calls * other BLE interface init APIs to setup the system. * * Parameters: * None * * Return: * None * *******************************************************************************/ CYBLE_API_RESULT_T BLE_Engine_Start(void) { CYBLE_API_RESULT_T apiResult; apiResult = CyBle_Start(BLE_StackEventHandler); if(apiResult != CYBLE_ERROR_OK) { CYASSERT(0); } /* ADD YOUR CODE TO REGISTER OTHER BLE SERVICE SPECIFIC EVENT HANDLERS */ return apiResult; }
/******************************************************************************* * Function Name: USBUART_PutData ****************************************************************************//** * * This function sends a specified number of bytes from the location specified * by a pointer to the PC. The USBUART_CDCIsReady() function should be * called before sending new data, to be sure that the previous data has * finished sending. * If the last sent packet is less than maximum packet size the USB transfer * of this short packet will identify the end of the segment. If the last sent * packet is exactly maximum packet size, it shall be followed by a zero-length * packet (which is a short packet) to assure the end of segment is properly * identified. To send zero-length packet, use USBUART_PutData() API * with length parameter set to zero. * * \param pData: pointer to the buffer containing data to be sent. * \param length: Specifies the number of bytes to send from the pData * buffer. Maximum length will be limited by the maximum packet * size for the endpoint. Data will be lost if length is greater than Max * Packet Size. * * \globalvars * * USBUART_cdcDataInEp: CDC IN endpoint number used for sending * data. * * \reentrant * No. * *******************************************************************************/ void USBUART_PutData(const uint8* pData, uint16 length) { uint8 epNumber = USBUART_cdcDataInEp[USBUART_activeCom]; /* Limit length to maximum packet size for endpoint. */ if (length > USBUART_EP[epNumber].bufferSize) { /* Caution: Data will be lost if length is greater than Max Packet Size. */ length = USBUART_EP[epNumber].bufferSize; /* Halt CPU in debug mode */ CYASSERT(0u != 0u); } USBUART_LoadInEP(epNumber, pData, length); }
/******************************************************************************* * Function Name: ADC_SAR_1_SetResolution ******************************************************************************** * * Summary: * Sets the Relution of the SAR. * This function does not affect the actual conversion with PSoC5 ES1 silicon. * * Parameters: * resolution: * 12 -> RES12 * 10 -> RES10 * 8 -> RES8 * * Return: * None. * * Side Effects: * The ADC resolution cannot be changed during a conversion cycle. The * recommended best practice is to stop conversions with * ADC_StopConvert(), change the resolution, then restart the * conversions with ADC_StartConvert(). * If you decide not to stop conversions before calling this API, you * should use ADC_IsEndConversion() to wait until conversion is complete * before changing the resolution. * If you call ADC_SetResolution() during a conversion, the resolution will * not be changed until the current conversion is complete. Data will not be * available in the new resolution for another 6 + "New Resolution(in bits)" * clock cycles. * You may need add a delay of this number of clock cycles after * ADC_SetResolution() is called before data is valid again. * Affects ADC_CountsTo_Volts(), ADC_CountsTo_mVolts(), and * ADC_CountsTo_uVolts() by calculating the correct conversion between ADC * counts and the applied input voltage. Calculation depends on resolution, * input range, and voltage reference. * *******************************************************************************/ void ADC_SAR_1_SetResolution(uint8 resolution) { uint8 tmpReg; /* remember resolution for the GetResult APIs */ #if(CY_PSOC5A) ADC_SAR_1_resolution = resolution; #endif /* End CY_PSOC5A */ /* Set SAR ADC resolution and sample width: 18 conversion cycles at 12bits + 1 gap */ switch (resolution) { case (uint8)ADC_SAR_1__BITS_12: tmpReg = ADC_SAR_1_SAR_RESOLUTION_12BIT | ADC_SAR_1_SAR_SAMPLE_WIDTH; break; case (uint8)ADC_SAR_1__BITS_10: /* Use 12bits for PSoC5 production silicon and shift the * results for lower resolution in GetResult16() API */ #if(CY_PSOC5A) tmpReg = ADC_SAR_1_SAR_RESOLUTION_12BIT | ADC_SAR_1_SAR_SAMPLE_WIDTH; #else tmpReg = ADC_SAR_1_SAR_RESOLUTION_10BIT | ADC_SAR_1_SAR_SAMPLE_WIDTH; #endif /* End CY_PSOC5A */ break; case (uint8)ADC_SAR_1__BITS_8: #if(CY_PSOC5A) tmpReg = ADC_SAR_1_SAR_RESOLUTION_12BIT | ADC_SAR_1_SAR_SAMPLE_WIDTH; #else tmpReg = ADC_SAR_1_SAR_RESOLUTION_8BIT | ADC_SAR_1_SAR_SAMPLE_WIDTH; #endif /* End CY_PSOC5A */ break; default: tmpReg = ADC_SAR_1_SAR_RESOLUTION_12BIT | ADC_SAR_1_SAR_SAMPLE_WIDTH; /* Halt CPU in debug mode if resolution is out of valid range */ CYASSERT(0u != 0u); break; } ADC_SAR_1_SAR_CSR2_REG = tmpReg; /* Calculate gain for convert counts to volts */ ADC_SAR_1_CalcGain(resolution); }
/******************************************************************************* * Function Name: main ******************************************************************************** * * Summary: * Main function. * * Parameters: * None * * Return: * None * *******************************************************************************/ int main() { CYBLE_API_RESULT_T apiResult; CYBLE_STATE_T bleState; CyGlobalIntEnable; PWM_Start(); UART_Start(); UART_UartPutString("Welcome to BLE OOB Pairing Demo\r\n"); apiResult = CyBle_Start(StackEventHandler); if(apiResult != CYBLE_ERROR_OK) { /* BLE stack initialization failed, check your configuration */ CYASSERT(0); } CyBle_IasRegisterAttrCallback(IasEventHandler); for(;;) { /* Single API call to service all the BLE stack events. Must be * called at least once in a BLE connection interval */ CyBle_ProcessEvents(); bleState = CyBle_GetState(); if(bleState != CYBLE_STATE_STOPPED && bleState != CYBLE_STATE_INITIALIZING) { /* Configure BLESS in DeepSleep mode */ CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP); /* Configure PSoC 4 BLE system in sleep mode */ CySysPmSleep(); /* BLE link layer timing interrupt will wake up the system */ } } }
/******************************************************************************* * Function Name: InitializeSystem ******************************************************************************** * * Summary: * This routine initializes all the componnets and firmware state. * * Parameters: * None * * Return: * None * *******************************************************************************/ void InitializeSystem(void) { CYBLE_API_RESULT_T apiResult; CyGlobalIntEnable; apiResult = CyBle_Start(StackEventHandler); /* Init the BLE stack and register an applicaiton callback */ if(apiResult != CYBLE_ERROR_OK) { /* BLE stack initialization failed, check your configuration */ CYASSERT(0); } /* Set XTAL divider to 3MHz mode */ CySysClkWriteEcoDiv(CY_SYS_CLK_ECO_DIV8); /* ILO is no longer required, shut it down */ CySysClkIloStop(); }
/******************************************************************************* * Function Name: ADC_SAR_Seq_0_CountsTo_Volts ******************************************************************************** * * Summary: * Converts the ADC output to Volts as a floating point number. * This function is not available when left data format justification selected. * * Parameters: * chan: The ADC channel number. * Result from the ADC conversion * * Return: * Results in Volts * * Global variables: * ADC_SAR_Seq_0_countsPer10Volt: used to convert ADC counts to Volts. * ADC_SAR_Seq_0_Offset: Used as the offset while converting ADC counts * to mVolts. * *******************************************************************************/ float32 ADC_SAR_Seq_0_CountsTo_Volts(uint32 chan, int16 adcCounts) { float32 volts; /* Halt CPU in debug mode if channel is out of valid range */ CYASSERT(chan < ADC_SAR_Seq_0_TOTAL_CHANNELS_NUM); /* Divide the adcCount when accumulate averaging mode selected */ #if(ADC_SAR_Seq_0_DEFAULT_AVG_MODE == ADC_SAR_Seq_0__ACCUMULATE) if((ADC_SAR_Seq_0_channelsConfig[chan] & ADC_SAR_Seq_0_AVERAGING_EN) != 0u) { adcCounts /= ADC_SAR_Seq_0_DEFAULT_AVG_SAMPLES_DIV; } #endif /* ADC_SAR_Seq_0_DEFAULT_AVG_MODE == ADC_SAR_Seq_0__ACCUMULATE */ /* Subtract ADC offset */ adcCounts -= ADC_SAR_Seq_0_offset[chan]; volts = ((float32)adcCounts * ADC_SAR_Seq_0_10V_COUNTS) / (float32)ADC_SAR_Seq_0_countsPer10Volt[chan]; return( volts ); }
/******************************************************************************* * Function Name: `$INSTANCE_NAME`_CountsTo_uVolts ******************************************************************************** * * Summary: * This function converts ADC counts to micro Volts * This function is not available when left data format justification selected. * * Parameters: * chan: The ADC channel number. * adcCounts: Result from the ADC conversion * * Return: * Results in uVolts * * Global variables: * `$INSTANCE_NAME`_countsPer10Volt: used to convert ADC counts to uVolts. * `$INSTANCE_NAME`_Offset: Used as the offset while converting ADC counts * to mVolts. * * Theory: * Care must be taken to not exceed the maximum value for a 31 bit signed * number in the conversion to uVolts and at the same time not loose * resolution. * To convert adcCounts to microVolts it is required to be multiplied * on 10 million and later divide on gain in counts per 10V. * *******************************************************************************/ int32 `$INSTANCE_NAME`_CountsTo_uVolts(uint32 chan, int16 adcCounts) { int64 uVolts; /* Halt CPU in debug mode if channel is out of valid range */ CYASSERT(chan < `$INSTANCE_NAME`_TOTAL_CHANNELS_NUM); /* Divide the adcCount when accumulate averaging mode selected */ #if(`$INSTANCE_NAME`_DEFAULT_AVG_MODE == `$INSTANCE_NAME`__ACCUMULATE) if((`$INSTANCE_NAME`_channelsConfig[chan] & `$INSTANCE_NAME`_AVERAGING_EN) != 0u) { adcCounts /= `$INSTANCE_NAME`_DEFAULT_AVG_SAMPLES_DIV; } #endif /* `$INSTANCE_NAME`_DEFAULT_AVG_MODE == `$INSTANCE_NAME`__ACCUMULATE */ /* Subtract ADC offset */ adcCounts -= `$INSTANCE_NAME`_offset[chan]; uVolts = ((int64)adcCounts * `$INSTANCE_NAME`_10UV_COUNTS) / `$INSTANCE_NAME`_countsPer10Volt[chan]; return( (int32)uVolts ); }
/******************************************************************************* * Function Name: ADC_SAR_Seq_0_GetResult16 ******************************************************************************** * * Summary: * Gets the data available in the SAR DATA register. * * Parameters: * chan: The ADC channel in which to return the result. The first channel * is 0 and the injection channel if enabled is the number of valid channels. * * Return: * Returns converted data as a signed 16-bit integer * *******************************************************************************/ int16 ADC_SAR_Seq_0_GetResult16(uint32 chan) { uint32 result; /* Halt CPU in debug mode if channel is out of valid range */ CYASSERT(chan < ADC_SAR_Seq_0_TOTAL_CHANNELS_NUM); if(chan < ADC_SAR_Seq_0_SEQUENCED_CHANNELS_NUM) { result = CY_GET_REG32((reg32 *)(ADC_SAR_Seq_0_SAR_CHAN_RESULT_IND + (uint32)(chan << 2u))) & ADC_SAR_Seq_0_RESULT_MASK; } else { #if(ADC_SAR_Seq_0_INJ_CHANNEL_ENABLED) result = ADC_SAR_Seq_0_SAR_INJ_RESULT_REG & ADC_SAR_Seq_0_RESULT_MASK; #else result = 0u; #endif /* ADC_SAR_Seq_0_INJ_CHANNEL_ENABLED */ } return ( (int16)result ); }
/******************************************************************************* * Function Name: `$INSTANCE_NAME`_GetResult16 ******************************************************************************** * * Summary: * Gets the data available in the SAR DATA register. * * Parameters: * chan: The ADC channel in which to return the result. The first channel * is 0 and the injection channel if enabled is the number of valid channels. * * Return: * Returns converted data as a signed 16-bit integer * *******************************************************************************/ int16 `$INSTANCE_NAME`_GetResult16(uint32 chan) { uint32 result; /* Halt CPU in debug mode if channel is out of valid range */ CYASSERT(chan < `$INSTANCE_NAME`_TOTAL_CHANNELS_NUM); if(chan < `$INSTANCE_NAME`_SEQUENCED_CHANNELS_NUM) { result = CY_GET_REG32((reg32 *)(`$INSTANCE_NAME`_SAR_CHAN_RESULT_IND + (uint32)(chan << 2u))) & `$INSTANCE_NAME`_RESULT_MASK; } else { #if(`$INSTANCE_NAME`_INJ_CHANNEL_ENABLED) result = `$INSTANCE_NAME`_SAR_INJ_RESULT_REG & `$INSTANCE_NAME`_RESULT_MASK; #else result = 0u; #endif /* `$INSTANCE_NAME`_INJ_CHANNEL_ENABLED */ } return ( (int16)result ); }