Example #1
0
void nrf24l01_RX_config_slave(void)
{
	ioport_set_pin_low(nrf24l01S_CE);
	
	SPI_MasterSSHigh(&PORTC, PIN4_bm);
	delay_us(20);
	SPI_MasterSSLow(&PORTC, PIN4_bm);
	delay_us(20);
	
	rf_writebuf_slave(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
	rf_writereg_slave(WRITE_REG + EN_AA, 0x01);//enable autoactive 0x01
	rf_writereg_slave(WRITE_REG + EN_RXADDR, 0x01);
	rf_writereg_slave(WRITE_REG + RF_CH, 40);
	rf_writereg_slave(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH);
	rf_writereg_slave(WRITE_REG + RF_SETUP, 0x09);
	rf_writereg_slave(WRITE_REG + CONFIG, 0x0f);
	
	//SPI_MasterSSLow(ssPort, PIN4_bm);
	ioport_set_pin_high(nrf24l01S_CE);
	delay_us(150);//at least 130us
	
	PORT_ConfigurePins( &PORTC,
	0x01,	//set pin PK0 as input 'IRQ';
	false,
	false,
	PORT_OPC_TOTEM_gc,
	PORT_ISC_FALLING_gc );//set falling edge as trigger;
	
	PORT_SetPinsAsInput( &PORTC, 0x01 );
	
	/* Configure Interrupt0 to have medium interrupt level, triggered by pin 0. */
	PORT_ConfigureInterrupt0( &PORTC, PORT_INT0LVL_MED_gc, 0x01 );		
}
uint8_t Radio_Transmit(radiopacket_t* payload, RADIO_TX_WAIT wait)
{
	//if (block && transmit_lock) while (transmit_lock);
	//if (!block && transmit_lock) return 0;
	uint8_t len = 32;

	// indicate that the driver is transmitting.
    transmit_lock = 1;

	// disable the radio while writing to the Tx FIFO.
    ioport_set_pin_low (CE);

	set_tx_mode();

    // for auto-ack to work, the pipe0 address must be set to the Tx address while the radio is transmitting.
    // The register will be set back to the original pipe 0 address when the TX_DS or MAX_RT interrupt is asserted.
    set_register(RX_ADDR_P0, (uint8_t*)tx_address, ADDRESS_LENGTH);

    // transfer the packet to the radio's Tx FIFO for transmission
    send_instruction(W_TX_PAYLOAD, payload, NULL, len);

    // start the transmission.
    ioport_set_pin_high (CE);

    if (wait == RADIO_WAIT_FOR_TX)
    {
    	while (transmit_lock);
    	return tx_last_status;
    }

    return RADIO_TX_SUCCESS;
}
Example #3
0
void CheckButtons(void)
{
	uint8_t statmp;
	uint8_t checkack;
	if (!ioport_get_value(GPIO_PUSH_BUTTON_4))
	{
		delay_ms(20);
		if (!ioport_get_value(GPIO_PUSH_BUTTON_4))
		{
			ioport_set_pin_low(LED4_GPIO);
			TX_BUF[0] = DATA;
			nrf24l01_TX_config_master(TX_BUF);
			Check_ACK_master(1); 

			delay_ms(100);
			ioport_set_pin_high(LED4_GPIO);
			//nrf24l01_RX_config();
			
			while(!ioport_get_value(GPIO_PUSH_BUTTON_4));	//wait the button the release
			DATA <<= 1;
			if(!DATA)
			   DATA = 0x01;
			   
		}
	}
}
Example #4
0
int main (void)
{
	/* Insert system clock initialization code here (sysclk_init()). */
	board_init();
	sysclk_init();
	delay_init(sysclk_get_cpu_hz());
	
	//gpio_configure_pin(PORTA5,1);
	
	ioport_init();
		
	ioport_set_pin_dir(LED_GREEN,IOPORT_DIR_OUTPUT);
	ioport_set_pin_dir(LED_RED,IOPORT_DIR_OUTPUT);
	
	/* Insert application code here, after the board has been initialized. */
	
	ioport_set_pin_high(LED_RED);
	ioport_set_pin_low(LED_GREEN);
	
	
	
	
	//gpio_set_pin_high(PORTA5);
	
	while(true){
		ioport_toggle_pin(LED_GREEN);
		ioport_toggle_pin(LED_RED);
		delay_ms(500);
	}
	
}
Example #5
0
void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{

    // #if OPTIMIZE_O0
    //
    //    spi_select_device(&SPIC, &spi_device_conf);
    //    spi_put(&SPIC, addr | 0x80);
    //    spi_write_packet(&SPIC,buffer,size);
    //    spi_deselect_device(&SPIC, &spi_device_conf);
    //
    // #elif  OPTIMIZE_OS

    ioport_set_pin_low(SX1276_CS_PIN);

    SpiInOut(addr | 0x80);

    for( uint16_t i = 0; i < size; i++ )
    {
        // 		usart_spi_write_single(SSD1306_USART_SPI,tx[i]);
        // 		rx[i]=usart_spi_get(SSD1306_USART_SPI);
        SpiInOut(buffer[i]);
    }

    ioport_set_pin_high(SX1276_CS_PIN);


    // #else
    // 	#error Neni definovana optimalizace
    // #endif


}
/**
 * \brief This function is used to indicate a test failure
 */
void test_fail_indication(void)
{
	while (1) {
		ioport_set_pin_low(LED_CRIT);
		delay_ms(200);
		ioport_set_pin_high(LED_CRIT);
		delay_ms(200);
	}
}
Example #7
0
// Initialize Pin and SPI like specified into config file
void nrf24l01_init()
{
	// Init SPI pins
	ioport_set_pin_dir(CONF_NRF24L01_SS_PIN, IOPORT_DIR_INPUT);
	ioport_set_pin_mode(CONF_NRF24L01_SS_PIN, IOPORT_MODE_PULLUP);
	ioport_set_pin_dir(CONF_NRF24L01_MOSI_PIN, IOPORT_DIR_OUTPUT);
	ioport_set_pin_mode(CONF_NRF24L01_MOSI_PIN, IOPORT_MODE_PULLUP);
	ioport_set_pin_high(CONF_NRF24L01_MOSI_PIN);
	ioport_set_pin_dir(CONF_NRF24L01_MISO_PIN, IOPORT_DIR_INPUT);
	ioport_set_pin_dir(CONF_NRF24L01_SCK_PIN, IOPORT_DIR_OUTPUT);
	ioport_set_pin_high(CONF_NRF24L01_SCK_PIN);
	
	// Init nrf24l01 pins
	ioport_set_pin_dir(CONF_NRF24L01_CE_PIN, IOPORT_DIR_OUTPUT);
	ioport_set_pin_dir(CONF_NRF24L01_CSn_PIN, IOPORT_DIR_OUTPUT);
	ioport_set_pin_dir(CONF_NRF24L01_IRQ_PIN, IOPORT_DIR_INPUT);

	ioport_set_pin_low(CONF_NRF24L01_CE_PIN);
	spi_deselect_device(&CONF_NRF24L01_SPI, &nrf24l01_spi_device_conf);
	
	spi_master_init(&CONF_NRF24L01_SPI);
	spi_master_setup_device(&CONF_NRF24L01_SPI, &nrf24l01_spi_device_conf, SPI_MODE_0, CONF_NRF24L01_CLOCK_SPEED, 0);
	spi_enable(&CONF_NRF24L01_SPI);
	
	// Wait nrf24l01 power on reset
	delay_ms(Tpor);
	
	nrf24l01_power_off();
	
	// Reset registers to default state
	nrf24l01_write_register(NRF24L01_CONFIG_REG, NRF24L01_CONFIG_REG_DEF);
	nrf24l01_write_register(NRF24L01_STATUS_REG, NRF24L01_STATUS_REG_DEF);
	// TODO: reset all registers
	
	// Config parameters sets in CONF_NRF24L01
	nrf24l01_set_power_amplifier(CONF_NRF24L01_PA);
	nrf24l01_set_data_rate(CONF_NRF24L01_DATA_RATE);
	nrf24l01_set_crc(CONF_NRF24L01_CRC);
	nrf24l01_set_addr_len(CONF_NRF24L01_ADDR_LEN);

	uint8_t nrf24l01_rx_addr[5] = { CONF_NRF24L01_RX_ADDR };
	uint8_t nrf24l01_tx_addr[5] = { CONF_NRF24L01_TX_ADDR };

	nrf24l01_set_rx_addr(nrf24l01_rx_addr);
	nrf24l01_set_tx_addr(nrf24l01_tx_addr);
	
	nrf24l01_write_register(NRF24L01_RF_CH_REG, CONF_NRF24L01_RF_CHANNEL);
	nrf24l01_write_register(NRF24L01_RX_PW_P0_REG, CONF_NRF24L01_PAYLOAD);
	nrf24l01_write_register(NRF24L01_RX_PW_P1_REG, CONF_NRF24L01_PAYLOAD);
	
	// Power-up (Power Down -> Standby-I)
	uint8_t configReg = nrf24l01_read_register(NRF24L01_CONFIG_REG);
	nrf24l01_write_register(NRF24L01_CONFIG_REG, configReg | NRF24L01_PWR_UP_BM);
	
	delay_us(Tpd2stby);
}
Example #8
0
// Power Up in RX Mode
void nrf24l01_primary_rx()
{
	PTX = 0;
	ioport_set_pin_low(CONF_NRF24L01_CE_PIN);
	
	uint8_t configReg = nrf24l01_read_register(NRF24L01_CONFIG_REG);
	nrf24l01_write_register(NRF24L01_CONFIG_REG, configReg | NRF24L01_PWR_UP_BM | NRF24L01_PRIM_RX_BM);
	
	ioport_set_pin_high(CONF_NRF24L01_CE_PIN);
	delay_us(Tstby2a);
	
	//uint8_t statusReg = nrf24l01_read_register(NRF24L01_STATUS_REG);
	nrf24l01_write_register(NRF24L01_STATUS_REG, NRF24L01_TX_DS_BM | NRF24L01_MAX_RT_BM);
}
Example #9
0
void SX1276WriteRxTx( uint8_t txEnable )
{
    if( txEnable != 0 )
    {
        // High in TX
        //ioport_set_pin_level(Sx1276_RxTxSwitch_PIN, true);
        ioport_set_pin_high(SX1276_RxTx_PIN);
    }
    else
    {
        //ioport_set_pin_level(Sx1276_RxTxSwitch_PIN, false);
        ioport_set_pin_low(SX1276_RxTx_PIN);
    }
}
RADIO_RX_STATUS Radio_Receive(radiopacket_t* buffer)
{
	uint8_t len = 32;
	uint8_t status;
	uint8_t pipe_number;
	uint8_t doMove = 1;
	RADIO_RX_STATUS result;

	transmit_lock = 0;

	ioport_set_pin_low (CE);

    status = get_status();
	pipe_number =  (status & 0xE) >> 1;

	if (pipe_number == RADIO_PIPE_EMPTY)
	{
		result = RADIO_RX_FIFO_EMPTY;
		doMove = 0;
	}

	if (rx_pipe_widths[pipe_number] > len)
	{
		// the buffer isn't big enough, so don't copy the data.
		result = RADIO_RX_INVALID_ARGS;
		doMove = 0;
	}

	if (doMove)
	{
		// Move the data payload into the local
		send_instruction(R_RX_PAYLOAD, (uint8_t*)buffer, (uint8_t*)buffer, rx_pipe_widths[pipe_number]);

		status = get_status();
		pipe_number =  (status & 0xE) >> 1;

		if (pipe_number != RADIO_PIPE_EMPTY)
			result = RADIO_RX_MORE_PACKETS;
		else
			result = RADIO_RX_SUCCESS;
	}

	ioport_set_pin_high (CE);

	transmit_lock = 0;

	//release_radio();

	return result;
}
Example #11
0
void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{

    ioport_set_pin_low(SX1276_CS_PIN);

    SpiInOut(addr & 0x7F);

    for( uint16_t i = 0; i < size; i++ )
    {
        // 		usart_spi_write_single(SSD1306_USART_SPI,tx[i]);
        // 		rx[i]=usart_spi_get(SSD1306_USART_SPI);
        buffer[i]=SpiInOut(0xFF);
    }

    ioport_set_pin_high(SX1276_CS_PIN);

}
void Radio_Init()
{
	transmit_lock = 0;

	// disable radio during config
	ioport_set_pin_low (CE);
		
	/* IRQ */
	// Enable radio interrupt.  This interrupt is triggered when data are received and when a transmission completes.
	ioport_configure_port_pin(&PORTC, PIN2_bm, IOPORT_PULL_UP | IOPORT_DIR_INPUT | IOPORT_SENSE_FALLING);
	PORTC.INT0MASK = PIN2_bm;
	PORTC.INTCTRL = PORT_INT0LVL_LO_gc;
	PMIC.CTRL |= PMIC_LOLVLEN_bm;

	/* CE */
	ioport_configure_port_pin(&PORTC, PIN3_bm, IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT);

	// A 10.3 ms delay is required between power off and power on states (controlled by 3.3 V supply).
	_delay_ms(11);
	
	/* Set up SPI */
	/* Slave select */
	ioport_configure_port_pin(&PORTC, PIN4_bm, IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT);

	/* MOSI, MISO, SCK */
	ioport_configure_port_pin(&PORTC, PIN5_bm, IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT);
	ioport_configure_port_pin(&PORTC, PIN6_bm, IOPORT_DIR_INPUT);
	ioport_configure_port_pin(&PORTC, PIN7_bm, IOPORT_INIT_HIGH | IOPORT_DIR_OUTPUT);
	
	spi_master_init(&SPID);
	spi_master_setup_device(&SPID, &spi_device_conf, SPI_MODE_0, 1000000, 0);
	spi_enable(&SPID);

	// Configure the radio registers that are not application-dependent.
	configure_registers();

	// A 1.5 ms delay is required between power down and power up states (controlled by PWR_UP bit in CONFIG)
	_delay_ms(2);

	// enable radio as a receiver
	ioport_set_pin_high (CE);
}
/**
 * \brief Initialize the LCD controller
 *
 * Call this function to initialize the hardware interface and the LCD
 * controller. When initialization is done the display is turned on and ready
 * to receive data.
 */
void st7565r_init(void)
{
	// Do a hard reset of the LCD display controller
	st7565r_hard_reset();

	// Initialize the interface
	st7565r_interface_init();

	// Set the A0 pin to the default state (command)
	ioport_set_pin_low(ST7565R_A0_PIN);

	// The column address is set to increasing
	st7565r_write_command(ST7565R_CMD_ADC_NORMAL);

	// Non-inverted display
	st7565r_display_invert_disable();

	// The common mode scan direction is reversed COM31->COM0
	st7565r_write_command(ST7565R_CMD_REVERSE_SCAN_DIRECTION);

	// Set the voltage bias ratio to 1/6
	st7565r_write_command(ST7565R_CMD_LCD_BIAS_1_DIV_6_DUTY33);

	// Set booster circuit, voltage regulator and voltage follower all to on
	st7565r_write_command(ST7565R_CMD_POWER_CTRL_ALL_ON);

	// Set the booster ratio to 2X,3X,4X
	st7565r_write_command(ST7565R_CMD_BOOSTER_RATIO_SET);
	st7565r_write_command(ST7565R_CMD_BOOSTER_RATIO_2X_3X_4X);

	// Set voltage resistor ratio to 1
	st7565r_write_command(ST7565R_CMD_VOLTAGE_RESISTOR_RATIO_1);

	/* Set contrast to min value, no need to check return value as the contrast
	is set to the defined min*/
	st7565r_set_contrast(ST7565R_DISPLAY_CONTRAST_MIN);

	// Turn on the display
	st7565r_display_on();
}
Example #14
0
void nrf24l01_TX_config_slave(uint8_t * writebuf)
{
	ioport_set_pin_low(nrf24l01S_CE);
	
	SPI_MasterSSHigh(&PORTC, PIN4_bm);
	delay_us(20);
	SPI_MasterSSLow(&PORTC, PIN4_bm);
	delay_us(20);
	
	rf_writebuf_slave(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);
	rf_writebuf_slave(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
	rf_writebuf_slave(WR_TX_PLOAD, writebuf, TX_PLOAD_WIDTH);
	rf_writereg_slave(WRITE_REG + EN_AA, 0x01);//enable autoactive 0x01
	rf_writereg_slave(WRITE_REG + EN_RXADDR, 0x01);
	rf_writereg_slave(WRITE_REG + SETUP_RETR, 0x0a);
	rf_writereg_slave(WRITE_REG + RF_CH, 40);
	rf_writereg_slave(WRITE_REG + RF_SETUP, 0x09);
	rf_writereg_slave(WRITE_REG + CONFIG, 0x0e);
	
	ioport_set_pin_high(nrf24l01S_CE);
	delay_us(12);//at least 10us
}
Example #15
0
void nrf24l01_RX_config_master(void)
{
	ioport_set_pin_low(nrf24l01M_CE);
	
	SPI_MasterSSHigh(&PORTF, PIN4_bm);
	delay_us(20);
	SPI_MasterSSLow(&PORTF, PIN4_bm);	
	delay_us(20);
	
	rf_writebuf_master(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH);
	rf_writereg_master(WRITE_REG + EN_AA, 0x01);//enable autoactive 0x01
	rf_writereg_master(WRITE_REG + EN_RXADDR, 0x01);
	rf_writereg_master(WRITE_REG + RF_CH, 40);
	rf_writereg_master(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH);
	//rf_writereg(WRITE_REG + RF_SETUP, 0x07);//1Mbps, 0dBm
	rf_writereg_master(WRITE_REG + RF_SETUP, 0x09);
	rf_writereg_master(WRITE_REG + CONFIG, 0x0f);
	
	//SPI_MasterSSLow(ssPort, PIN4_bm);	
	ioport_set_pin_high(nrf24l01M_CE);
	delay_us(150);//at least 130us
}
Example #16
0
// Sends a data package to the default address. Be sure to send the correct
// amount of bytes as configured as payload on the receiver.
void nrf24l01_send_data(uint8_t* value)
{
	// Wait until last packet is sent if in TX mode
	while (PTX) {
		uint8_t status = nrf24l01_read_register(NRF24L01_STATUS_REG);
		// Packet transmitted or Max retransmission
		if((status & (NRF24L01_TX_DS_BM  | NRF24L01_MAX_RT_BM))){
			PTX = 0;
			break;
		}
	}

	nrf24l01_write_register(NRF24L01_STATUS_REG, NRF24L01_TX_DS_BM | NRF24L01_MAX_RT_BM);

	ioport_set_pin_low(CONF_NRF24L01_CE_PIN);
	nrf24l01_primary_tx();
	spi_select_device(&CONF_NRF24L01_SPI, &nrf24l01_spi_device_conf);
	spi_write_single_packet(&CONF_NRF24L01_SPI, NRF24L01_W_TX_PAYLOAD);
	spi_write_packet(&CONF_NRF24L01_SPI, value, CONF_NRF24L01_PAYLOAD);
	spi_deselect_device(&CONF_NRF24L01_SPI, &nrf24l01_spi_device_conf);
	ioport_set_pin_high(CONF_NRF24L01_CE_PIN);                     // Start transmission
	delay_us(Thce);
	delay_us(Tstby2a);
}
Example #17
0
static void PowerDownInternalAnalogSensor(void){
	ioport_set_pin_low(INTERNAL_ANALOG_SENSOR_SUPPLY);
}
int main(void)
{
	// Set the sleep mode to initially lock.
	enum sleepmgr_mode mode = SLEEPMGR_ACTIVE;
	PORT_t *port;

	board_init();
	sysclk_init();

	// Turn on LED to indicate the device is active.
	ioport_set_pin_low(LED_PIN);

	// Configure pin change interrupt for asynch. wake-up on button pin.
	ioport_configure_pin(BUTTON_PIN, IOPORT_DIR_INPUT | IOPORT_PULL_UP |
			IOPORT_FALLING);

	port = ioport_pin_to_port(BUTTON_PIN);
#if XMEGA_E
	port->INTMASK = PIN2_bm;
	port->INTCTRL = PORT_INTLVL_LO_gc;
#else
	port->INT0MASK = PIN2_bm;
	port->INTCTRL = PORT_INT0LVL_LO_gc;
#endif


	// Enable RTC with ULP as clock source.
	sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_RTC);
	CLK.RTCCTRL = CLK_RTCSRC_ULP_gc | CLK_RTCEN_bm;

	// Configure RTC for wakeup at 1.5 second period (at 256x prescaling).
	RTC.PER = 6;
	RTC.INTCTRL = RTC_OVFINTLVL_LO_gc;

	// Wait until RTC is ready before continuing.
	do { } while (RTC.STATUS & RTC_SYNCBUSY_bm);

	// Initialize the sleep manager, lock initial mode.
	sleepmgr_init();
	sleepmgr_lock_mode(mode);

	// Enable low level interrupts for wakeups to occur.
	PMIC.CTRL = PMIC_LOLVLEN_bm;

	do {
		// Delay for 3 seconds to show the device is awake.
		mdelay(3000);

		// Turn off the LED, restart the RTC and go to sleep.
		ioport_set_pin_high(LED_PIN);
		RTC.CNT = 0;
		RTC.CTRL = RTC_PRESCALER_DIV256_gc;
		do { } while (RTC.STATUS & RTC_SYNCBUSY_bm);
		sleepmgr_enter_sleep();

		// Stop the RTC and turn on the LED.
		RTC.CTRL = RTC_PRESCALER_OFF_gc;
		ioport_set_pin_low(LED_PIN);

		// Unlock current mode, then lock the next one.
		sleepmgr_unlock_mode(mode);
		if (++mode < SLEEPMGR_NR_OF_MODES) {
			sleepmgr_lock_mode(mode);
		} else {
			mode = SLEEPMGR_ACTIVE;
			sleepmgr_lock_mode(mode);
		}
	} while (1);
}
Example #19
0
int main(void)
{
	struct adc_config         adc_conf;
	struct adc_channel_config adcch_conf;

	board_init();
	sysclk_init();
	sleepmgr_init();
	irq_initialize_vectors();
	cpu_irq_enable();

	// Initialize configuration structures.
	adc_read_configuration(&ADCA, &adc_conf);
	adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);

	/* Configure the ADC module:
	 * - unsigned, 12-bit results
	 * - bandgap (1 V) voltage reference
	 * - 200 kHz maximum clock rate
	 * - manual conversion triggering
	 */
	adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12,
			ADC_REF_BANDGAP);
	adc_set_clock_rate(&adc_conf, 200000UL);
	adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);

	adc_write_configuration(&ADCA, &adc_conf);

	/* Configure ADC channel 0:
	 * - single-ended measurement from configured input pin
	 * - interrupt flag set on completed conversion
	 */
	adcch_set_input(&adcch_conf, INPUT_PIN, ADCCH_NEG_NONE,
			1);
	adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);
	adcch_disable_interrupt(&adcch_conf);

	adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

	// Enable the ADC and do one dummy conversion.
	adc_enable(&ADCA);
	adc_start_conversion(&ADCA, ADC_CH0);
	adc_wait_for_interrupt_flag(&ADCA, ADC_CH0);

	// Light up LED 1, wait for button press.
	ioport_set_pin_low(LED1_PIN);
	wait_for_button();

	// Perform oversampling of offset.
	cal_data.offset = get_mean_sample_value();

	// Light up LED 2, wait for button press.
	ioport_set_pin_low(LED2_PIN);
	wait_for_button();

	// Perform oversampling of 0.9 V for gain calibration.
	cal_data.gain = get_mean_sample_value() - cal_data.offset;

	// Turn off LEDs.
	ioport_set_pin_high(LED1_PIN);
	ioport_set_pin_high(LED2_PIN);

	// Enable interrupts on ADC channel, then trigger first conversion.
	adcch_enable_interrupt(&adcch_conf);
	adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);
	adc_start_conversion(&ADCA, ADC_CH0);

	do {
		// Sleep until ADC interrupt triggers.
		sleepmgr_enter_sleep();
	} while (1);
}
Example #20
0
static void ExternalAnalogSensorPowerCtrlInit(void){
	ioport_set_pin_dir(EXTERNAL_ANALOG_SENSOR_SUPPLY,IOPORT_DIR_OUTPUT);
	ioport_set_pin_low(EXTERNAL_ANALOG_SENSOR_SUPPLY);
}
int main(void)
{
	static uint8_t ret = 0;
	uint8_t i = 0;
	uint8_t ibuf[16] = {0};
	static uint8_t test_pattern[PATTERN_TEST_LENGTH];
	sensor_data_t sensor_data;
	twi_master_options_t opt;

	irq_initialize_vectors();

	sysclk_init();

	/* Initialize the board.
	 * The board-specific conf_board.h file contains the configuration of
	 * the board initialization.
	 */
	board_init();
	gfx_mono_init();
	ioport_set_pin_high(NHD_C12832A1Z_BACKLIGHT);
	gfx_mono_draw_string("Reading....\r\n", 0, 0, &sysfont);
	gfx_mono_generic_draw_filled_rect(0, 8, 128, 8, GFX_PIXEL_CLR);

	/* configure the pins connected to LEDs as output and set their default
	 * initial state to low (LEDs off).
	 */
	ioport_configure_pin(LED_LOW, IOPORT_DIR_OUTPUT);
	ioport_configure_pin(LED_HIGH, IOPORT_DIR_OUTPUT);
	ioport_configure_pin(LED_CRIT, IOPORT_DIR_OUTPUT);
	ioport_configure_pin(LED_NORM, IOPORT_DIR_OUTPUT);

	ioport_set_pin_low(LED_LOW);
	ioport_set_pin_low(LED_HIGH);
	ioport_set_pin_low(LED_CRIT);
	ioport_set_pin_low(LED_NORM);

	/* Configure the ALERT/EVENT pin which is
	 * routed to pin 2 of J2 on A3BU Xplained
	 * This pin can be used for polling or interrupt
	 */
	ioport_configure_pin(EVENT_PIN, IOPORT_DIR_INPUT);

	attach_device(EXAMPLE_TS_DEVICE_ADDR, EXAMPLE_TS_DEVICE);
	opt.chip = EXAMPLE_TS_DEVICE_ADDR;
	opt.speed = TWI_SPEED;

	/* Initialize TWI driver with options */
	twi_master_setup(TWI_MODULE, &opt);

	sensor_data.config_reg.value = 0;
	/* Set configuration register to 12-bis resolution */
	sensor_data.config_reg.option.RES  = AT30TS7_RES12;

	if (write_config(sensor_data.config_reg.value) != TWI_SUCCESS) {
		test_fail_indication();
	}

	/* Set the polarity of ALERT/EVENT pin to low */

	if (set_config_option(&sensor_data, AT30TS_POL, AT30TS7_POL_ACTIVE_LOW) !=
	TWI_SUCCESS) {
	test_fail_indication();
	}

	/* Read the configuration register */
	if (read_config(&sensor_data) != TWI_SUCCESS) {
		test_fail_indication();
	}

#if defined _AT30TS00_ || defined _AT30TSE002B_
	/* Set t_high limit register to +75.0000C */
	if (write_tcrit(pos, 75, 0000) != TWI_SUCCESS) {
		test_fail_indication();
	}
#endif

	/* Set t_high limit register to +50.7500C */
	if (write_temperature_high(pos, 50, 7500) != TWI_SUCCESS) {
		test_fail_indication();
	}

	/* Set t_low limit register to -25.2500C */

	/*
	 * if (write_temperature_low(neg, 25, 2500)!= TWI_SUCCESS) {
	 * test_fail_indication();
	 * }
	 */

	/* Set t_low limit register to +35.5000C */
	if (write_temperature_low(pos, 35, 5000) != TWI_SUCCESS) {
		test_fail_indication();
	}

#if defined _AT30TS00_ || defined _AT30TSE002B_
	/* Read t_crit register register */
	if (read_tcrit(&sensor_data) != TWI_SUCCESS) {
		test_fail_indication();
	}
#endif

	/* Read t_high limit register */
	if (read_temperature_high(&sensor_data) != TWI_SUCCESS) {
		test_fail_indication();
	}

	/* Read t_low register register */
	if (read_temperature_low(&sensor_data) != TWI_SUCCESS) {
		test_fail_indication();
	}

	/* Non volatile register functionality */
#if defined _AT30TS750_  || defined _AT30TSE752_ || \
	defined _AT30TSE754_ || defined _AT30TSE758_

	/* Copy volatile registers to nonvolatile registers
	 * vol configuration register  -> nonvol configuration register
	 * vol t_high register -> nonvol t_high register
	 * vol t_low  register -> nonvol t_low register
	 */
        ret = ts75_copy_vol_nonvol_register();
	if (ret != TWI_SUCCESS) {
		test_fail_indication();
	}

	/* Read the nonvol configuration register */
	if (read_nvconfig(&sensor_data) != TWI_SUCCESS) {
		test_fail_indication();
	}

	/* Read the nonvol t_high register */
	if (read_nvthigh(&sensor_data) != TWI_SUCCESS) {
		test_fail_indication();
	}

	/* Read the nonvol t_low register */
	if (read_nvtlow(&sensor_data) != TWI_SUCCESS) {
		test_fail_indication();
	}

	/* Clear vol configuration register */
	if (write_config(0x0000) != TWI_SUCCESS) {
		test_fail_indication();
	}

	/* Read the vol configuration register */
	if (read_config(&sensor_data) != TWI_SUCCESS) {
		test_fail_indication();
	}

	/* Copy nonvolatile registers to volatile registers */
	if (ts75_copy_nonvol_vol_register() != TWI_SUCCESS) {
		test_fail_indication();
	}

	/* Read the configuration register */
	if (read_config(&sensor_data) != TWI_SUCCESS) {
		test_fail_indication();
	}
#endif
        /* To avoid 'variable unused' warning */
        test_pattern[0] = ibuf[0];
        ibuf[0] = test_pattern[0];

	/* EEPROM Test */
#if defined _AT30TSE002B_  || defined _AT30TSE752_ || \
	defined _AT30TSE754_   || defined _AT30TSE758_

	/* Generate Test Pattern */
	for (i = 0; i < PATTERN_TEST_LENGTH; i++) {
		test_pattern[i] = 0x41 + i; // 'ABCD...'
	}

	/* Perform a write access & check write result */
	if ((ret = ts_write_memory(EE_TEST_ADDR, PATTERN_TEST_LENGTH,
	(void *)test_pattern)) != TWI_SUCCESS) {
		gfx_mono_draw_string("EE Write Failed ", 0, 24, &sysfont);
		test_fail_indication();
	}

	/* Allow time for EEPROM to settle */
	delay_ms(5);
	/* Clear test_pattern */
	memset(ibuf, 0, sizeof(ibuf));

	/* Perform a read access & check read result */
	if (ts_read_eeprom(EE_TEST_ADDR, PATTERN_TEST_LENGTH,
			ibuf) != TWI_SUCCESS) {
		gfx_mono_draw_string("EE Read Failed ", 0, 24, &sysfont);
		test_fail_indication();
	}

	/* Check received data against sent data */
	for (i = 0; i < PATTERN_TEST_LENGTH; i++) {
		if (ibuf[i] != test_pattern[i]) {
			gfx_mono_draw_string("EE Read mismatch ", 0, 24,
				&sysfont);
			test_fail_indication();
		}
	}

	gfx_mono_draw_string("EE Write/Read OK", 0, 24, &sysfont);
	gfx_mono_draw_string((char const*)ibuf, 0, 16, &sysfont);
#endif
	/*
	 * Temperature reading contained in struct,i.e.
	 * temperature register value = 0x3240 (+50.25C), AT30TSE758 device
	 * sensor_data.temperature.itemp = 50 //!< integer part
	 * sensor_data.temperature.ftemp = 2500 //!< fractional part
	 * sensor_data.temperature.sign = 0 //!< sign (pos(+) = 0, neg(-) = 1)
	 * sensor_data.temperature.raw_value = 0x324 //!< raw data
	 */

	char senseData[50] = {0};
	while (1) {
		/* Read temperature */
		read_temperature(&sensor_data);
		sprintf(senseData, "%d.%04d DegC",
					sensor_data.temperature.itemp,
						sensor_data.temperature.ftemp);
		gfx_mono_draw_string(senseData, 0, 8, &sysfont);
		ioport_set_pin_low(LED_NORM);
		delay_ms(200);
		ioport_set_pin_high(LED_NORM);
		delay_ms(200);
	}
}
Example #22
0
/**
 * \brief Select given device on the SPI bus
 *
 * Set device specific setting and calls board chip select.
 *
 * \param spi Base address of the SPI instance.
 * \param device SPI device
 *
 */
