Ok so I guess I should post it in this topic instead of the gcc inline assembly one since it is sensor communication source code related.
So here goes:
Remember this delay function from the gcc inline assembly topic:
Code:void delay(u8 value)
{
//for 250ns value should be around 28
//should take in mind though all the other calculations
//happening too, so we could wait for less
int start= 0;
int current = 0;
start = *pTIMER4_COUNTER;
//*pTIMER_ENABLE |= TIMEN4;
if(*pTIMER4_COUNTER<start) //we had overflow and timer reset
current = 4294967295/*2^32-1*/-start+*pTIMER4_COUNTER;
else
current = *pTIMER4_COUNTER;
while((current-start) < value)
{
if(*pTIMER4_COUNTER<start) //we had overflow and timer reset
current = 4294967295/*2^32-1*/-start+*pTIMER4_COUNTER;
else
current = *pTIMER4_COUNTER;
}
//we are done so disable the timer
//*pTIMER_DISABLE |= TIMDIS4;
}
Notice that I have commented out both timer enable and disable. Why? It seems to get the microprocessor stuck when I enable and disable the timer in the delay function so I am forced to leave it running all the time after an initTMR4(). If anyone knows why it would be a great help.
Now for the main part. Communicating with the sensor. I had managed to do it with an ATMega8 microcontroller. Now with the blackfin it does not seem to go all that well.
Here is how I raise and lower clock and data:
Code:#define raiseClock() *pPORTHIO_SET |= 0x2000
#define lowerClock() *pPORTHIO_CLEAR |= 0x2000
#define raiseData() *pPORTHIO_SET |= 0x8000
#define lowerData() *pPORTHIO_CLEAR |= 0x8000
Tried it with PORTHIO too but reading the manual I saw these registers as more friendly to use.
As for the write to the sensor operation:
Code:void opticalWrite(u8 rAddr,u8 value)
{
int i = 0 ;
//we set the appropriate direction to the direction register
*pPORTHIO_DIR |= 0xC000; //pins 14 and 15 as output
//we make sure the address has '1' as its MSB
rAddr |= 0x80;
raiseClock();
//we send the register's address
for(i=8;i>=1;i--)
{
delay(28);
if(isSet(rAddr,i-1))
raiseData();
else
lowerData();
lowerClock();//SDA is changed on falling edges of the clock
delay(28);
raiseClock(); //and the sensor reads it on rising edges
}
//now follows the data
for(i=8;i>=1;i--)
{
delay(28);
if(isSet(value,i-1))
raiseData();
else
lowerData();
lowerClock();//SDA is changed on falling edges of the clock
delay(28);
raiseClock();//and the sensor reads it on rising edges
}
//the sensor's datasheet asks for 100us seconds delay between writes. Remember that
//when using the function
}
This should work? Right? Well it does not

. Trying opticalWrite(0x00,0x01) which should force the sensor to awake mode(much more bright led) did nothing when with the ATmega8 with the same functions it worked.
I would like to ask for your input. Honestly I don't know how to proceed from here. I would also really appreciate tips for debugging embedded code. This summer is the first time I find myself working with such kind of code. It is very very interesting but debugging is hell.
As for the physical hardware connections I checked them and they are correct. Sensor's CLK pin is connected to PH14 and Data pin to PH15.