00001 /* 00002 00003 Copyright (c) 2010, Embedded Adventures, www.embeddedadventures.com 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions are met: 00008 00009 - Redistributions of source code must retain the above copyright notice, 00010 this list of conditions and the following disclaimer. 00011 00012 - Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions and the following disclaimer in the 00014 documentation and/or other materials provided with the distribution. 00015 00016 - Neither the name of Embedded Adventures nor the names of its contributors 00017 may be used to endorse or promote products derived from this software 00018 without specific prior written permission. 00019 00020 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00024 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 00030 THE POSSIBILITY OF SUCH DAMAGE. 00031 00032 Contact us at admin@embeddedadventures.com 00033 00034 */ 00035 00036 00037 #include "hmc6352.h" 00038 #include "i2c.h" 00039 00040 void hmc6352_write_eeprom(uns8 addr, uns8 data) { 00041 00042 // Send hmc6352 addr (write) 00043 // Send command w (write to eeprom) 00044 // Send eeprom addr 00045 // Send data 00046 00047 i2c_start(); 00048 i2c_send_byte(hmc6352_device_addr | hmc6352_write); 00049 i2c_send_byte(hmc6352_write_to_eeprom); 00050 i2c_send_byte(addr); 00051 i2c_send_byte(data); 00052 i2c_stop(); 00053 } 00054 00055 uns8 hmc6352_read_eeprom(uns8 addr) { 00056 00057 // Send hmc6352 addr (write) 00058 // Send command r (read from eeprom) 00059 // Send eeprom addr 00060 00061 // Send hmc6352 addr (read) 00062 // Read data 00063 00064 uns8 data; 00065 00066 i2c_start(); 00067 i2c_send_byte(hmc6352_device_addr | hmc6352_write); 00068 i2c_send_byte(hmc6352_read_from_eeprom); 00069 i2c_send_byte(addr); 00070 i2c_stop(); 00071 00072 i2c_start(); 00073 i2c_send_byte(hmc6352_device_addr | hmc6352_read); 00074 data = i2c_receive_byte(); 00075 i2c_send_ack(); 00076 i2c_stop(); 00077 00078 return data; 00079 } 00080 00081 00082 void hmc6352_write_ram(uns8 addr, uns8 data) { 00083 00084 // Send hmc6352 addr (write) 00085 // Send command G (write to ram) 00086 // Send ram addr 00087 // Send data 00088 00089 i2c_start(); 00090 i2c_send_byte(hmc6352_device_addr | hmc6352_write); 00091 i2c_send_byte(hmc6352_write_to_ram); 00092 i2c_send_byte(addr); 00093 i2c_send_byte(data); 00094 i2c_stop(); 00095 00096 } 00097 00098 00099 uns8 hmc6352_read_ram(uns8 addr) { 00100 00101 // Send hmc6352 addr (write) 00102 // Send command g (read from ram) 00103 // Send ram addr 00104 00105 // Send hmc6352 addr (read) 00106 // Read data 00107 00108 uns8 data; 00109 00110 i2c_start(); 00111 i2c_send_byte(hmc6352_device_addr | hmc6352_write); 00112 i2c_send_byte(hmc6352_read_from_ram); 00113 i2c_send_byte(addr); 00114 i2c_stop(); 00115 00116 i2c_start(); 00117 i2c_send_byte(hmc6352_device_addr | hmc6352_read); 00118 data = i2c_receive_byte(); 00119 i2c_send_ack(); 00120 i2c_stop(); 00121 00122 return data; 00123 } 00124 00125 void hmc6352_sleep() { 00126 00127 // Send hmc6352 addr (write) 00128 // Send command S (sleep) 00129 00130 i2c_start(); 00131 i2c_send_byte(hmc6352_device_addr | hmc6352_write); 00132 i2c_send_byte(hmc6352_sleep_cmd); 00133 i2c_stop(); 00134 } 00135 00136 00137 void hmc6352_wake() { 00138 00139 // Send hmc6352 addr (write) 00140 // Send command W (wake) 00141 00142 i2c_start(); 00143 i2c_send_byte(hmc6352_device_addr | hmc6352_write); 00144 i2c_send_byte(hmc6352_wake_cmd); 00145 i2c_stop(); 00146 00147 } //37208656 00148 00149 void hmc6352_update_bridge_offsets() { 00150 00151 // Send hmc6352 addr (write) 00152 // Send command O (update bridge offsets) 00153 00154 i2c_start(); 00155 i2c_send_byte(hmc6352_device_addr | hmc6352_write); 00156 i2c_send_byte(hmc6352_update_bridge_cmd); 00157 i2c_stop(); 00158 } 00159 00160 void hmc6352_enter_cal() { 00161 00162 // Send hmc6352 addr (write) 00163 // Send command C (Enter user calibration mode) 00164 00165 i2c_start(); 00166 i2c_send_byte(hmc6352_device_addr | hmc6352_write); 00167 i2c_send_byte(hmc6352_enter_cal_cmd); 00168 i2c_stop(); 00169 } 00170 00171 void hmc6352_exit_cal() { 00172 00173 // Send hmc6352 addr (write) 00174 // Send command E (Exit user calibration mode) 00175 00176 i2c_start(); 00177 i2c_send_byte(hmc6352_device_addr | hmc6352_write); 00178 i2c_send_byte(hmc6352_exit_cal_cmd); 00179 i2c_stop(); 00180 } 00181 00182 void hmc6352_save_op_mode() { 00183 // Send hmc6352 addr (write) 00184 // Send command L (Save op mode to eeprom) 00185 00186 i2c_start(); 00187 i2c_send_byte(hmc6352_device_addr | hmc6352_write); 00188 i2c_send_byte(hmc6352_save_op_mode_cmd); 00189 i2c_stop(); 00190 00191 } 00192 00193 uns16 hmc6352_get_data() { 00194 00195 // Send hmc6352 addr (write) 00196 // Send command A (get data) 00197 00198 // Send hmc6352 addr (read) 00199 // Read data 00200 // Read data 00201 00202 uns16 data; 00203 00204 i2c_start(); 00205 i2c_send_byte(hmc6352_device_addr | hmc6352_write); 00206 i2c_send_byte(hmc6352_get_data_cmd); 00207 i2c_stop(); 00208 00209 i2c_start(); 00210 i2c_send_byte(hmc6352_device_addr | hmc6352_read); 00211 data = i2c_receive_byte(); 00212 delay_ms(1); 00213 i2c_send_ack(); 00214 data = data << 8; 00215 data = data + i2c_receive_byte(); 00216 delay_ms(1); 00217 i2c_send_ack(); 00218 i2c_stop(); 00219 00220 return data; 00221 } 00222 00223 00224 00225 void hmc6352_set_mode(uns8 mode) { 00226 00227 uns8 data; 00228 data = hmc6352_read_ram(0x74); 00229 00230 switch (mode) { 00231 case hmc6352_mode_standby: 00232 data.1 = 0; 00233 data.0 = 0; 00234 break; 00235 case hmc6352_mode_query: 00236 data.1 = 0; 00237 data.0 = 1; 00238 break; 00239 case hmc6352_mode_continuous: 00240 data.1 = 1; 00241 data.0 = 0; 00242 break; 00243 } 00244 hmc6352_write_ram(0x74, data); 00245 } 00246 00247 void hmc6352_setup_io() { 00248 i2c_setup_io(); 00249 }