void rfm69_sendFrame(char toAddress, const void* buffer, char bufferSize, char requestACK, char sendACK) { char thedata[63]; char i; //printf("Prepared to send a new frame\n\r"); rfm69_setMode(RF69_MODE_STANDBY); //turn off receiver to prevent reception while filling fifo while ((rfm69_readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00); // Wait for ModeReady ////writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00); // DIO0 is "Packet Sent" if (bufferSize > MAX_DATA_LEN) bufferSize = MAX_DATA_LEN; //printf("Preparing the packet\n\r"); for(i = 0; i < 63; i++) thedata[i] = 0; thedata[0] = REG_FIFO | 0x80; thedata[1] = bufferSize + 3; thedata[2] = toAddress; thedata[3] = _address; if(sendACK == 1) thedata[4] = 0x80; else if(requestACK == 1) thedata[4] = 0x40; else thedata[4] = 0x00; for(i = 0; i < bufferSize; i++) { thedata[i + 5] = ((char*)buffer)[i]; } //printf("Sending by SPI\n\r"); wiringPiSPIDataRW(SPI_DEVICE, thedata, bufferSize + 5); /* no need to wait for transmit mode to be ready since its handled by the radio */ rfm69_setMode(RF69_MODE_TX); ////while (digitalRead(_interruptPin) == 0); //wait for DIO0 to turn HIGH signalling transmission finish ////delay(10); //printf("Changing to TX mode\n\r"); while (!(rfm69_readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PACKETSENT)); // Wait for ModeReady rfm69_setMode(RF69_MODE_STANDBY); //printf("Done, Changing to standby mode\n\r"); }
void config_rf_chip(int p){ // unsigned char config[30][17]; int i,j,k; char len; unsigned char data[16]; unsigned char ARRAY[]=RADIO_CONFIGURATION_DATA_ARRAY; k=0; len=1; for(i=0;len;i++) { len=ARRAY[i+k]; if(len) for(j=1;j<(int)len+1;j++) data[j-1]=ARRAY[i+j+k]; if(len) wiringPiSPIDataRW(p,data,len); k+=len; } /* for(i=0;i!=30;i++)//A config egy dimenzios tombbol 2 dimenziosat csinal. Minden sor 0. eleme a sor hossza. { len=ARRAY[i+k]; config[i][0]=len; for(j=1;j!=(int)len+1;j++) config[i][j]=ARRAY[i+j+k]; k+=len; } for(i=0;i!=30;i++)//A config bite-ok lekuldese SPI-on { len=config[i][0]; for(j=1;j!=(int)len+1;j++) data[j-1]=config[i][j]; wiringPiSPIDataRW(p,data,len); CTS(); } */ }
int pollRudder(double *angle) { static int fsRaw = 0x7fffff; // Full scale digital static double fsU = 3.3; // Full scale volts static double fu = 0; // Filtered voltage static double fk = 0.1; // Filter constant // ******************************************************************************************* // Conversion to degrees, y angle in deg, x signal in volt. Straight line y=kx+m. degPerVolt = k. // uZeroDeg = voltage at 0 deg => m = - k * uZeroDeg = - degPerVolt * uZeroDeg // ******************************************************************************************* static double degPerVolt = 37.42 ; //static double uZeroDeg = 2.347; static double uZeroDeg = 2.60; // Check DRDY# if (digitalRead(6) == 0) { // Read 3 data bytes unsigned char rData[4]; rData[0] = 0x10; // RDATA cmd rData[1] = 0x00; rData[2] = 0x00; rData[3] = 0x00; wiringPiSPIDataRW(0, rData, 4); int res = rData[1] * 256 * 256 + rData[2] * 256 + rData[3]; double u = res * fsU / fsRaw; // Conversion to Volt //printf("%f\n", u); fu = u * fk + (1.0 - fk) * fu; // Digital filter *angle = degPerVolt *fu - degPerVolt * uZeroDeg; // Conversion to deg //printf("%f %f %f \n", u, fu, *angle); return 1; } return 0; }
static ERL_NIF_TERM xfer_2(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { unsigned int channel, length; int result, i, val; uint8_t *buffer; ERL_NIF_TERM cell, head, tail, list; if (argc < 2 || !enif_is_number(env, argv[0]) || !enif_is_list(env, argv[1])) { return enif_make_badarg(env); } if (!enif_get_uint(env, argv[0], &channel) || channel < 0 || channel > 1) { return enif_make_badarg(env); } if (!enif_get_list_length(env, argv[1], &length) || length == 0) { return enif_make_badarg(env); } buffer = (uint8_t *) enif_alloc(sizeof(uint8_t) * length); list = argv[1]; for (i = 0; enif_get_list_cell(env, list, &head, &tail); ++i, list = tail) { if (!enif_get_int(env, head, &val)) { return enif_make_badarg(env); } buffer[i] = val; } result = wiringPiSPIDataRW(channel, buffer, length); if (result == -1) { result = errno; enif_free(buffer); return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_int(env, result)); } list = enif_make_list(env, 0); for (i = length - 1; i >= 0; --i) { cell = enif_make_uint(env, (unsigned int) buffer[i]); list = enif_make_list_cell(env, cell, list); } enif_free(buffer); return enif_make_tuple2(env, enif_make_atom(env, "ok"), list); }
void RX_Command(int p,char *x){ *x='r'; unsigned char d[]={CMD_START_RX,0,0,0,FIX_PACKET_LENGTH,0,0,0}; wiringPiSPIDataRW(p,d,sizeof(d)); CTS(); }
/*--------------------------------------------------------------------------------- send readStatus to MCP2510 ---------------------------------------------------------------------------------*/ uint8_t MCP2510::readStatus() { uint8_t receivedData[3] = { 0xa0,0xFF, 0xFF }; wiringPiSPIDataRW(_csPin,receivedData,3); // Disable slave return receivedData[2]; }
/*--------------------------------------------------------------------------------- send reset to MCP2510 ---------------------------------------------------------------------------------*/ void MCP2510::reset() { // Disable slave pin CS uint8_t resetMessage[1] = { 0xc0 }; wiringPiSPIDataRW(_csPin,resetMessage,1); }
/*--------------------------------------------------------------------------------- send requestToSend to MCP2510 ---------------------------------------------------------------------------------*/ void MCP2510::requestToSend(uint8_t buffer) { // send command uint8_t data[1] = { 0x80 | buffer }; wiringPiSPIDataRW(_csPin,data,1); }
/*--------------------------------------------------------------------------------- send bitModify to MCP2510 ---------------------------------------------------------------------------------*/ void MCP2510::bitModify(uint8_t reg, uint8_t mask, uint8_t data) { // Disable slave pin CS uint8_t dataArr[4] = { 0x05, reg, mask, data}; wiringPiSPIDataRW(_csPin,dataArr,4); }
unsigned int sample(int channel) { buf[0] = 0x60 + channel; buf[1] = 0x00; wiringPiSPIDataRW(CHANNEL, buf, 2); return buf[1] + (buf[0] & 0x03) << 8; }
/*--------------------------------------------------------------------------------- Write data to MCP2510 register reg : MCP2510 register val : data value ---------------------------------------------------------------------------------*/ void MCP2510::write(uint8_t reg, uint8_t val ) { uint8_t data[3] = {0x02,reg,val}; wiringPiSPIDataRW(_csPin,data,3); // Disable slave pin CS }
void bitModReg(uint8_t regAddress, uint8_t mask, uint8_t byteToWrite){ uint8_t dataBuf[4] = {CMD_BIT_MOD, regAddress, mask, byteToWrite}; //returns an int, not sure what that int is wiringPiSPIDataRW (0, dataBuf, 4); }
uint8_t spi_send_recv(uint8_t byte) { wiringPiSPIDataRW(0, &byte, 1); return byte; }
int sendFrame(unsigned char *data, int length) { wiringPiSPIDataRW(0, data, length); }
// internal function - interrupt gets called when a packet is received void RFM69::interruptHandler() { #ifdef RASPBERRY unsigned char thedata[67]; char i; for(i = 0; i < 67; i++) thedata[i] = 0; // printf("interruptHandler %d\n", intCount); #endif //pinMode(4, OUTPUT); //digitalWrite(4, 1); if (_mode == RF69_MODE_RX && (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)) { //RSSI = readRSSI(); setMode(RF69_MODE_STANDBY); #ifdef RASPBERRY thedata[0] = REG_FIFO & 0x7F; thedata[1] = 0; // PAYLOADLEN thedata[2] = 0; // TargetID wiringPiSPIDataRW(SPI_DEVICE, thedata, 3); delayMicroseconds(MICROSLEEP_LENGTH); PAYLOADLEN = thedata[1]; PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution TARGETID = thedata[2]; #else select(); SPI.transfer(REG_FIFO & 0x7F); PAYLOADLEN = SPI.transfer(0); PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution TARGETID = SPI.transfer(0); #endif if(!(_promiscuousMode || TARGETID == _address || TARGETID == RF69_BROADCAST_ADDR) // match this node's address, or broadcast address or anything in promiscuous mode || PAYLOADLEN < 3) // address situation could receive packets that are malformed and don't fit this libraries extra fields { PAYLOADLEN = 0; unselect(); receiveBegin(); //digitalWrite(4, 0); return; } #ifdef RASPBERRY DATALEN = PAYLOADLEN - 3; thedata[0] = REG_FIFO & 0x77; thedata[1] = 0; //SENDERID thedata[2] = 0; //CTLbyte; for(i = 0; i< DATALEN; i++) { thedata[i+3] = 0; } wiringPiSPIDataRW(SPI_DEVICE, thedata, DATALEN + 3); SENDERID = thedata[1]; uint8_t CTLbyte = thedata[2]; ACK_RECEIVED = CTLbyte & 0x80; //extract ACK-requested flag ACK_REQUESTED = CTLbyte & 0x40; //extract ACK-received flag for (i= 0; i < DATALEN; i++) { DATA[i] = thedata[i+3]; } #else DATALEN = PAYLOADLEN - 3; SENDERID = SPI.transfer(0); uint8_t CTLbyte = SPI.transfer(0); ACK_RECEIVED = CTLbyte & RFM69_CTL_SENDACK; // extract ACK-received flag ACK_REQUESTED = CTLbyte & RFM69_CTL_REQACK; // extract ACK-requested flag interruptHook(CTLbyte); // TWS: hook to derived class interrupt function for (uint8_t i = 0; i < DATALEN; i++) { DATA[i] = SPI.transfer(0); } #endif if (DATALEN < RF69_MAX_DATA_LEN) DATA[DATALEN] = 0; // add null at end of string unselect(); setMode(RF69_MODE_RX); } RSSI = readRSSI(); //digitalWrite(4, 0); }
int FalconConfigureHardware(char *filename, int spiPort) { LogDebug(VB_SETTING, "FalconConfigureHardware(%s, %d)\n", filename, spiPort); char fbuf[FALCON_CFG_FILE_MAX_SIZE]; unsigned char *buf; buf = (unsigned char *)malloc(FALCON_CFG_BUF_SIZE); if (!buf) { LogErr(VB_SETTING, "Unable to allocate %d byte buffer for writing Falcon config: %s\n", FALCON_CFG_BUF_SIZE, strerror(errno)); return -1; } int bytesRead = FalconReadConfig(filename, fbuf); if (bytesRead != 1024) { LogErr(VB_SETTING, "Invalid data in %s, unable to write Falcon config\n", filename); free(buf); return -1; } bzero(buf, FALCON_CFG_BUF_SIZE); memcpy(buf, fbuf, bytesRead); int bytesWritten; DisableChannelOutput(); usleep(100000); if ((logLevel & LOG_DEBUG) && (logMask & VB_SETTING)) HexDump("Falcon Hardware Config", buf, bytesRead); bytesWritten = wiringPiSPIDataRW (0, (unsigned char *)buf, FALCON_CFG_BUF_SIZE); if (bytesWritten != FALCON_CFG_BUF_SIZE) { LogErr(VB_SETTING, "Error: wiringPiSPIDataRW returned %d, expecting %d\n", bytesWritten, FALCON_CFG_BUF_SIZE); } if ((logLevel & LOG_DEBUG) && (logMask & VB_SETTING)) HexDump("Falcon Hardware Config Response", buf, 8); usleep(10000); bzero(buf, FALCON_CFG_BUF_SIZE); memcpy(buf, fbuf, bytesRead); bytesWritten = wiringPiSPIDataRW (0, (unsigned char *)buf, FALCON_CFG_BUF_SIZE); if (bytesWritten != FALCON_CFG_BUF_SIZE) { LogErr(VB_CHANNELOUT, "Error: wiringPiSPIDataRW returned %d, expecting %d\n", bytesWritten, FALCON_CFG_BUF_SIZE); free(buf); usleep(100000); EnableChannelOutput(); return -1; } if ((logLevel & LOG_DEBUG) && (logMask & VB_SETTING)) HexDump("Falcon Hardware Config Response", buf, 8); free(buf); usleep(100000); EnableChannelOutput(); }
void show(){ digitalWrite(5, HIGH); wiringPiSPIDataRW(0, frame, 1024); }
int sendFrame(unsigned char *data, int length) { printf("sendFrame received %s, %d\n",data,length); wiringPiSPIDataRW(0, data, length); printf("received data: %s\n",data); }
int FalconPassThroughData(int offset, unsigned char *inBuf, int size) { LogDebug(VB_SETTING, "FalconPassThroughData(%p)\n", inBuf); if ((logLevel & LOG_DEBUG) && (logMask & VB_SETTING)) HexDump("Falcon Pass-through data", inBuf, size); // Disable channel outputs and let them quiesce before sending config info DisableChannelOutput(); usleep(100000); if (getSettingInt("FPDEnabled")) { unsigned char *buf = (unsigned char *)malloc(FALCON_CFG_BUF_SIZE); if (!buf) { LogErr(VB_SETTING, "Unable to allocate %d byte buffer for passing through Falcon data: %s\n", FALCON_CFG_BUF_SIZE, strerror(errno)); return -1; } bzero(buf, FALCON_CFG_BUF_SIZE); if(offset < (FALCON_CFG_BUF_SIZE - size)) { LogDebug(VB_SETTING, "Offset = %d\n", offset); } else { LogErr(VB_SETTING,"Offset %d is invalid: %s\n",offset, strerror(errno)); } buf[0] = 0xCC; buf[1] = 0xCC; buf[2] = 0xCC; buf[3] = 0xCC; buf[4] = 0xCC; buf[5] = 0x55; memcpy(buf+FALCON_CFG_HEADER_SIZE+offset, inBuf, size); int bytesWritten; bytesWritten = wiringPiSPIDataRW (0, (unsigned char *)buf, FALCON_CFG_BUF_SIZE); if (bytesWritten != FALCON_CFG_BUF_SIZE) { LogErr(VB_SETTING, "Error: wiringPiSPIDataRW returned %d, expecting %d\n", bytesWritten, FALCON_CFG_BUF_SIZE); } free(buf); usleep(100000); } // Pass data on to our regular channel outputs followed by blanking data bzero(sequence->m_seqData + offset, 4096); memcpy(sequence->m_seqData + offset, inBuf, FALCON_PASSTHROUGH_DATA_SIZE); sequence->SendSequenceData(); sequence->SendBlankingData(); // reset data so we don't keep reprogramming // Give changes time to take effect then turn back on channel outputs usleep(100000); EnableChannelOutput(); }
void rfm69_receive(void) { unsigned long timeout = 10000; char thedata[67]; char i; DATALEN = 0; SENDERID = 0; TARGETID = 0; PAYLOADLEN = 0; ACK_REQUESTED = 0; ACK_RECEIVED = 0; RSSI = 0; for(i = 0; i < 63; i++) DATA[i] = 0; // Receive Data until timeout or valid data while(1) { if (rfm69_readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY) rfm69_writeReg(REG_PACKETCONFIG2, (rfm69_readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks ////writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01); //set DIO0 to "PAYLOADREADY" in receive mode rfm69_setMode(RF69_MODE_RX); //printf("Ya estoy en modo RX\n\r"); // Receive Data until timeout (aprox 2s) while((rfm69_readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY) == 0) { timeout--; if(timeout == 0) { //printf("Timeout!\n\r"); rfm69_setMode(RF69_MODE_STANDBY); return; } //usleep(1); } //printf("He recibido algo!!\n\r"); // Received any packet! rfm69_setMode(RF69_MODE_STANDBY); thedata[0] = REG_FIFO & 0x7f; thedata[1] = 0; // PAYLOADLEN thedata[2] = 0; // TARGETID wiringPiSPIDataRW(SPI_DEVICE, thedata, 3); usleep(5); PAYLOADLEN = thedata[1]; PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; TARGETID = thedata[2]; //printf("Payload length: %i\n\r", PAYLOADLEN); //printf("Target id: %i\n\r", TARGETID); if(!(_promiscuousMode || TARGETID==_address || TARGETID==RF69_BROADCAST_ADDR)) {//match this node's address, or $ PAYLOADLEN = 0; } else { DATALEN = PAYLOADLEN - 3; thedata[0] = REG_FIFO & 0x7f; thedata[1] = 0; // SENDERID thedata[2] = 0; // CTLbyte for(i = 0; i < DATALEN; i++) { thedata[i+3] = 0; // DATA } wiringPiSPIDataRW(SPI_DEVICE, thedata, DATALEN + 3); SENDERID = thedata[1]; CTLBYTE = thedata[2]; //printf("Sender id: %i\n\r", thedata[1]); //printf("CTLbyte: %i\n\r", thedata[2]); //printf("Data: ...\n\r"); for(i = 0; i < DATALEN; i++) { //printf("%c\n\r", thedata[i+3]); DATA[i] = thedata[i+3]; } RSSI = rfm69_readRSSI(0); return; } } }
uint8_t SPI_nrf24::spi_shift(uint8_t data_write){ unsigned char* data_buffer = &data_write; wiringPiSPIDataRW(0,data_buffer,1); return *data_buffer; }
void writeReg(uint8_t regAddress, uint8_t val){ uint8_t dataBuf[3] = {CMD_WRITE, regAddress, val}; //returns an int, not sure what that int is wiringPiSPIDataRW (0, dataBuf, 3); }