52833 DFU UART MASTER Received a fault! id: 0x00004002,

Hello

let me describe my problem:
I am using 52833 to debug the serial port upgrade (UART-DFU), and can use the tools "nrftuil" and "UartSecureDFU.exe" to perform the serial port upgrade, because the use scenario requires another mcu to upgrade 52833. Then I debug and use another host-MCU (stm103) as the host to upgrade 52833. In the current upgrade process, after the init package is ready, the send data package appears
<error> app: Received a fault! id: 0x00004002, pc: 0x00000000, info: 0x200095C0, I compared every host-mcu command and 52833's response to the "UartSecureDFU.exe" log and it was consistent, UartSecureDFU.exe "tool can do it, but my host-mcu can't, I don't know why it is wrong when sending data packets;
My environment is:
52833 SDK version is 17.0.2

<info> app: Inside main

<info> nrf_dfu_settings: Backing up settings page to address 0x7E000.

<info> app: No firmware to activate.

<info> nrf_bootloader_wdt: WDT is not enabled

<info> app: Entering DFU mode.

<info> app: Initializing transports (found: 2)

<info> nrf_dfu_serial_uart: serial_dfu_transport_init()

<info> nrf_dfu_serial: Received ping 1

<info> nrf_dfu_serial_uart: Allocated buffer 20005030

<info> nrf_dfu_serial: bnb NRF_DFU_OP_RECEIPT_NOTIF_SET

<info> nrf_dfu_serial_uart: Allocated buffer 20005030

<info> nrf_dfu_serial: bnb NRF_DFU_OP_MTU_GET

<info> nrf_dfu_serial: bnb Received serial mtu

<info> nrf_dfu_serial_uart: Allocated buffer 20005030

<info> nrf_dfu_serial: bnb NRF_DFU_OP_OBJECT_SELECT

<info> nrf_dfu_serial_uart: Allocated buffer 20005030

<info> nrf_dfu_serial: bnb NRF_DFU_OP_OBJECT_CREATE

<info> nrf_dfu_serial_uart: Allocated buffer 20005030

<info> nrf_dfu_serial: bnb NRF_DFU_OP_OBJECT_WRITE

<info> nrf_dfu_serial_uart: Allocated buffer 200050B4

<info> nrf_dfu_serial: bnb NRF_DFU_OP_OBJECT_WRITE

<info> nrf_dfu_serial_uart: Allocated buffer 20005030

<info> nrf_dfu_serial: bnb NRF_DFU_OP_OBJECT_WRITE

<info> nrf_dfu_serial_uart: Allocated buffer 200050B4

<info> nrf_dfu_serial_uart: Allocated buffer 200050B4

<info> nrf_dfu_serial_uart: Allocated buffer 200050B4

<info> nrf_dfu_validation: Signature required. Checking signature.

<info> nrf_dfu_validation: Calculating hash (len: 64)

<info> nrf_dfu_validation: Verify signature

<info> nrf_dfu_validation: Image verified

<info> nrf_dfu_settings: Backing up settings page to address 0x7E000.

<info> nrf_dfu_serial: bnb NRF_DFU_OP_OBJECT_SELECT

<info> nrf_dfu_serial_uart: Allocated buffer 200050B4

<info> nrf_dfu_serial: bnb NRF_DFU_OP_OBJECT_CREATE

<info> nrf_dfu_serial_uart: Allocated buffer 200050B4

<info> nrf_dfu_serial: bnb NRF_DFU_OP_OBJECT_WRITE

<info> nrf_dfu_serial_uart: Allocated buffer 20005030

<error> app: Received a fault! id: 0x00004002, pc: 0x00000000, info: 0x200095C0

<info> nrf_dfu_serial: bnb NRF_DFU_OP_OBJECT_WRITE

<info> nrf_dfu_serial_uart: Allocated buffer 20005138

<info> nrf_dfu_serial: bnb NRF_DFU_OP_OBJECT_WRITE

<error> nrf_dfu_serial_uart: Failed to allocate buffer

<info> nrf_dfu_serial: bnb NRF_DFU_OP_OBJECT_WRITE

<error> nrf_dfu_serial_uart: Failed to allocate buffer

file:dfu.zip is boot

user_dfu_master_uart.c is host-mcu

#include "user_dfu_master_uart.h"
#include "user_flash.h"
#include "usart.h"

/***********************static**********************************/
// static serial_dfu_t m_dfu;  //ע��demo��δ�ûᾯ��
 static uint8_t encoded_slip_packet[MTU_SIZE] = {0};
 static uint8_t payload[ MAX_ACTUAL_PAYLOAD ] = {0};
 uint8_t red_flash_op_tmp[0x10];   //��������
  uint8_t BNB_TS_LOG[0x50];   //��������
  static uint8_t bnb_flash_tmp_slip[MTU_SIZE] = {0};
/***********************static**********************************/


/********************
*name: serial_dfu_transport_init
*auther: bnb
*effect: ���ڳ�ʼ��
*in��
*out: err_code����ʼ����� 1--�ɹ� ����--ʧ��
*time: 2024.02.28
********************/
uint32_t serial_dfu_transport_init(void)
{
    uint32_t err_code;

    //����Ӵ��ڳ�ʼ������
    
    //���ڳ�ʼ�������Ѿ���ǰ�ߵ�HAL��ʼ�������˴������ٽ���
    
    return err_code;
}

/********************
*name: nrf_delay_ms
*auther: bnb
*effect: ���뼶��ʱ����
*in��
*out: 
*time: 2024.02.28
********************/
void nrf_delay_ms(uint32_t ms_time)
{
    //������ʱ����
    HAL_Delay(ms_time);
    
}
/********************
*name: find_image_size
*auther: bnb
*effect: ����FLASH�й̼�ӳ������ļ��㷨���ٶ��ھ���֮��flash��û�д����κ����ݣ�����ʧ��
*in��   uint32_t start_addres:  ��ʼ��ַ����������ǽ��������Ĺ̼�д�뵽����flash��ʱ���������ʼ��ַ��
        uint32_t end_address��  ������ַ�����������flash�Ŀ�Ҫ������ַ�����粻֪������С��
                                ��ij����ַ��ʼд��������ַ��Ϊflash������ַ��ͨ���ú���������̼��Ĵ�С
*out:  img_size�� (��������Ǽ�����Ĺ̼��Ĵ�С��
*time: 2024.02.28
��ע���������ʹ���ǽ��̼���д�뵽ij������flash��֪���̼����Ǵ�flash���ĸ���ַ��ʼд�Ľ�start_address��end_address��
flash�кܴ�ĵ�ַ������̼����϶�д�����ĵط�����ͨ����������ַ��ʵ������̼��Ĵ�С
********************/
uint32_t find_image_size(uint32_t start_address, uint32_t end_address)
{
    uint8_t vRes = 0;
    uint32_t img_size = end_address - 1;
    while(img_size > start_address)
    {
        #if 0 //�����ԭʼ����
        if(*((uint8_t*)img_size) != 0xFF) break;
        img_size -= 1;
        #endif
        
        #if 1 //���������stm32flash������
        //��flash��Ӧ��ַ�������
        memset(red_flash_op_tmp,0,sizeof(red_flash_op_tmp));
        vRes = BSP_W25Qx_Read(red_flash_op_tmp, img_size, 1);
        if(0 != vRes){
            printf("read err from flash \r\n");
        }
        else{
            if(0xFF != red_flash_op_tmp[0]){
                break;
            }
            else{
                img_size -= 1;
            }
        }
        #endif
    }
    img_size = img_size + 1 - start_address;   
    
    return img_size;    
}

