void softReset(){
	char buf[] = { 0xFF, 0xFF, 0xFF, 0xFF}; //reset by send 32 times 1:
	bcm2835_spi_transfern(buf, sizeof(buf));
    //printf("reset: %02X %02X %02X %02X \n", buf[0], buf[1], buf[2], buf[3]);
	
	//reconfigure
	char bufConfig[] = { 0x10, 0x00 + gainSetting, 0x10 + currentInput}; //select comunication reg for write,  write config to comunication reg
	bcm2835_spi_transfern(bufConfig, sizeof(bufConfig));
	
}
long readSingleValue(){
	char bufDataRead[] = { 0x58 }; //select Data reg for read
	bcm2835_spi_transfern(bufDataRead, sizeof(bufDataRead));
	char bufDataValue[] = { 0x00, 0x00, 0x00}; //read Data
	bcm2835_spi_transfern(bufDataValue, sizeof(bufDataValue));
	
	long fullData = bufDataValue[0];
	fullData = (fullData << 8) | bufDataValue[1];
	fullData = (fullData << 8) | bufDataValue[2];
	return fullData;
}
//this one is slow and unsave....
long readMultiValueContinuousAVG(int amount, int delayMilis){
	long value = 0;
	char bufDataRead[] = { 0x5C }; //select Data reg for continous read 
	bcm2835_spi_transfern(bufDataRead, sizeof(bufDataRead));
	
	int valuesRead=0;
	int emptyCount=0;
	long fullData=0;
	int rdy=1;
	while (emptyCount<20 && valuesRead<amount) {
		char buf[] = { 0x00 };
		bcm2835_spi_transfern(buf, 1);
		
		if (buf[0] == 0xFF){
			delay(100); //Data not yet available, wait 100ms
			++emptyCount;
		} else {
			fullData = buf[0];
			
			char buf[] = { 0x00, 0x00 };
			bcm2835_spi_transfern(buf, 2);
			
			fullData = (fullData << 8) | buf[0];
			fullData = (fullData << 8) | buf[1];
			if (fullData != 0){ //it's not possible to be value 0
				value += fullData;
				valuesRead++;
			} else {
				++emptyCount;
			}
		}
		
		/*if (valuesRead>0){
			printf("Value at input %d: %d / %06X sum %d avg %d\n", currentInput, fullData, fullData, value, value / valuesRead);
		} else {
			printf("Value at input %d: %d / %06X sum %d\n", currentInput, fullData, fullData, value);
		}*/
		if (delayMilis>0){
			delay(delayMilis);
		}
	}
	
	char bufDataEndRead[] = { 0x58, 0x00, 0x00, 0x00 }; //stop continous read -> select Data reg for read   and read 3 bytes(to be sure it's back in command promt mode.)
	bcm2835_spi_transfern(bufDataEndRead, sizeof(bufDataEndRead));
	
	if (valuesRead == 0){
		return -1;
	} else {
		return value / valuesRead;
	}
}
Пример #4
0
uint8_t ReadRawRC(uint8_t Address)
{
	char buff[2];
	buff[0] = ((Address<<1)&0x7E)|0x80;
	bcm2835_spi_transfern(buff,2);
	return (uint8_t)buff[1];
}
/*!
 * Start sending frames
 */
