cs1180.c 8.5 KB

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