/********************
*name: uart_send_ping_packet
*auther: bnb
*effect: ����SLIP����
*in��
*out: 
*time: 2024.02.28
********************/
ret_code_t slip_encode(uint8_t * p_output,  uint8_t * p_input, uint32_t input_length, uint32_t * p_output_buffer_length)
{
    uint32_t input_index;
    *p_output_buffer_length = 0;
    
    if (p_output == NULL || p_input == NULL || p_output_buffer_length == NULL)
    {
        return NRF_ERROR_NULL;
    }

    for (input_index = 0; input_index < input_length; input_index++)
    {
        switch (p_input[input_index])
        {
            case SLIP_BYTE_END:
                p_output[(*p_output_buffer_length)++] = SLIP_BYTE_ESC;
                p_output[(*p_output_buffer_length)++] = SLIP_BYTE_ESC_END;
                break;

            case SLIP_BYTE_ESC:
                p_output[(*p_output_buffer_length)++] = SLIP_BYTE_ESC;
                p_output[(*p_output_buffer_length)++] = SLIP_BYTE_ESC_ESC;
                break;

            default:
                p_output[(*p_output_buffer_length)++] = p_input[input_index];
        }
    }
    p_output[(*p_output_buffer_length)++] = SLIP_BYTE_END;

    return NRF_SUCCESS;
}

/********************
*name: uart_encode_and_send
*auther: bnb
*effect: ��������52833�������ݺ������Ƚ�����ʹ��slip���룬Ȼ����ͨ�����ڷ���
*in��   serial_dfu_t * p_dfu�����ڽṹ��
        uint8_t *init_data��init-pack��ָ��
        uint32_t length��init-pack��size��С
*out: 
*time: 2024.02.28
********************/
//static void uart_encode_and_send(serial_dfu_t * p_dfu,uint8_t *data, uint32_t length) ////ע�͵�demo������stm32��HAL��Ĵ��ڶ���
static void uart_encode_and_send(UART_HandleTypeDef *huart, uint8_t *data, uint32_t length)
{   
    uint32_t encoded_slip_packet_length;
    (void)slip_encode(encoded_slip_packet, data, length, &encoded_slip_packet_length);
    
    //(void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length); //uart���ͺ�������Ҫ����STM32�Ĵ��ڷ��ͺ���
    //while(nrf_drv_uart_tx_in_progress(&p_dfu->uart_instance));   //���while�����ȴ���ֱ�����η��͵����ݷ����꣬���������
    HAL_UART_Transmit(&huart3, encoded_slip_packet, encoded_slip_packet_length,0xFFFF);
    //���õȴ����ڿ��У����������ʹ�ô��ڷ��ͺ����ķ���ֵ
    //while (HAL_UART_GetState(&huart3) != HAL_UART_STATE_READY);
    
}


#if 1 //�����о�����ʱ��ע��
/********************
*name: uart_send_ping_packet
*auther: bnb
*effect: ��������52833ping��
*in��
*out: 
*time: 2024.02.28
********************/
//static void uart_send_ping_packet(serial_dfu_t* p_dfu)  //ע�͵�demo������stm32��HAL��Ĵ��ڶ���
static void uart_send_ping_packet(UART_HandleTypeDef *huart)
{
    uint32_t encoded_slip_packet_length;
    uint8_t init_data[4]={0x09,0x01};
    uint32_t length= 2;
    
    (void)slip_encode(encoded_slip_packet, init_data, length, &encoded_slip_packet_length);

    // send ���ڷ������ݺ�������Ҫ����STM32�ķ��ͺ���
    //(void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length);
    HAL_UART_Transmit(&huart3, encoded_slip_packet, encoded_slip_packet_length,0xFFFF);
    
    nrf_delay_ms(5);
    
    //add by tlb nk ����λδ֪
    #if 1
    uint32_t encoded_slip_packet_length1;
    uint8_t init_data1[4]={0x02,0x00,0x00};
    uint32_t length1= 3;
    (void)slip_encode(encoded_slip_packet, init_data1, length1, &encoded_slip_packet_length1);
    // send ���ڷ������ݺ�������Ҫ����STM32�ķ��ͺ���
    //(void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length);
    HAL_UART_Transmit(&huart3, encoded_slip_packet, encoded_slip_packet_length1,0xFFFF);
    nrf_delay_ms(5);
    
     uint32_t encoded_slip_packet_length2;
    uint8_t init_data2[4]={0x07};
    uint32_t length2= 1;
    (void)slip_encode(encoded_slip_packet, init_data2, length2, &encoded_slip_packet_length2);
    // send ���ڷ������ݺ�������Ҫ����STM32�ķ��ͺ���
    //(void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length);
    HAL_UART_Transmit(&huart3, encoded_slip_packet, encoded_slip_packet_length2,0xFFFF);
    nrf_delay_ms(5);		
    
    uint32_t encoded_slip_packet_length3;
    uint8_t init_data3[4]={0x06,0x01};
    uint32_t length3= 2;
    (void)slip_encode(encoded_slip_packet, init_data3, length3, &encoded_slip_packet_length3);
    // send ���ڷ������ݺ�������Ҫ����STM32�ķ��ͺ���
    //(void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length);
    HAL_UART_Transmit(&huart3, encoded_slip_packet, encoded_slip_packet_length3,0xFFFF);
    nrf_delay_ms(15);		

    #endif
    //add end
    
    //add by tlb nk ����λδ֪
    #if 0
     payload[0]=0x02;
	   payload[1]=0x00;		
	   payload[2]=0x00;
     uart_encode_and_send(&huart3,payload,3);	
					nrf_delay_ms(5);		
	   payload[0]=0x07;
     uart_encode_and_send(&huart3,payload,1);	
					nrf_delay_ms(5);		
	   payload[0]=0x06;
	   payload[1]=0x01;		
     uart_encode_and_send(&huart3,payload,2);	
							nrf_delay_ms(15);	
    #endif
    //add end
    
    
}
#endif



