cs1180.c 4.6 KB

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