Ver código fonte

Работа с ringfs. Добавил время в мс.

TelenkovDmitry 10 meses atrás
pai
commit
25bc04e3a6

+ 48 - 12
fw/modules/log/log.c

@@ -91,7 +91,7 @@ void log_init(bool format)
     // Архив
     
 	ringfs_flash_archive.sector_size = spi_flash_desc.sector_size;
-	ringfs_flash_archive.sector_count = ARCHIVE_FLASH_SECTOR_COUNT;
+	ringfs_flash_archive.sector_count = 2; //ARCHIVE_FLASH_SECTOR_COUNT;
 
 	ringfs_init(&fs_archive, &ringfs_flash_archive, ARCHIV_ENTRY_VERSION, sizeof(archive_entry_t));
     
@@ -148,25 +148,34 @@ int log_discard(void *entry, entry_type_t entry_type, uint32_t timeout)
     return ret; 
 }
 
-// TODO unixtime ms
+//
 int log_append(void *entry, entry_type_t entry_type)
 {
     int ret;
     TM_RTC_t time;
     common_entry_t *entry_ptr = entry;
+    log_entry_t *log_etnry_ptr;
+    archive_entry_t *archive_etnry_ptr;
     
     ret = xSemaphoreTake(log_mutex, portMAX_DELAY);
     
     if (ret == pdFALSE)
         return ret;
-    if (!entry_ptr->timestamp) {
-        TM_RTC_GetDateTime(&time, TM_RTC_Format_BIN);
-        entry_ptr->timestamp = time.unix;
-    }
-    if (entry_type == LOG_ENTRY)
+  
+    entry_ptr->timestamp = rtc_get_ms();
+    
+    if (entry_type == LOG_ENTRY) 
+    {
+        log_etnry_ptr = entry;
+        log_etnry_ptr->crc = crc_8(entry, sizeof(log_entry_t) - 1);
         ret = ringfs_append(&fs_log, entry);
-    else if (entry_type == ARCHIVE_ENTRY)
+    }
+    else if (entry_type == ARCHIVE_ENTRY) 
+    {
+        archive_etnry_ptr = entry;
+        archive_etnry_ptr->crc = crc_8(entry, sizeof(archive_entry_t) - 1);
         ret = ringfs_append(&fs_archive, entry);
+    }
     else ret = -1;
     
     xSemaphoreGive(log_mutex);
@@ -174,6 +183,29 @@ int log_append(void *entry, entry_type_t entry_type)
     
 }
 
+// -------------------------------------------------------------------------- //
+// misc
+
+uint8_t crc_8(uint8_t *data, int length)
+{
+    uint8_t crc = 0x00;
+    uint8_t extract;
+    uint8_t sum;
+    
+    for (int i = 0; i < length; i++) {
+        extract = *data;
+        for (uint8_t tmp = 8; tmp; tmp--) {
+            sum = (crc ^ extract) & 0x01;
+            crc >>= 1;
+            if (sum)
+                crc ^= 0x8C;
+            extract >>= 1;
+        }
+        data++;
+    }
+    return crc;
+}
+
 // -------------------------------------------------------------------------- //
 // Tests
 
@@ -223,14 +255,13 @@ int test_archive(void)
 int test_add_random_archive_entry(uint32_t cnt_entry)
 {
     int ret;
-    archive_entry_t entry;
+    archive_entry_t entry= {0};
     
     DBG printf("Try append %u archive entry\r\n", cnt_entry);
 
     for (uint32_t i = 0; i < cnt_entry; i++)
     {
         entry.input_value = ringfs_count_exact(&fs_archive);
-        entry.crc = 0xAB;
         
         ret = log_append(&entry, ARCHIVE_ENTRY);
     }
@@ -244,7 +275,8 @@ void test_fetch(void)
 {
     archive_entry_t entry = {0};
     log_fetch(&entry, ARCHIVE_ENTRY, portMAX_DELAY);
-    printf("[entry] timestamp = %u, value = %u, crc = %u\r\n", (uint32_t)entry.timestamp, entry.input_value, entry.crc);
+    //printf("\r\n%" PRId64 " [ms]\r\n", rtc_get_ms());
+    printf("[entry] timestamp = % " PRId64 ", value = %u, crc = %u\r\n", entry.timestamp, entry.input_value, entry.crc);
 }
 
 //
@@ -269,7 +301,11 @@ void test_archive_format(void)
     ringfs_format(&fs_archive);
 }
 
-
+//
+void test_print_all_archive(void)
+{
+    
+}
 
 
 

