Pārlūkot izejas kodu

Рефакторинг. Избавляюсь от лишнего.

TelenkovDmitry 8 mēneši atpakaļ
vecāks
revīzija
7274d0a3af

+ 21 - 1
fw/modules/io/mux.c

@@ -4,6 +4,10 @@
 #include "task.h"
 #include <stdbool.h>
 
+
+void mux_task(void *argument);
+
+
 /* -------------------------------------------------------------------------- */
 #if defined (MDIO_88)
 mux_channel_t leds[LED_NUMBER] = {
@@ -147,6 +151,7 @@ void mux_gpio_init(void)
 void mux_led_proc(void)
 {
     uint8_t shift = 0;
+    bool flag = false;
     
     for (uint8_t i = 0; i < LED_NUMBER/4; i++)
     {
@@ -158,15 +163,21 @@ void mux_led_proc(void)
         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 1        
         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) 
         {
+            flag = true;
             vTaskDelay(1);
         }
         
+#endif        
         shift += 4;
     }
+    
+    if (!flag)
+        vTaskDelay(1);
 }
 
 //
@@ -220,3 +231,12 @@ void mux_led_status(bool state)
     }
 */    
 }
+
+//
+void mux_task(void *argument)
+{
+    for (;;)
+    {
+        mux_led_proc();
+    }
+}

+ 3 - 0
fw/modules/io/mux.h

@@ -200,6 +200,9 @@ void mux_led_test_toggle(void);
 //
 void mux_led_status(bool state);
 
+//
+void mux_task(void *argument);
+
 
 extern mux_channel_t leds[];
 

+ 19 - 0
fw/modules/misc/soft_wdt.c

@@ -0,0 +1,19 @@
+#include "at32f403a_407.h"
+#include "soft_wdt.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "common_gpio.h"
+
+
+
+//
+void soft_wdt(void *params)
+{
+    (void)params;
+    
+    for (;;)
+    {
+        extern_wdt_togle(); // extern WDT
+        vTaskDelay(100);
+    }
+}

+ 9 - 0
fw/modules/misc/soft_wdt.h

@@ -0,0 +1,9 @@
+#ifndef __SOFT_WDT_H
+#define __SOFT_WDT_H
+
+
+void soft_wdt(void *params);
+
+
+#endif  // __SOFT_TEST_H
+

+ 66 - 0
fw/modules/misc/usb_clock.c

@@ -0,0 +1,66 @@
+#include "at32f403a_407.h"
+#include "usb_clock.h"
+
+
+//
+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;
+
+        }
+    }
+}

+ 10 - 0
fw/modules/misc/usb_clock.h

@@ -0,0 +1,10 @@
+#ifndef __USB_CLOCK_H_
+#define __USB_CLOCK_H_
+
+
+
+void usb_clock48m_select(usb_clk48_s clk_s);
+
+
+
+#endif /* __USB_CLOCK_H_ */

+ 15 - 20
fw/modules/modbus/modbus.c

@@ -26,7 +26,8 @@
 #undef DBG
 #define DBG if(1)
 
-
+//UCHAR
+uint8_t mb_addr = 0; 
 static USHORT	usRegHoldingStart = REG_HOLDING_START;
 extern UCHAR    rcvAddress;
 extern bool psw_ok;
@@ -54,15 +55,15 @@ 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};
     
+    gpio_mbaddr_init();
+    
+    mb_addr = gpio_get_mbaddr();
+    
 	mb_init_params();
 
     mb_helper_tim_init(baud);
-    //eMBInit(MB_RTU, mb_addr, 4, baud, par, stop_bits);
     settings_init_mb_port(mb_addr);
 	eMBSetSlaveID(0x34, TRUE, ucSlaveID, 3);
 	eMBEnable();
