int main(int argc, char *argv[]) { int ret = 0; int fd; parse_opts(argc, argv); fd = open(device, O_RDWR); if (fd < 0) pabort("can't open device"); /* * spi mode */ printf("mode=%d\n",mode); // mode = 3; ret = ioctl(fd, SPI_IOC_WR_MODE, &mode); if (ret == -1) pabort("can't set spi mode"); ret = ioctl(fd, SPI_IOC_RD_MODE, &mode); if (ret == -1) pabort("can't get spi mode"); /* * bits per word */ ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits); if (ret == -1) pabort("can't set bits per word"); ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits); if (ret == -1) pabort("can't get bits per word"); /* * max speed hz */ speed = 8000000; ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't set max speed hz"); ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't get max speed hz"); printf("spi mode: %d\n", mode); printf("bits per word: %d\n", bits); printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000); while(1) { // sleep(1); spi_write_read(fd); // printf("\n"); } close(fd); return ret; }
void get_data(uint8_t * data) { NRF_CS_LOW; spi_transfer(R_RX_PAYLOAD); spi_write_read(data, 2); NRF_CS_HIGH; write_register(STATUS, (1<<RX_DR)); }
uint16_t adc_sample(int fd, uint8_t channel) { uint8_t buf[3]; buf[0] = (1<<4) | (8 | (channel & 7)); buf[1] = 0; buf[2] = 0; spi_write_read(fd, buf, 3); return (((buf[1] & 63) << 8) | buf[2]) >> 2; }
//Read the raw data from the ltc6811 cell voltage register void ltc6811_rdcv_reg(uint8_t reg, //Determines which cell voltage register is read back uint8_t total_ic, //the number of ICs in the uint8_t *data //An array of the unparsed cell codes ) { const uint8_t REG_LEN = 8; //number of bytes in each ICs register + 2 bytes for the PEC uint8_t cmd[4]; uint16_t cmd_pec; if (reg == 1) //1: RDCVA { cmd[1] = 0x04; cmd[0] = 0x00; } else if (reg == 2) //2: RDCVB { cmd[1] = 0x06; cmd[0] = 0x00; } else if (reg == 3) //3: RDCVC { cmd[1] = 0x08; cmd[0] = 0x00; } else if (reg == 4) //4: RDCVD { cmd[1] = 0x0A; cmd[0] = 0x00; } else if (reg == 5) //4: RDCVE { cmd[1] = 0x09; cmd[0] = 0x00; } else if (reg == 6) //4: RDCVF { cmd[1] = 0x0B; cmd[0] = 0x00; } cmd_pec = pec15_calc(2, cmd); cmd[2] = (uint8_t)(cmd_pec >> 8); cmd[3] = (uint8_t)(cmd_pec); output_low(LTC6811_CS); spi_write_read(cmd,4,data,(REG_LEN*total_ic)); output_high(LTC6811_CS); }
/* The function reads a single GPIO voltage register and stores thre read data in the *data point as a byte array. This function is rarely used outside of the ltc6811_rdaux() command. */ void ltc6811_rdaux_reg(uint8_t reg, //Determines which GPIO voltage register is read back uint8_t total_ic, //The number of ICs in the system uint8_t *data //Array of the unparsed auxiliary codes ) { const uint8_t REG_LEN = 8; // number of bytes in the register + 2 bytes for the PEC uint8_t cmd[4]; uint16_t cmd_pec; if (reg == 1) //Read back auxiliary group A { cmd[1] = 0x0C; cmd[0] = 0x00; } else if (reg == 2) //Read back auxiliary group B { cmd[1] = 0x0e; cmd[0] = 0x00; } else if (reg == 3) //Read back auxiliary group B { cmd[1] = 0x0D; cmd[0] = 0x00; } else if (reg == 4) //Read back auxiliary group B { cmd[1] = 0x0F; cmd[0] = 0x00; } else //Read back auxiliary group A { cmd[1] = 0x0C; cmd[0] = 0x00; } cmd_pec = pec15_calc(2, cmd); cmd[2] = (uint8_t)(cmd_pec >> 8); cmd[3] = (uint8_t)(cmd_pec); //wakeup_idle (); //This will guarantee that the ltc6811 isoSPI port is awake, this command can be removed. output_low(LTC6811_CS); spi_write_read(cmd,4,data,(REG_LEN*total_ic)); output_high(LTC6811_CS); }
//brief Function that reads configuration of LTC6803-1/-3 int8_t LTC6803_rdcfg(uint8_t total_ic, //Number of ICs in the system uint8_t r_config[][7] //A two dimensional array that the function stores the read configuration data. ) { uint8_t BYTES_IN_REG = 7; uint8_t cmd[2]; uint8_t *rx_data; int8_t pec_error = 0; uint8_t data_pec; uint8_t received_pec; rx_data = (uint8_t *) malloc((BYTES_IN_REG*total_ic)*sizeof(uint8_t)); //1 cmd[0] = 0x02; cmd[1] = 0xCE; output_low(LTC6803_CS); spi_write_read(cmd, 2, rx_data, (BYTES_IN_REG*total_ic)); //Read the configuration data of all ICs on the daisy chain into output_high(LTC6803_CS); //rx_data[] array for (uint8_t current_ic = 0; current_ic < total_ic; current_ic++) //executes for each LTC6803 in the daisy chain and packs the data { //into the r_config array as well as check the received Config data //for any bit errors //4.a for (uint8_t current_byte = 0; current_byte < BYTES_IN_REG; current_byte++) { r_config[current_ic][current_byte] = rx_data[current_byte + (current_ic*BYTES_IN_REG)]; } //4.b received_pec = r_config[current_ic][6]; data_pec = pec8_calc(6, &r_config[current_ic][0]); if (received_pec != data_pec) { pec_error = -1; } } free(rx_data); //5 return(pec_error); }