NUC472_NUC442_BSP V3.03.004
The Board Support Package for NUC472/NUC442
ps2.c
Go to the documentation of this file.
1/******************************************************************************
2 * @file ps2.c
3 * @version V1.00
4 * $Revision: 5 $
5 * $Date: 14/10/03 11:59a $
6 * @brief NUC472/NUC442 PS2 driver source file
7 *
8 * @note
9 * SPDX-License-Identifier: Apache-2.0
10 * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
11*****************************************************************************/
12
13#include <stdio.h>
14#include "NUC472_442.h"
15
16/*---------------------------------------------------------------------------------------------------------*/
17/* Includes of local headers */
18/*---------------------------------------------------------------------------------------------------------*/
19#include "ps2.h"
20
21
45void PS2_EnableInt(uint32_t u32Mask)
46{
47 PS2->CTL |= u32Mask;
48}
49
59void PS2_DisableInt(uint32_t u32Mask)
60{
61 PS2->CTL &= ~u32Mask;
62}
63
71void PS2_Open(void)
72{
73 /* Reset PS2 device */
74 SYS->IPRST1 |= SYS_IPRST1_PS2RST_Msk;
75 SYS->IPRST1 &= ~SYS_IPRST1_PS2RST_Msk;
76
77 /* Enable PS2 module */
78 PS2->CTL |= PS2_CTL_PS2EN_Msk;
79
80 /* Set One byte per trnasfer */
81 PS2->CTL &= ~PS2_CTL_TXFDEPTH_Msk;
82
83 /* Clear Tx FIFO */
85 PS2->CTL &= (~PS2_CTL_CLRFIFO_Msk);
86}
87
95void PS2_Close(void)
96{
97 /* Enable PS2 module */
98 PS2->CTL &= ~PS2_CTL_PS2EN_Msk;
99}
100
108uint8_t PS2_Read(void)
109{
110 return (uint8_t)(PS2->RXDAT & PS2_RXDAT_DAT_Msk);
111}
112
122int32_t PS2_Write(uint32_t *pu32Buf, uint32_t u32ByteCount)
123{
124 uint32_t u32TxFIFO_Depth = 16;
125 uint32_t u32delayno, txcnt, remainder;
126 uint8_t i=0;
127 uint32_t u32TimeOutCount = SystemCoreClock * 3; // 3 second timeout
128
129 txcnt = u32ByteCount / u32TxFIFO_Depth;
130
131 remainder = u32ByteCount % u32TxFIFO_Depth;
132 if(remainder) txcnt++;
133
134 u32delayno = 0;
135 while (!(PS2->STATUS & PS2_STATUS_TXEMPTY_Msk))
136 {
137 u32delayno++;
138 if (u32delayno >= u32TimeOutCount)
139 return FALSE; // Time Out
140 }
141
142 if(u32ByteCount >= u32TxFIFO_Depth)//Tx fifo is 16 bytes
143 PS2_SET_TX_BYTE_CNT(u32TxFIFO_Depth);
144
145 do
146 {
147 u32delayno = 0;
148 while (!(PS2->STATUS & PS2_STATUS_TXEMPTY_Msk))
149 {
150 u32delayno++;
151 if(u32delayno >= u32TimeOutCount)
152 return FALSE; // Time Out
153 }
154
155 if((txcnt == 1) && (remainder != 0))
156 PS2_SET_TX_BYTE_CNT(u32ByteCount);
157
158 PS2->TXDAT0 = pu32Buf[i];
159 PS2->TXDAT1 = pu32Buf[i+1];
160 PS2->TXDAT2 = pu32Buf[i+2];
161 PS2->TXDAT3 = pu32Buf[i+3];
162
163 i = i + 4;
164
165 }
166 while(--txcnt);
167
168 u32delayno = 0;
169 while(!(PS2->STATUS & PS2_STATUS_TXEMPTY_Msk))
170 {
171 u32delayno++;
172 if(u32delayno >= u32TimeOutCount)
173 return FALSE; // Time Out
174 }
175
176 return TRUE;
177
178}
179
180 /* end of group NUC472_442_PS2_EXPORTED_FUNCTIONS */
182 /* end of group NUC472_442_PS2_Driver */
184 /* end of group NUC472_442_Device_Driver */
186
187/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
188
189
190
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
#define PS2_CTL_PS2EN_Msk
Definition: NUC472_442.h:18958
#define PS2_RXDAT_DAT_Msk
Definition: NUC472_442.h:18997
#define PS2_STATUS_TXEMPTY_Msk
Definition: NUC472_442.h:19021
#define PS2_CTL_CLRFIFO_Msk
Definition: NUC472_442.h:18973
#define SYS_IPRST1_PS2RST_Msk
Definition: NUC472_442.h:24319
#define PS2
Definition: NUC472_442.h:28872
#define SYS
Definition: NUC472_442.h:28797
int32_t PS2_Write(uint32_t *pu32Buf, uint32_t u32ByteCount)
This function use to transmit PS2 data.
Definition: ps2.c:122
void PS2_Close(void)
This function use to disable PS2 function.
Definition: ps2.c:95
#define PS2_SET_TX_BYTE_CNT(u32Count)
This function use to set TX FIFO length.
Definition: ps2.h:48
void PS2_DisableInt(uint32_t u32Mask)
The function is used to disable PS2 specified interrupt.
Definition: ps2.c:59
void PS2_EnableInt(uint32_t u32Mask)
The function is used to enable PS2 specified interrupt.
Definition: ps2.c:45
void PS2_Open(void)
This function use to enable PS2 function and set one byte per trnasfer.
Definition: ps2.c:71
uint8_t PS2_Read(void)
This function use to read PS2 Rx data.
Definition: ps2.c:108
#define TRUE
Boolean true, define to use in API parameters or return value.
Definition: NUC472_442.h:29021
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: NUC472_442.h:29022
NUC472/NUC442 PS2 Driver Header File.
uint32_t SystemCoreClock