Subversion Repositories svn.mios32

Rev

Rev 674 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
611 this 1
/*
2
 * Bankstick r/w check
3
 * RUNING THIS APPLICATION WILL DESTROY ALL DATA ON YOUR BANKSTICKS!!!
4
 *
5
 * ==========================================================================
6
 *
7
 *  Copyright (C) 2009 Matthias M├Ąchler (maechler@mm-computing.ch / thismaechler@gmx.ch)
8
 *  Licensed for personal non-commercial use only.
9
 *  All other rights reserved.
10
 *
11
 * ==========================================================================
12
 */
13
 
14
/////////////////////////////////////////////////////////////////////////////
15
// Include files
16
/////////////////////////////////////////////////////////////////////////////
17
 
18
#include <mios32.h>
19
#include "app.h"
20
#include <FreeRTOS.h>
21
#include <task.h>
22
#include <queue.h>
643 this 23
#include <fram.h>
611 this 24
 
25
/////////////////////////////////////////////////////////////////////////////
26
// Local defines
27
/////////////////////////////////////////////////////////////////////////////
28
 
29
#define PRIORITY_TASK_DISPLAY  ( tskIDLE_PRIORITY + 1 )
30
 
643 this 31
#ifndef BS_CHECK_NUM_DEVICES
32
#define BS_CHECK_NUM_DEVICES 5
611 this 33
#endif
34
 
643 this 35
// data block size (max. 64 if BS_CHECK_USE_FRAM_LAYER==0)
36
#ifndef BS_CHECK_DATA_BLOCKSIZE
37
#define BS_CHECK_DATA_BLOCKSIZE 64
611 this 38
#endif
39
 
643 this 40
 
41
// data blocks per device
42
#ifndef BS_CHECK_NUM_BLOCKS_PER_DEVICE
43
#define BS_CHECK_NUM_BLOCKS_PER_DEVICE 1024
44
#endif
45
 
46
// fram layer usage
47
#ifndef BS_CHECK_USE_FRAM_LAYER
48
#define BS_CHECK_USE_FRAM_LAYER 0
49
#endif
50
 
611 this 51
// display messages
52
#define DISPLAY_TASK_DELAYMS 100
53
#define DISPLAY_TASK_MSG_COUNTDOWN_STARTVALUE 2
54
 
55
// error codes
56
#define BS_CHECK_ERROR_INIT 1
57
#define BS_CHECK_ERROR_AVAILABLE 2
58
#define BS_CHECK_ERROR_WRITE 4
59
#define BS_CHECK_ERROR_CHECK_WRITE_FINISHED 5
60
#define BS_CHECK_ERROR_READ 6
61
#define BS_CHECK_ERROR_COMPARE 7
62
 
63
// phases
64
#define BS_CHECK_PHASE_STARTWAIT 1
65
#define BS_CHECK_PHASE_START 2
66
#define BS_CHECK_PHASE_INIT 3
67
#define BS_CHECK_PHASE_WRITE 4
68
#define BS_CHECK_PHASE_READ 5
69
#define BS_CHECK_PHASE_FINISHED 16
70
 
643 this 71
 
72
// limit blocksize for banksticks to 64 bytes
73
#if (BS_CHECK_USE_FRAM_LAYER == 0 && BS_CHECK_DATA_BLOCKSIZE > 64)
74
#define BS_CHECK_DATA_BLOCKSIZE 64
75
#endif
76
 
77
 
619 this 78
// calculation of a buffer byte in respect to current bankstick, buffer and byte(i)
643 this 79
#define BS_CHECK_BUFFER_BYTE_VALUE ((run + bs + block+(block/3) + i) % 256)
619 this 80
 
611 this 81
/////////////////////////////////////////////////////////////////////////////
82
// Global Variables
83
/////////////////////////////////////////////////////////////////////////////
84
 
612 this 85
static volatile s8 phase;
611 this 86
 
612 this 87
static volatile u8 bs;
88
static volatile u16 block;
621 this 89
static volatile u32 run;
612 this 90
static volatile u8 time_read;
91
static volatile u8 time_write;
611 this 92
 