void EcgCapture::start()
{
    enableRegisterWrite();

    char FRAMES1[] = {0x40,0x00,0x00,0x00};
    bcm2835_spi_transfern(FRAMES1,sizeof(FRAMES1));
}
Пример #6
0
void LEDMatrix::Display() const {
	//The APA102 LEDs require 1 additional end frame per 64 LEDs
	int endFrameCount = 1 + colors.size() / 64;

	int byteSize = 4*(1 + colors.size() + endFrameCount);

	char *outBuffer = new char[byteSize];

	//Initialize start frame
	outBuffer[0] = 0;
	outBuffer[1] = 0;
	outBuffer[2] = 0;
	outBuffer[3] = 0;

	//Pack the pixel frames
	for(int i = 0; i < height; i++) {
		int offset = i*width, start, end, inc;

		//If odd-numbered row
		if(i % 2) {
			start = width - 1;
			end = -1;
			inc = -1;
		}
		//If even-numbered row
		else {
			start = 0;
			end = width;
			inc = 1;
		}

		for(int j = start, buffPtr = 4 + 4*offset; j != end; j += inc, buffPtr += 4) {
			outBuffer[buffPtr] = 0xFF;

			outBuffer[buffPtr+1] = colors[offset + j].GetBlue();
			outBuffer[buffPtr+2] = colors[offset + j].GetGreen();
			outBuffer[buffPtr+3] = colors[offset + j].GetRed();
		}
	}

	//Pack the end frames
	for(int i = 0, offset = 4 + 4*width*height; i < endFrameCount; i++, offset += 4) {
		outBuffer[offset] = 0xFF;
		outBuffer[offset+1] = 0x00;
		outBuffer[offset+2] = 0x00;
		outBuffer[offset+3] = 0x00;
	}
/*
	for(int i = 0; i < byteSize; i+= 4) {
		if(!(i % (4*width)))
			std::cout << std::endl;

		std::cout << std::hex << std::setw(2) << (int)((unsigned char)outBuffer[i+4]) << std::hex << std::setw(2) << (int)((unsigned char)outBuffer[i+5]);
		std::cout << std::hex << std::setw(2) << (int)((unsigned char)outBuffer[i+6]) << std::hex << std::setw(2) << (int)((unsigned char)outBuffer[i+7]) << "  ";
	}
*/
	bcm2835_spi_transfern(outBuffer, byteSize);

	delete[] outBuffer;
}
/*
	Name: spi_transmit
	Description: Send data over SPI, and receive at the same time. Received data is put into transmitted data buffer.
	Parameters:
		1. devsel : int - selects which device to communicate - 0=touch panel , 1=lcd
		2. data : pointer(array) uint8_t - data to be sent, and buffer for data to be received
		3. len : int - bytes count to be sent from buffeer
	Returns:
		0 - on success, negative - fail, see function content for error return values. Also check errno for more information.
		positive - number of bytes received
		
*/
int spi_transmit(int devsel, uint8_t *data, int len) {

	if (devsel == 0) {
		bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
		#ifdef _DEBUG_
			fprintf(stdout, "spi_transmit.CS=CS0 , ");
		#endif
	} else {
		bcm2835_spi_chipSelect(BCM2835_SPI_CS1);
		#ifdef _DEBUG_
			fprintf(stdout, "spi_transmit.CS=CS1 , ");
		#endif
	}
	
	#ifdef _DEBUG_
		fprintf(stdout, "data[%i]=",len);
		for(int i=0;i<len;i++) {
			uint8_t t = *(data+i);
			fprintf(stdout,"%02X ",t);
		}
		fprintf(stdout, "\n");
	#endif

	
	bcm2835_spi_transfern((char*)data, len);
	
	return len;
}
Пример #8
0
unsigned char ee_read(unsigned int BufferOffset)
{
   unsigned char temp;
   unsigned char temp2[1]={0xff};
	
	bcm2835_gpio_write(PIN_CS, LOW);  /*must be called*/
   delay_ms(500);
   bcm2835_spi_transfer(0xD4);
	delay_ms(50);
   bcm2835_spi_transfer(0xff);
	delay_ms(50);
   bcm2835_spi_transfer((unsigned char)(BufferOffset>>8));
	delay_ms(50);
   bcm2835_spi_transfer((unsigned char)BufferOffset);
	delay_ms(50);
   bcm2835_spi_transfer(0xff);
	delay_ms(50);

    bcm2835_spi_transfern(temp2,1); 
   
    delay_ms(50);
    bcm2835_gpio_write(PIN_CS, HIGH);  /*must be called*/
    delay_ms(300);

    return temp2[0];
}
Пример #9
0
void WriteRawRC(uint8_t Address, uint8_t value)
{
	char buff[2];

	buff[0] = (char)((Address<<1)&0x7E);
	buff[1] = (char)value;
	bcm2835_spi_transfern(buff,2);
}
int readStatus(){
	int currentStatus = 0;

	char buf[] = { 0x40, 0x00}; //select status reg for read, read status
	bcm2835_spi_transfern(buf, sizeof(buf));
	
	currentStatus = buf[1];
	return currentStatus;
}
void changeGain(int gain){
	if (gainSetting != gain){
		currentInput = gain;
		
		//reconfigure
		char bufConfig[] = { 0x10, 0x00 + gainSetting, 0x10 + currentInput}; //select comunication reg for write,  write config to comunication reg
		bcm2835_spi_transfern(bufConfig, sizeof(bufConfig));
	}
}
int readMode(){
	int fullMode = 0;

	char buf[] = { 0x48, 0x00, 0x00}; //select Mode reg for read, read Mode
	bcm2835_spi_transfern(buf, sizeof(buf));

	fullMode = buf[1];
	fullMode = (fullMode << 8) | buf[2];
	return fullMode;
}
void changeInput(int input){
	if (currentInput != input){
		currentInput = input;
		
		//reconfigure
		char bufConfig[] = { 0x10, 0x00 + gainSetting, 0x10 + currentInput}; //select comunication reg for write,  write config to comunication reg
		bcm2835_spi_transfern(bufConfig, sizeof(bufConfig));
		//printf("now on input %d\n", currentInput);
	}
}
Пример #14
0
int main(int argc, char **argv)
{
    if (!bcm2835_init())
        return 1;

    char buffer[4];
    buffer[0] = 50;
    int i,temp;

    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE3);                    //SCLK rising edge - clock idle state 1
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536);  //set clock frequency
    bcm2835_spi_chipSelect(BCM2835_SPI_CS1);                       //use chip select 1
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS1, LOW);       //chip select 0 to activate


    buffer[0]=buffer[1]=buffer[2]=buffer[3]=0;
    //bcm2835_spi_transfern(buffer,4);

    buffer[0] = 0x58;       //read the id
    bcm2835_spi_transfern(buffer,2);
    printf("id:%02X\n",buffer[1]);
    
    while(1)
    {
        buffer[0] = 0x50;       //read the temp
        bcm2835_spi_transfern(buffer,3);
        printf("status %02X %02X\n",buffer[1],buffer[2]);
        temp = buffer[1]; 
        temp = temp<<8;
        temp = temp + ( buffer[2] & 0xF8);
        printf("status %08x\n",temp);
        temp = temp>>3;
        temp = temp/16;
        printf("temp:%d\n",temp);
        sleep(1);
    }

    bcm2835_spi_end();
    bcm2835_close();
    return 0;
}
int readConfig(){
	int fullConfig = 0;

	char buf[] = { 0x50, 0x00, 0x00}; //select configuration reg for read, read configuration
	bcm2835_spi_transfern(buf, sizeof(buf));

	fullConfig = buf[1];
	fullConfig = (fullConfig << 8) | buf[2];
	return fullConfig;
}
/*!
 * Method to set a control register, returns true on success or false on failure.
 */
