Mouse code for the MacroRat
mbed-dev/targets/TARGET_NUVOTON/TARGET_NUC472/device/StdDriver/nuc472_ps2.c@46:b156ef445742, 2017-06-03 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sat Jun 03 00:22:44 2017 +0000
- Revision:
- 46:b156ef445742
- Parent:
- 18:6a4db94011d3
Final code for internal battlebot competition.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /****************************************************************************** |
sahilmgandhi | 18:6a4db94011d3 | 2 | * @file ps2.c |
sahilmgandhi | 18:6a4db94011d3 | 3 | * @version V1.00 |
sahilmgandhi | 18:6a4db94011d3 | 4 | * $Revision: 5 $ |
sahilmgandhi | 18:6a4db94011d3 | 5 | * $Date: 14/10/03 11:59a $ |
sahilmgandhi | 18:6a4db94011d3 | 6 | * @brief NUC472/NUC442 PS2 driver source file |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * @note |
sahilmgandhi | 18:6a4db94011d3 | 9 | * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved. |
sahilmgandhi | 18:6a4db94011d3 | 10 | *****************************************************************************/ |
sahilmgandhi | 18:6a4db94011d3 | 11 | |
sahilmgandhi | 18:6a4db94011d3 | 12 | #include <stdio.h> |
sahilmgandhi | 18:6a4db94011d3 | 13 | #include "NUC472_442.h" |
sahilmgandhi | 18:6a4db94011d3 | 14 | |
sahilmgandhi | 18:6a4db94011d3 | 15 | /*---------------------------------------------------------------------------------------------------------*/ |
sahilmgandhi | 18:6a4db94011d3 | 16 | /* Includes of local headers */ |
sahilmgandhi | 18:6a4db94011d3 | 17 | /*---------------------------------------------------------------------------------------------------------*/ |
sahilmgandhi | 18:6a4db94011d3 | 18 | //#include "ps2.h" |
sahilmgandhi | 18:6a4db94011d3 | 19 | |
sahilmgandhi | 18:6a4db94011d3 | 20 | |
sahilmgandhi | 18:6a4db94011d3 | 21 | /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver |
sahilmgandhi | 18:6a4db94011d3 | 22 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 23 | */ |
sahilmgandhi | 18:6a4db94011d3 | 24 | |
sahilmgandhi | 18:6a4db94011d3 | 25 | /** @addtogroup NUC472_442_PS2_Driver PS2 Driver |
sahilmgandhi | 18:6a4db94011d3 | 26 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 27 | */ |
sahilmgandhi | 18:6a4db94011d3 | 28 | |
sahilmgandhi | 18:6a4db94011d3 | 29 | |
sahilmgandhi | 18:6a4db94011d3 | 30 | /** @addtogroup NUC472_442_PS2_EXPORTED_FUNCTIONS PS2 Exported Functions |
sahilmgandhi | 18:6a4db94011d3 | 31 | @{ |
sahilmgandhi | 18:6a4db94011d3 | 32 | */ |
sahilmgandhi | 18:6a4db94011d3 | 33 | |
sahilmgandhi | 18:6a4db94011d3 | 34 | |
sahilmgandhi | 18:6a4db94011d3 | 35 | /** |
sahilmgandhi | 18:6a4db94011d3 | 36 | * @brief The function is used to enable PS2 specified interrupt. |
sahilmgandhi | 18:6a4db94011d3 | 37 | * |
sahilmgandhi | 18:6a4db94011d3 | 38 | * @param[in] u32Mask The specified interrupt of PS2 module: |
sahilmgandhi | 18:6a4db94011d3 | 39 | * - \ref PS2_CTL_TXIEN_Msk : PS2 Tx interrupt |
sahilmgandhi | 18:6a4db94011d3 | 40 | * - \ref PS2_CTL_RXIEN_Msk : PS2 Rx interrupt |
sahilmgandhi | 18:6a4db94011d3 | 41 | * |
sahilmgandhi | 18:6a4db94011d3 | 42 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 43 | */ |
sahilmgandhi | 18:6a4db94011d3 | 44 | void PS2_EnableInt(uint32_t u32Mask) |
sahilmgandhi | 18:6a4db94011d3 | 45 | { |
sahilmgandhi | 18:6a4db94011d3 | 46 | PS2->CTL |= u32Mask; |
sahilmgandhi | 18:6a4db94011d3 | 47 | } |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | /** |
sahilmgandhi | 18:6a4db94011d3 | 50 | * @brief The function is used to disable PS2 specified interrupt. |
sahilmgandhi | 18:6a4db94011d3 | 51 | * |
sahilmgandhi | 18:6a4db94011d3 | 52 | * @param[in] u32Mask The specified interrupt of PS2 module: |
sahilmgandhi | 18:6a4db94011d3 | 53 | * - \ref PS2_CTL_TXIEN_Msk : PS2 Tx interrupt |
sahilmgandhi | 18:6a4db94011d3 | 54 | * - \ref PS2_CTL_RXIEN_Msk : PS2 Rx interrupt |
sahilmgandhi | 18:6a4db94011d3 | 55 | * |
sahilmgandhi | 18:6a4db94011d3 | 56 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 57 | */ |
sahilmgandhi | 18:6a4db94011d3 | 58 | void PS2_DisableInt(uint32_t u32Mask) |
sahilmgandhi | 18:6a4db94011d3 | 59 | { |
sahilmgandhi | 18:6a4db94011d3 | 60 | PS2->CTL &= ~u32Mask; |
sahilmgandhi | 18:6a4db94011d3 | 61 | } |
sahilmgandhi | 18:6a4db94011d3 | 62 | |
sahilmgandhi | 18:6a4db94011d3 | 63 | /** |
sahilmgandhi | 18:6a4db94011d3 | 64 | * @brief This function use to enable PS2 function and set one byte per trnasfer. |
sahilmgandhi | 18:6a4db94011d3 | 65 | * |
sahilmgandhi | 18:6a4db94011d3 | 66 | * @param None |
sahilmgandhi | 18:6a4db94011d3 | 67 | * |
sahilmgandhi | 18:6a4db94011d3 | 68 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 69 | */ |
sahilmgandhi | 18:6a4db94011d3 | 70 | void PS2_Open(void) |
sahilmgandhi | 18:6a4db94011d3 | 71 | { |
sahilmgandhi | 18:6a4db94011d3 | 72 | /* Reset PS2 device */ |
sahilmgandhi | 18:6a4db94011d3 | 73 | SYS->IPRST1 |= SYS_IPRST1_PS2RST_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 74 | SYS->IPRST1 &= ~SYS_IPRST1_PS2RST_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 75 | |
sahilmgandhi | 18:6a4db94011d3 | 76 | /* Enable PS2 module */ |
sahilmgandhi | 18:6a4db94011d3 | 77 | PS2->CTL |= PS2_CTL_PS2EN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 78 | |
sahilmgandhi | 18:6a4db94011d3 | 79 | /* Set One byte per trnasfer */ |
sahilmgandhi | 18:6a4db94011d3 | 80 | PS2->CTL &= ~PS2_CTL_TXFDEPTH_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 81 | |
sahilmgandhi | 18:6a4db94011d3 | 82 | /* Clear Tx FIFO */ |
sahilmgandhi | 18:6a4db94011d3 | 83 | PS2->CTL |= PS2_CTL_CLRFIFO_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 84 | PS2->CTL &= (~PS2_CTL_CLRFIFO_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 85 | } |
sahilmgandhi | 18:6a4db94011d3 | 86 | |
sahilmgandhi | 18:6a4db94011d3 | 87 | /** |
sahilmgandhi | 18:6a4db94011d3 | 88 | * @brief This function use to disable PS2 function. |
sahilmgandhi | 18:6a4db94011d3 | 89 | * |
sahilmgandhi | 18:6a4db94011d3 | 90 | * @param None |
sahilmgandhi | 18:6a4db94011d3 | 91 | * |
sahilmgandhi | 18:6a4db94011d3 | 92 | * @return None |
sahilmgandhi | 18:6a4db94011d3 | 93 | */ |
sahilmgandhi | 18:6a4db94011d3 | 94 | void PS2_Close(void) |
sahilmgandhi | 18:6a4db94011d3 | 95 | { |
sahilmgandhi | 18:6a4db94011d3 | 96 | /* Enable PS2 module */ |
sahilmgandhi | 18:6a4db94011d3 | 97 | PS2->CTL &= ~PS2_CTL_PS2EN_Msk; |
sahilmgandhi | 18:6a4db94011d3 | 98 | } |
sahilmgandhi | 18:6a4db94011d3 | 99 | |
sahilmgandhi | 18:6a4db94011d3 | 100 | /** |
sahilmgandhi | 18:6a4db94011d3 | 101 | * @brief This function use to read PS2 Rx data. |
sahilmgandhi | 18:6a4db94011d3 | 102 | * |
sahilmgandhi | 18:6a4db94011d3 | 103 | * @param None |
sahilmgandhi | 18:6a4db94011d3 | 104 | * |
sahilmgandhi | 18:6a4db94011d3 | 105 | * @return Rx data |
sahilmgandhi | 18:6a4db94011d3 | 106 | */ |
sahilmgandhi | 18:6a4db94011d3 | 107 | uint8_t PS2_Read(void) |
sahilmgandhi | 18:6a4db94011d3 | 108 | { |
sahilmgandhi | 18:6a4db94011d3 | 109 | return (uint8_t)(PS2->RXDAT & PS2_RXDAT_DAT_Msk); |
sahilmgandhi | 18:6a4db94011d3 | 110 | } |
sahilmgandhi | 18:6a4db94011d3 | 111 | |
sahilmgandhi | 18:6a4db94011d3 | 112 | /** |
sahilmgandhi | 18:6a4db94011d3 | 113 | * @brief This function use to transmit PS2 data. |
sahilmgandhi | 18:6a4db94011d3 | 114 | * |
sahilmgandhi | 18:6a4db94011d3 | 115 | * @param[in] pu32Buf The buffer to send the data to PS2 transmission FIFO. |
sahilmgandhi | 18:6a4db94011d3 | 116 | * @param[in] u32ByteCount The byte number of data. |
sahilmgandhi | 18:6a4db94011d3 | 117 | * |
sahilmgandhi | 18:6a4db94011d3 | 118 | * @return FALSE: transmit data time-out |
sahilmgandhi | 18:6a4db94011d3 | 119 | * TRUE: transmit data successful |
sahilmgandhi | 18:6a4db94011d3 | 120 | */ |
sahilmgandhi | 18:6a4db94011d3 | 121 | int32_t PS2_Write(uint32_t *pu32Buf, uint32_t u32ByteCount) |
sahilmgandhi | 18:6a4db94011d3 | 122 | { |
sahilmgandhi | 18:6a4db94011d3 | 123 | uint32_t u32TxFIFO_Depth = 16; |
sahilmgandhi | 18:6a4db94011d3 | 124 | uint32_t u32delayno, txcnt, remainder; |
sahilmgandhi | 18:6a4db94011d3 | 125 | uint8_t i=0; |
sahilmgandhi | 18:6a4db94011d3 | 126 | |
sahilmgandhi | 18:6a4db94011d3 | 127 | txcnt = u32ByteCount / u32TxFIFO_Depth; |
sahilmgandhi | 18:6a4db94011d3 | 128 | |
sahilmgandhi | 18:6a4db94011d3 | 129 | remainder = u32ByteCount % u32TxFIFO_Depth; |
sahilmgandhi | 18:6a4db94011d3 | 130 | if(remainder) txcnt++; |
sahilmgandhi | 18:6a4db94011d3 | 131 | |
sahilmgandhi | 18:6a4db94011d3 | 132 | u32delayno = 0; |
sahilmgandhi | 18:6a4db94011d3 | 133 | while (!(PS2->STATUS & PS2_STATUS_TXEMPTY_Msk)) { |
sahilmgandhi | 18:6a4db94011d3 | 134 | u32delayno++; |
sahilmgandhi | 18:6a4db94011d3 | 135 | if (u32delayno >= 0xF0000000) |
sahilmgandhi | 18:6a4db94011d3 | 136 | return FALSE; // Time Out |
sahilmgandhi | 18:6a4db94011d3 | 137 | } |
sahilmgandhi | 18:6a4db94011d3 | 138 | |
sahilmgandhi | 18:6a4db94011d3 | 139 | if(u32ByteCount >= u32TxFIFO_Depth)//Tx fifo is 16 bytes |
sahilmgandhi | 18:6a4db94011d3 | 140 | PS2_SET_TX_BYTE_CNT(u32TxFIFO_Depth); |
sahilmgandhi | 18:6a4db94011d3 | 141 | |
sahilmgandhi | 18:6a4db94011d3 | 142 | do { |
sahilmgandhi | 18:6a4db94011d3 | 143 | u32delayno = 0; |
sahilmgandhi | 18:6a4db94011d3 | 144 | while (!(PS2->STATUS & PS2_STATUS_TXEMPTY_Msk)) { |
sahilmgandhi | 18:6a4db94011d3 | 145 | u32delayno++; |
sahilmgandhi | 18:6a4db94011d3 | 146 | if(u32delayno >= 0xF0000000) |
sahilmgandhi | 18:6a4db94011d3 | 147 | return FALSE; // Time Out |
sahilmgandhi | 18:6a4db94011d3 | 148 | } |
sahilmgandhi | 18:6a4db94011d3 | 149 | |
sahilmgandhi | 18:6a4db94011d3 | 150 | if((txcnt == 1) && (remainder != 0)) |
sahilmgandhi | 18:6a4db94011d3 | 151 | PS2_SET_TX_BYTE_CNT(u32ByteCount); |
sahilmgandhi | 18:6a4db94011d3 | 152 | |
sahilmgandhi | 18:6a4db94011d3 | 153 | PS2->TXDAT0 = pu32Buf[i]; |
sahilmgandhi | 18:6a4db94011d3 | 154 | PS2->TXDAT1 = pu32Buf[i+1]; |
sahilmgandhi | 18:6a4db94011d3 | 155 | PS2->TXDAT2 = pu32Buf[i+2]; |
sahilmgandhi | 18:6a4db94011d3 | 156 | PS2->TXDAT3 = pu32Buf[i+3]; |
sahilmgandhi | 18:6a4db94011d3 | 157 | |
sahilmgandhi | 18:6a4db94011d3 | 158 | i = i + 4; |
sahilmgandhi | 18:6a4db94011d3 | 159 | |
sahilmgandhi | 18:6a4db94011d3 | 160 | } while(--txcnt); |
sahilmgandhi | 18:6a4db94011d3 | 161 | |
sahilmgandhi | 18:6a4db94011d3 | 162 | u32delayno = 0; |
sahilmgandhi | 18:6a4db94011d3 | 163 | while(!(PS2->STATUS & PS2_STATUS_TXEMPTY_Msk)) { |
sahilmgandhi | 18:6a4db94011d3 | 164 | u32delayno++; |
sahilmgandhi | 18:6a4db94011d3 | 165 | if(u32delayno >= 0xF0000000) |
sahilmgandhi | 18:6a4db94011d3 | 166 | return FALSE; // Time Out |
sahilmgandhi | 18:6a4db94011d3 | 167 | } |
sahilmgandhi | 18:6a4db94011d3 | 168 | |
sahilmgandhi | 18:6a4db94011d3 | 169 | return TRUE; |
sahilmgandhi | 18:6a4db94011d3 | 170 | |
sahilmgandhi | 18:6a4db94011d3 | 171 | } |
sahilmgandhi | 18:6a4db94011d3 | 172 | |
sahilmgandhi | 18:6a4db94011d3 | 173 | |
sahilmgandhi | 18:6a4db94011d3 | 174 | /*@}*/ /* end of group NUC472_442_PS2_EXPORTED_FUNCTIONS */ |
sahilmgandhi | 18:6a4db94011d3 | 175 | |
sahilmgandhi | 18:6a4db94011d3 | 176 | /*@}*/ /* end of group NUC472_442_PS2_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 177 | |
sahilmgandhi | 18:6a4db94011d3 | 178 | /*@}*/ /* end of group NUC472_442_Device_Driver */ |
sahilmgandhi | 18:6a4db94011d3 | 179 | |
sahilmgandhi | 18:6a4db94011d3 | 180 | /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ |
sahilmgandhi | 18:6a4db94011d3 | 181 | |
sahilmgandhi | 18:6a4db94011d3 | 182 | |
sahilmgandhi | 18:6a4db94011d3 | 183 |