Author Topic: SAMD51 - AtmelStart CDC ACM Read Example?  (Read 281 times)

0 Members and 1 Guest are viewing this topic.

Offline MajorEETopic starter

  • Contributor
  • Posts: 17
SAMD51 - AtmelStart CDC ACM Read Example?
« on: July 06, 2024, 07:10:45 pm »
Does anyone have an example of a CDC Read using the CDC serial port middleware?

As soon as I invoke cdcf_acm_read() and send it something via TeraTerm my board disconnects via USB. Not sure how to troubleshoot it.

I can send just fine, but not receive.

Code: [Select]
#include <atmel_start.h>

int main(void)
{
/* Initializes MCU, drivers and middleware */
atmel_start_init();

char cdc_output_buffer[100] = {0};
int counter = 0;
int buffer_length = 0;
while (1){

sprintf(cdc_output_buffer, "Test: %i (%i)\r\n", counter, strlen(cdc_output_buffer));
cdcdf_acm_write(cdc_output_buffer, strlen(cdc_output_buffer));
delay_ms(500);
read_and_print_cdc();
delay_ms(500);
counter++;
}
}

void read_and_print_cdc(){
char cdc_debug_buffer[50] = {0};
char cdc_read_buffer[50] = {0};
int32_t cdc_resp = 0;
cdc_resp = cdcdf_acm_read(&cdc_debug_buffer, sizeof(cdc_debug_buffer));
//sprintf(cdc_debug_buffer, "Read Buffer: %s (%i)\r\n", cdc_read_buffer, strlen(cdc_debug_buffer));
//cdcdf_acm_write(cdc_debug_buffer, strlen(cdc_debug_buffer));
}
 

Offline MajorEETopic starter

  • Contributor
  • Posts: 17
Re: SAMD51 - AtmelStart CDC ACM Read Example?
« Reply #1 on: July 07, 2024, 04:27:23 pm »
I just wanted to provide an update for those of you with similar problems. Look in the usb_start.c file provided. There is an example of an echo program that echos back what it receives.

The architecture is built entirely on callbacks. Apparently USB has a 64-byte payload maximum, too. Ringbuffers come up frequently in this sort of application when I was searching for longer inputs if needed.

Apparently after a write a read needs to be called to have an empty buffer "waiting".

The application notes on the USB CDC ACM are outdated at best and I found no documentation on the provided functions. I have however managed to get the example to function.

Things to check:
- 1. Ensure USB is receiving 48MHz. If you're using the internal clock you need to turn on clock recovery mode.
- 2. Main CPU clock has to be fast enough to process the incoming transaction. I think I managed to get it as low as 24MHz. Lower than that it was not reliable at least with my configuration (based on internal oscillator).
- 3. Ensure that clocks are actually enabled. There is a checkbox. Easy to miss.
- 4. In prior versions (SAMD11) there is an interrupt on the USB driver that prevented operation of the middleware. Check that if you run into weird behavior.
- 5. If configured properly the USB will enumerate as a Atmel example device. (Check device manager in windows). You can change the enumeration in the middleware options. 0x0 I think it was is the generic VID that will remove the company name there. Not sure the ramifications of providing this VID. USB-IF has a registration process. I'm only doing testing as a hobby project so not worried about this.
- 6. If you are working on a big project, make a copy of it, launch that, and work on getting the basics of USB working. ("Smallest reproducible example"). This will remove a lot of headache.

Hopefully this helps someone in a similar situation.

Helpful resources (Some literal plug and play examples here):

- https://github.com/davepruitt/samd_tutorials -
- https://gitlab.com/nystrom.fredric/samd51-asf4-usbserial/-/tree/master?ref_type=heads - SAMD51 ASF4 USB Serial
- - SAMD51 with Atmel Start: a better USB virtual serial port ( David Pruitt)

Applicability: (What I used to get to this point)
- Microchip Studio V7.0.2594 / Atmel Kits 7.0.132
- Project created with Atmel Start
- Created project July 2024 using "latest" from Microchip Studio.
- May not be applicable to MPLab/Harmony IDE with Atmel plugin. Have not tried it.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf