Subversion Repositories svn.mios32

Rev

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

Rev Author Line No. Line
1411 tk 1
// $Id: app.c 2425 2016-11-03 00:44:22Z tk $
2
/*
3
 * SPI J16 Slave DMA
4
 *
5
 * ==========================================================================
6
 *
7
 *  Copyright (C) 2012 Thorsten Klose (tk@midibox.org)
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
 
21
#include <FreeRTOS.h>
22
#include <portmacro.h>
23
#include <task.h>
24
#include <queue.h>
25
#include <semphr.h>
26
 
27
 
28
// define priority level for SPI Handler task:
29
// use lower priority than MIOS32 specific tasks (3)
30
#define PRIORITY_TASK_SPI_HANDLER   ( tskIDLE_PRIORITY + 2 )
31
 
32
// local prototype of the task function
33
static void TASK_SPI_Handler(void *pvParameters);
34
 
35
 
36
/////////////////////////////////////////////////////////////////////////////
37
// Rx/Tx buffer
38
/////////////////////////////////////////////////////////////////////////////
39
#define SLAVE_SPI     0 // @J16
40
#define TRANSFER_BUFFER_SIZE 16
1412 tk 41
volatile u8 data_received;
1414 tk 42
u8 rx_buffer_tmp[TRANSFER_BUFFER_SIZE];
1413 tk 43
u8 rx_buffer[TRANSFER_BUFFER_SIZE];
44
u8 tx_buffer[TRANSFER_BUFFER_SIZE];
1411 tk 45
 
46
 
47
/////////////////////////////////////////////////////////////////////////////
48
// This hook is called after startup to initialize the application
49
/////////////////////////////////////////////////////////////////////////////
50
void APP_Init(void)
51
{
52
  // initialize all LEDs
53
  MIOS32_BOARD_LED_Init(0xffffffff);
54
 
55
 
56
  // initialize SPI interface
57
  // ensure that fast pin drivers are activated
58
  MIOS32_SPI_IO_Init(SLAVE_SPI, MIOS32_SPI_PIN_SLAVE_DRIVER_STRONG);
59
 
60
  // init SPI port
61
  MIOS32_SPI_TransferModeInit(SLAVE_SPI, MIOS32_SPI_MODE_SLAVE_CLK1_PHASE1, MIOS32_SPI_PRESCALER_128);
62
 
63
  // start task
2425 tk 64
  xTaskCreate(TASK_SPI_Handler, "SPI_Handler", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_SPI_HANDLER, NULL);
1411 tk 65
}
66
 
67
 
68
/////////////////////////////////////////////////////////////////////////////
69
// This task is running endless in background
70
/////////////////////////////////////////////////////////////////////////////
71
void APP_Background(void)
72
{
73
}
74
 
75
 
76
/////////////////////////////////////////////////////////////////////////////
77
// This hook is called when a MIDI package has been received
78
/////////////////////////////////////////////////////////////////////////////
79
void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package)
80
{
81
}
82
 
83
 
84
/////////////////////////////////////////////////////////////////////////////
85
// This hook is called before the shift register chain is scanned
86
/////////////////////////////////////////////////////////////////////////////
87
void APP_SRIO_ServicePrepare(void)
88
{
89
}
90
 
91
 
92
/////////////////////////////////////////////////////////////////////////////
93
// This hook is called after the shift register chain has been scanned
94
/////////////////////////////////////////////////////////////////////////////
95
void APP_SRIO_ServiceFinish(void)
96
{
97
}
98
 
99
 
100
/////////////////////////////////////////////////////////////////////////////
101
// This hook is called when a button has been toggled
102
// pin_value is 1 when button released, and 0 when button pressed
103
/////////////////////////////////////////////////////////////////////////////
104
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value)
105
{
106
}
107
 
108
 
109
/////////////////////////////////////////////////////////////////////////////
110
// This hook is called when an encoder has been moved
111
// incrementer is positive when encoder has been turned clockwise, else
112
// it is negative
113
/////////////////////////////////////////////////////////////////////////////
114
void APP_ENC_NotifyChange(u32 encoder, s32 incrementer)
115
{
116
}
117
 
118
 
119
/////////////////////////////////////////////////////////////////////////////
120
// This hook is called when a pot has been moved
121
/////////////////////////////////////////////////////////////////////////////
122
void APP_AIN_NotifyChange(u32 pin, u32 pin_value)
123
{
124
}
125
 
126
 
127
/////////////////////////////////////////////////////////////////////////////
128
// This task periodically sends a SPI datastream
129
/////////////////////////////////////////////////////////////////////////////
130
static void SPI_Callback(void)
131
{
132
  // called after SPI transfer:
133
 
1414 tk 134
  // copy RX values
1411 tk 135
  int i;
136
  for(i=0; i<TRANSFER_BUFFER_SIZE; ++i)
1414 tk 137
    rx_buffer[i] = rx_buffer_tmp[i];
1416 tk 138
 
139
  // notify new values to TASK_SPI_Handler
140
  ++data_received;
1414 tk 141
 
142
  // change TX values
143
  for(i=0; i<TRANSFER_BUFFER_SIZE; ++i)
1411 tk 144
    tx_buffer[i] += 0x10;
1413 tk 145
 
146
  // prepare next transfer
1414 tk 147
  MIOS32_SPI_TransferBlock(SLAVE_SPI, tx_buffer, rx_buffer_tmp, TRANSFER_BUFFER_SIZE, SPI_Callback);
1411 tk 148
}
149
 
150
static void TASK_SPI_Handler(void *pvParameters)
151
{
152
  // fill Tx Buffer with housenumbers
153
  {
154
    int i;
155
    for(i=0; i<TRANSFER_BUFFER_SIZE; ++i)
156
      tx_buffer[i] = i;
157
  }
158
 
1413 tk 159
  // initial transfer
160
  u8 last_data_received = data_received = 0;
161
  SPI_Callback();
162
 
1411 tk 163
  while( 1 ) {
164
    // toggle Status LED to as a sign of live
165
    MIOS32_BOARD_LED_Set(1, ~MIOS32_BOARD_LED_Get());
166
 
1413 tk 167
    while( last_data_received == data_received );
168
    last_data_received = data_received;
1412 tk 169
 
170
    // print received bytes
171
    MIOS32_MIDI_SendDebugHexDump((u8 *)rx_buffer, TRANSFER_BUFFER_SIZE);
1411 tk 172
  }
173
}