@@ -93,9 +94,14 @@ void mb_init(void)
 void modbus_task(void *params)
 {
 	(void)params;
-
+    uint8_t new_addr = 0;
+        
 	for (;;)
 	{
+        if (gpio_check_mbaddr(mb_addr, &new_addr)) {
+            eMBSetSlaveAddr((UCHAR)new_addr);
+        }
+                
 		eMBPoll();
 	}
 
@@ -104,8 +110,6 @@ void modbus_task(void *params)
 //
 void modbus_params(void *params)
 {
-    bool ret = false;
-    
     for (;;)
     {
         if (mb_action != MB_NO_ACTION)
@@ -158,19 +162,10 @@ void modbus_params(void *params)
 #endif                  
                 break;
                 
-                  
+                // Сохранить настройки и добавить запись в журнал
                 case MB_SAVE_SETTINGS :
                     
-                    // Изменились ли настройки
-                    bool change = settings_is_changed(&settings);
-                    
-                    // Сохранение настроек
-                    ret = settings_save(&settings);
-                  
-                    if (change) {
-                        log_add_entry(LOG_CHANGE_CONFIG, (log_event_state_t)ret, 0, 0);
-                    }
-                    
+                    settings_save_with_log();
 
                 break;
                 
@@ -503,7 +498,7 @@ eMBFuncReadFileRecordCB( UCHAR * pucFrame, USHORT * usLen )
     record_num = pucFrame[7] << 8;
     record_num |= pucFrame[8];
     
-#if 1
+#if 0
     DBG printf("Byte count: %u, reference_type: %u, \r\n",
                 byte_count, reference_type);
     

+ 19 - 0
fw/modules/settings/settings_api.c

@@ -10,6 +10,7 @@
 #include "at32_uid.h"
 #include "hash.h"
 #include "utility.h"
+#include "log_api.h"
 #include <math.h> 
 #include <string.h>
 #include <stdlib.h>
@@ -429,6 +430,24 @@ bool settings_save(settings_t *settings)
     return ret;
 }
 
+//
+bool settings_save_with_log(void)
+{
+    bool ret;
+  
+    // Изменились ли настройки
+    bool change = settings_is_changed(&settings);
+                    
+    // Сохранение настроек
+    ret = settings_save(&settings);
+                  
+    if (change) {
+        log_add_entry(LOG_CHANGE_CONFIG, (log_event_state_t)ret, 0, 0);
+    }
+    
+    return ret;
+}
+
 //
 bool settings_write_to_flash(uint8_t *data, uint32_t size)
 {

+ 3 - 0
fw/modules/settings/settings_api.h

@@ -175,6 +175,9 @@ void settings_do_def(settings_t *settings);
 // Запись структуры настроек во flash
 bool settings_save(settings_t *settings);
 
+//
+bool settings_save_with_log(void);
+
 //
 bool settings_write_to_flash(uint8_t *data, uint32_t size);
 

+ 5 - 0
fw/modules/terminal/terminal_sbs.cpp

@@ -11,6 +11,7 @@ extern "C" {
 #include "log_api.h"
 #include "rtc.h"
 #include "settings_api.h"
+#include "utility.h"
 }
 
 extern struct ringfs fs_log;
@@ -55,6 +56,9 @@ int SbsTerminal::execute(int argc, const char * const *argv)
     if (strcmp(argv[0], "reset") == 0) {
         NVIC_SystemReset();
     }
+    if (strcmp(argv[0], "os") == 0) {
+        print_os_stat();
+    }
     // ---------------------------------------------------------------------- //
     // Добавить N новых записей в журнал
     if (strcmp(argv[0], "add_lentry") == 0) {
@@ -171,6 +175,7 @@ int SbsTerminal::help(int argc, const char * const *argv)
     printl ("You can use the following commands:");
     printl ("  version                          Print software version");
     printl ("  reset                            Reset");
+    printl ("  os                               Print os statistic");
     printl ("  add_lentry                       Add N log entries");
     printl ("  add_aentry [entries] [channel]   Add N archive entries to channel");
     printl ("  linfo                            Print log info");

+ 53 - 0
fw/modules/testing/soft_test.c

@@ -0,0 +1,53 @@
+#include "at32f403a_407.h"
+#include "soft_test.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "input.h"
+#include <stdio.h>
+
+
+void test_hw_task(void *argument)
+{
+    for (;;)
+    {
+        vTaskDelay(1000);
+              
+        printf("\r\n");
+        for (uint8_t i = 0; i < 8; i++)
+        {
+            printf("Channel: %u, state = %u\r\n", i + 1, in_get(i));
+        }
+        
+#if 0      
+        vTaskDelay(100);
+        
+        mux_led_blink();
+#endif
+
+        //adc_test();
+    }
+}
+
+
+
+//
+void test_gpio(void *params)
+{
+    (void)params;
+    
+    for (;;)
+    {
+        vTaskDelay(100);
+        
+        //printf("%" PRId64 " \r\n", rtc_get_ms());
+
+        //en_crnt_alrm_in(0x02);
+        
+        //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());
+    }
+}

+ 14 - 0
fw/modules/testing/soft_test.h

@@ -0,0 +1,14 @@
+#ifndef __SOFT_TEST_H
+#define __SOFT_TEST_H
+
+
+//
+void test_hw_task(void *argument);
+  
+//
+void test_gpio(void *params);
+
+
+
+#endif  // __SOFT_TEST_H
+

+ 4 - 4
fw/user/FreeRTOSConfig.h

@@ -88,7 +88,7 @@ assembly files that include this header file. */
 #ifndef __IASMARM__
 	#include <stdint.h>
 	extern unsigned int system_core_clock;
-//    extern volatile unsigned long ulHighFrequencyTimerTicks;
+    extern volatile unsigned long ulHighFrequencyTimerTicks;
 #endif
 
 #define configENABLE_BACKWARD_COMPATIBILITY 1
@@ -107,7 +107,7 @@ assembly files that include this header file. */
 #define configIDLE_SHOULD_YIELD			1
 #define configUSE_MUTEXES				1
 #define configQUEUE_REGISTRY_SIZE		8
-#define configGENERATE_RUN_TIME_STATS	0
+#define configGENERATE_RUN_TIME_STATS	1
 #define configCHECK_FOR_STACK_OVERFLOW	2
 #define configUSE_RECURSIVE_MUTEXES		1
 #define configUSE_MALLOC_FAILED_HOOK	1
@@ -116,8 +116,8 @@ assembly files that include this header file. */
 #define configUSE_STATS_FORMATTING_FUNCTIONS	1
 #define configAPPLICATION_ALLOCATED_HEAP        0
     
-//#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() ( ulHighFrequencyTimerTicks = 0UL )
-//#define portGET_RUN_TIME_COUNTER_VALUE()         ulHighFrequencyTimerTicks    
+#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() ( ulHighFrequencyTimerTicks = 0UL )
+#define portGET_RUN_TIME_COUNTER_VALUE()         ulHighFrequencyTimerTicks    
 
 /* Co-routine definitions. */
 #define configUSE_CO_ROUTINES 		0

+ 62 - 208
fw/user/main.cpp

@@ -34,6 +34,10 @@ extern "C" {
 #include "dac_transport.h"
 #include "log.h"
 #include "swap.h"
+#include "utility.h"
+#include "soft_test.h"
+#include "soft_wdt.h"
+#include "usb_clock.h"
 }
 #include "terminal_sbs.h"
 #include "terminal_usartbridge.h"
@@ -44,12 +48,6 @@ extern "C" {
 
 
 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);
 
 
 //
@@ -60,42 +58,26 @@ int main(void)
     nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
 	__enable_irq();
   
+    crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
+    
     extend_SRAM();
   
     system_clock_config();
 
     delay_init();
 
-    // -------------------------------------------------------------------------
-    // CLI
-    sbsTerminal.configure();
-    terminalUsartBridge.configure();
-
-    // -------------------------------------------------------------------------
-    // USB
-#ifdef USB_RNDIS    
-    usb_clock48m_select(USB_CLK_HEXT);
-    crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
-    usb_init();
-#endif      
-        
-#if 1
-    taskENTER_CRITICAL();      
-    
-    xTaskCreate(soft_wdt, "soft_wdt", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);    
     
+    //taskENTER_CRITICAL();      
+            
     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);
+    xTaskCreate(mux_task, "mux_task", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    
+    
     
-    xTaskCreate(button_task, "button_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
-
 #if defined (MAI_12)    
     xTaskCreate(adc_task, "adc_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
 #endif
@@ -104,11 +86,10 @@ int main(void)
     xTaskCreate(dac_task, "dac_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
 #endif    
     
-    taskEXIT_CRITICAL();
+    //taskEXIT_CRITICAL();
     
     vTaskStartScheduler();
-#endif    
-    
+     
     while (1) {}
 }
 
@@ -116,14 +97,20 @@ int main(void)
 void init_task(void *argument)
 {
 // -------------------------------------------------------------------------- //    
-// Для теста
-    //sys_clear();
+// Software watchdog
+  
+    gpio_wdt_init();
+    xTaskCreate(soft_wdt, "soft_wdt", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL);    
+    
+// -------------------------------------------------------------------------
+// CLI
+    
+    sbsTerminal.configure();
+    terminalUsartBridge.configure();    
     
 // -------------------------------------------------------------------------- //    
-// Загрузка и проверка настроек
+// Настройки. Загрузка и проверка.
 
-    crm_periph_clock_enable(CRM_CRC_PERIPH_CLOCK, TRUE);
-  
     // Мьютекс для работы с настройками
     init_settings(); 
     
@@ -137,13 +124,9 @@ void init_task(void *argument)
     // Настройки параметров порта Modbus
     settings_set_modbus_params(settings.com_settings.mb_port);
     
-#if 0    
-    settings_set_all_default();
-    settings_save(&settings);
-#endif      
 // -------------------------------------------------------------------------- //    
 // Безопасный режим, входы, выходы
-
+    
     save_mode_init();
     // TODO Для теста отключаем безопасный режим работы
     save_mode_set(false);   
@@ -152,19 +135,17 @@ void init_task(void *argument)
     
     //in_exint_init();    
 
-    gpio_wdt_init();
-    gpio_mbaddr_init();
-    
 // -------------------------------------------------------------------------- //
-// Кнопки  
+// Кнопки [SET, RESET]
     
     button_init();
+    xTaskCreate(button_task, "button_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
     
 // -------------------------------------------------------------------------- //
 // Uptime    
     
     uptime_init();
-
+    
 // -------------------------------------------------------------------------- //
 // RTC    
     
@@ -227,28 +208,27 @@ void init_task(void *argument)
 */    
     
 // -------------------------------------------------------------------------- //    
-// RNDIS
-    
-    //usb_eth_init();
-        
-    // Тесты таймеров
-    //mux_led_test_init();
+// USB (RNDIS class)
+#ifdef USB_RNDIS    
+    usb_clock48m_select(USB_CLK_HEXT);
+    crm_periph_clock_enable(CRM_USB_PERIPH_CLOCK, TRUE);
+    usb_init();
+    usb_eth_init();
+#endif    
     
 // -------------------------------------------------------------------------- //    
-// SPI flash
-#if 1    
-    // Тесты SPI flash
+// SPI flash, журнал событий, архив
+
     common_spi_init();
     spi_flash_init();
-    //InitFS(PRIM_DRIVE);
-    //spi_flash_test();
-#endif     
-    
-// -------------------------------------------------------------------------- //    
-// LOG
     
     log_init(false);
     
+// -------------------------------------------------------------------------- //
+// Статистика FreeRTOS
+    
+    utl_stat_timer_init();
+    
 // -------------------------------------------------------------------------- //
 // EVENT. Обновление FW.
     
@@ -263,160 +243,34 @@ void init_task(void *argument)
 
 // -------------------------------------------------------------------------- //
 // EVENT. Включение питания/перезагрузка.    
+    
     log_add_entry(LOG_SYSTEM_BOOT, (log_event_state_t)0, 0, 0);
 
-// -------------------------------------------------------------------------- //    
-// Тесты USB
-
-    //usb_eth_init();
-     
-      
-    //vTaskDelete(NULL);
-            
-    settings_is_changed(&settings);
+// -------------------------------------------------------------------------- //  
     
-    for (;;)
-    {
-        mux_led_proc();
-    }
-
-// -----------------------------------------------------------------------------              
+// Удаляем стартовую задачу 
+    vTaskDelete(NULL);
     
-    //taskYIELD();
-}
-
-
-void test_hw_task(void *argument)
-{
-    for (;;)
-    {
-        vTaskDelay(1000);
-        
-        
-        printf("\r\n");
-        for (uint8_t i = 0; i < 8; i++)
-        {
-            printf("Channel: %u, state = %u\r\n", i + 1, in_get(i));
-        }
-        
-        
-#if 0      
-        vTaskDelay(100);
-        
-        mux_led_blink();
-#endif
-
-        //adc_test();
-    }
-}
+// -------------------------------------------------------------------------- //        
+// Тесты
 
+    //usb_eth_init();
+    //sys_clear();
+    //mux_led_test_init();
 
-//
-void soft_wdt(void *params)
-{
-    (void)params;
+#if 0
+    // Настройки по умолчанию 
+    settings_set_all_default();
+    settings_save(&settings);
+#endif    
     
-    for (;;)
-    {
-        extern_wdt_togle(); // extern WDT
-        vTaskDelay(100);
-    }
-}
-
-//
-void test_gpio(void *params)
-{
-    (void)params;
+// -------------------------------------------------------------------------- //        
+// Отдельный таск для предварительных тестов
     
-    for (;;)
-    {
-        vTaskDelay(100);
-        
-        //printf("%" PRId64 " \r\n", rtc_get_ms());
-
-        //en_crnt_alrm_in(0x02);
-        
-        //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;
+    // Разное
+    //xTaskCreate(test_hw_task, "hw_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
     
-    for (;;)
-    {
-        vTaskDelay(1000);
-        eMBSetSlaveAddr(gpio_get_mbaddr());
-    }
+    // Тесты GPIO
+    //xTaskCreate(test_gpio, "gpio_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
 }
 
-//
-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;
-
-    }
-  }
-}

BIN
output/fw.bin


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 366 - 384
project/ewarm/iap/iap.dep


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 571 - 562
project/ewarm/module_universal_io.dep


+ 13 - 15
project/ewarm/module_universal_io.ewp

@@ -385,6 +385,7 @@
                     <state>$PROJ_DIR$\..\..\fw\modules\shift_reg</state>
                     <state>$PROJ_DIR$\..\..\fw\modules\log</state>
                     <state>$PROJ_DIR$\..\..\fw\modules\terminal</state>
+                    <state>$PROJ_DIR$\..\..\fw\modules\testing</state>
                     <state>$PROJ_DIR$\..\..\fw\user</state>
                     <state>$PROJ_DIR$\..\..\libs\thirdparty\freertos\include</state>
                     <state>$PROJ_DIR$\..\..\libs\thirdparty\freertos\portable\IAR\ARM_CM4F</state>
@@ -2219,12 +2220,18 @@
                 <file>
                     <name>$PROJ_DIR$\..\..\fw\modules\misc\misc.c</name>
                 </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\fw\modules\misc\soft_wdt.c</name>
+                </file>
                 <file>
                     <name>$PROJ_DIR$\..\..\fw\modules\misc\swap.c</name>
                 </file>
                 <file>
                     <name>$PROJ_DIR$\..\..\fw\modules\misc\uptime.c</name>
                 </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\fw\modules\misc\usb_clock.c</name>
+                </file>
             </group>
             <group>
                 <name>modbus</name>
@@ -2268,6 +2275,12 @@
                     <name>$PROJ_DIR$\..\..\fw\modules\terminal\terminal_usartbridge.cpp</name>
                 </file>
             </group>
+            <group>
+                <name>testing</name>
+                <file>
+                    <name>$PROJ_DIR$\..\..\fw\modules\testing\soft_test.c</name>
+                </file>
+            </group>
             <group>
                 <name>usb</name>
                 <file>
@@ -2430,21 +2443,6 @@
         </group>
         <group>
             <name>thirdparty</name>
-            <group>
-                <name>FatFs</name>
-                <file>
-                    <name>$PROJ_DIR$\..\..\libs\thirdparty\fat_fs\src\diskio.c</name>
-                </file>
-                <file>
-                    <name>$PROJ_DIR$\..\..\libs\thirdparty\fat_fs\src\drivers\fatfs_spi_flash.c</name>
-                </file>
-                <file>
-                    <name>$PROJ_DIR$\..\..\libs\thirdparty\fat_fs\src\ff.c</name>
-                </file>
-                <file>
-                    <name>$PROJ_DIR$\..\..\libs\thirdparty\fat_fs\src\option\syscall.c</name>
-                </file>
-            </group>
             <group>
                 <name>freertos</name>
                 <group>

+ 12 - 15
project/ewarm/module_universal_io.ewt

@@ -2429,12 +2429,18 @@
                 <file>
                     <name>$PROJ_DIR$\..\..\fw\modules\misc\misc.c</name>
                 </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\fw\modules\misc\soft_wdt.c</name>
+                </file>
                 <file>
                     <name>$PROJ_DIR$\..\..\fw\modules\misc\swap.c</name>
                 </file>
                 <file>
                     <name>$PROJ_DIR$\..\..\fw\modules\misc\uptime.c</name>
                 </file>
+                <file>
+                    <name>$PROJ_DIR$\..\..\fw\modules\misc\usb_clock.c</name>
+                </file>
             </group>
             <group>
                 <name>modbus</name>
@@ -2478,6 +2484,12 @@
                     <name>$PROJ_DIR$\..\..\fw\modules\terminal\terminal_usartbridge.cpp</name>
                 </file>
             </group>
+            <group>
+                <name>testing</name>
+                <file>
+                    <name>$PROJ_DIR$\..\..\fw\modules\testing\soft_test.c</name>
+                </file>
+            </group>
             <group>
                 <name>usb</name>
                 <file>
@@ -2640,21 +2652,6 @@
         </group>
         <group>
             <name>thirdparty</name>
-            <group>
-                <name>FatFs</name>
-                <file>
-                    <name>$PROJ_DIR$\..\..\libs\thirdparty\fat_fs\src\diskio.c</name>
-                </file>
-                <file>
-                    <name>$PROJ_DIR$\..\..\libs\thirdparty\fat_fs\src\drivers\fatfs_spi_flash.c</name>
-                </file>
-                <file>
-                    <name>$PROJ_DIR$\..\..\libs\thirdparty\fat_fs\src\ff.c</name>
-                </file>
-                <file>
-                    <name>$PROJ_DIR$\..\..\libs\thirdparty\fat_fs\src\option\syscall.c</name>
-                </file>
-            </group>
             <group>
                 <name>freertos</name>
                 <group>

+ 5 - 0
shared/peripherals/inc/common_gpio.h

@@ -1,6 +1,8 @@
 #ifndef __COMMON_GPIO_H
 #define __COMMON_GPIO_H
 
+#include <stdbool.h>
+
 //
 void gpio_wdt_init(void);
 
@@ -10,6 +12,9 @@ void gpio_mbaddr_init(void);
 //
 uint8_t gpio_get_mbaddr(void);
 
+//
+bool gpio_check_mbaddr(uint8_t old_addr, uint8_t *new_addr);
+
 // Ревизия платы
 uint8_t gpio_get_rev(void);
 

+ 17 - 24
shared/peripherals/src/buttons.c

@@ -84,16 +84,10 @@ bool button_get_state_int(BUTTON_t button)
   */
 void BUTTON_NoFixHandler(BUTTON_NO_FIX_t *button)
 {
-    uint8_t temp;
-  
     if (button->stateNew != BUT_NO_DEFINE)
     {  
-        temp = (uint8_t)gpio_input_data_bit_read(button->port, button->pin);
-      
-        if (temp == 1)  // определяем текущее (новое) состояние кнопки        
-            button->stateNew = BUT_ON;
-        else
-            button->stateNew = BUT_OFF;
+        // определяем текущее (новое) состояние кнопки        
+        button->stateNew = (BUTTON_STATE_t)gpio_input_data_bit_read(button->port, button->pin);
   
         if (button->stateNew != button->stateOld)  // зафиксирован факт нажатия/отжатия
             button->stateNew = BUT_NO_DEFINE;  // переводим кнопку в неопределенное состояние
@@ -105,20 +99,14 @@ void BUTTON_NoFixHandler(BUTTON_NO_FIX_t *button)
         if (button->counterDelay > button->timeDelay)  // задержка закончилась
         {
             button->counterDelay = 0;
-            
-            temp = (uint8_t)gpio_input_data_bit_read(button->port, button->pin);
-   	  
             button->counterActivation = 0;
-		
-            //if (temp == 0)  // определяем текущее (новое) состояние кнопки        
-            if (temp == 1)  // определяем текущее (новое) состояние кнопки        
-            {  
-                button->stateNew = BUT_ON;
+            button->stateNew = (BUTTON_STATE_t)gpio_input_data_bit_read(button->port, button->pin);
+                
+            // определяем текущее (новое) состояние кнопки        
+            if (button->stateNew == BUT_ON) {  
                 button->unpressHandler(1);
             }  
-            else
-            {  
-                button->stateNew = BUT_OFF;
+            else {  
                 button->pressHandler();
             }
         }
@@ -176,19 +164,24 @@ void button_task(void *params)
     }
 }
 
-// Заглушки
-void void_handler(void) {}
-void void_handler_btn(uint8_t btn) {}
+// Заглушка
+void void_handler(void) 
+{}
 
-//
+// Заглушка
+void void_handler_btn(uint8_t btn) 
+{}
+
+// Перезагрузка контроллера
 void reset_handler(void)
 {
     NVIC_SystemReset();
 }
 
-//
+// Сброс настроек ( + запись в лог)
 void set_handler(uint8_t btn)
 {
+    settings_save_with_log();
 }
 
 

+ 15 - 2
shared/peripherals/src/common_gpio.c

@@ -67,14 +67,27 @@ uint8_t gpio_get_mbaddr(void)
     
     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 |= ((uint8_t)!gpio_input_data_bit_read(addr_in[i].port, addr_in[i].pin) << i);
     }
     
     ret = ret > 1 ? ret : 1;
-    
+        
     return ret;
 }
 
+//
+bool gpio_check_mbaddr(uint8_t old_addr, uint8_t *new_addr)
+{
+    uint8_t tmp = gpio_get_mbaddr();
+    
+    if (old_addr != tmp)
+    {
+        *new_addr = tmp;
+        return true;
+    }
+    return false;
+}
+
 // Ревизия платы
 uint8_t gpio_get_rev(void)
 {

+ 10 - 6
shared/rtc/rtc.c

@@ -4,12 +4,11 @@
 #include <string.h>
 #include <stdio.h>
 
-//TM_RTC_t calendar;
 
-/* Days in a month */
+// Days in a month
 uint8_t TM_RTC_Months[2][12] = {
-	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},	/* Not leap year */
-	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}	/* Leap year */
+	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},	// Not leap year
+	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}	// Leap year
 };
 
 // monthly correction data sheet
@@ -18,7 +17,7 @@ const uint8_t table_week[12] = {0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
 // monmonth data table of common year
 const uint8_t mon_table[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
-/* Internal RTC defines */
+// Internal RTC defines 
 #define TM_RTC_LEAP_YEAR(year) 			((((year) % 4 == 0) && ((year) % 100 != 0)) || ((year) % 400 == 0))
 #define TM_RTC_DAYS_IN_YEAR(x)			TM_RTC_LEAP_YEAR(x) ? 366 : 365
 #define TM_RTC_OFFSET_YEAR				1970
@@ -44,6 +43,8 @@ uint8_t TM_RTC_Init(void)
 {
     TM_RTC_t datatime;
     
+    uint16_t tmp = 0;
+    
     // enable pwc and bpr clocks
     crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
     crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);
@@ -51,6 +52,9 @@ uint8_t TM_RTC_Init(void)
     // enable the battery-powered domain write operations
     pwc_battery_powered_domain_access(TRUE);
     
+    tmp = bpr_data_read(BACKUP_RTC_KEY);
+    printf("RTC: %X\r\n", tmp);
+    
     // check if rtc is initialized 
     if (bpr_data_read(BACKUP_RTC_KEY) != 0x1234)
     {
@@ -369,7 +373,7 @@ void rtc_subtim_init(void)
     crm_periph_clock_enable(CRM_TMR5_PERIPH_CLOCK, TRUE);
 
     crm_clocks_freq_get(&crm_clocks_freq_struct);
-    tmr_base_init(TMR5, 9990, 24000 - 1);
+    tmr_base_init(TMR5, 9999, 24000 - 1);
     tmr_cnt_dir_set(TMR5, TMR_COUNT_UP);
 
     tmr_flag_clear(TMR5, TMR_OVF_FLAG);

+ 71 - 0
shared/utils/utility.c

@@ -5,8 +5,11 @@
 #include "utility.h"
 #include "at32f403a_407_int.h"
 #include <stdio.h>
+#include <string.h>
 
 
+volatile unsigned long ulHighFrequencyTimerTicks;
+
 
 #if 1
 // OS stack overflow hook
@@ -54,4 +57,72 @@ void print_binary_half_word(uint16_t val)
             printf(" ");
     }
     printf("\r\n");
+}
+
+//
+void utl_stat_timer_init(void)
+{
+    crm_clocks_freq_type crm_clocks_freq_struct = {0};
+    
+    crm_periph_clock_enable(CRM_TMR4_PERIPH_CLOCK, TRUE);
+
+    crm_clocks_freq_get(&crm_clocks_freq_struct);
+    tmr_base_init(TMR4, 999, 240 - 1);
+    tmr_cnt_dir_set(TMR4, TMR_COUNT_UP);
+
+    tmr_flag_clear(TMR4, TMR_OVF_FLAG);
+    
+    NVIC_ClearPendingIRQ(TMR4_GLOBAL_IRQn);
+    nvic_irq_enable(TMR4_GLOBAL_IRQn, 5, 0);
+       
+    tmr_counter_enable(TMR4, TRUE);
+    tmr_interrupt_enable(TMR4, TMR_OVF_INT, TRUE);
+
+}
+
+//
+void print_os_stat(void)
+{
+    char *ptr;
+    
+    ptr = (char*)pvPortMalloc(800);
+    
+    vTaskList(ptr);
+    
+    printf(ptr);
+    printf((char*)"\r\n");
+    printf((char*)"xPortGetFreeHeapSize = %d\n\r\r\n", xPortGetFreeHeapSize());
+        
+    memset(ptr, 0, 800);
+    vTaskGetRunTimeStats(ptr);
+    printf(ptr);
+    printf((char*)"\r\n");
+    
+    vPortFree(ptr);
+}
+
+//
+void TMR4_GLOBAL_IRQHandler(void)
+{
+    //static uint32_t cnt = 0;
+    //static uint32_t sec_cnt = 0;
+  
+    if (tmr_flag_get(TMR4, TMR_OVF_FLAG) != RESET)
+    {
+        tmr_flag_clear(TMR4, TMR_OVF_FLAG);
+        ulHighFrequencyTimerTicks++;
+        
+#if 0        
+        cnt++;
+        
+        if (cnt == 1000) {
+            cnt = 0;
+            sec_cnt++;
+            printf("TIM4 cnt: %u\r\n", sec_cnt);
+        }
+        
+        tmr_flag_clear(TMR4, TMR_OVF_FLAG);
+        ulHighFrequencyTimerTicks++;
+#endif        
+    }
 }

+ 6 - 0
shared/utils/utility.h

@@ -15,5 +15,11 @@ void print_binary_byte(uint8_t val);
 //
 void print_binary_half_word(uint16_t val);
 
+//
+void utl_stat_timer_init(void);
+
+//
+void print_os_stat(void);
+
 
 #endif /* __UTILITY_H_ */

BIN
tools/__pycache__/io_module.cpython-312.pyc


BIN
tools/__pycache__/log_reader.cpython-312.pyc


BIN
tools/__pycache__/mb_registers.cpython-312.pyc


BIN
tools/__pycache__/modbus.cpython-312.pyc


BIN
tools/__pycache__/sys_params.cpython-312.pyc


BIN
tools/__pycache__/updater.cpython-312.pyc


+ 2 - 2
tools/digital_io.py

@@ -94,7 +94,7 @@ def main():
     dev.modbus.MB_DEBUG = False
 
     '''Системные переменные и параметры'''
-    # dev.sys.get_system_vars()
+    dev.sys.get_system_vars()
 
     ''' Установить текущее время с учетом часового пояса'''
     # dev.sys.set_rtc()     
@@ -118,7 +118,7 @@ def main():
     # dev.log.get_all_log()
 
     '''Архив'''
-    dev.log.get_all_archive()
+    # dev.log.get_all_archive()
     # print(dev.log.get_archive_entry(0, 1))
 
     '''Сохранение настроек'''

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels