//Si4734 i2C functions //Roger Traylor 11.13.2011 //device driver for the si4734 chip. // header files #include #include #include #include #include #include #include "twi_master.h" //my defines for TWCR_START, STOP, RACK, RNACK, SEND #include "si4734.h" uint8_t si4734_wr_buf[9]; //buffer for holding data to send to the si4734 uint8_t si4734_rd_buf[15]; //buffer for holding data recieved from the si4734 uint8_t si4734_tune_status_buf[8]; //buffer for holding tune_status data enum radio_band{FM, AM, SW}; extern volatile enum radio_band current_radio_band; extern uint16_t eeprom_fm_freq; extern uint16_t eeprom_am_freq; extern uint16_t eeprom_sw_freq; extern uint8_t eeprom_volume; extern uint16_t current_fm_freq; extern uint16_t current_am_freq; extern uint16_t current_sw_freq; extern uint8_t current_volume; //******************************************************************************** // get_int_status() // uint8_t get_int_status(){ //send get_int_status command si4734_wr_buf[0] = 0x14; twi_start_wr(SI4734_ADDRESS, si4734_wr_buf, 1); _delay_ms(5); //get the interrupt status twi_start_rd(SI4734_ADDRESS, si4734_rd_buf, 1); return(si4734_rd_buf[0]); } //******************************************************************************** //******************************************************************************** // fm_tune_freq() // //takes current_fm_freq and sends it to the radio chip // void fm_tune_freq(){ si4734_wr_buf[0] = 0x20; //fm tune command si4734_wr_buf[1] = 0x00; //no FREEZE and no FAST tune si4734_wr_buf[2] = (uint8_t)(current_fm_freq >> 8); //freq high byte si4734_wr_buf[3] = (uint8_t)(current_fm_freq); //freq low byte si4734_wr_buf[4] = 0x00; //antenna tuning capactior //send fm tune command twi_start_wr(SI4734_ADDRESS, si4734_wr_buf, 5); _delay_ms(80); //get the interrupt status // return(get_int_status()); // return(1); } //******************************************************************************** //******************************************************************************** // am_tune_freq() // //takes current_am_freq and sends it to the radio chip // void am_tune_freq(){ si4734_wr_buf[0] = 0x40; //am tune command si4734_wr_buf[1] = 0x00; //no FAST tune si4734_wr_buf[2] = (uint8_t)(current_am_freq >> 8); //freq high byte si4734_wr_buf[3] = (uint8_t)(current_am_freq); //freq low byte si4734_wr_buf[4] = 0x00; //antenna tuning capactior high byte si4734_wr_buf[5] = 0x00; //antenna tuning capactior low byte //send fm tune command twi_start_wr(SI4734_ADDRESS, si4734_wr_buf, 6); _delay_ms(80); //get the interrupt status // return(get_int_status()); // return(1); } //******************************************************************************** //******************************************************************************** // sw_tune_freq() // //takes current_sw_freq and sends it to the radio chip //antcap low byte is 0x01 as per datasheet void sw_tune_freq(){ si4734_wr_buf[0] = 0x40; //am tune command si4734_wr_buf[1] = 0x00; //no FAST tune si4734_wr_buf[2] = (uint8_t)(current_sw_freq >> 8); //freq high byte si4734_wr_buf[3] = (uint8_t)(current_sw_freq); //freq low byte si4734_wr_buf[4] = 0x00; //antenna tuning capactior high byte si4734_wr_buf[5] = 0x01; //antenna tuning capactior low byte //send am tune command twi_start_wr(SI4734_ADDRESS, si4734_wr_buf, 6); _delay_ms(80); //get the interrupt status // return(get_int_status()); // return(1); } //******************************************************************************** // fm_pwr_up() // void fm_pwr_up(){ //restore the previous fm frequency current_fm_freq = eeprom_read_word(&eeprom_fm_freq); //TODO: only this one does not work current_volume = eeprom_read_byte(&eeprom_volume); //TODO: only this one does not work //send fm power up command si4734_wr_buf[0] = 0x01; si4734_wr_buf[1] = 0x50; //GPO2OEN and XOSCEN selected si4734_wr_buf[2] = 0x05; //analog audio outputs twi_start_wr(SI4734_ADDRESS, si4734_wr_buf, 3); _delay_ms(120); //startup delay as specified set_property(GPO_IEN, (1<> 8); //property high byte si4734_wr_buf[3] = (uint8_t)(property); //property low byte si4734_wr_buf[4] = (uint8_t)(property_value >> 8); //property value high byte si4734_wr_buf[5] = (uint8_t)(property_value); //property value low byte twi_start_wr(SI4734_ADDRESS, si4734_wr_buf, 6); _delay_ms(10); //set properties takes 10ms to complete }//set_property()