📄 main.c
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name : main.c
* Author : MCD Application Team
* Date First Issued : 05/18/2006 : Version 1.0
* Description : Main program body
********************************************************************************
* History:
* 05/24/2006 : Version 1.1
* 05/18/2006 : Version 1.0
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "91x_lib.h"
/* Private typedef -----------------------------------------------------------*/
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
/* Init Structure definition */
I2C_InitTypeDef I2C_Struct;
GPIO_InitTypeDef GPIO_Struct;
/* Private define ------------------------------------------------------------*/
#define BUFFER_SIZE 4
#define I2C_SLAVE_ADDRESS7 0xA0
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 Tx_Idx=0, Rx_Idx=0;
u8 I2C0_Buffer_Tx[BUFFER_SIZE], I2C0_Buffer_Rx[BUFFER_SIZE]={0, 0, 0, 0};
u8 I2C1_Buffer_Tx[BUFFER_SIZE], I2C1_Buffer_Rx[BUFFER_SIZE]={0, 0, 0, 0};
TestStatus TransferStatus1, TransferStatus2;
/* Private function prototypes -----------------------------------------------*/
void Peripherals_Init(u16 I2C0OwnAddr, u16 I2C1OwnAddr);
void Delay(u32 Xtime);
void Fill_Buffer(u8 *Buffer, u8 Data);
TestStatus Buffercmp(u8* pBuffer, u8* pBuffer1, u16 BufferLength);
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void main(void)
{
#ifdef DEBUG
debug();
#endif
/*--------------------------------------------------*/
/* Transmission Phase-------------------------------*/
/*--------------------------------------------------*/
/* Enable I2C0, I2C1 */
I2C_Cmd (I2C0, ENABLE);
I2C_Cmd (I2C1, ENABLE);
/* Configure GPIO0, I2C0 and I2C1 */
Peripherals_Init(0xA0, 0xA0);
/* Fill buffer to send */
Fill_Buffer(I2C0_Buffer_Tx, 0x1);
/* Send STRAT condition */
I2C_GenerateStart (I2C0, ENABLE);
/* Test on EV5 and clear it */
while( ! I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECT) ); // EV5
/* Send I2C1 Address for write */
I2C_Send7bitAddress (I2C0, I2C_SLAVE_ADDRESS7, I2C_MODE_TRANSMITTER);
/* Test on EV6 and clear it */
while( ! I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECTED) ); // EV6
/* Clear EV6 by set again the PE bit */
I2C_Cmd (I2C0, ENABLE);
/* Send Data to write on I2C1 slave */
while (Tx_Idx < BUFFER_SIZE)
{
/* Send Master data */
I2C_SendData(I2C0, I2C0_Buffer_Tx[Tx_Idx++]);
/* Test on EV2 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_SLAVE_BYTE_RECEIVED)); // EV2
/* Read slave received data */
I2C1_Buffer_Rx[Rx_Idx++] = I2C_ReceiveData(I2C1);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //EV8
}
/* Generate STOP condition to close communication */
I2C_GenerateSTOP (I2C0, ENABLE);
/* Disable I2C0 */
I2C_Cmd(I2C0, DISABLE);
I2C_Cmd(I2C1, DISABLE);
/* Check if the transmitted data is read correctly */
TransferStatus1 = Buffercmp(I2C0_Buffer_Tx, I2C1_Buffer_Rx, BUFFER_SIZE);
/* TransferStatus = PASSED, if the transmitted from I2C0 and received data
by the I2C1 are the same */
/* TransferStatus = FAILED, if the transmitted from I2C0 and received data
by the I2C1 are different */
/*--------------------------------------------------*/
/* Delay between transmission and reception --------*/
/*--------------------------------------------------*/
Delay(10000);
/*--------------------------------------------------*/
/* Reception Phase----------------------------------*/
/*--------------------------------------------------*/
/* reset counters */
Tx_Idx=Rx_Idx=0;
/* Enable I2C0 I2C1 */
I2C_Cmd (I2C0, ENABLE);
I2C_Cmd (I2C1, ENABLE);
/* Initialize I2C0 I2C1 */
I2C_Init(I2C0, &I2C_Struct);
I2C_Init(I2C1, &I2C_Struct);
/* Fill buffer to send */
Fill_Buffer(I2C1_Buffer_Tx, 0x5);
/* Send STRAT condition */
I2C_GenerateStart (I2C0, ENABLE);
/* Test on EV5 and clear it */
while( ! I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECT) ); // EV5
/* Send I2C1 Address */
I2C_Send7bitAddress (I2C0, I2C_SLAVE_ADDRESS7, I2C_MODE_RECEIVER);
/* Test on EV1 and clear it */
while( ! I2C_CheckEvent(I2C1, I2C_EVENT_SLAVE_ADDRESS_MATCHED) ); // EV1
/* Test on EV6 and clear it */
while( ! I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECTED) ); // EV6
/* Clear EV6 by set again the PE bit */
I2C_Cmd (I2C0, ENABLE);
/* Send Data to write on I2C0 Master */
while (Rx_Idx < BUFFER_SIZE)
{
/* Test on EV3 and clear it */
if(I2C_CheckEvent(I2C1, I2C_EVENT_SLAVE_BYTE_TRANSMITTED)) //EV3
/* Send Slave data */
I2C_SendData(I2C1, I2C1_Buffer_Tx[Tx_Idx++]);
/* Test on EV7 and clear it */
if ( I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED)) // EV7
{
/* Disable I2C0 acknowledgement */
if ( Rx_Idx == BUFFER_SIZE-2 )
{
I2C_AcknowledgeConfig (I2C0, DISABLE);
}
/* Generate STOP condition to close communication */
if ( Rx_Idx == BUFFER_SIZE-1 )
I2C_GenerateSTOP (I2C0, ENABLE);
/* Read Master received data */
I2C0_Buffer_Rx[Rx_Idx] = I2C_ReceiveData (I2C0);
Rx_Idx++;
/* Test on EV3-1 and clear it */
if ( I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED)) // EV3-1
I2C_SendData(I2C1, 0xFF);
}
}
/* Disable I2C0 */
I2C_Cmd (I2C0, DISABLE);
I2C_Cmd (I2C1, DISABLE);
/* Check if the transmitted data is read correctly */
TransferStatus2 = Buffercmp(I2C1_Buffer_Tx, I2C0_Buffer_Rx, BUFFER_SIZE);
/* TransferStatus = PASSED, if the transmitted from I2C1 and received data
by the I2C0 are the same */
/* TransferStatus = FAILED, if the transmitted from I2C1 and received data
by the I2C0 are different */
while(1);
}
/*******************************************************************************
Function name : void Delay(void)
Description : Delay
Input param : None
Output param : None
*******************************************************************************/
void Delay(u32 Xtime)
{
u32 j;
for(j=Xtime; j>0; j--);
}
/*******************************************************************************
* Function Name : Fill_Buffer
* Description : Fill buffer with corresponding Pattern
* Input : - Buffer: buffers to be filled.
* - Data : Data Pattern
* Output : None
* Return :
*******************************************************************************/
void Fill_Buffer(u8 *Buffer, u8 Data)
{
u8 IndexTmp;
/* Put in global buffer same values */
for( IndexTmp = 0; IndexTmp < BUFFER_SIZE; IndexTmp++ )
{
Buffer[IndexTmp] =Data++;
}
}
/*******************************************************************************
* Function Name : Buffercmp
* Description : Compares two buffers.
* Input : - pBuffer, pBuffer1: buffers to be compared.
* : - BufferLength: buffer's length
* Output : None
* Return : PASSED: pBuffer identical to pBuffer1
* FAILED: pBuffer differs from pBuffer1
*******************************************************************************/
TestStatus Buffercmp(u8* pBuffer, u8* pBuffer1, u16 BufferLength)
{
while(BufferLength--)
{
if(*pBuffer != *pBuffer1)
{
return FAILED;
}
pBuffer++;
pBuffer1++;
}
return PASSED;
}
/*******************************************************************************
* Function Name : Peripherals_Init
* Description : Initialize all used peripherals
* Input : - I2C0OwnAddr: I2C0 Own Address
* - I2C1OwnAddr: I2C1 Own Address
* Output : None
* Return :
*******************************************************************************/
void Peripherals_Init(u16 I2C0OwnAddr, u16 I2C1OwnAddr)
{
/* Peripherals enable */
SCU_APBPeriphClockConfig(__I2C0,ENABLE);
I2C_DeInit(I2C0);
SCU_APBPeriphClockConfig(__I2C1,ENABLE);
I2C_DeInit(I2C1);
SCU_APBPeriphClockConfig(__GPIO0, ENABLE);
GPIO_DeInit(GPIO0);
/* GPIO0 Config */
GPIO_DeInit(GPIO0);
GPIO_Struct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 ;
GPIO_Struct.GPIO_Type = GPIO_Type_OpenCollector;
GPIO_Struct.GPIO_IPConnected = GPIO_IPConnected_Enable;
GPIO_Struct.GPIO_Alternate=GPIO_OutputAlt2;
GPIO_Init(GPIO0, &GPIO_Struct);
/* I2C0 Configuration */
I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
I2C_Struct.I2C_Ack = I2C_Ack_Enable;
I2C_Struct.I2C_CLKSpeed = 400000;
I2C_Struct.I2C_OwnAddress = I2C0OwnAddr;
I2C_Init(I2C0, &I2C_Struct);
/* I2C1 Configuration */
/* I2C1 Configuration */
/* we keep the same config as I2C0 for the other I2C_Struct members */
/* We change just the address*/
I2C_Struct.I2C_OwnAddress = I2C1OwnAddr;
I2C_Init(I2C1, &I2C_Struct);
}
/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -