Bläddra i källkod

Тесты MAI. Адресация modbus теперь работает в IAP.

TelenkovDmitry 1 år sedan
förälder
incheckning
9d418ae62d

+ 216 - 196
fw/modules/io/mux.c

@@ -1,196 +1,216 @@
-#include "at32f403a_407.h"
-#include "mux.h"
-#include "FreeRTOS.h"
-#include "task.h"
-#include <stdbool.h>
-
-/* -------------------------------------------------------------------------- */
-#if defined (MDIO_88)
-mux_channel_t leds[LED_NUMBER] = {
-                                  {INP_1, {0, 0, 0}, LED_OFF, 0},
-                                  {INP_2, {0, 0, 0}, LED_OFF, 0},
-                                  {INP_3, {0, 0, 0}, LED_OFF, 0},
-                                  {INP_4, {0, 0, 0}, LED_OFF, 0},
-
-                                  {INP_5, {0, 1, 1}, LED_OFF, 0},
-                                  {INP_6, {0, 1, 1}, LED_OFF, 0},
-                                  {INP_7, {0, 1, 1}, LED_OFF, 0},
-                                  {INP_8, {0, 1, 1}, LED_OFF, 0},
-                                  
-                                  {OUT_1_G, {1, 0, 0}, LED_OFF, 0},
-                                  {OUT_2_G, {1, 0, 0}, LED_OFF, 0},
-                                  {OUT_3_G, {1, 0, 0}, LED_OFF, 0},
-                                  {OUT_4_G, {1, 0, 0}, LED_OFF, 0},
-                                  
-                                  {OUT_1_R, {0, 1, 0}, LED_OFF, 0},
-                                  {OUT_2_R, {0, 1, 0}, LED_OFF, 0},
-                                  {OUT_3_R, {0, 1, 0}, LED_OFF, 0},
-                                  {OUT_4_R, {0, 1, 0}, LED_OFF, 0},
-
-                                  {STATUS_G, {1, 1, 0}, LED_OFF, 0},
-                                  {STATUS_R, {1, 1, 0}, LED_OFF, 0},
-                                  {RX_G, {1, 1, 0}, LED_OFF, 0},
-                                  {TX_R, {1, 1, 0}, LED_OFF, 0},
-                                  
-                                  {OUT_5_R, {0, 0, 1}, LED_OFF, 0},
-                                  {OUT_6_R, {0, 0, 1}, LED_OFF, 0},
-                                  {OUT_7_R, {0, 0, 1}, LED_OFF, 0},
-                                  {OUT_8_R, {0, 0, 1}, LED_OFF, 0},
-
-                                  {OUT_5_G, {1, 0, 1}, LED_OFF, 0},
-                                  {OUT_6_G, {1, 0, 1}, LED_OFF, 0},
-                                  {OUT_7_G, {1, 0, 1}, LED_OFF, 0},
-                                  {OUT_8_G, {1, 0, 1}, LED_OFF, 0}
-                                 };
-/* -------------------------------------------------------------------------- */
-#elif defined (MAI_12)
-
-/*uint8_t         line[3];    // [line_0, line_1, line_2]*/
-mux_channel_t leds[LED_NUMBER] = {
-                                  {IO_1_G, {0, 0, 0}, LED_OFF, 0},
-                                  {IO_1_R, {0, 0, 0}, LED_OFF, 0},
-                                  {IO_2_G, {0, 0, 0}, LED_OFF, 0},
-                                  {IO_2_R, {0, 0, 0}, LED_OFF, 0},
-                                  
-                                  {IO_3_G, {1, 0, 0}, LED_OFF, 0},
-                                  {IO_3_R, {1, 0, 0}, LED_OFF, 0},
-                                  {IO_4_G, {1, 0, 0}, LED_OFF, 0},
-                                  {IO_4_R, {1, 0, 0}, LED_OFF, 0},
-                                  
-                                  {IO_5_G, {0, 1, 0}, LED_OFF, 0},
-                                  {IO_5_R, {0, 1, 0}, LED_OFF, 0},
-                                  {IO_6_G, {0, 1, 0}, LED_OFF, 0},
-                                  {IO_6_R, {0, 1, 0}, LED_OFF, 0},
-};
-#endif
-
-//
-void mux_led_init(mux_channel_t *ch)
-{
-}
-
-
-//
-void mux_gpio_init(void)
-{
-    gpio_init_type gpio_initstructure;
-  
-    
-    crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
-    crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
-    crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
-    
-    // LED_COL
-    //      COL_1 - PD6
-    //      COL_2 - PD7
-    //      COL_3 - PB6
-    //      COL_4 - PB7
-    gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
-    gpio_initstructure.gpio_pull           = GPIO_PULL_NONE;  
-    gpio_initstructure.gpio_mode           = GPIO_MODE_OUTPUT;  
-    gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
-    gpio_initstructure.gpio_pins           = GPIO_PINS_6 | GPIO_PINS_7;
-    gpio_init(GPIOB, &gpio_initstructure); 
-
-    gpio_initstructure.gpio_pins           = GPIO_PINS_6 | GPIO_PINS_7;
-    gpio_init(GPIOD, &gpio_initstructure); 
-    
-    gpio_bits_reset(GPIOB, GPIO_PINS_6 | GPIO_PINS_7);
-    gpio_bits_reset(GPIOD, GPIO_PINS_6 | GPIO_PINS_7);
-    
-    // LED_LINE (низкий уровень на пине = высокий уровень на входе MUX)
-    //      LINE_0 - PE3
-    //      LINE_1 - PE2
-    //      LINE_2 - PB9
-    gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
-    gpio_initstructure.gpio_pull           = GPIO_PULL_NONE;  
-    gpio_initstructure.gpio_mode           = GPIO_MODE_OUTPUT;  
-    gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
-    gpio_initstructure.gpio_pins           = GPIO_PINS_2 | GPIO_PINS_3;
-    gpio_init(GPIOE, &gpio_initstructure); 
-
-    gpio_initstructure.gpio_pins           = GPIO_PINS_9;
-    gpio_init(GPIOB, &gpio_initstructure);
-    
-    gpio_bits_reset(GPIOE, GPIO_PINS_2 | GPIO_PINS_3);
-    gpio_bits_reset(GPIOB, GPIO_PINS_9);
-}
-
-
-//
-void mux_led_proc(void)
-{
-    uint8_t shift = 0;
-    
-    for (uint8_t i = 0; i < LED_NUMBER/4; i++)
-    {
-        leds[shift].line[0] ? (LINE_0_RESET) : (LINE_0_SET);
-        leds[shift].line[1] ? (LINE_1_RESET) : (LINE_1_SET);
-        leds[shift].line[2] ? (LINE_2_RESET) : (LINE_2_SET);
-        
-        leds[i*4].state     == LED_ON ? (COL_1_SET) : (COL_1_RESET);
-        leds[i*4 + 1].state == LED_ON ? (COL_2_SET) : (COL_2_RESET);
-        leds[i*4 + 2].state == LED_ON ? (COL_3_SET) : (COL_3_RESET);
-        leds[i*4 + 3].state == LED_ON ? (COL_4_SET) : (COL_4_RESET);
-        
-        if (leds[i*4].state == LED_ON || leds[i*4 + 1].state == LED_ON || 
-            leds[i*4 + 2].state == LED_ON || leds[i*4 + 3].state == LED_ON) 
-        {
-            vTaskDelay(1);
-        }
-        
-        shift += 4;
-    }
-}
-
-//
-void mux_led_test_init(void)
-{
-    LINE_0_SET;
-    LINE_1_SET;
-    LINE_2_SET;
-}
-
-//
-void mux_led_test_toggle(void)
-{
-    static bool flag = false;
-    
-    if (!flag) {
-        COL_1_SET;
-        flag = true;
-    }
-    else {
-        COL_1_RESET;
-        flag = false;
-    }
-}
-
-//
-void mux_led_blink(void)
-{
-    for (int i = 0; i < LED_NUMBER; i++)
-    {
-        leds[i].state = LED_ON;
-        
-        //vTaskDelay(100);
-        
-        //leds[i].state = LED_OFF;
-    }
-}
-
-// true - normal
-// false - alarm
-void mux_led_status(bool state)
-{
-/*  
-    if (state) {
-        leds[STATUS_G].state = LED_ON;
-        leds[STATUS_R].state = LED_OFF;
-    }
-    else {
-        leds[STATUS_G].state = LED_OFF;
-        leds[STATUS_R].state = LED_ON;
-    }
-*/    
-}
+#include "at32f403a_407.h"
+#include "mux.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include <stdbool.h>
+
+/* -------------------------------------------------------------------------- */
+#if defined (MDIO_88)
+mux_channel_t leds[LED_NUMBER] = {
+                                  {INP_1, {0, 0, 0}, LED_OFF, 0},
+                                  {INP_2, {0, 0, 0}, LED_OFF, 0},
+                                  {INP_3, {0, 0, 0}, LED_OFF, 0},
+                                  {INP_4, {0, 0, 0}, LED_OFF, 0},
+
+                                  {INP_5, {0, 1, 1}, LED_OFF, 0},
+                                  {INP_6, {0, 1, 1}, LED_OFF, 0},
+                                  {INP_7, {0, 1, 1}, LED_OFF, 0},
+                                  {INP_8, {0, 1, 1}, LED_OFF, 0},
+                                  
+                                  {OUT_1_G, {1, 0, 0}, LED_OFF, 0},
+                                  {OUT_2_G, {1, 0, 0}, LED_OFF, 0},
+                                  {OUT_3_G, {1, 0, 0}, LED_OFF, 0},
+                                  {OUT_4_G, {1, 0, 0}, LED_OFF, 0},
+                                  
+                                  {OUT_1_R, {0, 1, 0}, LED_OFF, 0},
+                                  {OUT_2_R, {0, 1, 0}, LED_OFF, 0},
+                                  {OUT_3_R, {0, 1, 0}, LED_OFF, 0},
+                                  {OUT_4_R, {0, 1, 0}, LED_OFF, 0},
+
+                                  {STATUS_G, {1, 1, 0}, LED_OFF, 0},
+                                  {STATUS_R, {1, 1, 0}, LED_OFF, 0},
+                                  {RX_G, {1, 1, 0}, LED_OFF, 0},
+                                  {TX_R, {1, 1, 0}, LED_OFF, 0},
+                                  
+                                  {OUT_5_R, {0, 0, 1}, LED_OFF, 0},
+                                  {OUT_6_R, {0, 0, 1}, LED_OFF, 0},
+                                  {OUT_7_R, {0, 0, 1}, LED_OFF, 0},
+                                  {OUT_8_R, {0, 0, 1}, LED_OFF, 0},
+
+                                  {OUT_5_G, {1, 0, 1}, LED_OFF, 0},
+                                  {OUT_6_G, {1, 0, 1}, LED_OFF, 0},
+                                  {OUT_7_G, {1, 0, 1}, LED_OFF, 0},
+                                  {OUT_8_G, {1, 0, 1}, LED_OFF, 0}
+                                 };
+/* -------------------------------------------------------------------------- */
+#elif defined (MAI_12)
+
+/*uint8_t         line[3];    // [line_0, line_1, line_2]*/
+mux_channel_t leds[LED_NUMBER] = {
+                                  {IO_1_G, {0, 0, 0}, LED_OFF, 0},
+                                  {IO_1_R, {0, 0, 0}, LED_OFF, 0},
+                                  {IO_2_G, {0, 0, 0}, LED_OFF, 0},
+                                  {IO_2_R, {0, 0, 0}, LED_OFF, 0},
+                                  
+                                  {IO_3_G, {1, 0, 0}, LED_OFF, 0},
+                                  {IO_3_R, {1, 0, 0}, LED_OFF, 0},
+                                  {IO_4_G, {1, 0, 0}, LED_OFF, 0},
+                                  {IO_4_R, {1, 0, 0}, LED_OFF, 0},
+                                  
+                                  {IO_5_G, {0, 1, 0}, LED_OFF, 0},
+                                  {IO_5_R, {0, 1, 0}, LED_OFF, 0},
+                                  {IO_6_G, {0, 1, 0}, LED_OFF, 0},
+                                  {IO_6_R, {0, 1, 0}, LED_OFF, 0},
+                                  
+                                  {STATUS_G, {1, 1, 0}, LED_OFF, 0},
+                                  {STATUS_R, {1, 1, 0}, LED_OFF, 0},
+                                  {RX_G, {1, 1, 0}, LED_OFF, 0},
+                                  {TX_R, {1, 1, 0}, LED_OFF, 0},
+                                  
+                                  {IO_7_G, {0, 0, 1}, LED_OFF, 0},
+                                  {IO_7_R, {0, 0, 1}, LED_OFF, 0},
+                                  {IO_8_G, {0, 0, 1}, LED_OFF, 0},
+                                  {IO_8_R, {0, 0, 1}, LED_OFF, 0},
+                                  
+                                  {IO_9_G, {1, 0, 1}, LED_OFF, 0},
+                                  {IO_9_R, {1, 0, 1}, LED_OFF, 0},
+                                  {IO_10_G, {1, 0, 1}, LED_OFF, 0},
+                                  {IO_10_R, {1, 0, 1}, LED_OFF, 0},
+                                  
+                                  {IO_11_G, {0, 1, 1}, LED_OFF, 0},
+                                  {IO_11_R, {0, 1, 1}, LED_OFF, 0},
+                                  {IO_12_G, {0, 1, 1}, LED_OFF, 0},
+                                  {IO_12_R, {0, 1, 1}, LED_OFF, 0},
+};
+#endif
+
+//
+void mux_led_init(mux_channel_t *ch)
+{
+}
+
+
+//
+void mux_gpio_init(void)
+{
+    gpio_init_type gpio_initstructure;
+  
+    
+    crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
+    crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
+    crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
+    
+    // LED_COL
+    //      COL_1 - PD6
+    //      COL_2 - PD7
+    //      COL_3 - PB6
+    //      COL_4 - PB7
+    gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
+    gpio_initstructure.gpio_pull           = GPIO_PULL_NONE;  
+    gpio_initstructure.gpio_mode           = GPIO_MODE_OUTPUT;  
+    gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
+    gpio_initstructure.gpio_pins           = GPIO_PINS_6 | GPIO_PINS_7;
+    gpio_init(GPIOB, &gpio_initstructure); 
+
+    gpio_initstructure.gpio_pins           = GPIO_PINS_6 | GPIO_PINS_7;
+    gpio_init(GPIOD, &gpio_initstructure); 
+    
+    gpio_bits_reset(GPIOB, GPIO_PINS_6 | GPIO_PINS_7);
+    gpio_bits_reset(GPIOD, GPIO_PINS_6 | GPIO_PINS_7);
+    
+    // LED_LINE (низкий уровень на пине = высокий уровень на входе MUX)
+    //      LINE_0 - PE3
+    //      LINE_1 - PE2
+    //      LINE_2 - PB9
+    gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
+    gpio_initstructure.gpio_pull           = GPIO_PULL_NONE;  
+    gpio_initstructure.gpio_mode           = GPIO_MODE_OUTPUT;  
+    gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
+    gpio_initstructure.gpio_pins           = GPIO_PINS_2 | GPIO_PINS_3;
+    gpio_init(GPIOE, &gpio_initstructure); 
+
+    gpio_initstructure.gpio_pins           = GPIO_PINS_9;
+    gpio_init(GPIOB, &gpio_initstructure);
+    
+    gpio_bits_reset(GPIOE, GPIO_PINS_2 | GPIO_PINS_3);
+    gpio_bits_reset(GPIOB, GPIO_PINS_9);
+}
+
+
+//
+void mux_led_proc(void)
+{
+    uint8_t shift = 0;
+    
+    for (uint8_t i = 0; i < LED_NUMBER/4; i++)
+    {
+        leds[shift].line[0] ? (LINE_0_RESET) : (LINE_0_SET);
+        leds[shift].line[1] ? (LINE_1_RESET) : (LINE_1_SET);
+        leds[shift].line[2] ? (LINE_2_RESET) : (LINE_2_SET);
+        
+        leds[i*4].state     == LED_ON ? (COL_1_SET) : (COL_1_RESET);
+        leds[i*4 + 1].state == LED_ON ? (COL_2_SET) : (COL_2_RESET);
+        leds[i*4 + 2].state == LED_ON ? (COL_3_SET) : (COL_3_RESET);
+        leds[i*4 + 3].state == LED_ON ? (COL_4_SET) : (COL_4_RESET);
+        
+        if (leds[i*4].state == LED_ON || leds[i*4 + 1].state == LED_ON || 
+            leds[i*4 + 2].state == LED_ON || leds[i*4 + 3].state == LED_ON) 
+        {
+            vTaskDelay(1);
+        }
+        
+        shift += 4;
+    }
+}
+
+//
+void mux_led_test_init(void)
+{
+    LINE_0_SET;
+    LINE_1_SET;
+    LINE_2_SET;
+}
+
+//
+void mux_led_test_toggle(void)
+{
+    static bool flag = false;
+    
+    if (!flag) {
+        COL_1_SET;
+        flag = true;
+    }
+    else {
+        COL_1_RESET;
+        flag = false;
+    }
+}
+
+//
+void mux_led_blink(void)
+{
+    for (int i = 0; i < LED_NUMBER; i++)
+    {
+        leds[i].state = LED_ON;
+        
+        vTaskDelay(100);
+        
+        leds[i].state = LED_OFF;
+    }
+}
+
+// true - normal
+// false - alarm
+void mux_led_status(bool state)
+{
+/*  
+    if (state) {
+        leds[STATUS_G].state = LED_ON;
+        leds[STATUS_R].state = LED_OFF;
+    }
+    else {
+        leds[STATUS_G].state = LED_OFF;
+        leds[STATUS_R].state = LED_ON;
+    }
+*/    
+}

