Subversion Repositories HomeAutomation

Rev

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

  1. #include "bios_funs.h"
  2. #include "avr_cfg.h"
  3. #include <inttypes.h>
  4. #include <avr/io.h>
  5. #include <avr/interrupt.h>
  6. #include <avr/wdt.h>
  7. #include <avr/pgmspace.h>
  8. #include <stdio.h>
  9. #include "flash.h"
  10. #include "vectors.h" // Must be included after sfr_defs.h
  11.  
  12. //---------------------------------------------------------------------------
  13. // Globals
  14.  
  15. volatile long gMilliSecTick;
  16.  
  17. //---------------------------------------------------------------------------
  18. // Function definitions for the exported interface
  19.  
  20. void reset(void) {
  21.     //Just loop and let the watchdog reset
  22.     cli();
  23.     while(1);
  24. };
  25.  
  26. void bios_putchar(char c) {
  27.     UART_PUTCHAR(c);
  28. };
  29.  
  30. char bios_getchar(void) {
  31.     char c;
  32.     UART_GETCHAR(c)
  33.     return c;
  34. };
  35.  
  36. long timebase_get(void) {
  37.     uint8_t sreg;
  38.     long res;
  39.    
  40.     //Disable interrupts while reading tick count
  41.     sreg=SREG;
  42.     cli();
  43.    
  44.     res = gMilliSecTick;
  45.    
  46.     SREG=sreg;
  47.    
  48.     return res;
  49. }
  50.  
  51. //---------------------------------------------------------------------------
  52. // Function definitions for the private functions
  53.  
  54. static int console_getchar(FILE *stream) {
  55.     return bios_getchar();
  56. }
  57.  
  58. static int console_putchar(char c, FILE *stream) {
  59.     bios_putchar(c);
  60.     return 0;
  61. }
  62.  
  63. static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar,
  64.                                          _FDEV_SETUP_RW);
  65.    
  66.  
  67. //---------------------------------------------------------------------------
  68. // Interrupt Service Routines
  69. // Read app_vectors.h!
  70.  
  71. ISR(TIMEBASE_VECTOR) {
  72.     TIMEBASE_COUNTER -= TIMEBASE_RELOAD;
  73.     gMilliSecTick++;
  74.     wdt_reset();
  75. }
  76.  
  77. int main() {
  78.     void (*app_reset)(void) = 0;
  79.    
  80.     // Setup USART for debug channel
  81.     UART_INIT();
  82.     stdout = &bios_stdio;
  83.     stdin = &bios_stdio;
  84.    
  85.     // Initialize a suitable hardware timer to create a 1KHz interrupt.
  86.     TIMEBASE_INIT();
  87.    
  88.     // Enable watchdog timer to protect against an application locking the
  89.     // node by leaving interrupts disabled.
  90.     wdt_enable(WDTO_500MS);
  91.    
  92.     // Move interrupt vectors to start of bootloader section and enable interrupts
  93.     IVSEL_REG = _BV(IVCE);
  94.     IVSEL_REG = _BV(IVSEL);
  95.     sei();
  96.    
  97.     printf("AVR BIOS\n");
  98.    
  99.     if (pgm_read_word(0) != 0xffff) {
  100.         printf("Starting application.\n");
  101.         app_reset();
  102.     }
  103.    
  104.     printf("No application found.\n");
  105.     return 0;
  106. }
  107.