Add i2c capability for AVR
This commit is contained in:
43
src/board/arduino/mega2560/battery.c
Normal file
43
src/board/arduino/mega2560/battery.c
Normal file
@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include <board/i2c.h>
|
||||
|
||||
uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) {
|
||||
return i2c_get(address, command, (uint8_t *)data, 2);
|
||||
}
|
||||
|
||||
void battery_debug(void) {
|
||||
uint16_t data = 0;
|
||||
uint8_t err = 0;
|
||||
|
||||
#define command(N, A, V) { \
|
||||
printf(#N ": "); \
|
||||
err = smbus_read(A, V, &data); \
|
||||
if (err) { \
|
||||
printf("ERROR %02X\n", err); \
|
||||
return; \
|
||||
} \
|
||||
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
|
||||
}
|
10
src/board/arduino/mega2560/i2c.c
Normal file
10
src/board/arduino/mega2560/i2c.c
Normal 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;
|
||||
}
|
6
src/board/arduino/mega2560/include/board/battery.h
Normal file
6
src/board/arduino/mega2560/include/board/battery.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef _BOARD_BATTERY_H
|
||||
#define _BOARD_BATTERY_H
|
||||
|
||||
void battery_debug(void);
|
||||
|
||||
#endif // _BOARD_BATTERY_H
|
6
src/board/arduino/mega2560/include/board/i2c.h
Normal file
6
src/board/arduino/mega2560/include/board/i2c.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef _BOARD_I2C_H
|
||||
#define _BOARD_I2C_H
|
||||
|
||||
void i2c_init(unsigned long baud);
|
||||
|
||||
#endif // _BOARD_I2C_H
|
@ -2,9 +2,12 @@
|
||||
|
||||
#include <arch/gpio.h>
|
||||
#include <arch/uart.h>
|
||||
#include <board/battery.h>
|
||||
#include <board/i2c.h>
|
||||
|
||||
void init(void) {
|
||||
uart_stdio_init(0, __CONSOLE_BAUD__);
|
||||
i2c_init(50000);
|
||||
}
|
||||
|
||||
struct Gpio LED = GPIO(B, 7);
|
||||
@ -15,10 +18,14 @@ int main(void) {
|
||||
gpio_set_dir(&LED, true);
|
||||
gpio_set(&LED, false);
|
||||
printf("Hello from System76 EC for the Arduino Mega 2560!\n");
|
||||
|
||||
battery_debug();
|
||||
|
||||
for (;;) {
|
||||
int c = getchar();
|
||||
if (c == '\r') {
|
||||
putchar('\n');
|
||||
battery_debug();
|
||||
} else if (c > 0) {
|
||||
putchar(c);
|
||||
}
|
||||
|
Reference in New Issue
Block a user