uint16_t rfxx_wrt_cmd(uint16_t aCmd) { uint8_t i; uint16_t temp = 0; LOW_SCK(); LOW_SEL(); //for (i=0; i<16; i++) { for (i = 0; i < sizeof(uint16_t); ++i) { temp <<= 1; if (SDO_HI()) temp |= 0x0001; LOW_SCK(); (aCmd & 0x8000) ? (HI_SDI()) : (LOW_SDI()); /* if (aCmd & 0x8000) HI_SDI(); else LOW_SDI(); */ HI_SCK(); aCmd <<= 1; } LOW_SCK(); HI_SEL(); return temp; }
/* Write a Command to the RFM Module using SPI Requires: 16bit valid command Returns: 16bit result from transaction This is a bi-directional transfer. A command is clocked out to the RFM a bit at a time. At the same time a result is clocked back in a bit at a time. */ uint WriteCMD(uint CMD) { uint RESULT = 0; // Holds the received SDI uchar n=16; // number of bits in SPI Command we need to send LOW_SCK(); // SCK LOW LOW_SEL(); // CS LOW while(n--) // Send All 16 Bits MSB First { if (CMD&0x8000) { Write1(); // Write 1 via SDI } else { Write0(); // Write 0 via SPI } RESULT<<=1; // Shift left for next bit to receive if(SDI) // Check if we received a high on SDI RESULT |= 0x0001; // RESULT LSB = 1 CMD<<=1; // Shift left for next bit to send } LOW_SCK(); // SCK LOW HI_SEL(); // CS HIGH - Finished Sending Command return RESULT; }
/* Waits for RFM 12 to become ready by checking first bit of status register Before we can Tx or Rx data the RFM12 needs to be ready, i.e. not in the middle of transmitting a previous byte This function is blocking and will only return when it is ready to Tx or Rx */ void WaitReady(void) { //#ifdef DEBUG // always return for simulator // return; //#endif unsigned long timeout = 0; bit READY = 0; while(READY == 0) { LOW_SCK(); // SCK LOW LOW_SEL(); // CS LOW Write0(); if(SDI==1) // check RGIT bit of status (first bit) { // If HIGH then Tx/RX is ready to accept the next byte READY = 1; } LOW_SCK(); // SCK LOW LOW_SDO(); HI_SEL(); // CS HIGH - Finished Sending Command } }
unsigned int RFXX_WRT_CMD(unsigned int aCmd){ unsigned char i; unsigned int temp; temp=0; LOW_SCK(); LOW_SEL(); for(i=0;i<16;i++){ if(aCmd&0x8000){ HI_SDI(); }else{ LOW_SDI(); } HI_SCK(); temp<<=1; if(SDO_HI()){ temp|=0x0001; } LOW_SCK(); aCmd<<=1; }; HI_SEL(); return(temp); }