cs1180.c 4.7 KB

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