+ 16 - 1
fw/modules/log/log.h

@@ -71,6 +71,7 @@ typedef __packed struct
 	uint64_t timestamp;
 	log_type_t type:8;
 	char data[50];
+    uint8_t crc;
     
 } log_entry_t;
 
@@ -83,9 +84,23 @@ typedef __packed struct
 } archive_entry_t;
 
 
-
+//
 void log_init(bool format);
 
+//
+int log_fetch(void *entry, entry_type_t entry_type, uint32_t timeout);
+
+//
+int log_discard(void *entry, entry_type_t entry_type, uint32_t timeout);
+
+// 
+int log_append(void *entry, entry_type_t entry_type);
+
+
+// -------------------------------------------------------------------------- //
+// misc
+
+uint8_t crc_8(uint8_t *data, int length);
 
 // -------------------------------------------------------------------------- //
 // Tests

+ 63 - 2
fw/modules/misc/rtc.c

@@ -73,6 +73,10 @@ uint8_t TM_RTC_Init(void)
         // wait for the register write to complete
         rtc_wait_config_finish();
         
+        // enable the rtc second
+        nvic_irq_enable(RTC_IRQn, 6, 0);
+        rtc_interrupt_enable(RTC_TS_INT, TRUE);
+                
         // set rtc divider: set rtc period to 1sec 
         rtc_divider_set(32767);
         
@@ -103,6 +107,10 @@ uint8_t TM_RTC_Init(void)
         // wait for the register write to complete
         rtc_wait_config_finish();
     
+        // enable the rtc second
+        nvic_irq_enable(RTC_IRQn, 6, 0);
+        rtc_interrupt_enable(RTC_TS_INT, TRUE);
+        
         return 0;    
     }
 }
@@ -331,7 +339,6 @@ void TM_RTC_GetDateTimeFromUnix(TM_RTC_t* data, uint32_t unix)
 	data->date = unix + 1;
 }
 
-
 //
 void TM_RTC_PrintTime(void)
 {
@@ -346,7 +353,6 @@ void TM_RTC_PrintTime(void)
 
 }
 
-
 //
 uint32_t RTC_GetUnixTime(void)
 {
@@ -356,3 +362,58 @@ uint32_t RTC_GetUnixTime(void)
     return TM_RTC_GetUnixTimeStamp(&currentTime);
 }
 
+//
+void rtc_subtim_init(void)
+{
+    crm_clocks_freq_type crm_clocks_freq_struct = {0};
+    
+    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_cnt_dir_set(TMR5, TMR_COUNT_UP);
+
+    tmr_flag_clear(TMR5, TMR_OVF_FLAG);
+    
+    NVIC_ClearPendingIRQ(TMR5_GLOBAL_IRQn);
+    nvic_irq_enable(TMR5_GLOBAL_IRQn, 5, 0);
+       
+    tmr_counter_enable(TMR5, TRUE);
+    tmr_interrupt_enable(TMR5, TMR_OVF_INT, TRUE);
+}
+
+//
+uint64_t rtc_get_ms(void)
+{
+    return ((uint64_t)RTC_GetUnixTime()*1000 + TMR5->cval/10);
+}
+
+//
+uint32_t rtc_foo(void)
+{
+    return tmr_counter_value_get(TMR5);
+}
+
+//
+void TMR5_GLOBAL_IRQHandler(void)
+{
+    if (tmr_flag_get(TMR5, TMR_OVF_FLAG) != RESET)
+    {
+        tmr_flag_clear(TMR5, TMR_OVF_FLAG);
+        tmr_interrupt_enable(TMR5, TMR_OVF_INT, FALSE);
+        tmr_counter_enable(TMR5, FALSE);
+    }
+}
+
+//
+void RTC_IRQHandler(void)
+{
+    if (rtc_flag_get(RTC_TS_FLAG) != RESET)
+    {
+        rtc_flag_clear(RTC_TS_FLAG);
+        
+        tmr_interrupt_enable(TMR5, TMR_OVF_INT, TRUE);
+        tmr_counter_enable(TMR5, TRUE);
+        TMR5->cval = 0;
+    }
+}

+ 80 - 79
fw/modules/misc/rtc.h