/********************
*name: uart_send_init_packet
*auther: bnb
*effect: ��������52833����init pack��
*in��   serial_dfu_t * p_dfu�����ڽṹ��
        uint8_t *init_data��init-pack��ָ��
        uint32_t length��init-pack��size��С
*out: 
*time: 2024.02.28
********************/
//static void uart_send_init_packet(serial_dfu_t * p_dfu,uint8_t *init_data, uint32_t length)
//static void uart_send_init_packet(UART_HandleTypeDef *huart,uint8_t *init_data, uint32_t length)
static void uart_send_init_packet(UART_HandleTypeDef *huart,uint32_t *init_data, uint32_t length)
{
    //uint32_t i=length; //����δʹ��ע��
    uint32_t encoded_slip_packet_length;

    //Create Object
    payload[0]=0x01;
    payload[1]=0x01;//Command Object
    payload[2]=length;
    payload[3]=length>>8;
    payload[4]=length>>16;
    payload[5]=length>>24;
    //uart_encode_and_send(p_dfu,payload,6);//ΪʲôҪ�ȷ��������Ҫ��ϸ�����app.dat�ļ�  //ע�͵�demo������stm32��HAL��Ĵ��ڶ���
    uart_encode_and_send(&huart3,payload,6);//ΪʲôҪ�ȷ��������Ҫ��ϸ�����app.dat�ļ�
    nrf_delay_ms(5);
    uint32_t flash_address = (uint32_t)init_data;
    while(length>0)
    {   
        encoded_slip_packet[0]=0x08;//Write object opcode

        if (length>=MAX_ACTUAL_PAYLOAD) //���init-pack���ij��ȴ��ڵ����MAX_ACTUAL_PAYLOAD���Ƚ�MAX_ACTUAL_PAYLOAD���ȵ�����ת��slip��ʽ���ٽ�init-pack����ָ�������MAX_ACTUAL_PAYLOAD����
        {
            #if 1 //���ڹ̼����Ų����ڴ��ֻ������ÿ�η����ӻ�ǰ��flash�ж�Ӧ��ַ��ȡ�����ŵ��ڴ���
            memset(bnb_flash_tmp_slip,0,sizeof(bnb_flash_tmp_slip));
            if(W25Qx_OK != BSP_W25Qx_Read(bnb_flash_tmp_slip,flash_address,MAX_ACTUAL_PAYLOAD)){
                 printf("[BNB_TS]something wrong in Step4\r\n");
            }
            #endif
            (void)slip_encode(&encoded_slip_packet[1], bnb_flash_tmp_slip, MAX_ACTUAL_PAYLOAD, &encoded_slip_packet_length);
            //(void)slip_encode(&encoded_slip_packet[1], init_data, MAX_ACTUAL_PAYLOAD, &encoded_slip_packet_length);
            length= length-MAX_ACTUAL_PAYLOAD;
            //uint32_t)init_data = (uint32_t)init_data+MAX_ACTUAL_PAYLOAD;
            flash_address = flash_address + MAX_ACTUAL_PAYLOAD;
        }else
        {    ////���init-pack���ij�����MAX_ACTUAL_PAYLOAD
            #if 1 //���ڹ̼����Ų����ڴ��ֻ������ÿ�η����ӻ�ǰ��flash�ж�Ӧ��ַ��ȡ�����ŵ��ڴ���
            memset(bnb_flash_tmp_slip,0,sizeof(bnb_flash_tmp_slip));
            if(W25Qx_OK != BSP_W25Qx_Read(bnb_flash_tmp_slip,flash_address,length)){
                 printf("[BNB_TS]something wrong in Step4\r\n");
            }
            #endif
            (void)slip_encode(&encoded_slip_packet[1], bnb_flash_tmp_slip, length, &encoded_slip_packet_length);
            //(void)slip_encode(&encoded_slip_packet[1], init_data, length, &encoded_slip_packet_length);
            length=0;
        }
        //NRF_LOG_INFO("Data : %d %d %d", length,&init_data,MAX_ACTUAL_PAYLOAD);// ��־��ӡ data: length(init-pack��ʣ����Ҫ�������ݳ���) &init_data(init-pack��д���ĵ�ַ)
        printf("int-pack Data : wait=%d one_times=%d\r\n", length,MAX_ACTUAL_PAYLOAD);// ��־��ӡ data: length(init-pack��ʣ����Ҫ�������ݳ���) &init_data(init-pack��д���ĵ�ַ)
        // send
        //(void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length+1); //uart���ͺ�������Ҫ����stm32
        //while(nrf_drv_uart_tx_in_progress(&p_dfu->uart_instance));    //���while�����ȴ���ֱ�����η��͵����ݷ����꣬���������
        HAL_UART_Transmit(&huart3, encoded_slip_packet, encoded_slip_packet_length+1,0xFFFF);
        //���õȴ����ڿ��У����������ʹ�ô��ڷ��ͺ����ķ���ֵ
        //while (HAL_UART_GetState(&huart3) != HAL_UART_STATE_READY);
        
        //add tlb by niuke code
        nrf_delay_ms(5);
        
    }
    
    //�����DZ���init-pack���������
    
    //Ask for CRC
    payload[0]=0x03;    // NRF_DFU_OP_CRC_GET              = 0x03,     //!< Request CRC of selected object.  
    //uart_encode_and_send(p_dfu,payload,1);  // //ע�͵�demo������stm32��HAL��Ĵ��ڶ���
    uart_encode_and_send(&huart3,payload,1);
    
    
    //nrf_delay_ms(5);
    //add by tlb nk
    nrf_delay_ms(50);
    
    
    //To-DO: check CRC
    //Execute init packet //ִ�г�ʼ����
    payload[0]=0x04;    //NRF_DFU_OP_OBJECT_EXECUTE       = 0x04,     //!< Execute selected object.
    //uart_encode_and_send(p_dfu,payload,1); //ע�͵�demo������stm32��HAL��Ĵ��ڶ���
    uart_encode_and_send(&huart3,payload,1);
    
    //add tlb by niuke code
    nrf_delay_ms(500);
    
    #if 1 //add by bnb nk
	payload[0]=0x06;
	payload[1]=0x02;		
    uart_encode_and_send(&huart3,payload,2);
	nrf_delay_ms(500);	
    #endif
    
}

