cs1180.c 3.9 KB

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