📄 bsp_i2c.c
字号:
/*
*********************************************************************************************************
* MICRIUM BOARD SUPPORT SUPPORT
*
* (c) Copyright 2003-2006; Micrium, Inc.; Weston, FL
*
* All rights reserved. Protected by international copyright laws.
* Knowledge of the source code may NOT be used to develop a similar product.
* Please help us continue to provide the Embedded community with the finest
* software available. Your honesty is greatly appreciated.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* I2C FUNCTIONS
*
* NXP LPC2468
* on the
* Embedded Artists LPC2468 OEM Board
*
* Filename : bsp.c
* Version : V1.00
* Programmer(s) : BAN
* Note(s) : (1) Adapted from uCLinux distribution.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define BSP_GLOBALS
#include <includes.h>
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
#define I2CON_AA DEF_BIT_02
#define I2CON_SI DEF_BIT_03
#define I2CON_STO DEF_BIT_04
#define I2CON_STA DEF_BIT_05
#define I2CON_I2EN DEF_BIT_06
/* ---- I2C STATUS CODES: MASTER IN TRANSMITTER MODE ------ */
#define I2STAT_SLA_W_ACK 0x18
#define I2STAT_SLA_W_NOACK 0x20
#define I2STAT_DAT_T_ACK 0x28
#define I2STAT_DAT_T_NOACK 0x30
/* ------ I2C STATUS CODES: MASTER IN RECEIVER MODE ------- */
#define I2STAT_SLA_R_ACK 0x40
#define I2STAT_SLA_R_NOACK 0x48
#define I2STAT_DAT_R_ACK 0x50
#define I2STAT_DAT_R_NOACK 0x58
/* ---------- I2C STATUS CODES: MASTER AND SLAVE ---------- */
#define I2STAT_START 0x08
#define I2STAT_REPEATED_START 0x10
#define I2STAT_ARB_LOST 0x38
#define I2STAT_NOP 0x58
#define I2STAT_BUS_ERROR 0x00
//#define PCA9551_ADDRESS 0xC0
#define PCA9532_INPUT0 0x00
#define PCA9532_INPUT1 0x01
#define PCA9532_PSC0 0x02
#define PCA9532_PWM0 0x03
#define PCA9532_PSC1 0x04
#define PCA9532_PWM1 0x05
#define PCA9532_LS0 0x06
#define PCA9532_LS1 0x07
#define PCA9532_LS2 0x08
#define PCA9532_LS3 0x09
//#define SA56004_ADDRESS 0xA0
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL & GLOBAL VARIABLES
*********************************************************************************************************
*/
static CPU_INT08U I2C_Address;
static CPU_INT08U *I2C_BufPtr;
static CPU_INT08U I2C_Count;
static OS_EVENT *I2C_SemWait;
static OS_EVENT *I2C_SemBusy;
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*********************************************************************************************************
** GLOBAL FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* I2C_Init()
*
* Description : Initialize the I2C.
*
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************
*/
void I2C_Init (void)
{
CPU_INT32U pinsel;
CPU_INT08U err;
VICIntSelect &= ~(1 << VIC_I2C0); /* Configure the I2C interrupt as an IRQ source */
VICVectAddr9 = (CPU_INT32U)I2C_ISR_Handler; /* Set the vector address */
VICIntEnClear = (1 << VIC_I2C0); /* Disable the interrupt source */
pinsel = PINSEL1;
pinsel &= 0xFC3FFFFF;
pinsel |= 0x01400000;
PINSEL1 = pinsel;
I20CONCLR = 0x006C; /* Clear flags */
I20SCLH = 0x00F0;
I20SCLL = 0x0160;
I2C_SemWait = OSSemCreate(0);
#if (OS_EVENT_NAME_SIZE > 1)
OSEventNameSet(I2C_SemWait, "I2C Wait", &err);
#endif
I2C_SemBusy = OSSemCreate(1);
#if (OS_EVENT_NAME_SIZE > 1)
OSEventNameSet(I2C_SemBusy, "I2C Busy", &err);
#endif
}
/*
*********************************************************************************************************
* I2C_Read()
*
* Description : Read from the I2C bus.
*
* Argument(s) : p_buf Pointer to the buffer into which the bytes will be stored.
*
* count Number of bytes to read.
*
* Return(s) : none.
*********************************************************************************************************
*/
void I2C_Read (CPU_INT08U address,
CPU_INT08U *p_buf,
CPU_INT08U count)
{
CPU_INT08U err;
OSSemPend(I2C_SemBusy, 0, &err); /* Wait until I2C is free */
I20CONCLR = I2CON_I2EN
| I2CON_STA
| I2CON_SI
| I2CON_AA;
I20CONSET = I2CON_I2EN;
I2C_Address = (address | 0x01); /* Set lower bit of address */
I2C_BufPtr = p_buf;
I2C_Count = count;
I20CONSET = I2CON_STA
| I2CON_AA;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -