/* ################################################################### ** Filename : main.c ** Processor : S32K1xx ** Abstract : ** Main module. ** This module contains user's application code. ** Settings : ** Contents : ** No public methods ** ** ###################################################################*/ /*! ** @file main.c ** @version 01.00 ** @brief ** Main module. ** This module contains user's application code. */ /*! ** @addtogroup main_module main module documentation ** @{ */ /* MODULE main */ /* Including necessary module. Cpu.h contains other modules needed for compiling.*/ #include "Cpu.h" #include "clockMan1.h" #include "dmaController1.h" #include "pin_mux.h" #include "lpuart1.h" #include #include #include volatile int exit_code = 0; typedef enum { helloa = 0x00U, hia = 0x01U } can_commands_list; #define TX_MAILBOX (1UL) #define TX_MSG_ID (1UL) #define RX_MAILBOX (0UL) #define RX_MSG_ID (2UL) #define welcomeMsg "Now you can begin typing:\r\n" #define errorMsg "An error occurred! The application will stop!\r\n" #define BUFFER_SIZE 256U #define TIMEOUT 100U uint8_t buffer[BUFFER_SIZE]; uint8_t bufferIdx; uint8_t xinxi = (uint8_t)helloa; /* User includes (#include below this line is not maintained by Processor Expert) */ void Init(void); void rxCallback(void *driverState, uart_event_t event, void *userData) { /* Unused parameters */ (void)driverState; (void)userData; /* Check the event type */ if (event == UART_EVENT_RX_FULL) { /* The reception stops when newline is received or the buffer is full */ if ((buffer[bufferIdx] != '\n') && (bufferIdx != (BUFFER_SIZE - 2U))) { /* Update the buffer index and the rx buffer */ bufferIdx++; LPUART_DRV_SetRxBuffer(INST_LPUART1, &buffer[bufferIdx], 1U); } } } void Init(void) { CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT, g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT); CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_FORCIBLE); PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr); LPUART_DRV_Init(INST_LPUART1, &lpuart1_State, &lpuart1_InitConfig0); CAN_Init(&can_pal1_instance, &can_pal1_Config0); } /*! \brief The main function for the project. \details The startup initialization sequence is the following: * - startup asm routine * - main() */ int main(void) { /* Write your local variable definition here */ /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/ #ifdef PEX_RTOS_INIT PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */ #endif /*** End of Processor Expert internal initialization. ***/ uint32_t bytesRemaining; status_t status; /* Write your code here */ Init(); LPUART_DRV_InstallRxCallback(INST_LPUART1,rxCallback,NULL); LPUART_DRV_SendDataBlocking(INST_LPUART1, (uint8_t *)welcomeMsg, strlen(welcomeMsg), TIMEOUT); can_buff_config_t buffCfg = { .enableFD = true, .enableBRS = true, .fdPadding = 0U, .idType = CAN_MSG_ID_STD, .isRemote = false }; CAN_ConfigTxBuff(&can_pal1_instance,TX_MAILBOX, &buffCfg); can_message_t message = { .cs = 0U, .id = TX_MSG_ID, .data[0] = xinxi, .length = 1U }; while(1) { LPUART_DRV_ReceiveData(INST_LPUART1,buffer,1U); while(LPUART_DRV_GetReceiveStatus(INST_LPUART1,&bytesRemaining)==STATUS_BUSY); status=LPUART_DRV_GetReceiveStatus(INST_LPUART1,&bytesRemaining); if (status != STATUS_SUCCESS) { /* If an error occurred, send the error message and exit the loop */ LPUART_DRV_SendDataBlocking(INST_LPUART1, (uint8_t *)errorMsg, strlen(errorMsg), TIMEOUT); break; } /* Append string terminator to the received data */ bufferIdx++; buffer[bufferIdx] = 0U; if(strcmp((char *)buffer, "Hello") != 0) { xinxi=helloa; } else if(strcmp((char *)buffer, "Hello world") != 0) { xinxi=hia; } else { PINS_DRV_WritePin(PTD,15,1); } CAN_Send(&can_pal1_instance, TX_MAILBOX, &message); CAN_ConfigRxBuff(&can_pal1_instance, RX_MAILBOX, &buffCfg, RX_MSG_ID); can_message_t recvMsg; CAN_Receive(&can_pal1_instance, RX_MAILBOX, &recvMsg); while(CAN_GetTransferStatus(&can_pal1_instance, RX_MAILBOX) == STATUS_BUSY); if((recvMsg.data[0] == helloa) && recvMsg.id == RX_MSG_ID) { /* Toggle output value LED1 */ PINS_DRV_WritePin(PTD,0,1); } else if((recvMsg.data[0] == hia) && recvMsg.id == RX_MSG_ID) { /* Toggle output value LED0 */ PINS_DRV_WritePin(PTD,16,1); } /* Send the received data back */ LPUART_DRV_SendDataBlocking(INST_LPUART1, buffer, bufferIdx, TIMEOUT); /* Reset the buffer index to start a new reception */ bufferIdx = 0U; } /* For example: for(;;) { } */ /*** Don't write any code pass this line, or it will be deleted during code generation. ***/ /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/ #ifdef PEX_RTOS_START PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */ #endif /*** End of RTOS startup code. ***/ /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/ for(;;) { if(exit_code != 0) { break; } } return exit_code; /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/ } /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/ /* END main */ /*! ** @} */ /* ** ################################################################### ** ** This file was created by Processor Expert 10.1 [05.21] ** for the NXP S32K series of microcontrollers. ** ** ################################################################### */