cs1180.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 uint8_t cs1180_dumy_read(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. return data[0];
  59. }
  60. /* 对芯片的偏移误差和增益误差进行纠正 */
  61. __attribute__((unused)) static void cs1180_self_calibrate(void)
  62. {
  63. cs1180_cs(0);
  64. cs1180_send_cmd(CS1180_CALSELF);
  65. cs1180_cs(1);
  66. /* wait calibrate finished */
  67. while (IS_CS1180_NOT_READY());
  68. while (!IS_CS1180_NOT_READY());
  69. delay_us(33 * 1000);
  70. while (IS_CS1180_NOT_READY()); //drop first data
  71. }
  72. /* 对芯片的偏移误差进行纠正 */
  73. __attribute__((unused)) static void cs1180_self_offset_calibrate(void)
  74. {
  75. cs1180_cs(0);
  76. cs1180_send_cmd(CS1180_OCALSELF);
  77. cs1180_cs(1);
  78. /* wait calibrate finished */
  79. while (IS_CS1180_NOT_READY());
  80. while (!IS_CS1180_NOT_READY());
  81. delay_us(33 * 1000);
  82. while (IS_CS1180_NOT_READY()); //drop first data
  83. }
  84. /* 对芯片的增益误差进行纠正 */
  85. __attribute__((unused)) static void cs1180_self_gain_calibrate(void)
  86. {
  87. cs1180_cs(0);
  88. cs1180_send_cmd(CS1180_SLFGCAL);
  89. cs1180_cs(1);
  90. /* wait calibrate finished */
  91. while (IS_CS1180_NOT_READY());
  92. while (!IS_CS1180_NOT_READY());
  93. delay_us(33 * 1000);
  94. while (IS_CS1180_NOT_READY()); //drop first data
  95. }
  96. /* 对系统的失调误差(偏移误差)进行纠正, 必须要求输入为差分电压为0,
  97. */
  98. __attribute__((unused)) static void cs1180_sys_offset_calibrate(void)
  99. {
  100. cs1180_cs(0);
  101. cs1180_send_cmd(CS1180_OCALSYS);
  102. cs1180_cs(1);
  103. /* wait calibrate finished */
  104. while (IS_CS1180_NOT_READY());
  105. while (!IS_CS1180_NOT_READY());
  106. delay_us(33 * 1000);
  107. while (IS_CS1180_NOT_READY()); //drop first data
  108. }
  109. /* 对系统的增益误差进行纠正,必须输入正满幅度的电压, SP700,SP600未提供满辐电压,故这一项不做 */
  110. __attribute__((unused)) static void cs1180_sys_gain_calibrate(void)
  111. {
  112. cs1180_cs(0);
  113. cs1180_send_cmd(CS1180_GCALSYS);
  114. cs1180_cs(1);
  115. /* wait calibrate finished */
  116. while (IS_CS1180_NOT_READY());
  117. while (!IS_CS1180_NOT_READY());
  118. delay_us(33 * 1000);
  119. while (IS_CS1180_NOT_READY()); //drop first data
  120. }
  121. static void _cs1180_adc_set_gain(int gain){
  122. /* 输出频率 15hz,参考电压1.235V 输出数据高位在前,
  123. * 输入缓冲器关闭,采样频率 OSC/128,数据格式双极性
  124. */
  125. uint8_t data[] = {0x00, 0x01, 0x00};
  126. data[0] = 0xF & gain;
  127. cs1180_cs(0);
  128. spi_write_reg(0x00, data, 3);
  129. cs1180_cs(1);
  130. }
  131. static int cs1180_check_cali_offset(void){
  132. uint32_t offset = 0;
  133. cs1180_cs(0);
  134. spi_read_reg(0x07, (uint8_t *)&offset, 3);
  135. cs1180_cs(1);
  136. return offset != 0;
  137. }
  138. int cs1180_adc_set_gain_cali(int gain){
  139. int count = 0;
  140. do {
  141. cs1180_reset();
  142. delay_us(10);
  143. _cs1180_adc_set_gain(gain);
  144. delay_us(10);
  145. if (cs1180_dumy_read(gain) == gain){
  146. break;
  147. }
  148. count ++;
  149. }while(count <= 50);
  150. if (count >= 50) {
  151. if (cs1180_dumy_read(gain) != gain){
  152. return -1;
  153. }
  154. }
  155. _cs1180_gain = 1 << gain;
  156. cs1180_self_gain_calibrate();
  157. delay_us(10*1000);
  158. count = 0;
  159. do {
  160. cs1180_sys_offset_calibrate();
  161. if (cs1180_check_cali_offset()){
  162. break;
  163. }
  164. }while(count++ <= 5);
  165. sys_debug("cs1180 cali sys offset try count %d\n", count);
  166. cs1180_dumy_read(gain);
  167. return 0;
  168. }
  169. static void cs1180_reset(void){
  170. cs1180_cs(0);
  171. cs_delay();
  172. cs1180_send_cmd(CS1180_RESET);
  173. cs_delay();
  174. cs1180_cs(1);
  175. }
  176. void cs1180_adc_init(void){
  177. _cs1180_ready = 0;
  178. CS1180_PWR_ENABLE(0);
  179. delay_us(10 * 1000);
  180. CS1180_PWR_ENABLE(1);
  181. delay_us(20 * 1000);
  182. spi1_init();
  183. delay_us(10);
  184. #if 0
  185. cs1180_adc_set_gain_cali(CS1180_GAIN_1X);
  186. cs1180_adc_set_gain_cali(CS1180_GAIN_8X);
  187. cs1180_adc_set_gain_cali(CS1180_GAIN_32X);
  188. #endif
  189. cs1180_adc_set_gain_cali(CS1180_GAIN_128X);
  190. _cs1180_ready = _cali_gain_regs[16 * CS1180_NOW_GAIN] == CS1180_NOW_GAIN;
  191. }
  192. int _cs1180_check_gain(int gain){
  193. uint8_t data[13] = {0};
  194. cs1180_cs(0);
  195. cs_delay();
  196. spi_read_reg(0x0, data, 13);
  197. cs1180_cs(1);
  198. for (int i = 0; i < 13; i++){
  199. if (data[i] != _cali_gain_regs[16 * gain + i]){
  200. return 0;
  201. }
  202. }
  203. return 1;
  204. }
  205. int cs1180_adc_set_gain_online(int gain){
  206. int count = 0;
  207. if (_cali_gain_regs[16 * CS1180_NOW_GAIN] == gain){
  208. return 0;
  209. }
  210. do {
  211. cs1180_reset();
  212. delay_us(10);
  213. cs1180_cs(0);
  214. cs_delay();
  215. spi_write_reg(0x0, _cali_gain_regs + 16 * gain, 13);
  216. cs1180_cs(1);
  217. delay_us(10);
  218. if (_cs1180_check_gain(gain)){
  219. break;
  220. }
  221. count ++;
  222. }while(count <= 5);
  223. if (count >= 5) {
  224. if (_cs1180_check_gain(gain) != 1){
  225. return -1;
  226. }
  227. }
  228. CS1180_NOW_GAIN = gain;
  229. _cs1180_gain = 1 << gain;
  230. return 0;
  231. }
  232. int cs1180_change_gain(int current){
  233. if (abs(current) < 4000){ //4.5
  234. return cs1180_adc_set_gain_online(CS1180_GAIN_128X);
  235. }/*else if (abs(current) < 12 * 1000){ //18
  236. return cs1180_adc_set_gain_online(CS1180_GAIN_32X);
  237. }else if (abs(current) < 48 * 1000){ //72
  238. return cs1180_adc_set_gain_online(CS1180_GAIN_8X);
  239. }else if (abs(current) < 160 * 1000){
  240. return cs1180_adc_set_gain_online(CS1180_GAIN_1X);
  241. }*/
  242. return -1;
  243. }
  244. void cs1180_adc_shutdown(void){
  245. cs1180_cs(0);
  246. CS1180_PWR_ENABLE(0);
  247. spi1_deinit();
  248. _cs1180_ready = 0;
  249. }
  250. static int cs1180_may_error = 0;
  251. void cs1180_log(void){
  252. sys_error("cs1180 error %d\n", cs1180_may_error);
  253. for (int i = 0; i < 16; i++){
  254. sys_debug("Reg%d:0x%x\n", i, _cali_gain_regs[16 * CS1180_NOW_GAIN + i]);
  255. }
  256. }
  257. float cs1180_adc_sample(int *valide)
  258. {
  259. static int cs1180_low_ff_count = 0;
  260. uint8_t data[3] = {0,0,0};
  261. int a = 0;
  262. while (IS_CS1180_NOT_READY()); //当drdy 为高时,不读取数据
  263. #if 0
  264. int retry = 5;
  265. while(retry-- >= 0) {
  266. cs1180_cs(0);
  267. cs1180_send_cmd(CS1180_RDATA);
  268. delay_us(60);
  269. data[0] = cs1180_read_data(0xFF);
  270. data[1] = cs1180_read_data(0xFF);
  271. data[2] = cs1180_read_data(0xFF);
  272. cs1180_cs(1);
  273. a = (data[0] << 16) | (data[1] << 8) | data[2];
  274. a >>= 4;
  275. if (a != 0xFFFFF){//spi 上拉,所以如果读到的数据全F,说明可能cs1180没有数据输出,通过判断ready来确定是否要重读
  276. break;
  277. }
  278. delay_us(100);
  279. if (IS_CS1180_NOT_READY()){
  280. break;
  281. }
  282. sys_warning("cs1180 read adc retry!!!!\n");
  283. }
  284. if ((a == 0xFFFFF) && !IS_CS1180_NOT_READY() && (valide != NULL)){
  285. sys_error("cs1180 adc is not valide\n");
  286. *valide = 0;
  287. }
  288. #endif
  289. cs1180_cs(0);
  290. cs1180_send_cmd(CS1180_RDATA);
  291. delay_us(60);
  292. data[0] = cs1180_read_data(0xFF);
  293. data[1] = cs1180_read_data(0xFF);
  294. data[2] = cs1180_read_data(0xFF);
  295. cs1180_cs(1);
  296. a = (data[0] << 16) | (data[1] << 8) | data[2];
  297. a >>= 4;
  298. if (data[2] == 0xFF) {
  299. if (cs1180_low_ff_count ++ >= 10) {
  300. cs1180_low_ff_count = 0;
  301. cs1180_may_error ++;
  302. }
  303. }else {
  304. cs1180_low_ff_count = 0;
  305. }
  306. if (a & 0x80000) {
  307. a = ~a;
  308. a = - (a&0x7FFFF);
  309. }else {
  310. a = a&0x7FFFF;
  311. }
  312. return (int)(((float)a / _cs1180_gain) * 0.9f);
  313. }
  314. static int cs1180_send_cmd(uint8_t cmd){
  315. return spi1_send_byte(cmd, NULL);
  316. }
  317. static uint8_t cs1180_read_data(uint8_t data){
  318. uint8_t r_data = 0xFF;
  319. spi1_send_byte(data, &r_data);
  320. return r_data;
  321. }