|
|
@ -7,14 +7,11 @@ FRESULT result; |
|
|
|
FATFS FATFS_Obj; |
|
|
|
FIL upload_file; |
|
|
|
|
|
|
|
|
|
|
|
volatile uint8_t __attribute__ ((aligned (4))) dma_buff1[1030]; |
|
|
|
volatile uint8_t __attribute__ ((aligned (4))) dma_buff2[1030]; |
|
|
|
volatile uint8_t __attribute__ ((aligned (4))) dma_buff1[ESP_PACKET_SIZE]; |
|
|
|
volatile uint8_t __attribute__ ((aligned (4))) dma_buff2[ESP_PACKET_SIZE]; |
|
|
|
volatile uint8_t *dma_buff[] = {dma_buff1,dma_buff2}; |
|
|
|
volatile uint8_t dma_buff_index=0; |
|
|
|
volatile uint8_t *dma_buff_ptr; |
|
|
|
volatile uint32_t dma_timeout; |
|
|
|
volatile uint8_t *save; |
|
|
|
volatile uint16_t data_size; |
|
|
|
volatile uint8_t *buff; |
|
|
|
|
|
|
|
void mks_wifi_sd_init(void){ |
|
|
|
CardReader::release(); |
|
|
@ -44,7 +41,8 @@ void mks_wifi_start_file_upload(ESP_PROTOC_FRAME *packet){ |
|
|
|
uint32_t file_size, file_inc_size, file_size_writen; |
|
|
|
uint32_t dma_count; |
|
|
|
uint32_t usart1_brr; |
|
|
|
|
|
|
|
uint32_t dma_timeout; |
|
|
|
uint16_t data_size; |
|
|
|
FRESULT res; |
|
|
|
|
|
|
|
mks_wifi_sd_init(); |
|
|
@ -68,10 +66,14 @@ void mks_wifi_start_file_upload(ESP_PROTOC_FRAME *packet){ |
|
|
|
//открыть файл для записи
|
|
|
|
f_open((FIL *)&upload_file,str,FA_CREATE_ALWAYS | FA_WRITE); |
|
|
|
|
|
|
|
ui.set_status((const char *)"Upload file...",true); |
|
|
|
ui.update(); |
|
|
|
|
|
|
|
//Выключить прием по UART RX, включить через DMA, изменить скорость, Выставить флаг приема по DMA
|
|
|
|
USART1->CR1 = 0; |
|
|
|
|
|
|
|
safe_delay(100); |
|
|
|
//Сохранение делителя, чтобы потом восстановить
|
|
|
|
usart1_brr = USART1->BRR; |
|
|
|
|
|
|
|
USART1->CR1 = USART_CR1_UE; |
|
|
@ -80,11 +82,13 @@ void mks_wifi_start_file_upload(ESP_PROTOC_FRAME *packet){ |
|
|
|
USART1->CR3 = USART_CR3_DMAR; |
|
|
|
USART1->CR1 |= USART_CR1_RE; |
|
|
|
|
|
|
|
dma_buff_ptr=(uint8_t*)&dma_buff1; |
|
|
|
dma_buff_index=0; |
|
|
|
memset((uint8_t*)dma_buff[0],0,ESP_PACKET_SIZE); |
|
|
|
memset((uint8_t*)dma_buff[1],0,ESP_PACKET_SIZE); |
|
|
|
|
|
|
|
/*
|
|
|
|
Прием пакета начинается примерно через 2 секунды |
|
|
|
Прием пакета с данными начинается примерно через 2 секунды |
|
|
|
после переключения скорости. |
|
|
|
Без этой тупой задержки, UART успевает принять |
|
|
|
мусор, до пакета с данными и все ломается |
|
|
|
*/ |
|
|
@ -92,8 +96,8 @@ void mks_wifi_start_file_upload(ESP_PROTOC_FRAME *packet){ |
|
|
|
|
|
|
|
DMA1_Channel5->CCR = DMA_CCR_PL|DMA_CCR_MINC; |
|
|
|
DMA1_Channel5->CPAR = (uint32_t)&USART1->DR; |
|
|
|
DMA1_Channel5->CMAR = (uint32_t)dma_buff_ptr; |
|
|
|
DMA1_Channel5->CNDTR = 1024; |
|
|
|
DMA1_Channel5->CMAR = (uint32_t)dma_buff[dma_buff_index]; |
|
|
|
DMA1_Channel5->CNDTR = ESP_PACKET_SIZE; |
|
|
|
DMA1->IFCR = DMA_IFCR_CGIF5|DMA_IFCR_CTEIF5|DMA_IFCR_CHTIF5|DMA_IFCR_CTCIF5; |
|
|
|
DMA1_Channel5->CCR |= DMA_CCR_EN; |
|
|
|
|
|
|
@ -103,52 +107,55 @@ void mks_wifi_start_file_upload(ESP_PROTOC_FRAME *packet){ |
|
|
|
file_size_writen = 0; //Счетчик записанных в файл данных
|
|
|
|
|
|
|
|
dma_timeout = DMA_TIMEOUT; //Тайм-аут, на случай если передача зависла.
|
|
|
|
while(dma_timeout > 0){ |
|
|
|
|
|
|
|
while(dma_timeout-- > 0){ |
|
|
|
|
|
|
|
if(DMA1->ISR & DMA_ISR_TCIF5){ |
|
|
|
DMA1->IFCR = DMA_IFCR_CGIF5|DMA_IFCR_CTEIF5|DMA_IFCR_CHTIF5|DMA_IFCR_CTCIF5; |
|
|
|
|
|
|
|
//Указатель на полученный буфер
|
|
|
|
buff=dma_buff[dma_buff_index]; |
|
|
|
//переключить индекс
|
|
|
|
dma_buff_index = (dma_buff_index) ? 0 : 1; |
|
|
|
|
|
|
|
DMA1_Channel5->CCR = DMA_CCR_PL|DMA_CCR_MINC; |
|
|
|
DMA1_Channel5->CPAR = (uint32_t)&USART1->DR; |
|
|
|
if(dma_buff_index == 0){ |
|
|
|
dma_buff_index=1; |
|
|
|
dma_buff_ptr=(uint8_t*)&dma_buff2; |
|
|
|
save=(uint8_t*)&dma_buff1; |
|
|
|
}else{ |
|
|
|
dma_buff_index=0; |
|
|
|
dma_buff_ptr=(uint8_t*)&dma_buff1; |
|
|
|
save=(uint8_t*)&dma_buff2; |
|
|
|
} |
|
|
|
DMA1_Channel5->CMAR = (uint32_t)dma_buff_ptr; |
|
|
|
DMA1_Channel5->CNDTR = 1024; |
|
|
|
DMA1_Channel5->CMAR = (uint32_t)dma_buff[dma_buff_index]; |
|
|
|
DMA1_Channel5->CNDTR = ESP_PACKET_SIZE; |
|
|
|
DMA1_Channel5->CCR |= DMA_CCR_EN; |
|
|
|
|
|
|
|
WRITE(MKS_WIFI_IO4, HIGH); |
|
|
|
WRITE(MKS_WIFI_IO4, HIGH); //Остановить передачу от ESP
|
|
|
|
|
|
|
|
if(*buff != ESP_PROTOC_HEAD){ |
|
|
|
ERROR("Wrong packet head"); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
data_size = (*(buff+3) << 8) | *(buff+2); |
|
|
|
file_inc_size += (data_size - 4); //4 байта с номером сегмента и флагами
|
|
|
|
|
|
|
|
data_size = (*(save+3) << 8) | *(save+2); |
|
|
|
file_inc_size += (data_size - 4); |
|
|
|
DEBUG("[%d]Save %d bytes (%d of %d) ",dma_count,data_size,file_inc_size,file_size); |
|
|
|
|
|
|
|
res=f_write((FIL *)&upload_file,(uint8_t*)(save+8),(data_size-4),&bytes_writen); |
|
|
|
res=f_write((FIL *)&upload_file,(uint8_t*)(buff+8),(data_size-4),&bytes_writen); |
|
|
|
if(res){ |
|
|
|
ERROR("Write err %d",res); |
|
|
|
break; |
|
|
|
} |
|
|
|
file_size_writen+=bytes_writen; |
|
|
|
f_sync((FIL *)&upload_file); |
|
|
|
|
|
|
|
sprintf(str,"%ld/%ld",file_inc_size,file_size); |
|
|
|
ui.set_status((const char *)str,false); |
|
|
|
sprintf(str,"Upload %ld%%",file_inc_size*100/file_size); |
|
|
|
ui.set_status((const char *)str,true); |
|
|
|
ui.update(); |
|
|
|
|
|
|
|
file_size_writen+=bytes_writen; |
|
|
|
WRITE(MKS_WIFI_IO4, LOW); //Записано, сигнал ESP продолжать
|
|
|
|
|
|
|
|
f_sync((FIL *)&upload_file); |
|
|
|
WRITE(MKS_WIFI_IO4, LOW); |
|
|
|
|
|
|
|
if(*(save+7) == 0x80){ |
|
|
|
|
|
|
|
if(*(buff+7) == 0x80){ |
|
|
|
DEBUG("Last packet"); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
memset((uint8_t*)save,0,1024); |
|
|
|
memset((uint8_t*)buff,0,ESP_PACKET_SIZE); |
|
|
|
dma_count++; |
|
|
|
dma_timeout = DMA_TIMEOUT; |
|
|
|
} |
|
|
@ -156,27 +163,29 @@ void mks_wifi_start_file_upload(ESP_PROTOC_FRAME *packet){ |
|
|
|
if(DMA1->ISR & DMA_ISR_TEIF5){ |
|
|
|
ERROR("DMA Error"); |
|
|
|
} |
|
|
|
|
|
|
|
dma_timeout=dma_timeout-1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
f_close((FIL *)&upload_file); |
|
|
|
|
|
|
|
if( (file_size == file_inc_size) && (file_size == file_size_writen) ){ |
|
|
|
ui.set_status((const char *)"Upload done",false); |
|
|
|
ui.set_status((const char *)"Upload done",true); |
|
|
|
ui.update(); |
|
|
|
DEBUG("Upload ok"); |
|
|
|
}else{ |
|
|
|
ui.set_status((const char *)"Upload failed",false); |
|
|
|
ui.set_status((const char *)"Upload failed",true); |
|
|
|
DEBUG("Upload failed: %d; Recieve %d; SD write %d",file_size,file_inc_size,file_size_writen); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Восстановить USART1
|
|
|
|
USART1->CR1 = 0; |
|
|
|
USART1->CR1 = (USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE); |
|
|
|
USART1->CR3 = 0; |
|
|
|
USART1->BRR = usart1_brr; |
|
|
|
USART1->CR1 |= USART_CR1_UE; |
|
|
|
|
|
|
|
//Выключить DMA
|
|
|
|
DMA1->IFCR = DMA_IFCR_CGIF5|DMA_IFCR_CTEIF5|DMA_IFCR_CHTIF5|DMA_IFCR_CTCIF5; |
|
|
|
DMA1_Channel5->CCR = 0; |
|
|
|
DMA1_Channel5->CPAR = 0; |
|
|
@ -184,6 +193,9 @@ void mks_wifi_start_file_upload(ESP_PROTOC_FRAME *packet){ |
|
|
|
DMA1_Channel5->CNDTR = 0; |
|
|
|
|
|
|
|
mks_wifi_sd_deinit(); |
|
|
|
|
|
|
|
WRITE(MKS_WIFI_IO4, LOW); //Включить передачу от ESP
|
|
|
|
|
|
|
|
DEBUG("Settings restored"); |
|
|
|
|
|
|
|
} |
|
|
|