Advertisement
tovis

AVR UART handling basic test

Jun 9th, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.09 KB | None | 0 0
  1. /*
  2.  * Only testing purposes!!!
  3.  * It's tested on ATmega16A with 16MHz external crystal.
  4.  * Serial channel connected to PC (Linux minicom) using MAX3232 converter IC.
  5.  * Outputs always "Z" 0x5A, but after receive some characters (5 - 20) from PC it's stuck.
  6.  * Really strange phenomen.
  7.  */
  8.  
  9. /* *********************************************************************
  10.  * avr-uart.h
  11.  * AVR uart handler for ATmega16
  12.  * Common definitions
  13.  * by tovis 2015
  14.  **********************************************************************/
  15.  #ifndef  AVR_UART
  16.  #define  AVR_UART
  17.  #endif
  18.  
  19.  #ifndef  AVR_UART_ASM
  20.  //---------------------------------------------------------------------
  21.  void           uart_put(void);
  22.  void           uart_get(void);
  23.  void           uart_setup(void);
  24.  
  25.  //---------------------------------------------------------------------
  26.  // Helper routines
  27.  extern void    toggle_A0(void);
  28.  extern void    toggle_A1(void);
  29.  extern void    toggle_A2(void);
  30.  
  31.  extern void    toggle_A7(void);
  32.  
  33.  #endif
  34. /* *********************************************************************
  35.  * end avr-uart.h
  36.  **********************************************************************/
  37.  
  38. /* *********************************************************************
  39.  * avr-uart-test.c
  40.  * AVR uart handler for ATmega16
  41.  * Test main program
  42.  * by tovis 2015
  43.  **********************************************************************/
  44. #include  <avr/io.h>
  45. #include  <avr/interrupt.h>
  46.  
  47. #include  "avr-uart.h"
  48.  
  49. //----------------------------------------------------------------------
  50. #define F_CPU           16000000UL      // ATmega16A with external crystal 16MHz
  51.  
  52. //**********************************************************************
  53. void            main(void)
  54. {
  55.   DDRA   = 0xFF;                        // whole PORTA set as output
  56.   uart_setup();
  57.   sei();
  58.   while ( 1 )
  59.   {
  60.     uart_put();
  61.     uart_get();
  62.     toggle_A0();                        // ???
  63.   }
  64. } // end main
  65.  
  66.  /* ********************************************************************
  67.  * end avr-uart-test.c
  68.  **********************************************************************/
  69.  
  70. /* *********************************************************************
  71.  * avr-uart-isr.S
  72.  * AVR uart handler for ATmega16
  73.  * ISR routine and helper utilities
  74.  * by tovis 2015
  75.  **********************************************************************/
  76. #define AVR_UART_ASM
  77.  
  78. #include  <avr/io.h>
  79. #include  <avr/interrupt.h>
  80.  
  81. #include  "avr-uart.h"
  82.  
  83. //**********************************************************************
  84. .global BADISR_vect
  85. BADISR_vect:
  86.         CALL    toggle_A7
  87.         RJMP    BADISR_vect
  88. //**********************************************************************
  89. //----------------------------------------------------------------------
  90. .global uart_get
  91. uart_get:
  92.         SBIS    _SFR_IO_ADDR(UCSRA),RXC         //; check if received data available
  93.         RJMP    uart_get_notready
  94.         CALL    toggle_A2                       //; ???
  95.         PUSH    R16
  96.         IN      R16, _SFR_IO_ADDR(UDR)          //; read data from receiver buffer
  97.         POP     R16
  98. uart_get_notready:
  99.         RET
  100. //----------------------------------------------------------------------
  101. .global uart_put
  102. uart_put:
  103.         SBIS    _SFR_IO_ADDR(UCSRA), UDRE       //; check if UDR is empty
  104.         RJMP    uart_put_notready
  105.         CALL    toggle_A1                       //; ???
  106.         PUSH    R16
  107.         //; LDI     R16, 'o'
  108.         IN      R16, _SFR_IO_ADDR(SPL)
  109.         OUT     _SFR_IO_ADDR(UDR), R16
  110.         POP     R16
  111. uart_put_notready:
  112.         RET
  113. //----------------------------------------------------------------------
  114. .global uart_setup
  115. uart_setup:
  116.         PUSH    R16
  117.         EOR     R16, R16                        //; Clear R16
  118.         OUT     _SFR_IO_ADDR(UCSRB), R16        //; Disable USART interrupts
  119.                                                 //; Disable receive/transmit
  120.                                                 //; and UCSZ2 = 0 for 8 bit data
  121.         LDI     R16, lo8(103)                   //; Setup Baud rate 9600
  122.         OUT     _SFR_IO_ADDR(UBRRL), R16
  123.         LDI     R16, hi8(103)
  124.         ANDI    R16, 0x7F                       //; URSEL = 0 - access UBRRH
  125.         OUT     _SFR_IO_ADDR(UBRRH), R16
  126.  
  127.         OUT     _SFR_IO_ADDR(UCSRA), R16        //; Clrear status and U2X, MPCM
  128.         LDI     R16, 0b10000110                 //; Asynchronous 8n1
  129.         OUT     _SFR_IO_ADDR(UCSRC), R16        //; Set ssynchronous 8n1
  130.         //---
  131.         SBI     _SFR_IO_ADDR(UCSRB), TXEN       //; Transmitter enable
  132.         SBI     _SFR_IO_ADDR(UCSRB), RXEN       //; Receiver enable
  133.         POP     R16
  134.         RET
  135. //**********************************************************************
  136. .global toggle_A0
  137. toggle_A0:
  138.         SBIC    _SFR_IO_ADDR(PINA),  0
  139.         CBI     _SFR_IO_ADDR(PORTA), 0
  140.         SBIS    _SFR_IO_ADDR(PINA),  0
  141.         SBI     _SFR_IO_ADDR(PORTA), 0
  142.         RET
  143. //----------------------------------------------------------------------
  144. .global toggle_A1
  145. toggle_A1:
  146.         SBIC    _SFR_IO_ADDR(PINA),  1
  147.         CBI     _SFR_IO_ADDR(PORTA), 1
  148.         SBIS    _SFR_IO_ADDR(PINA),  1
  149.         SBI     _SFR_IO_ADDR(PORTA), 1
  150.         RET
  151. //----------------------------------------------------------------------
  152. .global toggle_A2
  153. toggle_A2:
  154.         SBIC    _SFR_IO_ADDR(PINA),  2
  155.         CBI     _SFR_IO_ADDR(PORTA), 2
  156.         SBIS    _SFR_IO_ADDR(PINA),  2
  157.         SBI     _SFR_IO_ADDR(PORTA), 2
  158.         RET
  159.  
  160. //----------------------------------------------------------------------
  161. .global toggle_A7
  162. toggle_A7:
  163.         SBIC    _SFR_IO_ADDR(PINA),  7
  164.         CBI     _SFR_IO_ADDR(PORTA), 7
  165.         SBIS    _SFR_IO_ADDR(PINA),  7
  166.         SBI     _SFR_IO_ADDR(PORTA), 7
  167.         RET
  168.  
  169. /***********************************************************************
  170.  * end avr-uart-isr.S
  171.  **********************************************************************/
  172.  
  173.  
  174. avr-uart-test.elf:     file format elf32-avr
  175.  
  176. Sections:
  177. Idx Name          Size      VMA       LMA       File off  Algn
  178.   0 .text         000000f6  00000000  00000000  00000054  2**1
  179.                   CONTENTS, ALLOC, LOAD, READONLY, CODE
  180.   1 .stab         00000cc0  00000000  00000000  0000014c  2**2
  181.                   CONTENTS, READONLY, DEBUGGING
  182.   2 .stabstr      000007a2  00000000  00000000  00000e0c  2**0
  183.                   CONTENTS, READONLY, DEBUGGING
  184.   3 .comment      00000011  00000000  00000000  000015ae  2**0
  185.                   CONTENTS, READONLY
  186.  
  187. Disassembly of section .text:
  188.  
  189. 00000000 <__vectors>:
  190.    0:   0c 94 2a 00     jmp 0x54    ; 0x54 <__ctors_end>
  191.    4:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  192.    8:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  193.    c:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  194.   10:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  195.   14:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  196.   18:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  197.   1c:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  198.   20:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  199.   24:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  200.   28:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  201.   2c:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  202.   30:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  203.   34:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  204.   38:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  205.   3c:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  206.   40:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  207.   44:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  208.   48:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  209.   4c:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  210.   50:   0c 94 34 00     jmp 0x68    ; 0x68 <__bad_interrupt>
  211.  
  212. 00000054 <__ctors_end>:
  213.   54:   11 24           eor r1, r1
  214.   56:   1f be           out 0x3f, r1    ; 63
  215.   58:   cf e5           ldi r28, 0x5F   ; 95
  216.   5a:   d4 e0           ldi r29, 0x04   ; 4
  217.   5c:   de bf           out 0x3e, r29   ; 62
  218.   5e:   cd bf           out 0x3d, r28   ; 61
  219.   60:   0e 94 6d 00     call    0xda    ; 0xda <main>
  220.   64:   0c 94 79 00     jmp 0xf2    ; 0xf2 <_exit>
  221.  
  222. 00000068 <__bad_interrupt>:
  223.   68:   0c 94 36 00     jmp 0x6c    ; 0x6c <__vector_default>
  224.  
  225. 0000006c <__vector_default>:
  226.   6c:   0e 94 68 00     call    0xd0    ; 0xd0 <toggle_A7>
  227.   70:   fd cf           rjmp    .-6         ; 0x6c <__vector_default>
  228.  
  229. 00000072 <uart_get>:
  230.   72:   5f 9b           sbis    0x0b, 7 ; 11
  231.   74:   05 c0           rjmp    .+10        ; 0x80 <uart_get_notready>
  232.   76:   0e 94 63 00     call    0xc6    ; 0xc6 <toggle_A2>
  233.   7a:   0f 93           push    r16
  234.   7c:   0c b1           in  r16, 0x0c   ; 12
  235.   7e:   0f 91           pop r16
  236.  
  237. 00000080 <uart_get_notready>:
  238.   80:   08 95           ret
  239.  
  240. 00000082 <uart_put>:
  241.   82:   5d 9b           sbis    0x0b, 5 ; 11
  242.   84:   06 c0           rjmp    .+12        ; 0x92 <uart_put_notready>
  243.   86:   0e 94 5e 00     call    0xbc    ; 0xbc <toggle_A1>
  244.   8a:   0f 93           push    r16
  245.   8c:   0d b7           in  r16, 0x3d   ; 61
  246.   8e:   0c b9           out 0x0c, r16   ; 12
  247.   90:   0f 91           pop r16
  248.  
  249. 00000092 <uart_put_notready>:
  250.   92:   08 95           ret
  251.  
  252. 00000094 <uart_setup>:
  253.   94:   0f 93           push    r16
  254.   96:   00 27           eor r16, r16
  255.   98:   0a b9           out 0x0a, r16   ; 10
  256.   9a:   07 e6           ldi r16, 0x67   ; 103
  257.   9c:   09 b9           out 0x09, r16   ; 9
  258.   9e:   00 e0           ldi r16, 0x00   ; 0
  259.   a0:   0f 77           andi    r16, 0x7F   ; 127
  260.   a2:   00 bd           out 0x20, r16   ; 32
  261.   a4:   0b b9           out 0x0b, r16   ; 11
  262.   a6:   06 e8           ldi r16, 0x86   ; 134
  263.   a8:   00 bd           out 0x20, r16   ; 32
  264.   aa:   53 9a           sbi 0x0a, 3 ; 10
  265.   ac:   54 9a           sbi 0x0a, 4 ; 10
  266.   ae:   0f 91           pop r16
  267.   b0:   08 95           ret
  268.  
  269. 000000b2 <toggle_A0>:
  270.   b2:   c8 99           sbic    0x19, 0 ; 25
  271.   b4:   d8 98           cbi 0x1b, 0 ; 27
  272.   b6:   c8 9b           sbis    0x19, 0 ; 25
  273.   b8:   d8 9a           sbi 0x1b, 0 ; 27
  274.   ba:   08 95           ret
  275.  
  276. 000000bc <toggle_A1>:
  277.   bc:   c9 99           sbic    0x19, 1 ; 25
  278.   be:   d9 98           cbi 0x1b, 1 ; 27
  279.   c0:   c9 9b           sbis    0x19, 1 ; 25
  280.   c2:   d9 9a           sbi 0x1b, 1 ; 27
  281.   c4:   08 95           ret
  282.  
  283. 000000c6 <toggle_A2>:
  284.   c6:   ca 99           sbic    0x19, 2 ; 25
  285.   c8:   da 98           cbi 0x1b, 2 ; 27
  286.   ca:   ca 9b           sbis    0x19, 2 ; 25
  287.   cc:   da 9a           sbi 0x1b, 2 ; 27
  288.   ce:   08 95           ret
  289.  
  290. 000000d0 <toggle_A7>:
  291.   d0:   cf 99           sbic    0x19, 7 ; 25
  292.   d2:   df 98           cbi 0x1b, 7 ; 27
  293.   d4:   cf 9b           sbis    0x19, 7 ; 25
  294.   d6:   df 9a           sbi 0x1b, 7 ; 27
  295.   d8:   08 95           ret
  296.  
  297. 000000da <main>:
  298. #define F_CPU           16000000UL      // ATmega16A with external crystal 16MHz
  299.  
  300. //**********************************************************************
  301. void            main(void)
  302. {
  303.   DDRA   = 0xFF;                        // whole PORTA set as output
  304.   da:   8f ef           ldi r24, 0xFF   ; 255
  305.   dc:   8a bb           out 0x1a, r24   ; 26
  306.   uart_setup();
  307.   de:   0e 94 4a 00     call    0x94    ; 0x94 <uart_setup>
  308.   sei();
  309.   e2:   78 94           sei
  310.   while ( 1 )
  311.   {
  312.     uart_put();
  313.   e4:   0e 94 41 00     call    0x82    ; 0x82 <uart_put>
  314.     uart_get();
  315.   e8:   0e 94 39 00     call    0x72    ; 0x72 <uart_get>
  316.     toggle_A0();                        // ???
  317.   ec:   0e 94 59 00     call    0xb2    ; 0xb2 <toggle_A0>
  318.   f0:   f9 cf           rjmp    .-14        ; 0xe4 <main+0xa>
  319.  
  320. 000000f2 <_exit>:
  321.   f2:   f8 94           cli
  322.  
  323. 000000f4 <__stop_program>:
  324.   f4:   ff cf           rjmp    .-2         ; 0xf4 <__stop_program>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement