XMEGA32e5 TWI interface - Makes the head hurt - NO MORE START!

Go To Last Post
87 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I attach it, you could try if using my built .elf makes a difference (some build problem on your side).

/Lars

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ahhh Ha.

I should start the analyser BEFORE starting debug session.  I get the same result!

 

Ok, this is a START, (pun intended)  I get teh address and an ACK, now to see what I need to do to satisfy the function in order to send a byte to data to the PCF to light up an LED.  The way I have it written is not sending the byte.

 

PROGRESS!!!

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, so as last reported I do get the address of the PCF and the WRITE bit, and an ACK.  But I do not get the '0X10' I am trying to send to set Bit 4 high on the PCF port.

 

I am trying to use this function in the START project:

i2c_error_t I2C_0_writeNBytes(i2c_address_t address, void *data, size_t len);

And my little code:

#include <atmel_start.h>
#include "i2c_simple_master.h"

uint8_t jim = 0x10;  //turns on a single bit
uint8_t *point;


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

	/* Replace with your application code */
	while (1) {
		I2C_0_writeNBytes(0x38, point,1);
	}
}

Which I thought would work, but I am not seeing the 0x10 on the logic analyser.

 

Time to go bang head against wall....

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

How much time does START actually save you?  Maybe they should call it STOP?? I prefer just to look a the datasheet, find the registers I need to use & use them...not sure what start is going to do for you beyond this...maybe set up some queues/buffers or state machine??

So what does using start for I2C work actually provide beyond what is already provided by the chip?

 

So far START has saved me from making solid progress on the damned thing.

 

Maybe they should call it STOP??

I did STOP.  I got tired of fighting with all the bloody files.

 

I prefer just to look a the datasheet, find the registers I need to use & use them

And I did just that!  Grabbed the data sheet and used a part right out of the tube with the 2 MHz internal clock and I came up with this:

/*
 * jims_xmega_twi_raw.c
 *
 * Created: 12/19/2019 8:18:40 PM
 * Author : jgmDESIGNS
 */ 
#define F_CPU 2000000UL
#include <avr/io.h>
#include <util/delay.h>


void TWI_SEND(uint8_t data)
{
	TWIC_MASTER_STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;    //force bus to IDLE state
	TWIC_MASTER_BAUD = 0x05;  //set SCL to 100khz
 	TWIC_MASTER_CTRLA = 0x08; //enable teh TWI module
	TWIC_MASTER_ADDR = 0x70;  //set teh address to the WRITE address of teh PCF8574A
	while((TWIC_MASTER_STATUS & TWI_MASTER_CLKHOLD_bm) == 0);   //wait for the flag to set before loading DATA
	TWIC_MASTER_STATUS = TWIC_MASTER_STATUS | 0X40;   //clear teh WIF flag
 	TWIC_MASTER_DATA = data;  //load some data
 	while((TWIC_MASTER_STATUS & TWI_MASTER_CLKHOLD_bm) == 0);  //wait for the flag to set
	TWIC_MASTER_STATUS = TWIC_MASTER_STATUS | 0X40;  //clear teh WIF flag
	
}

int main(void)
{
	
	
    /* Replace with your application code */
    while (1) 
    {
		TWI_SEND(0xAA);
		_delay_ms(500);
		TWI_SEND(0X55);
		_delay_ms(500);
		
    }
}

 

And it WORKED!!

 

But I run into an issue where I never see a stop condition.  According to the datasheet if I clear the interrupt flags by writing a 1 to them it will release the SCL line and clear the CLKHOLD flag, but it never clears the CLKHOLD line and I do not know why.

 

But HEY HO!  I got a blinking LED on my PCF8754A.  WITHOUT START!!!!!

 

YIPEEEE.

 

Some interesting facts:

 

1) without START, my little ugly duckling program that sort of works takes up 316 bytes of Program space and 0 bytes of Data memory.

 

2) with START, the program that I could not get running takes up 1998 bytes of Program Space and 78 Bytes of Data Memory

 

3) my little program only took 10 minutes to write and get going after about 20 minutes of reading the data sheets on the TWI in the XMEGA

 

