#define IT700_DRIVER_H_MAIN_FILE
#include <IT700Driver.h>
// NOP reset command
BYTE sbyNOPCmdDY[] =
{
HOST_EI_ATTENTION,
02,
00,
eHOST_EI_PACKET_TYPES_REQUEST,
(eHOST_EI_SERVICE_TYPES_EMBEDDED_SERVICES << HOST_EI_OPCODE_SERVICE_TYPE_SHIFT)
+ eHOST_EI_SERVICE_SUBTYPES_EMBEDDED_SERVICES_NO_OP
};
// Software reset command
BYTE sbyResetCmdDY[] =
{
HOST_EI_ATTENTION,
02,
00,
eHOST_EI_PACKET_TYPES_REQUEST,
(eHOST_EI_SERVICE_TYPES_STACK_SERVICES << HOST_EI_OPCODE_SERVICE_TYPE_SHIFT)
+ eHOST_EI_SERVICE_SUBTYPES_STACK_SERVICES_RESET,
0x22 //checksum. This command sent directly (not through s_bSendWaitForResponse)
};
// Go online command
BYTE sbyGoOnlineCmdDY[] =
{
HOST_EI_ATTENTION,
02,
00,
eHOST_EI_PACKET_TYPES_REQUEST,
(eHOST_EI_SERVICE_TYPES_STACK_SERVICES << HOST_EI_OPCODE_SERVICE_TYPE_SHIFT)
+ eHOST_EI_SERVICE_SUBTYPES_STACK_SERVICES_GO_ONLINE
};
/*----------------------------------------------------------------------
* Function name: IT700Driver_ClearIncomingRspPacket
* Input: void
* Output: void
* Purpose: Incoming Response Parsed - Ready to receive
the new one
*-----------------------------------------------------------------------*/
static void s_ClearIncomingRspPacket(void)
{
g_IT700Driver_struct.strInResp.bInRespRxFlag = FALSE;
g_IT700Driver_struct.byIncomingPacketOpcode = 0;
g_IT700Driver_struct.byIncomingPacketType = 0;
}
/*----------------------------------------------------------------------
* Function name: s_byCalcCheckSum
* Input: pointer to data, data length
* Output: checksum value
* Purpose: calaultion of checksum for host command
*-----------------------------------------------------------------------*/
static BYTE s_byCalcCheckSum(PBYTE pbyData, WORD wLength)
{
BYTE byResp = 0;
WORD i;
for (i=0;i<wLength;i++)
{
byResp += pbyData[i];
}
return byResp;
}
/*----------------------------------------------------------------------
* Function name: bSend_WaitForResponse
* Input: pointer to data, data length
* Output: timeout
* Purpose: transmit packet and wait for response
*-----------------------------------------------------------------------*/
static BOOL s_bSendWaitForResponse(PBYTE pbyData, WORD wLength)
{
WORD wTimeoutTicks;
BYTE byCheckSum;
s_ClearIncomingRspPacket();
byCheckSum = s_byCalcCheckSum(&pbyData[eHOST_EI_IDX_LENGTH_LSB], wLength - eHOST_EI_IDX_LENGTH_LSB);
// transmit packet via UART
HAL_UART_UART1_Transmit(pbyData, wLength);
// transmit checksum
HAL_UART_UART1_Transmit(&byCheckSum, 1);
// calculate timeout ticks:
wTimeoutTicks = IT700Driver_wCalculateTimeoutValue(IT700_DRIVER_TIMEOUT_TICKS);
// wait while packet received, and right command ID available
while(
(!g_IT700Driver_struct.strInResp.bInRespRxFlag) ||
(g_IT700Driver_struct.byIncomingPacketOpcode != pbyData[eHOST_EI_IDX_OPCODE])
)
{
if(IT700Driver_bIsTimeoutOccured(wTimeoutTicks))
{
return FALSE; // return FALSE if timeout
}
}
return TRUE;
}
/*----------------------------------------------------------------------
* Function name: IT700Driver_Reset_PIM_HW
* Input: void
* Output: Status
* Purpose: Hard Reset IT700 and wait for Reset response
*-----------------------------------------------------------------------*/
BOOL IT700Driver_bReset_PIM_HW(void)
{
WORD i;
WORD wTimeoutTicks;
BOOL bSuccess = TRUE;
g_IT700Driver_struct.bResetReceived = FALSE;
s_ClearIncomingRspPacket();
HAL_IO_togglePin // Clear RESET pin
(
PIM_RESET_PORT,
PIM_RESET_PIN,
OUT,
CLEAR
);
for(i=0;i<0xFFFF;i++); // delay - at least 7 mSec
HAL_IO_togglePin // Set RESET pin
(
PIM_RESET_PORT,
PIM_RESET_PIN,
OUT,
SET
);
// calculate timer timeout value
wTimeoutTicks = IT700Driver_wCalculateTimeoutValue(IT700_DRIVER_TIMEOUT_TICKS);
while(!g_IT700Driver_struct.bResetReceived) // wait for Reset response to
{ // be received from IT700
if(IT700Driver_bIsTimeoutOccured(wTimeoutTicks))
{
bSuccess = FALSE; // exit if timeout occurred
break;
}
}
if(
((g_IT700Driver_struct.strInResp.pbyInRespBuffer[0] ==
HOST_EI_RESET_CAUSE_SUCCESS) || // check if response is valid
(g_IT700Driver_struct.strInResp.pbyInRespBuffer[0] ==
HOST_EI_RESET_CAUSE_AUTOCONFIG) ||
(g_IT700Driver_struct.strInResp.pbyInRespBuffer[0] ==
HOST_EI_RESET_CAUSE_LOADDEFAULT)) && bSuccess
)
{
g_IT700Driver_struct.bResetReceived = FALSE; // reset received flag
}
else
{
bSuccess = FALSE;
}
// Incoming Packet Parsed - Release the Buffer
s_ClearIncomingRspPacket();
return bSuccess;
}
/*----------------------------------------------------------------------
* Function name: IT700Driver_Reset_PIM_HW
* Input: void
* Output: Status
* Purpose: Soft Reset IT700 and wait for Reset response
*-----------------------------------------------------------------------*/
BOOL IT700Driver_bReset_PIM_SW(void)
{
BOOL bSuccess = TRUE;
WORD wTimeoutTicks;
g_IT700Driver_struct.bResetReceived = FALSE;
s_ClearIncomingRspPacket();
HAL_UART_UART1_Transmit(sbyResetCmdDY, sizeof(sbyResetCmdDY)); // transmit reset command
wTimeoutTicks = IT700Driver_wCalculateTimeoutValue(IT700_DRIVER_TIMEOUT_TICKS);
while(!g_IT700Driver_struct.bResetReceived) // wait for response
{
if(IT700Driver_bIsTimeoutOccured(wTimeoutTicks))
{
bSuccess = FALSE;
break;
}
}
// check if response is valid
if(
((g_IT700Driver_struct.strInResp.pbyInRespBuffer[0] ==
HOST_EI_RESET_CAUSE_SUCCESS) || // check if response is valid
(g_IT700Driver_struct.strInResp.pbyInRespBuffer[0] ==
HOST_EI_RESET_CAUSE_AUTOCONFIG) ||
(g_IT700Driver_struct.strInResp.pbyInRespBuffer[0] ==
HOST_EI_RESET_CAUSE_LOADDEFAULT)) && bSuccess
)
{
g_IT700Driver_struct.bResetReceived = FALSE;
}
else
{
bSuccess = FALSE;
}
// Incoming Packet Parsed - Release the Buffer
s_ClearIncomingRspPacket();
return bSuccess;
}
/*----------------------------------------------------------------------
* Function name: IT700Driver_Init
* Input: void
* Output: void
* Purpose: Init Driver:
Init IT700Driver variables
Set UART CB Rx Function
Start Timeout Timer
*-----------------------------------------------------------------------*/
void IT700Driver_Init(void)
{
g_IT700Driver_struct.byIncomingPacketOpcode = 0;
g_IT700Driver_struct.byIncomingPacketType = 0;
g_IT700Driver_struct.pOnIncomingDataPacket = NULL;
g_IT700Driver_struct.wInLength = 0;
g_IT700Driver_struct.wInPLIdx = 0;
g_IT700Driver_struct.strInData.bInDataRxFlag = FALSE;
g_IT700Driver_struct.strInData.wIncomingDataPacketLength = 0;
g_IT700Driver_struct.strInResp.bInRespRxFlag = FALSE;
g_IT700Driver_struct.byRxState = eIT700DRIVER_RX_STATES_ATTN;
// set pointer to incoming data CB function:
HAL_UART_UART1_SetOnRxByteCB(IT700Driver_CB_OnRxByte);
// start timer
HAL_Timer_StartTimer0(0x00,HAL_TIMER_CLOCK_DIV_1024);
}
/*----------------------------------------------------------------------
* Function name: IT700Driver_CB_OnRxByte
* Input: incoming byte
* Output: void
* Purpose: process incoming data. Save incoming data to
1 of 3 buffers according to the packet ID:
1. Response Buffer
2. Rx Data Packet Buffer
3. NL Buffer
*-----------------------------------------------------------------------*/
void IT700Driver_CB_OnRxByte(BYTE byData)
{
switch(g_IT700Driver_struct.byRxState) // check the RX state
{
// state: no data available
case (eIT700DRIVER_RX_STATES_ATTN):
// check if attention char received
if(byData == HOST_EI_ATTENTION)
{
g_IT700Driver_struct.byRxState = eI
- 1
- 2
前往页