612 this 93
static volatile u8 last_error;
94
static volatile s32 last_error_code;
611 this 95
 
643 this 96
static volatile u8 buffer[BS_CHECK_DATA_BLOCKSIZE];
611 this 97
 
619 this 98
 
611 this 99
/////////////////////////////////////////////////////////////////////////////
100
// Local prototypes
101
/////////////////////////////////////////////////////////////////////////////
102
static void TASK_Display(void *pvParameters);
103
 
104
static void init_buffer(void);
105
static void clear_buffer(void);
106
static s32 check_buffer(void);
107
 
619 this 108
static s32 wait_write_finished(void);
109
static void report_iic_error(void);
110
 
611 this 111
/////////////////////////////////////////////////////////////////////////////
112
// Local functions
113
/////////////////////////////////////////////////////////////////////////////
114
 
115
static void init_buffer(void){
648 this 116
  u16 i;
643 this 117
  for (i=0;i<BS_CHECK_DATA_BLOCKSIZE;i++){
619 this 118
    buffer[i] = BS_CHECK_BUFFER_BYTE_VALUE;
613 this 119
    //if (block==0) MIOS32_MIDI_SendDebugMessage("buffer[%d]=%d",i,buffer[i]);
120
    }
611 this 121
  }
122
 
123
static void clear_buffer(void){
648 this 124
  u16 i;
643 this 125
  for (i=0;i<BS_CHECK_DATA_BLOCKSIZE;i++)
611 this 126
    buffer[i] = 0;
127
  }
128
 
129
static s32 check_buffer(void){
648 this 130
  u8 err=0;
131
  u16 i;
643 this 132
  for (i=0;i<BS_CHECK_DATA_BLOCKSIZE;i++){
619 this 133
    u8 value = BS_CHECK_BUFFER_BYTE_VALUE;
613 this 134
    if(buffer[i] != value){
135
      MIOS32_MIDI_SendDebugMessage("buffer[%d]=%d, should be %d",i,buffer[i],value);
136
      err = 1;
137
      }
138
    }
139
  return err ? 0 : 1;
611 this 140
  }
141
 
142
 
143
/////////////////////////////////////////////////////////////////////////////
144
// This hook is called after startup to initialize the application
145
/////////////////////////////////////////////////////////////////////////////
146
void APP_Init(void){
147
  // initialize all LEDs
148
  MIOS32_BOARD_LED_Init(0xffffffff);
615 this 149
  MIOS32_BOARD_LED_Set(0xffffffff,0);
611 this 150
  phase = BS_CHECK_PHASE_STARTWAIT;
151
  MIOS32_MIDI_SendDebugMessage("Bankstick r/w check");
152
  // setup display task
2425 tk 153
  xTaskCreate(TASK_Display, "Display", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_DISPLAY, NULL);
611 this 154
  }
155
 
156
 
