Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
firmware
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
card10
firmware
Commits
8a5926b5
Commit
8a5926b5
authored
5 years ago
by
Woazboat
Committed by
Florian Kargl
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
refactor(portexpander): defines for portexpander commands + common read/write functions
parent
b4c64ba8
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!65
Refactor portexpander code + new function for setting multiple pins at once
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/card10/portexpander.c
+51
-25
51 additions, 25 deletions
lib/card10/portexpander.c
with
51 additions
and
25 deletions
lib/card10/portexpander.c
+
51
−
25
View file @
8a5926b5
...
...
@@ -7,17 +7,57 @@
#include
<string.h>
#include
<stdbool.h>
// PCAL6408A I2C port expander
static
bool
detected
=
false
;
static
uint8_t
output_state
;
/* clang-format off */
#define PE_ADDR 0x42
#define PE_C_INPUT_PORT 0x00
#define PE_C_OUTPUT_PORT 0x01
#define PE_C_POLARITY_INV 0x02
#define PE_C_CONFIG 0x03
#define PE_C_OUTPUT_DRIVE_STR_0 0x40
#define PE_C_OUTPUT_DRIVE_STR_1 0x41
#define PE_C_INPUT_LATCH 0x42
#define PE_C_PULL_ENABLE 0x43
#define PE_C_PULL_SEL 0x44
#define PE_C_INT_MASK 0x45
#define PE_C_INT_STATUS 0x46
#define PE_C_OUTPUT_PORT_CONFIG 0x4F
#define PE_CONFIG_OUT 0
#define PE_CONFIG_IN 1
#define PE_PULL_DOWN 0
#define PE_PULL_UP 1
#define PE_OUT_PUSH_PULL 0
#define PE_OUT_OPEN_DRAIN 1
/* clang-format on */
#define PE_INPUT_MASK ((uint8_t)0b01101000) // 3, 5, 6 = input
static
int
portexpander_write
(
uint8_t
command
,
uint8_t
data
)
{
uint8_t
i2c_data
[
2
]
=
{
command
,
data
};
return
I2C_MasterWrite
(
MXC_I2C1_BUS0
,
PE_ADDR
,
i2c_data
,
2
,
0
);
}
static
int
portexpander_read
(
uint8_t
command
,
uint8_t
*
data
)
{
I2C_MasterWrite
(
MXC_I2C1_BUS0
,
PE_ADDR
,
&
command
,
1
,
1
);
return
I2C_MasterRead
(
MXC_I2C1_BUS0
,
PE_ADDR
,
data
,
1
,
0
);
}
void
portexpander_init
(
void
)
{
uint8_t
addr
=
0x21
;
int
ret
;
// Enable pull-ups for buttons
uint8_t
command
[]
=
{
0x43
,
0x68
};
ret
=
I2C_MasterWrite
(
MXC_I2C1_BUS0
,
addr
<<
1
,
command
,
2
,
0
);
// Enable pull-ups for buttons (type defaults to pull-up)
ret
=
portexpander_write
(
PE_C_PULL_ENABLE
,
PE_INPUT_MASK
);
if
(
ret
!=
2
)
{
printf
(
"portexpander NOT detected
\n
"
);
...
...
@@ -27,31 +67,22 @@ void portexpander_init(void)
detected
=
true
;
// Set _all_ outputs to open-drain to support the high side p-channel transistors.
command
[
0
]
=
0x4F
;
command
[
1
]
=
0x01
;
I2C_MasterWrite
(
MXC_I2C1_BUS0
,
addr
<<
1
,
command
,
2
,
0
);
portexpander_write
(
PE_C_OUTPUT_PORT_CONFIG
,
PE_OUT_OPEN_DRAIN
);
// Enable outputs for the transistors, the LED and the LCD reset
command
[
0
]
=
0x03
;
command
[
1
]
=
0x68
;
I2C_MasterWrite
(
MXC_I2C1_BUS0
,
addr
<<
1
,
command
,
2
,
0
);
portexpander_write
(
PE_C_CONFIG
,
PE_INPUT_MASK
);
// Set outputs to high (i.e. open-drain)
output_state
=
0x97
;
command
[
0
]
=
0x01
;
command
[
1
]
=
output_state
;
I2C_MasterWrite
(
MXC_I2C1_BUS0
,
addr
<<
1
,
command
,
2
,
0
);
output_state
=
~
PE_INPUT_MASK
;
portexpander_write
(
PE_C_OUTPUT_PORT
,
output_state
);
}
uint8_t
portexpander_get
(
void
)
{
uint8_t
addr
=
0x21
;
uint8_t
command
[]
=
{
0x00
};
uint8_t
buf
=
0xFF
;
uint8_t
buf
=
0xFF
;
if
(
detected
)
{
I2C_MasterWrite
(
MXC_I2C1_BUS0
,
addr
<<
1
,
command
,
1
,
1
);
I2C_MasterRead
(
MXC_I2C1_BUS0
,
addr
<<
1
,
&
buf
,
1
,
0
);
portexpander_read
(
PE_C_INPUT_PORT
,
&
buf
);
}
return
buf
;
...
...
@@ -64,9 +95,6 @@ bool portexpander_detected(void)
void
portexpander_set
(
uint8_t
pin
,
uint8_t
value
)
{
uint8_t
addr
=
0x21
;
uint8_t
command
[
2
];
if
(
detected
&&
pin
<
8
)
{
if
(
value
)
{
output_state
|=
(
1
<<
pin
);
...
...
@@ -74,8 +102,6 @@ void portexpander_set(uint8_t pin, uint8_t value)
output_state
&=
~
(
1
<<
pin
);
}
command
[
0
]
=
0x01
;
command
[
1
]
=
output_state
;
I2C_MasterWrite
(
MXC_I2C1_BUS0
,
addr
<<
1
,
command
,
2
,
0
);
portexpander_write
(
PE_C_OUTPUT_PORT
,
output_state
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment