Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 181 | andfri | 1 | #include "bios_funs.h" |
| 223 | andfri | 2 | #include "avr_cfg.h" |
| 181 | andfri | 3 | #include <inttypes.h> |
| 228 | andfri | 4 | #include <avr/io.h> |
| 181 | andfri | 5 | #include <avr/interrupt.h> |
| 223 | andfri | 6 | #include <avr/wdt.h> |
| 181 | andfri | 7 | #include <avr/pgmspace.h> |
| 8 | #include <stdio.h> |
||
| 228 | andfri | 9 | #include "flash.h" |
| 240 | andfri | 10 | #include "vectors.h" // Must be included after sfr_defs.h but before any ISR() |
| 250 | andfri | 11 | #include "mcp2515/mcp2515.h" |
| 181 | andfri | 12 | |
| 13 | //--------------------------------------------------------------------------- |
||
| 223 | andfri | 14 | // Globals |
| 15 | |||
| 16 | volatile long gMilliSecTick; |
||
| 17 | |||
| 18 | //--------------------------------------------------------------------------- |
||
| 181 | andfri | 19 | // Function definitions for the exported interface |
| 20 | |||
| 223 | andfri | 21 | void reset(void) { |
| 22 | //Just loop and let the watchdog reset |
||
| 23 | cli(); |
||
| 24 | while(1); |
||
| 250 | andfri | 25 | } |
| 223 | andfri | 26 | |
| 27 | void bios_putchar(char c) { |
||
| 228 | andfri | 28 | UART_PUTCHAR(c); |
| 250 | andfri | 29 | } |
| 181 | andfri | 30 | |
| 223 | andfri | 31 | char bios_getchar(void) { |
| 228 | andfri | 32 | char c; |
| 33 | UART_GETCHAR(c) |
||
| 34 | return c; |
||
| 250 | andfri | 35 | } |
| 181 | andfri | 36 | |
| 223 | andfri | 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 | } |
||
| 181 | andfri | 51 | |
| 250 | andfri | 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 | |||
| 181 | andfri | 61 | //--------------------------------------------------------------------------- |
| 62 | // Function definitions for the private functions |
||
| 223 | andfri | 63 | |
| 64 | static int console_getchar(FILE *stream) { |
||
| 228 | andfri | 65 | return bios_getchar(); |
| 223 | andfri | 66 | } |
| 67 | |||
| 68 | static int console_putchar(char c, FILE *stream) { |
||
| 228 | andfri | 69 | bios_putchar(c); |
| 223 | andfri | 70 | return 0; |
| 71 | } |
||
| 72 | |||
| 73 | static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar, |
||
| 181 | andfri | 74 | _FDEV_SETUP_RW); |
| 75 | |||
| 223 | andfri | 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 | |||
| 250 | andfri | 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 | |||
| 181 | andfri | 98 | int main() { |
| 99 | void (*app_reset)(void) = 0; |
||
| 223 | andfri | 100 | |
| 181 | andfri | 101 | // Setup USART for debug channel |
| 228 | andfri | 102 | UART_INIT(); |
| 181 | andfri | 103 | stdout = &bios_stdio; |
| 104 | stdin = &bios_stdio; |
||
| 105 | |||
| 223 | andfri | 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 |
||
| 228 | andfri | 114 | IVSEL_REG = _BV(IVCE); |
| 115 | IVSEL_REG = _BV(IVSEL); |
||
| 223 | andfri | 116 | sei(); |
| 117 | |||
| 181 | andfri | 118 | printf("AVR BIOS\n"); |
| 119 | |||
| 250 | andfri | 120 | if (pgm_read_word(0) != 0xffff) { |
| 121 | printf("Starting application.\n"); |
||
| 122 | app_reset(); |
||
| 123 | } |
||
| 223 | andfri | 124 | |
| 250 | andfri | 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"); |
||
| 240 | andfri | 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; |
||
| 250 | andfri | 145 | #define NODENUMBER 2 |
| 146 | #if NODENUMBER == 1 |
||
| 147 | txMsg.Id = 16; |
||
| 240 | andfri | 148 | txMsg.DataLength = 2; |
| 250 | andfri | 149 | #elif NODENUMBER == 2 |
| 150 | txMsg.Id = 32; |
||
| 151 | txMsg.DataLength = 5; |
||
| 152 | #else |
||
| 153 | # error NODENUMBER not set! |
||
| 154 | #endif |
||
| 240 | andfri | 155 | |
| 156 | /* main loop */ |
||
| 157 | while (1) { |
||
| 158 | |||
| 159 | /* send CAN message and check for CAN errors once every second */ |
||
| 250 | andfri | 160 | if ((timebase_get() - timeStamp) >= 1000) { |
| 240 | andfri | 161 | timeStamp = timebase_get(); |
| 162 | /* send txMsg */ |
||
| 250 | andfri | 163 | txMsg.Id++; |
| 240 | andfri | 164 | Can_Send(&txMsg); |
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 181 | andfri | 168 | return 0; |
| 169 | } |