cs1180.c 9.2 KB

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