void ADT7310_SetOneShotMode() { // single-shot measurement mode: write to 8-bit configuration // register (0x01): send 0x08 0x20 (one shot mode) ADT7310_CS_OUT &= ~ADT7310_CS_PIN; // low active --> activate SPI_Write(0x08); while (SPI_IsBusy()) {} SPI_Write(0x20); while (SPI_IsBusy()) {} ADT7310_CS_OUT |= ADT7310_CS_PIN; // low active --> deactivate }
uint16_t ADT7310_QueryValue() { uint16_t SensorValue; // send read command 0x50 and two dummy bytes ADT7310_CS_OUT &= ~ADT7310_CS_PIN; // low active --> activate SPI_Write(0x50); while (SPI_IsBusy()) {} SPI_Write(0xFF); while (SPI_IsBusy()) {} SensorValue = SPI_Read() << 8; // read MSB SPI_Write(0xFF); while (SPI_IsBusy()) {} ADT7310_CS_OUT |= ADT7310_CS_PIN; // low active --> deactivate SensorValue |= SPI_Read(); // read LSB return SensorValue; }
/*---------------------------------------------------------------------------------------------------------*/ static uint32_t SingleWrite(uint32_t u32Data) { uint32_t SPIdata=u32Data; SPI_SingleWrite(SPI1,&SPIdata); while (SPI_IsBusy(SPI1)); SPI_DumpRxRegister(SPI1,&SPIdata,1); return SPIdata; }
u32 SPI_Transfer(SPI_TypeDef *spiPort, u8 bitlen, u32 data) { SPI_SetBitLength(spiPort, bitlen); SPI_StartTransfer(spiPort, data); while(SPI_IsBusy(spiPort)); //for read operaion SPI_ClrRxFIFO(spiPort); if(bitlen == 8) return (SPI_ReadRxFIFO(spiPort) & 0xFF); else if(bitlen == 16) return (SPI_ReadRxFIFO(spiPort) & 0xFFFF); else if(bitlen == 32) return (SPI_ReadRxFIFO(spiPort)); return 0; }
/*---------------------------------------------------------------------------* * Routine: GainSpan_SPI_Update *---------------------------------------------------------------------------* * Description: * Update the state of the GainSpan SPI driver. It first determines * if any data has just been received and if so, processes it. * If SPI is not busy (and a transfer is no longer active), the * outgoing FIFO is checked to see if bytes can be sent out over * SPI. If so, those bytes are scheduled to send out. * If no bytes are to be sent, but the module has data to send us, * we'll put an IDLE character in the FIFO and request for more * to come in. * Inputs: * void * Outputs: * void *---------------------------------------------------------------------------*/ void GainSpan_SPI_Update(uint8_t channel) { uint16_t numBytes; /* Process any incoming bytes that were just sent */ if (G_GainSpan_SPI_IsTransferComplete) { G_GainSpan_SPI_IsTransferComplete = false; GainSpan_SPI_ProcessIncoming(); G_GainSpan_SPI_IsTransferActive = false; } else { /* Is the SPI bus busy? We cannot start a transfer until it is free. */ if ((!SPI_IsBusy(channel)) && (!G_GainSpan_SPI_IsTransferActive)) { /* The SPI bus is now free to start another transfer */ /* Try to send more data */ /* Are we allowed to send data? (XON/XOFF) */ if (G_GainSpan_SPI_CanTransmit) { /* Is there more data to send? */ if (G_GainSpan_SPI_TXIn != G_GainSpan_SPI_TXOut) { /* There is data to send, how many contiguous bytes can we send */ #if 0 if (G_GainSpan_SPI_TXIn > G_GainSpan_SPI_TXOut) { numBytes = G_GainSpan_SPI_TXIn - G_GainSpan_SPI_TXOut; } else { numBytes = GAINSPAN_SPI_TX_BUFFER_SIZE - G_GainSpan_SPI_TXOut; } #endif // NOTE: the module wants only 1 byte per chip select cycle numBytes = 1; /* Remember how many bytes were sent in this transfer so */ /* the returned bytes can be processed later */ G_GainSpan_SPI_NumSent = numBytes; G_GainSpan_SPI_IsTransferActive = true; /* Tell the SPI to send out this group of characters */ SPI_Transfer(GAINSPAN_SPI_CHANNEL, numBytes, G_GainSpan_SPI_TXBuffer + G_GainSpan_SPI_TXOut, G_GainSpan_SPI_TXBuffer + G_GainSpan_SPI_TXOut, IGainSpan_SPI_TransferComplete); //MSTimerDelay(1); } else { /* Nothing is being sent currently. */ /* Is the GainSpan module ready with data to return? If so, send an IDLE character */ /* to start feeding out the data */ if (GainSpan_SPI_IsDataReady(GAINSPAN_SPI_CHANNEL)) { GainSpan_SPI_SendByteLowLevel(GAINSPAN_SPI_CHAR_IDLE); } } } else { /* XON is active. We can send IDLE characters when data is ready */ if (GainSpan_SPI_IsDataReady(GAINSPAN_SPI_CHANNEL)) { // Send an IDLE character to look for the XON/XOFF */ G_GainSpan_SPI_XONIdleChar = GAINSPAN_SPI_CHAR_IDLE; // No bytes from the buffer are being sent G_GainSpan_SPI_NumSent = 0; G_GainSpan_SPI_IsTransferActive = true; SPI_Transfer(GAINSPAN_SPI_CHANNEL, 1, &G_GainSpan_SPI_XONIdleChar, &G_GainSpan_SPI_XONIdleChar, IGainSpan_SPI_TransferComplete); } } } } }