cs1180.c 4.5 KB

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