Explorar el Código

Работа с аналоговыми входами.

TelenkovDmitry hace 1 año
padre
commit
ab6505d1a2

BIN
doc/Доработка_аналогого_модуля_в_части_CPU.png


BIN
doc/Доработка_аналогого_модуля_в_части_управления_входами.png


+ 80 - 0
fw/modules/io/analog_input.c

@@ -0,0 +1,80 @@
+#include "at32f403a_407.h"
+#include "analog_input.h"
+#include "shift_reg.h"
+#include "utility.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include <stdio.h>
+
+
+uint8_t input_mux; // выход сдвигового регистра U1010 (управляет MUX 301, 401)
+                    // мультиплексоры отвечат за коммутацию аналоговых входов и АЦП
+
+
+//
+void ai_connect_channel_301(uint8_t ch)
+{
+    input_mux &= 0x70;
+    
+    switch (ch)
+    {
+        case AN_INP_1: input_mux |= 0x03; break; // U301 Y3
+        case AN_INP_2: input_mux |= 0x00; break; // U301 Y0
+        case AN_INP_3: input_mux |= 0x05; break; // U301 Y5
+        case AN_INP_4: input_mux |= 0x07; break; // U301 Y7
+        case AN_INP_5: input_mux |= 0x06; break; // U301 Y6
+        case AN_INP_6: input_mux |= 0x04; break; // U301 Y4
+        case V_ISO_CL: input_mux |= 0x01; break; // U301 Y1
+        case V_ISO   : input_mux |= 0x02; break; // U301 Y2
+        default: break;
+    }
+    
+    printf("Analog input connect register: ");
+    print_binary_byte(input_mux);
+    printf("\r\n");
+}
+
+//
+void ai_connect_channel_401(uint8_t ch)
+{
+    input_mux &= 0x07;
+    
+    switch (ch)
+    {
+        case AN_INP_7: input_mux |= (0x04 << 3); break; // U401 Y4
+        case AN_INP_8: input_mux |= (0x06 << 3); break; // U401 Y6
+        case AN_INP_9: input_mux |= (0x07 << 3); break; // U401 Y7
+        case AN_INP_10:input_mux |= (0x05 << 3); break; // U401 Y5
+        case AN_INP_11:input_mux |= (0x02 << 3); break; // U401 Y2
+        case AN_INP_12:input_mux |= (0x01 << 3); break; // U401 Y1
+        case CRNT_LIM_U_BFR_R: input_mux  |= (0x00 << 3); break; // U401 Y0
+        case CRNT_LIM_U_ABFR_R: input_mux |= (0x03 << 3); break; // U401 Y3
+        default: break;
+    }
+    
+    printf("Analog input connect register: ");
+    print_binary_byte(input_mux);
+    printf("\r\n");
+}
+
+//
+void ai_connect_test(void)
+{
+    ai_connect_channel_301(AN_INP_1);
+    ai_connect_channel_301(AN_INP_2);
+    ai_connect_channel_301(AN_INP_3);
+    ai_connect_channel_301(AN_INP_4);
+    ai_connect_channel_301(AN_INP_5);
+    ai_connect_channel_301(AN_INP_6);
+    ai_connect_channel_301(V_ISO_CL);
+    ai_connect_channel_301(V_ISO);
+    
+    ai_connect_channel_401(AN_INP_7);
+    ai_connect_channel_401(AN_INP_8);
+    ai_connect_channel_401(AN_INP_9);
+    ai_connect_channel_401(AN_INP_10);
+    ai_connect_channel_401(AN_INP_11);
+    ai_connect_channel_401(AN_INP_12);
+    ai_connect_channel_401(CRNT_LIM_U_BFR_R);
+    ai_connect_channel_401(CRNT_LIM_U_ABFR_R);
+}

+ 50 - 0
fw/modules/io/analog_input.h

@@ -0,0 +1,50 @@
+#ifndef __ANALOG_INPUT_H
+#define __ANALOG_INPUT_H
+
+#include <stdbool.h>
+
+/*
+#define AN_INP_1    (0)
+#define AN_INP_1    (0)
+#define AN_INP_1    (0)
+#define AN_INP_1    (0)
+#define AN_INP_1    (0)
+#define AN_INP_1    (0)
+#define AN_INP_1    (0)
+#define AN_INP_1    (0)
+*/
+
+typedef enum 
+{
+    AN_INP_1 = 0,
+    AN_INP_2,
+    AN_INP_3,
+    AN_INP_4,
+    AN_INP_5,
+    AN_INP_6,
+    AN_INP_7,
+    AN_INP_8,
+    AN_INP_9,
+    AN_INP_10,
+    AN_INP_11,
+    AN_INP_12,
+    V_ISO_CL,   // +24 V
+    V_ISO,      // +5 V
+    CRNT_LIM_U_BFR_R,
+    CRNT_LIM_U_ABFR_R
+    
+} ANALOG_IN;
+
+
+
+//
+void ai_connect_channel_301(uint8_t ch);
+
+//
+void ai_connect_channel_401(uint8_t ch);
+  
+//
+void ai_connect_test(void);
+
+#endif  // __ANALOG_INPUT_H
+

