File: Stm25pBlockP.nc

package info (click to toggle)
tinyos 2.1.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 47,476 kB
  • ctags: 36,607
  • sloc: ansic: 63,646; cpp: 14,974; java: 10,358; python: 5,215; makefile: 1,724; sh: 902; asm: 597; xml: 392; perl: 74; awk: 46
file content (238 lines) | stat: -rw-r--r-- 7,826 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Copyright (c) 2005-2006 Arch Rock Corporation
 * 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 the Arch Rock Corporation 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 THE
 * ARCHED ROCK 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 Jonathan Hui <jhui@archrock.com>
 * @version $Revision: 1.4 $ $Date: 2006-12-12 18:23:12 $
 */

module Stm25pBlockP {

  provides interface BlockRead as Read[ uint8_t id ];
  provides interface BlockWrite as Write[ uint8_t id ];
  provides interface StorageMap[ uint8_t id ];

  uses interface Stm25pSector as Sector[ uint8_t id ];
  uses interface Resource as ClientResource[ uint8_t id ];
  uses interface Leds;

}

implementation {

  enum {
    NUM_BLOCKS = uniqueCount( "Stm25p.Block" ),
  };
  
  typedef enum {
    S_IDLE,
    S_READ,
    S_CRC,
    S_WRITE,
    S_SYNC,
    S_ERASE,
  } stm25p_block_req_t;
  
  typedef struct stm25p_block_state_t {
    storage_addr_t addr;
    void* buf;
    storage_len_t len;
    stm25p_block_req_t req;
  } stm25p_block_state_t;
  
  stm25p_block_state_t m_block_state[ NUM_BLOCKS ];
  stm25p_block_state_t m_req;
  
  error_t newRequest( uint8_t client );
  void signalDone( uint8_t id, uint16_t crc, error_t error );
  
  command storage_addr_t StorageMap.getPhysicalAddress[ uint8_t id ]( storage_addr_t addr ) {
    return call Sector.getPhysicalAddress[ id ]( addr );
  }
  
  command storage_len_t Read.getSize[ uint8_t id ]() {
    return ( (storage_len_t)call Sector.getNumSectors[ id ]() 
	     << STM25P_SECTOR_SIZE_LOG2 );
  }
  
  command error_t Read.read[ uint8_t id ]( storage_addr_t addr, void* buf,
					   storage_len_t len ) {
    m_req.req = S_READ;
    m_req.addr = addr;
    m_req.buf = buf;
    m_req.len = len;
    return newRequest( id );
  }
  
  command error_t Read.computeCrc[ uint8_t id ]( storage_addr_t addr,
						 storage_len_t len,
						 uint16_t crc ) {
    m_req.req = S_CRC;
    m_req.addr = addr;
    m_req.buf = (void*)crc;
    m_req.len = len;
    return newRequest( id );
  }
  
  command error_t Write.write[ uint8_t id ]( storage_addr_t addr, void* buf, 
					     storage_len_t len ) {
    m_req.req = S_WRITE;
    m_req.addr = addr;
    m_req.buf = buf;
    m_req.len = len;
    return newRequest( id );
  }
  
  command error_t Write.sync[ uint8_t id ]() {
    m_req.req = S_SYNC;
    return newRequest( id );
  }
  
  command error_t Write.erase[ uint8_t id ]() {
    m_req.req = S_ERASE;
    return newRequest( id );
  }
  
  error_t newRequest( uint8_t client ) {
    
    if ( m_block_state[ client ].req != S_IDLE )
      return FAIL;

    call ClientResource.request[ client ]();
    m_block_state[ client ] = m_req;
    
    return SUCCESS;
    
  }
  
  event void ClientResource.granted[ uint8_t id ]() {
    
    switch( m_block_state[ id ].req ) {
    case S_READ:
      call Sector.read[ id ]( m_block_state[ id ].addr, 
			      m_block_state[ id ].buf, 
			      m_block_state[ id ].len );
      break;
    case S_CRC:
      call Sector.computeCrc[ id ]( (uint16_t)m_block_state[ id ].buf, 
				    m_block_state[ id ].addr, 
				    m_block_state[ id ].len );
      break;
    case S_WRITE:
      call Sector.write[ id ]( m_block_state[ id ].addr, 
			       m_block_state[ id ].buf, 
			       m_block_state[ id ].len );
      break;
    case S_ERASE:
      call Sector.erase[ id ]( 0, call Sector.getNumSectors[ id ]() );
      break;
    case S_SYNC:
      signalDone( id, 0, SUCCESS );
      break;
    case S_IDLE:
      break;
    }
    
  }
  
  event void Sector.readDone[ uint8_t id ]( stm25p_addr_t addr, uint8_t* buf, 
					    stm25p_len_t len, error_t error ) {
    signalDone( id, 0, error );
  }
  
  event void Sector.writeDone[ uint8_t id ]( stm25p_addr_t addr, uint8_t* buf, 
					     stm25p_len_t len, error_t error ){
    signalDone( id, 0, error );
  }
  
  event void Sector.eraseDone[ uint8_t id ]( uint8_t sector,
					     uint8_t num_sectors,
					     error_t error ) {
    signalDone( id, 0, error );
  }
  
  event void Sector.computeCrcDone[ uint8_t id ]( stm25p_addr_t addr, 
						  stm25p_len_t len,
						  uint16_t crc,
						  error_t error ) {
    signalDone( id, crc, error );
  }
  
  void signalDone( uint8_t id, uint16_t crc, error_t error ) {
    
    stm25p_block_req_t req = m_block_state[ id ].req;    
    
    call ClientResource.release[ id ]();
    m_block_state[ id ].req = S_IDLE;
    switch( req ) {
    case S_READ:
      signal Read.readDone[ id ]( m_block_state[ id ].addr, 
				  m_block_state[ id ].buf,
				  m_block_state[ id ].len, error );  
      break;
    case S_CRC:
      signal Read.computeCrcDone[ id ]( m_block_state[ id ].addr, 
					m_block_state[ id ].len, crc, error );
      break;
    case S_WRITE:
      signal Write.writeDone[ id ]( m_block_state[ id ].addr, 
				    m_block_state[ id ].buf,
				    m_block_state[ id ].len, error );
      break;
    case S_SYNC:
      signal Write.syncDone[ id ]( error );
      break;
    case S_ERASE:
      signal Write.eraseDone[ id ]( error );
      break;
    case S_IDLE:
      break;
    }
    
  }
  
  default event void Read.readDone[ uint8_t id ]( storage_addr_t addr, void* buf, storage_len_t len, error_t error ) {}
  default event void Read.computeCrcDone[ uint8_t id ]( storage_addr_t addr, storage_len_t len, uint16_t crc, error_t error ) {}
  default event void Write.writeDone[ uint8_t id ]( storage_addr_t addr, void* buf, storage_len_t len, error_t error ) {}
  default event void Write.eraseDone[ uint8_t id ]( error_t error ) {}
  default event void Write.syncDone[ uint8_t id ]( error_t error ) {}
  
  default command storage_addr_t Sector.getPhysicalAddress[ uint8_t id ]( storage_addr_t addr ) { return 0xffffffff; }
  default command uint8_t Sector.getNumSectors[ uint8_t id ]() { return 0; }
  default command error_t Sector.read[ uint8_t id ]( stm25p_addr_t addr, uint8_t* buf, stm25p_len_t len ) { return FAIL; }
  default command error_t Sector.write[ uint8_t id ]( stm25p_addr_t addr, uint8_t* buf, stm25p_len_t len ) { return FAIL; }
  default command error_t Sector.erase[ uint8_t id ]( uint8_t sector, uint8_t num_sectors ) { return FAIL; }
  default command error_t Sector.computeCrc[ uint8_t id ]( uint16_t crc, storage_addr_t addr, storage_len_t len ) { return FAIL; }
  default async command error_t ClientResource.request[ uint8_t id ]() { return FAIL; }
  default async command error_t ClientResource.release[ uint8_t id ]() { return FAIL; }
  
}