You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
3.2 KiB
168 lines
3.2 KiB
#include <stdio.h>
|
|
#include "max6884.h"
|
|
#include "dwt.h"
|
|
#include "gpio.h"
|
|
#include "cmd.h"
|
|
|
|
#define ADDR 0x52
|
|
|
|
static i2c_t _i2c;
|
|
|
|
int i2c6884_read(uint8_t slv_addr, uint8_t reg, signed char *buf, uint32_t count)
|
|
{
|
|
i2c_start(_i2c);
|
|
|
|
i2c_send_byte(_i2c, (slv_addr << 1));
|
|
if(i2c_wait_ack(_i2c))
|
|
{
|
|
i2c_stop(_i2c);
|
|
printf("address no ack\r\n");
|
|
return -1;
|
|
}
|
|
i2c_send_byte(_i2c, reg);
|
|
if(i2c_wait_ack(_i2c))
|
|
{
|
|
i2c_stop(_i2c);
|
|
return -1;
|
|
}
|
|
|
|
i2c_start(_i2c);
|
|
|
|
i2c_send_byte(_i2c,(slv_addr << 1) | 0x1);
|
|
if(i2c_wait_ack(_i2c))
|
|
{
|
|
i2c_stop(_i2c);
|
|
return -1;
|
|
}
|
|
|
|
for(int i=0; i<count; i++)
|
|
{
|
|
buf[i] = i2c_recv_byte(_i2c);
|
|
if(i == (count - 1))
|
|
i2c_nack(_i2c);
|
|
else
|
|
i2c_ack(_i2c);
|
|
}
|
|
i2c_stop(_i2c);
|
|
return count;
|
|
}
|
|
|
|
int i2c6884_write(uint8_t slv_addr, uint8_t reg, uint8_t data, uint32_t count)
|
|
{
|
|
//int i;
|
|
|
|
i2c_start(_i2c);
|
|
|
|
i2c_send_byte(_i2c, (slv_addr << 1));
|
|
|
|
if(i2c_wait_ack(_i2c)) {
|
|
i2c_stop(_i2c);
|
|
//printf("address no ack\r\n");
|
|
return -1;
|
|
}
|
|
#if 1
|
|
i2c_send_byte(_i2c,reg);
|
|
if(i2c_wait_ack(_i2c)) {
|
|
//printf("31730 reg first byte no ack reg = %x\r\n",reg);
|
|
i2c_stop(_i2c);
|
|
return -1;
|
|
}
|
|
|
|
i2c_send_byte(_i2c,data);
|
|
if(i2c_wait_ack(_i2c)) {
|
|
//printf("31730 data first byte no ack reg = %x\r\n",reg);
|
|
i2c_stop(_i2c);
|
|
return -1;
|
|
}
|
|
|
|
|
|
#else
|
|
i2c_send_byte(_i2c,reg);
|
|
if(i2c_wait_ack(_i2c)) {
|
|
//printf("first byte no ack\r\n");
|
|
i2c_stop(_i2c);
|
|
return -1;
|
|
}
|
|
|
|
#endif
|
|
|
|
i2c_stop(_i2c);
|
|
return count;
|
|
}
|
|
|
|
|
|
void max6884_init(i2c_t i2c)
|
|
{
|
|
_i2c = i2c;
|
|
gpio_init(PH4, GPIO_OUTPUT);
|
|
gpio_clear(PH4);
|
|
dwt_wait_ms(10);
|
|
gpio_set(PH4);
|
|
|
|
gpio_init(PH3, GPIO_INPUT); //WDOn
|
|
gpio_init(PH6, GPIO_OUTPUT); //WDIn
|
|
|
|
i2c6884_write(ADDR,0x96,0x00, 1);
|
|
dwt_wait_ms(10);
|
|
i2c6884_write(ADDR,0x8f,0x3B, 1);
|
|
}
|
|
|
|
static signed char max6884_read(uint8_t reg)
|
|
{
|
|
signed char buf;
|
|
i2c6884_read(ADDR, reg, &buf, 1);
|
|
return buf;
|
|
}
|
|
|
|
static int max6884_write(uint8_t reg, uint8_t data, int count)
|
|
{
|
|
i2c6884_write(ADDR, reg, data, count);
|
|
return 0;
|
|
}
|
|
|
|
float max6884_read_vol(uint8_t device_num)
|
|
{
|
|
uint8_t V_MSB = 0x00, V_LSB = 0x00;
|
|
signed char data_M, data_L;
|
|
uint16_t data = 0;
|
|
float vol = 0;
|
|
|
|
switch(device_num)
|
|
{
|
|
case 1:
|
|
V_MSB = 0x18;
|
|
V_LSB = 0x19;
|
|
break;
|
|
case 2:
|
|
V_MSB = 0x1A;
|
|
V_LSB = 0x1B;
|
|
break;
|
|
case 3:
|
|
V_MSB = 0x1C;
|
|
V_LSB = 0x1D;
|
|
break;
|
|
case 4:
|
|
V_MSB = 0x1E;
|
|
V_LSB = 0x1F;
|
|
break;
|
|
case 5:
|
|
V_MSB = 0x20;
|
|
V_LSB = 0x21;
|
|
break;
|
|
case 6:
|
|
V_MSB = 0x22;
|
|
V_LSB = 0x23;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
data_M = max6884_read(V_MSB);
|
|
data_L = max6884_read(V_LSB);
|
|
data = ((data | data_M) << 2) | (data_L & 0x03);
|
|
if(device_num == 3)
|
|
vol = data * 0.00732;
|
|
else
|
|
vol = data * 0.0036;
|
|
return vol;
|
|
}
|
|
|
|
|