Skip to content

Commit

Permalink
Add I2C Mode switch API
Browse files Browse the repository at this point in the history
  • Loading branch information
borg42 committed Mar 26, 2013
1 parent cab0847 commit 2973023
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 9 deletions.
6 changes: 4 additions & 2 deletions software/src/config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* temperature-bricklet
* Copyright (C) 2010-2012 Olaf Lüke <olaf@tinkerforge.com>
* Copyright (C) 2010-2013 Olaf Lüke <olaf@tinkerforge.com>
*
* config.h: Temperature Bricklet specific configuration
*
Expand Down Expand Up @@ -31,7 +31,7 @@

#define BRICKLET_FIRMWARE_VERSION_MAJOR 2
#define BRICKLET_FIRMWARE_VERSION_MINOR 0
#define BRICKLET_FIRMWARE_VERSION_REVISION 0
#define BRICKLET_FIRMWARE_VERSION_REVISION 1

#define BRICKLET_HARDWARE_VERSION_MAJOR 1
#define BRICKLET_HARDWARE_VERSION_MINOR 1
Expand Down Expand Up @@ -65,6 +65,8 @@ typedef struct {
char threshold_option_save[NUM_SIMPLE_VALUES];

uint32_t tick;

uint8_t i2c_mode;
} BrickContext;

#endif
66 changes: 61 additions & 5 deletions software/src/temperature.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* temperature-bricklet
* Copyright (C) 2010-2012 Olaf Lüke <olaf@tinkerforge.com>
* Copyright (C) 2010-2013 Olaf Lüke <olaf@tinkerforge.com>
*
* temperature.c: Implementation of Temperature Bricklet messages
*
Expand Down Expand Up @@ -58,14 +58,40 @@ const SimpleUnitProperty sup[] = {
const uint8_t smp_length = sizeof(smp);

void invocation(const ComType com, const uint8_t *data) {
simple_invocation(com, data);

if(((MessageHeader*)data)->fid > FID_LAST) {
BA->com_return_error(data, sizeof(MessageHeader), MESSAGE_ERROR_CODE_NOT_SUPPORTED, com);
switch(((MessageHeader*)data)->fid) {

case FID_GET_TEMPERATURE:
case FID_SET_TEMPERATURE_CALLBACK_PERIOD:
case FID_GET_TEMPERATURE_CALLBACK_PERIOD:
case FID_SET_TEMPERATURE_CALLBACK_THRESHOLD:
case FID_GET_TEMPERATURE_CALLBACK_THRESHOLD:
case FID_SET_DEBOUNCE_PERIOD:
case FID_GET_DEBOUNCE_PERIOD: {
simple_invocation(com, data);
break;
}

case FID_SET_I2C_MODE: {
set_i2c_mode(com, (SetI2CMode*)data);
break;
}

case FID_GET_I2C_MODE: {
get_i2c_mode(com, (GetI2CMode*)data);
break;
}

default: {
BA->com_return_error(data, sizeof(MessageHeader), MESSAGE_ERROR_CODE_NOT_SUPPORTED, com);
break;
}
}


}

void constructor(void) {
BC->i2c_mode = I2C_MODE_FAST;
simple_constructor();
}

Expand Down Expand Up @@ -101,6 +127,12 @@ int16_t temperature_read(void) {

uint16_t value;

if(BC->i2c_mode == I2C_MODE_SLOW) {
// Switch to 100khz
BA->twid->pTwi->TWI_CWGR = 0;
BA->twid->pTwi->TWI_CWGR = (1 << 16) | (158<< 8) | 158;
}

BA->bricklet_select(port);

Twi* twi = BA->twid->pTwi;
Expand Down Expand Up @@ -128,5 +160,29 @@ int16_t temperature_read(void) {

BA->bricklet_deselect(port);

// Switch back to 400khz
BA->twid->pTwi->TWI_CWGR = 0;
BA->twid->pTwi->TWI_CWGR = (76 << 8) | 76;

return two_complement_12_to_16(value >> 4)*TEMP_SCALE_MUL/TEMP_SCALE_DIV;
}

void get_i2c_mode(const ComType com, const GetI2CMode *data) {
GetI2CModeReturn gi2cmr;

gi2cmr.header = data->header;
gi2cmr.header.length = sizeof(GetI2CModeReturn);
gi2cmr.mode = BC->i2c_mode;

BA->send_blocking_with_timeout(&gi2cmr, sizeof(GetI2CModeReturn), com);
}

void set_i2c_mode(const ComType com, const SetI2CMode *data) {
if(data->mode > I2C_MODE_SLOW) {
BA->com_return_error(data, sizeof(MessageHeader), MESSAGE_ERROR_CODE_INVALID_PARAMETER, com);
return;
}

BC->i2c_mode = data->mode;
BA->com_return_setter(com, data);
}
24 changes: 22 additions & 2 deletions software/src/temperature.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* temperature-bricklet
* Copyright (C) 2010-2012 Olaf Lüke <olaf@tinkerforge.com>
* Copyright (C) 2010-2013 Olaf Lüke <olaf@tinkerforge.com>
*
* temperature.h: Implementation of Temperature Bricklet messages
*
Expand All @@ -26,6 +26,9 @@

#include "bricklib/com/com_common.h"

#define I2C_MODE_FAST 0
#define I2C_MODE_SLOW 1

#define FID_GET_TEMPERATURE 1
#define FID_SET_TEMPERATURE_CALLBACK_PERIOD 2
#define FID_GET_TEMPERATURE_CALLBACK_PERIOD 3
Expand All @@ -35,8 +38,25 @@
#define FID_GET_DEBOUNCE_PERIOD 7
#define FID_TEMPERATURE 8
#define FID_TEMPERATURE_REACHED 9
#define FID_SET_I2C_MODE 10
#define FID_GET_I2C_MODE 11

typedef struct {
MessageHeader header;
} __attribute__((__packed__)) GetI2CMode;

typedef struct {
MessageHeader header;
uint8_t mode;
} __attribute__((__packed__)) GetI2CModeReturn;

typedef struct {
MessageHeader header;
uint8_t mode;
} __attribute__((__packed__)) SetI2CMode;

#define FID_LAST 9
void get_i2c_mode(const ComType com, const GetI2CMode *data);
void set_i2c_mode(const ComType com, const SetI2CMode *data);

int32_t get_temperature(const int32_t value);
int16_t two_complement_12_to_16(const int16_t value);
Expand Down

0 comments on commit 2973023

Please sign in to comment.