|
@@ -61,28 +61,22 @@ static void spi_read_reg(uint8_t reg, uint8_t *data, uint8_t len){
|
|
|
|
|
|
|
|
static uint8_t cs1180_dumy_read(int gain){
|
|
static uint8_t cs1180_dumy_read(int gain){
|
|
|
uint8_t *data = _cali_gain_regs + 16 * gain;
|
|
uint8_t *data = _cali_gain_regs + 16 * gain;
|
|
|
- while (IS_CS1180_NOT_READY());
|
|
|
|
|
cs1180_cs(0);
|
|
cs1180_cs(0);
|
|
|
- cs_delay();
|
|
|
|
|
spi_read_reg(0x0, data, 16);
|
|
spi_read_reg(0x0, data, 16);
|
|
|
- cs_delay();
|
|
|
|
|
cs1180_cs(1);
|
|
cs1180_cs(1);
|
|
|
return data[0];
|
|
return data[0];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* 对芯片的偏移误差和增益误差进行纠正 */
|
|
/* 对芯片的偏移误差和增益误差进行纠正 */
|
|
|
-static void cs1180_self_calibrate(void)
|
|
|
|
|
|
|
+__attribute__((unused)) static void cs1180_self_calibrate(void)
|
|
|
{
|
|
{
|
|
|
cs1180_cs(0);
|
|
cs1180_cs(0);
|
|
|
- cs_delay();
|
|
|
|
|
cs1180_send_cmd(CS1180_CALSELF);
|
|
cs1180_send_cmd(CS1180_CALSELF);
|
|
|
- cs_delay();
|
|
|
|
|
cs1180_cs(1);
|
|
cs1180_cs(1);
|
|
|
- delay_us(50);
|
|
|
|
|
/* wait calibrate finished */
|
|
/* wait calibrate finished */
|
|
|
while (IS_CS1180_NOT_READY());
|
|
while (IS_CS1180_NOT_READY());
|
|
|
while (!IS_CS1180_NOT_READY());
|
|
while (!IS_CS1180_NOT_READY());
|
|
|
- delay_us(50 * 1000);
|
|
|
|
|
|
|
+ delay_us(33 * 1000);
|
|
|
while (IS_CS1180_NOT_READY()); //drop first data
|
|
while (IS_CS1180_NOT_READY()); //drop first data
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -92,10 +86,12 @@ __attribute__((unused)) static void cs1180_self_offset_calibrate(void)
|
|
|
cs1180_cs(0);
|
|
cs1180_cs(0);
|
|
|
cs1180_send_cmd(CS1180_OCALSELF);
|
|
cs1180_send_cmd(CS1180_OCALSELF);
|
|
|
cs1180_cs(1);
|
|
cs1180_cs(1);
|
|
|
- delay_us(50);
|
|
|
|
|
/* wait calibrate finished */
|
|
/* wait calibrate finished */
|
|
|
while (IS_CS1180_NOT_READY());
|
|
while (IS_CS1180_NOT_READY());
|
|
|
while (!IS_CS1180_NOT_READY());
|
|
while (!IS_CS1180_NOT_READY());
|
|
|
|
|
+ delay_us(33 * 1000);
|
|
|
|
|
+ while (IS_CS1180_NOT_READY()); //drop first data
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* 对芯片的增益误差进行纠正 */
|
|
/* 对芯片的增益误差进行纠正 */
|
|
@@ -104,26 +100,25 @@ __attribute__((unused)) static void cs1180_self_gain_calibrate(void)
|
|
|
cs1180_cs(0);
|
|
cs1180_cs(0);
|
|
|
cs1180_send_cmd(CS1180_SLFGCAL);
|
|
cs1180_send_cmd(CS1180_SLFGCAL);
|
|
|
cs1180_cs(1);
|
|
cs1180_cs(1);
|
|
|
- delay_us(50);
|
|
|
|
|
/* wait calibrate finished */
|
|
/* wait calibrate finished */
|
|
|
while (IS_CS1180_NOT_READY());
|
|
while (IS_CS1180_NOT_READY());
|
|
|
while (!IS_CS1180_NOT_READY());
|
|
while (!IS_CS1180_NOT_READY());
|
|
|
|
|
+ delay_us(33 * 1000);
|
|
|
|
|
+ while (IS_CS1180_NOT_READY()); //drop first data
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* 对系统的失调误差(偏移误差)进行纠正, 必须要求输入为差分电压为0,
|
|
/* 对系统的失调误差(偏移误差)进行纠正, 必须要求输入为差分电压为0,
|
|
|
*/
|
|
*/
|
|
|
-void cs1180_sys_offset_calibrate(void)
|
|
|
|
|
|
|
+__attribute__((unused)) static void cs1180_sys_offset_calibrate(void)
|
|
|
{
|
|
{
|
|
|
cs1180_cs(0);
|
|
cs1180_cs(0);
|
|
|
- cs_delay();
|
|
|
|
|
cs1180_send_cmd(CS1180_OCALSYS);
|
|
cs1180_send_cmd(CS1180_OCALSYS);
|
|
|
- delay_us(50);
|
|
|
|
|
cs1180_cs(1);
|
|
cs1180_cs(1);
|
|
|
- delay_us(50);
|
|
|
|
|
/* wait calibrate finished */
|
|
/* wait calibrate finished */
|
|
|
while (IS_CS1180_NOT_READY());
|
|
while (IS_CS1180_NOT_READY());
|
|
|
while (!IS_CS1180_NOT_READY());
|
|
while (!IS_CS1180_NOT_READY());
|
|
|
- delay_us(10 * 1000);
|
|
|
|
|
|
|
+ delay_us(33 * 1000);
|
|
|
while (IS_CS1180_NOT_READY()); //drop first data
|
|
while (IS_CS1180_NOT_READY()); //drop first data
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -133,10 +128,12 @@ __attribute__((unused)) static void cs1180_sys_gain_calibrate(void)
|
|
|
cs1180_cs(0);
|
|
cs1180_cs(0);
|
|
|
cs1180_send_cmd(CS1180_GCALSYS);
|
|
cs1180_send_cmd(CS1180_GCALSYS);
|
|
|
cs1180_cs(1);
|
|
cs1180_cs(1);
|
|
|
- delay_us(50);
|
|
|
|
|
/* wait calibrate finished */
|
|
/* wait calibrate finished */
|
|
|
while (IS_CS1180_NOT_READY());
|
|
while (IS_CS1180_NOT_READY());
|
|
|
while (!IS_CS1180_NOT_READY());
|
|
while (!IS_CS1180_NOT_READY());
|
|
|
|
|
+ delay_us(33 * 1000);
|
|
|
|
|
+ while (IS_CS1180_NOT_READY()); //drop first data
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static void _cs1180_adc_set_gain(int gain){
|
|
static void _cs1180_adc_set_gain(int gain){
|
|
@@ -146,13 +143,8 @@ static void _cs1180_adc_set_gain(int gain){
|
|
|
uint8_t data[] = {0x00, 0x01, 0x00};
|
|
uint8_t data[] = {0x00, 0x01, 0x00};
|
|
|
data[0] = 0xF & gain;
|
|
data[0] = 0xF & gain;
|
|
|
cs1180_cs(0);
|
|
cs1180_cs(0);
|
|
|
- cs_delay();
|
|
|
|
|
spi_write_reg(0x00, data, 3);
|
|
spi_write_reg(0x00, data, 3);
|
|
|
- cs_delay();
|
|
|
|
|
cs1180_cs(1);
|
|
cs1180_cs(1);
|
|
|
- _cs1180_gain = 1 << gain;
|
|
|
|
|
- cs1180_self_calibrate();
|
|
|
|
|
- cs1180_dumy_read(gain);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
int cs1180_adc_set_gain_cali(int gain){
|
|
int cs1180_adc_set_gain_cali(int gain){
|
|
@@ -168,12 +160,14 @@ int cs1180_adc_set_gain_cali(int gain){
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
count ++;
|
|
count ++;
|
|
|
- }while(count <= 20);
|
|
|
|
|
- if (count >= 20) {
|
|
|
|
|
|
|
+ }while(count <= 50);
|
|
|
|
|
+ if (count >= 50) {
|
|
|
if (cs1180_dumy_read(gain) != gain){
|
|
if (cs1180_dumy_read(gain) != gain){
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ _cs1180_gain = 1 << gain;
|
|
|
|
|
+ cs1180_self_gain_calibrate();
|
|
|
delay_us(10*1000);
|
|
delay_us(10*1000);
|
|
|
cs1180_sys_offset_calibrate();
|
|
cs1180_sys_offset_calibrate();
|
|
|
cs1180_dumy_read(gain);
|
|
cs1180_dumy_read(gain);
|
|
@@ -192,9 +186,9 @@ static void cs1180_reset(void){
|
|
|
void cs1180_adc_init(void){
|
|
void cs1180_adc_init(void){
|
|
|
_cs1180_ready = 0;
|
|
_cs1180_ready = 0;
|
|
|
CS1180_PWR_ENABLE(0);
|
|
CS1180_PWR_ENABLE(0);
|
|
|
- delay_us(100 * 1000);
|
|
|
|
|
|
|
+ delay_us(10 * 1000);
|
|
|
CS1180_PWR_ENABLE(1);
|
|
CS1180_PWR_ENABLE(1);
|
|
|
- delay_us(200 * 1000);
|
|
|
|
|
|
|
+ delay_us(20 * 1000);
|
|
|
spi1_init();
|
|
spi1_init();
|
|
|
delay_us(10);
|
|
delay_us(10);
|
|
|
#if 0
|
|
#if 0
|
|
@@ -232,7 +226,7 @@ int cs1180_adc_set_gain_online(int gain){
|
|
|
|
|
|
|
|
cs1180_cs(0);
|
|
cs1180_cs(0);
|
|
|
cs_delay();
|
|
cs_delay();
|
|
|
- spi_write_reg(0x0, _cali_gain_regs + 16 * gain, 16);
|
|
|
|
|
|
|
+ spi_write_reg(0x0, _cali_gain_regs + 16 * gain, 13);
|
|
|
cs1180_cs(1);
|
|
cs1180_cs(1);
|
|
|
delay_us(10);
|
|
delay_us(10);
|
|
|
|
|
|
|
@@ -277,6 +271,9 @@ void cs1180_adc_shutdown(void){
|
|
|
static int cs1180_may_error = 0;
|
|
static int cs1180_may_error = 0;
|
|
|
void cs1180_log(void){
|
|
void cs1180_log(void){
|
|
|
sys_error("cs1180 error %d\n", cs1180_may_error);
|
|
sys_error("cs1180 error %d\n", cs1180_may_error);
|
|
|
|
|
+ for (int i = 0; i < 16; i++){
|
|
|
|
|
+ sys_debug("Reg%d:0x%x\n", i, _cali_gain_regs[16 * CS1180_NOW_GAIN + i]);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
float cs1180_adc_sample(int *valide)
|
|
float cs1180_adc_sample(int *valide)
|
|
|
{
|
|
{
|