cs1180.c 4.9 KB

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