Update .clang-format and apply
Update .clang-format for LLVM 14.0, available on Ubuntu 22.04. There is still plenty that clang-format sucks at or does wrong, so either add some more blocks to disable it, or just put up with it. Signed-off-by: Tim Crawford <tcrawford@system76.com>
This commit is contained in:
committed by
Jeremy Soller
parent
c3267fc4ad
commit
e032c5f0f2
@ -1,22 +1,25 @@
|
|||||||
# https://releases.llvm.org/10.0.0/tools/clang/docs/ClangFormatStyleOptions.html
|
# https://releases.llvm.org/14.0.0/tools/clang/docs/ClangFormatStyleOptions.html
|
||||||
|
# LLVM 14 used to support Ubuntu 22.04 LTS.
|
||||||
---
|
---
|
||||||
Language: Cpp
|
Language: Cpp
|
||||||
AccessModifierOffset: -4
|
AccessModifierOffset: -4
|
||||||
AlignAfterOpenBracket: AlwaysBreak
|
AlignAfterOpenBracket: BlockIndent
|
||||||
AlignConsecutiveAssignments: false
|
AlignConsecutiveAssignments: false
|
||||||
AlignConsecutiveDeclarations: false
|
AlignConsecutiveBitFields: None
|
||||||
AlignConsecutiveMacros: true
|
AlignConsecutiveDeclarations: None
|
||||||
|
AlignConsecutiveMacros: false
|
||||||
AlignEscapedNewlines: DontAlign
|
AlignEscapedNewlines: DontAlign
|
||||||
AlignOperands: true
|
AlignOperands: DontAlign
|
||||||
AlignTrailingComments: false
|
AlignTrailingComments: false
|
||||||
AllowAllArgumentsOnNextLine: true
|
AllowAllArgumentsOnNextLine: false
|
||||||
AllowAllConstructorInitializersOnNextLine: true
|
AllowAllConstructorInitializersOnNextLine: false
|
||||||
AllowAllParametersOfDeclarationOnNextLine: false
|
AllowAllParametersOfDeclarationOnNextLine: false
|
||||||
AllowShortBlocksOnASingleLine: Empty
|
AllowShortBlocksOnASingleLine: Empty
|
||||||
AllowShortCaseLabelsOnASingleLine: false
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
AllowShortFunctionsOnASingleLine: Inline
|
AllowShortEnumsOnASingleLine: false
|
||||||
|
AllowShortFunctionsOnASingleLine: Empty
|
||||||
AllowShortIfStatementsOnASingleLine: Never
|
AllowShortIfStatementsOnASingleLine: Never
|
||||||
AllowShortLambdasOnASingleLine: All
|
AllowShortLambdasOnASingleLine: Empty
|
||||||
AllowShortLoopsOnASingleLine: false
|
AllowShortLoopsOnASingleLine: false
|
||||||
AlwaysBreakAfterReturnType: None
|
AlwaysBreakAfterReturnType: None
|
||||||
AlwaysBreakBeforeMultilineStrings: false
|
AlwaysBreakBeforeMultilineStrings: false
|
||||||
@ -26,42 +29,45 @@ BinPackParameters: false
|
|||||||
BraceWrapping:
|
BraceWrapping:
|
||||||
AfterCaseLabel: false
|
AfterCaseLabel: false
|
||||||
AfterClass: true
|
AfterClass: true
|
||||||
AfterControlStatement: false
|
AfterControlStatement: Never
|
||||||
AfterEnum: false
|
AfterEnum: false
|
||||||
AfterFunction: false
|
AfterFunction: false
|
||||||
AfterNamespace: true
|
AfterNamespace: false
|
||||||
AfterObjCDeclaration: false
|
AfterObjCDeclaration: false
|
||||||
AfterStruct: false
|
AfterStruct: false
|
||||||
AfterUnion: false
|
AfterUnion: false
|
||||||
AfterExternBlock: false
|
AfterExternBlock: false
|
||||||
BeforeCatch: false
|
BeforeCatch: false
|
||||||
BeforeElse: false
|
BeforeElse: false
|
||||||
|
BeforeLambdaBody: false
|
||||||
|
BeforeWhile: false
|
||||||
IndentBraces: false
|
IndentBraces: false
|
||||||
SplitEmptyFunction: false
|
SplitEmptyFunction: false
|
||||||
SplitEmptyRecord: false
|
SplitEmptyRecord: false
|
||||||
SplitEmptyNamespace: false
|
SplitEmptyNamespace: false
|
||||||
BreakAfterJavaFieldAnnotations: false
|
BreakAfterJavaFieldAnnotations: true
|
||||||
BreakBeforeBinaryOperators: None
|
BreakBeforeBinaryOperators: None
|
||||||
BreakBeforeBraces: Custom
|
BreakBeforeBraces: Custom
|
||||||
BreakBeforeTernaryOperators: false
|
BreakBeforeTernaryOperators: true
|
||||||
BreakConstructorInitializers: AfterColon
|
BreakConstructorInitializers: AfterColon
|
||||||
|
BreakConstructorInitializersBeforeComma: false
|
||||||
BreakInheritanceList: AfterColon
|
BreakInheritanceList: AfterColon
|
||||||
BreakStringLiterals: false
|
BreakStringLiterals: false
|
||||||
ColumnLimit: 96
|
ColumnLimit: 100
|
||||||
CommentPragmas: '^ IWYU pragma:'
|
CommentPragmas: '^ IWYU pragma:'
|
||||||
CompactNamespaces: false
|
CompactNamespaces: false
|
||||||
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||||
ConstructorInitializerIndentWidth: 4
|
ConstructorInitializerIndentWidth: 4
|
||||||
ContinuationIndentWidth: 4
|
ContinuationIndentWidth: 4
|
||||||
Cpp11BracedListStyle: false
|
Cpp11BracedListStyle: false
|
||||||
DeriveLineEnding: false
|
DeriveLineEnding: false
|
||||||
DerivePointerAlignment: false
|
DerivePointerAlignment: false
|
||||||
DisableFormat: false
|
DisableFormat: false
|
||||||
ExperimentalAutoDetectBinPacking: true
|
ExperimentalAutoDetectBinPacking: false
|
||||||
FixNamespaceComments: false
|
FixNamespaceComments: false
|
||||||
ForEachMacros:
|
ForEachMacros:
|
||||||
- foreach
|
- 'foreach'
|
||||||
IncludeBlocks: Preserve
|
IncludeBlocks: Regroup
|
||||||
IncludeCategories:
|
IncludeCategories:
|
||||||
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
|
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
|
||||||
Priority: 2
|
Priority: 2
|
||||||
@ -72,14 +78,17 @@ IncludeCategories:
|
|||||||
- Regex: '.*'
|
- Regex: '.*'
|
||||||
Priority: 1
|
Priority: 1
|
||||||
SortPriority: 0
|
SortPriority: 0
|
||||||
IncludeIsMainRegex: '(Test)?$'
|
IncludeIsMainRegex: '(_test)?$'
|
||||||
IncludeIsMainSourceRegex: ''
|
IncludeIsMainSourceRegex: ''
|
||||||
IndentCaseLabels: true
|
IndentCaseBlocks: true
|
||||||
|
IndentCaseLabels: false
|
||||||
|
IndentExternBlock: NoIndent
|
||||||
IndentGotoLabels: false
|
IndentGotoLabels: false
|
||||||
IndentPPDirectives: BeforeHash
|
IndentPPDirectives: None
|
||||||
IndentWidth: 4
|
IndentWidth: 4
|
||||||
IndentWrappedFunctionNames: false
|
IndentWrappedFunctionNames: false
|
||||||
JavaScriptQuotes: Leave
|
InsertTrailingCommas: Wrapped
|
||||||
|
JavaScriptQuotes: Double
|
||||||
JavaScriptWrapImports: true
|
JavaScriptWrapImports: true
|
||||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||||
MacroBlockBegin: ''
|
MacroBlockBegin: ''
|
||||||
@ -88,30 +97,29 @@ MaxEmptyLinesToKeep: 1
|
|||||||
NamespaceIndentation: None
|
NamespaceIndentation: None
|
||||||
ObjCBinPackProtocolList: Auto
|
ObjCBinPackProtocolList: Auto
|
||||||
ObjCBlockIndentWidth: 4
|
ObjCBlockIndentWidth: 4
|
||||||
|
ObjCBreakBeforeNestedBlockParam: true
|
||||||
ObjCSpaceAfterProperty: true
|
ObjCSpaceAfterProperty: true
|
||||||
ObjCSpaceBeforeProtocolList: true
|
ObjCSpaceBeforeProtocolList: false
|
||||||
|
PenaltyBreakAssignment: 1000
|
||||||
PenaltyBreakAssignment: 10
|
PenaltyBreakBeforeFirstCallParameter: 19
|
||||||
PenaltyBreakBeforeFirstCallParameter: 10
|
PenaltyBreakComment: 300
|
||||||
PenaltyBreakComment: 100
|
PenaltyBreakFirstLessLess: 120
|
||||||
PenaltyBreakFirstLessLess: 5
|
PenaltyBreakString: 1000
|
||||||
PenaltyBreakString: 100
|
|
||||||
PenaltyBreakTemplateDeclaration: 10
|
PenaltyBreakTemplateDeclaration: 10
|
||||||
PenaltyExcessCharacter: 5
|
PenaltyExcessCharacter: 1000000
|
||||||
PenaltyReturnTypeOnItsOwnLine: 100
|
PenaltyReturnTypeOnItsOwnLine: 1000
|
||||||
|
PointerAlignment: Right
|
||||||
PointerAlignment: Middle
|
|
||||||
ReflowComments: false
|
ReflowComments: false
|
||||||
SortIncludes: true
|
SortIncludes: false
|
||||||
SortUsingDeclarations: true
|
SortUsingDeclarations: true
|
||||||
SpaceAfterCStyleCast: false
|
SpaceAfterCStyleCast: false
|
||||||
SpaceAfterLogicalNot: false
|
SpaceAfterLogicalNot: false
|
||||||
SpaceAfterTemplateKeyword: true
|
SpaceAfterTemplateKeyword: false
|
||||||
SpaceBeforeAssignmentOperators: true
|
SpaceBeforeAssignmentOperators: true
|
||||||
SpaceBeforeCpp11BracedList: false
|
SpaceBeforeCpp11BracedList: false
|
||||||
SpaceBeforeCtorInitializerColon: true
|
SpaceBeforeCtorInitializerColon: true
|
||||||
SpaceBeforeInheritanceColon: true
|
SpaceBeforeInheritanceColon: true
|
||||||
SpaceBeforeParens: ControlStatements
|
SpaceBeforeParens: ControlStatementsExceptForEachMacros
|
||||||
SpaceBeforeRangeBasedForLoopColon: true
|
SpaceBeforeRangeBasedForLoopColon: true
|
||||||
SpaceBeforeSquareBrackets: false
|
SpaceBeforeSquareBrackets: false
|
||||||
SpaceInEmptyBlock: false
|
SpaceInEmptyBlock: false
|
||||||
@ -120,14 +128,14 @@ SpacesBeforeTrailingComments: 1
|
|||||||
SpacesInAngles: false
|
SpacesInAngles: false
|
||||||
SpacesInCStyleCastParentheses: false
|
SpacesInCStyleCastParentheses: false
|
||||||
SpacesInConditionalStatement: false
|
SpacesInConditionalStatement: false
|
||||||
SpacesInContainerLiterals: true
|
SpacesInContainerLiterals: false
|
||||||
SpacesInParentheses: false
|
SpacesInParentheses: false
|
||||||
SpacesInSquareBrackets: false
|
SpacesInSquareBrackets: false
|
||||||
Standard: Latest
|
Standard: Latest
|
||||||
StatementMacros:
|
StatementMacros:
|
||||||
- ARRAY_SIZE
|
|
||||||
- xstr
|
|
||||||
TabWidth: 4
|
TabWidth: 4
|
||||||
UseCRLF: false
|
UseCRLF: false
|
||||||
UseTab: Never
|
UseTab: Never
|
||||||
|
WhitespaceSensitiveMacros:
|
||||||
|
- 'STRINGIZE'
|
||||||
...
|
...
|
||||||
|
@ -9,13 +9,11 @@ void delay_ticks(uint16_t ticks);
|
|||||||
|
|
||||||
// 1 us * 9.2 MHz / 12 is 69/90
|
// 1 us * 9.2 MHz / 12 is 69/90
|
||||||
// Warning: this will round to the nearest tick
|
// Warning: this will round to the nearest tick
|
||||||
#define delay_us(X) \
|
#define delay_us(X) delay_ticks((uint16_t)((((uint32_t)(X)) * 69UL + 89UL) / 90UL));
|
||||||
delay_ticks((uint16_t)((((uint32_t)(X)) * 69UL + 89UL) / 90UL));
|
|
||||||
|
|
||||||
// 1 ns * 9.2 MHz / 12 is 69/90000
|
// 1 ns * 9.2 MHz / 12 is 69/90000
|
||||||
// Warning: this will round to the nearest tick
|
// Warning: this will round to the nearest tick
|
||||||
#define delay_ns(X) \
|
#define delay_ns(X) delay_ticks((uint16_t)((((uint32_t)(X)) * 69UL + 89999UL) / 90000UL));
|
||||||
delay_ticks((uint16_t)((((uint32_t)(X)) * 69UL + 89999UL) / 90000UL));
|
|
||||||
|
|
||||||
void delay_ms(uint8_t ms);
|
void delay_ms(uint8_t ms);
|
||||||
|
|
||||||
|
@ -19,11 +19,14 @@ int16_t i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
|
|||||||
TWCR = BIT(TWINT) | BIT(TWSTA) | BIT(TWEN);
|
TWCR = BIT(TWINT) | BIT(TWSTA) | BIT(TWEN);
|
||||||
// wait for end of transmission
|
// wait for end of transmission
|
||||||
count = TIMEOUT;
|
count = TIMEOUT;
|
||||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
while (!(TWCR & BIT(TWINT)) && count > 0)
|
||||||
if (count == 0) return -1;
|
count -= 1;
|
||||||
|
if (count == 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
// check if the start condition was successfully transmitted
|
// check if the start condition was successfully transmitted
|
||||||
if((TWSR & 0xF8) != TW_START) return -1;
|
if ((TWSR & 0xF8) != TW_START)
|
||||||
|
return -1;
|
||||||
|
|
||||||
// load slave addr into data register
|
// load slave addr into data register
|
||||||
TWDR = ((addr << 1) | read);
|
TWDR = ((addr << 1) | read);
|
||||||
@ -31,12 +34,15 @@ int16_t i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
|
|||||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||||
// wait for end of transmission
|
// wait for end of transmission
|
||||||
count = TIMEOUT;
|
count = TIMEOUT;
|
||||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
while (!(TWCR & BIT(TWINT)) && count > 0)
|
||||||
if (count == 0) return -1;
|
count -= 1;
|
||||||
|
if (count == 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
// check if the device has acknowledged the READ / WRITE mode
|
// check if the device has acknowledged the READ / WRITE mode
|
||||||
uint8_t twst = TW_STATUS & 0xF8;
|
uint8_t twst = TW_STATUS & 0xF8;
|
||||||
if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) return -1;
|
if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK))
|
||||||
|
return -1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -55,11 +61,14 @@ int16_t i2c_write(struct I2C * i2c, uint8_t * data, uint16_t length) {
|
|||||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||||
// wait for end of transmission
|
// wait for end of transmission
|
||||||
uint32_t count = TIMEOUT;
|
uint32_t count = TIMEOUT;
|
||||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
while (!(TWCR & BIT(TWINT)) && count > 0)
|
||||||
|
count -= 1;
|
||||||
// timed out
|
// timed out
|
||||||
if (count == 0) return -1;
|
if (count == 0)
|
||||||
|
return -1;
|
||||||
// failed to receive ack
|
// failed to receive ack
|
||||||
if((TWSR & 0xF8) != TW_MT_DATA_ACK) return -1;
|
if ((TWSR & 0xF8) != TW_MT_DATA_ACK)
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
@ -77,8 +86,10 @@ int16_t i2c_read(struct I2C * i2c, uint8_t * data, uint16_t length) {
|
|||||||
}
|
}
|
||||||
// wait for end of transmission
|
// wait for end of transmission
|
||||||
uint32_t count = TIMEOUT;
|
uint32_t count = TIMEOUT;
|
||||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
while (!(TWCR & BIT(TWINT)) && count > 0)
|
||||||
if (count == 0) return -1;
|
count -= 1;
|
||||||
|
if (count == 0)
|
||||||
|
return -1;
|
||||||
// return received data from TWDR
|
// return received data from TWDR
|
||||||
data[i] = TWDR;
|
data[i] = TWDR;
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,12 @@ static void (* volatile i2c_slave_new_cb)() = NULL;
|
|||||||
static void (*volatile i2c_slave_recv_cb)(uint8_t) = NULL;
|
static void (*volatile i2c_slave_recv_cb)(uint8_t) = NULL;
|
||||||
static uint8_t (*volatile i2c_slave_send_cb)() = NULL;
|
static uint8_t (*volatile i2c_slave_send_cb)() = NULL;
|
||||||
|
|
||||||
void i2c_slave_init(uint8_t address, void (*new_cb)(), void (*recv_cb)(uint8_t), uint8_t (*send_cb)()){
|
void i2c_slave_init(
|
||||||
|
uint8_t address,
|
||||||
|
void (*new_cb)(),
|
||||||
|
void (*recv_cb)(uint8_t),
|
||||||
|
uint8_t (*send_cb)()
|
||||||
|
) {
|
||||||
// ensure correct behavior by stopping before changing callbacks or address
|
// ensure correct behavior by stopping before changing callbacks or address
|
||||||
i2c_slave_stop();
|
i2c_slave_stop();
|
||||||
|
|
||||||
|
@ -3,7 +3,12 @@
|
|||||||
#ifndef _ARCH_I2C_SLAVE_H
|
#ifndef _ARCH_I2C_SLAVE_H
|
||||||
#define _ARCH_I2C_SLAVE_H
|
#define _ARCH_I2C_SLAVE_H
|
||||||
|
|
||||||
void i2c_slave_init(uint8_t address, void (*new_cb)(), void (*recv_cb)(uint8_t), uint8_t (*send_cb)());
|
void i2c_slave_init(
|
||||||
|
uint8_t address,
|
||||||
|
void (*new_cb)(),
|
||||||
|
void (*recv_cb)(uint8_t),
|
||||||
|
uint8_t (*send_cb)()
|
||||||
|
);
|
||||||
void i2c_slave_stop();
|
void i2c_slave_stop();
|
||||||
|
|
||||||
#endif // _ARCH_I2C_SLAVE_H
|
#endif // _ARCH_I2C_SLAVE_H
|
||||||
|
@ -68,7 +68,7 @@ uint8_t uart_can_read(struct Uart * uart) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint8_t uart_read(struct Uart *uart) {
|
uint8_t uart_read(struct Uart *uart) {
|
||||||
while (!uart_can_read(uart)) ;
|
while (!uart_can_read(uart)) {}
|
||||||
return *(uart->data);
|
return *(uart->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ uint8_t uart_can_write(struct Uart * uart) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void uart_write(struct Uart *uart, uint8_t data) {
|
void uart_write(struct Uart *uart, uint8_t data) {
|
||||||
while (!uart_can_write(uart)) ;
|
while (!uart_can_write(uart)) {}
|
||||||
*(uart->data) = data;
|
*(uart->data) = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,8 @@ void battery_debug(void) {
|
|||||||
uint16_t data = 0;
|
uint16_t data = 0;
|
||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
#define command(N, A, V) { \
|
#define command(N, A, V) \
|
||||||
|
{ \
|
||||||
printf(#N ": "); \
|
printf(#N ": "); \
|
||||||
res = smbus_read(A, V, &data); \
|
res = smbus_read(A, V, &data); \
|
||||||
if (res < 0) { \
|
if (res < 0) { \
|
||||||
|
@ -89,7 +89,6 @@ static struct Gpio GPIOS[24] = {
|
|||||||
GPIO(A, 0), GPIO(A, 1),
|
GPIO(A, 0), GPIO(A, 1),
|
||||||
};
|
};
|
||||||
#endif // !defined(FLIP)
|
#endif // !defined(FLIP)
|
||||||
// clang-format on
|
|
||||||
|
|
||||||
enum ParallelState {
|
enum ParallelState {
|
||||||
PARALLEL_STATE_UNKNOWN,
|
PARALLEL_STATE_UNKNOWN,
|
||||||
@ -115,6 +114,8 @@ static struct Parallel PORT = {
|
|||||||
.state = PARALLEL_STATE_UNKNOWN,
|
.state = PARALLEL_STATE_UNKNOWN,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
// Set port to all high-impedance inputs
|
// Set port to all high-impedance inputs
|
||||||
void parallel_hiz(struct Parallel *port) {
|
void parallel_hiz(struct Parallel *port) {
|
||||||
#define PIN(N, P) \
|
#define PIN(N, P) \
|
||||||
@ -135,7 +136,9 @@ void parallel_data_dir(struct Parallel * port, bool dir) {
|
|||||||
|
|
||||||
void parallel_data_set_high(struct Parallel *port, uint8_t byte) {
|
void parallel_data_set_high(struct Parallel *port, uint8_t byte) {
|
||||||
// By convention all lines are high, so only set the ones needed
|
// By convention all lines are high, so only set the ones needed
|
||||||
#define DATA_BIT(B) if (!(byte & (1 << B))) gpio_set(port->d ## B, true);
|
#define DATA_BIT(B) \
|
||||||
|
if (!(byte & (1 << B))) \
|
||||||
|
gpio_set(port->d##B, true);
|
||||||
DATA_BITS
|
DATA_BITS
|
||||||
#undef DATA_BIT
|
#undef DATA_BIT
|
||||||
}
|
}
|
||||||
@ -201,7 +204,9 @@ void parallel_state(struct Parallel * port, enum ParallelState state) {
|
|||||||
|
|
||||||
uint8_t parallel_read_data(struct Parallel *port) {
|
uint8_t parallel_read_data(struct Parallel *port) {
|
||||||
uint8_t byte = 0;
|
uint8_t byte = 0;
|
||||||
#define DATA_BIT(B) if (gpio_get(port->d ## B)) byte |= (1 << B);
|
#define DATA_BIT(B) \
|
||||||
|
if (gpio_get(port->d##B)) \
|
||||||
|
byte |= (1 << B);
|
||||||
DATA_BITS
|
DATA_BITS
|
||||||
#undef DATA_BIT
|
#undef DATA_BIT
|
||||||
return byte;
|
return byte;
|
||||||
@ -209,13 +214,22 @@ uint8_t parallel_read_data(struct Parallel * port) {
|
|||||||
|
|
||||||
void parallel_write_data(struct Parallel *port, uint8_t byte) {
|
void parallel_write_data(struct Parallel *port, uint8_t byte) {
|
||||||
// By convention all lines are high, so only set the ones needed
|
// By convention all lines are high, so only set the ones needed
|
||||||
#define DATA_BIT(B) if (!(byte & (1 << B))) gpio_set(port->d ## B, false);
|
|
||||||
|
#define DATA_BIT(B) \
|
||||||
|
if (!(byte & (1 << B))) \
|
||||||
|
gpio_set(port->d##B, false);
|
||||||
DATA_BITS
|
DATA_BITS
|
||||||
#undef DATA_BIT
|
#undef DATA_BIT
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: timeout
|
//TODO: timeout
|
||||||
int16_t parallel_transaction(struct Parallel * port, uint8_t * data, int16_t length, bool read, bool addr) {
|
int16_t parallel_transaction(
|
||||||
|
struct Parallel *port,
|
||||||
|
uint8_t *data,
|
||||||
|
int16_t length,
|
||||||
|
bool read,
|
||||||
|
bool addr
|
||||||
|
) {
|
||||||
if (!read) {
|
if (!read) {
|
||||||
// Set write line low
|
// Set write line low
|
||||||
gpio_set(port->write_n, false);
|
gpio_set(port->write_n, false);
|
||||||
@ -341,19 +355,24 @@ int16_t parallel_ecms_read(struct Parallel *port, uint16_t addr, uint8_t * data,
|
|||||||
int16_t res;
|
int16_t res;
|
||||||
|
|
||||||
res = parallel_set_address(port, &ADDRESS_ECMSADDR1, 1);
|
res = parallel_set_address(port, &ADDRESS_ECMSADDR1, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_write(port, ((uint8_t *)&addr) + 1, 1);
|
res = parallel_write(port, ((uint8_t *)&addr) + 1, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_set_address(port, &ADDRESS_ECMSADDR0, 1);
|
res = parallel_set_address(port, &ADDRESS_ECMSADDR0, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_write(port, (uint8_t *)&addr, 1);
|
res = parallel_write(port, (uint8_t *)&addr, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_set_address(port, &ADDRESS_ECMSDATA, 1);
|
res = parallel_set_address(port, &ADDRESS_ECMSDATA, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
return parallel_read(port, data, length);
|
return parallel_read(port, data, length);
|
||||||
}
|
}
|
||||||
@ -363,13 +382,16 @@ int16_t parallel_spi_reset(struct Parallel *port) {
|
|||||||
int16_t res;
|
int16_t res;
|
||||||
|
|
||||||
res = parallel_set_address(port, &ADDRESS_INDAR1, 1);
|
res = parallel_set_address(port, &ADDRESS_INDAR1, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_write(port, &SPI_ENABLE, 1);
|
res = parallel_write(port, &SPI_ENABLE, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_set_address(port, &ADDRESS_INDDR, 1);
|
res = parallel_set_address(port, &ADDRESS_INDDR, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
return parallel_write(port, &ZERO, 1);
|
return parallel_write(port, &ZERO, 1);
|
||||||
}
|
}
|
||||||
@ -379,13 +401,16 @@ int16_t parallel_spi_transaction(struct Parallel *port, uint8_t * data, int16_t
|
|||||||
int16_t res;
|
int16_t res;
|
||||||
|
|
||||||
res = parallel_set_address(port, &ADDRESS_INDAR1, 1);
|
res = parallel_set_address(port, &ADDRESS_INDAR1, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_write(port, &SPI_DATA, 1);
|
res = parallel_write(port, &SPI_DATA, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_set_address(port, &ADDRESS_INDDR, 1);
|
res = parallel_set_address(port, &ADDRESS_INDDR, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
return parallel_transaction(port, data, length, read, false);
|
return parallel_transaction(port, data, length, read, false);
|
||||||
}
|
}
|
||||||
@ -394,7 +419,12 @@ int16_t parallel_spi_transaction(struct Parallel *port, uint8_t * data, int16_t
|
|||||||
#define parallel_spi_write(P, D, L) parallel_spi_transaction(P, D, L, false)
|
#define parallel_spi_write(P, D, L) parallel_spi_transaction(P, D, L, false)
|
||||||
|
|
||||||
// "Hardware" accelerated SPI programming, requires ECINDARs to be set
|
// "Hardware" accelerated SPI programming, requires ECINDARs to be set
|
||||||
int16_t parallel_spi_program(struct Parallel * port, uint8_t * data, int16_t length, bool initialized) {
|
int16_t parallel_spi_program(
|
||||||
|
struct Parallel *port,
|
||||||
|
uint8_t *data,
|
||||||
|
int16_t length,
|
||||||
|
bool initialized
|
||||||
|
) {
|
||||||
static uint8_t aai[6] = { 0xAD, 0, 0, 0, 0, 0 };
|
static uint8_t aai[6] = { 0xAD, 0, 0, 0, 0, 0 };
|
||||||
int16_t res;
|
int16_t res;
|
||||||
int16_t i;
|
int16_t i;
|
||||||
@ -403,7 +433,8 @@ int16_t parallel_spi_program(struct Parallel * port, uint8_t * data, int16_t len
|
|||||||
for (i = 0; (i + 1) < length; i += 2) {
|
for (i = 0; (i + 1) < length; i += 2) {
|
||||||
// Disable chip to begin command
|
// Disable chip to begin command
|
||||||
res = parallel_spi_reset(port);
|
res = parallel_spi_reset(port);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
// If not initialized, the start address must be sent
|
// If not initialized, the start address must be sent
|
||||||
@ -415,7 +446,8 @@ int16_t parallel_spi_program(struct Parallel * port, uint8_t * data, int16_t len
|
|||||||
aai[5] = data[i + 1];
|
aai[5] = data[i + 1];
|
||||||
|
|
||||||
res = parallel_spi_write(port, aai, 6);
|
res = parallel_spi_write(port, aai, 6);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
} else {
|
} else {
|
||||||
@ -423,23 +455,28 @@ int16_t parallel_spi_program(struct Parallel * port, uint8_t * data, int16_t len
|
|||||||
aai[2] = data[i + 1];
|
aai[2] = data[i + 1];
|
||||||
|
|
||||||
res = parallel_spi_write(port, aai, 3);
|
res = parallel_spi_write(port, aai, 3);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for SPI busy flag to clear
|
// Wait for SPI busy flag to clear
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// Disable chip to begin command
|
// Disable chip to begin command
|
||||||
res = parallel_spi_reset(port);
|
res = parallel_spi_reset(port);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
status = 0x05;
|
status = 0x05;
|
||||||
res = parallel_spi_write(port, &status, 1);
|
res = parallel_spi_write(port, &status, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_spi_read(port, &status, 1);
|
res = parallel_spi_read(port, &status, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
if (!(status & 1)) break;
|
if (!(status & 1))
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -481,7 +518,8 @@ int parallel_main(void) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
// Read command and length
|
// Read command and length
|
||||||
res = serial_read(data, 2);
|
res = serial_read(data, 2);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
// Command is a character
|
// Command is a character
|
||||||
command = (char)data[0];
|
command = (char)data[0];
|
||||||
|
|
||||||
@ -499,7 +537,8 @@ int parallel_main(void) {
|
|||||||
// Length is received data + 1
|
// Length is received data + 1
|
||||||
length = ((int16_t)data[1]) + 1;
|
length = ((int16_t)data[1]) + 1;
|
||||||
// Truncate length to size of data
|
// Truncate length to size of data
|
||||||
if (length > sizeof(data)) length = sizeof(data);
|
if (length > sizeof(data))
|
||||||
|
length = sizeof(data);
|
||||||
|
|
||||||
switch (command) {
|
switch (command) {
|
||||||
// Buffer size
|
// Buffer size
|
||||||
@ -515,7 +554,8 @@ int parallel_main(void) {
|
|||||||
|
|
||||||
// Write data to serial
|
// Write data to serial
|
||||||
res = serial_write(data, length);
|
res = serial_write(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -532,7 +572,8 @@ int parallel_main(void) {
|
|||||||
if (parallel_peripheral_cycle(port, data, &read, &addr)) {
|
if (parallel_peripheral_cycle(port, data, &read, &addr)) {
|
||||||
if (!read && !addr) {
|
if (!read && !addr) {
|
||||||
res = serial_write(data, 1);
|
res = serial_write(data, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -543,11 +584,13 @@ int parallel_main(void) {
|
|||||||
case 'E':
|
case 'E':
|
||||||
// Read data from serial
|
// Read data from serial
|
||||||
res = serial_read(data, length);
|
res = serial_read(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
// Write data to serial
|
// Write data to serial
|
||||||
res = serial_write(data, length);
|
res = serial_write(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -562,7 +605,8 @@ int parallel_main(void) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
// Read current position
|
// Read current position
|
||||||
res = parallel_ecms_read(port, 0xF00, data, 1);
|
res = parallel_ecms_read(port, 0xF00, data, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
uint16_t tail = (uint16_t)data[0];
|
uint16_t tail = (uint16_t)data[0];
|
||||||
if (tail == 0 || head == tail) {
|
if (tail == 0 || head == tail) {
|
||||||
@ -578,11 +622,14 @@ int parallel_main(void) {
|
|||||||
|
|
||||||
while (head != tail) {
|
while (head != tail) {
|
||||||
head += 1;
|
head += 1;
|
||||||
if (head >= 256) { head = 1; }
|
if (head >= 256) {
|
||||||
|
head = 1;
|
||||||
|
}
|
||||||
|
|
||||||
// Read byte at head
|
// Read byte at head
|
||||||
res = parallel_ecms_read(port, 0xF00 + head, data, 1);
|
res = parallel_ecms_read(port, 0xF00 + head, data, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
// Print read byte
|
// Print read byte
|
||||||
serial_write(data, 1);
|
serial_write(data, 1);
|
||||||
@ -598,17 +645,20 @@ int parallel_main(void) {
|
|||||||
// Update parallel address if necessary
|
// Update parallel address if necessary
|
||||||
if (set_address) {
|
if (set_address) {
|
||||||
res = parallel_set_address(port, &address, 1);
|
res = parallel_set_address(port, &address, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
set_address = false;
|
set_address = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read data from parallel
|
// Read data from parallel
|
||||||
res = parallel_read(port, data, length);
|
res = parallel_read(port, data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
// Write data to serial
|
// Write data to serial
|
||||||
res = serial_write(data, length);
|
res = serial_write(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -618,17 +668,20 @@ int parallel_main(void) {
|
|||||||
|
|
||||||
// Read data from serial
|
// Read data from serial
|
||||||
res = serial_read(data, length);
|
res = serial_read(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
// Run accelerated programming function
|
// Run accelerated programming function
|
||||||
res = parallel_spi_program(port, data, length, program_aai);
|
res = parallel_spi_program(port, data, length, program_aai);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
program_aai = true;
|
program_aai = true;
|
||||||
|
|
||||||
// Send ACK of data length
|
// Send ACK of data length
|
||||||
data[0] = (uint8_t)(length - 1);
|
data[0] = (uint8_t)(length - 1);
|
||||||
res = serial_write(data, 1);
|
res = serial_write(data, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -638,23 +691,27 @@ int parallel_main(void) {
|
|||||||
|
|
||||||
// Read data from serial
|
// Read data from serial
|
||||||
res = serial_read(data, length);
|
res = serial_read(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
// Update parallel address if necessary
|
// Update parallel address if necessary
|
||||||
if (set_address) {
|
if (set_address) {
|
||||||
res = parallel_set_address(port, &address, 1);
|
res = parallel_set_address(port, &address, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
set_address = false;
|
set_address = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write data to parallel
|
// Write data to parallel
|
||||||
res = parallel_write(port, data, length);
|
res = parallel_write(port, data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
// Send ACK of data length
|
// Send ACK of data length
|
||||||
data[0] = (uint8_t)(length - 1);
|
data[0] = (uint8_t)(length - 1);
|
||||||
res = serial_write(data, 1);
|
res = serial_write(data, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,8 @@ void battery_debug(void) {
|
|||||||
uint16_t data = 0;
|
uint16_t data = 0;
|
||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
#define command(N, A, V) { \
|
#define command(N, A, V) \
|
||||||
|
{ \
|
||||||
printf(#N ": "); \
|
printf(#N ": "); \
|
||||||
res = smbus_read(A, V, &data); \
|
res = smbus_read(A, V, &data); \
|
||||||
if (res < 0) { \
|
if (res < 0) { \
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include <arch/uart.h>
|
#include <arch/uart.h>
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
|
|
||||||
// Mapping of 24-pin ribbon cable to parallel pins. See schematic
|
// Mapping of 24-pin ribbon cable to parallel pins. See schematic
|
||||||
#define PINS \
|
#define PINS \
|
||||||
/* Data (KSO0 - KSO7) - bi-directional */ \
|
/* Data (KSO0 - KSO7) - bi-directional */ \
|
||||||
@ -65,6 +66,7 @@ static struct Gpio GPIOS[13] = {
|
|||||||
GPIO(C, 1),
|
GPIO(C, 1),
|
||||||
GPIO(C, 0),
|
GPIO(C, 0),
|
||||||
};
|
};
|
||||||
|
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
// Parallel struct definition
|
// Parallel struct definition
|
||||||
@ -102,7 +104,9 @@ void parallel_data_dir(struct Parallel * port, bool dir) {
|
|||||||
|
|
||||||
void parallel_data_set_high(struct Parallel *port, uint8_t byte) {
|
void parallel_data_set_high(struct Parallel *port, uint8_t byte) {
|
||||||
// By convention all lines are high, so only set the ones needed
|
// By convention all lines are high, so only set the ones needed
|
||||||
#define DATA_BIT(B) if (!(byte & (1 << B))) gpio_set(port->d ## B, true);
|
#define DATA_BIT(B) \
|
||||||
|
if (!(byte & (1 << B))) \
|
||||||
|
gpio_set(port->d##B, true);
|
||||||
DATA_BITS
|
DATA_BITS
|
||||||
#undef DATA_BIT
|
#undef DATA_BIT
|
||||||
}
|
}
|
||||||
@ -149,7 +153,9 @@ void parallel_reset(struct Parallel * port, bool host) {
|
|||||||
|
|
||||||
uint8_t parallel_read_data(struct Parallel *port) {
|
uint8_t parallel_read_data(struct Parallel *port) {
|
||||||
uint8_t byte = 0;
|
uint8_t byte = 0;
|
||||||
#define DATA_BIT(B) if (gpio_get(port->d ## B)) byte |= (1 << B);
|
#define DATA_BIT(B) \
|
||||||
|
if (gpio_get(port->d##B)) \
|
||||||
|
byte |= (1 << B);
|
||||||
DATA_BITS
|
DATA_BITS
|
||||||
#undef DATA_BIT
|
#undef DATA_BIT
|
||||||
return byte;
|
return byte;
|
||||||
@ -157,13 +163,21 @@ uint8_t parallel_read_data(struct Parallel * port) {
|
|||||||
|
|
||||||
void parallel_write_data(struct Parallel *port, uint8_t byte) {
|
void parallel_write_data(struct Parallel *port, uint8_t byte) {
|
||||||
// By convention all lines are high, so only set the ones needed
|
// By convention all lines are high, so only set the ones needed
|
||||||
#define DATA_BIT(B) if (!(byte & (1 << B))) gpio_set(port->d ## B, false);
|
#define DATA_BIT(B) \
|
||||||
|
if (!(byte & (1 << B))) \
|
||||||
|
gpio_set(port->d##B, false);
|
||||||
DATA_BITS
|
DATA_BITS
|
||||||
#undef DATA_BIT
|
#undef DATA_BIT
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: timeout
|
//TODO: timeout
|
||||||
int16_t parallel_transaction(struct Parallel * port, uint8_t * data, int16_t length, bool read, bool addr) {
|
int16_t parallel_transaction(
|
||||||
|
struct Parallel *port,
|
||||||
|
uint8_t *data,
|
||||||
|
int16_t length,
|
||||||
|
bool read,
|
||||||
|
bool addr
|
||||||
|
) {
|
||||||
if (!read) {
|
if (!read) {
|
||||||
// Set write line low
|
// Set write line low
|
||||||
gpio_set(port->write_n, false);
|
gpio_set(port->write_n, false);
|
||||||
@ -283,13 +297,16 @@ int16_t parallel_spi_reset(struct Parallel *port) {
|
|||||||
int16_t res;
|
int16_t res;
|
||||||
|
|
||||||
res = parallel_set_address(port, &ADDRESS_INDAR1, 1);
|
res = parallel_set_address(port, &ADDRESS_INDAR1, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_write(port, &SPI_ENABLE, 1);
|
res = parallel_write(port, &SPI_ENABLE, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_set_address(port, &ADDRESS_INDDR, 1);
|
res = parallel_set_address(port, &ADDRESS_INDDR, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
return parallel_write(port, &ZERO, 1);
|
return parallel_write(port, &ZERO, 1);
|
||||||
}
|
}
|
||||||
@ -299,13 +316,16 @@ int16_t parallel_spi_transaction(struct Parallel *port, uint8_t * data, int16_t
|
|||||||
int16_t res;
|
int16_t res;
|
||||||
|
|
||||||
res = parallel_set_address(port, &ADDRESS_INDAR1, 1);
|
res = parallel_set_address(port, &ADDRESS_INDAR1, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_write(port, &SPI_DATA, 1);
|
res = parallel_write(port, &SPI_DATA, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_set_address(port, &ADDRESS_INDDR, 1);
|
res = parallel_set_address(port, &ADDRESS_INDDR, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
return parallel_transaction(port, data, length, read, false);
|
return parallel_transaction(port, data, length, read, false);
|
||||||
}
|
}
|
||||||
@ -314,7 +334,12 @@ int16_t parallel_spi_transaction(struct Parallel *port, uint8_t * data, int16_t
|
|||||||
#define parallel_spi_write(P, D, L) parallel_spi_transaction(P, D, L, false)
|
#define parallel_spi_write(P, D, L) parallel_spi_transaction(P, D, L, false)
|
||||||
|
|
||||||
// "Hardware" accelerated SPI programming, requires ECINDARs to be set
|
// "Hardware" accelerated SPI programming, requires ECINDARs to be set
|
||||||
int16_t parallel_spi_program(struct Parallel * port, uint8_t * data, int16_t length, bool initialized) {
|
int16_t parallel_spi_program(
|
||||||
|
struct Parallel *port,
|
||||||
|
uint8_t *data,
|
||||||
|
int16_t length,
|
||||||
|
bool initialized
|
||||||
|
) {
|
||||||
static uint8_t aai[6] = { 0xAD, 0, 0, 0, 0, 0 };
|
static uint8_t aai[6] = { 0xAD, 0, 0, 0, 0, 0 };
|
||||||
int16_t res;
|
int16_t res;
|
||||||
int16_t i;
|
int16_t i;
|
||||||
@ -323,7 +348,8 @@ int16_t parallel_spi_program(struct Parallel * port, uint8_t * data, int16_t len
|
|||||||
for (i = 0; (i + 1) < length; i += 2) {
|
for (i = 0; (i + 1) < length; i += 2) {
|
||||||
// Disable chip to begin command
|
// Disable chip to begin command
|
||||||
res = parallel_spi_reset(port);
|
res = parallel_spi_reset(port);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
// If not initialized, the start address must be sent
|
// If not initialized, the start address must be sent
|
||||||
@ -335,7 +361,8 @@ int16_t parallel_spi_program(struct Parallel * port, uint8_t * data, int16_t len
|
|||||||
aai[5] = data[i + 1];
|
aai[5] = data[i + 1];
|
||||||
|
|
||||||
res = parallel_spi_write(port, aai, 6);
|
res = parallel_spi_write(port, aai, 6);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
} else {
|
} else {
|
||||||
@ -343,23 +370,28 @@ int16_t parallel_spi_program(struct Parallel * port, uint8_t * data, int16_t len
|
|||||||
aai[2] = data[i + 1];
|
aai[2] = data[i + 1];
|
||||||
|
|
||||||
res = parallel_spi_write(port, aai, 3);
|
res = parallel_spi_write(port, aai, 3);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for SPI busy flag to clear
|
// Wait for SPI busy flag to clear
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// Disable chip to begin command
|
// Disable chip to begin command
|
||||||
res = parallel_spi_reset(port);
|
res = parallel_spi_reset(port);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
status = 0x05;
|
status = 0x05;
|
||||||
res = parallel_spi_write(port, &status, 1);
|
res = parallel_spi_write(port, &status, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = parallel_spi_read(port, &status, 1);
|
res = parallel_spi_read(port, &status, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
if (!(status & 1)) break;
|
if (!(status & 1))
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -401,7 +433,8 @@ int16_t parallel_main(void) {
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
// Read command and length
|
// Read command and length
|
||||||
res = serial_read(data, 2);
|
res = serial_read(data, 2);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
// Command is a character
|
// Command is a character
|
||||||
command = (char)data[0];
|
command = (char)data[0];
|
||||||
|
|
||||||
@ -419,7 +452,8 @@ int16_t parallel_main(void) {
|
|||||||
// Length is received data + 1
|
// Length is received data + 1
|
||||||
length = ((int16_t)data[1]) + 1;
|
length = ((int16_t)data[1]) + 1;
|
||||||
// Truncate length to size of data
|
// Truncate length to size of data
|
||||||
if (length > sizeof(data)) length = sizeof(data);
|
if (length > sizeof(data))
|
||||||
|
length = sizeof(data);
|
||||||
|
|
||||||
switch (command) {
|
switch (command) {
|
||||||
// Buffer size
|
// Buffer size
|
||||||
@ -435,7 +469,8 @@ int16_t parallel_main(void) {
|
|||||||
|
|
||||||
// Write data to serial
|
// Write data to serial
|
||||||
res = serial_write(data, length);
|
res = serial_write(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -453,7 +488,8 @@ int16_t parallel_main(void) {
|
|||||||
|
|
||||||
if (ret && !read && !addr) {
|
if (ret && !read && !addr) {
|
||||||
res = serial_write(data, 1);
|
res = serial_write(data, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -463,11 +499,13 @@ int16_t parallel_main(void) {
|
|||||||
case 'E':
|
case 'E':
|
||||||
// Read data from serial
|
// Read data from serial
|
||||||
res = serial_read(data, length);
|
res = serial_read(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
// Write data to serial
|
// Write data to serial
|
||||||
res = serial_write(data, length);
|
res = serial_write(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -476,17 +514,20 @@ int16_t parallel_main(void) {
|
|||||||
// Update parallel address if necessary
|
// Update parallel address if necessary
|
||||||
if (set_address) {
|
if (set_address) {
|
||||||
res = parallel_set_address(port, &address, 1);
|
res = parallel_set_address(port, &address, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
set_address = false;
|
set_address = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read data from parallel
|
// Read data from parallel
|
||||||
res = parallel_read(port, data, length);
|
res = parallel_read(port, data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
// Write data to serial
|
// Write data to serial
|
||||||
res = serial_write(data, length);
|
res = serial_write(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -494,17 +535,20 @@ int16_t parallel_main(void) {
|
|||||||
case 'P':
|
case 'P':
|
||||||
// Read data from serial
|
// Read data from serial
|
||||||
res = serial_read(data, length);
|
res = serial_read(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
// Run accelerated programming function
|
// Run accelerated programming function
|
||||||
res = parallel_spi_program(port, data, length, program_aai);
|
res = parallel_spi_program(port, data, length, program_aai);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
program_aai = true;
|
program_aai = true;
|
||||||
|
|
||||||
// Send ACK of data length
|
// Send ACK of data length
|
||||||
data[0] = (uint8_t)(length - 1);
|
data[0] = (uint8_t)(length - 1);
|
||||||
res = serial_write(data, 1);
|
res = serial_write(data, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -512,23 +556,27 @@ int16_t parallel_main(void) {
|
|||||||
case 'W':
|
case 'W':
|
||||||
// Read data from serial
|
// Read data from serial
|
||||||
res = serial_read(data, length);
|
res = serial_read(data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
// Update parallel address if necessary
|
// Update parallel address if necessary
|
||||||
if (set_address) {
|
if (set_address) {
|
||||||
res = parallel_set_address(port, &address, 1);
|
res = parallel_set_address(port, &address, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
set_address = false;
|
set_address = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write data to parallel
|
// Write data to parallel
|
||||||
res = parallel_write(port, data, length);
|
res = parallel_write(port, data, length);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
// Send ACK of data length
|
// Send ACK of data length
|
||||||
data[0] = (uint8_t)(length - 1);
|
data[0] = (uint8_t)(length - 1);
|
||||||
res = serial_write(data, 1);
|
res = serial_write(data, 1);
|
||||||
if (res < 0) goto err;
|
if (res < 0)
|
||||||
|
goto err;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -40,11 +40,13 @@ void fcommand(void) {
|
|||||||
break;
|
break;
|
||||||
// Set LED color
|
// Set LED color
|
||||||
case 0x03:
|
case 0x03:
|
||||||
|
// clang-format off
|
||||||
kbled_set_color(
|
kbled_set_color(
|
||||||
((uint32_t)fbuf[0]) |
|
((uint32_t)fbuf[0]) |
|
||||||
((uint32_t)fbuf[1] << 16) |
|
((uint32_t)fbuf[1] << 16) |
|
||||||
((uint32_t)fbuf[2] << 8)
|
((uint32_t)fbuf[2] << 8)
|
||||||
);
|
);
|
||||||
|
// clang-format on
|
||||||
break;
|
break;
|
||||||
// Set LED brightness
|
// Set LED brightness
|
||||||
case 0x06:
|
case 0x06:
|
||||||
|
@ -57,16 +57,13 @@ int16_t battery_charger_configure(void) {
|
|||||||
if (battery_get_end_threshold() == BATTERY_END_DEFAULT) {
|
if (battery_get_end_threshold() == BATTERY_END_DEFAULT) {
|
||||||
// Stop threshold not configured: Always charge on AC.
|
// Stop threshold not configured: Always charge on AC.
|
||||||
should_charge = true;
|
should_charge = true;
|
||||||
}
|
} else if (battery_info.charge > battery_get_end_threshold()) {
|
||||||
else if (battery_info.charge > battery_get_end_threshold()) {
|
|
||||||
// Stop threshold configured: Stop charging at threshold.
|
// Stop threshold configured: Stop charging at threshold.
|
||||||
should_charge = false;
|
should_charge = false;
|
||||||
}
|
} else if (battery_get_start_threshold() == BATTERY_START_DEFAULT) {
|
||||||
else if (battery_get_start_threshold() == BATTERY_START_DEFAULT) {
|
|
||||||
// Start threshold not configured: Always charge up to stop threshold.
|
// Start threshold not configured: Always charge up to stop threshold.
|
||||||
should_charge = true;
|
should_charge = true;
|
||||||
}
|
} else if (battery_info.charge < battery_get_start_threshold()) {
|
||||||
else if (battery_info.charge < battery_get_start_threshold()) {
|
|
||||||
// Start threshold configured: Start charging at threshold.
|
// Start threshold configured: Start charging at threshold.
|
||||||
should_charge = true;
|
should_charge = true;
|
||||||
}
|
}
|
||||||
@ -79,7 +76,8 @@ int16_t battery_charger_configure(void) {
|
|||||||
void battery_event(void) {
|
void battery_event(void) {
|
||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
#define command(N, V) { \
|
#define command(N, V) \
|
||||||
|
{ \
|
||||||
res = smbus_read(BATTERY_ADDRESS, V, &N); \
|
res = smbus_read(BATTERY_ADDRESS, V, &N); \
|
||||||
if (res < 0) { \
|
if (res < 0) { \
|
||||||
N = 0; \
|
N = 0; \
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
#include <common/macro.h>
|
#include <common/macro.h>
|
||||||
#include <common/debug.h>
|
#include <common/debug.h>
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
// Registers
|
// Registers
|
||||||
#define REG_CHARGE_CURRENT 0x14
|
#define REG_CHARGE_CURRENT 0x14
|
||||||
#define REG_CHARGE_VOLTAGE 0x15
|
#define REG_CHARGE_VOLTAGE 0x15
|
||||||
@ -40,7 +42,6 @@
|
|||||||
// Battery depletion threshold
|
// Battery depletion threshold
|
||||||
#define SBC_BAT_DEPL_VTH (0b11 << 14)
|
#define SBC_BAT_DEPL_VTH (0b11 << 14)
|
||||||
|
|
||||||
|
|
||||||
// Bits 0-5 are ignored. Bits 13-15 must be 0.
|
// Bits 0-5 are ignored. Bits 13-15 must be 0.
|
||||||
#define CHARGE_CURRENT_MASK 0x1FC0
|
#define CHARGE_CURRENT_MASK 0x1FC0
|
||||||
|
|
||||||
@ -85,35 +86,38 @@
|
|||||||
#error Invalid adapter:battery RSENSE ratio
|
#error Invalid adapter:battery RSENSE ratio
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
// XXX: Assumption: ac_last is initialized high.
|
// XXX: Assumption: ac_last is initialized high.
|
||||||
static bool charger_enabled = false;
|
static bool charger_enabled = false;
|
||||||
|
|
||||||
int16_t battery_charger_disable(void) {
|
int16_t battery_charger_disable(void) {
|
||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
if (!charger_enabled) return 0;
|
if (!charger_enabled)
|
||||||
|
return 0;
|
||||||
|
|
||||||
// Set charge option 0 with 175s watchdog
|
// Set charge option 0 with 175s watchdog
|
||||||
res = smbus_write(
|
res = smbus_write(
|
||||||
CHARGER_ADDRESS,
|
CHARGER_ADDRESS,
|
||||||
REG_CHARGE_OPTION_0,
|
REG_CHARGE_OPTION_0,
|
||||||
SBC_EN_LWPWR |
|
SBC_EN_LWPWR | SBC_WDTMR_ADJ_175S | SBC_PWM_FREQ_800KHZ | SBC_IDCHC_GAIN
|
||||||
SBC_WDTMR_ADJ_175S |
|
|
||||||
SBC_PWM_FREQ_800KHZ |
|
|
||||||
SBC_IDCHC_GAIN
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Disable charge current
|
// Disable charge current
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_CURRENT, 0);
|
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_CURRENT, 0);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Disable charge voltage
|
// Disable charge voltage
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_VOLTAGE, 0);
|
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_VOLTAGE, 0);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Disable input current
|
// Disable input current
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_INPUT_CURRENT, 0);
|
res = smbus_write(CHARGER_ADDRESS, REG_INPUT_CURRENT, 0);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
DEBUG("Charger disabled\n");
|
DEBUG("Charger disabled\n");
|
||||||
charger_enabled = false;
|
charger_enabled = false;
|
||||||
@ -123,40 +127,40 @@ int16_t battery_charger_disable(void) {
|
|||||||
int16_t battery_charger_enable(void) {
|
int16_t battery_charger_enable(void) {
|
||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
if (charger_enabled) return 0;
|
if (charger_enabled)
|
||||||
|
return 0;
|
||||||
|
|
||||||
res = battery_charger_disable();
|
res = battery_charger_disable();
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Set charge current in mA
|
// Set charge current in mA
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_CURRENT, CHARGE_CURRENT);
|
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_CURRENT, CHARGE_CURRENT);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Set charge voltage in mV
|
// Set charge voltage in mV
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_VOLTAGE, CHARGE_VOLTAGE);
|
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_VOLTAGE, CHARGE_VOLTAGE);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Set input current in mA
|
// Set input current in mA
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_INPUT_CURRENT, INPUT_CURRENT);
|
res = smbus_write(CHARGER_ADDRESS, REG_INPUT_CURRENT, INPUT_CURRENT);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Set charge option 0 with watchdog disabled
|
// Set charge option 0 with watchdog disabled
|
||||||
res = smbus_write(
|
res = smbus_write(
|
||||||
CHARGER_ADDRESS,
|
CHARGER_ADDRESS,
|
||||||
REG_CHARGE_OPTION_0,
|
REG_CHARGE_OPTION_0,
|
||||||
SBC_EN_LWPWR |
|
SBC_EN_LWPWR | SBC_PWM_FREQ_800KHZ | SBC_IDCHC_GAIN
|
||||||
SBC_PWM_FREQ_800KHZ |
|
|
||||||
SBC_IDCHC_GAIN
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Set the RSENSE ratio
|
// Set the RSENSE ratio
|
||||||
res = smbus_write(
|
res = smbus_write(
|
||||||
CHARGER_ADDRESS,
|
CHARGER_ADDRESS,
|
||||||
REG_CHARGE_OPTION_1,
|
REG_CHARGE_OPTION_1,
|
||||||
SBC_CMP_DEG_1US |
|
SBC_CMP_DEG_1US | SBC_PMON_RATIO | RSENSE_RATIO | SBC_BAT_DEPL_VTH
|
||||||
SBC_PMON_RATIO |
|
|
||||||
RSENSE_RATIO |
|
|
||||||
SBC_BAT_DEPL_VTH
|
|
||||||
);
|
);
|
||||||
|
|
||||||
DEBUG("Charger enabled\n");
|
DEBUG("Charger enabled\n");
|
||||||
@ -172,7 +176,8 @@ void battery_debug(void) {
|
|||||||
uint16_t data = 0;
|
uint16_t data = 0;
|
||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
#define command(N, A, V) { \
|
#define command(N, A, V) \
|
||||||
|
{ \
|
||||||
DEBUG(" " #N ": "); \
|
DEBUG(" " #N ": "); \
|
||||||
res = smbus_read(A, V, &data); \
|
res = smbus_read(A, V, &data); \
|
||||||
if (res < 0) { \
|
if (res < 0) { \
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
#include <common/debug.h>
|
#include <common/debug.h>
|
||||||
#include <common/macro.h>
|
#include <common/macro.h>
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
// Registers
|
// Registers
|
||||||
#define REG_CHARGE_CURRENT 0x14
|
#define REG_CHARGE_CURRENT 0x14
|
||||||
#define REG_CHARGE_VOLTAGE 0x15
|
#define REG_CHARGE_VOLTAGE 0x15
|
||||||
@ -54,6 +56,8 @@
|
|||||||
#error Invalid adapter RSENSE value
|
#error Invalid adapter RSENSE value
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
// Sense resistor values in milliohms.
|
// Sense resistor values in milliohms.
|
||||||
enum sense_resistor {
|
enum sense_resistor {
|
||||||
RSENSE_10 = 0,
|
RSENSE_10 = 0,
|
||||||
@ -67,7 +71,8 @@ static bool charger_enabled = false;
|
|||||||
int16_t battery_charger_disable(void) {
|
int16_t battery_charger_disable(void) {
|
||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
if (!charger_enabled) return 0;
|
if (!charger_enabled)
|
||||||
|
return 0;
|
||||||
|
|
||||||
// Set charge option 1 to converter frequency 600 KHz
|
// Set charge option 1 to converter frequency 600 KHz
|
||||||
//TODO: needed when charging disabled?
|
//TODO: needed when charging disabled?
|
||||||
@ -76,29 +81,30 @@ int16_t battery_charger_disable(void) {
|
|||||||
REG_CHARGE_OPTION_1,
|
REG_CHARGE_OPTION_1,
|
||||||
CHARGE_OPTION_1_600KHZ | ADAPTER_RSENSE | BATTERY_RSENSE
|
CHARGE_OPTION_1_600KHZ | ADAPTER_RSENSE | BATTERY_RSENSE
|
||||||
);
|
);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Set charge option 2 to PSYS enable
|
// Set charge option 2 to PSYS enable
|
||||||
//TODO: needed when charging disabled?
|
//TODO: needed when charging disabled?
|
||||||
res = smbus_write(
|
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_OPTION_2, CHARGE_OPTION_2_PSYS_EN);
|
||||||
CHARGER_ADDRESS,
|
if (res < 0)
|
||||||
REG_CHARGE_OPTION_2,
|
return res;
|
||||||
CHARGE_OPTION_2_PSYS_EN
|
|
||||||
);
|
|
||||||
if (res < 0) return res;
|
|
||||||
|
|
||||||
// Disable charge current
|
// Disable charge current
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_CURRENT, 0);
|
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_CURRENT, 0);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Disable charge voltage
|
// Disable charge voltage
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_VOLTAGE, 0);
|
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_VOLTAGE, 0);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Set input current in mA
|
// Set input current in mA
|
||||||
//TODO: needed when charging disabled?
|
//TODO: needed when charging disabled?
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_ADAPTER_CURRENT, INPUT_CURRENT);
|
res = smbus_write(CHARGER_ADDRESS, REG_ADAPTER_CURRENT, INPUT_CURRENT);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
DEBUG("Charger disabled\n");
|
DEBUG("Charger disabled\n");
|
||||||
charger_enabled = false;
|
charger_enabled = false;
|
||||||
@ -108,10 +114,12 @@ int16_t battery_charger_disable(void) {
|
|||||||
int16_t battery_charger_enable(void) {
|
int16_t battery_charger_enable(void) {
|
||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
if (charger_enabled) return 0;
|
if (charger_enabled)
|
||||||
|
return 0;
|
||||||
|
|
||||||
res = battery_charger_disable();
|
res = battery_charger_disable();
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Set charge option 1 to converter frequency 600 KHz
|
// Set charge option 1 to converter frequency 600 KHz
|
||||||
res = smbus_write(
|
res = smbus_write(
|
||||||
@ -119,27 +127,28 @@ int16_t battery_charger_enable(void) {
|
|||||||
REG_CHARGE_OPTION_1,
|
REG_CHARGE_OPTION_1,
|
||||||
CHARGE_OPTION_1_600KHZ | ADAPTER_RSENSE | BATTERY_RSENSE
|
CHARGE_OPTION_1_600KHZ | ADAPTER_RSENSE | BATTERY_RSENSE
|
||||||
);
|
);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Set charge option 2 to PSYS enable
|
// Set charge option 2 to PSYS enable
|
||||||
res = smbus_write(
|
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_OPTION_2, CHARGE_OPTION_2_PSYS_EN);
|
||||||
CHARGER_ADDRESS,
|
if (res < 0)
|
||||||
REG_CHARGE_OPTION_2,
|
return res;
|
||||||
CHARGE_OPTION_2_PSYS_EN
|
|
||||||
);
|
|
||||||
if (res < 0) return res;
|
|
||||||
|
|
||||||
// Set charge current in mA
|
// Set charge current in mA
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_CURRENT, CHARGE_CURRENT);
|
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_CURRENT, CHARGE_CURRENT);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Set charge voltage in mV
|
// Set charge voltage in mV
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_VOLTAGE, CHARGE_VOLTAGE);
|
res = smbus_write(CHARGER_ADDRESS, REG_CHARGE_VOLTAGE, CHARGE_VOLTAGE);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
// Set input current in mA
|
// Set input current in mA
|
||||||
res = smbus_write(CHARGER_ADDRESS, REG_ADAPTER_CURRENT, INPUT_CURRENT);
|
res = smbus_write(CHARGER_ADDRESS, REG_ADAPTER_CURRENT, INPUT_CURRENT);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
DEBUG("Charger enabled\n");
|
DEBUG("Charger enabled\n");
|
||||||
charger_enabled = true;
|
charger_enabled = true;
|
||||||
@ -158,7 +167,8 @@ void battery_debug(void) {
|
|||||||
uint16_t data = 0;
|
uint16_t data = 0;
|
||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
#define command(N, A, V) { \
|
#define command(N, A, V) \
|
||||||
|
{ \
|
||||||
DEBUG(" " #N ": "); \
|
DEBUG(" " #N ": "); \
|
||||||
res = smbus_read(A, V, &data); \
|
res = smbus_read(A, V, &data); \
|
||||||
if (res < 0) { \
|
if (res < 0) { \
|
||||||
|
@ -31,7 +31,8 @@ int16_t dgpu_temp = 0;
|
|||||||
|
|
||||||
#define DGPU_TEMP(X) ((int16_t)(X))
|
#define DGPU_TEMP(X) ((int16_t)(X))
|
||||||
|
|
||||||
#define FAN_POINT(T, D) { .temp = DGPU_TEMP(T), .duty = PWM_DUTY(D) }
|
#define FAN_POINT(T, D) \
|
||||||
|
{ .temp = DGPU_TEMP(T), .duty = PWM_DUTY(D) }
|
||||||
|
|
||||||
// Fan curve with temperature in degrees C, duty cycle in percent
|
// Fan curve with temperature in degrees C, duty cycle in percent
|
||||||
static struct FanPoint __code FAN_POINTS[] = {
|
static struct FanPoint __code FAN_POINTS[] = {
|
||||||
|
@ -13,33 +13,29 @@
|
|||||||
#include <8051.h>
|
#include <8051.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#define DEBUG_SET(REG, MASK, BITS) { \
|
#define DEBUG_SET(REG, MASK, BITS) \
|
||||||
|
{ \
|
||||||
DEBUG("%s: %X", #REG, REG); \
|
DEBUG("%s: %X", #REG, REG); \
|
||||||
REG = ((REG) & ~(MASK)) | (BITS); \
|
REG = ((REG) & ~(MASK)) | (BITS); \
|
||||||
DEBUG(" set to %X\n", REG); \
|
DEBUG(" set to %X\n", REG); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define DEBUG_ON(REG, BITS) \
|
#define DEBUG_ON(REG, BITS) DEBUG_SET(REG, BITS, BITS)
|
||||||
DEBUG_SET(REG, BITS, BITS)
|
|
||||||
|
|
||||||
#define DEBUG_OFF(REG, BITS) \
|
#define DEBUG_OFF(REG, BITS) DEBUG_SET(REG, BITS, 0)
|
||||||
DEBUG_SET(REG, BITS, 0)
|
|
||||||
|
|
||||||
#define DEBUG_CHANGED(REG) { \
|
#define DEBUG_CHANGED(REG) \
|
||||||
|
{ \
|
||||||
static uint8_t last_##REG = 0; \
|
static uint8_t last_##REG = 0; \
|
||||||
uint8_t new_##REG = REG; \
|
uint8_t new_##REG = REG; \
|
||||||
if (new_##REG != last_##REG) { \
|
if (new_##REG != last_##REG) { \
|
||||||
DEBUG( \
|
DEBUG("%S: %X changed to %X\n", #REG, last_##REG, new_##REG); \
|
||||||
"%S: %X changed to %X\n", \
|
|
||||||
#REG, \
|
|
||||||
last_ ## REG, \
|
|
||||||
new_ ## REG \
|
|
||||||
); \
|
|
||||||
last_##REG = new_##REG; \
|
last_##REG = new_##REG; \
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define VW_SET_DEBUG(W, V) { \
|
#define VW_SET_DEBUG(W, V) \
|
||||||
|
{ \
|
||||||
DEBUG("%s = %X\n", #W, V); \
|
DEBUG("%s = %X\n", #W, V); \
|
||||||
vw_set(&W, V); \
|
vw_set(&W, V); \
|
||||||
}
|
}
|
||||||
|
@ -44,10 +44,12 @@ uint8_t fan_duty(const struct Fan * fan, int16_t temp) __reentrant {
|
|||||||
if (temp > prev->temp) {
|
if (temp > prev->temp) {
|
||||||
int16_t dtemp = (cur->temp - prev->temp);
|
int16_t dtemp = (cur->temp - prev->temp);
|
||||||
int16_t dduty = ((int16_t)cur->duty) - ((int16_t)prev->duty);
|
int16_t dduty = ((int16_t)cur->duty) - ((int16_t)prev->duty);
|
||||||
|
// clang-format off
|
||||||
return (uint8_t)(
|
return (uint8_t)(
|
||||||
((int16_t)prev->duty) +
|
((int16_t)prev->duty) +
|
||||||
((temp - prev->temp) * dduty) / dtemp
|
((temp - prev->temp) * dduty) / dtemp
|
||||||
);
|
);
|
||||||
|
// clang-format on
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return prev->duty;
|
return prev->duty;
|
||||||
@ -121,9 +123,11 @@ uint8_t fan_smooth(uint8_t last_duty, uint8_t duty) __reentrant {
|
|||||||
// ramping down
|
// ramping down
|
||||||
if (duty < last_duty) {
|
if (duty < last_duty) {
|
||||||
// out of bounds (lower) safeguard
|
// out of bounds (lower) safeguard
|
||||||
|
// clang-format off
|
||||||
uint8_t smoothed = last_duty < MIN_FAN_SPEED + MAX_JUMP_DOWN
|
uint8_t smoothed = last_duty < MIN_FAN_SPEED + MAX_JUMP_DOWN
|
||||||
? MIN_FAN_SPEED
|
? MIN_FAN_SPEED
|
||||||
: last_duty - MAX_JUMP_DOWN;
|
: last_duty - MAX_JUMP_DOWN;
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
// use smoothed value if above min and if smoothed is closer than raw
|
// use smoothed value if above min and if smoothed is closer than raw
|
||||||
if (last_duty > MIN_SPEED_TO_SMOOTH && smoothed > duty) {
|
if (last_duty > MIN_SPEED_TO_SMOOTH && smoothed > duty) {
|
||||||
@ -134,9 +138,11 @@ uint8_t fan_smooth(uint8_t last_duty, uint8_t duty) __reentrant {
|
|||||||
// ramping up
|
// ramping up
|
||||||
if (duty > last_duty) {
|
if (duty > last_duty) {
|
||||||
// out of bounds (higher) safeguard
|
// out of bounds (higher) safeguard
|
||||||
|
// clang-format off
|
||||||
uint8_t smoothed = last_duty > MAX_FAN_SPEED - MAX_JUMP_UP
|
uint8_t smoothed = last_duty > MAX_FAN_SPEED - MAX_JUMP_UP
|
||||||
? MAX_FAN_SPEED
|
? MAX_FAN_SPEED
|
||||||
: last_duty + MAX_JUMP_UP;
|
: last_duty + MAX_JUMP_UP;
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
// use smoothed value if above min and if smoothed is closer than raw
|
// use smoothed value if above min and if smoothed is closer than raw
|
||||||
if (duty > MIN_SPEED_TO_SMOOTH && smoothed < duty) {
|
if (duty > MIN_SPEED_TO_SMOOTH && smoothed < duty) {
|
||||||
|
@ -15,6 +15,7 @@ volatile uint8_t __xdata __at(0x103D) ECINDAR2;
|
|||||||
volatile uint8_t __xdata __at(0x103E) ECINDAR3;
|
volatile uint8_t __xdata __at(0x103E) ECINDAR3;
|
||||||
volatile uint8_t __xdata __at(0x103F) ECINDDR;
|
volatile uint8_t __xdata __at(0x103F) ECINDDR;
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
#define SPI_DEVICE (0x70)
|
#define SPI_DEVICE (0x70)
|
||||||
#define SPI_FOLLOW_MODE (0x0F)
|
#define SPI_FOLLOW_MODE (0x0F)
|
||||||
#define SPI_CHIP_SELECT (0xFD)
|
#define SPI_CHIP_SELECT (0xFD)
|
||||||
@ -29,6 +30,7 @@ volatile uint8_t __xdata __at(0x103F) ECINDDR;
|
|||||||
#define SPI_ERASE_SECTOR_COMMAND (0xD7)
|
#define SPI_ERASE_SECTOR_COMMAND (0xD7)
|
||||||
|
|
||||||
#define SPI_STATUS_WIP (0x01)
|
#define SPI_STATUS_WIP (0x01)
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
void flash_enter_follow_mode(void);
|
void flash_enter_follow_mode(void);
|
||||||
void flash_exit_follow_mode(void);
|
void flash_exit_follow_mode(void);
|
||||||
@ -46,11 +48,11 @@ void flash_write_enable(void);
|
|||||||
* NOTE: __critical to ensure interrupts are disabled. This does mean that interrupt
|
* NOTE: __critical to ensure interrupts are disabled. This does mean that interrupt
|
||||||
* such as the timer will be block until flash acccess is complete
|
* such as the timer will be block until flash acccess is complete
|
||||||
*/
|
*/
|
||||||
|
// clang-format off
|
||||||
void flash_entry(uint32_t addr, uint8_t *data, uint32_t length, uint8_t command) __reentrant __critical {
|
void flash_entry(uint32_t addr, uint8_t *data, uint32_t length, uint8_t command) __reentrant __critical {
|
||||||
|
// clang-format on
|
||||||
// Only allow access from 64KB to 128KB.
|
// Only allow access from 64KB to 128KB.
|
||||||
if ((addr < 0x10000)
|
if ((addr < 0x10000) || (length > 0x10000) || ((addr + length) > 0x20000))
|
||||||
|| (length > 0x10000)
|
|
||||||
|| ((addr + length) > 0x20000))
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (command == FLASH_COMMAND_READ) {
|
if (command == FLASH_COMMAND_READ) {
|
||||||
@ -93,7 +95,6 @@ void flash_entry(uint32_t addr, uint8_t * data, uint32_t length, uint8_t command
|
|||||||
ECINDAR1 = SPI_CHIP_DESELECT;
|
ECINDAR1 = SPI_CHIP_DESELECT;
|
||||||
ECINDDR = 0x00;
|
ECINDDR = 0x00;
|
||||||
|
|
||||||
|
|
||||||
// Wait WIP to be cleared
|
// Wait WIP to be cleared
|
||||||
flash_wait();
|
flash_wait();
|
||||||
}
|
}
|
||||||
|
@ -12,9 +12,11 @@
|
|||||||
/** \cond INTERNAL
|
/** \cond INTERNAL
|
||||||
* Internal defines
|
* Internal defines
|
||||||
*/
|
*/
|
||||||
|
// clang-format off
|
||||||
#define FLASH_COMMAND_READ (0x0)
|
#define FLASH_COMMAND_READ (0x0)
|
||||||
#define FLASH_COMMAND_WRITE (0x1)
|
#define FLASH_COMMAND_WRITE (0x1)
|
||||||
#define FLASH_COMMAND_ERASE_1K (0x2)
|
#define FLASH_COMMAND_ERASE_1K (0x2)
|
||||||
|
// clang-format on
|
||||||
/** \endcond */
|
/** \endcond */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,6 +36,7 @@ static bool kbc_translate = true;
|
|||||||
// LED state
|
// LED state
|
||||||
uint8_t kbc_leds = 0;
|
uint8_t kbc_leds = 0;
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
// Values from linux/drivers/input/keyboard/atkbd.c
|
// Values from linux/drivers/input/keyboard/atkbd.c
|
||||||
static const uint16_t kbc_typematic_period[32] = {
|
static const uint16_t kbc_typematic_period[32] = {
|
||||||
33, // 30.0 cps = ~33.33ms
|
33, // 30.0 cps = ~33.33ms
|
||||||
@ -71,6 +72,7 @@ static const uint16_t kbc_typematic_period[32] = {
|
|||||||
470, // 2.1 cps = ~478.19ms
|
470, // 2.1 cps = ~478.19ms
|
||||||
500, // 2.0 cps = 500ms
|
500, // 2.0 cps = 500ms
|
||||||
};
|
};
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
static uint8_t kbc_buffer[16] = { 0 };
|
static uint8_t kbc_buffer[16] = { 0 };
|
||||||
static uint8_t kbc_buffer_head = 0;
|
static uint8_t kbc_buffer_head = 0;
|
||||||
@ -101,11 +103,13 @@ static bool kbc_buffer_push(uint8_t * scancodes, uint8_t len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool kbc_scancode(uint16_t key, bool pressed) {
|
bool kbc_scancode(uint16_t key, bool pressed) {
|
||||||
if (!kbc_first) return true;
|
if (!kbc_first)
|
||||||
|
return true;
|
||||||
if (kbc_translate) {
|
if (kbc_translate) {
|
||||||
key = keymap_translate(key);
|
key = keymap_translate(key);
|
||||||
}
|
}
|
||||||
if (!key) return true;
|
if (!key)
|
||||||
|
return true;
|
||||||
|
|
||||||
uint8_t scancodes[3] = { 0, 0, 0 };
|
uint8_t scancodes[3] = { 0, 0, 0 };
|
||||||
uint8_t scancodes_len = 0;
|
uint8_t scancodes_len = 0;
|
||||||
|
@ -28,12 +28,27 @@ void kbled_reset(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Keep the following functions for compatibility - they are set via USB HID
|
// Keep the following functions for compatibility - they are set via USB HID
|
||||||
uint8_t kbled_get(void) { /*Always off*/ return 0; }
|
uint8_t kbled_get(void) {
|
||||||
|
/* Always off */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t kbled_max(void) { /*Always off*/ return 0; }
|
uint8_t kbled_max(void) {
|
||||||
|
/* Always off */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void kbled_set(uint8_t level) { /*Fix unused variable*/ level = level; }
|
void kbled_set(uint8_t level) {
|
||||||
|
/* Fix unused variable */
|
||||||
|
level = level;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t kbled_get_color(void) { /*Always black*/ return 0; }
|
uint32_t kbled_get_color(void) {
|
||||||
|
/* Always black */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void kbled_set_color(uint32_t color) { /*Fix unused variable*/ color = color; }
|
void kbled_set_color(uint32_t color) {
|
||||||
|
/* Fix unused variable */
|
||||||
|
color = color;
|
||||||
|
}
|
||||||
|
@ -6,12 +6,27 @@ void kbled_init(void) {}
|
|||||||
|
|
||||||
void kbled_reset(void) {}
|
void kbled_reset(void) {}
|
||||||
|
|
||||||
uint8_t kbled_get(void) { /*Always off*/ return 0; }
|
uint8_t kbled_get(void) {
|
||||||
|
/* Always off */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t kbled_max(void) { /*Always off*/ return 0; }
|
uint8_t kbled_max(void) {
|
||||||
|
/* Always off */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void kbled_set(uint8_t level) { /*Fix unused variable*/ level = level; }
|
void kbled_set(uint8_t level) {
|
||||||
|
/* Fix unused variable */
|
||||||
|
level = level;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t kbled_get_color(void) { /*Always black*/ return 0; }
|
uint32_t kbled_get_color(void) {
|
||||||
|
/* Always black */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void kbled_set_color(uint32_t color) { /*Fix unused variable*/ color = color; }
|
void kbled_set_color(uint32_t color) {
|
||||||
|
/* Fix unused variable */
|
||||||
|
color = color;
|
||||||
|
}
|
||||||
|
@ -54,6 +54,12 @@ void kbled_set(uint8_t level) {
|
|||||||
KBLED_DACDAT = raw;
|
KBLED_DACDAT = raw;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t kbled_get_color(void) { /* Always white */ return 0xFFFFFF; }
|
uint32_t kbled_get_color(void) {
|
||||||
|
/* Always white */
|
||||||
|
return 0xFFFFFF;
|
||||||
|
}
|
||||||
|
|
||||||
void kbled_set_color(uint32_t color) { /*Fix unused variable*/ color = color; }
|
void kbled_set_color(uint32_t color) {
|
||||||
|
/* Fix unused variable */
|
||||||
|
color = color;
|
||||||
|
}
|
||||||
|
@ -178,25 +178,27 @@ static void hardware_hotkey(uint16_t key) {
|
|||||||
kbled_set(kbled_get() + 1);
|
kbled_set(kbled_get() + 1);
|
||||||
break;
|
break;
|
||||||
case K_KBD_COLOR:
|
case K_KBD_COLOR:
|
||||||
if (acpi_ecos != EC_OS_FULL) kbled_hotkey_color();
|
if (acpi_ecos != EC_OS_FULL)
|
||||||
|
kbled_hotkey_color();
|
||||||
break;
|
break;
|
||||||
case K_KBD_DOWN:
|
case K_KBD_DOWN:
|
||||||
if (acpi_ecos != EC_OS_FULL) kbled_hotkey_down();
|
if (acpi_ecos != EC_OS_FULL)
|
||||||
|
kbled_hotkey_down();
|
||||||
break;
|
break;
|
||||||
case K_KBD_UP:
|
case K_KBD_UP:
|
||||||
if (acpi_ecos != EC_OS_FULL) kbled_hotkey_up();
|
if (acpi_ecos != EC_OS_FULL)
|
||||||
|
kbled_hotkey_up();
|
||||||
break;
|
break;
|
||||||
case K_KBD_TOGGLE:
|
case K_KBD_TOGGLE:
|
||||||
if (acpi_ecos != EC_OS_FULL) kbled_hotkey_toggle();
|
if (acpi_ecos != EC_OS_FULL)
|
||||||
|
kbled_hotkey_toggle();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool kbscan_press(uint16_t key, bool pressed, uint8_t *layer) {
|
bool kbscan_press(uint16_t key, bool pressed, uint8_t *layer) {
|
||||||
// Wake from sleep on keypress
|
// Wake from sleep on keypress
|
||||||
if (pressed &&
|
if (pressed && lid_state && (power_state == POWER_STATE_S3)) {
|
||||||
lid_state &&
|
|
||||||
(power_state == POWER_STATE_S3)) {
|
|
||||||
pmc_swi();
|
pmc_swi();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,8 +210,10 @@ bool kbscan_press(uint16_t key, bool pressed, uint8_t * layer) {
|
|||||||
break;
|
break;
|
||||||
case (KT_FN):
|
case (KT_FN):
|
||||||
if (layer != NULL) {
|
if (layer != NULL) {
|
||||||
if (pressed) *layer = 1;
|
if (pressed)
|
||||||
else *layer = 0;
|
*layer = 1;
|
||||||
|
else
|
||||||
|
*layer = 0;
|
||||||
} else {
|
} else {
|
||||||
// In the case no layer can be set, reset bit
|
// In the case no layer can be set, reset bit
|
||||||
return false;
|
return false;
|
||||||
@ -344,8 +348,10 @@ void kbscan_event(void) {
|
|||||||
|
|
||||||
// A key was pressed or released
|
// A key was pressed or released
|
||||||
for (uint8_t j = 0; j < KM_IN; j++) {
|
for (uint8_t j = 0; j < KM_IN; j++) {
|
||||||
|
// clang-format off
|
||||||
bool new_b = new & BIT(j);
|
bool new_b = new & BIT(j);
|
||||||
bool last_b = last & BIT(j);
|
bool last_b = last & BIT(j);
|
||||||
|
// clang-format on
|
||||||
if (new_b != last_b) {
|
if (new_b != last_b) {
|
||||||
bool reset = false;
|
bool reset = false;
|
||||||
|
|
||||||
|
@ -36,19 +36,29 @@ bool keymap_erase_config(void) {
|
|||||||
|
|
||||||
bool keymap_load_config(void) {
|
bool keymap_load_config(void) {
|
||||||
// Check signature
|
// Check signature
|
||||||
if (flash_read_u16(CONFIG_ADDR) != CONFIG_SIGNATURE) return false;
|
if (flash_read_u16(CONFIG_ADDR) != CONFIG_SIGNATURE)
|
||||||
|
return false;
|
||||||
|
|
||||||
// Read the keymap if signature is valid
|
// Read the keymap if signature is valid
|
||||||
flash_read(CONFIG_ADDR + sizeof(CONFIG_SIGNATURE), (uint8_t *)DYNAMIC_KEYMAP, sizeof(DYNAMIC_KEYMAP));
|
flash_read(
|
||||||
|
CONFIG_ADDR + sizeof(CONFIG_SIGNATURE),
|
||||||
|
(uint8_t *)DYNAMIC_KEYMAP,
|
||||||
|
sizeof(DYNAMIC_KEYMAP)
|
||||||
|
);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool keymap_save_config(void) {
|
bool keymap_save_config(void) {
|
||||||
// Erase config region
|
// Erase config region
|
||||||
if (!keymap_erase_config()) return false;
|
if (!keymap_erase_config())
|
||||||
|
return false;
|
||||||
|
|
||||||
// Write the keymap
|
// Write the keymap
|
||||||
flash_write(CONFIG_ADDR + sizeof(CONFIG_SIGNATURE), (uint8_t *)DYNAMIC_KEYMAP, sizeof(DYNAMIC_KEYMAP));
|
flash_write(
|
||||||
|
CONFIG_ADDR + sizeof(CONFIG_SIGNATURE),
|
||||||
|
(uint8_t *)DYNAMIC_KEYMAP,
|
||||||
|
sizeof(DYNAMIC_KEYMAP)
|
||||||
|
);
|
||||||
|
|
||||||
// Write the length of the keymap, as a signature
|
// Write the length of the keymap, as a signature
|
||||||
flash_write_u16(CONFIG_ADDR, CONFIG_SIGNATURE);
|
flash_write_u16(CONFIG_ADDR, CONFIG_SIGNATURE);
|
||||||
|
@ -19,12 +19,14 @@
|
|||||||
* nWAIT = KSOH[1]
|
* nWAIT = KSOH[1]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
#define CTL_WRITE BIT(0)
|
#define CTL_WRITE BIT(0)
|
||||||
#define CTL_DATA BIT(1)
|
#define CTL_DATA BIT(1)
|
||||||
#define CTL_RESET BIT(2)
|
#define CTL_RESET BIT(2)
|
||||||
#define CTL_ADDR BIT(3)
|
#define CTL_ADDR BIT(3)
|
||||||
|
|
||||||
#define STS_WAIT BIT(1)
|
#define STS_WAIT BIT(1)
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
// Maximum peripheral response time in ms
|
// Maximum peripheral response time in ms
|
||||||
#define PARALLEL_TIMEOUT 10
|
#define PARALLEL_TIMEOUT 10
|
||||||
|
@ -36,7 +36,9 @@ int16_t peci_temp = 0;
|
|||||||
|
|
||||||
#define PECI_TEMP(X) ((int16_t)(X))
|
#define PECI_TEMP(X) ((int16_t)(X))
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
#define FAN_POINT(T, D) { .temp = PECI_TEMP(T), .duty = PWM_DUTY(D) }
|
#define FAN_POINT(T, D) { .temp = PECI_TEMP(T), .duty = PWM_DUTY(D) }
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
// Fan curve with temperature in degrees C, duty cycle in percent
|
// Fan curve with temperature in degrees C, duty cycle in percent
|
||||||
static struct FanPoint __code FAN_POINTS[] = {
|
static struct FanPoint __code FAN_POINTS[] = {
|
||||||
|
@ -26,7 +26,8 @@
|
|||||||
#define USE_S0IX 0
|
#define USE_S0IX 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define GPIO_SET_DEBUG(G, V) { \
|
#define GPIO_SET_DEBUG(G, V) \
|
||||||
|
{ \
|
||||||
DEBUG("%s = %s\n", #G, V ? "true" : "false"); \
|
DEBUG("%s = %s\n", #G, V ? "true" : "false"); \
|
||||||
gpio_set(&G, V); \
|
gpio_set(&G, V); \
|
||||||
}
|
}
|
||||||
@ -243,8 +244,8 @@ void power_on(void) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for VW changes
|
|
||||||
#if CONFIG_BUS_ESPI
|
#if CONFIG_BUS_ESPI
|
||||||
|
// Check for VW changes
|
||||||
espi_event();
|
espi_event();
|
||||||
#endif // CONFIG_BUS_ESPI
|
#endif // CONFIG_BUS_ESPI
|
||||||
|
|
||||||
@ -482,6 +483,7 @@ void power_event(void) {
|
|||||||
}
|
}
|
||||||
pg_last = pg_new;
|
pg_last = pg_new;
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
static bool rst_last = false;
|
static bool rst_last = false;
|
||||||
bool rst_new = gpio_get(&BUF_PLT_RST_N);
|
bool rst_new = gpio_get(&BUF_PLT_RST_N);
|
||||||
#if LEVEL >= LEVEL_DEBUG
|
#if LEVEL >= LEVEL_DEBUG
|
||||||
@ -498,6 +500,7 @@ void power_event(void) {
|
|||||||
#endif // CONFIG_BUS_ESPI
|
#endif // CONFIG_BUS_ESPI
|
||||||
}
|
}
|
||||||
rst_last = rst_new;
|
rst_last = rst_new;
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
#if HAVE_SLP_SUS_N
|
#if HAVE_SLP_SUS_N
|
||||||
#if LEVEL >= LEVEL_DEBUG
|
#if LEVEL >= LEVEL_DEBUG
|
||||||
|
@ -168,8 +168,7 @@ static enum Result cmd_keymap_set(void) {
|
|||||||
int16_t layer = smfi_cmd[SMFI_CMD_DATA];
|
int16_t layer = smfi_cmd[SMFI_CMD_DATA];
|
||||||
int16_t output = smfi_cmd[SMFI_CMD_DATA + 1];
|
int16_t output = smfi_cmd[SMFI_CMD_DATA + 1];
|
||||||
int16_t input = smfi_cmd[SMFI_CMD_DATA + 2];
|
int16_t input = smfi_cmd[SMFI_CMD_DATA + 2];
|
||||||
uint16_t key =
|
uint16_t key = ((uint16_t)smfi_cmd[SMFI_CMD_DATA + 3]) |
|
||||||
((uint16_t)smfi_cmd[SMFI_CMD_DATA + 3]) |
|
|
||||||
(((uint16_t)smfi_cmd[SMFI_CMD_DATA + 4]) << 8);
|
(((uint16_t)smfi_cmd[SMFI_CMD_DATA + 4]) << 8);
|
||||||
//TODO: consider only setting if the key has changed
|
//TODO: consider only setting if the key has changed
|
||||||
if (keymap_set(layer, output, input, key)) {
|
if (keymap_set(layer, output, input, key)) {
|
||||||
|
@ -39,7 +39,7 @@ struct Gpio __code VA_EC_EN = GPIO(J, 4);
|
|||||||
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
||||||
struct Gpio __code WLAN_PWR_EN = GPIO(A, 3);
|
struct Gpio __code WLAN_PWR_EN = GPIO(A, 3);
|
||||||
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
||||||
// clange-format on
|
// clang-format on
|
||||||
|
|
||||||
void gpio_init() {
|
void gpio_init() {
|
||||||
// Enable LPC reset on GPD2
|
// Enable LPC reset on GPD2
|
||||||
|
@ -36,7 +36,7 @@ struct Gpio __code VA_EC_EN = GPIO(J, 4);
|
|||||||
struct Gpio __code WLAN_EN = GPIO(F, 3);
|
struct Gpio __code WLAN_EN = GPIO(F, 3);
|
||||||
struct Gpio __code WLAN_PWR_EN = GPIO(G, 1);
|
struct Gpio __code WLAN_PWR_EN = GPIO(G, 1);
|
||||||
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
||||||
// clange-format on
|
// clang-format on
|
||||||
|
|
||||||
void gpio_init(void) {
|
void gpio_init(void) {
|
||||||
// PWRSW WDT 2 Enable 2
|
// PWRSW WDT 2 Enable 2
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <board/gpio.h>
|
#include <board/gpio.h>
|
||||||
#include <common/debug.h>
|
#include <common/debug.h>
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
struct Gpio __code ACIN_N = GPIO(B, 0);
|
struct Gpio __code ACIN_N = GPIO(B, 0);
|
||||||
struct Gpio __code AC_PRESENT = GPIO(E, 1);
|
struct Gpio __code AC_PRESENT = GPIO(E, 1);
|
||||||
struct Gpio __code ALL_SYS_PWRGD = GPIO(C, 0);
|
struct Gpio __code ALL_SYS_PWRGD = GPIO(C, 0);
|
||||||
@ -33,6 +34,7 @@ struct Gpio __code VA_EC_EN = GPIO(J, 4);
|
|||||||
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
||||||
struct Gpio __code WLAN_PWR_EN = GPIO(A, 3);
|
struct Gpio __code WLAN_PWR_EN = GPIO(A, 3);
|
||||||
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
void gpio_init() {
|
void gpio_init() {
|
||||||
// Enable LPC reset on GPD2
|
// Enable LPC reset on GPD2
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <board/gpio.h>
|
#include <board/gpio.h>
|
||||||
#include <common/debug.h>
|
#include <common/debug.h>
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
struct Gpio __code ACIN_N = GPIO(B, 0);
|
struct Gpio __code ACIN_N = GPIO(B, 0);
|
||||||
struct Gpio __code AC_PRESENT = GPIO(E, 1);
|
struct Gpio __code AC_PRESENT = GPIO(E, 1);
|
||||||
struct Gpio __code ALL_SYS_PWRGD = GPIO(C, 0);
|
struct Gpio __code ALL_SYS_PWRGD = GPIO(C, 0);
|
||||||
@ -34,6 +35,7 @@ struct Gpio __code VA_EC_EN = GPIO(J, 4);
|
|||||||
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
||||||
struct Gpio __code WLAN_PWR_EN = GPIO(A, 3);
|
struct Gpio __code WLAN_PWR_EN = GPIO(A, 3);
|
||||||
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
void gpio_init() {
|
void gpio_init() {
|
||||||
// Enable LPC reset on GPD2
|
// Enable LPC reset on GPD2
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <board/gpio.h>
|
#include <board/gpio.h>
|
||||||
#include <common/debug.h>
|
#include <common/debug.h>
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
struct Gpio __code ACIN_N = GPIO(B, 0);
|
struct Gpio __code ACIN_N = GPIO(B, 0);
|
||||||
struct Gpio __code AC_PRESENT = GPIO(E, 1);
|
struct Gpio __code AC_PRESENT = GPIO(E, 1);
|
||||||
struct Gpio __code ALL_SYS_PWRGD = GPIO(C, 0);
|
struct Gpio __code ALL_SYS_PWRGD = GPIO(C, 0);
|
||||||
@ -33,6 +34,7 @@ struct Gpio __code VA_EC_EN = GPIO(H, 7);
|
|||||||
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
||||||
struct Gpio __code WLAN_PWR_EN = GPIO(A, 3);
|
struct Gpio __code WLAN_PWR_EN = GPIO(A, 3);
|
||||||
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
void gpio_init() {
|
void gpio_init() {
|
||||||
// Enable LPC reset on GPD2
|
// Enable LPC reset on GPD2
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <board/gpio.h>
|
#include <board/gpio.h>
|
||||||
#include <common/debug.h>
|
#include <common/debug.h>
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
struct Gpio __code ACIN_N = GPIO(B, 0);
|
struct Gpio __code ACIN_N = GPIO(B, 0);
|
||||||
struct Gpio __code AC_PRESENT = GPIO(E, 1);
|
struct Gpio __code AC_PRESENT = GPIO(E, 1);
|
||||||
struct Gpio __code ALL_SYS_PWRGD = GPIO(C, 0);
|
struct Gpio __code ALL_SYS_PWRGD = GPIO(C, 0);
|
||||||
@ -33,6 +34,7 @@ struct Gpio __code VA_EC_EN = GPIO(J, 4);
|
|||||||
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
||||||
struct Gpio __code WLAN_PWR_EN = GPIO(D, 3);
|
struct Gpio __code WLAN_PWR_EN = GPIO(D, 3);
|
||||||
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
void gpio_init() {
|
void gpio_init() {
|
||||||
// Enable LPC reset on GPD2
|
// Enable LPC reset on GPD2
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
#include <board/gpio.h>
|
#include <board/gpio.h>
|
||||||
#include <ec/ec.h>
|
#include <ec/ec.h>
|
||||||
|
|
||||||
|
|
||||||
void board_init(void) {
|
void board_init(void) {
|
||||||
espi_init();
|
espi_init();
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ struct Gpio __code VA_EC_EN = GPIO(J, 4);
|
|||||||
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
||||||
struct Gpio __code WLAN_PWR_EN = GPIO(A, 3);
|
struct Gpio __code WLAN_PWR_EN = GPIO(A, 3);
|
||||||
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
||||||
// clange-format on
|
// clang-format on
|
||||||
|
|
||||||
void gpio_init(void) {
|
void gpio_init(void) {
|
||||||
// Not documented
|
// Not documented
|
||||||
|
@ -18,6 +18,9 @@ void board_init(void) {
|
|||||||
gpio_set(&SWI_N, true);
|
gpio_set(&SWI_N, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void board_on_ac(bool ac) { /* Fix unused variable */ ac = ac; }
|
void board_on_ac(bool ac) {
|
||||||
|
/* Fix unused variable */
|
||||||
|
ac = ac;
|
||||||
|
}
|
||||||
|
|
||||||
void board_event(void) {}
|
void board_event(void) {}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <board/gpio.h>
|
#include <board/gpio.h>
|
||||||
#include <common/debug.h>
|
#include <common/debug.h>
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
struct Gpio __code ACIN_N = GPIO(B, 0);
|
struct Gpio __code ACIN_N = GPIO(B, 0);
|
||||||
struct Gpio __code AC_PRESENT = GPIO(E, 1);
|
struct Gpio __code AC_PRESENT = GPIO(E, 1);
|
||||||
struct Gpio __code ALL_SYS_PWRGD = GPIO(C, 0);
|
struct Gpio __code ALL_SYS_PWRGD = GPIO(C, 0);
|
||||||
@ -32,6 +33,7 @@ struct Gpio __code VA_EC_EN = GPIO(J, 4); // renamed to EC_SLP_SUS#
|
|||||||
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
struct Gpio __code WLAN_EN = GPIO(G, 1);
|
||||||
struct Gpio __code WLAN_PWR_EN = GPIO(H, 4);
|
struct Gpio __code WLAN_PWR_EN = GPIO(H, 4);
|
||||||
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
struct Gpio __code XLP_OUT = GPIO(B, 4);
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
void gpio_init() {
|
void gpio_init() {
|
||||||
// Enable LPC reset on GPD2
|
// Enable LPC reset on GPD2
|
||||||
|
@ -6,10 +6,12 @@ int16_t i2c_recv(struct I2C * i2c, uint8_t addr, uint8_t* data, uint16_t length)
|
|||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
res = i2c_start(i2c, addr, true);
|
res = i2c_start(i2c, addr, true);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = i2c_read(i2c, data, length);
|
res = i2c_read(i2c, data, length);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
i2c_stop(i2c);
|
i2c_stop(i2c);
|
||||||
|
|
||||||
@ -20,36 +22,44 @@ int16_t i2c_send(struct I2C * i2c, uint8_t addr, uint8_t* data, uint16_t length)
|
|||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
res = i2c_start(i2c, addr, false);
|
res = i2c_start(i2c, addr, false);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = i2c_write(i2c, data, length);
|
res = i2c_write(i2c, data, length);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
i2c_stop(i2c);
|
i2c_stop(i2c);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t i2c_get(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, uint16_t length) __reentrant {
|
int16_t i2c_get(struct I2C *i2c, uint8_t addr, uint8_t reg, uint8_t *data, uint16_t length)
|
||||||
|
__reentrant {
|
||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
res = i2c_start(i2c, addr, false);
|
res = i2c_start(i2c, addr, false);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = i2c_write(i2c, ®, 1);
|
res = i2c_write(i2c, ®, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
return i2c_recv(i2c, addr, data, length);
|
return i2c_recv(i2c, addr, data, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t i2c_set(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, uint16_t length) __reentrant {
|
int16_t i2c_set(struct I2C *i2c, uint8_t addr, uint8_t reg, uint8_t *data, uint16_t length)
|
||||||
|
__reentrant {
|
||||||
int16_t res = 0;
|
int16_t res = 0;
|
||||||
|
|
||||||
res = i2c_start(i2c, addr, false);
|
res = i2c_start(i2c, addr, false);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
res = i2c_write(i2c, ®, 1);
|
res = i2c_write(i2c, ®, 1);
|
||||||
if (res < 0) return res;
|
if (res < 0)
|
||||||
|
return res;
|
||||||
|
|
||||||
return i2c_send(i2c, addr, data, length);
|
return i2c_send(i2c, addr, data, length);
|
||||||
}
|
}
|
||||||
|
@ -36,10 +36,14 @@ int16_t i2c_recv(struct I2C * i2c, uint8_t addr, uint8_t* data, uint16_t length)
|
|||||||
// Write multiple bytes to address in one transaction
|
// Write multiple bytes to address in one transaction
|
||||||
int16_t i2c_send(struct I2C *i2c, uint8_t addr, uint8_t *data, uint16_t length) __reentrant;
|
int16_t i2c_send(struct I2C *i2c, uint8_t addr, uint8_t *data, uint16_t length) __reentrant;
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
// Read multiple bytes from a register in one transaction
|
// Read multiple bytes from a register in one transaction
|
||||||
int16_t i2c_get(struct I2C *i2c, uint8_t addr, uint8_t reg, uint8_t *data, uint16_t length) __reentrant;
|
int16_t i2c_get(struct I2C *i2c, uint8_t addr, uint8_t reg, uint8_t *data, uint16_t length) __reentrant;
|
||||||
|
|
||||||
// Write multiple bytes to a register in one transaction
|
// Write multiple bytes to a register in one transaction
|
||||||
int16_t i2c_set(struct I2C *i2c, uint8_t addr, uint8_t reg, uint8_t *data, uint16_t length) __reentrant;
|
int16_t i2c_set(struct I2C *i2c, uint8_t addr, uint8_t reg, uint8_t *data, uint16_t length) __reentrant;
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
#endif // _COMMON_I2C_H
|
#endif // _COMMON_I2C_H
|
||||||
|
@ -45,7 +45,8 @@ struct I2C __code I2C_4 = {
|
|||||||
void i2c_reset(struct I2C *i2c, bool kill) {
|
void i2c_reset(struct I2C *i2c, bool kill) {
|
||||||
if (*(i2c->hosta) & HOSTA_BUSY) {
|
if (*(i2c->hosta) & HOSTA_BUSY) {
|
||||||
// Set kill bit
|
// Set kill bit
|
||||||
if (kill) *(i2c->hoctl) |= BIT(1);
|
if (kill)
|
||||||
|
*(i2c->hoctl) |= BIT(1);
|
||||||
// Wait for host to finish
|
// Wait for host to finish
|
||||||
while (*(i2c->hosta) & HOSTA_BUSY) {}
|
while (*(i2c->hosta) & HOSTA_BUSY) {}
|
||||||
}
|
}
|
||||||
|
@ -8,11 +8,13 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
#define GPIO_ALT (0b00U << 6)
|
#define GPIO_ALT (0b00U << 6)
|
||||||
#define GPIO_IN (0b10U << 6)
|
#define GPIO_IN (0b10U << 6)
|
||||||
#define GPIO_OUT (0b01U << 6)
|
#define GPIO_OUT (0b01U << 6)
|
||||||
#define GPIO_UP BIT(2)
|
#define GPIO_UP BIT(2)
|
||||||
#define GPIO_DOWN BIT(1)
|
#define GPIO_DOWN BIT(1)
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
struct Gpio {
|
struct Gpio {
|
||||||
volatile uint8_t __xdata *data;
|
volatile uint8_t __xdata *data;
|
||||||
|
@ -22,7 +22,8 @@ uint8_t kbc_read(struct Kbc * kbc) {
|
|||||||
|
|
||||||
static bool kbc_wait(struct Kbc *kbc, uint16_t timeout) {
|
static bool kbc_wait(struct Kbc *kbc, uint16_t timeout) {
|
||||||
while (*(kbc->status) & KBC_STS_OBF) {
|
while (*(kbc->status) & KBC_STS_OBF) {
|
||||||
if (timeout == 0) return false;
|
if (timeout == 0)
|
||||||
|
return false;
|
||||||
timeout -= 1;
|
timeout -= 1;
|
||||||
delay_us(1);
|
delay_us(1);
|
||||||
}
|
}
|
||||||
@ -30,14 +31,16 @@ static bool kbc_wait(struct Kbc * kbc, uint16_t timeout) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool kbc_keyboard(struct Kbc *kbc, uint8_t data, uint16_t timeout) {
|
bool kbc_keyboard(struct Kbc *kbc, uint8_t data, uint16_t timeout) {
|
||||||
if (!kbc_wait(kbc, timeout)) return false;
|
if (!kbc_wait(kbc, timeout))
|
||||||
|
return false;
|
||||||
*(kbc->status) &= ~0x20;
|
*(kbc->status) &= ~0x20;
|
||||||
*(kbc->keyboard_out) = data;
|
*(kbc->keyboard_out) = data;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool kbc_mouse(struct Kbc *kbc, uint8_t data, uint16_t timeout) {
|
bool kbc_mouse(struct Kbc *kbc, uint8_t data, uint16_t timeout) {
|
||||||
if (!kbc_wait(kbc, timeout)) return false;
|
if (!kbc_wait(kbc, timeout))
|
||||||
|
return false;
|
||||||
*(kbc->status) |= 0x20;
|
*(kbc->status) |= 0x20;
|
||||||
*(kbc->mouse_out) = data;
|
*(kbc->mouse_out) = data;
|
||||||
return true;
|
return true;
|
||||||
|
Reference in New Issue
Block a user