foxBMS  1.2.0
The foxBMS Battery Management System API Documentation
pex.c File Reference

Driver for the NXP PCA9539 port expander module. More...

#include "pex.h"
#include "diag.h"
#include "i2c.h"
#include "os.h"
Include dependency graph for pex.c:

Go to the source code of this file.

Macros

#define PEX_NR_OF_PORTS_PER_REGISTER   (8u)
 
#define PEX_DEFAULT_VALUE_ALL_1   (0xFFu)
 
#define PEX_DEFAULT_VALUE_ALL_0   (0x0u)
 
#define PEX_INPUT_PORT0_REGISTER_ADDRESS   (0x0u)
 
#define PEX_OUTPUT_PORT0_REGISTER_ADDRESS   (0x2u)
 
#define PEX_POL_INV_PORT0_REGISTER_ADDRESS   (0x4u)
 
#define PEX_DIRECTION_PORT0_REGISTER_ADDRESS   (0x6u)
 
#define PEX_PIN_POLARITY_RETAINED   (0u)
 
#define PEX_PIN_POLARITY_INVERTED   (1u)
 
#define PEX_PIN_DIRECTION_OUTPUT   (0u)
 
#define PEX_PIN_DIRECTION_INPUT   (1u)
 

Functions

static STD_RETURN_TYPE_e PEX_ReadInputs (void)
 reads input state of port expander pins over I2C. More...
 
static STD_RETURN_TYPE_e PEX_WriteOutputs (void)
 sets output state of port expander pins over I2C. More...
 
static STD_RETURN_TYPE_e PEX_WriteConfigPolarity (void)
 sets polarity inversion state of port expander pins over I2C. More...
 
static STD_RETURN_TYPE_e PEX_WriteConfigDirection (void)
 sets direction of port expander pins over I2C. More...
 
static void PEX_CopyToLocalVariable (void)
 copies values from the externally available variables to the local ones. More...
 
static void PEX_GetFromLocalVariable (void)
 copies values from the local variables to the externally available ones. More...
 
void PEX_Initialize (void)
 initialize local variable containing state of port expander. More...
 
void PEX_Trigger (void)
 implements reading/writing to the port expander registers. More...
 
void PEX_SetPin (uint8_t portExpander, uint8_t pin)
 sets pin to high. More...
 
void PEX_ResetPin (uint8_t portExpander, uint8_t pin)
 sets pin to low. More...
 
uint8_t PEX_GetPin (uint8_t portExpander, uint8_t pin)
 get pin state of port expander pins. More...
 
void PEX_SetPinDirectionInput (uint8_t portExpander, uint8_t pin)
 sets pin direction to input. More...
 
void PEX_SetPinDirectionOutput (uint8_t portExpander, uint8_t pin)
 sets pin to input. More...
 
void PEX_SetPinPolarityInverted (uint8_t portExpander, uint8_t pin)
 sets pin polarity to inverted. More...
 
void PEX_SetPinPolarityRetained (uint8_t portExpander, uint8_t pin)
 sets pin polarity to retained. More...
 

Variables

static uint8_t pex_i2cDataWrite [2u] = {0}
 