void spi_select_device(volatile void *spi, struct spi_device *device)
{
	ioport_set_pin_low(device->id);
}
int main( void )
{
	uint16_t i;

	/* Enable all three interrupt levels of the PMIC. */
	pmic_init();
	board_init();
	sysclk_init();
	sleepmgr_init();

	/* Assume that everything is ok*/
	success = true;

	/* Enable the AES clock. */
	sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_AES);

	/* Do AES decryption of a single block with AES interrupt handler. */
	//********************************************************
	//               DECIPHER IN MANUAL MODE:
	//  - 128bit cryptographic key and data
	//  - ECB cipher mode
	//  - XOR disable
	//  - Interrupt call back handler
	//********************************************************
	/* Generate last subkey. */
	if (!aes_lastsubkey_generate(key, lastsubkey)) {
		success = false;
	}

	/* Enable global interrupts. */
	cpu_irq_enable();

	/* Assume no interrupt is finished. */
	int_end = false;
	byte_count = 0;

	/* Set AES interrupt call back function. */
	aes_set_callback(&aes_isr_handler);

	/* Before using the AES it is recommended to do an AES software reset to
	 * put the module in known state, in case other parts of your code has
	 * accessed the AES module. */
	aes_software_reset();

	/* Set AES encryption of a single block in manual mode. */
	aes_configure(AES_DECRYPT, AES_MANUAL, AES_XOR_OFF);

	/* Enable the AES low level interrupt. */
	aes_isr_configure(AES_INTLVL_LO);

	/* Load key into AES key memory. */
	aes_set_key(lastsubkey);

	/* Load data into AES state memory. */
	aes_write_inputdata(cipher_text);

	/* Start encryption. */
	aes_start();

	do{
		/* Wait until the AES interrupt is finished. */
	} while (!int_end);

	/* Do AES encryption and decryption of multi blocks. */
	//********************************************************
	//               CIPHER IN AUTO MODE:
	//  - 128bit cryptographic key and data
	//  - CBC cipher mode
	//  - XOR on
	//  - Interrupt call back handler
	//********************************************************
	/* Assume no interrupt is finished. */
	int_end = false;
	byte_count = 0;

	/* Set AES interrupt call back function. */
	aes_set_callback(&aes_isr_cbc_encrypt_handler);

	/* Before using the AES it is recommended to do an AES software reset to
	 * put the module in known state, in case other parts of your code has
	 * accessed the AES module. */
	aes_software_reset();

	/* Load initial vector into AES state memory. */
	aes_write_inputdata(init);

	/* Set AES encryption of a single block in auto mode. */
	aes_configure(AES_ENCRYPT, AES_AUTO, AES_XOR_ON);

	/* Enable the AES low level interrupt. */
	aes_isr_configure(AES_INTLVL_LO);

	/* Load key into AES key memory. */
	aes_set_key(key);

	/* Load data into AES state memory. */
	aes_write_inputdata(data_block);
	// NOTE: since we're in auto mode, the ciphering process will start as soon
	// as the correct number of input data is written. In this case, the
	// process should start when we write the sixteenth byte.

	do{
		/* Wait until the AES interrupt is finished. */
	} while (!int_end);

	//********************************************************
	//               DECIPHER IN AUTO MODE:
	//  - 128bit cryptographic key and data
	//  - CBC cipher mode
	//  - XOR off
	//  - Interrupt call back handler
	//********************************************************
	/* Generate last subkey. */
	if (!aes_lastsubkey_generate(key, lastsubkey)) {
		success = false;
	}

	/* Assume no interrupt is finished. */
	int_end = false;
	byte_count = 0;

	/* Set AES interrupt call back function. */
	aes_set_callback(&aes_isr_cbc_decrypt_handler);

	/* Before using the AES it is recommended to do an AES software reset to
	 * put the module in known state, in case other parts of your code has
	 * accessed the AES module. */
	aes_software_reset();

	/* Set AES decryption of a single block in auto mode. */
	aes_configure(AES_DECRYPT, AES_AUTO, AES_XOR_OFF);

	/* Enable the AES low level interrupt. */
	aes_isr_configure(AES_INTLVL_LO);

	/* Load key into AES key memory. */
	aes_set_key(lastsubkey);

	/* Load data into AES state memory. */
	aes_write_inputdata(cipher_block_ans);
	// NOTE: since we're in auto mode, the ciphering process will start as soon
	// as the correct number of input data is written. In this case, the
	// process should start when we write the sixteenth byte.

	do{
		/* Wait until the AES interrupt is finished. */
	} while (!int_end);

	/* Check if decrypted answer is equal to plaintext. */
	for (i = 0; i < BLOCK_LENGTH * BLOCK_COUNT ; i++) {
		if (data_block[i] != plain_block_ans[i]){
			success = false;
		}
	}
	/* Disable the AES clock. */
	sysclk_disable_module(SYSCLK_PORT_GEN, SYSCLK_AES);

	/* Indicate final result by lighting LED. */
	if (success) {
		/* If the example ends up here every thing is ok. */
		ioport_set_pin_low(LED0_GPIO);
	} else {
		/* If the example ends up here something is wrong. */
		ioport_set_pin_low(LED1_GPIO);
	}

	while (true) {
		/* Go to sleep. */
		sleepmgr_enter_sleep();
	}
}
Example #24
0
/**
 * \brief Select given device on the SPI bus
 *
 * Set device specific setting and calls board chip select.
 *
 * \param spi Base address of the SPI instance.
 * \param device SPI device
 *
 */
