I2C. Testhal changed.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/i2c_dev@3558 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
barthess 2011-12-06 09:09:53 +00:00
parent dbee267868
commit 1253ee88be
10 changed files with 11 additions and 257 deletions

View File

@ -82,8 +82,6 @@ CSRC = $(PORTSRC) \
$(CHIBIOS)/os/various/syscalls.c \
main.c \
i2c_pns.c \
tmp75.c\
max1236.c\
lis3.c\
@ -127,7 +125,6 @@ INCDIR = $(PORTINC) $(KERNINC) $(TESTINC) \
# Compiler settings
#
# -lm <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
MCU = cortex-m3
#TRGT = arm-elf-

View File

@ -192,7 +192,7 @@
* @note Disabling this option saves both code and data space.
*/
#if !defined(I2C_USE_WAIT) || defined(__DOXYGEN__)
#define I2C_USE_WAIT FALSE
#define I2C_USE_WAIT TRUE
#endif
/**
@ -202,13 +202,6 @@
#define I2C_USE_MUTUAL_EXCLUSION TRUE
#endif
/**
* @brief Switch to asynchronouse driver with callbacks.
*/
#if !defined(I2C_SUPPORTS_CALLBACKS) || defined(__DOXYGEN__)
#define I2C_SUPPORTS_CALLBACKS TRUE
#endif
/*===========================================================================*/
/* MAC driver related settings. */
/*===========================================================================*/

View File

