Code style fixes
This commit is contained in:
parent
8e5d255b5b
commit
f58b387bd3
2 changed files with 104 additions and 90 deletions
|
@ -26,7 +26,7 @@
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* @(#)$Id: sht11.c,v 1.1 2007/10/22 12:19:58 nvt-se Exp $
|
* @(#)$Id: sht11.c,v 1.2 2008/07/08 08:23:47 adamdunkels Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -57,7 +57,7 @@
|
||||||
|
|
||||||
/* This can probably be reduced to 250ns according to data sheet. */
|
/* This can probably be reduced to 250ns according to data sheet. */
|
||||||
#define delay_400ns() _NOP()
|
#define delay_400ns() _NOP()
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
static void
|
static void
|
||||||
sstart(void)
|
sstart(void)
|
||||||
{
|
{
|
||||||
|
@ -75,20 +75,21 @@ sstart(void)
|
||||||
delay_400ns();
|
delay_400ns();
|
||||||
SCL_0();
|
SCL_0();
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
static void
|
static void
|
||||||
sreset(void)
|
sreset(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
SDA_1(); SCL_0();
|
SDA_1();
|
||||||
for (i = 0; i < 9 ; i++) {
|
SCL_0();
|
||||||
|
for(i = 0; i < 9 ; i++) {
|
||||||
SCL_1();
|
SCL_1();
|
||||||
delay_400ns();
|
delay_400ns();
|
||||||
SCL_0();
|
SCL_0();
|
||||||
}
|
}
|
||||||
sstart(); /* Start transmission, why??? */
|
sstart(); /* Start transmission, why??? */
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
/*
|
/*
|
||||||
* Return true if we received an ACK.
|
* Return true if we received an ACK.
|
||||||
*/
|
*/
|
||||||
|
@ -99,11 +100,12 @@ swrite(unsigned _c)
|
||||||
int i;
|
int i;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
for (i = 0; i < 8; i++, c <<= 1) {
|
for(i = 0; i < 8; i++, c <<= 1) {
|
||||||
if (c & 0x80)
|
if(c & 0x80) {
|
||||||
SDA_1();
|
SDA_1();
|
||||||
else
|
} else {
|
||||||
SDA_0();
|
SDA_0();
|
||||||
|
}
|
||||||
SCL_1();
|
SCL_1();
|
||||||
delay_400ns();
|
delay_400ns();
|
||||||
SCL_0();
|
SCL_0();
|
||||||
|
@ -118,7 +120,7 @@ swrite(unsigned _c)
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
static unsigned
|
static unsigned
|
||||||
sread(int send_ack)
|
sread(int send_ack)
|
||||||
{
|
{
|
||||||
|
@ -126,17 +128,19 @@ sread(int send_ack)
|
||||||
unsigned char c = 0x00;
|
unsigned char c = 0x00;
|
||||||
|
|
||||||
SDA_1();
|
SDA_1();
|
||||||
for (i = 0; i < 8; i++) {
|
for(i = 0; i < 8; i++) {
|
||||||
c <<= 1;
|
c <<= 1;
|
||||||
SCL_1();
|
SCL_1();
|
||||||
delay_400ns();
|
delay_400ns();
|
||||||
if (SDA_IS_1)
|
if(SDA_IS_1) {
|
||||||
c |= 0x1;
|
c |= 0x1;
|
||||||
|
}
|
||||||
SCL_0();
|
SCL_0();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (send_ack)
|
if(send_ack) {
|
||||||
SDA_0();
|
SDA_0();
|
||||||
|
}
|
||||||
SCL_1();
|
SCL_1();
|
||||||
delay_400ns();
|
delay_400ns();
|
||||||
SCL_0();
|
SCL_0();
|
||||||
|
@ -145,7 +149,7 @@ sread(int send_ack)
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
#define CRC_CHECK
|
#define CRC_CHECK
|
||||||
#ifdef CRC_CHECK
|
#ifdef CRC_CHECK
|
||||||
static unsigned char
|
static unsigned char
|
||||||
|
@ -162,23 +166,24 @@ rev8bits(unsigned char v)
|
||||||
r <<= s; /* Shift when v's highest bits are zero */
|
r <<= s; /* Shift when v's highest bits are zero */
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
/* BEWARE: Bit reversed CRC8 using polynomial ^8 + ^5 + ^4 + 1 */
|
/* BEWARE: Bit reversed CRC8 using polynomial ^8 + ^5 + ^4 + 1 */
|
||||||
static unsigned
|
static unsigned
|
||||||
crc8_add(unsigned acc, unsigned byte)
|
crc8_add(unsigned acc, unsigned byte)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
acc ^= byte;
|
acc ^= byte;
|
||||||
for (i = 0; i < 8; i++)
|
for(i = 0; i < 8; i++) {
|
||||||
if (acc & 0x80)
|
if(acc & 0x80) {
|
||||||
acc = (acc << 1) ^ 0x31;
|
acc = (acc << 1) ^ 0x31;
|
||||||
else
|
} else {
|
||||||
acc <<= 1;
|
acc <<= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
return acc & 0xff;
|
return acc & 0xff;
|
||||||
}
|
}
|
||||||
#endif /* CRC_CHECK */
|
#endif /* CRC_CHECK */
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
/*
|
/*
|
||||||
* Power up the device. The device can be used after an additional
|
* Power up the device. The device can be used after an additional
|
||||||
* 11ms waiting time.
|
* 11ms waiting time.
|
||||||
|
@ -195,7 +200,7 @@ sht11_init(void)
|
||||||
SHT11_PxOUT &= ~(BV(SHT11_ARCH_SDA) | BV(SHT11_ARCH_SCL));
|
SHT11_PxOUT &= ~(BV(SHT11_ARCH_SDA) | BV(SHT11_ARCH_SCL));
|
||||||
SHT11_PxDIR |= BV(SHT11_ARCH_PWR) | BV(SHT11_ARCH_SCL);
|
SHT11_PxDIR |= BV(SHT11_ARCH_PWR) | BV(SHT11_ARCH_SCL);
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
/*
|
/*
|
||||||
* Power of device.
|
* Power of device.
|
||||||
*/
|
*/
|
||||||
|
@ -206,24 +211,26 @@ sht11_off(void)
|
||||||
SHT11_PxOUT &= ~(BV(SHT11_ARCH_SDA) | BV(SHT11_ARCH_SCL));
|
SHT11_PxOUT &= ~(BV(SHT11_ARCH_SDA) | BV(SHT11_ARCH_SCL));
|
||||||
SHT11_PxDIR |= BV(SHT11_ARCH_PWR) | BV(SHT11_ARCH_SCL);
|
SHT11_PxDIR |= BV(SHT11_ARCH_PWR) | BV(SHT11_ARCH_SCL);
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
/*
|
/*
|
||||||
* Only commands MEASURE_HUMI or MEASURE_TEMP!
|
* Only commands MEASURE_HUMI or MEASURE_TEMP!
|
||||||
*/
|
*/
|
||||||
static unsigned
|
static unsigned int
|
||||||
scmd(unsigned cmd)
|
scmd(unsigned cmd)
|
||||||
{
|
{
|
||||||
unsigned long n;
|
unsigned long n;
|
||||||
|
|
||||||
if (cmd != MEASURE_HUMI && cmd != MEASURE_TEMP)
|
if(cmd != MEASURE_HUMI && cmd != MEASURE_TEMP) {
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
sstart(); /* Start transmission */
|
sstart(); /* Start transmission */
|
||||||
if (!swrite(cmd))
|
if(!swrite(cmd)) {
|
||||||
goto fail;
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
for (n = 0; n < 250000; n++)
|
for(n = 0; n < 250000; n++) {
|
||||||
if (!SDA_IS_1) {
|
if(!SDA_IS_1) {
|
||||||
unsigned t0, t1, rcrc;
|
unsigned t0, t1, rcrc;
|
||||||
t0 = sread(1);
|
t0 = sread(1);
|
||||||
t1 = sread(1);
|
t1 = sread(1);
|
||||||
|
@ -234,36 +241,38 @@ scmd(unsigned cmd)
|
||||||
crc = crc8_add(0x0, cmd);
|
crc = crc8_add(0x0, cmd);
|
||||||
crc = crc8_add(crc, t0);
|
crc = crc8_add(crc, t0);
|
||||||
crc = crc8_add(crc, t1);
|
crc = crc8_add(crc, t1);
|
||||||
if (crc != rev8bits(rcrc))
|
if(crc != rev8bits(rcrc)) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
return (t0 << 8) | t1;
|
return (t0 << 8) | t1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
sreset();
|
sreset();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
/*
|
/*
|
||||||
* Call may take up to 210ms.
|
* Call may take up to 210ms.
|
||||||
*/
|
*/
|
||||||
unsigned
|
unsigned int
|
||||||
sht11_temp(void)
|
sht11_temp(void)
|
||||||
{
|
{
|
||||||
return scmd(MEASURE_TEMP);
|
return scmd(MEASURE_TEMP);
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
/*
|
/*
|
||||||
* Call may take up to 210ms.
|
* Call may take up to 210ms.
|
||||||
*/
|
*/
|
||||||
unsigned
|
unsigned int
|
||||||
sht11_humidity(void)
|
sht11_humidity(void)
|
||||||
{
|
{
|
||||||
return scmd(MEASURE_HUMI);
|
return scmd(MEASURE_HUMI);
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
#if 0 /* But ok! */
|
#if 0 /* But ok! */
|
||||||
unsigned
|
unsigned
|
||||||
sht11_sreg(void)
|
sht11_sreg(void)
|
||||||
|
@ -271,8 +280,9 @@ sht11_sreg(void)
|
||||||
unsigned sreg, rcrc;
|
unsigned sreg, rcrc;
|
||||||
|
|
||||||
sstart(); /* Start transmission */
|
sstart(); /* Start transmission */
|
||||||
if (!swrite(STATUS_REG_R))
|
if(!swrite(STATUS_REG_R)) {
|
||||||
goto fail;
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
sreg = sread(1);
|
sreg = sread(1);
|
||||||
rcrc = sread(0);
|
rcrc = sread(0);
|
||||||
|
@ -294,16 +304,18 @@ sht11_sreg(void)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
#if 0
|
#if 0
|
||||||
int
|
int
|
||||||
sht11_set_sreg(unsigned sreg)
|
sht11_set_sreg(unsigned sreg)
|
||||||
{
|
{
|
||||||
sstart(); /* Start transmission */
|
sstart(); /* Start transmission */
|
||||||
if (!swrite(STATUS_REG_W))
|
if(!swrite(STATUS_REG_W)) {
|
||||||
goto fail;
|
goto fail;
|
||||||
if (!swrite(sreg))
|
}
|
||||||
|
if(!swrite(sreg)) {
|
||||||
goto fail;
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -312,14 +324,15 @@ sht11_set_sreg(unsigned sreg)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
#if 0
|
#if 0
|
||||||
int
|
int
|
||||||
sht11_reset(void)
|
sht11_reset(void)
|
||||||
{
|
{
|
||||||
sstart(); /* Start transmission */
|
sstart(); /* Start transmission */
|
||||||
if (!swrite(RESET))
|
if(!swrite(RESET)) {
|
||||||
goto fail;
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -328,3 +341,4 @@ sht11_reset(void)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
* SUCH DAMAGE.
|
* SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* @(#)$Id: sht11.h,v 1.1 2007/10/22 12:19:58 nvt-se Exp $
|
* @(#)$Id: sht11.h,v 1.2 2008/07/08 08:23:47 adamdunkels Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef SHT11_H
|
#ifndef SHT11_H
|
||||||
|
@ -35,9 +35,9 @@
|
||||||
void sht11_init(void);
|
void sht11_init(void);
|
||||||
void sht11_off(void);
|
void sht11_off(void);
|
||||||
|
|
||||||
unsigned sht11_temp(void);
|
unsigned int sht11_temp(void);
|
||||||
unsigned sht11_humidity(void);
|
unsigned int sht11_humidity(void);
|
||||||
unsigned sht11_sreg(void);
|
unsigned int sht11_sreg(void);
|
||||||
int sht11_set_sreg(unsigned);
|
int sht11_set_sreg(unsigned);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Add table
Reference in a new issue