增加风速风向检测 main master
authorkyz <kongyuzhong@google.com>
Thu, 17 Jul 2025 08:25:09 +0000 (16:25 +0800)
committerkyz <kongyuzhong@google.com>
Thu, 17 Jul 2025 08:30:32 +0000 (16:30 +0800)
MDK-ARM/mcu.uvprojx
user/user.h
user/user_adc.c
user/user_app.c
user/user_filter.c
user/user_location.c
user/user_noise_sensor.c
user/user_tcp.c
user/user_temperture.c [deleted file]
user/user_wind_sensor.c [new file with mode: 0644]
user_debug/test_main.c

index 633adf6b3d654dd72484f9447fc3c4aa699d1871..199fd442575337020255c9d11493b4c0af88bc16 100644 (file)
               <FileType>1</FileType>
               <FilePath>..\user\user_filter.c</FilePath>
             </File>
               <FileType>1</FileType>
               <FilePath>..\user\user_filter.c</FilePath>
             </File>
-            <File>
-              <FileName>user_temperture.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\user\user_temperture.c</FilePath>
-            </File>
             <File>
               <FileName>user_lora.c</FileName>
               <FileType>1</FileType>
             <File>
               <FileName>user_lora.c</FileName>
               <FileType>1</FileType>
               <FileType>1</FileType>
               <FilePath>..\user\user_alarm_sound.c</FilePath>
             </File>
               <FileType>1</FileType>
               <FilePath>..\user\user_alarm_sound.c</FilePath>
             </File>
+            <File>
+              <FileName>user_wind_sensor.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\user\user_wind_sensor.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>
           </Files>
         </Group>
         <Group>
index 469191bdf9e9a2822ab8352007d4e7ce2bf5a7d2..a3843a68876c1a98f050692a37c282a9309099a3 100644 (file)
@@ -7,6 +7,9 @@
 #define SW_VER_MAJOR  1
 #define SW_VER_MINOR  0
 
 #define SW_VER_MAJOR  1
 #define SW_VER_MINOR  0
 
+#define USER_SHELL_USE_UART1    0
+#define WIND_SENSOR_USE_UART1   1
+
 typedef osMutexId_t user_lock_t;
 
 
 typedef osMutexId_t user_lock_t;
 
 
@@ -21,10 +24,18 @@ typedef struct{
        int8_t state;
        float   db;
 }noise_t;
        int8_t state;
        float   db;
 }noise_t;
+
+typedef struct{
+       int8_t state; //0:normal, 1:strength err 2:direction err
+       float  strength;
+       float  direction;
+}wind_t;
+
 typedef struct{
        int8_t state;
        float freq;
 }sound_t;
 typedef struct{
        int8_t state;
        float freq;
 }sound_t;
