/** * @brief Write the command from the chip. * @retval status communication. */ I2C_Status_TypeDef I2C_writeCommand(uint8_t devAddr, uint8_t command, uint32_t timeout) { uint32_t zero_time; //Start condition I2C_GenerateSTART(I2C_USE, ENABLE); zero_time = GetTickCount(); while (!I2C_GetFlagStatus(I2C_USE,I2C_FLAG_SB)) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } //Send device address for write I2C_Send7bitAddress(I2C_USE, devAddr, I2C_Direction_Transmitter); zero_time = GetTickCount(); while (!I2C_CheckEvent(I2C_USE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } /* Send the device's internal address to write to */ I2C_SendData(I2C_USE, command); zero_time = GetTickCount(); while ((!I2C_GetFlagStatus(I2C_USE,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(I2C_USE,I2C_FLAG_BTF))) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } /* Send STOP Condition */ I2C_GenerateSTOP(I2C_USE, ENABLE); return I2C_STATUS_SUCCESS; }
MS561101BA_Status_Convert MS561101BA_rawPressure(uint8_t OSR,uint8_t *buffer) { MS561101BA_Status_Convert status_Convert; I2C_Status_TypeDef status_Com; uint64_t time_now; time_now = GetTickCount(); if(g_last_time_pressure == 0) { status_Com = MS561101BA_startConversion(MS561101BA_D1 + OSR); if(status_Com != I2C_STATUS_SUCCESS) return MS561101BA_STATUS_CONVERT_FAIL; g_last_time_pressure = time_now; status_Convert = MS561101BA_STATUS_START_CONVERT; } else { if(CheckTick(g_last_time_pressure,CONVERSION_TIME) == TRUE) { status_Com = MS561101BA_getConversion(buffer); if(status_Com != I2C_STATUS_SUCCESS) return MS561101BA_STATUS_CONVERT_FAIL; g_last_time_pressure = 0; status_Convert = MS561101BA_STATUS_CONVERT_FINISH; } else { status_Convert = MS561101BA_STATUS_CONVERTING; } } return status_Convert; }
/******************************************************************************* Function name: PC_AppProcess Decription: This function Process all income packed or out packed of USART1 Input: None Output: None *******************************************************************************/ void PC_AppProcess(void) { if(CheckTick(g_pc_zero_time,10) == TRUE)//5ms { g_pc_zero_time = GetTickCount(); // Send_frame_data_MPU(g_ax_raw,g_ay_raw,g_az_raw,g_gx_raw,g_gy_raw,g_gz_raw); Send_frame_data_MPU_and_Kalman(g_ax_raw,g_ay_raw,g_az_raw,g_gy_raw,g_gx_raw,g_gz_raw,g_pitch,g_roll,300);//gx va gy bi nguoc } }
void MMatchClanMap::Tick(u64 nClock) { if (!CheckTick(nClock)) return; // Update Clans for(MMatchClanMap::iterator iClan=begin(); iClan!=end();) { MMatchClan* pClan = (*iClan).second; pClan->Tick(nClock); if (pClan->CheckLifePeriod() == false) { DestroyClan(pClan->GetCLID(), &iClan); continue; } else { ++iClan; } } }
int main(void) { u8 loop = 1; char string[256]; //char string1[256]; uint16_t value; uint16_t sizestring; int icase = 0; initPA15(); init_USART1(BT_BAUD); systick_init(); led_Init(); button_init(); UART2_CONFIG(9600);// DMA_CONFIG(); setPA15On(); togglePA15(); while(loop){ //Send data through the bluetooth communication if (GPIOA->IDR & 0x0001) { if (CheckTick(delay_1,500)) { delay_1 = GetTickCount(); USART_puts(USART1, "troi oi 1 2 3 4 5 6 7 8 9 10\n"); led12(); } } if (CheckTick(delay_2,1000)) { delay_2 = GetTickCount(); //led13(); } if (CheckTick(delay_3,5000)) { delay_3 = GetTickCount(); led14(); } if (CheckTick(delay_4,100)) { delay_4 = GetTickCount(); while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); //while(DMA_GetFlagStatus(DMA1_Stream2,DMA_FLAG_TCIF1) == RESET); value = USART_ReceiveData(USART1); memset(string,0,sizeof(string[0])*256); // Clear all to 0 so string properly represented sprintf(string,"%c",value); //sizestring = sizeof(string); //sprintf(string1,"%c",sizestring); if (string != NULL) { //USART_puts(USART1,string); if (string != NULL) icase = 1; else if (memchr(string, '2', sizeof(string))) icase = 2; else if (memchr(string, '3', sizeof(string))) icase = 3; else if (memchr(string, '4', sizeof(string))) icase = 4; else if (memchr(string, '5', sizeof(string))) icase = 5; //else icase = 6; switch (icase) { case 1: { USART_puts(USART1,string); icase = 0; led15(); } break; case 2: { USART_puts(USART1,"di xuong"); icase = 0; //led14(); } break; case 3: { USART_puts(USART1,"sang trai"); icase = 0; //led13(); } break; case 4: { USART_puts(USART1,"sang phai"); icase = 0; } break; case 5: { USART_puts(USART1,"can bang"); icase = 0; } break; // case 6: // { // //USART_puts(USART1,string); // icase = 0; // } // break; default: break; } //usart_send_string(string); //USART_SendData(USART1,value); } else { //usart_send_string(string); } led15(); } if (CheckTick(delay_5,250)) { delay_5 = GetTickCount(); } /* Disable the UART connection */ //USART_Cmd(USART1, DISABLE); } }
/** * @brief Read the data from the chip. * @param RegName: specifies the register to be read. * This member can be one of the following values: * @retval status communication. */ I2C_Status_TypeDef I2C_readBytes(uint8_t devAddr, uint8_t RegName,uint8_t length,uint8_t *buffer, uint32_t timeout) { uint32_t zero_time; /* Configure DMA Peripheral */ I2C_DMA_Config(I2C_DMA_RX, buffer, length); /* Enable DMA NACK automatic generation */ I2C_DMALastTransferCmd(I2C_USE, ENABLE); //Start condition I2C_GenerateSTART(I2C_USE, ENABLE); zero_time = GetTickCount(); while (!I2C_GetFlagStatus(I2C_USE,I2C_FLAG_SB)) { if(CheckTick(zero_time,timeout) == TRUE) { return I2C_STATUS_TIMEOUT; } } //Send device address for write I2C_Send7bitAddress(I2C_USE, devAddr, I2C_Direction_Transmitter); zero_time = GetTickCount(); while (!I2C_CheckEvent(I2C_USE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } /* Send the device's internal address to write to */ I2C_SendData(I2C_USE, RegName); zero_time = GetTickCount(); while ((!I2C_GetFlagStatus(I2C_USE,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(I2C_USE,I2C_FLAG_BTF))) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } /* Send START condition a second time */ I2C_GenerateSTART(I2C_USE, ENABLE); /* Test on SB Flag */ zero_time = GetTickCount(); while (!I2C_GetFlagStatus(I2C_USE,I2C_FLAG_SB)) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } /* Send address for read */ I2C_Send7bitAddress(I2C_USE, devAddr, I2C_Direction_Receiver); /* Test on ADDR Flag */ zero_time = GetTickCount(); while (!I2C_CheckEvent(I2C_USE, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } /* Enable I2C DMA request */ I2C_DMACmd(I2C_USE,ENABLE); /* Enable DMA RX Channel */ DMA_Cmd(I2C_DMA_RX_CHANNEL, ENABLE); /* Wait until DMA Transfer Complete */ zero_time = GetTickCount(); while (!DMA_GetFlagStatus(I2C_DMA_RX_TCFLAG)) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } /* Send STOP Condition */ I2C_GenerateSTOP(I2C_USE, ENABLE); /* Disable DMA RX Channel */ DMA_Cmd(I2C_DMA_RX_CHANNEL, DISABLE); /* Disable I2C DMA request */ I2C_DMACmd(I2C_USE,DISABLE); /* Clear DMA RX Transfer Complete Flag */ DMA_ClearFlag(I2C_DMA_RX_TCFLAG); /* return a Reg value */ return I2C_STATUS_SUCCESS; }
/** * @brief Write the specified register from the chip. * @param RegName: specifies the register to be read. * This member can be one of the following values: * @retval status communication. */ I2C_Status_TypeDef I2C_writeReg(uint8_t devAddr, uint8_t RegName, uint8_t RegValue, uint32_t timeout) { uint8_t BufferTX = 0; uint32_t zero_time; BufferTX = RegValue; /* Configure DMA Peripheral */ I2C_DMA_Config(I2C_DMA_TX, (uint8_t*)(&BufferTX), 1); //Start condition I2C_GenerateSTART(I2C_USE, ENABLE); zero_time = GetTickCount(); while (!I2C_GetFlagStatus(I2C_USE,I2C_FLAG_SB)) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } //Send device address for write I2C_Send7bitAddress(I2C_USE, devAddr, I2C_Direction_Transmitter); zero_time = GetTickCount(); while (!I2C_CheckEvent(I2C_USE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } /* Send the device's internal address to write to */ I2C_SendData(I2C_USE, RegName); zero_time = GetTickCount(); while ((!I2C_GetFlagStatus(I2C_USE,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(I2C_USE,I2C_FLAG_BTF))) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } /* Enable I2C DMA request */ I2C_DMACmd(I2C_USE,ENABLE); /* Enable DMA TX Channel */ DMA_Cmd(I2C_DMA_TX_CHANNEL, ENABLE); /* Wait until DMA Transfer Complete */ zero_time = GetTickCount(); while (!DMA_GetFlagStatus(I2C_DMA_TX_TCFLA)) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } /* Wait until BTF Flag is set before generating STOP */ zero_time = GetTickCount(); while ((!I2C_GetFlagStatus(I2C_USE,I2C_FLAG_BTF))) { if(CheckTick(zero_time,timeout) == TRUE) return I2C_STATUS_TIMEOUT; } /* Send STOP Condition */ I2C_GenerateSTOP(I2C_USE, ENABLE); /* Disable DMA TX Channel */ DMA_Cmd(I2C_DMA_TX_CHANNEL, DISABLE); /* Disable I2C DMA request */ I2C_DMACmd(I2C_USE,DISABLE); /* Clear DMA RX Transfer Complete Flag */ DMA_ClearFlag(I2C_DMA_TX_TCFLA); return I2C_STATUS_SUCCESS; }