/********************
*name: uart_send_application_image
*auther: bnb
*effect: ��������52833����app.bin���ݰ���Ҳ���Ǿ���
*in��   serial_dfu_t * p_dfu�����ڽṹ��
        uint8_t *init_data��app.bin���ݰ�ָ��
        uint32_t length��app.bin���ݰ���size��С
*out: 
*time: 2024.02.28
********************/
//static void uart_send_application_image(serial_dfu_t * p_dfu,uint8_t *data, uint32_t length)
//static void uart_send_application_image(UART_HandleTypeDef *huart,uint8_t *data, uint32_t length)
static void uart_send_application_image(UART_HandleTypeDef *huart,uint32_t *data, uint32_t length)
{
    //uint32_t i=length; //����δʹ��ע��
    uint32_t encoded_slip_packet_length;

    //Create Object
    payload[0]=0x01;
    payload[1]=0x02;//Command Object
    payload[2]=length;
    payload[3]=length>>8;
    payload[4]=length>>16;
    payload[5]=length>>24;
    //uart_encode_and_send(p_dfu,payload,6);  //ע�͵�demo������stm32��HAL��Ĵ��ڶ���
    uart_encode_and_send(&huart3,payload,6);
    //nrf_delay_ms(5);
    nrf_delay_ms(1000);
    uint32_t flash_address = (uint32_t)data;
    printf("׼������image�̼����ĵ�һ������ ��ʱ10s���ڲ���\r\n");
    while(length>0)
    {   
        encoded_slip_packet[0]=0x08;//Write object opcode

        if (length>=MAX_ACTUAL_PAYLOAD) //����߼��ͷ���init-pack�����߼�����
        {
            #if 1 //���ڹ̼����Ų����ڴ��ֻ������ÿ�η����ӻ�ǰ��flash�ж�Ӧ��ַ��ȡ�����ŵ��ڴ���
            memset(bnb_flash_tmp_slip,0,sizeof(bnb_flash_tmp_slip));
            if(W25Qx_OK != BSP_W25Qx_Read(bnb_flash_tmp_slip,flash_address,MAX_ACTUAL_PAYLOAD)){
                 printf("[BNB_TS]something wrong in Step4\r\n");
            }
            #endif
            (void)slip_encode(&encoded_slip_packet[1], bnb_flash_tmp_slip, MAX_ACTUAL_PAYLOAD, &encoded_slip_packet_length);
            
            //(void)slip_encode(&encoded_slip_packet[1], data, MAX_ACTUAL_PAYLOAD, &encoded_slip_packet_length);
            length= length-MAX_ACTUAL_PAYLOAD;
            //data = data+MAX_ACTUAL_PAYLOAD;
            flash_address = flash_address + MAX_ACTUAL_PAYLOAD;
        }else
        {
            #if 1 //���ڹ̼����Ų����ڴ��ֻ������ÿ�η����ӻ�ǰ��flash�ж�Ӧ��ַ��ȡ�����ŵ��ڴ���
            memset(bnb_flash_tmp_slip,0,sizeof(bnb_flash_tmp_slip));
            if(W25Qx_OK != BSP_W25Qx_Read(bnb_flash_tmp_slip,flash_address,length)){
                 printf("[BNB_TS]something wrong in Step4\r\n");
            }
            #endif
            (void)slip_encode(&encoded_slip_packet[1], bnb_flash_tmp_slip, length, &encoded_slip_packet_length);
            
            //(void)slip_encode(&encoded_slip_packet[1], data, length, &encoded_slip_packet_length);
            length=0;
        }
        //NRF_LOG_INFO("Data : %d %d %d", length,&data,MAX_ACTUAL_PAYLOAD); // ��־��ӡ Data: length(app.bin����һ��4096�ֽ���ʣ����Ҫ�������ݳ���) &data(app.bin��д���ĵ�ַ)
        //printf("Data : %d %d %d", length,(int)&data,MAX_ACTUAL_PAYLOAD);//��־��ӡ Data: length(app.bin����һ��4096�ֽ���ʣ����Ҫ�������ݳ���) &data(app.bin��д���ĵ�ַ)
        printf("bin-data Data : wait=%d one_times=%d\r\n", length,MAX_ACTUAL_PAYLOAD);// ��־��ӡ data: length(init-pack��ʣ����Ҫ�������ݳ���) &init_data(init-pack��д���ĵ�ַ)
        // send
        //(void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length+1);  ////uart���ͺ�������Ҫ����stm32
        //while(nrf_drv_uart_tx_in_progress(&p_dfu->uart_instance));     ////���while�����ȴ���ֱ�����η��͵����ݷ����꣬���������
        HAL_UART_Transmit(&huart3, encoded_slip_packet, encoded_slip_packet_length+1,0xFFFF);
        //���õȴ����ڿ��У����������ʹ�ô��ڷ��ͺ����ķ���ֵ
        //while (HAL_UART_GetState(&huart3) != HAL_UART_STATE_READY);
        
        //add tlb by niuke code
        nrf_delay_ms(5);
        
    }
    //Ask for CRC
    payload[0]=0x03;
    //uart_encode_and_send(p_dfu,payload,1);  //ע�͵�demo������stm32��HAL��Ĵ��ڶ���
    uart_encode_and_send(&huart3,payload,1);
    //nrf_delay_ms(5);
    nrf_delay_ms(50);
    //To-DO: check CRC
    //Execute init packet
    payload[0]=0x04;
    //uart_encode_and_send(p_dfu,payload,1);  //ע�͵�demo������stm32��HAL��Ĵ��ڶ���
    uart_encode_and_send(&huart3,payload,1);
    //nrf_delay_ms(15);
    nrf_delay_ms(500);
}


/********************
*name: start_DFU
*auther: bnb
*effect: ������ʼִ��DFU��������bootloader����DFUģʽ�µ�52833����init-pack �� app-pack
*in��
*out: 
*time: 2024.02.28
********************/
void start_DFU(void)
{
    //uint8_t *data;
    uint32_t *data;
    //char tagid[18] = {0};  ע�͵�δʹ�þ���
    uint32_t data_size;
    uint32_t application_image_address = 0x02000;//��������app-pack����Ҫд��Ŀ�ʼ��ַ�������Dz���0x30000�Ƿ�Ҫ����Ҫȷ��
                                                 //���ֲ�s140��7.x.x��Э��ջ��app����ʼ��ַ��0x26000-0x78000,��С��328KB���м����ֵ��ַ��0x4f000��
                                                 //0x30000�Ļ�������ʼ��ַֻ��10KB��С����֪����Ϊʲôд��ôС�ģ����˾���д0x4f000��ʼΪ������д���ַ
    //nrf_gpio_pin_clear(LED_1); //��Ҫ����ԭ��������LED1�رգ����ڱ�ʾ����ǰ��״̬���豸���ڵ�״̬
    LED1_OFF;
    
    //data_size = find_image_size(0x9000,0x10000);//����Ǵ������̼��Ǵ�flash��0x9000��ַ��ʼд�ģ���֪���̼���д�����ĵ�ַ��ѡһ����һ��ĵ�ַ10000���������ʵ�Ĺ̼���д�����ĵ�ַ
                                                //����Ƕ�init-pack���ļ���
    data_size = find_image_size(0x0000,0x01000);//inck-pack��д��flash�ĵ�һ����ĵ�һ������
    
    
    //NRF_LOG_INFO("Init data size: %d", data_size); //��־��ӡ��init data�Ĵ�С��init dataӦ���ǹ̼���ѹ�������app.dat�ļ�
    printf("Init data size: %d", data_size);
    
    //data= (uint8_t *) 0x9000; //����data��init data��ʼд��ĵ�ַ
    //data= (uint8_t *) 0x0000; //
    data= (uint32_t *) 0x0000; //
    
    //��ʱ��printfʹ��data����Ȼ������dataδʹ��
    //sprintf(tagid, "%x %x %d", *data, *(data+1), MAX_ACTUAL_PAYLOAD);
    //printf("Data: %s\r\n",tagid); 
      
    #if 1
      memset(BNB_TS_LOG,0,sizeof(BNB_TS_LOG));
      if(BSP_W25Qx_Read(BNB_TS_LOG,(uint32_t)data,2)!= W25Qx_OK){
          printf("[BNB_TS]something wrong in Step4\r\n");
      }
    #endif
    
    printf("1111Data : %x %x %d\r\n", BNB_TS_LOG[0], BNB_TS_LOG[1],MAX_ACTUAL_PAYLOAD);
    printf("2222Data : %X %X %d\r\n",BNB_TS_LOG[0], BNB_TS_LOG[1],MAX_ACTUAL_PAYLOAD);
    //printf("Data : %x %x %d", *data, *(data+1),MAX_ACTUAL_PAYLOAD); 
    //NRF_LOG_INFO("Data : %x %x %d", *data, *(data+1),MAX_ACTUAL_PAYLOAD); 
                                                /*��־��ӡ data: 0x(*data��init data���ݵĵ�һ���ֽڵ�����) 
                                                0x(*(data+1)��init data���ݵĵڶ����ֽڵ�����) MAX_ACTUAL_PAYLOAD(���ʵ���غ����ֵǰ�߶�������63��Ϊʲô��ô��֪��)*/
    nrf_delay_ms(5);
    //uart_send_init_packet(&m_dfu,(uint8_t *) 0x9000,data_size);//uart����init-pack=app.dat���52833
    //uart_send_init_packet(&huart3,(uint8_t *) 0x4f000,data_size);//uart����init-pack=app.dat�ļ���52833  ���Զ���Ϊ0x4f000
    uart_send_init_packet(&huart3,(uint32_t *) 0x00,data_size);//uart����init-pack=app.dat�ļ���52833  ���Զ���Ϊ0x4f000
    nrf_delay_ms(500);
    
    data_size = find_image_size(application_image_address,application_image_address+0x09000); //application_image_addressǰ�߶���Ϊ0x30000
    //NRF_LOG_INFO("Image size: %d", data_size); //��־��ӡimage=app���ݰ�=app.bin�Ĵ�С
    printf("Image size: %d", data_size); //��־��ӡimage=app���ݰ�=app.bin�Ĵ�С
    //Max object size is 4096 bytes.    
    while(data_size>0)
    {   
        if ( data_size>=MAX_OBJECT_SIZE) //����߼��ͷ���init-pack�����߼�����  //���һ�����ֻ��4096�ֽڷ��ͣ���app.bin�ֳɶ�ε�4096�ֽڷ���
        {
            //uart_send_application_image(&m_dfu,,(uint8_t *) application_image_address,MAX_OBJECT_SIZE);
            //uart_send_application_image(&huart3,(uint8_t *) application_image_address,MAX_OBJECT_SIZE);
            uart_send_application_image(&huart3,(uint32_t *) application_image_address,MAX_OBJECT_SIZE);
            data_size = data_size- MAX_OBJECT_SIZE;
            application_image_address= application_image_address + MAX_OBJECT_SIZE;
        }else
        {
             //uart_send_application_image(&m_dfu,(uint8_t *) application_image_address,data_size);
             //uart_send_application_image(&huart3,(uint8_t *) application_image_address,data_size);
             uart_send_application_image(&huart3,(uint32_t *) application_image_address,data_size);
             data_size = 0;
        }
    }  
        
    nrf_delay_ms(1000);
    //nrf_gpio_pin_set(LED_1);  //��LED1�Ƶ������Դ�����ʾ�豸�����򣩽��е�״̬����ʼ���������ƣ���ʼ����DFUǰ�صƣ�������Ϻ��ٿ���
    LED1_ON;
}

