void SERIAL_Init(void) { transmitBuffer = (struct CircBuffer*) &outgoingUart; //set up buffer for receive newCircBuffer(transmitBuffer); receiveBuffer = (struct CircBuffer*) &incomingUart; //set up buffer for transmit newCircBuffer(receiveBuffer); UARTConfigure(UART1, 0x00); UARTSetDataRate(UART1, F_PB, 115200); UARTSetFifoMode(UART1, UART_INTERRUPT_ON_RX_NOT_EMPTY | UART_INTERRUPT_ON_TX_BUFFER_EMPTY); INTSetVectorPriority(INT_UART_1_VECTOR, INT_PRIORITY_LEVEL_4); //set the interrupt priority //RPC5R = 1; PPSOutput(1, RPC5, U1TX); PORTSetPinsDigitalIn(IOPORT_C, BIT_3); //U1RXRbits.U1RXR = 0b111; PPSInput(3, U1RX, RPC3); UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX)); INTEnable(INT_U1RX, INT_ENABLED); //INTSetFlag(INT_U1RX); //INTEnable(INT_U1TX, INT_ENABLED); //PutChar('z'); }
void midgParserInit(void){ // initialize the circular buffer #if __IN_DSPIC__ midgBuffer = (struct CircBuffer* )&midgParseBuffer; newCircBuffer(midgBuffer); #else midgBuffer = newCircBuffer(BSIZE); #endif }
void UART2Init(void) { // Configure and open the port; // U2MODE Register // ============== U2MODEbits.UARTEN = 0; // Disable the port U2MODEbits.USIDL = 0; // Stop on idle U2MODEbits.IREN = 0; // No IR decoder U2MODEbits.RTSMD = 0; // Ready to send mode (irrelevant) U2MODEbits.UEN = 0; // Only RX and TX U2MODEbits.WAKE = 1; // Enable at startup U2MODEbits.LPBACK = 0; // Disable loopback U2MODEbits.ABAUD = 0; // Disable autobaud U2MODEbits.URXINV = 0; // Normal operation (high is idle) U2MODEbits.PDSEL = 0; // No parity 8 bit U2MODEbits.STSEL = 0; // 1 stop bit U2MODEbits.BRGH = 1; IPC7 = 0x4400; // U2STA Register // ============== U2STAbits.URXISEL = 0; // RX interrupt on every character U2STAbits.OERR = 0; // clear overun error // U2BRG Register // ============== U2BRG = 86; // Set the baud rate to 115200 by 86 576000 by 173 // Enable the port; U2MODEbits.UARTEN = 1; // Enable the port U2STAbits.UTXEN = 1; // Enable TX UART_transmitBuffer=(struct CircBuffer*)&outgoingUart; //set up buffer for receive newCircBuffer(UART_transmitBuffer); //printf("%p\r\n",BufferB); #ifdef DMAON need_buffer=0; DMA0CON=0x0002; DMA0CONbits.DIR=0; //peripheral to ram DMA0CONbits.MODE=2; // ping pong DMA0CONbits.SIZE=1; //char supposedly in size, seems to be true DMA0CNT=DMABUFFERSIZE-1; DMA0REQ=0b0011110; DMA0PAD=(volatile unsigned int) &U2RXREG; DMA0STA=__builtin_dmaoffset(BufferA); DMA0STB=__builtin_dmaoffset(BufferB); IFS0bits.DMA0IF=0; IEC0bits.DMA0IE=1; DMA0CONbits.CHEN = 1; #else IEC1bits.U2RXIE = 1; // Enable RX interrupts UART_receiveBuffer=(struct CircBuffer*)&incomingUart; //set up buffer for transmit newCircBuffer(UART_receiveBuffer); #endif }
void SERIAL_Init(void) { transmitBuffer = (struct CircBuffer*) &outgoingUart; //set up buffer for receive newCircBuffer(transmitBuffer); receiveBuffer = (struct CircBuffer*) &incomingUart; //set up buffer for transmit newCircBuffer(receiveBuffer); UARTConfigure(UART1, 0x00); UARTSetDataRate(UART1, F_PB, 115200); UARTSetFifoMode(UART1, UART_INTERRUPT_ON_RX_NOT_EMPTY | UART_INTERRUPT_ON_RX_NOT_EMPTY); INTSetVectorPriority(INT_UART_1_VECTOR, INT_PRIORITY_LEVEL_4); //set the interrupt priority UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX)); INTEnable(INT_U1RX, INT_ENABLED); INTEnable(INT_U1TX, INT_ENABLED); }
void uartMavlinkBufferInit (void){ #if (WIN != 1) _U1RXIP = 6; /* Rx Interrupt priority set to 1 */ _U1RXIF = 0; _U1RXIE = 1; /* Enable Interrupt */ /* Configure Remappables Pins */ RPINR18 = 0x62; #endif uartMavlinkInBuffer = (struct CircBuffer*) &comMavlinkBuffer; newCircBuffer(uartMavlinkInBuffer); }
void spiSlaveInit(void) { // Initialize the circular buffer protBuffer = &spiBuffer; newCircBuffer (protBuffer); // Configure the Analog pins B2, and SPI pins as digital. // This is done as work-around for a bug in the // dspic blockset version 0.98d AD1PCFGLbits.PCFG2 = 1; AD2PCFGLbits.PCFG2 = 1; TRISFbits.TRISF7 = 1; TRISFbits.TRISF8 = 0; TRISBbits.TRISB2 = 1; // SPI1CON1 Register Settings SPI1CON1bits.DISSCK = 0; //Internal Serial Clock is Enabled. SPI1CON1bits.DISSDO = 0; //SDOx pin is controlled by the module. SPI1CON1bits.MODE16 = 0; //Communication is byte-wide (8 bits). SPI1CON1bits.SMP = 0; //Cleared in slave mode. SPI1CON1bits.CKE = 0; //Serial output data changes on transition from //Idle clock state to active clock state SPI1CON1bits.CKP = 0; //Idle state for clock is a low level; //active state is a high level SPI1CON1bits.SSEN = 1; // SS Used in slave mode SPI1CON1bits.MSTEN = 0; //Slave Mode Enabled // Configure the clock for 10 MHz SPI1CON1bits.SPRE = 7; //Secondary prescaler to 1:1 SPI1CON1bits.PPRE = 2; //Primary prescaler 4:1 // Enable the module SPI1STATbits.SPIROV = 0; //Clear overflow bit SPI1STATbits.SPIEN = 1; //Enable SPI Module // clear the interrupt flag IFS0bits.SPI1IF = 0; // clear the error flag IFS0bits.SPI1EIF = 0; // clear the overflow flag SPI1STATbits.SPIROV = 0; // Enable the interrupts IPC2bits.SPI1IP = 6; IEC0bits.SPI1EIE = 1; IEC0bits.SPI1IE = 1; INTCON1bits.NSTDIS = 1; }
void UART_init(uint8_t id, uint32_t baudRate){ //will need buffers for both the UARTS if(id == UART1_ID){ transmitBufferUart1 = (struct CircBuffer*) &outgoingUart1; //set up buffer for receive newCircBuffer(transmitBufferUart1); receiveBufferUart1 = (struct CircBuffer*) &incomingUart1; //set up buffer for transmit newCircBuffer(receiveBufferUart1); UARTConfigure(UART1, 0x00); UARTSetDataRate(UART1, F_PB, baudRate); UARTSetFifoMode(UART1, UART_INTERRUPT_ON_RX_NOT_EMPTY); mU1SetIntPriority(4); //set the interrupt priority UARTEnable(UART1, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX)); mU1RXIntEnable(1); mU1TXIntEnable(1); }else if(id == UART2_ID){ transmitBufferUart2 = (struct CircBuffer*) &outgoingUart2; //set up buffer for receive newCircBuffer(transmitBufferUart2); receiveBufferUart2 = (struct CircBuffer*) &incomingUart2; //set up buffer for transmit newCircBuffer(receiveBufferUart2); UARTConfigure(UART2, 0x00); UARTSetDataRate(UART2, F_PB, baudRate); UARTSetFifoMode(UART2, UART_INTERRUPT_ON_RX_NOT_EMPTY); mU2SetIntPriority(4); //set the interrupt priority UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_TX | UART_RX)); mU2RXIntEnable(1); mU2TXIntEnable(1); } }
void protParserInit(void){ // initialize the circular buffer ppBuffer = (struct CircBuffer* )&protParseBuffer; newCircBuffer(ppBuffer); // Initialize all global data structures memset(&gpsControlData, 0, sizeof(tGpsData)); memset(&rawControlData, 0, sizeof(tRawData)); memset(&statusControlData, 0, sizeof(tSensStatus)); memset(&attitudeControlData, 0, sizeof(tAttitudeData)); memset(&attitudeRotatedControlData, 0, sizeof(tAttitudeData)); memset(&dynTempControlData, 0, sizeof(tDynTempData)); memset(&biasControlData, 0, sizeof(tBiasData)); memset(&diagControlData, 0, sizeof(tDiagData)); memset(&xyzControlData, 0, sizeof(tXYZData)); memset(&aknControlData, 0, sizeof(tAknData)); memset(&pilControlData, 0, sizeof(tPilotData)); memset(&pwmControlData, 0, sizeof(tPWMData)); memset(&pidControlData, 0, sizeof(tPIDData)); memset(&queControlData, 0, sizeof(tQueryData)); memset(&wpsControlData, 0, sizeof(tWPData)); memset(&apsControlData, 0, sizeof(tAPStatusData)); memset(&comControlData, 0, sizeof(tCommandsData)); memset(&navControlData, 0, sizeof(tNavData)); memset(&senControlData, 0, sizeof(tSensData)); memset(&logControlData, 0, sizeof(tLogFloats)); filterControlData = 0; // Control MCU boot procedures #ifdef _IN_PC_ // #else aknControlData.reboot = 1; #endif // Manual mode apsControlData.controlType = CTRL_TYPE_MANUAL; }
void schedulerInit (void){ // initialize the circular buffers logBuffer = (struct CircBuffer* )&com2Buffer; newCircBuffer(logBuffer); uartBufferIn = (struct CircBuffer* )&com2BufferIn; newCircBuffer(uartBufferIn); // DMA0REQ Register // ================ DMA0REQ = 0x001F; // DMA0PAD Register // ================ DMA0PAD = (volatile unsigned int) &U2TXREG; // DMA0CON Register // ================ DMA0CONbits.AMODE = 0; // Register Indirect with post-increment DMA0CONbits.MODE = 1; // One-shot, No Ping-Pong Mode DMA0CONbits.DIR = 1; // Read from RAM and send to Periphereal DMA0CONbits.SIZE = 0; // Word Data Transfer // DMA0CNT Register // ============== DMA0CNT = MAXSEND-1; // DMA0STA Register // ================ DMA0STA= __builtin_dmaoffset(BufferA); // Enable DMA0 TX interrupts IFS0bits.DMA0IF = 0; // Clear DMA Interrupt Flag IEC0bits.DMA0IE = 1; // Enable DMA interrupt // Configure and open the port; // U2MODE Register // ============== U2MODEbits.ABAUD = 0; // Disable autobaud U2MODEbits.PDSEL = 0; // No parity 8 bit U2MODEbits.STSEL = 0; // 1 stop bit U2MODEbits.BRGH = 0; // Low speed mode // U2STA Register // ============== U2STAbits.UTXISEL0 = 0; // generate interrupt on every char U2STAbits.UTXISEL1 = 0; // for the DMA U2STAbits.URXISEL = 0; // RX interrupt with every char U2STAbits.OERR = 0; // clear overun error // U2BRG Register // ============== U2BRG = LOG_UBRG; // Set the baud rate for data logger // Initialize the Interrupt // ======================== IPC7bits.U2RXIP = 5; // Interrupt priority 5 IFS1bits.U2RXIF = 0; // Clear the interrupt flag IEC1bits.U2RXIE = 1; // Enable interrupts // Enable the port; U2MODEbits.UARTEN = 1; // Enable the port U2STAbits.UTXEN = 1; // Enable TX IEC4bits.U2EIE = 0; }
// UART and Buffer initialization void uartInit (void){ // initialize the circular buffer uartBuffer = (struct CircBuffer* )&com1Buffer; newCircBuffer(uartBuffer); // Configure and open the port; // U1MODE Register // ============== U1MODEbits.UARTEN = 0; // Disable the port U1MODEbits.USIDL = 0; // Stop on idle U1MODEbits.IREN = 0; // No IR decoder U1MODEbits.RTSMD = 0; // Ready to send mode (irrelevant) U1MODEbits.UEN = 0; // Only RX and TX U1MODEbits.WAKE = 1; // Enable at startup U1MODEbits.LPBACK = 0; // Disable loopback U1MODEbits.ABAUD = 0; // Disable autobaud U1MODEbits.URXINV = 0; // Normal operation (high is idle) U1MODEbits.PDSEL = 0; // No parity 8 bit U1MODEbits.STSEL = 0; // 1 stop bit U1MODEbits.BRGH = 0; // Low speed mode // U1STA Register // ============== U1STAbits.URXISEL = 2; // RX interrupt when 3 chars are in U1STAbits.OERR = 0; // clear overun error // U1BRG Register // ============== U1BRG = UCSCAP_UBRGI; // Set the baud rate for initial config of GPS // Enable the port; U1MODEbits.UARTEN = 1; // Enable the port U1STAbits.UTXEN = 1; // Enable TX // wait for the UART to settle int i; for( i = 0; i < 32700; i += 1 ) { Nop(); } // Configure the GPS sentences and change the baud Rate gpsSentenceConfig(); // Disable the port and TX; U1MODEbits.UARTEN = 0; // Disable the port // U1BRG Register // ============== U1BRG = UCSCAP_UBRGF; // Set the baud rate for operation of GPS // Enable the port; U1MODEbits.UARTEN = 1; // Enable the port U1STAbits.UTXEN = 1; // Enable TX for( i = 0; i < 32700; i += 1 ) { Nop(); } // Configure the frequency to 5 Hz gpsFreqConfig(); /* // Disable the port and TX; U1MODEbits.UARTEN = 0; // Disable the port // Initialize the Interrupt IPC2bits.U1RXIP = 6; // Interrupt priority 6 IFS0bits.U1RXIF = 0; // Clear the interrupt flag IEC0bits.U1RXIE = 1; // Enable interrupts // Enable the port; U1STAbits.UTXEN = 0; // Disable TX U1MODEbits.UARTEN = 1; // Enable the port */ }