初级会员
- 积分
- 184
- 金钱
- 184
- 注册时间
- 2020-9-24
- 在线时间
- 34 小时
|
1金钱
- /*传感器任务*/
- void sensorsTask(void *param)
- {
- sensorsInit(); /*传感器初始化*/
- vTaskDelay(150);
- sensorsSetupSlaveRead();/*设置传感器从模式读取*/
- while (1)
- {
- if (pdTRUE == xSemaphoreTake(sensorsDataReady, portMAX_DELAY))
- {
- /*确定数据长度*/
- u8 dataLen = (u8) (SENSORS_MPU6500_BUFF_LEN +
- (isMagPresent ? SENSORS_MAG_BUFF_LEN : 0) +
- (isBaroPresent ? SENSORS_BARO_BUFF_LEN : 0));
- i2cdevRead(I2C1_DEV, MPU6500_ADDRESS_AD0_HIGH, MPU6500_RA_ACCEL_XOUT_H, dataLen, buffer);
-
- /*处理原始数据,并放入数据队列中*/
- processAccGyroMeasurements(&(buffer[0]));
- if (isMagPresent)
- {
- processMagnetometerMeasurements(&(buffer[SENSORS_MPU6500_BUFF_LEN]));
- }
- if (isBaroPresent)
- {
- processBarometerMeasurements(&(buffer[isMagPresent ?
- SENSORS_MPU6500_BUFF_LEN + SENSORS_MAG_BUFF_LEN : SENSORS_MPU6500_BUFF_LEN]));
- }
-
- vTaskSuspendAll(); /*确保同一时刻把数据放入队列中*/
- xQueueOverwrite(accelerometerDataQueue, &sensors.acc);
- xQueueOverwrite(gyroDataQueue, &sensors.gyro);
- if (isMagPresent)
- {
- xQueueOverwrite(magnetometerDataQueue, &sensors.mag);
- }
- if (isBaroPresent)
- {
- xQueueOverwrite(barometerDataQueue, &sensors.baro);
- }
- xTaskResumeAll();
- }
- }
- }
复制代码
任务的流程:
  先初始化硬件I2C(Enable I2C_SENSORS error interrupts,IC DMA初始化),初始化传感器----->进入循环,等待传感器中断发送二值信号量------>得到信号量后读取传感器数据到buffer数组中---->处理buffer数据得到加速度,陀螺仪,磁力,气压等,然后把它们各自入到自己的队列中。
