示例#1
0
/*******************************************************************************
* Function Name: CyBtldrCommStart
********************************************************************************
*
* Summary:
*  Starts the UART communication component 
*
* Parameters:  
*  void
*
* Return: 
*  void 
*
* Side Effects:
*   None  
*
*******************************************************************************/
void CyBtldrCommStart(void)
{        
	/* Start UART component and clear the TX,RX buffers */
	UART_Start();
	UART_SpiUartClearRxBuffer();
	UART_SpiUartClearTxBuffer();
}
示例#2
0
int main()
{

	// striplights component init
    StripLights_Start();
	
	/* Start UART component and clear the TX,RX buffers */
	UART_Start();
	UART_SpiUartClearRxBuffer();

	// Switch on ESP8266's PD function
	CH_PD_SetDriveMode(CH_PD_DM_STRONG ) ;
	CH_PD_Write(1);

	// start UART for esp wifi 
	uWIFI_Start();
	uWIFI_SpiUartClearRxBuffer();
    
	CyGlobalIntEnable;  /* Un/comment this line to dis/enable global interrupts. */

	// LED output
	P1_6_SetDriveMode(P1_6_DM_STRONG ) ;
	
	// LED on.
	P1_6_Write(1);
	
	//cycle each of the primaries on all LED's, DisplayClear sets and pushes to LED's, MemoryClear just clears the buffer.
	StripLights_DisplayClear( StripLights_RED_MASK );	CyDelay( 500 ); 
	StripLights_DisplayClear( StripLights_GREEN_MASK );	CyDelay( 500 ); 
	StripLights_DisplayClear( StripLights_BLUE_MASK );	CyDelay( 500 ); 

	// Set to off
	StripLights_DisplayClear( 0 );

	// uncomment for echo back, useful to setup ESP8266 as well
	//echo_uart();
	
	run_server();
	
	
	//reboot on return
    CySoftwareReset();
    
    return 0;
}
void CyBtldrCommReset(void) {
    UART_SpiUartClearRxBuffer();
    UART_SpiUartClearTxBuffer();
}
示例#4
0
void Loop_Master_WaitingSlaveResponse()
{
    uint8 frame_len = 0;
    char itoastr[5];
    uint8 rssi;
    
    /* Testing with interrupts? */
    #ifdef TEST_USING_INTERRUPTS
        
        if (rfrxirqflag == 1)
        {
            frame_len = RFM69_DataPacket_RX(rfdatabytes, &rssi);

            UART_UartPutString("OK...(using ints)\n");

            UART_UartPutString("Slave: ");
            itoa(rfdatabytes[0], itoastr, 10);
            UART_UartPutString(itoastr);
            UART_UartPutString(" RSSI: ");
            itoa(rfdatabytes[1], itoastr, 10);
            UART_UartPutString(itoastr);
            UART_UartPutString(" Temperature: ");
            itoa(rfdatabytes[2], itoastr, 10);
            UART_UartPutString(itoastr);
            UART_UartPutString("\nSelect test :> ");
            
            // change to TX state.
            master_state = 0;
            
            rfrxirqflag = 0;
        }
        else
        {
            if (timercnt == 0) // timed out?
            {
                // change to TX state.
                master_state = 0; 
                UART_SpiUartClearRxBuffer();    // discard received data while in rx mode.
                UART_UartPutString("TimedOut...\n");
                UART_UartPutString("Select test :> ");
            }
        }
        
    #else
    
        // Waiting a packet from a slave containing
        // Slave address + RSSI at slave when master data received + temperature.
        frame_len = RFM69_DataPacket_RX(rfdatabytes, &rssi);

        if (frame_len != 0)
        {
            UART_UartPutString("OK...(polling rfm module)\n");

            UART_UartPutString("Slave: ");
            itoa(rfdatabytes[0], itoastr, 10);
            UART_UartPutString(itoastr);
            UART_UartPutString(" RSSI: ");
            itoa(rfdatabytes[1], itoastr, 10);
            UART_UartPutString(itoastr);
            UART_UartPutString(" Temperature: ");
            itoa(rfdatabytes[2], itoastr, 10);
            UART_UartPutString(itoastr);
            UART_UartPutString("\nSelect test :> ");
            
            // change to TX state.
            master_state = 0;
        }
        else
        {
            if (timercnt == 0) // timed out?
            {
                // change to TX state.
                master_state = 0; 
                UART_SpiUartClearRxBuffer();    // discard received data while in rx mode.
                UART_UartPutString("TimedOut...\n");
                UART_UartPutString("Select test :> ");
            }
        }     
    
    #endif
}
示例#5
0
/*******************************************************************************
* Function Name: CyBtldrCommRead
********************************************************************************
*
* Summary:
*  Receives the command. 
*
* Parameters:  
*  pData:    A pointer to the area to store the block of data received
*             from the device.
*  size:     Maximum size of the read buffer
*  count:    Pointer to an unsigned short variable to write the number
*             of bytes actually read.
*  timeOut:  Number of units to wait before returning because of a timeOut.
*            Timeout is measured in 10s of ms.
*
* Return: 
*  cystatus: This function will return CYRET_SUCCESS if at least one byte is received
*			 successfully within the timeout interval. If no data is received this 
*			 function will return CYRET_EMPTY.
*
* Theory: 
*  'receivedDataCount' is updated with number of bytes received in the UART RX 
*  interrupt routine. This variable is used to check whether some data is received 
*  within the timeout period specified in *.cydwr. If data is received before the timeout, 
*  the control will remain in another loop waiting for more data until no data is 
*  received for a BYTE2BYTE_TIME_OUT(2 ms) interval.
*
*  Note: Increase the BYTE2BYTE_TIME_OUT to 10 ms for baud rates less than 9600.  
* 
*  BYTE2BYTE_TIME_OUT is used for detecting timeout marking end of block data from host. 
*  This has to be set to a value which is greater than the expected maximum delay 
*  between two bytes during a block/packet transmission from the host. 
*  You have to account for the delay in hardware converters while calculating this value,
*  if you are using any USB-UART bridges.   
*******************************************************************************/
cystatus CyBtldrCommRead(uint8 * pData, uint16 Size, uint16 * Count, uint8 TimeOut)
{
    uint16 cntr,dataIndexCntr;
    uint16 tempCount,oldDataCount;
	
    cystatus status = CYRET_EMPTY;

    /* Check whether data is received within the timeout period. 
	*  Timeout period is in units of 10ms.
	*  If at least one byte is received within the timeout interval, wait for more data */
	for (cntr = 0; cntr < TimeOut*10; cntr++)
    {
	    receivedDataCount = UART_SpiUartGetRxBufferSize();
		
		/* If at least one byte is received within the timeout interval enter the next loop
		* waiting for more data reception */
		if(receivedDataCount!=0) 
	   	{
			/* Wait for more data until 2 ms byte to byte time out interval receivedDataCount 
			* variable is updated in on each data reception. If no data is received during the 
			* last 2 ms (BYTE2BYTE_TIME_OUT) then it is considered as end of transmitted data 
			* block (packet) from the host and the program execution will break from the data 
			* awaiting loop with status=CYRET_SUCCESS */
			do{
			   	oldDataCount = receivedDataCount;
				CyDelay(BYTE2BYTE_TIME_OUT);
				receivedDataCount = UART_SpiUartGetRxBufferSize();			    
			}while(receivedDataCount > oldDataCount);
			status = CYRET_SUCCESS;	
			break;
		}
		/* If no data is received, give a delay of 1ms and check again until the Timeout specified in .cydwr. */
		else 
		{
			CyDelay(1);
		}
    }
	
	/* Initialize the data read indexes and Count value*/
	*Count = 0;
	dataIndexCntr = 0;
	
	/* If receivedDataCount>0 , move the received data to the pData buffer */
	while(receivedDataCount > 0)
	{
		tempCount=receivedDataCount;
		*Count  =(*Count ) + tempCount;
		
		/* Check if buffer overflow will occur before moving the data */
		if(*Count < Size)
		{
			for (cntr = 0;((cntr < tempCount) ); cntr++)
			{
				/* Read the data and move it to the pData buffer */
				pData[dataIndexCntr++] = UART_SpiUartReadRxData();   
			}
			/* Disable the interrupts before updating the receivedDataCount and 
			*  re-enable the interrupts after updating */
			CyGlobalIntDisable;
			
			/* subtract the read data count from received data count */
			receivedDataCount=receivedDataCount-tempCount;
			
			CyGlobalIntEnable;
			
			/* Check if the last data received is End of packet(0x17) 
			*  If not wait for additional 5ms */
			if(pData[dataIndexCntr-1]!= END_OF_PACKET)
			    CyDelay(5);
	 	}	
		
		/* If there is no space to move data, break from the loop */
		else
		{
			*Count=(*Count)-tempCount;
			UART_SpiUartClearRxBuffer();
			status = CYRET_EMPTY;
			break;
		}
	}
	return status;
}
示例#6
0
/*******************************************************************************
* Function Name: CyBtldrCommReset
********************************************************************************
*
* Summary:
*  Resets the receive and transmit communication Buffers.
*
* Parameters:  
*  void 
*
* Return: 
*  void 
*
* Side Effects:
*  
*******************************************************************************/
void CyBtldrCommReset(void)
{
    /* Clear RX and TX buffers */
    UART_SpiUartClearRxBuffer();
    UART_SpiUartClearTxBuffer();
}
示例#7
0
文件: app_Ble.c 项目: Adrast/16gr4404
/*******************************************************************************
* Function Name: AppCallBack
********************************************************************************
*
* Summary:
*   Call back function for BLE stack to handle BLESS events
*
* Parameters:
*   event       - the event generated by stack
*   eventParam  - the parameters related to the corresponding event
*
* Return:
*   None.
*
*******************************************************************************/
void AppCallBack(uint32 event, void *eventParam)
{
    CYBLE_GATTC_READ_BY_TYPE_RSP_PARAM_T    *readResponse;
    CYBLE_GAPC_ADV_REPORT_T		            *advReport;
    CYBLE_GATTC_FIND_BY_TYPE_RSP_PARAM_T    *findResponse;
    CYBLE_GATTC_FIND_INFO_RSP_PARAM_T       *findInfoResponse;
    
    switch (event)
    {
        case CYBLE_EVT_STACK_ON:
      
            break;
        
        case CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT:
            
            advReport = (CYBLE_GAPC_ADV_REPORT_T *) eventParam;
            
            /* check if report has manfacturing data corresponding to the intended matching peer */
            if((advReport->eventType == CYBLE_GAPC_SCAN_RSP) && (advReport->dataLen == 0x06) \
                    && (advReport->data[1] == 0xff) && (advReport->data[2] == 0x31)  \
                    && (advReport->data[3] == 0x01) && (advReport->data[4] == 0x3b) \
                    && (advReport->data[5] == 0x04))
            {
                peerDeviceFound = true;
                
                memcpy(peerAddr.bdAddr, advReport->peerBdAddr, sizeof(peerAddr.bdAddr));
                peerAddr.type = advReport->peerAddrType;
                
                #ifdef PRINT_MESSAGE_LOG   
                    UART_UartPutString("\n\r\n\rServer with matching custom service discovered...");
                #endif
            }           
            
            break;    
            
        case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:
            
            /* RESET all flags */
            peerDeviceFound         = false;
            notificationEnabled     = false;
            infoExchangeState       = INFO_EXCHANGE_START;
            
            #ifdef PRINT_MESSAGE_LOG   
                UART_UartPutString("\n\r DISCONNECTED!!! \n\r ");
                while(0 != (UART_SpiUartGetTxBufferSize() + UART_GET_TX_FIFO_SR_VALID));
            #endif
            
            /* RESET Uart and flush all buffers */
            UART_Stop();
            UART_SpiUartClearTxBuffer();
            UART_SpiUartClearRxBuffer();
            UART_Start();
            
            break;
        
        case CYBLE_EVT_GATTC_READ_BY_TYPE_RSP:
            
            readResponse = (CYBLE_GATTC_READ_BY_TYPE_RSP_PARAM_T *) eventParam;
            
            if(0 == memcmp((uint8 *)&(readResponse->attrData.attrValue[5]), (uint8 *)uartTxAttrUuid, 16))
            {
                txCharHandle = readResponse->attrData.attrValue[3];
                txCharHandle |= (readResponse->attrData.attrValue[4] << 8);
                
                infoExchangeState |= TX_ATTR_HANDLE_FOUND;
            }
            else if(0 == memcmp((uint8 *)&(readResponse->attrData.attrValue[5]), (uint8 *)uartRxAttrUuid, 16))
            {
                rxCharHandle = readResponse->attrData.attrValue[3];
                rxCharHandle |= (readResponse->attrData.attrValue[4] << 8);
                
                infoExchangeState |= RX_ATTR_HANDLE_FOUND;
               
            }
            
            break;
            
        case CYBLE_EVT_GATTC_FIND_INFO_RSP:
            
            findInfoResponse = (CYBLE_GATTC_FIND_INFO_RSP_PARAM_T *) eventParam;
            
            if((0x29 == findInfoResponse->handleValueList.list[3]) && \
                                (0x02 == findInfoResponse->handleValueList.list[2]))
            {
                txCharDescHandle = findInfoResponse->handleValueList.list[0];
                txCharDescHandle |= findInfoResponse->handleValueList.list[1] << 8;
            
                infoExchangeState |= TX_CCCD_HANDLE_FOUND;
            }
           
            break;
            
        case CYBLE_EVT_GATTC_XCHNG_MTU_RSP:   
            
            /*set the 'mtuSize' variable based on the minimum MTU supported by both devices */
            if(CYBLE_GATT_MTU > ((CYBLE_GATT_XCHG_MTU_PARAM_T *)eventParam)->mtu)
            {
                mtuSize = ((CYBLE_GATT_XCHG_MTU_PARAM_T *)eventParam)->mtu;
            }
            else
            {
                mtuSize = CYBLE_GATT_MTU;
            }
            
            infoExchangeState |= MTU_XCHNG_COMPLETE;
            
            break;
            
        case CYBLE_EVT_GATTC_HANDLE_VALUE_NTF:
            
            HandleUartRxTraffic((CYBLE_GATTC_HANDLE_VALUE_NTF_PARAM_T *)eventParam);
			
            break;
        
        case CYBLE_EVT_GATTC_FIND_BY_TYPE_VALUE_RSP:
            
            findResponse            = (CYBLE_GATTC_FIND_BY_TYPE_RSP_PARAM_T *) eventParam;
            
            bleUartServiceHandle    = findResponse->range->startHandle;
            bleUartServiceEndHandle = findResponse->range->endHandle;
            
            infoExchangeState |= BLE_UART_SERVICE_HANDLE_FOUND;
            
            break;
        
        case CYBLE_EVT_GATTS_XCNHG_MTU_REQ:
            
            /*set the 'mtuSize' variable based on the minimum MTU supported by both devices */
            if(CYBLE_GATT_MTU > ((CYBLE_GATT_XCHG_MTU_PARAM_T *)eventParam)->mtu)
            {
                mtuSize = ((CYBLE_GATT_XCHG_MTU_PARAM_T *)eventParam)->mtu;
            }
            else
            {
                mtuSize = CYBLE_GATT_MTU;
            }
            
            break;    
        
        case CYBLE_EVT_GATTC_WRITE_RSP:
            
            notificationEnabled = true;
            
            #ifdef PRINT_MESSAGE_LOG   
                UART_UartPutString("\n\rNotifications enabled\n\r");
                UART_UartPutString("\n\rStart entering data:\n\r");
            #endif
            
            break;
        
        case CYBLE_EVT_GATT_CONNECT_IND:
            
            #ifdef PRINT_MESSAGE_LOG   
                UART_UartPutString("\n\rConnection established");             
            #endif
            
            break;
            
        default:            
            break;
    }
}