@@ -1,79 +1,80 @@
-#ifndef __RTC_H
-#define __RTC_H
-
-
-#include "at32f403a_407.h"
-
-
-#define TM_RTC_LEAP_YEAR(year) 			((((year) % 4 == 0) && ((year) % 100 != 0)) || ((year) % 400 == 0))
-
-
-
-typedef struct
-{
-    __IO uint8_t  seconds;      /*!< Seconds parameter, from 00 to 59 */
-    __IO uint16_t subseconds;   /*!< Subsecond downcounter. When it reaches zero, it's reload value is the same as
-                                     @ref RTC_SYNC_PREDIV, so in our case 0x3FF = 1023, 1024 steps in one second */
-	__IO uint8_t  minutes;      /*!< Minutes parameter, from 00 to 59 */
-	__IO uint8_t  hours;        /*!< Hours parameter, 24Hour mode, 00 to 23 */
-	__IO uint8_t  day;          /*!< Day in a week, from 1 to 7 */
-	__IO uint8_t  date;         /*!< Date in a month, 1 to 31 */
-	__IO uint8_t  month;        /*!< Month in a year, 1 to 12 */
-	__IO uint8_t  year;         /*!< Year parameter, 00 to 99, 00 is 2000 and 99 is 2099 */
-	__IO uint32_t unix;         /*!< Seconds from 01.01.1970 00:00:00 */
-  
-} TM_RTC_t;
-
-
-// RTC Result enumeration
-typedef enum {
-	TM_RTC_Result_Ok,           /*!< Everything OK */
-	TM_RTC_Result_Error         /*!< An error occurred */
-} TM_RTC_Result_t;
-
-
-
-// RTC date and time format
-typedef enum {
-	TM_RTC_Format_BIN = 0x00, /*!< RTC data in binary format */
-	TM_RTC_Format_BCD         /*!< RTC data in binary-coded decimal format */
-} TM_RTC_Format_t;
-
-
-
-//
-uint8_t TM_RTC_Init(void);
-
-//
-void TM_RTC_SetDataTimeUnix(uint32_t unixTime);
-
-//
-TM_RTC_Result_t TM_RTC_SetDateTime(TM_RTC_t* data);
-
-//
-TM_RTC_Result_t TM_RTC_SetDateTimeString(char* str);
-
-//
-void TM_RTC_GetDateTime(TM_RTC_t* data, TM_RTC_Format_t format);
-
-//
-uint32_t TM_RTC_GetUnixTimeStamp(TM_RTC_t* data);
-
-//
-void TM_RTC_GetDateTimeFromUnix(TM_RTC_t* data, uint32_t unix);
-
-//
-void TM_RTC_PrintTime(void);
-
-//
-uint32_t RTC_GetUnixTime(void);
-
-#if 0
-//
-void sntp_config(void);
-
-//
-void lwip_time_sinhro(uint32_t utime);
-#endif
-
-#endif
+#ifndef __RTC_H
+#define __RTC_H
+
+
+#include "at32f403a_407.h"
+
+
+#define TM_RTC_LEAP_YEAR(year) 			((((year) % 4 == 0) && ((year) % 100 != 0)) || ((year) % 400 == 0))
+
+
+
+typedef struct
+{
+    __IO uint8_t  seconds;      /*!< Seconds parameter, from 00 to 59 */
+    __IO uint16_t subseconds;   /*!< Subsecond downcounter. When it reaches zero, it's reload value is the same as
+                                     @ref RTC_SYNC_PREDIV, so in our case 0x3FF = 1023, 1024 steps in one second */
+	__IO uint8_t  minutes;      /*!< Minutes parameter, from 00 to 59 */
+	__IO uint8_t  hours;        /*!< Hours parameter, 24Hour mode, 00 to 23 */
+	__IO uint8_t  day;          /*!< Day in a week, from 1 to 7 */
+	__IO uint8_t  date;         /*!< Date in a month, 1 to 31 */
+	__IO uint8_t  month;        /*!< Month in a year, 1 to 12 */
+	__IO uint8_t  year;         /*!< Year parameter, 00 to 99, 00 is 2000 and 99 is 2099 */
+	__IO uint32_t unix;         /*!< Seconds from 01.01.1970 00:00:00 */
+  
+} TM_RTC_t;
+
+
+// RTC Result enumeration
+typedef enum {
+	TM_RTC_Result_Ok,           /*!< Everything OK */
+	TM_RTC_Result_Error         /*!< An error occurred */
+} TM_RTC_Result_t;
+
+
+
+// RTC date and time format
+typedef enum {
+	TM_RTC_Format_BIN = 0x00, /*!< RTC data in binary format */
+	TM_RTC_Format_BCD         /*!< RTC data in binary-coded decimal format */
+} TM_RTC_Format_t;
+
+
+
+//
+uint8_t TM_RTC_Init(void);
+
+//
+void TM_RTC_SetDataTimeUnix(uint32_t unixTime);
+
+//
+TM_RTC_Result_t TM_RTC_SetDateTime(TM_RTC_t* data);
+
+//
+TM_RTC_Result_t TM_RTC_SetDateTimeString(char* str);
+
+//
+void TM_RTC_GetDateTime(TM_RTC_t* data, TM_RTC_Format_t format);
+
+//
+uint32_t TM_RTC_GetUnixTimeStamp(TM_RTC_t* data);
+
+//
+void TM_RTC_GetDateTimeFromUnix(TM_RTC_t* data, uint32_t unix);
+
+//
+void TM_RTC_PrintTime(void);
+
+//
+uint32_t RTC_GetUnixTime(void);
+
+//
+void rtc_subtim_init(void);
+
+//
+uint64_t rtc_get_ms(void);
+
+//
+uint32_t rtc_foo(void);
+
+#endif