void spi_select_device(SPI_t *spi, struct spi_device *device)
{
	ioport_set_pin_low(device->id);
}
Example #25
0
static void	VFCPowerCtrlInit(void){
	ioport_set_pin_dir(VFC_SUPPLY,IOPORT_DIR_OUTPUT);
	ioport_set_pin_low(VFC_SUPPLY);
}
Example #26
0
int main( void )
{
	uint8_t i;

	board_init();
	sysclk_init();
	sleepmgr_init();

	/* Assume that everything is ok */
	success = true;

	/* Enable the AES clock. */
	sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_AES);

	/* Do AES encryption and decryption of a single block. */
	//********************************************************
	//               CIPHER IN MANUAL MODE:
	//  - 128bit cryptographic key and data
	//  - ECB cipher mode
	//  - XOR disable
	//  - Polling AES State Ready Interrupt flag
	//********************************************************
	/* Before using the AES it is recommended to do an AES software reset to
	 * put the module in known state, in case other parts of your code has
	 * accessed the AES module. */
	aes_software_reset();

	/* Set AES encryption of a single block in manual mode. */
	aes_configure(AES_ENCRYPT, AES_MANUAL, AES_XOR_OFF);

	/* Disable the AES interrupt. */
	aes_isr_configure(AES_INTLVL_OFF);

	/* Load key into AES key memory. */
	aes_set_key(key);

	/* Load data into AES state memory. */
	aes_write_inputdata(plain_text);

	/* Start encryption. */
	aes_start();

	do {
		/* Wait until AES is finished or an error occurs. */
	} while (aes_is_busy());

	/* Store the result if not error. */
	if (!aes_is_error()) {
		aes_read_outputdata(single_ans);
	} else {
		success = false;
	}

	/* Check if encrypted answer is equal to cipher result. */
	for (i = 0; i < BLOCK_LENGTH ; i++ ) {
		if (cipher_text[i] != single_ans[i]) {
			success = false;
		}
	}

	//********************************************************
	//               DECIPHER IN AUTO MODE:
	//  - 128bit cryptographic key and data
	//  - ECB cipher mode
	//  - XOR disable
	//  - Polling AES State Ready Interrupt flag
	//********************************************************
	/* Generate last subkey. */
	if (!aes_lastsubkey_generate(key, lastsubkey)) {
		success = false;
	}

	/* Before using the AES it is recommended to do an AES software reset to
	 * put the module in known state, in case other parts of your code has
	 * accessed the AES module. */
	aes_software_reset();

	/* Set AES decryption of a single block in auto mode. */
	aes_configure(AES_DECRYPT, AES_AUTO, AES_XOR_OFF);

	/* Disable the AES interrupt. */
	aes_isr_configure(AES_INTLVL_OFF);

	/* Load key into AES key memory. */
	aes_set_key(lastsubkey);

	/* Load data into AES state memory. */
	aes_write_inputdata(cipher_text);
	// NOTE: since we're in auto mode, the ciphering process will start as soon
	// as the correct number of input data is written. In this case, the
	// process should start when we write the sixteenth byte.

	do {
		/* Wait until AES is finished or an error occurs. */
	} while (aes_is_busy());

	/* Store the result if not error. */
	if (!aes_is_error()) {
		aes_read_outputdata(single_ans);
	} else {
		success = false;
	}

	/* Check if decrypted answer is equal to plaintext. */
	for (i = 0; i < BLOCK_LENGTH ; i++ ) {
		if (plain_text[i] != single_ans[i]) {
			success = false;
		}
	}

	/* Disable the AES clock. */
	sysclk_disable_module(SYSCLK_PORT_GEN, SYSCLK_AES);

	/* Indicate final result by lighting LED. */
	if (success) {
		/* If the example ends up here every thing is ok. */
		ioport_set_pin_low(LED0_GPIO);
	} else {
		/* If the example ends up here something is wrong. */
		ioport_set_pin_low(LED1_GPIO);
	}

	while (true) {
		/* Go to sleep. */
		sleepmgr_enter_sleep();
	}
}
Example #27
0
int main( void )
{
	uint8_t i;

	board_init();
	sysclk_init();
	sleepmgr_init();

	/* Assume that everything is ok*/
	success = true;

	/* Enable the AES clock. */
	sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_AES);

	/* Do AES encryption of a single block with DMA support. */
	//********************************************************
	//               CIPHER IN MANUAL MODE:
	//  - 128bit cryptographic key and data
	//  - ECB cipher mode
	//  - XOR disable
	//  - DMA support for AES input and output
	//********************************************************
	/* Before using the AES it is recommended to do an AES software reset to
	 * put the module in known state, in case other parts of your code has
	 * accessed the AES module. */
	aes_software_reset();

	/* Set AES encryption of a single block in manual mode. */
	aes_configure(AES_ENCRYPT, AES_MANUAL, AES_XOR_OFF);

	/* Disable the AES interrupt. */
	aes_isr_configure(AES_INTLVL_OFF);

	/* Set KEY and write STATE using DMA channel 0. */
	aes_dma_input();

	/* Start encryption. */
	aes_start();

	do {
		/* Wait until AES is finished or an error occurs. */
	} while (aes_is_busy());

	/* Store the result if not error. */
	if (!aes_is_error()){
		/* Read STATE using DMA channel 0. */
		aes_dma_output();
	} else {
		success = false;
	}

	/* Check if encrypted answer is equal to cipher result. */
	for (i = 0; i < BLOCK_LENGTH ; i++ ) {
		if (cipher_text[i] != single_ans[i]) {
			success = false;
		}
	}

	/* Disable the AES clock. */
	sysclk_disable_module(SYSCLK_PORT_GEN, SYSCLK_AES);

	/* Indicate final result by lighting LED. */
	if (success) {
		/* If the example ends up here every thing is ok. */
		ioport_set_pin_low(LED0_GPIO);
	} else {
		/* If the example ends up here something is wrong. */
		ioport_set_pin_low(LED1_GPIO);
	}

	while (true) {
		/* Go to sleep. */
		sleepmgr_enter_sleep();
	}
}
Example #28
0
void usart_spi_select_device(USART_t *usart, struct usart_spi_device *device)
{
	ioport_set_pin_low(device->id);
}
//! \brief Main example doing DES encryption/decryption.
int main( void )
{
	uint8_t i;

	board_init();
	sleepmgr_init();

	bool success = true;

	/* Example of how to use Single DES encryption and decryption functions. */
	des_encrypt(data, single_ans, keys);
	des_decrypt(single_ans, single_ans, keys);

	/* Check if decrypted answer is equal to plaintext. */
	for (i = 0; i < DES_BLOCK_LENGTH ; i++ ){
		if (data[i] != single_ans[i]){
			success = false;
			break;
		}
	}

	if (success){

		/* Example of how to use 3DES encryption and decryption functions. */
		des_3des_encrypt(data, single_ans, keys);
		des_3des_decrypt(single_ans, single_ans, keys);

		/* Check if decrypted answer is equal to plaintext. */
		for (i = 0; i < DES_BLOCK_LENGTH ; i++ ){
			if (data[i] != single_ans[i]){
				success = false;
				break;
		 	}
		}
	}

	if (success){
		/* Example of how to use DES Cipher Block Chaining encryption and
		 * decryption functions.
		 */
		des_cbc_encrypt(data_block, cipher_block_ans, keys, init, true, DES_BLOCK_COUNT);
		des_cbc_decrypt(cipher_block_ans, block_ans, keys, init, true, DES_BLOCK_COUNT);

		/* Check if decrypted answer is equal to plaintext. */
		for (i = 1; i < (DES_BLOCK_LENGTH * DES_BLOCK_COUNT); i++ ){
			if (data_block[i] != block_ans[i]){
				success = false;
				break;
			}
		}
	}

	/* Indicate final result by lighting LED. */
	if (success) {
		/* If the example ends up here every thing is ok. */
		ioport_set_pin_low(LED0_GPIO);
	} else {
		/* If the example ends up here something is wrong. */
		ioport_set_pin_low(LED1_GPIO);
	}

	while (true) {
		/* Go to sleep. */
		sleepmgr_enter_sleep();
	}
}
Example #30
0
static void PowerDownVFC(void){
	ioport_set_pin_low(VFC_SUPPLY);
}