Subversion Repositories svn.mios32

Rev

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

Rev Author Line No. Line
682 tk 1
// $Id: app.c 2425 2016-11-03 00:44:22Z tk $
2
/*
3
 * MIOS32 Tutorial #006: RTOS Tasks
4
 * see README.txt for details
5
 *
6
 * ==========================================================================
7
 *
8
 *  Copyright (C) 2009 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 "app.h"
21
 
22
// include everything FreeRTOS related we don't understand yet ;)
23
#include <FreeRTOS.h>
24
#include <portmacro.h>
25
#include <task.h>
26
#include <queue.h>
27
#include <semphr.h>
28
 
29
 
30
// define priority level for J5_SCAN task:
31
// use same priority as MIOS32 specific tasks (3)
32
#define PRIORITY_TASK_J5_SCAN   ( tskIDLE_PRIORITY + 3 )
33
 
34
// local prototype of the task function
35
static void TASK_J5_Scan(void *pvParameters);
36
 
37
 
38
/////////////////////////////////////////////////////////////////////////////
39
// This hook is called after startup to initialize the application
40
/////////////////////////////////////////////////////////////////////////////
41
void APP_Init(void)
42
{
43
  // initialize all LEDs
44
  MIOS32_BOARD_LED_Init(0xffffffff);
45
 
46
  // initialize all pins of J5A, J5B and J5C as inputs with internal Pull-Up
47
  int pin;
48
  for(pin=0; pin<12; ++pin)
49
    MIOS32_BOARD_J5_PinInit(pin, MIOS32_BOARD_PIN_MODE_INPUT_PU);
50
 
51
  // start task
2425 tk 52
  xTaskCreate(TASK_J5_Scan, "J5_Scan", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_J5_SCAN, NULL);
682 tk 53
}
54
 
55
 
56
/////////////////////////////////////////////////////////////////////////////
57
// This task is running endless in background
58
/////////////////////////////////////////////////////////////////////////////
59
void APP_Background(void)
60
{
61
}
62
 
63
 
64
/////////////////////////////////////////////////////////////////////////////
1919 tk 65
// This hook is called each mS from the main task which also handles DIN, ENC
66
// and AIN events. You could add more jobs here, but they shouldn't consume
67
// more than 300 uS to ensure the responsiveness of buttons, encoders, pots.
68
// Alternatively you could create a dedicated task for application specific
69
// jobs as explained in $MIOS32_PATH/apps/tutorials/006_rtos_tasks
70
/////////////////////////////////////////////////////////////////////////////
71
void APP_Tick(void)
72
{
73
}
74
 
75
 
76
/////////////////////////////////////////////////////////////////////////////
77
// This hook is called each mS from the MIDI task which checks for incoming
78
// MIDI events. You could add more MIDI related jobs here, but they shouldn't
79
// consume more than 300 uS to ensure the responsiveness of incoming MIDI.
80
/////////////////////////////////////////////////////////////////////////////
81
void APP_MIDI_Tick(void)
82
{
83
}
84
 
85
 
86
/////////////////////////////////////////////////////////////////////////////
682 tk 87
// This hook is called when a MIDI package has been received
88
/////////////////////////////////////////////////////////////////////////////
89
void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package)
90
{
91
}
92
 
93
 
94
/////////////////////////////////////////////////////////////////////////////
95
// This hook is called before the shift register chain is scanned
96
/////////////////////////////////////////////////////////////////////////////
97
void APP_SRIO_ServicePrepare(void)
98
{
99
}
100
 
101
 
102
/////////////////////////////////////////////////////////////////////////////
103
// This hook is called after the shift register chain has been scanned
104
/////////////////////////////////////////////////////////////////////////////
105
void APP_SRIO_ServiceFinish(void)
106
{
107
}
108
 
109
 
110
/////////////////////////////////////////////////////////////////////////////
111
// This hook is called when a button has been toggled
112
// pin_value is 1 when button released, and 0 when button pressed
113
/////////////////////////////////////////////////////////////////////////////
114
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value)
115
{
116
}
117
 
118
 
119
/////////////////////////////////////////////////////////////////////////////
120
// This hook is called when an encoder has been moved
121
// incrementer is positive when encoder has been turned clockwise, else
122
// it is negative
123
/////////////////////////////////////////////////////////////////////////////
124
void APP_ENC_NotifyChange(u32 encoder, s32 incrementer)
125
{
126
}
127
 
128
 
129
/////////////////////////////////////////////////////////////////////////////
130
// This hook is called when a pot has been moved
131
/////////////////////////////////////////////////////////////////////////////
132
void APP_AIN_NotifyChange(u32 pin, u32 pin_value)
133
{
134
}
135
 
136
 
137
/////////////////////////////////////////////////////////////////////////////
138
// This task scans J5 pins periodically
139
/////////////////////////////////////////////////////////////////////////////
140
static void TASK_J5_Scan(void *pvParameters)
141
{
142
  u8 old_state[12]; // to store the state of 12 pins
143
  u8 debounce_ctr[12]; // to store debounce delay counters
144
  portTickType xLastExecutionTime;
145
 
146
  // initialize pin state and debounce counters to inactive value
147
  int pin;
148
  for(pin=0; pin<12; ++pin) {
149
    old_state[pin] = 1;
150
    debounce_ctr[pin] = 0;
151
  }
152
 
153
  // Initialise the xLastExecutionTime variable on task entry
154
  xLastExecutionTime = xTaskGetTickCount();
155
 
156
  while( 1 ) {
157
    vTaskDelayUntil(&xLastExecutionTime, 1 / portTICK_RATE_MS);
158
 
159
    // toggle Status LED to as a sign of live
1919 tk 160
    MIOS32_BOARD_LED_Set(0x0001, ~MIOS32_BOARD_LED_Get());
682 tk 161
 
162
    // check each J5 pin for changes
163
    int pin;
164
    for(pin=0; pin<12; ++pin) {
165
      // debounce counter active?
166
      if( debounce_ctr[pin] ) {
167
    // decrement value, wait for another mS
168
    --debounce_ctr[pin];
169
      } else {
170
    // check for new pin state
171
    u8 new_state = MIOS32_BOARD_J5_PinGet(pin);
172
 
173
    // pin changed?
174
    if( new_state != old_state[pin] ) {
175
      // store new state
176
      old_state[pin] = new_state;
177
 
178
      // // delay next check for 20 mS
179
      debounce_ctr[pin] = 20;
180
 
181
      // send Note On/Off Event depending on new pin state
182
      if( new_state == 0 ) // Jumper closed -> 0V
183
        MIOS32_MIDI_SendNoteOn(DEFAULT, Chn1, 0x3c + pin, 127);
184
      else                 // Jumper opened -> ca. 3.3V
185
        MIOS32_MIDI_SendNoteOff(DEFAULT, Chn1, 0x3c + pin, 0);
186
    }
187
      }
188
    }
189
  }
190
}