?? i2c_slave.c
字號:
//*****************************************************************************
//
// i2c_slave.c - I2C slave interface for the main microcontroller.
//
// Copyright (c) 2006-2007 Luminary Micro, Inc. All rights reserved.
//
// Software License Agreement
//
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
//
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws. All rights are reserved. Any use in violation
// of the foregoing restrictions may subject the user to criminal sanctions
// under applicable laws, as well as to civil liability for the breach of the
// terms and conditions of this license.
//
// THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 220 of sw01246.
//
//*****************************************************************************
#include "hw_i2c.h"
#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "gpio.h"
#include "i2c.h"
#include "interrupt.h"
#include "i2ccmds.h"
#include "draw.h"
#include "main.h"
#include "switches.h"
#include "table.h"
//*****************************************************************************
//
//! \page main_i2c_slave_intro Introduction
//!
//! The main microcontroller receives commands from the user interface
//! microcontroller via an I2C link. The I2C interface on the main
//! microcontroller is run in slave mode; I2C writes provide configuration and
//! commands, and I2C reads return status of the CNC machine.
//!
//! The I2C interface is operated in interrupt driven mode so that it incurs a
//! minimal overhead to the remainder of the application. Since the I2C
//! interface operates one byte per interrupt, special handling is required for
//! multi-byte commands and status. The first interrupt will determine what
//! operation is being performed (status read, sending the name string, etc.).
//! Flags are set that help determine how subsequent interrupts (and therefore
//! bytes) are handled.
//!
//! The code for the I2C slave interface is contained in
//! <tt>main_micro/i2c_slave.c</tt>, with <tt>main_micro/i2c_slave.h</tt>
//! containing the API definitions for use by the remainder of the
//! application.
//
//*****************************************************************************
//*****************************************************************************
//
//! \defgroup main_i2c_slave_api Definitions
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
//! The GPIO pin on port B that is used for the I2C clock signal.
//
//*****************************************************************************
#define PIN_SCL GPIO_PIN_2
//*****************************************************************************
//
//! The GPIO pin on port B that is used for the I2C data signal.
//
//*****************************************************************************
#define PIN_SDA GPIO_PIN_3
//*****************************************************************************
//
//! The speed used for diagnostic moves along the X axis, specified in steps
//! per second.
//
//*****************************************************************************
#define X_SPEED 2400
//*****************************************************************************
//
//! The acceleration used for diagnostic moves along the X axis, specified in
//! steps per second^2.
//
//*****************************************************************************
#define X_ACCEL 30000
//*****************************************************************************
//
//! The speed used for diagnostic moves along the Y axis, specified in steps
//! per second.
//
//*****************************************************************************
#define Y_SPEED 2400
//*****************************************************************************
//
//! The acceleration used for diagnostic moves along the Y axis, specified in
//! steps per second^2.
//
//*****************************************************************************
#define Y_ACCEL 30000
//*****************************************************************************
//
//! The speed used for diagnostic moves along the Z axis, specified in steps
//! per second.
//
//*****************************************************************************
#define Z_SPEED 1200
//*****************************************************************************
//
//! The acceleration used for diagnostic moves along the Z axis, specified in
//! steps per second^2.
//
//*****************************************************************************
#define Z_ACCEL 30000
//*****************************************************************************
//
//! A set of flags for controlling the I2C slave interface; contains the
//! #I2CS_FLAG_START, #I2CS_FLAG_STOP, #I2CS_FLAG_NAME, #I2CS_FLAG_ICONS, and
//! #I2CS_FLAG_TOOL flags.
//
//*****************************************************************************
static unsigned long g_ulI2CFlags;
//*****************************************************************************
//
//! The bit number in #g_ulI2CFlags of the flag that is set when an I2C start
//! condition is detected.
//
//*****************************************************************************
#define I2CS_FLAG_START 0
//*****************************************************************************
//
//! The bit number in #g_ulI2CFlags of the flag that is set when an I2C stop
//! condition is detected.
//
//*****************************************************************************
#define I2CS_FLAG_STOP 1
//*****************************************************************************
//
//! The bit number in #g_ulI2CFlags of the flag that is set when the name
//! string is being transferred via the I2C interface.
//
//*****************************************************************************
#define I2CS_FLAG_NAME 2
//*****************************************************************************
//
//! The bit number in #g_ulI2CFlags of the flag that is set when the icon set
//! is being transferred via the I2C interface.
//
//*****************************************************************************
#define I2CS_FLAG_ICONS 3
//*****************************************************************************
//
//! The bit number in #g_ulI2CFlags of the flag that is set when the tool
//! selection is being transferred via the I2C interface.
//
//*****************************************************************************
#define I2CS_FLAG_TOOL 4
//*****************************************************************************
//
//! This array contains the status being written in response to a I2C read
//! request.
//
//*****************************************************************************
static unsigned char g_pucI2CStatus[16];
//*****************************************************************************
//
//! The current index into the name string (for name writes) or the status
//! buffer (for status reads); used to sequence through the corresponding
//! buffers one byte at a time for each I2C data request.
//
//*****************************************************************************
static unsigned long g_ulI2CIndex;
//*****************************************************************************
//
//! Looks for I2C start and stop conditions.
//!
//! This interrupt handler is called in response to edges on the I2C data line.
//! If the data line changes while the I2C clock line is high, then a start or
//! stop condition has occurred.
//!
//! \return None.
//
//*****************************************************************************
void
GPIOBIntHandler(void)
{
unsigned long ulPins;
//
// Clear the GPIO pin interrupt.
//
GPIOPinIntClear(GPIO_PORTB_BASE, PIN_SDA);
//
// Read the current state of the I2C clock and data lines.
//
ulPins = GPIOPinRead(GPIO_PORTB_BASE, PIN_SCL | PIN_SDA);
//
// If the clock line is high and the data line is low, then a start
// condition has just occurred.
//
if(ulPins == PIN_SCL)
{
//
// Indicate that a start has just occurred.
//
HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_START) = 1;
}
//
// If the clock line is high and the data line is high, then a stop
// condition has just occurred.
//
else if(ulPins == (PIN_SCL | PIN_SDA))
{
//
// Indicate that a stop has just occurred.
//
HWREGBITW(&g_ulI2CFlags, I2CS_FLAG_STOP) = 1;
}
}
//*****************************************************************************
//
//! Handle a command sent from the I2C master.
//!
//! This function processes a command sent from the I2C master. Most commands
//! are handled completely from the single command byte, while the name string,
//! icon set, and tool selection commands require additional I2C transfers.
//!
//! \return None.
//
//*****************************************************************************
static void
I2CHandleCommand(void)
{
long lXPos, lYPos, lZPos;
//
// Read the command byte from the I2C slave and determine how to handle it.
//
switch(I2CSlaveDataGet(I2C_SLAVE_BASE))
{
//
// A positive move along the X axis is being requested.
//
case CMD_X_PLUS:
{
//
// See if the table is presently moving.
//
if(!TableIsMoving() && !DrawIsDrawing())
{
//
// The table is not moving, so get the current table position.
//
TableGetPosition(&lXPos, &lYPos, &lZPos);
//
// Move in the positive direction along the X axis.
//
TableMoveLine(lXPos + 0x40000000, lYPos, lZPos, X_SPEED,
X_ACCEL);
}
//
// Done with this command.
//
break;
}
//
// A negative move along the X axis is being requested.
//
case CMD_X_MINUS:
{
//
// See if the table is presently moving.
//
if(!TableIsMoving() && !DrawIsDrawing())
{
//
// The table is not moving, so get the current table position.
//
TableGetPosition(&lXPos, &lYPos, &lZPos);
//
// Move in the negative direction along the X axis.
//
TableMoveLine(lXPos - 0x40000000, lYPos, lZPos, X_SPEED,
X_ACCEL);
}
//
// Done with this command.
//
break;
}
//
// A positive move along the Y axis is being requested.
//
case CMD_Y_PLUS:
{
//
// See if the table is presently moving.
//
if(!TableIsMoving() && !DrawIsDrawing())
{
//
// The table is not moving, so get the current table position.
//
TableGetPosition(&lXPos, &lYPos, &lZPos);
//
// Move in the positive direction along the Y axis.
//
TableMoveLine(lXPos, lYPos + 0x40000000, lZPos, Y_SPEED,
Y_ACCEL);
}
//
// Done with this command.
//
break;
}
//
// A negative move along the Y axis is being requested.
//
case CMD_Y_MINUS:
{
//
// See if the table is presently moving.
//
if(!TableIsMoving() && !DrawIsDrawing())
{
//
// The table is not moving, so get the current table position.
//
TableGetPosition(&lXPos, &lYPos, &lZPos);
//
// Move in the negative direction along the Y axis.
//
TableMoveLine(lXPos, lYPos - 0x40000000, lZPos, Y_SPEED,
Y_ACCEL);
}
//
// Done with this command.
//
break;
}
//
// A positive move along the Z axis is being requested.
//
case CMD_Z_PLUS:
{
//
// See if the table is presently moving.
//
if(!TableIsMoving() && !DrawIsDrawing())
{
//
// The table is not moving, so get the current table position.
//
TableGetPosition(&lXPos, &lYPos, &lZPos);
//
// Move in the positive direction along the Z axis.
//
TableMoveLine(lXPos, lYPos, lZPos + 0x40000000, Z_SPEED,
Z_ACCEL);
}
//
// Done with this command.
//
break;
}
//
// A negative move along the Z axis is being requested.
//
case CMD_Z_MINUS:
{
//
// See if the table is presently moving.
//
if(!TableIsMoving() && !DrawIsDrawing())
{
//
// The table is not moving, so get the current table position.
//
TableGetPosition(&lXPos, &lYPos, &lZPos);
//
// Move in the negative direction along the Z axis.
//
TableMoveLine(lXPos, lYPos, lZPos - 0x40000000, Z_SPEED,
Z_ACCEL);
}
//
// Done with this command.
//
break;
}
//
// Demo mode one is being selected.
//
case CMD_DEMO1:
{
//
// Set the demo mode to one.
//
g_ulDemoMode = 1;
//
// Done with this command.
//
break;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -