Created
October 12, 2020 08:55
-
-
Save Heegiiny/2dce73cc4ceb0e8144405d279cb603bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* USER CODE BEGIN 0 */ | |
| uint32_t data_buf_i = 0; | |
| // Буферы данных | |
| #define DATA_BUF_SIZE 1024 | |
| uint8_t data_buf[DATA_BUF_SIZE]; | |
| uint8_t comm_buf[DATA_BUF_SIZE]; | |
| // Буферы для текущего передаваемого байта | |
| #define SPI_TX_BUF_SIZE 1 | |
| uint8_t spi_tx_buf[SPI_TX_BUF_SIZE]; | |
| uint8_t spi_tx_buf2[SPI_TX_BUF_SIZE]; | |
| uint8_t c = 0; | |
| uint8_t d = 0; | |
| uint8_t e = 0; | |
| uint8_t f = 0; | |
| uint8_t c1 = 0; | |
| uint8_t d1 = 0; | |
| uint8_t e1 = 0; | |
| uint8_t f1 = 0; | |
| void _f1() { | |
| if (f == 0 && data_buf_i > 30 && data_buf_i < 37) { | |
| if (c == 0 && data_buf[data_buf_i] == 192) { | |
| c = 1; | |
| return; | |
| } | |
| if (c == 1 && d == 0) { | |
| d = 1; | |
| return; | |
| } | |
| if (c == 1 && d == 1 && e == 0) { | |
| // Нижнее давление | |
| CDC_Transmit_FS(&data_buf[data_buf_i], 1); | |
| e = 1; | |
| return; | |
| } | |
| if (c == 1 && d == 1 && e == 1 && f == 0) { | |
| // Верхнее давление | |
| CDC_Transmit_FS(&data_buf[data_buf_i], 1); | |
| f = 1; | |
| return; | |
| } | |
| } | |
| if (f1 == 0 && data_buf_i > 50 && data_buf_i < 57) { | |
| if (c1 == 0 && data_buf[data_buf_i] == 192) { | |
| c1 = 1; | |
| return; | |
| } | |
| if (c1 == 1 && d1 == 0) { | |
| d1 = 1; | |
| return; | |
| } | |
| if (c1 == 1 && d1 == 1 && e1 == 0) { | |
| // Пульс | |
| CDC_Transmit_FS(&data_buf[data_buf_i], 1); | |
| e1 = 1; | |
| return; | |
| } | |
| if (c1 == 1 && d1 == 1 && e1 == 1 && f1 == 0) { | |
| // Пульс 2 | |
| //CDC_Transmit_FS(&data_buf[data_buf_i], 1); | |
| f1 = 1; | |
| return; | |
| } | |
| } | |
| } | |
| void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) | |
| { | |
| if(hspi == &hspi1) | |
| { | |
| if(hspi1.TxXferCount==0) | |
| { | |
| spi1_rx(); | |
| } | |
| } | |
| if(hspi == &hspi2) | |
| { | |
| if(hspi2.TxXferCount==0) | |
| { | |
| spi2_rx(); | |
| data_buf[data_buf_i] = spi_tx_buf2[0]; | |
| comm_buf[data_buf_i] = spi_tx_buf[0] >> 5; | |
| _f1(); | |
| /* | |
| if (data_buf_i == 33) { | |
| // Нижнее давление | |
| CDC_Transmit_FS(spi_tx_buf2[0], 1); | |
| } | |
| if (data_buf_i == 34) { | |
| // Верхнее давление | |
| CDC_Transmit_FS(spi_tx_buf2[0], 1); | |
| } | |
| if (data_buf_i == 52) { | |
| // Пульс | |
| if (spi_tx_buf2[0] == 255) { | |
| CDC_Transmit_FS(data_buf[54], 1); | |
| } else { | |
| CDC_Transmit_FS(spi_tx_buf2[0], 1); | |
| } | |
| }*/ | |
| data_buf_i++; | |
| if (data_buf_i == 1023) { | |
| data_buf_i = 0; | |
| } | |
| } | |
| } | |
| } | |
| // Заполняем память в буферах нулями | |
| void clear_mem() { | |
| memset(data_buf, 0, DATA_BUF_SIZE); | |
| memset(comm_buf, 0, DATA_BUF_SIZE); | |
| memset(spi_tx_buf, 0, SPI_TX_BUF_SIZE); | |
| memset(spi_tx_buf2, 0, SPI_TX_BUF_SIZE); | |
| } | |
| /** Активирования приема по SPI */ | |
| void spi1_rx() { | |
| HAL_StatusTypeDef spi_status; | |
| spi_status = HAL_SPI_Receive_IT(&hspi1, spi_tx_buf, SPI_TX_BUF_SIZE); | |
| } | |
| void spi2_rx() { | |
| HAL_StatusTypeDef spi_status2; | |
| spi_status2 = HAL_SPI_Receive_IT(&hspi2, spi_tx_buf2, SPI_TX_BUF_SIZE); | |
| } | |
| // Обработка сигнала тактирования SPI | |
| void spi_clk_proc() { | |
| uint8_t prev_nss = 1; | |
| HAL_GPIO_WritePin(SCK_ON_OUT_GPIO_Port, SCK_ON_OUT_Pin, GPIO_PIN_RESET); | |
| uint8_t current_pack_clk_count = 0; | |
| uint8_t prev_clk = 0; | |
| while (1) | |
| { | |
| uint8_t curr_clk = 0; | |
| curr_clk = HAL_GPIO_ReadPin(SCK_COPY_IN_GPIO_Port, SCK_COPY_IN_Pin); | |
| uint8_t curr_nss = 1; | |
| curr_nss = HAL_GPIO_ReadPin(NSS_IN_GPIO_Port, NSS_IN_Pin); | |
| if (curr_nss == 0 && prev_nss == 1) { | |
| // Новый пакет | |
| current_pack_clk_count = 0; | |
| } | |
| if (curr_nss == 1 && prev_nss == 0) { | |
| // Конец пакета | |
| current_pack_clk_count = 0; | |
| HAL_GPIO_WritePin(SCK_ON_OUT_GPIO_Port, SCK_ON_OUT_Pin, GPIO_PIN_RESET); | |
| } | |
| if (curr_clk == 0 && prev_clk == 1) { | |
| current_pack_clk_count++; | |
| if (current_pack_clk_count > 2) { | |
| HAL_GPIO_WritePin(SCK_ON_OUT_GPIO_Port, SCK_ON_OUT_Pin, GPIO_PIN_SET); | |
| } | |
| } | |
| prev_nss = curr_nss; | |
| prev_clk = curr_clk; | |
| } | |
| } | |
| void user_code_2() { | |
| clear_mem(); | |
| spi1_rx(); | |
| spi2_rx(); | |
| spi_clk_proc(); | |
| } | |
| /* USER CODE END 0 */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment