int main(void) { FPUEnable(); led_Init(); button_Init(); can_Init(); pcsr_Init(); //can_SetLogging(0, 0x001, 0x3FF, LogHandler); //uint8_t new_data[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; //uint8_t new_data_mask[] = { 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF }; //can_SetFiltering(0, 0x001, 0x3FF, new_data, new_data_mask); //uint8_t data[] = { 0xFF, 0x00, 0xFF, 0x00, '\n' }; //pcsr_WriteData("READY!\r\n", 8); while(UARTCharsAvail(UART2_BASE)) { led_Byte(UARTCharGetNonBlocking(UART2_BASE)); } while(1) { uint8_t buf[22]; pcsr_ReadData(buf, 1); if(buf[0] == FUNCTION_LOG) { pcsr_ReadData(buf + 1, 5); uint16_t arbid = buf[2] + (buf[3] << 8); uint16_t arbid_mask = buf[4] + (buf[5] << 8); can_SetLogging(buf[1], arbid, arbid_mask, LogHandler); } else if(buf[0] == FUNCTION_FILTER) { pcsr_ReadData(buf + 1, 21); uint16_t arbid = buf[2] + (buf[3] << 8); uint16_t arbid_mask = buf[4] + (buf[5] << 8); can_SetFiltering(buf[1], arbid, arbid_mask, buf + 6, buf + 14); } else if(buf[0] == FUNCTION_RESET) { can_ResetFunctions(); //led_Byte(buf[0]); } } }
void J1939_init(const unsigned char startAddress,const unsigned char *name) { unsigned char i=0; TXstart=TXend=RXstart=RXend=0; TXsize=RXsize=0; ComandedAddress = startAddress; J1939_Address = startAddress; for (i;i<8;i++) Name[i] = name[i]; can_Init(); // 250 Kbit/s for (i=0; i<BUFFER_TX_COUNT;i++) { can_SetBuffer(START_BUFFER_TX+i,0,0xFFFFFE); clear_CPU_operation_detection(); } for (i=0; i<BUFFER_COUNT; i++) { can_SetBuffer(START_BUFFER+i,1,((unsigned long int)startAddress)<<8); can_EnableIRQ(START_BUFFER+i); clear_CPU_operation_detection(); } for (i=0; i<BUFFER_GLOBAL_COUNT; i++) { can_SetBuffer(START_BUFFER_GLOBAL+i,1,0x0000F000); can_EnableIRQ(START_BUFFER_GLOBAL+i); clear_CPU_operation_detection(); } can_SetMask0(0xFF00FF); can_SetMask1(0xFF0FFF); //can_Mode(0); J1939_Flags.CannotClaimAddress = 0; J1939_Flags.WaitingForAddressClaimContention = 0; J1939_Flags.ReceivedMessagesDropped = 0; J1939_Flags.AddressRequest = 0; J1939_Flags.Connected = 0; J1939_Flags.BAM = 0; J1939_Flags.WaitingForConnection = 0; J1939_Flags.ConnectFalied = 0; J1939_Flags.TransmisionError = 0; J1939_Flags.TransmisionComplete = 0; J1939_Flags.RemoteAddress = J1939_NULL_ADDRESS; J1939_timer = timer_Start(); can_DisableAllIRQ(); J1939_ClaimAddress(J1939_CLAIM_ADDRESS_TX); while (J1939_Flags.WaitingForAddressClaimContention) { clear_CPU_operation_detection(); J1939_poll(5); // clear_CPU_operation_detection(); } can_EnableAllIRQ(); return; }
DMPAPI(COMPort *) com_Init(int com) { COMPort *port; if ((port = (COMPort *)CreateCOMPort(com)) == NULL) return NULL; switch (com) { case COM1: case COM2: case COM3: case COM4: case COM5: case COM6: case COM7: case COM8: case COM9: case COM10: { if ((port->func = (void *)CreateUART(com)) == NULL) goto FAIL; if (uart_Init(port->func) == false) { uart_Close(port->func); goto FAIL; } port->Close = uart_Close; port->SetTimeOut = uart_SetTimeOut; port->Read = uart_Read; port->Receive = uart_Receive; port->QueryRxQueue = uart_QueryRxQueue; port->RxQueueFull = uart_RxQueueFull; port->RxQueueEmpty = uart_RxQueueEmpty; port->FlushRxQueue = uart_FlushRxQueue; port->Write = uart_Write; port->Send = uart_Send; port->QueryTxQueue = uart_QueryTxQueue; port->TxQueueFull = uart_TxQueueFull; port->TxQueueEmpty = uart_TxQueueEmpty; port->FlushTxQueue = uart_FlushTxQueue; port->TxReady = uart_TxReady; port->FlushWFIFO = uart_FlushWFIFO; port->SetBPS = uart_SetBaud; port->SetFormat = uart_SetFormat; port->SetFlowControl = uart_SetFlowControl; port->EnableFIFO = uart_SetHWFIFO; port->SetWFIFOSize = uart_SetWFIFOSize; port->ClearRFIFO = uart_ClearRFIFO; port->ClearWFIFO = uart_ClearWFIFO; port->SetLSRHandler = uart_SetLSRHandler; port->SetMSRHandler = uart_SetMSRHandler; port->GetLSR = uart_GetLSR; port->GetMSR = uart_GetMSR; port->EnableHalfDuplex = uart_EnableHalfDuplex; port->EnableFullDuplex = uart_EnableFullDuplex; port->EnableDebugMode = uart_EnableDebugMode; port->DisableDebugMode = uart_DisableDebugMode; return port; } case USB_COM: { if ((port->func = (void *)CreateUSBDevice()) == NULL) goto FAIL; if (set_usb_pins == false) { err_print((char*)"%s: no set USB-Device pins.\n", __FUNCTION__); usb_Close(port->func); goto FAIL; } if (usb_SetUSBPins(port->func, usb_detect_port, usb_detect_pin, usb_on_off_port, usb_on_off_pin) == false) { usb_Close(port->func); goto FAIL; } if (usb_Init(port->func) == false) { usb_Close(port->func); goto FAIL; } port->Close = usb_Close; port->SetTimeOut = usb_SetTimeOut; port->Read = usb_Read; port->Receive = usb_Receive; port->QueryRxQueue = usb_QueryRxQueue; port->RxQueueFull = usb_RxQueueFull; port->RxQueueEmpty = usb_RxQueueEmpty; port->FlushRxQueue = usb_FlushRxQueue; port->Write = usb_Write; port->Send = usb_Send; port->QueryTxQueue = usb_QueryTxQueue; port->TxQueueFull = usb_TxQueueFull; port->TxQueueEmpty = usb_TxQueueEmpty; port->FlushTxQueue = usb_FlushTxQueue; port->TxReady = usb_TxReady; port->FlushWFIFO = usb_FlushWFIFO; port->Ready = usb_Ready; port->GetLineCoding = usb_GetLineCoding; port->SetSerialState = usb_SetSerialState; port->GetControlLineState= usb_GetControlLineState; return port; } case CAN_BUS: { if ((port->func = (void *)CreateCANBus(IO_PORT)) == NULL) goto FAIL; if (can_Init(port->func) == false) { can_Close(port->func); goto FAIL; } port->Close = can_Close; port->SetTimeOut = can_SetTimeOut; port->QueryRxQueue = can_QueryRxQueue; port->RxQueueFull = can_RxQueueFull; port->RxQueueEmpty = can_RxQueueEmpty; port->FlushRxQueue = can_FlushRxQueue; port->QueryTxQueue = can_QueryTxQueue; port->TxQueueFull = can_TxQueueFull; port->TxQueueEmpty = can_TxQueueEmpty; port->FlushTxQueue = can_FlushTxQueue; port->TxReady = can_TxReady; port->FlushWFIFO = can_FlushWFIFO; port->SetBPS = can_SetBPS; port->Reset = can_Reset; port->AddIDFilter = can_AddIDFilter; port->GetIDFilter = can_GetIDFilter; port->DelIDFilter = can_DelIDFilter; port->ClearIDList = can_ClearIDList; port->EnableBypass = can_EnableBypass; port->DisableBypass = can_DisableBypass; port->SetEWLimit = can_SetEWLimit; port->GetEWLimit = can_GetEWLimit; port->GetTxErrorCount = can_GetTxErrorCount; port->GetRxErrorCount = can_GetRxErrorCount; port->GetNowState = can_GetNowState; port->EnableStoreError = can_EnableStoreError; port->DisableStoreError = can_DisableStoreError; port->SetCANBusOffHandler= can_SetCANBusOffHandler; port->PopError = can_PopError; port->GetLastError = can_GetLastError; port->ReadCAN = can_Read; port->WriteCAN = can_Write; return port; } default: break; }; FAIL: ker_Mfree(port); return NULL; }
int main() { SYSCTL->RCGCGPIO |= 0x01; // GPIOA SYSCTL->RCGCUART |= 0x01; // UART0 // Pins [0:1] digital enable GPIOA->DEN |= 0x3; // Pins [0:1] output GPIOA->DIR |= 0x3; // Pins [0:1] high //GPIOA->DATA |= 0x3; // Pins [0:1] alternate functionality (UART0 Rx/Tx) GPIOA->AFSEL |= 0x3; // Clear and set port mux for GPIOB pins [0:1] GPIOA->PCTL &= ~0xFF; GPIOA->PCTL |= 0x11; // Disable UART0 UART0->CTL &= ~0x1; // Clear and set integer baud-rate divisor UART0->IBRD &= ~0xFFFF; UART0->IBRD |= (16000000 / (16u * 9600u)) & 0xFFFF; // Clear and set fractional baud-rate divisor UART0->FBRD &= ~0x3F; UART0->FBRD |= (((16000000u * 128u) / (16u * 9600u) + 1)/2) & 0x3F; // Clear and set line control (8-bit, no FIFOs, 1 stop bit) UART0->LCRH &= ~0xFF; UART0->LCRH |= 0x70; // Clear and set clock source (PIOSC) UART0->CC &= ~0xF; UART0->CC |= 0x5; // Enable DMA on Tx line // UART0->DMACTL |= 0x2; // Enable interrupts for data received // UART0->IM |= 0x10; // Enable loopback operation // UART0->CTL |= 0x80; // Enable UART0 UART0->CTL |= 0x1; led_Init(); can_Init(); //can_ReadInit(0x400, 0x7F8, 4); CanPacket packet = { .arbid = 0x165, // Right Mirror Move Right //.data = { 0x00, 0x00, 0x00, 0x01, 0x10, 0x2C, 0x80, 0x20 }, // Unlock doors .data = { 0x20, 0xC0, 0x00, 0x00, 0x10, 0x65, 0x00, 0x00 }, }; can_Loopback(); //CanPacket packet2; //can_ReadBlock(&packet2); led_Set(true, false, false); // Init for the lists init_list(id_4D, 0x4D); init_list(id_11A, 0x11A); init_list(id_130, 0x130); init_list(id_139, 0x139); init_list(id_156, 0x156); init_list(id_165, 0x165); init_list(id_167, 0x167); init_list(id_171, 0x171); init_list(id_178, 0x178); init_list(id_202, 0x202); init_list(id_179, 0x179); init_list(id_204, 0x204); init_list(id_185, 0x185); init_list(id_25C, 0x25C); init_list(id_1A0, 0x1A0); init_list(id_200, 0x200); init_list(id_230, 0x230); init_list(id_25A, 0x25A); init_list(id_25B, 0x25B); init_list(id_270, 0x270); init_list(id_280, 0x280); init_list(id_312, 0x312); init_list(id_352, 0x352); init_list(id_365, 0x365); init_list(id_366, 0x366); init_list(id_367, 0x367); init_list(id_368, 0x368); init_list(id_369, 0x369); init_list(id_410, 0x410); init_list(id_421, 0x421); init_list(id_42D, 0x42D); init_list(id_42F, 0x42F); init_list(id_43E, 0x43E); init_list(id_440, 0x440); init_list(id_472, 0x472); init_list(id_473, 0x473); init_list(id_474, 0x474); init_list(id_475, 0x475); init_list(id_476, 0x476); init_list(id_477, 0x477); init_list(id_595, 0x595); while(1) { //uint8_t buf[12]; //pc_ReadData(buf, 2); //led_Byte(buf[0]); //led_Byte(buf[1]); //UART0->DR = 'a'; for(unsigned i = 0 ; i < 50000 ; i ++); can_Inject(&packet); } /* //FPUEnable(); pc_comm_init(); count = 0; // Enable clocking to peripherals SYSCTL->RCGCTIMER |= 1; // TIMER0 // Enable TIMER0A Interrupt NVIC->ISER[0] |= (1 << TIMER0A_IRQn); // Kill TIMER0A TIMER0->CTL &= ~0x1; // 32 Bit timer configuration for TIMER0 TIMER0->CFG = 0; // Regular, one-shot timer mode TIMER0->TAMR = 1; // .5 seconds at 16MHz TIMER0->TAILR = 8000000; // TIMER0A timeout interrupt mask TIMER0->IMR = 1; // Enable TIMER0A, enable debug stalling TIMER0->CTL |= 0x3; while(1){}; */ }
int main(void) { utDisInt; utInitInt(); utInitSys(); utEnInt; /* Can bus init */ can_Init(); /* iobus init */ iobus_init(); /* 步进电机初始化 */ #if defined(motor_device) /* L6470端口及SPI初始化 */ dSPIN_Peripherals_Init(); /* L6470复位 */ dSPIN_Reset_And_Standby(dSPIN_Device_All); /* L6470中断初始化 */ dSPIN_Interrupt_Channel_Config(); /* 电机参数初始化 */ motor_init_set(); ENC_Init(); #endif utPrt("********************************\r\n"); utPrt("CPU: STM32F103RCT6 at 72 MHz \r\n"); utPrt("Board: TMCS Board (Version2) \r\n"); utPrt("Editor: Tsui \r\n"); utPrt("Compant: TMCS \r\n"); utPrt("Shell: Shell Version1 \r\n"); utPrt("Compile Date: %s \r\n",__DATE__); utPrt("Compile TIME: %s \r\n",__TIME__); utPrt("********************************\r\n"); /* set can baud rate at 250Kbit */ can_set_baudrate(can_parameter._t_bit_rate); #if 0 g_tCanTxMsg.DLC = 3; g_tCanTxMsg.Data[0] = 0x11; g_tCanTxMsg.Data[1] = 0x22; g_tCanTxMsg.Data[2] = 0x33; SendCanMsg(&g_tCanTxMsg); #endif DetectCal(); iobus_reload(); utDelay1s(1); ENC_Clean(); /* Start os */ utPrt("Start utOs......\n"); utStartOS(); }