Subversion Repositories svn.mios32

Rev

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

Rev Author Line No. Line
588 tk 1
// $Id: app.c 2425 2016-11-03 00:44:22Z tk $
2
/*
3
 * USB Mass Storage Device
4
 *
5
 * ==========================================================================
6
 *
7
 *  Copyright (C) 2009 Thorsten Klose (tk@midibox.org)
8
 *  Licensed for personal non-commercial use only.
9
 *  All other rights reserved.
10
 *
11
 * ==========================================================================
12
 */
13
 
14
 
15
/////////////////////////////////////////////////////////////////////////////
16
// Include files
17
/////////////////////////////////////////////////////////////////////////////
18
 
19
#include <mios32.h>
20
#include <msd.h>
21
 
22
#include "app.h"
23
 
24
#include <FreeRTOS.h>
25
#include <portmacro.h>
26
#include <task.h>
27
#include <queue.h>
28
#include <semphr.h>
29
 
30
 
31
/////////////////////////////////////////////////////////////////////////////
32
// for optional debugging messages via MIDI
33
// should be enabled for this application!
34
/////////////////////////////////////////////////////////////////////////////
35
#define DEBUG_VERBOSE_LEVEL 1
36
#define DEBUG_MSG MIOS32_MIDI_SendDebugMessage
37
 
38
 
39
/////////////////////////////////////////////////////////////////////////////
40
// Local definitions
41
/////////////////////////////////////////////////////////////////////////////
42
 
43
#define PRIORITY_TASK_SDCARD    ( tskIDLE_PRIORITY + 2 )
44
 
45
 
46
/////////////////////////////////////////////////////////////////////////////
47
// local variables
48
/////////////////////////////////////////////////////////////////////////////
49
static u8 sdcard_available;
50
 
51
 
52
/////////////////////////////////////////////////////////////////////////////
53
// Local Prototypes
54
/////////////////////////////////////////////////////////////////////////////
55
static void TASK_SDCard(void *pvParameters);
56
 
57
 
58
/////////////////////////////////////////////////////////////////////////////
59
// This hook is called after startup to initialize the application
60
/////////////////////////////////////////////////////////////////////////////
61
s32 MASS_PROP_Init(u32 mode);
62
void APP_Init(void)
63
{
64
  // initialize all LEDs
65
  MIOS32_BOARD_LED_Init(0xffffffff);
66
 
67
  // init SD Card driver
68
  MIOS32_SDCARD_Init(0);
69
 
70
  // start task
2425 tk 71
  xTaskCreate(TASK_SDCard, "SDCard", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_SDCARD, NULL);
588 tk 72
 
1235 tk 73
  // optional:
74
  // allows to send debug messages to UART0 since USB is allocated
75
  //MIOS32_MIDI_DebugPortSet(UART0);
76
 
588 tk 77
#if DEBUG_VERBOSE_LEVEL >= 1
78
  // print welcome message on MIOS terminal
79
  DEBUG_MSG("\n");
80
  DEBUG_MSG("====================\n");
81
  DEBUG_MSG("%s\n", MIOS32_LCD_BOOT_MSG_LINE1);
82
  DEBUG_MSG("====================\n");
83
  DEBUG_MSG("\n");
84
#endif
85
}
86
 
87
 
88
/////////////////////////////////////////////////////////////////////////////
89
// This task is called periodically each mS
90
/////////////////////////////////////////////////////////////////////////////
91
static void TASK_SDCard(void *pvParameters)
92
{
93
  u16 second_delay_ctr = 0;
94
  portTickType xLastExecutionTime;
95
 
96
  // turn off LED (will be turned on once SD card connected/detected)
97
  MIOS32_BOARD_LED_Set(0x1, 0x0);
98
 
99
  // Initialise the xLastExecutionTime variable on task entry
100
  xLastExecutionTime = xTaskGetTickCount();
101
 
102
  while( 1 ) {
103
    vTaskDelayUntil(&xLastExecutionTime, 1 / portTICK_RATE_MS);
104
 
105
    // each second:
106
    // check if SD card is available
107
    // High-speed access if SD card was previously available
108
 
109
    if( ++second_delay_ctr >= 1000 ) {
110
      second_delay_ctr = 0;
111
 
1235 tk 112
      MIOS32_IRQ_Disable();
588 tk 113
      u8 prev_sdcard_available = sdcard_available;
114
      sdcard_available = MIOS32_SDCARD_CheckAvailable(prev_sdcard_available);
1235 tk 115
      MIOS32_IRQ_Enable();
588 tk 116
 
117
      if( sdcard_available && !prev_sdcard_available ) {
118
    MIOS32_BOARD_LED_Set(0x1, 0x1); // turn on LED
119
 
120
#if DEBUG_VERBOSE_LEVEL >= 0
121
    // always print...
122
    DEBUG_MSG("SD Card has been connected!\n");
123
#endif
124
 
125
    // switch to mass storage device
126
    MSD_Init(0);
127
 
128
    } else if( !sdcard_available && prev_sdcard_available ) {
129
    // re-init USB for MIDI
130
    MIOS32_USB_Init(1);
131
 
132
    MIOS32_BOARD_LED_Set(0x1, 0x0); // turn off LED
133
#if DEBUG_VERBOSE_LEVEL >= 0
134
    // always print...
135
    DEBUG_MSG("SD Card disconnected!\n");
136
#endif
137
      }
138
    }
139
 
140
    // each millisecond:
141
    // handle USB access if device is available
142
    if( sdcard_available )
143
      MSD_Periodic_mS();
144
  }
145
}
146
 
