Subversion Repositories HomeAutomation

Rev

Rev 149 | Blame | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * Serial communication software module.
  3.  *
  4.  * @author  Jimmy Myhrman
  5.  *
  6.  * @date    2006-11-22
  7.  */
  8.  
  9. /*-----------------------------------------------------------------------------
  10.  * Includes
  11.  *---------------------------------------------------------------------------*/
  12. #include <serial.h>
  13. #include <uart.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16.  
  17.  
  18. /*-----------------------------------------------------------------------------
  19.  * Prerequisites
  20.  *---------------------------------------------------------------------------*/
  21. #ifndef SERIAL_BAUDRATE
  22.     #error SERIAL_BAUDRATE not specified! Edit serial_cfg.h !
  23. #endif
  24.  
  25. #ifndef SERIAL_RX_BUFFER_SIZE
  26.     #error SERIAL_RX_BUFFER_SIZE not specified! Edit serial_cfg.h !
  27. #endif
  28.  
  29. #ifndef SERIAL_TX_BUFFER_SIZE
  30.     #error SERIAL_TX_BUFFER_SIZE not specified! Edit serial_cfg.h !
  31. #endif
  32.  
  33.  
  34. /*-----------------------------------------------------------------------------
  35.  * Private Function Prototypes
  36.  *---------------------------------------------------------------------------*/
  37. static int serial_putchar(char c, FILE *stream);
  38.  
  39.  
  40. /*-----------------------------------------------------------------------------
  41.  * Variables
  42.  *---------------------------------------------------------------------------*/
  43. static FILE myStdOut = FDEV_SETUP_STREAM(serial_putchar, NULL,_FDEV_SETUP_WRITE);
  44.  
  45.  
  46. /*-----------------------------------------------------------------------------
  47.  * Public Functions
  48.  *---------------------------------------------------------------------------*/
  49.  
  50. /**
  51.  * Initializes the serial communication. Sets the baudrate and redirects
  52.  * STDOUT to the serial communication channel.
  53.  */
  54. void Serial_Init() {
  55.     uart_init((UART_BAUD_SELECT((SERIAL_BAUDRATE),F_CPU)));
  56.    
  57.     stdout = &myStdOut;
  58. }
  59.  
  60.  
  61. /*-----------------------------------------------------------------------------
  62.  * Private Functions
  63.  *---------------------------------------------------------------------------*/
  64.  
  65. static int serial_putchar(char c, FILE *stream) {
  66.     if (c == '\n') serial_putchar('\r', stream);
  67.     uart_putc(c);
  68.     return 0;
  69. }
  70.