-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for PCA95X4 I2C GPIO expander (#253)
- Loading branch information
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |