unsigned char* read(void) { askAboutSending(); unsigned char *readBuffer = NULL; int statusRxFifoFilling = 0; int howManyRead = 0; statusRxFifoFilling = USIC_GetRxFIFOFillingLevel(UART001_0_USIC_CH); if(((UART001_GetFlagStatus(&UART001_Handle0,UART001_FIFO_STD_RECV_BUF_FLAG)) == UART001_SET)) { statusRxFifoFilling = USIC_GetRxFIFOFillingLevel(UART001_0_USIC_CH); if(statusRxFifoFilling != 0) { readBuffer = (unsigned char *)malloc((sizeof(unsigned char)*statusRxFifoFilling) + 1); howManyRead = UART001_ReadDataBytes(&UART001_Handle0, readBuffer, statusRxFifoFilling); readBuffer[statusRxFifoFilling] = '\0'; } UART001_ClearFlag(&UART001_Handle0,UART001_FIFO_STD_RECV_BUF_FLAG); while(!USIC_ubIsRxFIFOempty(UART001_0_USIC_CH)) { USIC_FlushRxFIFO(UART001_0_USIC_CH); } } stopAskingAboutSending(); return readBuffer; }
void read(void) { emptyReadData(); if(((UART001_GetFlagStatus(&UART001_Handle0,UART001_FIFO_STD_RECV_BUF_FLAG)) == UART001_SET)) { IO004_TogglePin(IO004_Handle1); statusRxFifoFilling = USIC_GetRxFIFOFillingLevel(UART001_0_USIC_CH); howManyRead = UART001_ReadDataBytes(&UART001_Handle0, readData, statusRxFifoFilling); if(strcmp(readData, "LSM9DS1") == 0) { chosenSensor = 1; } else if(strcmp(readData, "TEMP_SENSOR") == 0) { chosenSensor = 2; } UART001_ClearFlag(&UART001_Handle0,UART001_FIFO_STD_RECV_BUF_FLAG); while(!USIC_ubIsRxFIFOempty(UART001_0_USIC_CH)) { USIC_FlushRxFIFO(UART001_0_USIC_CH); } copyData(); stopAskingAboutSending(); } }
void DaisyChain(void) { uint8_t i=0; status_t status=0; uint32_t data=0; static enum MotorState LastmotorState = Stopped; if ( motorState == StartUp ) { CharCNT = 0; USIC_FlushRxFIFO(UART001_Handle0.UartRegs); } if ( (LastmotorState == StartUp) && (motorState != Running) )//(motorState == Running)StartUp { CCU40_CC42->TCCLR |= 0x02; //定时器清零 //Start slicesCCU4定时器运行 CCU40_CC42->TCSET |= 0x01UL; CharCNT++; if ( CharCNT >= 13 ) //能收到连续的13字节完整包 { CharCNT = 0; LastmotorState = motorState; CCU40_CC42->TCCLR |= 3UL; //定时器停止运行 USIC_FlushRxFIFO(UART001_Handle0.UartRegs);///added } else { return; } } else LastmotorState = motorState; // if (DaisyTimeOut) // StopMotor(); if(USIC_GetRxFIFOFillingLevel(UART001_Handle0.UartRegs) >= DAISY_BUFFER_SIZE) { CCU40_CC42->TCCLR |= 3UL; //定时器停止运行 //Read data from UART buffer UART001_ReadDataBytes(&UART001_Handle0,FifoRecBuffer,DAISY_BUFFER_SIZE); //Assumption that communication is lost --> emtpy Receive Buffer if (FifoRecBuffer[DAISY_BUFFER_SIZE-1] != DAISY_STOP_BYTE) { //IO004_TogglePin(IO004_Handle1); USIC_FlushRxFIFO(UART001_Handle0.UartRegs); return; } uint8_t cmd = FifoRecBuffer[0]; uint16_t params = (FifoRecBuffer[1] << 8 | FifoRecBuffer[2]); switch (cmd) { case START_MOTOR: StartMotor(); break; case STOP_MOTOR: StopMotor(); break; case SET_REF_CURRENT: SetReferenceCurrent(params); break; } for(i=DAISY_MESSAGE_LENGTH; i<DAISY_BUFFER_SIZE-1; i++) FifoTransBuffer[i-DAISY_MESSAGE_LENGTH]=FifoRecBuffer[i]; //Status-Code FifoTransBuffer[i-DAISY_MESSAGE_LENGTH]=status; i++; //Data FifoTransBuffer[i-DAISY_MESSAGE_LENGTH]=(uint8_t)(data >> 8); i++; FifoTransBuffer[i-DAISY_MESSAGE_LENGTH]=(uint8_t)data; i++; FifoTransBuffer[i-DAISY_MESSAGE_LENGTH]=DAISY_STOP_BYTE; DaisyTimeOut = 0; DaisyCount++; UART001_WriteDataBytes(&UART001_Handle0, FifoTransBuffer, DAISY_BUFFER_SIZE); }
uint8_t I2CreadByte(uint8_t address, uint8_t subAddress) { uint32_t stageOfReading = 0; //deviceAddress address = *((deviceAddress*)T); I2C001_DataType data1; data1.Data1.TDF_Type = I2C_TDF_MStart; data1.Data1.Data = ((address<<1) | I2C_WRITE); /*while(!*/I2C001_WriteData(&I2C001_Handle0,&data1);/*);*/ delay(DELAY*10); I2C001_DataType data2; data2.Data1.TDF_Type = I2C_TDF_MTxData; data2.Data1.Data = subAddress; /*while(!*/I2C001_WriteData(&I2C001_Handle0,&data2);/*);*/ delay(DELAY*10); //delay(15000); I2C001_DataType data3; data3.Data1.TDF_Type = I2C_TDF_MRStart; data3.Data1.Data = ((address<<1) | I2C_READ); /*while(!*/I2C001_WriteData(&I2C001_Handle0,&data3);/*);*/ delay(DELAY*10); I2C001_DataType data4; data4.Data1.TDF_Type = I2C_TDF_MRxAck1; data4.Data1.Data = ubyteFF; /*while(!*/I2C001_WriteData(&I2C001_Handle0,&data4);/*);*/ /*while (!(I2C001_SET == I2C001_GetFlagStatus(&I2C001_Handle0, I2C001_FLAG_RIF))) { int i = 0; if(I2C001_GetFlagStatus(&I2C001_Handle0, I2C001_FLAG_NACK_RECEIVED)) { break; } };*/ delay(DELAY); I2C001_DataType data5; data5.Data1.TDF_Type = I2C_TDF_MStop; data5.Data1.Data = ubyteFF; /*while(!*/I2C001_WriteData(&I2C001_Handle0,&data5);/*);*/ stageOfReading++; int k = 0; while(0 == received){k++; if(k > 4000) break; } received = 0; delay(DELAY*10); int j = USIC_GetRxFIFOFillingLevel(I2C001_Handle0.I2CRegs); //uint16_t DataReceive1 = 0; //(USIC_ubIsRxFIFOempty(I2C001_Handle0.I2CRegs)){}; //delay(15000); /*while(I2C001_GetFlagStatus(&I2C001_Handle0, I2C001_FLAG_SRBI) == I2C001_RESET) { int i = 0; I2C001_ClearFlag(&I2C001_Handle0, I2C001_FLAG_RIF); I2C001_ClearFlag(&I2C001_Handle0, I2C001_FLAG_RSIF); } while(USIC_ubIsRxFIFOempty(I2C001_Handle0.I2CRegs)); I2C001_ReadData(&I2C001_Handle0,&DataReceive1); int j = USIC_GetRxFIFOFillingLevel(I2C001_Handle0.I2CRegs);*/ //I2C001_Handle0.I2CRegs->TRBSCR; /*while(I2C001_SET == I2C001_GetFlagStatus(&I2C001_Handle0, I2C001_FLAG_RIF)) { I2C001_ClearFlag(&I2C001_Handle0, I2C001_FLAG_RIF); } while(I2C001_SET == I2C001_GetFlagStatus(&I2C001_Handle0, I2C001_FLAG_RSIF)) { I2C001_ClearFlag(&I2C001_Handle0, I2C001_FLAG_RSIF); } while(I2C001_SET == I2C001_GetFlagStatus(&I2C001_Handle0, I2C001_FLAG_DLIF)) { I2C001_ClearFlag(&I2C001_Handle0, I2C001_FLAG_DLIF); } while(I2C001_SET == I2C001_GetFlagStatus(&I2C001_Handle0, I2C001_FLAG_RBERI)) { I2C001_ClearFlag(&I2C001_Handle0, I2C001_FLAG_RBERI); }*/ //I2C001_ClearFlag(&I2C001_Handle0, I2C001_FLAG_RSIF); /*uint16_t DataReceive1 = 0; while (!I2C001_GetFlagStatus(&I2C001_Handle0, I2C001_FLAG_RIF)); if(I2C001_ReadData(&I2C001_Handle0,&DataReceive1)) { I2C001_ClearFlag(&I2C001_Handle0, I2C001_FLAG_RIF); I2C001_ClearFlag(&I2C001_Handle0, I2C001_FLAG_RSIF); stageOfReading++; delay(DELAY); } else { stageOfReading--; }*/ return (uint8_t)DataReceive1; }
void readHumidTimerHandler(void *T) { I2C001_DataType data1; data1.Data1.TDF_Type = I2C_TDF_MRStart; data1.Data1.Data = ((HTDU21D_ADDRESS << 1) | I2C_READ); I2C001_WriteData(&I2C001_Handle1,&data1); delay11(DELAY); if(I2C001_GetFlagStatus(&I2C001_Handle1,I2C001_FLAG_NACK_RECEIVED) == I2C001_SET) { I2C001_ClearFlag(&I2C001_Handle1,I2C001_FLAG_NACK_RECEIVED); errorCounter++; } else { uint8_t msb = 0x00; uint8_t lsb = 0x00; uint8_t checksum = 0x00; USIC_CH_TypeDef* I2CRegs = I2C001_Handle1.I2CRegs; I2C001_DataType data2; data2.Data1.TDF_Type = I2C_TDF_MRxAck0; data2.Data1.Data = ubyteFF; I2C001_WriteData(&I2C001_Handle1,&data2); delay11(DELAY); if(!USIC_ubIsRxFIFOempty(I2CRegs)) { msb = (uint8_t)I2CRegs->OUTR; //Result = (bool)TRUE; } I2C001_DataType data3; data3.Data1.TDF_Type = I2C_TDF_MRxAck0; data3.Data1.Data = ubyteFF; I2C001_WriteData(&I2C001_Handle1,&data3); delay11(DELAY); if(!USIC_ubIsRxFIFOempty(I2CRegs)) { lsb = (uint8_t)I2CRegs->OUTR; //Result = (bool)TRUE; } I2C001_DataType data4; data4.Data1.TDF_Type = I2C_TDF_MRxAck1; data4.Data1.Data = ubyteFF; I2C001_WriteData(&I2C001_Handle1,&data4); delay11(DELAY); I2C001_DataType data5; data5.Data1.TDF_Type = I2C_TDF_MStop; data5.Data1.Data = ubyteFF; I2C001_WriteData(&I2C001_Handle1,&data5); delay11(DELAY); int d = USIC_GetRxFIFOFillingLevel(I2CRegs); // Read receive buffer, put the data in DataReceive1 if(!USIC_ubIsRxFIFOempty(I2CRegs)) { checksum = (uint8_t)I2CRegs->OUTR; //Result = (bool)TRUE; } unsigned int rawHumid = ((unsigned int) msb << 8) | (unsigned int) lsb; if(check_crc(rawHumid, checksum) != 0) { wrong_checksum = 1; } rawHumid &= 0xFFFC; //Zero out the status bits but keep them in place //Given the raw temperature data, calculate the actual temperature float tempRH = rawHumid / (float)65536; //2^16 = 65536 rh = -6 + (125 * tempRH); //From page 14 readHumid = 1; } }