lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (C) 2016, ZIXC Corporation. |
| 3 | * |
| 4 | * File Name: |
| 5 | * File Mark: |
| 6 | * Description: |
| 7 | * Others: |
| 8 | * Version: 1.0 |
| 9 | * Author: geanfeng |
| 10 | * Date: 2013-3-4 |
| 11 | * History 1: |
| 12 | * Date: |
| 13 | * Version: |
| 14 | * Author: |
| 15 | * Modification: |
| 16 | * History 2: |
| 17 | ********************************************************************************/ |
| 18 | |
| 19 | /**************************************************************************** |
| 20 | * Include files |
| 21 | ****************************************************************************/ |
| 22 | #include <linux/types.h> |
| 23 | #include <usb/common.h> |
| 24 | #include <command.h> |
| 25 | #include <common.h> |
| 26 | #include <net.h> |
| 27 | #include <usb/usb_config.h> |
| 28 | /**************************************************************************** |
| 29 | * Local Macros |
| 30 | ****************************************************************************/ |
| 31 | /**************************************************************************** |
| 32 | * Local Types |
| 33 | ****************************************************************************/ |
| 34 | |
| 35 | /**************************************************************************** |
| 36 | * Global Variables |
| 37 | ****************************************************************************/ |
| 38 | int dl_serial_initial = 0; |
| 39 | /**************************************************************************** |
| 40 | * Global Function Prototypes |
| 41 | ****************************************************************************/ |
| 42 | extern int tsp_usb_init(void); |
| 43 | extern WORD32 usb_read(WORD32 dwLen, BYTE *pchBuf); |
| 44 | |
| 45 | |
| 46 | extern WORD32 usb_write(WORD32 dwLen, BYTE *pchBuf); |
| 47 | |
| 48 | extern int UART_Read(char *pchBuf, int dwLen); |
| 49 | extern int UART_Write(char *pchBuf, int dwLen); |
| 50 | /**************************************************************************** |
| 51 | * Function Definitions |
| 52 | ****************************************************************************/ |
| 53 | |
| 54 | /******************************************************************************* |
| 55 | * Function:downloader_serial_init |
| 56 | * Description: |
| 57 | * Parameters: |
| 58 | * Input: |
| 59 | * |
| 60 | * Output: |
| 61 | * |
| 62 | * Returns: |
| 63 | * |
| 64 | * |
| 65 | * Others: |
| 66 | ********************************************************************************/ |
| 67 | void downloader_serial_init(void) |
| 68 | { |
| 69 | #if CONFIG_USB_DL |
| 70 | usb_boot(SYS_USB_BASE); /*usb*/ |
| 71 | #endif |
| 72 | dl_serial_initial =1; |
| 73 | } |
| 74 | /******************************************************************************* |
| 75 | * Function:downloader_serial_read |
| 76 | * Description: |
| 77 | * Parameters: |
| 78 | * Input: |
| 79 | * |
| 80 | * Output: |
| 81 | * |
| 82 | * Returns: |
| 83 | * |
| 84 | * |
| 85 | * Others: |
| 86 | ********************************************************************************/ |
| 87 | unsigned int g_testLen = 0; |
| 88 | unsigned int g_testAddr = 0; |
| 89 | unsigned int g_testFlag1 = 0; |
| 90 | int downloader_serial_read(char * buffer,unsigned int len) |
| 91 | { |
| 92 | int ret = 0; |
| 93 | |
| 94 | if(!dl_serial_initial) |
| 95 | return 0; |
| 96 | g_testAddr = (unsigned int)&len; |
| 97 | #if CONFIG_USB_DL |
| 98 | ret = usb_read(len,buffer); |
| 99 | #else |
| 100 | ret = UART_Read(buffer,len); |
| 101 | #endif |
| 102 | g_testLen = len; |
| 103 | g_testFlag1 = 1; |
| 104 | if(!ret) |
| 105 | { |
| 106 | return 0; /*error return 0*/ |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | |
| 111 | return ret;/*actual length*/ |
| 112 | } |
| 113 | } |
| 114 | /******************************************************************************* |
| 115 | * Function:downloader_serial_read_actuallen |
| 116 | * Description: |
| 117 | * Parameters: |
| 118 | * Input: |
| 119 | * |
| 120 | * Output: |
| 121 | * |
| 122 | * Returns: |
| 123 | * |
| 124 | * |
| 125 | * Others: |
| 126 | ********************************************************************************/ |
| 127 | int downloader_serial_read_actuallen(char * buffer,unsigned int len) |
| 128 | { |
| 129 | char* pBuffer = buffer; |
| 130 | uint readLength = 0; |
| 131 | uint length = 0; |
| 132 | //printf("start to rececive len = %d \n",len); |
| 133 | #if CONFIG_USB_DL |
| 134 | while(readLength < len) |
| 135 | { |
| 136 | length = downloader_serial_read(pBuffer,512); |
| 137 | //printf(" receiving len = %d \n",length); |
| 138 | if(length) |
| 139 | { |
| 140 | readLength += length; |
| 141 | //printf(" readLength = %d \n",readLength); |
| 142 | pBuffer += length; |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | continue; |
| 147 | } |
| 148 | } |
| 149 | //printf("finish receiving len = %d \n",len); |
| 150 | #else |
| 151 | |
| 152 | length = downloader_serial_read(pBuffer,len); |
| 153 | #endif |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /******************************************************************************* |
| 158 | * Function:downloader_serial_write |
| 159 | * Description: |
| 160 | * Parameters: |
| 161 | * Input: |
| 162 | * |
| 163 | * Output: |
| 164 | * |
| 165 | * Returns: |
| 166 | * |
| 167 | * |
| 168 | * Others: |
| 169 | ********************************************************************************/ |
| 170 | int downloader_serial_write(const char * buffer,unsigned int len) |
| 171 | { |
| 172 | if(!dl_serial_initial) |
| 173 | return 0; |
| 174 | |
| 175 | #if CONFIG_USB_DL |
| 176 | return usb_write(len,(char *)buffer); |
| 177 | #else |
| 178 | return UART_Write((char *)buffer,len); |
| 179 | #endif |
| 180 | } |
| 181 | /******************************************************************************* |
| 182 | * Function:downloader_serial_write_actuallen |
| 183 | * Description: |
| 184 | * Parameters: |
| 185 | * Input: |
| 186 | * |
| 187 | * Output: |
| 188 | * |
| 189 | * Returns: |
| 190 | * |
| 191 | * |
| 192 | * Others: |
| 193 | ********************************************************************************/ |
| 194 | int downloader_serial_write_actuallen(const char * buffer,unsigned int len) |
| 195 | { |
| 196 | const char* pBuffer = buffer; |
| 197 | uint writeLength = 0; |
| 198 | uint length = 0; |
| 199 | |
| 200 | /*need modify*/ |
| 201 | while(writeLength < len) |
| 202 | { |
| 203 | |
| 204 | length = MIN(len-writeLength,136192); |
| 205 | //printf("prepare to write len=%d\n",length); |
| 206 | downloader_serial_write(pBuffer,length); |
| 207 | //printf("this time usb write len=%d\n",length); |
| 208 | writeLength += length; |
| 209 | //printf("usb have writen len=%d\n",writeLength); |
| 210 | |
| 211 | pBuffer += length; |
| 212 | } |
| 213 | return 0; |
| 214 | } |
| 215 | |