static uint8_t pex_inputPort0 [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_inputPort1 [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_outputPort0 [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_outputPort1 [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_configPolarityPort0 [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_configPolarityPort1 [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_configDirectionPort0 [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_configDirectionPort1 [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_inputPort0Local [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_inputPort1Local [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_outputPort0Local [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_outputPort1Local [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_configPolarityPort0Local [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_configPolarityPort1Local [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_configDirectionPort0Local [PEX_NR_OF_PORT_EXPANDERS] = {0}
 
static uint8_t pex_configDirectionPort1Local [PEX_NR_OF_PORT_EXPANDERS] = {0}
 

Detailed Description

Driver for the NXP PCA9539 port expander module.

SPDX-License-Identifier: BSD-3-Clause

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

We kindly request you to use one or more of the following phrases to refer to foxBMS in your hardware, software, documentation or advertising materials:

  • ″This product uses parts of foxBMS®″
  • ″This product includes parts of foxBMS®″
  • ″This product is derived from foxBMS®″
Author
foxBMS Team
Date
2021-08-02 (date of creation)
Updated
2021-10-04 (date of last update)
Prefix
PEX

Definition in file pex.c.

Macro Definition Documentation

◆ PEX_DEFAULT_VALUE_ALL_0

#define PEX_DEFAULT_VALUE_ALL_0   (0x0u)

Initial value: all 0

Definition at line 68 of file pex.c.

◆ PEX_DEFAULT_VALUE_ALL_1

#define PEX_DEFAULT_VALUE_ALL_1   (0xFFu)

Initial value: all 1

Definition at line 66 of file pex.c.

◆ PEX_DIRECTION_PORT0_REGISTER_ADDRESS

#define PEX_DIRECTION_PORT0_REGISTER_ADDRESS   (0x6u)

Address of Configuration Port 0 register (port 1 is one higher, 0x7u)

Definition at line 77 of file pex.c.

◆ PEX_INPUT_PORT0_REGISTER_ADDRESS

#define PEX_INPUT_PORT0_REGISTER_ADDRESS   (0x0u)

Address of Input Port 0 register (port 1 is one higher, 0x1u)

Definition at line 71 of file pex.c.

◆ PEX_NR_OF_PORTS_PER_REGISTER

#define PEX_NR_OF_PORTS_PER_REGISTER   (8u)

Number of ports per register

Definition at line 63 of file pex.c.

◆ PEX_OUTPUT_PORT0_REGISTER_ADDRESS

#define PEX_OUTPUT_PORT0_REGISTER_ADDRESS   (0x2u)

Address of Output Port 0 register (port 1 is one higher, 0x3u)

Definition at line 73 of file pex.c.

◆ PEX_PIN_DIRECTION_INPUT

#define PEX_PIN_DIRECTION_INPUT   (1u)

Port expander pin direction configuration in registers

Definition at line 88 of file pex.c.

◆ PEX_PIN_DIRECTION_OUTPUT

#define PEX_PIN_DIRECTION_OUTPUT   (0u)

Port expander pin direction configuration in registers

Definition at line 87 of file pex.c.

◆ PEX_PIN_POLARITY_INVERTED

#define PEX_PIN_POLARITY_INVERTED   (1u)

Port expander pin polarity inversion in registers

Definition at line 82 of file pex.c.

◆ PEX_PIN_POLARITY_RETAINED

#define PEX_PIN_POLARITY_RETAINED   (0u)

Port expander pin polarity inversion in registers

Definition at line 81 of file pex.c.

◆ PEX_POL_INV_PORT0_REGISTER_ADDRESS

#define PEX_POL_INV_PORT0_REGISTER_ADDRESS   (0x4u)

Address of Polarity Inversion Port 0 register (port 1 is one higher, 0x5u)

Definition at line 75 of file pex.c.

Function Documentation

◆ PEX_CopyToLocalVariable()

static void PEX_CopyToLocalVariable ( void  )
static

copies values from the externally available variables to the local ones.

Definition at line 247 of file pex.c.

Here is the call graph for this function:

◆ PEX_GetFromLocalVariable()

static void PEX_GetFromLocalVariable ( void  )
static

copies values from the local variables to the externally available ones.

Definition at line 260 of file pex.c.

Here is the call graph for this function:

◆ PEX_GetPin()

uint8_t PEX_GetPin ( uint8_t  portExpander,
uint8_t  pin 
)

get pin state of port expander pins.

Returns
pin state: PEX_PIN_LOW or PEX_PIN_HIGH

Definition at line 350 of file pex.c.

Here is the call graph for this function:

◆ PEX_Initialize()

void PEX_Initialize ( void  )

initialize local variable containing state of port expander.

In the datasheet, pins are grouped in two sets, numbered from 00 to 07 and from 10 to 17. Each set is handled by a separate register, so pin 00 is numbered 0, pin 01 is numbered 01, .., pin 07 is numbered 07. But also pin 10 is numbered 0, pin 11 is numbered 01, .., pin 17 is numbered 07. Using defines from 0 to 15, it is possible to get with pin number which register has to be addressed: 0 to 7 means register for 00 to 07, 8 to 15 means register for 10 to 17. Applying (pin%8) for 8 to 15 gives 0 to 7, so the numbering in register is obtained immediately.

Default state of output registers Datasheet: Rev. 9 - 8 November 2017, Table 7 and 8

Default state of polarity inversion registers Datasheet: Rev. 9 - 8 November 2017, Table 9 and 10

Default state of direction registers Datasheet: Rev. 9 - 8 November 2017, Table 11 and 12

Definition at line 279 of file pex.c.

◆ PEX_ReadInputs()

static STD_RETURN_TYPE_e PEX_ReadInputs ( void  )
static

reads input state of port expander pins over I2C.

Returns
STD_OK if I2C transmission ok, STD_NOT_OK otherwise

Definition at line 166 of file pex.c.

Here is the call graph for this function:

◆ PEX_ResetPin()

void PEX_ResetPin ( uint8_t  portExpander,
uint8_t  pin 
)

sets pin to low.

Parameters
portExpanderport expander number
pinpin number

Definition at line 335 of file pex.c.

Here is the call graph for this function:

◆ PEX_SetPin()

void PEX_SetPin ( uint8_t  portExpander,
uint8_t  pin 
)

sets pin to high.

Parameters
portExpanderport expander number
pinpin number

Definition at line 320 of file pex.c.

Here is the call graph for this function:

◆ PEX_SetPinDirectionInput()

void PEX_SetPinDirectionInput ( uint8_t  portExpander,
uint8_t  pin 
)

sets pin direction to input.

Parameters
portExpanderport expander number
pinpin number

Definition at line 378 of file pex.c.

Here is the call graph for this function:

◆ PEX_SetPinDirectionOutput()

void PEX_SetPinDirectionOutput ( uint8_t  portExpander,
uint8_t  pin 
)

sets pin to input.

Parameters
portExpanderport expander number
pinpin number

Definition at line 393 of file pex.c.

Here is the call graph for this function:

◆ PEX_SetPinPolarityInverted()

void PEX_SetPinPolarityInverted ( uint8_t  portExpander,
uint8_t  pin 
)

sets pin polarity to inverted.

Parameters
portExpanderport expander number
pinpin number

Definition at line 409 of file pex.c.

Here is the call graph for this function:

◆ PEX_SetPinPolarityRetained()

void PEX_SetPinPolarityRetained ( uint8_t  portExpander,
uint8_t  pin 
)

sets pin polarity to retained.

Parameters
portExpanderport expander number
pinpin number

Definition at line 424 of file pex.c.

Here is the call graph for this function:

◆ PEX_Trigger()

void PEX_Trigger ( void  )

implements reading/writing to the port expander registers.

This function reads the desired port expander state (outputs, configuration) from the externally available variables and stores it in the local variable. It then applies this state to the devices via the I2C bus. It reads the pin input state via the I2C bus and writes it to the externally available port expander state.

Definition at line 301 of file pex.c.

Here is the call graph for this function:

◆ PEX_WriteConfigDirection()

static STD_RETURN_TYPE_e PEX_WriteConfigDirection ( void  )
static

sets direction of port expander pins over I2C.

Returns
STD_OK if I2C transmission ok, STD_NOT_OK otherwise

Direction Port 0 as address, next read register will be Direction Port 1 Datasheet: Rev. 9 - 8 November 2017 Figure 10: one register pair can be written in one transaction

Definition at line 228 of file pex.c.

Here is the call graph for this function:

◆ PEX_WriteConfigPolarity()

static STD_RETURN_TYPE_e PEX_WriteConfigPolarity ( void  )
static

sets polarity inversion state of port expander pins over I2C.

Returns
STD_OK if I2C transmission ok, STD_NOT_OK otherwise

Inversion Polarity Port 0 as address, next read register will be Inversion Polarity Port 1 Datasheet: Rev. 9 - 8 November 2017 Figure 10: one register pair can be written in one transaction

Definition at line 209 of file pex.c.

Here is the call graph for this function:

◆ PEX_WriteOutputs()

static STD_RETURN_TYPE_e PEX_WriteOutputs ( void  )
static

sets output state of port expander pins over I2C.

Returns
STD_OK if I2C transmission ok, STD_NOT_OK otherwise

Outport Port 0 as address, next read register will be Output Port 1 Datasheet: Rev. 9 - 8 November 2017 Figure 10: one register pair can be written in one transaction

Definition at line 190 of file pex.c.

Here is the call graph for this function:

Variable Documentation

◆ pex_configDirectionPort0

uint8_t pex_configDirectionPort0[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These variables are used to configure the port expanders (input, output, configuration) from external modules.

Definition at line 106 of file pex.c.

◆ pex_configDirectionPort0Local

uint8_t pex_configDirectionPort0Local[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These local variables hold the state of the port expanders (input, output, configuration)

Definition at line 120 of file pex.c.

◆ pex_configDirectionPort1

uint8_t pex_configDirectionPort1[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These variables are used to configure the port expanders (input, output, configuration) from external modules.

Definition at line 107 of file pex.c.

◆ pex_configDirectionPort1Local

uint8_t pex_configDirectionPort1Local[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These local variables hold the state of the port expanders (input, output, configuration)

Definition at line 121 of file pex.c.

◆ pex_configPolarityPort0

uint8_t pex_configPolarityPort0[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These variables are used to configure the port expanders (input, output, configuration) from external modules.

Definition at line 104 of file pex.c.

◆ pex_configPolarityPort0Local

uint8_t pex_configPolarityPort0Local[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These local variables hold the state of the port expanders (input, output, configuration)

Definition at line 118 of file pex.c.

◆ pex_configPolarityPort1

uint8_t pex_configPolarityPort1[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These variables are used to configure the port expanders (input, output, configuration) from external modules.

Definition at line 105 of file pex.c.

◆ pex_configPolarityPort1Local

uint8_t pex_configPolarityPort1Local[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These local variables hold the state of the port expanders (input, output, configuration)

Definition at line 119 of file pex.c.

◆ pex_i2cDataWrite

uint8_t pex_i2cDataWrite[2u] = {0}
static

I2C write buffer for PEX

Definition at line 93 of file pex.c.

◆ pex_inputPort0

uint8_t pex_inputPort0[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These variables are used to configure the port expanders (input, output, configuration) from external modules.

Definition at line 100 of file pex.c.

◆ pex_inputPort0Local

uint8_t pex_inputPort0Local[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These local variables hold the state of the port expanders (input, output, configuration)

Definition at line 114 of file pex.c.

◆ pex_inputPort1

uint8_t pex_inputPort1[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These variables are used to configure the port expanders (input, output, configuration) from external modules.

Definition at line 101 of file pex.c.

◆ pex_inputPort1Local

uint8_t pex_inputPort1Local[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These local variables hold the state of the port expanders (input, output, configuration)

Definition at line 115 of file pex.c.

◆ pex_outputPort0

uint8_t pex_outputPort0[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These variables are used to configure the port expanders (input, output, configuration) from external modules.

Definition at line 102 of file pex.c.

◆ pex_outputPort0Local

uint8_t pex_outputPort0Local[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These local variables hold the state of the port expanders (input, output, configuration)

Definition at line 116 of file pex.c.

◆ pex_outputPort1

uint8_t pex_outputPort1[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These variables are used to configure the port expanders (input, output, configuration) from external modules.

Definition at line 103 of file pex.c.

◆ pex_outputPort1Local

uint8_t pex_outputPort1Local[PEX_NR_OF_PORT_EXPANDERS] = {0}
static

These local variables hold the state of the port expanders (input, output, configuration)

Definition at line 117 of file pex.c.