- /**
- * 事件中断服务函数
- */
- static void i2cdrvEventIsrHandler(I2cDrv* i2c);
- /**
- * 错误中断服务函数
- */
- static void i2cdrvErrorIsrHandler(I2cDrv* i2c);
- /**
- * DMA中断服务函数
- */
- static void i2cdrvDmaIsrHandler(I2cDrv* i2c);
- /**
- * 事件中断服务函数
- */
- static void i2cdrvDmaIsrHandler(I2cDrv* i2c)
- {
- if (DMA_GetFlagStatus(i2c->def->dmaRxStream, i2c->def->dmaRxTCFlag)) // Tranasfer complete
- {
- i2cdrvClearDMA(i2c);
- i2cNotifyClient(i2c);
- // Are there any other messages to transact?
- i2cTryNextMessage(i2c);
- }
- if (DMA_GetFlagStatus(i2c->def->dmaRxStream, i2c->def->dmaRxTEFlag)) // Transfer error
- {
- DMA_ClearITPendingBit(i2c->def->dmaRxStream, i2c->def->dmaRxTEFlag);
- //TODO: Best thing we could do?
- i2c->txMessage.status = i2cNack;
- i2cNotifyClient(i2c);
- i2cTryNextMessage(i2c);
- }
- }
- /**
- * 事件中断服务函数
- */
- static void i2cdrvEventIsrHandler(I2cDrv* i2c)
- {
- uint16_t SR1;
- uint16_t SR2;
- // read the status register first
- SR1 = i2c->def->i2cPort->SR1;
- // Start bit event
- if (SR1 & I2C_SR1_SB)
- {
- i2c->messageIndex = 0;
- if(i2c->txMessage.direction == i2cWrite ||
- i2c->txMessage.internalAddress != I2C_NO_INTERNAL_ADDRESS)
- {
- I2C_Send7bitAddress(i2c->def->i2cPort, i2c->txMessage.slaveAddress << 1, I2C_Direction_Transmitter);
- }
- else
- {
- I2C_AcknowledgeConfig(i2c->def->i2cPort, ENABLE);
- I2C_Send7bitAddress(i2c->def->i2cPort, i2c->txMessage.slaveAddress << 1, I2C_Direction_Receiver);
- }
- }
- // Address event
- else if (SR1 & I2C_SR1_ADDR)
- {
- if(i2c->txMessage.direction == i2cWrite ||
- i2c->txMessage.internalAddress != I2C_NO_INTERNAL_ADDRESS)
- {
- SR2 = i2c->def->i2cPort->SR2; // clear ADDR
- // In write mode transmit is always empty so can send up to two bytes
- if (i2c->txMessage.internalAddress != I2C_NO_INTERNAL_ADDRESS)
- {
- if (i2c->txMessage.isInternal16bit)
- {
- I2C_SendData(i2c->def->i2cPort, (i2c->txMessage.internalAddress & 0xFF00) >> 8);
- I2C_SendData(i2c->def->i2cPort, (i2c->txMessage.internalAddress & 0x00FF));
- }
- else
- {
- I2C_SendData(i2c->def->i2cPort, (i2c->txMessage.internalAddress & 0x00FF));
- }
- i2c->txMessage.internalAddress = I2C_NO_INTERNAL_ADDRESS;
- }
- I2C_ITConfig(i2c->def->i2cPort, I2C_IT_BUF, ENABLE); // allow us to have an EV7
- }
- else // Reading, start DMA transfer
- {
- if(i2c->txMessage.messageLength == 1)
- {
- I2C_AcknowledgeConfig(i2c->def->i2cPort, DISABLE);
- }
- else
- {
- I2C_DMALastTransferCmd(i2c->def->i2cPort, ENABLE); // No repetitive start
- }
- // Disable buffer I2C interrupts
- I2C_ITConfig(i2c->def->i2cPort, I2C_IT_EVT | I2C_IT_BUF, DISABLE);
- // Enable the Transfer Complete interrupt
- DMA_ITConfig(i2c->def->dmaRxStream, DMA_IT_TC | DMA_IT_TE, ENABLE);
- I2C_DMACmd(i2c->def->i2cPort, ENABLE); // Enable before ADDR clear
- __DMB(); // Make sure instructions (clear address) are in correct order
- SR2 = i2c->def->i2cPort->SR2; // clear ADDR
- }
- }
- // Byte transfer finished
- else if (SR1 & I2C_SR1_BTF)
- {
- SR2 = i2c->def->i2cPort->SR2;
- if (SR2 & I2C_SR2_TRA) // In write mode?
- {
- if (i2c->txMessage.direction == i2cRead) // internal address read
- {
- /* Internal address written, switch to read */
- i2c->def->i2cPort->CR1 = (I2C_CR1_START | I2C_CR1_PE); // Generate start
- }
- else
- {
- i2cNotifyClient(i2c);
- // Are there any other messages to transact? If so stop else repeated start.
- i2cTryNextMessage(i2c);
- }
- }
- else // Reading. Shouldn't happen since we use DMA for reading.
- {
- ASSERT(1);
- i2c->txMessage.buffer[i2c->messageIndex++] = I2C_ReceiveData(i2c->def->i2cPort);
- if(i2c->messageIndex == i2c->txMessage.messageLength)
- {
- i2cNotifyClient(i2c);
- // Are there any other messages to transact?
- i2cTryNextMessage(i2c);
- }
- }
- // A second BTF interrupt might occur if we don't wait for it to clear.
- // TODO Implement better method.
- while (i2c->def->i2cPort->CR1 & 0x0100) { ; }
- }
- // Byte received
- else if (SR1 & I2C_SR1_RXNE) // Should not happen when we use DMA for reception.
- {
- i2c->txMessage.buffer[i2c->messageIndex++] = I2C_ReceiveData(i2c->def->i2cPort);
- if(i2c->messageIndex == i2c->txMessage.messageLength)
- {
- I2C_ITConfig(i2c->def->i2cPort, I2C_IT_BUF, DISABLE); // disable RXE to get BTF
- }
- }
- // Byte ready to be transmitted
- else if (SR1 & I2C_SR1_TXE)
- {
- if (i2c->txMessage.direction == i2cRead)
- {
- // Disable TXE to flush and get BTF to switch to read.
- // Switch must be done in BTF or strange things happen.
- I2C_ITConfig(i2c->def->i2cPort, I2C_IT_BUF, DISABLE);
- }
- else
- {
- I2C_SendData(i2c->def->i2cPort, i2c->txMessage.buffer[i2c->messageIndex++]);
- if(i2c->messageIndex == i2c->txMessage.messageLength)
- {
- // Disable TXE to allow the buffer to flush and get BTF
- I2C_ITConfig(i2c->def->i2cPort, I2C_IT_BUF, DISABLE);
- }
- }
- }
- }
复制代码 上面这段就看不懂了????
|
|