console.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Simple console input/output, over serial port #0
  3. Partially copied from Jim Lynch's tutorial
  4. */
  5. #include "console.h"
  6. #define PINSEL0 *(volatile unsigned int *)0xE002C000
  7. #define U0THR *(volatile unsigned int *)0xE000C000
  8. #define U0RBR *(volatile unsigned int *)0xE000C000
  9. #define U0DLL *(volatile unsigned int *)0xE000C000
  10. #define U0DLM *(volatile unsigned int *)0xE000C004
  11. #define U0FCR *(volatile unsigned int *)0xE000C008
  12. #define U0LCR *(volatile unsigned int *)0xE000C00C
  13. #define U0LSR *(volatile unsigned int *)0xE000C014
  14. /* Initialize Serial Interface */
  15. void ConsoleInit(int iDivider)
  16. {
  17. PINSEL0 = (PINSEL0 & ~0x0000000F) | 0x00000005; /* Enable RxD0 and TxD0 */
  18. U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
  19. U0DLL = iDivider & 0xFF; /* set divider / baud rate */
  20. U0DLM = iDivider >> 8;
  21. U0LCR = 0x03; /* DLAB = 0 */
  22. // enable FIFO
  23. U0FCR = 1;
  24. }
  25. /* Write character to Serial Port */
  26. int putchar(int ch)
  27. {
  28. if (ch == '\n') {
  29. while (!(U0LSR & 0x20));
  30. U0THR = '\r';
  31. }
  32. while (!(U0LSR & 0x20));
  33. U0THR = ch;
  34. return ch;
  35. }
  36. int getchar(void)
  37. { /* Read character from Serial Port */
  38. while (!(U0LSR & 0x01));
  39. return (U0RBR);
  40. }
  41. int puts(char *s)
  42. {
  43. while (*s) {
  44. putchar(*s++);
  45. }
  46. //putchar('\n');
  47. return 1;
  48. }