Skip to content

Commit

Permalink
Added support for PCA95X4 I2C GPIO expander (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
evoggy committed Oct 3, 2017
1 parent 0e886c2 commit 2837643
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/hal/interface/pca95x4.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2017 Bitcraze AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* pca95x4.h - Functions for interfacing PCA95X4 I2C GPIO extender
*/
#ifndef __PCA95X4_H__
#define __PCA95X4_H__

#include <stdint.h>
#include <stdbool.h>

#define PCA95X4_DEFAULT_ADDRESS 0b0100000

#define PCA95X4_INPUT_REG (0x00)
#define PCA95X4_OUTPUT_REG (0x01)
#define PCA95X4_POL_REG (0x02)
#define PCA95X4_CONFIG_REG (0x03)

#define PCA95X4_P0 (1 << 0)
#define PCA95X4_P1 (1 << 1)
#define PCA95X4_P2 (1 << 2)
#define PCA95X4_P3 (1 << 3)
#define PCA95X4_P4 (1 << 4)
#define PCA95X4_P5 (1 << 5)
#define PCA95X4_P6 (1 << 6)
#define PCA95X4_P7 (1 << 7)

/**
* Initilize the PCA95X4 sub-system.
*/
void pca95x4Init();

/**
* Test the PCA95X4 sub-system.
*/
bool pca95x4Test();

/**
* Set the output register value.
*/
bool pca95x4ConfigOutput(uint32_t val);

/**
* Set output bits.
*/
bool pca95x4SetOutput(uint32_t mask);

/**
* Reset output bits.
*/
bool pca95x4ClearOutput(uint32_t mask);

#endif //__PCA95X4_H__
85 changes: 85 additions & 0 deletions src/hal/src/pca95x4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* || ____ _ __
* +------+ / __ )(_) /_______________ _____ ___
* | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2017 Bitcraze AB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* pca95x4.c - Functions for interfacing PCA95X4 I2C GPIO extender
*/
#define DEBUG_MODULE "PCA95X4"

#include "i2cdev.h"

#include "pca95x4.h"

#include "debug.h"

static uint8_t devAddr;
static I2C_Dev *I2Cx;

void pca95x4Init()
{
i2cdevInit(I2C1_DEV);
I2Cx = I2C1_DEV;
devAddr = PCA95X4_DEFAULT_ADDRESS;
}

bool pca95x4Test()
{
uint8_t tb;
bool pass;

// Test reading the config register
pass = i2cdevReadByte(I2Cx, devAddr, PCA95X4_CONFIG_REG, &tb);

return pass;
}

bool pca95x4ConfigOutput(uint32_t val) {
bool pass;

pass = i2cdevWriteByte(I2Cx, devAddr, PCA95X4_CONFIG_REG, val);
DEBUG_PRINT("Config Output: 0x%X\n", val);
return pass;
}

bool pca95x4SetOutput(uint32_t mask) {
uint8_t val;
bool pass;

pass = i2cdevReadByte(I2Cx, devAddr, PCA95X4_OUTPUT_REG, &val);
val |= mask;
DEBUG_PRINT("Set Output: 0x%X\n", val);
pass = i2cdevWriteByte(I2Cx, devAddr, PCA95X4_OUTPUT_REG, val);

return pass;
}

bool pca95x4ClearOutput(uint32_t mask) {
uint8_t val;
bool pass;

pass = i2cdevReadByte(I2Cx, devAddr, PCA95X4_OUTPUT_REG, &val);
val &= ~mask;
DEBUG_PRINT("Clear Output: 0x%X\n", val);
pass = i2cdevWriteByte(I2Cx, devAddr, PCA95X4_OUTPUT_REG, val);

return pass;
}

0 comments on commit 2837643

Please sign in to comment.