+ 49 - 49
fw/modules/misc/uptime.c

@@ -1,49 +1,49 @@
-#include "uptime.h"
-#include "rtc.h"
-#include "io_utils.h"
-#include <stdio.h>
-
-
-uint32_t uptime = 0;
-uint32_t rtc_unix = 0;
-
-//
-void get_uptime(uint32_t *value)
-{
-    *value = uptime;
-}
-
-
-//
-void uptime_init(void)
-{
-    crm_clocks_freq_type crm_clocks_freq_struct = {0};
-    
-    crm_periph_clock_enable(CRM_TMR10_PERIPH_CLOCK, TRUE);
-
-    crm_clocks_freq_get(&crm_clocks_freq_struct);
-    tmr_base_init(TMR10, 9999, (crm_clocks_freq_struct.ahb_freq / 10000) - 1);
-    tmr_cnt_dir_set(TMR10, TMR_COUNT_UP);
-        
-    tmr_flag_clear(TMR10, TMR_OVF_FLAG);
-
-    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
-    nvic_irq_enable(TMR1_OVF_TMR10_IRQn, 5, 0);
-       
-    tmr_counter_enable(TMR10, TRUE);
-    
-    tmr_interrupt_enable(TMR10, TMR_OVF_INT, TRUE);
-}
-
-
-void TMR1_OVF_TMR10_IRQHandler(void)
-{
-    if(tmr_flag_get(TMR10, TMR_OVF_FLAG) != RESET)
-    {
-        tmr_flag_clear(TMR10, TMR_OVF_FLAG);
-        uptime++;
-        rtc_unix = RTC_GetUnixTime();
-        save_mode_inc_cnt();
-    }
-}
-
+#include "uptime.h"
+#include "rtc.h"
+#include "io_utils.h"
+#include <stdio.h>
+
+
+uint32_t uptime = 0;
+uint32_t rtc_unix = 0;
+
+//
+void get_uptime(uint32_t *value)
+{
+    *value = uptime;
+}
+
+
+//
+void uptime_init(void)
+{
+    crm_clocks_freq_type crm_clocks_freq_struct = {0};
+    
+    crm_periph_clock_enable(CRM_TMR10_PERIPH_CLOCK, TRUE);
+
+    crm_clocks_freq_get(&crm_clocks_freq_struct);
+    tmr_base_init(TMR10, 9999, (crm_clocks_freq_struct.ahb_freq / 10000) - 1);
+    tmr_cnt_dir_set(TMR10, TMR_COUNT_UP);
+        
+    tmr_flag_clear(TMR10, TMR_OVF_FLAG);
+
+    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
+    nvic_irq_enable(TMR1_OVF_TMR10_IRQn, 5, 0);
+       
+    tmr_counter_enable(TMR10, TRUE);
+    
+    tmr_interrupt_enable(TMR10, TMR_OVF_INT, TRUE);
+}
+
+
+void TMR1_OVF_TMR10_IRQHandler(void)
+{
+    if (tmr_flag_get(TMR10, TMR_OVF_FLAG) != RESET)
+    {
+        tmr_flag_clear(TMR10, TMR_OVF_FLAG);
+        uptime++;
+        rtc_unix = RTC_GetUnixTime();
+        save_mode_inc_cnt();
+    }
+}
+

+ 18 - 2
fw/modules/terminal/terminal_sbs.cpp

@@ -4,9 +4,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
+#include <inttypes.h>
 
 extern "C" {
-    #include "log.h"
+#include "log.h"
+#include "rtc.h"  
 }
 
 SbsTerminal sbsTerminal;
@@ -44,6 +46,9 @@ int SbsTerminal::execute(int argc, const char * const *argv)
     if (strcmp(argv[0], "version") == 0) {
         return version(argc, argv);
     }
+    if (strcmp(argv[0], "reset") == 0) {
+        NVIC_SystemReset();
+    }
     // ---------------------------------------------------------------------- //
     // Archive API
     
@@ -71,6 +76,11 @@ int SbsTerminal::execute(int argc, const char * const *argv)
         test_archive_format();
         return 0;
     }
+    // ---------------------------------------------------------------------- //
+    if (strcmp(argv[0], "mstime") == 0) {
+        printf("\r\n%" PRId64 " [ms]\r\n", rtc_get_ms());
+        return 0;
+    }
     
     // ---------------------------------------------------------------------- //
     else {
@@ -103,7 +113,13 @@ int SbsTerminal::help(int argc, const char * const *argv)
     printeol();
     printeol();
     printl ("You can use the following commands:");
-    printll("  version        Print software version");
+    printl ("  version        Print software version");
+    printl ("  reset          Reset");
+    printl ("  add_aentry     Add N archive entrys");
+    printl ("  ainfo          Print archive info");
+    printl ("  afetch         Fetch archive entry");
+    printll("  aformat        Format archive partition");
+    
     printeol();
 
     return 0;

+ 19 - 17
fw/user/main.cpp

@@ -64,22 +64,17 @@ int main(void)
     delay_init();
 
     // -------------------------------------------------------------------------
-    // Debug
-    //uart_print_init(115200);
+    // CLI
     sbsTerminal.configure();
     terminalUsartBridge.configure();
-    
-    
-    //usb_clock48m_select(USB_CLK_HEXT);
-    
+
+    // -------------------------------------------------------------------------
+    // USB
+#if 0    
+    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();
-      
+    usb_init();
+#endif      
         
 #if 1
     taskENTER_CRITICAL();      
@@ -97,10 +92,14 @@ int main(void)
     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);
