/** * \brief Configures one or more pin(s) of a PIO controller as outputs, with the * given default value. Optionally, the multi-drive feature can be enabled * on the pin(s). * * \param pPio Pointer to a PIO controller. * \param dwMask Bitmask indicating which pin(s) to configure. * \param defaultValue Default level on the pin(s). * \param enableMultiDrive Indicates if the pin(s) shall be configured as open-drain. * \param enablePullUp Indicates if the pin shall have its pull-up activated. */ extern void PIO_SetOutput( Pio* pPio, uint32_t dwMask, uint32_t dwDefaultValue, uint32_t dwMultiDriveEnable, uint32_t dwPullUpEnable ) { PIO_DisableInterrupt( pPio, dwMask ) ; PIO_PullUp( pPio, dwMask, dwPullUpEnable ) ; /* Enable multi-drive if necessary */ if ( dwMultiDriveEnable ) { pPio->PIO_MDER = dwMask ; } else { pPio->PIO_MDDR = dwMask ; } /* Set default value */ if ( dwDefaultValue ) { pPio->PIO_SODR = dwMask ; } else { pPio->PIO_CODR = dwMask ; } /* Configure pin(s) as output(s) */ pPio->PIO_OER = dwMask ; pPio->PIO_PER = dwMask ; }
/** * \brief Configures one or more pin(s) or a PIO controller as inputs. Optionally, * the corresponding internal pull-up(s) and glitch filter(s) can be enabled. * * \param pPio Pointer to a PIO controller. * \param dwMask Bitmask indicating which pin(s) to configure as input(s). * \param dwAttribute . */ extern void PIO_SetInput( Pio* pPio, uint32_t dwMask, uint32_t dwAttribute ) { PIO_DisableInterrupt( pPio, dwMask ) ; PIO_PullUp( pPio, dwMask, dwAttribute & PIO_PULLUP ) ; /* Enable Input Filter if necessary */ if ( dwAttribute & (PIO_DEGLITCH | PIO_DEBOUNCE) ) { pPio->PIO_IFER = dwMask ; } else { pPio->PIO_IFDR = dwMask ; } /* Enable de-glitch or de-bounce if necessary */ if ( dwAttribute & PIO_DEGLITCH ) { pPio->PIO_SCIFSR = dwMask ; } else { if ( dwAttribute & PIO_DEBOUNCE ) { pPio->PIO_SCIFSR = dwMask ; } } /* Configure pin as input */ pPio->PIO_ODR = dwMask ; pPio->PIO_PER = dwMask ; }
/** * \brief Configures one or more pin(s) or a PIO controller as inputs. Optionally, * the corresponding internal pull-up(s) and glitch filter(s) can be enabled. * * \param pPio Pointer to a PIO controller. * \param dwMask Bitmask indicating which pin(s) to configure as input(s). * \param dwAttribute . */ extern void PIO_SetInput( Pio* pPio, uint32_t dwMask, uint32_t dwAttribute ) { PIO_DisableInterrupt( pPio, dwMask ) ; PIO_PullUp( pPio, dwMask, dwAttribute & PIO_PULLUP ) ; /* Enable Input Filter if necessary */ if ( dwAttribute & (PIO_DEGLITCH | PIO_DEBOUNCE) ) { pPio->PIO_IFER = dwMask ; } else { pPio->PIO_IFDR = dwMask ; } /* Enable de-glitch or de-bounce if necessary */ #if (defined _SAM3S_) || (defined _SAM3S8_) || (defined _SAM3N_) if ( dwAttribute & PIO_DEGLITCH ) { pPio->PIO_IFSCDR = dwMask ; } else { if ( dwAttribute & PIO_DEBOUNCE ) { pPio->PIO_IFSCER = dwMask ; } } #elif (defined _SAM3U_) || (defined _SAM3XA_) if ( dwAttribute & PIO_DEGLITCH ) { pPio->PIO_SCIFSR = dwMask ; } else { if ( dwAttribute & PIO_DEBOUNCE ) { pPio->PIO_SCIFSR = dwMask ; } } #else #error "The specified chip is not supported." #endif /* Configure pin as input */ pPio->PIO_ODR = dwMask ; pPio->PIO_PER = dwMask ; }
extern void digitalWrite( uint32_t ulPin, uint32_t ulVal ) { /* Handle */ if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) { return ; } if ((g_pinStatus[ulPin] & 0xF) == PIN_STATUS_PWM) { pinMode(ulPin, OUTPUT); } g_pinStatus[ulPin] = (g_pinStatus[ulPin] & 0x0F) | (ulVal << 4) ; if ( PIO_GetOutputDataStatus( g_APinDescription[ulPin].pPort, g_APinDescription[ulPin].ulPin ) == 0 ) { PIO_PullUp( g_APinDescription[ulPin].pPort, g_APinDescription[ulPin].ulPin, ulVal ) ; } else { PIO_SetOutput( g_APinDescription[ulPin].pPort, g_APinDescription[ulPin].ulPin, ulVal, 0, PIO_PULLUP ) ; } }
extern void digitalWrite( uint32_t ulPin, uint32_t ulVal ) { /* Handle */ if ( g_APinDescription[ulPin].ulPinType == PIO_NOT_A_PIN ) { return ; } /* Added by Y.Ishioka */ if( ulPin == 13 ) { led_set( ulVal & 0x01 ) ; return ; } if ( PIO_GetOutputDataStatus( g_APinDescription[ulPin].pPort, g_APinDescription[ulPin].ulPin ) == 0 ) { PIO_PullUp( g_APinDescription[ulPin].pPort, g_APinDescription[ulPin].ulPin, ulVal ) ; } else { PIO_SetOutput( g_APinDescription[ulPin].pPort, g_APinDescription[ulPin].ulPin, ulVal, 0, PIO_PULLUP ) ; } }
/** * * \return 1 if the pins have been configured properly; otherwise 0. */ extern uint32_t PIO_Configure( Pio* pPio, const EPioType dwType, const uint32_t dwMask, const uint32_t dwAttribute ) { /* Configure pins */ switch ( dwType ) { case PIO_PERIPH_A : case PIO_PERIPH_B : #if (defined _SAM3S_) || (defined _SAM3S8_) || (defined _SAM3N_) case PIO_PERIPH_C : case PIO_PERIPH_D : #endif /* (defined _SAM3S_) || (defined _SAM3S8_) || (defined _SAM3N_) */ /* Put the pin under control of peripheral */ PIO_SetPeripheral( pPio, dwType, dwMask ) ; /* Disable interrupts on the pin(s) */ PIO_DisableInterrupt( pPio, dwMask ) ; /* Enable Pullup */ PIO_PullUp( pPio, dwMask, (dwAttribute & PIO_PULLUP) ) ; break; case PIO_INPUT : PIO_SetInput( pPio, dwMask, dwAttribute ) ; break; case PIO_OUTPUT_0 : case PIO_OUTPUT_1 : PIO_SetOutput( pPio, dwMask, (dwType == PIO_OUTPUT_1), (dwAttribute & PIO_OPENDRAIN) ? 1 : 0, (dwAttribute & PIO_PULLUP) ? 1 : 0); break ; default : return 0 ; } return 1 ; }
//Set up the PWM on the pin specified with the alignment and polarity specified customPWM::customPWM(int channel, bool alignment, bool polarity) { period = globPeriod; _lowFreq = period/3; //Calculate the low duty cycle setting, for the motor controller currentDuty = DUTY; motorDuty = DUTY; //Get the appropriate number if a pin is given switch(channel) { case 34: _channel = 0; break; case 36: _channel = 1; break; case 38: _channel = 2; break; case 40: _channel = 3; break; case 9: _channel = 4; break; case 8: _channel = 5; break; case 7: _channel = 6; break; case 6: _channel = 7; break; default: assert(0); //You didn't give it a correct pin } //If its not enabled yet, then enable it with the default settings if(!isEnabled) { customPWMinit(globFrequency, globPeriod); } //Arduino code to set the correct settings on the pins PIO_SetPeripheral(port[_channel], type[_channel], pin[_channel]); //Set the peripheral of the pin and remove control from PIO PIO_DisableInterrupt(port[_channel], pin[_channel]); //Disable the interupt on the pin, not needed since it is disabled in SetPeripheral PIO_PullUp(port[_channel], pin[_channel], (conf[_channel] & PIO_PULLUP)); //Enables the pullup resistor on the pin //Configure the channels //Needed to access the registers because the Arduino code disables the changing of alignment or polarity unsigned int holder = (0xBu & 0xF) | (polarity<<9) | (alignment<<8); point->PWM_CH_NUM[_channel].PWM_CMR = holder; //Set the period (max value) PWMC_SetPeriod(point, _channel, period); //Set the default duty cycle PWMC_SetDutyCycle(point, _channel, DUTY); //Enable the pin and make it go PWMC_EnableChannel(point, _channel); }