Sampling audio via UART (with PEx) with KL26Z64

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sampling audio via UART (with PEx) with KL26Z64

Jump to solution
1,067 Views
gustavsl
Contributor III

Hi. I'm using a custom board with KL26Z64 and I'm trying to make a program that measures the ADC (to which a mic is connected) and sends the value via UART (serial) with a certain sample rate (in this case, 10kHz) indefinitely.

I want to capture audio to do some processing on a PC later.

I started a project with Processor Expert and added the ADC, Term (which uses AsynchroSerial) and TimerInt components. I set the timer interrupt period to 100us.

The baud rate is set to 38400 baud.

My ADC total conversion time is set to 11.68us and I'm running 3 conversions.

My code to do this task is as follows (on the timer interrupt event):

void TI1_OnInterrupt(void)

{

    AD1_Measure(TRUE);

    AD1_GetValue16(&leitura);

    Term1_SendNum(leitura);

    Term1_SendStr("\r\n");

}

As a result, I get the ADC values on my serial monitor. However, when I import these values to, say, MATLAB for processing, I see that the audio was not sampled at the correct rate.

What would be the most efficient way to do this?

Thanks,

Gustavo.

Labels (1)
1 Solution
626 Views
mjbcswitzerland
Specialist V

Gustav

The USBDM includes a USB-CDC to UART bridge to the KL25 and so the KL25 is communicating via UART. This means that the USBDM is not a design reference in this sense.

In the uTasker project you would send a sample to the UART output by changing the code from your version (although preferably not located in the IRQ):

Your code:

    AD1_Measure(TRUE); 

    AD1_GetValue16(&leitura); 

    Term1_SendNum(leitura); 

    Term1_SendStr("\r\n");

to

uTasker code:

    AD1_Measure(TRUE); 

    AD1_GetValue16(&leitura); 

    fnDebugHex(leitura, (sizeof(leitura) | WITH_CR_LF)); // per default this is sent to the UART debug output


To send it to a USB-CDC output instead USB-CDC would be added to the project using the defines:

#define USB_INTERFACE

#define USE_USB_CDC

#define USB_CDC_COUNT   1                  (can be 1 to 7 on the Kinetis)


Then the code would redirect to a USB-CDC interface instead by doing:


DebugHandle = USBPortID_comms[0]; // where there can be up to 7 to choose from

    AD1_Measure(TRUE); 

    AD1_GetValue16(&leitura); 

    fnDebugHex(leitura, (sizeof(leitura) | WITH_CR_LF)); // this is now set to the defined USB-CDC connection

So to use USB-CDC in the uTasker project you need to add one line of code to set the output to be used (after enabling the framework support with three defines).

See also the FRDM-KL25Z binaries at µTasker Kinetis FRDM-KL25Z support which includes a 3 way USB-CDC composite UART bridge configuration that you can try on your FRDM-KL25Z.

Regards

Mark

Kinetis: µTasker Kinetis support

KL26: µTasker Kinetis FRDM-KL26Z support / µTasker Kinetis Teensy LC support

ADC/DAC: http://www.utasker.com/docs/uTasker/uTaskerADC.pdf

USB User's Guide: http://www.utasker.com/docs/uTasker/USB_User_Guide.PDF

Composite USB: µTasker USB Device Configuration

For the complete "out-of-the-box" Kinetis experience and faster time to market


View solution in original post

5 Replies
626 Views
mjbcswitzerland
Specialist V

Hi Gustav

If you send a single sample point as ASCII you need to send 6 characters (eg. "0123\r\n").

At 38400 Baud this takes about (286us * 6) = 1.7ms

This means that the highest sample rate that you can achieved at the moment is 581Hz for a single ADC channel.

Note that the PEx generated code will probably be blocking code so no further interrupts can be handled until the serial transission has completed.

Therefore you certainly need much faster data transfer that 38400 Baud ( >660kBaud for a single channel); USB-CDC would probably be more suitable, which can easily achieve 8 MBaud.

However I wouldn't send from the ADC interrupt routine but instead use DMA to a buffer and then send from the buffer (which can bridge short periods where transmission may be delayed).

Complete solutions for such tasks are included in the uTasker project.

Regards

Mark

Kinetis: µTasker Kinetis support

KL26: µTasker Kinetis FRDM-KL26Z support / µTasker Kinetis Teensy LC support

ADC/DAC: http://www.utasker.com/docs/uTasker/uTaskerADC.pdf

USB User's Guide: http://www.utasker.com/docs/uTasker/USB_User_Guide.PDF

Composite USB: µTasker USB Device Configuration

For the complete "out-of-the-box" Kinetis experience and faster time to market

626 Views
gustavsl
Contributor III

Hi, Mark. I'm using a FRDM-KL25Z board as a debugger (with USBDM). I know USBDM offers CDC support. How would I implement it using uTasker?

Thanks,

Gustavo.

0 Kudos
627 Views
mjbcswitzerland
Specialist V

Gustav

The USBDM includes a USB-CDC to UART bridge to the KL25 and so the KL25 is communicating via UART. This means that the USBDM is not a design reference in this sense.

In the uTasker project you would send a sample to the UART output by changing the code from your version (although preferably not located in the IRQ):

Your code:

    AD1_Measure(TRUE); 

    AD1_GetValue16(&leitura); 

    Term1_SendNum(leitura); 

    Term1_SendStr("\r\n");

to

uTasker code:

    AD1_Measure(TRUE); 

    AD1_GetValue16(&leitura); 

    fnDebugHex(leitura, (sizeof(leitura) | WITH_CR_LF)); // per default this is sent to the UART debug output


To send it to a USB-CDC output instead USB-CDC would be added to the project using the defines:

#define USB_INTERFACE

#define USE_USB_CDC

#define USB_CDC_COUNT   1                  (can be 1 to 7 on the Kinetis)


Then the code would redirect to a USB-CDC interface instead by doing:


DebugHandle = USBPortID_comms[0]; // where there can be up to 7 to choose from

    AD1_Measure(TRUE); 

    AD1_GetValue16(&leitura); 

    fnDebugHex(leitura, (sizeof(leitura) | WITH_CR_LF)); // this is now set to the defined USB-CDC connection

So to use USB-CDC in the uTasker project you need to add one line of code to set the output to be used (after enabling the framework support with three defines).

See also the FRDM-KL25Z binaries at µTasker Kinetis FRDM-KL25Z support which includes a 3 way USB-CDC composite UART bridge configuration that you can try on your FRDM-KL25Z.

Regards

Mark

Kinetis: µTasker Kinetis support

KL26: µTasker Kinetis FRDM-KL26Z support / µTasker Kinetis Teensy LC support

ADC/DAC: http://www.utasker.com/docs/uTasker/uTaskerADC.pdf

USB User's Guide: http://www.utasker.com/docs/uTasker/USB_User_Guide.PDF

Composite USB: µTasker USB Device Configuration

For the complete "out-of-the-box" Kinetis experience and faster time to market


626 Views
gustavsl
Contributor III

Thanks, Mark. I'll try this approach (it'll be my first time using uTasker). It seems ideal.

May I get back to you in case of any issues (or in the uTasker community forum)?

Thanks,

Gustavo.

0 Kudos
626 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

From customer description, the ADC sampling rate is 10KHz, and each sampling point will do three conversion.

When one sampling point ADC conversion done, it will transfer sampling point conversion result (three conversion results) to UART.

So, the three conversion result will make the sampling frequency modified, which is not 10KHz.

Customer just need to adjust the three ADC conversion results to one and send to UART port, which could get the 10KHz sampling rate.

Wish it helps.


Have a great day,
best regards,

Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------