| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#include <stdarg.h> |
| 23 |
#include <comdef.h> |
| 24 |
#include <uart.h> |
| 25 |
#include <sys.h> |
| 26 |
#include <ezGPIO_fullMux_ctrl_macro.h> |
| 27 |
#include <clkgen_ctrl_macro.h> |
| 28 |
#include <rstgen_ctrl_macro.h> |
| 29 |
#include <stdarg.h> |
| 30 |
#include <stdio.h> |
| 31 |
|
| 32 |
|
| 33 |
#define UART_CLK (100000000UL)///(64000000UL)/// |
| 34 |
|
| 35 |
|
| 36 |
#define UART_BUADRATE_32MCLK_9600 9600 |
| 37 |
#define UART_BUADRATE_32MCLK_57600 57600 |
| 38 |
#define UART_BUADRATE_32MCLK_115200 115200 |
| 39 |
#define UART_BUADRATE_32MCLK_125000 125000 |
| 40 |
#define UART_BUADRATE_32MCLK_222222 222222 ///230400 |
| 41 |
#define UART_BUADRATE_32MCLK_333333 333333 ///380400 |
| 42 |
#define UART_BUADRATE_32MCLK_400000 400000 ///460800 |
| 43 |
#define UART_BUADRATE_32MCLK_500000 500000 |
| 44 |
#define UART_BUADRATE_32MCLK_666666 666666 ///921600 |
| 45 |
#define UART_BUADRATE_32MCLK_1M 1000000 |
| 46 |
#define UART_BUADRATE_32MCLK_2M 2000000 |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#define UART_BUADRATE_64MCLK_38400 38400 |
| 51 |
#define UART_BUADRATE_64MCLK_57600 57600 |
| 52 |
#define UART_BUADRATE_64MCLK_115200 115200 |
| 53 |
#define UART_BUADRATE_64MCLK_230400 230400 |
| 54 |
#define UART_BUADRATE_64MCLK_380400 380400 |
| 55 |
#define UART_BUADRATE_64MCLK_444444 444444 ///460800 |
| 56 |
#define UART_BUADRATE_64MCLK_500000 500000 |
| 57 |
#define UART_BUADRATE_64MCLK_571428 571428 |
| 58 |
#define UART_BUADRATE_64MCLK_666666 666666 ///921600 |
| 59 |
|
| 60 |
#define UART_BUADRATE_64MCLK_800000 800000 ///921600 |
| 61 |
#define UART_BUADRATE_64MCLK_1M 1000000 |
| 62 |
#define UART_BUADRATE_64MCLK_2M 2000000 |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#define UART_BUADRATE_64MCLK_4M 4000000 ///shiboqi |
| 67 |
|
| 68 |
volatile int uart_txfifo_available; |
| 69 |
|
| 70 |
#define UART_PORT 3 |
| 71 |
|
| 72 |
static const u32 uart_base[4] = { |
| 73 |
UART0_BASE_ADDR, |
| 74 |
UART1_HS_BASE_ADDR, |
| 75 |
UART2_BASE_ADDR, |
| 76 |
UART3_BASE_ADDR, |
| 77 |
|
| 78 |
}; |
| 79 |
|
| 80 |
static unsigned int serial_in(int id, int offset) |
| 81 |
{ |
| 82 |
offset <<= 2; |
| 83 |
return readw(uart_base[id] + offset); |
| 84 |
} |
| 85 |
|
| 86 |
static void serial_out(int id, int offset, int value) |
| 87 |
{ |
| 88 |
offset <<= 2; |
| 89 |
writew(value, uart_base[id] + offset); |
| 90 |
} |
| 91 |
static void wait_for_xmitr() |
| 92 |
{ |
| 93 |
unsigned int status; |
| 94 |
|
| 95 |
do { |
| 96 |
status = serial_in(UART_PORT, UART_USR); |
| 97 |
} while (!(status & UART_USR_Tx_FIFO_NFUL)); |
| 98 |
} |
| 99 |
|
| 100 |
void uart_handler() { |
| 101 |
uart_txfifo_available = 1; |
| 102 |
u32 value = readl(uart_base[UART_PORT] + UART_IER_REG); |
| 103 |
value &= (~IER_TBE); |
| 104 |
writel(value, uart_base[UART_PORT] + UART_IER_REG); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
void uart_init(int id) |
| 109 |
{ |
| 110 |
unsigned int divisor; |
| 111 |
unsigned char lcr_cache; |
| 112 |
unsigned char val; |
| 113 |
|
| 114 |
|
| 115 |
_ENABLE_CLOCK_clk_uart3_apb_; |
| 116 |
_ENABLE_CLOCK_clk_uart3_core_; |
| 117 |
|
| 118 |
_ASSERT_RESET_rstgen_rstn_uart3_apb_; |
| 119 |
_ASSERT_RESET_rstgen_rstn_uart3_core_; |
| 120 |
_CLEAR_RESET_rstgen_rstn_uart3_core_; |
| 121 |
_CLEAR_RESET_rstgen_rstn_uart3_apb_; |
| 122 |
SET_GPIO_14_dout_uart3_pad_sout; |
| 123 |
|
| 124 |
SET_GPIO_14_doen_LOW; |
| 125 |
SET_GPIO_13_doen_HIGH; |
| 126 |
SET_GPIO_uart3_pad_sin(13); |
| 127 |
|
| 128 |
|
| 129 |
divisor = (UART_CLK / 9600) >> 4; |
| 130 |
|
| 131 |
lcr_cache = readl(uart_base[id] + UART_LCR_REG); |
| 132 |
writel((LCR_DLAB | lcr_cache), uart_base[id] + UART_LCR_REG); |
| 133 |
|
| 134 |
writel((unsigned char)(divisor & 0xff), uart_base[id] + UART_BRDL_REG); |
| 135 |
writel((unsigned char)((divisor >> 8) & 0xff), uart_base[id] + UART_BRDH_REG); |
| 136 |
|
| 137 |
|
| 138 |
writel(lcr_cache, uart_base[id] + UART_LCR_REG); |
| 139 |
|
| 140 |
|
| 141 |
writel((LCR_CS8 | LCR_1_STB | LCR_PDIS), uart_base[id] + UART_LCR_REG); |
| 142 |
writel(0, uart_base[id] + UART_MDC_REG); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
writel((FCR_FIFO | FCR_MODE1 | FCR_FIFO_8 | FCR_RCVRCLR | FCR_XMITCLR), uart_base[id] + UART_FCR_REG); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
int _putc(char c) { |
| 154 |
int timecnt = 0; |
| 155 |
int ret = 0; |
| 156 |
|
| 157 |
do |
| 158 |
{ |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
}while((readl((uart_base[UART_PORT] + UART_LSR_REG)) & LSR_THRE) == 0); |
| 163 |
|
| 164 |
writel(c, uart_base[UART_PORT] + UART_THR_REG); |
| 165 |
|
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
void rlSendString(char *s) |
| 171 |
{ |
| 172 |
while (*s){ |
| 173 |
_putc(*s++); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static void sys_out_char(const char c) |
| 178 |
{ |
| 179 |
_putc( c); |
| 180 |
} |
| 181 |
int CtrlBreak( void ) |
| 182 |
{ |
| 183 |
int retflag; |
| 184 |
|
| 185 |
do{ |
| 186 |
retflag = serial_getc( ); |
| 187 |
if( retflag == 0x03 ){ |
| 188 |
break; |
| 189 |
} |
| 190 |
}while( retflag ); |
| 191 |
return retflag; |
| 192 |
} |
| 193 |
int serial_getc() |
| 194 |
{ |
| 195 |
unsigned int status; |
| 196 |
|
| 197 |
status = serial_in(UART_PORT, REG_LSR); |
| 198 |
while (!(status & (1 << 0))) |
| 199 |
status = serial_in(UART_PORT, REG_LSR); |
| 200 |
|
| 201 |
status = serial_in(UART_PORT, REG_RDR); |
| 202 |
return status; |
| 203 |
} |
| 204 |
|
| 205 |
void serial_gets(char *pstr) |
| 206 |
{ |
| 207 |
unsigned char c; |
| 208 |
unsigned char *pstrorg; |
| 209 |
|
| 210 |
pstrorg = (unsigned char *) pstr; |
| 211 |
again: |
| 212 |
|
| 213 |
while ((c = serial_getc()) != '\r') |
| 214 |
{ |
| 215 |
if (c == '\b'){ |
| 216 |
if ((int) pstrorg < (int) pstr){ |
| 217 |
rlSendString("\b \b"); |
| 218 |
pstr--; |
| 219 |
} |
| 220 |
}else{ |
| 221 |
*pstr++ = c; |
| 222 |
sys_out_char(c); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
*pstr = '\0'; |
| 227 |
|
| 228 |
rlSendString("\r\n"); |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
int _inbyte(unsigned short timeout) |
| 233 |
{ |
| 234 |
unsigned int c; |
| 235 |
unsigned int delay = timeout*20; |
| 236 |
|
| 237 |
while (!(serial_in(UART_PORT, REG_LSR) & LSR_RXRDY)) { |
| 238 |
udelay(50); |
| 239 |
if (timeout && (--delay == 0)) { |
| 240 |
return -1; |
| 241 |
} |
| 242 |
} |
| 243 |
c = serial_in(UART_PORT, REG_RDR); |
| 244 |
return c; |
| 245 |
} |
| 246 |
|
| 247 |
void _outbyte(int c) |
| 248 |
{ |
| 249 |
while(!(serial_in(UART_PORT, REG_LSR) & LSR_TEMT)) |
| 250 |
; |
| 251 |
|
| 252 |
serial_out(UART_PORT, REG_THR, c); |
| 253 |
} |
| 254 |
|
| 255 |
void _puts(const char * s) { |
| 256 |
while (*s != '\0'){ |
| 257 |
_putc(*s++); |
| 258 |
} |
| 259 |
} |
| 260 |
int __serial_tstc() |
| 261 |
{ |
| 262 |
return ((serial_in(UART_PORT, REG_LSR)) & (1 << 0)); |
| 263 |
} |
| 264 |
int serial_tstc() |
| 265 |
{ |
| 266 |
return __serial_tstc(); |
| 267 |
} |
| 268 |
void print_ubyte_hex(unsigned char bval) |
| 269 |
{ |
| 270 |
static const char digits[16] = "0123456789ABCDEF"; |
| 271 |
char tmp[2]; |
| 272 |
int dig=0; |
| 273 |
|
| 274 |
dig = ((bval&0xf0)>>4); |
| 275 |
tmp[0] = digits[dig]; |
| 276 |
dig = (bval&0x0f); |
| 277 |
tmp[1] = digits[dig]; |
| 278 |
_putc(tmp[0]); |
| 279 |
_putc(tmp[1]); |
| 280 |
} |
| 281 |
int serial_nowait_getc() |
| 282 |
{ |
| 283 |
unsigned int status; |
| 284 |
|
| 285 |
status = serial_in(UART_PORT, REG_LSR); |
| 286 |
if (!(status & (1 << 0))) { |
| 287 |
status = 0; |
| 288 |
goto out; |
| 289 |
} |
| 290 |
status = serial_in(UART_PORT, REG_RDR); |
| 291 |
out: |
| 292 |
return status; |
| 293 |
} |
| 294 |
|
| 295 |
void sys_itoa(int value, char* string, unsigned char radix) |
| 296 |
{ |
| 297 |
int i; |
| 298 |
int count = 0; |
| 299 |
unsigned long temp; |
| 300 |
unsigned long res; |
| 301 |
char ch[512]; |
| 302 |
|
| 303 |
if (radix == 10 && value < 0){ |
| 304 |
ch[0] = '-';count++;temp = 0 - value; |
| 305 |
}else{ |
| 306 |
temp = value; |
| 307 |
} |
| 308 |
|
| 309 |
while (1) |
| 310 |
{ |
| 311 |
res = temp % radix; |
| 312 |
temp = temp / radix; |
| 313 |
if (res < 10) |
| 314 |
ch[count++] = res + '0'; |
| 315 |
else |
| 316 |
ch[count++] = res - 10 + 'a'; |
| 317 |
if (temp == 0) |
| 318 |
break; |
| 319 |
} |
| 320 |
ch[count] = '\0'; |
| 321 |
string[0] = '\0'; |
| 322 |
for (i = 0; i < count; i++) |
| 323 |
string[count - i - 1] = ch[i]; |
| 324 |
|
| 325 |
string[count] = '\0'; |
| 326 |
} |
| 327 |
static void sys_console_out(const char* buf, unsigned int nbytes) |
| 328 |
{ |
| 329 |
while (nbytes--) |
| 330 |
sys_out_char(*buf++); |
| 331 |
} |