void Nrf24l::send(uint8_t * value) // Sends a data package to the default address. Be sure to send the correct // amount of bytes as configured as payload on the receiver. { uint8_t status; status = getStatus(); while (PTX) { status = getStatus(); if((status & ((1 << TX_DS) | (1 << MAX_RT)))){ PTX = 0; break; } } // Wait until last paket is send ceLow(); powerUpTx(); // Set to transmitter mode , Power up flushTx(); nrfSpiWrite(W_TX_PAYLOAD, value, false, payload); // Write payload ceHi(); // Start transmission ceLow(); }
void Nrf24l::setRADDR(uint8_t * adr) // Sets the receiving address { ceLow(); writeRegister(RX_ADDR_P1,adr,mirf_ADDR_LEN); ceHi(); }
void Nrf24l::powerUpRx(){ PTX = 0; ceLow(); configRegister(CONFIG, mirf_CONFIG | ( (1<<PWR_UP) | (1<<PRIM_RX) ) ); ceHi(); configRegister(STATUS,(1 << TX_DS) | (1 << MAX_RT)); }
void Nrf24l::send(uint8_t * value) // Sends a data package to the default address. Be sure to send the correct // amount of bytes as configured as payload on the receiver. { uint8_t status; status = getStatus(); while (PTX) { status = getStatus(); if((status & ((1 << TX_DS) | (1 << MAX_RT)))){ PTX = 0; break; } } // Wait until last paket is send ceLow(); powerUpTx(); // Set to transmitter mode , Power up csnLow(); // Pull down chip select spi->transfer( FLUSH_TX ); // Write cmd to flush tx fifo csnHi(); // Pull up chip select csnLow(); // Pull down chip select spi->transfer( W_TX_PAYLOAD ); // Write cmd to write payload transmitSync(value,payload); // Write payload csnHi(); // Pull up chip select ceHi(); // Start transmission }
void Nrf24l::send_head() // Sends a data package to the default address. Be sure to send the correct // amount of bytes as configured as payload on the receiver. { uint8_t status; status = getStatus(); while (PTX) { status = getStatus(); //TX_DS 5 0 R/W 数据发送完成中断。当数据发送完成后产生中 //断。如果工作在自动应答模式下,只有当接收到应答信号后此位置一。 //写‘1’清除中断。 //MAX_RT 4 0 R/W 达到最多次重发中断。 //写‘1’清除中断。 //如果MAX_RT 中断产生则必须清除后系统才 //能进行通讯。 if((status & ((1 << TX_DS) | (1 << MAX_RT)))){ PTX = 0; break; } } // Wait until last paket is send ceLow(); powerUpTx(); // Set to transmitter mode , Power up csnLow(); // Pull down chip select spi->transfer( FLUSH_TX ); // Write cmd to flush tx fifo csnHi(); // Pull up chip select csnLow(); // Pull down chip select //W_RX_PAYLOAD 1010 0000 写TX 有效数据:1-32 字节。写操作从字节0 开始。 //应用于发射模式下 spi->transfer( W_TX_PAYLOAD ); // Write cmd to write payload }
void Nrf24l::init() // Initializes pins to communicate with the MiRF module // Should be called in the early initializing phase at startup. { GPIO_InitTypeDef PORT; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC,ENABLE); // TODO: 根据情况更改 // Configure CE pin as output with Push-Pull PORT.GPIO_Speed = GPIO_Speed_50MHz; PORT.GPIO_Pin = nRF24_CE_PIN; PORT.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(nRF24_CE_PORT,&PORT); // Configure CS pin as output with Push-Pull PORT.GPIO_Pin = nRF24_CS_PIN; PORT.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(nRF24_CS_PORT,&PORT); // pinMode(cePin,OUTPUT); // pinMode(csnPin,OUTPUT); ceLow(); csnHi(); // Initialize spi module spi->begin(); }
void Nrf24l::powerDown(){ ceLow(); configRegister(CONFIG, baseConfig); flushRx(); flushTx(); }
void Nrf24l::powerUpRx() { PTX = 0; ceLow(); configRegister(CONFIG, baseConfig | _BV(PWR_UP) | _BV(PRIM_RX)); configRegister(STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT)); ceHi(); }
void powerUpRx(){ PTX = 0; ceLow(); uint8_t result = mirf_CONFIG | ( (1<<PWR_UP) | (1<<PRIM_RX)); result |= (1<<MASK_MAX_RT) | (1<<MASK_TX_DS); //ativa nIQR apenas quando tem algo para receber (tx mode) writeRegister(CONFIG, &result,1); ceHi(); result = (1 << TX_DS) | (1 << MAX_RT); writeRegister(STATUS,&result,1); }
nRF24L01p::nRF24L01p(const byte csn,const byte ce): _csn(csn),_ce(ce){ pinMode(_csn,OUTPUT); pinMode(_ce,OUTPUT); csnHigh(); ceLow(); _txIndex=0; _rxIndex=0; _prim_rx=false; _prim_tx=false; _init=false; }
void Nrf24l::init() // Initializes pins to communicate with the MiRF module // Should be called in the early initializing phase at startup. { pinMode(cePin, OUTPUT); pinMode(csnPin, OUTPUT); ceLow(); csnHi(); // Initialize spi module spi->begin(); }
// 上电 void Nrf24l::powerUpRx(){ PTX = 0; ceLow(); // 使用 ‘0’-8 位CRC 校验模式,开启1:上电模式,开启接收模式 configRegister(CONFIG, mirf_CONFIG | ( (1<<PWR_UP) | (1<<PRIM_RX) ) ); ceHi(); // 清除之前的数据发送完成中断。不是关闭中断 // 当数据发送完成后产生中断。如果工作在自动应答模式下, // 只有当接收到应答信号后此位置一。写‘1’清除中断。 // 清除之前的达到最多次重发中断。不是关闭中断 // 写‘1’清除中断。如果MAX_RT 中断产生则必须清除后系统才能进行通讯。 configRegister(STATUS,(1 << TX_DS) | (1 << MAX_RT)); }
boolean nRF24L01p::send(boolean _modeSend){ if(_prim_tx==false){ _txIndex=32; return false; } if(_prim_rx==true){ ceLow(); primPTX(); } csnLow(); if(_modeSend==false || (_prim_rx && _prim_tx)){ SPI.transfer(W_TX_PAYLOAD); }else{ SPI.transfer(W_TX_PAYLOAD_NOACK); } for(int i=0;i<_txIndex;i++){ SPI.transfer(_txPayLoad[i]); } csnHigh(); cePulse(); unsigned long tmC1=micros(); while(1){ if(bitRead(getStatus(),5)){ //writeReg(STATUS,_status|(1<<6)); writeReg(STATUS,_status|(1<<5)); writeReg(STATUS,_status|(1<<4)); break; } if(bitRead(getStatus(),4)){ writeReg(STATUS,_status|(1<<4)); cePulse(); } unsigned long tmC2=micros(); if(tmC2-tmC1>100L){ //was 250000L - lowered to make the timeout wait much shorter //Serial.println("send(SLOW) returned false"); //Serial.println("connection lost "); return false; } } _txIndex=0; if(_prim_rx==true){ primPRX(); ceHigh(); } return true; }
void send_rf(uint8_t * value) // Sends a data package to the default address. Be sure to send the correct // amount of bytes as configured as payload on the receiver. { uint8_t status; status = getStatus(); while (PTX) { status = getStatus(); if((status & ((1 << TX_DS) | (1 << MAX_RT)))){ PTX = 0; break; } } // Wait until last paket is send ceLow(); powerUpTx(); // Set to transmitter mode , Power up csnLow(); uint8_t result = FLUSH_TX; SSPSend(&result, 1); csnHi(); uint8_t* rx = (uint8_t*)malloc(sizeof(uint8_t)*(payload+1)); rx[0] = W_TX_PAYLOAD; uint8_t a; for(a = 1; a < payload+1; a++){ rx[a] = value[a-1]; } csnLow(); SSPSend(rx, payload+1); free(rx); csnHi(); ceHi(); // Start transmission while(isSending()); }
void nRF24L01p::testLow(){ if(_init==true && _prim_rx==true)ceLow(); }
void nRF24L01p::cePulse(){ ceHigh(); delayMicroseconds(10); ceLow(); }
void Nrf24l::powerDown(){ // 掉电模式 ceLow(); configRegister(CONFIG, mirf_CONFIG ); }