Files
AWR1642_Mailbox_UART/mmw_mss_16xx/mss_main.c
2021-04-07 10:53:34 +03:00

348 lines
9.7 KiB
C

/* Standard Include Files. */
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
/* BIOS/XDC Include Files. */
#include <xdc/std.h>
#include <xdc/cfg/global.h>
#include <xdc/runtime/IHeap.h>
#include <xdc/runtime/System.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/Memory.h>
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Event.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/heaps/HeapBuf.h>
#include <ti/sysbios/heaps/HeapMem.h>
#include <ti/sysbios/knl/Event.h>
#include <ti/sysbios/family/arm/v7a/Pmu.h>
/* mmWave SDK Include Files: */
#include <ti/common/sys_common.h>
#include <ti/drivers/soc/soc.h>
#include <ti/drivers/esm/esm.h>
#include <ti/drivers/crc/crc.h>
#include <ti/drivers/uart/UART.h>
#include <ti/drivers/gpio/gpio.h>
#include <ti/drivers/mailbox/mailbox.h>
#include <ti/control/mmwave/mmwave.h>
#include <ti/control/mmwavelink/mmwavelink.h>
#include <ti/utils/cli/cli.h>
#include <ti/drivers/osal/DebugP.h>
#include <ti/drivers/osal/HwiP.h>
#include <ti/utils/cycleprofiler/cycle_profiler.h>
#include <ti/drivers/pinmux/pinmux.h>
#include <ti/drivers/osal/MemoryP.h>
#include <ti/utils/testlogger/logger.h>
SOC_Handle socHandle;
Event_Handle event;
uint8_t period = 0;
uint8_t buffer = 0;
uint8_t id_buf = 0;
uint8_t value_buf = 0;
uint8_t value = 0;
uint8_t id = 0;
uint8_t metka = 0;
UART_Handle handle;
UInt eventMask1 = Event_Id_00;
UInt eventMask2 = Event_Id_01;
UInt eventMask3 = Event_Id_02;
uint32_t BAUDRATE = 115200;
uint8_t STOPBITS = 1;
uint8_t DATALEN = 8;
uint8_t PARITY_VALUE = 4;
uint8_t gMCPIUARTPollingModeEnable = 0;
Event_Handle event1;
uint32_t gCPUClockFrequency = (200 * 1000000);
uint8_t uartDataLenths [] = {
0, // Padding
0,
0,
0,
0,
(uint8_t) UART_LEN_5,
(uint8_t) UART_LEN_6,
(uint8_t) UART_LEN_7,
(uint8_t) UART_LEN_8
};
uint8_t uartParityTypes [] = {
(uint8_t) UART_PAR_ZERO,
(uint8_t) UART_PAR_ONE,
(uint8_t) UART_PAR_EVEN,
(uint8_t) UART_PAR_ODD,
(uint8_t) UART_PAR_NONE
};
uint8_t uartStopBits [] = {
0, // Padding
(uint8_t) UART_STOP_ONE,
(uint8_t) UART_STOP_TWO
};
Mbox_Handle peerMailbox;
#define BIG_MSG_SIZE 3
volatile uint8_t mboxProcToken = 0;
//void MmwDemo_sleep(void)
//{
// /* issue WFI (Wait For Interrupt) instruction */
// asm(" IDLE ");
//}
static int32_t mboxWrite_ch0(uint8_t *message, int32_t len)
{
int32_t retVal = -1;
retVal = Mailbox_write(peerMailbox, message, len);
if (retVal == len)
{
retVal = 0;
}
return retVal;
}
static void mboxReadProc_ch0()
{
uint8_t mes;
int32_t retVal = 0;
int32_t ret = 0;
/* Read the message from the peer mailbox: We are not trying to protect the read
* from the peer mailbox because this is only being invoked from a single thread */
retVal = Mailbox_read(peerMailbox, &mes, 1);
if (retVal < 0)
{
/* Error: Unable to read the message. Setup the error code and return values */
System_printf("Error read dss\n");
return;
}
else
{
Mailbox_readFlush (peerMailbox);
UART_write(handle, "\n", 1);
System_printf("%d\n", mes);
UART_write(handle, &mes, 1);
UART_write(handle, "\n", 1);
if (ret != 0)
{
System_printf ("Error: Mailbox send message failed \n");
}
/* We are done: There are no messages available from the peer execution domain. */
return;
}
}
void mboxCallback_ch0 (Mbox_Handle handle, Mailbox_Type peer)
{
/* Message has been received from the peer endpoint. */
// mboxReadProc_ch0();
mboxProcToken = 1;
}
void mBox(UArg a0, UArg a1)
{
while(true){
if (mboxProcToken == 1)
{
mboxProcToken = 0;
/* If the mailbox has a message and the frame processing task has finished. */
mboxReadProc_ch0();
}
}
}
void Work_UART(UArg arg0, UArg arg1)
{
uint8_t index[128];
uint8_t message[BIG_MSG_SIZE];
UART_Params params;
uint8_t len;
/* Setup the default UART Parameters */
UART_Params_init(&params);
params.writeDataMode = UART_DATA_TEXT;
params.readDataMode = UART_DATA_TEXT;
params.readReturnMode = UART_RETURN_NEWLINE;
params.isPinMuxDone = 1;
params.baudRate = BAUDRATE;
params.dataLength = (UART_LEN) uartDataLenths[DATALEN];
handle = UART_open(0, &params);
if (handle == NULL)
{
printf("Error: Unable to open the UART Instance\n");
return;
}
while(TRUE)
{
len = UART_read(handle, index, sizeof(index)/sizeof(uint8_t)) - 1;
if(len <= 13)
{
if(memcmp(index, "Get ID ", 7) == 0)
{
int i;
for(i = 7; i < len; i++)
{
if(index[i] >= '0' && index[i] <= '9')
{
id_buf = id_buf * 10 + (index[i] - '0');
}
}
message[0] = 0;
id = id_buf;
id_buf = 0;
message[1] = id;
mboxWrite_ch0(message, BIG_MSG_SIZE);
}
else if(memcmp(index, "Set ", 4) == 0)
{
int i;
int pointer = 0;
for(i = 4; i < len; i++)
{
if(index[i] >= '0' && index[i] <= '9')
{
value_buf = value_buf * 10 + (index[i] - '0');
} else if(index[i] == 'I') {
pointer = i;
value = value_buf;
value_buf = 0;
message[2] = value;
break;
}
}
if(memcmp(index, " ID ", 4)) {
int i;
for(i = pointer + 2; i < len; i++)
{
if(index[i] >= '0' && index[i] <= '9')
{
id_buf = id_buf * 10 + (index[i] - '0');
}
}
id = id_buf;
id_buf = 0;
message[0] = 1;
message[1] = id;
mboxWrite_ch0(message, BIG_MSG_SIZE);
}
}
}
else {
UART_write(handle, "\nError\n", 7);
}
}
}
static void InitTask(UArg arg0, UArg arg1)
{
Task_Params taskParamsUART;
Task_Params taskParams;
int32_t errCode;
Mailbox_Config cfg;
Mailbox_init(MAILBOX_TYPE_MSS);
if(Mailbox_Config_init(&cfg) < 0)
{
System_printf ("Error: Mailbox init failed\n");
return;
}
/* Setup the configuration: */
cfg.chType = MAILBOX_CHTYPE_MULTI;
cfg.chId = MAILBOX_CH_ID_0;
cfg.writeMode = MAILBOX_MODE_BLOCKING;
cfg.readMode = MAILBOX_MODE_CALLBACK;
cfg.readCallback = &mboxCallback_ch0;
peerMailbox = Mailbox_open(MAILBOX_TYPE_DSS, &cfg, &errCode);
if (peerMailbox == NULL)
{
System_printf ("Error: Mailbox open failed 0\n");
return;
}
/* Initialize the UART */
UART_init();
/* Test the GPIO Output: Configure pin K13 as GPIO_2 output */
/* Setup the PINMUX to bring out the MSS UART-1 */
Pinmux_Set_FuncSel(SOC_XWR16XX_PINN5_PADBE, SOC_XWR16XX_PINN5_PADBE_MSS_UARTA_TX);
Pinmux_Set_OverrideCtrl(SOC_XWR16XX_PINN5_PADBE, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
Pinmux_Set_FuncSel(SOC_XWR16XX_PINN4_PADBD, SOC_XWR16XX_PINN4_PADBD_MSS_UARTA_RX);
Pinmux_Set_OverrideCtrl(SOC_XWR16XX_PINN4_PADBD, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
/* Initialize the Task Parameters. */
Task_Params_init(&taskParamsUART);
taskParamsUART.stackSize = 4*1024;
taskParamsUART.priority = 3;
Task_create(Work_UART, &taskParamsUART, NULL);
/* Initialize the Task Parameters Mailbox. */
Task_Params_init(&taskParams);
taskParams.priority = 2;
taskParams.stackSize = 3 * 1024;
Task_create(mBox, &taskParams, NULL);
return;
}
int main (void)
{
int32_t errCode;
Task_Params taskParams;
SOC_Cfg socCfg;
Error_Block eb;
/* Initialize the ESM: Dont clear errors as TI RTOS does it */
ESM_init(0U);
/* Initialize the SOC confiugration: */
memset ((void *)&socCfg, 0, sizeof(SOC_Cfg));
/* Populate the SOC configuration: */
socCfg.clockCfg = SOC_SysClock_INIT;
/* Initialize the SOC Module: This is done as soon as the application is started
* to ensure that the MPU is correctly configured. */
socHandle = SOC_init (&socCfg, &errCode);
if (socHandle == NULL)
{
System_printf ("Error: SOC Module Initialization failed [Error code %d]\n", errCode);
return -1;
}
event = Event_create(NULL, &eb);
if (event == NULL) {
System_printf ("Error: Event not create\n");
return -1;
}
/* Initialize the Task Parameters. */
Task_Params_init(&taskParams);
taskParams.stackSize = 2*1024;
Task_create(InitTask, &taskParams, NULL);
/* Start BIOS */
BIOS_start();
return 0;
}