4) the Fat B***ard START took about 15 minutes to configure in START, 10 seconds to generate, and 5 DAYS off and on to finally realise that I am banging my head into a wall....with the wall winning.

 

 

So, if anyone can help me figure out how to get the stop condition I would appreciate it.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Congratulations! I don't see a separate init function and you seem to re-init things every time you send but I'm not an expert so I will leave intelligent comments to others.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks John,

 

Yes, I init it everytime, and thats temporary as the original program I wrote wasa  one pass wonder.  Once I had that working I wanted it to blink the LED so I made the ugly duckling you see in my post.  For now I am leaving it as I toast my success with some tea.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Our TWI/I2C expert Mr Prentice will come along and fix things unless he has succumbed to the man flu. cheeky

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am sure David with thrash my success in some fashion, but I always learn something from him.

Hopefully he recovers soon....

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
So, if anyone can help me figure out how to get the stop condition I would appreciate it.

 

In the Xmega E Manual, section 18.11.3 CTRLC: 

   Use the CMD bits to send the stop condition.

 

See  AVR1308: Using the XMEGA TWI

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
with START, the program that I could not get running takes up 1998 bytes of Program Space and 78 Bytes of Data Memory

My observation with ASF was that its code tries to be completely generic - so it has everything necessary to control every single possible option, in every possible mode,  on every possible instance of the peripheral.

It also tends to explicitly set every option - even if the default is already what you want.

 

When one writes one's own code, one tends to focus on just the particular use-case at hand.

 

I think that probably explains (a lot of) the bloat ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Chuck99 wrote:

jgmdesign wrote:
So, if anyone can help me figure out how to get the stop condition I would appreciate it.

 

In the Xmega E Manual, section 18.11.3 CTRLC: 

   Use the CMD bits to send the stop condition.

 

See  AVR1308: Using the XMEGA TWI

 

 

 

Tried that and no good. Will try again though.
Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

so it has everything necessary to control every single possible option,

Jack of all trades, master of none??

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A bit like using printf (with the FP support) when all you really need is to print a few fixed strings.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

A bit like using printf (with the FP support) when all you really need is to print a few fixed strings.

 

 

Exactly!

 

Ok, back to my latest issue.

 

I still cannot seem to see a stop condition when the last byte is sent.  I would think the I2C controller would take care of this like it does on other AVR's but this XMEGA seems to be an entirely different beast.

 

Sigh....

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

 

This little nugget has a line at the end of every function:

 

TWI.MASTER.CTRLC = TWI_MASTER_CMD_STOP_gc;

 

I changed it to make Studio happy:

TWIC_MASTER_CTRLC = TWI_MASTER_CMD_STOP_gc;

 

and added it to my main loop, and moved the init stuff to its own function:

/*
 * jims_xmega_twi_raw.c
 *
 * Created: 12/19/2019 8:18:40 PM
 * Author : jgmDESIGNS
 */ 
#define F_CPU 2000000UL
#include <avr/io.h>
#include <util/delay.h>


void xmega_init(void)
{
	TWIC_MASTER_STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;
	TWIC_MASTER_BAUD = 0x05;
	TWIC_MASTER_CTRLA = 0x08;
}

void TWI_SEND(uint8_t data)
{
	
	TWIC_MASTER_ADDR = 0x70;
	while((TWIC_MASTER_STATUS & TWI_MASTER_CLKHOLD_bm) == 0);
	TWIC_MASTER_STATUS = TWIC_MASTER_STATUS | 0X40;
	TWIC_MASTER_DATA = data;
	while((TWIC_MASTER_STATUS & TWI_MASTER_CLKHOLD_bm) == 0);
	TWIC_MASTER_STATUS = TWIC_MASTER_STATUS | 0X40;
	
}

int main(void)
{
	xmega_init();
	
	
    /* Replace with your application code */
    while (1) 
    {
		TWI_SEND(0xAA);
		TWIC_MASTER_CTRLC = TWI_MASTER_CMD_STOP_gc;
		_delay_ms(500);
		TWI_SEND(0X55);
		TWIC_MASTER_CTRLC = TWI_MASTER_CMD_STOP_gc;
		_delay_ms(500);
		
		
    }
}

