cs1180.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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) && (offset != 0xFFFFFF);
  156. }
  157. void cs1180_read_all_regs(void){
  158. cs1180_save_regs(CS1180_GAIN_128X);
  159. for (int i = 0; i < 16; i++){
  160. sys_debug("Reg%d:0x%x\n", i, _cali_gain_regs[16 * CS1180_NOW_GAIN + i]);
  161. }
  162. }
  163. static int save_reg_errors = 0;
  164. int cs1180_adc_set_gain_cali(int gain){
  165. int count = 0;
  166. do {
  167. cs1180_reset();
  168. delay_us(10);
  169. _cs1180_adc_set_gain(gain);
  170. if (count ++ > 50) {
  171. return 0;
  172. }
  173. delay_us(20);
  174. }while(!_cs1180_check_gain(gain));
  175. delay_us(50);
  176. cs1180_self_calibrate();
  177. count = 0;
  178. do {
  179. cs1180_sys_offset_calibrate();
  180. if (cs1180_check_cali_offset()){
  181. break;
  182. }
  183. delay_us(20);
  184. }while(count++ <= 5);
  185. if (count > 5){
  186. return 0;
  187. }
  188. _cs1180_gain = 1 << gain;
  189. count = 0;
  190. do {
  191. delay_us(50);
  192. cs1180_save_regs(gain);
  193. uint8_t *data = _cali_gain_regs + 16 * gain;
  194. uint32_t offset = data[9]<<16 | data[8] << 8 | data[7];
  195. if ((offset != 0) && (offset != 0xffffff)){
  196. break;
  197. }
  198. save_reg_errors ++;
  199. }while(count ++ >= 5);
  200. return 1;
  201. }
  202. static void cs1180_reset(void){
  203. cs1180_cs(0);
  204. cs_delay();
  205. cs1180_send_cmd(CS1180_RESET);
  206. cs_delay();
  207. cs1180_cs(1);
  208. }
  209. void cs1180_adc_init(void){
  210. int count = 0;
  211. do {
  212. _cs1180_ready = 0;
  213. CS1180_PWR_ENABLE(0);
  214. delay_us(50 * 1000);
  215. CS1180_PWR_ENABLE(1);
  216. delay_us(50 * 1000);
  217. spi1_init();
  218. delay_us(10);
  219. _cs1180_ready = cs1180_adc_set_gain_cali(CS1180_GAIN_128X);
  220. if (_cs1180_ready || (count++ >= 5)) {
  221. break;
  222. }
  223. cs1180_adc_shutdown();
  224. }while(1);
  225. sys_debug("cs1180 init retry %d\n", count);
  226. }
  227. int _cs1180_check_regs(int gain){
  228. uint8_t data[13] = {0};
  229. cs1180_cs(0);
  230. cs_delay();
  231. spi_read_reg(0x0, data, 13);
  232. cs1180_cs(1);
  233. for (int i = 0; i < 13; i++){
  234. if (data[i] != _cali_gain_regs[16 * gain + i]){
  235. return 0;
  236. }
  237. }
  238. return 1;
  239. }
  240. static int cs1180_reinit = 0;
  241. //return 1: cs1180 is OK, 0: cs1180 can not work
  242. int cs1180_adc_set_gain_online(int gain){
  243. int count = 0;
  244. uint8_t *data = _cali_gain_regs + 16 * gain;
  245. if (_cs1180_ready == 1){
  246. return 1;
  247. }
  248. if (data[0] != gain || (data[7] == 0 && data[8] == 0 && data[9] == 0)){
  249. /*cs1180_adc_init();*/ //没有初始化成功过,或者校准没成功过
  250. return _cs1180_ready;
  251. }
  252. cs1180_reinit ++;
  253. do {
  254. cs1180_reset();
  255. delay_us(10);
  256. cs1180_cs(0);
  257. spi_write_reg(0x0, _cali_gain_regs + 16 * gain, 13);
  258. cs1180_cs(1);
  259. delay_us(10);
  260. if (_cs1180_check_regs(gain)){
  261. CS1180_NOW_GAIN = gain;
  262. _cs1180_gain = 1 << gain;
  263. _cs1180_ready = 1;
  264. return 1;
  265. }
  266. count ++;
  267. }while(count <= 5);
  268. return 0;
  269. }
  270. int cs1180_change_gain(int current){
  271. if (abs(current) < MIN_CURRENT_FOR_CS1180){ //4.5
  272. return cs1180_adc_set_gain_online(CS1180_GAIN_128X);
  273. }/*else if (abs(current) < 12 * 1000){ //18
  274. return cs1180_adc_set_gain_online(CS1180_GAIN_32X);
  275. }else if (abs(current) < 48 * 1000){ //72
  276. return cs1180_adc_set_gain_online(CS1180_GAIN_8X);
  277. }else if (abs(current) < 160 * 1000){
  278. return cs1180_adc_set_gain_online(CS1180_GAIN_1X);
  279. }*/
  280. return -1;
  281. }
  282. void cs1180_adc_shutdown(void){
  283. cs1180_cs(0);
  284. CS1180_PWR_ENABLE(0);
  285. spi1_deinit();
  286. _cs1180_ready = 0;
  287. }
  288. static int cs1180_may_error = 0;
  289. void cs1180_log(void){
  290. sys_error("cs1180 error %d-%d, ready %d, reinit %d\n", cs1180_may_error, save_reg_errors, _cs1180_ready, cs1180_reinit);
  291. for (int i = 0; i < 16; i++){
  292. sys_debug("Reg%d:0x%x\n", i, _cali_gain_regs[16 * CS1180_NOW_GAIN + i]);
  293. }
  294. }
  295. float cs1180_adc_sample(int *valide)
  296. {
  297. static int cs1180_low_ff_count = 0;
  298. uint8_t data[3] = {0,0,0};
  299. int a = 0;
  300. while (IS_CS1180_NOT_READY()); //当drdy 为高时,不读取数据
  301. #if 0
  302. int retry = 5;
  303. while(retry-- >= 0) {
  304. cs1180_cs(0);
  305. cs1180_send_cmd(CS1180_RDATA);
  306. delay_us(60);
  307. data[0] = cs1180_read_data(0xFF);
  308. data[1] = cs1180_read_data(0xFF);
  309. data[2] = cs1180_read_data(0xFF);
  310. cs1180_cs(1);
  311. a = (data[0] << 16) | (data[1] << 8) | data[2];
  312. a >>= 4;
  313. if (a != 0xFFFFF){//spi 上拉,所以如果读到的数据全F,说明可能cs1180没有数据输出,通过判断ready来确定是否要重读
  314. break;
  315. }
  316. delay_us(100);
  317. if (IS_CS1180_NOT_READY()){
  318. break;
  319. }
  320. sys_warning("cs1180 read adc retry!!!!\n");
  321. }
  322. if ((a == 0xFFFFF) && !IS_CS1180_NOT_READY() && (valide != NULL)){
  323. sys_error("cs1180 adc is not valide\n");
  324. *valide = 0;
  325. }
  326. #endif
  327. cs1180_cs(0);
  328. cs1180_send_cmd(CS1180_RDATA);
  329. delay_us(60);
  330. data[0] = cs1180_read_data(0xFF);
  331. data[1] = cs1180_read_data(0xFF);
  332. data[2] = cs1180_read_data(0xFF);
  333. cs1180_cs(1);
  334. a = (data[0] << 16) | (data[1] << 8) | data[2];
  335. a >>= 4;
  336. if (data[2] == 0xFF) {
  337. if (cs1180_low_ff_count ++ >= 10) {
  338. cs1180_low_ff_count = 0;
  339. cs1180_may_error ++;
  340. delay_us(20);
  341. cs1180_dumy_read();
  342. delay_us(20);
  343. cs1180_dumy_read();
  344. if (valide) {
  345. *valide = 0;
  346. }
  347. }
  348. }else {
  349. cs1180_low_ff_count = 0;
  350. }
  351. if (a & 0x80000) {
  352. a = ~a;
  353. a = - (a&0x7FFFF);
  354. }else {
  355. a = a&0x7FFFF;
  356. }
  357. return (int)(((float)a / _cs1180_gain));
  358. }
  359. static int cs1180_send_cmd(uint8_t cmd){
  360. return spi1_send_byte(cmd, NULL);
  361. }
  362. static uint8_t cs1180_read_data(uint8_t data){
  363. uint8_t r_data = 0xFF;
  364. spi1_send_byte(data, &r_data);
  365. return r_data;
  366. }