Files
AWR1642_UARTandLED/mss_main.c
2021-03-31 19:06:28 +03:00

264 lines
7.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 metka = 0;
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
};
void Flashing_LED(UArg arg0, UArg arg1)
{
UInt flag;
while (TRUE)
{
flag = Event_pend(event1, Event_Id_NONE, eventMask1 + eventMask2 + eventMask3, BIOS_WAIT_FOREVER);
if(flag & eventMask1) {
GPIO_write(arg0, 1U);
continue;
}
else if(flag & eventMask3) {
GPIO_write(arg0, 0U);
continue;
}
while(TRUE){
GPIO_toggle(arg0);
if(Event_pend(event1, Event_Id_NONE, eventMask1 + eventMask3, period)) {
break;
}
}
}
}
void Work_UART(UArg arg0, UArg arg1)
{
uint8_t index[128];
UART_Params params;
UART_Handle handle;
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, "On", 2) == 0)
{
Event_post(event1, eventMask1);
UART_write(handle, "\nOk\n", 4);
}
else if(memcmp(index, "Off", 3) == 0)
{
Event_post(event1, eventMask3);
UART_write(handle, "\nOk\n", 4);
}
else if(memcmp(index, "Period ", 7) == 0) {
int i;
for(i = 7; i < len; i++)
{
if(index[i] >= '0' && index[i] <= '9')
{
buffer = buffer * 10 + (index[i] - '0');
}
else {
metka = 1;
break;
}
}
if(metka == 1 || buffer == 0) {
UART_write(handle, "\nError\n", 7);
metka = 0;
continue;
}
UART_write(handle, "\nOk\n", 4);
period = buffer;
buffer = 0;
Event_post(event1, eventMask2);
}
else {
UART_write(handle, "\nError\n", 7);
}
}
else {
UART_write(handle, "\nError\n", 7);
}
}
}
static void InitTask(UArg arg0, UArg arg1)
{
Task_Params taskParamsUART;
Task_Params taskParamsLED;
Task_Params_init(&taskParamsLED);
taskParamsLED.stackSize = 4*1024;
taskParamsLED.priority = 1;
taskParamsLED.arg0 = (UArg)SOC_XWR16XX_GPIO_2;
Task_create(Flashing_LED, &taskParamsLED, NULL);
Event_Params eventParams;
Event_Params_init(&eventParams);
event1 = Event_create(&eventParams, NULL);
/* Initialize the GPIO */
GPIO_init ();
/* Initialize the UART */
UART_init();
/* Test the GPIO Output: Configure pin K13 as GPIO_2 output */
Pinmux_Set_OverrideCtrl(SOC_XWR16XX_PINK13_PADAZ, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
Pinmux_Set_FuncSel(SOC_XWR16XX_PINK13_PADAZ, SOC_XWR16XX_PINK13_PADAZ_GPIO_2);
GPIO_setConfig (SOC_XWR16XX_GPIO_2, GPIO_CFG_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 = 2;
Task_create(Work_UART, &taskParamsUART, 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;
}