//***************************************************************************** // // gpio.c - API for GPIO ports // // Copyright (c) 2005-2012 Texas Instruments Incorporated. All rights reserved. // Software License Agreement // // Texas Instruments (TI) is supplying this software for use solely and // exclusively on TI's microcontroller products. The software is owned by // TI and/or its suppliers, and is protected under applicable copyright // laws. You may not combine this software with "viral" open-source // software in order to form a larger program. // // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL // DAMAGES, FOR ANY REASON WHATSOEVER. // // This is part of revision 8555 of the Stellaris Peripheral Driver Library. // //***************************************************************************** //***************************************************************************** // //! \addtogroup gpio_api //! @{ // //***************************************************************************** #include "inc/hw_gpio.h" #include "inc/hw_ints.h" #include "inc/hw_memmap.h" #include "inc/hw_sysctl.h" #include "inc/hw_types.h" #include "driverlib/debug.h" #include "driverlib/gpio.h" #include "driverlib/interrupt.h" #include "driverlib/sysctl.h" #include "driverlib/uart.h" #include #include "utils/uartstdio.h" #include "inc/hw_can.h" #include "driverlib/can.h" #define CAN_IDLE 0 #define CAN_SENDING 1 #define CAN_WAIT_RX 2 #define CAN_PROCESS 3 // // Size of the FIFOs allocated to the CAN controller. // #define CAN_FIFO_SIZE 32 // // Message object used by the transmit message FIFO. // #define TRANSMIT_MESSAGE_ID 11 // // Message object used by the receive message FIFO. // #define RECEIVE_MESSAGE_ID 8 // // The number of FIFO transfers that cause a toggle of the LED. // #define TOGGLE_RATE 100 // // The CAN bit rate. // #define CAN_BITRATE 250000 #ifdef DEBUG void __error__(char *pcFilename, unsigned long ulLine) { } #endif // // This structure holds all of the state information for the CAN transfers. // struct { // // This holds the information for the data receive message object that is // used to receive data for each CAN controller. // tCANMsgObject MsgObjectRx; // // This holds the information for the data send message object that is used // to send data for each CAN controller. // tCANMsgObject MsgObjectTx; // // Receive buffer. // unsigned char pucBufferRx[CAN_FIFO_SIZE]; // // Transmit buffer. // unsigned char pucBufferTx[CAN_FIFO_SIZE]; // // Bytes remaining to be received. // unsigned long ulBytesRemaining; // // Bytes transmitted. // unsigned long ulBytesTransmitted; // // The current state of the CAN controller. // /* enum { CAN_IDLE, CAN_SENDING, CAN_WAIT_RX, CAN_PROCESS, } */ char eState; } g_sCAN; //***************************************************************************** // // This function configures the transmit FIFO and copies data into the FIFO. // //***************************************************************************** int CANTransmitFIFO(unsigned char *pucData, unsigned long ulSize) { int iIdx; // // This is the message object used to send button updates. This message // object will not be "set" right now as that would trigger a transmission. // g_sCAN.MsgObjectTx.ulMsgID = TRANSMIT_MESSAGE_ID; g_sCAN.MsgObjectTx.ulMsgIDMask = 0; // // This enables interrupts for transmitted messages. // g_sCAN.MsgObjectTx.ulFlags = MSG_OBJ_TX_INT_ENABLE; // // Return the maximum possible number of bytes that can be sent in a single // FIFO. // if(ulSize > CAN_FIFO_SIZE) { return(CAN_FIFO_SIZE); } // // Loop through all eight message objects that are part of the transmit // FIFO. // for(iIdx = 0; iIdx < 8; iIdx++) { // // If there are more than eight bytes remaining then use a full message // to transfer these 8 bytes. // if(ulSize > 8) { // // Set the length of the message, which can only be eight bytes // in this case as it is all that can be sent with a single message // object. // g_sCAN.MsgObjectTx.ulMsgLen = 8; g_sCAN.MsgObjectTx.pucMsgData = &pucData[iIdx * 8]; // // Set the MSG_OBJ_FIFO to indicate that this is not the last // data in a chain of FIFO entries. // g_sCAN.MsgObjectTx.ulFlags |= MSG_OBJ_FIFO; // // There are now eight less bytes to transmit. // ulSize -= 8; // // Write out this message object. // CANMessageSet(CAN0_BASE, iIdx + 1, &g_sCAN.MsgObjectTx, MSG_OBJ_TYPE_TX); } // // If there are less than or exactly eight bytes remaining then use a // message object to transfer these 8 bytes and do not set the // MSG_OBJ_FIFO flag to indicate that this is the last of the entries // in this FIFO. // else { // // Set the length to the remaining bytes and transmit the data. // g_sCAN.MsgObjectTx.ulMsgLen = ulSize; g_sCAN.MsgObjectTx.pucMsgData = &pucData[iIdx * 8]; // // Write out this message object. // CANMessageSet(CAN0_BASE, iIdx + 1, &g_sCAN.MsgObjectTx, MSG_OBJ_TYPE_TX); } } return(0); } //***************************************************************************** // // Send a string to the UART. // //***************************************************************************** int putchar (int iCh) { UARTCharPut (UART0_BASE, iCh); return (iCh); } void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount) { // // Loop while there are more characters to send. // while(ulCount--) { // // Write the next character to the UART. // UARTCharPutNonBlocking(UART0_BASE, *pucBuffer++); } } void main () { int iIdx; unsigned long Clk; // óñòàíîâèòü ÷àñòó íà 80 ÌÃö SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |SYSCTL_OSC_MAIN); Clk=SysCtlClockGet(); // // Configure CAN 0 Pins. // SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1); // // Enable the CAN controller. // SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); // // Reset the state of all the message object and the state of the CAN // module to a known state. // CANInit(CAN0_BASE); // // Configure the bit rate for the CAN device, the clock rate to the CAN // controller is fixed at 50MHz for this class of device and the bit rate is // set to CAN_BITRATE. // CANBitRateSet(CAN0_BASE, 50000000, CAN_BITRATE); // // Take the CAN0 device out of INIT state. // CANEnable(CAN0_BASE); // // Set the initial state to idle. // g_sCAN.eState = CAN_IDLE; // // Initialize the CAN FIFO buffer. // for(iIdx = 0; iIdx < CAN_FIFO_SIZE; iIdx++) { g_sCAN.pucBufferTx[iIdx] = iIdx + 0x1; } // // Reset the buffer pointer. // g_sCAN.MsgObjectRx.pucMsgData = g_sCAN.pucBufferRx; // // Set the total number of bytes expected. // g_sCAN.ulBytesRemaining = CAN_FIFO_SIZE; SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); //GPIOPinConfigure(GPIO_PD2_U1RX); //GPIOPinConfigure(GPIO_PD3_U1TX); // Óñòàíîâèòü GPIO A0 è A1 êàê âûâîäû UART GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); UARTStdioInit(0); UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 9600, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); /*while (1) { for (int counter=0;counter<5000000; counter++){} //UARTprintf ( "UARTprintf \n" ); printf ( "Hello\n" ); } }*/ while(1) { switch(g_sCAN.eState) { case CAN_IDLE: { // // Switch to sending state. // g_sCAN.eState = CAN_SENDING; // // Initialize the transmit count to zero. // g_sCAN.ulBytesTransmitted = 0; // // Schedule all of the CAN transmissions. // CANTransmitFIFO(g_sCAN.pucBufferTx, CAN_FIFO_SIZE); break; } case CAN_SENDING: { // // Wait for all bytes to go out. // if(g_sCAN.ulBytesTransmitted == CAN_FIFO_SIZE) { // // Switch to wait for RX state. // g_sCAN.eState = CAN_WAIT_RX; } break; } case CAN_WAIT_RX: { // // Wait for all new data to be received. // if(g_sCAN.ulBytesRemaining == 0) { // // Switch to wait for Process data state. // g_sCAN.eState = CAN_PROCESS; // // Reset the buffer pointer. // g_sCAN.MsgObjectRx.pucMsgData = g_sCAN.pucBufferRx; // // Reset the number of bytes expected. // g_sCAN.ulBytesRemaining = CAN_FIFO_SIZE; } break; } case CAN_PROCESS: { // // Compare the received data to the data that was sent out. // for(iIdx = 0; iIdx < CAN_FIFO_SIZE; iIdx++) { if(g_sCAN.pucBufferTx[iIdx] != g_sCAN.pucBufferRx[iIdx]) { // // Detected an Error Condition. // UARTprintf ( "Error \n" ); break; } } // // Change the CAN FIFO data. // for(iIdx = 0; iIdx < CAN_FIFO_SIZE; iIdx++) { // // Increment the data to change it. // g_sCAN.pucBufferTx[iIdx] += 0xB; } // // Return to the idle state. // g_sCAN.eState = CAN_IDLE; break; } default: { break; } } } }