+ 162 - 168
fw/modules/io/mux.h

@@ -1,168 +1,162 @@
-#ifndef __MUX_H
-#define __MUX_H
-
-#include <stdbool.h>
-
-//
-
-#define LINE_0_SET      GPIOE->scr = GPIO_PINS_3
-#define LINE_0_RESET    GPIOE->clr = GPIO_PINS_3
-
-#define LINE_1_SET      GPIOE->scr = GPIO_PINS_2
-#define LINE_1_RESET    GPIOE->clr = GPIO_PINS_2
-
-#define LINE_2_SET      GPIOB->scr = GPIO_PINS_9
-#define LINE_2_RESET    GPIOB->clr = GPIO_PINS_9
-
-#define COL_1_SET      GPIOD->scr = GPIO_PINS_6
-#define COL_1_RESET    GPIOD->clr = GPIO_PINS_6
-
-#define COL_2_SET      GPIOD->scr = GPIO_PINS_7
-#define COL_2_RESET    GPIOD->clr = GPIO_PINS_7
-
-#define COL_3_SET      GPIOB->scr = GPIO_PINS_6
-#define COL_3_RESET    GPIOB->clr = GPIO_PINS_6
-
-#define COL_4_SET      GPIOB->scr = GPIO_PINS_7
-#define COL_4_RESET    GPIOB->clr = GPIO_PINS_7
-
-/* -------------------------------------------------------------------------- */
-#if defined (MDIO_88)
-
-#define LED_NUMBER      28
-
-typedef enum
-{
-    INP_1 = 0, 
-    INP_2, 
-    INP_3, 
-    INP_4,
-    
-    INP_5, 
-    INP_6, 
-    INP_7, 
-    INP_8,
-    
-    OUT_1_G,
-    OUT_2_G,
-    OUT_3_G,
-    OUT_4_G,
-    
-    OUT_1_R,
-    OUT_2_R,
-    OUT_3_R,
-    OUT_4_R,
-    
-    STATUS_G,
-    STATUS_R,
-    RX_G,
-    TX_R,
-    
-    OUT_5_R,
-    OUT_6_R,
-    OUT_7_R,
-    OUT_8_R,
-    
-    OUT_5_G,
-    OUT_6_G,
-    OUT_7_G,
-    OUT_8_G,
-      
-} led_t;
-/* -------------------------------------------------------------------------- */
-#elif defined (MAI_12)
-
-#define LED_NUMBER      12
-
-typedef enum
-{
-    IO_1_G = 0, 
-    IO_1_R, 
-    
-    IO_2_G,
-    IO_2_R,
-    
-    IO_3_G,
-    IO_3_R,
-    
-    IO_4_G,
-    IO_4_R,
-    
-    IO_5_G,
-    IO_5_R,
-    
-    IO_6_G,
-    IO_6_R,
-    
-    IO_7_G,
-    IO_7_R,
-    
-    IO_8_G,
-    IO_8_R,
-    
-    IO_9_G,
-    IO_9_R,
-    
-    IO_10_G,
-    IO_10_R,
-    
-    IO_11_G,
-    IO_11_R,
-    
-    IO_12_G,
-    IO_12_R,
-        
-    STATUS_G,
-    STATUS_R,
-    RX_G,
-    TX_R,
-    
-} led_t;
-#endif
-/* -------------------------------------------------------------------------- */
-
-typedef enum
-{
-    LED_OFF = 0, 
-    LED_ON,
-    LED_BLINK,
-    
-} led_state_t;
-
-
-typedef struct 
-{
-    led_t           name;
-    uint8_t         line[3];    // [line_0, line_1, line_2]
-    led_state_t     state;
-    uint32_t        cnt;
-    
-} mux_channel_t;
-
-
-//
-void mux_led_init(mux_channel_t *ch);
-
-//
-void mux_gpio_init(void);
-
-//
-void mux_led_proc(void);
-
-//
-void mux_led_blink(void);
-
-//
-void mux_led_test_init(void);
-
-//
-void mux_led_test_toggle(void);
-
-//
-void mux_led_status(bool state);
-
-
-extern mux_channel_t leds[];
-
-#endif  // __MUX_H
-
+#ifndef __MUX_H
+#define __MUX_H
+
+#include <stdbool.h>
+
+//
+
+#define LINE_0_SET      GPIOE->scr = GPIO_PINS_3
+#define LINE_0_RESET    GPIOE->clr = GPIO_PINS_3
+
+#define LINE_1_SET      GPIOE->scr = GPIO_PINS_2
+#define LINE_1_RESET    GPIOE->clr = GPIO_PINS_2
+
+#define LINE_2_SET      GPIOB->scr = GPIO_PINS_9
+#define LINE_2_RESET    GPIOB->clr = GPIO_PINS_9
+
+#define COL_1_SET      GPIOD->scr = GPIO_PINS_6
+#define COL_1_RESET    GPIOD->clr = GPIO_PINS_6
+
+#define COL_2_SET      GPIOD->scr = GPIO_PINS_7
+#define COL_2_RESET    GPIOD->clr = GPIO_PINS_7
+
+#define COL_3_SET      GPIOB->scr = GPIO_PINS_6
+#define COL_3_RESET    GPIOB->clr = GPIO_PINS_6
+
+#define COL_4_SET      GPIOB->scr = GPIO_PINS_7
+#define COL_4_RESET    GPIOB->clr = GPIO_PINS_7
+
+/* -------------------------------------------------------------------------- */
+#if defined (MDIO_88)
+
+#define LED_NUMBER      28
+
+typedef enum
+{
+    INP_1 = 0, 
+    INP_2, 
+    INP_3, 
+    INP_4,
+    
+    INP_5, 
+    INP_6, 
+    INP_7, 
+    INP_8,
+    
+    OUT_1_G,
+    OUT_2_G,
+    OUT_3_G,
+    OUT_4_G,
+    
+    OUT_1_R,
+    OUT_2_R,
+    OUT_3_R,
+    OUT_4_R,
+    
+    STATUS_G,
+    STATUS_R,
+    RX_G,
+    TX_R,
+    
+    OUT_5_R,
+    OUT_6_R,
+    OUT_7_R,
+    OUT_8_R,
+    
+    OUT_5_G,
+    OUT_6_G,
+    OUT_7_G,
+    OUT_8_G,
+      
+} led_t;
+/* -------------------------------------------------------------------------- */
+#elif defined (MAI_12)
+
+#define LED_NUMBER      28
+
+typedef enum
+{
+    IO_1_G = 0, 
+    IO_1_R, 
+    IO_2_G,
+    IO_2_R,
+    
+    IO_3_G,
+    IO_3_R,
+    IO_4_G,
+    IO_4_R,
+    
+    IO_5_G,
+    IO_5_R,
+    IO_6_G,
+    IO_6_R,
+    
+    STATUS_G,
+    STATUS_R,
+    RX_G,
+    TX_R,
+    
+    IO_7_G,
+    IO_7_R,
+    IO_8_G,
+    IO_8_R,
+    
+    IO_9_G,
+    IO_9_R,
+    IO_10_G,
+    IO_10_R,
+    
+    IO_11_G,
+    IO_11_R,
+    IO_12_G,
+    IO_12_R,
+    
+} led_t;
+#endif
+/* -------------------------------------------------------------------------- */
+
+typedef enum
+{
+    LED_OFF = 0, 
+    LED_ON,
+    LED_BLINK,
+    
+} led_state_t;
+
+
+typedef struct 
+{
+    led_t           name;
+    uint8_t         line[3];    // [line_0, line_1, line_2]
+    led_state_t     state;
+    uint32_t        cnt;
+    
+} mux_channel_t;
+
+
+//
+void mux_led_init(mux_channel_t *ch);
+
+//
+void mux_gpio_init(void);
+
+//
+void mux_led_proc(void);
+
+//
+void mux_led_blink(void);
+
+//
+void mux_led_test_init(void);
+
+//
+void mux_led_test_toggle(void);
+
+//
+void mux_led_status(bool state);
+
+
+extern mux_channel_t leds[];
+
+#endif  // __MUX_H
+

