1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 // Software and any modification/derivatives thereof.
18 // No right, ownership, or interest to MStar Software and any
19 // modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 // supplied together with third party`s software and the use of MStar
23 // Software may require additional licenses from third parties.
24 // Therefore, you hereby agree it is your sole responsibility to separately
25 // obtain any and all third party right and license necessary for your use of
26 // such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 // MStar`s confidential information and you agree to keep MStar`s
30 // confidential information in strictest confidence and not disclose to any
31 // third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 // kind. Any warranties are hereby expressly disclaimed by MStar, including
35 // without limitation, any warranties of merchantability, non-infringement of
36 // intellectual property rights, fitness for a particular purpose, error free
37 // and in conformity with any international standard. You agree to waive any
38 // claim against MStar for any loss, damage, cost or expense that you may
39 // incur related to your use of MStar Software.
40 // In no event shall MStar be liable for any direct, indirect, incidental or
41 // consequential damages, including without limitation, lost of profit or
42 // revenues, lost or damage of data, and unauthorized system use.
43 // You agree that this Section 4 shall still apply without being affected
44 // even if MStar Software has been modified by MStar in accordance with your
45 // request or instruction for your use, except otherwise agreed by both
46 // parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 // services in relation with MStar Software to you for your use of
50 // MStar Software in conjunction with your or your customer`s product
51 // ("Services").
52 // You understand and agree that, except otherwise agreed by both parties in
53 // writing, Services are provided on an "AS IS" basis and the warranty
54 // disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 // or otherwise:
58 // (a) conferring any license or right to use MStar name, trademark, service
59 // mark, symbol or any other identification;
60 // (b) obligating MStar or any of its affiliates to furnish any person,
61 // including without limitation, you and your customers, any assistance
62 // of any kind whatsoever, or any information; or
63 // (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 // of Taiwan, R.O.C., excluding its conflict of law rules.
67 // Any and all dispute arising out hereof or related hereto shall be finally
68 // settled by arbitration referred to the Chinese Arbitration Association,
69 // Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 // Rules of the Association by three (3) arbitrators appointed in accordance
71 // with the said Rules.
72 // The place of arbitration shall be in Taipei, Taiwan and the language shall
73 // be English.
74 // The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ///////////////////////////////////////////////////////////////////////////////
94
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file drvUART.c
98 /// @brief UART Driver Interface
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101
102
103 //-------------------------------------------------------------------------------------------------
104 // Include Files
105 //-------------------------------------------------------------------------------------------------
106 // Common Definition
107 #include "MsCommon.h"
108 #include "MsVersion.h"
109 #include <string.h>
110 #include "MsIRQ.h"
111 #include "drvMMIO.h"
112
113 #include "ULog.h"
114
115 // Internal Definition
116 #include "regUART.h"
117 #include "drvUART.h"
118 #include "halUART.h"
119 #include "utopia.h"
120
121 //-------------------------------------------------------------------------------------------------
122 // Driver Compiler Options
123 //-------------------------------------------------------------------------------------------------
124
125
126 //-------------------------------------------------------------------------------------------------
127 // Local Defines
128 //-------------------------------------------------------------------------------------------------
129 #define TAG_UART "UART"
130
131 #define UART_DBG_FUNC() if (_uartDbgLevel >= E_UART_DBGLV_ALL) \
132 {MS_DEBUG_MSG(ULOGD(TAG_UART, "\t==== %s ====\n", __FUNCTION__));}
133 #define UART_DBG_INFO(x, args...) if (_uartDbgLevel >= E_UART_DBGLV_INFO ) \
134 {MS_DEBUG_MSG(ULOGI(TAG_UART, x, ##args));}
135 #define UART_DBG_ERR(x, args...) if (_uartDbgLevel >= E_UART_DBGLV_ERR_ONLY) \
136 {MS_DEBUG_MSG(ULOGE(TAG_UART, x, ##args));}
137
138 //-------------------------------------------------------------------------------------------------
139 // Local Structurs
140 //-------------------------------------------------------------------------------------------------
141 #if (defined(MCU_AEON))
142 struct devio aeon_uart_io =
143 {
144 .open = HAL_UART_Aeon_Open,
145 .close = HAL_UART_Aeon_Close,
146 .read = HAL_UART_Aeon_Read,
147 .write = HAL_UART_Aeon_Write,
148 .poll = HAL_UART_Aeon_Poll,
149 .ioctl = HAL_UART_Aeon_Ioctl,
150 .event = 0,
151 };
152 #endif
153
154 struct devio piu_uart_io =
155 {
156 .open = HAL_UART_PIU_Open,
157 .close = HAL_UART_PIU_Close,
158 .read = HAL_UART_PIU_Read,
159 .write = HAL_UART_PIU_Write,
160 .poll = HAL_UART_PIU_Poll,
161 .ioctl = HAL_UART_PIU_Ioctl,
162 .event = 0,
163 };
164
165 #undef putchar
166 #undef kbhit
167 #undef getchar
168 //-------------------------------------------------------------------------------------------------
169 // Global Variables
170 //-------------------------------------------------------------------------------------------------
171
172
173 //-------------------------------------------------------------------------------------------------
174 // Local Variables
175 //-------------------------------------------------------------------------------------------------
176
177 #define HAL_UART_CAPS (UART_CAPS_AEON | UART_CAPS_PIU)
178
179 // Uart driver info
180 static UART_DrvInfo _uartInfo =
181 {
182 .Init = FALSE,
183 .Caps = HAL_UART_CAPS,
184 };
185
186 static UART_DrvStatus _uartStatus =
187 {
188 .HwBusy = FALSE,
189 };
190
191 static MSIF_Version _uartVersion = {
192 .DDI = { UART_DRV_VERSION },
193 };
194
195 static UART_DbgLv _uartDbgLevel = E_UART_DBGLV_ERR_ONLY;
196
197 // UART Driver parameter
198 #define UART_MAX 4
199 #if defined(CONFIG_FPGA)
200 #define DEFAULT_BAUDRATE 38400
201 #else
202 #define DEFAULT_BAUDRATE 115200
203 #endif
204
205 static UART_DEVICE_TYPE _eDefaultUart = E_UART_PIU_UART0;
206 static MS_U32 _u32DefaultUartBaudrate = DEFAULT_BAUDRATE;
207 static MS_U32 _hDefaultUart ;
208 static MS_BOOL _bInitFlag =FALSE;
209
210 typedef struct _UART_DrvDisc
211 {
212 UART_DEVICE_TYPE Uart_dev;
213 MS_U32 baudrate;
214 MS_U32 UartHandle;
215 ms_uart_rx_callback rx_cb;
216 MS_BOOL enable;
217 }UART_DrvDisc;
218
219 static UART_DrvDisc _UartDisc[UART_MAX]=
220 {
221 {E_UART_PIU_UART0,DEFAULT_BAUDRATE,0,NULL,FALSE},
222 {E_UART_PIU_UART1,DEFAULT_BAUDRATE,0,NULL,FALSE},
223 {E_UART_PIU_UART2,DEFAULT_BAUDRATE,0,NULL,FALSE},
224 {E_UART_PIU_FUART0,DEFAULT_BAUDRATE,0,NULL,FALSE}
225 };
226
227 #if UART_TYPE_EYWA
228
229 static Eywa_FastUart_struct _EywaPacket =
230 {
231 .data_type = E_EYWA_INVALID,
232 .data_type_inv = E_EYWA_INV_INVALID,
233 .len = EYWA_PAYLOAD_LEN,
234 .payload = {0},
235 .parity = EYWA_DEFAULT_PARITY
236 };
237 #endif
238
239 //-------------------------------------------------------------------------------------------------
240 // Debug Functions
241 //-------------------------------------------------------------------------------------------------
242
243
244 //-------------------------------------------------------------------------------------------------
245 // Local Functions
246 //-------------------------------------------------------------------------------------------------
247
248
249 //-------------------------------------------------------------------------------------------------
250 // Global Functions
251 //-------------------------------------------------------------------------------------------------
MDrv_UART_SetIOMapBase(void)252 UART_Result MDrv_UART_SetIOMapBase(void)
253 {
254 MS_VIRT u32BaseAddr = 0;
255 MS_PHY u32BaseSize = 0;
256 MS_VIRT u32PMBaseAddr = 0;
257 MS_PHY u32PMBaseSize = 0;
258
259 if(!MDrv_MMIO_GetBASE(&u32BaseAddr, &u32BaseSize, MS_MODULE_UART))
260 {
261 UART_DBG_ERR("Get IOMAP Base faill!\n");
262 return E_UART_FAIL;
263 }
264 if(!MDrv_MMIO_GetBASE(&u32PMBaseAddr, &u32PMBaseSize, MS_MODULE_PM))
265 {
266 UART_DBG_ERR("Get IOMAP Base faill!\n");
267 return E_UART_FAIL;
268 }
269 UART_DBG_INFO("Get IOMAP ID:%u Base:%lx!\n", MS_MODULE_UART, u32BaseAddr);
270 UART_DBG_INFO("Get PM IOMAP ID:%u Base:%lx!\n", MS_MODULE_PM, u32PMBaseAddr);
271 HAL_UART_SetIOMapBase(u32BaseAddr, u32PMBaseAddr);
272
273 return E_UART_OK;
274 }
275
MDrv_UART_Init(UART_DEVICE_TYPE uart_dev,MS_U32 baudrate)276 void MDrv_UART_Init(UART_DEVICE_TYPE uart_dev, MS_U32 baudrate)
277 {
278 if ( E_UART_FAIL == MDrv_UART_SetIOMapBase())
279 {
280 return;
281 }
282
283 #if (defined(MCU_AEON))
284 _uart_dev[E_UART_AEON].device.magic = DEVICE_MAGIC ;
285 _uart_dev[E_UART_AEON].device.refcnt = 0 ;
286 _uart_dev[E_UART_AEON].device.flags = 0 ;
287 _uart_dev[E_UART_AEON].device.devio = &aeon_uart_io ;
288 _uart_dev[E_UART_AEON].regbase = UART_AEON_ADDR ;
289 _uart_dev[E_UART_AEON].uarttype = UART_TYPE_AEON ;
290 #if defined(__AEONR2__)
291 _uart_dev[E_UART_AEON_R2].device.magic = DEVICE_MAGIC ;
292 _uart_dev[E_UART_AEON_R2].device.refcnt = 0 ;
293 _uart_dev[E_UART_AEON_R2].device.flags = 1 ;
294 _uart_dev[E_UART_AEON_R2].device.devio = &aeon_uart_io ;
295 _uart_dev[E_UART_AEON_R2].regbase = UART_AEON_ADDR ;
296 _uart_dev[E_UART_AEON_R2].uarttype = UART_TYPE_AEON ;
297 #endif
298
299 _uart_dev[E_UART_SECURE_R2].device.magic = DEVICE_MAGIC ;
300 _uart_dev[E_UART_SECURE_R2].device.refcnt = 0 ;
301 _uart_dev[E_UART_SECURE_R2].device.flags = 2 ;
302 _uart_dev[E_UART_SECURE_R2].device.devio = &aeon_uart_io ;
303 _uart_dev[E_UART_SECURE_R2].regbase = UART_AEON_ADDR ;
304 _uart_dev[E_UART_SECURE_R2].uarttype = UART_TYPE_AEON ;
305 #endif
306
307 #if defined (UART0_PIU_ADDR)
308 _uart_dev[E_UART_PIU_UART0].device.magic = DEVICE_MAGIC ;
309 _uart_dev[E_UART_PIU_UART0].device.refcnt = 0 ;
310 _uart_dev[E_UART_PIU_UART0].device.flags = 0 ;
311 _uart_dev[E_UART_PIU_UART0].device.devio = &piu_uart_io ;
312 _uart_dev[E_UART_PIU_UART0].regbase = UART0_PIU_ADDR ;
313 _uart_dev[E_UART_PIU_UART0].uarttype = UART_TYPE_PIU ;
314 #endif
315
316 #if defined (UART1_PIU_ADDR)
317 _uart_dev[E_UART_PIU_UART1].device.magic = DEVICE_MAGIC ;
318 _uart_dev[E_UART_PIU_UART1].device.refcnt = 0 ;
319 _uart_dev[E_UART_PIU_UART1].device.flags = 0 ;
320 _uart_dev[E_UART_PIU_UART1].device.devio = &piu_uart_io ;
321 _uart_dev[E_UART_PIU_UART1].regbase = UART1_PIU_ADDR ;
322 _uart_dev[E_UART_PIU_UART1].uarttype = UART_TYPE_PIU ;
323 #endif
324
325 #if defined (UART2_PIU_ADDR)
326 _uart_dev[E_UART_PIU_UART2].device.magic = DEVICE_MAGIC ;
327 _uart_dev[E_UART_PIU_UART2].device.refcnt = 0 ;
328 _uart_dev[E_UART_PIU_UART2].device.flags = 0 ;
329 _uart_dev[E_UART_PIU_UART2].device.devio = &piu_uart_io ;
330 _uart_dev[E_UART_PIU_UART2].regbase = UART2_PIU_ADDR ;
331 _uart_dev[E_UART_PIU_UART2].uarttype = UART_TYPE_PIU ;
332 #endif
333
334 #if defined (FUART_PIU_ADDR)
335 _uart_dev[E_UART_PIU_FUART0].device.magic = DEVICE_MAGIC ;
336 _uart_dev[E_UART_PIU_FUART0].device.refcnt = 0 ;
337 _uart_dev[E_UART_PIU_FUART0].device.flags = 0 ;
338 _uart_dev[E_UART_PIU_FUART0].device.devio = &piu_uart_io ;
339 _uart_dev[E_UART_PIU_FUART0].regbase = FUART_PIU_ADDR ;
340 _uart_dev[E_UART_PIU_FUART0].uarttype = UART_TYPE_PIU ;
341 #endif
342
343 // default initialize just init once & open uart0 for DefaultUart
344 _bInitFlag = TRUE;
345 // default initialize
346 _hDefaultUart = mdrv_uart_open(uart_dev) ; // default uart port
347 mdrv_uart_set_baudrate( _hDefaultUart, baudrate) ; // set default baud rate
348
349 // Driver Initialization
350 _uartInfo.Init = TRUE;
351 _uartStatus.HwBusy = FALSE;
352 }
353
MDrv_UART_GetDev(UART_DEVICE_TYPE uart_dev)354 struct device* MDrv_UART_GetDev(UART_DEVICE_TYPE uart_dev)
355 {
356 if((uart_dev>=UART_DEV_MAX) || (uart_dev>=E_UART_OFF))
357 return (struct device *)0;
358 return &_uart_dev[uart_dev].device;
359 }
360
MDrv_UART_GetInfo(void)361 const UART_DrvInfo* MDrv_UART_GetInfo(void)
362 {
363 return (&_uartInfo);
364 }
365
MDrv_UART_GetLibVer(const MSIF_Version ** ppVersion)366 UART_Result MDrv_UART_GetLibVer(const MSIF_Version **ppVersion)
367 {
368 // No mutex check, it can be called before Init
369 if (!ppVersion)
370 {
371 return E_UART_FAIL;
372 }
373
374 *ppVersion = &_uartVersion;
375
376 return E_UART_OK;
377 }
378
MDrv_UART_GetStatus(UART_DrvStatus * pStatus)379 UART_Result MDrv_UART_GetStatus(UART_DrvStatus *pStatus)
380 {
381 memcpy(pStatus, &_uartStatus, sizeof(UART_DrvStatus));
382 return E_UART_OK;
383 }
384
385
MDrv_UART_SetRxMode(MS_BOOL bEnable,void * rx_buf,MS_U16 buf_len,ms_uart_rx_callback rx_cb)386 UART_Result MDrv_UART_SetRxMode(MS_BOOL bEnable, void *rx_buf, MS_U16 buf_len, ms_uart_rx_callback rx_cb)
387 {
388 if (bEnable)
389 {
390 mdrv_uart_rx_enable(_hDefaultUart);
391 }
392 else
393 {
394 mdrv_uart_rx_disable(_hDefaultUart);
395 }
396
397 if (rx_buf && buf_len)
398 {
399 mdrv_uart_set_rx_buffer(_hDefaultUart, (void *)rx_buf, buf_len);
400 }
401 else
402 {
403 mdrv_uart_set_rx_buffer(_hDefaultUart, (void *)NULL, 0);
404 }
405
406 if (rx_cb)
407 {
408 mdrv_uart_set_rx_callback(_hDefaultUart, rx_cb);
409 }
410 else
411 {
412 mdrv_uart_set_rx_callback(_hDefaultUart, NULL);
413 }
414 return E_UART_OK;
415 }
416
417
MDrv_UART_SetDbgLevel(MS_U8 level)418 void MDrv_UART_SetDbgLevel(MS_U8 level)
419 {
420 _uartDbgLevel = (UART_DbgLv)level;
421 }
422
MDrv_UART_SetPMRxEnable(MS_BOOL bEnable)423 UART_Result MDrv_UART_SetPMRxEnable(MS_BOOL bEnable)
424 {
425 return HAL_UART_SetPMRxEnable(bEnable);
426 }
427
MDrv_UART_PutChar(MS_U8 u8Ch)428 void MDrv_UART_PutChar(MS_U8 u8Ch)
429 {
430 MS_U8 u8Str[2] ;
431
432 _uartStatus.HwBusy = TRUE;
433 u8Str[0] = u8Ch ;
434 u8Str[1] = 0 ;
435 mdrv_uart_write( _hDefaultUart, u8Str, 0x01) ;
436 _uartStatus.HwBusy = FALSE;
437 }
438
MDrv_UART_PutString(char * pu8Str)439 void MDrv_UART_PutString(char *pu8Str)
440 {
441 _uartStatus.HwBusy = TRUE;
442
443 mdrv_uart_write( _hDefaultUart, (const MS_U8 *)pu8Str, strlen(pu8Str)) ;
444
445 _uartStatus.HwBusy = FALSE;
446 }
447
448
449
450 #if 0 //[TODO] - jerry //[SVP] add for NDS/SVP RPC
451 #define UART_GETSTR_TIMEOUT 1000
452 #define UART_GETCHAR_DELAY 5 // 5ms (min:2ms)
453 #endif
454
MDrv_UART_GetChar(void)455 MS_U8 MDrv_UART_GetChar(void)
456 {
457 MS_U8 u8Str[2] ;
458
459 _uartStatus.HwBusy = TRUE;
460 #if 0 //[TODO] - jerry //[SVP] add for NDS/SVP RPC
461 MS_U16 len = 0;
462 #if defined(MSOS_TYPE_ECOS) //[SVP] add for NDS SVP RPC
463 while (1)
464 {
465 #endif
466 len = mdrv_uart_read( _hDefaultUart, u8Str, 0x01) ;
467 #if defined(MSOS_TYPE_ECOS) //[SVP] add for NDS SVP RPC
468 if (len) break;
469
470 // MsOS_DelayTask(1);
471 }
472 #endif
473 #else
474 mdrv_uart_read( _hDefaultUart, u8Str, 0x01) ;
475 #endif
476
477 u8Str[1] = 0 ;
478 _uartStatus.HwBusy = FALSE;
479
480 return u8Str[0] ;
481 }
482
483
484 #if 0 //[TODO] - jerry //[SVP] add for NDS/SVP RPC
485 MS_U8 MDrv_UART_GetString(char *pu8Str, MS_U32 max_len)
486 {
487 MS_U32 start_time = 0;
488 MS_U16 len, ret_len = 0;
489
490 _uartStatus.HwBusy = TRUE;
491
492 start_time = MsOS_GetSystemTime();
493 while (max_len > 0)
494 {
495 len = mdrv_uart_read( _hDefaultUart, pu8Str, max_len);
496 if (len) // len <= max_len
497 {
498 max_len -= len;
499 pu8Str += len;
500 ret_len += len;
501 start_time = MsOS_GetSystemTime();
502 }
503 if ((MsOS_GetSystemTime() - start_time) > UART_GETSTR_TIMEOUT)
504 {
505 break;
506 }
507 MsOS_DelayTask(UART_GETCHAR_DELAY);
508 }
509
510 if (max_len > 0)
511 {
512 *(MS_U8*)pu8Str = 0x00;
513 }
514
515 _uartStatus.HwBusy = FALSE;
516
517 return ret_len;
518 }
519 #endif
520
521
MDrv_UART_GetFile(MS_U8 * pu8Buf,MS_U32 u32Len)522 void MDrv_UART_GetFile(MS_U8 *pu8Buf, MS_U32 u32Len)
523 {
524 _uartStatus.HwBusy = TRUE;
525 mdrv_uart_read( _hDefaultUart, pu8Buf, u32Len) ;
526 _uartStatus.HwBusy = FALSE;
527 }
528
mdrv_uart_open(UART_DEVICE_TYPE uart_dev)529 MS_U32 mdrv_uart_open(UART_DEVICE_TYPE uart_dev)
530 {
531 struct device *dev;
532
533 _uartStatus.HwBusy = TRUE;
534 if( _uart_dev[uart_dev].device.magic != DEVICE_MAGIC )
535 return 0 ;
536 dev = &_uart_dev[uart_dev].device;
537 _uartStatus.HwBusy = FALSE;
538 if((uart_dev >= E_UART_PIU_UART0)&&(uart_dev <= E_UART_PIU_FUART0))
539 {
540 _UartDisc[uart_dev-3].enable = TRUE;
541 _UartDisc[uart_dev-3].Uart_dev = uart_dev;
542 _UartDisc[uart_dev-3].UartHandle = ((MS_U32)(dev->devio->open((device_t)dev, 0)));
543 return _UartDisc[uart_dev-3].UartHandle;
544 }
545 else
546 return ((MS_U32)(dev->devio->open((device_t)dev, 0)));
547 }
548
mdrv_uart_close(MS_VIRT hUart)549 MS_U16 mdrv_uart_close(MS_VIRT hUart)
550 {
551 struct device *dev;
552 MS_U8 i = 0;
553 MS_VIRT defaulthUart = (MS_VIRT)_hDefaultUart;
554 ms_uart_dev_t phUart = (ms_uart_dev_t) hUart;
555
556 _uartStatus.HwBusy = TRUE;
557 if (hUart == 0)
558 {
559 phUart = (ms_uart_dev_t) defaulthUart;
560 }
561 if (phUart)
562 {
563 dev = &(phUart->device);
564 if (dev->devio->close)
565 {
566 dev->devio->close((device_t)dev);
567 }
568 for(i=0;i<UART_MAX;i++)
569 {
570 if(hUart ==_UartDisc[i].UartHandle)
571 {
572 _UartDisc[i].enable = FALSE;
573 break;
574 }
575 }
576 }
577 _uartStatus.HwBusy = FALSE;
578
579 return -1;
580 }
581
mdrv_uart_set_rx_callback(MS_VIRT hUart,ms_uart_rx_callback rx_cb)582 MS_U16 mdrv_uart_set_rx_callback(MS_VIRT hUart, ms_uart_rx_callback rx_cb)
583 {
584 struct device *dev;
585 ms_uart_dev_t phUart = (ms_uart_dev_t) hUart;
586 MS_U8 i=0;
587
588 ULOGE(TAG_UART,"[Lwc Debug] %s : %d\n",__FUNCTION__,__LINE__);
589 if(_bInitFlag) //bakup rx call back fun
590 {
591 for(i=0;i<UART_MAX;i++)
592 {
593 if(_UartDisc[i].UartHandle == hUart)
594 {
595 _UartDisc[i].rx_cb=rx_cb;
596 break;
597 }
598 }
599 }
600 _uartStatus.HwBusy = TRUE;
601 if (hUart)
602 {
603 dev =&(phUart->device);
604 if (dev->devio->ioctl)
605 {
606 return dev->devio->ioctl((device_t)dev, IOCTL_SET_RXCALLBACK, (MS_VIRT)&rx_cb);
607 }
608 }
609 _uartStatus.HwBusy = FALSE;
610
611 return 0;
612 }
613
mdrv_uart_set_rx_callback_halreg(MS_VIRT hUart,ms_uart_rx_callback_halreg rx_cb)614 MS_U16 mdrv_uart_set_rx_callback_halreg(MS_VIRT hUart, ms_uart_rx_callback_halreg rx_cb)
615 {
616 struct device *dev;
617
618 ms_uart_dev_t phUart = (ms_uart_dev_t) hUart;
619
620 _uartStatus.HwBusy = TRUE;
621 if (hUart)
622 {
623 dev = &(phUart->device);
624 if (dev->devio->ioctl)
625 {
626 return dev->devio->ioctl((device_t)dev, IOCTL_SET_RXCALLBACK_HALREG, (MS_VIRT)&rx_cb);
627 }
628 }
629 _uartStatus.HwBusy = FALSE;
630
631 return 0;
632 }
633
mdrv_uart_set_tx_buffer(MS_VIRT hUart,void * tx_buffer,MS_U16 len)634 MS_U16 mdrv_uart_set_tx_buffer(MS_VIRT hUart, void *tx_buffer, MS_U16 len)
635 {
636 _uartStatus.HwBusy = TRUE;
637
638 ms_uart_dev_t phUart = (ms_uart_dev_t) hUart;
639
640 if (hUart)
641 {
642 #ifdef MSOS_TYPE_NOS
643 MsOS_CPU_LockInterrupt();
644 #endif
645 phUart->tx_buf = tx_buffer;
646 phUart->tx_buf_in = 0;
647 phUart->tx_buf_out = 0;
648 phUart->tx_buf_len = len;
649 #ifdef MSOS_TYPE_NOS
650 MsOS_CPU_UnLockInterrupt();
651 #endif
652 }
653 _uartStatus.HwBusy = FALSE;
654
655 return 0;
656 }
657
mdrv_uart_set_rx_buffer(MS_VIRT hUart,void * rx_buffer,MS_U16 len)658 MS_U16 mdrv_uart_set_rx_buffer(MS_VIRT hUart, void *rx_buffer, MS_U16 len)
659 {
660 _uartStatus.HwBusy = TRUE;
661
662 ms_uart_dev_t phUart = (ms_uart_dev_t) hUart;
663
664 if (hUart)
665 {
666 #ifdef MSOS_TYPE_NOS
667 MsOS_CPU_LockInterrupt();
668 #endif
669 phUart->rx_buf = rx_buffer;
670 phUart->rx_buf_in = 0;
671 phUart->rx_buf_out = 0;
672 phUart->rx_buf_len = len;
673 #ifdef MSOS_TYPE_NOS
674 MsOS_CPU_UnLockInterrupt();
675 #endif
676 }
677 _uartStatus.HwBusy = FALSE;
678
679 return 0;
680 }
681
mdrv_uart_rx_enable(MS_VIRT hUart)682 MS_U16 mdrv_uart_rx_enable(MS_VIRT hUart)
683 {
684 ms_uart_dev_t phUART = (ms_uart_dev_t) hUart;
685
686 _uartStatus.HwBusy = TRUE;
687 if (hUart)
688 {
689 phUART->rx_flags &= ~RX_FLAG_DISABLED;
690 return 0;
691 }
692 _uartStatus.HwBusy = FALSE;
693
694 return 0;
695 }
696
mdrv_uart_tx_enable(MS_VIRT hUart)697 MS_U16 mdrv_uart_tx_enable(MS_VIRT hUart)
698 {
699 ms_uart_dev_t phUART = (ms_uart_dev_t) hUart;
700
701 _uartStatus.HwBusy = TRUE;
702 if (hUart)
703 {
704 phUART->tx_flags &= ~TX_FLAG_DISABLED;
705 return 1;
706 }
707 _uartStatus.HwBusy = FALSE;
708
709 return 0;
710 }
711
mdrv_uart_rx_disable(MS_VIRT hUart)712 MS_U16 mdrv_uart_rx_disable(MS_VIRT hUart)
713 {
714 ms_uart_dev_t phUART = (ms_uart_dev_t) hUart;
715
716 _uartStatus.HwBusy = TRUE;
717 if (hUart)
718 {
719 phUART->rx_flags |= RX_FLAG_DISABLED;
720 return 1;
721 }
722 _uartStatus.HwBusy = FALSE;
723
724 return 0;
725 }
726
mdrv_uart_tx_disable(MS_VIRT hUart)727 MS_U16 mdrv_uart_tx_disable(MS_VIRT hUart)
728 {
729 ms_uart_dev_t phUART = (ms_uart_dev_t) hUart;
730
731 _uartStatus.HwBusy = TRUE;
732 if (hUart)
733 {
734 phUART->tx_flags |= TX_FLAG_DISABLED;
735 return 1;
736 }
737 _uartStatus.HwBusy = FALSE;
738
739 return 0;
740 }
741
mdrv_uart_write(MS_VIRT hUart,const MS_U8 * buf,MS_U16 len)742 MS_U16 mdrv_uart_write(MS_VIRT hUart, const MS_U8 *buf, MS_U16 len)
743 {
744 struct device *dev;
745
746 ms_uart_dev_t phUART = (ms_uart_dev_t) hUart;
747
748 _uartStatus.HwBusy = TRUE;
749 dev = &(phUART->device);
750 if (dev->devio->write && !( ((ms_uart_dev_t)hUart)->tx_flags & TX_FLAG_DISABLED))
751 {
752 return dev->devio->write((device_t)dev, (const char *)buf, (MS_U32)len, 0);
753 }
754 _uartStatus.HwBusy = FALSE;
755
756 return 0;
757 }
758
mdrv_uart_read(MS_VIRT hUart,MS_U8 * buf,MS_U32 len)759 MS_U16 mdrv_uart_read(MS_VIRT hUart, MS_U8 *buf, MS_U32 len)
760 {
761 struct device *dev;
762
763 ms_uart_dev_t phUART = (ms_uart_dev_t) hUart;
764
765 _uartStatus.HwBusy = TRUE;
766 dev = &(phUART->device);
767 if (dev->devio->read && !( ((ms_uart_dev_t)hUart)->rx_flags & RX_FLAG_DISABLED))
768 {
769 return dev->devio->read((device_t)dev, (char *)buf, len, 0);
770 }
771 _uartStatus.HwBusy = FALSE;
772
773 return 0;
774 }
775
mdrv_uart_poll(MS_VIRT hUart,int types)776 MS_U16 mdrv_uart_poll(MS_VIRT hUart, int types)
777 {
778 struct device *dev;
779
780 ms_uart_dev_t phUART = (ms_uart_dev_t) hUart;
781
782 _uartStatus.HwBusy = TRUE;
783 dev = &(phUART->device);
784 if (dev->devio->poll)
785 {
786 return dev->devio->poll((device_t)dev, types);
787 }
788 _uartStatus.HwBusy = FALSE;
789
790 return 0;
791 }
792
793
mdrv_uart_set_baudrate(MS_VIRT hUart,MS_U32 baudrate)794 MS_U16 mdrv_uart_set_baudrate(MS_VIRT hUart, MS_U32 baudrate)
795 {
796 struct device *dev;
797
798 ms_uart_dev_t phUART = (ms_uart_dev_t) hUart;
799
800 MS_U8 i=0;
801
802 for(i=0;i<UART_MAX;i++)
803 {
804 if(hUart == _UartDisc[i].UartHandle)
805 {
806 _UartDisc[i].baudrate = baudrate;
807 break;
808 }
809 }
810 _uartStatus.HwBusy = TRUE;
811 if (hUart)
812 {
813 dev = &(phUART->device);
814 if (dev->devio->ioctl)
815 {
816 return dev->devio->ioctl((device_t)dev, IOCTL_SET_BAUDRATE, (MS_VIRT)&baudrate);
817 }
818 }
819 _uartStatus.HwBusy = FALSE;
820
821 return 0;
822 }
823
mdrv_uart_get_connection(UART_PORT_TYPE uart_port)824 UART_DEVICE_TYPE mdrv_uart_get_connection(UART_PORT_TYPE uart_port)
825 {
826 UART_DEVICE_TYPE enUartDeviceType;
827
828 _uartStatus.HwBusy = TRUE;
829 enUartDeviceType = HAL_UART_Get_Connection(uart_port);
830 _uartStatus.HwBusy = FALSE;
831
832 return enUartDeviceType;
833 }
834
mdrv_uart_connect(UART_PORT_TYPE uart_port,UART_DEVICE_TYPE uart_dev)835 MS_U16 mdrv_uart_connect(UART_PORT_TYPE uart_port, UART_DEVICE_TYPE uart_dev)
836 {
837 MS_U16 u16Ret;
838
839 _uartStatus.HwBusy = TRUE;
840 u16Ret = HAL_UART_Connect(uart_port, uart_dev);
841 _uartStatus.HwBusy = FALSE;
842 return u16Ret;
843 }
844
mdrv_uart_connect_mux(UART_DEVICE_TYPE uart_dev,UART_PORT_TYPE uart_port,UART_PAD_TYPE uart_pad)845 MS_U16 mdrv_uart_connect_mux(UART_DEVICE_TYPE uart_dev, UART_PORT_TYPE uart_port, UART_PAD_TYPE uart_pad)
846 {
847 MS_U16 u16Ret = TRUE;
848
849 _uartStatus.HwBusy = TRUE;
850
851 u16Ret &= HAL_UART_Connect(uart_port, uart_dev);
852 if (u16Ret == FALSE)
853 {
854 ULOGE(TAG_UART, "Error: %s, uart_connect fail\n", __FUNCTION__);
855 }
856
857 //check uart_port and relative uart_port_pad
858 u16Ret &= HAL_UART_SetPadMux(uart_pad);
859 if (u16Ret == FALSE)
860 {
861 ULOGE(TAG_UART, "Error: %s, uart set port pad fail\n", __FUNCTION__);
862 }
863
864 _uartStatus.HwBusy = FALSE;
865
866 return u16Ret;
867 }
868
mdrv_uart_invert(UART_PORT_TYPE uart_port,MS_BOOL bInv)869 MS_U16 mdrv_uart_invert(UART_PORT_TYPE uart_port, MS_BOOL bInv)
870 {
871 MS_U16 u16Ret;
872
873 _uartStatus.HwBusy = TRUE;
874 u16Ret = HAL_UART_Invert(uart_port, bInv);
875 _uartStatus.HwBusy = FALSE;
876
877 return u16Ret;
878 }
879
880 #if UART_TYPE_EYWA
881
Eywa_Cal8BitsChecksum(MS_U8 * buf,MS_U32 len)882 MS_U8 Eywa_Cal8BitsChecksum(MS_U8 *buf, MS_U32 len)
883 {
884 MS_S32 checkSum = 0;
885 MS_U8 i;
886
887 for( i = 0 ; i < len; i++ )
888 {
889 checkSum += buf[i];
890 }
891
892 return (MS_U8)( ( -checkSum ) & 0xFF );
893 }
894
895
Eywa_UART_RecvHandler(MS_U8 c)896 void Eywa_UART_RecvHandler(MS_U8 c)
897 {
898 //MS_U8 inx=0;
899 //MDrv_WDT_Init((WDT_DbgLv)0);
900 //MDrv_TIMER_Count(E_TIMER_0,1);
901 //_EywaPacket.payload[inx] = c;
902 ULOGD(TAG_UART, "[Eywa]packet.payload = %x\n", c);
903
904 #if 0
905 if( c==EYWA_PACKET_ID && _EywaPacket.count == 0 )
906 //if( c == EYWA_PACKET_ID )
907 {
908 _EywaPacket.data_type = E_EYWA_TOUCHPAD;
909 _EywaPacket.data_type_inv = E_EYWA_INV_TOUCHPAD;
910 _EywaPacket.len = 8;
911 _EywaPacket.payload[0] = c;
912 _EywaPacket.parity = EYWA_DEFAULT_PARITY;
913 _EywaPacket.count = 7;
914 ULOGD(TAG_UART, "[Eywa]packet.payload[0] = %x\n", _EywaPacket.payload[0]);
915 }
916 else
917 {
918 if( _EywaPacket.count > 0 )
919 {
920 inx = 8 - _EywaPacket.count;
921 _EywaPacket.payload[inx] = c;
922 ULOGD(TAG_UART, "[Eywa]packet.payload[%d] = %x\n", inx, _EywaPacket.payload[inx]);
923 _EywaPacket.count -= 1;
924 }
925 #if 0
926 if(_EywaPacket.payload[7])
927 {
928 ULOGD(TAG_UART, "[Eywa]packet.data_type = %x\n", _EywaPacket.data_type);
929 ULOGD(TAG_UART, "[Eywa]packet.data_type_inv = %x\n", _EywaPacket.data_type_inv);
930 ULOGD(TAG_UART, "[Eywa]packet.len = %x\n", _EywaPacket.len);
931 for(inx = 0; inx < 8; inx++)
932 {
933 ULOGD(TAG_UART, "[Eywa]packet.payload[%d] = %x\n", inx, _EywaPacket.payload[inx]);
934 }
935 ULOGD(TAG_UART, "[Eywa]packet.parity = %x\n", _EywaPacket.parity);
936 }
937 #endif
938 }
939 #endif
940 MsOS_EnableInterrupt(E_INT_IRQ_UART1);
941 //printf("duration = %d ms\n",MDrv_TIMER_GetMs(E_TIMER_0));
942
943 }
944
945
Eywa_UART_Init(void)946 void Eywa_UART_Init(void)
947 {
948 MS_U32 g_EywaUART;
949 MS_U16 inx = 0;
950 MS_U8 p_buf[8];
951
952 ULOGD(TAG_UART, "[Eywa]Uart init\n");
953
954 //second UART mode select PAD_GPIO4/PAD_GPIO3
955 *(volatile MS_U32*)(0xbf200000+(0x0F02*4)) |= 0x0300;
956
957 mdrv_uart_connect(E_UART_PORT1, E_UART_PIU_UART1);
958
959 g_EywaUART = mdrv_uart_open(E_UART_PIU_UART1);
960 mdrv_uart_set_baudrate(g_EywaUART,57600);
961 //mdrv_uart_tx_enable(g_EywaUART);
962 //mdrv_uart_rx_enable(g_EywaUART);
963 //mdrv_uart_set_rx_buffer(g_EywaUART,p_buf,sizeof(p_buf));
964 //mdrv_uart_set_rx_callback(g_EywaUART, Eywa_UART_RecvHandler);
965
966 while(1)
967 {
968 mdrv_uart_read(g_EywaUART,p_buf,sizeof(p_buf));
969 if(p_buf[0]==0x52)
970 {
971 //_EywaPacket.payload[0] = p_buf;
972 for(inx = 0; inx < 8; inx++)
973 {
974 //mdrv_uart_read(g_EywaUART,p_buf,sizeof(p_buf));
975 _EywaPacket.payload[inx] = p_buf[inx];
976 }
977
978 if(_EywaPacket.payload[7] == Eywa_Cal8BitsChecksum(_EywaPacket.payload,7))
979 {
980 _EywaPacket.data_type = E_EYWA_TOUCHPAD;
981 _EywaPacket.data_type_inv = E_EYWA_INV_TOUCHPAD;
982 _EywaPacket.len = 8;
983 _EywaPacket.parity = EYWA_DEFAULT_PARITY;
984
985
986 ULOGD(TAG_UART, "[Eywa]packet.data_type = %x\n", _EywaPacket.data_type);
987 ULOGD(TAG_UART, "[Eywa]packet.data_type_inv = %x\n", _EywaPacket.data_type_inv);
988 ULOGD(TAG_UART, "[Eywa]packet.len = %x\n", _EywaPacket.len);
989 for(inx = 0; inx < 8; inx++)
990 {
991 ULOGD(TAG_UART, "[Eywa]packet.payload[%d] = %x\n", inx, _EywaPacket.payload[inx]);
992 }
993 ULOGD(TAG_UART, "[Eywa]packet.parity = %x\n", _EywaPacket.parity);
994 }
995 else
996 {
997 ULOGE(TAG_UART, "[Eywa]packet checksum error!!\n");
998 }
999
1000 }
1001 }
1002
1003 }
1004 #endif
1005
1006
1007 //#if !(defined(CHIP_JANUS) && defined (MSOS_TYPE_ECOS))
1008 #if !defined (MSOS_TYPE_ECOS) && !defined (MSOS_TYPE_UCOS)
putchar(int c)1009 int putchar(int c)
1010 {
1011 char x;
1012
1013 _uartStatus.HwBusy = TRUE;
1014 if (_hDefaultUart == DEVICE_NULL)
1015 {
1016 return -1;
1017 }
1018 if (c == '\n')
1019 {
1020 x = '\r';
1021 mdrv_uart_write(_hDefaultUart, (MS_U8 *)&x, 1);
1022 }
1023 x = c;
1024 mdrv_uart_write(_hDefaultUart, (MS_U8 *)&x, 1);
1025 _uartStatus.HwBusy = FALSE;
1026
1027 return c;
1028 }
1029 #endif
1030
kbhit(void)1031 int kbhit(void)
1032 {
1033 _uartStatus.HwBusy = TRUE;
1034 if (_hDefaultUart != DEVICE_NULL)
1035 {
1036 return mdrv_uart_poll(_hDefaultUart, POLLIN);
1037 }
1038 _uartStatus.HwBusy = FALSE;
1039
1040 return 0;
1041 }
1042
1043 //#if !(defined(CHIP_JANUS) && defined (MSOS_TYPE_ECOS))
1044 #if !defined (MSOS_TYPE_ECOS) && !defined (MSOS_TYPE_UCOS)
getchar(void)1045 int getchar(void)
1046 {
1047 char x = 0;
1048
1049 _uartStatus.HwBusy = TRUE;
1050 if ((_hDefaultUart != DEVICE_NULL) && (mdrv_uart_read(_hDefaultUart, (MS_U8 *)&x, 1) == 1))
1051 {
1052 return (int) (unsigned char) x;
1053 }
1054 _uartStatus.HwBusy = FALSE;
1055
1056 return -1;
1057 }
1058 #endif
1059
1060
MDrv_UART_SetPowerState(EN_POWER_MODE u16PowerState)1061 MS_U32 MDrv_UART_SetPowerState(EN_POWER_MODE u16PowerState)
1062 {
1063 static EN_POWER_MODE _prev_u16PowerState = E_POWER_MECHANICAL;
1064 MS_U32 u32Return = UTOPIA_STATUS_FAIL;
1065 MS_U8 i=0;
1066 if (u16PowerState == E_POWER_SUSPEND)
1067 {
1068 _prev_u16PowerState = u16PowerState;
1069 u32Return = UTOPIA_STATUS_SUCCESS;//SUSPEND_OK;
1070 //default uart re_init
1071 _bInitFlag = FALSE;
1072 //disable interrupt
1073 for(i=0;i<UART_MAX;i++)
1074 {
1075 if(_UartDisc[i].enable ==TRUE)
1076 {
1077 if(_UartDisc[i].rx_cb != NULL)
1078 mdrv_uart_set_rx_callback(_UartDisc[i].UartHandle,NULL);
1079 }
1080 }
1081 }
1082 else if (u16PowerState == E_POWER_RESUME)
1083 {
1084
1085 if (_prev_u16PowerState == E_POWER_SUSPEND)
1086 {
1087 MDrv_UART_Init(_eDefaultUart, _u32DefaultUartBaudrate);
1088 for(i=0;i<UART_MAX;i++)
1089 {
1090 if(_UartDisc[i].enable ==TRUE)
1091 {
1092 _UartDisc[i].UartHandle=mdrv_uart_open(_UartDisc[i].Uart_dev);
1093
1094 if(_UartDisc[i].UartHandle)
1095 {
1096 mdrv_uart_set_baudrate(_UartDisc[i].UartHandle, _UartDisc[i].baudrate); // set default baud rate
1097 mdrv_uart_set_rx_callback(_UartDisc[i].UartHandle,_UartDisc[i].rx_cb);
1098 }
1099 }
1100 }
1101 _prev_u16PowerState = u16PowerState;
1102 u32Return = UTOPIA_STATUS_SUCCESS;//RESUME_OK;
1103 }
1104 else
1105 {
1106 ULOGE(TAG_UART, "[%s,%5d]It is not suspended yet. We shouldn't resume\n",__FUNCTION__,__LINE__);
1107 u32Return = UTOPIA_STATUS_FAIL;//SUSPEND_FAILED;
1108 }
1109 }
1110 else
1111 {
1112 ULOGE(TAG_UART, "[%s,%5d]Do Nothing: %d\n",__FUNCTION__,__LINE__,u16PowerState);
1113 u32Return = UTOPIA_STATUS_FAIL;
1114 }
1115
1116 return u32Return;// for success
1117 }
1118
MDrv_UART_SetUARTSecurityBank(UART_DEVICE_TYPE uart_dev)1119 void MDrv_UART_SetUARTSecurityBank(UART_DEVICE_TYPE uart_dev)
1120 {
1121 return HAL_UART_SetUARTSecurityBank(uart_dev);
1122 }