bool EcgCapture::setReg(QByteArray writeCommand)
{
    //Validate the input
    if (writeCommand.size() != 4)
        return 0;

    enableRegisterWrite();
    bcm2835_spi_transfern(writeCommand.data(), writeCommand.size());

    return 1;
}
Пример #17
0
char MPU9250::WriteReg( uint8_t WriteAddr,char WriteData )
{
  unsigned int recieved;

  char buff[2];
  buff[0] = WriteAddr;
  buff[1] = WriteData;
  bcm2835_spi_chipSelect(MPU9250_PIN);                      // The default
  bcm2835_spi_transfern(buff,2);
  bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE);
  return buff[1];
}
Пример #18
0
void set_V(char *msb)
{
    char mosi[3] = {0};

    mosi[0] = 0b00010000 | (msb[0] & 0b00001111);
    mosi[1] = msb[1];
    mosi[2] = msb[2];

    bcm2835_gpio_write(GPIO12, HIGH);
    bcm2835_gpio_write(GPIO12, LOW);
    bcm2835_spi_transfern(mosi, 3);
    bcm2835_gpio_write(GPIO12, HIGH);
}
long readMultiValueAVG(int amount, int delayMilis){
	long value = 0;
	
	int valuesRead=0;
	int emptyCount=0;
	long fullData=0;
	while (emptyCount<20 && valuesRead<amount) {
		char bufDataRead[] = { 0x58 }; //select Data reg for read
		bcm2835_spi_transfern(bufDataRead, sizeof(bufDataRead));
		char bufDataValue[] = { 0x00, 0x00, 0x00}; //read Data
		bcm2835_spi_transfern(bufDataValue, sizeof(bufDataValue));
		
		fullData = bufDataValue[0];
		fullData = (fullData << 8) | bufDataValue[1];
		fullData = (fullData << 8) | bufDataValue[2];
		if (fullData>0){
			value +=fullData;
			++valuesRead;
		} else {
			++emptyCount;
		}
		/*if (valuesRead>0){
			printf("Value at input %d: %d sum %d avg %d\n", currentInput, fullData, value, value / valuesRead);
		} else {
			printf("Value at input %d: %d sum %d\n", currentInput, fullData, value);
		}*/
		if (delayMilis>0){
			delay(delayMilis);
		}
	}
	
	if (valuesRead == 0){
		return -1;
	} else {
		return value / valuesRead;
	}
}
Пример #20
0
static PyObject *
PyBCM2835_spi_transfern(PyObject *self, PyObject *args)
{
	char *buf;
	int buf_len;
	uint32_t len;

	if (!PyArg_ParseTuple(args,"s#i",&buf, &buf_len,&len)) {
		return NULL;
	}

	bcm2835_spi_transfern(buf,len);

	Py_RETURN_NONE;
}
Пример #21
0
void bw_spi_dio_read_id(device_info_t *device_info) {
	char buf[BW_DIO_ID_STRING_LENGTH + 1];
	int i = 0;
	for (i = 0; i <= BW_DIO_ID_STRING_LENGTH; i++) {
		buf[i] = '\0';
	}

	buf[0] = device_info->slave_address | 1;
	buf[1] = BW_PORT_READ_ID_STRING;

	dio_spi_setup(device_info);
	bcm2835_spi_transfern(buf, BW_DIO_ID_STRING_LENGTH);
	uwait(BW_DIO_SPI_BYTE_WAIT_US);
	printf("[%s]\n", &buf[1]);
}
Пример #22
0
void init_DAC()
{
    char mosi[3] = {0};

    //RESET
    mosi[0] = 0b01000000;
    mosi[1] = 0b00000000;
    mosi[2] = 0b00000100;
    bcm2835_gpio_write(GPIO12, HIGH);
    bcm2835_gpio_write(GPIO12, LOW);
    bcm2835_spi_transfern(mosi, 3);
    bcm2835_gpio_write(GPIO12, HIGH);

    usleep(1000);

    //write to control register
    mosi[0] = 0b00100000;
    mosi[1] = 0b00000000;
    mosi[2] = 0b00010010;
    bcm2835_gpio_write(GPIO12, HIGH);
    bcm2835_gpio_write(GPIO12, LOW);
    bcm2835_spi_transfern(mosi, 3);
    bcm2835_gpio_write(GPIO12, HIGH);
}
Пример #23
0
uint8_t SlushMotor::SPIXfer(uint8_t data) {
	char dataPacket[1];

	dataPacket[0] = (char) data;

	bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE);
	bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64);
	bcm2835_spi_setDataMode(BCM2835_SPI_MODE3);

	bcm2835_gpio_clr(m_nSpiChipSelect);
	bcm2835_spi_transfern(dataPacket, 1);
	bcm2835_gpio_set(m_nSpiChipSelect);

	return (uint8_t) dataPacket[0];
}
Пример #24
0
void MPU9250::ReadRegs( uint8_t ReadAddr, char *ReadBuf, unsigned int Bytes )
{

  char buff[1+Bytes];
  buff[0] = ReadAddr | READ_FLAG;
  for(int i = 1; i < 1+Bytes; i++)
    buff[i] = 0x00;
  bcm2835_spi_chipSelect(MPU9250_PIN);                      // The default
  bcm2835_spi_transfern(buff,1+Bytes);
  bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE);
  memcpy(ReadBuf,buff+1,Bytes);  


  bcm2835_delayMicroseconds(50);

}
Пример #25
0
Файл: spi.c Проект: sjhx/etrv
void spi_transfern(char *buf, uint32_t len) {
	int ret;
        //dump("TR tx", (uint8_t *)buf, len);
	
#ifdef BCM
	bcm2835_spi_transfern(buf, len);
#else
	uint8_t *rx = malloc(len);
	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)buf,
		.rx_buf = (unsigned long)rx,
		.len = len,
		.delay_usecs = delay,
		.speed_hz = speed,
		.bits_per_word = bits,
	};

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1) {
		free(rx);
		pabort("can't send spi message");
	}
	memcpy(buf, rx, len);
	free(rx);
