Subversion Repositories svn.mios32

Rev

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

Rev Author Line No. Line
823 philetaylo 1
// $Id: app.c 674 2009-07-29 19:54:48Z tk $
2
/*
3
 *
4
 * See README.txt for details
5
 *
6
 * ==========================================================================
7
 *
8
 *  Copyright (C) 2008 Thorsten Klose (tk@midibox.org)
9
 *  Licensed for personal non-commercial use only.
10
 *  All other rights reserved.
11
 *
12
 * ==========================================================================
13
 */
14
 
15
/////////////////////////////////////////////////////////////////////////////
16
// Include files
17
/////////////////////////////////////////////////////////////////////////////
18
 
19
#include <mios32.h>
20
#include <string.h>
21
 
22
#include "app.h"
829 philetaylo 23
#include "uip.h"
24
#include "timer.h"
25
#include "pt.h"
823 philetaylo 26
#include "uip_task.h"
829 philetaylo 27
#include "glcd_font.h"
823 philetaylo 28
 
29
/////////////////////////////////////////////////////////////////////////////
30
// Global Variables
31
/////////////////////////////////////////////////////////////////////////////
32
 
33
volatile u8 print_msg;
34
volatile u8 sdcard_available;
35
 
36
xSemaphoreHandle xSDCardSemaphore;
37
 
38
/////////////////////////////////////////////////////////////////////////////
39
// Local variables
40
/////////////////////////////////////////////////////////////////////////////
41
 
42
// Mutex for controlling access to SDCARD/ENC28J60
43
xSemaphoreHandle xSPI0Semaphore;
44
 
45
/////////////////////////////////////////////////////////////////////////////
46
// This hook is called after startup to initialize the application
47
/////////////////////////////////////////////////////////////////////////////
48
void APP_Init(void)
49
{
50
  s32 i;
51
 
52
  // initialize all LEDs
53
  MIOS32_BOARD_LED_Init(0xffffffff);
54
 
55
  // MUST be initialized before the SPI functions
56
  xSPI0Semaphore = xSemaphoreCreateMutex();
57
  xSDCardSemaphore = xSemaphoreCreateMutex();
58
 
59
  // Init filesystem and start SD Card monitoring thread
60
  FS_Init(0);
2425 tk 61
  xTaskCreate(TASK_Period1S, "Period1S", configMINIMAL_STACK_SIZE, NULL, ( tskIDLE_PRIORITY + 4 ), NULL);
823 philetaylo 62
 
63
  // start uIP task
64
  UIP_TASK_Init(0);
65
 
66
  // print first message
67
  print_msg = PRINT_MSG_INIT;
68
 
69
  // print welcome message on MIOS terminal
70
  MIOS32_MIDI_SendDebugMessage("\n");
71
  MIOS32_MIDI_SendDebugMessage("====================\n");
72
  MIOS32_MIDI_SendDebugMessage("%s\n", MIOS32_LCD_BOOT_MSG_LINE1);
73
  MIOS32_MIDI_SendDebugMessage("====================\n");
74
  MIOS32_MIDI_SendDebugMessage("\n");
75
}
76
 
77
 
78
/////////////////////////////////////////////////////////////////////////////
79
// This task is running endless in background
80
/////////////////////////////////////////////////////////////////////////////
81
void APP_Background(void)
82
{
83
  int i;
829 philetaylo 84
  struct ntp_tm tm;
85
  const char *month_names[] = {
86
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
87
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
88
 
89
  const char *weekday_names[] = {
90
    "Sun", "Mon", "Tue", "Wed",
91
    "Thu", "Fri", "Sat" };
92
  mios32_sys_time_t t;
93
  char timestring[64];
823 philetaylo 94
  // clear LCD screen
95
  MIOS32_LCD_Clear();
96
 
97
  // endless loop: print status information on LCD
98
  while( 1 ) {
99
    // new message requested?
100
    // TODO: add FreeRTOS specific queue handling!
101
    u8 new_msg = PRINT_MSG_NONE;
102
    portENTER_CRITICAL(); // port specific FreeRTOS function to disable tasks (nested)
103
    if( print_msg ) {
104
      new_msg = print_msg;
105
      print_msg = PRINT_MSG_NONE; // clear request
106
    }
107
    portEXIT_CRITICAL(); // port specific FreeRTOS function to enable tasks (nested)
108
 
109
    switch( new_msg ) {
110
      case PRINT_MSG_INIT:
829 philetaylo 111
        MIOS32_LCD_FontInit((u8 *)GLCD_FONT_NORMAL);
823 philetaylo 112
        MIOS32_LCD_CursorSet(0, 0);
113
        MIOS32_LCD_PrintString("see README.txt   ");
114
        MIOS32_LCD_CursorSet(0, 1);
115
        MIOS32_LCD_PrintString("for details     ");
116
    break;
117
 
118
      case PRINT_MSG_STATUS:
119
      {
829 philetaylo 120
        MIOS32_LCD_CursorSet(0, 0);
121
        // request status screen again (will stop once a new screen is requested by another task)
122
        print_msg = PRINT_MSG_STATUS;
823 philetaylo 123
      }
124
      break;
125
    }
829 philetaylo 126
 
127
 
128
    t = MIOS32_SYS_TimeGet();
129
    // convert ntp seconds since 1900 into useful time structure
130
    ntp_tmtime(t.seconds, &tm);
131
    // honour dailight savings
132
    ntp_dst(&tm);
133
    MIOS32_LCD_FontInit((u8 *)GLCD_FONT_SMALL);
134
    MIOS32_LCD_CursorSet(15, 12);
135
    MIOS32_LCD_PrintFormattedString("%s %02d %s %04d %02d:%02d:%02d", weekday_names[tm.weekday], tm.day, month_names[tm.month-1], tm.year,tm.hour, tm.minute, tm.second);
136
 
137
 
823 philetaylo 138
  }
139
}
140
 
141
 
142
 
143
/////////////////////////////////////////////////////////////////////////////
144
// This hook is called when a MIDI package has been received
145
/////////////////////////////////////////////////////////////////////////////
146
void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package)
147
{
148
}
149
 