And it works!  Stop Condition has now been achieved!

YIPEEE!

 

Of course this is not a very pretty way to do things, but I am at least making respectable(in my world at least) progress.  Certainly a lot better than wading through the Starthole.

 

Now to see if I can read the PCF8574A and output to a PORT.

 

Cheers,

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good show. That is an example of the value of studying alternate code implementations. 

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Glad you're further down that road; may you arrive at the (a) destination.

 

P.S.

Recommend you add to the posted source code

  • copyright else no one has the right to copy your creation
  • license for how and why to copy

The most humorous license?

License:WTFPL-2 - Free Software Directory

 

P.P.S.

The above P.S. may be moot by AVR Freaks T&C.

edit :

Website Usage and Limitation of Liability | Microchip Technology

...

OWNERSHIP OF INFORMATION:

...

CONTENT:

...

OPEN SOURCE COMPONENTS:

...

via "Site Use Terms" (page bottom)

Indeed moot as the code is for XMEGA with code copyright to Microchip (not JIm); restrictions would be reduced by FLOSS.

 


via Fast, Deterministic, and Portable Counting Leading Zeros « State Space

 

"Dare to be naïve." - Buckminster Fuller

Last Edited: Tue. Dec 24, 2019 - 04:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
wading through the Starthole.

 

laugh

Letting the smoke out since 1978

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jim, I came across this app note that has code for xmega tw master, you may find it helpful.

AVR944 LED driver for AVR

 

Happy holidays

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gchapman wrote:
copyright else no one has the right to copy your creation

 

Laughable....I know I need to spruce it up as there are flags that one should monitor that I did not as I was trying to just get it going.

 

gchapman wrote:
The above P.S. may be moot by AVR Freaks T&C.

gchapman wrote:
Indeed moot as the code is for XMEGA with code copyright to Microchip (not JIm); restrictions would be reduced by FLOSS.

 

Like I would care or worry.  If The Empire needs my little POS few lines to make their bottom line they can have it.  Like I wrote...Worry, me not.

 

ki0bk wrote:

Jim, I came across this app note that has code for xmega tw master, you may find it helpful.

AVR944 LED driver for AVR

 

Happy holidays

Jim

 

Thanks!  Will take a look at that too!

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And while down loading AVR944 a few minutes ago I found "AVR1308 Using the XMEGA TWI" in my bottomless pit of AVR stuff.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well the TWI read is proving a little bit of a challenge....

 

I am attempting to do a single read of my PCF8754A then output the read to PORTA.

 

The code I have works with one big exception....It does not assert the STOP condition unless I put a 1 millsecond delay in the code.

 

Now in order to assert the STOP according to the datasheet, I need to clear the RIF and CLKHLD flags.  There are a few ways to do this according to the datasheet:

 

Ok, here's the code:

/*
 * jims_xmega_twi_raw.c
 *
 * Created: 12/19/2019 8:18:40 PM
 * Author : jgmDESIGNS
 */
#define F_CPU 2000000UL
#include <avr/io.h>
#include <util/delay.h>

#define  SLAVE_ADDRESS  0X38
#define PCF8574A_WRITE SLAVE_ADDRESS << 1
#define PCF8574A_READ ((SLAVE_ADDRESS << 1) + 1)


