Enable Arduino Uno i2c debugging

This commit is contained in:
Jeremy Soller
2019-11-25 14:55:31 -07:00
parent 08c800fa6d
commit 561d3bdb7a
6 changed files with 91 additions and 2 deletions

View File

@ -0,0 +1,47 @@
#include <stdio.h>
#include <common/i2c.h>
int smbus_read(uint8_t address, uint8_t command, uint16_t * data) {
return i2c_get(address, command, (uint8_t *)data, 2);
}
int smbus_write(uint8_t address, uint8_t command, uint16_t data) {
return i2c_set(address, command, (uint8_t *)&data, 2);
}
void battery_debug(void) {
uint16_t data = 0;
int res = 0;
#define command(N, A, V) { \
printf(#N ": "); \
res = smbus_read(A, V, &data); \
if (res < 0) { \
printf("ERROR %04X\n", -res); \
} else { \
printf("%04X\n", data); \
} \
}
printf("Battery:\n");
command(Temperature, 0x0B, 0x08);
command(Voltage, 0x0B, 0x09);
command(Current, 0x0B, 0x0A);
command(Charge, 0x0B, 0x0D);
printf("Charger:\n");
command(ChargeOption0, 0x09, 0x12);
command(ChargeOption1, 0x09, 0x3B);
command(ChargeOption2, 0x09, 0x38);
command(ChargeOption3, 0x09, 0x37);
command(ChargeCurrent, 0x09, 0x14);
command(ChargeVoltage, 0x09, 0x15);
command(DishargeCurrent, 0x09, 0x39);
command(InputCurrent, 0x09, 0x3F);
command(ProchotOption0, 0x09, 0x3C);
command(ProchotOption1, 0x09, 0x3D);
command(ProchotStatus, 0x09, 0x3A);
#undef command
}

View File

@ -0,0 +1,10 @@
#include <avr/io.h>
#include <board/cpu.h>
#include <board/i2c.h>
void i2c_init(unsigned long baud) {
TWAR = 0;
TWBR = (uint8_t)(((F_CPU / baud) - 16 ) / 2);
TWCR = 0;
}

View File

@ -0,0 +1,6 @@
#ifndef _BOARD_BATTERY_H
#define _BOARD_BATTERY_H
void battery_debug(void);
#endif // _BOARD_BATTERY_H

View File

@ -0,0 +1,6 @@
#ifndef _BOARD_I2C_H
#define _BOARD_I2C_H
void i2c_init(unsigned long baud);
#endif // _BOARD_I2C_H

View File

@ -1,10 +1,24 @@
#include <stdio.h> #include <stdio.h>
#include <arch/gpio.h> #include <arch/gpio.h>
#include <arch/i2c_slave.h>
#include <arch/uart.h> #include <arch/uart.h>
#include <board/battery.h>
#include <board/i2c.h>
void init(void) { void init(void) {
uart_stdio_init(0, __CONSOLE_BAUD__); uart_stdio_init(0, __CONSOLE_BAUD__);
i2c_init(50000);
}
static void i2c_slave_new() {}
static void i2c_slave_recv(uint8_t data) {
printf("%c", data);
}
static uint8_t i2c_slave_send() {
return 0;
} }
struct Gpio LED = GPIO(B, 5); struct Gpio LED = GPIO(B, 5);
@ -15,10 +29,16 @@ int main(void) {
gpio_set_dir(&LED, true); gpio_set_dir(&LED, true);
gpio_set(&LED, false); gpio_set(&LED, false);
printf("Hello from System76 EC for the Arduino Uno!\n"); printf("Hello from System76 EC for the Arduino Uno!\n");
battery_debug();
for (;;) { for (;;) {
i2c_slave_init(0x76, i2c_slave_new, i2c_slave_recv, i2c_slave_send);
int c = getchar(); int c = getchar();
i2c_slave_stop();
if (c == '\r') { if (c == '\r') {
putchar('\n'); putchar('\n');
battery_debug();
} else if (c > 0) { } else if (c > 0) {
putchar(c); putchar(c);
} }

View File

@ -10,10 +10,10 @@ INCLUDE+=$(wildcard $(BOARD_DIR)/keymap/*.h)
# 3 - INFO # 3 - INFO
# 4 - DEBUG # 4 - DEBUG
# 5 - TRACE # 5 - TRACE
CFLAGS+=-DLEVEL=2 CFLAGS+=-DLEVEL=4
# Enable I2C debug on 0x76 # Enable I2C debug on 0x76
#CFLAGS+=-DI2C_DEBUGGER=0x76 CFLAGS+=-DI2C_DEBUGGER=0x76
# Add scratch ROM source # Add scratch ROM source
SCRATCH_DIR=$(BOARD_DIR)/scratch SCRATCH_DIR=$(BOARD_DIR)/scratch