cs1180.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. do {
  197. cs1180_reset();
  198. delay_us(10);
  199. cs1180_cs(0);
  200. cs_delay();
  201. spi_write_reg(0x0, _cali_gain_regs + 16 * gain, 13);
  202. cs1180_cs(1);
  203. delay_us(10);
  204. if (_cs1180_check_gain(gain)){
  205. break;
  206. }
  207. count ++;
  208. }while(count <= 5);
  209. if (count >= 5) {
  210. if (_cs1180_check_gain(gain) != 1){
  211. return -1;
  212. }
  213. }
  214. CS1180_NOW_GAIN = gain;
  215. _cs1180_gain = 1 << gain;
  216. return 0;
  217. }
  218. int cs1180_change_gain(int current){
  219. if (abs(current) < 4000){ //4.5
  220. return cs1180_adc_set_gain_online(CS1180_GAIN_128X);
  221. }/*else if (abs(current) < 12 * 1000){ //18
  222. return cs1180_adc_set_gain_online(CS1180_GAIN_32X);
  223. }else if (abs(current) < 48 * 1000){ //72
  224. return cs1180_adc_set_gain_online(CS1180_GAIN_8X);
  225. }else if (abs(current) < 160 * 1000){
  226. return cs1180_adc_set_gain_online(CS1180_GAIN_1X);
  227. }*/
  228. return -1;
  229. }
  230. void cs1180_adc_shutdown(void){
  231. cs1180_cs(0);
  232. CS1180_PWR_ENABLE(0);
  233. spi1_deinit();
  234. _cs1180_ready = 0;
  235. }
  236. static int cs1180_may_error = 0;
  237. void cs1180_log(void){
  238. sys_error("cs1180 error %d\n", cs1180_may_error);
  239. for (int i = 0; i < 16; i++){
  240. sys_debug("Reg%d:0x%x\n", i, _cali_gain_regs[16 * CS1180_NOW_GAIN + i]);
  241. }
  242. }
  243. float cs1180_adc_sample(int *valide)
  244. {
  245. static int cs1180_low_ff_count = 0;
  246. uint8_t data[3] = {0,0,0};
  247. int a = 0;
  248. while (IS_CS1180_NOT_READY()); //当drdy 为高时,不读取数据
  249. #if 0
  250. int retry = 5;
  251. while(retry-- >= 0) {
  252. cs1180_cs(0);
  253. cs1180_send_cmd(CS1180_RDATA);
  254. delay_us(60);
  255. data[0] = cs1180_read_data(0xFF);
  256. data[1] = cs1180_read_data(0xFF);
  257. data[2] = cs1180_read_data(0xFF);
  258. cs1180_cs(1);
  259. a = (data[0] << 16) | (data[1] << 8) | data[2];
  260. a >>= 4;
  261. if (a != 0xFFFFF){//spi 上拉,所以如果读到的数据全F,说明可能cs1180没有数据输出,通过判断ready来确定是否要重读
  262. break;
  263. }
  264. delay_us(100);
  265. if (IS_CS1180_NOT_READY()){
  266. break;
  267. }
  268. sys_warning("cs1180 read adc retry!!!!\n");
  269. }
  270. if ((a == 0xFFFFF) && !IS_CS1180_NOT_READY() && (valide != NULL)){
  271. sys_error("cs1180 adc is not valide\n");
  272. *valide = 0;
  273. }
  274. #endif
  275. cs1180_cs(0);
  276. cs1180_send_cmd(CS1180_RDATA);
  277. delay_us(60);
  278. data[0] = cs1180_read_data(0xFF);
  279. data[1] = cs1180_read_data(0xFF);
  280. data[2] = cs1180_read_data(0xFF);
  281. cs1180_cs(1);
  282. a = (data[0] << 16) | (data[1] << 8) | data[2];
  283. a >>= 4;
  284. if (data[2] == 0xFF) {
  285. if (cs1180_low_ff_count ++ >= 10) {
  286. cs1180_low_ff_count = 0;
  287. cs1180_may_error ++;
  288. }
  289. }else {
  290. cs1180_low_ff_count = 0;
  291. }
  292. if (a & 0x80000) {
  293. a = ~a;
  294. a = - (a&0x7FFFF);
  295. }else {
  296. a = a&0x7FFFF;
  297. }
  298. return (int)(((float)a / _cs1180_gain) * 0.9f);
  299. }
  300. static int cs1180_send_cmd(uint8_t cmd){
  301. return spi1_send_byte(cmd, NULL);
  302. }
  303. static uint8_t cs1180_read_data(uint8_t data){
  304. uint8_t r_data = 0xFF;
  305. spi1_send_byte(data, &r_data);
  306. return r_data;
  307. }