150
 
151
/////////////////////////////////////////////////////////////////////////////
152
// This hook is called before the shift register chain is scanned
153
/////////////////////////////////////////////////////////////////////////////
154
void APP_SRIO_ServicePrepare(void)
155
{
156
}
157
 
158
 
159
/////////////////////////////////////////////////////////////////////////////
160
// This hook is called after the shift register chain has been scanned
161
/////////////////////////////////////////////////////////////////////////////
162
void APP_SRIO_ServiceFinish(void)
163
{
164
}
165
 
166
 
167
/////////////////////////////////////////////////////////////////////////////
168
// This hook is called when a button has been toggled
169
// pin_value is 1 when button released, and 0 when button pressed
170
/////////////////////////////////////////////////////////////////////////////
171
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value)
172
{
173
}
174
 
175
 
176
/////////////////////////////////////////////////////////////////////////////
177
// This hook is called when an encoder has been moved
178
// incrementer is positive when encoder has been turned clockwise, else
179
// it is negative
180
/////////////////////////////////////////////////////////////////////////////
181
void APP_ENC_NotifyChange(u32 encoder, s32 incrementer)
182
{
183
}
184
 
185
 
186
/////////////////////////////////////////////////////////////////////////////
187
// This hook is called when a pot has been moved
188
/////////////////////////////////////////////////////////////////////////////
189
void APP_AIN_NotifyChange(u32 pin, u32 pin_value)
190
{
191
}
192
 
193
static void TASK_Period1S(void *pvParameters)
194
{
195
  portTickType xLastExecutionTime;
196
  sdcard_available = 0;
197
  MIOS32_MIDI_SendDebugMessage("About to initialize SDCard Reader\n");
198
 
199
  // initialize SD Card
200
  MIOS32_SDCARD_Init(0);
201
  //MIOS32_SDCARD_PowerOn(); not needed.
202
  // Initialise the xLastExecutionTime variable on task entry
203
  xLastExecutionTime = xTaskGetTickCount();
204
 
205
  while( 1 ) {
206
     vTaskDelayUntil(&xLastExecutionTime, 1000 / portTICK_RATE_MS);
207
     // Must have this mutex before accessing the sdcard.
208
     MUTEX_SDCARD_TAKE
209
     // check if SD card is available
210
     // High-speed access if SD card was previously available
211
     u8 prev_sdcard_available = sdcard_available;
212
     sdcard_available = MIOS32_SDCARD_CheckAvailable(prev_sdcard_available);
213
 
214
    if( sdcard_available && !prev_sdcard_available ) {
215
      MIOS32_MIDI_SendDebugMessage("SD Card has been connected!\n");
216
      s32 status;
217
      if( (status=FS_mount_fs()) < 0 ) {
218
        MIOS32_MIDI_SendDebugMessage("File system cannot be mounted, status: %d\n", status);
219
      } //else {
220
        // This is where we do any post mounting tasks.
221
      // }
222
    } else if( !sdcard_available && prev_sdcard_available ) {
223
        MIOS32_MIDI_SendDebugMessage("SD Card has been disconnected!\n");
224
    }
225
    MUTEX_SDCARD_GIVE
226
  }
227
 
228
}
229
 
230
 
231
void APP_MutexSPI0Take(void)
232
{
233
    // This must block as we aren't telling the calling process that the semaphore couldn't be obtained!
234
    while( xSemaphoreTake(xSPI0Semaphore, (portTickType)1) != pdTRUE );
235
    return;
236
}
237
 
238
 
239
void APP_MutexSPI0Give(void)
240
{
241
    xSemaphoreGive(xSPI0Semaphore);
242
    return;
243
}
244