cs1180.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. int cs1180_adc_set_gain_cali(int gain){
  132. int count = 0;
  133. do {
  134. cs1180_reset();
  135. delay_us(10);
  136. _cs1180_adc_set_gain(gain);
  137. delay_us(10);
  138. if (cs1180_dumy_read(gain) == gain){
  139. break;
  140. }
  141. count ++;
  142. }while(count <= 50);
  143. if (count >= 50) {
  144. if (cs1180_dumy_read(gain) != gain){
  145. return -1;
  146. }
  147. }
  148. _cs1180_gain = 1 << gain;
  149. cs1180_self_gain_calibrate();
  150. delay_us(10*1000);
  151. cs1180_sys_offset_calibrate();
  152. cs1180_dumy_read(gain);
  153. return 0;
  154. }
  155. static void cs1180_reset(void){
  156. cs1180_cs(0);
  157. cs_delay();
  158. cs1180_send_cmd(CS1180_RESET);
  159. cs_delay();
  160. cs1180_cs(1);
  161. }
  162. void cs1180_adc_init(void){
  163. _cs1180_ready = 0;
  164. CS1180_PWR_ENABLE(0);
  165. delay_us(10 * 1000);
  166. CS1180_PWR_ENABLE(1);
  167. delay_us(20 * 1000);
  168. spi1_init();
  169. delay_us(10);
  170. #if 0
  171. cs1180_adc_set_gain_cali(CS1180_GAIN_1X);
  172. cs1180_adc_set_gain_cali(CS1180_GAIN_8X);
  173. cs1180_adc_set_gain_cali(CS1180_GAIN_32X);
  174. #endif
  175. cs1180_adc_set_gain_cali(CS1180_GAIN_128X);
  176. _cs1180_ready = _cali_gain_regs[16 * CS1180_NOW_GAIN] == CS1180_NOW_GAIN;
  177. }
  178. int _cs1180_check_gain(int gain){
  179. uint8_t data[13] = {0};
  180. cs1180_cs(0);
  181. cs_delay();
  182. spi_read_reg(0x0, data, 13);
  183. cs1180_cs(1);
  184. for (int i = 0; i < 13; i++){
  185. if (data[i] != _cali_gain_regs[16 * gain + i]){
  186. return 0;
  187. }
  188. }
  189. return 1;
  190. }
  191. int cs1180_adc_set_gain_online(int gain){
  192. int count = 0;
  193. if (_cali_gain_regs[16 * CS1180_NOW_GAIN] == gain){
  194. return 0;
  195. }
  196. sys_debug("change gain to %d\n", gain);
  197. do {
  198. cs1180_reset();
  199. delay_us(10);
  200. cs1180_cs(0);
  201. cs_delay();
  202. spi_write_reg(0x0, _cali_gain_regs + 16 * gain, 13);
  203. cs1180_cs(1);
  204. delay_us(10);
  205. if (_cs1180_check_gain(gain)){
  206. break;
  207. }
  208. count ++;
  209. }while(count <= 5);
  210. if (count >= 5) {
  211. if (_cs1180_check_gain(gain) != 1){
  212. sys_error("change gain error!!!\n");
  213. return -1;
  214. }
  215. }
  216. CS1180_NOW_GAIN = gain;
  217. _cs1180_gain = 1 << gain;
  218. sys_debug("change gain success!\n");
  219. return 0;
  220. }
  221. int cs1180_change_gain(int current){
  222. if (abs(current) < 4000){ //4.5
  223. return cs1180_adc_set_gain_online(CS1180_GAIN_128X);
  224. }/*else if (abs(current) < 12 * 1000){ //18
  225. return cs1180_adc_set_gain_online(CS1180_GAIN_32X);
  226. }else if (abs(current) < 48 * 1000){ //72
  227. return cs1180_adc_set_gain_online(CS1180_GAIN_8X);
  228. }else if (abs(current) < 160 * 1000){
  229. return cs1180_adc_set_gain_online(CS1180_GAIN_1X);
  230. }*/
  231. return -1;
  232. }
  233. void cs1180_adc_shutdown(void){
  234. cs1180_cs(0);
  235. CS1180_PWR_ENABLE(0);
  236. spi1_deinit();
  237. _cs1180_ready = 0;
  238. }
  239. static int cs1180_may_error = 0;
  240. void cs1180_log(void){
  241. sys_error("cs1180 error %d\n", cs1180_may_error);
  242. for (int i = 0; i < 16; i++){
  243. sys_debug("Reg%d:0x%x\n", i, _cali_gain_regs[16 * CS1180_NOW_GAIN + i]);
  244. }
  245. }
  246. float cs1180_adc_sample(int *valide)
  247. {
  248. static int cs1180_low_ff_count = 0;
  249. uint8_t data[3] = {0,0,0};
  250. int a = 0;
  251. while (IS_CS1180_NOT_READY()); //当drdy 为高时,不读取数据
  252. #if 0
  253. int retry = 5;
  254. while(retry-- >= 0) {
  255. cs1180_cs(0);
  256. cs1180_send_cmd(CS1180_RDATA);
  257. delay_us(60);
  258. data[0] = cs1180_read_data(0xFF);
  259. data[1] = cs1180_read_data(0xFF);
  260. data[2] = cs1180_read_data(0xFF);
  261. cs1180_cs(1);
  262. a = (data[0] << 16) | (data[1] << 8) | data[2];
  263. a >>= 4;
  264. if (a != 0xFFFFF){//spi 上拉,所以如果读到的数据全F,说明可能cs1180没有数据输出,通过判断ready来确定是否要重读
  265. break;
  266. }
  267. delay_us(100);
  268. if (IS_CS1180_NOT_READY()){
  269. break;
  270. }
  271. sys_warning("cs1180 read adc retry!!!!\n");
  272. }
  273. if ((a == 0xFFFFF) && !IS_CS1180_NOT_READY() && (valide != NULL)){
  274. sys_error("cs1180 adc is not valide\n");
  275. *valide = 0;
  276. }
  277. #endif
  278. cs1180_cs(0);
  279. cs1180_send_cmd(CS1180_RDATA);
  280. delay_us(60);
  281. data[0] = cs1180_read_data(0xFF);
  282. data[1] = cs1180_read_data(0xFF);
  283. data[2] = cs1180_read_data(0xFF);
  284. cs1180_cs(1);
  285. a = (data[0] << 16) | (data[1] << 8) | data[2];
  286. a >>= 4;
  287. if (data[2] == 0xFF) {
  288. if (cs1180_low_ff_count ++ >= 10) {
  289. cs1180_low_ff_count = 0;
  290. cs1180_may_error ++;
  291. }
  292. }else {
  293. cs1180_low_ff_count = 0;
  294. }
  295. if (a & 0x80000) {
  296. a = ~a;
  297. a = - (a&0x7FFFF);
  298. }else {
  299. a = a&0x7FFFF;
  300. }
  301. return (int)(((float)a / _cs1180_gain) * 0.9f);
  302. }
  303. static int cs1180_send_cmd(uint8_t cmd){
  304. return spi1_send_byte(cmd, NULL);
  305. }
  306. static uint8_t cs1180_read_data(uint8_t data){
  307. uint8_t r_data = 0xFF;
  308. spi1_send_byte(data, &r_data);
  309. return r_data;
  310. }