/********************
*name: dfu_master_uart_init
*auther: bnb
*effect: ��ʼ��DFU_UARTģ�飬ͨ��UART����Ϊ���������ӻ����й̼�����
*in��
*out: err_code--�̼������������ֵ  1������ ����ֵ�쳣
*time: 2024.02.28
********************/
int dfu_master_uart_init(void)
{
    uint32_t err_code = 0;
    
    dfu_end_flag = 0;//add only ts
    
    #if 1//DFU ����ͨ�����ڸ�52833�����̼�
    //int32_t volatile temp; //����δʹ�á�����
    //APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    
    //�˴������־��ʼ�����������������������л���log��ӡ
    
    //NRF_LOG_DEFAULT_BACKENDS_INIT();
    serial_dfu_transport_init(); //��ʼ�������ڸ�52833ͨ�Ž��������Ĵ��ڣ�ʹ���ж�
    nrf_delay_ms(500); //��ʱ500���룬��Ҫ������δ֪
    uart_send_ping_packet(&huart3); //������52833��ping��
    
    //add by tlb nk code
    nrf_delay_ms(1000);
    
    //nrf_gpio_cfg_output(LED_1);//���õ�STM32������һ�������������ߴ���dfu�������̵���˸����������ܵ�״̬
    //nrf_gpio_pin_set(LED_1);
    
    //nrf_gpio_cfg_input(BUTTON_1,NRF_GPIO_PIN_PULLUP); //���ð���������������ʼ����DFU��������52833����init-pack ��app-data

    //while(1)
    //{
        //if(nrf_gpio_pin_read(BUTTON_1) == 0)
        //if(1)//while1�еȼ�⵽���������½���if�↑ʼִ��DFU�������ݴ���
        //{
            start_DFU();
          //  break; //����ʱע�����ţ��ʱҪ�ſ�����Ҫ�������ط���ֵ
        //}
    //}
    #endif
    
    dfu_end_flag = 1;//add only ts
    
    return err_code;
}



/******************************************************************************************************************
*ʹ��˵����
*��������Ҫ��52833�����ĵط�����dfu_master_uart_init���ɣ�ʹ��ǰ��Ҫ���������ƽ̨ʵ�����½ӿ�
*(1) �����˵�uart���շ��ӿ�
*(2) �����˵ĺ��뼶��ʱ����
*
*
*
*
*
*******************************************************************************************************************/

3312.dfu.zip

