I had to change the code:
//KAB void AT91F_SetTwiClock(void);
void AT91F_SetTwiClock(int32 freq); //KAB
//KAB #define AT91C_TWI_CLOCK 8000
#define AT91C_TWI_CLOCK 400000 //this is for a 400KHz I2C RTC KAB
AT91F_SetTwiClock(AT91C_TWI_CLOCK);
/****************************************************************************
**
** Sets up the TWI bus for the AT91SAM7X
**
** Parameters: freq desired frequency (e.g., 100000, 400000)
**
** Returns: NONE
**
****************************************************************************/
//KAB void AT91F_SetTwiClock(void)
void AT91F_SetTwiClock(int32 freq) //KAB
{
int32 sclock; //this was int16 KAB
/* Here, CKDIV = 1 and CHDIV=CLDIV ==> CLDIV = CHDIV = 1/4*((Fmclk/FTWI) -6)*/
//KAB sclock = (10*AT91B_MCK / AT91C_TWI_CLOCK);
sclock = (10*AT91B_MCK / freq); //KAB
if((sclock % 10) >= 5)
sclock = (sclock / 10) - 5;
else
sclock = (sclock / 10) - 6;
sclock = (sclock + (4 - sclock % 4)) >> 2; /* div 4 */
AT91C_BASE_TWI->TWI_CWGR = ( 1 << 16) | (sclock <<

| sclock;
}
Explanation:
1. The procedure did not take a parameter, which you need if you have I2C devices that work at 100KHz and others that work at 400KHz, for example.
2. sclock was int16, which was not big enough. It might have been OK if parethesis were added so the divide was done before the multiply by 10... did not even try that.