uint32_t send_cmd(uint8_t temp, uint8_t par)
{
uint32_t i;
uint8_t temp2;
LPC_IOCON->TDI_PIO0_11 = 0x00000089;
/* Step 1: relese both clock & data */
LPC_IOCON->TMS_PIO0_12 = 0x00000091; /* Data pin input wit pull up enabled */
LPC_GPIO->DIR[0] &= 0xFFFFEFFF; /* Set pin as input */
LPC_IOCON->TDO_PIO0_13 = 0x00000091; /* Data pin input wit pull up enabled */
LPC_GPIO->DIR[0] &= 0xFFFFDFFF; /* Set pin as input */
/* Step 2: Bring the clock line low */
LPC_IOCON->TDO_PIO0_13 = 0x00000081;
LPC_GPIO->DIR[0] |= 0x00002000;
/* Step 3: Wait 100 us */
wait(100);
/* Step 4: If data already low or clock high, then error PS2 busy */
if( (LPC_GPIO->W0[12] == 0) || (LPC_GPIO->W0[13] != 0) )
{
return 1;
}
/* Step 5: Bring the Data line low */
LPC_IOCON->TMS_PIO0_12 = 0x00000081;
LPC_GPIO->DIR[0] |= 0x00001000;
/* Step 6: Release the Clock line. */
LPC_IOCON->TDO_PIO0_13 = 0x00000091;
LPC_GPIO->DIR[0] &= 0xFFFFDFFF; //release clock line
/* Step 7: Wait for the device to bring the Clock line low. */
while(LPC_GPIO->W0[13] != 0); // Wait for the device to bring the Clock line low
/* Step 8: Write 8 bit data, LSB first */
for( i = 0 ; i < 8 ; i++ )
{
temp2 = 0x01 << i ; //send LSB
temp2 = temp2 & temp;
if(temp2 == 0)
{ // if bit 0 then make data pin as output
LPC_IOCON->TMS_PIO0_12 = 0x00000081;
LPC_GPIO->DIR[0] |= 0x00001000;
}
else
{
LPC_IOCON->TMS_PIO0_12 = 0x00000091; // otherwise make it input
LPC_GPIO->DIR[0] &= 0xFFFFEFFF;
}
while(LPC_GPIO->W0[13] == 0); //while clock not high
while(LPC_GPIO->W0[13] != 0); //while clock not low
}
/* Step 9: Send parity bit */
if(par == 0) //parity bit
{
LPC_IOCON->TMS_PIO0_12 = 0x00000081;
LPC_GPIO->DIR[0] |= 0x00001000;
}
else
{
LPC_IOCON->TMS_PIO0_12 = 0x00000091;
LPC_GPIO->DIR[0] &= 0xFFFFEFFF;
}
while(LPC_GPIO->W0[13] == 0); //while clock not high
while(LPC_GPIO->W0[13] != 0); //while clock not low
/* Step 10: release data line */
LPC_IOCON->TMS_PIO0_12 = 0x00000091; /* make them input & pull high */ //release data line
LPC_GPIO->DIR[0] &= 0xFFFFEFFF;
/* Step 10: Wait for the device to bring Data low & clcok low */
while(LPC_GPIO->W0[12] != 0); //while data not low
while(LPC_GPIO->W0[13] != 0); //while clock not low
/* Step 11: Wait for the device to release Data and Clock */
while( ( LPC_GPIO->W0[13] == 0 ) || ( LPC_GPIO->W0[12] == 0 ) );
return 0;
}
void wait(uint8_t temp)
{
LPC_SYSCON->SYSAHBCLKCTRL |= (1UL<<9); /* enable clock */
LPC_CT32B0->TCR = 0x02; /* reset timer */
LPC_CT32B0->PR = 0x00; /* prescaler value */
LPC_CT32B0->MR0 = temp * 72;
LPC_CT32B0->IR = 0x003f; /* disable interrupt initially */
LPC_CT32B0->MCR = 0x04; /* TC will be zero on match & give int */
LPC_CT32B0->TCR = 0x01;
while(LPC_CT32B0->TCR & 0x01);
}
|