void xmega_init_read(void)
{
    //TWI Configuration
    TWIC_MASTER_STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;		//force TWI BUS INTO IDLE STATE
    TWIC_MASTER_BAUD = 0x05;		//SET SCL FOR 100KHZ
    TWIC_MASTER_CTRLA = 0x08;		//ENABLE TWI MASTER
    TWIC_MASTER_ADDR = PCF8574A_WRITE;		//LOAD ADDRESS REGISTER WITH PCF8754A WRITE ADDRESS
    while((TWIC_MASTER_STATUS & TWI_MASTER_CLKHOLD_bm) == 0);		//WAIT FOR TEH TRANSFER TO COMPLETE
    TWIC_MASTER_STATUS = TWIC_MASTER_STATUS | 0X40;		//CLEAR TEH FLAG FOR WRITE INTERRUPT
    TWIC_MASTER_DATA = 0XFF;	//SET ALL PORTS TO LOGIC 1
    while((TWIC_MASTER_STATUS & TWI_MASTER_CLKHOLD_bm) == 0);	//WAIT FOR TRANSFER TO COMPLETE
    TWIC_MASTER_STATUS = TWIC_MASTER_STATUS | 0X40;		//CLEAR TEH FLAG FOR WRITE INTERRUPT
    TWIC_MASTER_CTRLC = TWI_MASTER_CMD_STOP_gc;		//SEND TWI STOP CONDITION
    TWIC_MASTER_CTRLB = 0X01;    //TURN ON SMART MODE


    //PORTA configuration
    PORTA_DIR = 0xFF;		//all outputs
}

int main(void)
{
     xmega_init_read();		//SET UP XMEGA FOR TWI READ
	
    TWIC_MASTER_ADDR = PCF8574A_READ;		//LOAD ADDRESS REGISTER WITH PCF8574A READ ADDRESS
    uint8_t data = 0;		//LOCAL DATA VARIABLE

    while((TWIC_MASTER_STATUS & TWI_MASTER_RIF_bm) == 0);	//WAIT FOR WRITE TRANSACTION TO COMPLETE
    data = TWIC_MASTER_DATA;	//READ THE REGISTER
    //_delay_ms(1);		//<- this will allow for the STOP condition to occur
    TWIC_MASTER_CTRLC = 0X07;	//SEND NACK AND COMMAND TWI ENGINE TO SEND STOP CONDITION
    
    PORTA_OUT = data;
    
     while(1)
        {


        }

}

In the above I commented out the _delay_ms();  If I uncomment it the stop condition does occur, but a millisecond later than it should.

 

Yeah, I know that 0x07 is a 'magic number' in reality its setting the last three bits in CTRLC as per the datasheet:

 

So I I am thinking I am following the rules, but yet I am missing something obviously.  The logic analyser backs up what I see in the debugger.  The STOP condition is never asserted unless I put the delay in place.  I suppose I could switch to interrupt driven TWI, but for a one time read it seems a little overkill.

 

Some suggestions appreciated

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well this is just frustrating the hell out of me.

 

If I remove the delay line I get this on the analyser:

 

I get the WRITE to setup the PCF8754A, and I see the READ, with the XMEGA putting in the NACK as per the datasheet requirements, but I do not get the STOP condition.

 

Now when I add the _DELAY_MS(1) I get this(two screenshots as I cannot condense into one and be able to read the analyser data):

 

It looks like the xmega is reading the PCF twice.

 

Grrrr...I am missing something very simple here...

 

JIm

 

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

GOT IT!!

 

As I said, I was missing something simple.  According to the datasheet, I clear the RIF and CLKHLD flags when reading the TWI DATA register.  Well when you do this you send an ACK and the TWI engine starts another read.  Moving one line of the code fixed it:

 

From this:

 xmega_init_read();		//SET UP XMEGA FOR TWI READ
	
    TWIC_MASTER_ADDR = PCF8574A_READ;		//LOAD ADDRESS REGISTER WITH PCF8574A READ ADDRESS
    uint8_t data = 0;		//LOCAL DATA VARIABLE

    while((TWIC_MASTER_STATUS & TWI_MASTER_RIF_bm) == 0);	//WAIT FOR WRITE TRANSACTION TO COMPLETE
    TWIC_MASTER_STATUS = TWIC_MASTER_STATUS | TWI_MASTER_RIF_bm;		//CLEAR THE READ FLAG
    data = TWIC_MASTER_DATA;	//READ THE REGISTER
    TWIC_MASTER_CTRLC = 0x07;		//SEND TWI STOP CONDITION with NACK

