TWI Bus scanner for Xmegas

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi everybody

 

i wrote the code below to scan TWI bus for available devices with no success.

any suggestions???

 

 

/*
 * GccApplication2.c
 *
 * Created: 04/01/2019 12:19:55 ق.ظ
 * Author : delta2delta@gmail.com
 */ 

#include <avr/io.h>
#define F_CPU		2000000UL
#include <util/delay.h>
#include <stdint.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include "uart_ATX.h"
#include "twi_master_driver.h"

#define uart_sel	uartC1
#define uc1			&USARTC1
TWI_Master_t	twie;
#define F_TWI	100000

int main(void)
{
	_delay_ms(2000);
	uart_init(uc1,uart_sel,baud_9600,conf_8_no_1);
	TWI_MasterInit(&twie,&TWIE,TWI_MASTER_INTLVL_LO_gc,TWI_BAUD(F_CPU,F_TWI));
		PMIC.CTRL |= PMIC_LOLVLEN_bm;
		sei();
	uint8_t data=0;
	char buffer[100];
    volatile uint8_t address=0;
	volatile uint8_t	res=0;
	uart_put_string("scanning I2C bus...",uc1,1);

    // start scan-------------------------------------

	for (address=0;address<127;address++)
	{
		sprintf(buffer,"scanning address  %d \r\n",address);
		uart_put_string(buffer,uc1,0);

		res=TWI_MasterWriteRead(&twie,address,&data,1,0);
		if (res==1)
    		{
    			uart_put_string("succeess!!!!",uc1,1);
    			sprintf(buffer,"device address is %d \r\n",address);
    			uart_put_string(buffer,uc1,1);
    		}

		sprintf(buffer,"result for address %d is %d \r\n",address,res);
		uart_put_string(buffer,uc1,1);
	}

	uart_put_string("scan finished!!!",uc1,1);

    while (1)
    {

    }
}

ISR(TWIC_TWIM_vect){
	TWI_MasterInterruptHandler(&twie);
}

      result is like :

 

scanning I2C bus...
scanning address  0
scanning I2C bus...
scanning address  0
scanning I2C bus...
scanning address  0
succeess!!!!
device address is 0

result for address 0 is 1

scanning address  1
result for address 1 is 0

scanning address  2
result for address 2 is 0

scanning address  3
result for address 3 is 0

scanning address  4
result for address 4 is 0

scanning address  5
result for address 5 is 0

scanning address  6
result for address 6 is 0

scanning address  7
result for address 7 is 0

scanning address  8
result for address 8 is 0

scanning address  9
result for address 9 is 0

scanning address  10
result for address 10 is 0

scanning address  11
result for address 11 is 0

scanning address  12
result for address 12 is 0

scanning address  13
result for address 13 is 0

scanning address  14
result for address 14 is 0

scanning address  15
result for address 15 is 0

scanning address  16
result for address 16 is 0

scanning address  17
result for address 17 is 0

scanning address  18
result for address 18 is 0

scanning address  19
result for address 19 is 0

scanning address  20
result for address 20 is 0

scanning address  21
result for address 21 is 0

scanning address  22
result for address 22 is 0

scanning address  23
result for address 23 is 0

scanning address  24
result for address 24 is 0

scanning address  25
result for address 25 is 0

scanning address  26
result for address 26 is 0

scanning address  27
result for address 27 is 0

scanning address  28
result for address 28 is 0

scanning address  29
result for address 29 is 0

scanning address  30
result for address 30 is 0

scanning address  31
result for address 31 is 0

scanning address  32
result for address 32 is 0

scanning address  33
result for address 33 is 0

scanning address  34
result for address 34 is 0

scanning address  35
result for address 35 is 0

scanning address  36
result for address 36 is 0

scanning address  37
result for address 37 is 0

scanning address  38
result for address 38 is 0

scanning address  39
result for address 39 is 0

scanning address  40
result for address 40 is 0

scanning address  41
result for address 41 is 0

scanning address  42
result for address 42 is 0

scanning address  43
result for address 43 is 0

scanning address  44
result for address 44 is 0

scanning address  45
result for address 45 is 0

scanning address  46
result for address 46 is 0

scanning address  47
result for address 47 is 0

scanning address  48
result for address 48 is 0

scanning address  49
result for address 49 is 0

scanning address  50
result for address 50 is 0

scanning address  51
result for address 51 is 0

scanning address  52
result for address 52 is 0

scanning address  53
result for address 53 is 0

scanning address  54
result for address 54 is 0

scanning address  55
result for address 55 is 0

scanning address  56
result for address 56 is 0

