Skip to content

Commit

Permalink
Merge pull request #8 from ramonaoptics/fix_i2c_with_new_Platformio
Browse files Browse the repository at this point in the history
Ensure that the I2C library is compiled with the teensy 3.2
  • Loading branch information
hmaarrfk authored May 31, 2021
2 parents 870eb95 + 63d4242 commit 02e303e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ install:
- pip install -U platformio
- platformio update
script:
- platformio run -e $PLATOFORMIO_ENVIRONMENT
- platformio run --verbose --environment $PLATOFORMIO_ENVIRONMENT
- mv .pio/build/${PLATOFORMIO_ENVIRONMENT}/firmware.elf teensytoany-${PLATOFORMIO_ENVIRONMENT}.elf
- mv .pio/build/${PLATOFORMIO_ENVIRONMENT}/firmware.hex teensytoany-${PLATOFORMIO_ENVIRONMENT}.hex
- cp teensytoany-${PLATOFORMIO_ENVIRONMENT}.elf teensytoany-${PLATOFORMIO_ENVIRONMENT}-${TRAVIS_BRANCH}.elf
Expand Down
3 changes: 1 addition & 2 deletions src/i2c.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "i2c.hpp"
#include <errno.h>
// Teensy 3.0
#if defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISK)
#if TEENSY_TO_ANY_HAS_I2C
#include <i2c_t3.h>
int I2CMaster::init(int baudrate, int timeout_ms, int address_size,
int address_msb_first) {
Expand Down
17 changes: 13 additions & 4 deletions src/i2c.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#pragma once
#if defined(__arm__) && defined(TEENSYDUINO) && defined(KINETISK)
#define HAS_I2C 1
// only support teensies 3.1, 3.2, 3.5, and 3.6
// See list of microcontroller units
// https://docs.platformio.org/en/latest/platforms/teensy.html
#if defined(TEENSYDUINO) && \
(defined(__MK20DX256__) || defined(__MK64FX512__) || \
defined(__MK66FX1M0__))
#define TEENSY_TO_ANY_HAS_I2C 1
#else
#define TEENSY_TO_ANY_HAS_I2C 0
#warning I2C library not available for this build.
#endif

#if TEENSY_TO_ANY_HAS_I2C
#include <unistd.h>

class I2CMaster {
Expand All @@ -22,6 +33,4 @@ class I2CMaster {
bool is_initialized = false;
bool slave_8bit_address = true;
};
#else
#define HAS_I2C 0
#endif
18 changes: 9 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ inline SPISettings my_spi_settings() {
return SPISettings(spi_baudrate, spi_bit_order, spi_data_mode);
}

#if HAS_I2C
#if TEENSY_TO_ANY_HAS_I2C
I2CMaster i2c;
#endif

Expand Down Expand Up @@ -71,7 +71,7 @@ int version_func(CommandRouter *cmd, int argc, const char **argv) {
}

int i2c_init(CommandRouter *cmd, int argc, const char **argv) {
#if HAS_I2C
#if TEENSY_TO_ANY_HAS_I2C
int baudrate = 100'000;
int timeout_ms = 200'000; // 200ms
int address_size = 2;
Expand All @@ -96,15 +96,15 @@ int i2c_init(CommandRouter *cmd, int argc, const char **argv) {
}

int i2c_reset(CommandRouter *cmd, int argc, const char **argv) {
#if HAS_I2C
#if TEENSY_TO_ANY_HAS_I2C
return i2c.reset();
#else
return -1;
#endif
}

int i2c_write_uint16(CommandRouter *cmd, int argc, const char **argv) {
#if HAS_I2C
#if TEENSY_TO_ANY_HAS_I2C
if (argc != 4)
return EINVAL;

Expand All @@ -118,7 +118,7 @@ int i2c_write_uint16(CommandRouter *cmd, int argc, const char **argv) {
}

int i2c_write_uint8(CommandRouter *cmd, int argc, const char **argv) {
#if HAS_I2C
#if TEENSY_TO_ANY_HAS_I2C
if (argc != 4)
return EINVAL;

Expand All @@ -133,7 +133,7 @@ int i2c_write_uint8(CommandRouter *cmd, int argc, const char **argv) {
}

int i2c_read_uint16(CommandRouter *cmd, int argc, const char **argv) {
#if HAS_I2C
#if TEENSY_TO_ANY_HAS_I2C
if (argc != 3)
return EINVAL;

Expand All @@ -153,7 +153,7 @@ int i2c_read_uint16(CommandRouter *cmd, int argc, const char **argv) {
}

int i2c_read_uint8(CommandRouter *cmd, int argc, const char **argv) {
#if HAS_I2C
#if TEENSY_TO_ANY_HAS_I2C
if (argc != 3)
return EINVAL;

Expand All @@ -173,7 +173,7 @@ int i2c_read_uint8(CommandRouter *cmd, int argc, const char **argv) {

int i2c_read_no_register_uint8(CommandRouter *cmd, int argc,
const char **argv) {
#if HAS_I2C
#if TEENSY_TO_ANY_HAS_I2C
if (argc != 2)
return EINVAL;

Expand All @@ -193,7 +193,7 @@ int i2c_read_no_register_uint8(CommandRouter *cmd, int argc,

int i2c_write_no_register_uint8(CommandRouter *cmd, int argc,
const char **argv) {
#if HAS_I2C
#if TEENSY_TO_ANY_HAS_I2C
if (argc != 3)
return EINVAL;

Expand Down

0 comments on commit 02e303e

Please sign in to comment.