Subversion Repositories svn.mios32

Rev

Rev 1413 | 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 Master
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 same priority as MIOS32 specific tasks (3)
30
#define PRIORITY_TASK_SPI_HANDLER   ( tskIDLE_PRIORITY + 3 )
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 MASTER_SPI     0 // @J16
40
#define MASTER_CS_PIN  0 // =RC1
41
#define TRANSFER_BUFFER_SIZE 16
1413 tk 42
u8 rx_buffer[TRANSFER_BUFFER_SIZE];
43
u8 tx_buffer[TRANSFER_BUFFER_SIZE];
1411 tk 44
 
45
 
46
/////////////////////////////////////////////////////////////////////////////
47
// This hook is called after startup to initialize the application
48
/////////////////////////////////////////////////////////////////////////////
49
void APP_Init(void)
50
{
51
  // initialize all LEDs
52
  MIOS32_BOARD_LED_Init(0xffffffff);
53
 
54
 
55
  // initialize SPI interface
56
  // ensure that fast pin drivers are activated
57
  MIOS32_SPI_IO_Init(MASTER_SPI, MIOS32_SPI_PIN_DRIVER_STRONG);
58
 
59
  // init SPI port
60
  MIOS32_SPI_TransferModeInit(MASTER_SPI, MIOS32_SPI_MODE_CLK1_PHASE1, MIOS32_SPI_PRESCALER_128);
61
 
62
  // ensure that chip select line deactivated
63
  MIOS32_SPI_RC_PinSet(MASTER_SPI, MASTER_CS_PIN, 1); // spi, rc_pin, pin_value
64
 
65
  // start task
2425 tk 66
  xTaskCreate(TASK_SPI_Handler, "SPI_Handler", configMINIMAL_STACK_SIZE, NULL, PRIORITY_TASK_SPI_HANDLER, NULL);
1411 tk 67
}
68
 
69
 
70
/////////////////////////////////////////////////////////////////////////////
71
// This task is running endless in background
72
/////////////////////////////////////////////////////////////////////////////
73
void APP_Background(void)
74
{
75
}
76
 
77
 
78
/////////////////////////////////////////////////////////////////////////////
79
// This hook is called when a MIDI package has been received
80
/////////////////////////////////////////////////////////////////////////////
81
void APP_MIDI_NotifyPackage(mios32_midi_port_t port, mios32_midi_package_t midi_package)
82
{
83
}
84
 
85
 
86
/////////////////////////////////////////////////////////////////////////////
87
// This hook is called before the shift register chain is scanned
88
/////////////////////////////////////////////////////////////////////////////
89
void APP_SRIO_ServicePrepare(void)
90
{
91
}
92
 
93
 
94
/////////////////////////////////////////////////////////////////////////////
95
// This hook is called after the shift register chain has been scanned
96
/////////////////////////////////////////////////////////////////////////////
97
void APP_SRIO_ServiceFinish(void)
98
{
99
}
100
 
101
 
102
/////////////////////////////////////////////////////////////////////////////
103
// This hook is called when a button has been toggled
104
// pin_value is 1 when button released, and 0 when button pressed
105
/////////////////////////////////////////////////////////////////////////////
106
void APP_DIN_NotifyToggle(u32 pin, u32 pin_value)
107
{
108
}
109
 
110
 
111
/////////////////////////////////////////////////////////////////////////////
112
// This hook is called when an encoder has been moved
113
// incrementer is positive when encoder has been turned clockwise, else
114
// it is negative
115
/////////////////////////////////////////////////////////////////////////////
116
void APP_ENC_NotifyChange(u32 encoder, s32 incrementer)
117
{
118
}
119
 
120
 
121
/////////////////////////////////////////////////////////////////////////////
122
// This hook is called when a pot has been moved
123
/////////////////////////////////////////////////////////////////////////////
124
void APP_AIN_NotifyChange(u32 pin, u32 pin_value)
125
{
126
}
127
 
128
 
129
/////////////////////////////////////////////////////////////////////////////
130
// This task periodically sends a SPI datastream
131
/////////////////////////////////////////////////////////////////////////////
132
static void SPI_Callback(void)
133
{
134
  // called after SPI transfer:
135
 
136
  // deactivate CS pin
137
  MIOS32_SPI_RC_PinSet(MASTER_SPI, MASTER_CS_PIN, 1); // spi, rc_pin, pin_value
138
 
139
  // change TX values
140
  int i;
141
  for(i=0; i<TRANSFER_BUFFER_SIZE; ++i)
142
    tx_buffer[i] += 0x10;
143
}
144
 
145
static void TASK_SPI_Handler(void *pvParameters)
146
{
147
  portTickType xLastExecutionTime;
148
 
149
  // Initialise the xLastExecutionTime variable on task entry
150
  xLastExecutionTime = xTaskGetTickCount();
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
 
159
  while( 1 ) {
160
    // wait for 100 mS
161
    vTaskDelayUntil(&xLastExecutionTime, 100 / portTICK_RATE_MS);
162
 
163
    // toggle Status LED to as a sign of live
164
    MIOS32_BOARD_LED_Set(1, ~MIOS32_BOARD_LED_Get());
165
 
166
    // activate CS pin
167
    MIOS32_SPI_RC_PinSet(MASTER_SPI, MASTER_CS_PIN, 0); // spi, rc_pin, pin_value
168
 
169
    // send data non-blocking
170
    MIOS32_SPI_TransferBlock(MASTER_SPI, tx_buffer, rx_buffer, TRANSFER_BUFFER_SIZE, SPI_Callback);
171
  }
172
}