Init: I2C_DeInit(I2C3); // Set I2C3-Unit back to default-values // Enable I2C-Interrupts I2C_ITConfig(I2C3, I2C_IT_EVT, ENABLE); // Enable I2C Event-Interrupt on I2C3 I2C_ITConfig(I2C3, I2C_IT_BUF, ENABLE); // Enable I2C Buffer-unterrupt on I2C3 I2C_ITConfig(I2C3, I2C_IT_ERR, ENABLE); // Enable I2C Error-Interrupt on I2C3 NVIC_InitTypeDef NVIC_InitStructure; // Event-Interrupt NVIC_InitStructure.NVIC_IRQChannel = I2C3_EV_IRQn; // Select Channel (I2C3 Event) NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // Set Priority (lower value = higher priority) NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // Set Subpriority, same as above NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable this Interrupt NVIC_Init(&NVIC_InitStructure); // Load settings into registers // Error-Interrupt NVIC_InitStructure.NVIC_IRQChannel = I2C3_ER_IRQn; // Select Channel (I2C3 Error) NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // Set Priority (lower value = higher priority) NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // Set Subpriority, same as above NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable this Interrupt NVIC_Init(&NVIC_InitStructure); // Load settings into registers // Configure the I2C3-Unit I2C_InitTypeDef I2C_initStructure; I2C_initStructure.I2C_Mode = I2C_Mode_I2C; // Interface = I2C I2C_initStructure.I2C_DutyCycle = I2C_DutyCycle_2; // I2C-duty-cycle = 2 I2C_initStructure.I2C_OwnAddress1 = I2C_address_own*2; // Own I2C-address I2C_initStructure.I2C_Ack = I2C_Ack_Enable; // Enable ACK-signal (acknowledge) I2C_initStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;// 7-Bit address-width I2C_initStructure.I2C_ClockSpeed = I2C_clock; // Clock-speed I2C_Init(I2C3, &I2C_initStructure); // Load settings into I2C-Registers I2C_StretchClockCmd(I2C3, ENABLE); // Enable Clock-Stretch I2C_Cmd(I2C3, ENABLE); // Enable I2C3-Unit Interrupthandler: void I2C3_EV_IRQHandler(void) { static uint8_t rx_buffer[4]; // Buffer for storing received data static uint8_t tx_buffer2[4]; // Buffer for storing data to transmit static uint8_t tx_counter = 0; // Transmitting-counter static uint8_t rx_counter = 0; // Receiving-Counter switch(I2C_GetLastEvent(I2C3)) { // MAster has send slave-address for sending data case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED: { while((I2C3->SR1 & I2C_SR1_ADDR) == I2C_SR1_ADDR) // Clear ADDR-Flag { I2C3->SR1; I2C3->SR2; } rx_counter = 0; // Reset RX-Counter break; } // Master has send slave-address for requesting data case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED: { while((I2C3->SR1 & I2C_SR1_ADDR) == I2C_SR1_ADDR) // Clear ADDR-Flag { I2C3->SR1; I2C3->SR2; } I2C_Eval_Request(); // Evaluate the received command tx_buffer2[0] = (uint8_t) tx_buffer; // Store lower byte of data-answer in array tx_buffer2[1] = (uint8_t) (tx_buffer >> 8); // Store higher byte of data-answer in array tx_counter = 0; // Reset TX-Counter break; } // Master has send data-byte case I2C_EVENT_SLAVE_BYTE_RECEIVED: { if (rx_counter == 0) { I2C_Command = I2C_ReceiveData(I2C3); // First received byte = command } else { rx_buffer[rx_counter-1] = I2C_ReceiveData(I2C3); // Store other bytes in receiving-buffer } rx_counter++; // Increment rx-counter break; } // Master requests data-byte case I2C_EVENT_SLAVE_BYTE_TRANSMITTING: case I2C_EVENT_SLAVE_BYTE_TRANSMITTED: I2C_SendData(I2C3, tx_buffer2[tx_counter]); // Send data from tx-buffer on position of tx-counter tx_counter++; // Increment tx-counter break; case I2C_EVENT_SLAVE_ACK_FAILURE: I2C3 ->SR1 &= 0x00FF; // Clear AF-Flag break; case I2C_EVENT_SLAVE_STOP_DETECTED: { while((I2C3->SR1 & I2C_SR1_STOPF) == I2C_SR1_STOPF) // Clear STOP-Flag { I2C3->SR1; I2C3->CR1 |= 0x01; } I2C_Eval_Receive(rx_buffer[0] | (rx_buffer[1]<<8)); // Evaluate the received data break; } } // End of switch }