Do less re-initialization of smbus, add battery charger enabling

This commit is contained in:
Jeremy Soller 2019-11-05 14:42:15 -07:00
parent aee44bbce0
commit 976874f525
No known key found for this signature in database
GPG Key ID: E988B49EE78A7FB1
5 changed files with 44 additions and 52 deletions

View File

@ -3,18 +3,6 @@
#include <board/smbus.h> #include <board/smbus.h>
uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) { uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) {
// Wait for last command
while (HOSTAA & 1) {}
// Clear result
HOSTAA = HOSTAA;
// Clock to 400 KHz
SCLKTSA = 3;
// Enable host interface
HOCTL2A = 1 << 0;
// Read value from address // Read value from address
TRASLAA = (address << 1) | (1 << 0); TRASLAA = (address << 1) | (1 << 0);
HOCMDA = command; HOCMDA = command;
@ -32,9 +20,6 @@ uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) {
uint8_t status = HOSTAA; uint8_t status = HOSTAA;
HOSTAA = status; HOSTAA = status;
// Disable host interface
HOCTL2A = 0;
// If there were no errors, set value and return 0 // If there were no errors, set value and return 0
uint8_t error = (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2); uint8_t error = (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
if (!(status & error)) { if (!(status & error)) {
@ -48,18 +33,6 @@ uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) {
} }
uint8_t smbus_write(uint8_t address, uint8_t command, uint16_t data) { uint8_t smbus_write(uint8_t address, uint8_t command, uint16_t data) {
// Wait for last command
while (HOSTAA & 1) {}
// Clear result
HOSTAA = HOSTAA;
// Clock to 400 KHz
SCLKTSA = 3;
// Enable host interface
HOCTL2A = 1 << 0;
// Write value to address // Write value to address
TRASLAA = (address << 1); TRASLAA = (address << 1);
HOCMDA = command; HOCMDA = command;
@ -80,14 +53,41 @@ uint8_t smbus_write(uint8_t address, uint8_t command, uint16_t data) {
uint8_t status = HOSTAA; uint8_t status = HOSTAA;
HOSTAA = status; HOSTAA = status;
// Disable host interface
HOCTL2A = 0;
// If there were no errors, set value and return 0 // If there were no errors, set value and return 0
uint8_t error = (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2); uint8_t error = (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
//TODO: custom error type or flags for errors //TODO: custom error type or flags for errors
return (status & error); return (status & error);
} }
uint8_t battery_charger_disable(void) {
uint8_t err = 0;
// Disable charge current
err = smbus_write(0x09, 0x14, 0);
if (err) return err;
// Disable charge voltage
err = smbus_write(0x09, 0x15, 0);
if (err) return err;
}
uint8_t battery_charger_enable(void) {
uint8_t err = 0;
err = battery_charger_disable();
if (err) return err;
// Set charge current to ~1.5 A
err = smbus_write(0x09, 0x14, 0x0600);
if (err) return err;
// Set charge voltage to ~13 V
err = smbus_write(0x09, 0x15, 0x3300);
if (err) return err;
return 0;
}
void battery_debug(void) { void battery_debug(void) {
uint16_t data = 0; uint16_t data = 0;
uint8_t err = 0; uint8_t err = 0;

View File

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

View File

@ -99,9 +99,9 @@ void touchpad_event(struct Ps2 * ps2) {
//TODO //TODO
} }
struct Gpio __code LED_PWR = GPIO(A, 7);
//struct Gpio __code LED_PWR = GPIO(A, 7); struct Gpio __code LED_SSD_N = GPIO(G, 6);
//struct Gpio __code LED_AIRPLANE_N = GPIO(G, 6); struct Gpio __code LED_AIRPLANE_N = GPIO(G, 6);
void main(void) { void main(void) {
init(); init();

View File

@ -8,4 +8,10 @@ void smbus_init(void) {
SMB25MS = 0x19; SMB25MS = 0x19;
SMB45P3USL = 0x5C; SMB45P3USL = 0x5C;
SMB45P3USH = 0x01; SMB45P3USH = 0x01;
// Clock to 400 KHz
SCLKTSA = 3;
// Enable host interface
HOCTL2A = 1 << 0;
} }

View File

@ -3,23 +3,11 @@
#include <ec/smbus.h> #include <ec/smbus.h>
void i2c_write(unsigned char value) { void i2c_write(unsigned char value) {
// Write value to 0x76
TRASLAA = 0x76 << 1;
HOCMDA = value;
for (;;) { for (;;) {
// Wait for last command
while (HOSTAA & 1) {}
// Clear result
HOSTAA = HOSTAA;
// Clock to 400 KHz
SCLKTSA = 3;
// Enable host interface with i2c compatibility
HOCTL2A = (1 << 1) | (1 << 0);
// Write value to 0x76
TRASLAA = 0x76 << 1;
HOCMDA = value;
// Start command // Start command
HOCTLA = (1 << 6) | (0b001 << 2); HOCTLA = (1 << 6) | (0b001 << 2);
@ -33,9 +21,6 @@ void i2c_write(unsigned char value) {
uint8_t status = HOSTAA; uint8_t status = HOSTAA;
HOSTAA = status; HOSTAA = status;
// Disable host interface
HOCTL2A = 0;
// If there were no errors, return // If there were no errors, return
uint8_t error = (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2); uint8_t error = (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2);
if (!(status & error)) { if (!(status & error)) {