/**************************************************************************************************
Filename: hal_i2c.c
Revised: $Date: 2010-10-12 17:32:20 -0700 (Tue, 12 Oct 2010) $
Revision: $Revision: 24108 $
Description: I2C driver implementation.
Copyright 2006 - 2010 Texas Instruments Incorporated. All rights reserved.
IMPORTANT: Your use of this Software is limited to those specific rights
granted under the terms of a software license agreement between the user
who downloaded the software, his/her employer (which must be your employer)
and Texas Instruments Incorporated (the "License"). You may not use this
Software unless you agree to abide by the terms of the License. The License
limits your use, and you acknowledge, that the Software may not be modified,
copied or distributed unless embedded on a Texas Instruments microcontroller
or used solely and exclusively in conjunction with a Texas Instruments radio
frequency transceiver, which is integrated into your product. Other than for
the foregoing purpose, you may not use, reproduce, copy, prepare derivative
works of, modify, distribute, perform, display or sell this Software and/or
its documentation for any purpose.
YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
Should you have any questions regarding your right to use this Software,
contact Texas Instruments Incorporated at www.TI.com.
**************************************************************************************************/
/**************************************************************************************************
This file contains the I2C interface to off-chip serial EEPROM. While
much of the driver is generic, certain portions are specific to the
Atmel part AT24C1024. For example, the 17th address bit occurs in the
device address byte where one of the address pins should be. Different
1 Mb parts use a different bit location. This could be abstracted at
a later time if this is the only differentce among these parts.
**************************************************************************************************/
#include "ioCC2530.h"
//#include "gDef.h"
//#include "zcomdef.h"
#include "hal_i2c.h"
#include "hal_defs.h"
#include "hal_mcu.h"
#define STATIC static
#if !defined HAL_I2C_RETRY_CNT
#define HAL_I2C_RETRY_CNT 3
#endif
// the default cofiguration below uses P1.6 for SDA and P0.0 for SCL.
// change these as needed.
#ifndef OCM_CLK_PORT
#define OCM_CLK_PORT 0
#endif
#ifndef OCM_DATA_PORT
#define OCM_DATA_PORT 0
#endif
#ifndef OCM_CLK_PIN
#define OCM_CLK_PIN 7
#endif
#ifndef OCM_DATA_PIN
#define OCM_DATA_PIN 6
#endif
// General I/O definitions
#define IO_GIO 0 // General purpose I/O
#define IO_PER 1 // Peripheral function
#define IO_IN 0 // Input pin
#define IO_OUT 1 // Output pin
#define IO_PUD 0 // Pullup/pulldn input
#define IO_TRI 1 // Tri-state input
#define IO_PUP 0 // Pull-up input pin
#define IO_PDN 1 // Pull-down input pin
#define OCM_ADDRESS (0xA0)
#define OCM_READ (0x01)
#define OCM_WRITE (0x00)
#define SMB_ACK (0)
#define SMB_NAK (1)
#define SEND_STOP (0)
#define NOSEND_STOP (1)
#define SEND_START (0)
#define NOSEND_START (1)
// device specific as to where the 17th address bit goes...
// ************************* MACROS ************************************
#undef P
/* I/O PORT CONFIGURATION */
#define CAT1(x,y) x##y // Concatenates 2 strings
#define CAT2(x,y) CAT1(x,y) // Forces evaluation of CAT1
// OCM port I/O defintions
// Builds I/O port name: PNAME(1,INP) ==> P1INP
#define PNAME(y,z) CAT2(P,CAT2(y,z))
// Builds I/O bit name: BNAME(1,2) ==> P1_2
#define BNAME(port,pin) CAT2(CAT2(P,port),CAT2(_,pin))
// OCM port I/O defintions
#define OCM_SCL BNAME(OCM_CLK_PORT, OCM_CLK_PIN)
#define OCM_SDA BNAME(OCM_DATA_PORT, OCM_DATA_PIN)
#define IO_DIR_PORT_PIN(port, pin, dir) \
{\
if ( dir == IO_OUT ) \
PNAME(port,DIR) |= (1<<(pin)); \
else \
PNAME(port,DIR) &= ~(1<<(pin)); \
}
#define OCM_DATA_HIGH()\
{ \
IO_DIR_PORT_PIN(OCM_DATA_PORT, OCM_DATA_PIN, IO_IN); \
}
#define OCM_DATA_LOW() \
{ \
IO_DIR_PORT_PIN(OCM_DATA_PORT, OCM_DATA_PIN, IO_OUT); \
OCM_SDA = 0;\
}
#define IO_FUNC_PORT_PIN(port, pin, func) \
{ \
if( port < 2 ) \
{ \
if ( func == IO_PER ) \
PNAME(port,SEL) |= (1<<(pin)); \
else \
PNAME(port,SEL) &= ~(1<<(pin)); \
} \
else \
{ \
if ( func == IO_PER ) \
P2SEL |= (1<<(pin>>1)); \
else \
P2SEL &= ~(1<<(pin>>1)); \
} \
}
#define IO_IMODE_PORT_PIN(port, pin, mode) \
{ \
if ( mode == IO_TRI ) \
PNAME(port,INP) |= (1<<(pin)); \
else \
PNAME(port,INP) &= ~(1<<(pin)); \
}
#define IO_PUD_PORT(port, dir) \
{ \
if ( dir == IO_PDN ) \
P2INP |= (1<<(port+5)); \
else \
P2INP &= ~(1<<(port+5)); \
}
STATIC void hali2cSend( uint8 *buffer, uint16 len, uint8 sendStart, uint8 sendStop );
STATIC _Bool hali2cSendByte( uint8 dByte );
STATIC void hali2cWrite( bool dBit );
STATIC void hali2cClock( bool dir );
STATIC void hali2cStart( void );
STATIC void hali2cStop( void );
STATIC void hali2cReceive( uint8 address, uint8 *buffer, uint16 len );
STATIC uint8 hali2cReceiveByte( void );
STATIC _Bool hali2cRead( void );
STATIC void hali2cSendDeviceAddress(uint8 address);
STATIC __near_func void hali2cWait( uint8 );
STATIC uint8 s_xmemIsInit;
/*********************************************************************
* @fn HalI2CInit
* @brief Initializes two-wire serial I/O bus
* @param void
* @return void
*/
void HalI2CInit( void )
{
if (!s_xmemIsInit) {
s_xmemIsInit = 1;
// Set port pins as inputs
IO_DIR_PORT_PIN( OCM_CLK_PORT, OCM_CLK_PIN, IO_IN );
IO_DIR_PORT_PIN( OCM_DATA_PORT, OCM_DATA_PIN, IO_IN );
// Set for general I/O operation
IO_FUNC_PORT_PIN( OCM_CLK_PORT, OCM_CLK_PIN, IO_GIO );
IO_FUNC_PORT_PIN( OCM_DATA_PORT, OCM_DATA_PIN, IO_GIO );
// Set I/O mode for pull-up/pull-down
IO_IMODE_PORT_PIN( OCM_CLK_PORT, OCM_CLK_PIN, IO_PUD );
IO_IMODE_PORT_PIN( OCM_DATA_PORT, OCM_DATA_PIN, IO_PUD );
// Set pins to pull-up
IO_PUD_PORT( OCM_CLK_PORT, IO_PUP );
IO_PUD_PORT( OCM_DATA_PORT, IO_PUP );
}
}
int8 HalI2CReceive(uint8 address, uint8 *buf, uint16 len)
{
hali2cReceive(address, buf, len);
return 0;
}
int8 HalI2CSend(uint8 address, uint8 *buf, uint16 len)
{
// begin the write sequence with the address byte
hali2cSendDeviceAddress(address);
hali2cSend(buf, len, NOSEND_START, SEND_STOP);
return 0;
}
void HalI2cStart(uint8 address)
{
hali2cSendDeviceAddress(address);;
}
void HalI2CSendByte(uint8 *buf, uint16 len)
{
hali2cSend(buf, len, NOSEND_START, NOSEND_STOP);
}
void HalI2CStop(void)
{
hali2cStop();
}
/*********************************************************************
* @fn hali2cSend
* @brief Sends buffer contents to SM-Bus
CC2530 I2C 驱动
4星 · 超过85%的资源 需积分: 50 110 浏览量
2017-07-11
13:34:37
上传
评论 2
收藏 6KB RAR 举报
詆調
- 粉丝: 160
- 资源: 10