scanning address  57
result for address 57 is 0

scanning address  58
result for address 58 is 0

scanning address  59
result for address 59 is 0

scanning address  60
result for address 60 is 0

scanning address  61
result for address 61 is 0

scanning address  62
result for address 62 is 0

scanning address  63
result for address 63 is 0

scanning address  64
result for address 64 is 0

scanning address  65
result for address 65 is 0

scanning address  66
result for address 66 is 0

scanning address  67
result for address 67 is 0

scanning address  68
result for address 68 is 0

scanning address  69
result for address 69 is 0

scanning address  70
result for address 70 is 0

scanning address  71
result for address 71 is 0

scanning address  72
result for address 72 is 0

scanning address  73
result for address 73 is 0

scanning address  74
result for address 74 is 0

scanning address  75
result for address 75 is 0

scanning address  76
result for address 76 is 0

scanning address  77
result for address 77 is 0

scanning address  78
result for address 78 is 0

scanning address  79
result for address 79 is 0

scanning address  80
result for address 80 is 0

scanning address  81
result for address 81 is 0

scanning address  82
result for address 82 is 0

scanning address  83
result for address 83 is 0

scanning address  84
result for address 84 is 0

scanning address  85
result for address 85 is 0

scanning address  86
result for address 86 is 0

scanning address  87
result for address 87 is 0

scanning address  88
result for address 88 is 0

scanning address  89
result for address 89 is 0

scanning address  90
result for address 90 is 0

scanning address  91
result for address 91 is 0

scanning address  92
result for address 92 is 0

scanning address  93
result for address 93 is 0

scanning address  94
result for address 94 is 0

scanning address  95
result for address 95 is 0

scanning address  96
result for address 96 is 0

scanning address  97
result for address 97 is 0

scanning address  98
result for address 98 is 0

scanning address  99
result for address 99 is 0

scanning address  100
result for address 100 is 0

scanning address  101
result for address 101 is 0

scanning address  102
result for address 102 is 0

scanning address  103
result for address 103 is 0

scanning address  104
result for address 104 is 0

scanning address  105
result for address 105 is 0

scanning address  106
result for address 106 is 0

scanning address  107
result for address 107 is 0

scanning address  108
result for address 108 is 0

scanning address  109
result for address 109 is 0

scanning address  110
result for address 110 is 0

scanning address  111
result for address 111 is 0

scanning address  112
result for address 112 is 0

scanning address  113
result for address 113 is 0

scanning address  114
result for address 114 is 0

scanning address  115
result for address 115 is 0

scanning address  116
result for address 116 is 0

scanning address  117
result for address 117 is 0

scanning address  118
result for address 118 is 0

scanning address  119
result for address 119 is 0

scanning address  120
result for address 120 is 0

scanning address  121
result for address 121 is 0

scanning address  122
result for address 122 is 0

scanning address  123
result for address 123 is 0

scanning address  124
result for address 124 is 0

scanning address  125
result for address 125 is 0

scanning address  126
result for address 126 is 0

scan finished!!!

 

Last Edited: Sun. Mar 31, 2019 - 09:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you have a device on the bus? If so, what 7-bit address does it use?

 

Be aware of the  following Special Addresses ( from https://www.totalphase.com/suppo... )

 

Reserved Addresses

The I2C specification has reserved two sets of eight addresses, 1111XXX and 0000XXX. These addresses are used for special purposes. The following table has been taken from the I2C Specifications (2000).

Slave Address R/W Bit Description
000 0000 0 General call address
000 0000 1 START byte(1)
000 0001 X CBUS address(2)
000 0010 X Reserved for different bus format (3)
000 0011 X Reserved for future purposes
000 01XX X Hs-mode master code
111 10XX X 10-bit slave addressing
111 11XX X Reserved for future purposes

(1) No device is allowed to acknowledge at the reception of the START byte.

(2) The CBUS address has been reserved to enable the inter-mixing of CBUS compatible and I2C-bus compatible devices in the same system. I2C-bus compatible devices are not allowed to respond on reception of this address.

(3) The address reserved for a different bus format is included to enable I2C and other protocols to be mixed. Only I2C-bus compatible devices that can work with such formats and protocols are allowed to respond to this address.

 

Jim 

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Sun. Mar 31, 2019 - 09:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have DS3231 clock module on bus with address 0x68 and its working in normal use.

 

but when i try to scan for other addresses encounter problem.

 

I'm not sure about the code because I'm trying to use AVR1308 app note header file(TWI_MASTER_DRIVER)

and not familiar with.

 

so asked for help

:-)

 

seems the twiwriteread() function does not return ACK from slave but somthing to tell that transaction was successful or not.

Last Edited: Mon. Apr 1, 2019 - 12:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

even this one did not work:

 

int main(void)
{
		TWIE.CTRL=0;
		TWIE.MASTER.BAUD=0x05;
		TWIE.MASTER.CTRLA = TWI_SLAVE_INTLVL_LO_gc |
		TWI_MASTER_RIEN_bm |
		TWI_MASTER_WIEN_bm |
		TWI_MASTER_ENABLE_bm;
		TWIE.MASTER.CTRLB= 2;

		TWIE.MASTER.STATUS = TWI_MASTER_RIF_bm|TWI_MASTER_WIF_bm|TWI_MASTER_ARBLOST_bm|TWI_MASTER_BUSERR_bm|TWI_MASTER_BUSSTATE0_bm;

		_delay_ms(2000);
		uart_init(uc1,uart_sel,baud_9600,conf_8_no_1);

			uint8_t data=0;
			char buffer[100];
			volatile uint8_t address=0;
			volatile uint8_t	res=0;
			 PMIC_CTRL|=PMIC_HILVLEN_bm|PMIC_LOLVLEN_bm|PMIC_MEDLVLEN_bm;
			 sei();
			uart_put_string("scanning I2C bus...",uc1,1);
			for (address=0;address<256;address+2)
			{
				TWIE.MASTER.ADDR=address;
				_delay_us(2);

				sprintf(buffer,"status is %X \r\n",TWIE.MASTER.STATUS);
				uart_put_string(buffer,uc1,0);
				TWIE.MASTER.CTRLC=3;

			}

status is always 12

Last Edited: Mon. Apr 1, 2019 - 02:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

--------------------------------------ISSUE HANDLED-----------------------------------

 

Reading and Coding(digging) from scratch always works-in cost of time-of course.

 

 

for whom maybe interested in lovely ATXmega's :

 

here's the result :

 

/*
 * GccApplication3.c
 *
 * Created: 04/01/2019 01:15:18 ق.ظ
 * Author : delta2delta@gmail.com
 */ 

#include <avr/io.h>
#define F_CPU		2000000UL
#include <util/delay.h>
#include <stdint.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include "uart_ATX.h"

#define uart_sel	uartC1
#define uc1			&USARTC1

char buffer[50];
volatile uint8_t address=0;

void scan(void);			

int main(void)
{
		_delay_ms(100);   // to stabilize power
		
		TWIC.MASTER.BAUD=0x05;     // f_twi 100k
		TWIC.MASTER.CTRLA = TWI_SLAVE_INTLVL_LO_gc |TWI_MASTER_RIEN_bm |TWI_MASTER_WIEN_bm |TWI_MASTER_ENABLE_bm;
		TWIC.MASTER.STATUS = TWI_MASTER_RIF_bm|TWI_MASTER_WIF_bm|TWI_MASTER_ARBLOST_bm|TWI_MASTER_BUSERR_bm|TWI_MASTER_BUSSTATE0_bm;
		uart_init(uc1,uart_sel,baud_9600,conf_8_no_1);
		PMIC_CTRL|=PMIC_LOLVLEN_bm;
		 sei();
		scan();
		
    while (1) 
    {
		uart_put_string("press \'+\' to retry",uc1,1);
		buffer[0]=uart_getchar(uc1);
		if (buffer[0]=='+')   //   to  reinitiate scan if needed
		{
			scan();          
		}
		
    }
}         //END Main

void scan(void){
	address=0;
	uart_put_string("scanning I2C bus...",uc1,1);
	for (address=0;address<254;address+=2)
	{
		TWIC.MASTER.ADDR=address;
		while(!(TWIC.MASTER.STATUS & TWI_MASTER_WIF_bm));     //wait for send address completion
		_delay_ms(10);
		if (!(TWIC.MASTER.STATUS & TWI_MASTER_RXACK_bm))        // check if there is a device listening
		{
			sprintf(buffer,"\tdevice found at 0x%2X \r\n",address>>1);
			uart_put_string(buffer,uc1,0);
		}
		
		TWIC.MASTER.CTRLC = TWI_MASTER_CMD_REPSTART_gc;
		TWIC.MASTER.STATUS= TWI_MASTER_RIF_bm|TWI_MASTER_WIF_bm|TWI_MASTER_ARBLOST_bm|TWI_MASTER_BUSERR_bm|TWI_MASTER_BUSSTATE0_bm;
		
	}
	uart_put_string("scan finished",uc1,1);
}      // END 

 

 

P.S:

Admins please close the subject.