/** * @brief Reads the STATUS register in the nRF24L01 * @param Device: The device to use * @retval The status register */ uint8_t NRF24L01_GetStatus(NRF24L01_Device* Device) { SELECT_DEVICE(Device); uint8_t status = SPI_WriteRead(Device->SPIDevice, NOP); DESELECT_DEVICE(Device); return status; }
/** * @brief * @param Device: The device to use * @retval None */ void NRF24L01_ReadRegister(NRF24L01_Device* Device, uint8_t Register, uint8_t* Buffer, uint8_t BufferSize) { SELECT_DEVICE(Device); uint8_t status = SPI_WriteRead(Device->SPIDevice, R_REGISTER | Register); /* R_REGISTER command only have 5 data bytes, datasheet page 51 */ if (BufferSize > 5) BufferSize = 5; /* LSByte first, datasheet page 50 */ for (int32_t i = BufferSize-1; i >= 0; i--) { Buffer[i] = SPI_WriteRead(Device->SPIDevice, NOP); } DESELECT_DEVICE(Device); }
/** * @brief * @param Device: The device to use * @retval None */ void NRF24L01_WriteRegister(NRF24L01_Device* Device, uint8_t Register, uint8_t* Buffer, uint8_t BufferSize) { DISABLE_DEVICE(Device); /* W_REGISTER is executable in power down or standby modes only */ SELECT_DEVICE(Device); uint8_t status = SPI_WriteRead(Device->SPIDevice, W_REGISTER | Register); /* W_REGISTER command only have 5 data bytes, datasheet page 51 */ if (BufferSize > 5) BufferSize = 5; /* LSByte first, datasheet page 50 */ for (int32_t i = BufferSize-1; i >= 0; i--) { /* Have to do WriteRead for some reason, otherwise one byte will be lost */ status = SPI_WriteRead(Device->SPIDevice, Buffer[i]); } DESELECT_DEVICE(Device); ENABLE_DEVICE(Device); }
bool HMC5883L::readRawAxis() { SELECT_DEVICE(*fd, HMC5883L_ADDRESS, deviceName); // readBlockData(*fd, HMC5883L_DATA_REGISTER_BEGIN, 6, u8Buffer); readReg8( *fd, 0x03, &iBuffer[ 0 ]); readReg8( *fd, 0x04, &iBuffer[ 1 ]); readReg8( *fd, 0x05, &iBuffer[ 2 ]); readReg8( *fd, 0x06, &iBuffer[ 3 ]); readReg8( *fd, 0x07, &iBuffer[ 4 ]); readReg8( *fd, 0x08, &iBuffer[ 5 ]); raw.XAxis = (int)(int16_t) ((iBuffer[0] << 8) | iBuffer[1]); raw.ZAxis = (int)(int16_t) ((iBuffer[2] << 8) | iBuffer[3]); raw.YAxis = (int)(int16_t) ((iBuffer[4] << 8) | iBuffer[5]); return true; }
/** * @brief Get data from the RX buffer * @param Device: The device to use * @param Storage: Pointer to where the data should be stored * @retval The amount of data received */ uint8_t NRF24L01_GetDataFromRxBuffer(NRF24L01_Device* Device, uint8_t* Buffer) { SELECT_DEVICE(Device); SPI_WriteRead(Device->SPIDevice, R_RX_PAYLOAD); uint8_t dataCount = SPI_WriteRead(Device->SPIDevice, NOP); /* * Only get the maximum amount of data which can be stored in one payload (MAX_DATA_COUNT) * When dataCount was used as a limit we got a hardfault because there is no control if * dataCount is > MAX_DATA_COUNT and the data where Buffer exist might become corrupt */ for (uint32_t i = 0; i < MAX_DATA_COUNT; i++) { Buffer[i] = SPI_WriteRead(Device->SPIDevice, NOP); } DESELECT_DEVICE(Device); /* * Flush just in case */ NRF24L01_FlushRxBuffer(Device); return dataCount; }
/** * @brief Write data and send it to the address specified in TX_ADDR * @param Device: The device to use * @param Data: Pointer to where the data is stored * @param ByteCount: The number of bytes in Data * @retval None */ ErrorStatus NRF24L01_WritePayload(NRF24L01_Device* Device, uint8_t* Data, uint8_t DataCount) { /* You can only send the amount of data specified in MAX_DATA_COUNT */ if (DataCount > MAX_DATA_COUNT) return ERROR; /* Try to take the semaphore */ if (xSemaphoreTake(Device->xTxSemaphore, 100 / portTICK_PERIOD_MS) == pdTRUE) { /* Semaphore was taken so we can proceed with sending new data */ DISABLE_DEVICE(Device); /* Disable the device while sending data to TX buffer */ NRF24L01_PowerUpInTxMode(Device); /* Power up in TX mode */ NRF24L01_FlushTxBuffer(Device); /* Flush the TX buffer */ SELECT_DEVICE(Device); SPI_WriteRead(Device->SPIDevice, W_TX_PAYLOAD); /* We want to write the TX payload */ SPI_WriteRead(Device->SPIDevice, DataCount); /* Write the data count */ uint32_t i; for (i = 0; i < DataCount; i++) { SPI_WriteRead(Device->SPIDevice, Data[i]); /* Write the data */ } for (i++; i <= MAX_DATA_COUNT; i++) { SPI_WriteRead(Device->SPIDevice, PAYLOAD_FILLER_DATA); /* Fill the rest of the payload with filler data */ } DESELECT_DEVICE(Device); ENABLE_DEVICE(Device); return SUCCESS; } else return ERROR; }
/** * @brief Flush the RX Buffer * @param Device: The device to use * @retval None */ void NRF24L01_FlushRxBuffer(NRF24L01_Device* Device) { SELECT_DEVICE(Device); SPI_WriteRead(Device->SPIDevice, FLUSH_RX); DESELECT_DEVICE(Device); }
bool HMC5883L::setScale(float gauss) { __u8 regValue = 0x00; SELECT_DEVICE(*fd, HMC5883L_ADDRESS, deviceName); if(gauss == 0.88f) { regValue = 0x00; scale = 0.73f; } else if(gauss == 1.3f) { regValue = 0x01; scale = 0.92f; } else if(gauss == 1.9f) { regValue = 0x02; scale = 1.22; } else if(gauss == 2.5f) { regValue = 0x03; scale = 1.52f; } else if(gauss == 4.0f) { regValue = 0x04; scale = 2.27f; } else if(gauss == 4.7f) { regValue = 0x05; scale = 2.56f; } else if(gauss == 5.6f) { regValue = 0x06; scale = 3.03f; } else if(gauss == 8.1f) { regValue = 0x07; scale = 4.35f; } else { printf("Switching to default scale values in %s\n", deviceName ); regValue = 0x01; scale = 0.92f; } // Setting is in the top 3 bits of the register. regValue = regValue << 5; return writeReg8(*fd, HMC5883L_CONFIGURATION_REGISTER_B, regValue); }
bool HMC5883L::setMeasurementMode(__u8 mode) { SELECT_DEVICE(*fd, HMC5883L_ADDRESS, deviceName); return writeReg8(*fd, HMC5883L_MODE_REGISTER, mode);//writeByte(*fd, HMC5883L_MODE_REGISTER, mode); }