AVR ISP adapter (avr109/910 compatible, using ATmega16)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.1KB

  1. /***************************************************************************
  2. * Copyright (C) 2006 - 2020 by Olaf Rempel *
  3. * razzor AT kopf MINUS tisch DOT de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License, *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <avr/io.h>
  20. #include "target.h"
  21. #include "uart.h"
  22. #define UART_CALC_BAUDRATE(baudRate) (((uint32_t)F_CPU) / (((uint32_t)baudRate)*16) -1)
  23. /* ***********************************************************************
  24. * uart_send
  25. * *********************************************************************** */
  26. void uart_send(uint8_t data)
  27. {
  28. #if defined(__AVR_ATmega16__)
  29. loop_until_bit_is_set(UCSRA, UDRE);
  30. UDR = data;
  31. #elif defined(__AVR_ATmega328P__)
  32. loop_until_bit_is_set(UCSR0A, UDRE0);
  33. UDR0 = data;
  34. #endif
  35. } /* uart_send */
  36. /* ***********************************************************************
  37. * uart_recv
  38. * *********************************************************************** */
  39. uint8_t uart_recv(void)
  40. {
  41. #if defined(__AVR_ATmega16__)
  42. loop_until_bit_is_set(UCSRA, RXC);
  43. return UDR;
  44. #elif defined(__AVR_ATmega328P__)
  45. loop_until_bit_is_set(UCSR0A, RXC0);
  46. return UDR0;
  47. #endif
  48. } /* uart_recv */
  49. /* ***********************************************************************
  50. * uart_rx_ready
  51. * *********************************************************************** */
  52. uint8_t uart_rx_ready(void)
  53. {
  54. #if defined(__AVR_ATmega16__)
  55. return ((UCSRA & (1<<RXC)) == (1<<RXC));
  56. #elif defined(__AVR_ATmega328P__)
  57. return ((UCSR0A & (1<<RXC0)) == (1<<RXC0));
  58. #endif
  59. } /* uart_rx_ready */
  60. /* ***********************************************************************
  61. * uart_send_buf
  62. * *********************************************************************** */
  63. void uart_send_buf(const uint8_t * p_data, uint16_t data_length)
  64. {
  65. while (data_length--)
  66. {
  67. uart_send(*p_data++);
  68. }
  69. } /* uart_send_buf */
  70. /* ***********************************************************************
  71. * uart_recv_buf
  72. * *********************************************************************** */
  73. void uart_recv_buf(uint8_t * p_data, uint16_t data_length)
  74. {
  75. while (data_length--)
  76. {
  77. *p_data++ = uart_recv();
  78. }
  79. } /* uart_recv_buf */
  80. /* ***********************************************************************
  81. * uart_init
  82. * *********************************************************************** */
  83. void uart_init(void)
  84. {
  85. #if defined(OSCCAL_VALUE)
  86. OSCCAL = OSCCAL_VALUE;
  87. #endif /* defined(OSCCAL_VALUE) */
  88. #if defined(__AVR_ATmega16__)
  89. /* Set baud rate */
  90. UBRRH = (UART_CALC_BAUDRATE(BAUDRATE)>>8) & 0xFF;
  91. UBRRL = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF);
  92. /* enable usart with 8n1 */
  93. UCSRB = (1<<TXEN) | (1<<RXEN);
  94. UCSRC = (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0);
  95. #elif defined(__AVR_ATmega328P__)
  96. /* Set baud rate */
  97. UBRR0H = (UART_CALC_BAUDRATE(BAUDRATE)>>8) & 0xFF;
  98. UBRR0L = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF);
  99. /* enable usart with 8n1 */
  100. UCSR0B = (1<<TXEN0) | (1<<RXEN0);
  101. UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
  102. #endif
  103. } /* uart_init */