/*! * Method used to test if the device is working properly. It works by writing to a * register and then reading the same register. * * @deprecated Currently not in use. */ void EcgCapture::testDevice() { QByteArray TESTIN; TESTIN.resize(4); QByteArray TESTOUT; TESTOUT.resize(4); TESTIN[0] = 0x01; TESTIN[1] = 0x00; TESTIN[2] = 0x00; TESTIN[3] = 0x00; TESTOUT[0] = 0x11; TESTOUT[1] = 0x11; TESTOUT[2] = 0x11; TESTOUT[3] = 0x11; qDebug()<<TESTOUT.toHex(); bcm2835_spi_transfernb(TESTIN.data(), TESTOUT.data(), TESTIN.size()); qDebug()<<TESTOUT.toHex(); if(bcm2835_gpio_lev(PIN16)==LOW) { qDebug()<<"LOW"; } else { qDebug()<<"HIGH"; } QByteArray FRAMES; QByteArray DATAOUT; FRAMES.resize(4); DATAOUT.resize(4); FRAMES[0] = 0x00; FRAMES[1] = 0x00; FRAMES[2] = 0x00; FRAMES[3] = 0x00; DATAOUT[0] = 0x11; DATAOUT[1] = 0x11; DATAOUT[2] = 0x11; DATAOUT[3] = 0x11; qDebug()<<DATAOUT.toHex(); bcm2835_spi_transfernb(FRAMES.data(), DATAOUT.data(), FRAMES.size()); qDebug()<<DATAOUT.toHex(); }
uint8_t RF24::read_register(uint8_t reg) { uint8_t result; #if defined (RF24_LINUX) csn(LOW); uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; *ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) ); *ptx++ = NOP ; // Dummy operation, just for reading bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); result = *++prx; // result is 2nd byte of receive buffer #elif defined (__arm__) && !defined ( CORE_TEENSY ) _SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ) , SPI_CONTINUE); result = _SPI.transfer(csn_pin,0xff); #else csn(LOW); _SPI.transfer( R_REGISTER | ( REGISTER_MASK & reg ) ); result = _SPI.transfer(0xff); csn(HIGH); #endif return result; }
uint8_t RF24::read_payload(void* buf, uint8_t len) { uint8_t status; uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; uint8_t size ; uint8_t* current = reinterpret_cast<uint8_t*>(buf); uint8_t data_len = min(len,payload_size); uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len; size = data_len + blank_len + 1; // Add register value to transmit buffer if (debug) printf("[Reading %u bytes %u blanks]",data_len,blank_len); *ptx++ = R_RX_PAYLOAD; while(size--) *ptx++ = NOP; // Size has been lost during while, re affect size = data_len + blank_len + 1; // Add register value to transmit buffer bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); // 1st byte is status status = *prx++; // Decrement before to skip 1st status byte while ( --size ) *current++ = *prx++; return status; }
uint8_t RF24::write_register(uint8_t reg, uint8_t value) { uint8_t status; IF_SERIAL_DEBUG(printf_P(PSTR("write_register(%02x,%02x)\r\n"),reg,value)); #if defined (RF24_LINUX) csn(LOW); uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) ); *ptx = value ; bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); status = *prx++; // status is 1st byte of receive buffer #elif defined (__arm__) && !defined ( CORE_TEENSY ) status = _SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE); _SPI.transfer(csn_pin,value); #else csn(LOW); status = _SPI.transfer( W_REGISTER | ( REGISTER_MASK & reg ) ); _SPI.transfer(value); csn(HIGH); #endif return status; }
uint8_t RF24::write_payload(const void* buf, uint8_t len, const uint8_t writeType) { uint8_t status; uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; uint8_t size ; const uint8_t* current = reinterpret_cast<const uint8_t*>(buf); uint8_t data_len = min(len,payload_size); uint8_t blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len; size = data_len + blank_len + 1 ; // Add register value to transmit buffer if (debug) printf("[Writing %u bytes %u blanks]",data_len,blank_len); *ptx++ = W_TX_PAYLOAD; while ( data_len-- ) *ptx++ = *current++; while ( blank_len-- ) *ptx++ = 0; bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); status = *prx; // status is 1st byte of receive buffer return status; }
quint8 QRF24::writePayload(const void* buf, quint8 len) { quint8 status; quint8 * prx = spi_rxbuff; quint8 * ptx = spi_txbuff; quint8 size ; const quint8* current = reinterpret_cast<const quint8*>(buf); quint8 data_len = qMin(len,payload_size); quint8 blank_len = dynamic_payloads_enabled ? 0 : payload_size - data_len; size = data_len + blank_len + 1 ; // Add register value to transmit buffer if (debug) qDebug("[Writing %u bytes %u blanks]",data_len,blank_len); *ptx++ = W_TX_PAYLOAD; while ( data_len-- ) *ptx++ = *current++; while ( blank_len-- ) *ptx++ = 0; bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); status = *prx; // status is 1st byte of receive buffer return status; }
/************************************************************************** Function: spi_lcd_off Purpose: Turn off lcd display Returns: Note: Assumes spi_lcd_init() has been called *************************************************************************/ void spi_lcd_off(void) { u8 tx_buff[4] = { CMD_CODE, CMD_OFF, 0, 0 }; u8 rx_buff[4]; bcm2835_spi_transfernb(tx_buff, rx_buff, 2); bcm2835_delayMicroseconds(100); }
uint8_t readRegister(uint8_t cs_pin, uint8_t addr) { char spibuf[2]; spibuf[0] = addr & 0x7F; spibuf[1] = 0x00; bcm2835_gpio_write(cs_pin,0); bcm2835_spi_transfernb( spibuf, spibuf, sizeof(spibuf) ); bcm2835_gpio_write(cs_pin,1); return spibuf[1]; }
uint8_t RF24::read_register(uint8_t reg) { uint8_t result; uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; *ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) ); *ptx++ = NOP ; // Dummy operation, just for reading bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); result = *++prx; // result is 2nd byte of receive buffer return result; }
quint8 QRF24::writeRegister(quint8 reg, const quint8* buf, quint8 len) { quint8 status; quint8 * prx = spi_rxbuff; quint8 * ptx = spi_txbuff; quint8 size = len + 1; // Add register value to transmit buffer *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) ); while ( len-- ) *ptx++ = *buf++; bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); status = *prx; // status is 1st byte of receive buffer return status; }
uint8_t RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len) { uint8_t status; uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; uint8_t size = len + 1; // Add register value to transmit buffer *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) ); while ( len-- ) *ptx++ = *buf++; bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); status = *prx; // status is 1st byte of receive buffer return status; }
static PyObject * PyBCM2835_spi_transfernb(PyObject *self, PyObject *args) { char *tbuf; char *rbuf; int tbuf_len; int rbuf_len; uint32_t len; if (!PyArg_ParseTuple(args,"s#s#i",&tbuf, &tbuf_len, &rbuf, &rbuf_len,&len)) { return NULL; } bcm2835_spi_transfernb(tbuf,rbuf,len); Py_RETURN_NONE; }
uint8_t RF24::write_register(uint8_t reg, uint8_t value) { uint8_t status; uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) ); *ptx = value ; bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); status = *prx++; // status is 1st byte of receive buffer if (debug) printf("write_register(%02x,%02x)\r\n",reg,value); return status; }
quint8 QRF24::writeRegister(quint8 reg, quint8 value) { quint8 status; quint8 * prx = spi_rxbuff; quint8 * ptx = spi_txbuff; *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) ); *ptx = value ; bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, 2); status = *prx++; // status is 1st byte of receive buffer // if (debug) // qDebug("write_register(%02x,%02x)\r\n",reg,value); return status; }
uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len) { uint8_t status; #if defined (RF24_LINUX) csn(LOW); //In this case, calling csn(LOW) configures the spi settings uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; uint8_t size = len + 1; // Add register value to transmit buffer *ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) ); while (len--){ *ptx++ = NOP; } // Dummy operation, just for reading #if defined (RF24_SPIDEV) _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); #else bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); #endif status = *prx++; // status is 1st byte of receive buffer // decrement before to skip status byte while ( --size ){ *buf++ = *prx++; } #elif defined (__arm__) && !defined ( CORE_TEENSY ) status = _SPI.transfer(csn_pin, R_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE ); while ( len-- > 1 ){ *buf++ = _SPI.transfer(csn_pin,0xff, SPI_CONTINUE); } *buf++ = _SPI.transfer(csn_pin,0xff); #else csn(LOW); status = _SPI.transfer( R_REGISTER | ( REGISTER_MASK & reg ) ); while ( len-- ){ *buf++ = _SPI.transfer(0xff); } csn(HIGH); #endif return status; }
int main(int argc, char **argv) { if (!bcm2835_init()) { printf("oops, could not init bcm2835\n"); return 1; } else { printf("Initialized"); } bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_1024); // ~ 4 MHz bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default uint8_t mosi[12] = { 0x60, 0x00 }; uint8_t miso[12] = { 0 }; printf("Starting"); int data[30000]; for (int i = 0; i < 30000; i++) { bcm2835_spi_transfernb(mosi, miso, 2); // // printf("%d\n", miso[1] + ((miso[0] & 3) << 8)); // data[i] = miso[1] + ((miso[0] & 3) << 10); data[i] = miso[1] + miso[0]; } bcm2835_spi_end(); bcm2835_close(); FILE *fp=fopen("output2.csv","wr"); for(int i=0;i<30000;i++) { fprintf(fp,"%d,\n",data[i]); } fclose(fp); return 0; }
uint8_t RF24::write_register(uint8_t reg, const uint8_t* buf, uint8_t len) { uint8_t status; #if defined (RF24_LINUX) csn(LOW); uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; uint8_t size = len + 1; // Add register value to transmit buffer *ptx++ = ( W_REGISTER | ( REGISTER_MASK & reg ) ); while ( len-- ) *ptx++ = *buf++; #if defined (RF24_SPIDEV) _SPI.transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); #else bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); #endif status = *prx; // status is 1st byte of receive buffer #elif defined (__arm__) && !defined ( CORE_TEENSY ) status = _SPI.transfer(csn_pin, W_REGISTER | ( REGISTER_MASK & reg ), SPI_CONTINUE ); while ( --len){ _SPI.transfer(csn_pin,*buf++, SPI_CONTINUE); } _SPI.transfer(csn_pin,*buf++); #else csn(LOW); status = _SPI.transfer( W_REGISTER | ( REGISTER_MASK & reg ) ); while ( len-- ) _SPI.transfer(*buf++); csn(HIGH); #endif return status; }
uint8_t RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len) { uint8_t status; uint8_t * prx = spi_rxbuff; uint8_t * ptx = spi_txbuff; uint8_t size = len + 1; // Add register value to transmit buffer *ptx++ = ( R_REGISTER | ( REGISTER_MASK & reg ) ); while (len--) *ptx++ = NOP ; // Dummy operation, just for reading bcm2835_spi_transfernb( (char *) spi_txbuff, (char *) spi_rxbuff, size); status = *prx++; // status is 1st byte of receive buffer // decrement before to skip status byte while ( --size ) *buf++ = *prx++; return status; }
int mcp3008_adc_read(uint8_t single_diff, uint8_t channel, uint16_t *adcout) { if(single_diff != MCP3008_ADC_SINGLE && single_diff != MCP3008_ADC_DIFF ) return -1; if(channel > ADC_7 ) return -1; bcm2835_spi_begin(); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE3); // The default bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_1024); // ~ 4 MHz bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE); // // Active the CS pin MCP3008_CS_CLR; // SleepMs(1); uint8_t tx[10] = { MCP3008_SPI_START_BYTE, (single_diff|channel)<<4 }; uint8_t rx[10] = { 0 }; bcm2835_spi_transfernb(tx, rx, 3); bcm2835_spi_end(); // 0100 aaa r/w MCP3008_CS_SET; *adcout = ((rx[1]&0x03) << 8) | rx[2]; return (EXIT_SUCCESS); }
//void bcm2835_spi_transfernb(char* tbuf, char* rbuf, uint32_t len); /// Call bcm2835_spi_transfernb with 3 parameters /// \par Refer /// \par Modify void ope_spi_transfernb(void) { char *tbuf; char *rbuf; uint32_t len; get_int_code(); get_int_code(); get_int_code(); tbuf = *(char **)(buff+1); rbuf = *(char **)(buff+5); len = *((uint32_t *)(buff+9)); // printf("B nb tbuf=%p rbuf=%p len=%d \n",tbuf,rbuf,len); bcm2835_spi_transfernb( bi_send_buff, bi_rec_buff, len ); set_ope_code( OPE_SPI_TRANSFERNB ); set_int_code( (int)rbuf ); set_int_code( len ); //dump_buff(); //printf("w_buff=%p w_buff->wp=%d w_buff->rp=%d \n",w_buff,w_buff->wp,w_buff->rp); put_reply(); //printf("w_buff=%p w_buff->wp=%d w_buff->rp=%d \n",w_buff,w_buff->wp,w_buff->rp); mark_sync(); }
/* Writes (and reads) an number of bytes to SPI // Read bytes are copied over onto the transmit buffer */ void bcm2835_spi_transfern(char* buf, uint32_t len) { bcm2835_spi_transfernb(buf, buf, len); }
// Writes (and reads) an number of bytes to SPI void bcm2835_spi_transfernb(char* tbuf, char* rbuf, uint32_t len, uint32_t delay) { volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4; volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/4; uint32_t TXCnt=0; uint32_t RXCnt=0; uint32_t IGCnt = 0; uint32_t offset=0; uint32_t flip = 1; #if !SPI_GRAYONLY len -= 2*144; len *= 2; #endif // This is Polled transfer as per section 10.6.1 // BUG ALERT: what happens if we get interupted in this section, and someone else // accesses a different peripheral? // Clear TX and RX fifos bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR); // Set TA = 1 bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA); #if SPI_TX // Use the FIFO's to reduce the interbyte times while((TXCnt < len)||(RXCnt < len)) { // TX fifo not full, so add some more bytes while(((bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_TXD))&&(TXCnt < len )) { bcm2835_peri_write_nb(fifo, tbuf[TXCnt]); TXCnt++; } #else /* Feed the dog before */ bcm2835_peri_write_nb(fifo, 0x00); bcm2835_peri_write_nb(fifo, 0x00); bcm2835_peri_write_nb(fifo, 0x00); int first = 4; while(RXCnt < len){ #endif //Rx fifo not empty, so get the next received bytes while(((bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD))&&( RXCnt < len )) { #if !SPI_TX bcm2835_peri_write_nb(fifo, 0x00); #endif #if TEX_W == 176 #if SPI_GRAYONLY if(!(RXCnt % 174)){ offset += 2; rbuf[RXCnt+offset-1] = 0xff; } #else if(flip && !(RXCnt % 174)){ offset += 2; } #endif #endif #if SPI_GRAYONLY rbuf[RXCnt + offset] = bcm2835_peri_read_nb(fifo); RXCnt++; #else /* Receiving full data, need to ignore UV values */ if(flip){ rbuf[RXCnt + offset] = bcm2835_peri_read_nb(fifo); RXCnt++; }else{ bcm2835_peri_read_nb(fifo); } flip ^= 1; #endif } usleep(delay); /* Let other threads do things */ } // Wait for DONE to be set while (!(bcm2835_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE)) ; // Set TA = 0, and also set the barrier bcm2835_peri_set_bits(paddr, 0, BCM2835_SPI0_CS_TA); //printf("Len: %d\t Rx: %d\tOffset: %d\tIg: %d\n", len, RXCnt, offset, IGCnt); //printf("%x %x %x\n", rbuf[RXCnt + offset - 3], rbuf[RXCnt + offset - 2], rbuf[RXCnt + offset - 1]); } // Writes an number of bytes to SPI void bcm2835_spi_writenb(char* tbuf, uint32_t len) { volatile uint32_t* paddr = bcm2835_spi0 + BCM2835_SPI0_CS/4; volatile uint32_t* fifo = bcm2835_spi0 + BCM2835_SPI0_FIFO/4; // This is Polled transfer as per section 10.6.1 // BUG ALERT: what happens if we get interupted in this section, and someone else // accesses a different peripheral? // Clear TX and RX fifos bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_CLEAR, BCM2835_SPI0_CS_CLEAR); // Set TA = 1 bcm2835_peri_set_bits(paddr, BCM2835_SPI0_CS_TA, BCM2835_SPI0_CS_TA); uint32_t i; for (i = 0; i < len; i++) { // Maybe wait for TXD while (!(bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_TXD)) ; // Write to FIFO, no barrier bcm2835_peri_write_nb(fifo, tbuf[i]); // Read from FIFO to prevent stalling while (bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD) (void) bcm2835_peri_read_nb(fifo); } // Wait for DONE to be set while (!(bcm2835_peri_read_nb(paddr) & BCM2835_SPI0_CS_DONE)) { while (bcm2835_peri_read(paddr) & BCM2835_SPI0_CS_RXD) (void) bcm2835_peri_read_nb(fifo); }; // Set TA = 0, and also set the barrier bcm2835_peri_set_bits(paddr, 0, BCM2835_SPI0_CS_TA); } // Writes (and reads) an number of bytes to SPI // Read bytes are copied over onto the transmit buffer void bcm2835_spi_transfern(char* buf, uint32_t len) { bcm2835_spi_transfernb(buf, buf, len, 0); }
void VM205::transfer(char* tbuf, char* rbuf, uint32_t len) { bcm2835_spi_transfernb(tbuf, rbuf, len); }
void MCP3008_getRawValue(void) { bcm2835_spi_transfernb(send_buf, receive_buf,3); }
/*! * Reads a single frame * * \return QVector<double> singleFrame */ const QVector<double> EcgCapture::readFrame() { unsigned int counter = 0; char FRAMES[] = {0x00,0x00,0x00,0x00}; char DATAOUT[] = {0,0,0,0}; int lead = 0; QVector<double> frame; bool ecgFlag = false; bool respFlag = false; bool loffFlag = false; csEnable(); while (counter<nFrames) { if(bcm2835_gpio_lev(PIN16)==LOW) { bcm2835_spi_transfernb(FRAMES, DATAOUT, sizeof(FRAMES)); //Uncomment theese lines to check if frames are dropped //NOTE: writing to the terminal is slow so for performance reasons // theese code snipped is not used. // if (DATAOUT[0] == 0xb0) { // test++; // if (test%100 == 0) { // printf("%d\n",test); // } // } lead = 0; if (DATAOUT[0] == 0x11) { //Lead I lead |= (DATAOUT[1] << 16); lead |= (DATAOUT[2] << 8); lead |= DATAOUT[3]; ecgFlag = true; } else if (DATAOUT[0] == 0x12) { //Lead II lead |= (DATAOUT[1] << 16); lead |= (DATAOUT[2] << 8); lead |= DATAOUT[3]; ecgFlag = true; } else if (DATAOUT[0] == 0x13) { //Lead III lead |= (DATAOUT[1] << 16); lead |= (DATAOUT[2] << 8); lead |= DATAOUT[3]; ecgFlag = true; } else if (DATAOUT[0] == 27) { //Resp mag. lead |= (DATAOUT[1] << 16); lead |= (DATAOUT[2] << 8); lead |= DATAOUT[3]; respFlag = true; } else if (DATAOUT[0] == 0x1D) { //LOFF int mask = 0xF0; int tmp = (DATAOUT[1] & mask) >> 4; if (tmp == 0x00) { //Leads connected lead = 0; } else { //Leads disconnected lead = 1; } loffFlag = true; } if (ecgFlag) { frame.append(ecgVoltageConversion(lead, leadMode)); //frame.append(filterEcgVal(ecgVoltageConversion(lead))); } else if (respFlag) { frame.append(filterVal(respVoltageConversion(lead))); } else if (loffFlag) { if (lead == 0) { //Leads connected frame.append(0.0); } else { //Leads disconnected frame.append(1.0); } } ecgFlag = false; respFlag = false; loffFlag = false; counter++; }
int get(uint8_t DIS_num, uint8_t cmd) { uint8_t curr_total_att = ATT_TOTAL; uint8_t curr_a5_att = ATT_A5; uint8_t curr_got_a5_att = ATT_GOT_A5; char read_tmp_buf[7]; char dummy_byte = 0x55; char tmp = 0x55; uint8_t success = 0; // Стандартный стартовый байт write_packet.write_pos.start_byte = 0xAA; // Запишем команду write_packet.write_pos.cmd_number = cmd; // Данные - нули for (uint8_t i = 0; i < 4; i++) { write_packet.write_pos.data[i] = 0x00; } // Считаем CRC write_packet.write_pos.crc = Crc8(write_packet.write_frame, 6); // Начинаем пробовать получить 0хА5 while (curr_total_att) { curr_total_att--; // printf("Chip select... \n"); chipSelect(DIS_num); // printf("Done Chip select... \n"); bcm2835_spi_transfernb((char*) write_packet.write_frame, read_tmp_buf, 7); // printf("Transferred cmd... \n"); // ждем получения 0хА5 while (curr_a5_att) { curr_a5_att--; bcm2835_spi_transfernb(&dummy_byte, &tmp, 1); bcm2835_delay(1); // если получили, то перестаем ждать if (0xA5 == tmp) { break; } } // если был получен А5 if (tmp == 0xA5) { // printf("Got 0xA5 \n"); // ждем пока пройдут все A5 while (((tmp == 0xA5) || (tmp == 0x55)) && curr_got_a5_att) { curr_got_a5_att--; bcm2835_spi_transfernb(&dummy_byte, &tmp, 1); } // получаем сами данные if (curr_got_a5_att) { // printf("Got data \n"); bcm2835_spi_transfernb(&dummy_byte, (char*) read_packet.read_frame, 5); success = 1; chipSelect(DIS_NONE); break; } curr_got_a5_att = ATT_GOT_A5; } else { curr_a5_att = ATT_A5; } chipSelect(DIS_NONE); } if (success == 1) { // скопируем в буфер данные for(uint8_t i = 0; i < 4; i++){ usr_data_buf_ptr[i] = read_packet.read_pos.data[i]; read_packet.read_pos.data[i] = 0; } return 0; } else { printf("Failed to get \n"); return 1; } }
void SpiPlateformImplementation::writeReadMult( const uint8_t *send , uint8_t *rec , uint32_t length) { #ifdef TARGET_RASPBERRY_PI bcm2835_spi_transfernb( (char*) send ,(char*) rec , length); #endif }
/** *@brief Reads an array of data from the SPI bus *@param sArray Buffer of data to send *@param rArray Buffer of data to send *@param length Length of data buffer *@return none */ void SPI_Read_Array(char* sArray, char* rArray, char length) { bcm2835_spi_transfernb(sArray,rArray,length); }
void magnet_thread(void) { printf("Magnet thread started.\n"); bcm2835_spi_begin(); bcm2835_gpio_fsel(D6, BCM2835_GPIO_FSEL_INPT); // ATTENTION: If there's a problem in reading encoder data (the D6 and/or D5 Pins), this is the issue. Can be solved by changing the library. bcm2835_gpio_fsel(D5, BCM2835_GPIO_FSEL_INPT); bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_32); // 32 = 7.8125 MHz, 128 = 1.95 Mhz //bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default //bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default /* // the sensor can be configured without the eeprom by writing data into it's registers using the following code char write_buf[] = {0xD2, 0x0B, 0x00}; // Write data: first byte is op code (D2 for Register write), second is address third is data bcm2835_gpio_write(PA0, LOW); bcm2835_spi_transfern(write_buf, sizeof(write_buf)); bcm2835_gpio_write(PA0, HIGH); // repeat the above code for settting up values of all the registers */ while(start == 0) {} bcm2835_gpio_fsel(D6, BCM2835_GPIO_FSEL_INPT); // ATTENTION: If there's a problem in reading encoder data (the D6 and/or D5 Pins), this is the issue. Can be solved by changing the library. bcm2835_gpio_fsel(D5, BCM2835_GPIO_FSEL_INPT); while(1) { if(sample_magnet) { //code for obtaining value from iC-MU char mag_buf[] = {0xF5, 0x00}; // Data to send: first byte is op code, rest depends on the opcode char mag_in[] = {0x00, 0x00}; bcm2835_gpio_write(PA0, LOW); bcm2835_spi_transfernb(mag_buf, mag_in, sizeof(mag_in)); bcm2835_gpio_write(PA0, HIGH); // printf("OUT at %X is %X\n", mag_buf[0], mag_buf[1]); // printf("IN at %X is %X\n", mag_in[0], mag_in[1]); if(mag_in[1] == 0x80) printf(""); else printf("DATA NOT VALID\n"); char status[] = {0xA6}; char status_in[] = {0x00, 0x00, 0x00}; bcm2835_gpio_write(PA0, LOW); bcm2835_spi_transfernb(status, status_in, sizeof(status_in)); bcm2835_gpio_write(PA0, HIGH); // printf("OUT at %X is %X\n", status[0], status[1]); // printf("status is %X and data is %X, %X, %X, %X, %X, %X\n", status_in[0], status_in[1], status_in[2], status_in[3], status_in[4], status_in[5], status_in[6]); float mag_reading = (256.0 * status_in[1]) + status_in[2]; // code for calculating xd float mag_position = (360.0 * mag_reading) / 65536.0; // printf("mag: Reading: %f, angle: %f, read: %X, %X\n", mag_reading, mag_position, status_in[1], status_in[2]); // xd = pow(sin(mag_reading),-0.5); xd = mag_position / 2.0; // xd = 100.0; //bcm2835_gpio_write(MAG_PIN, LOW); // reset sampling flag sample_magnet = 0; //set magnet flag high magnet_flag = 1; } } // bcm2835_spi_end(); }