cs1180.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include <string.h>
  2. #include "spi.h"
  3. #include "cs1180.h"
  4. #include "clock.h"
  5. #define CS1180_RDATA 0X01
  6. #define CS1180_RDATAC 0X03
  7. #define CS1180_STOPC 0x0f
  8. #define CS1180_RREG 0x10
  9. #define CS1180_WREG 0x50
  10. #define CS1180_CALSELF 0xf0
  11. #define CS1180_OCALSELF 0xf1
  12. #define CS1180_SLFGCAL 0xf2
  13. #define CS1180_OCALSYS 0xf3
  14. #define CS1180_GCALSYS 0xf4
  15. #define CS1180_WAKEUP 0xfb
  16. #define CS1180_SYNC 0xfc
  17. #define CS1180_SLEEP 0xfd
  18. #define CS1180_RESET 0xfe
  19. static float _cs1180_gain = 1.0f;
  20. #define CS1180_INIT_GAIN CS1180_GAIN_128X
  21. static int cs1180_send_cmd(uint8_t data);
  22. static uint8_t cs1180_read_data(uint8_t data);
  23. static void cs1180_reset(void);
  24. void cs_delay(void)
  25. {
  26. uint32_t count = 60;
  27. while(count--);
  28. }
  29. static int _cs1180_ready = 0;
  30. int cs1180_is_ready(void){
  31. return _cs1180_ready;
  32. }
  33. static void spi_write_reg(uint8_t reg, uint8_t *data, uint8_t len){
  34. cs1180_send_cmd(CS1180_WREG|reg);
  35. cs1180_send_cmd(len - 1);
  36. while(len -- > 0){
  37. cs1180_send_cmd(*data);
  38. data++;
  39. }
  40. }
  41. static void spi_read_reg(uint8_t reg, uint8_t *data, uint8_t len){
  42. cs1180_send_cmd(CS1180_RREG|reg);
  43. cs1180_send_cmd(len - 1);
  44. while(len -- > 0){
  45. *data = cs1180_read_data(0xFF);
  46. data++;
  47. }
  48. }
  49. static uint8_t cs1180_dumy_read(void){
  50. uint8_t data[16] = {0x5A, 0x5A, 0x5A};
  51. while (IS_CS1180_READY());
  52. cs1180_cs(0);
  53. cs_delay();
  54. spi_read_reg(0x0, data, 16);
  55. cs_delay();
  56. cs1180_cs(1);
  57. return data[0];
  58. }
  59. /* 对芯片的偏移误差和增益误差进行纠正 */
  60. static void cs1180_self_calibrate(void)
  61. {
  62. cs1180_cs(0);
  63. cs_delay();
  64. cs1180_send_cmd(CS1180_CALSELF);
  65. cs_delay();
  66. cs1180_cs(1);
  67. delay_us(50);
  68. /* wait calibrate finished */
  69. while (IS_CS1180_READY());
  70. while (!IS_CS1180_READY());
  71. delay_us(50 * 1000);
  72. while (IS_CS1180_READY()); //drop first data
  73. }
  74. /* 对芯片的偏移误差进行纠正 */
  75. __attribute__((unused)) static void cs1180_self_offset_calibrate(void)
  76. {
  77. cs1180_cs(0);
  78. cs1180_send_cmd(CS1180_OCALSELF);
  79. cs1180_cs(1);
  80. delay_us(50);
  81. /* wait calibrate finished */
  82. while (IS_CS1180_READY());
  83. while (!IS_CS1180_READY());
  84. }
  85. /* 对芯片的增益误差进行纠正 */
  86. __attribute__((unused)) static void cs1180_self_gain_calibrate(void)
  87. {
  88. cs1180_cs(0);
  89. cs1180_send_cmd(CS1180_SLFGCAL);
  90. cs1180_cs(1);
  91. delay_us(50);
  92. /* wait calibrate finished */
  93. while (IS_CS1180_READY());
  94. while (!IS_CS1180_READY());
  95. }
  96. /* 对系统的失调误差(偏移误差)进行纠正, 必须要求输入为差分电压为0,
  97. */
  98. void cs1180_sys_offset_calibrate(void)
  99. {
  100. cs1180_cs(0);
  101. cs_delay();
  102. cs1180_send_cmd(CS1180_OCALSYS);
  103. delay_us(50);
  104. cs1180_cs(1);
  105. delay_us(50);
  106. /* wait calibrate finished */
  107. while (IS_CS1180_READY());
  108. while (!IS_CS1180_READY());
  109. delay_us(10 * 1000);
  110. while (IS_CS1180_READY()); //drop first data
  111. }
  112. /* 对系统的增益误差进行纠正,必须输入正满幅度的电压, SP700,SP600未提供满辐电压,故这一项不做 */
  113. __attribute__((unused)) static void cs1180_sys_gain_calibrate(void)
  114. {
  115. cs1180_cs(0);
  116. cs1180_send_cmd(CS1180_GCALSYS);
  117. cs1180_cs(1);
  118. delay_us(50);
  119. /* wait calibrate finished */
  120. while (IS_CS1180_READY());
  121. while (!IS_CS1180_READY());
  122. }
  123. static void _cs1180_adc_set_gain(int gain){
  124. /* 输出频率 15hz,参考电压1.235V 输出数据高位在前,
  125. * 输入缓冲器关闭,采样频率 OSC/128,数据格式双极性
  126. */
  127. uint8_t data[] = {0x00, 0x01, 0x00};
  128. data[0] = 0xF & gain;
  129. cs1180_cs(0);
  130. cs_delay();
  131. spi_write_reg(0x00, data, 3);
  132. cs_delay();
  133. cs1180_cs(1);
  134. _cs1180_gain = 1 << gain;
  135. cs1180_self_calibrate();
  136. cs1180_dumy_read();
  137. }
  138. int cs1180_adc_set_gain(int gain){
  139. int count = 0;
  140. do {
  141. cs1180_reset();
  142. delay_us(10);
  143. _cs1180_adc_set_gain(gain);
  144. delay_us(10);
  145. if (cs1180_dumy_read() == gain){
  146. break;
  147. }
  148. count ++;
  149. }while(count <= 20);
  150. if (count >= 20) {
  151. if (cs1180_dumy_read() != gain){
  152. return -1;
  153. }
  154. }
  155. delay_us(10*1000);
  156. cs1180_sys_offset_calibrate();
  157. cs1180_dumy_read();
  158. return 0;
  159. }
  160. static void cs1180_reset(void){
  161. cs1180_cs(0);
  162. cs_delay();
  163. cs1180_send_cmd(CS1180_RESET);
  164. cs_delay();
  165. cs1180_cs(1);
  166. }
  167. void cs1180_adc_init(void){
  168. _cs1180_ready = 0;
  169. CS1180_PWR_ENABLE(0);
  170. delay_us(100 * 1000);
  171. CS1180_PWR_ENABLE(1);
  172. delay_us(200 * 1000);
  173. spi1_init();
  174. delay_us(10);
  175. _cs1180_ready = cs1180_adc_set_gain(CS1180_INIT_GAIN) == 0;
  176. }
  177. void cs1180_adc_shutdown(void){
  178. CS1180_PWR_ENABLE(0);
  179. spi1_deinit();
  180. _cs1180_ready = 0;
  181. }
  182. float cs1180_adc_sample(void)
  183. {
  184. uint8_t data[3] = {0,0,0};
  185. int a = 0;
  186. while (IS_CS1180_READY()); //当drdy 为高时,不读取数据
  187. cs1180_cs(0);
  188. //spi_read_reg(0xD, data, 3);
  189. cs1180_send_cmd(CS1180_RDATA);
  190. data[0] = cs1180_read_data(0xFF);
  191. data[1] = cs1180_read_data(0xFF);
  192. data[2] = cs1180_read_data(0xFF);
  193. cs1180_cs(1);
  194. a = (data[0] << 16) | (data[1] << 8) | data[2];
  195. a >>= 4;
  196. if (a & 0x80000) {
  197. a = ~a;
  198. a = - (a&0x7FFFF);
  199. }else {
  200. a = a&0x7FFFF;
  201. }
  202. return ((float)a) / _cs1180_gain;
  203. }
  204. static int cs1180_send_cmd(uint8_t cmd){
  205. return spi1_send_byte(cmd, NULL);
  206. }
  207. static uint8_t cs1180_read_data(uint8_t data){
  208. uint8_t r_data = 0xFF;
  209. spi1_send_byte(data, &r_data);
  210. return r_data;
  211. }