Beispiel #1
0
int sl_UartSetMode(const SlUartIfParams_t* pUartParams)
{
	_SlUartSetModeMsg_u Msg;
        UINT32 magicCode = 0xFFFFFFFF;
        
	Msg.Cmd.BaudRate = pUartParams->BaudRate;
	Msg.Cmd.FlowControlEnable = pUartParams->FlowControlEnable;

    
    VERIFY_RET_OK(_SlDrvCmdOp((_SlCmdCtrl_t *)&_SlUartSetModeCmdCtrl, &Msg, NULL));
        
	/* cmd response OK, we can continue with the handshake */
	if (SL_RET_CODE_OK == Msg.Rsp.status)
	{
            sl_IfMaskIntHdlr();

            /* Close the comm port */
            sl_IfClose(g_pCB->FD);
              
              /* Re-open the comm port */
            sl_IfOpen((char*)pUartParams, UART_IF_OPEN_FLAG_RE_OPEN);

            sl_IfUnMaskIntHdlr();
              
            /* send the magic code and wait for the response */
            sl_IfWrite(g_pCB->FD, (unsigned char*)&magicCode, 4);
            
            magicCode = UART_SET_MODE_MAGIC_CODE;
            sl_IfWrite(g_pCB->FD, (unsigned char*)&magicCode, 4);
            
            /* clear magic code */
            magicCode = 0;
            
            /* wait (blocking) till the magic code to be returned from device */
            sl_IfRead(g_pCB->FD, (unsigned char*)&magicCode, 4);
            
            /* check for the received magic code matching */
            if (UART_SET_MODE_MAGIC_CODE != magicCode)
            {
                _SL_ASSERT(0);
            }
	}
       
	return (int)Msg.Rsp.status;
}
/*!
    \brief Test the spi communication

    \param[in]      none

    \return         upon successful, the function shall return 0.
                    Otherwise, -1 shall be returned

    \warning
*/
static int TestSpi()
{
    const _SlSyncPattern_t     H2NCnysPattern = H2N_CNYS_PATTERN;

    _SlFd_t FD = 0;
    unsigned int             irqCheck = 0;
    unsigned char            pBuf[8] = {'\0'};
    unsigned char            SyncCnt  = 0;
    unsigned char            ShiftIdx = 0;
  
    /* Start the state machine */
    SetTestStage(TEST_STAGE_SPI_TEST_BEGIN);
  
    /* Initialize the Spi interface */
    FD = sl_IfOpen(0, 0);
    SetTestStage(TEST_STAGE_SPI_OPEN_PASSED);
  
    /* Register IRQ handler */
    sl_IfRegIntHdlr((P_EVENT_HANDLER)DiagIrqHandler, 0);
  
    /*Make Sure that the device is turned off */
    sl_DeviceDisable();
    SetTestStage(TEST_STAGE_DISABLE_PASSED);
  
    /* Save IRQ counter before activating the Device */
    irqCheck = g_irqCounter;
  
    /* Turn on the device */
    sl_DeviceEnable();
    SetTestStage(TEST_STAGE_ENABLE_PASSED);
  
    /* Wait until we get an increment on IRQ value */
    while( irqCheck == g_irqCounter )
    {
    }  
  
    SetTestStage(TEST_STAGE_SPI_IRQ_PASSED);
  
    /* Generate the sync pattern for getting the response */
    sl_IfWrite(FD, (unsigned char *)&H2NCnysPattern, SYNC_PATTERN_LEN);
    SetTestStage(TEST_STAGE_SPI_WRITE_PASSED);
  
    /* Read 4 bytes from the Spi */
    sl_IfRead(FD, &pBuf[0], 4);
  
    /* Sync on read pattern */
    while ( ! N2H_SYNC_PATTERN_MATCH(pBuf, TxSeqNum))
    {
        /* Read next 4 bytes to Low 4 bytes of buffer */
        if(0 == (SyncCnt % SYNC_PATTERN_LEN))
        {
            sl_IfRead(FD, &pBuf[4], 4);
        }
     
        /* Shift Buffer Up for checking if the sync is shifted */
        for(ShiftIdx = 0; ShiftIdx < 7; ShiftIdx++)
        {
            pBuf[ShiftIdx] = pBuf[ShiftIdx+1];
        }
        pBuf[7] = 0;
        SyncCnt++;
    }
  
    /*Sync pattern found. If needed, complete number of read bytes to multiple
    * of 4 (protocol align) */
    SyncCnt %= SYNC_PATTERN_LEN;
  
    if(SyncCnt > 0)
    {
        *(UINT32 *)&pBuf[0] = *(UINT32 *)&pBuf[4];
        sl_IfRead(FD, &pBuf[SYNC_PATTERN_LEN - SyncCnt], SyncCnt);
    }
    else
    {
        sl_IfRead(FD, &pBuf[0], 4);
    }
  
    /* Scan for possible double pattern */
    while( N2H_SYNC_PATTERN_MATCH(pBuf, TxSeqNum))
    {
        sl_IfRead(FD, &pBuf[0], SYNC_PATTERN_LEN);
    }
  
    /* Read the Resp Specific header (4 more bytes) */
    sl_IfRead(FD, &pBuf[SYNC_PATTERN_LEN], 4);
    SetTestStage(TEST_STAGE_SPI_READ_PASSED);
  
    /* Check the init complete message opcode */
    if(SL_OPCODE_DEVICE_INITCOMPLETE != (*(unsigned short*)(pBuf)))
    {
        UartWrite((unsigned char *)"Error in Spi Testing\r\n");
        return -1; /* failed to read init complete */
    }
  
    SetTestStage(TEST_STAGE_INIT_COMPLETE_READ_PASSED);
    SetTestStage(TEST_STAGE_SPI_TEST_COMPLETE);
  
    return SUCCESS;
}