cs1180.c 8.5 KB

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