]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/msp430/adc12/AdcStreamP.nc
Merge TinyOS 2.1.1 into master.
[tinyos-2.x.git] / tos / chips / msp430 / adc12 / AdcStreamP.nc
index af14a4779ad8d84f35ac91ad4c8647ff7469c190..a7f623865782e034497c650cd07825ea98949ec8 100644 (file)
@@ -43,7 +43,7 @@
  */
 #include "Timer.h"
 
-module AdcStreamP {
+module AdcStreamP @safe() {
   provides {
     interface Init @atleastonce();
     interface ReadStream<uint16_t>[uint8_t client];
@@ -67,13 +67,15 @@ implementation {
   /* Stream data */
   struct list_entry_t {
     uint16_t count;
-    struct list_entry_t *next;
+    struct list_entry_t * ONE_NOK next;
   };
   struct list_entry_t *bufferQueue[NSTREAM];
-  struct list_entry_t **bufferQueueEnd[NSTREAM];
-  uint16_t *lastBuffer, lastCount;
+  struct list_entry_t * ONE_NOK * bufferQueueEnd[NSTREAM];
+  uint16_t * COUNT_NOK(lastCount) lastBuffer, lastCount;
 
-  norace uint16_t *buffer, *pos, count;
+  norace uint16_t count;
+  norace uint16_t * COUNT_NOK(count) buffer; 
+  norace uint16_t * BND_NOK(buffer, buffer+count) pos;
   norace uint32_t now, period;
   norace bool periodModified;
 
@@ -91,12 +93,13 @@ implementation {
     call SingleChannel.getData[client]();
   }
 
-  command error_t ReadStream.postBuffer[uint8_t c](uint16_t *buf, uint16_t n) {
+  error_t postBuffer(uint8_t c, uint16_t *buf, uint16_t n)
+  {
     if (n < sizeof(struct list_entry_t))
       return ESIZE;
     atomic
     {
-      struct list_entry_t *newEntry = (struct list_entry_t *)buf;
+      struct list_entry_t * ONE newEntry = TCAST(struct list_entry_t * ONE, buf);
 
       if (!bufferQueueEnd[c]) // Can't post right now.
         return FAIL;
@@ -109,6 +112,10 @@ implementation {
     return SUCCESS;
   }
 
+  command error_t ReadStream.postBuffer[uint8_t c](uint16_t *buf, uint16_t n) {
+    return postBuffer(c, buf, n);
+  }
+
   task void readStreamDone() {
     uint8_t c = client;
     uint32_t actualPeriod = period;
@@ -131,8 +138,10 @@ implementation {
     uint8_t c = client;
 
     atomic entry = bufferQueue[c];
-    for (; entry; entry = entry->next)
-      signal ReadStream.bufferDone[c](FAIL, (uint16_t *)entry, entry->count);
+    for (; entry; entry = entry->next) {
+      uint16_t tmp_count __DEPUTY_UNUSED__ = entry->count;
+      signal ReadStream.bufferDone[c](FAIL, TCAST(uint16_t * COUNT_NOK(tmp_count),entry), entry->count);
+    }
 
     atomic
     {
@@ -179,11 +188,14 @@ implementation {
       }
       else
       {
+        uint16_t tmp_count;
         bufferQueue[client] = entry->next;
         if (!bufferQueue[client])
           bufferQueueEnd[client] = &bufferQueue[client];
-        pos = buffer = (uint16_t *)entry;
+       pos = buffer = NULL;
         count = entry->count;
+        tmp_count = count;
+       pos = buffer = TCAST(uint16_t * COUNT_NOK(tmp_count), entry);
         if (startNextAlarm)
           nextAlarm();
         return SUCCESS;
@@ -197,8 +209,12 @@ implementation {
       msp430adc12_channel_config_t config = *call AdcConfigure.getConfiguration[c]();
       config.sampcon_ssel = SAMPCON_SOURCE_SMCLK; // assumption: SMCLK runs at 1 MHz
       config.sampcon_id = SAMPCON_CLOCK_DIV_1; 
-      call SingleChannel.configureMultiple[c]( &config, pos, count, period);
-      call SingleChannel.getData[c]();
+      if (call SingleChannel.configureMultiple[c]( &config, pos, count, period) == SUCCESS)
+        call SingleChannel.getData[c]();
+      else {
+        postBuffer(c, pos, count);
+        post readStreamFail();
+      }
     }
   }
 
@@ -236,7 +252,7 @@ implementation {
     else
     {
       *pos++ = data;
-      if (!--count)
+      if (pos == buffer + count)
       {
         atomic
         {
@@ -249,8 +265,8 @@ implementation {
           }
           else
           {
+           lastCount = count;
             lastBuffer = buffer;
-            lastCount = pos - buffer;
           }
         }
         post bufferDone();