/** * Initialize the I2C */ void InitI2C() { //I2C Pin Config mPORTBSetPinsDigitalOut(I2C_SCL_Pin | I2C_SDA_Pin); I2CEnable(I2C1, FALSE); //Soft reset I2C Bus by pulsing the clock line 10 times mPORTBSetBits(I2C_SCL_Pin | I2C_SDA_Pin); unsigned int i; unsigned int wait; for (i = 0; i < 20; i++) { for (wait = 0; wait < 20; wait++); mPORTBToggleBits(I2C_SCL_Pin); } mPORTBSetBits(I2C_SCL_Pin | I2C_SDA_Pin); // Configure Various I2C Options //!!!!! - Slew rate control off(High speed mode enabled), If enabled, RA0 and RA1 fail to work, see silicon errata (Microchip Hardware Bugs) I2CConfigure(I2C1, I2C_ENABLE_SLAVE_CLOCK_STRETCHING | I2C_ENABLE_HIGH_SPEED); // Set the I2C baud rate int I2C_actualClock = I2CSetFrequency(I2C1, SYS_FREQ, I2C_Clock); UART_SendString("I2C Clock: "); UART_SendInt(I2C_actualClock); UART_SendString(" Hz\n\r"); // Enable the I2C bus I2CEnable(I2C1, TRUE); while (!I2CTransmitterIsReady(I2C1)); // configure the interrupt priority for the I2C peripheral //INTSetVectorPriority(INT_I2C_1_VECTOR,INT_PRIORITY_LEVEL_3); //INTClearFlag(INT_I2C1); //INTEnable(INT_I2C1,INT_ENABLED); }
int main(void) { // === config the uart, DMA, SPI =========== PT_setup(); // == SPI == //enable SPI at 10 MHz clock to meet digital potentiometer specifications SpiChnOpen(spiChn, SPI_OPEN_ON | SPI_OPEN_MODE16 | SPI_OPEN_MSTEN | SPI_OPEN_CKE_REV , spiClkDiv); // === setup system wide interrupts ==================== INTEnableSystemMultiVectoredInt(); // === set up i/o port pin =============================== //Port B bits, 3,7,8, and 9 are used to select digital output //Port B bit 4 is used as chip select for treble digital potentiometer //Port B bit 13 is used as a select signal for output effect selection multiplexer //Additional functionality would use the TFT to display which output was being //selected mPORTBSetPinsDigitalOut(BIT_4 | BIT_3|BIT_7 | BIT_8 | BIT_9 |BIT_13); //Set port as output //Port A Bits 0,2,and 3 are used to configure digital potentiometers (chip selects). Port A bit 4 is used //for multiplexing whether to have input from Digital effector chip, distortion, //or pure tonestack sound mPORTASetPinsDigitalOut(BIT_0 | BIT_2 | BIT_3 | BIT_4); // === now the threads =================================== // init the threads PT_INIT(&pt_cmd); PT_INIT(&pt_time); //==Digipot spi stuff // SCK2 is pin 26 // SDO1 (MOSI) is in PPS output group 1, could be connected to RB5 which is pin 14 PPSOutput(2, RPB5, SDO1); // control CS for DAC //mPORTBSetPinsDigitalOut(BIT_4); //CS mPORTBSetBits(BIT_4 | BIT_6); //=== mPORTASetBits(BIT_0 | BIT_2 | BIT_3 | BIT_4); //CS pins active high mPORTAClearBits(BIT_4); mPORTBClearBits(BIT_13); mPORTBSetBits(BIT_13); // schedule the threads while(1) { //cmd used as command center for all effects PT_SCHEDULE(protothread_cmd(&pt_cmd)); } } // main
void MyMIWI_Init(void) { // Configure Pins for MRF24J40MB mPORTESetBits(RST_MIWI); mPORTESetPinsDigitalOut(RST_MIWI); mPORTBSetBits(MIWI_WAKE); mPORTBSetPinsDigitalOut(MIWI_WAKE); // Configure the INT3 controller for MIWI // Set RD10/INT3 as input mPORTDSetPinsDigitalIn(BIT_10); // Clear corresponding bits in INTCON for falling edge trigger INTCONCLR = _INTCON_INT3EP_MASK; // Set up interrupt prioirty and sub-priority INTSetVectorPriority(INT_EXTERNAL_3_VECTOR, My_INT_EXTERNAL_3_PRIORITY); INTSetVectorSubPriority(INT_EXTERNAL_3_VECTOR, My_INT_EXTERNAL_3_SUB_PRIORITY); // Clear the interrupt flags INTClearFlag(INT_INT3); // Enable INT3 INTEnable(INT_INT3, INT_ENABLED); // WARNING : Change in file MRF24J40.c in Microchip Application Library // the line : void __ISR(_EXTERNAL_1_VECTOR, ipl4) _INT1Interrupt(void) // by : void __ISR(_EXTERNAL_3_VECTOR, ipl4) _INT3Interrupt(void) }
void MyWIFI_Init(void) { // Configure Pins for MRF24J40MB mPORTESetBits(RST_WIFI); mPORTESetPinsDigitalOut(RST_WIFI); mPORTBSetBits(WIFI_HIB); mPORTBSetPinsDigitalOut(WIFI_HIB); mPORTBClearBits(WIFI_WP); // Has to be pulled low mPORTBSetPinsDigitalOut(WIFI_WP); /* // Configure the INT4 controller for WIFI // Set RD11/INT4 as input mPORTDSetPinsDigitalIn(BIT_11); // Clear corresponding bits in INTCON for falling edge trigger INTCONCLR = _INTCON_INT4EP_MASK; // Set up interrupt prioirty and sub-priority INTSetVectorPriority(INT_EXTERNAL_4_VECTOR, My_INT_EXTERNAL_4_PRIORITY); INTSetVectorSubPriority(INT_EXTERNAL_4_VECTOR, My_INT_EXTERNAL_4_SUB_PRIORITY); // Clear the interrupt flags INTClearFlag(INT_INT4); // Enable INT4 INTEnable(INT_INT4, INT_ENABLED); */ }
void MyFlash_Init(void) { unsigned int intStatus; unsigned int theStatus; char theStr[64]; // Configure Flash Pins (Port B) and set to 1 (default value) mPORTBSetBits(FLASH_WP | FLASH_HOLD); mPORTBSetPinsDigitalOut(FLASH_WP | FLASH_HOLD); // Set the Block Protection bits (BP3-0) to 0 (Disable all protections) FLASH_ENABLE_ACCESS; MySPI_PutC(FLASH_EN_WR_STATUS); FLASH_DISABLE_ACCESS; FLASH_ENABLE_ACCESS; MySPI_PutC(FLASH_WRITE_STATUS); MySPI_PutC(0x00); FLASH_DISABLE_ACCESS; /* // Read the Status Register FLASH_ENABLE_ACCESS; MySPI_PutC(FLASH_READ_STATUS); theStatus = MySPI_GetC(); FLASH_DISABLE_ACCESS; sprintf(theStr, "Flash Status 0: 0x%x\n>", theStatus); MyConsole_SendMsg(theStr); */ }
void InitBluetooth(void) { #if DESIRED_BAUDRATE == 9600 mPORTBSetPinsDigitalOut(BIT_9); mPORTBSetBits(BIT_9);// BT high level, the reset polarity is active low mPORTASetPinsDigitalOut(BIT_3); mPORTASetBits(BIT_3);// Set Baud rate (high = force 9,600, low = 115 K or firmware setting) mPORTASetPinsDigitalOut(BIT_2); mPORTASetBits(BIT_2);// Set BT master (high = auto-master mode) #elif DESIRED_BAUDRATE == 115200 mPORTBSetPinsDigitalOut(BIT_9); mPORTBSetBits(BIT_9);// BT high level, the reset polarity is active low mPORTASetPinsDigitalOut(BIT_3); mPORTAClearBits(BIT_3);// Set Baud rate (high = force 9,600, low = 115 K or firmware setting) mPORTASetPinsDigitalOut(BIT_2); mPORTASetBits(BIT_2);// Set BT master (high = auto-master mode) #elif DESIRED_BAUDRATE ==921600 //nothing #endif UARTConfigure(UART_MODULE_ID2, UART_ENABLE_PINS_TX_RX_ONLY|UART_ENABLE_PINS_CTS_RTS); UARTConfigure(UART_MODULE_ID2, UART_ENABLE_HIGH_SPEED); UARTSetFifoMode(UART_MODULE_ID2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART_MODULE_ID2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART_MODULE_ID2, GetPeripheralClock(), DESIRED_BAUDRATE); UARTEnable(UART_MODULE_ID2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // Configure UART RX Interrupt INTEnable(INT_SOURCE_UART_RX(UART_MODULE_ID2), INT_ENABLED); INTSetVectorPriority(INT_VECTOR_UART(UART_MODULE_ID2), INT_PRIORITY_LEVEL_2); INTSetVectorSubPriority(INT_VECTOR_UART(UART_MODULE_ID2), INT_SUB_PRIORITY_LEVEL_1); // Enable multi-vector interrupts //WriteStringXbee("*** UART Bluetooth demarre ***\r\n"); }
int main(void) { // === config the uart, DMA, vref, timer5 ISR ============= PT_setup(); // === setup system wide interrupts ==================== INTEnableSystemMultiVectoredInt(); // === set up i/o port pin =============== mPORTASetBits(BIT_0 | BIT_1 ); //Clear bits to ensure light is off. mPORTASetPinsDigitalOut(BIT_0 | BIT_1 ); //Set port as output mPORTBSetBits(BIT_0 ); //Clear bits to ensure light is off. mPORTBSetPinsDigitalOut(BIT_0 ); //Set port as output // === now the threads ==================== // init the thread control semaphores PT_SEM_INIT(&control_t1, 0); // start blocked PT_SEM_INIT(&control_t2, 1); // start unblocked PT_SEM_INIT(&send_sem, 1); // start with ready to send // init the threads PT_INIT(&pt1); PT_INIT(&pt2); PT_INIT(&pt3); PT_INIT(&pt4); PT_INIT(&pt5); // init the optional rate scheduler PT_RATE_INIT(); // schedule the threads while(1) { PT_RATE_LOOP(); // not necessary if you use PT_SCHEDULE PT_RATE_SCHEDULE(protothread1(&pt1), t1_rate); if (run_t4) PT_RATE_SCHEDULE(protothread4(&pt4), t4_rate); //run always PT_RATE_SCHEDULE(protothread2(&pt2), t1_rate); if (cmd[0] != 'k') PT_RATE_SCHEDULE(protothread3(&pt3),t3_rate); PT_SCHEDULE(protothread5(&pt5)); /* // alternate scheme PT_SCHEDULE(protothread1(&pt1)); if (run_t4) PT_SCHEDULE(protothread4(&pt4)); PT_SCHEDULE(protothread2(&pt2)); if (run_t4) PT_SCHEDULE(protothread4(&pt4)); if (cmd[0] != 'k') PT_SCHEDULE(protothread3(&pt3)); */ } } // main
/* TIMER 2 Interrupt Handler - configured to 50us periods */ void __ISR(_TIMER_2_VECTOR, ipl3) Timer2Handler(void) { // clear the interrupt flag mT2ClearIntFlag(); counterTrigger--; if(counterTrigger==5){ mPORTBSetBits(BIT_8 | BIT_9| BIT_10 | BIT_11); //Sends trigger signal to the four sensors } if(counterTrigger == 0){ mPORTBClearBits(BIT_8 | BIT_9| BIT_10 | BIT_11); //Shut down trigger signal } delayFront++; delayBack++; delayRight++; delayLeft++; }
void TURN_ON_LED(short lednumber) { switch(lednumber) { case 0: {} case 1: {} case 2: { mPORTDSetBits(1 << lednumber); break; } case 3: {} case 4: { mPORTBSetBits(1 << (lednumber+1)); } default: { break; } } }
// Initialization void SSD1351::begin(void) { mPORTBSetBits(BIT_15); // Parallel Master Port (PMP) Initialization /* Serial.println (" * Configuring PMP"); int PMP_CONTROL = PMP_ON | PMP_READ_WRITE_EN | PMP_CS2_CS1_EN | PMP_CS1_POL_LO | PMP_READ_POL_HI | PMP_WRITE_POL_HI; int PMP_MODE = PMP_IRQ_OFF | PMP_AUTO_ADDR_OFF | PMP_DATA_BUS_8 | PMP_MODE_MASTER1 | PMP_WAIT_BEG_1 | PMP_WAIT_MID_2 | PMP_WAIT_END_1; int PMP_ADDRCFG = PMP_PEN_OFF; // no address bits used int PMP_INTERRUPT = PMP_INT_OFF; // no interrupts used mPMPOpen(PMP_CONTROL, PMP_MODE, PMP_ADDRCFG, PMP_INTERRUPT); */ // Bit-bang initialization PORTSetPinsDigitalOut(IOPORT_E, OLED_DATA); PORTSetPinsDigitalOut(IOPORT_D, OLED_RW); PORTSetPinsDigitalOut(IOPORT_D, OLED_EN); PORTSetPinsDigitalOut(IOPORT_D, OLED_CS); // Set pin tristates pinMode(SSD1351_DC, OUTPUT); pinMode(SSD1351_RESET, OUTPUT); pinMode(SSD1351_BOOST_EN, OUTPUT); // Disable OLED 13V boost supply Serial.println (" * Disabling Boost Regulator"); disableBoostRegulator(); // Reset display Serial.println (" * Resetting display"); digitalWrite(SSD1351_RESET, 1); delay(500); digitalWrite(SSD1351_RESET, 0); delay(500); digitalWrite(SSD1351_RESET, 1); delay(500); // Initialization Sequence Serial.println (" * Beginning initialization sequence"); writeCommand(SSD1351_CMD_COMMANDLOCK); // set command lock writeData(0x12); writeCommand(SSD1351_CMD_COMMANDLOCK); // set command lock writeData(0xB1); writeCommand(SSD1351_CMD_DISPLAYOFF); // 0xAE writeCommand(SSD1351_CMD_CLOCKDIV); // 0xB3 writeCommand(0xF1); // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16) writeCommand(SSD1351_CMD_MUXRATIO); writeData(127); writeCommand(SSD1351_CMD_SETREMAP); writeData(0x74); writeCommand(SSD1351_CMD_SETCOLUMN); writeData(0x00); writeData(0x7F); writeCommand(SSD1351_CMD_SETROW); writeData(0x00); writeData(0x7F); writeCommand(SSD1351_CMD_STARTLINE); // 0xA1 if (SSD1351_HEIGHT == 96) { writeData(96); } else { writeData(0); } writeCommand(SSD1351_CMD_DISPLAYOFFSET); // 0xA2 writeData(0x0); writeCommand(SSD1351_CMD_SETGPIO); writeData(0x00); writeCommand(SSD1351_CMD_FUNCTIONSELECT); writeData(0x01); // internal (diode drop) //writeData(0x01); // external bias // writeCommand(SSSD1351_CMD_SETPHASELENGTH); // writeData(0x32); writeCommand(SSD1351_CMD_PRECHARGE); // 0xB1 writeCommand(0x32); writeCommand(SSD1351_CMD_VCOMH); // 0xBE writeCommand(0x05); writeCommand(SSD1351_CMD_NORMALDISPLAY); // 0xA6 writeCommand(SSD1351_CMD_CONTRASTABC); writeData(0xC8); writeData(0x80); writeData(0xC8); writeCommand(SSD1351_CMD_CONTRASTMASTER); writeData(0x0F); writeCommand(SSD1351_CMD_SETVSL ); writeData(0xA0); writeData(0xB5); writeData(0x55); writeCommand(SSD1351_CMD_PRECHARGE2); writeData(0x01); // Clear framebuffer for (int i=0; i<(width*height); i++) { framebuffer[i] = 0; } // Update screen updateScreen(); // Enable OLED high voltage supply Serial.println (" * Enabling boost regulator"); enableBoostRegulator(); Serial.println (" * Turning display on"); writeCommand(SSD1351_CMD_DISPLAYON); //--turn on oled panel }
void __ISR(_I2C_1_VECTOR, ipl3) _SlaveI2CHandler(void) { unsigned char temp; static unsigned int dIndex; // check for MASTER and Bus events and respond accordingly if (IFS1bits.I2C1MIF) { mI2C1MClearIntFlag(); return; } if (IFS1bits.I2C1BIF) {//bus collision, reset I2C state machine I2Cstate = 0; mI2C1BClearIntFlag(); return; } // handle the incoming message if ((I2C1STATbits.R_W == 0) && (I2C1STATbits.D_A == 0)) { // reset any state variables needed by a message sequence // perform a dummy read temp = SlaveReadI2C1(); I2C1CONbits.SCLREL = 1; // release the clock I2Cstate = 0; } else if ((I2C1STATbits.R_W == 0) && (I2C1STATbits.D_A == 1)) {//data received, input to slave WDTCount = 0; WriteTimer1(0); // writing data to our module I2CDataIn = SlaveReadI2C1(); I2C1CONbits.SCLREL = 1; // release clock stretch bit if (I2Cstate == 0 && I2CDataIn != ADDR_CLR_I2C_STATE) { I2Cstate = 1; I2C_request = I2CDataIn; } else if (I2Cstate == 1) { switch (I2C_request) { case ADDR_M3_PWM: M3_Dir = I2CDataIn >> 7 & 0x01;//take top bit if (((Status2 & M3_CUR) && (config1 & CUR_SENSE_EN)) || (config1 & MOTOR_HALT)) { M3_PWM = 0; } else { if (I2CDataIn & 0x80) { mPORTBClearBits(BIT_13); M3_PWM = (((int) ((~I2CDataIn) + 1)) << 4); } else { mPORTBSetBits(BIT_13); M3_PWM = ((int) I2CDataIn) << 4; } } SetDCOC4PWM(M3_PWM); break; case ADDR_M4_PWM: M4_Dir = I2CDataIn >> 7 & 0x01;//take top bit if (((Status2 & M4_CUR) && (config1 & CUR_SENSE_EN)) || (config1 & MOTOR_HALT)) { M4_PWM = 0; } else { if (I2CDataIn & 0x80) { mPORTBClearBits(BIT_14); M4_PWM = (((int) ((~I2CDataIn) + 1)) << 4); } else { mPORTBSetBits(BIT_14); M4_PWM = ((int) I2CDataIn) << 4; } } SetDCOC5PWM(M4_PWM); break; case ADDR_M5_PWM: M5_Dir = I2CDataIn >> 7 & 0x01;//take top bit if (((Status2 & M5_CUR) && (config1 & CUR_SENSE_EN)) || (config1 & MOTOR_HALT)) { M5_PWM = 0; } else { if (I2CDataIn & 0x80) { mPORTBClearBits(BIT_15); M5_PWM = (((int) ((~I2CDataIn) + 1)) << 4); } else { mPORTBSetBits(BIT_15); M5_PWM = ((int) I2CDataIn) << 4; } } SetDCOC3PWM(M5_PWM); break; case ADDR_M6_PWM: M6_Dir = I2CDataIn >> 7 & 0x01;//take top bit if (((Status2 & M6_CUR) && (config1 & CUR_SENSE_EN)) || (config1 & MOTOR_HALT)) { M6_PWM = 0; } else { if (I2CDataIn & 0x80) { mPORTBClearBits(BIT_11); M6_PWM = (((int) ((~I2CDataIn) + 1)) << 4); } else { mPORTBSetBits(BIT_11); M6_PWM = ((int) I2CDataIn) << 4; } } SetDCOC2PWM(M6_PWM); break; case ADDR_M7_PWM: M7_Dir = I2CDataIn >> 7 & 0x01;//take top bit if (((Status2 & M7_CUR) && (config1 & CUR_SENSE_EN)) || (config1 & MOTOR_HALT)) { M7_PWM = 0; } else { if (I2CDataIn & 0x80) { mPORTBClearBits(BIT_7); M7_PWM = (((int) ((~I2CDataIn) + 1)) << 4); } else { mPORTBSetBits(BIT_7); M7_PWM = ((int) I2CDataIn) << 4; } } SetDCOC1PWM(M7_PWM); break; case ADDR_Config1: config1 = I2CDataIn; if (config1 & MOTOR_HALT) { SetDCOC1PWM(0); SetDCOC2PWM(0); SetDCOC3PWM(0); SetDCOC4PWM(0); SetDCOC5PWM(0); } else { SetDCOC1PWM(M7_PWM); SetDCOC2PWM(M6_PWM); SetDCOC3PWM(M5_PWM); SetDCOC4PWM(M3_PWM); SetDCOC5PWM(M4_PWM); } break; default: break; } I2Cstate = 0; } } else if ((I2C1STATbits.R_W == 1) && (I2C1STATbits.D_A == 0)) {
int main(void) { int a = 0; unsigned char data_mix = 0, data_mix_old = 0; SDEV_TYPE1 S1 = {0}, *S1_p = &S1; char comm_buffer[MAXSTRLEN]; int update_rate = 4; int eresult = 0, records = 0, file_errors = 0; // Init remote device data S1.obits.out_byte = 0xff; // Enable multi-vectored interrupts INTEnableSystemMultiVectoredInt(); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //STEP 1. Configure cache, wait states and peripheral bus clock // Configure the device for maximum performance but do not change the PBDIV // Given the options, this function will change the flash wait states, RAM // wait state and enable prefetch cache but will not change the PBDIV. // The PBDIV value is already set via the pragma FPBDIV option above.. SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); srand(ReadCoreTimer()); // Seed the pseudo random generator // SCLK1 pin 25 - SD pin 5 PORTSetPinsDigitalOut(IOPORT_B, BIT_14); // Clock output // SCLK2 pin 26 - PIC pin 18 PORTSetPinsDigitalOut(IOPORT_B, BIT_15); // Clock output PPSInput(2, SDI1, RPB8); //Assign SDI1 to pin 17 - SD pin 7 PORTSetPinsDigitalIn(IOPORT_B, BIT_8); // Input for SDI1 PPSInput(3, SDI2, RPB13); //Assign SDI2 to pin 24 PORTSetPinsDigitalIn(IOPORT_B, BIT_13); // Input for SDI2 PPSOutput(2, RPB11, SDO1); // Set 22 pin as output for SDO1 - SD pin 2 PPSOutput(2, RPB5, SDO2); // Set 14 pin as output for SDO2 PORTSetPinsDigitalOut(IOPORT_B, BIT_2); // CS line pin 6 - SD pin 1 mPORTBSetBits(BIT_2); // deselect SDCARD PORTSetPinsDigitalOut(IOPORT_B, BIT_3); // select pin enable for SPI slaves bit 2 to 74hc138 mPORTBSetBits(BIT_3); // deselect Slave1 bit2 PORTSetPinsDigitalOut(IOPORT_A, BIT_3); // spare digital output mPORTASetBits(BIT_3); // //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Diag Led pins PORTSetPinsDigitalOut(IOPORT_A, BIT_0 | BIT_1); // bi-color LED PORTSetPinsDigitalOut(IOPORT_B, BIT_0 | BIT_1); // programming inputs /device select outputs address for SPI slaves bits [0..1] 74hc138 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // STEP 3. initialize the port pin states = outputs low mPORTASetBits(BIT_0 | BIT_1); mPORTBClearBits(BIT_0 | BIT_1); RTC_Init(); // Start Timer5 for background processes and 1ms soft timers Blink_Init(); // Start Timer4 background blink driver blink_led(RED_LED, LED_ON, FALSE); init_spi_ports(); ps_select(0); // select the pic18 slave1 on spi port 2, select 0 // send some text to clean the buffers SpiStringWrite("\r\n \n\r"); eresult = f_mount(&FatFs, "0:", 1); if (eresult) { sprintf(comm_buffer, "\r\n Mount error %i ", eresult); SpiStringWrite(comm_buffer); /* Register work area to the logical drive 1 */ blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } else { Show_MMC_Info(); blink_led(RED_LED, LED_OFF, FALSE); blink_led(GREEN_LED, LED_ON, TRUE); } /* Create destination file on the drive 1 */ eresult = f_open(&File[0], "0:logfile.txt", FA_CREATE_ALWAYS | FA_WRITE); while (1) { // loop and move data V.Timer1 = update_rate; while (V.Timer1); /* loop back testing */ data_mix_old = data_mix; // save the old data data_mix = rand(); // make new data if (S1_p->char_ready || !S1_p->ibits.in_bits.eject) { S1_p->rec_data = S1_p->rec_tmp; // led1_green(); } else { // led1_off(); } S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (S1_p->ibits.in_bits.card_detect) { S1_p->obits.out_bits.eject_led = OFF; SD_NOTRDY = STA_NOINIT; } else { S1_p->obits.out_bits.eject_led = ~S1_p->obits.out_bits.eject_led; } for (S1_p->chan_no = 0; S1_p->chan_no < 4; S1_p->chan_no++) { S1_p->adc_data[S1_p->chan_no] = SpiADCRead(S1_p->chan_no); } a = 0; if (((records % 100)) == 0 && !eresult) { if (S1_p->char_ready) { snprintf(comm_buffer, 64, "\r\n %x , %i, %x , %i , %i , %i , %i ", S1_p->rec_data, records, S1_p->ibits.in_byte, S1_p->adc_data[0], S1_p->adc_data[1], S1_p->adc_data[2], S1_p->adc_data[3]); } else { snprintf(comm_buffer, 64, "\r\n %x , %i , %i , %i , %i , %i ", S1_p->ibits.in_byte, records, S1_p->adc_data[0], S1_p->adc_data[1], S1_p->adc_data[2], S1_p->adc_data[3]); } S1_p->rec_tmp = SpiStringWrite(comm_buffer); } if (SpiSerialReadOk() || !S1_p->ibits.in_bits.eject) { S1_p->char_ready = TRUE; if ((S1_p->rec_tmp == 'f') || !S1_p->ibits.in_bits.eject) { if (S1_p->ibits.in_bits.card_detect) { // no disk blink_led(0, LED_ON, FALSE); S1_p->obits.out_bits.eject_led = OFF; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); SpiStringWrite("\r\n Insert Disk! y/n "); while (!SpiSerialReadReady() && S1_p->ibits.in_bits.card_detect) S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); V.Timer1 = update_rate; while (V.Timer1); if (!S1_p->ibits.in_bits.card_detect) { SpiStringWrite("/\r\n Mounting Disk "); S1_p->obits.out_bits.eject_led = ON; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); eresult = f_mount(&FatFs, "0:", 1); if (eresult) { sprintf(comm_buffer, "\r\n Mount error %i ", eresult); SpiStringWrite(comm_buffer); /* Register work area to the logical drive 1 */ blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } else { Show_MMC_Info(); blink_led(RED_LED, LED_OFF, FALSE); blink_led(GREEN_LED, LED_ON, TRUE); } /* Create destination file on the drive 1 */ eresult = f_open(&File[0], "0:logfile.txt", FA_CREATE_ALWAYS | FA_WRITE); SpiStringWrite("\r\n Mount Complete "); S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (S1_p->ibits.in_bits.card_detect) SD_NOTRDY = STA_NOINIT; } } else { // Eject current disk blink_led(0, LED_ON, FALSE); S1_p->obits.out_bits.eject_led = OFF; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); f_close(&File[0]); SpiStringWrite("\r\n Eject Disk? y/n "); while (!SpiSerialReadReady() && !S1_p->ibits.in_bits.card_detect) S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); V.Timer1 = update_rate; while (V.Timer1); if (S1_p->ibits.in_bits.card_detect) { SpiStringWrite("\r\n Ejecting Disk "); S1_p->obits.out_bits.eject_led = ON; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (f_mount(0, "0:", 0)) SpiStringWrite("\r\n UMount error "); /* Unregister work area from the logical drive 1 */ SpiStringWrite("\r\n Ejection Complete "); eresult = 1; S1_p->ibits.in_byte = SpiPortWrite(S1_p->obits.out_byte); if (S1_p->ibits.in_bits.card_detect) SD_NOTRDY = STA_NOINIT; } } } } else { S1_p->char_ready = FALSE; } V.Timer1 = update_rate; while (V.Timer1); // only GREEN if the data sent and received match or eject button is pressed if (S1_p->ibits.in_bits.eject && (S1_p->rec_data != data_mix_old)) { // blink_led(0, LED_ON, TRUE); } else { // blink_led(0, LED_ON, FALSE); } if (!eresult) { a = f_puts(comm_buffer, &File[0]); blink_led(RED_LED, LED_OFF, FALSE); blink_led(GREEN_LED, LED_ON, TRUE); records++; } else { file_errors++; if ((file_errors % 100) == 0) { SpiStringWrite("\r\n not logged "); blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } } if (a == (-1)) { if ((file_errors % 100) == 0) { sprintf(comm_buffer, "\r\n File Write error %i ", a); SpiStringWrite(comm_buffer); blink_led(GREEN_LED, LED_OFF, FALSE); blink_led(RED_LED, LED_ON, FALSE); } } f_sync(&File[0]); } }
static PT_THREAD (protothread_cmd(struct pt *pt)) { PT_BEGIN(pt); while(1) { // send the prompt via DMA to serial sprintf(PT_send_buffer,"cmd>"); // by spawning a print thread PT_SPAWN(pt, &pt_DMA_output, PT_DMA_PutSerialBuffer(&pt_DMA_output) ); //spawn a thread to handle terminal input // the input thread waits for input // -- BUT does NOT block other threads // string is returned in "PT_term_buffer" PT_SPAWN(pt, &pt_input, PT_GetSerialBuffer(&pt_input) ); // returns when the thread dies // in this case, when <enter> is pushed // now parse the string sscanf(PT_term_buffer, "%s %d", cmd, &value); if (cmd[0]=='d' ) { //set mux to DSP chip mPORTAClearBits(BIT_4); mPORTBClearBits(BIT_13); mPORTASetBits(BIT_4); mPORTBClearBits(BIT_3 | BIT_7 | BIT_8 | BIT_9); if(((value << 6) & (0x0040))){ mPORTBSetBits(BIT_3); } //select sound effect mPORTBSetBits(value << 6); } if (cmd[0] == 's'){ //distortion //set mux to Distortion mPORTAClearBits(BIT_4); mPORTBClearBits(BIT_13); } if (cmd[0] == 'n'){ //normal: only tone stack //set mux to channel 0 mPORTAClearBits(BIT_4); mPORTBClearBits(BIT_13); mPORTBSetBits(BIT_13); } //pots if (cmd[0] == 't'){ //treble // CS low to start transaction mPORTBClearBits(BIT_4); // start transaction deliverSPICh1Datum(value); // CS high mPORTBSetBits(BIT_4); // end transaction } if (cmd[0] == 'b'){ //bass // CS low to start transaction mPORTAClearBits(BIT_0); // start transaction deliverSPICh1Datum(value); // CS high mPORTASetBits(BIT_0); // end transaction } if (cmd[0] == 'v'){ //level // CS low to start transaction mPORTAClearBits(BIT_2); // start transaction deliverSPICh1Datum(value); // CS high mPORTASetBits(BIT_2); // end transaction } if (cmd[0] == 'm'){ //mid // CS low to start transaction mPORTAClearBits(BIT_3); // start transaction deliverSPICh1Datum(value); // CS high mPORTASetBits(BIT_3); // end transaction } // never exit while } // END WHILE(1) PT_END(pt); } // thread 3
void switchMonitorOn() { mPORTBSetBits(BIT_14); }