From 2d0ecadc5e9a8eafea92c92a23b0b919f3dfdae7 Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Sat, 26 Jan 2019 18:02:19 +0100 Subject: [PATCH] a lot of refactoring in drivers --- Debug/src/subdir.mk | 23 ++- include/main.h | 3 + src/delay.c | 57 +++++++ src/delay.h | 24 +++ src/it_handlers.c | 19 ++- src/{main.cpp => main.c} | 77 ++++------ src/rte_wx.c | 19 +++ src/rte_wx.h | 26 ++++ src/wx_handler.c | 66 ++++++++ src/wx_handler.h | 14 ++ system/include/aprs/afsk.h | 16 +- system/include/drivers/_dht22.h | 2 + system/include/drivers/dallas.h | 11 +- system/include/drivers/i2c.h | 1 + system/include/drivers/ms5611.h | 24 ++- system/src/aprs/afsk.c | 47 +----- system/src/aprs/telemetry.c | 2 +- system/src/drivers/dallas.c | 9 +- system/src/drivers/i2c.c | 4 + system/src/drivers/ms5611.c | 265 +++++++++++++++++++------------- 20 files changed, 474 insertions(+), 235 deletions(-) create mode 100644 src/delay.c create mode 100644 src/delay.h rename src/{main.cpp => main.c} (84%) create mode 100644 src/rte_wx.c create mode 100644 src/rte_wx.h create mode 100644 src/wx_handler.c create mode 100644 src/wx_handler.h diff --git a/Debug/src/subdir.mk b/Debug/src/subdir.mk index 2cd0446..e499a14 100644 --- a/Debug/src/subdir.mk +++ b/Debug/src/subdir.mk @@ -9,11 +9,14 @@ C_SRCS += \ ../src/PathConfig.c \ ../src/TimerConfig.c \ ../src/_write.c \ -../src/it_handlers.c +../src/delay.c \ +../src/it_handlers.c \ +../src/main.c \ +../src/rte_wx.c \ +../src/wx_handler.c CPP_SRCS += \ -../src/BlinkLed.cpp \ -../src/main.cpp +../src/BlinkLed.cpp OBJS += \ ./src/BlinkLed.o \ @@ -22,8 +25,11 @@ OBJS += \ ./src/PathConfig.o \ ./src/TimerConfig.o \ ./src/_write.o \ +./src/delay.o \ ./src/it_handlers.o \ -./src/main.o +./src/main.o \ +./src/rte_wx.o \ +./src/wx_handler.o C_DEPS += \ ./src/KissCommunication.d \ @@ -31,11 +37,14 @@ C_DEPS += \ ./src/PathConfig.d \ ./src/TimerConfig.d \ ./src/_write.d \ -./src/it_handlers.d +./src/delay.d \ +./src/it_handlers.d \ +./src/main.d \ +./src/rte_wx.d \ +./src/wx_handler.d CPP_DEPS += \ -./src/BlinkLed.d \ -./src/main.d +./src/BlinkLed.d # Each subdirectory must supply rules for building sources it contributes diff --git a/include/main.h b/include/main.h index 2acc557..b039a3f 100644 --- a/include/main.h +++ b/include/main.h @@ -5,6 +5,8 @@ extern uint32_t master_time; +extern uint32_t wx_sensors_pool_timer; + extern AX25Ctx ax25; extern Afsk a; @@ -24,6 +26,7 @@ extern float td; extern double pressure; uint16_t main_get_adc_sample(void); +void main_wx_decremenet_counter(void); diff --git a/src/delay.c b/src/delay.c new file mode 100644 index 0000000..f34ce32 --- /dev/null +++ b/src/delay.c @@ -0,0 +1,57 @@ +/* + * delay.c + * + * Created on: 26.01.2019 + * Author: mateusz + */ + +#include "main.h" + +uint16_t preset_delay_msecs = 0; +uint8_t preset_use_random = 0; + +// counter decrement in Systick handler +uint16_t delay_cnt = 0; + +void delay_fixed(uint16_t delay_in_msecs) { + + delay_cnt = delay_in_msecs; + + while(delay_cnt > 0); + + return; + +} + +void delay_random(void) { + + uint16_t sample = main_get_adc_sample(); + + // random element of delay value could vary from 0 to 300msecs in 20msec steps + delay_cnt = (uint16_t)(preset_delay_msecs / 4) + (sample % 15) * 20; + +} + +void delay_set(uint16_t delay_in_msecs, uint8_t randomize) { + preset_delay_msecs = delay_in_msecs; + + if (randomize == 1) { + preset_use_random = 1; + } + else { + preset_use_random = 0; + } +} + +void delay_from_preset(void) { + + delay_cnt = preset_delay_msecs; + + while(delay_cnt > 0); + + if (preset_use_random == 1) { + delay_random(); + } + + +} diff --git a/src/delay.h b/src/delay.h new file mode 100644 index 0000000..e21a533 --- /dev/null +++ b/src/delay.h @@ -0,0 +1,24 @@ +/* + * delay.h + * + * Created on: 26.01.2019 + * Author: mateusz + */ + +#ifndef DELAY_H_ +#define DELAY_H_ + +extern uint16_t delay_cnt; + +void delay_set(uint16_t delay_in_msecs, uint8_t randomize); +void delay_fixed(uint16_t delay_in_msecs); +void delay_from_preset(void); +void delay_random(void); + +inline void delay_decrement_counter(void) { + if (delay_cnt > 0) + delay_cnt -= 10; +} + + +#endif /* DELAY_H_ */ diff --git a/src/it_handlers.c b/src/it_handlers.c index ea79b46..fc3c015 100644 --- a/src/it_handlers.c +++ b/src/it_handlers.c @@ -16,7 +16,8 @@ #include "aprs/telemetry.h" #include "aprs/beacon.h" #include "main.h" -#include "afsk.h" +//#include "afsk.h" +#include "delay.h" #include "diag/Trace.h" @@ -31,17 +32,21 @@ char adc_sample_count = 0, adc_sample_c2 = 0; // Zmienna odliczająca próbki unsigned short int AdcBuffer[4]; // Bufor przechowujący kolejne wartości rejestru DR short int AdcValue; -// Systick interrupt used for time measurements, checking timeouts andSysTick_Handler +// Systick interrupt used for time measurements, checking timeouts and SysTick_Handler void SysTick_Handler(void) { // systick interrupt is generated every 10ms master_time += 10; + // decrementing a timer to trigger meteo measuremenets + main_wx_decremenet_counter(); + srl_keep_timeout(); i2cKeepTimeout(); - afsk_decrement_delay_counter(); + delay_decrement_counter(); + } void USART1_IRQHandler(void) { @@ -75,6 +80,7 @@ void TIM2_IRQHandler( void ) { void TIM3_IRQHandler(void) { // wysylanie wlasnej pozycji i danych WX TIM3->SR &= ~(1<<0); +/* #ifdef _METEO temperature = SensorBringTemperature(); td = DallasQuery(); @@ -87,13 +93,14 @@ void TIM3_IRQHandler(void) { #endif if (dht22State == DHT22_STATE_DONE || dht22State == DHT22_STATE_TIMEOUT) dht22State = DHT22_STATE_IDLE; - +*/ + /* #ifndef _MUTE_OWN if (WXInterval != 0 && WXI >= WXInterval) { trace_printf("Pogoda\r\n"); temperature = SensorBringTemperature(); - td = DallasQuery(); +// td = DallasQuery(); trace_printf("temperatura DS: %d\r\n", (int)td); pressure = (float)SensorBringPressure(); trace_printf("cisnienie MS: %d\r\n", (int)pressure); @@ -105,7 +112,7 @@ void TIM3_IRQHandler(void) { else WXI++; #endif -#endif +*/ if (BcnInterval != 0 && BcnI >= BcnInterval) { #ifndef _MUTE_OWN // while(ax25.afsk->hdlc.raw_dcd == TRUE); diff --git a/src/main.cpp b/src/main.c similarity index 84% rename from src/main.cpp rename to src/main.c index a29c328..77fd653 100644 --- a/src/main.cpp +++ b/src/main.c @@ -6,6 +6,7 @@ #include #include "main.h" +#include "delay.h" #include "station_config.h" @@ -27,12 +28,13 @@ //#include "BlinkLed.h" #ifdef _METEO +#include "wx_handler.h" #include "drivers/dallas.h" #include "drivers/ms5611.h" #include "drivers/i2c.h" #include "drivers/tx20.h" -#include "drivers/_dht22.h" #include "aprs/wx.h" +#include "rte_wx.h" #endif #ifdef _DALLAS_AS_TELEM @@ -60,6 +62,9 @@ // global variable incremented by the SysTick handler to measure time in miliseconds uint32_t master_time = 0; +// global variable used as a timer to trigger meteo sensors mesurements +uint32_t wx_sensors_pool_timer = 65500; + // global variables represending the AX25/APRS stack AX25Ctx ax25; Afsk a; @@ -79,13 +84,6 @@ unsigned char BcnInterval, WXInterval, BcnI = _BCN_INTERVAL - 2, WXI = _WX_INTER unsigned short rx10m = 0, tx10m = 0, digi10m = 0, kiss10m = 0; int t = 0; -float temperature; -float td; -double pressure = 0.0; - -#ifdef _METEO -dht22Values dht, dht_valid; -#endif static void message_callback(struct AX25Msg *msg) { @@ -110,10 +108,18 @@ main(int argc, char* argv[]) // Configuring the SysTick timer to generate interrupt 100x per second (one interrupt = 10ms) SysTick_Config(SystemCoreClock / 100); +#if defined _RANDOM_DELAY + // configuring a default delay value + delay_set(_DELAY_BASE, 1); +#elif !defined _RANDOM_DELAY + delay_set(_DELAY_BASE, 0); + +#endif + + // configuring an APRS path used to transmit own packets (telemetry, wx, beacons) path_len = ConfigPath(path); #ifdef _METEO -// DHT22_Init(); i2cConfigure(); #endif LedConfig(); @@ -131,18 +137,14 @@ main(int argc, char* argv[]) #endif srl_init(); - td = 0.0f; - temperature = 0.0f; - BcnInterval = _BCN_INTERVAL; WXInterval = _WX_INTERVAL; TelemInterval = 10; #ifdef _METEO - SensorReset(0xEC); - td = DallasQuery(); - SensorReadCalData(0xEC, SensorCalData); - SensorStartMeas(0); + ms5611_reset(&rte_wx_ms5611_qf); + ms5611_read_calibration(SensorCalData, &rte_wx_ms5611_qf); + ms5611_trigger_measure(0, 0); #endif // preparing initial beacon which will be sent to host PC using KISS protocol via UART @@ -189,21 +191,11 @@ main(int argc, char* argv[]) AFSK_Init(&a); ax25_init(&ax25, &a, 0, message_callback); + // getting all meteo measuremenets to be sure that WX frames want be sent with zeros + wx_get_all_measurements(); + srl_receive_data(100, FEND, FEND, 0, 0, 0); - -#ifdef _METEO - temperature = SensorBringTemperature(); - td = DallasQuery(); -#ifdef _DBG_TRACE - trace_printf("temperatura DS: %d\r\n", (int)td); -#endif - pressure = (float)SensorBringPressure(); -#ifdef _DBG_TRACE - trace_printf("cisnienie MS: %d\r\n", (int)pressure); -#endif -#endif - GPIO_ResetBits(GPIOC, GPIO_Pin_8 | GPIO_Pin_9); TimerConfig(); @@ -245,25 +237,15 @@ main(int argc, char* argv[]) srl_receive_data(120, FEND, FEND, 0, 0, 0); } #ifdef _METEO - dht22_timeout_keeper(); + if (wx_sensors_pool_timer < 10) { - switch (dht22State) { - case DHT22_STATE_IDLE: - dht22_comm(&dht); - break; - case DHT22_STATE_DATA_RDY: - dht22_decode(&dht); - break; - case DHT22_STATE_DATA_DECD: - dht_valid = dht; // powrot do stanu DHT22_STATE_IDLE jest w TIM3_IRQHandler - dht22State = DHT22_STATE_DONE; + wx_get_all_measurements(); -#ifdef _DBG_TRACE - trace_printf("DHT22: temperature=%d,humi=%d\r\n", dht_valid.scaledTemperature, dht_valid.humidity); -#endif - break; - default: break; + wx_sensors_pool_timer = 65500; } + + // dht22 sensor communication pooling + wx_pool_dht22(); #endif } // Infinite loop, never return. @@ -273,6 +255,11 @@ uint16_t main_get_adc_sample(void) { return (uint16_t) ADC1->DR; } +void main_wx_decremenet_counter(void) { + if (wx_sensors_pool_timer > 0) + wx_sensors_pool_timer -= 10; +} + #pragma GCC diagnostic pop // ---------------------------------------------------------------------------- diff --git a/src/rte_wx.c b/src/rte_wx.c new file mode 100644 index 0000000..9fc7f80 --- /dev/null +++ b/src/rte_wx.c @@ -0,0 +1,19 @@ +/* + * rte_wx.c + * + * Created on: 26.01.2019 + * Author: mateusz + */ + + +#include "rte_wx.h" + +float rte_wx_temperature_dallas = 0.0f, rte_wx_temperature_dallas_valid = 0.0f; +float rte_wx_temperature = 0.0f, rte_wx_temperature_valid = 0.0f; +float rte_wx_pressure = 0.0f, rte_wx_pressure_valid = 0.0f; + +dht22Values rte_wx_dht, rte_wx_dht_valid; // quality factor inside this structure +DallasQF rte_wx_dallas_qf; +ms5611_qf_t rte_wx_ms5611_qf; + + diff --git a/src/rte_wx.h b/src/rte_wx.h new file mode 100644 index 0000000..214e0eb --- /dev/null +++ b/src/rte_wx.h @@ -0,0 +1,26 @@ +/* + * rte_wx.h + * + * Created on: 26.01.2019 + * Author: mateusz + */ + +#include "drivers/_dht22.h" +#include "drivers/dallas.h" +#include "drivers/ms5611.h" + + +#ifndef RTE_WX_H_ +#define RTE_WX_H_ + +extern float rte_wx_temperature_dallas, rte_wx_temperature_dallas_valid; +extern float rte_wx_temperature, rte_wx_temperature_valid; +extern float rte_wx_pressure, rte_wx_pressure_valid; + +extern dht22Values rte_wx_dht, rte_wx_dht_valid; + +extern DallasQF rte_wx_dallas_qf; +extern ms5611_qf_t rte_wx_ms5611_qf; + + +#endif /* RTE_WX_H_ */ diff --git a/src/wx_handler.c b/src/wx_handler.c new file mode 100644 index 0000000..a301f6d --- /dev/null +++ b/src/wx_handler.c @@ -0,0 +1,66 @@ +/* + * wx_handler.c + * + * Created on: 26.01.2019 + * Author: mateusz + */ + +#include "drivers/_dht22.h" +#include "drivers/ms5611.h" +#include "rte_wx.h" + +void wx_get_all_measurements(void) { + + int32_t return_value = 0; + + // quering dallas DS12B20 thermometer for current temperature + rte_wx_temperature_dallas = DallasQuery(&rte_wx_dallas_qf); + + // checking if communication was successfull + if (rte_wx_temperature_dallas != -128.0f) + rte_wx_temperature_dallas_valid = rte_wx_temperature_dallas; + + // quering MS5611 sensor for temperature + return_value = ms5611_get_temperature(&rte_wx_temperature, &rte_wx_ms5611_qf); + + if (return_value == MS5611_OK) { + rte_wx_temperature_valid = rte_wx_temperature; + + } + + // quering MS5611 sensor for pressure + return_value = ms5611_get_pressure(&rte_wx_pressure, &rte_wx_ms5611_qf); + + if (return_value == MS5611_OK) { + rte_wx_pressure_valid = rte_wx_pressure; + } + + // if humidity sensor is idle trigger the communiction & measuremenets + if (dht22State == DHT22_STATE_DONE || dht22State == DHT22_STATE_TIMEOUT) + dht22State = DHT22_STATE_IDLE; + +} + +void wx_pool_dht22(void) { + + dht22_timeout_keeper(); + + switch (dht22State) { + case DHT22_STATE_IDLE: + dht22_comm(&rte_wx_dht); + break; + case DHT22_STATE_DATA_RDY: + dht22_decode(&rte_wx_dht); + break; + case DHT22_STATE_DATA_DECD: + rte_wx_dht_valid = rte_wx_dht; // powrot do stanu DHT22_STATE_IDLE jest w TIM3_IRQHandler + dht22State = DHT22_STATE_DONE; + +#ifdef _DBG_TRACE + trace_printf("DHT22: temperature=%d,humi=%d\r\n", dht_valid.scaledTemperature, dht_valid.humidity); +#endif + break; + default: break; + } + +} diff --git a/src/wx_handler.h b/src/wx_handler.h new file mode 100644 index 0000000..383f2d0 --- /dev/null +++ b/src/wx_handler.h @@ -0,0 +1,14 @@ +/* + * wx_handler.h + * + * Created on: 26.01.2019 + * Author: mateusz + */ + +#ifndef WX_HANDLER_H_ +#define WX_HANDLER_H_ + +void wx_get_all_measurements(void); +void wx_pool_dht22(void); + +#endif /* WX_HANDLER_H_ */ diff --git a/system/include/aprs/afsk.h b/system/include/aprs/afsk.h index 142b611..973f739 100644 --- a/system/include/aprs/afsk.h +++ b/system/include/aprs/afsk.h @@ -149,15 +149,8 @@ typedef struct Afsk */ uint16_t trailer_len; - uint16_t tx_delay_base; - bool tx_delay_randomize; - - -} Afsk; - -extern int16_t afsk_transmit_delay; - +} Afsk; #ifdef __cplusplus extern "C" { @@ -169,13 +162,6 @@ void AFSK_ADC_ISR(Afsk *afsk, int16_t curr_sample); void afsk_txStart(Afsk *af); -void afsk_delay_tx(Afsk *afsk); - -inline void afsk_decrement_delay_counter() { - if (afsk_transmit_delay > 0) - afsk_transmit_delay -= 10; -} - /*********************************************************************************************************************/ uint8_t AFSK_DAC_ISR(Afsk *afsk); /*********************************************************************************************************************/ diff --git a/system/include/drivers/_dht22.h b/system/include/drivers/_dht22.h index fe9002e..bc8b877 100644 --- a/system/include/drivers/_dht22.h +++ b/system/include/drivers/_dht22.h @@ -8,6 +8,8 @@ #ifndef INCLUDE_DRIVERS__DHT22_H_ #define INCLUDE_DRIVERS__DHT22_H_ +#include "stdint.h" + #define DHT22_START_SIG_DURATION 200 #define DHT22_WAITING_FOR_START_RESP_DURATION 12 #define DHT22_LOW_LEVEL_BEFORE_BIT 10 diff --git a/system/include/drivers/dallas.h b/system/include/drivers/dallas.h index e52fc4a..6fc2af8 100644 --- a/system/include/drivers/dallas.h +++ b/system/include/drivers/dallas.h @@ -30,16 +30,25 @@ typedef struct DallasStruct { uint8_t shift; }DallasStruct; +typedef enum DallasQF { + DALLAS_QF_UNKNOWN = 0, + DALLAS_QF_FULL, + DALLAS_QF_DEGRADATED, + DALLAS_QF_NOT_AVALIABLE +}DallasQF; + void DallasInit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint16_t GPIO_PinSource); void DallasConfigTimer(void); void DallasDeConfigTimer(void); char DallasReset(void); -float DallasQuery(void); +float DallasQuery(DallasQF *qf); void DallasSendByte(char data); char DallasReceiveByte(void); uint8_t CalculateCRC8(uint8_t *addr, uint8_t len); + + /* C++ detection */ #ifdef __cplusplus } diff --git a/system/include/drivers/i2c.h b/system/include/drivers/i2c.h index e5df7ed..bfce79e 100644 --- a/system/include/drivers/i2c.h +++ b/system/include/drivers/i2c.h @@ -30,6 +30,7 @@ void i2cKeepTimeout(void); //void I2C1_EV_IRQHandler(void); //void I2C1_ER_IRQHandler(void); +extern volatile i2c_state_t i2c_state; extern volatile uint16_t i2cRemoteAddr; extern volatile uint8_t i2cTXData[32]; diff --git a/system/include/drivers/ms5611.h b/system/include/drivers/ms5611.h index 6d85577..970efca 100644 --- a/system/include/drivers/ms5611.h +++ b/system/include/drivers/ms5611.h @@ -3,22 +3,34 @@ #include +#include "stdint.h" + /* C++ detection */ #ifdef __cplusplus extern "C" { #endif +#define MS5611_WRONG_PARAM_VALUE -3 +#define MS5611_TIMEOUT_DURING_MEASURMENT -2 +#define MS5611_SENSOR_NOT_AVALIABLE -1 +#define MS5611_OK 0 -void SensorReset(int addr); -int SensorReadCalData(int addr, int* cal_data); +typedef enum ms5611_qf { + MS5611_QF_UNKNOWN = 0, + MS5611_QF_FULL = 1, + MS5611_QF_NOT_AVALIABLE = 2 +}ms5611_qf_t; + +int32_t ms5611_reset(ms5611_qf_t *qf); +int32_t ms5611_read_calibration(int32_t* cal_data, ms5611_qf_t* qf); unsigned char crc4(int n_prom[]); -long int SensorStartMeas(int param_to_meas); -float SensorBringTemperature(void); -double SensorBringPressure(void); +int32_t ms5611_trigger_measure(int param_to_meas, int32_t* out); +int32_t ms5611_get_temperature(float* out, ms5611_qf_t* qf); +int32_t ms5611_get_pressure(float* out, ms5611_qf_t *qf); extern char state; -extern int SensorCalData[8]; +extern int32_t SensorCalData[8]; /* C++ detection */ diff --git a/system/src/aprs/afsk.c b/system/src/aprs/afsk.c index 7b8417e..4bc80c7 100644 --- a/system/src/aprs/afsk.c +++ b/system/src/aprs/afsk.c @@ -8,9 +8,6 @@ #include #include -#include "station_config.h" - - #define BIT_DIFFER(bitline1, bitline2) (((bitline1) ^ (bitline2)) & 0x01) #define EDGE_FOUND(bitline) BIT_DIFFER((bitline), (bitline) >> 1) @@ -28,9 +25,7 @@ static int16_t corr_space_q[SAMPLEPERBIT]; /* cos 2200 Hz */ static uint8_t ptr=0; char PersistRand; -char DrawCounter; - -int16_t afsk_transmit_delay = 0; +char DrawCounter; extern unsigned short tx10m; @@ -369,45 +364,7 @@ void AFSK_Init(Afsk *afsk) { } afsk->phase_inc = MARK_INC; - afsk->tx_delay_base = _DELAY_BASE * 50; -#ifdef _RANDOM_DELAY - afsk->tx_delay_randomize = true; -#else - afsk->tx_delay_randomize = false; -#endif - -} - -void afsk_delay_tx(Afsk *afsk) { - afsk_transmit_delay = afsk->tx_delay_base; - - while (afsk_transmit_delay > 0); - - if (afsk->tx_delay_randomize == true) { - - } - else { - return; - } -} - -///*********************************************************************************************************************/ -//char AFSK_MAC(char PersistValue) { -///*********************************************************************************************************************/ -// DrawCounter = 0; // resetowanie licznika losowan -// do { -// /* Petla wykonuje sie do puki wartosc wylosowana jest wieksza niz zadany -// parametr Persist. Aby zapobiec czekaniu w nieskonczonosc, jezeli po 3 sekundach -// warunek dalej jest niespelniony to ramka i tak jest nadawana */ -// if(DrawCounter < 30); -// /* Zmienna DrawCounter jest licznikiem losowan, ktory jednoczesnie przechowuje informacje -// ile czsu uplynelo od inicjacji transmisji. Co kazde losowanie jest ona zwiekszana o -// wartosc Slottime. Poniewaz ustawienie Slottime rowne 1 oznacza losowanie co 100ms to -// 30 bedie oznaczalo 3 sekundy*/ -// else -// break; -// }while(PersistRand > PersistValue); -//} +} diff --git a/system/src/aprs/telemetry.c b/system/src/aprs/telemetry.c index 3c01711..d5751cc 100644 --- a/system/src/aprs/telemetry.c +++ b/system/src/aprs/telemetry.c @@ -26,7 +26,7 @@ void SendSimpleTelemetry(char num) { #ifdef _DALLAS_AS_TELEM char qf = '1', degr = '0', nav = '0'; - temperature = DallasQuery(); +// temperature = DallasQuery(); if (temperature == -128.0f) { qf = '0'; diff --git a/system/src/drivers/dallas.c b/system/src/drivers/dallas.c index 1a41cfa..2073429 100644 --- a/system/src/drivers/dallas.c +++ b/system/src/drivers/dallas.c @@ -155,7 +155,7 @@ char __attribute__((optimize("O0"))) DallasReceiveByte(void) { return data; } -float __attribute__((optimize("O0"))) DallasQuery(void) { +float __attribute__((optimize("O0"))) DallasQuery(DallasQF *qf) { unsigned char data[9]; int crc; char temp1, temp2, sign, i; @@ -193,8 +193,15 @@ float __attribute__((optimize("O0"))) DallasQuery(void) { temperature = -1.0f * (128.0f - (float)temp1 - (float)temp2 * 0.0625f); } else { + *qf = DALLAS_QF_NOT_AVALIABLE; return -128.0f; } + + if (temperature < -25.0f || temperature > 38.75f) + *qf = DALLAS_QF_DEGRADATED; + else + *qf = DALLAS_QF_FULL; + return temperature; } diff --git a/system/src/drivers/i2c.c b/system/src/drivers/i2c.c index 362d89d..e04afc9 100644 --- a/system/src/drivers/i2c.c +++ b/system/src/drivers/i2c.c @@ -67,6 +67,8 @@ void i2cConfigure() { // funkcja konfiguruje pierwszy kontroler i2c!!! I2C1->CR2 |= I2C_CR2_ITBUFEN; I2C1->CR2 |= I2C_CR2_ITERREN; + i2c_state = I2C_IDLE; + } int i2cSendData(int addr, int* data, int null) { @@ -240,6 +242,8 @@ void i2cErrIrqHandler(void) { if (i2cErrorCounter > MAX_I2C_ERRORS_PER_COMM) { i2cStop(); + + i2c_state = I2C_ERROR; } } diff --git a/system/src/drivers/ms5611.c b/system/src/drivers/ms5611.c index 2a0745c..52cf871 100644 --- a/system/src/drivers/ms5611.c +++ b/system/src/drivers/ms5611.c @@ -1,6 +1,7 @@ #include "drivers/ms5611.h" #include "drivers/i2c.h" -#include "drivers/dallas.h" +//#include "drivers/dallas.h" +#include "../src/delay.h" // adres do zapisu: 0xEC // adres do oczytu: 0xED @@ -12,92 +13,105 @@ char state; // zmienna sygnalizuj�ca przebieg pomiaru.. // sekwencja stan�w 0 -> 1 -> 2 -> 1 -> 2 -> 1 (...) -int SensorCalData[8]; +int32_t SensorCalData[8]; double SensorDT = 0.0; +uint8_t sensor_avaliable = 0; + // resetowanie sensora i pobieranie jego danych kalibracyjnych -void SensorReset(int addr) { - DallasConfigTimer(); - delay_5us = 5000; +int32_t ms5611_reset(ms5611_qf_t *qf) { + int txbuf[] = {0x1E, '\0' }; // komenda 0x1E resetuj�ca czujnik - while(i2cTXing != 0 && i2cRXing !=0) { - if (delay_5us == 0) { - DallasDeConfigTimer(); - return; - } - }; + i2cSendData(0xEC, txbuf, 0); // wys�anie danych pod adres 0xEC czyli do czujnika - while(i2cTXing != 0){ - if (delay_5us == 0) { - DallasDeConfigTimer(); - return; - } - }; // czekanie na zako�czenie transmisji -// Delay10ms(); // oczekiwanie 10ms, tyle miej wi�cej trwa resetowanie czujnika -// SensorReadCalData(0xEC, SensorCalData); // odczytywanie danych kalibracyjnych - DallasDeConfigTimer(); - return; + + while (i2c_state != I2C_IDLE && i2c_state != I2C_ERROR); + + // if reset was successfull enable a driver + if (i2c_state == I2C_IDLE) { + sensor_avaliable = 1; + + // wait for sensor reset + delay_fixed(50); + + } + else { + *qf = MS5611_QF_NOT_AVALIABLE; + + return MS5611_SENSOR_NOT_AVALIABLE; + } + + return MS5611_OK; } -int SensorReadCalData(int addr, int* cal_data) { +int32_t ms5611_read_calibration(int32_t* cal_data, ms5611_qf_t *out) { + + if (sensor_avaliable == 0) { + *out = MS5611_QF_NOT_AVALIABLE; + + return MS5611_SENSOR_NOT_AVALIABLE; + } + int i,j; int txbuf[2]; int rxbuf[] = {0x00, 0x00}; j = 0; - DallasConfigTimer(); - delay_5us = 5000; for (i=0; i<=0xE; i+=2) { - while(i2cTXing != 0 && i2cRXing !=0) { - if (delay_5us == 0) { - DallasDeConfigTimer(); - return -2; - } - }; + + // + txbuf[0] = 0xA0 + i; // 0xA0 to adres pierwszej sta�ej kalibracyjnej, ka�da z nich ma 16 bit�w txbuf[1] = '\0'; i2cSendData(0xEC, txbuf, 0); // wysy�anie adresu do odczytania - while(i2cTXing != 0){ - if (delay_5us == 0) { - DallasDeConfigTimer(); - return -2; - } - }; + + while (i2c_state != I2C_IDLE && i2c_state != I2C_ERROR); + + if (i2c_state == I2C_ERROR) { + *out = MS5611_QF_NOT_AVALIABLE; + + return MS5611_TIMEOUT_DURING_MEASURMENT; + } + i2cReceiveData(0xED, rxbuf, 2); // odbi�r danych z czujnika - while(i2cRXing != 0){ - if (delay_5us == 0) { - DallasDeConfigTimer(); - return -2; - } - }; + + while (i2c_state != I2C_IDLE && i2c_state != I2C_ERROR); + + if (i2c_state == I2C_ERROR) { + *out = MS5611_QF_NOT_AVALIABLE; + + return MS5611_TIMEOUT_DURING_MEASURMENT; + } + *(cal_data + j) = ((i2cRXData[0] << 8) | i2cRXData[1]); // przepisywanie danych z bufor�w do tablicy j++; } - DallasDeConfigTimer(); if (crc4(cal_data) == 0x08) // sprawdzanie poprawno�ci odebranych danych return 0; else return -1; } -long int SensorStartMeas(int param_to_meas) { +int32_t ms5611_trigger_measure(int param_to_meas, int32_t* out) { int txbuf[] = { 0x00, 0x00}; long int output; - delay_5us = 5000; - DallasConfigTimer(); + + if (sensor_avaliable == 0) { + return MS5611_SENSOR_NOT_AVALIABLE; + } + if(param_to_meas == 0x00) { //////////////////////////// //// POMIAR TEMPERATURY //// //////////////////////////// txbuf[0] = 0x54; // oversampling 1024 i2cSendData(0xEC,txbuf, 0); // wys�anie rozkazu rozpocz�cia pomiaru - while(i2cTXing != 0){ - if (delay_5us == 0) { - DallasDeConfigTimer(); - return -2; - } - }; - DallasDeConfigTimer(); - return 0; + + while (i2c_state != I2C_IDLE && i2c_state != I2C_ERROR); + + if (i2c_state == I2C_IDLE) + return MS5611_OK; + else + return MS5611_TIMEOUT_DURING_MEASURMENT; } else if(param_to_meas == 0x01) { // pomiar D1 //////////////////////////// @@ -105,34 +119,35 @@ long int SensorStartMeas(int param_to_meas) { //////////////////////////// txbuf[0] = 0x00; i2cSendData(0xEC,txbuf, 0x01); // wys�anie rozkazu odczytu wyniku - while(i2cTXing != 0){ - if (delay_5us == 0) { - DallasDeConfigTimer(); - return -2; - } - }; + + while (i2c_state != I2C_IDLE && i2c_state != I2C_ERROR); + + if (i2c_state == I2C_ERROR) { + return MS5611_TIMEOUT_DURING_MEASURMENT; + } + i2cReceiveData(0xED, txbuf, 3); - while(i2cRXing != 0){ - if (delay_5us == 0) { - DallasDeConfigTimer(); - return -2; - } - }; - output = ((i2cRXData[0] << 16) | (i2cRXData[1] << 8) | i2cRXData[2]); + + while (i2c_state != I2C_IDLE && i2c_state != I2C_ERROR); + + if (i2c_state == I2C_ERROR) { + return MS5611_TIMEOUT_DURING_MEASURMENT; + } + + *out = ((i2cRXData[0] << 16) | (i2cRXData[1] << 8) | i2cRXData[2]); //////////////////////////// //// POMIAR CI�NIENIA //// //////////////////////////// txbuf[0] = 0x44; // oversampling 1024 i2cSendData(0xEC,txbuf, 0); // wys�anie rozkazu rozpocz�cia pomiaru - while(i2cTXing != 0){ - if (delay_5us == 0) { - DallasDeConfigTimer(); - return -2; - } - }; -// Delay10ms(); // oczekiwanie na zako�czenie pomiaru - DallasDeConfigTimer(); - return output; + + while (i2c_state != I2C_IDLE && i2c_state != I2C_ERROR); + + if (i2c_state == I2C_ERROR) { + return MS5611_TIMEOUT_DURING_MEASURMENT; + } + + return MS5611_OK; } else if(param_to_meas == 0x02) { // pomiar D2 ////////////////////////// @@ -140,66 +155,100 @@ long int SensorStartMeas(int param_to_meas) { ////////////////////////// txbuf[0] = 0x00; i2cSendData(0xEC,txbuf, 0x01); // wys�anie rozkazu odczytu wyniku - while(i2cTXing != 0){ - if (delay_5us == 0) { - DallasDeConfigTimer(); - return -2; - } - }; + + while (i2c_state != I2C_IDLE && i2c_state != I2C_ERROR); + + if (i2c_state == I2C_ERROR) { + return MS5611_TIMEOUT_DURING_MEASURMENT; + } + i2cReceiveData(0xED, txbuf, 3); - while(i2cRXing != 0){ - if (delay_5us == 0) { - DallasDeConfigTimer(); - return -2; - } - }; - output = ((i2cRXData[0] << 16) | (i2cRXData[1] << 8) | i2cRXData[2]); + + while (i2c_state != I2C_IDLE && i2c_state != I2C_ERROR); + + if (i2c_state == I2C_ERROR) { + return MS5611_TIMEOUT_DURING_MEASURMENT; + } + + *out = ((i2cRXData[0] << 16) | (i2cRXData[1] << 8) | i2cRXData[2]); //////////////////////////// //// POMIAR TEMPERATURY //// //////////////////////////// txbuf[0] = 0x54; // oversampling 1024 i2cSendData(0xEC,txbuf, 0); // wys�anie rozkazu rozpocz�cia pomiaru - while(i2cTXing != 0){ - if (delay_5us == 0) { - DallasDeConfigTimer(); - return -2; - } - }; -// Delay10ms(); // oczekiwanie na zako�czenie pomiaru - DallasDeConfigTimer(); - return output; + + while (i2c_state != I2C_IDLE && i2c_state != I2C_ERROR); + + if (i2c_state == I2C_ERROR) { + return MS5611_TIMEOUT_DURING_MEASURMENT; + } + + return MS5611_OK; } - DallasDeConfigTimer(); - return -1; + + return MS5611_WRONG_PARAM_VALUE; } // pomiar temperatury -float SensorBringTemperature(void) { +int32_t ms5611_get_temperature(float* out, ms5611_qf_t *qf) { + int32_t raw; + double raw_temp, dt, temp; float output; - raw_temp = SensorStartMeas(0x01); + + if (sensor_avaliable == 0) { + *qf = MS5611_QF_NOT_AVALIABLE; + + return MS5611_SENSOR_NOT_AVALIABLE; + } + + ms5611_trigger_measure(0x01, &raw); + + raw_temp = raw; + dt = raw_temp - SensorCalData[5] * pow(2,8); temp = 2000 + dt * SensorCalData[5] / pow(2,23); output = (float)temp; output /= 100; SensorDT = dt; - return output; + + *qf = MS5611_QF_FULL; + + *out = output; + + return MS5611_OK; } // pomiar ci�nienia -double SensorBringPressure(void) { - long long int raw_p, off, sens, p; +int32_t ms5611_get_pressure(float* out, ms5611_qf_t *qf) { + int32_t raw; + + long long int raw_p, off, sens, p; // int64_t float output_p; - raw_p = SensorStartMeas(0x02); + + if (sensor_avaliable == 0) { + *qf = MS5611_QF_NOT_AVALIABLE; + + return MS5611_SENSOR_NOT_AVALIABLE; + } + + ms5611_trigger_measure(0x02, &raw); + + raw_p = raw; + off = SensorCalData[2] * pow(2,16) + (SensorCalData[4] * SensorDT) / pow(2,7); sens = SensorCalData[1] * pow(2,15) + (SensorCalData[3] * SensorDT) / pow(2,8); p = (raw_p * sens / pow(2,21) - off) / pow(2,15); output_p = (double)p; output_p /= 100; -// output_p += 435; - return output_p; + + *qf = MS5611_QF_FULL; + + *out = output_p; + + return MS5611_OK; } // funkcja obliczaj�ca sum� kontroln� CRC4 z wsp�czynnik�w kalibracyjncyh