Parents
  • /**
     * Copyright (c) 2014 - 2017, Nordic Semiconductor ASA
     * 
     * All rights reserved.
     * 
     * Redistribution and use in source and binary forms, with or without modification,
     * are permitted provided that the following conditions are met:
     * 
     * 1. Redistributions of source code must retain the above copyright notice, this
     *    list of conditions and the following disclaimer.
     * 
     * 2. Redistributions in binary form, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, must reproduce the above copyright notice, this list of
     *    conditions and the following disclaimer in the documentation and/or other
     *    materials provided with the distribution.
     * 
     * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     * 
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     * 
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     * 
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     * 
     */
    /** @file
    * @defgroup temperature_example_main main.c
    * @{
    * @ingroup temperature_example
    * @brief Temperature Example Application main file.
    * @details
    * This file contains the source code for a sample application using the temperature sensor.
    * This contains workaround for PAN_028 rev2.0A anomalies 28, 29,30 and 31. PAN 43 is not covered.
    *  - PAN_028 rev2.0A anomaly 28 - TEMP: Negative measured values are not represented correctly
    *  - PAN_028 rev2.0A anomaly 29 - TEMP: Stop task clears the TEMP register.
    *  - PAN_028 rev2.0A anomaly 30 - TEMP: Temp module analog front end does not power down when DATARDY event occurs.
    *  - PAN_028 rev2.0A anomaly 31 - TEMP: Temperature offset value has to be manually loaded to the TEMP module
    *  - PAN_028 rev2.0A anomaly 43 - TEMP: Using PPI between DATARDY event and START task is not functional.
    *
    */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include "nrf.h"
    #include "nrf_delay.h"
    #include "boards.h"
    #include "app_error.h"
    #include "bsp.h"
    #include "slip.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    #include "nrf_drv_uart.h"
    #include "nrf_serial_dfu.h"
    #define MTU_SIZE 129
    #define MAX_ACTUAL_PAYLOAD (MTU_SIZE/2-2)
    #define MAX_OBJECT_SIZE 4096
    uint32_t find_image_size(uint32_t start_address, uint32_t end_address);
    
    /** @brief Function for main application entry.
     */
     static serial_dfu_t m_dfu;
     static void uart_event_handler(nrf_drv_uart_event_t * p_event, void * p_context)
    {
        uint32_t err_code;
        switch (p_event->type)
        {
            case NRF_DRV_UART_EVT_TX_DONE:
        
                break;
    
            case NRF_DRV_UART_EVT_RX_DONE:
                //Todo: instead of using delay response code checking should be done here.
                err_code = nrf_drv_uart_rx(&m_dfu.uart_instance, &m_dfu.uart_buffer, 100);
                if (err_code != NRF_SUCCESS)
                {
                    NRF_LOG_ERROR("Failed initializing rx");
                }
                break;
    
            case NRF_DRV_UART_EVT_ERROR:
        
                break;
        }
    }
    
    // Simple algorithm for finding the end of the firmware image in the flash
    // Assumes that there is nothing stored in the flash after the image, otherwise it will fail
    uint32_t find_image_size(uint32_t start_address, uint32_t end_address)
    {
        uint32_t img_size = end_address - 1;
        while(img_size > start_address)
        {
            if(*((uint8_t*)img_size) != 0xFF) break;
            img_size -= 1;
        }
        img_size = img_size + 1 - start_address;   
        
        return img_size;    
    }
    
    static uint8_t encoded_slip_packet[ MTU_SIZE ] = {0};
    static uint8_t payload[ MAX_ACTUAL_PAYLOAD ] = {0};
    static void uart_encode_and_send(serial_dfu_t * p_dfu,uint8_t *data, uint32_t length)
    {    uint32_t encoded_slip_packet_length;
        (void)slip_encode(encoded_slip_packet, data, length, &encoded_slip_packet_length);
        (void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length);
         while(nrf_drv_uart_tx_in_progress(&p_dfu->uart_instance));  
    }
        
    static void uart_send_init_packet(serial_dfu_t * p_dfu,uint8_t *init_data, uint32_t length)
    {
        uint32_t i=length;
        uint32_t encoded_slip_packet_length;
    
        //Create Object
        payload[0]=0x01;
        payload[1]=0x01;//Command Object
        payload[2]=length;
        payload[3]=length>>8;
        payload[4]=length>>16;
        payload[5]=length>>24;
        uart_encode_and_send(p_dfu,payload,6);
        nrf_delay_ms(5);
        while(length>0)
        {   
            encoded_slip_packet[0]=0x08;//Write object opcode
    
            if (length>=MAX_ACTUAL_PAYLOAD)
            {
                (void)slip_encode(&encoded_slip_packet[1], init_data, MAX_ACTUAL_PAYLOAD, &encoded_slip_packet_length);
                length= length-MAX_ACTUAL_PAYLOAD;
                init_data = init_data+MAX_ACTUAL_PAYLOAD;
            }else
            {
                (void)slip_encode(&encoded_slip_packet[1], init_data, length, &encoded_slip_packet_length);
                length=0;
            }
            NRF_LOG_INFO("Data : %d %d %d", length,&init_data,MAX_ACTUAL_PAYLOAD);
            // send
            (void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length+1);
             while(nrf_drv_uart_tx_in_progress(&p_dfu->uart_instance));    
            
        }
        //Ask for CRC
         payload[0]=0x03;
         uart_encode_and_send(p_dfu,payload,1);
         nrf_delay_ms(5);
        //To-DO: check CRC
        //Execute init packet
         payload[0]=0x04;
         uart_encode_and_send(p_dfu,payload,1);
        
    }
    static void uart_send_application_image(serial_dfu_t * p_dfu,uint8_t *data, uint32_t length)
    {
        uint32_t i=length;
        uint32_t encoded_slip_packet_length;
    
        //Create Object
        payload[0]=0x01;
        payload[1]=0x02;//Command Object
        payload[2]=length;
        payload[3]=length>>8;
        payload[4]=length>>16;
        payload[5]=length>>24;
        uart_encode_and_send(p_dfu,payload,6);
        nrf_delay_ms(5);
        while(length>0)
        {   
            encoded_slip_packet[0]=0x08;//Write object opcode
    
            if (length>=MAX_ACTUAL_PAYLOAD)
            {
                (void)slip_encode(&encoded_slip_packet[1], data, MAX_ACTUAL_PAYLOAD, &encoded_slip_packet_length);
                length= length-MAX_ACTUAL_PAYLOAD;
                data = data+MAX_ACTUAL_PAYLOAD;
            }else
            {
                (void)slip_encode(&encoded_slip_packet[1], data, length, &encoded_slip_packet_length);
                length=0;
            }
            NRF_LOG_INFO("Data : %d %d %d", length,&data,MAX_ACTUAL_PAYLOAD);
            // send
            (void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length+1);
             while(nrf_drv_uart_tx_in_progress(&p_dfu->uart_instance));    
            
        }
        //Ask for CRC
         payload[0]=0x03;
         uart_encode_and_send(p_dfu,payload,1);
         nrf_delay_ms(5);
        //To-DO: check CRC
        //Execute init packet
         payload[0]=0x04;
         uart_encode_and_send(p_dfu,payload,1);
         nrf_delay_ms(15);
    }
    
    
    static void uart_send_ping_packet(serial_dfu_t          * p_dfu        )
    {
        uint32_t encoded_slip_packet_length;
        uint8_t init_data[4]={0x09,0x01};
        uint32_t length= 2;
        (void)slip_encode(encoded_slip_packet, init_data, length, &encoded_slip_packet_length);
    
      
    
        // send
        (void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length);
    }
    
    
    void start_DFU(void)
    {
        uint32_t data_size;
        uint32_t application_image_address = 0x30000;
        nrf_gpio_pin_clear(LED_1);
        data_size = find_image_size(0x9000,0x10000);
        NRF_LOG_INFO("Init data size: %d", data_size);
        uint8_t *data;
        data= (uint8_t *) 0x9000;
        NRF_LOG_INFO("Data : %x %x %d", *data, *(data+1),MAX_ACTUAL_PAYLOAD);
        nrf_delay_ms(5);
        uart_send_init_packet(&m_dfu,(uint8_t *) 0x9000,data_size);
        nrf_delay_ms(500);
        
        data_size = find_image_size(application_image_address,application_image_address+0x40000);
        NRF_LOG_INFO("Image size: %d", data_size);
        //Max object size is 4096 bytes.
         while(data_size>0)
        {   
            if ( data_size>=MAX_OBJECT_SIZE)
            {
                
                uart_send_application_image(&m_dfu,(uint8_t *) application_image_address,MAX_OBJECT_SIZE);
                data_size = data_size- MAX_OBJECT_SIZE;
                application_image_address= application_image_address + MAX_OBJECT_SIZE;
            }else
            {
                 uart_send_application_image(&m_dfu,(uint8_t *) application_image_address,data_size);
                 data_size = 0;
            }
        }  
            
        nrf_delay_ms(1000);
        nrf_gpio_pin_set(LED_1);
    }
    
    uint32_t serial_dfu_transport_init(void)
    {
        uint32_t err_code;
    
        //leds_init();
    
        m_dfu.slip.p_buffer      = m_dfu.recv_buffer;
        m_dfu.slip.current_index = 0;
        m_dfu.slip.buffer_len    = sizeof(m_dfu.recv_buffer);
        m_dfu.slip.state         = SLIP_STATE_DECODING;
    
        nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG;
    
        uart_config.pseltxd   = TX_PIN_NUMBER;
        uart_config.pselrxd   = RX_PIN_NUMBER;
        uart_config.pselcts   = CTS_PIN_NUMBER;
        uart_config.pselrts   = RTS_PIN_NUMBER;
        uart_config.hwfc      = NRF_UART_HWFC_ENABLED;
        uart_config.p_context = &m_dfu;
    
    
        nrf_drv_uart_t instance =  NRF_DRV_UART_INSTANCE(0);
        memcpy(&m_dfu.uart_instance, &instance, sizeof(instance));
    
        err_code =  nrf_drv_uart_init(&m_dfu.uart_instance, &uart_config, uart_event_handler);
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_ERROR("Failed initializing uart");
            return err_code;
        }
    
        nrf_drv_uart_rx_enable(&m_dfu.uart_instance);
    
        err_code = nrf_drv_uart_rx(&m_dfu.uart_instance, &m_dfu.uart_buffer, 100);
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_ERROR("Failed initializing rx");
        }
        
        NRF_LOG_DEBUG("UART initialized");
    
        return err_code;
    }
    
    
    uint32_t serial_dfu_transport_close(void)
    {
        nrf_drv_uart_uninit(&m_dfu.uart_instance);
        return NRF_SUCCESS;
    }
    int main(void)
    {
        // This function contains workaround for PAN_028 rev2.0A anomalies 28, 29,30 and 31.
        int32_t volatile temp;
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
        serial_dfu_transport_init();
        nrf_delay_ms(500);
        uart_send_ping_packet(&m_dfu);
        nrf_gpio_cfg_output(LED_1);
        nrf_gpio_pin_set(LED_1);
        nrf_gpio_cfg_input(BUTTON_1,NRF_GPIO_PIN_PULLUP);
       while(1)
        {
            
            if(nrf_gpio_pin_read(BUTTON_1) == 0)
            {
                start_DFU();
            }
        }
    }
    
    
    /** @} */
    
    My host program is written according to the following main.c, my debugging program is written more chaotic, and there are garbled comments, you can see this main.c

