I am trying to get a simple UART application to work in MQX 4.1 (CodeWarrior) on a TWR K60F120M kit. I have 2 tasks opening 2 different UARTS, one is writing and one is reading. I am using UART4 to write and UART2 to read. On the TWR headers, I have placed a wire to jumper between B80 (UART4 write pin) and A76 (UART2 read pin). I am using the fopen(), fread(), and fwrite() functions. I am using the interrupt-enabled drivers by opening the ports with ittyc and ittye.
This should be a very simple operation, but I cannot get it to work. Can anyone help me here? Am I using the wrong functions? Am I not setting up something properly here?
Here is the console output I receive from this from the printf() calls:
UART2 task started
UART4 task started
UART2: 0 1 2 3 4 5 6 7 8 9 10 10 11 12 10 14 15 16 17 18 20 21 22 23 24 25 26 27 28 29 30 31
UART2: 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
UART2: 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 18 20 21 22 23 24 25 26 27 28 29
UART2: 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
Since I am sending a ramp pattern continuously, I expect to receive that without breaks in the data, and not repeated values, regardless of the starting value that I receive.
Here is the source code:
//*****************************************************************************
//*****************************************************************************
// main.c
//*****************************************************************************
//*****************************************************************************
#include <mqx.h>
#include <bsp.h>
#include <fio.h>
#include <timer.h>
#include <serial.h>
#if ! BSPCFG_ENABLE_IO_SUBSYSTEM
#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option.
#endif
#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option.
#endif
// Task IDs
#define UART2_TASK 7
#define UART4_TASK 8
extern void uart2_task(uint32_t);
extern void uart4_task(uint32_t);
const TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
// Task Index, Function, Stack, Priority, Name, Attributes, Param, Time Slice
{ UART2_TASK, uart2_task, 1000, 7, "uart2", MQX_AUTO_START_TASK, 0, 0 },
{ UART4_TASK, uart4_task, 1000, 8, "uart4", MQX_AUTO_START_TASK, 0, 0 },
{ 0 }
};
FILE_PTR uart2_serial_device;
FILE_PTR uart4_serial_device;
uint8_t write_buf[128] = {0};
uint8_t read_buf1[32] = {0};
uint8_t read_buf2[32] = {0};
uint8_t read_buf3[32] = {0};
uint8_t read_buf4[32] = {0};
//-----------------------------------------------------------
// UART2
// Port assignment: UART2 Tx = PTD3; UART2 Rx = PTD2
// TWR header pins: UART2 Tx = A77; UART4 Rx = A76
//-----------------------------------------------------------
void uart2_task(uint32_t initial_data)
{
int i;
printf("UART2 task started\n");
// Configure PTD2 & PTD3 for ALT3 function
PORTD_PCR2 = PORT_PCR_MUX(3);
PORTD_PCR3 = PORT_PCR_MUX(3);
uart2_serial_device = fopen("ittyc:", BSP_DEFAULT_IO_OPEN_MODE);
fread(&read_buf1[0], 1, 32, uart2_serial_device);
fread(&read_buf2[0], 1, 32, uart2_serial_device);
fread(&read_buf3[0], 1, 32, uart2_serial_device);
fread(&read_buf4[0], 1, 32, uart2_serial_device);
printf("UART2: ");
for (i=0; i<32; i++)
printf("%d ", read_buf1[i]);
printf("\n");
printf("UART2: ");
for (i=0; i<32; i++)
printf("%d ", read_buf2[i]);
printf("\n");
printf("UART2: ");
for (i=0; i<32; i++)
printf("%d ", read_buf3[i]);
printf("\n");
printf("UART2: ");
for (i=0; i<32; i++)
printf("%d ", read_buf4[i]);
printf("\n");
fclose(uart2_serial_device);
_task_block();
}
//-----------------------------------------------------------
// UART4
// Port assignment: UART4 Tx = PTC15; UART4 Rx = PTC14
// TWR header pins: UART4 Tx = B80; UART4 Rx = B79
//-----------------------------------------------------------
void uart4_task(uint32_t initial_data)
{
int i;
printf("UART4 task started\n");
for (i=0; i<128; i++)
write_buf[i] = (i & 0xFF);
// Configure PTC14 & PTC15 for ALT3 function
PORTC_PCR14 = PORT_PCR_MUX(3);
PORTC_PCR15 = PORT_PCR_MUX(3);
PORTE_PCR24 = PORT_PCR_MUX(0); //Disable default UART4_TX
PORTE_PCR25 = PORT_PCR_MUX(0); //Disable default UART4_RX
uart4_serial_device = fopen("ittye:", BSP_DEFAULT_IO_OPEN_MODE);
while(1)
{
fwrite(&write_buf[0], 1, 128, uart4_serial_device);
}
fclose(uart4_serial_device);
_task_block();
}
/* EOF */