📄 i2c_slave.c
字号:
//
// Demo mode two is being selected.
//
case CMD_DEMO2:
{
//
// Set the demo mode to two.
//
g_ulDemoMode = 2;
//
// Done with this command.
//
break;
}
//
// Demo mode three is being selected.
//
case CMD_DEMO3:
{
//
// Set the demo mode to three.
//
g_ulDemoMode = 3;
//
// Done with this command.
//
break;
}
//
// Demo mode four is being selected.
//
case CMD_DEMO4:
{
//
// Set the demo mode to four.
//
g_ulDemoMode = 4;
//
// Done with this command.
//
break;
}
//
// Demo mode five is being selected.
//
case CMD_DEMO5:
{
//
// Set the demo mode to five.
//
g_ulDemoMode = 5;
//
// Done with this command.
//
break;
}
//
// Demo mode six is being selected.
//
case CMD_DEMO6:
{
//
// Set the demo mode to six.
//
g_ulDemoMode = 6;
//
// Done with this command.
//
break;
}
//
// The name string is being sent.
//
case CMD_NAME_STRING:
{
//
// Clear out the name string.
//
g_pcNameString[0] = '\0';
//
// Set the index of the next character to be received to the
// beginning of the name string.
//
g_ulI2CIndex = 0;
//
// Indicate that the name string is being sent so that further
// bytes received are saved.
//
HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_NAME) = 1;
//
// Done with this command.
//
break;
}
//
// The selected icon set is being sent.
//
case CMD_ICONS:
{
//
// Indicate that the icon set is being sent so that further bytes
// received are processed.
//
HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_ICONS) = 1;
//
// Done with this command.
//
break;
}
//
// The tool selection is being sent.
//
case CMD_TOOL:
{
//
// Indicate that the tool is being sent so that further bytes
// received are processed.
//
HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_TOOL) = 1;
//
// Done with this command.
//
break;
}
//
// The table should start running the selected demo.
//
case CMD_RUN:
{
//
// Indicate that the machine should start running.
//
HWREGBITW(&g_ulFlags, MAIN_FLAG_RUN) = 1;
//
// Done with this command.
//
break;
}
//
// The table should stop running.
//
case CMD_STOP:
{
//
// Stop the drawing if one is in progress.
//
if(DrawIsDrawing())
{
DrawStop();
}
//
// Stop the table if it is moving.
//
if(TableIsMoving())
{
TableStop();
}
//
// Done with this command.
//
break;
}
//
// A request to move the table into the shipping positoin.
//
case CMD_SHIP:
{
//
// See if the table is presently moving.
//
if(!TableIsMoving() && !DrawIsDrawing())
{
//
// Indicate that the table should be moved to the shipping
// position.
//
HWREGBITW(&g_ulFlags, MAIN_FLAG_SHIP) = 1;
//
// Indicate that the table should start running.
//
HWREGBITW(&g_ulFlags, MAIN_FLAG_RUN) = 1;
}
//
// Done with this command.
//
break;
}
}
}
//*****************************************************************************
//
//! I2C slave interrupt handler.
//!
//! This function is called when the I2C slave generates an interrupt. It
//! either transmits status information or receives commands depending upon the
//! data transfer requested by the I2C master.
//!
//! \return None.
//
//*****************************************************************************
void
I2CIntHandler(void)
{
long lXPos, lYPos, lZPos;
unsigned long ulStatus;
//
// Get the I2C slave status, which indicates if a transmit or receive is
// required.
//
ulStatus = I2CSlaveStatus(I2C_SLAVE_BASE);
//
// Clear the I2C slave interrupt.
//
I2CSlaveIntClear(I2C_SLAVE_BASE);
//
// See if a data transmit is being requested.
//
if(ulStatus & I2C_SLAVE_CSR_TREQ)
{
//
// See if a start condition occurred immediately before this request.
//
if(HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_START) == 1)
{
//
// A start condition just occurred, so the status transmit should
// be started from the beginning. Clear the start condition
// indicator.
//
HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_START) = 0;
//
// Get the current position of the table.
//
TableGetPosition(&lXPos, &lYPos, &lZPos);
//
// Fill in the status information.
//
g_pucI2CStatus[0] = ((DrawIsDrawing() || TableIsMoving()) ? 0x01 :
0x00);
g_pucI2CStatus[1] = SwitchesLimitRead();
g_pucI2CStatus[2] = 0;
g_pucI2CStatus[3] = 0;
g_pucI2CStatus[4] = lXPos & 0xff;
g_pucI2CStatus[5] = (lXPos >> 8) & 0xff;
g_pucI2CStatus[6] = (lXPos >> 16) & 0xff;
g_pucI2CStatus[7] = (lXPos >> 24) & 0xff;
g_pucI2CStatus[8] = lYPos & 0xff;
g_pucI2CStatus[9] = (lYPos >> 8) & 0xff;
g_pucI2CStatus[10] = (lYPos >> 16) & 0xff;
g_pucI2CStatus[11] = (lYPos >> 24) & 0xff;
g_pucI2CStatus[12] = lZPos & 0xff;
g_pucI2CStatus[13] = (lZPos >> 8) & 0xff;
g_pucI2CStatus[14] = (lZPos >> 16) & 0xff;
g_pucI2CStatus[15] = (lZPos >> 24) & 0xff;
//
// Set the status index to zero so that the first byte of the
// status information will be transmitted next.
//
g_ulI2CIndex = 0;
}
//
// See if there is more status information to be transmitted.
//
if(g_ulI2CIndex < 16)
{
//
// Transmit the next byte of status information.
//
I2CSlaveDataPut(I2C_SLAVE_BASE, g_pucI2CStatus[g_ulI2CIndex++]);
}
else
{
//
// All the status information has been transmitted, so pad the
// remainder of the request with zeros.
//
I2CSlaveDataPut(I2C_SLAVE_BASE, 0x00);
}
}
//
// See if a data receive is being requested.
//
if(ulStatus & I2C_SLAVE_CSR_RREQ)
{
//
// See if a start condition occurred immediately before this request.
//
if(HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_START) == 1)
{
//
// A start condition just occurred, so command reception should be
// started from the beginning. Clear the start condition
// indicator.
//
HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_START) = 0;
//
// Clear the name receive flag.
//
HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_NAME) = 0;
//
// Handle the command that was just sent by the I2C master.
//
I2CHandleCommand();
}
//
// Otherwise, see if the name is being sent and there is more space
// available in the local buffer.
//
else if((HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_NAME) == 1) &&
(g_ulI2CIndex != (sizeof(g_pcNameString) - 1)))
{
//
// Receive the next character of the name string.
//
g_pcNameString[g_ulI2CIndex++] = I2CSlaveDataGet(I2C_SLAVE_BASE);
//
// NULL terminate the name string.
//
g_pcNameString[g_ulI2CIndex] = '\0';
}
//
// Otherwise, see if the icon set is being sent and this is the first
// byte of the set.
//
else if(HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_ICONS) == 1)
{
//
// Receive the byte containing the selected icons.
//
g_ulIcons = I2CSlaveDataGet(I2C_SLAVE_BASE);
//
// Clear the icon set flag so that further bytes are ignored.
//
HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_ICONS) = 0;
}
//
// Otherwise, see if the tool selection is being sent and this is the
// first byte of the selection.
//
else if(HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_TOOL) == 1)
{
//
// Receive the byte containing the selected tool.
//
g_ulTool = I2CSlaveDataGet(I2C_SLAVE_BASE);
//
// Clear the tool selection flag so that further bytes are ignored.
//
HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_TOOL) = 0;
}
//
// Otherwise, this byte should simply be ignored.
//
else
{
I2CSlaveDataGet(I2C_SLAVE_BASE);
}
}
}
//*****************************************************************************
//
//! Initialize the I2C slave interface.
//!
//! This function initializes the I2C slave interface and prepares it for
//! communication with the user interface microcontroller.
//!
//! \return None.
//
//*****************************************************************************
void
I2CInit(void)
{
//
// Clear the I2C flags.
//
g_ulI2CFlags = 0;
//
// Configure the I2C pins for proper operation.
//
GPIODirModeSet(GPIO_PORTB_BASE, PIN_SCL | PIN_SDA, GPIO_DIR_MODE_HW);
GPIOPadConfigSet(GPIO_PORTB_BASE, PIN_SCL | PIN_SDA, GPIO_STRENGTH_2MA,
GPIO_PIN_TYPE_OD);
//
// Initialize the I2C slave interface and set its address.
//
I2CSlaveInit(I2C_SLAVE_BASE, I2C_SLAVE_ADDR);
//
// Enable the I2C slave interrupt.
//
I2CSlaveIntEnable(I2C_SLAVE_BASE);
IntEnable(INT_I2C);
//
// Setup the data pin GPIO interrupt so that it occurs on both rising and
// falling edges of the I2C data line.
//
GPIOIntTypeSet(GPIO_PORTB_BASE, PIN_SDA, GPIO_BOTH_EDGES);
GPIOPinIntEnable(GPIO_PORTB_BASE, PIN_SDA);
IntEnable(INT_GPIOB);
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -