/* write_7366(): Writes bytes in array "bytearray" to register "reg". The number of bytes written depends on the reister and mode. Config registers will write 1 byte. Other registers will write the number of bytes specified by COUNTER_BYTES */ void write_7366(int module, int reg,unsigned char *bytearray) { unsigned char ir = (0x2 << 6 ) | (reg << 3); //Instruction unsigned char ReadData; if ( (reg == MDR0) || (reg == MDR1) || (reg == STR) ) { //One byte to write ss_low(module); delay(); //Setup time SpiChnPutC(SPICHN, ir); //Write instruction after TX buffer empty while(SpiChnRxBuffFull(SPICHN)==0); //Wait for RX buffer full ReadData = SpiChnGetC(SPICHN); //Read what was clocked in during last write (nothing) SpiChnPutC(SPICHN, bytearray[0]); //Clock out write byte after TX buffer empty while(SpiChnRxBuffFull(SPICHN)==0); //Wait for RX buffer full ReadData = SpiChnGetC(SPICHN); //Read what was clocked in during last write (garbage /dont care) ss_high(module); //End comm return; } if ( (reg == DTR) || (reg == CNTR) || (reg == OTR) ) { //1-4 bytes to read ss_low(module); delay(); //Setup time SpiChnPutC(SPICHN, ir); //Write instruction after TX buffer empty while(SpiChnRxBuffFull(SPICHN)==0); //Wait for RX buffer full ReadData = SpiChnGetC(SPICHN); //Read what was clocked in during last write (nothing) //Do reads int i; for (i=0;i<COUNTER_BYTES;i++) { SpiChnPutC(SPICHN, bytearray[i]); //Clock out byte after TX buffer empty while(SpiChnRxBuffFull(SPICHN)==0); //Wait for RX buffer full ReadData = SpiChnGetC(SPICHN); //Read what was clocked in during last write (don't care) } ss_high(module); //End comm return; } }
/************************************************** * Function name : * Created by : * Date created : * Description : * Notes : ***************************************************/ void read_callback(U8 *Rx_buffer, U8 length) { ss_high(); __delay_cycles(1000); ss_low(); SPI_transfer (0, read_packet.read_frame, 7, control_callback); }
/* clear_reg_7366(): Clears the given register */ void clear_reg_7366(int module, int reg) { char ReadData; char ir = (reg << 3); //Instruction ss_low(module); delay(); //Setup time SpiChnPutC(SPICHN, ir); //Write instruction after TX buffer empty while(SpiChnRxBuffFull(SPICHN)==0); //Wait for RX buffer full ReadData = SpiChnGetC(SPICHN); //Read what was clocked in during last write (nothing) ss_high(module); }
void start_conversion() { uint16_t REG = 0; //Steg 2 conversion ss_low(); send_spi(START_CONVERSION); REG = get_spi(0xFF); ss_high(); if (REG & (1 << 15)) start_conversion(); REG = 0; }
void deactivate_adc() { uint16_t REG = 0; //Avaktivera adc omvandlingen ss_low(); send_spi(DEACTIVATE_ADC); REG = get_spi(0xFF); ss_high(); if (REG & (1 << 15)) deactivate_adc(); REG = 0; }
void activate_adc() { uint16_t REG = 0; //Steg 1 sätt ADC till aktiv mode ss_low(); send_spi(ACTIVATE_ADC); REG = get_spi(0xFF); ss_high(); if (REG & (1 << 15)) activate_adc(); REG = 0; _delay_us(200); }
/************************************************** * Function name : * Created by : * Date created : * Description : * Notes : ***************************************************/ void sensor_read (U8 command) { DIS_command = command; write_packet.write_pos.start_byte = 0xAA; write_packet.write_pos.cmd_number = command; write_packet.write_pos.CRC = Crc8(write_packet.write_frame, 6); for(U8 i = 0; i < 8; i++) { read_packet.read_frame[i] = 0x00; } ss_low(); SPI_transfer (write_packet.write_frame, read_packet.read_frame, 7, read_callback); }
uint16_t get_angular_rate() { uint16_t REG = 0; uint16_t DATA = 0; //Steg 3 poll ss_low(); send_spi(POLL); _delay_us(200); //if (REG & (1 << 15)) get_angular_rate(); REG = get_spi(0xFF); ss_high(); REG = REG >> 1; DATA = REG & 0x07FF; return DATA; }
uint8_t spi_send_receive(uint8_t send_data, uint8_t* received_data, uint8_t chipselect){ switch(state.send_receive){ case IDLE: ss_high(chipselect); ss_low(chipselect); SPDR = send_data; state.send_receive = SENDING; break; case SENDING: if(!(SPSR & (1<<SPIF))){ state.send_receive = RECEIVING; } break; case RECEIVING: *received_data = SPDR; ss_high(chipselect); state.send_receive = IDLE; break; } return state.send_receive; }
/************************************************** * Function name : * Created by : * Date created : * Description : * Notes : ***************************************************/ void control_callback(U8 *Rx_buffer, U8 length) { ss_high(); configuration_flag = 0x01; if(read_packet.read_pos.control_byte != 0xA5) { ss_high(); __delay_cycles(1000); ss_low(); SPI_transfer (0, read_packet.read_frame, 7, control_callback); } // if(read_packet.read_pos.control_byte == 0x00) // { // error_cnt++; // if(error_cnt < ERROR_CNT) // { // SPI_transfer (0, read_packet.read_frame, 7, control_callback); // } // else // { // error_cnt = 0x00; // ss_high(); // sensor_union[DIS_number].sensor_data.status = ERROR_CONTROL_BYTE; // sensor_cb(); // } // } // else // { // if(read_packet.read_pos.control_byte == 0xA5) // { // error_cnt = 0x00; // ss_high(); // sensor_union[DIS_number].sensor_data.status = OK; // sensor_cb(); // } // else // { // if(read_packet.read_pos.status_byte == 0xA5) // { // error_cnt = 0x00; // ss_high(); // for(U8 i=0; i <7; i++) // { // read_packet.read_frame[i] = read_packet.read_frame[i + 1]; // } // sensor_union[DIS_number].sensor_data.status = OK; // sensor_cb(); // } // else // { // error_cnt++; // if(error_cnt < ERROR_CNT) // { // ss_high(); // ss_low(); // SPI_transfer (write_packet.write_frame, read_packet.read_frame, 7, read_callback); // } // else // { // error_cnt = 0x00; // ss_high(); // sensor_union[DIS_number].sensor_data.status = ERROR_CONTROL_BYTE; // sensor_cb(); // } // } // } // } }