From 494064e439cefc913e71e870e9f298a139c73a49 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 7 Feb 2022 10:58:12 +0100 Subject: [PATCH 1/5] Update mhz19.c --- mhz19.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mhz19.c b/mhz19.c index 9c14508..0e01511 100644 --- a/mhz19.c +++ b/mhz19.c @@ -40,11 +40,11 @@ uint16 MHZ19_Read(void) { if (response[0] != 0xFF || response[1] != 0x86) { LREPMaster("MHZ18 Invalid response\r\n"); - HalLedSet(HAL_LED_ALL, HAL_LED_MODE_FLASH); + //HalLedSet(HAL_LED_ALL, HAL_LED_MODE_FLASH); return AIR_QUALITY_INVALID_RESPONSE; } const uint16 ppm = (((uint16)response[2]) << 8) | response[3]; LREP("MHZ18 Received COâ‚‚=%d ppm\r\n", ppm); return ppm; -} \ No newline at end of file +} From de9b05a40b293cc9b50d71116a28c5c325a54d31 Mon Sep 17 00:00:00 2001 From: Utyf Date: Sat, 12 Feb 2022 10:13:41 +0300 Subject: [PATCH 2/5] Allow debug to UART1 P1_6 - TX, P1_7 - RX To configure, add to preinclude.h: #define HAL_UART_DMA 2 #define UART_PORT HAL_UART_PORT_1 --- Debug.c | 2 ++ Debug.h | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Debug.c b/Debug.c index 5977488..6b9d263 100755 --- a/Debug.c +++ b/Debug.c @@ -14,7 +14,9 @@ void vprint(const char *fmt, va_list argp) { } #ifdef DO_DEBUG_UART +#ifndef UART_PORT #define UART_PORT HAL_UART_PORT_0 +#endif bool DebugInit() { halUARTCfg_t halUARTConfig; halUARTConfig.configured = TRUE; diff --git a/Debug.h b/Debug.h index c9ed0a9..c7da137 100755 --- a/Debug.h +++ b/Debug.h @@ -31,7 +31,6 @@ (byte & 0x02 ? '1' : '0'), \ (byte & 0x01 ? '1' : '0') -extern halUARTCfg_t halUARTConfig; void vprint(const char *fmt, va_list argp); extern bool DebugInit(void); From acc26b48125564f8a9545dab725a08beb3183f53 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sat, 12 Mar 2022 23:15:35 +0100 Subject: [PATCH 3/5] Changed: debug on p1.6 / p1.7 Changed: airquality functions / approach Updated: ds18b20 lib --- Debug.c | 8 +++++--- Debug.h | 1 - README.md | 6 +++++- air_quality.h | 2 +- bettery.c => battery.c | 0 ds18b20.c | 18 ++++++++++++++---- mhz19.c | 7 +++---- senseair.c | 10 +++++----- 8 files changed, 33 insertions(+), 19 deletions(-) rename bettery.c => battery.c (100%) mode change 100755 => 100644 diff --git a/Debug.c b/Debug.c index 5977488..6d8d66e 100755 --- a/Debug.c +++ b/Debug.c @@ -14,7 +14,9 @@ void vprint(const char *fmt, va_list argp) { } #ifdef DO_DEBUG_UART -#define UART_PORT HAL_UART_PORT_0 +#ifndef DEBUG_UART_PORT +#define DEBUG_UART_PORT HAL_UART_PORT_0 +#endif bool DebugInit() { halUARTCfg_t halUARTConfig; halUARTConfig.configured = TRUE; @@ -28,7 +30,7 @@ bool DebugInit() { halUARTConfig.intEnable = TRUE; halUARTConfig.callBackFunc = NULL; HalUARTInit(); - if (HalUARTOpen(UART_PORT, &halUARTConfig) == HAL_UART_SUCCESS) { + if (HalUARTOpen(DEBUG_UART_PORT, &halUARTConfig) == HAL_UART_SUCCESS) { LREPMaster("Initialized debug module \r\n"); return true; } @@ -39,7 +41,7 @@ void LREPMaster(uint8 *data) { if (data == NULL) { return; } - HalUARTWrite(UART_PORT, data, osal_strlen((char *)data)); + HalUARTWrite(DEBUG_UART_PORT, data, osal_strlen((char *)data)); } void LREP(char *format, ...) { diff --git a/Debug.h b/Debug.h index c9ed0a9..c7da137 100755 --- a/Debug.h +++ b/Debug.h @@ -31,7 +31,6 @@ (byte & 0x02 ? '1' : '0'), \ (byte & 0x01 ? '1' : '0') -extern halUARTCfg_t halUARTConfig; void vprint(const char *fmt, va_list argp); extern bool DebugInit(void); diff --git a/README.md b/README.md index dfc2932..364ade1 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# Collection of reusable componets for Z-stack 3.0.2 \ No newline at end of file +# Collection of reusable componets for Z-stack 3.0.2 + + +# How to compile +Follow this article https://zigdevwiki.github.io/Begin/IAR_install/ diff --git a/air_quality.h b/air_quality.h index a1a0113..834859d 100644 --- a/air_quality.h +++ b/air_quality.h @@ -4,7 +4,7 @@ #define AIR_QUALITY_INVALID_RESPONSE 0xFFFF typedef void (*request_measure_t)(void); -typedef uint16 (*read_t)(void); +typedef uint16 (*read_t)(uint8 *); typedef void (*set_ABC_t)(bool isEnabled); typedef struct { diff --git a/bettery.c b/battery.c old mode 100755 new mode 100644 similarity index 100% rename from bettery.c rename to battery.c diff --git a/ds18b20.c b/ds18b20.c index 1c45d55..ea39d93 100755 --- a/ds18b20.c +++ b/ds18b20.c @@ -36,7 +36,17 @@ static void ds18b20_setResolution(uint8 resolution); static int16 ds18b20_convertTemperature(uint8 temp1, uint8 temp2, uint8 resolution); static void _delay_us(uint16 microSecs) { - MicroWait(microSecs); + while (microSecs--) + { + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + asm("NOP"); + } } static void _delay_ms(uint16 milliSecs) { @@ -102,16 +112,16 @@ static uint8 ds18b20_read_byte(void) { // Sends reset pulse static uint8 ds18b20_Reset(void) { - TSENS_SBIT = 0; TSENS_DIR |= TSENS_BV; // output - _delay_us(600); + TSENS_SBIT = 0; + _delay_us(500); TSENS_DIR &= ~TSENS_BV; // input _delay_us(70); uint8 i = TSENS_SBIT; _delay_us(200); TSENS_SBIT = 1; TSENS_DIR |= TSENS_BV; // output - _delay_us(600); + _delay_us(500); return i; } diff --git a/mhz19.c b/mhz19.c index 9c14508..c726916 100644 --- a/mhz19.c +++ b/mhz19.c @@ -13,7 +13,7 @@ static void MHZ19_SetABC(bool isEnabled); static void MHZ19_RequestMeasure(void); -static uint16 MHZ19_Read(void); +static uint16 MHZ19_Read(uint8 *response); zclAirSensor_t MHZ19_dev = {&MHZ19_RequestMeasure, &MHZ19_Read, &MHZ19_SetABC}; @@ -34,13 +34,12 @@ void MHZ19_RequestMeasure(void) { HalUARTWrite(CO2_UART_PORT, MHZ19_COMMAND_GET_PPM, sizeof(MHZ19_COMMAND_GET_PPM) / sizeof(MHZ19_COMMAND_GET_PPM[0])); } -uint16 MHZ19_Read(void) { - uint8 response[MHZ18_RESPONSE_LENGTH]; +uint16 MHZ19_Read(uint8 *response) { + HalUARTRead(CO2_UART_PORT, (uint8 *)&response, sizeof(response) / sizeof(response[0])); if (response[0] != 0xFF || response[1] != 0x86) { LREPMaster("MHZ18 Invalid response\r\n"); - HalLedSet(HAL_LED_ALL, HAL_LED_MODE_FLASH); return AIR_QUALITY_INVALID_RESPONSE; } diff --git a/senseair.c b/senseair.c index 5bda0e2..f4cd88c 100644 --- a/senseair.c +++ b/senseair.c @@ -6,11 +6,11 @@ #include "hal_uart.h" #ifndef CO2_UART_PORT -#define CO2_UART_PORT HAL_UART_PORT_1 +#define CO2_UART_PORT HAL_UART_PORT_0 #endif static void SenseAir_RequestMeasure(void); -static uint16 SenseAir_Read(void); +static uint16 SenseAir_Read(uint8 *); static void SenseAir_SetABC(bool isEnabled); extern zclAirSensor_t sense_air_dev = {&SenseAir_RequestMeasure, &SenseAir_Read, &SenseAir_SetABC}; @@ -33,9 +33,9 @@ void SenseAir_RequestMeasure(void) { HalUARTWrite(CO2_UART_PORT, readCO2, sizeof(readCO2) / sizeof(readCO2[0])); } -uint16 SenseAir_Read(void) { - uint8 response[SENSEAIR_RESPONSE_LENGTH]; - HalUARTRead(CO2_UART_PORT, (uint8 *)&response, sizeof(response) / sizeof(response[0])); +uint16 SenseAir_Read(uint8 *response) { + + LREPMaster("Read startet \r\n"); if (response[0] != 0xFE || response[1] != 0x04) { LREPMaster("Invalid response\r\n"); From 4b88e9162ff1392808fb518fb452fb5ddcdc9ade Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 21 Mar 2022 19:36:31 +0100 Subject: [PATCH 4/5] Changed: Debbuging port to p1.6/p.1.7 Changed: New way to get air sensor response --- Debug.c | 2 +- air_quality.h | 1 + mhz19.c | 6 ++---- senseair.c | 21 ++++++++++++++++----- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Debug.c b/Debug.c index 6d8d66e..e9f4c11 100755 --- a/Debug.c +++ b/Debug.c @@ -15,7 +15,7 @@ void vprint(const char *fmt, va_list argp) { #ifdef DO_DEBUG_UART #ifndef DEBUG_UART_PORT -#define DEBUG_UART_PORT HAL_UART_PORT_0 +#define DEBUG_UART_PORT HAL_UART_PORT_1 #endif bool DebugInit() { halUARTCfg_t halUARTConfig; diff --git a/air_quality.h b/air_quality.h index 834859d..73f51b4 100644 --- a/air_quality.h +++ b/air_quality.h @@ -2,6 +2,7 @@ #define AIR_QUALITY_H #define AIR_QUALITY_INVALID_RESPONSE 0xFFFF +#define AIR_QUALITY_ABC_RESPONSE 0xFFF0 typedef void (*request_measure_t)(void); typedef uint16 (*read_t)(uint8 *); diff --git a/mhz19.c b/mhz19.c index c726916..0cb9b64 100644 --- a/mhz19.c +++ b/mhz19.c @@ -6,7 +6,7 @@ #include "hal_uart.h" #ifndef CO2_UART_PORT -#define CO2_UART_PORT HAL_UART_PORT_1 +#define CO2_UART_PORT HAL_UART_PORT_0 #endif #define MHZ18_RESPONSE_LENGTH 13 @@ -35,8 +35,6 @@ void MHZ19_RequestMeasure(void) { } uint16 MHZ19_Read(uint8 *response) { - - HalUARTRead(CO2_UART_PORT, (uint8 *)&response, sizeof(response) / sizeof(response[0])); if (response[0] != 0xFF || response[1] != 0x86) { LREPMaster("MHZ18 Invalid response\r\n"); @@ -44,6 +42,6 @@ uint16 MHZ19_Read(uint8 *response) { } const uint16 ppm = (((uint16)response[2]) << 8) | response[3]; - LREP("MHZ18 Received COâ‚‚=%d ppm\r\n", ppm); + //LREP("MHZ18 Received COâ‚‚=%d ppm\r\n", ppm); return ppm; } \ No newline at end of file diff --git a/senseair.c b/senseair.c index f4cd88c..ea85557 100644 --- a/senseair.c +++ b/senseair.c @@ -36,16 +36,27 @@ void SenseAir_RequestMeasure(void) { uint16 SenseAir_Read(uint8 *response) { LREPMaster("Read startet \r\n"); - + + uint8 is_equal = 1; + for (uint8 i=0; i<5; ++i) { + if (response[i] != enableABC[i]) { + is_equal = 0; + break; + } + } + + if (is_equal) + return AIR_QUALITY_ABC_RESPONSE; + if (response[0] != 0xFE || response[1] != 0x04) { LREPMaster("Invalid response\r\n"); return AIR_QUALITY_INVALID_RESPONSE; } const uint8 length = response[2]; - const uint16 status = (((uint16)response[3]) << 8) | response[4]; - const uint16 ppm = (((uint16)response[length + 1]) << 8) | response[length + 2]; - - LREP("SenseAir Received COâ‚‚=%d ppm Status=0x%X\r\n", ppm, status); + //const uint16 status = (((uint16)response[3]) << 8) | response[4]; + uint16 ppm = (((uint16)response[length + 1]) << 8) | response[length + 2]; + //LREP("SenseAir Received CO2=%d ppm Status=0x%X\r\n", ppm, status); + //printf("SenseAir Received CO2=%d ppm Status=0x%X\r\n", ppm, status); return ppm; } \ No newline at end of file From 6ae6c2613436a28e6b0c58dee217b24d8d30180c Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 25 Apr 2022 09:31:16 +0200 Subject: [PATCH 5/5] added us100 --- Debug.c | 8 ++++---- air_quality.h | 2 +- mhz19.c | 6 +++--- senseair.c | 10 +++++----- us100.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ us100.h | 16 ++++++++++++++++ 6 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 us100.c create mode 100644 us100.h diff --git a/Debug.c b/Debug.c index 6b9d263..6d8d66e 100755 --- a/Debug.c +++ b/Debug.c @@ -14,8 +14,8 @@ void vprint(const char *fmt, va_list argp) { } #ifdef DO_DEBUG_UART -#ifndef UART_PORT -#define UART_PORT HAL_UART_PORT_0 +#ifndef DEBUG_UART_PORT +#define DEBUG_UART_PORT HAL_UART_PORT_0 #endif bool DebugInit() { halUARTCfg_t halUARTConfig; @@ -30,7 +30,7 @@ bool DebugInit() { halUARTConfig.intEnable = TRUE; halUARTConfig.callBackFunc = NULL; HalUARTInit(); - if (HalUARTOpen(UART_PORT, &halUARTConfig) == HAL_UART_SUCCESS) { + if (HalUARTOpen(DEBUG_UART_PORT, &halUARTConfig) == HAL_UART_SUCCESS) { LREPMaster("Initialized debug module \r\n"); return true; } @@ -41,7 +41,7 @@ void LREPMaster(uint8 *data) { if (data == NULL) { return; } - HalUARTWrite(UART_PORT, data, osal_strlen((char *)data)); + HalUARTWrite(DEBUG_UART_PORT, data, osal_strlen((char *)data)); } void LREP(char *format, ...) { diff --git a/air_quality.h b/air_quality.h index a1a0113..834859d 100644 --- a/air_quality.h +++ b/air_quality.h @@ -4,7 +4,7 @@ #define AIR_QUALITY_INVALID_RESPONSE 0xFFFF typedef void (*request_measure_t)(void); -typedef uint16 (*read_t)(void); +typedef uint16 (*read_t)(uint8 *); typedef void (*set_ABC_t)(bool isEnabled); typedef struct { diff --git a/mhz19.c b/mhz19.c index 9c14508..9baa2fc 100644 --- a/mhz19.c +++ b/mhz19.c @@ -13,7 +13,7 @@ static void MHZ19_SetABC(bool isEnabled); static void MHZ19_RequestMeasure(void); -static uint16 MHZ19_Read(void); +static uint16 MHZ19_Read(uint8 *response); zclAirSensor_t MHZ19_dev = {&MHZ19_RequestMeasure, &MHZ19_Read, &MHZ19_SetABC}; @@ -34,8 +34,8 @@ void MHZ19_RequestMeasure(void) { HalUARTWrite(CO2_UART_PORT, MHZ19_COMMAND_GET_PPM, sizeof(MHZ19_COMMAND_GET_PPM) / sizeof(MHZ19_COMMAND_GET_PPM[0])); } -uint16 MHZ19_Read(void) { - uint8 response[MHZ18_RESPONSE_LENGTH]; +uint16 MHZ19_Read(uint8 *response) { + HalUARTRead(CO2_UART_PORT, (uint8 *)&response, sizeof(response) / sizeof(response[0])); if (response[0] != 0xFF || response[1] != 0x86) { diff --git a/senseair.c b/senseair.c index 5bda0e2..f4cd88c 100644 --- a/senseair.c +++ b/senseair.c @@ -6,11 +6,11 @@ #include "hal_uart.h" #ifndef CO2_UART_PORT -#define CO2_UART_PORT HAL_UART_PORT_1 +#define CO2_UART_PORT HAL_UART_PORT_0 #endif static void SenseAir_RequestMeasure(void); -static uint16 SenseAir_Read(void); +static uint16 SenseAir_Read(uint8 *); static void SenseAir_SetABC(bool isEnabled); extern zclAirSensor_t sense_air_dev = {&SenseAir_RequestMeasure, &SenseAir_Read, &SenseAir_SetABC}; @@ -33,9 +33,9 @@ void SenseAir_RequestMeasure(void) { HalUARTWrite(CO2_UART_PORT, readCO2, sizeof(readCO2) / sizeof(readCO2[0])); } -uint16 SenseAir_Read(void) { - uint8 response[SENSEAIR_RESPONSE_LENGTH]; - HalUARTRead(CO2_UART_PORT, (uint8 *)&response, sizeof(response) / sizeof(response[0])); +uint16 SenseAir_Read(uint8 *response) { + + LREPMaster("Read startet \r\n"); if (response[0] != 0xFE || response[1] != 0x04) { LREPMaster("Invalid response\r\n"); diff --git a/us100.c b/us100.c new file mode 100644 index 0000000..d940f50 --- /dev/null +++ b/us100.c @@ -0,0 +1,48 @@ +#include "Debug.h" +#include "us100.h" +#include "OnBoard.h" + +#ifndef US100_UART_PORT +#define US100_UART_PORT HAL_UART_PORT_0 +#endif + + +uint8 readDistance[] = {0x55}; +uint8 readTemp[] = {0x50}; + +void US100_RequestMeasureDistance(void) { + current_phase = DISTANCE_MEASURMENT; + HalUARTWrite(US100_UART_PORT, readDistance, sizeof(readDistance) / sizeof(readDistance[0])); +} +void US100_RequestMeasureTemp(void) { + current_phase = TEMPERATURE_MEASURMENT; + HalUARTWrite(US100_UART_PORT, readTemp, sizeof(readTemp) / sizeof(readTemp[0])); +} + +uint16 US100_ReadDistance(void) +{ + + uint8 response[2]; + HalUARTRead(US100_UART_PORT, (uint8 *)&response, sizeof(response) / sizeof(response[0])); + uint16 mmDist = response[0] * 256 + response[1]; + if((mmDist > 1) && (mmDist < 10000)) + { + printf("Distance=%d\r\n", mmDist); + LREP("Distance=%d \r\n", 0); + return mmDist; + } + return 0; +} + +uint16 US100_ReadTemp(void){ + uint8 response[1]; + HalUARTRead(US100_UART_PORT, (uint8 *)&response, sizeof(response) / sizeof(response[0])); + + uint16 temp = response[0]; + if((temp > 1) && (temp < 130)) // temprature is in range + { + temp -= 45; // correct 45º offset + printf("Temperature=%d\r\n", temp); + } +return temp; +} \ No newline at end of file diff --git a/us100.h b/us100.h new file mode 100644 index 0000000..1297fdf --- /dev/null +++ b/us100.h @@ -0,0 +1,16 @@ +#ifndef us100_h +void US100_RequestMeasureDistance(void); +void US100_RequestMeasureTemp(void); +uint16 US100_ReadDistance(void); +uint16 US100_ReadTemp(void); + +enum { + DISTANCE_MEASURMENT = 0, + TEMPERATURE_MEASURMENT = 1, + NOTHING = 2 +}; + +extern uint8 current_phase; + +#define us100_h +#endif