module XE1205SpiImplP {
- provides interface XE1205Fifo as Fifo @atmostonce();
- provides interface XE1205Register as Reg[uint8_t id];
+ provides interface XE1205Fifo as Fifo @atmostonce();
+ provides interface XE1205Register as Reg[uint8_t id];
- provides interface Init @atleastonce();
+ provides interface Init @atleastonce();
- provides interface Resource[ uint8_t id ];
+ provides interface Resource[ uint8_t id ];
- uses interface Resource as SpiResource;
- uses interface GeneralIO as NssDataPin;
- uses interface GeneralIO as NssConfigPin;
- uses interface SpiByte;
- uses interface SpiPacket;
+ uses interface Resource as SpiResource;
+ uses interface GeneralIO as NssDataPin;
+ uses interface GeneralIO as NssConfigPin;
+ uses interface SpiByte;
+ uses interface SpiPacket;
}
implementation {
#include "xe1205debug.h"
- enum {
- RESOURCE_COUNT = uniqueCount( "XE1205Spi.Resource" ),
- NO_HOLDER = 0xff,
- };
-
-
- bool m_resource_busy = FALSE;
- uint8_t m_requests = 0;
- uint8_t m_holder = NO_HOLDER;
-
- command error_t Init.init() {
- call NssDataPin.makeOutput();
- call NssConfigPin.makeOutput();
- call NssDataPin.set();
- call NssConfigPin.set();
- return SUCCESS;
- }
-
- async command error_t Resource.request[ uint8_t id ]() {
- atomic {
- if ( m_resource_busy )
- m_requests |= 1 << id;
- else {
- m_holder = id;
- m_resource_busy = TRUE;
- call SpiResource.request();
- }
+ enum {
+ RESOURCE_COUNT = uniqueCount( "XE1205Spi.Resource" ),
+ NO_HOLDER = 0xff,
+ };
+
+
+ bool m_resource_busy = FALSE;
+ uint8_t m_requests = 0;
+ uint8_t m_holder = NO_HOLDER;
+
+ command error_t Init.init() {
+ call NssDataPin.makeOutput();
+ call NssConfigPin.makeOutput();
+ call NssDataPin.set();
+ call NssConfigPin.set();
+ return SUCCESS;
+ }
+
+ async command error_t Resource.request[ uint8_t id ]() {
+ atomic {
+ if ( m_resource_busy )
+ m_requests |= 1 << id;
+ else {
+ m_holder = id;
+ m_resource_busy = TRUE;
+
+ call SpiResource.request();
+ }
+ }
+ return SUCCESS;
}
- return SUCCESS;
- }
- async command error_t Resource.immediateRequest[ uint8_t id ]() {
- error_t error;
- atomic {
- if ( m_resource_busy )
- return EBUSY;
- error = call SpiResource.immediateRequest();
- if ( error == SUCCESS ) {
- m_holder = id;
- m_resource_busy = TRUE;
- }
- xe1205check(9, error);
+ async command error_t Resource.immediateRequest[ uint8_t id ]() {
+ error_t error;
+ atomic {
+ if ( m_resource_busy )
+ return EBUSY;
+ error = call SpiResource.immediateRequest();
+ if ( error == SUCCESS ) {
+ m_holder = id;
+ m_resource_busy = TRUE;
+
+ }
+ xe1205check(9, error);
+ }
+ return error;
}
- return error;
- }
-
- async command error_t Resource.release[ uint8_t id ]() {
- uint8_t i;
- atomic {
- if ( m_holder != id ) {
- xe1205check(11, 1);
- return FAIL;
- }
-
- m_holder = NO_HOLDER;
- call SpiResource.release();
- if ( !m_requests ) {
- m_resource_busy = FALSE;
- }
- else {
- for ( i = m_holder + 1; ; i++ ) {
- if ( i >= RESOURCE_COUNT )
- i = 0;
- if ( m_requests & ( 1 << i ) ) {
- m_holder = i;
- m_requests &= ~( 1 << i );
- call SpiResource.request();
+
+ async command error_t Resource.release[ uint8_t id ]() {
+ uint8_t i;
+ atomic {
+ if ( m_holder != id ) {
+ xe1205check(11, 1);
+ return FAIL;
+ }
+
+ m_holder = NO_HOLDER;
+ call SpiResource.release();
+ if ( !m_requests ) {
+ m_resource_busy = FALSE;
+
+ }
+ else {
+ for ( i = m_holder + 1; ; i++ ) {
+ if ( i >= RESOURCE_COUNT )
+ i = 0;
+ if ( m_requests & ( 1 << i ) ) {
+ m_holder = i;
+ m_requests &= ~( 1 << i );
+ call SpiResource.request();
+ return SUCCESS;
+ }
+ }
+ }
return SUCCESS;
- }
}
- }
- return SUCCESS;
}
- }
- async command uint8_t Resource.isOwner[ uint8_t id ]() {
- atomic return (m_holder == id);
- }
+ async command uint8_t Resource.isOwner[ uint8_t id ]() {
+ atomic return (m_holder == id);
+ }
- event void SpiResource.granted() {
- uint8_t holder;
- atomic holder = m_holder;
- signal Resource.granted[ holder ]();
- }
+ event void SpiResource.granted() {
+ uint8_t holder;
+ atomic holder = m_holder;
+ signal Resource.granted[ holder ]();
+ }
- default event void Resource.granted[ uint8_t id ]() {
- }
+ default event void Resource.granted[ uint8_t id ]() {
+ }
- async command error_t Fifo.write(uint8_t* data, uint8_t length) {
- error_t status;
+ async command error_t Fifo.write(uint8_t* data, uint8_t length) __attribute__ ((noinline)){
-#if 1
- if (call NssDataPin.get() != TRUE || call NssConfigPin.get() != TRUE)
- xe1205check(8, 1);
+#if 0
+ if (call NssDataPin.get() != TRUE || call NssConfigPin.get() != TRUE)
+ xe1205check(8, 1);
#endif
- call NssDataPin.clr();
- call NssConfigPin.set();
- status = call SpiPacket.send(data, NULL, length);
- if (status != SUCCESS) {
- xe1205check(1, status);
- call NssDataPin.set();
- return status;
- }
- return SUCCESS;
- }
-
- async event void SpiPacket.sendDone(uint8_t* tx_buf, uint8_t* rx_buf,
- uint16_t len, error_t error)
- {
-
- xe1205check(2, error);
-#if 1
- if (call NssConfigPin.get() != TRUE) xe1205check(4, 1);
- if (call NssDataPin.get() != FALSE) xe1205check(12, 1);
-#endif
- call NssDataPin.set();
- if (tx_buf)
- signal Fifo.writeDone(error);
- else
- signal Fifo.readDone(error);
- }
+ call SpiPacket.send(data, NULL, length);
- async command error_t Fifo.read(uint8_t* data, uint8_t length) {
- error_t status;
+ return SUCCESS;
+ }
-#if 1
- if (call NssDataPin.get() != TRUE || call NssConfigPin.get() != TRUE)
- xe1205check(5, 1);
+ async event void SpiPacket.sendDone(uint8_t* tx_buf, uint8_t* rx_buf,
+ uint16_t len, error_t error)
+ {
+
+#if 0
+ if (call NssConfigPin.get() != TRUE) xe1205check(4, 1);
+ if (call NssDataPin.get() != FALSE) xe1205check(12, 1);
#endif
+ TOSH_SET_NSS_DATA_PIN();
+ if (tx_buf) {
- call NssDataPin.clr();
+ signal Fifo.writeDone(error);
+ } else
+ signal Fifo.readDone(error);
+ }
- status = call SpiPacket.send(NULL, data, length);
- if (status != SUCCESS) {
- xe1205check(3, status);
- call NssDataPin.set();
- return status;
- }
- return SUCCESS;
- }
+ async command error_t Fifo.read(uint8_t* data, uint8_t length) {
+ error_t status;
+
+#if 0
+ if (call NssDataPin.get() != TRUE || call NssConfigPin.get() != TRUE)
+ xe1205check(5, 1);
+#endif
- async command void Reg.read[uint8_t addr](uint8_t* data)
- {
+ TOSH_CLR_NSS_DATA_PIN();
+ status = call SpiPacket.send(NULL, data, length);
+ if (status != SUCCESS) {
+ xe1205check(3, status);
+ call NssDataPin.set();
+ return status;
+ }
+ return SUCCESS;
+ }
+
+ async command void Reg.read[uint8_t addr](uint8_t* data)
+ {
#if 1
- if (call NssDataPin.get() != TRUE || call NssConfigPin.get() != TRUE)
- xe1205check(6, 1);
+ if (call NssDataPin.get() != TRUE || call NssConfigPin.get() != TRUE)
+ xe1205check(6, 1);
#endif
- call NssDataPin.set();
- call NssConfigPin.clr();
- call SpiByte.write(XE1205_READ(addr));
- *data = call SpiByte.write(0);
- call NssConfigPin.set();
- }
+ call NssDataPin.set();
+ call NssConfigPin.clr();
+ call SpiByte.write(XE1205_READ(addr));
+ *data = call SpiByte.write(0);
+ call NssConfigPin.set();
+ }
- async command void Reg.write[uint8_t addr](uint8_t data)
- {
+ async command void Reg.write[uint8_t addr](uint8_t data)
+ {
#if 1
- if (call NssDataPin.get() != TRUE || call NssConfigPin.get() != TRUE)
- xe1205check(7, 1);
+ if (call NssDataPin.get() != TRUE || call NssConfigPin.get() != TRUE)
+ xe1205check(7, 1);
#endif
- call NssDataPin.set();
- call NssConfigPin.clr();
- call SpiByte.write(XE1205_WRITE(addr));
- call SpiByte.write(data);
- call NssConfigPin.set();
- }
+ call NssDataPin.set();
+ call NssConfigPin.clr();
+ call SpiByte.write(XE1205_WRITE(addr));
+ call SpiByte.write(data);
+ call NssConfigPin.set();
+ }
- default async event void Fifo.readDone(error_t error) {}
- default async event void Fifo.writeDone(error_t error) {}
+ default async event void Fifo.readDone(error_t error) {}
+ default async event void Fifo.writeDone(error_t error) {}
}