+ 428 - 428
fw/modules/modbus/modbus.c

@@ -1,428 +1,428 @@
-#include "at32f403a_407.h"
-#include "common_config.h"
-#include "modbus.h"
-#include "update.h"
-#include "FreeRTOS.h"
-#include "task.h"
-#include "mb.h"
-#include "mbport.h"
-#include "mbrtu.h"
-#include "tim_delay.h"
-#include "modbus_params.h"
-#include "common_gpio.h"
-#include "io_utils.h"
-#include <stdio.h>
-#include <string.h>
-#include <stdbool.h>
-
-#define REG_HOLDING_START		( 1 )
-#define REG_HOLDING_NREGS		( 5 )
-
-
-
-static USHORT	usRegHoldingStart = REG_HOLDING_START;
-extern UCHAR    rcvAddress;
-
-//osThreadId modbus_task_handle;
-static void modbus_task(void *params);
-//osThreadId modbus_params_handle;
-static void modbus_params(void *params);
-
-//osTimerId reset_timer_handle;
-//osTimerId settings_timer_handle;
-//osTimerId modbus_timer_handle;
-
-mb_delay_action_t mb_action = MB_NO_ACTION;
-uint8_t new_slave_addr;
-bool mb_package_flag = false;
-
-
-
-//
-void mb_init(void)
-{
-    uint32_t baud = 115200;
-    eMBParity par = MB_PAR_NONE;
-    UCHAR mb_addr = cm_gpio_get_mb_addr();
-    unsigned int stop_bits = 1;
-	const UCHAR ucSlaveID[] = {0xAA, 0xBB, 0xCC};
-    
-	mb_init_params();
-
-    mb_helper_tim_init(baud);
-    eMBInit(MB_RTU, mb_addr, 4, baud, par, stop_bits);
-	eMBSetSlaveID(0x34, TRUE, ucSlaveID, 3);
-	eMBEnable();
-    
-    
-    xTaskCreate(modbus_task, "modbus_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-    
-    xTaskCreate(modbus_params, "modbus_params", 4*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-    
-    // Таймер для перехода в IAP
-    update_create_timer();
-    
-#if 0    
-	osTimerDef(vResetTimer, modbus_reset);
-	reset_timer_handle = osTimerCreate(osTimer(vResetTimer), osTimerOnce, NULL);
-    
-    osTimerDef(vSettingsTimer, settings_timer);
-    settings_timer_handle = osTimerCreate(osTimer(vSettingsTimer), osTimerOnce, NULL);
-    
-    osTimerDef(vModbusTimer, modbus_port_timer);
-    modbus_timer_handle = osTimerCreate(osTimer(vModbusTimer), osTimerOnce, NULL);
-#endif    
-}
-
-//
-void modbus_task(void *params)
-{
-	(void)params;
-
-	for (;;)
-	{
-		eMBPoll();
-	}
-
-}
-
-//
-void modbus_params(void *params)
-{
-    for (;;)
-    {
-        if (mb_action != MB_NO_ACTION)
-        {
-            switch (mb_action)
-            {
-                // Установка нового адреса
-                case MB_CHANGE_ADDR :
-    
-                    //settings.com_settings.mb_addr = new_slave_addr;  
-                    eMBSetSlaveAddr(new_slave_addr);
-                      
-                break;
-                
-                case MB_CHANGE_PORT_SETTINGS :
-#if 0                  
-                    osDelay(200);
-                    eMBRTUStop();
-                    settings_set_modbus_params(temp_modbus_port);
-                    settings_init_mb_port(settings.com_settings.mb_addr);
-                    MBDBG settings_print_modbus();
-                    eMBRTUStart();
-                    eMBEnable();
-                    mb_package_flag = false;
-                    osTimerStart(modbus_timer_handle, 9800);
-#endif                  
-                break;
-                
-                // Полное восстановление дефолтных настроек
-                case MB_DEF_SETTINGS :
-#if 0
-                    settings_set_default(true);
-                    settings_save();
-#endif                  
-                break;  
-                
-                // Частичное восстановление дефолтных настроек
-                case MB_PART_DEF_SETTINGS :
-#if 0
-                    settings_set_default(false);
-                    settings_save();
-#endif                  
-                break;
-                
-                  
-                case MB_SAVE_SETTINGS :
-#if 0                
-                    settings_save();
-#endif                  
-                break;
-                
-                case MB_SAVE_SYS_SETTINGS :
-#if 0
-                    if (set_sys_settings_flag) {
-                        memcpy(&sys_settings, &temp_sys_settings, sizeof(sys_settings));
-                        sys_settings_save();
-                    }
-#endif                  
-                break;
-                
-                case MB_RESET :
-                  
-                    vTaskDelay(100);
-                    NVIC_SystemReset();
-                  
-                break;
-                
-                default : break;
-            }
-            
-            mb_action = MB_NO_ACTION;
-        }
-        vTaskDelay(10);
-    }
-}
-
-
-// Отложенная перезагрузка в Bootloader
-void modbus_reset(void const * params)
-{
-#if 0  
-	MBDBG printf("Switch to bootloader!\r\n");
-	rtc_set_load_mode(1);
-	HAL_NVIC_SystemReset();
-#endif    
-}
-
-
-// Запуск таймера на изменение системных настроек
-void settings_timer(void const * params)
-{
-#if 0  
-    set_sys_settings_flag = false;
-    psw_ok = false;
-#endif    
-}
-
-
-// Запуск таймера для изменение настроек modbus
-void modbus_port_timer(void const * params)
-{
-#if 0
-    eMBRTUStop();
-  
-    // Были транзакции по modbus с новыми настройками. 
-    // Нужно сохранить новые настройки.
-    if (mb_package_flag)
-    {
-        settings.com_settings.mb_port = temp_modbus_port;
-        settings_set_modbus_params(settings.com_settings.mb_port);
-        settings_init_mb_port(settings.com_settings.mb_addr);
-        settings_save();
-        
-    }
-    // Нужно вернуть старые настройки
-    else
-    {
-        temp_modbus_port = settings.com_settings.mb_port;
-        settings_set_modbus_params(settings.com_settings.mb_port);
-        settings_init_mb_port(settings.com_settings.mb_addr);
-    }
-  
-    
-    MBDBG settings_print_modbus();
-    eMBRTUStart();
-    eMBEnable();
-#endif    
-}
-
-// ------------------------------------------------------------------- //
-//						Modbus callback's
-// ------------------------------------------------------------------- //
-
-//
-// 65 (0x41) Read Input Registers	 
-eMBErrorCode
-eMBUpdateCB( UCHAR * pucFrame, USHORT * usLen)
-{
-	eMBErrorCode    eStatus = MB_ENOERR;
-
-    mb_package_flag = true;
-    
-	// Команда перехода в bootloader.
-	if (pucFrame[1] == 0x01) {
-        update_tim_start();
-	}
-
-    return eStatus;
-}
-
-eMBErrorCode
-eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
-{
-	return MB_ENOREG;
-}
-
-
-// 03 (0x03) Read Holding Registers
-// чтение N регистров управления и уставок
-// 16 (0x10) Write Multiple registers
-// запись N регистров управления и уставок (0x10)
-eMBErrorCode
-eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
-{
-	eMBErrorCode eStatus = MB_ENOERR;
-
-	int			 iRegIndex;
-    
-    mb_delay_action_t mb_act = MB_NO_ACTION;
-	uint16_t regs_cnt = usNRegs;
-	uint16_t index_param;
-	uint8_t *ptr_buf = pucRegBuffer;
-	uint16_t size;
-
-/*    
-	printf("usAddress: 0x%X\r\n", usAddress);
-	printf("usNRegs: %u\r\n", usNRegs);
-	printf("eMode: %u\r\n", eMode);
-    printf("pucRegBuffer :%X\r\n", pucRegBuffer[0]);
-*/  
-    
-    save_mode_reset_cnt();
-      
-    mb_package_flag = true;
-    
-	iRegIndex = (int)(usAddress - usRegHoldingStart);
-
-    
-    
-#if 1    
-	switch (eMode)
-	{
-		case MB_REG_READ :
-
-			while (regs_cnt)
-			{
-				// Поиск регистра
-				if (!mb_find_param(iRegIndex, &index_param, &size))
-				{
-					return MB_ENOREG; // нет запрашиваемого регистра
-				}
-				else
-				{
-					MBDBG printf("reg: %X, index: %u, size: %u\r\n", iRegIndex, index_param, size);
-                    MBDBG printf("reg: %X\r\n", iRegIndex);
-					mb_get_param(ptr_buf,  index_param); // Вызов функции записи
-					iRegIndex += size;
-					ptr_buf += 2*size;
-					regs_cnt -= size;
-				}
-			}
-
-
-		break;
-
-		case MB_REG_WRITE :
-
-			while (regs_cnt)
-			{
-				// Поиск регистра
-				if (!mb_find_param(iRegIndex, &index_param, &size))
-				{
-					return MB_ENOREG; // нет запрашиваемого регистра
-				}
-				else
-				{
-					MBDBG printf("reg: %X, index: %u, size: %u\r\n", iRegIndex, index_param, size);
-					mb_act = mb_set_param(ptr_buf, index_param); // Вызов функции записи
-
-                    if (mb_act != MB_NO_ACTION)
-                        mb_action = mb_act;
-                    
-					iRegIndex += size;
-					ptr_buf += 2*size;
-					regs_cnt -= size;
-                    
-                    if (mb_act == MB_PAS_ERR)
-                        return MB_EINVAL;
-				}
-			}
-
-		break;
-	}
-#endif
-	return eStatus;
-}
-
-// 17 (0x11) Report Slave ID (Serial Line only) 
-// чтение информации об устройстве (0x11)
-eMBException
-eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen )
-{
-#if 1
-    mb_package_flag = true;
-    
-	//*usLen = mb_get_info(&pucFrame[1]) + 3;
-#endif
-	return MB_EX_NONE;
-}
-
-
-// чтение/запись значений из нескольких регистров флагов (Coil Status).
-// чтения N регистров параметров
-eMBErrorCode
-eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
-{
-    return MB_ENOREG;
-}
-
-eMBErrorCode
-eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
-{
-    return MB_ENOREG;
-}
-
-
-// 0x42
-// Запрос на изменение адреса
-// Если запрос широковещательный, то нужно проверить присланный ID.
-eMBException    
-eMBSetAddrIdCB( UCHAR * pucFrame, USHORT * usLen )
-{
-#if 0  
-    mb_package_flag = true;
-    
-    if (rcvAddress == MB_ADDRESS_BROADCAST)
-    {
-        if (mb_set_addr_id(&pucFrame[1], &new_slave_addr))
-            mb_action = MB_CHANGE_ADDR;
-    }
-    // Адресный запрос. ID не проверяем
-    else
-    {
-        if (mb_set_addr(&pucFrame[1], &new_slave_addr))
-        {
-            pucFrame[1] = new_slave_addr;
-            mb_action = MB_CHANGE_ADDR;
-        }
-        else 
-            pucFrame[1] = MB_BOOT_ERR_WRONG_CONTENT;
-    }
-    
-    *usLen = 2;
-#endif    
-    return MB_EX_NONE;
-}
-
-
-// 0x43
-eMBException
-eMBSetAddrSerialCB( UCHAR * pucFrame, USHORT * usLen )
-{
-#if 0  
-    mb_package_flag = true;
-      
-    if (rcvAddress == MB_ADDRESS_BROADCAST)
-    {
-        if (mb_set_addr_serial(&pucFrame[1], &new_slave_addr))
-            mb_action = MB_CHANGE_ADDR;
-    }
-    // Адресный запрос. ID не проверяем
-    else
-    {
-        if (mb_set_addr(&pucFrame[1], &new_slave_addr))
-        {
-            pucFrame[1] = new_slave_addr;
-            mb_action = MB_CHANGE_ADDR;
-        }
-        else 
-            pucFrame[1] = MB_BOOT_ERR_WRONG_CONTENT;
-    }
-    
-    *usLen = 2;
-#endif    
-    return MB_EX_NONE;
-}
-
+#include "at32f403a_407.h"
+#include "common_config.h"
+#include "modbus.h"
+#include "update.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "mb.h"
+#include "mbport.h"
+#include "mbrtu.h"
+#include "tim_delay.h"
+#include "modbus_params.h"
+#include "common_gpio.h"
+#include "io_utils.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+
+#define REG_HOLDING_START		( 1 )
+#define REG_HOLDING_NREGS		( 5 )
+
+
+
+static USHORT	usRegHoldingStart = REG_HOLDING_START;
+extern UCHAR    rcvAddress;
+
+//osThreadId modbus_task_handle;
+static void modbus_task(void *params);
+//osThreadId modbus_params_handle;
+static void modbus_params(void *params);
+
+//osTimerId reset_timer_handle;
+//osTimerId settings_timer_handle;
+//osTimerId modbus_timer_handle;
+
+mb_delay_action_t mb_action = MB_NO_ACTION;
+uint8_t new_slave_addr;
+bool mb_package_flag = false;
+
+
+
+//
+void mb_init(void)
+{
+    uint32_t baud = 115200;
+    eMBParity par = MB_PAR_NONE;
+    UCHAR mb_addr = gpio_get_mbaddr();
+    unsigned int stop_bits = 1;
+	const UCHAR ucSlaveID[] = {0xAA, 0xBB, 0xCC};
+    
+	mb_init_params();
+
+    mb_helper_tim_init(baud);
+    eMBInit(MB_RTU, mb_addr, 4, baud, par, stop_bits);
+	eMBSetSlaveID(0x34, TRUE, ucSlaveID, 3);
+	eMBEnable();
+    
+    
+    xTaskCreate(modbus_task, "modbus_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    
+    xTaskCreate(modbus_params, "modbus_params", 4*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    
+    // Таймер для перехода в IAP
+    update_create_timer();
+    
+#if 0    
+	osTimerDef(vResetTimer, modbus_reset);
+	reset_timer_handle = osTimerCreate(osTimer(vResetTimer), osTimerOnce, NULL);
+    
+    osTimerDef(vSettingsTimer, settings_timer);
+    settings_timer_handle = osTimerCreate(osTimer(vSettingsTimer), osTimerOnce, NULL);
+    
+    osTimerDef(vModbusTimer, modbus_port_timer);
+    modbus_timer_handle = osTimerCreate(osTimer(vModbusTimer), osTimerOnce, NULL);
+#endif    
+}
+
+//
+void modbus_task(void *params)
+{
+	(void)params;
+
+	for (;;)
+	{
+		eMBPoll();
+	}
+
+}
+
+//
+void modbus_params(void *params)
+{
+    for (;;)
+    {
+        if (mb_action != MB_NO_ACTION)
+        {
+            switch (mb_action)
+            {
+                // Установка нового адреса
+                case MB_CHANGE_ADDR :
+    
+                    //settings.com_settings.mb_addr = new_slave_addr;  
+                    eMBSetSlaveAddr(new_slave_addr);
+                      
+                break;
+                
+                case MB_CHANGE_PORT_SETTINGS :
+#if 0                  
+                    osDelay(200);
+                    eMBRTUStop();
+                    settings_set_modbus_params(temp_modbus_port);
+                    settings_init_mb_port(settings.com_settings.mb_addr);
+                    MBDBG settings_print_modbus();
+                    eMBRTUStart();
+                    eMBEnable();
+                    mb_package_flag = false;
+                    osTimerStart(modbus_timer_handle, 9800);
+#endif                  
+                break;
+                
+                // Полное восстановление дефолтных настроек
+                case MB_DEF_SETTINGS :
+#if 0
+                    settings_set_default(true);
+                    settings_save();
+#endif                  
+                break;  
+                
+                // Частичное восстановление дефолтных настроек
+                case MB_PART_DEF_SETTINGS :
+#if 0
+                    settings_set_default(false);
+                    settings_save();
+#endif                  
+                break;
+                
+                  
+                case MB_SAVE_SETTINGS :
+#if 0                
+                    settings_save();
+#endif                  
+                break;
+                
+                case MB_SAVE_SYS_SETTINGS :
+#if 0
+                    if (set_sys_settings_flag) {
+                        memcpy(&sys_settings, &temp_sys_settings, sizeof(sys_settings));
+                        sys_settings_save();
+                    }
+#endif                  
+                break;
+                
+                case MB_RESET :
+                  
+                    vTaskDelay(100);
+                    NVIC_SystemReset();
+                  
+                break;
+                
+                default : break;
+            }
+            
+            mb_action = MB_NO_ACTION;
+        }
+        vTaskDelay(10);
+    }
+}
+
+
+// Отложенная перезагрузка в Bootloader
+void modbus_reset(void const * params)
+{
+#if 0  
+	MBDBG printf("Switch to bootloader!\r\n");
+	rtc_set_load_mode(1);
+	HAL_NVIC_SystemReset();
+#endif    
+}
+
+
+// Запуск таймера на изменение системных настроек
+void settings_timer(void const * params)
+{
+#if 0  
+    set_sys_settings_flag = false;
+    psw_ok = false;
+#endif    
+}
+
+
+// Запуск таймера для изменение настроек modbus
+void modbus_port_timer(void const * params)
+{
+#if 0
+    eMBRTUStop();
+  
+    // Были транзакции по modbus с новыми настройками. 
+    // Нужно сохранить новые настройки.
+    if (mb_package_flag)
+    {
+        settings.com_settings.mb_port = temp_modbus_port;
+        settings_set_modbus_params(settings.com_settings.mb_port);
+        settings_init_mb_port(settings.com_settings.mb_addr);
+        settings_save();
+        
+    }
+    // Нужно вернуть старые настройки
+    else
+    {
+        temp_modbus_port = settings.com_settings.mb_port;
+        settings_set_modbus_params(settings.com_settings.mb_port);
+        settings_init_mb_port(settings.com_settings.mb_addr);
+    }
+  
+    
+    MBDBG settings_print_modbus();
+    eMBRTUStart();
+    eMBEnable();
+#endif    
+}
+
+// ------------------------------------------------------------------- //
+//						Modbus callback's
+// ------------------------------------------------------------------- //
+
+//
+// 65 (0x41) Read Input Registers	 
+eMBErrorCode
+eMBUpdateCB( UCHAR * pucFrame, USHORT * usLen)
+{
+	eMBErrorCode    eStatus = MB_ENOERR;
+
+    mb_package_flag = true;
+    
+	// Команда перехода в bootloader.
+	if (pucFrame[1] == 0x01) {
+        update_tim_start();
+	}
+
+    return eStatus;
+}
+
+eMBErrorCode
+eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
+{
+	return MB_ENOREG;
+}
+
+
+// 03 (0x03) Read Holding Registers
+// чтение N регистров управления и уставок
+// 16 (0x10) Write Multiple registers
+// запись N регистров управления и уставок (0x10)
+eMBErrorCode
+eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
+{
+	eMBErrorCode eStatus = MB_ENOERR;
+
+	int			 iRegIndex;
+    
+    mb_delay_action_t mb_act = MB_NO_ACTION;
+	uint16_t regs_cnt = usNRegs;
+	uint16_t index_param;
+	uint8_t *ptr_buf = pucRegBuffer;
+	uint16_t size;
+
+/*    
+	printf("usAddress: 0x%X\r\n", usAddress);
+	printf("usNRegs: %u\r\n", usNRegs);
+	printf("eMode: %u\r\n", eMode);
+    printf("pucRegBuffer :%X\r\n", pucRegBuffer[0]);
+*/  
+    
+    save_mode_reset_cnt();
+      
+    mb_package_flag = true;
+    
+	iRegIndex = (int)(usAddress - usRegHoldingStart);
+
+    
+    
+#if 1    
+	switch (eMode)
+	{
+		case MB_REG_READ :
+
+			while (regs_cnt)
+			{
+				// Поиск регистра
+				if (!mb_find_param(iRegIndex, &index_param, &size))
+				{
+					return MB_ENOREG; // нет запрашиваемого регистра
+				}
+				else
+				{
+					MBDBG printf("reg: %X, index: %u, size: %u\r\n", iRegIndex, index_param, size);
+                    MBDBG printf("reg: %X\r\n", iRegIndex);
+					mb_get_param(ptr_buf,  index_param); // Вызов функции записи
+					iRegIndex += size;
+					ptr_buf += 2*size;
+					regs_cnt -= size;
+				}
+			}
+
+
+		break;
+
+		case MB_REG_WRITE :
+
+			while (regs_cnt)
+			{
+				// Поиск регистра
+				if (!mb_find_param(iRegIndex, &index_param, &size))
+				{
+					return MB_ENOREG; // нет запрашиваемого регистра
+				}
+				else
+				{
+					MBDBG printf("reg: %X, index: %u, size: %u\r\n", iRegIndex, index_param, size);
+					mb_act = mb_set_param(ptr_buf, index_param); // Вызов функции записи
+
+                    if (mb_act != MB_NO_ACTION)
+                        mb_action = mb_act;
+                    
+					iRegIndex += size;
+					ptr_buf += 2*size;
+					regs_cnt -= size;
+                    
+                    if (mb_act == MB_PAS_ERR)
+                        return MB_EINVAL;
+				}
+			}
+
+		break;
+	}
+#endif
+	return eStatus;
+}
+
+// 17 (0x11) Report Slave ID (Serial Line only) 
+// чтение информации об устройстве (0x11)
+eMBException
+eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen )
+{
+#if 1
+    mb_package_flag = true;
+    
+	//*usLen = mb_get_info(&pucFrame[1]) + 3;
+#endif
+	return MB_EX_NONE;
+}
+
+
+// чтение/запись значений из нескольких регистров флагов (Coil Status).
+// чтения N регистров параметров
+eMBErrorCode
+eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
+{
+    return MB_ENOREG;
+}
+
+eMBErrorCode
+eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
+{
+    return MB_ENOREG;
+}
+
+
+// 0x42
+// Запрос на изменение адреса
+// Если запрос широковещательный, то нужно проверить присланный ID.
+eMBException    
+eMBSetAddrIdCB( UCHAR * pucFrame, USHORT * usLen )
+{
+#if 0  
+    mb_package_flag = true;
+    
+    if (rcvAddress == MB_ADDRESS_BROADCAST)
+    {
+        if (mb_set_addr_id(&pucFrame[1], &new_slave_addr))
+            mb_action = MB_CHANGE_ADDR;
+    }
+    // Адресный запрос. ID не проверяем
+    else
+    {
+        if (mb_set_addr(&pucFrame[1], &new_slave_addr))
+        {
+            pucFrame[1] = new_slave_addr;
+            mb_action = MB_CHANGE_ADDR;
+        }
+        else 
+            pucFrame[1] = MB_BOOT_ERR_WRONG_CONTENT;
+    }
+    
+    *usLen = 2;
+#endif    
+    return MB_EX_NONE;
+}
+
+
+// 0x43
+eMBException
+eMBSetAddrSerialCB( UCHAR * pucFrame, USHORT * usLen )
+{
+#if 0  
+    mb_package_flag = true;
+      
+    if (rcvAddress == MB_ADDRESS_BROADCAST)
+    {
+        if (mb_set_addr_serial(&pucFrame[1], &new_slave_addr))
+            mb_action = MB_CHANGE_ADDR;
+    }
+    // Адресный запрос. ID не проверяем
+    else
+    {
+        if (mb_set_addr(&pucFrame[1], &new_slave_addr))
+        {
+            pucFrame[1] = new_slave_addr;
+            mb_action = MB_CHANGE_ADDR;
+        }
+        else 
+            pucFrame[1] = MB_BOOT_ERR_WRONG_CONTENT;
+    }
+    
+    *usLen = 2;
+#endif    
+    return MB_EX_NONE;
+}
+

+ 282 - 280
fw/user/main.c

@@ -1,280 +1,282 @@
-#include "main.h"
-
-
-void init_task(void *argument);
-void test_hw_task(void *argument);
-void soft_wdt(void *params);
-void test_gpio(void *params);
-void misc_task(void *params);   // TODO перенести в другой модуль
-
-void usb_clock48m_select(usb_clk48_s clk_s);
-
-//
-int main(void)
-{
-    __disable_irq();
-    nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x08021000);
-    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
-	__enable_irq();
-  
-    //extend_SRAM();
-  
-    system_clock_config();
-
-    delay_init();
-
-    // -------------------------------------------------------------------------
-    // Debug
-    uart_print_init(115200);
-
-    
-    //usb_clock48m_select(USB_CLK_HEXT);
-    
-    crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
-          
-    
-    //printf("\n\n\n\nModule universal IO [FW %s] loading....\r\n\n", VERSION);
-        
-    //
-    //usb_init();
-    
-#if 1
-    taskENTER_CRITICAL();      
-    
-    xTaskCreate(soft_wdt, "soft_wdt", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);    
-    
-    xTaskCreate(init_task, "init_task", 10*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-    
-    xTaskCreate(test_hw_task, "hw_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-    
-    xTaskCreate(test_gpio, "gpio_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-    
-    //xTaskCreate(input_task, "input_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-    
-    xTaskCreate(misc_task, "misc_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-    
-    taskEXIT_CRITICAL();
-    
-    vTaskStartScheduler();
-#endif    
-    
-    while (1) {}
-}
-
-
-void init_task(void *argument)
-{
-    // Для теста
-    //sys_clear();
-    
-// -------------------------------------------------------------------------- //    
-// Загрузка и проверка настроек
-
-    crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
-  
-    // Мьютекс для работы с настройками
-    init_settings(); 
-    
-    // Системные настройки
-    sys_settings_load(&sys_settings);
-    
-    // Основные настройки
-    settings_load(&settings);
-    
-#if 0    
-    settings_set_all_default();
-    settings_save(&settings);
-#endif      
-// -------------------------------------------------------------------------- //    
-// Безопасный режим, входы, выходы
-
-    save_mode_init();
-    // TODO Для теста отключаем безопасный режим работы
-    save_mode_set(false);   
-/*    
-    io_port_init();
-    in_exint_init();
-    cm_gpio_init();
-*/    
-// -------------------------------------------------------------------------- //
-// Uptime    
-    
-    uptime_init();
-    //out_as_pwm();
-// -------------------------------------------------------------------------- //
-// RTC    
-    
-    TM_RTC_Init();
-    
-// -------------------------------------------------------------------------- //    
-// Мультиплексор
-    
-    mux_gpio_init();
-    
-// -------------------------------------------------------------------------- //        
-// Modbus    
-    
-    mb_init();
-    
-// -------------------------------------------------------------------------- //            
-// Базовая инициализация входов/выходов
-// TODO потом брать значения из настроек
-
-    //io_init();
-    
-// -------------------------------------------------------------------------- //        
-// Сброс счетчика попыток загрузок
-    
-    update_reset_boot_try();    
-    
-// -------------------------------------------------------------------------- //    
-// Тесты
-    //pwm_test(); // тесы PWM
-    
-// -------------------------------------------------------------------------- //    
-// RNDIS
-    
-    //usb_eth_init();
-        
-    // Тесты таймеров
-    //mux_led_test_init();
-    
-    // Тесты SPI flash
-    //common_spi_init();
-    //InitFS(PRIM_DRIVE);
-    //spi_flash_test();
-      
-    // Тесты USB
-    //usb_eth_init();
-    
-    
-      
-    //vTaskDelete(NULL);
-    
-    
-    for (;;)
-    {
-        mux_led_proc();
-    }
-
-// -----------------------------------------------------------------------------              
-    
-    //taskYIELD();
-}
-
-
-void test_hw_task(void *argument)
-{
-    for (;;)
-    {
-        vTaskDelay(100);
-        
-        mux_led_blink();
-    }
-}
-
-
-//
-void soft_wdt(void *params)
-{
-    (void)params;
-    
-    for (;;)
-    {
-        extern_wdt_togle(); // extern WDT
-        vTaskDelay(100);
-    }
-}
-
-//
-void test_gpio(void *params)
-{
-    (void)params;
-    
-    for (;;)
-    {
-        vTaskDelay(1000);
-        //io_test();
-        //out_test();
-        //load_test();
-        
-        //printf("HW rev: %u\r\n", cm_gpio_get_rev());
-        printf("Save mode: %u\r\n", save_mode_get());
-    }
-}
-
-//
-void misc_task(void *params)
-{
-    (void)params;
-    
-    for (;;)
-    {
-        vTaskDelay(1000);
-        eMBSetSlaveAddr(cm_gpio_get_mb_addr());
-    }
-}
-
-//
-void usb_clock48m_select(usb_clk48_s clk_s)
-{
-  if(clk_s == USB_CLK_HICK)
-  {
-    crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_HICK);
-
-    /* enable the acc calibration ready interrupt */
-    crm_periph_clock_enable(CRM_ACC_PERIPH_CLOCK, TRUE);
-
-    /* update the c1\c2\c3 value */
-    acc_write_c1(7980);
-    acc_write_c2(8000);
-    acc_write_c3(8020);
-
-    /* open acc calibration */
-    acc_calibration_mode_enable(ACC_CAL_HICKTRIM, TRUE);
-  }
-  else
-  {
-    switch(system_core_clock)
-    {
-      /* 48MHz */
-      case 48000000:
-        crm_usb_clock_div_set(CRM_USB_DIV_1);
-        break;
-
-      /* 72MHz */
-      case 72000000:
-        crm_usb_clock_div_set(CRM_USB_DIV_1_5);
-        break;
-
-      /* 96MHz */
-      case 96000000:
-        crm_usb_clock_div_set(CRM_USB_DIV_2);
-        break;
-
-      /* 120MHz */
-      case 120000000:
-        crm_usb_clock_div_set(CRM_USB_DIV_2_5);
-        break;
-
-      /* 144MHz */
-      case 144000000:
-        crm_usb_clock_div_set(CRM_USB_DIV_3);
-        break;
-
-      /* 168MHz */
-      case 168000000:
-        crm_usb_clock_div_set(CRM_USB_DIV_3_5);
-        break;
-
-      /* 192MHz */
-      case 192000000:
-        crm_usb_clock_div_set(CRM_USB_DIV_4);
-        break;
-
-      default:
-        break;
-
-    }
-  }
-}
+#include "main.h"
+
+
+void init_task(void *argument);
+void test_hw_task(void *argument);
+void soft_wdt(void *params);
+void test_gpio(void *params);
+void misc_task(void *params);   // TODO перенести в другой модуль
+
+void usb_clock48m_select(usb_clk48_s clk_s);
+
+//
+int main(void)
+{
+    __disable_irq();
+    nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x08021000);
+    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
+	__enable_irq();
+  
+    //extend_SRAM();
+  
+    system_clock_config();
+
+    delay_init();
+
+    // -------------------------------------------------------------------------
+    // Debug
+    uart_print_init(115200);
+
+    
+    //usb_clock48m_select(USB_CLK_HEXT);
+    
+    crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
+          
+    
+    //printf("\n\n\n\nModule universal IO [FW %s] loading....\r\n\n", VERSION);
+        
+    //
+    //usb_init();
+    
+#if 1
+    taskENTER_CRITICAL();      
+    
+    xTaskCreate(soft_wdt, "soft_wdt", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);    
+    
+    xTaskCreate(init_task, "init_task", 10*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    
+    xTaskCreate(test_hw_task, "hw_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    
+    xTaskCreate(test_gpio, "gpio_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    
+    //xTaskCreate(input_task, "input_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    
+    xTaskCreate(misc_task, "misc_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    
+    taskEXIT_CRITICAL();
+    
+    vTaskStartScheduler();
+#endif    
+    
+    while (1) {}
+}
+
+
+void init_task(void *argument)
+{
+    // Для теста
+    //sys_clear();
+    
+// -------------------------------------------------------------------------- //    
+// Загрузка и проверка настроек
+
+    crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
+  
+    // Мьютекс для работы с настройками
+    init_settings(); 
+    
+    // Системные настройки
+    sys_settings_load(&sys_settings);
+    
+    // Основные настройки
+    settings_load(&settings);
+    
+#if 0    
+    settings_set_all_default();
+    settings_save(&settings);
+#endif      
+// -------------------------------------------------------------------------- //    
+// Безопасный режим, входы, выходы
+
+    save_mode_init();
+    // TODO Для теста отключаем безопасный режим работы
+    save_mode_set(false);   
+/*    
+    io_port_init();
+    in_exint_init();
+*/    
+
+    gpio_wdt_init();
+    gpio_mbaddr_init();
+// -------------------------------------------------------------------------- //
+// Uptime    
+    
+    uptime_init();
+    //out_as_pwm();
+// -------------------------------------------------------------------------- //
+// RTC    
+    
+    TM_RTC_Init();
+    
+// -------------------------------------------------------------------------- //    
+// Мультиплексор
+    
+    mux_gpio_init();
+    
+// -------------------------------------------------------------------------- //        
+// Modbus    
+    
+    mb_init();
+    
+// -------------------------------------------------------------------------- //            
+// Базовая инициализация входов/выходов
+// TODO потом брать значения из настроек
+
+    //io_init();
+    
+// -------------------------------------------------------------------------- //        
+// Сброс счетчика попыток загрузок
+    
+    update_reset_boot_try();    
+    
+// -------------------------------------------------------------------------- //    
+// Тесты
+    //pwm_test(); // тесы PWM
+    
+// -------------------------------------------------------------------------- //    
+// RNDIS
+    
+    //usb_eth_init();
+        
+    // Тесты таймеров
+    //mux_led_test_init();
+    
+    // Тесты SPI flash
+    //common_spi_init();
+    //InitFS(PRIM_DRIVE);
+    //spi_flash_test();
+      
+    // Тесты USB
+    //usb_eth_init();
+    
+    
+      
+    //vTaskDelete(NULL);
+    
+    
+    for (;;)
+    {
+        mux_led_proc();
+    }
+
+// -----------------------------------------------------------------------------              
+    
+    //taskYIELD();
+}
+
+
+void test_hw_task(void *argument)
+{
+    for (;;)
+    {
+        vTaskDelay(100);
+        
+        //mux_led_blink();
+    }
+}
+
+
+//
+void soft_wdt(void *params)
+{
+    (void)params;
+    
+    for (;;)
+    {
+        extern_wdt_togle(); // extern WDT
+        vTaskDelay(100);
+    }
+}
+
+//
+void test_gpio(void *params)
+{
+    (void)params;
+    
+    for (;;)
+    {
+        vTaskDelay(1000);
+        //io_test();
+        //out_test();
+        //load_test();
+        
+        //printf("HW rev: %u\r\n", cm_gpio_get_rev());
+        //printf("Save mode: %u\r\n", save_mode_get());
+    }
+}
+
+//
+void misc_task(void *params)
+{
+    (void)params;
+    
+    for (;;)
+    {
+        vTaskDelay(1000);
+        eMBSetSlaveAddr(gpio_get_mbaddr());
+    }
+}
+
+//
+void usb_clock48m_select(usb_clk48_s clk_s)
+{
+  if(clk_s == USB_CLK_HICK)
+  {
+    crm_usb_clock_source_select(CRM_USB_CLOCK_SOURCE_HICK);
+
+    /* enable the acc calibration ready interrupt */
+    crm_periph_clock_enable(CRM_ACC_PERIPH_CLOCK, TRUE);
+
+    /* update the c1\c2\c3 value */
+    acc_write_c1(7980);
+    acc_write_c2(8000);
+    acc_write_c3(8020);
+
+    /* open acc calibration */
+    acc_calibration_mode_enable(ACC_CAL_HICKTRIM, TRUE);
+  }
+  else
+  {
+    switch(system_core_clock)
+    {
+      /* 48MHz */
+      case 48000000:
+        crm_usb_clock_div_set(CRM_USB_DIV_1);
+        break;
+
+      /* 72MHz */
+      case 72000000:
+        crm_usb_clock_div_set(CRM_USB_DIV_1_5);
+        break;
+
+      /* 96MHz */
+      case 96000000:
+        crm_usb_clock_div_set(CRM_USB_DIV_2);
+        break;
+
+      /* 120MHz */
+      case 120000000:
+        crm_usb_clock_div_set(CRM_USB_DIV_2_5);
+        break;
+
+      /* 144MHz */
+      case 144000000:
+        crm_usb_clock_div_set(CRM_USB_DIV_3);
+        break;
+
+      /* 168MHz */
+      case 168000000:
+        crm_usb_clock_div_set(CRM_USB_DIV_3_5);
+        break;
+
+      /* 192MHz */
+      case 192000000:
+        crm_usb_clock_div_set(CRM_USB_DIV_4);
+        break;
+
+      default:
+        break;
+
+    }
+  }
+}

+ 372 - 371
iap/modules/modbus/modbus.c

@@ -1,371 +1,372 @@
-#include "at32f403a_407.h"
-#include "common_config.h"
-#include "modbus.h"
-#include "modbus_params.h"
-#include "FreeRTOS.h"
-#include "task.h"
-#include "mb.h"
-#include "mbport.h"
-#include "mbrtu.h"
-#include "tim_delay.h"
-#include "settings_api.h"
-#include "iap.h"
-#include <stdio.h>
-#include <string.h>
-#include <stdbool.h>
-
-#define REG_HOLDING_START		( 1 )
-#define REG_HOLDING_NREGS		( 5 )
-
-
-
-static USHORT	usRegHoldingStart = REG_HOLDING_START;
-extern UCHAR    rcvAddress;
-
-//osThreadId modbus_task_handle;
-static void modbus_task(void *params);
-//osThreadId modbus_params_handle;
-static void modbus_params(void *params);
-
-//osTimerId reset_timer_handle;
-//osTimerId settings_timer_handle;
-//osTimerId modbus_timer_handle;
-
-mb_delay_action_t mb_action = MB_NO_ACTION;
-uint8_t new_slave_addr;
-bool mb_package_flag = false;
-
-
-
-//
-void mb_init(void)
-{
-    uint32_t baud;
-    eMBParity par;
-    UCHAR mb_addr = cm_gpio_get_mb_addr();
-    unsigned int stop_bits = 1;
-	const UCHAR ucSlaveID[] = {0xAA, 0xBB, 0xCC};
-
-    iap_init();
-    
-	mb_init_params();
-
-    settings_set_modbus_params(settings.com_settings.mb_port);
-
-    baud = settings_get_mb_baud(&mb_port_settings);
-    par = settings_get_mb_par(&mb_port_settings);
-    
-    mb_helper_tim_init(baud);
-    eMBInit(MB_RTU, mb_addr, 4, baud, par, stop_bits);
-	eMBSetSlaveID(0x34, TRUE, ucSlaveID, 3);
-	eMBEnable();
-    
-    
-    xTaskCreate(modbus_task, "modbus_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-    
-    xTaskCreate(modbus_params, "modbus_params", 4*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-    
-    
-#if 0    
-	osTimerDef(vResetTimer, modbus_reset);
-	reset_timer_handle = osTimerCreate(osTimer(vResetTimer), osTimerOnce, NULL);
-    
-    osTimerDef(vSettingsTimer, settings_timer);
-    settings_timer_handle = osTimerCreate(osTimer(vSettingsTimer), osTimerOnce, NULL);
-    
-    osTimerDef(vModbusTimer, modbus_port_timer);
-    modbus_timer_handle = osTimerCreate(osTimer(vModbusTimer), osTimerOnce, NULL);
-#endif    
-}
-
-//
-void modbus_task(void *params)
-{
-	(void)params;
-
-	for (;;)
-	{
-		eMBPoll();
-	}
-
-}
-
-//
-void modbus_params(void *params)
-{
-    for (;;)
-    {
-        if (mb_action != MB_NO_ACTION)
-        {
-            switch (mb_action)
-            {
-                // Установка нового адреса
-                case MB_CHANGE_ADDR :
-    
-                    //settings.com_settings.mb_addr = new_slave_addr;  
-                    eMBSetSlaveAddr(new_slave_addr);
-                      
-                break;
-                
-                case MB_CHANGE_PORT_SETTINGS :
-#if 0                  
-                    osDelay(200);
-                    eMBRTUStop();
-                    settings_set_modbus_params(temp_modbus_port);
-                    settings_init_mb_port(settings.com_settings.mb_addr);
-                    MBDBG settings_print_modbus();
-                    eMBRTUStart();
-                    eMBEnable();
-                    mb_package_flag = false;
-                    osTimerStart(modbus_timer_handle, 9800);
-#endif                  
-                break;
-                
-                // Полное восстановление дефолтных настроек
-                case MB_DEF_SETTINGS :
-#if 0
-                    settings_set_default(true);
-                    settings_save();
-#endif                  
-                break;  
-                
-                // Частичное восстановление дефолтных настроек
-                case MB_PART_DEF_SETTINGS :
-#if 0
-                    settings_set_default(false);
-                    settings_save();
-#endif                  
-                break;
-                
-                  
-                case MB_SAVE_SETTINGS :
-#if 0                
-                    settings_save();
-#endif                  
-                break;
-                
-                case MB_SAVE_SYS_SETTINGS :
-#if 0
-                    if (set_sys_settings_flag) {
-                        memcpy(&sys_settings, &temp_sys_settings, sizeof(sys_settings));
-                        sys_settings_save();
-                    }
-#endif                  
-                break;
-                
-                case MB_RESET :
-                  
-                    vTaskDelay(100);
-                    NVIC_SystemReset();
-                  
-                break;
-                
-                default : break;
-            }
-            
-            mb_action = MB_NO_ACTION;
-        }
-        vTaskDelay(10);
-    }
-}
-
-
-// Отложенная перезагрузка в Bootloader
-void modbus_reset(void const * params)
-{
-#if 0  
-	MBDBG printf("Switch to bootloader!\r\n");
-	rtc_set_load_mode(1);
-	HAL_NVIC_SystemReset();
-#endif    
-}
-
-
-// Запуск таймера на изменение системных настроек
-void settings_timer(void const * params)
-{
-#if 0  
-    set_sys_settings_flag = false;
-    psw_ok = false;
-#endif    
-}
-
-
-// Запуск таймера для изменение настроек modbus
-void modbus_port_timer(void const * params)
-{
-#if 0
-    eMBRTUStop();
-  
-    // Были транзакции по modbus с новыми настройками. 
-    // Нужно сохранить новые настройки.
-    if (mb_package_flag)
-    {
-        settings.com_settings.mb_port = temp_modbus_port;
-        settings_set_modbus_params(settings.com_settings.mb_port);
-        settings_init_mb_port(settings.com_settings.mb_addr);
-        settings_save();
-        
-    }
-    // Нужно вернуть старые настройки
-    else
-    {
-        temp_modbus_port = settings.com_settings.mb_port;
-        settings_set_modbus_params(settings.com_settings.mb_port);
-        settings_init_mb_port(settings.com_settings.mb_addr);
-    }
-  
-    
-    MBDBG settings_print_modbus();
-    eMBRTUStart();
-    eMBEnable();
-#endif    
-}
-
-// ------------------------------------------------------------------- //
-//						Modbus callback's
-// ------------------------------------------------------------------- //
-
-
-// 04 (0x04) Read Input Registers	 
-eMBErrorCode
-eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
-{
-    return MB_ENOREG;
-}
-
-
-// 03 (0x03) Read Holding Registers
-// 16 (0x10) Write Multiple registers
-#if 0
-eMBErrorCode
-eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
-{
-    return MB_ENOREG;
-}
-#endif
-
-// 17 (0x11) Report Slave ID (Serial Line only) 
-eMBException
-eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen )
-{
-    return MB_EX_ILLEGAL_FUNCTION;
-}
-
-
-// чтение/запись значений из нескольких регистров флагов (Coil Status).
-// чтения N регистров параметров
-eMBErrorCode
-eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
-{
-    return MB_ENOREG;
-}
-
-eMBErrorCode
-eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
-{
-    return MB_ENOREG;
-}
-
-// 0x42
-eMBException    
-eMBSetAddrIdCB( UCHAR * pucFrame, USHORT * usLen )
-{
-   
-    return MB_EX_ILLEGAL_FUNCTION;
-}
-
-
-// 0x43
-eMBException
-eMBSetAddrSerialCB( UCHAR * pucFrame, USHORT * usLen )
-{
-    return MB_EX_ILLEGAL_FUNCTION;
-}
-
-// 03 (0x03) Read Holding Registers
-// чтение N регистров управления и уставок
-// 16 (0x10) Write Multiple registers
-// запись N регистров управления и уставок (0x10)
-eMBErrorCode
-eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
-{
-	eMBErrorCode eStatus = MB_ENOERR;
-
-	int			 iRegIndex;
-    
-    mb_delay_action_t mb_act = MB_NO_ACTION;
-	uint16_t regs_cnt = usNRegs;
-	uint16_t index_param;
-	uint8_t *ptr_buf = pucRegBuffer;
-	uint16_t size;
-#if 0
-	printf("usAddress: 0x%X\r\n", usAddress);
-	printf("usNRegs: %u\r\n", usNRegs);
-	printf("eMode: %u\r\n", eMode);
-    printf("pucRegBuffer :%X\r\n", pucRegBuffer[0]);
-#endif
-    mb_package_flag = true;
-    
-	iRegIndex = (int)(usAddress - usRegHoldingStart);
-
-#if 1    
-	switch (eMode)
-	{
-		case MB_REG_READ :
-
-			while (regs_cnt)
-			{
-				// Поиск регистра
-				if (!mb_find_param(iRegIndex, &index_param, &size))
-				{
-					return MB_ENOREG; // нет запрашиваемого регистра
-				}
-				else
-				{
-					MBDBG printf("reg: %X, index: %u, size: %u\r\n", iRegIndex, index_param, size);
-                    MBDBG printf("reg: %X\r\n", iRegIndex);
-					mb_get_param(ptr_buf,  index_param); // Вызов функции записи
-					iRegIndex += size;
-					ptr_buf += 2*size;
-					regs_cnt -= size;
-				}
-			}
-
-
-		break;
-
-		case MB_REG_WRITE :
-
-			while (regs_cnt)
-			{
-				// Поиск регистра
-				if (!mb_find_param(iRegIndex, &index_param, &size))
-				{
-					return MB_ENOREG; // нет запрашиваемого регистра
-				}
-				else
-				{
-					MBDBG printf("reg: %X, index: %u, size: %u\r\n", iRegIndex, index_param, size);
-					mb_act = mb_set_param(ptr_buf, index_param); // Вызов функции записи
-
-                    if (mb_act != MB_NO_ACTION)
-                        mb_action = mb_act;
-                    
-					iRegIndex += size;
-					ptr_buf += 2*size;
-					regs_cnt -= size;
-                    
-                    if (mb_act == MB_PAS_ERR)
-                        return MB_EINVAL;
-				}
-			}
-
-		break;
-	}
-#endif
-	return eStatus;
-}
-
-
-
+#include "at32f403a_407.h"
+#include "common_config.h"
+#include "modbus.h"
+#include "modbus_params.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "mb.h"
+#include "mbport.h"
+#include "mbrtu.h"
+#include "tim_delay.h"
+#include "settings_api.h"
+#include "iap.h"
+#include "common_gpio.h"
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+
+#define REG_HOLDING_START		( 1 )
+#define REG_HOLDING_NREGS		( 5 )
+
+
+
+static USHORT	usRegHoldingStart = REG_HOLDING_START;
+extern UCHAR    rcvAddress;
+
+//osThreadId modbus_task_handle;
+static void modbus_task(void *params);
+//osThreadId modbus_params_handle;
+static void modbus_params(void *params);
+
+//osTimerId reset_timer_handle;
+//osTimerId settings_timer_handle;
+//osTimerId modbus_timer_handle;
+
+mb_delay_action_t mb_action = MB_NO_ACTION;
+uint8_t new_slave_addr;
+bool mb_package_flag = false;
+
+
+
+//
+void mb_init(void)
+{
+    uint32_t baud;
+    eMBParity par;
+    UCHAR mb_addr = gpio_get_mbaddr();
+    unsigned int stop_bits = 1;
+	const UCHAR ucSlaveID[] = {0xAA, 0xBB, 0xCC};
+
+    iap_init();
+    
+	mb_init_params();
+
+    settings_set_modbus_params(settings.com_settings.mb_port);
+
+    baud = settings_get_mb_baud(&mb_port_settings);
+    par = settings_get_mb_par(&mb_port_settings);
+    
+    mb_helper_tim_init(baud);
+    eMBInit(MB_RTU, mb_addr, 4, baud, par, stop_bits);
+	eMBSetSlaveID(0x34, TRUE, ucSlaveID, 3);
+	eMBEnable();
+    
+    
+    xTaskCreate(modbus_task, "modbus_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    
+    xTaskCreate(modbus_params, "modbus_params", 4*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    
+    
+#if 0    
+	osTimerDef(vResetTimer, modbus_reset);
+	reset_timer_handle = osTimerCreate(osTimer(vResetTimer), osTimerOnce, NULL);
+    
+    osTimerDef(vSettingsTimer, settings_timer);
+    settings_timer_handle = osTimerCreate(osTimer(vSettingsTimer), osTimerOnce, NULL);
+    
+    osTimerDef(vModbusTimer, modbus_port_timer);
+    modbus_timer_handle = osTimerCreate(osTimer(vModbusTimer), osTimerOnce, NULL);
+#endif    
+}
+
+//
+void modbus_task(void *params)
+{
+	(void)params;
+
+	for (;;)
+	{
+		eMBPoll();
+	}
+
+}
+
+//
+void modbus_params(void *params)
+{
+    for (;;)
+    {
+        if (mb_action != MB_NO_ACTION)
+        {
+            switch (mb_action)
+            {
+                // Установка нового адреса
+                case MB_CHANGE_ADDR :
+    
+                    //settings.com_settings.mb_addr = new_slave_addr;  
+                    eMBSetSlaveAddr(new_slave_addr);
+                      
+                break;
+                
+                case MB_CHANGE_PORT_SETTINGS :
+#if 0                  
+                    osDelay(200);
+                    eMBRTUStop();
+                    settings_set_modbus_params(temp_modbus_port);
+                    settings_init_mb_port(settings.com_settings.mb_addr);
+                    MBDBG settings_print_modbus();
+                    eMBRTUStart();
+                    eMBEnable();
+                    mb_package_flag = false;
+                    osTimerStart(modbus_timer_handle, 9800);
+#endif                  
+                break;
+                
+                // Полное восстановление дефолтных настроек
+                case MB_DEF_SETTINGS :
+#if 0
+                    settings_set_default(true);
+                    settings_save();
+#endif                  
+                break;  
+                
+                // Частичное восстановление дефолтных настроек
+                case MB_PART_DEF_SETTINGS :
+#if 0
+                    settings_set_default(false);
+                    settings_save();
+#endif                  
+                break;
+                
+                  
+                case MB_SAVE_SETTINGS :
+#if 0                
+                    settings_save();
+#endif                  
+                break;
+                
+                case MB_SAVE_SYS_SETTINGS :
+#if 0
+                    if (set_sys_settings_flag) {
+                        memcpy(&sys_settings, &temp_sys_settings, sizeof(sys_settings));
+                        sys_settings_save();
+                    }
+#endif                  
+                break;
+                
+                case MB_RESET :
+                  
+                    vTaskDelay(100);
+                    NVIC_SystemReset();
+                  
+                break;
+                
+                default : break;
+            }
+            
+            mb_action = MB_NO_ACTION;
+        }
+        vTaskDelay(10);
+    }
+}
+
+
+// Отложенная перезагрузка в Bootloader
+void modbus_reset(void const * params)
+{
+#if 0  
+	MBDBG printf("Switch to bootloader!\r\n");
+	rtc_set_load_mode(1);
+	HAL_NVIC_SystemReset();
+#endif    
+}
+
+
+// Запуск таймера на изменение системных настроек
+void settings_timer(void const * params)
+{
+#if 0  
+    set_sys_settings_flag = false;
+    psw_ok = false;
+#endif    
+}
+
+
+// Запуск таймера для изменение настроек modbus
+void modbus_port_timer(void const * params)
+{
+#if 0
+    eMBRTUStop();
+  
+    // Были транзакции по modbus с новыми настройками. 
+    // Нужно сохранить новые настройки.
+    if (mb_package_flag)
+    {
+        settings.com_settings.mb_port = temp_modbus_port;
+        settings_set_modbus_params(settings.com_settings.mb_port);
+        settings_init_mb_port(settings.com_settings.mb_addr);
+        settings_save();
+        
+    }
+    // Нужно вернуть старые настройки
+    else
+    {
+        temp_modbus_port = settings.com_settings.mb_port;
+        settings_set_modbus_params(settings.com_settings.mb_port);
+        settings_init_mb_port(settings.com_settings.mb_addr);
+    }
+  
+    
+    MBDBG settings_print_modbus();
+    eMBRTUStart();
+    eMBEnable();
+#endif    
+}
+
+// ------------------------------------------------------------------- //
+//						Modbus callback's
+// ------------------------------------------------------------------- //
+
+
+// 04 (0x04) Read Input Registers	 
+eMBErrorCode
+eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
+{
+    return MB_ENOREG;
+}
+
+
+// 03 (0x03) Read Holding Registers
+// 16 (0x10) Write Multiple registers
+#if 0
+eMBErrorCode
+eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
+{
+    return MB_ENOREG;
+}
+#endif
+
+// 17 (0x11) Report Slave ID (Serial Line only) 
+eMBException
+eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen )
+{
+    return MB_EX_ILLEGAL_FUNCTION;
+}
+
+
+// чтение/запись значений из нескольких регистров флагов (Coil Status).
+// чтения N регистров параметров
+eMBErrorCode
+eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
+{
+    return MB_ENOREG;
+}
+
+eMBErrorCode
+eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
+{
+    return MB_ENOREG;
+}
+
+// 0x42
+eMBException    
+eMBSetAddrIdCB( UCHAR * pucFrame, USHORT * usLen )
+{
+   
+    return MB_EX_ILLEGAL_FUNCTION;
+}
+
+
+// 0x43
+eMBException
+eMBSetAddrSerialCB( UCHAR * pucFrame, USHORT * usLen )
+{
+    return MB_EX_ILLEGAL_FUNCTION;
+}
+
+// 03 (0x03) Read Holding Registers
+// чтение N регистров управления и уставок
+// 16 (0x10) Write Multiple registers
+// запись N регистров управления и уставок (0x10)
+eMBErrorCode
+eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
+{
+	eMBErrorCode eStatus = MB_ENOERR;
+
+	int			 iRegIndex;
+    
+    mb_delay_action_t mb_act = MB_NO_ACTION;
+	uint16_t regs_cnt = usNRegs;
+	uint16_t index_param;
+	uint8_t *ptr_buf = pucRegBuffer;
+	uint16_t size;
+#if 0
+	printf("usAddress: 0x%X\r\n", usAddress);
+	printf("usNRegs: %u\r\n", usNRegs);
+	printf("eMode: %u\r\n", eMode);
+    printf("pucRegBuffer :%X\r\n", pucRegBuffer[0]);
+#endif
+    mb_package_flag = true;
+    
+	iRegIndex = (int)(usAddress - usRegHoldingStart);
+
+#if 1    
+	switch (eMode)
+	{
+		case MB_REG_READ :
+
+			while (regs_cnt)
+			{
+				// Поиск регистра
+				if (!mb_find_param(iRegIndex, &index_param, &size))
+				{
+					return MB_ENOREG; // нет запрашиваемого регистра
+				}
+				else
+				{
+					MBDBG printf("reg: %X, index: %u, size: %u\r\n", iRegIndex, index_param, size);
+                    MBDBG printf("reg: %X\r\n", iRegIndex);
+					mb_get_param(ptr_buf,  index_param); // Вызов функции записи
+					iRegIndex += size;
+					ptr_buf += 2*size;
+					regs_cnt -= size;
+				}
+			}
+
+
+		break;
+
+		case MB_REG_WRITE :
+
+			while (regs_cnt)
+			{
+				// Поиск регистра
+				if (!mb_find_param(iRegIndex, &index_param, &size))
+				{
+					return MB_ENOREG; // нет запрашиваемого регистра
+				}
+				else
+				{
+					MBDBG printf("reg: %X, index: %u, size: %u\r\n", iRegIndex, index_param, size);
+					mb_act = mb_set_param(ptr_buf, index_param); // Вызов функции записи
+
+                    if (mb_act != MB_NO_ACTION)
+                        mb_action = mb_act;
+                    
+					iRegIndex += size;
+					ptr_buf += 2*size;
+					regs_cnt -= size;
+                    
+                    if (mb_act == MB_PAS_ERR)
+                        return MB_EINVAL;
+				}
+			}
+
+		break;
+	}
+#endif
+	return eStatus;
+}
+
+
+

+ 154 - 147
iap/user/main.c

@@ -1,148 +1,155 @@
-#include "main.h"
-
-
-void soft_wdt(void *params);
-void init_task(void *params);
-bool jump_to_app(uint32_t address);
-void (*pftarget)(void);
-
-bool boot_failed = false;
-
-
-
-int main()
-{
-    uint8_t load_mode;
-	uint8_t boot_try;
-	uint8_t fw_invalid = 0;
-    
-    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
-    
-    crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
-    crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);
-    pwc_battery_powered_domain_access(TRUE);
-    
-    system_clock_config();
-    
-    delay_init();
-
-    uart_print_init(115200);
-    
-    // Флаг - нужно ли обновляться
-    load_mode = bpr_data_read(BPR_DATA1);
-    
-    // Число попыток загрузки основного ПО
-    boot_try = bpr_data_read(BPR_DATA2);
-    
-    DBG printf("[IAP] load_mode: %u, boot_try: %u\r\n", load_mode, boot_try);
-#if 1    
-    // Если есть попытки, то пытаемся загрузить FW.
-	// Используем ограниченное число попыток загрузить основное FW.
-    if (boot_try > 1)
-    {
-        boot_try--;
-        bpr_data_write(BPR_DATA2, boot_try);
-        
-        if (!jump_to_app(USER_FLASH_FIRST_PAGE_ADDRESS))
-        {
-            // Флеш пустая. Нечего загружать
-            fw_invalid = 1;
-            boot_failed = false;
-            DBG printf("FW empty! Bootloader starting...\n\r");
-        }
-    }
-    else if (boot_try == 1)
-    {
-        boot_failed = true;
-        DBG printf("FW boot failed. Bootloader starting...\r\n");
-        
-        load_mode = 1;
-        boot_try = 0;
-        bpr_data_write(BPR_DATA1, load_mode);
-        bpr_data_write(BPR_DATA2, boot_try);
-    }
-    
-    if  ((load_mode == 0) && (!fw_invalid))
-    {
-        DBG printf("Run main FW\r\n");
-        bpr_data_write(BPR_DATA2, BOOT_TRY);
-        DBG printf("Go to main FW...\r\n");
-        jump_to_app(USER_FLASH_FIRST_PAGE_ADDRESS);
-    }
-#endif    
-        
-    DBG printf("IAP starting...\r\n");
-
-#if 0    
-    // -------------------------------------------------------------------------
-    // Для теста. Сброс.
-    DBG printf("For test reset load_mode flag and jump to FW\r\n");
-    bpr_data_write(BPR_DATA1, 0);
-    jump_to_app(0x08021000); 
-#endif
-    
-    //
-    cm_gpio_init();
-
-    taskENTER_CRITICAL();      
-    
-    xTaskCreate(soft_wdt, "soft_wdt", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);    
-    
-    xTaskCreate(init_task, "init_task", 10*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-    
-    taskEXIT_CRITICAL();
-    
-    vTaskStartScheduler();
-
-    while (1) {}
-}
-
-
-// 
-bool jump_to_app(uint32_t address)
-{
-    uint32_t stkptr, jumpaddr;
-    stkptr = *(uint32_t *)address;
-    jumpaddr = *(uint32_t *)(address + sizeof(uint32_t));
- 
-    //if (((*(__IO uint32_t*)USER_FLASH_FIRST_PAGE_ADDRESS) & 0x20000000) == 0x20000000) 
-    if  (*(__IO uint32_t*)USER_FLASH_FIRST_PAGE_ADDRESS != 0xFFFFFFFF) 
-    {
-        __set_MSP(stkptr);
-        pftarget = (void (*) (void))jumpaddr;
-        pftarget();
-    }
-    return false;
-}
-
-
-//
-void soft_wdt(void *params)
-{
-    (void)params;
-    
-    for (;;)
-    {
-        extern_wdt_togle(); // extern WDT
-        vTaskDelay(500);
-    }
-}
-
-//
-void init_task(void *params)
-{
-    (void)params;
-    
-    crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
-    
-    init_settings();
-    settings_load(&settings);    
-    
-    mb_init();
-    
-    for (;;)
-    {
-      
-        vTaskDelay(500);
-    }
+#include "main.h"
+
+
+void soft_wdt(void *params);
+void init_task(void *params);
+bool jump_to_app(uint32_t address);
+void (*pftarget)(void);
+
+bool boot_failed = false;
+
+
+
+int main()
+{
+    uint8_t load_mode;
+	uint8_t boot_try;
+	uint8_t fw_invalid = 0;
+    
+    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
+    
+    crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
+    crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
+    crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
+    crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
+    crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
+    
+    crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
+    crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
+    crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);
+    pwc_battery_powered_domain_access(TRUE);
+    
+    system_clock_config();
+    
+    delay_init();
+
+    uart_print_init(115200);
+    
+    // Флаг - нужно ли обновляться
+    load_mode = bpr_data_read(BPR_DATA1);
+    
+    // Число попыток загрузки основного ПО
+    boot_try = bpr_data_read(BPR_DATA2);
+    
+    DBG printf("[IAP] load_mode: %u, boot_try: %u\r\n", load_mode, boot_try);
+#if 1    
+    // Если есть попытки, то пытаемся загрузить FW.
+	// Используем ограниченное число попыток загрузить основное FW.
+    if (boot_try > 1)
+    {
+        boot_try--;
+        bpr_data_write(BPR_DATA2, boot_try);
+        
+        if (!jump_to_app(USER_FLASH_FIRST_PAGE_ADDRESS))
+        {
+            // Флеш пустая. Нечего загружать
+            fw_invalid = 1;
+            boot_failed = false;
+            DBG printf("FW empty! Bootloader starting...\n\r");
+        }
+    }
+    else if (boot_try == 1)
+    {
+        boot_failed = true;
+        DBG printf("FW boot failed. Bootloader starting...\r\n");
+        
+        load_mode = 1;
+        boot_try = 0;
+        bpr_data_write(BPR_DATA1, load_mode);
+        bpr_data_write(BPR_DATA2, boot_try);
+    }
+    
+    if  ((load_mode == 0) && (!fw_invalid))
+    {
+        DBG printf("Run main FW\r\n");
+        bpr_data_write(BPR_DATA2, BOOT_TRY);
+        DBG printf("Go to main FW...\r\n");
+        jump_to_app(USER_FLASH_FIRST_PAGE_ADDRESS);
+    }
+#endif    
+        
+    DBG printf("IAP starting...\r\n");
+
+#if 0    
+    // -------------------------------------------------------------------------
+    // Для теста. Сброс.
+    DBG printf("For test reset load_mode flag and jump to FW\r\n");
+    bpr_data_write(BPR_DATA1, 0);
+    jump_to_app(0x08021000); 
+#endif
+    
+    gpio_wdt_init();
+    gpio_mbaddr_init();
+
+    taskENTER_CRITICAL();      
+    
+    xTaskCreate(soft_wdt, "soft_wdt", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);    
+    
+    xTaskCreate(init_task, "init_task", 10*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    
+    taskEXIT_CRITICAL();
+    
+    vTaskStartScheduler();
+
+    while (1) {}
+}
+
+
+// 
+bool jump_to_app(uint32_t address)
+{
+    uint32_t stkptr, jumpaddr;
+    stkptr = *(uint32_t *)address;
+    jumpaddr = *(uint32_t *)(address + sizeof(uint32_t));
+ 
+    //if (((*(__IO uint32_t*)USER_FLASH_FIRST_PAGE_ADDRESS) & 0x20000000) == 0x20000000) 
+    if  (*(__IO uint32_t*)USER_FLASH_FIRST_PAGE_ADDRESS != 0xFFFFFFFF) 
+    {
+        __set_MSP(stkptr);
+        pftarget = (void (*) (void))jumpaddr;
+        pftarget();
+    }
+    return false;
+}
+
+
+//
+void soft_wdt(void *params)
+{
+    (void)params;
+    
+    for (;;)
+    {
+        extern_wdt_togle(); // extern WDT
+        vTaskDelay(500);
+    }
+}
+
+//
+void init_task(void *params)
+{
+    (void)params;
+    
+    crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
+    
+    init_settings();
+    settings_load(&settings);    
+    
+    mb_init();
+    
+    for (;;)
+    {
+      
+        vTaskDelay(500);
+    }
 }

BIN
output/fw.bin


Filskillnaden har hållts tillbaka eftersom den är för stor
+ 483 - 476
project/ewarm/iap/iap.dep


Filskillnaden har hållts tillbaka eftersom den är för stor
+ 966 - 973
project/ewarm/module_universal_io.dep


+ 21 - 21
shared/peripherals/inc/common_gpio.h

@@ -1,21 +1,21 @@
-#ifndef __COMMON_GPIO_H
-#define __COMMON_GPIO_H
-
-//
-void cm_gpio_init(void);
-
-//
-void cm_gpio_addr_init(void);
-
-//
-uint8_t cm_gpio_get_mb_addr(void);
-
-// Ревизия платы
-uint8_t cm_gpio_get_rev(void);
-
-//
-void extern_wdt_togle(void);
-
-
-#endif  // __COMMON_GPIO_H
-
+#ifndef __COMMON_GPIO_H
+#define __COMMON_GPIO_H
+
+//
+void gpio_wdt_init(void);
+
+//
+void gpio_mbaddr_init(void);
+
+//
+uint8_t gpio_get_mbaddr(void);
+
+// Ревизия платы
+uint8_t gpio_get_rev(void);
+
+//
+void extern_wdt_togle(void);
+
+
+#endif  // __COMMON_GPIO_H
+

+ 118 - 120
shared/peripherals/src/common_gpio.c

@@ -1,121 +1,119 @@
-#include "at32f403a_407.h"
-#include "common_gpio.h"
-#include "FreeRTOS.h"
-#include "task.h"
-#include "io.h"
-#include <stdbool.h>
-
-
-#define ADDR_PIN_NUM    4
-
-#define HW_REV_PIN_NUM  3
-
-//
-din_t addr_in[ADDR_PIN_NUM] = {
-    {GPIOC, GPIO_PINS_4, 0, 0, false, 0}, // 1   
-    {GPIOA, GPIO_PINS_7, 0, 0, false, 0}, // 2
-    {GPIOA, GPIO_PINS_6, 0, 0, false, 0}, // 3 
-    {GPIOA, GPIO_PINS_5, 0, 0, false, 0}, // 4 
-};
-
-//
-din_t rev_in[ADDR_PIN_NUM] = {
-    {GPIOB, GPIO_PINS_13, 0, 0, false, 0}, // 1   
-    {GPIOB, GPIO_PINS_14, 0, 0, false, 0}, // 2
-    {GPIOB, GPIO_PINS_15, 0, 0, false, 0}, // 3 
-};
-
-
-
-//
-void cm_gpio_init(void)
-{
-    gpio_init_type gpio_initstructure;
-        
-    crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
-    
-    // WDT
-    gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
-    gpio_initstructure.gpio_pull           = GPIO_PULL_NONE;  
-    gpio_initstructure.gpio_mode           = GPIO_MODE_OUTPUT;  
-    gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
-    gpio_initstructure.gpio_pins           = GPIO_PINS_7;
-    gpio_init(GPIOC, &gpio_initstructure); 
-    
-    cm_gpio_addr_init();
-}
-
-// Пины переключателя modbus-адреса
-void cm_gpio_addr_init(void)
-{
-    gpio_init_type gpio_init_struct;
-
-    gpio_default_para_init(&gpio_init_struct);
-        
-    for (uint8_t i = 0; i < ADDR_PIN_NUM; i++)
-    {
-        gpio_init_struct.gpio_pull           = GPIO_PULL_UP;  
-        gpio_init_struct.gpio_mode           = GPIO_MODE_INPUT;  
-        gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
-        gpio_init_struct.gpio_pins           = addr_in[i].pin ;
-        gpio_init(addr_in[i].port, &gpio_init_struct);
-    }
-}
-
-//
-uint8_t cm_gpio_get_mb_addr(void)
-{
-    uint8_t ret = 0;
-    
-    for (uint8_t i = 0; i < ADDR_PIN_NUM; i++)
-    {
-        ret |= (uint8_t)gpio_input_data_bit_read(addr_in[i].port, addr_in[i].pin) << i;
-    }
-    
-    ret = ret > 1 ? ret : 1;
-    
-    return ret;
-}
-
-// Ревизия платы
-uint8_t cm_gpio_get_rev(void)
-{
-    uint8_t ret = 0;
-    
-    gpio_init_type gpio_init_struct;
-
-    gpio_default_para_init(&gpio_init_struct);
-        
-    for (uint8_t i = 0; i < HW_REV_PIN_NUM; i++)
-    {
-        gpio_init_struct.gpio_pull           = GPIO_PULL_UP;  
-        gpio_init_struct.gpio_mode           = GPIO_MODE_INPUT;  
-        gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
-        gpio_init_struct.gpio_pins           = rev_in[i].pin ;
-        gpio_init(rev_in[i].port, &gpio_init_struct);
-    }
-    
-    for (uint8_t i = 0; i < HW_REV_PIN_NUM; i++)
-    {
-        ret |= (uint8_t)gpio_input_data_bit_read(rev_in[i].port, rev_in[i].pin) << i;
-    }
-    
-    ret = ret > 1 ? ret : 1;
-    
-    return ret;
-}
-
-//
-void extern_wdt_togle(void)
-{
-    static bool toogle = false;
-    
-    if (!toogle) {
-        toogle = true;
-        gpio_bits_set(GPIOC, GPIO_PINS_7);
-    }
-    else {
-        toogle = false;
-        gpio_bits_reset(GPIOC, GPIO_PINS_7);
-    }
+#include "at32f403a_407.h"
+#include "common_gpio.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "io.h"
+#include <stdbool.h>
+
+
+#define ADDR_PIN_NUM    4
+
+#define HW_REV_PIN_NUM  3
+
+//
+din_t addr_in[ADDR_PIN_NUM] = {
+    {GPIOC, GPIO_PINS_4, 0, 0, false, 0}, // 1   
+    {GPIOA, GPIO_PINS_7, 0, 0, false, 0}, // 2
+    {GPIOA, GPIO_PINS_6, 0, 0, false, 0}, // 3 
+    {GPIOA, GPIO_PINS_5, 0, 0, false, 0}, // 4 
+};
+
+//
+din_t rev_in[ADDR_PIN_NUM] = {
+    {GPIOB, GPIO_PINS_13, 0, 0, false, 0}, // 1   
+    {GPIOB, GPIO_PINS_14, 0, 0, false, 0}, // 2
+    {GPIOB, GPIO_PINS_15, 0, 0, false, 0}, // 3 
+};
+
+
+
+//
+void gpio_wdt_init(void)
+{
+    gpio_init_type gpio_initstructure;
+        
+    crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
+    
+    // WDT
+    gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
+    gpio_initstructure.gpio_pull           = GPIO_PULL_NONE;  
+    gpio_initstructure.gpio_mode           = GPIO_MODE_OUTPUT;  
+    gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
+    gpio_initstructure.gpio_pins           = GPIO_PINS_7;
+    gpio_init(GPIOC, &gpio_initstructure); 
+}
+
+// Пины переключателя modbus-адреса
+void gpio_mbaddr_init(void)
+{
+    gpio_init_type gpio_init_struct;
+
+    gpio_default_para_init(&gpio_init_struct);
+        
+    for (uint8_t i = 0; i < ADDR_PIN_NUM; i++)
+    {
+        gpio_init_struct.gpio_pull           = GPIO_PULL_UP;  
+        gpio_init_struct.gpio_mode           = GPIO_MODE_INPUT;  
+        gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
+        gpio_init_struct.gpio_pins           = addr_in[i].pin ;
+        gpio_init(addr_in[i].port, &gpio_init_struct);
+    }
+}
+
+//
+uint8_t gpio_get_mbaddr(void)
+{
+    uint8_t ret = 0;
+    
+    for (uint8_t i = 0; i < ADDR_PIN_NUM; i++)
+    {
+        ret |= (uint8_t)gpio_input_data_bit_read(addr_in[i].port, addr_in[i].pin) << i;
+    }
+    
+    ret = ret > 1 ? ret : 1;
+    
+    return ret;
+}
+
+// Ревизия платы
+uint8_t gpio_get_rev(void)
+{
+    uint8_t ret = 0;
+    
+    gpio_init_type gpio_init_struct;
+
+    gpio_default_para_init(&gpio_init_struct);
+        
+    for (uint8_t i = 0; i < HW_REV_PIN_NUM; i++)
+    {
+        gpio_init_struct.gpio_pull           = GPIO_PULL_UP;  
+        gpio_init_struct.gpio_mode           = GPIO_MODE_INPUT;  
+        gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
+        gpio_init_struct.gpio_pins           = rev_in[i].pin ;
+        gpio_init(rev_in[i].port, &gpio_init_struct);
+    }
+    
+    for (uint8_t i = 0; i < HW_REV_PIN_NUM; i++)
+    {
+        ret |= (uint8_t)gpio_input_data_bit_read(rev_in[i].port, rev_in[i].pin) << i;
+    }
+    
+    ret = ret > 1 ? ret : 1;
+    
+    return ret;
+}
+
+//
+void extern_wdt_togle(void)
+{
+    static bool toogle = false;
+    
+    if (!toogle) {
+        toogle = true;
+        gpio_bits_set(GPIOC, GPIO_PINS_7);
+    }
+    else {
+        toogle = false;
+        gpio_bits_reset(GPIOC, GPIO_PINS_7);
+    }
 }

Vissa filer visades inte eftersom för många filer har ändrats