| 1 | /************************************************************************* |
|---|
| 2 | ** |
|---|
| 3 | ** Broadcom Corp. Confidential |
|---|
| 4 | ** Copyright 1999, 2000 Broadcom Corp. All Rights Reserved. |
|---|
| 5 | ** |
|---|
| 6 | ** THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | ** AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | ** EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | ** |
|---|
| 10 | ** File: serial.h |
|---|
| 11 | ** Description: polled uart io |
|---|
| 12 | ** Created: Jeff Fisher |
|---|
| 13 | ** REVISION: |
|---|
| 14 | ** |
|---|
| 15 | ****************************************************************************/ |
|---|
| 16 | |
|---|
| 17 | #include "serial.h" |
|---|
| 18 | #include "burt.h" |
|---|
| 19 | #ifdef NEXUS_UCOS_II |
|---|
| 20 | extern BREG_Handle GetREG(); |
|---|
| 21 | extern BCHP_Handle GetCHP(); |
|---|
| 22 | extern BINT_Handle GetINT(); |
|---|
| 23 | #else |
|---|
| 24 | #include "gist.h" |
|---|
| 25 | #endif |
|---|
| 26 | #include "bkni_print.h" |
|---|
| 27 | |
|---|
| 28 | //#define POLLED_SERIAL_IO |
|---|
| 29 | |
|---|
| 30 | urt_t urt; |
|---|
| 31 | BURT_Handle urt_handle; |
|---|
| 32 | BURT_ChannelHandle urt_channel; |
|---|
| 33 | |
|---|
| 34 | /* use IRQ0 ua interrupt for reading RX */ |
|---|
| 35 | static void uart_callback_func(void *context, int param) |
|---|
| 36 | { |
|---|
| 37 | #ifndef POLLED_SERIAL_IO |
|---|
| 38 | |
|---|
| 39 | unsigned long sizeRemaining, maxRead; |
|---|
| 40 | uint32_t bytesRead; |
|---|
| 41 | BERR_Code errCode; |
|---|
| 42 | BURT_RxError error; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | if (BURT_IsRxDataAvailable_Isr(urt_channel) == false) { |
|---|
| 46 | return; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | bos_acquire_mutex(&urt.lock, -1); |
|---|
| 50 | if (urt.rxWrite >= urt.rxRead) { |
|---|
| 51 | sizeRemaining = urt.rxBufferSize - urt.rxWrite + urt.rxRead -1; |
|---|
| 52 | } else { |
|---|
| 53 | sizeRemaining = urt.rxRead - urt.rxWrite; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | maxRead = (urt.rxWrite<= urt.rxRead)?sizeRemaining:(urt.rxBufferSize - urt.rxWrite); |
|---|
| 57 | |
|---|
| 58 | if (maxRead) |
|---|
| 59 | { |
|---|
| 60 | errCode = BURT_Read_Isr(urt_channel, urt.pRxFifo + urt.rxWrite, maxRead, &bytesRead, &error); |
|---|
| 61 | if (error != BURT_RxError_eNoError) { |
|---|
| 62 | switch (error) { |
|---|
| 63 | case BURT_RxError_eReceiverOverRun: |
|---|
| 64 | BKNI_Printf("=== receiver overrun\n"); |
|---|
| 65 | break; |
|---|
| 66 | case BURT_RxError_eReceiverFrameError: |
|---|
| 67 | BKNI_Printf("=== receiver frame error\n"); |
|---|
| 68 | break; |
|---|
| 69 | case BURT_RxError_eReceiverParityError: |
|---|
| 70 | BKNI_Printf("=== receiver parity error\n"); |
|---|
| 71 | break; |
|---|
| 72 | default: |
|---|
| 73 | break; |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | sizeRemaining -= bytesRead; |
|---|
| 78 | urt.rxWrite += bytesRead; |
|---|
| 79 | if (urt.rxWrite >= urt.rxBufferSize) { |
|---|
| 80 | urt.rxWrite = 0; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | bos_release_mutex(&urt.lock); |
|---|
| 84 | |
|---|
| 85 | /* re-enable interrupt. */ |
|---|
| 86 | BURT_EnableRxInt(urt_channel, true); |
|---|
| 87 | BURT_EnableTxInt(urt_channel, true); |
|---|
| 88 | #endif |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /***********************************************************************/ |
|---|
| 92 | /* serial_start(UartChannel *uart)*/ |
|---|
| 93 | /* start or stop interrupt based UART */ |
|---|
| 94 | /***********************************************************************/ |
|---|
| 95 | void serial_start(volatile UartChannel *uart, bool start) |
|---|
| 96 | { |
|---|
| 97 | uart->sdw_lsr = 0; |
|---|
| 98 | #ifndef POLLED_SERIAL_IO |
|---|
| 99 | |
|---|
| 100 | BURT_EnableRxInt(urt_channel, start ? true : false); |
|---|
| 101 | BURT_EnableTxInt(urt_channel, start ? true : false); |
|---|
| 102 | |
|---|
| 103 | /* don't print out anything when using this function */ |
|---|
| 104 | if (!start) { |
|---|
| 105 | uint32_t bytesRead; |
|---|
| 106 | BERR_Code errCode; |
|---|
| 107 | BURT_RxError error; |
|---|
| 108 | while (BURT_IsRxDataAvailable(urt_channel)) { |
|---|
| 109 | errCode = BURT_Read(urt_channel, urt.pRxFifo + urt.rxWrite, 1, &bytesRead, &error); |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | bos_acquire_mutex(&urt.lock, -1); |
|---|
| 113 | urt.rxRead = urt.rxWrite = 0; |
|---|
| 114 | bos_release_mutex(&urt.lock); |
|---|
| 115 | #endif |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /***********************************************************************/ |
|---|
| 119 | /* serial_init(UartChannel *uart,unsigned int baud)*/ |
|---|
| 120 | /***********************************************************************/ |
|---|
| 121 | void serial_init(volatile UartChannel *uart, unsigned int baud) |
|---|
| 122 | { |
|---|
| 123 | #if (BCHP_CHIP==7550) || (BCHP_CHIP==7552) |
|---|
| 124 | #ifdef POLLED_SERIAL_IO |
|---|
| 125 | uart->sdw_lsr = 0; |
|---|
| 126 | #else |
|---|
| 127 | BURT_ChannelSettings channelSettings; |
|---|
| 128 | uint32_t tval; |
|---|
| 129 | /* do one read to prevent annoying interrupt at startup |
|---|
| 130 | when serial dongle is disconnected */ |
|---|
| 131 | tval = uart->sdw_rbr_thr_dll & 0xFF; |
|---|
| 132 | |
|---|
| 133 | urt.rxBufferSize = 256; |
|---|
| 134 | urt.rxRead = urt.rxWrite = 0; |
|---|
| 135 | urt.pRxFifo = BKNI_Malloc(urt.rxBufferSize); |
|---|
| 136 | bos_create_mutex(&urt.lock); |
|---|
| 137 | |
|---|
| 138 | BURT_Open(&urt_handle, GetCHP(), GetREG(), GetINT(), NULL); |
|---|
| 139 | BURT_GetChannelDefaultSettings(urt_handle, 0, &channelSettings); |
|---|
| 140 | channelSettings.intMode = false; |
|---|
| 141 | channelSettings.rxIntMode = true; |
|---|
| 142 | channelSettings.baud = baud; |
|---|
| 143 | channelSettings.bits = BURT_DataBits_eDataBits8; |
|---|
| 144 | channelSettings.parity = BURT_Parity_eNone; |
|---|
| 145 | |
|---|
| 146 | BURT_OpenChannel(urt_handle, &urt_channel, 0, &channelSettings); |
|---|
| 147 | BURT_RegisterCallback(urt_channel, uart_callback_func); |
|---|
| 148 | #endif |
|---|
| 149 | #else |
|---|
| 150 | /*---------------------------------------------------------------------*/ |
|---|
| 151 | /* Dissable channel's receiver and transmitter. */ |
|---|
| 152 | /*---------------------------------------------------------------------*/ |
|---|
| 153 | uart->control = 0; |
|---|
| 154 | // uart->control &= ~(TXEN|RXEN); |
|---|
| 155 | |
|---|
| 156 | /*---------------------------------------------------------------------*/ |
|---|
| 157 | /* disable interrupts on this channel . */ |
|---|
| 158 | /*---------------------------------------------------------------------*/ |
|---|
| 159 | uart->rxstat = 0; |
|---|
| 160 | uart->txstat = 0; |
|---|
| 161 | |
|---|
| 162 | /*-----------------------------------------------------------------*/ |
|---|
| 163 | /* set character size and parity control bit (8,none only) */ |
|---|
| 164 | /*-----------------------------------------------------------------*/ |
|---|
| 165 | uart->control = BITM8; |
|---|
| 166 | baud = ((XTALFREQ / baud) /16); |
|---|
| 167 | uart->baudh = (unsigned char)((baud >> 8) & 0xFF); |
|---|
| 168 | uart->baudl = (unsigned char)(baud & 0xFF); |
|---|
| 169 | |
|---|
| 170 | /*---------------------------------------------------------------------*/ |
|---|
| 171 | /* Finally, re-enable the transmitter and receiver. */ |
|---|
| 172 | /*---------------------------------------------------------------------*/ |
|---|
| 173 | uart->control |= (TXEN|RXEN); |
|---|
| 174 | #endif |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | /***********************************************************************/ |
|---|
| 178 | /* serial_getc(UartChannel *uart, int block)*/ |
|---|
| 179 | /***********************************************************************/ |
|---|
| 180 | #define RXRDA 0x01 |
|---|
| 181 | #define RXOVFERR 0x02 |
|---|
| 182 | #define RXPARERR 0x04 |
|---|
| 183 | #define RXFRAMERR 0x08 |
|---|
| 184 | #define RXBIERR 0x10 /* BI error */ |
|---|
| 185 | #define RXFIFOERR 0x80 /* Rx FIFO error */ |
|---|
| 186 | int serial_getc(volatile UartChannel *uart, bool block) |
|---|
| 187 | { |
|---|
| 188 | #if (BCHP_CHIP==7550) || (BCHP_CHIP==7552) |
|---|
| 189 | #ifdef POLLED_SERIAL_IO |
|---|
| 190 | int blen = 1; |
|---|
| 191 | unsigned int status; |
|---|
| 192 | char tval; |
|---|
| 193 | |
|---|
| 194 | while (blen > 0) |
|---|
| 195 | { |
|---|
| 196 | status = uart->sdw_lsr; |
|---|
| 197 | if(status & (RXOVFERR | RXPARERR | RXFRAMERR | RXBIERR | RXFIFOERR)) |
|---|
| 198 | { |
|---|
| 199 | /* Just read the bad character to clear the bit. */ |
|---|
| 200 | tval = uart->sdw_rbr_thr_dll & 0xFF; |
|---|
| 201 | } |
|---|
| 202 | else if(status & RXRDA) |
|---|
| 203 | { |
|---|
| 204 | tval = uart->sdw_rbr_thr_dll & 0xFF; |
|---|
| 205 | blen--; |
|---|
| 206 | } |
|---|
| 207 | else { |
|---|
| 208 | /* block if required */ |
|---|
| 209 | if (!block) { |
|---|
| 210 | return 0; |
|---|
| 211 | } |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | return tval; |
|---|
| 215 | #else |
|---|
| 216 | char tval; |
|---|
| 217 | while (1) { |
|---|
| 218 | if (urt.rxWrite == urt.rxRead) { // no data yet. |
|---|
| 219 | if (!block) { |
|---|
| 220 | #ifdef CONFIG_GP |
|---|
| 221 | return -1; |
|---|
| 222 | #else |
|---|
| 223 | return 0; |
|---|
| 224 | #endif |
|---|
| 225 | } |
|---|
| 226 | else { |
|---|
| 227 | continue; |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | else { |
|---|
| 231 | tval = urt.pRxFifo[urt.rxRead]; |
|---|
| 232 | bos_acquire_mutex(&urt.lock, -1); |
|---|
| 233 | urt.rxRead++; |
|---|
| 234 | if (urt.rxRead >= urt.rxBufferSize) { |
|---|
| 235 | urt.rxRead = 0; |
|---|
| 236 | } |
|---|
| 237 | bos_release_mutex(&urt.lock); |
|---|
| 238 | return tval; |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | #endif |
|---|
| 242 | #else |
|---|
| 243 | if (!block) |
|---|
| 244 | { |
|---|
| 245 | if (!(uart->rxstat & RXDATARDY)) { |
|---|
| 246 | return 0; |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | while (!(uart->rxstat & RXDATARDY)) ; |
|---|
| 250 | |
|---|
| 251 | return (unsigned char)uart->rxdata; |
|---|
| 252 | #endif |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | /***********************************************************************/ |
|---|
| 256 | /* serial_putc(UartChannel *uart, unsigned int c)*/ |
|---|
| 257 | /***********************************************************************/ |
|---|
| 258 | static inline void serial_do_putc(volatile UartChannel *uart, int c) |
|---|
| 259 | { |
|---|
| 260 | #if (BCHP_CHIP==7550) || (BCHP_CHIP==7552) |
|---|
| 261 | if(uart->sdw_scr == 0){ |
|---|
| 262 | while(0 == (uart->sdw_lsr & THRE)) |
|---|
| 263 | { |
|---|
| 264 | #ifndef POLLED_SERIAL_IO |
|---|
| 265 | if (!bos_in_interrupt()) |
|---|
| 266 | BKNI_Sleep(1); |
|---|
| 267 | #endif |
|---|
| 268 | } |
|---|
| 269 | uart->sdw_scr = 32; |
|---|
| 270 | } |
|---|
| 271 | uart->sdw_scr--; |
|---|
| 272 | uart->sdw_rbr_thr_dll = (unsigned char)c; |
|---|
| 273 | #else |
|---|
| 274 | while (!(uart->txstat & TXDREGEMT)) |
|---|
| 275 | ; |
|---|
| 276 | |
|---|
| 277 | uart->txdata = (unsigned char)c; |
|---|
| 278 | #endif |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | /***********************************************************************/ |
|---|
| 282 | /* serial_putc(UartChannel *uart, unsigned int c)*/ |
|---|
| 283 | /***********************************************************************/ |
|---|
| 284 | void serial_putc(volatile UartChannel *uart, int c) |
|---|
| 285 | { |
|---|
| 286 | serial_do_putc(uart,c); |
|---|
| 287 | } |
|---|