cs1180.c 7.5 KB

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