botgob

driverusart

May 21st, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. /* * libMD407, driver_usart.c * Drivrutiner för STDIN_FILENO, STDOUT_FILENO och STDERR_FILENO */
  2. #include <libmd407.h>
  3.  
  4. /* Deklarationer av de funktioner som ingår, krävs för tabellen */
  5.  
  6. #define USART1 ((USART *) 0x40011000)
  7.  
  8. #define BIT_UE (1<<13)
  9. #define BIT_TE (1<<3)
  10. #define BIT_RE (1<<2)
  11. #define BIT_RXNE (1<<5)
  12. #define BIT_TXE (1<<7)
  13.  
  14.  
  15. typedef struct tag_usart{
  16. volatile unsigned short sr;
  17. volatile unsigned short Unused0;
  18. volatile unsigned short dr;
  19. volatile unsigned short Unused1;
  20. volatile unsigned short brr;
  21. volatile unsigned short Unused2;
  22. volatile unsigned short cr1;
  23. volatile unsigned short Unused3;
  24. volatile unsigned short cr2;
  25. volatile unsigned short Unused4;
  26. volatile unsigned short cr3;
  27. volatile unsigned short Unused5;
  28. volatile unsigned short gtpr;
  29. } USART;
  30.  
  31. char _tstchar( void ){
  32. if( (USART1->sr & BIT_RXNE ) == BIT_RXNE)
  33. return (char) USART1->dr;
  34. return 0;
  35. }
  36.  
  37. char _outchar( char c ){
  38. while(( USART1->sr & BIT_TXE ) == 0);
  39. USART1->dr = (unsigned short) c;
  40. }
  41.  
  42. static int usart_init( int initval ) {
  43. USART1->brr = 0x2D9;
  44. USART1->cr2 = 0;
  45. USART1->cr1 = BIT_UE | BIT_TE | BIT_RE;
  46. }
  47. static void usart_deinit( int deinitval) {
  48. USART1->brr = 0;
  49. USART1->cr2=0;
  50. USART1->cr1 = 0;
  51. }
  52. static int usart_write(char *ptr, int len){
  53. for(int i = 0; i < len; i++){
  54. _outchar(*ptr++);
  55. }
  56. }
  57. static int usart_read(char *ptr, int len){
  58. char c = 0;
  59. while(!c){
  60. c = _tstchar();
  61. }
  62. ptr = c;
  63. //return c;
  64. }
  65. static int usart_isatty(void){ return 1; }
  66.  
  67. DEV_DRIVER_DESC StdIn = {
  68. {"stdin"},
  69. usart_init,
  70. usart_deinit,
  71. 0,
  72. usart_isatty,
  73. 0,
  74. 0,
  75. 0,
  76. 0,
  77. usart_read
  78. };
  79.  
  80. DEV_DRIVER_DESC StdOut = {
  81. {"stdout"},
  82. 0,
  83. 0,
  84. 0,
  85. usart_isatty,
  86. 0,
  87. 0,
  88. 0,
  89. usart_write,
  90. 0
  91. };
  92.  
  93. DEV_DRIVER_DESC StdErr = {
  94. {"stderr"},
  95. 0,
  96. 0,
  97. 0,
  98. usart_isatty,
  99. 0,
  100. 0,
  101. 0,
  102. usart_write,
  103. 0
  104. };
  105.  
  106.  
Advertisement
Add Comment
Please, Sign In to add comment