Reply
  • /**
     * Copyright (c) 2014 - 2017, Nordic Semiconductor ASA
     * 
     * All rights reserved.
     * 
     * Redistribution and use in source and binary forms, with or without modification,
     * are permitted provided that the following conditions are met:
     * 
     * 1. Redistributions of source code must retain the above copyright notice, this
     *    list of conditions and the following disclaimer.
     * 
     * 2. Redistributions in binary form, except as embedded into a Nordic
     *    Semiconductor ASA integrated circuit in a product or a software update for
     *    such product, must reproduce the above copyright notice, this list of
     *    conditions and the following disclaimer in the documentation and/or other
     *    materials provided with the distribution.
     * 
     * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
     *    contributors may be used to endorse or promote products derived from this
     *    software without specific prior written permission.
     * 
     * 4. This software, with or without modification, must only be used with a
     *    Nordic Semiconductor ASA integrated circuit.
     * 
     * 5. Any software provided in binary form under this license must not be reverse
     *    engineered, decompiled, modified and/or disassembled.
     * 
     * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
     * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
     * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     * 
     */
    /** @file
    * @defgroup temperature_example_main main.c
    * @{
    * @ingroup temperature_example
    * @brief Temperature Example Application main file.
    * @details
    * This file contains the source code for a sample application using the temperature sensor.
    * This contains workaround for PAN_028 rev2.0A anomalies 28, 29,30 and 31. PAN 43 is not covered.
    *  - PAN_028 rev2.0A anomaly 28 - TEMP: Negative measured values are not represented correctly
    *  - PAN_028 rev2.0A anomaly 29 - TEMP: Stop task clears the TEMP register.
    *  - PAN_028 rev2.0A anomaly 30 - TEMP: Temp module analog front end does not power down when DATARDY event occurs.
    *  - PAN_028 rev2.0A anomaly 31 - TEMP: Temperature offset value has to be manually loaded to the TEMP module
    *  - PAN_028 rev2.0A anomaly 43 - TEMP: Using PPI between DATARDY event and START task is not functional.
    *
    */
    
    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include "nrf.h"
    #include "nrf_delay.h"
    #include "boards.h"
    #include "app_error.h"
    #include "bsp.h"
    #include "slip.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    #include "nrf_drv_uart.h"
    #include "nrf_serial_dfu.h"
    #define MTU_SIZE 129
    #define MAX_ACTUAL_PAYLOAD (MTU_SIZE/2-2)
    #define MAX_OBJECT_SIZE 4096
    uint32_t find_image_size(uint32_t start_address, uint32_t end_address);
    
    /** @brief Function for main application entry.
     */
     static serial_dfu_t m_dfu;
     static void uart_event_handler(nrf_drv_uart_event_t * p_event, void * p_context)
    {
        uint32_t err_code;
        switch (p_event->type)
        {
            case NRF_DRV_UART_EVT_TX_DONE:
        
                break;
    
            case NRF_DRV_UART_EVT_RX_DONE:
                //Todo: instead of using delay response code checking should be done here.
                err_code = nrf_drv_uart_rx(&m_dfu.uart_instance, &m_dfu.uart_buffer, 100);
                if (err_code != NRF_SUCCESS)
                {
                    NRF_LOG_ERROR("Failed initializing rx");
                }
                break;
    
            case NRF_DRV_UART_EVT_ERROR:
        
                break;
        }
    }
    
    // Simple algorithm for finding the end of the firmware image in the flash
    // Assumes that there is nothing stored in the flash after the image, otherwise it will fail
    uint32_t find_image_size(uint32_t start_address, uint32_t end_address)
    {
        uint32_t img_size = end_address - 1;
        while(img_size > start_address)
        {
            if(*((uint8_t*)img_size) != 0xFF) break;
            img_size -= 1;
        }
        img_size = img_size + 1 - start_address;   
        
        return img_size;    
    }
    
    static uint8_t encoded_slip_packet[ MTU_SIZE ] = {0};
    static uint8_t payload[ MAX_ACTUAL_PAYLOAD ] = {0};
    static void uart_encode_and_send(serial_dfu_t * p_dfu,uint8_t *data, uint32_t length)
    {    uint32_t encoded_slip_packet_length;
        (void)slip_encode(encoded_slip_packet, data, length, &encoded_slip_packet_length);
        (void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length);
         while(nrf_drv_uart_tx_in_progress(&p_dfu->uart_instance));  
    }
        
    static void uart_send_init_packet(serial_dfu_t * p_dfu,uint8_t *init_data, uint32_t length)
    {
        uint32_t i=length;
        uint32_t encoded_slip_packet_length;
    
        //Create Object
        payload[0]=0x01;
        payload[1]=0x01;//Command Object
        payload[2]=length;
        payload[3]=length>>8;
        payload[4]=length>>16;
        payload[5]=length>>24;
        uart_encode_and_send(p_dfu,payload,6);
        nrf_delay_ms(5);
        while(length>0)
        {   
            encoded_slip_packet[0]=0x08;//Write object opcode
    
            if (length>=MAX_ACTUAL_PAYLOAD)
            {
                (void)slip_encode(&encoded_slip_packet[1], init_data, MAX_ACTUAL_PAYLOAD, &encoded_slip_packet_length);
                length= length-MAX_ACTUAL_PAYLOAD;
                init_data = init_data+MAX_ACTUAL_PAYLOAD;
            }else
            {
                (void)slip_encode(&encoded_slip_packet[1], init_data, length, &encoded_slip_packet_length);
                length=0;
            }
            NRF_LOG_INFO("Data : %d %d %d", length,&init_data,MAX_ACTUAL_PAYLOAD);
            // send
            (void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length+1);
             while(nrf_drv_uart_tx_in_progress(&p_dfu->uart_instance));    
            
        }
        //Ask for CRC
         payload[0]=0x03;
         uart_encode_and_send(p_dfu,payload,1);
         nrf_delay_ms(5);
        //To-DO: check CRC
        //Execute init packet
         payload[0]=0x04;
         uart_encode_and_send(p_dfu,payload,1);
        
    }
    static void uart_send_application_image(serial_dfu_t * p_dfu,uint8_t *data, uint32_t length)
    {
        uint32_t i=length;
        uint32_t encoded_slip_packet_length;
    
        //Create Object
        payload[0]=0x01;
        payload[1]=0x02;//Command Object
        payload[2]=length;
        payload[3]=length>>8;
        payload[4]=length>>16;
        payload[5]=length>>24;
        uart_encode_and_send(p_dfu,payload,6);
        nrf_delay_ms(5);
        while(length>0)
        {   
            encoded_slip_packet[0]=0x08;//Write object opcode
    
            if (length>=MAX_ACTUAL_PAYLOAD)
            {
                (void)slip_encode(&encoded_slip_packet[1], data, MAX_ACTUAL_PAYLOAD, &encoded_slip_packet_length);
                length= length-MAX_ACTUAL_PAYLOAD;
                data = data+MAX_ACTUAL_PAYLOAD;
            }else
            {
                (void)slip_encode(&encoded_slip_packet[1], data, length, &encoded_slip_packet_length);
                length=0;
            }
            NRF_LOG_INFO("Data : %d %d %d", length,&data,MAX_ACTUAL_PAYLOAD);
            // send
            (void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length+1);
             while(nrf_drv_uart_tx_in_progress(&p_dfu->uart_instance));    
            
        }
        //Ask for CRC
         payload[0]=0x03;
         uart_encode_and_send(p_dfu,payload,1);
         nrf_delay_ms(5);
        //To-DO: check CRC
        //Execute init packet
         payload[0]=0x04;
         uart_encode_and_send(p_dfu,payload,1);
         nrf_delay_ms(15);
    }
    
    
    static void uart_send_ping_packet(serial_dfu_t          * p_dfu        )
    {
        uint32_t encoded_slip_packet_length;
        uint8_t init_data[4]={0x09,0x01};
        uint32_t length= 2;
        (void)slip_encode(encoded_slip_packet, init_data, length, &encoded_slip_packet_length);
    
      
    
        // send
        (void)nrf_drv_uart_tx(&p_dfu->uart_instance, encoded_slip_packet, encoded_slip_packet_length);
    }
    
    
    void start_DFU(void)
    {
        uint32_t data_size;
        uint32_t application_image_address = 0x30000;
        nrf_gpio_pin_clear(LED_1);
        data_size = find_image_size(0x9000,0x10000);
        NRF_LOG_INFO("Init data size: %d", data_size);
        uint8_t *data;
        data= (uint8_t *) 0x9000;
        NRF_LOG_INFO("Data : %x %x %d", *data, *(data+1),MAX_ACTUAL_PAYLOAD);
        nrf_delay_ms(5);
        uart_send_init_packet(&m_dfu,(uint8_t *) 0x9000,data_size);
        nrf_delay_ms(500);
        
        data_size = find_image_size(application_image_address,application_image_address+0x40000);
        NRF_LOG_INFO("Image size: %d", data_size);
        //Max object size is 4096 bytes.
         while(data_size>0)
        {   
            if ( data_size>=MAX_OBJECT_SIZE)
            {
                
                uart_send_application_image(&m_dfu,(uint8_t *) application_image_address,MAX_OBJECT_SIZE);
                data_size = data_size- MAX_OBJECT_SIZE;
                application_image_address= application_image_address + MAX_OBJECT_SIZE;
            }else
            {
                 uart_send_application_image(&m_dfu,(uint8_t *) application_image_address,data_size);
                 data_size = 0;
            }
        }  
            
        nrf_delay_ms(1000);
        nrf_gpio_pin_set(LED_1);
    }
    
    uint32_t serial_dfu_transport_init(void)
    {
        uint32_t err_code;
    
        //leds_init();
    
        m_dfu.slip.p_buffer      = m_dfu.recv_buffer;
        m_dfu.slip.current_index = 0;
        m_dfu.slip.buffer_len    = sizeof(m_dfu.recv_buffer);
        m_dfu.slip.state         = SLIP_STATE_DECODING;
    
        nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG;
    
        uart_config.pseltxd   = TX_PIN_NUMBER;
        uart_config.pselrxd   = RX_PIN_NUMBER;
        uart_config.pselcts   = CTS_PIN_NUMBER;
        uart_config.pselrts   = RTS_PIN_NUMBER;
        uart_config.hwfc      = NRF_UART_HWFC_ENABLED;
        uart_config.p_context = &m_dfu;
    
    
        nrf_drv_uart_t instance =  NRF_DRV_UART_INSTANCE(0);
        memcpy(&m_dfu.uart_instance, &instance, sizeof(instance));
    
        err_code =  nrf_drv_uart_init(&m_dfu.uart_instance, &uart_config, uart_event_handler);
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_ERROR("Failed initializing uart");
            return err_code;
        }
    
        nrf_drv_uart_rx_enable(&m_dfu.uart_instance);
    
        err_code = nrf_drv_uart_rx(&m_dfu.uart_instance, &m_dfu.uart_buffer, 100);
        if (err_code != NRF_SUCCESS)
        {
            NRF_LOG_ERROR("Failed initializing rx");
        }
        
        NRF_LOG_DEBUG("UART initialized");
    
        return err_code;
    }
    
    
    uint32_t serial_dfu_transport_close(void)
    {
        nrf_drv_uart_uninit(&m_dfu.uart_instance);
        return NRF_SUCCESS;
    }
    int main(void)
    {
        // This function contains workaround for PAN_028 rev2.0A anomalies 28, 29,30 and 31.
        int32_t volatile temp;
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
        serial_dfu_transport_init();
        nrf_delay_ms(500);
        uart_send_ping_packet(&m_dfu);
        nrf_gpio_cfg_output(LED_1);
        nrf_gpio_pin_set(LED_1);
        nrf_gpio_cfg_input(BUTTON_1,NRF_GPIO_PIN_PULLUP);
       while(1)
        {
            
            if(nrf_gpio_pin_read(BUTTON_1) == 0)
            {
                start_DFU();
            }
        }
    }
    
    
    /** @} */
    
    My host program is written according to the following main.c, my debugging program is written more chaotic, and there are garbled comments, you can see this main.c

Children
Related