重映射就是将引脚功能重新定义到其他引脚,例如PA9是USART1-TX默认的管脚,但是现在PA9用做它用了,那可以将USART1-TX重新映射到PB6,当然这种映射不是随意的想映射到哪个脚就哪个脚,芯片内部已经固定了只能映射到固定的地方。
#ifndef _I2C_H_
#define _I2C_H_
#include "STM32DEF.h"
#define I2C1_IsValid 1
#if I2C1_IsValid
#define I2C_Speed 400000
#define I2C1_GPIOxRemapEN 1//非0使能映射
extern void I2C1_AllInit(void);
extern void I2C1_WaitDevieceStandbyState(uint8_t Addr);
#endif
#endif
#include "I2C1.h"
#if I2C1_IsValid
#include "stdio.h"
static void I2C1_GPIOxConfig(void);
static void I2C1_HardwareConfig(void);
void I2C1_AllInit(void) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
#if I2C1_GPIOxRemapEN
GPIO_PinRemapConfig(GPIO_Remap_I2C1,ENABLE);
#endif
I2C1_GPIOxConfig();
I2C1_HardwareConfig();
}
void I2C_Main(void) { } /* Private functions ---------------------------------------------------------*/
/******************************************************************************* *
Function Name : GPIO_Configuration * Description : Configure the used I/O ports pin * Input : None * Output : None * Return : None
*******************************************************************************/
static void I2C1_GPIOxConfig(void) {
GPIO_InitTypeDef GPIO_InitStructure;
#if I2C1_GPIOxRemapEN == 0
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
/* Configure I2C1 pins: SCL and SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
#else
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
#endif }
/******************************************************************************* *
Function Name : I2C_HardwareConfig * Description : I2C I2C_HardwareConfig * Input : None * Output : None * Return : None *******************************************************************************/
static void I2C1_HardwareConfig(void) {
I2C_InitTypeDef I2C_InitStructure; /* I2C configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = I2C_Speed; /* Apply I2C configuration after enabling it */
I2C_Init(I2C1, &I2C_InitStructure); /* I2C Peripheral Enable */
I2C_Cmd(I2C1, ENABLE); //I2C_AcknowledgeConfig(I2C1, ENABLE);
printf("\n\r I2C_Configuration----\n\r"); }
/******************************************************************************* *
Function Name : I2C_EE_WaitEepromStandbyState * Description : Wait for EEPROM Standby state * Input : None * Output : None * Return : None
*******************************************************************************/
void I2C1_WaitDevieceStandbyState(uint8_t Addr) {
vu16 SR1_Tmp = 0; do { /* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE); /* Read I2C1 SR1 register */
SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1); /* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, Addr, I2C_Direction_Transmitter);
}while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002)); /* Clear AF flag */
I2C_ClearFlag(I2C1, I2C_FLAG_AF); /* STOP condition */
I2C_GenerateSTOP(I2C1, ENABLE); // Added by Najoua 27/08/2008 }
#endif