// 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); }
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; }
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 }
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 ); }
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; } } }
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); } }
void SX1276InitIo( void ) { ioport_configure_pin(SX1276_MOSI_PIN, IOPORT_DIR_OUTPUT); ioport_configure_pin(SX1276_MISO_PIN, IOPORT_DIR_INPUT); ioport_configure_pin(SX1276_SCK_PIN, IOPORT_DIR_OUTPUT); ioport_configure_pin(SX1276_RxTx_PIN, IOPORT_DIR_OUTPUT); ioport_set_pin_high(SX1276_RxTx_PIN); // Initializing of GPS SPI - via USART //*************************************************************************/ //ioport_set_pin_dir(SX1276_CS_PIN, IOPORT_DIR_OUTPUT ); ioport_configure_pin(SX1276_CS_PIN, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH ); //*************************************************************************// //ioport_set_pin_dir(SX1276_RxTx_PIN, IOPORT_DIR_OUTPUT ); //ioport_configure_pin(SX1276_RxTx_PIN, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH ); //ioport_set_pin_dir(SX1276_RESET_PIN, IOPORT_DIR_OUTPUT ); ioport_configure_pin(SX1276_RESET_PIN, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); //NIRQ //ioport_set_pin_dir(SX1276_DI0_PIN, IOPORT_DIR_INPUT ); ioport_configure_pin(SX1272_DI0_PIN, IOPORT_DIR_INPUT); // ioport_configure_pin(SX1276_DIO1_PIN, IOPORT_DIR_INPUT); // ioport_configure_pin(SX1276_DIO2_PIN, IOPORT_DIR_INPUT); // spi_master_init(&SPIC); // spi_master_setup_device(&SPIC, &spi_device_conf, SPI_MODE_0, 8000000, 0); //Max 10 MHz // spi_enable(&SPIC); }
int main(void) { pmic_init(); board_init(); sysclk_init(); sleepmgr_init(); cpu_irq_enable(); #if (BOARD == XMEGA_A3BU_XPLAINED) /* The status LED must be used as LED2, so we turn off * the green led which is in the same packaging. */ ioport_set_pin_high(LED3_GPIO); #endif /* * Unmask clock for TIMER_EXAMPLE */ tc_enable(&TIMER_EXAMPLE); /* * Configure interrupts callback functions for TIMER_EXAMPLE * overflow interrupt, CCA interrupt and CCB interrupt */ tc_set_overflow_interrupt_callback(&TIMER_EXAMPLE, example_ovf_interrupt_callback); tc_set_cca_interrupt_callback(&TIMER_EXAMPLE, example_cca_interrupt_callback); tc_set_ccb_interrupt_callback(&TIMER_EXAMPLE, example_ccb_interrupt_callback); /* * Configure TC in normal mode, configure period, CCA and CCB * Enable both CCA and CCB channels */ tc_set_wgm(&TIMER_EXAMPLE, TC_WG_NORMAL); tc_write_period(&TIMER_EXAMPLE, TIMER_EXAMPLE_PERIOD); tc_write_cc(&TIMER_EXAMPLE, TC_CCA, TIMER_EXAMPLE_PERIOD / 2); tc_write_cc(&TIMER_EXAMPLE, TC_CCB, TIMER_EXAMPLE_PERIOD / 4); tc_enable_cc_channels(&TIMER_EXAMPLE,(enum tc_cc_channel_mask_enable_t)(TC_CCAEN | TC_CCBEN)); /* * Enable TC interrupts (overflow, CCA and CCB) */ tc_set_overflow_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO); tc_set_cca_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO); tc_set_ccb_interrupt_level(&TIMER_EXAMPLE, TC_INT_LVL_LO); /* * Run TIMER_EXAMPLE at TIMER_EXAMPLE_PERIOD(31250Hz) resolution */ tc_set_resolution(&TIMER_EXAMPLE, TIMER_EXAMPLE_PERIOD); do { /* Go to sleep, everything is handled by interrupts. */ sleepmgr_enter_sleep(); } while (1); }
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(); gfx_mono_init(); // Enable back light of display ioport_set_pin_high(LCD_BACKLIGHT_ENABLE_PIN); // 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 * - VCC voltage reference * - 200 kHz maximum clock rate * - manual conversion triggering * - temperature sensor enabled * - callback function */ adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12, ADC_REF_VCC); adc_set_clock_rate(&adc_conf, 200000UL); adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0); adc_enable_internal_input(&adc_conf, ADC_INT_TEMPSENSE); adc_write_configuration(&ADCA, &adc_conf); adc_set_callback(&ADCA, &adc_handler); /* Configure ADC channel 0: * - single-ended measurement from temperature sensor * - interrupt flag set on completed conversion * - interrupts disabled */ adcch_set_input(&adcch_conf, ADCCH_POS_PIN1, ADCCH_NEG_NONE, 1); adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE); adcch_enable_interrupt(&adcch_conf); adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf); // Enable the ADC and start the first conversion. adc_enable(&ADCA); adc_start_conversion(&ADCA, ADC_CH0); do { // Sleep until ADC interrupt triggers. sleepmgr_enter_sleep(); } while (1); }
/** * \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); } }
int main(void) { board_init(); // Initialize graphics library gfx_mono_init(); // Enable backlight ioport_set_pin_high(LCD_BACKLIGHT_ENABLE_PIN); gfx_mono_draw_string("Tests successful: \r\n1 2 3 4 5", 0, 0, &sysfont); if (test_erase() == STATUS_OK) { gfx_mono_draw_string("OK", 0, 2 * SYSFONT_HEIGHT + 1, &sysfont); } else { gfx_mono_draw_string("ERR", 0, 2 * SYSFONT_HEIGHT + 1, &sysfont); } if (test_write() == STATUS_OK) { gfx_mono_draw_string("OK", 4 * SYSFONT_WIDTH, 2 * SYSFONT_HEIGHT + 1, &sysfont); } else { gfx_mono_draw_string("ERR", 4 * SYSFONT_WIDTH, 2 * SYSFONT_HEIGHT + 1, &sysfont); } if (test_atomic_write() == STATUS_OK) { gfx_mono_draw_string("OK", 8 * SYSFONT_WIDTH, 2 * SYSFONT_HEIGHT + 1, &sysfont); } else { gfx_mono_draw_string("ERR", 8 * SYSFONT_WIDTH, 2 * SYSFONT_HEIGHT + 1, &sysfont); } if (test_split_write() == STATUS_OK) { gfx_mono_draw_string("OK", 12 * SYSFONT_WIDTH, 2 * SYSFONT_HEIGHT + 1, &sysfont); } else { gfx_mono_draw_string("ERR", 12 * SYSFONT_WIDTH, 2 * SYSFONT_HEIGHT + 1, &sysfont); } if (test_erase_bytes() == STATUS_OK) { gfx_mono_draw_string("OK", 16 * SYSFONT_WIDTH, 2 * SYSFONT_HEIGHT + 1, &sysfont); } else { gfx_mono_draw_string("ERR", 16 * SYSFONT_WIDTH, 2 * SYSFONT_HEIGHT + 1, &sysfont); } while (true) {} }
/** * \brief Main application routine * - Initializes the board and LCD display * - Initialize ADC ,to read ADC offset and configure for oversampling * - If number of sample Reached to total number of oversample required, * call function to start process on oversampled ADC readings */ int main( void ) { /* * Initialize basic features for the AVR XMEGA family. * - PMIC is needed to enable all interrupt levels. * - Board init for setting up GPIO and board specific features. * - Sysclk init for configuring clock speed and turning off unused * peripherals. * - Sleepmgr init for setting up the basics for the sleep manager, */ board_init(); sysclk_init(); pmic_init(); sleepmgr_init(); /* Initialize ST7565R controller and LCD display */ gfx_mono_init(); /* Display headings on LCD for oversampled result */ gfx_mono_draw_string("Oversampled", 0, 0, &sysfont); /* Display headings on LCD for normal result */ gfx_mono_draw_string("Normal", 80, 0, &sysfont); /* Initialize ADC ,to read ADC offset and configure ADC for oversampling **/ init_adc(); /* Enable global interrupt */ cpu_irq_enable(); /* Switch ON LCD back light */ ioport_set_pin_high(NHD_C12832A1Z_BACKLIGHT); /* Set LCD contrast */ st7565r_set_contrast(ST7565R_DISPLAY_CONTRAST_MIN); /* Continuous Execution Loop */ while (1) { /* * Check if number of sample reached to total Number of * oversample required by checking status of * adc_oversampled_flag */ if (adc_oversampled_flag == true) { /* Reset the adc_oversampled_flag */ adc_oversampled_flag = false; /* Process all received ADC samples and calculate analog * input */ adc_oversampled(); } } }
int main(void) { board_init(); #if (BOARD == XMEGA_A3BU_XPLAINED) /* Turn off status LED and back light, * as it is used to show success of tests. */ ioport_set_pin_high(LED3_GPIO); #endif if (test_atomic_write_app_table() == STATUS_OK) { /* Toggle LED to indicate success */ gpio_toggle_pin(LED_PIN_0); } if (test_split_write_app_table() == STATUS_OK) { /* Toggle LED to indicate success */ gpio_toggle_pin(LED_PIN_1); } if (test_atomic_write_boot() == STATUS_OK) { /* Toggle LED to indicate success */ gpio_toggle_pin(LED_PIN_2); } if (test_split_write_boot() == STATUS_OK) { /* Toggle LED to indicate success */ gpio_toggle_pin(LED_PIN_3); } #if (BOARD == XMEGA_A3BU_XPLAINED) /* Turn on the LCD back light to show that we are done */ ioport_set_pin_high(LCD_BACKLIGHT_ENABLE_PIN); #else /* Turn on LED 7 to show that we are done */ gpio_toggle_pin(LED_PIN_7); #endif while (true) {} }
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; }
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); } }
// 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); }
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); }
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 }
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 }
// 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); }
/** * \brief Deselect given device on the SPI bus * * Calls board chip deselect. * * \param spi Base address of the SPI instance. * \param device SPI device */ void spi_deselect_device(volatile void *spi, struct spi_device *device) { ioport_set_pin_high(device->id); }
static void PowerUpInternalAnalogSensor(void){ ioport_set_pin_high(INTERNAL_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); } }
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); }
/** * \brief Deselect given device on the SPI bus * * Calls board chip deselect. * * \param spi Base address of the SPI instance. * \param device SPI device * * \pre SPI device must be selected with spi_select_device() first */ void spi_deselect_device(SPI_t *spi, struct spi_device *device) { ioport_set_pin_high(device->id); }
static void PowerUpVFC(void){ ioport_set_pin_high(VFC_SUPPLY); }
void usart_spi_deselect_device(USART_t *usart, struct usart_spi_device *device) { ioport_set_pin_high(device->id); }
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); }