/** * @brief This function handles I2C2 Error interrupt request. * @param None * @retval None */ void I2Cx_EV_IRQHandler(void) { #if defined (I2C_MASTER) switch (I2C_GetLastEvent(I2Cx)) { /* EV5 */ case I2C_EVENT_MASTER_MODE_SELECT : #ifdef I2C_10BITS_ADDRESS /* Send Header to Slave for write */ I2C_SendData(I2Cx, HEADER_ADDRESS_Write); break; /* EV9 */ case I2C_EVENT_MASTER_MODE_ADDRESS10: #endif /* I2C_10BITS_ADDRESS */ /* Send slave Address for write */ I2C_Send7bitAddress(I2Cx, (uint8_t)SLAVE_ADDRESS, I2C_Direction_Transmitter); break; /* EV6 */ case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED: /* Send the I2C transaction code */ I2C_SendData(I2Cx, CmdTransmitted); break; /* EV8 */ case I2C_EVENT_MASTER_BYTE_TRANSMITTING: case I2C_EVENT_MASTER_BYTE_TRANSMITTED: if (Tx_Idx == GetVar_NbrOfDataToTransfer()) { /* Send STOP condition */ I2C_GenerateSTOP(I2Cx, ENABLE); I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_BUF, DISABLE); } else { /* Transmit Data TxBuffer */ I2C_SendData(I2Cx, TxBuffer[Tx_Idx++]); } break; default: break; } #endif /* I2C_MASTER*/ #if defined (I2C_SLAVE) /* Get Last I2C Event */ Event = I2C_GetLastEvent(I2Cx); switch (Event) { /* Check on EV1*/ case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: Rx_Idx = 0x00; break; /* Check on EV2*/ case I2C_EVENT_SLAVE_BYTE_RECEIVED: case (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_SR1_BTF): if (CmdReceived == 0x00) { CmdReceived = I2C_ReceiveData(I2Cx); } else { RxBuffer[Rx_Idx++] = I2C_ReceiveData(I2Cx); } break; /* Check on EV4 */ case (I2C_EVENT_SLAVE_STOP_DETECTED): I2C_GetFlagStatus(I2Cx, I2C_FLAG_STOPF); I2C_Cmd(I2Cx, ENABLE); break; default: break; } #endif /* I2C_SLAVE*/ }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f2xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f2xx.c file */ /* I2C configuration ---------------------------------------------------------*/ I2C_Config(); /* Initialize LEDs mounted on STM322xG-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); #if defined (I2C_MASTER) /* Initialize push-buttons mounted on STM322xG-EVAL board */ TimeOut = USER_TIMEOUT; while ((IOE_Config() != IOE_OK) && (TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } #endif /* I2C_MASTER */ /* SysTick configuration -----------------------------------------------------*/ SysTickConfig(); /*************************************Master Code******************************/ #if defined (I2C_MASTER) /* I2C De-initialize */ I2C_DeInit(I2Cx); /*!< I2C Struct Initialize */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DUTYCYCLE; I2C_InitStructure.I2C_OwnAddress1 = 0xA0; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED; #ifndef I2C_10BITS_ADDRESS I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; #else I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_10bit; #endif /* I2C_10BITS_ADDRESS */ /*!< I2C Initialize */ I2C_Init(I2Cx, &I2C_InitStructure); /* Enable Error Interrupt */ I2C_ITConfig(I2Cx, I2C_IT_ERR , ENABLE); /* I2C ENABLE */ I2C_Cmd(I2Cx, ENABLE); while (1) { CmdTransmitted = 0x00; NumberOfByte = 0x00; Tx_Idx = 0x00; /* Clear PressedButton by reading joystick */ PressedButton = IOE_JoyStickGetState(); /* Waiting joystick pressed */ while (PressedButton == JOY_NONE) { PressedButton = IOE_JoyStickGetState(); } /* I2C in Master Transmitter Mode ----------------------------------------*/ switch (PressedButton) { /* JOY_RIGHT button pressed */ case JOY_RIGHT: NumberOfByte = CMD_RIGHT_SIZE; CmdTransmitted = CMD_RIGHT; break; /* JOY_LEFT button pressed */ case JOY_LEFT: NumberOfByte = CMD_LEFT_SIZE; CmdTransmitted = CMD_LEFT; break; /* JOY_UP button pressed */ case JOY_UP: NumberOfByte = CMD_UP_SIZE; CmdTransmitted = CMD_UP; break; /* JOY_DOWN button pressed */ case JOY_DOWN: NumberOfByte = CMD_DOWN_SIZE; CmdTransmitted = CMD_DOWN; break; /* JOY_SEL button pressed */ case JOY_SEL: NumberOfByte = CMD_SEL_SIZE; CmdTransmitted = CMD_SEL; break; default: break; } if (CmdTransmitted != 0x00) { /* Enable Error and Buffer Interrupts */ I2C_ITConfig(I2Cx, (I2C_IT_EVT | I2C_IT_BUF), ENABLE); /* Generate the Start condition */ I2C_GenerateSTART(I2Cx, ENABLE); /* Data transfer is performed in the I2C interrupt routine */ /* Wait until end of data transfer or time out */ TimeOut = USER_TIMEOUT; while ((Tx_Idx < GetVar_NbrOfDataToTransfer())&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } TimeOut = USER_TIMEOUT; while ((I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } } } #endif /* I2C_MASTER */ /**********************************Slave Code**********************************/ #if defined (I2C_SLAVE) I2C_DeInit(I2Cx); /* Initialize I2C peripheral */ /*!< I2C Init */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DUTYCYCLE; I2C_InitStructure.I2C_OwnAddress1 = SLAVE_ADDRESS; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED; #ifndef I2C_10BITS_ADDRESS I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; #else I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_10bit; #endif /* I2C_10BITS_ADDRESS */ I2C_Init(I2Cx, &I2C_InitStructure); /* Enable Error Interrupt */ I2C_ITConfig(I2Cx, (I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE); /* I2C ENABLE */ I2C_Cmd(I2Cx, ENABLE); /* Infinite Loop */ while (1) { CmdReceived = 0x00; NumberOfByte = 0x00; /* Clear the RxBuffer */ Fill_Buffer(RxBuffer, RXBUFFERSIZE); while (CmdReceived == 0x00) {} /* Wait until end of data transfer */ while (Rx_Idx < GetVar_NbrOfDataToReceive()) {} /* I2C in Slave Receiver Mode --------------------------------------------*/ if (CmdReceived != 0x00) { switch (Rx_Idx) { /* Right button pressed */ case CMD_RIGHT_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE) == PASSED) { /* Turn ON LED2 and LED3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED4); } break; /* Left button pressed*/ case CMD_LEFT_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE) == PASSED) { /* Turn ON LED4 */ STM_EVAL_LEDOn(LED4); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); } break; /* Up button pressed */ case CMD_UP_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE) == PASSED) { /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); } break; /* Down button pressed */ case CMD_DOWN_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE) == PASSED) { /* Turn ON LED3 */ STM_EVAL_LEDOn(LED3); /* Turn all other LEDs off */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } break; /* Sel button pressed */ case CMD_SEL_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE) == PASSED) { /* Turn ON all LEDs */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } } } #endif /* I2C_SLAVE */ }