cs1180.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 = 60;
  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_read_gain(void){
  186. uint8_t data[] = {0xff, 0xff, 0xff};
  187. cs1180_cs(0);
  188. cs_delay();
  189. spi_read_reg(0x0, data, 3);
  190. cs1180_cs(1);
  191. return data[0];
  192. }
  193. int cs1180_adc_set_gain_online(int gain){
  194. int count = 0;
  195. if (_cali_gain_regs[16 * CS1180_NOW_GAIN] == gain){
  196. return 0;
  197. }
  198. sys_debug("change gain to %d\n", gain);
  199. do {
  200. cs1180_reset();
  201. delay_us(10);
  202. cs1180_cs(0);
  203. cs_delay();
  204. spi_write_reg(0x0, _cali_gain_regs + 16 * gain, 16);
  205. cs1180_cs(1);
  206. delay_us(10);
  207. if (_cs1180_read_gain() == gain){
  208. break;
  209. }
  210. count ++;
  211. }while(count <= 5);
  212. if (count >= 5) {
  213. if (_cs1180_read_gain() != gain){
  214. sys_error("change gain error!!!\n");
  215. return -1;
  216. }
  217. }
  218. CS1180_NOW_GAIN = gain;
  219. _cs1180_gain = 1 << gain;
  220. sys_debug("change gain success!\n");
  221. return 0;
  222. }
  223. int cs1180_change_gain(int current){
  224. if (abs(current) < 4000){ //4.5
  225. return cs1180_adc_set_gain_online(CS1180_GAIN_128X);
  226. }else if (abs(current) < 16 * 1000){ //18
  227. return cs1180_adc_set_gain_online(CS1180_GAIN_32X);
  228. }/*else if (abs(current) < 48 * 1000){ //72
  229. return cs1180_adc_set_gain_online(CS1180_GAIN_8X);
  230. }else if (abs(current) < 160 * 1000){
  231. return cs1180_adc_set_gain_online(CS1180_GAIN_1X);
  232. }*/
  233. return -1;
  234. }
  235. void cs1180_adc_shutdown(void){
  236. CS1180_PWR_ENABLE(0);
  237. spi1_deinit();
  238. _cs1180_ready = 0;
  239. }
  240. float cs1180_adc_sample(int *valide)
  241. {
  242. uint8_t data[3] = {0,0,0};
  243. int a = 0;
  244. int retry = 5;
  245. while (IS_CS1180_NOT_READY()); //当drdy 为高时,不读取数据
  246. while(retry-- >= 0) {
  247. cs1180_cs(0);
  248. cs1180_send_cmd(CS1180_RDATA);
  249. delay_us(100);
  250. data[0] = cs1180_read_data(0xFF);
  251. data[1] = cs1180_read_data(0xFF);
  252. data[2] = cs1180_read_data(0xFF);
  253. cs1180_cs(1);
  254. a = (data[0] << 16) | (data[1] << 8) | data[2];
  255. a >>= 4;
  256. if (a != 0xFFFFF){//spi 上拉,所以如果读到的数据全F,说明可能cs1180没有数据输出,通过判断ready来确定是否要重读
  257. break;
  258. }
  259. delay_us(100);
  260. if (IS_CS1180_NOT_READY()){
  261. break;
  262. }
  263. sys_warning("cs1180 read adc retry!!!!\n");
  264. }
  265. if ((a == 0xFFFFF) && !IS_CS1180_NOT_READY() && (valide != NULL)){
  266. sys_error("cs1180 adc is not valide\n");
  267. *valide = 0;
  268. }
  269. if (a & 0x80000) {
  270. a = ~a;
  271. a = - (a&0x7FFFF);
  272. }else {
  273. a = a&0x7FFFF;
  274. }
  275. return (int)(((float)a / _cs1180_gain) * 0.9f);
  276. }
  277. static int cs1180_send_cmd(uint8_t cmd){
  278. return spi1_send_byte(cmd, NULL);
  279. }
  280. static uint8_t cs1180_read_data(uint8_t data){
  281. uint8_t r_data = 0xFF;
  282. spi1_send_byte(data, &r_data);
  283. return r_data;
  284. }