uart.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /* PL011 UART registers */
  28. #define UART0_DR ((volatile unsigned int*)(MMIO_BASE+0x00201000))
  29. #define UART0_FR ((volatile unsigned int*)(MMIO_BASE+0x00201018))
  30. #define UART0_IBRD ((volatile unsigned int*)(MMIO_BASE+0x00201024))
  31. #define UART0_FBRD ((volatile unsigned int*)(MMIO_BASE+0x00201028))
  32. #define UART0_LCRH ((volatile unsigned int*)(MMIO_BASE+0x0020102C))
  33. #define UART0_CR ((volatile unsigned int*)(MMIO_BASE+0x00201030))
  34. #define UART0_IMSC ((volatile unsigned int*)(MMIO_BASE+0x00201038))
  35. #define UART0_ICR ((volatile unsigned int*)(MMIO_BASE+0x00201044))
  36. /**
  37. * Set baud rate and characteristics (115200 8N1) and map to GPIO
  38. */
  39. void uart_init()
  40. {
  41. register unsigned int r;
  42. /* initialize UART */
  43. *UART0_CR = 0; // turn off UART0
  44. /* set up clock for consistent divisor values */
  45. mbox[0] = 9*4;
  46. mbox[1] = MBOX_REQUEST;
  47. mbox[2] = MBOX_TAG_SETCLKRATE; // set clock rate
  48. mbox[3] = 12;
  49. mbox[4] = 8;
  50. mbox[5] = 2; // UART clock
  51. mbox[6] = 4000000; // 4Mhz
  52. mbox[7] = 0; // clear turbo
  53. mbox[8] = MBOX_TAG_LAST;
  54. mbox_call(MBOX_CH_PROP);
  55. /* map UART0 to GPIO pins */
  56. r=*GPFSEL1;
  57. r&=~((7<<12)|(7<<15)); // gpio14, gpio15
  58. r|=(4<<12)|(4<<15); // alt0
  59. *GPFSEL1 = r;
  60. *GPPUD = 0; // enable pins 14 and 15
  61. r=150; while(r--) { asm volatile("nop"); }
  62. *GPPUDCLK0 = (1<<14)|(1<<15);
  63. r=150; while(r--) { asm volatile("nop"); }
  64. *GPPUDCLK0 = 0; // flush GPIO setup
  65. *UART0_ICR = 0x7FF; // clear interrupts
  66. *UART0_IBRD = 2; // 115200 baud
  67. *UART0_FBRD = 0xB;
  68. *UART0_LCRH = 0x7<<4; // 8n1, enable FIFOs
  69. *UART0_CR = 0x301; // enable Tx, Rx, UART
  70. }
  71. /**
  72. * Send a character
  73. */
  74. void uart_send(unsigned int c) {
  75. /* wait until we can send */
  76. do{asm volatile("nop");}while(*UART0_FR&0x20);
  77. /* write the character to the buffer */
  78. *UART0_DR=c;
  79. }
  80. /**
  81. * Receive a character
  82. */
  83. char uart_getc() {
  84. /* wait until something is in the buffer */
  85. do{asm volatile("nop");}while(*UART0_FR&0x10);
  86. /* read it and return */
  87. return (char)(*UART0_DR);
  88. }