void OledHostInit() { // unsigned int tcfg; // ALA unused /* Initialize SPI port 1. */ SPI1CON = 0; SPI1BRG = 15; //8Mhz, with 80Mhz PB clock SPI1STATbits.SPIROV = 0; SPI1CONbits.CKP = 1; SPI1CONbits.MSTEN = 1; SPI1CONbits.ON = 1; /* Make power control pins be outputs with the supplies off */ PORTSetBits(prtVddCtrl, bitVddCtrl); PORTSetBits(prtVbatCtrl, bitVbatCtrl); PORTSetPinsDigitalOut(prtVddCtrl, bitVddCtrl); //VDD power control (1=off) PORTSetPinsDigitalOut(prtVbatCtrl, bitVbatCtrl); //VBAT power control (1=off) /* Make the Data/Command select, Reset, and SPI CS pins be outputs. */ PORTSetBits(prtDataCmd, bitDataCmd); PORTSetPinsDigitalOut(prtDataCmd, bitDataCmd); //Data/Command# select PORTSetBits(prtReset, bitReset); PORTSetPinsDigitalOut(prtReset, bitReset); PORTSetBits(prtSelect, bitSelect); PORTSetPinsDigitalOut(prtSelect, bitSelect); }
void setup_ports (void) { if( _UART_JE_ == 1 ) //UART connected to JE pins { // UART JE port pins - /* JE-01 CN20/U1CTS/RD14 RD14 JE-02 U1RTS/BCLK1/CN21/RD15 RD15 JE-03 U1RX/RF2 RF2 JE-04 U1TX/RF8 RF8 */ PORTSetPinsDigitalIn (IOPORT_F, BIT_2); PORTSetPinsDigitalOut (IOPORT_F, BIT_8); } if( _UART_JH_ == 1 ) //UART connected to JH pins { // UART JH port pins - /* JH-01 U2CTS/RF12 RF12 JH-02 U2RTS/BCLK2/RF13 RF13 JH-03 PMA9/U2RX/CN17/RF4 RF4 JH-04 PMA8/U2TX/CN18/RF5 RF5 */ PORTSetPinsDigitalIn (IOPORT_F, BIT_4); PORTSetPinsDigitalOut (IOPORT_F, BIT_5); } }
void PmodHB5_INIT(UART_MODULE uartID) { PORTSetPinsDigitalOut(IOPORT_D, BIT_7); //HB5 Direction PORTSetPinsDigitalOut(IOPORT_D, BIT_1); //HB5 Enable PORTSetPinsDigitalIn(IOPORT_D, BIT_9); //HB5 Sensor A PORTSetPinsDigitalIn(IOPORT_C, BIT_1); //HB5 Sensor B hBridge.sensorAport = IOPORT_D; hBridge.sensorAportBit = BIT_9; hBridge.sensorBport = IOPORT_C; hBridge.sensorBportBit = BIT_1; hBridge.directionPort = IOPORT_D; hBridge.directionPortBit = BIT_7; hBridge.currentDirection = PMOD_HB5_DIR_CW; hBridge.newDirection = PMOD_HB5_DIR_CW; hBridge.ocChannel = 2; OpenOC2(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); OpenTimer2(T2_ON | T2_PS_1_256, SYSTEM_CLOCK/PB_DIV/PRESCALE/(TOGGLES_PER_SEC/2)); OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_256, T1_TICK); ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2); INTEnableSystemMultiVectoredInt(); UARTPutS("\r\nPmodHB5 init complete\r\n",uartID); }
void MOTtaskInit() { //make sure the motors are disabled PORTSetPinsDigitalOut(MOT_RST_IOPORT, MOT_RST_BIT); PORTSetBits(MOT_RST_IOPORT, MOT_RST_BIT); //set all PWM ports high PORTSetPinsDigitalOut(PWM_1_IOPORT, PWM_1_BIT); PORTSetBits(PWM_1_IOPORT, PWM_1_BIT); PORTSetPinsDigitalOut(PWM_2_IOPORT, PWM_2_BIT); PORTSetBits(PWM_2_IOPORT, PWM_2_BIT); PORTSetPinsDigitalOut(PWM_3_IOPORT, PWM_3_BIT); PORTSetBits(PWM_2_IOPORT, PWM_3_BIT); PORTSetPinsDigitalOut(PWM_4_IOPORT, PWM_4_BIT); PORTSetBits(PWM_2_IOPORT, PWM_4_BIT); //start the MOT main task if (xTaskCreate(MOTtask, /* The function that implements the task. */ (signed char *) "Motor Task", /* The text name assigned to the task - for debug only as it is not used by the kernel. */ MOT_TASK_STACK_SIZE, /* The size of the stack to allocate to the task. */ (void *) 0, /* The parameter passed to the task. */ MOT_TASK_PRIORITY, /* The priority assigned to the task. */ NULL) /* The task handle is not required, so NULL is passed. */ != pdPASS) { DebugPrint( "MOT Task Fail"); while(1); } }
////////////////////////////////////////////////////////////////////////////////// /// name: initialization /// params: none /// return: void /// desc: contains the configuration of hardware and clock, as well as /// the initialization of variables void intialization(void) { // button input pin PORTSetPinsDigitalIn(IOPORT_E, BUTTON); // LED output pin PORTSetPinsDigitalOut(IOPORT_E, LED_1); // motor direction pin PORTSetPinsDigitalOut(IOPORT_B, DC_MOTOR_1); // configuring timer OpenTimer2(T2_ON | T2_PS_1_1, TERMINAL_COUNT); OpenOC4(OC_ON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0x80, 0x60); // start these values at 0 _risetime = 0; _falltime = 0; _hitime = 0; // motor starts stationary _motorState = STOP; _motorGo = 0; // start arm fully retracted _faketach = 0; // set these values to 1, as they should start out not // equal to _risetime and _falltime for initial pwm read _store = 1; _ftore = 1; }
void LcdInit(void) { PORTSetPinsDigitalOut(IOPORT_E, PE_LCD_DATA); PORTSetPinsDigitalOut(IOPORT_G, PG_LCD_RS | PG_LCD_RW | PG_LCD_E); PORTClearBits(IOPORT_G, PG_LCD_E); // clear the FIFO lcdFifoWr = 0; lcdFifoRd = 0; lcdDelay = 0; // Initialize a 4 line LCD LcdFifoCmd(LCD_CMD_FSET); LcdFifoCmd(LCD_CMD_FSET); LcdFifoCmd(LCD_CMD_4LINE); LcdFifoCmd(LCD_CMD_FCLR); LcdFifoCmd(LCD_CMD_DISP_ON); LcdFifoCmd(LCD_CMD_CLEAR); LcdFifoCmd(LCD_CMD_WR_DATA); LcdNewState(LCD1_VERSION); LcdButtonWaitClear(); lcdDemoRate = 40; lcdDemoPause = 5000; } // LcdInit()
void initLEDs() { //LEDs are connected to bits 12-15 of I/O Port G PORTSetPinsDigitalOut (IOPORT_G, LD1); PORTSetPinsDigitalOut (IOPORT_G, LD2); PORTSetPinsDigitalOut (IOPORT_G, LD3); PORTSetPinsDigitalOut (IOPORT_G, LD4); }
/* * Intializes hbridges for the motors */ static void init_hbridge_data() { memset(&Motors[LEFT_MOTOR], 0, sizeof(struct hbridge_info)); memset(&Motors[RIGHT_MOTOR], 0, sizeof(struct hbridge_info)); // Enable reading value of sensor A PORTSetPinsDigitalIn(HBRIDGE_LEFT_SA_PORT, HBRIDGE_LEFT_SA_BIT); PORTSetPinsDigitalIn(HBRIDGE_LEFT_SB_PORT, HBRIDGE_LEFT_SB_BIT); PORTSetPinsDigitalIn(HBRIDGE_RIGHT_SA_PORT, HBRIDGE_RIGHT_SA_BIT); PORTSetPinsDigitalIn(HBRIDGE_RIGHT_SB_PORT, HBRIDGE_RIGHT_SB_BIT); // Enable writing direction pin PORTSetPinsDigitalOut(HBRIDGE_LEFT_DIR_PORT, HBRIDGE_LEFT_DIR_BIT); PORTSetPinsDigitalOut(HBRIDGE_RIGHT_DIR_PORT, HBRIDGE_RIGHT_DIR_BIT); // Setup left motor Motors[LEFT_MOTOR].state.hbridge_id = LEFT_MOTOR; Motors[LEFT_MOTOR].sensor_bit_pos[PMOD_HB_SENSOR_A] = HBRIDGE_LEFT_SA_BIT; Motors[LEFT_MOTOR].sensor_port[PMOD_HB_SENSOR_A] = HBRIDGE_LEFT_SA_PORT; Motors[LEFT_MOTOR].sensor_bit_pos[PMOD_HB_SENSOR_B] = HBRIDGE_LEFT_SB_BIT; Motors[LEFT_MOTOR].sensor_port[PMOD_HB_SENSOR_B] = HBRIDGE_LEFT_SB_PORT; Motors[LEFT_MOTOR].dir_port = HBRIDGE_LEFT_DIR_PORT; Motors[LEFT_MOTOR].dir_bit = HBRIDGE_LEFT_DIR_BIT; Motors[LEFT_MOTOR].ocn = HBRIDGE_LEFT_OC; Motors[LEFT_MOTOR].tmrn = HBRIDGE_LEFT_TMR; // Setup right motor Motors[RIGHT_MOTOR].state.hbridge_id = RIGHT_MOTOR; Motors[RIGHT_MOTOR].sensor_bit_pos[PMOD_HB_SENSOR_A] = HBRIDGE_RIGHT_SA_BIT; Motors[RIGHT_MOTOR].sensor_port[PMOD_HB_SENSOR_A] = HBRIDGE_RIGHT_SA_PORT; Motors[RIGHT_MOTOR].sensor_bit_pos[PMOD_HB_SENSOR_B] = HBRIDGE_RIGHT_SB_BIT; Motors[RIGHT_MOTOR].sensor_port[PMOD_HB_SENSOR_B] = HBRIDGE_RIGHT_SB_PORT; Motors[RIGHT_MOTOR].dir_port = HBRIDGE_RIGHT_DIR_PORT; Motors[RIGHT_MOTOR].dir_bit = HBRIDGE_RIGHT_DIR_BIT; Motors[RIGHT_MOTOR].ocn = HBRIDGE_RIGHT_OC; Motors[RIGHT_MOTOR].tmrn = HBRIDGE_RIGHT_TMR; // Setup function pointers Motors[LEFT_MOTOR].state.read_sensor_state = &read_sensor_state; Motors[RIGHT_MOTOR].state.read_sensor_state = &read_sensor_state; Motors[LEFT_MOTOR].state.set_direction = &set_direction; Motors[RIGHT_MOTOR].state.set_direction = &set_direction; Motors[LEFT_MOTOR].state.clear_direction = &clear_direction; Motors[RIGHT_MOTOR].state.clear_direction = &clear_direction; Motors[LEFT_MOTOR].state.set_speed = &set_speed; Motors[RIGHT_MOTOR].state.set_speed = &set_speed; Motors[LEFT_MOTOR].state.get_speed = &get_speed; Motors[RIGHT_MOTOR].state.get_speed = &get_speed; // Intialize direction set_hbridge_direction(&(Motors[LEFT_MOTOR].state), PMOD_HB_CCW); set_hbridge_direction(&(Motors[RIGHT_MOTOR].state), PMOD_HB_CW); // Intialize speed set_hbridge_speed(&(Motors[LEFT_MOTOR].state), 0); set_hbridge_speed(&(Motors[LEFT_MOTOR].state), 0); set_target_speed(LEFT_MOTOR, 0); set_target_speed(RIGHT_MOTOR, 0); }
/******************************************************************** Funciton: void CUPLDInitialize(void) ********************************************************************/ void CPLDInitialize(void) { CPLDSetGraphicsConfiguration(CPLD_GFX_CONFIG_8BIT); CPLDSetSPIFlashConfiguration(CPLD_SPI2); PORTSetPinsDigitalOut(IOPORT_G, BIT_14 | BIT_12); PORTSetPinsDigitalOut(IOPORT_A, BIT_7 | BIT_6); }
static void SetPic32MZIoPins(void) { PORTSetPinsDigitalOut(_ETH_MDC_PORT, _ETH_MDC_BIT); PORTSetPinsDigitalIn(_ETH_MDIO_PORT, _ETH_MDIO_BIT); PORTSetPinsDigitalOut(_ETH_TXEN_PORT, _ETH_TXEN_BIT); PORTSetPinsDigitalOut(_ETH_TXD0_PORT, _ETH_TXD0_BIT); PORTSetPinsDigitalOut(_ETH_TXD1_PORT, _ETH_TXD1_BIT); PORTSetPinsDigitalIn(_ETH_RXCLK_PORT, _ETH_RXCLK_BIT); PORTSetPinsDigitalIn(_ETH_RXDV_PORT, _ETH_RXDV_BIT); PORTSetPinsDigitalIn(_ETH_RXD0_PORT, _ETH_RXD0_BIT); PORTSetPinsDigitalIn(_ETH_RXD1_PORT, _ETH_RXD1_BIT); PORTSetPinsDigitalIn(_ETH_RXERR_PORT, _ETH_RXERR_BIT); }
int main(void) { char buffer[80]; unsigned int pb_clock; float actual_baud; const int baud = 500000; // max baud rate using arduino interface pb_clock = SYSTEMConfigPerformance(SYS_CLK); // if sys_clock > 100MHz, pb_clock = sys_clock/2 else pb_clock = sys_clock PORTSetPinsDigitalOut(IOPORT_E, BIT_4); // led // setup UART PPSUnLock; PPSInput(1,U1RX,RPF4); // Rx - F4 (pin 49) 5V tolerent PPSOutput(2,RPF5,U1TX); // Tx - F5 (pin 50) 5V tolerent PPSLock; actual_baud = U1_init(pb_clock, baud); sprintf(buffer, "SYSCLK: %d\r\n", SYS_CLK); U1_write(buffer); sprintf(buffer, "PBCLK: %d\r\n", pb_clock); U1_write(buffer); sprintf(buffer, "U1BRG: %d\r\n", U1BRG); U1_write(buffer); sprintf(buffer, "target baud: %d\r\n", baud); U1_write(buffer); sprintf(buffer, "actual baud: %f\r\n", actual_baud); U1_write(buffer); timer1_init(); timer_delay_test(); }
void Disp_Init(){ PORTClearBits(IOPORT_C , BIT_3|BIT_2|BIT_1); PORTClearBits(IOPORT_E , BIT_2|BIT_3|BIT_4|BIT_5|BIT_6|BIT_7); PORTClearBits(IOPORT_G , BIT_12|BIT_13|BIT_14|BIT_15); PORTSetPinsDigitalOut(IOPORT_C , BIT_3|BIT_2|BIT_1); PORTSetPinsDigitalOut(IOPORT_E , BIT_2|BIT_3|BIT_4|BIT_5|BIT_6|BIT_7); PORTSetPinsDigitalOut(IOPORT_G , BIT_12|BIT_13|BIT_14|BIT_15); mPORTCOpenDrainOpen(BIT_3|BIT_2|BIT_1); mPORTEOpenDrainOpen(BIT_2|BIT_3|BIT_4|BIT_5|BIT_6|BIT_7); mPORTGOpenDrainOpen(BIT_12|BIT_13|BIT_14|BIT_15); WR=0; //WR PSB=1; //PSB }
void BSP_IO_Init (void) { #ifdef _TARGET_440H // Each LED pin is set up as an output PORTSetPinsDigitalOut(IOPORT_D, BIT_6 | BIT_7); PORTSetPinsDigitalOut(IOPORT_F, BIT_0 | BIT_1); DisableCN0; #else // Each LED pin is set up as an output //PORTSetPinsDigitalOut(IO_LED1); //PORTSetPinsDigitalOut(IO_LED2); PORTSetPinsDigitalOut(IO_LED3); PORTSetPinsDigitalOut(IO_LED4); DisableCN0; PORTSetPinsDigitalOut(IO_LED5); // Motor direction PORTSetPinsDigitalOut(IO_Motor_dir_0); PORTSetPinsDigitalOut(IO_Motor_dir_1); START_Init(); COLOR_Init(); #endif CLIC_Init(); }
/** * Initialize the PIC32MX to communicate with the UG-23832HSWEG04 OLED display through the SSD1306 * display controller. */ void OledHostInit(void) { // Open SPI2 as a master in 1-byte mode running at 10MHz. // The peripheral bus is running at 10Mhz, and we want a 10MHz SPI bus clock. int pbClkDiv = 20000000 / 10000000; SpiChnOpen(SPI_CHANNEL2, SPI_OPEN_MSTEN | SPI_OPEN_CKP_HIGH | SPI_OPEN_MODE8, pbClkDiv); // Set RF4-6 as digital outputs for controlling data/command selection, logic power, and display // power. They're all initialized high beforehand, because that disables power. PORTSetBits(IOPORT_F, OLED_DRIVER_CNTLR_POWER_BIT | OLED_DRIVER_OLED_POWER_BIT | OLED_DRIVER_MODE_BIT); PORTSetPinsDigitalOut(OLED_DRIVER_MODE_PORT, OLED_DRIVER_MODE_BIT); // RF4 sets whether the next SPI byte is a data or command byte. PORTSetPinsDigitalOut(OLED_DRIVER_CNTLR_POWER_PORT, OLED_DRIVER_CNTLR_POWER_BIT); // RF5 controls power to the SSD1306 display controller. PORTSetPinsDigitalOut(OLED_DRIVER_OLED_POWER_PORT, OLED_DRIVER_OLED_POWER_BIT); // RF6 controls power to the UG-23832HSWEG04 OLED display. // Set RG9 as a digital output, tied to the reset pin on the SG1306 controller, low => reset. PORTSetBits(OLED_DRIVER_RESET_PORT, OLED_DRIVER_RESET_BIT); PORTSetPinsDigitalOut(OLED_DRIVER_RESET_PORT, OLED_DRIVER_RESET_BIT); }
void Setup_ports(void) { //Setup ESC pins for PWM output PORTSetPinsDigitalOut(IOPORT_D, BIT_0); //PWM1-OC1 digital pin 3 PORTSetPinsDigitalOut(IOPORT_D, BIT_1); //PWM2-OC2 digital pin 5 PORTSetPinsDigitalOut(IOPORT_D, BIT_2); //PWM3-OC3 digital pin 6 PORTSetPinsDigitalOut(IOPORT_D, BIT_3); //PWM4-OC4 digital pin 9 //Setup Input Capture pins PORTSetPinsDigitalIn(IOPORT_D, BIT_8); //IC1 digital pin 2 (THRO) PORTSetPinsDigitalIn(IOPORT_D, BIT_9); //IC2 digital pin 7 (AILE) PORTSetPinsDigitalIn(IOPORT_D, BIT_10); //IC3 digital pin 8 (ELEV) PORTSetPinsDigitalIn(IOPORT_D, BIT_11); //IC4 digital pin 35 (RUDD) PORTSetPinsDigitalIn(IOPORT_D, BIT_12); //IC5 digital pin 10 (GEAR) //Toggle pin for Timer 5 //PORTSetPinsDigitalOut(IOPORT_F, BIT_1); //pin 4 }
void DeviceInit() { //On MX4ck LED1 is on RB10 // LED2 is on RB11 // LED3 is on RB12 // LED4 is on RB13 //Set ports for onboard LEDs to outputs PORTSetPinsDigitalOut (IOPORT_B, BIT_10|BIT_11| BIT_12|BIT_13); }
/** * @fn void gpioInit( void ); * @brief Configuration des GPIO */ void gpioInit( void ) { // Désactivation du JTAG pour accéder aux LEDs mJTAGPortEnable( DEBUG_JTAGPORT_OFF ); // RD1 en sortie PORTSetPinsDigitalOut( IOPORT_D, BIT_1 ); // Initialisation de RD1 au niveau bas mPORTDClearBits( BIT_1 ); }
void MainInit(void) { PORTSetPinsDigitalOut(IOPORT_D, PD_LED_HEARTBEAT); sysSec = 0; sysTicks = 0; sysTickEvent = 0; SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_64, T1_TICK); // set up the timer interrupt with a priority of 2 ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2); } // MainInit()
void glcd_init(void) { #if defined(GLCD_CONTROLLER_PCD8544) /* Set up remappable outputs for PIC32, SPI: DO and SCK */ SpiChnOpen(SPI_CHANNEL2, SPI_CON_ON|SPI_CON_MSTEN, 4); /* Set SS, DC and RST pins as output */ PORTSetPinsDigitalOut(LCD_CS_PORT, LCD_CS_PIN); PORTSetPinsDigitalOut(LCD_DC_RESET_PORT, LCD_DC_PIN | LCD_RESET_PIN); /* Deselect LCD */ GLCD_DESELECT(); /* Send reset pulse to LCD */ glcd_reset(); /* Initialise the display */ glcd_PCD8544_init(); #else #error "Controller not supported" #endif /* GLCD_CONTROLLER_* */ }
void lcdconfig() { PORTSetPinsDigitalOut(IOPORT_D,BIT_4 | BIT_5); mPORTESetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); lcdcmnd(0x38); //init lcd 2 line 5x7 matrix delay(10); lcdcmnd(0x0E); //display on cursor blink delay(10); lcdcmnd(0x01); // clear display delay(10); lcdcmnd(0x06); // shift cursor right delay(10); lcdcmnd(0x84); // cursor at line 1 pos. 4 delay(10); }
void lcdini() { mPORTESetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); PORTSetPinsDigitalOut(IOPORT_D,BIT_5 | BIT_4); lcdcmd(0x38); msdelay(10); lcdcmd(0x0E); msdelay(10); lcdcmd(0x01); msdelay(10); lcdcmd(0x06); msdelay(10); lcdcmd(0x80); return; }
// *--------------------------------------------------------------------------------* int main(){ UINT8 k=0; UINT16 Conversion; mJTAGPortEnable(0); // JTAG des-habilitado SYSTEMConfigPerformance(GetSystemClock()); // Activa pre-cache.- LED1_OUTPUT(); LED2_OUTPUT(); LED3_OUTPUT(); LED4_OUTPUT(); SW1_INPUT(); SW2_INPUT(); PORTSetPinsDigitalOut(IOPORT_D, BIT_1); // Backlight del TFT mPORTDSetBits(BIT_1); vLCDTFTInit(); vLCDTFTFillScreen(ColorWhite); vADC_Init(); while(1){ if(SW1_STATUS()==0){ vLCDTFTRectangle(0,0,200,319,1,Colores[k]); if(++k==6){k=0;} DelayMs(250); } if(SW2_STATUS()==0){ LED2_TOGGLE(); LED4_TOGGLE(); DelayMs(250); } Conversion=ADC_Conversion(); vLCDTFTRectangle(201,0,239,319,1,ColorWhite); // 1023 -> 0; 0 -> 239 vLCDTFTRectangle(202,((-0.31183*Conversion)+319.0),238,319,1,ColorRed); DelayMs(50); } }
void OledHostInit() { #if defined (_BOARD_UNO_) || defined(_BOARD_UC32_) /* Initialize SPI port 2. */ SPI2CON = 0; SPI2BRG = 4; //8Mhz, with 80Mhz PB clock SPI2STATbits.SPIROV = 0; SPI2CONbits.CKP = 1; SPI2CONbits.MSTEN = 1; SPI2CONbits.ON = 1; #elif defined (_BOARD_MEGA_) /* Initialize pins for bit bang SPI. The Arduino Mega boards, ** and therefore the Max32 don't have the SPI port on the same ** connector pins as the Uno. The Basic I/O Shield doesn't even ** connect to the pins where the SPI port is located. So, for ** the Max32 board we need to do bit-banged SPI. */ PORTSetBits(prtSck, bitSck); PORTSetBits(prtMosi, bitMosi); PORTSetPinsDigitalOut(prtSck, bitSck); PORTSetPinsDigitalOut(prtMosi, bitMosi); #else //#error "No Supported Board Defined" #endif PORTSetBits(prtDataCmd, bitDataCmd); PORTSetBits(prtVddCtrl, bitVddCtrl); PORTSetBits(prtVbatCtrl, bitVbatCtrl); PORTSetPinsDigitalOut(prtDataCmd, bitDataCmd); //Data/Command# select PORTSetPinsDigitalOut(prtVddCtrl, bitVddCtrl); //VDD power control (1=off) PORTSetPinsDigitalOut(prtVbatCtrl, bitVbatCtrl); //VBAT power control (1=off) /* Make the RG9 pin be an output. On the Basic I/O Shield, this pin ** is tied to reset. */ PORTSetBits(prtReset, bitReset); PORTSetPinsDigitalOut(prtReset, bitReset); }
int main(void) { //LOCALS unsigned int temp; unsigned int channel1, channel2; M1_stepPeriod = M2_stepPeriod = M3_stepPeriod = M4_stepPeriod = 50; // in tens of u-seconds unsigned char M1_state = 0, M2_state = 0, M3_state = 0, M4_state = 0; SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); /* TIMER1 - now configured to interrupt at 10 khz (every 100us) */ OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, T1_TICK); ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2); /* TIMER2 - 100 khz interrupt for distance measure*/ OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, T2_TICK); ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_3); //It is off until trigger /* PORTA b2 and b3 for servo-PWM */ mPORTAClearBits(BIT_2 | BIT_3); mPORTASetPinsDigitalOut(BIT_2 | BIT_3); /* ULTRASONICS: some bits of PORTB for ultrasonic sensors */ PORTResetPins(IOPORT_B, BIT_8 | BIT_9| BIT_10 | BIT_11 ); PORTSetPinsDigitalOut(IOPORT_B, BIT_8 | BIT_9| BIT_10 | BIT_11); //trigger /* Input Capture pins for echo signals */ //interrupt on every risging/falling edge starting with a rising edge PORTSetPinsDigitalIn(IOPORT_D, BIT_8| BIT_9| BIT_10| BIT_11); //INC1, INC2, INC3, INC4 Pin mIC1ClearIntFlag(); OpenCapture1( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//front ConfigIntCapture1(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); OpenCapture2( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//back ConfigIntCapture2(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); OpenCapture3( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//left ConfigIntCapture3(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); OpenCapture4( IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//right ConfigIntCapture4(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3); /* PINS used for the START (RD13) BUTTON */ PORTSetPinsDigitalIn(IOPORT_D, BIT_13); #define CONFIG (CN_ON | CN_IDLE_CON) #define INTERRUPT (CHANGE_INT_ON | CHANGE_INT_PRI_2) mCNOpen(CONFIG, CN19_ENABLE, CN19_PULLUP_ENABLE); temp = mPORTDRead(); /* PORT D and E for motors */ //motor 1 mPORTDSetBits(BIT_4 | BIT_5 | BIT_6 | BIT_7); // Turn on PORTD on startup. mPORTDSetPinsDigitalOut(BIT_4 | BIT_5 | BIT_6 | BIT_7); // Make PORTD output. //motor 2 mPORTCSetBits(BIT_1 | BIT_2 | BIT_3 | BIT_4); // Turn on PORTC on startup. mPORTCSetPinsDigitalOut(BIT_1 | BIT_2 | BIT_3 | BIT_4); // Make PORTC output. //motor 3 and 4 mPORTESetBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); // Turn on PORTE on startup. mPORTESetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); // Make PORTE output. // UART2 to connect to the PC. // This initialization assumes 36MHz Fpb clock. If it changes, // you will have to modify baud rate initializer. UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY); UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY); UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1); UARTSetDataRate(UART2, GetPeripheralClock(), BAUD); UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX)); // 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); /* PORTD for LEDs - DEBUGGING */ mPORTDClearBits(BIT_0 | BIT_1 | BIT_2); mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2); // Congifure Change/Notice Interrupt Flag ConfigIntCN(INTERRUPT); // configure for multi-vectored mode INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); // enable interrupts INTEnableInterrupts(); counterDistanceMeasure=600; //measure ULTRASONICS distance each 60 ms while (1) { /***************** Robot MAIN state machine *****************/ unsigned char ret = 0; switch (Robo_State) { case 0: MotorsON = 0; Robo_State = 0; InvInitialOrientation(RESET); TestDog(RESET); GoToRoom4short(RESET); BackToStart(RESET); InitialOrientation(RESET); GoToCenter(RESET); GoToRoom4long(RESET); break; case 1: ret = InvInitialOrientation(GO); if (ret == 1) { Robo_State = 2; } break; case 2: ret = TestDog(GO); if (ret == 1) { Robo_State = 3; //DOG not found } else if (ret == 2) { Robo_State = 4; //DOG found } break; case 3: ret = GoToRoom4short(GO); if (ret == 1) { Robo_State = 0; } break; case 4: ret = BackToStart(GO); if (ret == 1) { Robo_State = 5; } break; case 5: ret = GoToCenter(GO); if (ret == 1) { Robo_State = 6; } break; case 6: ret = GoToRoom4long(GO); if (ret == 1) { Robo_State = 0; } break; } if (frontDistance < 30 || backDistance < 30 || leftDistance < 30 || rightDistance < 30) mPORTDSetBits(BIT_0); else mPORTDClearBits(BIT_0); /***************************************************************/ /***************** Motors State Machine ************************/ if (MotorsON) { /**************************** MOTOR MAP M1 O-------------O M2 ON EVEN MOTORS, STEPS MUST BE INVERTED | /\ | i.e. FORWARD IS BACKWARD | / \ | | || | | || | M3 O-------------O M4 *****************************/ if (M1_counter == 0) { switch (M1_state) { case 0: // set 0011 step (0x3 , 1); if (M1forward) M1_state = 1; else M1_state = 3; break; case 1: // set 1001 step (0x9 , 1); if (M1forward) M1_state = 2; else M1_state = 0; break; case 2: // set 1100 step (0xC , 1); if (M1forward) M1_state = 3; else M1_state = 1; break; case 3: // set 0110 default: step (0x6 , 1); if (M1forward) M1_state = 0; else M1_state = 2; break; } M1_counter = M1_stepPeriod; step_counter[0]--; if (directionNow == countingDirection) step_counter[1]--; } if (M2_counter == 0) { switch (M2_state) { case 0: // set 0011 step (0x3 , 2); if (M2forward) M2_state = 1; else M2_state = 3; break; case 1: // set 0110 step (0x6 , 2); if (M2forward) M2_state = 2; else M2_state = 0; break; case 2: // set 1100 step (0xC , 2); if (M2forward) M2_state = 3; else M2_state = 1; break; case 3: // set 1001 default: step (0x9 , 2); if (M2forward) M2_state = 0; else M2_state = 2; break; } M2_counter = M2_stepPeriod; } if (M3_counter == 0) { switch (M3_state) { case 0: // set 0011 step (0x3 , 3); if (M3forward) M3_state = 1; else M3_state = 3; break; case 1: // set 1001 step (0x9 , 3); if (M3forward) M3_state = 2; else M3_state = 0; break; case 2: // set 1100 step (0xC , 3); if (M3forward) M3_state = 3; else M3_state = 1; break; case 3: // set 0110 default: step (0x6 , 3); if (M3forward) M3_state = 0; else M3_state = 2; break; } M3_counter = M3_stepPeriod; } if (M4_counter == 0) { switch (M4_state) { case 0: // set 0011 step (0x3 , 4); if (M4forward) M4_state = 1; else M4_state = 3; break; case 1: // set 0110 step (0x6 , 4); if (M4forward) M4_state = 2; else M4_state = 0; break; case 2: // set 1100 step (0xC , 4); if (M4forward) M4_state = 3; else M4_state = 1; break; case 3: // set 1001 default: step (0x9 , 4); if (M4forward) M4_state = 0; else M4_state = 2; break; } M4_counter = M4_stepPeriod; } } else { //motors off mPORTDSetBits(BIT_4 | BIT_5 | BIT_6 | BIT_7); mPORTCSetBits(BIT_1 | BIT_2 | BIT_3 | BIT_4); mPORTESetBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); } /************************************************************/ /******* TEST CODE, toggles the servos (from 90 deg. to -90 deg.) every 1 s. ********/ /* if (auxcounter == 0) { servo1_angle = 0; if (servo2_angle == 90) servo2_angle = -90; else servo2_angle = 90; auxcounter = 20000; // toggle angle every 2 s. } */ servo1_angle = 0; servo2_angle = -90; /* if (frontDistance > 13 && frontDistance < 17) { servo2_angle = 90; } else servo2_angle = -90; */ /*******************************************************************/ /****************** SERVO CONTROL ******************/ /* Changing the global servoX_angle at any point in the code will move the servo to the desired angle. */ servo1_counter = (servo1_angle + 90)*(18)/180 + 6; // between 600 and 2400 us if (servo1_period == 0) { mPORTASetBits(BIT_2); servo1_period = SERVOMAXPERIOD; /* 200 * 100us = 20000us period */ } servo2_counter = (servo2_angle + 90)*(18)/180 + 6; // between 600 and 2400 us if (servo2_period == 0) { mPORTASetBits(BIT_3); servo2_period = SERVOMAXPERIOD; /* 200 * 100us = 20000us period */ } /*****************************************************/ } /* end of while(1) */ return 0; }
// 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 }
/** * Initialize an SD flash memory card. * * \param[in] sckRateID SPI clock rate selector. See setSckRate(). * \param[in] chipSelectPin SD chip select pin number. * * \return The value one, true, is returned for success and * the value zero, false, is returned for failure. The reason for failure * can be determined by calling errorCode() and errorData(). */ uint8_t Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { errorCode_ = inBlock_ = partialBlockRead_ = type_ = 0; // 16-bit init start time allows over a minute uint16_t t0 = (uint16_t)millis(); uint32_t arg; SPI2CON = 0; DDPCONbits.JTAGEN = 0; AD1PCFG = 0xFFFF; chipSelectPin_ = chipSelectPin; pinMode(chipSelectPin_, OUTPUT); PORTSetPinsDigitalOut(prtSCK, bnSCK); PORTSetPinsDigitalOut(prtSDO, bnSDO); PORTSetPinsDigitalIn(prtSDI, bnSDI); // set pin modes chipSelectHigh(); // must supply min of 74 clock cycles with CS high. for (uint8_t i = 0; i < 10; i++) spiSend(0XFF); chipSelectLow(); // command to go idle in SPI mode while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) { if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) { error(SD_CARD_ERROR_CMD0); goto fail; } } // check SD version if ((cardCommand(CMD8, 0x1AA) & R1_ILLEGAL_COMMAND)) { type(SD_CARD_TYPE_SD1); } else { // only need last byte of r7 response for (uint8_t i = 0; i < 4; i++) status_ = spiRec(); if (status_ != 0XAA) { error(SD_CARD_ERROR_CMD8); goto fail; } type(SD_CARD_TYPE_SD2); } // initialize card and send host supports SDHC if SD2 arg = type() == SD_CARD_TYPE_SD2 ? 0X40000000 : 0; while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) { // check for timeout if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) { error(SD_CARD_ERROR_ACMD41); goto fail; } } // if SD2 read OCR register to check for SDHC card if (type() == SD_CARD_TYPE_SD2) { if (cardCommand(CMD58, 0)) { error(SD_CARD_ERROR_CMD58); goto fail; } if ((spiRec() & 0XC0) == 0XC0) type(SD_CARD_TYPE_SDHC); // discard rest of ocr - contains allowed voltage range for (uint8_t i = 0; i < 3; i++) spiRec(); } chipSelectHigh(); #ifndef SOFTWARE_SPI return setSckRate(sckRateID); #else // SOFTWARE_SPI return true; #endif // SOFTWARE_SPI fail: chipSelectHigh(); return false; }
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]); } }
void I2C1update(void) { if (I2C1STAT & 0x0400) { // bus collision i2c1Mode = I2C1_MODE_ERROR; i2c1BusState = I2C1_BUS_ERROR; I2CEnable(I2C1, FALSE); } if (i2c1Delay) { i2c1Delay--; return; } if (i2c1Mode == I2C1_MODE_IDLE) { if (i2c1QueueRd != i2c1QueueWr) { // show that I2C1 is busy i2c1Mode = I2C1_MODE_WRITE; // clear the write and read indices i2c1Queue[i2c1QueueRd].wi = 0; i2c1Queue[i2c1QueueRd].ri = 0; // start I2C1 transfer i2c1BusState = I2C1_BUS_WR_DATA; I2C1CONSET = _I2CCON_SEN_MASK; } } // I2C1_IDLE else if (i2c1Mode == I2C1_MODE_WRITE_COMPLETE) { I2C1process(); i2c1QueueRd++; if (i2c1QueueRd >= I2C1_QUEUE_SIZE) { i2c1QueueRd = 0; } i2c1Mode = I2C1_MODE_IDLE; } // I2C1_WRITE_COMPLETE else if (i2c1Mode == I2C1_MODE_READ_COMPLETE) { I2C1process(); i2c1QueueRd++; if (i2c1QueueRd >= I2C1_QUEUE_SIZE) { i2c1QueueRd = 0; } i2c1Mode = I2C1_MODE_IDLE; } // I2C1_READ_COMPLETE else if (i2c1Mode == I2C1_MODE_ERROR) { // usually caused is SDA is low before start if (!(PORTA & PA_SCL1)) { // is clock is low, raise it PORTSetPinsDigitalOut(IOPORT_A, PA_SCL1); PORTSetBits(IOPORT_A, PA_SCL1); } else { if (!(PORTA & PA_SDA1)) { // if SDA is still low, try another clock pulse PORTSetPinsDigitalOut(IOPORT_A, PA_SCL1); PORTClearBits(IOPORT_A, PA_SCL1); } else { // SDA is now high, try clearing the error PORTSetPinsDigitalOut(IOPORT_A, PA_SDA1); PORTClearBits(IOPORT_A, PA_SDA1); i2c1Mode = I2C1_MODE_ERR_START; } } } else if (i2c1Mode == I2C1_MODE_ERR_START) { PORTSetBits(IOPORT_A, PA_SDA1); i2c1Mode = I2C1_MODE_ERR_STOP; } else if (i2c1Mode == I2C1_MODE_ERR_STOP) { I2C1STAT &= ~0x400; IFS0 &= ~0x20000000; PORTSetPinsDigitalIn(IOPORT_A, PA_SCL1 | PA_SDA1); I2CEnable(I2C1, TRUE); i2c1Mode = I2C1_MODE_IDLE; } } // I2C1update()
int main(void) { bool ret_val = false; int i = 0; char message[20]; my_delay_timer delay_timer_ref = my_delay_timer::get_instance(); delay_timer_ref.init(SYS_CLOCK / PB_DIV); INTEnableSystemMultiVectoredInt(); my_I2C_handler i2c_driver = my_I2C_handler::get_instance(); ret_val = i2c_driver.init(I2C2, SYS_CLOCK / PB_DIV, DESIRED_I2C_FREQ_1KHZ); if (!ret_val) { WOOP_WOOP_WOOP(); } ret_val = i2c_driver.CLS_init(); if (!ret_val) { WOOP_WOOP_WOOP(); } ret_val = i2c_driver.CLS_write_to_line("CLS initialized", 1); if (!ret_val) { WOOP_WOOP_WOOP(); } ret_val = i2c_driver.temp_init(); if (!ret_val) { WOOP_WOOP_WOOP(); } // turn an LED on and off for a little light show because...reasons PORTSetPinsDigitalOut(IOPORT_B, BIT_10); PORTClearBits(IOPORT_B, BIT_10); // loop forever i = 0; bool LED_is_on = false; float temp = 0.0f; while(1) { ret_val = i2c_driver.temp_read(&temp, true); if (ret_val) { snprintf(message, CLS_LINE_SIZE, "temp = '%.3f'", temp); } else { snprintf(message, CLS_LINE_SIZE, "temp = XXXX"); } i2c_driver.CLS_write_to_line(message, 1); snprintf(message, CLS_LINE_SIZE, "i = '%d'", i); i2c_driver.CLS_write_to_line(message, 2); if (LED_is_on) { PORTClearBits(IOPORT_B, BIT_10); LED_is_on = false; } else { PORTSetBits(IOPORT_B, BIT_10); LED_is_on = true; } i++; delay_timer_ref.delay_ms(200); } }
int main(void) { SpiOpenFlags spiFlags; AudioStereo test_sine[]={ 0 , 0 , 946234 , 946234 , 1877546 , 1877546 , 2779247 , 2779247 , 3637119 , 3637119 , 4437630 , 4437630 , 5168158 , 5168158 , 5817180 , 5817180 , 6374462 , 6374462 , 6831216 , 6831216 , 7180237 , 7180237 , 7416020 , 7416020 , 7534850 , 7534850 , 7534850 , 7534850 , 7416020 , 7416020 , 7180237 , 7180237 , 6831216 , 6831216 , 6374462 , 6374462 , 5817180 , 5817180 , 5168158 , 5168158 , 4437630 , 4437630 , 3637119 , 3637119 , 2779247 , 2779247 , 1877546 , 1877546 , 946234 , 946234 , 0 , 0 , -946234 , -946234 , -1877546 , -1877546 , -2779247 , -2779247 , -3637119 , -3637119 , -4437630 , -4437630 , -5168158 , -5168158 , -5817180 , -5817180 , -6374462 , -6374462 , -6831216 , -6831216 , -7180237 , -7180237 , -7416020 , -7416020 , -7534850 , -7534850 , -7534850 , -7534850 , -7416020 , -7416020 , -7180237 , -7180237 , -6831216 , -6831216 , -6374462 , -6374462 , -5817180 , -5817180 , -5168158 , -5168158 , -4437630 , -4437630 , -3637119 , -3637119 , -2779247 , -2779247 , }; // Initialize audio codec. WM8960CodecOpen(); WM8960CodecConfigVolume(0,0); WM8960CodecConfigSampleRate(SAMPLE_RATE_16000_HZ); WM8960CodecConfigVolume(volADC,volDAC); //Congigure MIPS, Prefetch Cache module. SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE); INTEnableSystemMultiVectoredInt(); //Test tone vector sampled at 48kHz. txBuffer = test_sine; //Configure the direction of used pins and //configure as digital pins. PORTSetPinsDigitalIn(IOPORT_G, BIT_7); PORTSetPinsDigitalOut(IOPORT_G, BIT_6); PORTSetPinsDigitalOut(IOPORT_G, BIT_8); PORTSetPinsDigitalOut(IOPORT_G, BIT_9); // //Configure Reference Clock Output to 12.288MHz. // mOSCREFOTRIMSet(REFTRIM); // OSCREFConfig(OSC_REFOCON_USBPLL, //USB-PLL clock output used as REFCLKO source // OSC_REFOCON_OE | OSC_REFOCON_ON, //Enable and turn on the REFCLKO // RODIV); //Configure SPI in I2S mode with 24-bit stereo audio. spiFlags= SPI_OPEN_MSTEN | //Master mode enable SPI_OPEN_SSEN | //Enable slave select function SPI_OPEN_CKP_HIGH | //Clock polarity Idle High Actie Low SPI_OPEN_MODE32 | //Data mode: 32b SPI_OPEN_FRMEN | // Enable Framed SPI SPI_OPEN_FSP_IN | // Frame Sync Pulse is input SPI_OPEN_FSP_HIGH; //Frame Sync Pulse is active high //Configure and turn on the SPI1 module. SpiChnEnable(WM8960DRV_SPI_MODULE, FALSE); SpiChnConfigure(WM8960DRV_SPI_MODULE, spiFlags); SpiChnSetBitRate(WM8960DRV_SPI_MODULE, GetPeripheralClock(), 1024000); SpiChnEnable(WM8960DRV_SPI_MODULE, TRUE); //Enable SPI2 interrupt. INTSetVectorPriority(INT_SPI_2_VECTOR, INT_PRIORITY_LEVEL_4); INTSetVectorSubPriority(INT_SPI_2_VECTOR, INT_SUB_PRIORITY_LEVEL_0); INTEnable(INT_SPI2, INT_ENABLED); SpiChnPutC(SPI_CHANNEL2, 0); //Dummy write to start the SPI //while (1); return 0; }