cs1180.c 8.7 KB

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