+ 130 - 122
fw/modules/shift_reg/shift_reg.c

@@ -1,123 +1,131 @@
-#include "at32f403a_407.h"
-#include "shift_reg.h"
-#include "FreeRTOS.h"
-#include "task.h"
-#include <stdio.h>
-#include <stdbool.h>
-
-
-#define SH_SPI  SPI3
-
-
-// PD1 -  ART_nCS_LS_CH16/DAC2
-// PC10 - ART_CLK_LS (SPI3_CK remap)
-// PC11 - ART_MISO_CH16/CS_LS (SPI3_MISO remap)
-// PC12 - ART_MOSI_LS (SPI3_MOSI remap)
-
-
-// PD0 - ART_nCS_LS_CH712/DAC3
-// PD2 - ART_MISO_CH712
-
-void sh_init(void)
-{
-    spi_init_type spi_init_struct;
-    gpio_init_type gpio_initstructure;
-    
-  
-    crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
-    crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
-    crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
-    
-    gpio_pin_remap_config(SPI3_GMUX_0011, TRUE);    // Переключить PC10, PC11, PC12
-    
-    // SCK
-    gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
-    gpio_initstructure.gpio_pull           = GPIO_PULL_DOWN;  
-    gpio_initstructure.gpio_mode           = GPIO_MODE_MUX;  
-    gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
-    gpio_initstructure.gpio_pins           = GPIO_PINS_10;
-    gpio_init(GPIOC, &gpio_initstructure);
-  
-    // MISO
-    gpio_initstructure.gpio_pull           = GPIO_PULL_UP;  
-    gpio_initstructure.gpio_mode           = GPIO_MODE_INPUT;  
-    gpio_initstructure.gpio_pins           = GPIO_PINS_11;
-    gpio_init(GPIOC, &gpio_initstructure);
-    
-    // MOSI
-    gpio_initstructure.gpio_pull           = GPIO_PULL_UP;  
-    gpio_initstructure.gpio_mode           = GPIO_MODE_MUX; 
-    gpio_initstructure.gpio_pins           = GPIO_PINS_12;
-    gpio_init(GPIOC, &gpio_initstructure);
-       
-    // CS
-    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_1;
-    gpio_init(GPIOD, &gpio_initstructure); 
-    
-    
-    crm_periph_clock_enable(CRM_SPI3_PERIPH_CLOCK, TRUE);
-    
-    spi_default_para_init(&spi_init_struct);
-    spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
-    spi_init_struct.master_slave_mode = SPI_MODE_MASTER;
-    spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_32; //SPI_MCLK_DIV_2;
-    spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
-    spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
-    spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_HIGH;//SPI_CLOCK_POLARITY_LOW;
-    spi_init_struct.clock_phase = SPI_CLOCK_PHASE_2EDGE;
-    spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
-    
-    spi_init(SPI3, &spi_init_struct);
- 
-    spi_hardware_cs_output_enable(SPI3, FALSE);
-    
-    spi_enable(SPI3, TRUE);
-}
-
-
-//
-void sh_test(uint8_t val)
-{
-    uint8_t ret;
-    
-#if 0    
-    gpio_bits_set(GPIOD, GPIO_PINS_1);
-    gpio_bits_reset(GPIOD, GPIO_PINS_1);
-    
-    while (spi_i2s_flag_get(SH_SPI, SPI_I2S_TDBE_FLAG) == RESET);
-    SH_SPI->dt = val;
-  
-    while (spi_i2s_flag_get(SH_SPI, SPI_I2S_RDBF_FLAG) == RESET);
-    ret = SH_SPI->dt;
-
-/*    
-    gpio_bits_set(GPIOD, GPIO_PINS_1);
-
-    vTaskDelay(1);
-    
-    gpio_bits_reset(GPIOD, GPIO_PINS_1);
-*/    
-    printf("SH return: %X\r\n", ret);
-#endif    
-    
-    while (spi_i2s_flag_get(SH_SPI, SPI_I2S_TDBE_FLAG) == RESET);
-    SH_SPI->dt = val;
-  
-    while (spi_i2s_flag_get(SH_SPI, SPI_I2S_RDBF_FLAG) == RESET);
-    ret = SH_SPI->dt;
-
-    gpio_bits_set(GPIOD, GPIO_PINS_1);
-    gpio_bits_reset(GPIOD, GPIO_PINS_1);
-
-    printf("SH return: %X\r\n", ret & 0x3F);
-}
-
-//
-void sh_delay(void)
-{
-    for (unsigned int i = 0; i < 1; i++) {}
+#include "at32f403a_407.h"
+#include "shift_reg.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include <stdio.h>
+#include <stdbool.h>
+
+
+#define SH_SPI  SPI2
+
+
+// PD1 -  ART_nCS_LS_CH16/DAC2
+// PC10 - ART_CLK_LS (SPI3_CK remap)
+// PC11 - ART_MISO_CH16/CS_LS (SPI3_MISO remap)
+// PC12 - ART_MOSI_LS (SPI3_MOSI remap)
+
+
+// PD0 - ART_nCS_LS_CH712/DAC3
+// PD2 - ART_MISO_CH712
+
+void sh_init(void)
+{
+    spi_init_type spi_init_struct;
+    gpio_init_type gpio_initstructure;
+    
+  
+    crm_periph_clock_enable(CRM_IOMUX_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);
+    
+    // SCK
+    gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
+    gpio_initstructure.gpio_pull           = GPIO_PULL_DOWN;  
+    gpio_initstructure.gpio_mode           = GPIO_MODE_MUX;  
+    gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
+    gpio_initstructure.gpio_pins           = GPIO_PINS_13;
+    gpio_init(GPIOB, &gpio_initstructure);
+  
+    // MISO
+    gpio_initstructure.gpio_pull           = GPIO_PULL_UP;  
+    gpio_initstructure.gpio_mode           = GPIO_MODE_INPUT;  
+    gpio_initstructure.gpio_pins           = GPIO_PINS_14;
+    gpio_init(GPIOB, &gpio_initstructure);
+    
+    // MOSI
+    gpio_initstructure.gpio_pull           = GPIO_PULL_UP;  
+    gpio_initstructure.gpio_mode           = GPIO_MODE_MUX; 
+    gpio_initstructure.gpio_pins           = GPIO_PINS_15;
+    gpio_init(GPIOB, &gpio_initstructure);
+
+    // CS
+    // Эти два CS управляют двумя парами регистров
+    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_0 | GPIO_PINS_1;
+    gpio_init(GPIOD, &gpio_initstructure); 
+    
+    // CS для сдвигового регистра U1010
+    gpio_initstructure.gpio_pins           = GPIO_PINS_3;
+    gpio_init(GPIOD, &gpio_initstructure); 
+        
+    crm_periph_clock_enable(CRM_SPI2_PERIPH_CLOCK, TRUE);
+    
+    spi_default_para_init(&spi_init_struct);
+    spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
+    spi_init_struct.master_slave_mode = SPI_MODE_MASTER;
+    spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_32; //SPI_MCLK_DIV_2;
+    spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
+    spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
+    spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_HIGH;//SPI_CLOCK_POLARITY_LOW;
+    spi_init_struct.clock_phase = SPI_CLOCK_PHASE_2EDGE;
+    spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
+    
+    spi_init(SH_SPI, &spi_init_struct);
+ 
+    spi_hardware_cs_output_enable(SH_SPI, FALSE);
+    
+    spi_enable(SH_SPI, TRUE);
+}
+
+// Управление сигналами EN_CRNT_SNS_1..EN_CRNT_SNS_12
+// Получение сигналов ALRM_IN_1..ALRM_IN_12
+void en_crnt_alrm_in(uint16_t val)
+{
+    uint16_t ret = 0;
+    
+    // Данные отправлеяются во второй регистр U402
+    while (spi_i2s_flag_get(SH_SPI, SPI_I2S_TDBE_FLAG) == RESET);
+    SH_SPI->dt = 0x02;
+  
+    // Данные из второго регистра U400
+    while (spi_i2s_flag_get(SH_SPI, SPI_I2S_RDBF_FLAG) == RESET);
+    ret = SH_SPI->dt & 0x3F;
+
+    // Данные отправлеяются во второй регистр U302
+    while (spi_i2s_flag_get(SH_SPI, SPI_I2S_TDBE_FLAG) == RESET);
+    SH_SPI->dt = 0x01;
+  
+    // Данные из первого регистра U300
+    while (spi_i2s_flag_get(SH_SPI, SPI_I2S_RDBF_FLAG) == RESET);
+    ret |= (SH_SPI->dt & 0x3F) << 8;
+    
+    gpio_bits_set(GPIOD, GPIO_PINS_0 | GPIO_PINS_1);
+    gpio_bits_reset(GPIOD, GPIO_PINS_0 | GPIO_PINS_1);
+
+    printf("SH return: %X\r\n", ret);
+}
+
+// Подключает выбранный канал к АЦП
+void an_inp(uint16_t val)
+{
+    uint16_t ret;
+    
+    while (spi_i2s_flag_get(SH_SPI, SPI_I2S_TDBE_FLAG) == RESET);
+    SH_SPI->dt = val;
+  
+    while (spi_i2s_flag_get(SH_SPI, SPI_I2S_RDBF_FLAG) == RESET);
+    ret = SH_SPI->dt;
+    
+    gpio_bits_set(GPIOD, GPIO_PINS_3);
+    gpio_bits_reset(GPIOD, GPIO_PINS_3);
+}
+
+//
+void sh_delay(void)
+{
+    for (unsigned int i = 0; i < 1; i++) {}
 }

+ 24 - 18
fw/modules/shift_reg/shift_reg.h

@@ -1,18 +1,24 @@
-#ifndef __SHIFT_REG_H
-#define __SHIFT_REG_H
-
-#include <stdbool.h>
-
-
-//
-void sh_init(void);
-
-//
-void sh_test(uint8_t val);
-
-//
-void sh_delay(void);
-
-
-#endif  // __SHIFT_REG_H
-
+#ifndef __SHIFT_REG_H
+#define __SHIFT_REG_H
+
+#include <stdbool.h>
+
+
+//
+void sh_init(void);
+
+//
+void en_crnt_alrm_in(uint16_t val);
+
+//
+void an_inp(uint16_t val);
+
+//
+void an_inp(uint16_t val);
+
+//
+void sh_delay(void);
+
+
+#endif  // __SHIFT_REG_H
+

+ 310 - 306
fw/user/main.c

@@ -1,306 +1,310 @@
-#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);
-    
-    xTaskCreate(button_task, "button_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
- 
-    //xTaskCreate(adc_task, "adc_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();
-    
-// -------------------------------------------------------------------------- //
-// Кнопки  
-    
-    button_init();
-    
-// -------------------------------------------------------------------------- //
-// Uptime    
-    
-    uptime_init();
-
-// -------------------------------------------------------------------------- //
-// RTC    
-    
-    TM_RTC_Init();
-    
-// -------------------------------------------------------------------------- //    
-// Мультиплексор
-    
-    mux_gpio_init();
-    
-// -------------------------------------------------------------------------- //        
-// Modbus    
-    
-    mb_init();
-    
-// -------------------------------------------------------------------------- //            
-// Базовая инициализация входов/выходов
-// TODO потом брать значения из настроек
-
-    //io_init();
-    
-// -------------------------------------------------------------------------- //        
-// Сброс счетчика попыток загрузок
-    
-    update_reset_boot_try();    
-    
-// -------------------------------------------------------------------------- //    
-// Тесты
-    //pwm_test(); // тесы PWM
-    gpio_get_rev();
-    
-    sh_init();
-    
-      
-// -------------------------------------------------------------------------- //    
-// RNDIS
-    
-    //usb_eth_init();
-        
-    // Тесты таймеров
-    //mux_led_test_init();
-    
-#if 0    
-    // Тесты SPI flash
-    common_spi_init();
-    InitFS(PRIM_DRIVE);
-    spi_flash_test();
-#endif     
-    
-    // Тесты USB
-    //usb_eth_init();
-     
-      
-    //vTaskDelete(NULL);
-            
-    for (;;)
-    {
-        mux_led_proc();
-    }
-
-// -----------------------------------------------------------------------------              
-    
-    //taskYIELD();
-}
-
-
-void test_hw_task(void *argument)
-{
-    for (;;)
-    {
-        vTaskDelay(100);
-        
-#if 0      
-        vTaskDelay(100);
-        
-        mux_led_blink();
-#endif
-
-        //adc_test();
-    }
-}
-
-
-//
-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);
-        
-        sh_test(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;
-    
-    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;
-
-    }
-  }
-}
+#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);
+    
+    xTaskCreate(button_task, "button_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+ 
+    //xTaskCreate(adc_task, "adc_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();
+    
+// -------------------------------------------------------------------------- //
+// Кнопки  
+    
+    button_init();
+    
+// -------------------------------------------------------------------------- //
+// Uptime    
+    
+    uptime_init();
+
+// -------------------------------------------------------------------------- //
+// RTC    
+    
+    TM_RTC_Init();
+    
+// -------------------------------------------------------------------------- //    
+// Мультиплексор
+    
+    mux_gpio_init();
+    
+// -------------------------------------------------------------------------- //        
+// Modbus    
+    
+    mb_init();
+    
+// -------------------------------------------------------------------------- //            
+// Базовая инициализация входов/выходов
+// TODO потом брать значения из настроек
+
+    //io_init();
+    
+// -------------------------------------------------------------------------- //        
+// Сброс счетчика попыток загрузок
+    
+    update_reset_boot_try();    
+    
+// -------------------------------------------------------------------------- //    
+// Тесты
+    //pwm_test(); // тесы PWM
+    gpio_get_rev();
+    
+    sh_init();
+    
+// Тесты аналоговых входов    
+    ai_connect_test();
+      
+// -------------------------------------------------------------------------- //    
+// RNDIS
+    
+    //usb_eth_init();
+        
+    // Тесты таймеров
+    //mux_led_test_init();
+    
+#if 0    
+    // Тесты SPI flash
+    common_spi_init();
+    InitFS(PRIM_DRIVE);
+    spi_flash_test();
+#endif     
+    
+    // Тесты USB
+    //usb_eth_init();
+     
+      
+    //vTaskDelete(NULL);
+            
+    for (;;)
+    {
+        mux_led_proc();
+    }
+
+// -----------------------------------------------------------------------------              
+    
+    //taskYIELD();
+}
+
+
+void test_hw_task(void *argument)
+{
+    for (;;)
+    {
+        vTaskDelay(100);
+        
+#if 0      
+        vTaskDelay(100);
+        
+        mux_led_blink();
+#endif
+
+        //adc_test();
+    }
+}
+
+
+//
+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);
+        
+        //en_crnt_alrm_in(0x02);
+        
+        an_inp(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;
+    
+    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;
+
+    }
+  }
+}

