cs1180.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include <string.h>
  2. #include "spi.h"
  3. #include "cs1180.h"
  4. #include "clock.h"
  5. #include "libs/shark_types.h"
  6. #include "libs/logger.h"
  7. #define CS1180_RDATA 0X01
  8. #define CS1180_RDATAC 0X03
  9. #define CS1180_STOPC 0x0f
  10. #define CS1180_RREG 0x10
  11. #define CS1180_WREG 0x50
  12. #define CS1180_CALSELF 0xf0
  13. #define CS1180_OCALSELF 0xf1
  14. #define CS1180_SLFGCAL 0xf2
  15. #define CS1180_OCALSYS 0xf3
  16. #define CS1180_GCALSYS 0xf4
  17. #define CS1180_WAKEUP 0xfb
  18. #define CS1180_SYNC 0xfc
  19. #define CS1180_SLEEP 0xfd
  20. #define CS1180_RESET 0xfe
  21. static float _cs1180_gain = 1.0f;
  22. static int CS1180_NOW_GAIN = CS1180_GAIN_128X;
  23. static uint8_t _cali_gain_regs[16 * 8];
  24. static int cs1180_send_cmd(uint8_t data);
  25. static uint8_t cs1180_read_data(uint8_t data);
  26. static void cs1180_reset(void);
  27. void cs_delay(void)
  28. {
  29. uint32_t count = 1;
  30. while(count--);
  31. }
  32. static int _cs1180_ready = 0;
  33. int cs1180_is_ready(void){
  34. return _cs1180_ready;
  35. }
  36. static void spi_write_reg(uint8_t reg, uint8_t *data, uint8_t len){
  37. cs1180_send_cmd(CS1180_WREG|reg);
  38. cs1180_send_cmd(len - 1);
  39. while(len -- > 0){
  40. cs1180_send_cmd(*data);
  41. data++;
  42. }
  43. }
  44. static void spi_read_reg(uint8_t reg, uint8_t *data, uint8_t len){
  45. cs1180_send_cmd(CS1180_RREG|reg);
  46. cs1180_send_cmd(len - 1);
  47. delay_us(60);
  48. while(len -- > 0){
  49. *data = cs1180_read_data(0xFF);
  50. data++;
  51. }
  52. }
  53. static uint8_t cs1180_dumy_read(int gain){
  54. uint8_t *data = _cali_gain_regs + 16 * gain;
  55. while (IS_CS1180_NOT_READY());
  56. cs1180_cs(0);
  57. cs_delay();
  58. spi_read_reg(0x0, data, 16);
  59. cs_delay();
  60. cs1180_cs(1);
  61. return data[0];
  62. }
  63. /* 对芯片的偏移误差和增益误差进行纠正 */
  64. static void cs1180_self_calibrate(void)
  65. {
  66. cs1180_cs(0);
  67. cs_delay();
  68. cs1180_send_cmd(CS1180_CALSELF);
  69. cs_delay();
  70. cs1180_cs(1);
  71. delay_us(50);
  72. /* wait calibrate finished */
  73. while (IS_CS1180_NOT_READY());
  74. while (!IS_CS1180_NOT_READY());
  75. delay_us(50 * 1000);
  76. while (IS_CS1180_NOT_READY()); //drop first data
  77. }
  78. /* 对芯片的偏移误差进行纠正 */
  79. __attribute__((unused)) static void cs1180_self_offset_calibrate(void)
  80. {
  81. cs1180_cs(0);
  82. cs1180_send_cmd(CS1180_OCALSELF);
  83. cs1180_cs(1);
  84. delay_us(50);
  85. /* wait calibrate finished */
  86. while (IS_CS1180_NOT_READY());
  87. while (!IS_CS1180_NOT_READY());
  88. }
  89. /* 对芯片的增益误差进行纠正 */
  90. __attribute__((unused)) static void cs1180_self_gain_calibrate(void)
  91. {
  92. cs1180_cs(0);
  93. cs1180_send_cmd(CS1180_SLFGCAL);
  94. cs1180_cs(1);
  95. delay_us(50);
  96. /* wait calibrate finished */
  97. while (IS_CS1180_NOT_READY());
  98. while (!IS_CS1180_NOT_READY());
  99. }
  100. /* 对系统的失调误差(偏移误差)进行纠正, 必须要求输入为差分电压为0,
  101. */
  102. void cs1180_sys_offset_calibrate(void)
  103. {
  104. cs1180_cs(0);
  105. cs_delay();
  106. cs1180_send_cmd(CS1180_OCALSYS);
  107. delay_us(50);
  108. cs1180_cs(1);
  109. delay_us(50);
  110. /* wait calibrate finished */
  111. while (IS_CS1180_NOT_READY());
  112. while (!IS_CS1180_NOT_READY());
  113. delay_us(10 * 1000);
  114. while (IS_CS1180_NOT_READY()); //drop first data
  115. }
  116. /* 对系统的增益误差进行纠正,必须输入正满幅度的电压, SP700,SP600未提供满辐电压,故这一项不做 */
  117. __attribute__((unused)) static void cs1180_sys_gain_calibrate(void)
  118. {
  119. cs1180_cs(0);
  120. cs1180_send_cmd(CS1180_GCALSYS);
  121. cs1180_cs(1);
  122. delay_us(50);
  123. /* wait calibrate finished */
  124. while (IS_CS1180_NOT_READY());
  125. while (!IS_CS1180_NOT_READY());
  126. }
  127. static void _cs1180_adc_set_gain(int gain){
  128. /* 输出频率 15hz,参考电压1.235V 输出数据高位在前,
  129. * 输入缓冲器关闭,采样频率 OSC/128,数据格式双极性
  130. */
  131. uint8_t data[] = {0x00, 0x01, 0x00};
  132. data[0] = 0xF & gain;
  133. cs1180_cs(0);
  134. cs_delay();
  135. spi_write_reg(0x00, data, 3);
  136. cs_delay();
  137. cs1180_cs(1);
  138. _cs1180_gain = 1 << gain;
  139. cs1180_self_calibrate();
  140. cs1180_dumy_read(gain);
  141. }
  142. int cs1180_adc_set_gain_cali(int gain){
  143. int count = 0;
  144. do {
  145. cs1180_reset();
  146. delay_us(10);
  147. _cs1180_adc_set_gain(gain);
  148. delay_us(10);
  149. if (cs1180_dumy_read(gain) == gain){
  150. break;
  151. }
  152. count ++;
  153. }while(count <= 20);
  154. if (count >= 20) {
  155. if (cs1180_dumy_read(gain) != gain){
  156. return -1;
  157. }
  158. }
  159. delay_us(10*1000);
  160. cs1180_sys_offset_calibrate();
  161. cs1180_dumy_read(gain);
  162. return 0;
  163. }
  164. static void cs1180_reset(void){
  165. cs1180_cs(0);
  166. cs_delay();
  167. cs1180_send_cmd(CS1180_RESET);
  168. cs_delay();
  169. cs1180_cs(1);
  170. }
  171. void cs1180_adc_init(void){
  172. _cs1180_ready = 0;
  173. CS1180_PWR_ENABLE(0);
  174. delay_us(100 * 1000);
  175. CS1180_PWR_ENABLE(1);
  176. delay_us(200 * 1000);
  177. spi1_init();
  178. delay_us(10);
  179. cs1180_adc_set_gain_cali(CS1180_GAIN_1X);
  180. cs1180_adc_set_gain_cali(CS1180_GAIN_8X);
  181. cs1180_adc_set_gain_cali(CS1180_GAIN_32X);
  182. cs1180_adc_set_gain_cali(CS1180_GAIN_128X);
  183. _cs1180_ready = _cali_gain_regs[16 * CS1180_NOW_GAIN] == CS1180_NOW_GAIN;
  184. }
  185. int _cs1180_check_gain(int gain){
  186. uint8_t data[13] = {0};
  187. cs1180_cs(0);
  188. cs_delay();
  189. spi_read_reg(0x0, data, 13);
  190. cs1180_cs(1);
  191. for (int i = 0; i < 13; i++){
  192. if (data[i] != _cali_gain_regs[16 * gain + i]){
  193. return 0;
  194. }
  195. }
  196. return 1;
  197. }
  198. int cs1180_adc_set_gain_online(int gain){
  199. int count = 0;
  200. if (_cali_gain_regs[16 * CS1180_NOW_GAIN] == gain){
  201. return 0;
  202. }
  203. sys_debug("change gain to %d\n", gain);
  204. do {
  205. cs1180_reset();
  206. delay_us(10);
  207. cs1180_cs(0);
  208. cs_delay();
  209. spi_write_reg(0x0, _cali_gain_regs + 16 * gain, 16);
  210. cs1180_cs(1);
  211. delay_us(10);
  212. if (_cs1180_check_gain(gain)){
  213. break;
  214. }
  215. count ++;
  216. }while(count <= 5);
  217. if (count >= 5) {
  218. if (_cs1180_check_gain(gain) != 1){
  219. sys_error("change gain error!!!\n");
  220. return -1;
  221. }
  222. }
  223. CS1180_NOW_GAIN = gain;
  224. _cs1180_gain = 1 << gain;
  225. sys_debug("change gain success!\n");
  226. return 0;
  227. }
  228. int cs1180_change_gain(int current){
  229. if (abs(current) < 4000){ //4.5
  230. return cs1180_adc_set_gain_online(CS1180_GAIN_128X);
  231. }else if (abs(current) < 12 * 1000){ //18
  232. return cs1180_adc_set_gain_online(CS1180_GAIN_32X);
  233. }/*else if (abs(current) < 48 * 1000){ //72
  234. return cs1180_adc_set_gain_online(CS1180_GAIN_8X);
  235. }else if (abs(current) < 160 * 1000){
  236. return cs1180_adc_set_gain_online(CS1180_GAIN_1X);
  237. }*/
  238. return -1;
  239. }
  240. void cs1180_adc_shutdown(void){
  241. CS1180_PWR_ENABLE(0);
  242. spi1_deinit();
  243. _cs1180_ready = 0;
  244. }
  245. float cs1180_adc_sample(int *valide)
  246. {
  247. uint8_t data[3] = {0,0,0};
  248. int a = 0;
  249. int retry = 5;
  250. while (IS_CS1180_NOT_READY()); //当drdy 为高时,不读取数据
  251. while(retry-- >= 0) {
  252. cs1180_cs(0);
  253. cs1180_send_cmd(CS1180_RDATA);
  254. delay_us(100);
  255. data[0] = cs1180_read_data(0xFF);
  256. data[1] = cs1180_read_data(0xFF);
  257. data[2] = cs1180_read_data(0xFF);
  258. cs1180_cs(1);
  259. a = (data[0] << 16) | (data[1] << 8) | data[2];
  260. a >>= 4;
  261. if (a != 0xFFFFF){//spi 上拉,所以如果读到的数据全F,说明可能cs1180没有数据输出,通过判断ready来确定是否要重读
  262. break;
  263. }
  264. delay_us(100);
  265. if (IS_CS1180_NOT_READY()){
  266. break;
  267. }
  268. sys_warning("cs1180 read adc retry!!!!\n");
  269. }
  270. if ((a == 0xFFFFF) && !IS_CS1180_NOT_READY() && (valide != NULL)){
  271. sys_error("cs1180 adc is not valide\n");
  272. *valide = 0;
  273. }
  274. if (a & 0x80000) {
  275. a = ~a;
  276. a = - (a&0x7FFFF);
  277. }else {
  278. a = a&0x7FFFF;
  279. }
  280. return (int)(((float)a / _cs1180_gain) * 0.9f);
  281. }
  282. static int cs1180_send_cmd(uint8_t cmd){
  283. return spi1_send_byte(cmd, NULL);
  284. }
  285. static uint8_t cs1180_read_data(uint8_t data){
  286. uint8_t r_data = 0xFF;
  287. spi1_send_byte(data, &r_data);
  288. return r_data;
  289. }