Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DSP_200kHz by
Revision 51:43143a3fc2d7, committed 2015-01-31
- Comitter:
- timmey9
- Date:
- Sat Jan 31 20:17:58 2015 +0000
- Parent:
- 50:33524a27e08c
- Child:
- 52:5a40cc58c4c2
- Commit message:
- Cleaned up the code a little bit.
Changed in this revision
--- a/adc.cpp Sat Jan 31 19:46:00 2015 +0000
+++ b/adc.cpp Sat Jan 31 20:17:58 2015 +0000
@@ -14,11 +14,16 @@
single vs continuous mode
does asynchronous clock have to do with this?
*/
-DigitalOut toggle1(PTC16);
+DigitalOut toggle1(PTA0);
DigitalOut green(LED_GREEN);
DigitalOut red(LED_RED);
-Serial debug(USBTX,USBRX);
+//Serial debug(USBTX,USBRX);
+
+
+/* The ADCs are setup so that ADC0 and ADC1 are triggered by the PDB.
+ * When the conversions are complete, ADC0 and ADC1 then trigger DMA0
+ * and DMA1, respectively. */
void adc_init()
{
// red, indicating now ready
@@ -32,7 +37,6 @@
// Set ADC hardware trigger to PDB0
SIM_SOPT7 = SIM_SOPT7_ADC0TRGSEL(0); // Select triggering by PDB and select pre-trigger A
SIM_SOPT7 = SIM_SOPT7_ADC1TRGSEL(0); // Select triggering by PDB and select pre-trigger A
- debug.printf("SIM_SOPT7: 0x%08x\r\n",SIM_SOPT7); //(0x00000000)
// Setup Configuration Register 1
ADC0_CFG1 = 0; // clear register
@@ -123,7 +127,8 @@
ADC1_SC1A = 0; // clear register
ADC1_SC1A &= ~ADC_SC1_DIFF_MASK; // select single-ended mode
ADC1_SC1A |= ADC_SC1_AIEN_MASK; // enable interrupt (for debugging)
- ADC1_SC1A |= ADC_SC1_ADCH(14); // select channel 13
+ ADC1_SC1A |= ADC_SC1_ADCH(14); // select channel 14
+
// Check if ADC is finished initializing TODO: This part doesn't seem right, but I did it according to 871
while( (ADC0_SC1B&ADC_SC1_COCO_MASK)) {}
@@ -136,22 +141,25 @@
red = 1;
green = 0;
- /*
- debug.printf("ADC0_SC1a: %08x\r\n",ADC0_SC1A); //(0x0000004d)
- debug.printf("ADC0_SC1b: %08x\r\n",ADC0_SC1B); //(0x0000001f)
- debug.printf("ADC0_CFG1: %08x\r\n",ADC0_CFG1); //(0x0000000c)
- debug.printf("ADC0_CFG2: %08x\r\n",ADC0_CFG2); //(0x00000004)
- debug.printf("ADC0_RA: %08x\r\n",ADC0_RA); //(0x00000000)
- debug.printf("ADC0_RB: %08x\r\n",ADC0_RB); //(0x00000000)
- debug.printf("ADC0_SC2: %08x\r\n",ADC0_SC2); //(0x00000044)
- debug.printf("ADC0_SC3: %08x\r\n\n",ADC0_SC3); //(0x00000000)
- */
-
// Enable the ISR vector
//NVIC_SetVector(ADC0_IRQn, (uint32_t)&ADC0_IRQHandler);
//NVIC_EnableIRQ(ADC0_IRQn);
}
+
+void ADC0_IRQHandler() {
+
+ toggle1 = !toggle1;
+}
+
+
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * DEBUG: This is supposed to put the ADC in continuous *
+ * mode so it samples without the PCB. But for *
+ * some reason it isn't working. I haven't deleted *
+ * it just in case it is needed for debug purposes *
+ * in the future. *
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void adc_start() {
// reset DMA
dma_reset();
@@ -169,6 +177,13 @@
ADC1_SC1A |= ADC_SC1_ADCH(14); // write to SC1A causing a trigger
}
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * DEBUG: This is supposed to revert back from adc_start() *
+ * but because adc_start() isn't working, this *
+ * function is good for nothing. I held on to *
+ * it just in case it is needed for debug purposes *
+ * in the future. *
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void adc_stop() {
// set ADC to hardware trigger
ADC0_SC2 |= ADC_SC2_ADTRG_MASK;
@@ -179,7 +194,12 @@
ADC1_SC3 &= ~ADC_SC3_ADCO_MASK;
}
-// DEBUG: this function causes the continuous sampling to break
+/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+ * DEBUG: This is supposed to trigger a software conversion *
+ * and take a single sample. However, it only *
+ * worked for ADC1 for some reason. It is here for *
+ * possible debug purposes in the future. *
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void adc_single_sample() {
ADC0_SC3 &= ~ADC_SC3_ADCO_MASK; // single conversion mode
ADC1_SC3 &= ~ADC_SC3_ADCO_MASK; // single conversion mode
@@ -193,7 +213,14 @@
ADC1_SC2 |= ADC_SC2_ADTRG_MASK; // set ADC to software trigger
}
-void ADC0_IRQHandler() {
-
- toggle1 = !toggle1;
-}
\ No newline at end of file
+
+/*
+ debug.printf("ADC0_SC1a: %08x\r\n",ADC0_SC1A); //(0x0000004d)
+ debug.printf("ADC0_SC1b: %08x\r\n",ADC0_SC1B); //(0x0000001f)
+ debug.printf("ADC0_CFG1: %08x\r\n",ADC0_CFG1); //(0x0000000c)
+ debug.printf("ADC0_CFG2: %08x\r\n",ADC0_CFG2); //(0x00000004)
+ debug.printf("ADC0_RA: %08x\r\n",ADC0_RA); //(0x00000000)
+ debug.printf("ADC0_RB: %08x\r\n",ADC0_RB); //(0x00000000)
+ debug.printf("ADC0_SC2: %08x\r\n",ADC0_SC2); //(0x00000044)
+ debug.printf("ADC0_SC3: %08x\r\n\n",ADC0_SC3); //(0x00000000)
+ */
\ No newline at end of file
--- a/adc.h Sat Jan 31 19:46:00 2015 +0000 +++ b/adc.h Sat Jan 31 20:17:58 2015 +0000 @@ -4,28 +4,22 @@ #include "mbed.h" #include "dma.h" -/*** - * Sets ADC to continuous conversion mode with software trigger, then - * triggers the ADC. - **/ -void adc_start(); /*** - * Sets ADC to single conversion mode with hardware trigger. So the - * ADC stops unless triggered by a timer. - **/ -void adc_stop(); - -/*** - * Initializes the ADC to 16-bit single-ended CH13, hardware triggered + * Initializes the ADC to 16-bit single-ended, hardware triggered * with DMA enabled. **/ void adc_init(); /*** - * Toggles PTC16 when conversion completes + * Toggles PTA0 when conversion completes **/ void ADC0_IRQHandler(); -void adc_single_sample(); + + + +//void adc_single_sample(); +//void adc_start(); +//void adc_stop(); #endif /* ADC_H_ */ \ No newline at end of file
--- a/dma.cpp Sat Jan 31 19:46:00 2015 +0000
+++ b/dma.cpp Sat Jan 31 20:17:58 2015 +0000
@@ -8,9 +8,12 @@
uint16_t sample_array0[TOTAL_SAMPLES];
uint16_t sample_array1[TOTAL_SAMPLES];
uint16_t angle_array[TOTAL_SAMPLES];
-//DigitalIn AMT20_A(PTB18); // FTM2_QD_PHA, apparently the k64f has a quadrature decoder. look into this (page 264)
-//DigitalIn AMT20_B(PTB10); // FTM2_QD_PHB
+
+
+/* DMA0 and DMA1 are triggered by ADC0 and ADC1 (which are triggered
+ * by the PDB). However, DMA2 is triggered directly by the PDB. This
+ * is becuase DMA2 is reading FTM2, which cannot trigger the DMA. */
void dma_init()
{
// Enable clock for DMAMUX and DMA
@@ -22,12 +25,10 @@
DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(40); // ADC0
DMAMUX_CHCFG1 |= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(41); // ADC1
DMAMUX_CHCFG2 |= DMAMUX_CHCFG_ENBL_MASK | DMAMUX_CHCFG_SOURCE(48); // Set trigger source to PDB (Don't set DMA Trig Enable because that is for the PIT)
- /* Source number Source module Source description
+ /* Source number Source module
40 ADC0
41 ADC1
- 30 FTM2 Channel 0
- 31 FTM2 Channel 1
- 48 PDB -
+ 48 PDB
*/
@@ -85,9 +86,17 @@
DMA_TCD0_CSR = 0;
DMA_TCD1_CSR = 0;
DMA_TCD2_CSR = 0;
-
-
- /*pc.printf("DMA_CR: %08x\r\n", DMA_CR);
+}
+
+void dma_reset() {
+ // Set memory address for destinations back to the beginning
+ dma_init();
+}
+
+
+
+
+/*pc.printf("DMA_CR: %08x\r\n", DMA_CR);
pc.printf("DMA_ES: %08x\r\n", DMA_ES);
pc.printf("DMA_ERQ: %08x\r\n", DMA_ERQ);
pc.printf("DMA_EEI: %08x\r\n", DMA_EEI);
@@ -101,10 +110,4 @@
pc.printf("DMA_CINT: %02x\r\n", DMA_CINT);
pc.printf("DMA_INT: %08x\r\n", DMA_INT);
pc.printf("DMA_ERR: %08x\r\n", DMA_ERR);
- pc.printf("DMA_HRS: %08x\r\n", DMA_HRS);*/
-}
-
-void dma_reset() {
- // Set memory address for destinations back to the beginning
- dma_init();
-}
+ pc.printf("DMA_HRS: %08x\r\n", DMA_HRS);*/
\ No newline at end of file
--- a/main.cpp Sat Jan 31 19:46:00 2015 +0000
+++ b/main.cpp Sat Jan 31 20:17:58 2015 +0000
@@ -10,17 +10,16 @@
DigitalOut led_red(LED_RED);
DigitalOut led_green(LED_GREEN);
DigitalOut led_blue(LED_BLUE);
+DigitalOut quadA(PTC17);
+DigitalOut quadB(PTC16);
AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz
-DigitalOut quadA(PTC17);
-DigitalOut quadB(PTC16);
+
extern int len;
extern uint16_t sample_array0[];
extern uint16_t sample_array1[];
extern uint16_t angle_array[];
-bool switcher = false;
-
using namespace std;
int main() {
@@ -33,10 +32,10 @@
pc.baud(230400);
pc.printf("Starting\r\n");
- quad_init();
+ quad_init(); // initialize FTM2 to quadrature decoder
adc_init(); // always initialize adc before dma
- dma_init();
- pdb_init();
+ dma_init(); // initializes DMAs
+ pdb_init(); // initialize PDB0 as the timer for ADCs and DMA2
led_green = 1;
@@ -56,13 +55,6 @@
case 'g':
pc.printf("Quad Cnt: %i\r\n", quad_read());
break;
- /*
- case 'A':
- case 'a': // needs debugging
- adc_start();
- for(int i = 0; i < 1500; i++) asm("nop");
- adc_stop();
- // then proceed to 's' to display the array*/
case 'F': // clear the samples
case 'f':
for(int i = 0; i < len; i++) {sample_array0[i] = 0; sample_array1[i] = 0; angle_array[i] = 0;}
@@ -73,19 +65,8 @@
for(int i = 0; i < len; i++) pc.printf("%i: %f\t %f\t %i\r\n",i,sample_array0[i]*3.3/65535,sample_array1[i]*3.3/65535, angle_array[i]);
pc.printf("\r\n");
break;
- /*
- case 'D':
- case 'd':
- adc_single_sample(); // trigger ADCs, which trigger DMA
- DMA_TCD2_CSR |= DMA_CSR_START_MASK; // trigger encoder DMA
- break;*/
-
-
-
-
-
// Programmable Delay Block debug
case 'B':
case 'b':
--- a/pdb.cpp Sat Jan 31 19:46:00 2015 +0000
+++ b/pdb.cpp Sat Jan 31 20:17:58 2015 +0000
@@ -1,7 +1,11 @@
#include "pdb.h"
-Serial debug2(USBTX,USBRX);
+//Serial debug2(USBTX,USBRX);
+
+/* The PDB is setup to run continuous (when enabled) so it will
+ * periodically trigger the ADCs to sample and trigger DMA2 to
+ * save the quadrature angle. The PDB can be started and stopped. */
void pdb_init() {
// initialize the Programmable Delay Block
@@ -32,8 +36,27 @@
| PDB_SC_MULT(0) // Multiplication factor 20 for the prescale divider for the counter clock
| PDB_SC_CONT_MASK // Contintuous, rather than one-shot, mode
| PDB_SC_LDOK_MASK; // Need to ok the loading or it will not load certain regsiters!
- //PDB0_SC &= ~PDB_SC_CONT_MASK; // enable one-shot mode for debugging.
+ //NVIC_SetVector(PDB0_IRQn, (uint32_t)&PDB0_IRQHandler);
+ //NVIC_EnableIRQ(PDB0_IRQn);
+}
+
+void PDB0_IRQHandler() {
+
+}
+
+void pdb_start() {
+ PDB0_SC |= PDB_SC_PDBEN_MASK; // PDB enabled
+ PDB0_SC |= PDB_SC_SWTRIG_MASK; // enable software trigger (start the PDB)
+}
+
+void pdb_stop() {
+ //PDB0_SC &= ~PDB_SC_CONT_MASK; // change to one-shot mode, so pdb will stop after current count
+ PDB0_SC &= ~PDB_SC_PDBEN_MASK; // PDB disabled
+}
+
+
+/*
debug2.printf("PDB0_SC: %08x\r\n",PDB0_SC);
debug2.printf("PDB0_MOD: %08x\r\n",PDB0_MOD);
debug2.printf("PDB0_CNT: %08x\r\n",PDB0_CNT);
@@ -54,24 +77,4 @@
debug2.printf("PDB0_PO0DLY: %08x\r\n",PDB0_PO0DLY);
debug2.printf("PDB0_PO1DLY: %08x\r\n",PDB0_PO1DLY);
debug2.printf("PDB0_PO2DLY: %08x\r\n\n",PDB0_PO2DLY);
-
- //NVIC_SetVector(PDB0_IRQn, (uint32_t)&PDB0_IRQHandler);
- //NVIC_EnableIRQ(PDB0_IRQn);
-}
-
-
-void PDB0_IRQHandler() {
- //switcher = !switcher;
-
-}
-
-
-void pdb_start() {
- PDB0_SC |= PDB_SC_PDBEN_MASK; // PDB enabled
- PDB0_SC |= PDB_SC_SWTRIG_MASK; // enable software trigger (start the PDB)
-}
-
-void pdb_stop() {
- //PDB0_SC &= ~PDB_SC_CONT_MASK; // change to one-shot mode, so pdb will stop after current count
- PDB0_SC &= ~PDB_SC_PDBEN_MASK; // PDB disabled
-}
\ No newline at end of file
+ */
\ No newline at end of file
--- a/pdb.h Sat Jan 31 19:46:00 2015 +0000 +++ b/pdb.h Sat Jan 31 20:17:58 2015 +0000 @@ -7,8 +7,6 @@ #include "mbed.h" -extern bool switcher; - void pdb_init(); void pdb_start(); void pdb_stop();