157
/////////////////////////////////////////////////////////////////////////////
158
// This task is running endless in background
159
/////////////////////////////////////////////////////////////////////////////
160
void APP_Background(void){
161
  u8 i;
162
  while(1){
163
    switch(phase){
164
      case BS_CHECK_PHASE_INIT:
165
    bs = 0;
166
    block = 0;
167
    last_error = 0;
168
    last_error_code = 0;
169
    time_read = 0xff;
170
    time_write = 0xff;
171
        // initialize IIC..
621 this 172
        if(run == 0){
173
      MIOS32_MIDI_SendDebugMessage("Bankstick check init...");
643 this 174
#if BS_CHECK_USE_FRAM_LAYER == 0
621 this 175
      if( (last_error_code = MIOS32_IIC_BS_Init(0)) != 0){
643 this 176
#else
177
      if( (last_error_code = FRAM_Init(0)) != 0){
178
#endif
621 this 179
        last_error =  BS_CHECK_ERROR_INIT;
180
        phase = BS_CHECK_PHASE_FINISHED;
181
        }
182
      else{
643 this 183
        for(i = 0;i < BS_CHECK_NUM_DEVICES; i++){
184
#if BS_CHECK_USE_FRAM_LAYER == 0
648 this 185
          if ( (MIOS32_IIC_BS_CheckAvailable(i)) == 0) {
643 this 186
#else
648 this 187
          if ( (FRAM_CheckAvailable(i)) != 0) {
643 this 188
#endif
621 this 189
        report_iic_error();
190
        last_error = BS_CHECK_ERROR_AVAILABLE;
191
        last_error_code = i;
192
        MIOS32_MIDI_SendDebugMessage("Bankstick %d not available",i);
193
        phase = BS_CHECK_PHASE_FINISHED;
194
        break;
195
        }
196
          }
197
            }
611 this 198
          }
199
        else{
621 this 200
          MIOS32_MIDI_SendDebugMessage("Start next check, run %d",run+1);
201
          }
202
        if(!last_error)
203
          phase = BS_CHECK_PHASE_WRITE;
611 this 204
        break;
205
      case BS_CHECK_PHASE_WRITE:
206
        // write blocks
619 this 207
        if(bs == 0 && block == 0)
208
          MIOS32_MIDI_SendDebugMessage("Start writing blocks...");
611 this 209
        init_buffer();
643 this 210
#if BS_CHECK_USE_FRAM_LAYER == 0
211
        if( (last_error_code = MIOS32_IIC_BS_Write(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){
212
#else
213
        if( (last_error_code = FRAM_Write(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){
214
#endif
619 this 215
          report_iic_error();
216
          last_error =  BS_CHECK_ERROR_WRITE;
611 this 217
      phase = BS_CHECK_PHASE_FINISHED;
218
          }
219
        else{
619 this 220
      if( (last_error_code = wait_write_finished()) < 0){
221
        report_iic_error();
616 this 222
            last_error =  BS_CHECK_ERROR_CHECK_WRITE_FINISHED;
614 this 223
        phase = BS_CHECK_PHASE_FINISHED;
611 this 224
        }
643 this 225
      else if( ++block >= BS_CHECK_NUM_BLOCKS_PER_DEVICE ){
619 this 226
            MIOS32_MIDI_SendDebugMessage("Writing bankstick %d finished",bs);
611 this 227
        block = 0;
643 this 228
        if( ++bs >= BS_CHECK_NUM_DEVICES ){
611 this 229
          bs = 0;
230
          phase = BS_CHECK_PHASE_READ;
231
          }
232
        }
233
          }
234
        break;
235
      case BS_CHECK_PHASE_READ:
236
        // read blocks
619 this 237
        if(bs == 0 && block == 0)
238
          MIOS32_MIDI_SendDebugMessage("Start reading / comparing blocks...");
611 this 239
        clear_buffer();
643 this 240
#if BS_CHECK_USE_FRAM_LAYER == 0
241
        if( (last_error_code = MIOS32_IIC_BS_Read(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){
242
#else
243
        if( (last_error_code = FRAM_Read(bs,block*BS_CHECK_DATA_BLOCKSIZE,(char*)buffer,BS_CHECK_DATA_BLOCKSIZE)) != 0){
244
#endif
619 this 245
          report_iic_error();
611 this 246
      last_error =  BS_CHECK_ERROR_READ;
247
      phase = BS_CHECK_PHASE_FINISHED;
248
          }
249
    else{
250
      if( !check_buffer() ){
251
        last_error =  BS_CHECK_ERROR_COMPARE;
252
        last_error_code = 0;// no further error specification
253
        phase = BS_CHECK_PHASE_FINISHED;       
254
        }
643 this 255
      else if( ++block >= BS_CHECK_NUM_BLOCKS_PER_DEVICE ){
619 this 256
            MIOS32_MIDI_SendDebugMessage("Reading / comparing bankstick %d finished",bs);
611 this 257
        block = 0;
643 this 258
        if( ++bs >= BS_CHECK_NUM_DEVICES ){
621 this 259
              MIOS32_MIDI_SendDebugMessage("Check run %d finished",run+1);
260
              if(++run >= BS_CHECK_NUM_RUNS)
261
                phase = BS_CHECK_PHASE_FINISHED;
262
              else{
263
                bs = 0;
264
                phase = BS_CHECK_PHASE_START;
265
                }
611 this 266
          }
267
        }
268
          }
269
        break;
270
      }
271
    }
272
  }
273
 
619 this 274
 
275
static s32 wait_write_finished(void){
643 this 276
#if BS_CHECK_USE_FRAM_LAYER == 0
619 this 277
  s32 err_code;
278
   do{
279
     err_code = MIOS32_IIC_BS_CheckWriteFinished(bs);
280
     } while( err_code > 0 );
281
  return err_code;
643 this 282
#else
283
  return 0;
284
#endif
619 this 285
  }
286
 
287
static void report_iic_error(void){
643 this 288
#if BS_CHECK_USE_FRAM_LAYER == 0
619 this 289
  MIOS32_MIDI_SendDebugMessage("IIC error %d",MIOS32_IIC_LastErrorGet(MIOS32_IIC_BS_PORT));
643 this 290
#else
291
  MIOS32_MIDI_SendDebugMessage("IIC error %d",MIOS32_IIC_LastErrorGet(FRAM_IIC_PORT));
292
#endif
619 this 293
  }
294
 
295
 
611 this 296
/////////////////////////////////////////////////////////////////////////////
297
// This task is running endless in background to drive display-output
298
/////////////////////////////////////////////////////////////////////////////
299
 
300
 
301
static void TASK_Display(void *pvParameters){
302
  char * err_on;
303
  s32 msg_countdown = 0;
304
  while(1){
305
    switch(phase){
306
      case BS_CHECK_PHASE_STARTWAIT:
307
        if(msg_countdown != -phase){
308
          MIOS32_LCD_Clear();
309
          MIOS32_LCD_CursorSet(0,0);
310
          MIOS32_LCD_PrintString("Bankstick check");
311
          MIOS32_LCD_CursorSet(0,1);
312
          MIOS32_LCD_PrintString("Push a button/key..");
313
          MIOS32_MIDI_SendDebugMessage("Push any button or MIDI-key to start the check..");
314
          msg_countdown = -phase;
315
          }
316
        break;
317
      case BS_CHECK_PHASE_START:
318
        msg_countdown = -phase;
319
        phase = BS_CHECK_PHASE_INIT;
320
        break;
321
      case BS_CHECK_PHASE_WRITE:
322
        MIOS32_LCD_Clear();
323
    MIOS32_LCD_CursorSet(0,0);
621 this 324
    MIOS32_LCD_PrintFormattedString("Write blocks 0x%04X",block);
325
        MIOS32_LCD_CursorSet(0,1);
326
        MIOS32_LCD_PrintFormattedString("Bankstick %d, Run %d",bs,run+1);
611 this 327
        if(msg_countdown <= 0){
621 this 328
          MIOS32_MIDI_SendDebugMessage("Write blocks 0x%04X (bankstick %d, run %d)",block,bs,run+1);
611 this 329
          msg_countdown = 0;// restart message countdown if it was disabled
330
          }
331
        break;
332
      case BS_CHECK_PHASE_READ:
333
        MIOS32_LCD_Clear();
334
    MIOS32_LCD_CursorSet(0,0);
335
        MIOS32_LCD_PrintFormattedString("Read blocks 0x%04X",block);
621 this 336
        MIOS32_LCD_CursorSet(0,1);
337
        MIOS32_LCD_PrintFormattedString("Bankstick %d, Run %d",bs,run+1);
611 this 338
        if(msg_countdown <= 0){
621 this 339
          MIOS32_MIDI_SendDebugMessage("Read blocks 0x%04X (bankstick %d, run %d)",block,bs,run+1);
611 this 340
          msg_countdown = 0;// restart message countdown if it was disabled
341
          }
342
        break;
343
      case BS_CHECK_PHASE_FINISHED:
344
        if(msg_countdown != -phase){// was this stuff already printed/ sent do debug condsole?
345
          msg_countdown = -phase;// disable further message output in this phase
346
      MIOS32_LCD_Clear();
347
      MIOS32_LCD_CursorSet(0,0);
348
      if(last_error){
349
        switch(last_error){
350
          case BS_CHECK_ERROR_AVAILABLE:
351
        err_on = "available";
352
        break;
353
          case BS_CHECK_ERROR_INIT:
354
        err_on = "init";
355
        break;
356
          case BS_CHECK_ERROR_WRITE:
357
        err_on = "write";
358
        break;
359
          case BS_CHECK_ERROR_CHECK_WRITE_FINISHED:
360
        err_on = "write wait";
361
        break;
362
          case BS_CHECK_ERROR_READ:
363
        err_on = "read";
364
        break;
365
          case BS_CHECK_ERROR_COMPARE:
366
        err_on = "compare";
367
        break;
368
          }
614 this 369
        MIOS32_LCD_PrintFormattedString("Err: %s %d",err_on,last_error_code);
611 this 370
        MIOS32_LCD_CursorSet(0,1);
643 this 371
        MIOS32_LCD_PrintFormattedString("BS:%d B:0x%04x R:%d",bs,block,run);
622 this 372
        MIOS32_MIDI_SendDebugMessage("Error on %s, Error code %d, Bankstick %d, Block %d, Run %d",
373
              err_on,last_error_code,bs,block,run);
611 this 374
        }
375
      else{
376
        MIOS32_LCD_PrintFormattedString("Bankstick check");
377
        MIOS32_LCD_CursorSet(0,1);   
621 this 378
        MIOS32_LCD_PrintFormattedString("success (%d runs)",BS_CHECK_NUM_RUNS);
379
        MIOS32_MIDI_SendDebugMessage("Banstick check finished successfully (%d runs)!",BS_CHECK_NUM_RUNS);            
611 this 380
        }
381
      MIOS32_MIDI_SendDebugMessage("Push any button or MIDI-key to re-start the check..");
382
          }
383
        break;
384
      }
385
    // if msg_countdown is < 0, don't change it (used to stop further message output by phase)
386
    if(msg_countdown >= 0)
387
      msg_countdown = msg_countdown ? msg_countdown - 1 : DISPLAY_TASK_MSG_COUNTDOWN_STARTVALUE;
388
    // wait DISPLAY_TASK_DELAY mS
389
    vTaskDelay(DISPLAY_TASK_DELAYMS / portTICK_RATE_MS);
390
    }
391
  }
392
 
674 tk 393
 
611 this 394
/////////////////////////////////////////////////////////////////////////////
674 tk 395
// This hook is called when a MIDI package has been received
611 this 396
/////////////////////////////////////////////////////////////////////////////
674 tk 397
void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package)
398
{
621 this 399
  if( midi_package.type == NoteOn && midi_package.velocity > 0 ){
400
    run = 0;
401
    phase = BS_CHECK_PHASE_START;
611 this 402
  }
403
}
404
 
405
 
406
/////////////////////////////////////////////////////////////////////////////
407
// This hook is called before the shift register chain is scanned
408
/////////////////////////////////////////////////////////////////////////////
409
void APP_SRIO_ServicePrepare(void)
410
{
411
}
412
 
413
 
414
/////////////////////////////////////////////////////////////////////////////
415
// This hook is called after the shift register chain has been scanned
416
/////////////////////////////////////////////////////////////////////////////
417
void APP_SRIO_ServiceFinish(void)
418
{
419
}
420
 
421
 
422
/////////////////////////////////////////////////////////////////////////////
423
// This hook is called when a button has been toggled
424
// pin_value is 1 when button released, and 0 when button pressed
425
/////////////////////////////////////////////////////////////////////////////
426
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value){
621 this 427
  if(!pin_value){
428
    run = 0;
611 this 429
    phase = BS_CHECK_PHASE_START;
621 this 430
    }
611 this 431
  }
432
 
433
 
434
/////////////////////////////////////////////////////////////////////////////
435
// This hook is called when an encoder has been moved
436
// incrementer is positive when encoder has been turned clockwise, else
437
// it is negative
438
/////////////////////////////////////////////////////////////////////////////
439
void APP_ENC_NotifyChange(u32 encoder, s32 incrementer)
440
{
441
}
442
 
443
 
444
/////////////////////////////////////////////////////////////////////////////
445
// This hook is called when a pot has been moved
446
/////////////////////////////////////////////////////////////////////////////
447
void APP_AIN_NotifyChange(u32 pin, u32 pin_value)
448
{
449
}