cs1180.c 9.0 KB

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