/* * Author: Yevgeniy Kiveisha * Copyright (c) 2014 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include "nrf24l01.h" using namespace upm; NRF24l01::NRF24l01 (uint8_t cs) { maa_init(); nrfInitModule (cs, 8); } NRF24l01::~NRF24l01 () { maa_result_t error = MAA_SUCCESS; error = maa_spi_stop(m_spi); if (error != MAA_SUCCESS) { maa_result_print(error); } error = maa_gpio_close (m_cePinCtx); if (error != MAA_SUCCESS) { maa_result_print(error); } error = maa_gpio_close (m_csnPinCtx); if (error != MAA_SUCCESS) { maa_result_print(error); } } void NRF24l01::nrfInitModule (uint8_t chip_select, uint8_t chip_enable) { maa_result_t error = MAA_SUCCESS; m_csn = chip_select; m_ce = chip_enable; m_channel = 1; m_csnPinCtx = maa_gpio_init (m_csn); if (m_csnPinCtx == NULL) { fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", m_csn); exit (1); } m_cePinCtx = maa_gpio_init (m_ce); if (m_cePinCtx == NULL) { fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", m_ce); exit (1); } error = maa_gpio_dir (m_csnPinCtx, MAA_GPIO_OUT); if (error != MAA_SUCCESS) { maa_result_print (error); } error = maa_gpio_dir (m_cePinCtx, MAA_GPIO_OUT); if (error != MAA_SUCCESS) { maa_result_print (error); } nrfCELow (); m_spi = maa_spi_init (0); } void NRF24l01::nrfConfigModule() { /* Set RF channel */ nrfConfigRegister (RF_CH, m_channel); /* Set length of incoming payload */ nrfConfigRegister (RX_PW_P0, m_payload); nrfConfigRegister (RX_PW_P1, m_payload); /* Set length of incoming payload for broadcast */ nrfConfigRegister (RX_PW_P2, m_payload); /* Start receiver */ nrfPowerUpRX (); nrfFlushRX (); } /* Clocks only one byte into the given MiRF register */ void NRF24l01::nrfConfigRegister(uint8_t reg, uint8_t value) { nrfCSOn (); maa_spi_write (m_spi, W_REGISTER | (REGISTER_MASK & reg)); maa_spi_write (m_spi, value); nrfCSOff (); } void NRF24l01::nrfPowerUpRX() { m_ptx = 0; nrfCELow(); nrfConfigRegister(CONFIG, mirf_CONFIG | ( (1<