uint8_t *i2c_data; TaskHandle_t pendingTask; void I2C1_EV_IRQHandler() { if (LL_I2C_IsActiveFlag_TXIS(I2C1)) LL_I2C_TransmitData8(I2C1, (*i2c_data++)); if (LL_I2C_IsActiveFlag_RXNE(I2C1)) *i2c_data++ = LL_I2C_ReceiveData8(I2C1); if (LL_I2C_IsActiveFlag_STOP(I2C1)) { xTaskNotifyFromISR(pendingTask, 0, 0, NULL); LL_I2C_ClearFlag_STOP(I2C1); } } void I2C_TX_RTOS(uint8_t addr, uint8_t *data, uint8_t len) { i2c_data = data; pendingTask = xTaskGetCurrentTaskHandle(); LL_I2C_HandleTransfer(I2C1, addr, LL_I2C_ADDRSLAVE_7BIT, len, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE); xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); } void I2C_RX_RTOS(uint8_t addr, uint8_t *data, uint8_t len) { i2c_data = data; pendingTask = xTaskGetCurrentTaskHandle(); LL_I2C_HandleTransfer(I2C1, addr, LL_I2C_ADDRSLAVE_7BIT, len, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ); xTaskNotifyWait(0, 0, NULL, portMAX_DELAY); } void I2C_Test_Task() { LL_I2C_EnableIT_TX(I2C1); LL_I2C_EnableIT_RX(I2C1); LL_I2C_EnableIT_STOP(I2C1); const uint8_t addr = 0x39 << 1; uint8_t data[2] = {0x42, 0b00010010}; I2C_TX_RTOS(addr, data, sizeof(data)); while (1) { vTaskDelay(100); uint8_t data2[1] = {0x56}; I2C_TX_RTOS(addr, data2, sizeof(data2)); uint8_t data_read[2]; I2C_RX_RTOS(addr, data_read, sizeof(data_read)); printf("CLEAR = %u\n", data_read[0] + (data_read[1] << 8)); } } void StartDefaultTask(void *argument) { /* USER CODE BEGIN 5 */ I2C_Test_Task(); /* USER CODE END 5 */ }
本作品使用基于以下许可授权:Creative Commons Attribution-ShareAlike 4.0 International License.