-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
146 lines (120 loc) · 4.11 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* main.c
*
* Author: Caroline Lichtenberger
*
* NOTE 1: Exception of main.c and unless preceded by Opto in the header file name,
* header file is property of and authored by Atmel Corporation
* (see copyright notices in respective header/source files)
*/
#include <avr32/io.h>
#include <stdint.h>
#include "compiler.h"
#include "intc.h"
#include "interrupt/interrupt_avr32.h"
#include "dsp.h"
#include "avr32\uc3l064.h"
#include "avr32\adcifb_101.h"
#include "avr32\pdca_122.h"
#include "avr32\pm_411.h"
#include "avr32\scif_102.h"
#include "avr32\tc_2231.h"
#include "avr32\spi_211.h"
#include "OptoDMA.h"
#include "OptoADC.h"
#include "OptoSPI.h"
#include "OptoGPIOConfig.h"
#include "OptoClk.h"
#include "OptoTC.h"
#include "OptoFilter.h"
#define cpu_freq 30000000
#define pb_freq 30000000
#define rcmhz_freq 120000000
#define spi_freq 5000000
/************************************************************************/
/* Globals needed in both main() and ISR */
/************************************************************************/
// ADC interface register map
volatile avr32_adcifb_t *adc = &AVR32_ADCIFB;
// DMA storage location for ADC channels
uint16_t adc_dma_loc[6];
uint16_t summed_sig = 0;
// DMA interface register map
avr32_pdca_channel_t *dma_adc;
// SPI interface for DAC communication
volatile avr32_spi_t *spi = &AVR32_SPI;
uint32_t twim_addr = 0x60; // device address
// TC for timing interrupt
volatile avr32_tc_channel_t *tc = &AVR32_TC0.channel[1];
/************************************************************************/
/* ISR */
/************************************************************************/
__attribute__((__interrupt__))
static void opto_isr(void){
uint32_t temp = 0;
Disable_global_interrupt();
summed_sig = 0;
// Start ADC conversion
adc_start(adc);
summed_sig = sum_signal(adc_dma_loc);
write_spi(spi, summed_sig);
tc->idr = 0xFFFF;
temp = tc->idr;
initTC_interrupts(1, tc);
}
int main(void)
{
// Need to ensure no interrupts trigger while configuring system
Disable_global_interrupt();
// Configure clock source to run at 30 MHz with RC120MHz clock
// Default clock is RCSYS at 115 kHz
// Note: these variables are for debugging purposes (i.e. ensure you
// have correct clock settings)
volatile avr32_pm_t *pm_set = &AVR32_PM;
volatile avr32_scif_t *clk = &AVR32_SCIF;
initClock(clk, pm_set);
// initialize storage area
for(int i = 0; i < 6; ++i){
adc_dma_loc[i] = 0;
}
// init and enable DMA
init_adc_dma(AVR32_PDCA_PID_ADC_RX, dma_adc, adc_dma_loc);
// Configure and enable GPIO for ADC and SPI
assign_gpio_module(AVR32_ADCIFB_AD_0_PIN, AVR32_ADCIFB_AD_0_FUNCTION);
assign_gpio_module(AVR32_ADCIFB_AD_1_PIN, AVR32_ADCIFB_AD_1_FUNCTION);
assign_gpio_module(AVR32_ADCIFB_AD_2_PIN, AVR32_ADCIFB_AD_2_FUNCTION);
assign_gpio_module(AVR32_ADCIFB_AD_4_PIN, AVR32_ADCIFB_AD_4_FUNCTION);
assign_gpio_module(AVR32_ADCIFB_AD_5_PIN, AVR32_ADCIFB_AD_5_FUNCTION);
assign_gpio_module(AVR32_ADCIFB_AD_6_PIN, AVR32_ADCIFB_AD_6_FUNCTION);
assign_gpio_module(AVR32_SPI_SCK_0_0_PIN, AVR32_SPI_SCK_0_0_FUNCTION);
assign_gpio_module(AVR32_SPI_MOSI_0_0_PIN, AVR32_SPI_MOSI_0_0_FUNCTION);
assign_gpio_module(AVR32_SPI_MISO_0_0_PIN, AVR32_SPI_MISO_0_0_FUNCTION);
assign_gpio_module(AVR32_SPI_NPCS_0_0_PIN, AVR32_SPI_NPCS_0_0_FUNCTION);
// Initialize ADC
adc_init(adc);
// Enable DMA for ADC
enable_dma(AVR32_PDCA_PID_ADC_RX, dma_adc);
// Enable ADC channels
adc_enable(adc, AVR32_ADCIFB_CH0);
adc_enable(adc, AVR32_ADCIFB_CH1);
adc_enable(adc, AVR32_ADCIFB_CH2);
adc_enable(adc, AVR32_ADCIFB_CH4);
adc_enable(adc, AVR32_ADCIFB_CH5);
adc_enable(adc, AVR32_ADCIFB_CH6);
// Initialize SPI
initSPI(spi);
init_SPI_clk(spi, pb_freq, spi_freq);
enable_spi(spi);
// Initialize interrupt vectors
INTC_init_interrupts();
INTC_register_interrupt(&opto_isr, AVR32_TC0_IRQ1, AVR32_INTC_INT3);
// Initialize TC
initTC(1, tc);
set_TC_RC(1, tc);
initTC_interrupts(1, tc);
start_TC(1, tc);
// Wait for ISR (triggers every 0.021 ms or 48 kHz)
while(1){
// Wait for it....
}
}