Alright .. So i'm able to compile and generate a hex file for this code without any errors.
//
// battiny.c
//
//
// Created by woot on 2025-02-12.
//
#define F_CPU 8000000UL // set internal clock to 8MHz
#include "battiny.h"
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include "cpu_init.c"
#include "I2C.h"
// MAX1704x Resgisters definitions
// All registers contain two bytes of data and span to two addresses.
// Registers which are present on the MAX17048/49 only are prefixed with MAX17048_
//#define VCELL 0x02 // r - 12-bit A/D measurement of battery voltage
//#define SOC 0x04 // r - 16-bit state of charge (SOC)
#define MODE 0x06 // w - Sends special commands ot IC
//#define VERSION 0x08 // r - Returns IC version
#define SLEEP 0x0A // r/w - (MAX17048/49) Thresholds for entering hibernation
//#define CONFIG 0x0C // r/w - Battery compensation - default 0x971C
#define CVALRT 0x14 // r/w - (MAX17048/49) Configures vcell range to generate alerts default 0x00FF
#define CRATE 0x16 // r/w - (MAX17048/49) Charge rate 0.208%/hr
#define VRESET_ID 0x18 // r/w - (MAX17048/49) Reset voltage and ID - default 0x96__
#define STATUS 0x1A // r/w - (MAX17048/49) Status of ID default 0x01__
#define COMMAND 0xFE // w - Sends special commands to IC
// MAX17043 Config Register Bits
/*
#define config_SLEEP (1 << 7)
#define config_ALSC 0x0040 // MAX17048/49 only
#define config_ALERT (1 << 5)
#define config_THRESHOLD 0
*/
// By default the device address is 0x36
uint8_t addr = 0x36; // device address
uint8_t buffer[2] = {0}; // read into from device
volatile uint8_t timeout_cnt;
volatile uint8_t bod_flag;
static void max1704x_reset(){
// we need to write 2 bytes to the command register
// to reset device at power on.
uint8_t buf[2] = {0x00, 0x54};
// w - (device_addr, memery_addr, data_to_write, data_length);
I2C_write_bytes(addr, COMMAND, buf, 2);
}
// test - get device version
static void max1704x_version(){
uint8_t VERSION = 0x08;
uint16_t version;
I2C_read_bytes(addr, &VERSION, buffer, 2);
version = buffer[0] << 8 | buffer[1];
printf("Device version: %d \n", (int16_t)version);
}
// get the battery voltage.
// full_scale = 5.12; MAX17043 VCELL is 12-bit, 1.25mV per LSB
static void getVoltage(){
uint8_t VCELL = 0x02;
uint16_t voltage; // store vcell data..
I2C_read_bytes(addr, &VCELL, buffer, 2);
voltage = buffer[0] << 8 | buffer[1];
voltage = (voltage) >> 4;
float divider = 4096.0 / 5.12;
printf("Battery Voltage: %.3f %% \n", (((float)voltage)/divider));
}
// get the stage of charge.. the percentage % of charge
static void getSOC(){
uint16_t soc;
uint8_t VSOC = 0x04;
float percent;
// i2c - write - (device_addr, memery_addr, data_length);
// i2c - read - (uint8_t slave_addr, uint8_t *addr_ptr, uint8_t slave_reg, uint8_t num_bytes);
I2C_read_bytes(addr, &VSOC, buffer, 2);
soc = buffer[0] << 8 | buffer[1];
percent = (float)((soc & 0xFF00) >> 8);
percent += ((float)(soc & 0x00FF)) / 256.0;
printf("battery percentage: %.3f %% \n", (float)percent);
}
// default - quick start on reset.
static void quick_Start(){
uint8_t buf[] = {0x40, 0x00};
// i2c - write - (device_addr, memery_addr, data_to_write, data_length);
I2C_write_bytes(addr, MODE, buf, 2);
}
int main(void){
F_CPU_init();
I2C_init();
I2C_start(addr);
I2C_wait_ACK();
_delay_ms(100); // probably not needed but is to give device time to enable
max1704x_reset(); // start device
max1704x_version(); // test - get device version
}
I'm unable to figure out why the compiler thinks the arguments or values i'm passing to the functions might be of different type other the type that it is meant to be passed.
Here's the warnings example:
battiny.c:29:17: warning: passing argument 2 of 'I2C_write_bytes' makes pointer from integer without a cast [-Wint-conversion]
#define COMMAND 0xFE // w - Sends special commands to IC
^
battiny.c:50:27: note: in expansion of macro 'COMMAND'
I2C_write_bytes(addr, COMMAND, buf, 2);
^~~~~~~
Why's there an arrow under the 0 of 0xFE ?? what's the problem with it ?
Also, what is all these..
warning: passing argument 2 of 'I2C_write_bytes' makes pointer from integer without a cast [-Wint-conversion]
what is makes pointer from integer without a cast ??
how do i cast a pointer from an integer ??
why does AVR-GCC generates these warnings when make with raspberry pico doesn't generate these errors ??
Here are all the whole warnings..
(seer) allumette-MacBook:battery woot$ avr-gcc -O -mmcu=attiny1616 battiny.c -o ./battiny.o
battiny.c: In function 'max1704x_reset':
battiny.c:50:27: warning: passing argument 2 of 'I2C_write_bytes' makes pointer from integer without a cast [-Wint-conversion]
I2C_write_bytes(addr, (int)COMMAND, buf, 2);
^
In file included from I2C.h:10:0,
from battiny.c:14:
I2C.c:262:9: note: expected 'uint8_t * {aka unsigned char *}' but argument is of type 'int'
uint8_t I2C_write_bytes(uint8_t slave_addr, uint8_t *addr_ptr, uint8_t slave_reg, uint8_t num_bytes)
^~~~~~~~~~~~~~~
battiny.c:50:41: warning: passing argument 3 of 'I2C_write_bytes' makes integer from pointer without a cast [-Wint-conversion]
I2C_write_bytes(addr, (int)COMMAND, buf, 2);
^~~
In file included from I2C.h:10:0,
from battiny.c:14:
I2C.c:262:9: note: expected 'uint8_t {aka unsigned char}' but argument is of type 'uint8_t * {aka unsigned char *}'
uint8_t I2C_write_bytes(uint8_t slave_addr, uint8_t *addr_ptr, uint8_t slave_reg, uint8_t num_bytes)
^~~~~~~~~~~~~~~
battiny.c: In function 'max1704x_version':
battiny.c:58:36: warning: passing argument 3 of 'I2C_read_bytes' makes integer from pointer without a cast [-Wint-conversion]
I2C_read_bytes(addr, &VERSION, buffer, 2);
^~~~~~
In file included from I2C.h:10:0,
from battiny.c:14:
I2C.c:227:9: note: expected 'uint8_t {aka unsigned char}' but argument is of type 'uint8_t * {aka unsigned char *}'
uint8_t I2C_read_bytes(uint8_t slave_addr, uint8_t *addr_ptr, uint8_t slave_reg, uint8_t num_bytes)
^~~~~~~~~~~~~~
battiny.c: In function 'getVoltage':
battiny.c:68:34: warning: passing argument 3 of 'I2C_read_bytes' makes integer from pointer without a cast [-Wint-conversion]
I2C_read_bytes(addr, &VCELL, buffer, 2);
^~~~~~
In file included from I2C.h:10:0,
from battiny.c:14:
I2C.c:227:9: note: expected 'uint8_t {aka unsigned char}' but argument is of type 'uint8_t * {aka unsigned char *}'
uint8_t I2C_read_bytes(uint8_t slave_addr, uint8_t *addr_ptr, uint8_t slave_reg, uint8_t num_bytes)
^~~~~~~~~~~~~~
battiny.c: In function 'getSOC':
battiny.c:82:33: warning: passing argument 3 of 'I2C_read_bytes' makes integer from pointer without a cast [-Wint-conversion]
I2C_read_bytes(addr, &VSOC, buffer, 2);
^~~~~~
In file included from I2C.h:10:0,
from battiny.c:14:
I2C.c:227:9: note: expected 'uint8_t {aka unsigned char}' but argument is of type 'uint8_t * {aka unsigned char *}'
uint8_t I2C_read_bytes(uint8_t slave_addr, uint8_t *addr_ptr, uint8_t slave_reg, uint8_t num_bytes)
^~~~~~~~~~~~~~
battiny.c: In function 'quick_Start':
battiny.c:21:14: warning: passing argument 2 of 'I2C_write_bytes' makes pointer from integer without a cast [-Wint-conversion]
#define MODE 0x06 // w - Sends special commands ot IC
^
battiny.c:93:27: note: in expansion of macro 'MODE'
I2C_write_bytes(addr, MODE, buf, 2);
^~~~
In file included from I2C.h:10:0,
from battiny.c:14:
I2C.c:262:9: note: expected 'uint8_t * {aka unsigned char *}' but argument is of type 'int'
uint8_t I2C_write_bytes(uint8_t slave_addr, uint8_t *addr_ptr, uint8_t slave_reg, uint8_t num_bytes)
^~~~~~~~~~~~~~~
battiny.c:93:33: warning: passing argument 3 of 'I2C_write_bytes' makes integer from pointer without a cast [-Wint-conversion]
I2C_write_bytes(addr, MODE, buf, 2);
^~~
In file included from I2C.h:10:0,
from battiny.c:14:
I2C.c:262:9: note: expected 'uint8_t {aka unsigned char}' but argument is of type 'uint8_t * {aka unsigned char *}'
uint8_t I2C_write_bytes(uint8_t slave_addr, uint8_t *addr_ptr, uint8_t slave_reg, uint8_t num_bytes)
^~~~~~~~~~~~~~~
(base) woots-MacBook:battery woot$
Any solutions ?