+ 42 - 41
fw/user/main.h

@@ -1,42 +1,43 @@
-#ifndef __MAIN_H
-#define __MAIN_H
-
-#include "at32f403a_407.h"
-#include "at32f403a_407_board.h"
-#include "at32f403a_407_clock.h"
-#include "common_config.h"
-#include "FreeRTOS.h"
-#include "task.h"
-#include "queue.h"
-#include "semphr.h"
-#include "usb_eth.h"
-#include "mux.h"
-#include "misc.h"
-#include "spi_common.h"
-#include "user_fatfs.h"
-#include "spi_flash.h"
-#include "usb_eth.h"
-#include "extended_sram.h"
-#include "modbus.h"
-#include "common_gpio.h"
-#include "io.h"
-#include "input.h"
-#include "output.h"
-#include "sys_api.h"
-#include "settings_api.h"
-#include "update.h"
-#include "uptime.h"
-#include "rtc.h"
-#include "mb.h"
-#include "io_utils.h"
-#include "buttons.h"
-#include "adc_transport.h"
-#include "shift_reg.h"
-#include <stdio.h>
-#include <stdbool.h>
-#include <string.h>
-
-
-
-
+#ifndef __MAIN_H
+#define __MAIN_H
+
+#include "at32f403a_407.h"
+#include "at32f403a_407_board.h"
+#include "at32f403a_407_clock.h"
+#include "common_config.h"
+#include "FreeRTOS.h"
+#include "task.h"
+#include "queue.h"
+#include "semphr.h"
+#include "usb_eth.h"
+#include "mux.h"
+#include "misc.h"
+#include "spi_common.h"
+#include "user_fatfs.h"
+#include "spi_flash.h"
+#include "usb_eth.h"
+#include "extended_sram.h"
+#include "modbus.h"
+#include "common_gpio.h"
+#include "io.h"
+#include "input.h"
+#include "output.h"
+#include "sys_api.h"
+#include "settings_api.h"
+#include "update.h"
+#include "uptime.h"
+#include "rtc.h"
+#include "mb.h"
+#include "io_utils.h"
+#include "buttons.h"
+#include "adc_transport.h"
+#include "shift_reg.h"
+#include "analog_input.h"
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+
+
+
+
 #endif

