+++ /dev/null
-/*\r
- * Copyright (c) 2005-2006 Arch Rock Corporation\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- * - Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * - Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the\r
- * distribution.\r
- * - Neither the name of the Arch Rock Corporation nor the names of\r
- * its contributors may be used to endorse or promote products derived\r
- * from this software without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE\r
- * ARCHED ROCK OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\r
- * OF THE POSSIBILITY OF SUCH DAMAGE\r
- */\r
-\r
-/**\r
- * @author Jonathan Hui <jhui@archrock.com>\r
- * @version $Revision$ $Date$\r
- */\r
-\r
-module CC2420SpiImplP {\r
-\r
- provides interface Resource[ uint8_t id ];\r
- provides interface CC2420Fifo as Fifo[ uint8_t id ];\r
- provides interface CC2420Ram as Ram[ uint16_t id ];\r
- provides interface CC2420Register as Reg[ uint8_t id ];\r
- provides interface CC2420Strobe as Strobe[ uint8_t id ];\r
-\r
- uses interface Resource as SpiResource;\r
- uses interface SpiByte;\r
- uses interface SpiPacket;\r
- uses interface Leds;\r
-\r
-}\r
-\r
-implementation {\r
-\r
- enum {\r
- RESOURCE_COUNT = uniqueCount( "CC2420Spi.Resource" ),\r
- NO_HOLDER = 0xff,\r
- };\r
-\r
- norace uint16_t m_addr;\r
- bool m_resource_busy = FALSE;\r
- uint8_t m_requests = 0;\r
- uint8_t m_holder = NO_HOLDER;\r
-\r
- default event void Resource.granted[ uint8_t id ]() {\r
- }\r
-\r
- async command error_t Resource.request[ uint8_t id ]() {\r
- atomic {\r
- if ( m_resource_busy )\r
- m_requests |= 1 << id;\r
- else {\r
- m_holder = id;\r
- m_resource_busy = TRUE;\r
- call SpiResource.request();\r
- }\r
- }\r
- return SUCCESS;\r
- }\r
- \r
- async command error_t Resource.immediateRequest[ uint8_t id ]() {\r
- error_t error;\r
- atomic {\r
- if ( m_resource_busy )\r
- return EBUSY;\r
- error = call SpiResource.immediateRequest();\r
- if ( error == SUCCESS ) {\r
- m_holder = id;\r
- m_resource_busy = TRUE;\r
- }\r
- }\r
- return error;\r
- }\r
-\r
- async command error_t Resource.release[ uint8_t id ]() {\r
- uint8_t i;\r
- atomic {\r
- if ( m_holder != id )\r
- return FAIL;\r
- m_holder = NO_HOLDER;\r
- call SpiResource.release();\r
- if ( !m_requests ) {\r
- m_resource_busy = FALSE;\r
- }\r
- else {\r
- for ( i = m_holder + 1; ; i++ ) {\r
- if ( i >= RESOURCE_COUNT )\r
- i = 0;\r
- if ( m_requests & ( 1 << i ) ) {\r
- m_holder = i;\r
- m_requests &= ~( 1 << i );\r
- call SpiResource.request();\r
- return SUCCESS;\r
- }\r
- }\r
- }\r
- return SUCCESS;\r
- }\r
- }\r
- \r
- async command uint8_t Resource.isOwner[ uint8_t id ]() {\r
- atomic return m_holder == id;\r
- }\r
-\r
- event void SpiResource.granted() {\r
- uint8_t holder;\r
- atomic holder = m_holder;\r
- signal Resource.granted[ holder ]();\r
- }\r
-\r
- async command cc2420_status_t Fifo.beginRead[ uint8_t addr ]( uint8_t* data, \r
- uint8_t len ) {\r
- \r
- cc2420_status_t status;\r
- \r
- m_addr = addr | 0x40;\r
- \r
- status = call SpiByte.write( m_addr );\r
- call Fifo.continueRead[ addr ]( data, len );\r
- \r
- return status;\r
- \r
- }\r
-\r
- async command error_t Fifo.continueRead[ uint8_t addr ]( uint8_t* data,\r
- uint8_t len ) {\r
- call SpiPacket.send( NULL, data, len );\r
- return SUCCESS;\r
- }\r
-\r
- async command cc2420_status_t Fifo.write[ uint8_t addr ]( uint8_t* data, \r
- uint8_t len ) {\r
-\r
- uint8_t status;\r
-\r
- m_addr = addr;\r
-\r
- status = call SpiByte.write( m_addr );\r
- call SpiPacket.send( data, NULL, len );\r
-\r
- return status;\r
-\r
- }\r
-\r
- async command cc2420_status_t Ram.read[ uint16_t addr ]( uint8_t offset,\r
- uint8_t* data, \r
- uint8_t len ) {\r
-\r
- cc2420_status_t status;\r
-\r
- addr += offset;\r
-\r
- call SpiByte.write( addr | 0x80 );\r
- status = call SpiByte.write( ( ( addr >> 1 ) & 0xc0 ) | 0x20 );\r
- for ( ; len; len-- )\r
- *data++ = call SpiByte.write( 0 );\r
-\r
- return status;\r
-\r
- }\r
-\r
- async event void SpiPacket.sendDone( uint8_t* tx_buf, uint8_t* rx_buf, \r
- uint16_t len, error_t error ) {\r
- if ( m_addr & 0x40 )\r
- signal Fifo.readDone[ m_addr & ~0x40 ]( rx_buf, len, error );\r
- else\r
- signal Fifo.writeDone[ m_addr ]( tx_buf, len, error );\r
- }\r
-\r
- async command cc2420_status_t Ram.write[ uint16_t addr ]( uint8_t offset,\r
- uint8_t* data, \r
- uint8_t len ) {\r
-\r
- cc2420_status_t status = 0;\r
-\r
- addr += offset;\r
-\r
- call SpiByte.write( addr | 0x80 );\r
- call SpiByte.write( ( addr >> 1 ) & 0xc0 );\r
- for ( ; len; len-- )\r
- status = call SpiByte.write( *data++ );\r
-\r
- return status;\r
-\r
- }\r
-\r
- async command cc2420_status_t Reg.read[ uint8_t addr ]( uint16_t* data ) {\r
-\r
- cc2420_status_t status;\r
- \r
- status = call SpiByte.write( addr | 0x40 );\r
- *data = (uint16_t)call SpiByte.write( 0 ) << 8;\r
- *data |= call SpiByte.write( 0 );\r
- \r
- return status;\r
-\r
- }\r
-\r
- async command cc2420_status_t Reg.write[ uint8_t addr ]( uint16_t data ) {\r
-\r
- call SpiByte.write( addr );\r
- call SpiByte.write( data >> 8 );\r
- return call SpiByte.write( data & 0xff );\r
-\r
- }\r
-\r
- async command cc2420_status_t Strobe.strobe[ uint8_t addr ]() {\r
- return call SpiByte.write( addr );\r
- }\r
-\r
- default async event void Fifo.readDone[ uint8_t addr ]( uint8_t* rx_buf, uint8_t rx_len, error_t error ) {}\r
- default async event void Fifo.writeDone[ uint8_t addr ]( uint8_t* tx_buf, uint8_t tx_len, error_t error ) {}\r
-\r
-}\r