-    
+
+#if defined (MAI_12)    
+    xTaskCreate(adc_task, "adc_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+#endif
+
+#if defined (MAO_8)    
     xTaskCreate(dac_task, "dac_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+#endif    
     
     taskEXIT_CRITICAL();
     
@@ -162,6 +161,7 @@ void init_task(void *argument)
 // RTC    
     
     TM_RTC_Init();
+    rtc_subtim_init();
     
 // -------------------------------------------------------------------------- //    
 // Мультиплексор
@@ -292,8 +292,10 @@ void test_gpio(void *params)
     
     for (;;)
     {
-        vTaskDelay(1000);
+        vTaskDelay(100);
         
+        //printf("%" PRId64 " \r\n", rtc_get_ms());
+
         //en_crnt_alrm_in(0x02);
         
         //io_test();

+ 1 - 0
libs/thirdparty/ringfs/ringfs.c

@@ -174,6 +174,7 @@ int ringfs_format(struct ringfs *fs)
     fs->write.slot = 0;
     fs->cursor.sector = 0;
     fs->cursor.slot = 0;
+    fs->cursor_position = 0;
 
     return 0;
 }

BIN
output/fw.bin


Diferenças do arquivo suprimidas por serem muito extensas
+ 372 - 372
project/ewarm/iap/iap.dep


Diferenças do arquivo suprimidas por serem muito extensas
+ 727 - 727
project/ewarm/module_universal_io.dep


+ 77 - 77
shared/freemodbus/port/tim_delay.c

@@ -1,77 +1,77 @@
-#include "at32f403a_407.h"
-#include "tim_delay.h"
-#include "mux.h"
-
-
-static bool tx_enable = false;
-
-
-//
-void mb_helper_tim_init(uint32_t baudrate)
-{
-    float foo;
-    
-    foo = 1.0/((float)baudrate / 11.0);
-    foo *= 2000.0; // время в мс (длительность в два символа)
-    
-    crm_clocks_freq_type crm_clocks_freq_struct = {0};
-    
-    crm_clocks_freq_get(&crm_clocks_freq_struct);
-    
-    nvic_irq_disable(TMR6_GLOBAL_IRQn);
-    
-    crm_periph_clock_enable(CRM_TMR6_PERIPH_CLOCK, TRUE);
-
-    tmr_base_init(TMR6, (uint32_t)(foo * 1000) - 1, (crm_clocks_freq_struct.ahb_freq / 1000000) - 1);
-    tmr_cnt_dir_set(TMR6, TMR_COUNT_UP);
-    
-    NVIC_ClearPendingIRQ(TMR6_GLOBAL_IRQn);
-    nvic_irq_enable(TMR6_GLOBAL_IRQn, 5, 0);
-}
-
-
-//
-void mb_helper_tim_enable(void)
-{
-    tmr_flag_clear(TMR6, TMR_OVF_FLAG);
-    tmr_interrupt_enable(TMR6, TMR_OVF_INT, TRUE);
-    tmr_counter_value_set(TMR6, 0);
-    tmr_counter_enable(TMR6, TRUE);  
-}
-
-
-//
-void mb_helper_tim_disable(void)
-{
-    tmr_flag_clear(TMR6, TMR_OVF_FLAG);
-    tmr_interrupt_enable(TMR6, TMR_OVF_INT, FALSE);
-    tmr_counter_value_set(TMR6, 0);
-    tmr_counter_enable(TMR6, FALSE);
-}
-
-
-//
-void mb_helper_set_tx_state(bool state)
-{
-    tx_enable = state;
-}
-
-
-//
-void TMR6_GLOBAL_IRQHandler(void)
-{
-    if(tmr_flag_get(TMR6, TMR_OVF_FLAG) != RESET)
-    {
-        tmr_flag_clear(TMR6, TMR_OVF_FLAG);
-        if (tx_enable) {
-            usart_interrupt_enable(USART3, USART_TDBE_INT, TRUE);
-            leds[TX_R].state = LED_ON;
-        }
-        else {
-            gpio_bits_reset(GPIOD, GPIO_PINS_10);
-            leds[TX_R].state = LED_OFF;
-        }
-        mb_helper_tim_disable();
-    }
-}
-
+#include "at32f403a_407.h"
+#include "tim_delay.h"
+#include "mux.h"
+
+
+static bool tx_enable = false;
+
+
+//
+void mb_helper_tim_init(uint32_t baudrate)
+{
+    float foo;
+    
+    foo = 1.0/((float)baudrate / 11.0);
+    foo *= 2000.0; // время в мс (длительность в два символа)
+    
+    crm_clocks_freq_type crm_clocks_freq_struct = {0};
+    
+    crm_clocks_freq_get(&crm_clocks_freq_struct);
+    
+    nvic_irq_disable(TMR6_GLOBAL_IRQn);
+    
+    crm_periph_clock_enable(CRM_TMR6_PERIPH_CLOCK, TRUE);
+
+    tmr_base_init(TMR6, (uint32_t)(foo * 1000) - 1, (crm_clocks_freq_struct.ahb_freq / 1000000) - 1);
+    tmr_cnt_dir_set(TMR6, TMR_COUNT_UP);
+    
+    NVIC_ClearPendingIRQ(TMR6_GLOBAL_IRQn);
+    nvic_irq_enable(TMR6_GLOBAL_IRQn, 5, 0);
+}
+
+
+//
+void mb_helper_tim_enable(void)
+{
+    tmr_flag_clear(TMR6, TMR_OVF_FLAG);
+    tmr_interrupt_enable(TMR6, TMR_OVF_INT, TRUE);
+    tmr_counter_value_set(TMR6, 0);
+    tmr_counter_enable(TMR6, TRUE);  
+}
+
+
+//
+void mb_helper_tim_disable(void)
+{
+    tmr_flag_clear(TMR6, TMR_OVF_FLAG);
+    tmr_interrupt_enable(TMR6, TMR_OVF_INT, FALSE);
+    tmr_counter_value_set(TMR6, 0);
+    tmr_counter_enable(TMR6, FALSE);
+}
+
+
+//
+void mb_helper_set_tx_state(bool state)
+{
+    tx_enable = state;
+}
+
+
+//
+void TMR6_GLOBAL_IRQHandler(void)
+{
+    if(tmr_flag_get(TMR6, TMR_OVF_FLAG) != RESET)
+    {
+        tmr_flag_clear(TMR6, TMR_OVF_FLAG);
+        if (tx_enable) {
+            usart_interrupt_enable(USART3, USART_TDBE_INT, TRUE);
+            leds[TX_R].state = LED_ON;
+        }
+        else {
+            gpio_bits_reset(GPIOD, GPIO_PINS_10);
+            leds[TX_R].state = LED_OFF;
+        }
+        mb_helper_tim_disable();
+    }
+}
+

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff