mirror of
https://github.com/eddyem/tsys01.git
synced 2026-02-01 04:45:05 +03:00
Fix bug in I2C multiplexer, measurement works
This commit is contained in:
parent
8d43610e1b
commit
5e73de26a5
@ -8,7 +8,7 @@ MCU = F042x6
|
|||||||
# hardware definitions
|
# hardware definitions
|
||||||
DEFS := -DUSARTNUM=1 -DI2CPINS=67
|
DEFS := -DUSARTNUM=1 -DI2CPINS=67
|
||||||
#DEFS += -DCHECK_TMOUT
|
#DEFS += -DCHECK_TMOUT
|
||||||
DEFS += -DEBUG
|
#DEFS += -DEBUG
|
||||||
# change this linking script depending on particular MCU model,
|
# change this linking script depending on particular MCU model,
|
||||||
# for example, if you have STM32F103VBT6, you should write:
|
# for example, if you have STM32F103VBT6, you should write:
|
||||||
LDSCRIPT = ld/stm32f042k.ld
|
LDSCRIPT = ld/stm32f042k.ld
|
||||||
|
|||||||
35
STM32/TSYS_controller/can.c
Normal file
35
STM32/TSYS_controller/can.c
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* geany_encoding=koi8-r
|
||||||
|
* can.c
|
||||||
|
*
|
||||||
|
* Copyright 2018 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
|
||||||
|
*
|
||||||
|
* 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; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* 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, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#include "can.h"
|
||||||
|
|
||||||
|
static uint8_t CAN_addr = 0;
|
||||||
|
|
||||||
|
// get CAN address data from GPIO pins
|
||||||
|
void readCANaddr(){
|
||||||
|
CAN_addr = READ_CAN_INV_ADDR();
|
||||||
|
CAN_addr = ~CAN_addr & 0x7;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t getCANaddr(){
|
||||||
|
return CAN_addr;
|
||||||
|
}
|
||||||
32
STM32/TSYS_controller/can.h
Normal file
32
STM32/TSYS_controller/can.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* geany_encoding=koi8-r
|
||||||
|
* can.h
|
||||||
|
*
|
||||||
|
* Copyright 2018 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
|
||||||
|
*
|
||||||
|
* 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; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* 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, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||||
|
* MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#ifndef __CAN_H__
|
||||||
|
#define __CAN_H__
|
||||||
|
|
||||||
|
#include "hardware.h"
|
||||||
|
|
||||||
|
void readCANaddr();
|
||||||
|
uint8_t getCANaddr();
|
||||||
|
|
||||||
|
#endif // __CAN_H__
|
||||||
@ -22,6 +22,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
|
#include "usart.h"
|
||||||
|
|
||||||
I2C_SPEED curI2Cspeed = LOW_SPEED;
|
I2C_SPEED curI2Cspeed = LOW_SPEED;
|
||||||
|
|
||||||
@ -37,13 +38,17 @@ void gpio_setup(void){
|
|||||||
GPIO_MODER_MODER10_O | GPIO_MODER_MODER11_O |
|
GPIO_MODER_MODER10_O | GPIO_MODER_MODER11_O |
|
||||||
GPIO_MODER_MODER0_O | GPIO_MODER_MODER1_O |
|
GPIO_MODER_MODER0_O | GPIO_MODER_MODER1_O |
|
||||||
GPIO_MODER_MODER2_O | GPIO_MODER_MODER12_O;
|
GPIO_MODER_MODER2_O | GPIO_MODER_MODER12_O;
|
||||||
// multiplexer outputs are push-pull:
|
// multiplexer outputs are push-pull, GPIOB->OTYPER = 0
|
||||||
GPIOB->OTYPER |= GPIO_OTYPER_OT_0 | GPIO_OTYPER_OT_1 | GPIO_OTYPER_OT_2 |
|
|
||||||
GPIO_OTYPER_OT_12;
|
|
||||||
MUL_OFF();
|
MUL_OFF();
|
||||||
// PA8 - power enable
|
// PA8 - power enable
|
||||||
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER8)) |
|
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER8)) |
|
||||||
GPIO_MODER_MODER8_O;
|
GPIO_MODER_MODER8_O;
|
||||||
|
// PA13..15 - CAN address, pullup inputs
|
||||||
|
GPIOA->PUPDR = (GPIOA->PUPDR & ~(GPIO_PUPDR_PUPDR13 | GPIO_PUPDR_PUPDR14 |
|
||||||
|
GPIO_PUPDR_PUPDR15)
|
||||||
|
) |
|
||||||
|
GPIO_PUPDR_PUPDR13_0 | GPIO_PUPDR_PUPDR14_0 |
|
||||||
|
GPIO_PUPDR_PUPDR15_0;
|
||||||
pin_set(LED0_port, LED0_pin); // clear LEDs
|
pin_set(LED0_port, LED0_pin); // clear LEDs
|
||||||
pin_set(LED1_port, LED1_pin);
|
pin_set(LED1_port, LED1_pin);
|
||||||
}
|
}
|
||||||
@ -54,6 +59,7 @@ void i2c_setup(I2C_SPEED speed){
|
|||||||
}else{
|
}else{
|
||||||
curI2Cspeed = speed;
|
curI2Cspeed = speed;
|
||||||
}
|
}
|
||||||
|
MSG("setup I2C\n");
|
||||||
I2C1->CR1 = 0;
|
I2C1->CR1 = 0;
|
||||||
#if I2CPINS == 910
|
#if I2CPINS == 910
|
||||||
/*
|
/*
|
||||||
|
|||||||
@ -72,6 +72,9 @@
|
|||||||
// check overcurrent (PB3 == 0)
|
// check overcurrent (PB3 == 0)
|
||||||
#define SENSORS_OVERCURNT() ((1<<3) != (GPIOB->IDR & (1<<3)))
|
#define SENSORS_OVERCURNT() ((1<<3) != (GPIOB->IDR & (1<<3)))
|
||||||
|
|
||||||
|
// CAN address - PA13..PA15
|
||||||
|
#define READ_CAN_INV_ADDR() ((GPIOA->IDR & (0x7<<13))>>13)
|
||||||
|
|
||||||
typedef enum{
|
typedef enum{
|
||||||
VERYLOW_SPEED,
|
VERYLOW_SPEED,
|
||||||
LOW_SPEED,
|
LOW_SPEED,
|
||||||
|
|||||||
@ -23,6 +23,7 @@
|
|||||||
#include "stm32f0.h"
|
#include "stm32f0.h"
|
||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
#include "i2c.h"
|
#include "i2c.h"
|
||||||
|
#include "usart.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* I2C for TSYS01
|
* I2C for TSYS01
|
||||||
@ -51,9 +52,17 @@ static uint32_t cntr;
|
|||||||
*/
|
*/
|
||||||
uint8_t write_i2c(uint8_t addr, uint8_t data){
|
uint8_t write_i2c(uint8_t addr, uint8_t data){
|
||||||
cntr = Tms;
|
cntr = Tms;
|
||||||
while(I2C1->ISR & I2C_ISR_BUSY) if(Tms - cntr > I2C_TIMEOUT) return 0; // check busy
|
I2C1->ICR = 0x3f38; // clear all errors
|
||||||
|
while(I2C1->ISR & I2C_ISR_BUSY) if(Tms - cntr > I2C_TIMEOUT){
|
||||||
|
MSG("always busy\n");
|
||||||
|
return 0; // check busy
|
||||||
|
}
|
||||||
cntr = Tms;
|
cntr = Tms;
|
||||||
while(I2C1->CR2 & I2C_CR2_START) if(Tms - cntr > I2C_TIMEOUT) return 0; // check start
|
while(I2C1->CR2 & I2C_CR2_START) if(Tms - cntr > I2C_TIMEOUT){
|
||||||
|
MSG("always start\n");
|
||||||
|
return 0; // check start
|
||||||
|
}
|
||||||
|
//I2C1->ICR = 0x3f38; // clear all errors
|
||||||
I2C1->CR2 = 1<<16 | addr | I2C_CR2_AUTOEND; // 1 byte, autoend
|
I2C1->CR2 = 1<<16 | addr | I2C_CR2_AUTOEND; // 1 byte, autoend
|
||||||
// now start transfer
|
// now start transfer
|
||||||
I2C1->CR2 |= I2C_CR2_START;
|
I2C1->CR2 |= I2C_CR2_START;
|
||||||
@ -61,11 +70,19 @@ uint8_t write_i2c(uint8_t addr, uint8_t data){
|
|||||||
while(!(I2C1->ISR & I2C_ISR_TXIS)){ // ready to transmit
|
while(!(I2C1->ISR & I2C_ISR_TXIS)){ // ready to transmit
|
||||||
if(I2C1->ISR & I2C_ISR_NACKF){
|
if(I2C1->ISR & I2C_ISR_NACKF){
|
||||||
I2C1->ICR |= I2C_ICR_NACKCF;
|
I2C1->ICR |= I2C_ICR_NACKCF;
|
||||||
|
//I2C1->ICR = 0x3f38;
|
||||||
|
MSG("NACK\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(Tms - cntr > I2C_TIMEOUT){
|
||||||
|
//I2C1->ICR = 0x3f38;
|
||||||
|
MSG("Timeout\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(Tms - cntr > I2C_TIMEOUT) return 0;
|
|
||||||
}
|
}
|
||||||
I2C1->TXDR = data; // send data
|
I2C1->TXDR = data; // send data
|
||||||
|
// wait for data gone
|
||||||
|
while(I2C1->ISR & I2C_ISR_BUSY) if(Tms - cntr > I2C_TIMEOUT){break;}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,9 +93,17 @@ uint8_t write_i2c(uint8_t addr, uint8_t data){
|
|||||||
uint8_t read_i2c(uint8_t addr, uint32_t *data, uint8_t nbytes){
|
uint8_t read_i2c(uint8_t addr, uint32_t *data, uint8_t nbytes){
|
||||||
uint32_t result = 0;
|
uint32_t result = 0;
|
||||||
cntr = Tms;
|
cntr = Tms;
|
||||||
while(I2C1->ISR & I2C_ISR_BUSY) if(Tms - cntr > 5) return 0; // check busy
|
MSG("read_i2c\n");
|
||||||
|
while(I2C1->ISR & I2C_ISR_BUSY) if(Tms - cntr > I2C_TIMEOUT){
|
||||||
|
MSG("always busy\n");
|
||||||
|
return 0; // check busy
|
||||||
|
}
|
||||||
cntr = Tms;
|
cntr = Tms;
|
||||||
while(I2C1->CR2 & I2C_CR2_START) if(Tms - cntr > 5) return 0; // check start
|
while(I2C1->CR2 & I2C_CR2_START) if(Tms - cntr > I2C_TIMEOUT){
|
||||||
|
MSG("always start\n");
|
||||||
|
return 0; // check start
|
||||||
|
}
|
||||||
|
// I2C1->ICR = 0x3f38; // clear all errors
|
||||||
// read N bytes
|
// read N bytes
|
||||||
I2C1->CR2 = (nbytes<<16) | addr | 1 | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN;
|
I2C1->CR2 = (nbytes<<16) | addr | 1 | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN;
|
||||||
I2C1->CR2 |= I2C_CR2_START;
|
I2C1->CR2 |= I2C_CR2_START;
|
||||||
@ -88,9 +113,15 @@ uint8_t read_i2c(uint8_t addr, uint32_t *data, uint8_t nbytes){
|
|||||||
while(!(I2C1->ISR & I2C_ISR_RXNE)){ // wait for data
|
while(!(I2C1->ISR & I2C_ISR_RXNE)){ // wait for data
|
||||||
if(I2C1->ISR & I2C_ISR_NACKF){
|
if(I2C1->ISR & I2C_ISR_NACKF){
|
||||||
I2C1->ICR |= I2C_ICR_NACKCF;
|
I2C1->ICR |= I2C_ICR_NACKCF;
|
||||||
|
//I2C1->ICR = 0x3f38;
|
||||||
|
MSG("NACK\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(Tms - cntr > I2C_TIMEOUT){
|
||||||
|
//I2C1->ICR = 0x3f38;
|
||||||
|
MSG("Timeout\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if(Tms - cntr > 5) return 0;
|
|
||||||
}
|
}
|
||||||
result = (result << 8) | I2C1->RXDR;
|
result = (result << 8) | I2C1->RXDR;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,19 +21,19 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// timeout in ms
|
// timeout of I2C bus in ms
|
||||||
#define I2C_TIMEOUT (15)
|
#define I2C_TIMEOUT (100)
|
||||||
// CSB=1, address 1110110
|
// CSB=1, address 1110110
|
||||||
#define TSYS01_ADDR0 (0x76 << 1)
|
#define TSYS01_ADDR0 (0x76 << 1)
|
||||||
// CSB=0, address 1110111
|
// CSB=0, address 1110111
|
||||||
#define TSYS01_ADDR1 (0x77 << 1)
|
#define TSYS01_ADDR1 (0x77 << 1)
|
||||||
// registers: reset, read ADC value, start converstion, sart of PROM
|
// registers: reset, read ADC value, start converstion, start of PROM
|
||||||
#define TSYS01_RESET (0x1E)
|
#define TSYS01_RESET (0x1E)
|
||||||
#define TSYS01_ADC_READ (0x00)
|
#define TSYS01_ADC_READ (0x00)
|
||||||
#define TSYS01_START_CONV (0x48)
|
#define TSYS01_START_CONV (0x48)
|
||||||
#define TSYS01_PROM_ADDR0 (0xA0)
|
#define TSYS01_PROM_ADDR0 (0xA0)
|
||||||
// conversion time = 10ms
|
// conversion time (with reserve)
|
||||||
#define CONV_TIME (10)
|
#define CONV_TIME (15)
|
||||||
|
|
||||||
uint8_t read_i2c(uint8_t addr, uint32_t *data, uint8_t nbytes);
|
uint8_t read_i2c(uint8_t addr, uint32_t *data, uint8_t nbytes);
|
||||||
uint8_t write_i2c(uint8_t addr, uint8_t data);
|
uint8_t write_i2c(uint8_t addr, uint8_t data);
|
||||||
|
|||||||
@ -23,6 +23,7 @@
|
|||||||
#include "usart.h"
|
#include "usart.h"
|
||||||
#include "i2c.h"
|
#include "i2c.h"
|
||||||
#include "sensors_manage.h"
|
#include "sensors_manage.h"
|
||||||
|
#include "can.h"
|
||||||
|
|
||||||
#pragma message("USARTNUM=" STR(USARTNUM))
|
#pragma message("USARTNUM=" STR(USARTNUM))
|
||||||
#pragma message("I2CPINS=" STR(I2CPINS))
|
#pragma message("I2CPINS=" STR(I2CPINS))
|
||||||
@ -61,7 +62,7 @@ void iwdg_setup(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(void){
|
int main(void){
|
||||||
uint32_t lastT = 0;
|
uint32_t lastT = 0, lastS = 0;
|
||||||
int16_t L = 0;
|
int16_t L = 0;
|
||||||
char *txt;
|
char *txt;
|
||||||
sysreset();
|
sysreset();
|
||||||
@ -70,8 +71,11 @@ int main(void){
|
|||||||
usart_setup();
|
usart_setup();
|
||||||
i2c_setup(LOW_SPEED);
|
i2c_setup(LOW_SPEED);
|
||||||
iwdg_setup();
|
iwdg_setup();
|
||||||
|
readCANaddr();
|
||||||
|
|
||||||
SEND("Greetings!\n");
|
SEND("Greetings! My address is ");
|
||||||
|
printu(getCANaddr());
|
||||||
|
newline();
|
||||||
|
|
||||||
while (1){
|
while (1){
|
||||||
IWDG->KR = IWDG_REFRESH; // refresh watchdog
|
IWDG->KR = IWDG_REFRESH; // refresh watchdog
|
||||||
@ -79,7 +83,10 @@ int main(void){
|
|||||||
LED_blink(LED0);
|
LED_blink(LED0);
|
||||||
lastT = Tms;
|
lastT = Tms;
|
||||||
}
|
}
|
||||||
sensors_process();
|
if(lastS > Tms || Tms - lastS > 5){ // run sensors proc. once per 5ms
|
||||||
|
sensors_process();
|
||||||
|
lastS = Tms;
|
||||||
|
}
|
||||||
if(usartrx()){ // usart1 received data, store in in buffer
|
if(usartrx()){ // usart1 received data, store in in buffer
|
||||||
L = usart_getline(&txt);
|
L = usart_getline(&txt);
|
||||||
char _1st = txt[0];
|
char _1st = txt[0];
|
||||||
@ -89,7 +96,7 @@ int main(void){
|
|||||||
case 'C': // 'C' - show coefficients
|
case 'C': // 'C' - show coefficients
|
||||||
showcoeffs();
|
showcoeffs();
|
||||||
break;
|
break;
|
||||||
case 'D':
|
case 'O':
|
||||||
sensors_on();
|
sensors_on();
|
||||||
break;
|
break;
|
||||||
case 'T': // 'T' - get temperature
|
case 'T': // 'T' - get temperature
|
||||||
@ -111,14 +118,40 @@ int main(void){
|
|||||||
i2c_setup(HIGH_SPEED);
|
i2c_setup(HIGH_SPEED);
|
||||||
SEND("High speed\n");
|
SEND("High speed\n");
|
||||||
break;
|
break;
|
||||||
|
case 'G':
|
||||||
|
SEND("Can address: ");
|
||||||
|
printu(getCANaddr());
|
||||||
|
newline();
|
||||||
|
break;
|
||||||
|
#ifdef EBUG
|
||||||
|
case 'd':
|
||||||
|
case 'g':
|
||||||
|
case 't':
|
||||||
|
case 's':
|
||||||
|
senstest(_1st);
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
sensors_process();
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
default: // help
|
default: // help
|
||||||
SEND("'C' - show coefficients\n"
|
SEND("'C' - show coefficients\n"
|
||||||
"'D' - slave discovery\n"
|
"'O' - turn On sensors\n"
|
||||||
"'T' - get raw temperature\n"
|
"'T' - get raw temperature\n"
|
||||||
"'R' - reinit I2C\n"
|
"'R' - reinit I2C\n"
|
||||||
"'V' - very low speed\n"
|
"'V' - very low speed\n"
|
||||||
"'L' - low speed\n"
|
"'L' - low speed\n"
|
||||||
"'H' - high speed\n");
|
"'H' - high speed\n"
|
||||||
|
"'G' - get CAN address\n"
|
||||||
|
#ifdef EBUG
|
||||||
|
"\t\tTEST OPTIONS\n"
|
||||||
|
"'d' - discovery\n"
|
||||||
|
"'g' - get coeff\n"
|
||||||
|
"'t' - measure temper\n"
|
||||||
|
"'s' - show temper measured\n"
|
||||||
|
"'p' - sensors_process()\n"
|
||||||
|
#endif
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -59,7 +59,7 @@ SensorsState sensors_get_state(){return Sstate;}
|
|||||||
static uint16_t calc_t(uint32_t t, int i){
|
static uint16_t calc_t(uint32_t t, int i){
|
||||||
uint16_t *coeff = coefficients[curr_mul_addr][i];
|
uint16_t *coeff = coefficients[curr_mul_addr][i];
|
||||||
if(coeff[0] == 0) return BAD_TEMPERATURE; // what is with coeffs?
|
if(coeff[0] == 0) return BAD_TEMPERATURE; // what is with coeffs?
|
||||||
if(t < 6500000 || t > 13000000) return BAD_TEMPERATURE; // wrong value - too small or too large
|
if(t < 600000 || t > 30000000) return BAD_TEMPERATURE; // wrong value - too small or too large
|
||||||
int j;
|
int j;
|
||||||
double d = (double)t/256., tmp = 0.;
|
double d = (double)t/256., tmp = 0.;
|
||||||
// k0*(-1.5e-2) + 0.1*1e-5*val*(1*k1 + 1e-5*val*(-2.*k2 + 1e-5*val*(4*k3 + 1e-5*val*(-2*k4))))
|
// k0*(-1.5e-2) + 0.1*1e-5*val*(1*k1 + 1e-5*val*(-2.*k2 + 1e-5*val*(4*k3 + 1e-5*val*(-2*k4))))
|
||||||
@ -146,20 +146,46 @@ static uint8_t resetproc(){
|
|||||||
static uint8_t getcoefsproc(){
|
static uint8_t getcoefsproc(){
|
||||||
uint8_t i, j;
|
uint8_t i, j;
|
||||||
const uint8_t regs[5] = {0xAA, 0xA8, 0xA6, 0xA4, 0xA2}; // commands for coefficients
|
const uint8_t regs[5] = {0xAA, 0xA8, 0xA6, 0xA4, 0xA2}; // commands for coefficients
|
||||||
|
#ifdef EBUG
|
||||||
|
MSG("sens_present[0]=");
|
||||||
|
printu(sens_present[0]);
|
||||||
|
SEND(", sens_present[1]=");
|
||||||
|
printu(sens_present[1]);
|
||||||
|
newline();
|
||||||
|
#endif
|
||||||
for(i = 0; i < 2; ++i){
|
for(i = 0; i < 2; ++i){
|
||||||
if(!(sens_present[i] & (1<<curr_mul_addr))) continue; // no sensors @ given line
|
if(!(sens_present[i] & (1<<curr_mul_addr))) continue; // no sensors @ given line
|
||||||
uint8_t err = 5;
|
uint8_t err = 5;
|
||||||
uint16_t *coef = coefficients[curr_mul_addr][i];
|
uint16_t *coef = coefficients[curr_mul_addr][i];
|
||||||
|
#ifdef EBUG
|
||||||
|
SEND("muladdr: ");
|
||||||
|
usart_putchar('0'+curr_mul_addr);
|
||||||
|
SEND(", i: ");
|
||||||
|
usart_putchar('0'+i);
|
||||||
|
newline();
|
||||||
|
#endif
|
||||||
for(j = 0; j < 5; ++j){
|
for(j = 0; j < 5; ++j){
|
||||||
uint32_t K;
|
uint32_t K;
|
||||||
|
MSG("N=");
|
||||||
|
#ifdef EBUG
|
||||||
|
usart_putchar('0'+j);
|
||||||
|
newline();
|
||||||
|
#endif
|
||||||
if(write_i2c(Taddr[i], regs[j])){
|
if(write_i2c(Taddr[i], regs[j])){
|
||||||
|
MSG("write\n");
|
||||||
if(read_i2c(Taddr[i], &K, 2)){
|
if(read_i2c(Taddr[i], &K, 2)){
|
||||||
coef[i] = K;
|
MSG("read: ");
|
||||||
|
#ifdef EBUG
|
||||||
|
printu(K);
|
||||||
|
newline();
|
||||||
|
#endif
|
||||||
|
coef[j] = K;
|
||||||
--err;
|
--err;
|
||||||
}else break;
|
}else break;
|
||||||
}else break;
|
}else break;
|
||||||
}
|
}
|
||||||
if(err){ // restart all procedures if we can't get coeffs of present sensor
|
if(err){ // restart all procedures if we can't get coeffs of present sensor
|
||||||
|
MSG("Error: can't get all coefficients!\n");
|
||||||
sensors_on();
|
sensors_on();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -172,11 +198,20 @@ static uint8_t msrtempproc(){
|
|||||||
uint8_t i, j;
|
uint8_t i, j;
|
||||||
for(i = 0; i < 2; ++i){
|
for(i = 0; i < 2; ++i){
|
||||||
if(!(sens_present[i] & (1<<curr_mul_addr))) continue; // no sensors @ given line
|
if(!(sens_present[i] & (1<<curr_mul_addr))) continue; // no sensors @ given line
|
||||||
|
MSG("Start measurement for #");
|
||||||
|
#ifdef EBUG
|
||||||
|
usart_putchar('0'+curr_mul_addr);
|
||||||
|
usart_putchar('_');
|
||||||
|
usart_putchar('0'+i);
|
||||||
|
newline();
|
||||||
|
#endif
|
||||||
for(j = 0; j < 5; ++j){
|
for(j = 0; j < 5; ++j){
|
||||||
if(write_i2c(Taddr[i], TSYS01_START_CONV)) break;
|
if(write_i2c(Taddr[i], TSYS01_START_CONV)) break;
|
||||||
|
MSG("try more\n");
|
||||||
if(!write_i2c(Taddr[i], TSYS01_RESET)) i2c_setup(CURRENT_SPEED); // maybe I2C restart will solve the problem?
|
if(!write_i2c(Taddr[i], TSYS01_RESET)) i2c_setup(CURRENT_SPEED); // maybe I2C restart will solve the problem?
|
||||||
}
|
}
|
||||||
if(j == 5){
|
if(j == 5){
|
||||||
|
MSG("error start monitoring, reset\n");
|
||||||
sensors_on(); // all very bad
|
sensors_on(); // all very bad
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -190,16 +225,32 @@ static uint8_t gettempproc(){
|
|||||||
for(i = 0; i < 2; ++i){
|
for(i = 0; i < 2; ++i){
|
||||||
if(!(sens_present[i] & (1<<curr_mul_addr))) continue; // no sensors @ given line
|
if(!(sens_present[i] & (1<<curr_mul_addr))) continue; // no sensors @ given line
|
||||||
uint8_t err = 1;
|
uint8_t err = 1;
|
||||||
|
MSG("Sensor #");
|
||||||
|
#ifdef EBUG
|
||||||
|
usart_putchar('0'+curr_mul_addr);
|
||||||
|
usart_putchar('_');
|
||||||
|
usart_putchar('0'+i);
|
||||||
|
newline();
|
||||||
|
#endif
|
||||||
if(write_i2c(Taddr[i], TSYS01_ADC_READ)){
|
if(write_i2c(Taddr[i], TSYS01_ADC_READ)){
|
||||||
uint32_t t;
|
uint32_t t;
|
||||||
|
MSG("Read\n");
|
||||||
if(read_i2c(Taddr[i], &t, 3) && t){
|
if(read_i2c(Taddr[i], &t, 3) && t){
|
||||||
|
#ifdef EBUG
|
||||||
|
SEND("Calc from ");
|
||||||
|
printu(t);
|
||||||
|
newline();
|
||||||
|
#endif
|
||||||
if(BAD_TEMPERATURE != (Temperatures[curr_mul_addr][i] = calc_t(t, i))){
|
if(BAD_TEMPERATURE != (Temperatures[curr_mul_addr][i] = calc_t(t, i))){
|
||||||
err = 0;
|
err = 0;
|
||||||
++Ntemp_measured;
|
++Ntemp_measured;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(err) write_i2c(Taddr[i], TSYS01_RESET);
|
if(err){
|
||||||
|
MSG("Can't read temperature\n");
|
||||||
|
write_i2c(Taddr[i], TSYS01_RESET);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -219,7 +270,10 @@ static uint8_t sensors_scan(uint8_t (* procfn)()){
|
|||||||
callctr = 0;
|
callctr = 0;
|
||||||
uint8_t s = procfn();
|
uint8_t s = procfn();
|
||||||
MUL_OFF();
|
MUL_OFF();
|
||||||
if(s) return 0;
|
if(s){ // start scan again if error
|
||||||
|
curr_mul_addr = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
if(++curr_mul_addr > MUL_MAX_ADDRESS){ // scan is over
|
if(++curr_mul_addr > MUL_MAX_ADDRESS){ // scan is over
|
||||||
curr_mul_addr = 0;
|
curr_mul_addr = 0;
|
||||||
return 1;
|
return 1;
|
||||||
@ -287,17 +341,21 @@ void sensors_process(){
|
|||||||
MSG("init->reset\n");
|
MSG("init->reset\n");
|
||||||
i2c_setup(CURRENT_SPEED);
|
i2c_setup(CURRENT_SPEED);
|
||||||
Sstate = SENS_RESETING;
|
Sstate = SENS_RESETING;
|
||||||
|
lastSensT = Tms;
|
||||||
|
overcurnt_ctr = 0;
|
||||||
break;
|
break;
|
||||||
case SENS_RESETING: // reset & discovery procedure
|
case SENS_RESETING: // reset & discovery procedure
|
||||||
overcurnt_ctr = 0;
|
if(Tms - lastSensT > POWERUP_TIME){
|
||||||
if(sensors_scan(resetproc)){
|
overcurnt_ctr = 0;
|
||||||
count_sensors(); // get total amount of sensors
|
if(sensors_scan(resetproc)){
|
||||||
if(Nsens_present){
|
count_sensors(); // get total amount of sensors
|
||||||
|
if(Nsens_present){
|
||||||
MSG("reset->getcoeff\n");
|
MSG("reset->getcoeff\n");
|
||||||
Sstate = SENS_GET_COEFFS;
|
Sstate = SENS_GET_COEFFS;
|
||||||
}else{ // no sensors found
|
}else{ // no sensors found
|
||||||
MSG("reset->off\n");
|
MSG("reset->off\n");
|
||||||
Sstate = SENS_OFF;
|
sensors_off();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -306,8 +364,8 @@ MSG("reset->off\n");
|
|||||||
MSG("got coeffs for ");
|
MSG("got coeffs for ");
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
printu(Nsens_present);
|
printu(Nsens_present);
|
||||||
|
SEND(" sensors ->start\n");
|
||||||
#endif
|
#endif
|
||||||
MSG(" sensors ->start\n");
|
|
||||||
Sstate = SENS_START_MSRMNT;
|
Sstate = SENS_START_MSRMNT;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -345,10 +403,44 @@ MSG("sleep->start\n");
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SENS_OVERCURNT: // try to reinit all after overcurrent
|
case SENS_OVERCURNT: // try to reinit all after overcurrent
|
||||||
MSG("overcurrent occured!\n");
|
MSG("try to turn on after overcurrent\n");
|
||||||
sensors_on();
|
sensors_on();
|
||||||
break;
|
break;
|
||||||
default: // do nothing
|
default: // do nothing
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef EBUG
|
||||||
|
void senstest(char cmd){
|
||||||
|
MUL_OFF();
|
||||||
|
SENSORS_ON();
|
||||||
|
if(SENSORS_OVERCURNT()){
|
||||||
|
SENSORS_OFF();
|
||||||
|
SEND("Overcurrent!\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
curr_mul_addr = 0;
|
||||||
|
MUL_ADDRESS(0);
|
||||||
|
MUL_ON();
|
||||||
|
switch (cmd){
|
||||||
|
case 'd': // discovery once
|
||||||
|
resetproc();
|
||||||
|
count_sensors();
|
||||||
|
break;
|
||||||
|
case 'g':
|
||||||
|
getcoefsproc();
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
msrtempproc();
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
gettempproc();
|
||||||
|
showtemperature();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Sstate = SENS_OFF;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|||||||
@ -26,6 +26,8 @@
|
|||||||
|
|
||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
|
|
||||||
|
// time for power up procedure (500ms)
|
||||||
|
#define POWERUP_TIME (500)
|
||||||
// time between two readings (3sec)
|
// time between two readings (3sec)
|
||||||
#define SLEEP_TIME (3000)
|
#define SLEEP_TIME (3000)
|
||||||
// error in measurement == -300degrC
|
// error in measurement == -300degrC
|
||||||
@ -53,4 +55,8 @@ void sensors_on();
|
|||||||
void showcoeffs();
|
void showcoeffs();
|
||||||
void showtemperature();
|
void showtemperature();
|
||||||
|
|
||||||
|
#ifdef EBUG
|
||||||
|
void senstest(char cmd);
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // __SENSORS_MANAGE_H__
|
#endif // __SENSORS_MANAGE_H__
|
||||||
|
|||||||
Binary file not shown.
@ -34,7 +34,7 @@
|
|||||||
#define NEWLINE() do{}while(LINE_BUSY == usart_send_blocking('\n', 1))
|
#define NEWLINE() do{}while(LINE_BUSY == usart_send_blocking('\n', 1))
|
||||||
|
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
#define MSG(str) SEND(str)
|
#define MSG(str) do{SEND(__FILE__ " (L" STR(__LINE__) "): " str);}while(0)
|
||||||
#else
|
#else
|
||||||
#define MSG(str)
|
#define MSG(str)
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user