uart.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2018 bzt (bztsrc@github)
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. #include "gpio.h"
  26. #include "mbox.h"
  27. #include "sprintf.h"
  28. /* PL011 UART registers */
  29. #define UART0_DR ((volatile unsigned int*)(MMIO_BASE+0x00201000))
  30. #define UART0_FR ((volatile unsigned int*)(MMIO_BASE+0x00201018))
  31. #define UART0_IBRD ((volatile unsigned int*)(MMIO_BASE+0x00201024))
  32. #define UART0_FBRD ((volatile unsigned int*)(MMIO_BASE+0x00201028))
  33. #define UART0_LCRH ((volatile unsigned int*)(MMIO_BASE+0x0020102C))
  34. #define UART0_CR ((volatile unsigned int*)(MMIO_BASE+0x00201030))
  35. #define UART0_IMSC ((volatile unsigned int*)(MMIO_BASE+0x00201038))
  36. #define UART0_ICR ((volatile unsigned int*)(MMIO_BASE+0x00201044))
  37. // get address from linker
  38. extern volatile unsigned char _end;
  39. /**
  40. * Set baud rate and characteristics (115200 8N1) and map to GPIO
  41. */
  42. void uart_init()
  43. {
  44. register unsigned int r;
  45. /* initialize UART */
  46. *UART0_CR = 0; // turn off UART0
  47. /* set up clock for consistent divisor values */
  48. mbox[0] = 9*4;
  49. mbox[1] = MBOX_REQUEST;
  50. mbox[2] = MBOX_TAG_SETCLKRATE; // set clock rate
  51. mbox[3] = 12;
  52. mbox[4] = 8;
  53. mbox[5] = 2; // UART clock
  54. mbox[6] = 4000000; // 4Mhz
  55. mbox[7] = 0; // clear turbo
  56. mbox[8] = MBOX_TAG_LAST;
  57. mbox_call(MBOX_CH_PROP);
  58. /* map UART0 to GPIO pins */
  59. r=*GPFSEL1;
  60. r&=~((7<<12)|(7<<15)); // gpio14, gpio15
  61. r|=(4<<12)|(4<<15); // alt0
  62. *GPFSEL1 = r;
  63. *GPPUD = 0; // enable pins 14 and 15
  64. r=150; while(r--) { asm volatile("nop"); }
  65. *GPPUDCLK0 = (1<<14)|(1<<15);
  66. r=150; while(r--) { asm volatile("nop"); }
  67. *GPPUDCLK0 = 0; // flush GPIO setup
  68. *UART0_ICR = 0x7FF; // clear interrupts
  69. *UART0_IBRD = 2; // 115200 baud
  70. *UART0_FBRD = 0xB;
  71. *UART0_LCRH = 0x7<<4; // 8n1, enable FIFOs
  72. *UART0_CR = 0x301; // enable Tx, Rx, UART
  73. }
  74. /**
  75. * Send a character
  76. */
  77. void uart_send(unsigned int c) {
  78. /* wait until we can send */
  79. do{asm volatile("nop");}while(*UART0_FR&0x20);
  80. /* write the character to the buffer */
  81. *UART0_DR=c;
  82. }
  83. /**
  84. * Receive a character
  85. */
  86. char uart_getc() {
  87. char r;
  88. /* wait until something is in the buffer */
  89. do{asm volatile("nop");}while(*UART0_FR&0x10);
  90. /* read it and return */
  91. r=(char)(*UART0_DR);
  92. /* convert carrige return to newline */
  93. return r=='\r'?'\n':r;
  94. }
  95. /**
  96. * Display a string
  97. */
  98. void printf(char *fmt, ...) {
  99. __builtin_va_list args;
  100. __builtin_va_start(args, fmt);
  101. // we don't have memory allocation yet, so we
  102. // simply place our string after our code
  103. char *s = (char*)&_end;
  104. // use sprintf to format our string
  105. vsprintf(s,fmt,args);
  106. // print out as usual
  107. while(*s) {
  108. /* convert newline to carrige return + newline */
  109. if(*s=='\n')
  110. uart_send('\r');
  111. uart_send(*s++);
  112. }
  113. }