root/nano-RK/projects/basic_bmac/main.c
| Revision 528, 7.8 kB (checked in by agr, 4 months ago) |
|---|
| Line | |
|---|---|
| 1 | /****************************************************************************** |
| 2 | * Nano-RK, a real-time operating system for sensor networks. |
| 3 | * Copyright (C) 2007, Real-Time and Multimedia Lab, Carnegie Mellon University |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This is the Open Source Version of Nano-RK included as part of a Dual |
| 7 | * Licensing Model. If you are unsure which license to use please refer to: |
| 8 | * http://www.nanork.org/nano-RK/wiki/Licensing |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, version 2.0 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | *******************************************************************************/ |
| 23 | |
| 24 | #include <nrk.h> |
| 25 | #include <include.h> |
| 26 | #include <ulib.h> |
| 27 | #include <stdio.h> |
| 28 | #include <avr/sleep.h> |
| 29 | #include <hal.h> |
| 30 | #include <bmac.h> |
| 31 | #include <nrk_error.h> |
| 32 | |
| 33 | // Only require MAC address for address decode |
| 34 | #define MAC_ADDR 0x0001 |
| 35 | |
| 36 | nrk_task_type RX_TASK; |
| 37 | NRK_STK rx_task_stack[NRK_APP_STACKSIZE]; |
| 38 | void rx_task (void); |
| 39 | |
| 40 | |
| 41 | nrk_task_type TX_TASK; |
| 42 | NRK_STK tx_task_stack[NRK_APP_STACKSIZE]; |
| 43 | void tx_task (void); |
| 44 | |
| 45 | void nrk_create_taskset (); |
| 46 | |
| 47 | uint8_t tx_buf[RF_MAX_PAYLOAD_SIZE]; |
| 48 | uint8_t rx_buf[RF_MAX_PAYLOAD_SIZE]; |
| 49 | uint8_t aes_key[16]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e, 0x0f}; |
| 50 | |
| 51 | int main () |
| 52 | { |
| 53 | uint16_t div; |
| 54 | nrk_setup_ports (); |
| 55 | nrk_setup_uart (UART_BAUDRATE_115K2); |
| 56 | |
| 57 | nrk_init (); |
| 58 | |
| 59 | nrk_led_clr (0); |
| 60 | nrk_led_clr (1); |
| 61 | nrk_led_clr (2); |
| 62 | nrk_led_clr (3); |
| 63 | |
| 64 | nrk_time_set (0, 0); |
| 65 | |
| 66 | bmac_task_config (); |
| 67 | |
| 68 | nrk_create_taskset (); |
| 69 | nrk_start (); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | void rx_task () |
| 75 | { |
| 76 | uint8_t i, len; |
| 77 | int8_t rssi, val; |
| 78 | uint8_t *local_rx_buf; |
| 79 | nrk_time_t check_period; |
| 80 | printf ("rx_task PID=%d\r\n", nrk_get_pid ()); |
| 81 | |
| 82 | // init bmac on channel 25 |
| 83 | bmac_init (15); |
| 84 | |
| 85 | // Enable AES 128 bit encryption |
| 86 | // When encryption is active, messages from plaintext |
| 87 | // source will still be received. |
| 88 | bmac_encryption_set_key(aes_key,16); |
| 89 | bmac_encryption_enable(); |
| 90 | // bmac_encryption_disable(); |
| 91 | |
| 92 | // By default the RX check rate is 100ms |
| 93 | // below shows how to change that |
| 94 | //check_period.secs=0; |
| 95 | //check_period.nano_secs=200*NANOS_PER_MS; |
| 96 | //val=bmac_set_rx_check_rate(check_period); |
| 97 | |
| 98 | // The default Clear Channel Assement RSSI threshold is -45 |
| 99 | // Setting this value higher means that you will only trigger |
| 100 | // receive with a very strong signal. Setting this lower means |
| 101 | // bmac will try to receive fainter packets. If the value is set |
| 102 | // too high or too low performance will suffer greatly. |
| 103 | // bmac_set_cca_thresh(-45); |
| 104 | |
| 105 | |
| 106 | //if(val==NRK_ERROR) nrk_kprintf( PSTR("ERROR setting bmac rate\r\n" )); |
| 107 | // This sets the next RX buffer. |
| 108 | // This can be called at anytime before releaseing the packet |
| 109 | // if you wish to do a zero-copy buffer switch |
| 110 | bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE); |
| 111 | |
| 112 | while (1) { |
| 113 | // Wait until an RX packet is received |
| 114 | val = bmac_wait_until_rx_pkt (); |
| 115 | // Get the RX packet |
| 116 | nrk_led_set (ORANGE_LED); |
| 117 | local_rx_buf = bmac_rx_pkt_get (&len, &rssi); |
| 118 | if( bmac_rx_pkt_is_encrypted()==1 ) nrk_kprintf( PSTR( "Packet Encrypted\r\n" )); |
| 119 | printf ("Got RX packet len=%d RSSI=%d [", len, rssi); |
| 120 | for (i = 0; i < len; i++) |
| 121 | printf ("%c", rx_buf[i]); |
| 122 | printf ("]\r\n"); |
| 123 | nrk_led_clr (ORANGE_LED); |
| 124 | // Release the RX buffer so future packets can arrive |
| 125 | bmac_rx_pkt_release (); |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | uint8_t ctr_cnt[4]; |
| 131 | |
| 132 | void tx_task () |
| 133 | { |
| 134 | uint8_t j, i, val, len, cnt; |
| 135 | int8_t v; |
| 136 | nrk_sig_t tx_done_signal; |
| 137 | nrk_sig_mask_t ret; |
| 138 | nrk_time_t r_period; |
| 139 | |
| 140 | printf ("tx_task PID=%d\r\n", nrk_get_pid ()); |
| 141 | |
| 142 | // Wait until the tx_task starts up bmac |
| 143 | // This should be called by all tasks using bmac that |
| 144 | // do not call bmac_init()... |
| 145 | while (!bmac_started ()) |
| 146 | nrk_wait_until_next_period (); |
| 147 | |
| 148 | |
| 149 | // Sample of using Reservations on TX packets |
| 150 | // This example allows 2 packets to be sent every 5 seconds |
| 151 | // r_period.secs=5; |
| 152 | // r_period.nano_secs=0; |
| 153 | // v=bmac_tx_reserve_set( &r_period, 2 ); |
| 154 | // if(v==NRK_ERROR) nrk_kprintf( PSTR("Error setting b-mac tx reservation (is NRK_MAX_RESERVES defined?)\r\n" )); |
| 155 | |
| 156 | |
| 157 | // Get and register the tx_done_signal if you want to |
| 158 | // do non-blocking transmits |
| 159 | tx_done_signal = bmac_get_tx_done_signal (); |
| 160 | nrk_signal_register (tx_done_signal); |
| 161 | |
| 162 | ctr_cnt[0]=0; ctr_cnt[1]=0; ctr_cnt[2]=0; ctr_cnt[3]=0; |
| 163 | cnt = 0; |
| 164 | while (1) { |
| 165 | // Build a TX packet |
| 166 | sprintf (tx_buf, "This is a test %d", cnt); |
| 167 | nrk_led_set (BLUE_LED); |
| 168 | |
| 169 | // Auto ACK is an energy efficient link layer ACK on packets |
| 170 | // If Auto ACK is enabled, then bmac_tx_pkt() will return failure |
| 171 | // if no ACK was received. In a broadcast domain, the ACK's will |
| 172 | // typically collide. To avoid this, one can use address decoding. |
| 173 | // The functions are as follows: |
| 174 | // bmac_auto_ack_enable(); |
| 175 | // bmac_auto_ack_disable(); |
| 176 | |
| 177 | // Address decoding is a way of preventing the radio from receiving |
| 178 | // packets that are not address to a particular node. This will |
| 179 | // supress ACK packets from nodes that should not automatically ACK. |
| 180 | // The functions are as follows: |
| 181 | // bmac_addr_decode_set_my_mac(uint16_t MAC_ADDR); |
| 182 | // bmac_addr_decode_dest_mac(uint16_t DST_ADDR); // 0xFFFF is broadcast |
| 183 | // bmac_addr_decode_enable(); |
| 184 | // bmac_addr_decode_disable(); |
| 185 | |
| 186 | ctr_cnt[0]=cnt; |
| 187 | if(ctr_cnt[0]==255) ctr_cnt[1]++; |
| 188 | if(ctr_cnt[1]==255) ctr_cnt[2]++; |
| 189 | if(ctr_cnt[2]==255) ctr_cnt[3]++; |
| 190 | // You need to increase the ctr on each packet to make the |
| 191 | // stream cipher not repeat. |
| 192 | bmac_encryption_set_ctr_counter(&ctr_cnt,4); |
| 193 | |
| 194 | // For blocking transmits, use the following function call. |
| 195 | // For this there is no need to register |
| 196 | val=bmac_tx_pkt(tx_buf, strlen(tx_buf)); |
| 197 | if(val==NRK_OK) cnt++; |
| 198 | else nrk_kprintf( PSTR( "NO ack or Reserve Violated!\r\n" )); |
| 199 | |
| 200 | |
| 201 | // This function shows how to transmit packets in a |
| 202 | // non-blocking manner |
| 203 | // val = bmac_tx_pkt_nonblocking(tx_buf, strlen (tx_buf)); |
| 204 | // nrk_kprintf (PSTR ("Tx packet enqueued\r\n")); |
| 205 | // This functions waits on the tx_done_signal |
| 206 | // ret = nrk_event_wait (SIG(tx_done_signal)); |
| 207 | |
| 208 | // Just check to be sure signal is okay |
| 209 | // if(ret & SIG(tx_done_signal) == 0 ) |
| 210 | // nrk_kprintf (PSTR ("TX done signal error\r\n")); |
| 211 | |
| 212 | // If you want to see your remaining reservation |
| 213 | // printf( "reserve=%d ",bmac_tx_reserve_get() ); |
| 214 | |
| 215 | // Task gets control again after TX complete |
| 216 | nrk_kprintf (PSTR ("Tx task sent data!\r\n")); |
| 217 | nrk_led_clr (BLUE_LED); |
| 218 | nrk_wait_until_next_period (); |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | void nrk_create_taskset () |
| 224 | { |
| 225 | |
| 226 | |
| 227 | RX_TASK.task = rx_task; |
| 228 | nrk_task_set_stk( &RX_TASK, rx_task_stack, NRK_APP_STACKSIZE); |
| 229 | RX_TASK.prio = 2; |
| 230 | RX_TASK.FirstActivation = TRUE; |
| 231 | RX_TASK.Type = BASIC_TASK; |
| 232 | RX_TASK.SchType = PREEMPTIVE; |
| 233 | RX_TASK.period.secs = 1; |
| 234 | RX_TASK.period.nano_secs = 0; |
| 235 | RX_TASK.cpu_reserve.secs = 1; |
| 236 | RX_TASK.cpu_reserve.nano_secs = 500 * NANOS_PER_MS; |
| 237 | RX_TASK.offset.secs = 0; |
| 238 | RX_TASK.offset.nano_secs = 0; |
| 239 | nrk_activate_task (&RX_TASK); |
| 240 | |
| 241 | TX_TASK.task = tx_task; |
| 242 | nrk_task_set_stk( &TX_TASK, tx_task_stack, NRK_APP_STACKSIZE); |
| 243 | TX_TASK.prio = 2; |
| 244 | TX_TASK.FirstActivation = TRUE; |
| 245 | TX_TASK.Type = BASIC_TASK; |
| 246 | TX_TASK.SchType = PREEMPTIVE; |
| 247 | TX_TASK.period.secs = 1; |
| 248 | TX_TASK.period.nano_secs = 0; |
| 249 | TX_TASK.cpu_reserve.secs = 1; |
| 250 | TX_TASK.cpu_reserve.nano_secs = 500 * NANOS_PER_MS; |
| 251 | TX_TASK.offset.secs = 0; |
| 252 | TX_TASK.offset.nano_secs = 0; |
| 253 | nrk_activate_task (&TX_TASK); |
| 254 | |
| 255 | |
| 256 | |
| 257 | printf ("Create done\r\n"); |
| 258 | } |
Note: See TracBrowser for help on using the browser.
