Subversion Repositories HomeAutomation

Rev

Rev 248 | 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 but before any ISR()
  11. #include "mcp2515/mcp2515.h"
  12.  
  13. //---------------------------------------------------------------------------
  14. // Globals
  15.  
  16. volatile long gMilliSecTick;
  17.  
  18. //---------------------------------------------------------------------------
  19. // Function definitions for the exported interface
  20.  
  21. void reset(void) {
  22.     //Just loop and let the watchdog reset
  23.     cli();
  24.     while(1);
  25. }
  26.  
  27. void bios_putchar(char c) {
  28.     UART_PUTCHAR(c);
  29. }
  30.  
  31. char bios_getchar(void) {
  32.     char c;
  33.     UART_GETCHAR(c)
  34.     return c;
  35. }
  36.  
  37. long timebase_get(void) {
  38.     uint8_t sreg;
  39.     long res;
  40.    
  41.     //Disable interrupts while reading tick count
  42.     sreg=SREG;
  43.     cli();
  44.    
  45.     res = gMilliSecTick;
  46.    
  47.     SREG=sreg;
  48.    
  49.     return res;
  50. }
  51.  
  52. // TODO: Insert can driver's Can_Send directly in bios interface.
  53. Can_Return_t can_send(Can_Message_t* msg) {
  54.     return CAN_FAIL;
  55. }
  56.  
  57. //Can_Return_t can_receive(Can_Message_t *msg) {
  58. //  return CAN_FAIL;
  59. //}
  60.  
  61. //---------------------------------------------------------------------------
  62. // Function definitions for the private functions
  63.  
  64. static int console_getchar(FILE *stream) {
  65.     return bios_getchar();
  66. }
  67.  
  68. static int console_putchar(char c, FILE *stream) {
  69.     bios_putchar(c);
  70.     return 0;
  71. }
  72.  
  73. static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar,
  74.                                          _FDEV_SETUP_RW);
  75.    
  76.  
  77. //---------------------------------------------------------------------------
  78. // Interrupt Service Routines
  79. // Read app_vectors.h!
  80.  
  81. ISR(TIMEBASE_VECTOR) {
  82.     TIMEBASE_COUNTER -= TIMEBASE_RELOAD;
  83.     gMilliSecTick++;
  84.     wdt_reset();
  85. }
  86.  
  87. void Can_Process(Can_Message_t* msg) {
  88.     if (msg->ExtendedFlag && ((msg->Id & CAN_FUNCT_MASK) == CAN_MSG_TYPE_BIOS)) {
  89.         printf("BIOS received msg!\n");
  90.     } else if (bios->can_callback) {
  91.         printf("Calling app callback.\n");
  92.         bios->can_callback(msg);
  93.     } else {
  94.         printf("BIOS received application msg but no handler installed!\n");
  95.     }
  96. }
  97.  
  98. int main() {
  99.     void (*app_reset)(void) = 0;
  100.    
  101.     // Setup USART for debug channel
  102.     UART_INIT();
  103.     stdout = &bios_stdio;
  104.     stdin = &bios_stdio;
  105.    
  106.     // Initialize a suitable hardware timer to create a 1KHz interrupt.
  107.     TIMEBASE_INIT();
  108.    
  109.     // Enable watchdog timer to protect against an application locking the
  110.     // node by leaving interrupts disabled.
  111.     wdt_enable(WDTO_500MS);
  112.    
  113.     // Move interrupt vectors to start of bootloader section and enable interrupts
  114.     IVSEL_REG = _BV(IVCE);
  115.     IVSEL_REG = _BV(IVSEL);
  116.     sei();
  117.    
  118.     printf("AVR BIOS\n");
  119.    
  120.     if (pgm_read_word(0) != 0xffff) {
  121.         printf("Starting application.\n");
  122.         app_reset();
  123.     }
  124.    
  125.     printf("No application found.\n");
  126.    
  127.     printf("\n------------------------------------------------------------\n");
  128.     printf(  "   CAN Test: Periodic Transmission\n");
  129.     printf(  "------------------------------------------------------------\n");
  130.    
  131.     printf("CanInit...\n");
  132.     if (Can_Init() != CAN_OK) {
  133.         printf("FAILED!\n");
  134.     }
  135.     else {
  136.         printf("OK!\n");
  137.     }
  138.    
  139.     uint32_t timeStamp = 0;
  140.    
  141.     Can_Message_t txMsg;
  142.     //The databytes are just what happens to be in the memory. They are never set.
  143.     txMsg.RemoteFlag = 0;
  144.     txMsg.ExtendedFlag = 1;
  145. #define NODENUMBER 2
  146. #if NODENUMBER == 1
  147.     txMsg.Id = 16;
  148.     txMsg.DataLength = 2;
  149. #elif NODENUMBER == 2
  150.     txMsg.Id = 32;
  151.     txMsg.DataLength = 5;
  152. #else
  153. # error NODENUMBER not set!
  154. #endif
  155.    
  156.     /* main loop */
  157.     while (1) {
  158.        
  159.         /* send CAN message and check for CAN errors once every second */
  160.         if ((timebase_get() - timeStamp) >= 1000) {
  161.             timeStamp = timebase_get();
  162.             /* send txMsg */
  163.             txMsg.Id++;
  164.             Can_Send(&txMsg);
  165.         }
  166.     }
  167.  
  168.     return 0;
  169. }
  170.