📄 halcc2420m.nc
字号:
/* * Copyright (c) 2007 University of Copenhagen * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the * distribution. * - Neither the name of University of Copenhagen nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. *//** * * @author Marcus Chang <marcus@diku.dk> * @author Klaus S. Madsen <klaussm@diku.dk> */module HALCC2420M { provides { interface HALCC2420; interface Init; interface StdControl as HALCC2420Control; } uses { interface StdControl as HPLCC2420Control; interface HPLCC2420; interface HPLCC2420RAM; interface HPLCC2420FIFO; interface HPLCC2420Status; interface GpioInterrupt as MSP430Interrupt; interface Spi; interface BusArbitration; interface StdOut; interface HPL1wire;// interface HPL16MhzCrystal; }}implementation {#include "HPLSpi.h"#include "hplcc2420.h"#define MCU_ENABLE_IRQ(x) call MSP430Interrupt.enableFallingEdge();#define MCU_DISABLE_IRQ(x) call MSP430Interrupt.disable();#define CC2420_READ_STATUS(x) call HPLCC2420.cmd(CC_REG_SNOP)#define CC2420_OSCILLATOR_DISABLE(x) call HPLCC2420.cmd(CC_REG_SXOSCOFF)#define CRYSTAL_OSCILLATOR_ENABLE(x) P1OUT &= ~0x40; /* turn on crystal oscillator */#define CRYSTAL_OSCILLATOR_DISABLE(X) P1OUT |= 0x40; /* turn off crystal oscillator */#define debug_fsm(x) //call StdOut.print("FSM: "); call StdOut.printHexword(call HPLCC2420.read(CC_REG_FSMSTATE)); call StdOut.print("\r\n"); void CC2420_SET_PANID(uint16_t panid); void CC2420_SET_SHORTADDR(uint16_t shortAddr); void CC2420_SET_IEEEADDR(ieee_mac_addr_t extAddress); void CC2420_RX_ENABLE(); void CC2420_RX_DISABLE(); int8_t CC2420_CHANNEL_SET(uint8_t channel); int8_t CC2420_POWER_SET(uint8_t new_power); bool CC2420_OSCILLATOR_ENABLE(); void CC2420_CONTROL_SET(); void CC2420_RESET(); void CC2420_TX_WAIT(); task void startTask(); task void stopTask(); task void initTask(); task void transmitTask(); task void setChannelTask(); task void setTransmitPowerTask(); task void rxEnableTask(); task void rxDisableTask(); task void addressEnableTask(); task void addressDisableTask(); task void setShortAddressTask(); task void setPanAddressTask();/**************************************************************************************************** Queue related**************************************************************************************************/#define QUEUE_SIZE 0x0C enum { TASK_START = 0x01, TASK_STOP = 0x02, TASK_INIT = 0x03, TASK_SET_POWER = 0x04, TASK_SET_CHANNEL = 0x05, TASK_TRANSMIT = 0x06, TASK_RX_ENABLE = 0x07, TASK_RX_DISABLE = 0x08, TASK_ADDR_ENABLE = 0x09, TASK_ADDR_DISABLE = 0x0A, TASK_SET_SHORT = 0x0B, TASK_SET_PANID = 0x0C, /* High-priority task - skip the queue */ /* TASK_FLUSH_BUFFER = 0x0C, */ /* TASK_RECEIVED_PACKET = 0x0D, */ }; bool taskPosted[QUEUE_SIZE]; uint8_t queue[QUEUE_SIZE]; uint8_t first, last, recent; bool queuePut(uint8_t id) { if ( (queue[id] != 0) || (last == id) || (id > QUEUE_SIZE - 1) ) return FALSE; if (first == 0) { first = id; last = id; } else { if (recent == id) { queue[id] = first; first = id; } else { queue[last] = id; last = id; } } return TRUE; } uint8_t queueGet() { uint8_t retval; retval = first; first = queue[retval]; queue[retval] = 0; if (first == 0) { last = 0; recent = 0; } else { recent = retval; } return retval; } bool queueEmpty() { if (first == 0) { return TRUE; } else { return FALSE; } } void postNextTask() { uint8_t id; id = queueGet(); switch(id) { case TASK_START: post startTask(); break; case TASK_STOP: post stopTask(); break; case TASK_INIT: post initTask(); break; case TASK_SET_POWER: post setTransmitPowerTask(); break; case TASK_SET_CHANNEL: post setChannelTask(); break; case TASK_TRANSMIT: post transmitTask(); break; case TASK_RX_ENABLE: post rxEnableTask(); break; case TASK_RX_DISABLE: post rxDisableTask(); break; case TASK_ADDR_ENABLE: post addressEnableTask(); break; case TASK_ADDR_DISABLE: post addressDisableTask(); break; case TASK_SET_SHORT: post setShortAddressTask(); break; case TASK_SET_PANID: post setPanAddressTask(); break; default: break; } return; }/**************************************************************************************************** Bus arbitration**************************************************************************************************/ event error_t BusArbitration.busFree() { if (!queueEmpty()) postNextTask(); return SUCCESS; }/**************************************************************************************************** StdControl**************************************************************************************************/ // uint16_t mcr0 = 0x0AE2, iocfg0 = 0x007F; uint16_t mcr0 = 0x0000, iocfg0 = 0x007F; MDMCTRL0_t * mcr0Ptr; ieee_mac_addr_t ieeeAddress; mac_addr_t shortAddress, panid; bool radioOn; bool rxEnabled; uint8_t receivedPacket[128]; uint8_t * receivedPacketPtr; /********************************************************************** * Init *********************************************************************/ command error_t Init.init() { receivedPacketPtr = receivedPacket; mcr0Ptr = (MDMCTRL0_t *) &mcr0; /* No auto Ack. Preamble length: 3 zero bytes. */ mcr0Ptr->preamble_length = LEADING_ZERO_BYTES_3; mcr0Ptr->autoack = FALSE; mcr0Ptr->autocrc = TRUE; mcr0Ptr->cca_mode = 3; mcr0Ptr->cca_hyst = CCA_HYST_2DB; mcr0Ptr->adr_decode = TRUE; mcr0Ptr->pan_coordinator = FALSE; mcr0Ptr->reserved_frame_mode = FALSE; post initTask(); return SUCCESS; } task void initTask() { b1w_reg devices[2]; bool foundId = FALSE; uint8_t n_devices = 0, retry = 0, i; uint16_t tmp; /*********************************************** ** Reserve bus (1-wire and SPI) ***********************************************/ if (call BusArbitration.getBus() != SUCCESS) { queuePut(TASK_INIT); return; } /****************************************** ** 1-wire related ** ******************************************/ /* read lowest unique ID from device(s) */ while(!n_devices && (retry++ < 5)) { call HPL1wire.enable(); n_devices = call HPL1wire.search(devices, 2); call HPL1wire.disable(); } /* use ID in IEEE address if successfull */ for (i = 0; i < n_devices; i++) { if ((devices[i][0] != 0xFF) && (devices[i][1] != 0xFF) && (devices[i][2] != 0xFF) && (devices[i][3] != 0xFF) && (devices[i][4] != 0xFF) && (devices[i][5] != 0xFF) && (devices[i][6] != 0xFF) && (devices[i][7] != 0xFF) ) { /* ieeeAddress[3] = devices[0][2]; ieeeAddress[4] = devices[0][3]; ieeeAddress[5] = devices[0][4]; ieeeAddress[6] = devices[0][5]; ieeeAddress[7] = devices[0][6]; */ memcpy( &(ieeeAddress[3]), &(devices[i][2]), 5); foundId = TRUE; } } if (!foundId) { /* if no unique ID use TOS instead */ ieeeAddress[3] = 0; ieeeAddress[4] = 0; ieeeAddress[5] = 0; ieeeAddress[6] = TOS_NODE_ID >> 8; ieeeAddress[7] = TOS_NODE_ID; } /****************************************** ** Enable SPI ** ******************************************/ call Spi.enable(BUS_STE | BUS_PHASE_INVERT, 1); /* reset radio with SPI */ CC2420_RESET(); /* turn on oscillator, enabling RAM access */ CC2420_OSCILLATOR_ENABLE(); /****************************************** ** RAM related ** ******************************************/ /* read and add manufacture ID to IEEE */ tmp = call HPLCC2420.read(CC_REG_MANFIDH); ieeeAddress[0] = tmp >> 8; tmp = call HPLCC2420.read(CC_REG_MANFIDL); ieeeAddress[1] = tmp >> 8; ieeeAddress[2] = tmp; /* Write IEEE adress to radio */ CC2420_SET_IEEEADDR(ieeeAddress); /* Write lowest bits as default shortAdr */ shortAddress = ieeeAddress[6]; shortAddress = (shortAddress << 8) + ieeeAddress[7]; CC2420_SET_SHORTADDR(shortAddress); /* Write scan address in PAN ID */ panid = shortAddress; CC2420_SET_PANID(panid); /* disable oscillator (ram access) */ CC2420_OSCILLATOR_DISABLE(); /* disable crystal */ CRYSTAL_OSCILLATOR_DISABLE(); radioOn = FALSE; rxEnabled = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -