Exemplo n.º 1
0
/*============================================================================
Name    :   BitBangSPI_Send_Byte
------------------------------------------------------------------------------
Purpose :   Send and Read one byte using BitBangSPI Interface
Input   :   Data to send to slave
Output  :   Data read from slave
Notes   :	Called from BitBangSPI_Send_Message in this file
============================================================================*/
uint8_t BitBangSPI_Send_Byte(uint8_t c)
{
    unsigned bit;
    
    for (bit = 0; bit < 8; bit++) {
        /* write MOSI on trailing edge of previous clock */
        if (c & 0x80)
            REG( PORT, SPI_BB_MOSI )->OVRS =  ( 1 << MOSI_BB );
        else
            REG( PORT, SPI_BB_MOSI )->OVRC =  ( 1 << MOSI_BB );

        c <<= 1;

        /* half a clock cycle before leading/rising edge */
        cpu_delay_us(3u, F_CPU); /* SHOULD BE ~3uS. */

        REG( PORT, SPI_BB_SCK )->OVRS = ( 1 << SCK_BB );

        /* half a clock cycle before trailing/falling edge */
         cpu_delay_us(3u, F_CPU); /* SHOULD BE ~3uS. */

        /* read MISO on trailing edge */
        c |= ((REG( PORT, SPI_BB_MISO )->PVR >> MISO_BB) & 0x01);
        REG( PORT, SPI_BB_SCK )->OVRC = ( 1 << SCK_BB );
    }

    REG( PORT, SPI_BB_MOSI )->OVRC =  ( 1 << MOSI_BB );

    cpu_delay_us(18u, F_CPU);/* SHOULD BE ~18uS. */


    return c;
}
Exemplo n.º 2
0
void LEDPWM()
{
	LED_Init();

	LED_Off(LED0|LED1|LED2|LED3);

	unsigned int counter = 0;

	LED_On(LED0);

	while (1)
	{
		if (counter == 0)
		LED_On(LED1|LED2|LED3);

		if (counter == 3)
		LED_Off(LED1);
		if (counter == 2)
		LED_Off(LED2);
		if (counter == 1)
		LED_Off(LED3);

		counter = (counter + 1) % 4;
		cpu_delay_us(500, FOSCRC);
	}
}
Exemplo n.º 3
0
void LEDPulsating()
{
	LED_Init();

	LED_Off(LED0|LED1|LED2|LED3);

	unsigned int counter = 0;
	unsigned int div = 8;
	unsigned int cycle = 0, ccycle = 0;
	unsigned int cycle_counter = 0;

	while (1)
	{
		if (cycle)
		{
			if (counter == 0)
			LED_On(LED0|LED1|LED2|LED3);
			else if (counter == cycle)
			LED_Off(LED0|LED1|LED2|LED3);
		}

		counter = (counter + 1) % div;
		cycle_counter = (cycle_counter + 1) % (256/div);

		if (!cycle_counter)
		{
			ccycle = (ccycle + 1) % (2*div);
			if (ccycle < div)
			cycle = ccycle;
			else
			cycle = 2*div - 1 - ccycle;
		}
		cpu_delay_us(2000/div, FOSCRC);
	}
}
Exemplo n.º 4
0
/*! \brief Read device register content.
 *
 * \param reg_index Register address.
 * \returns Register content.
 */
uint8_t at42qt1060_read_reg(uint8_t reg_index)
{
uint8_t data;
twi_package_t twi_package;

	twi_package.chip = AT42QT1060_TWI_ADDRESS;
	twi_package.addr_length = 0;
	twi_package.buffer = &reg_index;
	twi_package.length = 1;
	while(twi_master_write(AT42QT1060_TWI, &twi_package)!=TWI_SUCCESS);
	/* We need a delay here to make this work although this is not
	* specified in the datasheet.
	* Also there seems to be a bug in the TWI module or the driver
	* since some delay here (code or real delay) adds about 500us
	* between the write and the next read cycle.
	*/
	cpu_delay_us(20, cpu_hz);

	twi_package.chip = AT42QT1060_TWI_ADDRESS;
	twi_package.addr_length = 0;
	twi_package.buffer = &data;
	twi_package.length = 1;
	while(twi_master_read(AT42QT1060_TWI, &twi_package)!=TWI_SUCCESS);

	return data;
}
/*============================================================================
*  Name    :   BitBangSPI_Send_Byte
*  ------------------------------------------------------------------------------
*  Purpose :   Send and Read one byte using BitBangSPI Interface
*  Input   :   Data to send to slave
*  Output  :   Data read from slave
*  Notes   :	Called from BitBangSPI_Send_Message in this file
*  ============================================================================*/
uint8_t BitBangSPI_Send_Byte(uint8_t c)
{
	uint32_t bit;
	bool i;
	for (bit = 0u; bit < 8u; bit++) {
		/* write MOSI on trailing edge of previous clock */
		if (c & 0x80u) {
			port_pin_set_output_level(CONCAT(PIN_P, SPI_BB_MOSI,
					MOSI_BB), 1u);
		} else {
			port_pin_set_output_level(CONCAT(PIN_P, SPI_BB_MOSI,
					MOSI_BB), 0u);
		}

		c <<= 1u;

		/* half a clock cycle before leading/rising edge */
		cpu_delay_us(1u); /* SHOULD BE ~3uS. */
		port_pin_set_output_level(CONCAT(PIN_P, SPI_BB_SCK, SCK_BB), 1u);

		/* half a clock cycle before trailing/falling edge */
		cpu_delay_us(1u); /* SHOULD BE ~3uS. */

		/* read MISO on trailing edge */
		i = (port_pin_get_input_level(CONCAT(PIN_P, SPI_BB_MISO, MISO_BB)));
		if (i == true) {
			c |= (0x01u);
		} else {
		}

		port_pin_set_output_level(CONCAT(PIN_P, SPI_BB_SCK, SCK_BB), 0u);
	}

	port_pin_set_output_level(CONCAT(PIN_P, SPI_BB_MOSI, MOSI_BB), 0u);

	cpu_delay_us(50u); /* SHOULD BE ~18uS. */

	return c;
}
Exemplo n.º 6
0
//the main function
int main(void) {

	//change the clock to osc0 = 12MHz
	AVR32_PM.oscctrl0 = AVR32_PM_OSCCTRL0_MODE_CRYSTAL_G3<<AVR32_PM_OSCCTRL0_MODE_OFFSET | 3<<AVR32_PM_OSCCTRL0_STARTUP_OFFSET;
	AVR32_PM.mcctrl |= AVR32_PM_MCCTRL_OSC0EN_MASK;
	while (!(AVR32_PM.poscsr & AVR32_PM_POSCSR_OSC0RDY_MASK));
	AVR32_PM.mcctrl |= AVR32_PM_MCCTRL_MCSEL_OSC0;

	LED_Init();
	Switch_Init();

	LED_Off(LED0|LED1|LED2|LED3);

	unsigned int counter = 0;
	unsigned int div = 32;
	unsigned int cycle = 0, ccycle = 0;
	unsigned int cycle_counter = 0;

	while (1)
	{
		if (cycle)
		{
			if (counter == 0)
			LED_On(LED0|LED1|LED2|LED3);
			else if (counter == cycle)
			LED_Off(LED0|LED1|LED2|LED3);
		}

		counter = (counter + 1) % div;
		cycle_counter = (cycle_counter + 1) % (2048/div);

		if (!cycle_counter)
		{
			ccycle = (ccycle + 1) % (2*div);
			if (ccycle < div)
			cycle = ccycle;
			else
			cycle = 2*div - 1 - ccycle;
		}
		cpu_delay_us(2000/div, FOSC0);
	}

	return 0;
}
Exemplo n.º 7
0
int main(void) {

	// Switch the main clock to the external oscillator 0
	pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
	
	board_init();

	Switch_Init();

	LED_Off(LED0|LED1|LED2|LED3);

	unsigned int counter = 0;
	unsigned int div = 32;
	unsigned int cycle = 0, ccycle = 0;
	unsigned int cycle_counter = 0;

	while (1)
	{
		if (cycle)
		{
			if (counter == 0)
			LED_On(LED0|LED1|LED2|LED3);
			else if (counter == cycle)
			LED_Off(LED0|LED1|LED2|LED3);
		}

		counter = (counter + 1) % div;
		cycle_counter = (cycle_counter + 1) % (2048/div);

		if (!cycle_counter)
		{
			ccycle = (ccycle + 1) % (2*div);
			if (ccycle < div)
			cycle = ccycle;
			else
			cycle = 2*div - 1 - ccycle;
		}
		cpu_delay_us(2000/div, FOSC0);
	}

	return 0;
}
Exemplo n.º 8
0
void delay_us(unsigned long delay)
{
  cpu_delay_us(delay, s_fcpu_hz);
}
Exemplo n.º 9
0
void usb_suspend_action(void)
{
#if (UC3A || UC3B)
   volatile avr32_pm_t *pm = &AVR32_PM;
   unsigned long clock;
   unsigned long cksel_save;
   U32 startup;

   pm->AWEN.usb_waken = 1;

   // Save the clock source, Clock Select (CKSEL) and startup time since we will reconfigure them
   clock = pm_get_clock(&AVR32_PM);
   startup = AVR32_PM.OSCCTRL0.startup;
   pm_cksel_get(&AVR32_PM, &cksel_save);

   // Setup a long startup time. We will control it with a software delay.
   // Moreover, this also ensures that the PLL0 will not be blocked due to a
   // bad oscillator state/frequency in the case of a short startup (0 or 1.1 ms).
   AVR32_PM.OSCCTRL0.startup = AVR32_PM_OSCCTRL0_STARTUP_2048_RCOSC;

   // Switch on the internal RC oscillator
   pm_switch_to_clock(&AVR32_PM, AVR32_PM_MCSEL_SLOW);

   // Reset the Clock Select in order to ensure we are running on
   // the internal RC osc at full speed.
   pm_cksel(&AVR32_PM
   , 0, 0 // PBA
   , 0, 0 // PBB
   , 0, 0 // HSB
   );

   // If there is a chance that any PB write operations are incomplete, the CPU
   // should perform a read operation from any register on the PB bus before
   // executing the sleep instruction.
   AVR32_INTC.ipr[0];  // Dummy read

   // Entering into Sleep mode.
   SLEEP(AVR32_PM_SMODE_STATIC);
   // Exit from Sleep mode.

   // The host may start using the USB 10 ms (FS) or 3 ms (HS) after the RESUME.
   // Waits for oscillator startup with a time lower than USB requirements.
   cpu_delay_us(1400, AVR32_PM_RCOSC_FREQUENCY);

   // Restore CKSEL
   pm_cksel_set(&AVR32_PM, cksel_save);

   // Shorten the startup time, since OSC0 is ready.
   AVR32_PM.OSCCTRL0.startup = AVR32_PM_OSCCTRL0_STARTUP_0_RCOSC;

   // Switch back to the previous clock.
   pm_switch_to_clock(&AVR32_PM, clock);

   // Restore startup time
   AVR32_PM.OSCCTRL0.startup = startup;
   pm->AWEN.usb_waken = 0;
#else
  Enable_global_interrupt();
  //! @todo Implement this on the silicon version
  //Enter_power_down_mode();  // For example...
#if 0
   pm->AWEN.usb_waken = 1;

   // If there is a chance that any PB write operations are incomplete, the CPU
   // should perform a read operation from any register on the PB bus before
   // executing the sleep instruction.
   AVR32_INTC.ipr[0];  // Dummy read

   // Entering into Sleep mode.
   SLEEP(AVR32_PM_SMODE_STATIC);
   // Exit from Sleep mode.

   pm->AWEN.usb_waken = 0;
#endif
#endif
}
void delay_us(unsigned long delay,unsigned long fcpu_hz)
{
  cpu_delay_us(delay, fcpu_hz);
}
Exemplo n.º 11
0
/*
 * @brief spi_txrx SPI transmit and receive the data
 *
 * @param bufTx Transmit Buffer
 * @param lenbufTx Length of transmit buffer
 * @param returnType
 * @param bufRx SPI Rx Buffer
 * @param Max Length of Rx Buffer size
 * @param isFirst 
 * @param isLast
 *
 */
void spi_txrx(uint8_t*  bufTx,
			 uint8_t      lenbufTx,
			 spi_return_t returnType,
			 uint8_t*     bufRx,
			 uint8_t      maxLenBufRx,
			 spi_first_t  isFirst,
             spi_last_t   isLast)
{
   uint8_t spi_byte;
#ifdef SPI_IN_INTERRUPT_MODE	
   /* Disable the interrupts */
   cpu_irq_disable();
#endif
   
   /* register spi frame to send */
   spi_vars.pNextTxByte      =  bufTx;
   spi_vars.numTxedBytes     =  0;
   spi_vars.txBytesLeft      =  lenbufTx;
   spi_vars.returnType       =  returnType;
   spi_vars.pNextRxByte      =  bufRx;
   spi_vars.maxRxBytes       =  maxLenBufRx;
   spi_vars.isFirst          =  isFirst;
   spi_vars.isLast           =  isLast;
   
   /* SPI is now busy */
   spi_vars.busy             =  1;
   
   /* lower CS signal to have slave listening */
   if (spi_vars.isFirst==SPI_FIRST) {
	  /* Start SPI transaction by pulling SEL low */
	 port_pin_set_level(AT86RFX_SPI_CS, SET_LOW);
	 cpu_delay_us(10);

   }
   
#ifdef SPI_IN_INTERRUPT_MODE
   /* implementation 1. use a callback function when transaction finishes */
   
   /* Send the Read command byte */	
   
   spi_write_buffer_wait(&master, spi_vars.pNextTxByte, 1);
   
   /* re-enable interrupts */
   cpu_irq_enable();
#else
   /* implementation 2. busy wait for each byte to be sent */
   /* send all bytes */
   while (spi_vars.txBytesLeft > 0) 
   {
	   /* write next byte to TX buffer */
	   /* Send the Read command byte */
		spi_byte = *spi_vars.pNextTxByte;
		spi_txrx_data(&spi_byte);

	   /* save the byte just received in the RX buffer */
	   switch (spi_vars.returnType) {
		   case SPI_FIRSTBYTE:
		   if (spi_vars.numTxedBytes==0) {				
				*spi_vars.pNextRxByte = spi_byte;			   
		   }
		   break;
		   case SPI_BUFFER:		   
		   *spi_vars.pNextRxByte = spi_byte;
		   spi_vars.pNextRxByte++;
		   break;
		   case SPI_LASTBYTE:		   
		   *spi_vars.pNextRxByte = spi_byte;
		   break;
	   }
	   /* one byte less to go */
	   spi_vars.pNextTxByte++;
	   spi_vars.numTxedBytes++;
	   spi_vars.txBytesLeft--;
   }
   
   /* put CS signal high to signal end of transmission to slave */
   if (spi_vars.isLast==SPI_LAST) 
   {
	   /* Stop the SPI transaction by setting SEL high */
	   port_pin_set_level(AT86RFX_SPI_CS, SET_HIGH);
   }
   
   /* SPI is not busy anymore */
   spi_vars.busy             =  0;
#endif			 
}