-
Notifications
You must be signed in to change notification settings - Fork 0
/
samp.c
198 lines (166 loc) · 6.21 KB
/
samp.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// ---------- Sampling Interrupt --------------
// Sample and send one data packet
// TIM1 generates hardware CNV pulse at 2ms period
// RPA is sampled at CNV edge and held by external ADC
// Sample SWPMON internal ADC
// Output next sweep value to DAC
// Read external ADC via SPI
// Sample HK internal ADC
#ifndef STM32F0 // helps Xcode resolve headers
#define STM32F0
#endif
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/timer.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/spi.h>
#include <libopencm3/stm32/adc.h>
#include <libopencm3/stm32/dac.h>
#include "uart.h"
#include "samp.h"
#include "HK.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
#define SYNC 0xBB
// **** GLOBALS used by sample ISR ***
extern long pakcount;
extern long sweeptable[];
extern long step;
extern long nsteps;
extern long flip;
int counter = 15;
extern uint32_t period; // step period (for setup)
unsigned binary = 0;
// ***** TIM1 ISR interrupts at rising edge of CNV *******************
void tim1_cc_isr(void)
{
SWP_convert(); // sample sweep before changing it
// while (!(ADC_ISR(ADC1) & ADC_ISR_EOS)); // wait for end of sampling
// update sweep DAC
dac_load_data_buffer_single(DAC1, sweeptable[step], DAC_ALIGN_RIGHT12, DAC_CHANNEL2);
if ((step <= 0) || step >= (nsteps - 1))
flip = -flip; // up-down sweep
step += flip; // next step
// Packet format: SYNC,SEQ, RPAm,RPAl, SWPm,SWPl, HKm,HKl
// putch(SYNC); // Send start packet
// putch(pakcount & 0xFF); // Send SEQ byte
// Assume external ADC conversion is complete by now (needs 900ns)
SPI1_DR = 0x1; // read out external ADC via SPI
while (!(SPI1_SR & SPI_SR_RXNE))
; // wait for SPI transfer complete
////////////////VVV NEW CODE VVV//////////////////////////////////
// old raw: raw value that we have been using in the past, straight from SPI1_DR.
////////////////VVV prints old raw in binary VVV//////////////////////////////////
// unsigned n = SPI1_DR;
// unsigned i;
// int zero = 0;
// int one = 0;
// // decimal --> binary function
// for (i = 1 << 15; i > 0; i = i / 2)
// {
// (n & i) ? putch('1') : putch('0');
// }
// putch('\r');
// putch('\n');
// putch('\n');
////////////////^^^ prints old raw in binary ^^^//////////////////////////////////
////////////////VVV prints old raw in decimal VVV//////////////////////////////////
// unsigned n = SPI1_DR;
// char str[16];
// sprintf(str, "%d", n);
// for (int i = 0; i < 5; i++)
// {
// putch(str[i]);
// }
// putch('\r');
// putch('\n');
// putch('\n');
////////////////^^^prints old raw in decimal ^^^//////////////////////////////////
// new raw: I noticed that the 16 bits of the old raw were always all 1s or all 0s.
// So, I took those 16 bits and interpreted them as 1 bit.
// 16 bits of new raw = 256 bits of old raw.
////////////////VVV prints new raw in binary VVV//////////////////////////////////
// unsigned n = SPI1_DR;
// if (n > 32766)
// {
// putch('1');
// }
// else
// {
// putch('0');
// }
// if (counter <= 0)
// {
// putch('\r');
// putch('\n');
// putch('\n');
// counter = 15;
// binary = 0;
// }
// counter--;
////////////////^^^ prints new raw in binary ^^^//////////////////////////////////
////////////////VVV prints new raw in decimal VVV//////////////////////////////////
// unsigned n = SPI1_DR;
// if (n > 32766)
// {
// // putch('1');
// int power = 1;
// for (int i = 0; i < counter; i++) {
// power = power * 2;
// }
// binary += power;
// }
// if (counter <= 0)
// {
// char str[16];
// sprintf(str, "%d", binary);
// for (int i = 0; i < 16; i++)
// {
// putch(str[i]);
// }
// putch('\r');
// putch('\n');
// putch('\n');
// counter = 15;
// binary = 0;
// }
// counter--;
////////////////^^^ prints new raw in decimal ^^^//////////////////////////////////
////////////////^^^ NEW CODE ^^^//////////////////////////////////
// putswab(SPI1_DR); // Send RPA sample (SPI readout has bytes swapped)
// extern ADC readout completed. Force CNV low
TIM1_CCMR1 = TIM_CCMR1_OC1M_FORCE_LOW; // (assumes all other bits are zero)
TIM1_CCMR1 = TIM_CCMR1_OC1M_TOGGLE;
// putwd(SWP_read()); // Send SWP sample from prev SWP_convert()
// putwd(HK_samp()); // Send one of 8 different HK vals muxed by lsbs of pakcount
pakcount++;
TIM1_SR = ~TIM_SR_CC1IF; // clear interrupt
}
// ********************************************************************
// Set up SPI and timer for sampling
// Assumes GPIO bits are already set up
void StartSAMP(void)
{
// Set up SPI interface to external ADC and DAC
spi_reset(SPI1);
// 10MHz. CPOL=0 clock normally lo. CPHA=1 clock on falling edge.
spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_4, SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE, SPI_CR1_CPHA_CLK_TRANSITION_1, SPI_CR1_MSBFIRST);
spi_set_data_size(SPI1, 16);
spi_enable_software_slave_management(SPI1);
spi_set_nss_high(SPI1);
spi_enable(SPI1);
// Set up TIMER 1 to generate CNV pulse
timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // FIXME THIS IS NEW
// timer_reset(TIM1);
timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_TOGGLE); // TIM1_CH1 CNV
timer_enable_oc_output(TIM1, TIM_OC1);
timer_enable_break_main_output(TIM1);
timer_set_oc_value(TIM1, TIM_OC1, period >> 1);
timer_set_prescaler(TIM1, 7); // need prescale TIM1 is 16-bit
timer_set_period(TIM1, period - 1);
// interrupt on CNV leading edge
timer_generate_event(TIM1, TIM_EGR_CC1G | TIM_EGR_TG);
nvic_enable_irq(NVIC_TIM1_CC_IRQ);
timer_enable_irq(TIM1, TIM_DIER_CC1IE);
TIM_CR1(TIM1) |= TIM_CR1_CEN; // Start timer
}