void motors_init() { PIO_Configure(&pin_m0_en, 1); PIO_Configure(&pin_m1_en, 1); PIO_Configure(&pin_m2_en, 1); PIO_Configure(&pin_m_brake, 1); PIO_Configure(&pin_m0_dir, 1); PIO_Configure(&pin_m1_dir, 1); PIO_Configure(&pin_m2_dir, 1); PMC_EnablePeripheral(ID_PWM); // power up pwm controller plz PIO_Configure(&pin_m0_vref, 1); // PWML0 PIO_Configure(&pin_m1_vref, 1); // PWMH1 PIO_Configure(&pin_m2_vref, 1); // PWMH3 PWM->PWM_CH_NUM[0].PWM_CMR = 0; // pass-through divider. 64 MHz clock. PWM->PWM_CH_NUM[0].PWM_CPRD = 255; // 8-bit current control, 125 kHz cycle PWM->PWM_CH_NUM[0].PWM_CDTY = 0; // zero duty for now PWM->PWM_CH_NUM[1].PWM_CMR = 0; PWM->PWM_CH_NUM[1].PWM_CPRD = 255; PWM->PWM_CH_NUM[1].PWM_CDTY = 255; PWM->PWM_CH_NUM[3].PWM_CMR = 0; PWM->PWM_CH_NUM[3].PWM_CPRD = 255; PWM->PWM_CH_NUM[3].PWM_CDTY = 255; PWM->PWM_ENA = 0xb; // enable channels 0,1,3 PIO_Set(&pin_m_brake); PIO_Set(&pin_m0_en); PIO_Set(&pin_m1_en); PIO_Set(&pin_m2_en); }
//------------------------------------------------------------------------------ /// Set the backlight of the LCD. /// \param level Backlight brightness level [1..32], 32 is maximum level. //------------------------------------------------------------------------------ void LCDD_SetBacklight (unsigned int level) { unsigned int i; const Pin pPins[] = {BOARD_BACKLIGHT_PIN}; // Enable pins PIO_Configure(pPins, PIO_LISTSIZE(pPins)); // Switch off backlight PIO_Clear(pPins); i = 600 * (BOARD_MCK / 1000000); // wait for at least 500us while(i--); // Set new backlight level for (i = 0; i < level; i++) { PIO_Clear(pPins); PIO_Clear(pPins); PIO_Clear(pPins); PIO_Set(pPins); PIO_Set(pPins); PIO_Set(pPins); // PIO_Clear(pPins); // PIO_Clear(pPins); // PIO_Clear(pPins); } // PIO_Set(pPins); }
/** * \brief Set the backlight of the LCD. * * \param level Backlight brightness level [1..16], 1 means maximum brightness. */ void LCDD_SetBacklight (uint32_t level) { uint32_t i; const Pin pPins[] = {BOARD_BACKLIGHT_PIN}; /* Ensure valid level */ level = (level < 1) ? 1 : level; level = (level > 16) ? 16 : level; /* Enable pins */ PIO_Configure(pPins, PIO_LISTSIZE(pPins)); /* Switch off backlight */ PIO_Clear(pPins); i = 600 * (BOARD_MCK / 1000000); /* wait for at least 500us */ while(i--); /* Set new backlight level */ for (i = 0; i < level; i++) { PIO_Clear(pPins); PIO_Clear(pPins); PIO_Clear(pPins); PIO_Set(pPins); PIO_Set(pPins); PIO_Set(pPins); } }
void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable ) { if( xRxEnable ) { USART_SetReceiverEnabled( xUSARTHWMappings[ucUsedPort].pUsart, 1 ); USART_EnableIt( xUSARTHWMappings[ucUsedPort].pUsart, US_IDR_RXRDY ); } else { USART_DisableIt( xUSARTHWMappings[ucUsedPort].pUsart, US_IDR_RXRDY ); USART_SetReceiverEnabled( xUSARTHWMappings[ucUsedPort].pUsart, 0 ); } if( xTxEnable ) { if( NULL != xUSARTHWMappings[ucUsedPort].USARTNotREPin ) { PIO_Set( xUSARTHWMappings[ucUsedPort].USARTNotREPin ); } if( NULL != xUSARTHWMappings[ucUsedPort].USARTDEPin ) { PIO_Set( xUSARTHWMappings[ucUsedPort].USARTDEPin ); } USART_SetTransmitterEnabled( xUSARTHWMappings[ucUsedPort].pUsart, 1 ); USART_EnableIt( xUSARTHWMappings[ucUsedPort].pUsart, US_IER_TXRDY ); USART_DisableIt( xUSARTHWMappings[ucUsedPort].pUsart, US_IER_TXEMPTY ); } else { USART_DisableIt( xUSARTHWMappings[ucUsedPort].pUsart, US_IDR_TXRDY ); USART_EnableIt( xUSARTHWMappings[ucUsedPort].pUsart, US_IER_TXEMPTY ); } }
void motor_enaxis(unsigned char axis, unsigned char en){ switch(axis){ case(0): if(!en) PIO_Set(&XEN); else PIO_Clear(&XEN); break; case(1): if(!en) PIO_Set(&YEN); else PIO_Clear(&YEN); break; case(2): if(!en) PIO_Set(&ZEN); else PIO_Clear(&ZEN); break; case(3): if(!en) PIO_Set(&E0EN); else PIO_Clear(&E0EN); break; case(4): if(!en) PIO_Set(&E1EN); else PIO_Clear(&E1EN); break; } }
void vParTestSetLED( unsigned portBASE_TYPE uxLED, signed portBASE_TYPE xValue ) { if( uxLED < partestNUM_LEDS ) { if( xValue ) { /* Turn the LED on. */ portENTER_CRITICAL(); { if( xLEDPins[ uxLED ].type == PIO_OUTPUT_0 ) { PIO_Set( &( xLEDPins[ uxLED ]) ); } else { PIO_Clear( &( xLEDPins[ uxLED ] ) ); } } portEXIT_CRITICAL(); } else { /* Turn the LED off. */ portENTER_CRITICAL(); { if( xLEDPins[ uxLED ].type == PIO_OUTPUT_0 ) { PIO_Clear( &( xLEDPins[ uxLED ] ) ); } else { PIO_Set( &( xLEDPins[ uxLED ] ) ); } } portEXIT_CRITICAL(); } } }
//--------------------------- void glcd_init(void) { unsigned int tcmr,tfmr; unsigned char i; PIO_Configure(GLcdpins,PIO_LISTSIZE(GLcdpins)); PIO_Configure(sscPins, PIO_LISTSIZE(sscPins)); PIO_Set(&GLcdpins[3]); PIO_Set(&GLcdpins[4]); SSC_Configure(AT91C_BASE_SSC,AT91C_ID_SSC,500000,BOARD_MCK); tcmr=AT91C_SSC_CKS_TK|AT91C_SSC_CKO_DATA_TX|AT91C_SSC_START_CONTINOUS; tfmr=SSC_DATLEN(8)|SSC_DATNB(15)|SSC_FSLEN(16)|AT91C_SSC_FSOS_LOW|AT91C_SSC_FSDEN ; SSC_ConfigureTransmitter(AT91C_BASE_SSC,tcmr,tfmr); SSC_EnableTransmitter(AT91C_BASE_SSC); PIO_Set(&GLcdpins[5]); //PIO_Clear(&GLcdpins[5]); for(i = 0; i < 3; i++); GLCD_WriteCommand((DISPLAY_ON_CMD | ON), i); }
void motor_setdir(unsigned char axis, unsigned char dir){ switch(axis){ case(0): if(dir) PIO_Set(&XDIR); else PIO_Clear(&XDIR); break; case(1): if(dir) PIO_Set(&YDIR); else PIO_Clear(&YDIR); break; case(2): if(dir) PIO_Set(&ZDIR); else PIO_Clear(&ZDIR); break; case(3): if(dir) PIO_Set(&E0DIR); else PIO_Clear(&E0DIR); break; case(4): if(dir) PIO_Set(&E1DIR); else PIO_Clear(&E1DIR); break; } }
//------------------------ //------------------------------------------------------------------------------------------------- // //------------------------------------------------------------------------------------------------- void GLCD_DisableController(unsigned char controller) { switch(controller){ case 0 : PIO_Set(&GLcdpins[4]); break; case 1 : PIO_Set(&GLcdpins[3]); break; } }
void stepper_enable(void) { PIO_Set(&pin_reset); PIO_Clear(&pin_enable); PIO_Set(&pin_sleep); stepper_state = STEPPER_STATE_STOP; stepper_speedramp_state = STEPPER_SPEEDRAMP_STATE_STOP; }
void AD5206_sendbit(unsigned char bit){ volatile unsigned int uDummy; for (uDummy=0; uDummy<512; ++uDummy); PIO_Clear(&SCK); if(bit) PIO_Set(&MOSI); else PIO_Clear(&MOSI); for (uDummy=0; uDummy<512; ++uDummy); PIO_Set(&SCK); }
int main(void) { device_init(); init_display(); PIO_Set(&display_pins[0]); PIO_Clear(&display_pins[1]); PIO_Clear(&display_pins[2]); PIO_Set(&display_pins[3]); while(1){ } }
void motor_setopts(unsigned char axis, unsigned char ustepbits, unsigned char current){ Pin MS1; Pin MS2; unsigned char channel; switch(axis){ case 0: MS1=XMS1; MS2=XMS2; channel=3; break; case 1: MS1=YMS1; MS2=YMS2; channel=1; break; case 2: MS1=ZMS1; MS2=ZMS2; channel=0; break; case 3: MS1=E0MS1; MS2=E0MS2; channel=2; break; case 4: MS1=E1MS1; MS2=E1MS2; channel=5; break; case 6: ustepbits=4; channel=4; break; default: return; } if(ustepbits<4){ if(ustepbits&1) PIO_Set(&MS1); else PIO_Clear(&MS1); if(ustepbits&2) PIO_Set(&MS2); else PIO_Clear(&MS2); } AD5206_setchan(channel,current); //printf("Setting channel %u to current value %u and ustep value %u\r\n",channel, current, ustepbits); }
// ============================================================================ void lcd_putchar(char ch) { PIO_Set(&Lcdpins[0]); setD4567(ch); PIO_Set(&Lcdpins[1]); clcd_minDelay(); PIO_Clear(&Lcdpins[1]); clcd_minDelay(); setD4567(ch<<4); PIO_Set(&Lcdpins[1]); clcd_minDelay(); PIO_Clear(&Lcdpins[1]); clcd_Delay(); }
// ============================================================================ void lcd_command(char cmd) //Sends Command to LCD { PIO_Clear(&Lcdpins[0]); setD4567(cmd); PIO_Set(&Lcdpins[1]); clcd_minDelay(); PIO_Clear(&Lcdpins[1]); clcd_minDelay(); setD4567(cmd<<4); PIO_Set(&Lcdpins[1]); clcd_minDelay(); PIO_Clear(&Lcdpins[1]); clcd_Delay(); }
void motor_setup(){ Pin MOTPINS[]={XMS1,XMS2,XEN,XSTEP,XDIR,YMS1,YMS2,YEN,YSTEP,YDIR,ZMS1,ZMS2,ZEN,ZSTEP,ZDIR,E0MS1,E0MS2,E0EN,E0STEP,E0DIR,E1MS1,E1MS2,E1EN,E1STEP,E1DIR,}; PIO_Configure(MOTPINS,25); PIO_Set(&XEN); PIO_Set(&YEN); PIO_Set(&ZEN); PIO_Set(&E0EN); PIO_Set(&E1EN); AD5206_setup(); int i; for(i=0;i<5;i++) motor_setopts(i,pa.axis_ustep[i],pa.axis_current[i]); printf("done setting up motors\r\n\n"); }
//------------------------------------- void GLCD_WriteData(unsigned char dataToWrite) { unsigned int byte; DELAY() PIO_Clear(&GLcdpins[2]); PIO_Set(&GLcdpins[0]); byte = dataToWrite; DATAPORT(byte); GLCD_EnableController(screen_x / 64); PIO_Set(&GLcdpins[1]); DELAY(); PIO_Clear(&GLcdpins[1]); GLCD_DisableController(screen_x / 64); screen_x++; }
/*! \brief Enable or disable Power of E4K * \param[in] e4k E4K reference * \param[in] on Enable (1) or disable (0) Power */ void sam3u_e4k_power(struct e4k_state *e4k, int on) { if (on) PIO_Set(&pin_pwdn); else PIO_Clear(&pin_pwdn); }
int main(void) { /// Init DBGU TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK); printf("-- %s: blink-interrupt Project with at91lib v%s --\n\r", BOARD_NAME, SOFTPACK_VERSION); printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__); /// Init PIOA & PIOB interrupt PIO_InitializeInterrupts(0); /// Init LED PIO_Configure(&pinLed, 1); TRACE_INFO("LED:\n\r"); /// Configure and enable the interrupt PIO_ConfigureIt(&pinButton, (void (*)(const Pin *)) User_InterruptHandler); PIO_EnableIt(&pinButton); while (1) { PIO_Set(&pinLed); delay(1000); PIO_Clear(&pinLed); delay(1000); } }
void dc_enable(void) { PIO_Set(&pin_enable); dc_enabled = true; dc_led_error_reason &= ~DC_LED_ERROR_SHUTDOWN; led_off(LED_STD_RED); }
void set_pin(const Pin *pin, const uint8_t state) { if (state) PIO_Set(pin); else PIO_Clear(pin); }
/* Function: USBD_Disconnect Disconnects the pull-up from the D+ line of the USB. */ void USBD_Disconnect() { TRACE_DEBUG( "Disc "); #if defined(BOARD_USB_PULLUP_EXTERNAL) const Pin pinPullUp = PIN_USB_PULLUP; if (pinPullUp.attribute == PIO_OUTPUT_0) { PIO_Clear(&pinPullUp); } else { PIO_Set(&pinPullUp); } #elif defined(BOARD_USB_PULLUP_INTERNAL) AT91C_BASE_UDP->UDP_TXVC &= ~AT91C_UDP_PUON; #elif defined(BOARD_USB_PULLUP_MATRIX) AT91C_BASE_MATRIX->MATRIX_USBPCR &= ~AT91C_MATRIX_USBPCR_PUON; #elif !defined(BOARD_USB_PULLUP_ALWAYSON) #error Unsupported pull-up type. #endif // Device returns to the Powered state if (deviceState > USBD_STATE_POWERED) { deviceState = USBD_STATE_POWERED; } }
uint16_t tactile_read_adc(uint8_t adc_chan, const uint8_t num_samples) { // 0x3c49 const uint16_t adc_cfg = (0x3c09 | (adc_chan << 7)) << 2; volatile uint16_t rx; uint32_t adc_sum = 0; for (int i = 0; i < 3 + num_samples; i++) { PIO_Clear(&pin_cs_adc_tactile); ADC_T_EN; SPI->SPI_TDR = adc_cfg; while ((SPI->SPI_SR & SPI_SR_TXEMPTY) == 0) { } rx = SPI->SPI_RDR; ADC_T_DIS; PIO_Set(&pin_cs_adc_tactile); ADC_T_CYC; if (i >= 3) { adc_sum += rx; for (volatile int j = 0; j < 100; j++) { } } } return adc_sum / num_samples; }
unsigned char Get_Current( unsigned char channel, unsigned char repeat_times, unsigned char *pData) { unsigned char status = 0; unsigned int i, range_flag ; unsigned int total, average; unsigned int adc_result[MAX_REPEAT_TIMES]; if( channel > 2 ) { //channel is limited to 0 ~ 2 return 0x22;//error } range_flag = 0x80000000 ; //0.33 Ohm, larger current range if( repeat_times > MAX_REPEAT_TIMES || repeat_times==0 ) { return 0x23; //error } OSTimeDly(100); //delay_ms(100); ????? status = Init_AD7799( channel ); if(status!=0) { return status; //init failed, return ; } adc_result[0] = AD7799_GetConvertedData(); //first test data discard adc_result[0] = AD7799_GetConvertedData(); if(adc_result[0] < RANGE_LIMIT ) { range_flag = 0x00000000 ; //220 Ohm, small current range PIO_Set( &pinsControls[6+channel] );//switch to 220 Ohm : 0.1uA ~ 100uA range OSTimeDly(1000); //delay_ms(1000); adc_result[0] = AD7799_GetConvertedData(); } //printf("adc_result[0] = 0x%8X\r\n",adc_result[0] ); total = adc_result[0]; for(i=1; i<repeat_times; i++) { adc_result[i] = AD7799_GetConvertedData(); total += adc_result[i]; //printf("adc_result[%d] = 0x%8X\r\n",i,adc_result[i] ); } // reset R to 0.33//220 after test // OSTimeDlyHMSM(0,0,3,0); //delay_ms(100); ????? OSTimeDly(2000); //delay_ms(2000); if(range_flag == 0) { PIO_Clear( &pinsControls[6+ channel] );//switch to 0.33//220 Ohm : 0.1mA ~ 30mA range } average = total/repeat_times + range_flag; *(pData++) = average>>0 & 0xFF; *(pData++) = average>>8 & 0xFF; *(pData++) = average>>16 & 0xFF; *pData = average>>24 & 0xFF; //printf("adc_data = 0x%8X\r\n",average ); return status ; }
void tick_task(uint8_t tick_type) { if(tick_type == TICK_TASK_TYPE_CALCULATION) { stepper_tick_calc_counter++; stepper_current_sum += adc_channel_get_data(STEPPER_CURRENT_CHANNEL); if(stepper_tick_calc_counter % 100 == 0) { stepper_current = stepper_current_sum/100; stepper_current_sum = 0; } // Switch Output Voltage between extern and stack if(stepper_get_external_voltage() < STEPPER_VOLTAGE_EPSILON) { PIO_Set(&pin_voltage_switch); } else { PIO_Clear(&pin_voltage_switch); } } else if(tick_type == TICK_TASK_TYPE_MESSAGE) { stepper_tick_counter++; if(stepper_position_reached) { stepper_position_reached = false; stepper_position_reached_signal(); } stepper_check_error_signals(); } }
/*! \brief Enable or disable standby mode of E4K * \param[in] e4k E4K reference * \param[in] on Enable (1) or disable (0) STBY */ void sam3u_e4k_stby(struct e4k_state *e4k, int on) { if (on) PIO_Clear(&pin_rfstby); else PIO_Set(&pin_rfstby); }
/** * Turns the given LED on if it exists; otherwise does nothing. * \param led Number of the LED to turn on. * \return 1 if the LED has been turned on; 0 otherwise. */ extern uint32_t LED_Set( uint32_t dwLed ) { #ifdef PINS_LEDS /* Check if LED exists */ if ( dwLed >= numLeds ) { return 0 ; } /* Turn LED on */ if ( pinsLeds[dwLed].type == PIO_OUTPUT_0 ) { PIO_Set( &pinsLeds[dwLed] ) ; } else { PIO_Clear( &pinsLeds[dwLed] ) ; } return 1 ; #else return 0 ; #endif }
void AD5206_setup(){ Pin SPIPINS[]={MOSI,SCK,CS}; PIO_Configure(SPIPINS,3); PIO_Set(&CS); }
// Switch to TX mode void nrf24l01plus_mode_tx_send(void) { PIO_Clear(&CE); SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 unsigned chars) & Prim:TX. SPI_RW_Reg(FLUSH_TX,0); SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH); // write playload to TX_FIFO PIO_Set(&CE); // Set CE pin high to enable RX device }
void stepper_set_sync_rect(bool sr) { Pin srpin = PIN_SYNC_RECT; if(sr) { PIO_Clear(&srpin); } else { PIO_Set(&srpin); } }