#endif
}

void spi_writenb(char * buf, uint32_t len) {
	int ret;
	dump("WR tx", (uint8_t *)buf, len);
#ifdef BCM
	bcm2835_spi_writenb(buf, len);
#else
	struct spi_ioc_transfer tr = {
		.tx_buf = (unsigned long)buf,
		.rx_buf = (unsigned long)0,
		.len = len,
		.delay_usecs = delay,
		.speed_hz = speed,
		.bits_per_word = bits,
	};

	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
	if (ret < 1)
		pabort("can't send spi message");
#endif
}
Пример #26
0
void main(int argc, char* argv)
{
   /* Pour le debugging
      1 == Print sur la console les operations des fonctions 
      0 == Comportement normal du programme */
   bcm2835_set_debug(0);

   /* Initialisation de la librairie */
   if(bcm2835_close())
   {
      /* code d'initialisation bcm */
      printf("Version de la librairie : %d", bcm2835_version());

      if(bcm2835_spi_begin())
      {
         /* Initialisation de la SPI
            Voir header pour valeurs */
         bcm2835_spi_setClockDivider(CLK_SPEED);
         bcm2835_spi_setDataMode(PI_MODE);
         bcm2835_spi_setBitOrder(BIT_ORDER);
         /* Selecte un CS pour le transfert de donnees */
         bcm2835_spi_chipSelect(CS_SLAVE);

         /* Test de transfert avec retour */
         uint8_t ret;
         ret = bcm2835_spi_transfer(5);
         printf("Retour de l'Arduino : %d", ret);

         /* Test de transfert sans retour */
         bcm2835_spi_transfern("A", 1);

         bcm2835_spi_end();
      }
      else
      {
         printf("Erreur de l'init de SPI\n");
      }

      bcm2835_close(void); /* Libere la memoire et ferme la librairie */
   }
   else
   {
      printf("Echec de l'ouverture de la librairie\n");
   }
}
//void bcm2835_spi_transfern(char* buf, uint32_t len);
/// Call bcm2835_spi_transfern with 2 parameters
/// \par            Refer
/// \par            Modify
void ope_spi_transfern(void)
{
uint32_t len;
char *buf;
    get_int_code();
    get_int_code();
    buf = *(char **)(buff+1);
    len = *(uint32_t *)(buff+5);
//  printf("B n buf=%p len=%d \n",buf,len);
    bcm2835_spi_transfern( bi_send_buff, len );
    set_ope_code( OPE_SPI_TRANSFERN );
    set_int_code( (int)buf );
    set_int_code( len );
    strncpy( bi_rec_buff, bi_send_buff, 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();
}
Пример #28
0
uint8_t
wiring_write_then_read(uint8_t* out, uint16_t out_len, 
	               uint8_t* in, uint16_t in_len)
{
	uint8_t transfer_buf[out_len + in_len];
	unsigned int ret = 0;

	memset(transfer_buf, 0, out_len + in_len);
	
	if (NULL != out) {
		memcpy(transfer_buf, out, out_len);
		ret += out_len;
	}

	if (NULL != in) {
		ret += in_len;
	}

	bcm2835_spi_transfern((char*)transfer_buf, ret);

	memcpy(in, &transfer_buf[out_len], in_len);

	return ret;
}
Пример #29
0
void VM205::transfer(char* buf, uint32_t len) {
	bcm2835_spi_transfern(buf, len);
}
Пример #30
0
void SPI_Send(unsigned char *buffer, int size) {
	bcm2835_spi_transfern((char*)&buffer[0], size);
}