+
 typedef struct{
        bd_state_t state;
        float lat;
 typedef struct{
        bd_state_t state;
        float lat;
@@ -35,6 +46,7 @@ typedef struct{
        noise_t noise;
        sound_t sound;
        position_t pos;
        noise_t noise;
        sound_t sound;
        position_t pos;
+       wind_t wind;
 }sensor_info_t;
 
 typedef struct{
 }sensor_info_t;
 
 typedef struct{
@@ -48,6 +60,7 @@ void user_adc_start( void );
 
 //
 void user_noise_info_update(  float db, int8_t state );
 
 //
 void user_noise_info_update(  float db, int8_t state );
+void user_wind_info_update(  float strength, float direction, int8_t state );
 void user_sound_info_update( float freq, int8_t state );
 void user_position_update( float lat, float lon, bd_state_t state );
 void user_sensor_info_read( sensor_info_t *info );
 void user_sound_info_update( float freq, int8_t state );
 void user_position_update( float lat, float lon, bd_state_t state );
 void user_sensor_info_read( sensor_info_t *info );
index 650f244d20b68d2ba1aa279209f2911efa8182b5..18ad0fa3837ecbf2ff36975f9898347033f4a0e9 100644 (file)
@@ -130,9 +130,9 @@ int32_t user_tcp_write_data( uint8_t *data, uint32_t data_sz );
 extern UART_HandleTypeDef huart1;
 extern int g_adc_debug;
 
 extern UART_HandleTypeDef huart1;
 extern int g_adc_debug;
 
-static uint16_t freq_list[1024];
-static uint16_t amp_list[1024];
-static uint32_t num = 0;
+//static uint16_t freq_list[1024];
+//static uint16_t amp_list[1024];
+//static uint32_t num = 0;
 
 extern int alarm_data_add( uint16_t in_freq, uint16_t in_amp );
 extern int DetectAlarm( void );
 
 extern int alarm_data_add( uint16_t in_freq, uint16_t in_amp );
 extern int DetectAlarm( void );
@@ -143,6 +143,7 @@ int alarm_detected = 0;
 int adc_debug( int enable )
 {
        g_adc_debug = enable;
 int adc_debug( int enable )
 {
        g_adc_debug = enable;
+       return 0;
 }
 
 extern void init_fft(void);
 }
 
 extern void init_fft(void);
index 3c34880c8863cc92030c793d566fa7d87ef359ac..140055161c7044850cd2ebfbfe6a98c1958b6c28 100644 (file)
@@ -9,10 +9,12 @@ extern void user_sound_task_start( void );
 extern void user_noise_task_start( void );
 extern void user_bd_task_start( void );
 extern void user_lora_task_start( void );
 extern void user_noise_task_start( void );
 extern void user_bd_task_start( void );
 extern void user_lora_task_start( void );
+extern void user_wind_task_start( void );
 
 extern void bd_rx_cb( void );
 extern void shell_uart_rx_cb( void );
 extern void lora_rx_cb( void );
 
 extern void bd_rx_cb( void );
 extern void shell_uart_rx_cb( void );
 extern void lora_rx_cb( void );
+extern void user_rs485_rx_cb( void );
 
 extern uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len);
 extern uint32_t adc_finished;
 
 extern uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len);
 extern uint32_t adc_finished;
@@ -26,7 +28,11 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
 {
                if( huart->Instance == USART1 ){
                        //USART1 debug shell
 {
                if( huart->Instance == USART1 ){
                        //USART1 debug shell
+#if USER_SHELL_USE_UART1
                        shell_uart_rx_cb();
                        shell_uart_rx_cb();
+#elif WIND_SENSOR_USE_UART1
+                       user_rs485_rx_cb();
+#endif
                }else if( huart->Instance == USART2 ){
                        //USART2 BD Tx/Rx
                        
                }else if( huart->Instance == USART2 ){
                        //USART2 BD Tx/Rx
                        
@@ -47,7 +53,13 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
 
 int fputc( int ch, FILE *fd )
 {
 
 int fputc( int ch, FILE *fd )
 {
+#if USER_SHELL_USE_UART1
+       
+#elif WIND_SENSOR_USE_UART1
+
+#else
        HAL_UART_Transmit( &huart1, (uint8_t*)&ch, 1, 100 );
        HAL_UART_Transmit( &huart1, (uint8_t*)&ch, 1, 100 );
+#endif
        return 0;
 }
 
        return 0;
 }
 
@@ -73,6 +85,15 @@ void user_noise_info_update(  float db, int8_t state )
        user_unlock( g_dev.lock );
 }
 
        user_unlock( g_dev.lock );
 }
 
+void user_wind_info_update(  float strength, float direction, int8_t state )
+{
+       user_lock( g_dev.lock );
+       g_dev.sensor.wind.strength = strength;
+       g_dev.sensor.wind.direction = direction;
+       g_dev.sensor.wind.state = state;
+       user_unlock( g_dev.lock );
+}
+
 void user_sound_info_update( float freq, int8_t state  )
 {
        user_lock( g_dev.lock );
 void user_sound_info_update( float freq, int8_t state  )
 {
        user_lock( g_dev.lock );
@@ -134,7 +155,7 @@ void main_task(void *argument)
        HAL_GPIO_WritePin( PB15_ETH_RESET_GPIO_Port, PB15_ETH_RESET_Pin,  GPIO_PIN_RESET );
        osDelay( 100 );
        HAL_GPIO_WritePin( PB15_ETH_RESET_GPIO_Port, PB15_ETH_RESET_Pin,  GPIO_PIN_SET );
        HAL_GPIO_WritePin( PB15_ETH_RESET_GPIO_Port, PB15_ETH_RESET_Pin,  GPIO_PIN_RESET );
        osDelay( 100 );
        HAL_GPIO_WritePin( PB15_ETH_RESET_GPIO_Port, PB15_ETH_RESET_Pin,  GPIO_PIN_SET );
-       
+
        MX_LWIP_Init();
 
        Tcp_Server_Init();
        MX_LWIP_Init();
 
        Tcp_Server_Init();
@@ -144,6 +165,7 @@ void main_task(void *argument)
        user_sound_task_start();
        user_lora_task_start();
        user_bd_task_start();
        user_sound_task_start();
        user_lora_task_start();
        user_bd_task_start();
+       user_wind_task_start();
 
        delay_ticks = xTaskGetTickCount();
        for(;;)
 
        delay_ticks = xTaskGetTickCount();
        for(;;)
index 231c537955855b658968fe40b6b2e4df948023d2..0ee90fa8530415fd6290b0aeead831f11c2dd6c2 100644 (file)
@@ -7,8 +7,8 @@
 #define BLOCK_SIZE           1         /* µ÷ÓÃÒ»´Îarm_fir_f32´¦ÀíµÄ²ÉÑùµã¸öÊý */
 #define NUM_TAPS            21      /* Â˲¨Æ÷ϵÊý¸öÊý */
 
 #define BLOCK_SIZE           1         /* µ÷ÓÃÒ»´Îarm_fir_f32´¦ÀíµÄ²ÉÑùµã¸öÊý */
 #define NUM_TAPS            21      /* Â˲¨Æ÷ϵÊý¸öÊý */
 
-static float32_t testInput[TEST_LENGTH_SAMPLES]; /* ²ÉÑùµã */
-static float32_t testOutput[TEST_LENGTH_SAMPLES];               /* Â˲¨ºóµÄÊä³ö */
+//static float32_t testInput[TEST_LENGTH_SAMPLES]; /* ²ÉÑùµã */
+//static float32_t testOutput[TEST_LENGTH_SAMPLES];               /* Â˲¨ºóµÄÊä³ö */
 static float32_t firStateF32[BLOCK_SIZE + NUM_TAPS - 1];        /* ×´Ì¬»º´æ£¬´óСnumTaps + blockSize - 1*/
 
 
 static float32_t firStateF32[BLOCK_SIZE + NUM_TAPS - 1];        /* ×´Ì¬»º´æ£¬´óСnumTaps + blockSize - 1*/
 
 
@@ -33,11 +33,11 @@ void user_data_lp( uint16_t *in, uint16_t *out, int len )
 {
     uint32_t i;
     arm_fir_instance_f32 S;
 {
     uint32_t i;
     arm_fir_instance_f32 S;
-    float32_t  *inputF32, *outputF32;
+//    float32_t  *inputF32, *outputF32;
 
     /* ³õʼ»¯ÊäÈëÊä³ö»º´æÖ¸Õë */
 
     /* ³õʼ»¯ÊäÈëÊä³ö»º´æÖ¸Õë */
-    inputF32 = &testInput[0];
-    outputF32 = &testOutput[0];
+//    inputF32 = &testInput[0];
+//    outputF32 = &testOutput[0];
 
     /* ³õʼ»¯½á¹¹ÌåS */
                arm_fir_init_f32(&S,                            
 
     /* ³õʼ»¯½á¹¹ÌåS */
                arm_fir_init_f32(&S,                            
index 8bc29aac20df06efc634410c32d3dea2b8542a96..07785f5caeeb2352855a2203fdd505befcceb76a 100644 (file)
@@ -36,6 +36,7 @@ int bd_log( int on )
 {
        bd_log_flag = on;
        printf( "bd log out %s\n", on?"enable":"disable" );
 {
        bd_log_flag = on;
        printf( "bd log out %s\n", on?"enable":"disable" );
+       return 0;
 }
 
 void bd_parse( void )
 }
 
 void bd_parse( void )
index 83b480b6b03e57414bf6d3483df2cdbc1e012bef..ad029a449cb2b123b54d814c7ea1f3ac3abebdc2 100644 (file)
@@ -2,9 +2,6 @@
 #include <main.h>
 #include "user.h"
 
 #include <main.h>
 #include "user.h"
 
-extern void user_noise_update(  float noise, int8_t state );
-
-static float noise_value = 0;
 static int8_t noise_state = 0;
 
 static uint8_t recv_byte;
 static int8_t noise_state = 0;
 
 static uint8_t recv_byte;
index d7d46e768318453fd753f338ff9f007fd3b118f4..7c69d2914bae7a8abec83dfbed81102b6ae753d4 100644 (file)
@@ -48,7 +48,7 @@ int32_t user_tcp_write_data_sync( uint8_t *data, uint32_t data_sz )
        if( tcp_connected_pcb ){
 
                for( int i=0; i<5; i++ ){
        if( tcp_connected_pcb ){
 
                for( int i=0; i<5; i++ ){
-                       err = tcp_write( tcp_connected_pcb, data, data_sz, 0); //TCP_WRITE_FLAG_COPY
+                       err = (err_enum_t)tcp_write( tcp_connected_pcb, data, data_sz, 0); //TCP_WRITE_FLAG_COPY
                        if( err == ERR_OK ){
                                ret = 0;
                                tcp_output( tcp_connected_pcb );
                        if( err == ERR_OK ){
                                ret = 0;
                                tcp_output( tcp_connected_pcb );
@@ -114,7 +114,7 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len );
     55AA 0002  0001 0008  XX...  XXXX
 CMD:
    0001: query    LEN 0   query period:5s
     55AA 0002  0001 0008  XX...  XXXX
 CMD:
    0001: query    LEN 0   query period:5s
-   0002: resp     LEN 8 , DATA: noise + alarm
+   0002: resp     LEN 32 , DATA: noise + alarm + pos + wind
    1001: Lora Tx
    1002: Lora Rx
    2001: BD   Tx
    1001: Lora Tx
    1002: Lora Rx
    2001: BD   Tx
@@ -201,7 +201,10 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len )
                                        resp_data[10] = sensor.pos.state;
                                        memcpy( &resp_data[11], &sensor.pos.lat, 4 );
                                        memcpy( &resp_data[15], &sensor.pos.lon, 4 );
                                        resp_data[10] = sensor.pos.state;
                                        memcpy( &resp_data[11], &sensor.pos.lat, 4 );
                                        memcpy( &resp_data[15], &sensor.pos.lon, 4 );
-                                       memset( &resp_data[19], 0, 13 );
+                                       resp_data[19] = sensor.wind.state;
+                                       memcpy( &resp_data[20], &sensor.wind.strength, 4 );
+                                       memcpy( &resp_data[24], &sensor.wind.direction, 4 );
+                                       memset( &resp_data[28], 0, 4 );
                                        resp_sz = wrap_pkt( resp, rx_pkt_sn, 0x0101, resp_data, 32 );
                                        break;
                                case 0x0003: //lora transmit
                                        resp_sz = wrap_pkt( resp, rx_pkt_sn, 0x0101, resp_data, 32 );
                                        break;
                                case 0x0003: //lora transmit
diff --git a/user/user_temperture.c b/user/user_temperture.c
deleted file mode 100644 (file)
index 49b6e31..0000000
+++ /dev/null
@@ -1,125 +0,0 @@
-
-#include "user.h"
-
-#if 0
-
-/*
-ÅäÖÃTIM3µÄCOUNTER À´×ö¼ÆÊ± µ¥Î»us¡£
-GPIO PE3 ÅäÖÃΪÖжϴ¥·¢£¬ÖжϺ¯ÊýÖмǼ¸ßµçƽ±£³Öʱ¼ä
-
-¶ÁȡָÁ ·¢ËÍÆô¶¯Ö¸ÁÆô¶¯TIM3 Æô¶¯IOÖжϣ¬ÖжÏÖжÁÈ¡TIM3µÄcounter¼Ä´æÆ÷£¬¼Ç¼¸ßµçƽ±£³Öʱ¼ä
-¶ÁȡָÁîºóÑÓ³Ù10ms £¬¼ì²é¼Ç¼Êý¾Ý£¬½âÎöζÈÄ£¿éµÄ»ØÓ¦±¨ÎÄÀ´»ñÈ¡ÎÂʪ¶ÈÊý¾Ý
-*/
-
-extern TIM_HandleTypeDef htim3;
-
-int user_temperature_read( float *tem, float * hum );
-
-static void temp_read_start( void );
-static void temp_read_stop( void );
-
-static float s_tem;
-static float s_hum;
-
-struct tem_bit{
-       uint8_t h_us;
-};
-
-struct tem_bit tem_bit_data[ 50 ]; //ack + bit x 40 + end
-int tem_bit_count;
-
-
-void tem_int_cb( void )
-{
-       int us;
-       int level;
-
-       level = HAL_GPIO_ReadPin( DQ_IN_GPIO_Port, DQ_IN_Pin )==GPIO_PIN_SET?1:0;
-       us    = __HAL_TIM_GET_COUNTER( &htim3 );
-
-       if( tem_bit_count > 42 )
-               return;
-
-       if( level==0 ){
-               tem_bit_data[tem_bit_count].h_us = us;
-       }else{
-               tem_bit_count++;
-       }
-
-       __HAL_TIM_SET_COUNTER( &htim3, 0 );
-}
-       
-void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
-{
-       if( GPIO_Pin==DQ_IN_Pin ){
-               tem_int_cb();
-       }
-}
-
-void tem_pin_cfg_out( int level )
-{
-  GPIO_InitTypeDef GPIO_InitStruct = {0};
-
-  HAL_NVIC_DisableIRQ(EXTI3_IRQn);
-  HAL_GPIO_WritePin(SYS_LED_GPIO_Port, SYS_LED_Pin, GPIO_PIN_RESET);
-
-  /*Configure GPIO pin : DQ_IN_Pin */
-  GPIO_InitStruct.Pin = DQ_IN_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  HAL_GPIO_Init(DQ_IN_GPIO_Port, &GPIO_InitStruct);
-
-  
-}
-
-void tem_pin_cfg_input( void )
-{
-  GPIO_InitTypeDef GPIO_InitStruct = {0};
-
-  /*Configure GPIO pin : DQ_IN_Pin */
-  GPIO_InitStruct.Pin = DQ_IN_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  HAL_GPIO_Init(DQ_IN_GPIO_Port, &GPIO_InitStruct);
-  __HAL_GPIO_EXTI_CLEAR_IT(DQ_IN_Pin);
-
-}
-
-void temp_read_start( void )
-{
-       tem_pin_cfg_out( 0 );
-       osDelay(20);
-       tem_pin_cfg_input();
-
-       tem_bit_count = 0;
-       __HAL_TIM_SET_COUNTER( &htim3, 0 );
-       HAL_TIM_Base_Start( &htim3 );
-
-       HAL_NVIC_SetPriority(EXTI3_IRQn, 5, 0);
-       HAL_NVIC_EnableIRQ(EXTI3_IRQn);
-}
-
-void temp_read_stop( void )
-{
-       HAL_NVIC_DisableIRQ(EXTI3_IRQn);
-       HAL_TIM_Base_Stop( &htim3 );
-       __HAL_TIM_SET_COUNTER( &htim3, 0 );
-       
-}
-
-int user_temperature_read( float *tem, float * hum )
-{
-       temp_read_start();
-       osDelay(10*100);
-       temp_read_stop();
-
-       if( tem_bit_count > 42 ){
-
-               *tem = s_tem;
-               *hum = s_hum;
-               return 0;
-       }       
-       return -1;
-}
-
-#endif
diff --git a/user/user_wind_sensor.c b/user/user_wind_sensor.c
new file mode 100644 (file)
index 0000000..c487fdb
--- /dev/null
@@ -0,0 +1,219 @@
+
+#include "user.h"
+
+extern UART_HandleTypeDef huart1;
+
+#define WIND_STRENGTH_ADDR   1
+#define WIND_DIRECTION_ADDR  2
+#define WIND_SENSOR_BAUD     9600
+
+/******************************************************************************************
+·çËÙ/·çÏò´«¸ÐÆ÷ĬÈϲÎÊý µØÖ·1 ²¨ÌØÂÊ 4800
+
+д«¸ÐÆ÷ͨ¹ýPC¹¤¾ßÅäÖòÎÊý
+1. ·çËÙ´«¸ÐÆ÷  µØÖ· 1  ²¨ÌØÂÊ 9600
+2. ·çÏò´«¸ÐÆ÷  µØÖ· 2  ²¨ÌØÂÊ 9600
+
+·çËÙ´«¸ÐÆ÷  ¼Ä´æÆ÷0  µ¥Î» 0.1 m/s
+·çÏò´«¸ÐÆ÷  ¼Ä´æÆ÷0  µ¥Î» 0.1 ¶È
+******************************************************************************************/
+
+static void wind_sensor_uart_init( void )
+{
+  huart1.Instance = USART1;
+  huart1.Init.BaudRate = WIND_SENSOR_BAUD;
+  huart1.Init.WordLength = UART_WORDLENGTH_8B;
+  huart1.Init.StopBits = UART_STOPBITS_1;
+  huart1.Init.Parity = UART_PARITY_NONE;
+  huart1.Init.Mode = UART_MODE_TX_RX;
+  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
+  if (HAL_UART_Init(&huart1) != HAL_OK)
+  {
+    Error_Handler();
+  }
+
+}
+
+static int32_t rs485_rx_flag = 0;
+void user_rs485_rx_cb( void )
+{
+    rs485_rx_flag = 1;
+}
+
+int32_t rs485_tx( uint8_t *data, uint16_t sz )
+{
+    uint8_t dummy;
+
+    while( HAL_UART_Receive( &huart1, &dummy, 1, 0 ) == HAL_OK){
+    };
+        
+    HAL_UART_Transmit( &huart1, data, sz, 100 );
+
+    return 0;
+}
+
+int32_t rs485_rx( uint8_t *rx_data, uint8_t rx_n, uint32_t timeout_ms )
+{
+    uint32_t tick;
+    rs485_rx_flag = 0;
+    tick = xTaskGetTickCount();
+
+    if( HAL_UART_Receive_IT( &huart1, rx_data, rx_n ) != HAL_OK){
+        return -1;
+    };
+    while( xTaskGetTickCount() - tick < timeout_ms  && !rs485_rx_flag){
+        osDelay(2);
+    }
+    if( !rs485_rx_flag ){
+        HAL_UART_AbortReceive_IT(&huart1);
+    }
+    return rs485_rx_flag?0:-1;
+}
+
+int32_t mb_master_write_reg( uint8_t device_addr, uint16_t reg, uint16_t reg_value )
+{
+    uint8_t cmd[16];
+    uint16_t crc;
+    uint16_t n = 0;
+    //int32_t rx_n;
+
+    cmd[n++] = device_addr; 
+    cmd[n++] = 0x06;
+    cmd[n++] = (reg>>8)&0xff;
+    cmd[n++] = reg &0xff;
+    cmd[n++] = (reg_value>>8)&0xff;
+    cmd[n++] = reg_value &0xff;
+    crc = usMBCRC16( cmd, n );
+    cmd[n++] = crc &0xff;
+    cmd[n++] = (crc>>8)&0xff;
+
+    uint8_t rx_data[50];
+    rs485_tx( cmd, n );
+    if( rs485_rx( rx_data, 8, 200 ) == 0 ){
+    }else{
+        //printf( "rx err!\n" );
+    }
+    return 0;
+}
+
+
+int32_t mb_master_read_regs( uint8_t device_addr, uint16_t reg, uint16_t* reg_values, uint16_t reg_nr )
+{
+    uint8_t cmd[16];
+    uint16_t crc;
+    uint16_t n = 0;
+    int32_t rx_n;
+
+    cmd[n++] = device_addr; 
+    cmd[n++] = 0x03;
+    cmd[n++] = (reg>>8)&0xff;
+    cmd[n++] = reg &0xff;
+    cmd[n++] = (reg_nr>>8)&0xff;
+    cmd[n++] = reg_nr &0xff;
+    crc = usMBCRC16( cmd, n );
+    cmd[n++] = crc &0xff;
+    cmd[n++] = (crc>>8)&0xff;
+
+    uint8_t rx_data[50];
+    memset( rx_data, 0x55, 20 );
+    rs485_tx( cmd, n );
+    rx_n = reg_nr*2+5;
+    if( rs485_rx( rx_data, rx_n, 200 ) == 0 ){
+        if( rx_data[0] == device_addr && rx_data[1] == 3 && rx_data[2] == reg_nr*2 ){
+            //check modbus-crc
+            if( usMBCRC16( rx_data, rx_n-2 ) == rx_data[rx_n-2]| ( rx_data[rx_n-1]<<8) ){
+                for( int i=0; i<reg_nr; i++ ){
+                    reg_values[i] = (rx_data[i*2+3]<<8)|rx_data[i*2+4];
+                }
+                return 0;
+            }
+        }
+    }else{
+        //printf( "rx err!\n" );
+    }
+    return -1;
+}
+
+void user_wind_sensor_init( void )
+{
+       wind_sensor_uart_init();
+}
+
+int32_t user_wind_strength_read( float *strength )
+{
+       uint16_t reg_value;
+       if( mb_master_read_regs( 1, 0, &reg_value, 1 ) == 0 ){
+               if( strength )
+                       *strength = reg_value/10.0f;
+               return 0;
+       }else{
+               return -1;
+       }
+}
+
+int32_t user_wind_direction_read( float *direction )
+{
+       uint16_t reg_value;
+       if( mb_master_read_regs( 2, 0, &reg_value, 1 ) == 0 ){
+               if( direction )
+                       *direction = reg_value/10.0f;
+               return 0;
+       }else{
+               return -1;
+       }
+}
+
+static void user_wind_task(void *argument)
+{
+       float strength;
+       float direction;
+       int32_t strength_err=0;
+       int32_t direction_err=0;
+       int8_t wind_state = 0;
+
+       user_wind_sensor_init();
+
+       while(1){
+
+               osDelay(2000);
+               strength = 0;
+               direction = 0;
+
+               if( user_wind_strength_read( &strength ) == 0 ){
+                       strength_err = 0;
+                       //user_noise_info_update( noise, noise_state );
+                       //printf( "noise db:%.1f ...\n", noise );
+               }else{
+                       strength_err++;
+               }
+               osDelay(2000);
+               //printf( "wind direction read ...\n" );
+               if( user_wind_direction_read( &direction ) == 0 ){
+                       direction_err = 0;
+                       //user_noise_info_update( noise, noise_state );
+                       //printf( "noise db:%.1f ...\n", noise );
+               }else{
+                       direction_err++;
+               }
+
+               wind_state = 0;
+               wind_state = ( strength_err > 10 )?1:0;
+               wind_state = ( strength_err > 10 )?2:0;
+               user_wind_info_update( strength, direction, wind_state );
+       }
+}
+
+/* Definitions for defaultTask */
+osThreadId_t windTaskHandle;
+const osThreadAttr_t windTask_attributes = {
+  .name = "windTask",
+  .stack_size = 1024,
+  .priority = (osPriority_t) osPriorityNormal,
+};
+
+void user_wind_task_start( void )
+{
+       windTaskHandle = osThreadNew(user_wind_task, NULL, &windTask_attributes);
+}
+
index 3f5cebd1d9e2d784e18c00c041b4217515272f48..c07d22e631cf01dbb53a042f94e0deeb33129dea 100644 (file)
@@ -1,9 +1,6 @@
 #include "user.h"
 #include "shell.h"
 
 #include "user.h"
 #include "shell.h"
 
-#define SW_VER_MAJOR   1
-#define SW_VER_MINOR   2
-
 extern UART_HandleTypeDef huart1;
 
 #define huart_shell  huart1
 extern UART_HandleTypeDef huart1;
 
 #define huart_shell  huart1
@@ -16,7 +13,6 @@ static uint32_t shell_rx_wr;
 static uint32_t shell_rx_rd;
 
 static uint8_t shell_rx_byte;
 static uint32_t shell_rx_rd;
 
 static uint8_t shell_rx_byte;
-static uint8_t uart_rx_flag;
 
 void shell_uart_rx_start( void )
 {
 
 void shell_uart_rx_start( void )
 {
@@ -105,7 +101,9 @@ const osThreadAttr_t shellTask_attributes = {
 
 void user_shell_start( void )
 {
 
 void user_shell_start( void )
 {
+#if USER_SHELL_USE_UART1
        shellTaskHandle = osThreadNew(user_shell_task, NULL, &shellTask_attributes);
        shellTaskHandle = osThreadNew(user_shell_task, NULL, &shellTask_attributes);
+#endif
 }
 
 
 }