I am using TWR-K21F120M board. Using MQX4.0.
I am interfacing a finger print scanner to UART0. I am unable to write data to it for the first time. But it takes data for the second time.
unsigned char buffer[] = {0x02, 0x61, 0x02, 0x21, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x01, 0xDD, 0x69, 0x1B, 0x03};
unsigned char buffer1[100] = {0x00};
uint_8 count;
uint_32 Baudrate=9600;
UARTA = fopen("ttya:",(char const*)(IO_SERIAL_RAW_IO|IO_SERIAL_TRANSLATION)); //opening the device
if(NULL==UARTA)
{
_task_block();
}
if( MQX_OK !=_io_ioctl(UARTA,IO_IOCTL_SERIAL_SET_BAUD,&Baudrate)) //UART0 settings
{
_task_block();
}
while(TRUE)
{
count = write(UARTA,buffer,sizeof(buffer)); //Writing data to UARTA,Even though the return value is positive, the data is not going to the peripheral connected to the UART0
for(i = 0; i < 4; i++)
{
read(UARTA,buffer1,4); //
}
for(i = 0; i < 4; i++)
printf(" the buffer is %02X", buffer1[i]);
}
Can anybody explain why this is happening.?
Hi Chandra,
It is possible that your device is doing some kind of auto calibration for the baude rate. This is common in some UART devices.
Have you checked if the datasheet mentions this?
I would also recomend a interrupt approach. You can take a look of a source code that uses the itty driver. Take a look to this appnote and its source code:
http://cache.freescale.com/files/32bit/doc/app_note/AN4022.pdf?fsrch=1&sr=3
I hope this helps you. Good luck with your project!
Best Regards,
Garabo
Hi Garabo,
Thanks for the reply.
I thought the same way, but the peripheral datasheet doesn't say anything about the baudrate auto calibration. though we also worked with the MC9S08JM32 controller previously and we didnt face this issue, we also worked with K20 controller. I am facing issue with the K21. Currently we are opting for baremetal.
If there is anything like auto calibration on the controller side, how to manage that.
In the below code what are the settings/values that need to be changed.
sbr = (uint16)((sysclk*1000)/(baud * 16));
/* Save off the current value of the UARTx_BDH except for the SBR field */
temp = UART_BDH_REG(uartch) & ~(UART_BDH_SBR(0x1F));
UART_BDH_REG(uartch) = temp | UART_BDH_SBR(((sbr & 0x1F00) >> 8));
UART_BDL_REG(uartch) = (uint8)(sbr & UART_BDL_SBR_MASK);
/* Determine if a fractional divider is needed to get closer to the baud rate */
brfa = (((sysclk*32000)/(baud * 16)) - (sbr * 32));
/* Save off the current value of the UARTx_C4 register except for the BRFA field */
temp = UART_C4_REG(uartch) & ~(UART_C4_BRFA(0x1F));
UART_C4_REG(uartch) = temp | UART_C4_BRFA(brfa);
Thanks and Regards,
Chandrasekhar