@ -4,29 +4,12 @@
#include "i2c_pns.h"
#include "lis3.h"
#include "tmp75.h"
#include "max1236.h"
/* I2C1 */
static const I2CConfig i2cfg1 = {
OPMODE_I2C,
100000,
STD_DUTY_CYCLE,
0,
0,
0,
0,
};
/* I2C2 */
static const I2CConfig i2cfg2 = {
OPMODE_I2C,
100000,
STD_DUTY_CYCLE,
0,
0,
0,
0,
};
@ -35,22 +18,14 @@ void I2CInit_pns(void){
i2cInit();
i2cStart(&I2CD1, &i2cfg1);
i2cStart(&I2CD2, &i2cfg2);
/* tune ports for I2C1*/
palSetPadMode(IOPORT2, 6, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
palSetPadMode(IOPORT2, 7, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
/* tune ports for I2C2*/
palSetPadMode(IOPORT2, 10, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
palSetPadMode(IOPORT2, 11, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
/* startups. Pauses added just to be safe */
chThdSleepMilliseconds(1000);
init_max1236();
chThdSleepMilliseconds(1000);
chThdSleepMilliseconds(100);
init_lis3();
chThdSleepMilliseconds(1000);
}

View File

@ -18,8 +18,8 @@
/* buffers */
static i2cblock_t accel_rx_data[ACCEL_RX_DEPTH];
static i2cblock_t accel_tx_data[ACCEL_TX_DEPTH];
static uint8_t accel_rx_data[ACCEL_RX_DEPTH];
static uint8_t accel_tx_data[ACCEL_TX_DEPTH];
static int16_t acceleration_x = 0;
static int16_t acceleration_y = 0;
@ -42,8 +42,8 @@ static void i2c_lis3_cb(I2CDriver *i2cp, const I2CSlaveConfig *i2cscfg){
/* Accelerometer lis3lv02dq config */
static const I2CSlaveConfig lis3 = {
i2c_lis3_cb,
i2c_lis3_error_cb,
i2c_lis3_cb,
i2c_lis3_error_cb,
};
@ -67,9 +67,9 @@ int init_lis3(void){
*/
void request_acceleration_data(void){
accel_tx_data[0] = ACCEL_OUT_DATA | AUTO_INCREMENT_BIT; // register address
//i2cAcquireBus(&I2CD1);
i2cAcquireBus(&I2CD1);
i2cMasterTransmit(&I2CD1, &lis3, lis3_addr, accel_tx_data, 1, accel_rx_data, 6);
//i2cReleaseBus(&I2CD1);
i2cReleaseBus(&I2CD1);
acceleration_x = accel_rx_data[0] + (accel_rx_data[1] << 8);
acceleration_y = accel_rx_data[2] + (accel_rx_data[3] << 8);

View File

@ -17,14 +17,10 @@
* amount of time.
*/
#include <stdlib.h>
#include "ch.h"
#include "hal.h"
#include "i2c_pns.h"
#include "tmp75.h"
#include "max1236.h"
#include "lis3.h"
@ -44,39 +40,9 @@ static msg_t Blink(void *arg) {
return 0;
}
/* Temperature polling thread */
static WORKING_AREA(PollTmp75ThreadWA, 128);
static msg_t PollTmp75Thread(void *arg) {
(void)arg;
systime_t time = chTimeNow();
while (TRUE) {
time += MS2ST(1001);
/* Call reading function */
request_temperature();
chThdSleepUntil(time);
}
return 0;
}
/* MAX1236 polling thread */
static WORKING_AREA(PollMax1236ThreadWA, 128);
static msg_t PollMax1236Thread(void *arg) {
(void)arg;
systime_t time = chTimeNow();
while (TRUE) {
time += MS2ST(200);
/* Call reading function */
read_max1236();
chThdSleepUntil(time);
}
return 0;
}
/*
* Accelerometer thread
*/
static WORKING_AREA(PollAccelThreadWA, 128);
static msg_t PollAccelThread(void *arg) {
(void)arg;
@ -104,22 +70,6 @@ int main(void) {
I2CInit_pns();
/* Create temperature thread */
chThdCreateStatic(PollTmp75ThreadWA,
sizeof(PollTmp75ThreadWA),
NORMALPRIO,
PollTmp75Thread,
NULL);
/* Create max1236 thread */
chThdCreateStatic(PollMax1236ThreadWA,
sizeof(PollMax1236ThreadWA),
NORMALPRIO,
PollMax1236Thread,
NULL);
/* Create accelerometer thread */
chThdCreateStatic(PollAccelThreadWA,
sizeof(PollAccelThreadWA),

View File

@ -1,75 +0,0 @@
/**
* Maxim ADC has not so suitable default settings after startup.
* So we will create init function to tune this ADC.
*/
#include <stdlib.h>
#include "ch.h"
#include "hal.h"
#include "max1236.h"
#define max1236_addr 0b0110100
/* Data buffers */
static i2cblock_t max1236_rx_data[MAX1236_RX_DEPTH];
static i2cblock_t max1236_tx_data[MAX1236_TX_DEPTH];
/* ADC results */
static uint16_t ch1 = 0, ch2 = 0, ch3 = 0, ch4 = 0;
/* Error trap */
static void i2c_max1236_error_cb(I2CDriver *i2cp, const I2CSlaveConfig *i2cscfg){
(void)i2cscfg;
int status = 0;
status = i2cp->id_i2c->SR1;
while(TRUE);
}
/* This callback raise up when transfer finished */
static void i2c_max1236_cb(I2CDriver *i2cp, const I2CSlaveConfig *i2cscfg){
(void)*i2cp;
(void)*i2cscfg;
/* get ADC data */
}
/* ADC maxim MAX1236 config */
static const I2CSlaveConfig max1236 = {
i2c_max1236_cb,
i2c_max1236_error_cb,
};
/**
* Initilization routine. See datasheet on page 13 to understand
* how to initialize ADC.
*/
void init_max1236(void){
/* this data we must send via IC to setup ADC */
max1236_tx_data[0] = 0b10000011; /* config register content. Consult datasheet */
max1236_tx_data[1] = 0b00000111; /* config register content. Consult datasheet */
/* transmit out 2 bytes */
i2cAcquireBus(&I2CD2);
i2cMasterTransmit(&I2CD2, &max1236, max1236_addr, max1236_tx_data, 2, max1236_rx_data, 0);
i2cReleaseBus(&I2CD2);
}
/* Now simply read 8 bytes to get all 4 ADC channels */
void read_max1236(void){
i2cAcquireBus(&I2CD2);
i2cMasterReceive(&I2CD2, &max1236, max1236_addr, max1236_rx_data, 8);
i2cReleaseBus(&I2CD2);
ch1 = ((max1236_rx_data[0] & 0xF) << 8) + max1236_rx_data[1];
ch2 = ((max1236_rx_data[2] & 0xF) << 8) + max1236_rx_data[3];
ch3 = ((max1236_rx_data[4] & 0xF) << 8) + max1236_rx_data[5];
ch4 = ((max1236_rx_data[6] & 0xF) << 8) + max1236_rx_data[7];
}

View File

@ -1,14 +0,0 @@
#include "ch.h"
#ifndef MAX1236_H_
#define MAX1236_H_
#define MAX1236_RX_DEPTH 8
#define MAX1236_TX_DEPTH 2
void init_max1236(void);
void read_max1236(void);
#endif /* MAX1236_H_ */

View File

@ -174,13 +174,6 @@
#define STM32_I2C_I2C2_DMA_PRIORITY 4
#define STM32_I2C_I2C1_DMA_ERROR_HOOK() chSysHalt()
#define STM32_I2C_I2C2_DMA_ERROR_HOOK() chSysHalt()
/* I2C1 */
#define STM32_I2C_I2C1_USE_GPT_TIM GPTD1
#define STM32_I2C_I2C1_USE_POLLING_WAIT TRUE
/* I2C2 */
#define STM32_I2C_I2C2_USE_GPT_TIM GPTD2
#define STM32_I2C_I2C2_USE_POLLING_WAIT TRUE
/*
* USB driver system settings.

View File

@ -1,52 +0,0 @@
/**
* TMP75 is most simple I2C device in our case. It is already useful with
* default settings after powerup.
* You only must read 2 sequential bytes from it.
*/
#include <stdlib.h>
#include "ch.h"
#include "hal.h"
#include "tmp75.h"
/* input buffer */
static i2cblock_t tmp75_rx_data[TMP75_RX_DEPTH];
/* temperature value */
static int16_t temperature = 0;
/* Simple error trap */
static void i2c_tmp75_error_cb(I2CDriver *i2cp, const I2CSlaveConfig *i2cscfg){
(void)i2cscfg;
int status = 0;
status = i2cp->id_i2c->SR1;
while(TRUE);
}
/* This callback raise up when transfer finished */
static void i2c_tmp75_cb(I2CDriver *i2cp, const I2CSlaveConfig *i2cscfg){
(void)*i2cp;
(void)*i2cscfg;
/* store temperature value */
}
/* Fill TMP75 config. */
static const I2CSlaveConfig tmp75 = {
i2c_tmp75_cb,
i2c_tmp75_error_cb,
};
#define tmp75_addr 0b1001000
/* This is main function. */
void request_temperature(void){
i2cAcquireBus(&I2CD2);
i2cMasterReceive(&I2CD2, &tmp75, tmp75_addr, tmp75_rx_data, 2);
i2cReleaseBus(&I2CD2);
temperature = (tmp75_rx_data[0] << 8) + tmp75_rx_data[1];
}

View File

@ -1,13 +0,0 @@
#ifndef TMP75_H_
#define TMP75_H_
/* buffers depth */
#define TMP75_RX_DEPTH 2
#define TMP75_TX_DEPTH 2
void init_tmp75(void);
void request_temperature(void);
#endif /* TMP75_H_ */