|
|
@@ -73,12 +73,20 @@ static void MX_TIM1_Init(void);
|
|
|
#include <string.h>
|
|
|
|
|
|
extern UART_HandleTypeDef huart2;
|
|
|
+//
|
|
|
+//int _write(int file, char *ptr, int len) {
|
|
|
+// HAL_UART_Transmit(&huart2, (uint8_t *)ptr, len, HAL_MAX_DELAY);
|
|
|
+// return len;
|
|
|
+//}
|
|
|
|
|
|
-int _write(int file, char *ptr, int len) {
|
|
|
- HAL_UART_Transmit(&huart2, (uint8_t *)ptr, len, HAL_MAX_DELAY);
|
|
|
- return len;
|
|
|
+int __io_putchar(int ch)
|
|
|
+{
|
|
|
+ // Write character to ITM ch.0
|
|
|
+ ITM_SendChar(ch);
|
|
|
+ return(ch);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
#define RX_DATA_SIZE 4096
|
|
|
#define RC_DATA_SIZE 256
|
|
|
|
|
|
@@ -87,22 +95,73 @@ typedef struct Packet {
|
|
|
uint8_t data[RC_DATA_SIZE];
|
|
|
} Packet;
|
|
|
|
|
|
-static uint8_t rx_buffer[258];
|
|
|
-static uint8_t rx_buffer_idx;
|
|
|
+#define CHUNK_SIZE 256
|
|
|
+
|
|
|
+typedef enum {
|
|
|
+ STATE_WAITING = 0,
|
|
|
+ STATE_DOWNLOADING
|
|
|
+} State;
|
|
|
+
|
|
|
+static State state = STATE_WAITING;
|
|
|
+
|
|
|
+static uint32_t expected_file_size = 0;
|
|
|
+static uint32_t received_size = 0;
|
|
|
+
|
|
|
+static uint8_t rx_size_buf[2]; // буфер для длины
|
|
|
+static uint8_t rx_file_buf[256]; // буфер для данных
|
|
|
+
|
|
|
+void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
|
|
+{
|
|
|
+ if (huart->Instance != USART2) return;
|
|
|
+
|
|
|
+ switch (state)
|
|
|
+ {
|
|
|
+ case STATE_WAITING: {
|
|
|
+ expected_file_size = (uint16_t)rx_size_buf[0]
|
|
|
+ | ((uint16_t)rx_size_buf[1] << 8);
|
|
|
+
|
|
|
+ printf("File size: %u bytes\n", expected_file_size);
|
|
|
+
|
|
|
+ received_size = 0;
|
|
|
+ state = STATE_DOWNLOADING;
|
|
|
+
|
|
|
+ // вычисляем первый блок
|
|
|
+ uint16_t chunk = (expected_file_size - received_size > CHUNK_SIZE)
|
|
|
+ ? CHUNK_SIZE
|
|
|
+ : (expected_file_size - received_size);
|
|
|
|
|
|
-static uint8_t tmp_buffer[258];
|
|
|
+ HAL_UART_Receive_DMA(&huart2, rx_file_buf, chunk);
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
-void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) {
|
|
|
- // memcpy(rx_buffer + (RC_DATA_SIZE * rc_idx), rc_buffer, RC_DATA_SIZE);
|
|
|
+ case STATE_DOWNLOADING: {
|
|
|
+ received_size += huart->RxXferSize;
|
|
|
|
|
|
- // rc_idx += 1;
|
|
|
- // if (rc_idx >= RX_DATA_SIZE) {
|
|
|
- // rc_idx = 0;
|
|
|
- // }
|
|
|
+ printf("Received: %u / %u\n", received_size, expected_file_size);
|
|
|
|
|
|
- HAL_UART_Receive_DMA(&huart2, rx_buffer, sizeof(rx_buffer));
|
|
|
+ if (received_size >= expected_file_size) {
|
|
|
+ state = STATE_WAITING;
|
|
|
+ expected_file_size = 0;
|
|
|
+ received_size = 0;
|
|
|
+ memset(rx_size_buf, 0, sizeof(rx_size_buf));
|
|
|
+ memset(rx_file_buf, 0, sizeof(rx_file_buf));
|
|
|
+
|
|
|
+ HAL_UART_Receive_DMA(&huart2, rx_size_buf, 2);
|
|
|
+
|
|
|
+ printf("Download finished!\n");
|
|
|
+ } else {
|
|
|
+ // вычисляем следующий блок
|
|
|
+ uint16_t remaining = expected_file_size - received_size;
|
|
|
+ uint16_t chunk = (remaining > CHUNK_SIZE) ? CHUNK_SIZE : remaining;
|
|
|
+
|
|
|
+ HAL_UART_Receive_DMA(&huart2, rx_file_buf, chunk);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/* USER CODE END 0 */
|
|
|
|
|
|
/**
|
|
|
@@ -139,7 +198,7 @@ int main(void)
|
|
|
MX_TIM1_Init();
|
|
|
/* USER CODE BEGIN 2 */
|
|
|
|
|
|
- HAL_UART_Receive_DMA(&huart2, rx_buffer, sizeof(rx_buffer));
|
|
|
+ HAL_UART_Receive_DMA(&huart2, rx_size_buf, 2);
|
|
|
|
|
|
// at45db_init();
|
|
|
// uart_receive_loop();
|