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. #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 void cs1180_save_regs(int gain){
  54. uint8_t *data = _cali_gain_regs + 16 * gain;
  55. cs1180_cs(0);
  56. spi_read_reg(0x0, data, 16);
  57. cs1180_cs(1);
  58. }
  59. static void cs1180_dymu_read(){
  60. uint8_t data[16];
  61. cs1180_cs(0);
  62. spi_read_reg(0x0, data, 16);
  63. cs1180_cs(1);
  64. }
  65. /* 对芯片的偏移误差和增益误差进行纠正 */
  66. __attribute__((unused)) static void cs1180_self_calibrate(void)
  67. {
  68. cs1180_cs(0);
  69. cs1180_send_cmd(CS1180_CALSELF);
  70. cs1180_cs(1);
  71. /* wait calibrate finished */
  72. while (IS_CS1180_NOT_READY());
  73. while (!IS_CS1180_NOT_READY());
  74. delay_us(33 * 1000);
  75. while (IS_CS1180_NOT_READY()); //drop first data
  76. }
  77. /* 对芯片的偏移误差进行纠正 */
  78. __attribute__((unused)) static void cs1180_self_offset_calibrate(void)
  79. {
  80. cs1180_cs(0);
  81. cs1180_send_cmd(CS1180_OCALSELF);
  82. cs1180_cs(1);
  83. /* wait calibrate finished */
  84. while (IS_CS1180_NOT_READY());
  85. while (!IS_CS1180_NOT_READY());
  86. delay_us(33 * 1000);
  87. while (IS_CS1180_NOT_READY()); //drop first data
  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. /* wait calibrate finished */
  96. while (IS_CS1180_NOT_READY());
  97. while (!IS_CS1180_NOT_READY());
  98. delay_us(33 * 1000);
  99. while (IS_CS1180_NOT_READY()); //drop first data
  100. }
  101. /* 对系统的失调误差(偏移误差)进行纠正, 必须要求输入为差分电压为0,
  102. */
  103. __attribute__((unused)) static void cs1180_sys_offset_calibrate(void)
  104. {
  105. cs1180_cs(0);
  106. cs1180_send_cmd(CS1180_OCALSYS);
  107. cs1180_cs(1);
  108. /* wait calibrate finished */
  109. delay_us(20);
  110. while (IS_CS1180_NOT_READY());
  111. while (!IS_CS1180_NOT_READY());
  112. delay_us(1000);
  113. while (IS_CS1180_NOT_READY()); //drop first data
  114. }
  115. /* 对系统的增益误差进行纠正,必须输入正满幅度的电压, SP700,SP600未提供满辐电压,故这一项不做 */
  116. __attribute__((unused)) static void cs1180_sys_gain_calibrate(void)
  117. {
  118. cs1180_cs(0);
  119. cs1180_send_cmd(CS1180_GCALSYS);
  120. cs1180_cs(1);
  121. /* wait calibrate finished */
  122. while (IS_CS1180_NOT_READY());
  123. while (!IS_CS1180_NOT_READY());
  124. delay_us(33 * 1000);
  125. while (IS_CS1180_NOT_READY()); //drop first data
  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. spi_write_reg(0x00, data, 3);
  135. cs1180_cs(1);
  136. }
  137. static int _cs1180_check_gain(int gain){
  138. uint8_t data[] = {0xFF, 0xFF, 0xFF};
  139. cs1180_cs(0);
  140. spi_read_reg(0x00, data, 3);
  141. cs1180_cs(1);
  142. if (data[0] == gain && data[1] == 0x01 && data[2] == 0x00){
  143. return 1;
  144. }
  145. return 0;
  146. }
  147. static int cs1180_check_cali_offset(void){
  148. uint32_t offset = 0;
  149. cs1180_cs(0);
  150. spi_read_reg(0x07, (uint8_t *)&offset, 3);
  151. cs1180_cs(1);
  152. return offset != 0;
  153. }
  154. int cs1180_adc_set_gain_cali(int gain){
  155. int count = 0;
  156. do {
  157. cs1180_reset();
  158. delay_us(10);
  159. _cs1180_adc_set_gain(gain);
  160. delay_us(10);
  161. if (_cs1180_check_gain(gain)){
  162. break;
  163. }
  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. cs1180_dymu_read();
  325. delay_us(50);
  326. cs1180_dymu_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. }