pio_type platform_pio_op( unsigned port, pio_type pinmask, int op ) { Pin* pin; pio_type retval = 1; pin = pio_port_desc + port; pin->mask = pinmask; switch( op ) { case PLATFORM_IO_PORT_SET_VALUE: case PLATFORM_IO_PIN_SET: PIO_Set( pin ); break; case PLATFORM_IO_PIN_CLEAR: PIO_Clear( pin ); break; case PLATFORM_IO_PORT_DIR_INPUT: pin->mask = 0x7FFFFFFF; case PLATFORM_IO_PIN_DIR_INPUT: pin->type = PIO_INPUT; PIO_Configure( pin, 1 ); break; case PLATFORM_IO_PORT_DIR_OUTPUT: pin->mask = 0x7FFFFFFF; case PLATFORM_IO_PIN_DIR_OUTPUT: pin->type = PIO_OUTPUT_0; PIO_Configure( pin, 1 ); break; case PLATFORM_IO_PORT_GET_VALUE: pin->mask = 0x7FFFFFFF; pin->type = PIO_INPUT; retval = PIO_Get( pin ); break; case PLATFORM_IO_PIN_GET: retval = PIO_Get( pin ) & pinmask ? 1 : 0; break; case PLATFORM_IO_PIN_PULLUP: pin->pio->PIO_PPUER = pinmask; break; case PLATFORM_IO_PIN_NOPULL: pin->pio->PIO_PPUDR = pinmask; break; default: retval = 0; break; } return retval; }
/** * @brief Interrupt handler for Button 2 - decrements the counter value */ static void ISR_Bp2(void) { // Check if the button has been pressed if (!PIO_Get(&pinPB2)) { //wait until button gets released while (!PIO_Get(&pinPB2)) ; counter = ((counter-1)%8); } }
uint8_t stepper_get_step_mode(void) { bool usm[2] = {PIO_Get(&pin_usm[0]), PIO_Get(&pin_usm[1])}; if(!usm[0] && !usm[1]) { return STEP_MODE_FULL; } else if(usm[0] && !usm[1]) { return STEP_MODE_HALF; } else if(!usm[0] && usm[1]) { return STEP_MODE_QUARTER; } else { return STEP_MODE_EIGTH; } }
static void prvConfigureVBus( void ) { const Pin xVBusPin = PIN_USB_VBUS; const uint32_t ulPriority = 7; /* Highest. */ /* Configure PIO to generate an interrupt on status change. */ PIO_Configure( &xVBusPin, 1 ); PIO_ConfigureIt( &xVBusPin ); /* Ensure interrupt is disabled before setting the mode and installing the handler. The priority of the tick interrupt should always be set to the lowest possible. */ AIC->AIC_SSR = ID_PIOE; AIC->AIC_IDCR = AIC_IDCR_INTD; AIC->AIC_SMR = AIC_SMR_SRCTYPE_EXT_POSITIVE_EDGE | ulPriority; AIC->AIC_SVR = ( uint32_t ) prvVBusISRHandler; /* Start with the interrupt clear. */ AIC->AIC_ICCR = AIC_ICCR_INTCLR; PIO_EnableIt( &xVBusPin ); AIC_EnableIT( ID_PIOE ); /* Check current level on VBus */ if( PIO_Get( &xVBusPin ) != pdFALSE ) { /* If VBUS present, force the connect */ USBD_Connect(); } else { USBD_Disconnect(); } }
//------------------------------------------------------------------------------ /// Interrupt service routine for the PIT. Debounces the wake-up pin input. //------------------------------------------------------------------------------ static void ISR_Pit(void) { static unsigned long debounceCounter = DEBOUNCE_TIME; unsigned long pisr = 0; // Read the PISR pisr = PIT_GetStatus() & AT91C_PITC_PITS; if (pisr != 0) { // Read the PIVR. It acknowledges the IT PIT_GetPIVR(); } // Button released if (PIO_Get(&pinWakeUp)) { debounceCounter = DEBOUNCE_TIME; } // Button still pressed else { debounceCounter--; } // End of debounce time if (debounceCounter == 0) { debounceCounter = DEBOUNCE_TIME; PIT_DisableIT(); AT91C_BASE_PITC->PITC_PIMR &= ~AT91C_PITC_PITEN; COMPOSITEDDriver_RemoteWakeUp(); } }
//------------------------------------------------------------------------------ /// Interrupt service routine for the system tick. Debounces the wake-up pin input. //------------------------------------------------------------------------------ void SysTick_Handler(void) { // Button released if (PIO_Get(&pinWakeUp)) { debounceCounter = DEBOUNCE_TIME; } // Button still pressed else { debounceCounter--; } // End of debounce time if (debounceCounter == 0) { // Disable debounce timer SysTick_Configure(1, BOARD_MCK/1000, 0); debounceCounter = DEBOUNCE_TIME; HIDDMouseDriver_RemoteWakeUp(); } }
//updates position value if turning clockwise void ENCODER_ISR_UP (const Pin *encoder_pin_cw ) { //only updates on change from high to low if(!PIO_Get(encoder_pin_cw)){ encoder_position_output++; } }
void wifi_data_poll(void) { while((PIO_Get(&extension_pins[WIFI_DATA_RDY]) || (!wifi_ringbuffer_is_empty())) && (wifi_buffer_size_recv == 0)) { wifi_data_next(WIFI_LOW_LEVEL_SPI_IDLE_CHAR, true); } }
int Accel_Z_ST(void) { char value[2]; while(!PIO_Get(&(accIntPins[0]))){nop();} // wait for data ready pin ReadAccelData(0x2C|(1<<7), value, 2); // ST chip return ((signed char)value[1]<<8)|value[0]; }
void usb_isr_vbus(const Pin *pin) { // Check current level on VBus if (PIO_Get(&pin_usb_detect)) { USBD_Connect(); } else { USBD_Disconnect(); } }
void TC0_IrqHandler(void) { // acknowledge interrupt tc_channel_interrupt_ack(&ENCODER_TC_CHANNEL); if(led_is_on(LED_STD_RED)) { led_off(LED_STD_RED); } else { led_on(LED_STD_RED); } uint8_t encoder_value = PIO_Get(&pin_encoder_a); encoder_value |= (PIO_Get(&pin_encoder_b) << 1); int8_t add = encoder_table[encoder_value_last][encoder_value]; encoder_count += add; encoder_count_external += add; encoder_value_last = encoder_value; }
bool wifi_data_next_handle_ringbuffer(char *ndata, bool transceive) { if(transceive) { if(wifi_buffer_size_recv != 0) { if(PIO_Get(&extension_pins[WIFI_DATA_RDY])) { if(!wifi_ringbuffer_is_full()) { *ndata = wifi_low_level_write_byte(WIFI_LOW_LEVEL_SPI_IDLE_CHAR); wifi_ringbuffer_add(*ndata); } } return false; } else { if(wifi_ringbuffer_is_empty()) { if(PIO_Get(&extension_pins[WIFI_DATA_RDY])) { *ndata = wifi_low_level_write_byte(WIFI_LOW_LEVEL_SPI_IDLE_CHAR); return true; } else { return false; } } else { wifi_ringbuffer_get(ndata); return true; } } } else { if(wifi_buffer_size_recv != 0) { // What should we do if ringbuffer is full here? if(wifi_ringbuffer_is_full()) { logwifie("Ringbuffer full, possibly overflow\n\r"); } wifi_ringbuffer_add(*ndata); return false; } // We can handle new data now, but we first have to use up ringbuffer if(!wifi_ringbuffer_is_empty()) { char tmp; wifi_ringbuffer_get(&tmp); wifi_ringbuffer_add(*ndata); *ndata = tmp; } return true; } return true; }
//------------------------------------------------------------------------------ /// Interrupt service routine for the remote wake-up pin. Starts the debouncing /// sequence. //------------------------------------------------------------------------------ static void WakeUpHandler(const Pin *pin) { TRACE_DEBUG("Wake-up handler\n\r"); // Check current level on the remote wake-up pin if (!PIO_Get(&pinWakeUp)) { ConfigurePit(); } }
static void AccelCallback(void) { #ifdef STACCEL char value[6]; while(!PIO_Get(&(accIntPins[0]))){nop();} // wait for data ready pin ReadAccelData(0x28|(1<<7),value,6); // ST chip g_AccelX = ((signed char)value[1]<<8)|value[0]; g_AccelY = ((signed char)value[3]<<8)|value[2]; g_AccelZ = ((signed char)value[5]<<8)|value[4]; #else char value[3]; if(g_idle) return; while(!PIO_Get(&(accIntPins[0]))){nop();} // wait for data ready pin ReadAccelData(0x6,value,3); g_AccelX = (signed char)value[0]; g_AccelY = (signed char)value[1]; g_AccelZ = (signed char)value[2]; #endif }
//------------------------------------------------------------------------------ /// Configures the VBus pin to trigger an interrupt when the level on that pin /// changes. //------------------------------------------------------------------------------ static void VBus_Configure( void ) { // Configure PIO PIO_Configure(&pinVbus, 1); PIO_ConfigureIt(&pinVbus, ISR_Vbus); PIO_EnableIt(&pinVbus); // Check current level on VBus if (PIO_Get(&pinVbus)) // if VBUS present, force the connect USBD_Connect(); }
/** * PIO interrupt service routine. Checks if the smartcard has been connected * or disconnected. */ static void ISR_PioSmartCard(const Pin *pPin) { /* Check all pending interrupts */ if ((pinSmartCard.pio->PIO_ISR & pinSmartCard.mask) != 0) { /* Check current level on pin */ if (PIO_Get(&pinSmartCard ) == 0) printf("-I- Smartcard inserted\n\r"); else printf("-I- Smartcard removed\n\r"); } }
static void prvVBusISRHandler( void ) { const Pin xVBusPin = PIN_USB_VBUS; /* Check current level on VBus to detect a connect/disconnect. */ if( PIO_Get( &xVBusPin ) != 0 ) { USBD_Connect(); } else { USBD_Disconnect(); } }
//------------------------------------------------------------------------------ /// PIO interrupt service routine. Checks if the smartcard has been connected /// or disconnected. //------------------------------------------------------------------------------ static void ISR_PioSmartCard(const Pin *pPin) { // Check current level on pin if (PIO_Get(&pinSmartCard) == 0) { printf("-H- Insert\n\r"); CCID_Insertion(); } else { printf("-H- Removal\n\r"); CCID_Removal(); } }
void dc_check_error_callbacks(void) { const uint16_t external_voltage = dc_get_external_voltage(); const uint16_t stack_voltage = dc_get_stack_voltage(); // Under Voltage if external voltage is below minimum voltage (regardless // of stack voltage), or if external voltage is zero and stack voltage is // below minimum voltage if((dc_message_tick_counter % 1000 == 0 && dc_enabled) && ((external_voltage > DC_VOLTAGE_EPSILON && external_voltage < dc_minimum_voltage) || (external_voltage < DC_VOLTAGE_EPSILON && stack_voltage > DC_VOLTAGE_EPSILON && stack_voltage < dc_minimum_voltage))) { UnderVoltageCallback uvs; com_make_default_header(&uvs, com_info.uid, sizeof(UnderVoltageCallback), FID_UNDER_VOLTAGE); uvs.voltage = external_voltage < DC_VOLTAGE_EPSILON ? stack_voltage : external_voltage; send_blocking_with_timeout(&uvs, sizeof(UnderVoltageCallback), com_info.current); led_on(LED_STD_RED); // If there is no under voltage, we are currently enabled and the // status flag is low: There is a short-circuit or over-temperature // -> Emergency Shutdown } else if(!PIO_Get(&pin_status_flag) && dc_enabled && dc_mode == DC_MODE_DRIVE_BRAKE) { dc_emergency_shutdown_counter++; // Wait for DC_MAX_EMERGENCY_SHUTDOWN ms until callback is emitted if(dc_emergency_shutdown_counter >= DC_MAX_EMERGENCY_SHUTDOWN) { EmergencyShutdownCallback ess; com_make_default_header(&ess, com_info.uid, sizeof(EmergencyShutdownCallback), FID_EMERGENCY_SHUTDOWN); send_blocking_with_timeout(&ess, sizeof(EmergencyShutdownCallback), com_info.current); dc_disable(); dc_emergency_shutdown_counter = 0; dc_led_error_reason |= DC_LED_ERROR_SHUTDOWN; led_on(LED_STD_RED); } } else { dc_emergency_shutdown_counter = 0; if(!(dc_led_error_reason & DC_LED_ERROR_SHUTDOWN) && (dc_tick_counter % 1000 == 0)) { led_off(LED_STD_RED); } } }
//------------------------------------------------------------------------------ /// Detect if SD card is connected //------------------------------------------------------------------------------ static unsigned char CardIsConnected(unsigned char slot) { if (slot == 0) { #if defined(BOARD_SD_PIN_CD) PIO_Configure(&pinCardDetect, 1); return PIO_Get(&pinCardDetect) ? 0 : 1; #else return 1; #endif } if (slot == 1) { #if defined(BOARD_SD_MCI1_PIN_CD) PIO_Configure(&pinCardDetect1, 1); return PIO_Get(&pinCardDetect1) ? 0 : 1; #else return 1; #endif } return 0; }
void AccelCalibration(void) { #ifdef STACCEL return; #else char value[9]; short avgOffset[3]; int i,j; g_idle = 1; // put the callback funciton on idle so it doesn't interrupt // set the Accelerometer to measurement mode value[0] = 5; WriteAccelData(0x16, value, 1); // clear out the old calibration values first!!!!!!!! g_calValues[0] = 0; g_calValues[1] = 0; g_calValues[2] = 0; value[0] = value[1] = value[2] = value[3] = value[4] = value[5] = 0; WriteAccelData(0x10, value, 6); usleep(200); for(j=0;j<10;j++){ // get a set of readings from the Accelerometer while(!PIO_Get(&(accIntPins[0]))){nop();} // wait for data ready pin ReadAccelData(0x0,value,9); // read in 9 bytes for both 10bit and 8bit for(i=0;i<3;i++){ avgOffset[i] = value[i*2+1]<<8; avgOffset[i] |= value[i*2]; if(avgOffset[i] > 512) avgOffset[i] -=1024; g_calValues[i] += avgOffset[i]; // sum up values } usleep(100000); } // take the average g_calValues[0] /= 10; g_calValues[1] /= 10; g_calValues[2] /= 10; // multiply the offsets by 2, stated in the datasheet unsure why has to do with 1/2 LSB g_calValues[0] = -g_calValues[0]*2; g_calValues[1] = -g_calValues[1]*2; g_calValues[2] = (64 - g_calValues[2])*2; // write data to offset register AccelSetCalibration(); g_idle = 0; // turn back on the callback #endif }
//------------------------------------------------------------------------------ /// Checks if the device is write protected. /// \return 1 if protected. //------------------------------------------------------------------------------ static unsigned char CardIsProtected(unsigned char slot) { if (slot == 0) { #ifdef BOARD_SD_PIN_WP PIO_Configure(&pinMciWriteProtect, 1); return (PIO_Get(&pinMciWriteProtect) != 0); #else return 0; #endif } if (slot == 1) { #ifdef BOARD_SD_MCI1_PIN_WP PIO_Configure(&pinMciWriteProtect1, 1); return (PIO_Get(&pinMciWriteProtect1) != 0); #else return 0; #endif } return 0; }
//----------------------------------------------------------------------------- /// Monitor keyboard buttons & Update key status in HID driver //----------------------------------------------------------------------------- static void HIDDKeyboardProcessKeys(void) { unsigned int i; unsigned char pressedKeys[NUM_KEYS]; unsigned char pressedKeysSize = 0; unsigned char releasedKeys[NUM_KEYS]; unsigned char releasedKeysSize = 0; // Monitor buttons for (i=0; i < PIO_LISTSIZE(pinsPushButtons); i++) { // Check if button state has changed unsigned char isButtonPressed = PIO_Get(&(pinsPushButtons[i])); if (isButtonPressed != keyStatus[i]) { // Update button state if (!isButtonPressed) { // Key has been pressed TRACE_INFO("-I- Key %u has been pressed\n\r", i); keyStatus[i] = 0; pressedKeys[pressedKeysSize] = keyCodes[i]; pressedKeysSize++; HIDDKeyboardDriver_RemoteWakeUp(); } else { // Key has been released TRACE_INFO("-I- Key %u has been released\n\r", i); keyStatus[i] = 1; releasedKeys[releasedKeysSize] = keyCodes[i]; releasedKeysSize++; } } } // Update key status in the HID driver if necessary if ((pressedKeysSize != 0) || (releasedKeysSize != 0)) { unsigned char status; do { status = HIDDKeyboardDriver_ChangeKeys(pressedKeys, pressedKeysSize, releasedKeys, releasedKeysSize); } while (status != USBD_STATUS_SUCCESS); } }
/** * Monitor keyboard buttons & Update key status in HID driver */ static void HIDDKeyboardProcessKeys(void) { uint32_t i; uint8_t pressedKeys[NUM_KEYS]; uint8_t pressedKeysSize = 0; uint8_t releasedKeys[NUM_KEYS]; uint8_t releasedKeysSize = 0; /* Monitor buttons */ for (i=0; i < PIO_LISTSIZE(pinsPushButtons); i++) { /* Check if button state has changed */ uint8_t isButtonPressed = PIO_Get(&(pinsPushButtons[i])); if (isButtonPressed != keyStatus[i]) { /* Update button state */ if (!isButtonPressed) { /* Key has been pressed */ TRACE_INFO("-I- Key %u has been pressed\n\r", (unsigned int)i); keyStatus[i] = 0; pressedKeys[pressedKeysSize] = keyCodes[i]; pressedKeysSize++; HIDDKeyboard_RemoteWakeUp(); } else { /* Key has been released */ TRACE_INFO("-I- Key %u has been released\n\r", (unsigned int)i); keyStatus[i] = 1; releasedKeys[releasedKeysSize] = keyCodes[i]; releasedKeysSize++; } } } /* Update key status in the HID driver if necessary */ if ((pressedKeysSize != 0) || (releasedKeysSize != 0)) { uint8_t status; do { status = HIDDKeyboard_ChangeKeys(pressedKeys, pressedKeysSize, releasedKeys, releasedKeysSize); } while (status != USBD_STATUS_SUCCESS); } }
//------------------------------------------------------------------------------ /// Handles interrupts coming from PIO controllers. //------------------------------------------------------------------------------ static void ISR_VBus(const Pin *pPin) { // Check current level on VBus if (PIO_Get(&pinVbus)) { TRACE_INFO("VBUS conn\n\r"); USBD_Connect(); } else { TRACE_INFO("VBUS discon\n\r"); USBD_Disconnect(); } }
extern int digitalRead( uint32_t ulPin ) { if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) { return LOW ; } if ( PIO_Get( g_APinDescription[ulPin].pPort, PIO_INPUT, g_APinDescription[ulPin].ulPin ) == 1 ) { return HIGH ; } return LOW ; }
unsigned int Get_Port_Detect( void ) { unsigned char i = 0 ; unsigned int value = 0 ; for( i=9; i<=12; i++ ) { value <<= 1; value +=PIO_Get( &pinsGpios[i] ); } return value; }
unsigned int Get_Switches( void ) { unsigned char i = 0 ; unsigned int value = 0 ; for( i=0; i<PIO_LISTSIZE( pinsSwitches ); i++ ) { value <<= 1; value +=PIO_Get( &pinsSwitches[i] ); } return value; }
//------------------------------------------------------------------------------ /// Interrupt service routine for the remote wake-up pin. Starts the debouncing /// sequence. //------------------------------------------------------------------------------ static void WakeUpHandler(const Pin *pin) { TRACE_DEBUG("Wake-up handler\n\r"); // Check current level on the remote wake-up pin if (!PIO_Get(&pinWakeUp)) { #if defined(at91sam3uek) ConfigureSysTick(); #else ConfigurePit(); #endif } }
static void do_abort() { if (state_getState() == STOP) { // acknowledge error const Pin stop = PIN_STOP; if (PIO_Get(&stop)) { state_reset(); OK(); } else NOK(); } else { state_reset(); OK(); } }