Загрузка данных


#include "MDR32Fx.h"
#include "MDR32F9Qx_config.h"
#include "MDR32F9Qx_uart.h"
#include "MDR32F9Qx_port.h"
#include "MDR32F9Qx_rst_clk.h"

static PORT_InitTypeDef PortInit;
static UART_InitTypeDef UART_InitStructure;

// ?????????? ?????? ?? ?????????
uint8_t Recieve_buff[256];
volatile uint8_t Recieve_Wr = 0, Recieve_Rd = 0, Recieve_Cnt = 0;

// ?????????? ?????????? UART1 (??? ?? ???. 57)
void UART1_IRQHandler (void) {
    if (UART_GetITStatus(MDR_UART1, UART_IT_RX) == SET) {
        UART_ClearITPendingBit(MDR_UART1, UART_IT_RX);
        
        Recieve_buff[Recieve_Wr++] = (uint8_t)UART_ReceiveData(MDR_UART1);
        Recieve_Cnt++;
    }
}

// ??????? ????????????? ??????????? (??????? ?? PORTA)
void init_leds(void) {
    RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTA, ENABLE);
    PortInit.PORT_Pin   = PORT_Pin_All;
    PortInit.PORT_OE    = PORT_OE_OUT;
    PortInit.PORT_FUNC  = PORT_FUNC_PORT;
    PortInit.PORT_MODE  = PORT_MODE_DIGITAL;
    PortInit.PORT_SPEED = PORT_SPEED_SLOW;
    PORT_Init(MDR_PORTA, &PortInit);
    PORT_Write(MDR_PORTA, 0xFF); // ????? ???
}

// ??????? ????????????? UART (?? PORTB)
void init_UART(void) {
    RST_CLK_PCLKcmd(RST_CLK_PCLK_PORTB | RST_CLK_PCLK_UART1, ENABLE);
    
    // ????????? ????? ????? B ??? UART (PB5 - TX, PB6 - RX)
    PortInit.PORT_Pin   = PORT_Pin_5 | PORT_Pin_6;
    PortInit.PORT_FUNC  = PORT_FUNC_ALTER; // ?????????????? ???????
    PortInit.PORT_MODE  = PORT_MODE_DIGITAL;
    PORT_Init(MDR_PORTB, &PortInit);
    
    // ???????????? UART (???????? 28800)
    UART_StructInit(&UART_InitStructure);
    UART_InitStructure.UART_BaudRate            = 28800; // ???? ???????
    UART_InitStructure.UART_WordLength          = UART_WordLength8b;
    UART_InitStructure.UART_StopBits            = UART_StopBits1;
    UART_InitStructure.UART_Parity              = UART_Parity_No;
    UART_InitStructure.UART_FIFOMode            = UART_FIFO_ON;
    UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_RXE | UART_HardwareFlowControl_TXE;
    
    UART_Init(MDR_UART1, &UART_InitStructure);
    UART_BRGInit(MDR_UART1, UART_HCLKdiv1);
    
    // ???????? ?????????? ? ??? UART
    NVIC_EnableIRQ(UART1_IRQn);
    UART_ITConfig(MDR_UART1, UART_IT_RX, ENABLE);
    UART_Cmd(MDR_UART1, ENABLE);
}

// ??????????????? ??????? ??? ?????? ????? ?? ?????????? ??????
uint8_t GetByte(void) {
    uint8_t byte = Recieve_buff[Recieve_Rd++];
    Recieve_Cnt--;
    return byte;
}

int main (void) {
    init_leds();
    init_UART();

    while (1) {
        // ????? ???????? 8 ????? ????? 7 ????: 0A 11 0A 15 [Data] 85 [CRC]
        if (Recieve_Cnt >= 7) {
            // ???? ?????? ?????????
            if (Recieve_buff[Recieve_Rd] == 0x0A) {
                uint8_t b1 = GetByte(); // 0x0A
                uint8_t b2 = GetByte(); // 0x11
                uint8_t b3 = GetByte(); // 0x0A
                uint8_t b4 = GetByte(); // 0x15
                
                if (b2 == 0x11 && b3 == 0x0A && b4 == 0x15) {
                    uint8_t data = GetByte(); // ???? ??????
                    uint8_t footer = GetByte(); // ?????? ???? 0x85
                    uint8_t received_crc = GetByte(); // ???????? ??????????? ?????
                    
                    if (footer == 0x85) {
                        // ??????? CRC ?? ????? ???????
                        uint32_t sum = b1 + b2 + b3 + b4 + data + footer;
                        uint8_t my_crc = (uint8_t)(0x100 - (sum & 0xFF));
                        
                        if (my_crc == received_crc) {
                            PORT_Write(MDR_PORTA, ~data); // ??? ?? - ??????? ?? ??????????
                        }
                    }
                }
            } else {
                // ???? ?????? ???? ?? 0x0A, ?????? ???????? ???????
                GetByte();
            }
        }
    }
}