// Set up the SPI for the MicroSD card void sdSetup(void) { // Configure Port Direction TRISDbits.TRISD5 = 0; // Turn RD5 into output for SCLK TRISDbits.TRISD4 = 1; // Turn RD4 into input for MISO TRISDbits.TRISD3 = 0; // Turn RD3 into output for MOSI TRISDbits.TRISD2 = 0; // Turn RD2 into output for SS TRISDbits.TRISD1 = 1; // Turn RD1 into input for CD (card detect) // Configure PPS pins for MicroSD iPPSInput(IN_FN_PPS_SDI1,IN_PIN_PPS_RP25); // Assign SDI1 to pin RP25 iPPSOutput(OUT_PIN_PPS_RP20,OUT_FN_PPS_SCK1OUT); // Assign SCK1OUT to pin RP20 iPPSOutput(OUT_PIN_PPS_RP22,OUT_FN_PPS_SDO1); // Assign SDO1 to pin RP22 iPPSOutput(OUT_PIN_PPS_RP23,OUT_FN_PPS_SS1OUT); // Assign SS1OUT to pin RP23 // Close SPI in case it's already open CloseSPI1(); // Enable SPI interface // Clear and disable SPI interupts for now SPI1_Clear_Intr_Status_Bit; DisableIntSPI1; ConfigIntSPI1(SPI_INT_DIS); // Interrupts disabled OpenSPI1(0x0000, MASTER_ENABLE_ON, SPI_ENABLE); // Master Mode // SPI enabled }
void main(void) { uint8_t max = MAX; dp_std_status_t current_status; dp_std_command_t cmd_from_master; int8_t adc = 1; // Debug LED TRISCbits.TRISC2 = 0; PORTCbits.RC2 = 0; // Initialize LEDs led_initialize(); // Initialize ADC OpenADC ( ADC_FOSC_16 & ADC_RIGHT_JUST & ADC_4_TAD, // ADC_4_TAD = Converting over 4*(1/(FOSC/16)) = 4*(1/250kHz) = 16us ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS, // FOSC = 4 MHz 0b0000000000000001 // wieso geht ADC_1ANA nicht? ); // Open SPI OpenSPI1(SLV_SSON, MODE_00, SMPMID); // Initial ADC conversion and SPI reading ConvertADC(); cmd_from_master.byte = spi_tranceive(0); /* MAIN LOOP: * At first we read the ADC value of the piezo-weight sensors if already present. * Then we fill this data into a status package which is to be sent to our SPI master. * After that we await a command from our SPI master and send our status to it (tranceive). * If we got a color, we light our LEDs per PWM. If we got a command, we analyze it and take action. */ while (1) { if (!BusyADC()) { adc = ReadADC() >> 2; // we just need 8bits from the returned 10bit sample ConvertADC(); // already start next conversion. Meanwhile we're gonna do stuff with SPI } current_status.data.value = adc; current_status.is_pressed = 0; // TODO: Analyze the adc value and set this one to 0 or 1 cmd_from_master.byte = spi_tranceive(current_status.byte); // TODO: error handling needed: what if spi is not present? Will we operate on our own? if (cmd_from_master.is_rgb) { // we got a color PORTCbits.RC2 = 0; // Debug LED led_set_rgb(cmd_from_master.rgb.r*10, cmd_from_master.rgb.g*10, cmd_from_master.rgb.b*10); // TODO: introduce multiplicator to reach 100% PWM } else { // we got a command PORTCbits.RC2 = 1; // Debug LED } }
/**************************************************************************** Function: bool DRV_SPI_Initialize(SpiChannel chn, SpiOpenFlags oFlags, unsigned int fpbDiv) Summary: This function initializes the SPI channel and also sets the brg register. Description: This function initializes the SPI channel and also sets the brg register. The SPI baudrate BR is given by: BR=Fpb/(2*(SPIBRG+1)) The input parametes fpbDiv specifies the Fpb divisor term (2*(SPIBRG+1)), so the BRG is calculated as SPIBRG=fpbDiv/2-1. Precondition: None Parameters: chn - the channel to set oFlags - a SpiOpenFlags or __SPIxCONbits_t structure that sets the module behavior fpbDiv - Fpb divisor to extract the baud rate: BR=Fpb/fpbDiv. Returns: true if success false otherwise Remarks: - The baud rate is always obtained by dividing the Fpb to an even number between 2 and 1024. - When selecting the number of bits per character, SPI_OPEN_MODE32 has the highest priority. If SPI_OPEN_MODE32 is not set, then SPI_OPEN_MODE16 selects the character width. - The SPI_OPEN_SSEN is taken into account even in master mode. If it is set the library will properly se the SS pin as an digital output. ***************************************************************************/ bool DRV_SPI_Initialize(SpiChannel chn, SpiOpenFlags oFlags, unsigned int fpbDiv) { #if defined (__C32__) SpiChnOpen(chn, oFlags, fpbDiv); #elif defined (__C30__) volatile uint16_t con1 = 0; uint16_t con2 = 0; uint16_t con3 = 0; uint8_t i; if((SYS_CLK_PeripheralClockGet()/fpbDiv) > 10000000ul) { SYS_ASSERT(false, "Requested SPI frequency is not supported!"); return false; // the SPI clock is selected more than 10MHz. // Select the frequency as per the data sheet of the particular 16bit device. } for(i = 0; i < SPI_CLK_TBL_ELEMENT_COUNT; i++) { if((SYS_CLK_PeripheralClockGet()/fpbDiv) <= SpiClkTbl[i].clock) { con1 = SpiClkTbl[i].scale; break; } } con1 |= oFlags; con3 |= SPI_EN; switch(chn) { case 1: OpenSPI1(con1,con2,con3); break; case 2: SPI2STAT &= 0x7FFF; OpenSPI2(con1,con2,con3); break; default: SYS_ASSERT(false, "Requested SPI channel is not supported!"); return false; } #endif return true; }
/** * @brief: Initialisation SPI * @param: void * @return: void */ void SPI_Init(void) { // Turn off leds before configuring the IO pin as output SET_CSB; // same as LATDCLR = 0x0007 mPORTBSetPinsDigitalOut(CSB_BIT); // same as TRISDCLR = 0x0007 unsigned int config1 = FRAME_ENABLE_OFF|ENABLE_SDO_PIN|SPI_MODE8_ON|SPI_SMP_OFF|SPI_CKE_OFF|SLAVE_ENABLE_OFF|CLK_POL_ACTIVE_LOW|MASTER_ENABLE_ON|SEC_PRESCAL_4_1|PRI_PRESCAL_16_1; unsigned int config2 = SPI_ENABLE|SPI_IDLE_CON; OpenSPI1(config1, config2); // DBPRINTF("\r\nSPI INIT FINISHED\n"); }
static void initSPI_OSD(uint16_t priPre, uint16_t secPre) { uint16_t SPICON1Value = 0, SPICON2Value = 0; uint16_t SPISTATValue = 0; #if defined(__dsPIC33E__) SPICON1Value = ENABLE_SDO_PIN & SPI_MODE16_OFF & ENABLE_SCK_PIN & SPI_SMP_OFF & SPI_CKE_ON & SLAVE_ENABLE_OFF & CLK_POL_ACTIVE_HIGH & MASTER_ENABLE_ON & secPre & priPre; // SPICON2Value = FRAME_ENABLE_OFF & FRAME_SYNC_OUTPUT; // & FIFO_BUFFER_DISABLE; // SPISTATValue = SPI_ENABLE & SPI_IDLE_CON & SPI_RX_OVFLOW_CLR; // & BUF_INT_SEL_5; SPISTATValue = SPI_ENABLE; #else SPICON1Value = ENABLE_SDO_PIN & SPI_MODE16_OFF & ENABLE_SCK_PIN & SPI_SMP_ON & SPI_CKE_OFF & SLAVE_ENABLE_OFF & CLK_POL_ACTIVE_LOW & MASTER_ENABLE_ON & secPre & priPre; SPICON2Value = FRAME_ENABLE_OFF & FRAME_SYNC_OUTPUT; SPISTATValue = SPI_ENABLE & SPI_IDLE_CON & SPI_RX_OVFLOW_CLR; #endif #if (BOARD_TYPE == UDB4_BOARD || BOARD_TYPE == UDB5_BOARD) CloseSPI1(); // ConfigIntSPI1(SPI_INT_DIS & SPI_INT_PRI_6); OpenSPI1(SPICON1Value, SPICON2Value, SPISTATValue); SPI1STATbits.SPIROV = 0; // clear SPI receive overflow _SPI1IF = 0; // clear any pending interrupts // _SPI1IP = INT_PRI_SPI1; // set interrupt priority // _SPI1IE = 1; // turn on SPI interrupts #elif (BOARD_TYPE == AUAV3_BOARD) CloseSPI3(); // ConfigIntSPI3(SPI_INT_DIS & SPI_INT_PRI_6); OpenSPI3(SPICON1Value, SPICON2Value, SPISTATValue); SPI3STATbits.SPIROV = 0; // clear SPI receive overflow _SPI3IF = 0; // clear any pending interrupts // _SPI3IP = INT_PRI_SPI3; // set interrupt priority // _SPI3IE = 1; // turn on SPI interrupts // SPIxSTAT 801C, SPIxCON1 0079, SPIxCON2 0000 #endif }
//<editor-fold defaultstate="collapsed" desc="SPI Setup"> void Setup_SPI1() { OpenSPI1( ( ENABLE_SCK_PIN &// // FIFO_BUFFER_DISABLE & ENABLE_SDO_PIN &// SPI_MODE16_OFF &// SPI_SMP_ON &// SPI_CKE_ON &// SLAVE_ENABLE_OFF &// CLK_POL_ACTIVE_HIGH &// MASTER_ENABLE_ON &// SEC_PRESCAL_4_1 &// PRI_PRESCAL_4_1// ) , ( FRAME_ENABLE_OFF & //NOT USED FRAME_SYNC_OUTPUT & //NOT USED FRAME_POL_ACTIVE_HIGH & //NOT USED FRAME_SYNC_EDGE_PRECEDE //NOT USED ) , ( SPI_ENABLE &// SPI_IDLE_CON &// SPI_RX_OVFLOW_CLR // ) ); ConfigIntSPI1(SPI_INT_EN & SPI_INT_PRI_5); TRIS_SCLK; TRIS_MOSI; TRIS_MISO; MAP_SCLK; MAP_MOSI; MAP_MISO; printf("SPI1 Setup\r\n"); return; }
void Setup_SPI1() { OpenSPI1( ( ENABLE_SCK_PIN &// // FIFO_BUFFER_DISABLE & ENABLE_SDO_PIN &// SPI_MODE16_OFF &// SPI_SMP_ON &// SPI_CKE_ON &// SLAVE_ENABLE_OFF &// CLK_POL_ACTIVE_HIGH &// MASTER_ENABLE_ON &// SEC_PRESCAL_4_1 &// PRI_PRESCAL_4_1// ) , ( FRAME_ENABLE_OFF & //NOT USED FRAME_SYNC_OUTPUT & //NOT USED FRAME_POL_ACTIVE_HIGH & //NOT USED FRAME_SYNC_EDGE_PRECEDE //NOT USED ) , ( SPI_ENABLE &// SPI_IDLE_CON &// SPI_RX_OVFLOW_CLR // ) ); ConfigIntSPI1(SPI_INT_EN & SPI_INT_PRI_5); PPSOutput(OUT_FN_PPS_SCK1, OUT_PIN_PPS_RP8);//CLK PPSOutput(OUT_FN_PPS_SDO1, OUT_PIN_PPS_RP7);//MOSI PPSInput(IN_FN_PPS_SDI1, IN_PIN_PPS_RP6); //MISO TRISBbits.TRISB8 = 0;//Clk TRISBbits.TRISB7 = 0;//MOSI TRISBbits.TRISB6 = 1;//MISO printf("SPI1 Setup\r\n"); return; }
int32_t main(void) { #ifndef PIC32_STARTER_KIT /*The JTAG is on by default on POR. A PIC32 Starter Kit uses the JTAG, but for other debug tool use, like ICD 3 and Real ICE, the JTAG should be off to free up the JTAG I/O */ DDPCONbits.JTAGEN = 0; #endif /*Refer to the C32 peripheral library documentation for more information on the SYTEMConfig function. This function sets the PB divider, the Flash Wait States, and the DRM /wait states to the optimum value. It also enables the cacheability for the K0 segment. It could has side effects of possibly alter the pre-fetch buffer and cache. It sets the RAM wait states to 0. Other than the SYS_FREQ, this takes these parameters. The top 3 may be '|'ed together: SYS_CFG_WAIT_STATES (configures flash wait states from system clock) SYS_CFG_PB_BUS (configures the PB bus from the system clock) SYS_CFG_PCACHE (configures the pCache if used) SYS_CFG_ALL (configures the flash wait states, PB bus, and pCache)*/ /* TODO Add user clock/system configuration code if appropriate. */ SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL); /* Initialize I/O and Peripherals for application */ InitApp(); /*Configure Multivector Interrupt Mode. Using Single Vector Mode is expensive from a timing perspective, so most applications should probably not use a Single Vector Mode*/ // Configure UART2 RX Interrupt INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0); // configure for multi-vectored mode INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); // enable interrupts INTEnableInterrupts(); /* TODO <INSERT USER APPLICATION CODE HERE> */ //Open UART2 OpenUART2(UART_EN, UART_BRGH_FOUR|UART_RX_ENABLE | UART_TX_ENABLE, 21); //Open SPI 1 channel PORTBbits.RB11 = 1; OpenSPI1( SPI_MODE8_ON | MASTER_ENABLE_ON | SEC_PRESCAL_1_1 | PRI_PRESCAL_1_1 | FRAME_ENABLE_OFF | CLK_POL_ACTIVE_HIGH | ENABLE_SDO_PIN , SPI_ENABLE ); SPI1BRG=39; initRadio(); setTXAddress("UNIT2"); setRXAddress(0,"UNIT1"); char temp; char text[6]; text[0]='H'; text[1]='e'; text[2]='l'; text[3]='l'; text[4]='o'; text[5]='!'; while(1) { setTransmitter(); PORTBbits.RB11 = 0; DelayMs(20); transmitData(&text[0],6); printf("Hello world! \r\n"); PORTBbits.RB11 = 1; DelayMs(20); } }
int main(void) { DBINIT(); DBPRINTF("MAIN.... \n"); unsigned int temp; int POWER; //bool RELAY; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //STEP 1. Configure cache, wait states and peripheral bus clock SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // STEP 2. configure the port registers mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_9); mPORTESetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // STEP 3. initialize the port pin states = outputs low mPORTDClearBits(BIT_0 | BIT_1 | BIT_2); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // STEP 4. enable change notice, enable discrete pins and weak pullups mCNOpen(CONFIG, PINS, PULLUPS); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // STEP 5. read port(s) to clear mismatch on change notice pins temp = mPORTDRead(); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // STEP 6. clear change notice interrupt flag ConfigIntCN(INTERRUPT); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // STEP 7. enable multi-vector interrupts INTEnableSystemMultiVectoredInt(); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // STEP 8. configure SPI port and MCP3909 OpenSPI1(FRAME_ENABLE_ON | ENABLE_SDO_PIN | SPI_MODE32_ON | SPI_CKE_ON | SLAVE_ENABLE_OFF | CLK_POL_ACTIVE_LOW | MASTER_ENABLE_ON , SPI_ENABLE | SPI_FRZ_CONTINUE | SPI_IDLE_STOP | SPI_RX_OVFLOW_CLR); CONFIG_3909(); DBPRINTF("CONFIGURED.... \n"); while(1) { // Toggle LED's to signify code is looping mPORTDToggleBits(BIT_0); // toggle LED1 (same as LATDINV = 0x0002) SAMPLE(); POWER = MEASURE_POWER(); DBPRINTF("power measured! \n"); DelayMs(1000); /************************ *ETHERNET COMMUNICATIONS* ************************/ }; }
void main(void) { char packet[] = {'T', 'e', 's', 't'}; unsigned char i = 0; unsigned char packet_size = sizeof (packet) / sizeof (packet[0]); unsigned char cArray[10] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'}; // temporary local array unsigned char writtenArray[10]; // local variables for all test cases and their initialisation unsigned char chData; unsigned char *pReadArray; unsigned char *pWriteArray; unsigned char j = 0; unsigned char i = 0; pReadArray = pWriteArray = writtenArray; /* Configure the oscillator for the device */ ConfigureOscillator(); /* Initialize I/O and Peripherals for application */ InitApp(); // LCD test TRISAbits.RA2 = 0; // our chip select pin needs to be an output so that we can toggle it CS = 1; // set CS pin to high, meaning we are sending any information to the MCP23S17 chip // configure SPI: the MCP23S17 chip's max frequency is 10MHz, let's use 10MHz/64 (Note FOSC=10Mhz, our external oscillator) OpenSPI1(SPI_FOSC_64, MODE_10, SMPEND); // frequency, master-slave mode, sampling type // set LCD pins DB0-DB7 as outputs setIODIR(IODIRB_ADDRESS, 0x00); // set RS and E LCD pins as outputs setIODIR(IODIRA_ADDRESS, 0x00); // RS=0, E=0 setGPIO(IODIRA_ADDRESS, 0x00); // Function set: 8 bit, 2 lines, 5x8 lcdCommand(0b00111111); // Cursor or Display Shift lcdCommand(0b00001111); // clear display lcdCommand(0b00000001); // entry mode lcdCommand(0b00000110); // send characters lcdWriteString((unsigned char *) "Waiting..."); // using the string function lcdGoTo(0x40); // go to line two /*lcdChar('S'); // using the single character function lcdChar('P'); lcdChar('I'); lcdChar(' '); lcdChar('L'); lcdChar('i'); lcdChar('b'); lcdChar('r'); lcdChar('a'); lcdChar('r'); lcdChar('y'); */ /////////////////////////////////////////////////// /* TODO <INSERT USER APPLICATION CODE HERE> */ /* while(1) { i = 0; do { UARTIntPutChar(packet[i++]); } while (i < packet_size); while (!vUARTIntStatus.UARTIntTxBufferEmpty); Delay10KTCYx(1000); } */ TRISD = 0; PORTD = 0; while (1) { /* for (j = 0; j < 100; j++) { i = 0; do { if (vUARTIntStatus.UARTIntTxBufferEmpty) UARTIntPutChar(cArray[i++]); } while (i < 10); } */ if (!(vUARTIntStatus.UARTIntRxError) && !(vUARTIntStatus.UARTIntRxOverFlow) && !(vUARTIntStatus.UARTIntRxBufferEmpty)) { if (UARTIntGetChar(&chData)) { PORTD = chData; lcdChar(chData); } } } }
void spi1Init(unsigned int prescale) { OpenSPI1(0x0520 | prescale, 0x0000, 0x8000); }