Subversion Repositories svn.mios32

Rev

Rev 1412 | 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 Polling
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
41
u8 rx_buffer[TRANSFER_BUFFER_SIZE];
42
u8 tx_buffer[TRANSFER_BUFFER_SIZE];
43
 
44
 
45
/////////////////////////////////////////////////////////////////////////////
46
// This hook is called after startup to initialize the application
47
/////////////////////////////////////////////////////////////////////////////
48
void APP_Init(void)
49
{
50
  // initialize all LEDs
51
  MIOS32_BOARD_LED_Init(0xffffffff);
52
 
53
 
54
  // initialize SPI interface
55
  // ensure that fast pin drivers are activated
56
  MIOS32_SPI_IO_Init(SLAVE_SPI, MIOS32_SPI_PIN_SLAVE_DRIVER_STRONG);
57
 
58
  // init SPI port
59
  MIOS32_SPI_TransferModeInit(SLAVE_SPI, MIOS32_SPI_MODE_SLAVE_CLK1_PHASE1, MIOS32_SPI_PRESCALER_128);
60
 
61
  // start task
2425 tk 62
  xTaskCreate(TASK_SPI_Handler, "SPI_Handler", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_SPI_HANDLER, NULL);
1411 tk 63
}
64
 
65
 
66
/////////////////////////////////////////////////////////////////////////////
67
// This task is running endless in background
68
/////////////////////////////////////////////////////////////////////////////
69
void APP_Background(void)
70
{
71
}
72
 
73
 
74
/////////////////////////////////////////////////////////////////////////////
75
// This hook is called when a MIDI package has been received
76
/////////////////////////////////////////////////////////////////////////////
77
void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package)
78
{
79
}
80
 
81
 
82
/////////////////////////////////////////////////////////////////////////////
83
// This hook is called before the shift register chain is scanned
84
/////////////////////////////////////////////////////////////////////////////
85
void APP_SRIO_ServicePrepare(void)
86
{
87
}
88
 
89
 
90
/////////////////////////////////////////////////////////////////////////////
91
// This hook is called after the shift register chain has been scanned
92
/////////////////////////////////////////////////////////////////////////////
93
void APP_SRIO_ServiceFinish(void)
94
{
95
}
96
 
97
 
98
/////////////////////////////////////////////////////////////////////////////
99
// This hook is called when a button has been toggled
100
// pin_value is 1 when button released, and 0 when button pressed
101
/////////////////////////////////////////////////////////////////////////////
102
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value)
103
{
104
}
105
 
106
 
107
/////////////////////////////////////////////////////////////////////////////
108
// This hook is called when an encoder has been moved
109
// incrementer is positive when encoder has been turned clockwise, else
110
// it is negative
111
/////////////////////////////////////////////////////////////////////////////
112
void APP_ENC_NotifyChange(u32 encoder, s32 incrementer)
113
{
114
}
115
 
116
 
117
/////////////////////////////////////////////////////////////////////////////
118
// This hook is called when a pot has been moved
119
/////////////////////////////////////////////////////////////////////////////
120
void APP_AIN_NotifyChange(u32 pin, u32 pin_value)
121
{
122
}
123
 
124
 
125
/////////////////////////////////////////////////////////////////////////////
126
// This task periodically sends a SPI datastream
127
/////////////////////////////////////////////////////////////////////////////
128
static void SPI_Callback(void)
129
{
130
  // called after SPI transfer:
131
 
132
  // change TX values
133
  int i;
134
  for(i=0; i<TRANSFER_BUFFER_SIZE; ++i)
135
    tx_buffer[i] += 0x10;
136
}
137
 
138
static void TASK_SPI_Handler(void *pvParameters)
139
{
140
  // fill Tx Buffer with housenumbers
141
  {
142
    int i;
143
    for(i=0; i<TRANSFER_BUFFER_SIZE; ++i)
144
      tx_buffer[i] = i;
145
  }
146
 
147
  while( 1 ) {
148
    int i;
149
    for(i=0; i<TRANSFER_BUFFER_SIZE; ++i) {
150
      // toggle Status LED to as a sign of live
151
      MIOS32_BOARD_LED_Set(1, ~MIOS32_BOARD_LED_Get());
152
 
153
      // receive byte
154
      rx_buffer[i] = MIOS32_SPI_TransferByte(SLAVE_SPI, tx_buffer[i]);
155
    }
156
 
157
    SPI_Callback();
1412 tk 158
 
159
    // print received bytes
160
    MIOS32_MIDI_SendDebugHexDump((u8 *)rx_buffer, TRANSFER_BUFFER_SIZE);
1411 tk 161
  }
162
}