To this:

 xmega_init_read();		//SET UP XMEGA FOR TWI READ
	
    TWIC_MASTER_ADDR = PCF8574A_READ;		//LOAD ADDRESS REGISTER WITH PCF8574A READ ADDRESS
    uint8_t data = 0;		//LOCAL DATA VARIABLE

    while((TWIC_MASTER_STATUS & TWI_MASTER_RIF_bm) == 0);	//WAIT FOR WRITE TRANSACTION TO COMPLETE
    TWIC_MASTER_STATUS = TWIC_MASTER_STATUS | TWI_MASTER_RIF_bm;		//CLEAR THE READ FLAG
	TWIC_MASTER_CTRLC = 0x07;		//SEND TWI STOP CONDITION with NACK
    data = TWIC_MASTER_DATA;	//READ THE REGISTER

 

Solves the problem and now I see this on the analyser:

 

YIPEE!!!!!

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Now I am going to try the Codevision Wizard setting up the TWI and see what happens

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Budweiser time ....or maybe something a bit stronger...

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:

Budweiser time ....or maybe something a bit stronger...

 

In a prior life yeah.

 

The CV wizard leaves a few things to be desired.....Cannot set up certain bits in the registers for TWI like I can the way I am doing things currently.  Also cannot do a TWI polling setup...only Interrupt driven.

 

Hmmmmm

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, so it seems I am not the only one having issues with the CV 'twi_master_trans() function:

 

https://www.avrfreaks.net/forum/...

 

 

 

For mine:

char buffer = 0xff;    //send buffer
char rec ;     //receive buffer

//init functions
void PCF8574_init(void)
{

	
twi_master_trans(twic_master, 0x38, &buffer, 1, &rec, 0);
	
}

 

When I try and build I get the following error:

Severity    Code    Description    Project    File    Line
Error        function argument #1 of type 'struct TWI_MASTER_INFO_t' is incompatible with required parameter of type 'struct TWI_MASTER_INFO_t *'    xmega_twi_pcf_CV    Y:\AVR_Projects\XMEGA_Projects\XMEGA_TWI_PCF_CV\xmega_twi_pcf_CV.c    50

 

Off to the internet I go in search of explanation.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
Error        function argument #1 of type 'struct TWI_MASTER_INFO_t' is incompatible with required parameter of type 'struct TWI_MASTER_INFO_t *

It is expecting a pointer.

 

So you need

 

twi_master_trans( &twic_master, 0x38, &buffer, 1, &rec, 0 );

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The CV TWI project generated with CV3.07 builds without errors, only a warning about some data being set but not used.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

jgmdesign wrote:
Error        function argument #1 of type 'struct TWI_MASTER_INFO_t' is incompatible with required parameter of type 'struct TWI_MASTER_INFO_t *

It is expecting a pointer.

 

So you need

 

twi_master_trans( &twic_master, 0x38, &buffer, 1, &rec, 0 );

 

Yeah, I figured that out AFTER i posted and smacked my head against the wall.  OOPS!

 

The Codevision created code, resembles START in some respects as there are several files associated with setups and such, and you have to dig around a little to get the gist of it, but on the big plus side it only took me 90 minutes to get the CV stuff working as opposed to several days with START, only to throw START out the window!!

 

Now, heres what I am trying to figure out....the " twi_master_trans() " function is a really neat way to do a single read(or multiple if you are filling a buffer), but what if you want to read a single register >>like the only one in the PCF8574A<< continuously?  As it stands with " twi_master_trans() " it spits out the address of the slave everytime, and then gets the data.  What if I want to send the address ONCE, and then continuously read from that register? 

 

Hmmmm...time to think this one through a bit(no pun)

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
I figured that out AFTER i posted and smacked my head against the wall.  OOPS!

I had to look at it several times before spotting the subtle difference!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
throw START out the window!!

are all these exclamation marks contagious ... ?

 

cheeky

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
are all these exclamation marks contagious ... ?

Indeed.  THey are somewhat satisfying at times. 

 

Maybe you can help me on this before I send a note to Pavel.

 

I am using CV extension inside Studio.  I want to add a Timer.  How does one run the CV Wizard inside studio and tell it to update the current solution?

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, I don't use CV - I was just looking at your post.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Sorry, I don't use CV - I was just looking at your post.

 

Ahhh poop.  OK.  Thanks

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Pages