BIN
output/fw.bin


La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 360 - 364
project/ewarm/iap/iap.dep


La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 793 - 769
project/ewarm/module_universal_io.dep


+ 3 - 0
project/ewarm/module_universal_io.ewp

@@ -2167,6 +2167,9 @@
             </group>
             <group>
                 <name>io</name>
+                <file>
+                    <name>$PROJ_DIR$\..\..\fw\modules\io\analog_input.c</name>
+                </file>
                 <file>
                     <name>$PROJ_DIR$\..\..\fw\modules\io\input.c</name>
                 </file>

+ 3 - 0
project/ewarm/module_universal_io.ewt

@@ -2384,6 +2384,9 @@
             </group>
             <group>
                 <name>io</name>
+                <file>
+                    <name>$PROJ_DIR$\..\..\fw\modules\io\analog_input.c</name>
+                </file>
                 <file>
                     <name>$PROJ_DIR$\..\..\fw\modules\io\input.c</name>
                 </file>

+ 40 - 33
shared/utils/utility.c

@@ -1,33 +1,40 @@
-#include "FreeRTOS.h"
-#include "task.h"
-#include "queue.h"
-#include "semphr.h"
-#include "utility.h"
-#include "at32f403a_407_int.h"
-
-#ifdef PRINTF_STDLIB
-#include <stdio.h>
-#endif
-#ifdef PRINTF_CUSTOM
-#include "tinystdio.h"
-#endif
-
-#if 1
-// OS stack overflow hook
-#if (configCHECK_FOR_STACK_OVERFLOW > 0)
-void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
-{
-    NVIC_SystemReset();
-}
-#endif
-#endif
-
-// OS malloc failed hook
-#if (configUSE_MALLOC_FAILED_HOOK > 0)
-void vApplicationMallocFailedHook(void)
-{
-    NVIC_SystemReset();
-    while(1);
-}
-#endif
-
+#include "FreeRTOS.h"
+#include "task.h"
+#include "queue.h"
+#include "semphr.h"
+#include "utility.h"
+#include "at32f403a_407_int.h"
+#include <stdio.h>
+
+
+#if 1
+// OS stack overflow hook
+#if (configCHECK_FOR_STACK_OVERFLOW > 0)
+void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
+{
+    NVIC_SystemReset();
+}
+#endif
+#endif
+
+// OS malloc failed hook
+#if (configUSE_MALLOC_FAILED_HOOK > 0)
+void vApplicationMallocFailedHook(void)
+{
+    NVIC_SystemReset();
+    while(1);
+}
+#endif
+
+
+//
+void print_binary_byte(uint8_t val)
+{
+    for (uint8_t i = 0; i < 8; i++)
+    {
+        if (val & (1 << i))
+            printf("1");
+        else
+            printf("0");
+    }
+}