147
 
148
/////////////////////////////////////////////////////////////////////////////
149
// This task is running endless in background
150
/////////////////////////////////////////////////////////////////////////////
151
void APP_Background(void)
152
{
153
  // init LCD
154
  MIOS32_LCD_Clear();
155
  MIOS32_LCD_CursorSet(0, 0);
156
  MIOS32_LCD_PrintString("see README.txt   ");
157
  MIOS32_LCD_CursorSet(0, 1);
158
  MIOS32_LCD_PrintString("for details     ");
159
 
160
  // endless loop
161
  while( 1 ) {
162
    if( sdcard_available ) {
163
      MIOS32_LCD_CursorSet(0, 0);
164
      MIOS32_LCD_PrintString("SD Card     ");
165
      MIOS32_LCD_PrintChar(MSD_CheckAvailable() ? 'U' : ' ');
166
      MIOS32_LCD_PrintChar(MSD_LUN_AvailableGet(0) ? 'M' : ' ');
167
      MIOS32_LCD_PrintChar(MSD_RdLEDGet(250) ? 'R' : ' ');
168
      MIOS32_LCD_PrintChar(MSD_WrLEDGet(250) ? 'W' : ' ');
169
      MIOS32_LCD_CursorSet(0, 1);
170
      MIOS32_LCD_PrintString("connected       ");
171
    } else {
172
      MIOS32_LCD_CursorSet(0, 0);
173
      MIOS32_LCD_PrintString("SD Card         ");
174
      MIOS32_LCD_CursorSet(0, 1);
175
      MIOS32_LCD_PrintString("disconnected    ");
176
    }
177
  }
178
}
179
 
180
 
181
/////////////////////////////////////////////////////////////////////////////
674 tk 182
// This hook is called when a MIDI package has been received
588 tk 183
/////////////////////////////////////////////////////////////////////////////
674 tk 184
void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package)
588 tk 185
{
186
}
187
 
188
 
189
/////////////////////////////////////////////////////////////////////////////
190
// This hook is called before the shift register chain is scanned
191
/////////////////////////////////////////////////////////////////////////////
192
void APP_SRIO_ServicePrepare(void)
193
{
194
}
195
 
196
 
197
/////////////////////////////////////////////////////////////////////////////
198
// This hook is called after the shift register chain has been scanned
199
/////////////////////////////////////////////////////////////////////////////
200
void APP_SRIO_ServiceFinish(void)
201
{
202
}
203
 
204
 
205
/////////////////////////////////////////////////////////////////////////////
206
// This hook is called when a button has been toggled
207
// pin_value is 1 when button released, and 0 when button pressed
208
/////////////////////////////////////////////////////////////////////////////
209
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value)
210
{
211
}
212
 
213
 
214
/////////////////////////////////////////////////////////////////////////////
215
// This hook is called when an encoder has been moved
216
// incrementer is positive when encoder has been turned clockwise, else
217
// it is negative
218
/////////////////////////////////////////////////////////////////////////////
219
void APP_ENC_NotifyChange(u32 encoder, s32 incrementer)
220
{
221
}
222
 
223
 
224
/////////////////////////////////////////////////////////////////////////////
225
// This hook is called when a pot has been moved
226
/////////////////////////////////////////////////////////////////////////////
227
void APP_AIN_NotifyChange(u32 pin, u32 pin_value)
228
{
229
}