/**
******************************************************************************
* @file CYE-STM32F4-C V1.0/main.c
* @Author CYESS Howard Lin <howard.lin@cyess.com>& Green Yeh
* @version V1.0.0
* @date 2015/01/31 main
* @brief Main program body
*/
/* Private function prototypes -----------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction);
void I2C_write(I2C_TypeDef* I2Cx, uint8_t data);
void I2C_stop(I2C_TypeDef* I2Cx);
void init_I2C1();
int main(void)
{
uint16_t i;
SystemCoreClockUpdate();
if (SysTick_Config(SystemCoreClock /1000)){
while (1);
}
I2C_LowLevel_Init();
init_I2C1();
while(1){
I2C_start(I2C1, 0xA0, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode
I2C_write(I2C1, 0x00); // write one byte to the slave
I2C_stop(I2C1);
Delay(10);
I2C_write(I2C1, 0x55); // write another byte to the slave
I2C_stop(I2C1); // stop the transmission
Delay(10);
}
}
void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction){
// wait until I2C1 is not busy anymore
while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
// wait for I2C1 EV5 --> Slave has acknowledged start condition
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
// Send slave Address for write
I2C_Send7bitAddress(I2Cx, address, direction);
/* wait for I2C1 EV6, check if
* either Slave has acknowledged Master transmitter or
* Master receiver mode, depending on the transmission
* direction
*/
if(direction == I2C_Direction_Transmitter){
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
} else if(direction == I2C_Direction_Receiver){
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
}
}
void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
{
I2C_SendData(I2Cx, data);
// wait for I2C1 EV8_2 --> byte has been transmitted
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(EVAL_COM1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
{}
return ch;
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1){}
}
#endif
void init_I2C1()
{
GPIO_InitTypeDef GPIO_InitStruct;
I2C_InitTypeDef I2C_InitStruct;
// ?? I2C1 ? RCC ??
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
// ?? GPIOB ??,????? PB6 ??? SCL ???PB7 ??? SDA ??
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // ?? PB6 ? PB7
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // ????? ?? ( AF - Alternate Function )
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // ?? GPIO ??? 50 MHz
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // set output to open drain --> the line has to be only pulled low, not driven high
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // ??????
GPIO_Init(GPIOB, &GPIO_InitStruct); // ??? GPIOB
// ?? I2C1 ? AF
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); // SCL
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); // SDA
// ?? I2C1
I2C_InitStruct.I2C_ClockSpeed = 100000; // ?? I2C ????? 100kHz
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C ??
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard
I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode
I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on)
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // ?? I2C ????? 7 bit
I2C_Init(I2C1, &I2C_InitStruct); // ??? I2C1
// ?? I2C1
I2C_Cmd(I2C1, ENABLE);