+ 11 - 55
shared/utils/utility.h

@@ -1,55 +1,11 @@
-#ifndef __UTILITY_H_
-#define __UTILITY_H_
-
-
-#include "common_config.h"
-#include <stdbool.h>
-
-
-typedef enum 
-{
-    TR_GSM_TX_SIM1 = BPR_DATA5,
-    TR_GSM_RX_SIM1 = BPR_DATA7,
-    TR_GSM_TX_SIM2 = BPR_DATA9,
-    TR_GSM_RX_SIM2 = BPR_DATA11, 
-    TR_ETH_TX      = BPR_DATA13,
-    TR_ETH_RX      = BPR_DATA15
-} trf_channel_t;
-
-
-//
-void define_revision(void);
-
-//
-float get_adc(ADC_REV_CH_t channel);
-
-
-// -------------------------------------------------------------------------- //
-// Подсчет трафика
-
-//
-void trf_inc(trf_channel_t channel, uint32_t size);
-
-//
-uint32_t trf_get_ch(trf_channel_t channel);
-
-//
-void trf_print(void);
-
-//
-void trf_reset_all(void);
-
-
-// -------------------------------------------------------------------------- //
-// API поддержки режима 9 бит для счетчиков серии РиМ.
-
-//
-void set_recv_byte_flag(bool state);
-
-//
-bool get_recv_byte_flag(void);
-
-//
-void set_len_recv_data(uint16_t len);
-
-#endif /* __UTILITY_H_ */
+#ifndef __UTILITY_H_
+#define __UTILITY_H_
+
+
+//
+void print_binary_byte(uint8_t val);
+
+
+
+
+#endif /* __UTILITY_H_ */

Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio