CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Porting stepper motor library for TMC2209 driver
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
WrySun



Joined: 03 Jan 2023
Posts: 14

View user's profile Send private message

PostPosted: Thu Jan 19, 2023 2:24 am     Reply with quote

I imagine most applications, including mine, will be fine just writing to the driver. Reading is mostly for error checking, such as the IFCNT register that increments on each successful write datagram. Or checking the state of the pins with the IOIN register. I've tried reading these as well but driver still refuses to respond.

It's understandable I get the datagram echoed back on the Rx pin since it's connected to the Tx pin, and that's a good way to know that both pins are working as intended. If I got any reply I would change this function to throw away those echo bytes and only keep the reply datagram. I've tried waiting different amounts of time but the 2209 driver never responds so I think it must find something wrong with my datagram.

Here's the CRC function, slightly changed from the one in the datasheet to give the checksum as a return value instead:

Code:
uint8_t calcCRC(uint8_t *datagram, uint8_t datagram_size)
{
  uint8_t crc = 0;
  uint8_t currentByte;
  for (uint8_t i=0; i<(datagram_size - 1); i++)
  {
    currentByte = datagram[i];
    for (uint8_t j=0; j<8; j++)
    {
      if ((crc >> 7) ^ (currentByte & 0x01)) {
        crc = (crc << 1) ^ 0x07;
      }
      else { crc = crc << 1; }
      currentByte = currentByte >> 1;
    }
  }
  return crc;
}


I'll post back if I find the solution since this seems to be the only thread on the TMC2209 driver.
WrySun



Joined: 03 Jan 2023
Posts: 14

View user's profile Send private message

PostPosted: Thu Jan 19, 2023 2:30 am     Reply with quote

Ttelmah wrote:
I did manage to read it.
Was not using a PIC for this, but the way it was done was that the receive
interrupt routine, was given the count of sent bytes, and it simply read
and threw away each 'sent' byte. What was left was the stuff being
returned. The main code only received these bytes.
Reading is not needed for 90% of stuff. However there are some useful
things like the temperature and load statuses. Also you could read what
current was actually being delivered.


Awesome. Could you please give me the datagram you sent in binary so that I can copy it and see if I get the same response?

My datagram for reading the GCONF register ended up like this but I get no response with it:
00000101 00000000 00000000 01001000
Sync=5, slave address=0, register address+read bit=0 and CRC=72.

My read datagram looks like this
Code:
typedef union {
    uint8_t data[4];
    struct {
        uint8_t sync;
        uint8_t slave;
        TMC_addr_t addr;
        uint8_t crc;
    } msg;
} TMC_uart_read_datagram_t;

typedef union {
    uint8_t value;
    struct {
        uint8_t
        idx   :7,
        write :1;
    };
} TMC_addr_t;
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Jan 19, 2023 7:30 am     Reply with quote

Are you sure you have the wiring right?.

Understand it is the RX pin on the PIC, that has to connect to the UART
pin on the TMC2209. If you connect the TX pin, you won't be able to get
any data back. Sounds exactly like what you are seeing.
You need to check which of the pins on your 'module' actually connects
to the UART pin on the TMC. Suspect you may have to reverse your TX
and RX connections.

Yes, I think I'm right on this. See this thread, which is using a module:

[url]
https://github.com/teemuatlut/TMCStepper/issues/87
[/url]

It seems that the people making the module have labelled the lines
as TX and RX to go to the processor's TX and RX.

Duh!...
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jan 19, 2023 8:31 am     Reply with quote

Sigh, here we are thinking it's a software / code issue when really,as Mr T says, it is probably a simple wiring error....

BTW, I'll never understand why 2 and 3 got swapped on real Rs-232 comports..grrr sorry, I digress....

I had a look at the PDF and yes, the 'UART single wire ' pin is supposed to be connected to the RX pin of the PIC, clear as day. well not here...I have LOTS of freezing rain coming down...yuck......

sadly , I may look for a 12F683 to make a 19KHz generator.....
am I nutz ???
WrySun



Joined: 03 Jan 2023
Posts: 14

View user's profile Send private message

PostPosted: Fri Jan 20, 2023 1:35 am     Reply with quote

Ttelmah wrote:
Are you sure you have the wiring right?.

Understand it is the RX pin on the PIC, that has to connect to the UART
pin on the TMC2209. If you connect the TX pin, you won't be able to get
any data back. Sounds exactly like what you are seeing.
You need to check which of the pins on your 'module' actually connects
to the UART pin on the TMC. Suspect you may have to reverse your TX
and RX connections.

Yes, I think I'm right on this. See this thread, which is using a module:

[url]
https://github.com/teemuatlut/TMCStepper/issues/87
[/url]

It seems that the people making the module have labelled the lines
as TX and RX to go to the processor's TX and RX.

Duh!...


Yes, this may be the mistake.

The TMC2209 v3.0 module I bought has an RX and a TX pin (indicating the 1k resistor is included on the module), but the resistance between these pins is much higher than 1k ohm. I measured between other pins to discover that the pin labeled CLK is the actual TX pin and the pin labled TX turns out to be an input pin used to switch between stealth and spread modes of the driver. The seller did not list this mistake but I've confirmed this to be the case through some Googling.

However, despite finding this out I still could not get read working. My PIC RX is connected to the module's TX and my PIC TX is connected to the module's RX. It is possible I just need to reverse these pins. I had assumed my PIC TX should be connected to the module's RX, but perhaps the module manufacturer did not see it this way.

Thanks for the suggestion!
WrySun



Joined: 03 Jan 2023
Posts: 14

View user's profile Send private message

PostPosted: Fri Jan 20, 2023 2:11 am     Reply with quote

Yes, I tested reversing the pins and can confirm this was my mistake. Read and write both work properly now which is a great relief.

This is the TMC2209 v3.0 module I'm using:
https://www.aliexpress.com/item/1005004018605377.html

For anyone else struggling with this issue, on this module the 1k resistor is integrated, but:
PIC UART1 RX (C7 on my PIC) must be connected to module's RX pin (labeled RX)
PIC UART1 TX (C6 on my PIC) must be connected to module's TX pin (mislabeled CLK)
Pin incorrectly labled TX is actually an input pin where LOW=StealthChop and HIGH=SpreadCycle (these driver modes can be switched between using UART too)

Thanks a lot for the help on this!


Last edited by WrySun on Fri Jan 20, 2023 2:18 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Jan 20, 2023 2:15 am     Reply with quote

Brilliant. Very Happy

It was funny, I found other people having the same problem online
as you saw in the link I gave. The 'designers' of these modules seem
to have a slightly warped idea of how things should be labelled!....

Best Wishes
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jan 20, 2023 7:32 am     Reply with quote

NICE... problem solved...
just a 'silly' wiring error and not some ugly, crazy,quirky software 'issue'.
I KNOW how easy it is to swap C6 and C7 pins. Got into the habit to use Red wire for Receive, Green for Go (transmit), relative to the PIC.

It's actually a good idea to read the entire 'configuration' of the modules. I don't know if there's a 'restore to factory' method, but it'd be nice. Say you custom configured a module,used it , then put on shelf as a spare, months later, use on new project,power up and 'oopsy' things go really weird !!
being able to READ back is,well, important !
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group