uart.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /* Auxilary mini UART registers */
  27. #define AUX_ENABLE ((volatile unsigned int*)(MMIO_BASE+0x00215004))
  28. #define AUX_MU_IO ((volatile unsigned int*)(MMIO_BASE+0x00215040))
  29. #define AUX_MU_IER ((volatile unsigned int*)(MMIO_BASE+0x00215044))
  30. #define AUX_MU_IIR ((volatile unsigned int*)(MMIO_BASE+0x00215048))
  31. #define AUX_MU_LCR ((volatile unsigned int*)(MMIO_BASE+0x0021504C))
  32. #define AUX_MU_MCR ((volatile unsigned int*)(MMIO_BASE+0x00215050))
  33. #define AUX_MU_LSR ((volatile unsigned int*)(MMIO_BASE+0x00215054))
  34. #define AUX_MU_MSR ((volatile unsigned int*)(MMIO_BASE+0x00215058))
  35. #define AUX_MU_SCRATCH ((volatile unsigned int*)(MMIO_BASE+0x0021505C))
  36. #define AUX_MU_CNTL ((volatile unsigned int*)(MMIO_BASE+0x00215060))
  37. #define AUX_MU_STAT ((volatile unsigned int*)(MMIO_BASE+0x00215064))
  38. #define AUX_MU_BAUD ((volatile unsigned int*)(MMIO_BASE+0x00215068))
  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. *AUX_ENABLE |=1; // enable UART1, AUX mini uart
  47. *AUX_MU_IER = 0;
  48. *AUX_MU_CNTL = 0;
  49. *AUX_MU_LCR = 3; // 8 bits
  50. *AUX_MU_MCR = 0;
  51. *AUX_MU_IER = 0;
  52. *AUX_MU_IIR = 0xc6; // disable interrupts
  53. *AUX_MU_BAUD = 270; // 115200 baud
  54. /* map UART1 to GPIO pins */
  55. r=*GPFSEL1;
  56. r&=~((7<<12)|(7<<15)); // gpio14, gpio15
  57. r|=(2<<12)|(2<<15); // alt5
  58. *GPFSEL1 = r;
  59. *GPPUD = 0; // enable pins 14 and 15
  60. r=150; while(r--) { asm volatile("nop"); }
  61. *GPPUDCLK0 = (1<<14)|(1<<15);
  62. r=150; while(r--) { asm volatile("nop"); }
  63. *GPPUDCLK0 = 0; // flush GPIO setup
  64. *AUX_MU_CNTL = 3; // enable Tx, Rx
  65. }
  66. /**
  67. * Send a character
  68. */
  69. void uart_send(unsigned int c) {
  70. /* wait until we can send */
  71. do{asm volatile("nop");}while(!(*AUX_MU_LSR&0x20));
  72. /* write the character to the buffer */
  73. *AUX_MU_IO=c;
  74. }
  75. /**
  76. * Receive a character
  77. */
  78. char uart_getc() {
  79. char r;
  80. /* wait until something is in the buffer */
  81. do{asm volatile("nop");}while(!(*AUX_MU_LSR&0x01));
  82. /* read it and return */
  83. r=(char)(*AUX_MU_IO);
  84. /* convert carrige return to newline */
  85. return r=='\r'?'\n':r;
  86. }
  87. /**
  88. * Display a string
  89. */
  90. void uart_puts(char *s) {
  91. while(*s) {
  92. /* convert newline to carrige return + newline */
  93. if(*s=='\n')
  94. uart_send('\r');
  95. uart_send(*s++);
  96. }
  97. }
  98. /**
  99. * Display a binary value in hexadecimal
  100. */
  101. void uart_hex(unsigned int d) {
  102. unsigned int n;
  103. int c;
  104. for(c=28;c>=0;c-=4) {
  105. // get highest tetrad
  106. n=(d>>c)&0xF;
  107. // 0-9 => '0'-'9', 10-15 => 'A'-'F'
  108. n+=n>9?0x37:0x30;
  109. uart_send(n);
  110. }
  111. }