IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Git Source Tree
Root/
| 1 | /* |
| 2 | * cntr/cntr.c - CNTR initialization and main loop |
| 3 | * |
| 4 | * Written 2008-2010 by Werner Almesberger |
| 5 | * Copyright 2008-2010 Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | #include "regs.h" |
| 17 | #include "io.h" |
| 18 | #include "usb.h" |
| 19 | #include "cntr/ep0.h" |
| 20 | #include "version.h" |
| 21 | #include "cntr.h" |
| 22 | |
| 23 | |
| 24 | uint8_t cntr[4]; |
| 25 | enum hw_type hw_type; |
| 26 | |
| 27 | |
| 28 | static void delay(unsigned ms) |
| 29 | { |
| 30 | int x; |
| 31 | |
| 32 | while (ms--) |
| 33 | for (x = 0; x < 1488; x) |
| 34 | x++; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | static void init_io(void) |
| 39 | { |
| 40 | int i; |
| 41 | |
| 42 | /* SDCC bug - needs parentheses here */ |
| 43 | hw_type = VERSION_ID1 ? |
| 44 | (VERSION_ID2 ? HW_TYPE_V2 : HW_TYPE_V3) : |
| 45 | HW_TYPE_V1; |
| 46 | |
| 47 | for (i = HW_TYPE_V1; i != hw_type; i++) { |
| 48 | /* flash LED a second or third time */ |
| 49 | LEDv2 = 0; |
| 50 | delay(250); |
| 51 | LEDv2 = 1; |
| 52 | delay(250); |
| 53 | } |
| 54 | if (hw_type == HW_TYPE_V2) |
| 55 | PROBE_TERM_MODE |= 1 << PROBE_TERM_BIT; |
| 56 | |
| 57 | /* |
| 58 | * Signal Mode Value |
| 59 | * |
| 60 | * PROBE_T0 open drain 1 (input) |
| 61 | * PROBE_ECI open drain 1 (input) |
| 62 | * PROBE_INT0 open drain 1 (input) |
| 63 | * |
| 64 | * PROBE_TERM open drain 0 version 1 |
| 65 | * PROBE_TERM push-pull 1 version 2 |
| 66 | * |
| 67 | * LEDv1 push-pull 0 (set up by boot loader) |
| 68 | * LEDv2 push-pull 0 (set up by boot loader) |
| 69 | * |
| 70 | * all unused open drain 0 |
| 71 | */ |
| 72 | |
| 73 | P0 = 1 << PROBE_INT0_BIT; |
| 74 | P1 = (1 << PROBE_T0_BIT) | (1 << PROBE_ECI_BIT); |
| 75 | P2 = 0; |
| 76 | P3 = 0; |
| 77 | |
| 78 | if (hw_type == HW_TYPE_V2 || hw_type == HW_TYPE_V3) |
| 79 | PROBE_TERM = 1; |
| 80 | |
| 81 | /* |
| 82 | * Disable pull-ups |
| 83 | */ |
| 84 | XBR1 |= WEAKPUD; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | static void setup_counter(void) |
| 89 | { |
| 90 | TCON = TR0; /* enable Timer 0 */ |
| 91 | TMOD = C_T0 | T0M0; /* clock from T0, mode 1: 16 bit counter */ |
| 92 | |
| 93 | XBR1 |= T0E; /* route T0 to port */ |
| 94 | |
| 95 | P0SKIP = 0x7f; /* assign T0 to P0_7 */ |
| 96 | } |
| 97 | |
| 98 | |
| 99 | static void read_counter(void) |
| 100 | { |
| 101 | uint8_t th; |
| 102 | |
| 103 | th = TH0; |
| 104 | while (1) { |
| 105 | cntr[0] = TL0; |
| 106 | if (th == TH0) |
| 107 | break; |
| 108 | th = TH0; |
| 109 | } |
| 110 | if (th < cntr[1]) { |
| 111 | cntr[2]++; |
| 112 | if (!cntr[2]) |
| 113 | cntr[3]++; |
| 114 | } |
| 115 | cntr[1] = th; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | void main(void) |
| 120 | { |
| 121 | init_io(); |
| 122 | setup_counter(); |
| 123 | |
| 124 | usb_init(); |
| 125 | ep0_init(); |
| 126 | |
| 127 | while (1) { |
| 128 | read_counter(); |
| 129 | usb_poll(); |
| 130 | } |
| 131 | } |
| 132 |
