void rs485_init(void) { ioport_configure_pin(IOPORT_CREATE_PIN(PORTC, 3), IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH); ioport_configure_pin(IOPORT_CREATE_PIN(PORTC, 2), IOPORT_DIR_INPUT); ioport_configure_pin(IOPORT_CREATE_PIN(PORTC, 7), IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH); ioport_configure_pin(IOPORT_CREATE_PIN(PORTC, 6), IOPORT_DIR_INPUT); //rs485 enable usart_rs232_options_t usart_opt; usart_opt.baudrate = 250000; usart_opt.charlength = USART_CHSIZE_8BIT_gc; usart_opt.paritytype = USART_PMODE_DISABLED_gc; usart_opt.stopbits = true; usart_init_rs232(&RS485_1_UART,&usart_opt); usart_set_rx_interrupt_level(&RS485_1_UART,USART_INT_LVL_HI); usart_init_rs232(&RS485_2_UART,&usart_opt); usart_set_rx_interrupt_level(&RS485_2_UART,USART_INT_LVL_HI); }
void main_cdc_open(uint8_t port) { switch(port){ case 0: // COMM0 lithneProgrammer.resetMain(); uart_open(&USART_COMM0); usart_set_rx_interrupt_level(&USART_COMM0, USART_INT_LVL_HI); // receive interrupt break; case 1: // Switched to XBEE direct: close COMM1 uart_close(&USART_COMM1); // close COMM1, this also disables interrupts usart_set_rx_interrupt_level(&USART_XBEE, USART_INT_LVL_HI); break; } }
/** * Name : usart_init * * Synopsis : void usart_init (void) * * Description : Initialize the defined USARTs * * Set the Baud rate * * 8N1 (8 data bits, No Parity, 1 Stop bit) * * Enable receive interrupt * */ void usart_init (void) { usart_serial_options_t serial_options; // 8 data bits, No parity, 1 stop bit serial_options.charlength = USART_CHSIZE_8BIT_gc; serial_options.paritytype = USART_PMODE_DISABLED_gc; serial_options.stopbits = false; // Set baudrate, initialize and enable receive interrupt serial_options.baudrate = RADIO_UART_BAUDRATE; usart_serial_init (radio.USART, &serial_options); usart_set_rx_interrupt_level (radio.USART,USART_RXCINTLVL_LO_gc); serial_options.baudrate = CDHIB_UART_BAUDRATE; usart_serial_init (cdhib.USART, &serial_options); usart_set_rx_interrupt_level (cdhib.USART,USART_RXCINTLVL_LO_gc); }
void USART_L_init(void) { usart_set_mode(&Wireless_L_USART,USART_CMODE_ASYNCHRONOUS_gc); usart_format_set(&Wireless_L_USART,USART_CHSIZE_8BIT_gc,USART_PMODE_DISABLED_gc,false); usart_set_rx_interrupt_level(&Wireless_L_USART,USART_INT_LVL_MED); //usart_set_tx_interrupt_level(&Wireless_L_USART,USART_INT_LVL_MED); usart_set_baudrate(&Wireless_L_USART,USART_BUADRATE,F_CPU); usart_tx_enable(&Wireless_L_USART); usart_rx_enable(&Wireless_L_USART); }
void main_cdc_close(uint8_t port) { switch(port){ case 0: // COMM0 uart_close(&USART_COMM0); // close COMM0, this also disables interrupts break; case 1: // Switch back to XBEE forwarding/programming, open COMM1 uart_open(&USART_COMM1); // restore communication with main processor on COMM1 usart_set_rx_interrupt_level(&USART_COMM1, USART_INT_LVL_HI); break; } }
/*! \brief Main function. */ int main(void) { uint8_t tx_buf[] = "\n\rHello AVR world ! : "; uint8_t i; /* Initialize the board. * The board-specific conf_board.h file contains the configuration of * the board initialization. */ board_init(); sysclk_init(); pmic_init(); cpu_irq_enable(); sleepmgr_init(); sleepmgr_lock_mode(SLEEPMGR_STDBY); /* USART options. */ static usart_xmegae_rs232_options_t USART_SERIAL_OPTIONS = { .baudrate = USART_SERIAL_EXAMPLE_BAUDRATE, .charlength = USART_SERIAL_CHAR_LENGTH, .paritytype = USART_SERIAL_PARITY, .stopbits = USART_SERIAL_STOP_BIT, .start_frame_detection = false, .one_wire = false, .pec_length = USART_SERIAL_VARIABLE_CHAR_LENGTH, .pec_action = USART_PECACT_PERC01_gc, .encoding_type = USART_DECTYPE_DATA_gc, .encoding_stream = USART_LUTACT_OFF_gc, }; /* Initialize usart driver in RS232 mode */ usart_xmegae_init_rs232(USART_SERIAL_EXAMPLE, &USART_SERIAL_OPTIONS); usart_set_rx_interrupt_level(USART_SERIAL_EXAMPLE, USART_INT_LVL_LO); /* Send "message header" */ for (i = 0; i < sizeof(tx_buf); i++) { usart_putchar(USART_SERIAL_EXAMPLE, tx_buf[i]); while (!usart_tx_is_complete(USART_SERIAL_EXAMPLE)) { } usart_clear_tx_complete(USART_SERIAL_EXAMPLE); } /* Incoming character is process under interrupt * main loop simply enters sleep mode */ while (true) { sleepmgr_enter_sleep(); } }
/** * Name : usart_init * * Synopsis : void usart_init (void) * * Description : Initialize the defined USARTs * * Set the Baud rate * * 8N1 (8 data bits, No Parity, 1 Stop bit) * * Enable receive interrupt * */ void usart_init (void) { usart_serial_options_t serial_options; // 8 data bits, No parity, 1 stop bit serial_options.charlength = USART_CHSIZE_8BIT_gc; serial_options.paritytype = USART_PMODE_DISABLED_gc; serial_options.stopbits = false; // Set baudrate, initialize and enable receive interrupt serial_options.baudrate = POWER_UART_BAUDRATE; usart_serial_init (power.USART, &serial_options); usart_set_rx_interrupt_level (power.USART,USART_RXCINTLVL_LO_gc); power.USART->BAUDCTRLA = 0x01; power.USART->BAUDCTRLB = 0xD0; serial_options.baudrate = GPS_UART_BAUDRATE; usart_serial_init (gps.USART, &serial_options); usart_set_rx_interrupt_level (gps.USART,USART_RXCINTLVL_LO_gc); serial_options.baudrate = RADIO_UART_BAUDRATE; usart_serial_init (radio.USART, &serial_options); usart_set_rx_interrupt_level (radio.USART,USART_RXCINTLVL_LO_gc); radio.USART->BAUDCTRLA = 0x02; radio.USART->BAUDCTRLB = 0x96; serial_options.baudrate = FC_UART_BAUDRATE; usart_serial_init (fc.USART, &serial_options); usart_set_rx_interrupt_level (fc.USART,USART_RXCINTLVL_LO_gc); fc.USART->BAUDCTRLA = 0x02; fc.USART->BAUDCTRLB = 0x96; //fc.USART->BAUDCTRLA = 0x96; //fc.USART->BAUDCTRLB = 0x90; #ifdef STAR_UART serial_options.baudrate = STAR_UART_BAUDRATE; usart_serial_init (star.USART, &serial_options); usart_set_rx_interrupt_level (star.USART,USART_RXCINTLVL_LO_gc); star.USART->BAUDCTRLA = 0x01; star.USART->BAUDCTRLB = 0xD0; #endif #ifdef SUN_UART serial_options.baudrate = SUN_UART_BAUDRATE; usart_serial_init (sun.USART, &serial_options); usart_set_rx_interrupt_level (sun.USART,USART_RXCINTLVL_LO_gc); sun.USART->BAUDCTRLA = 0x96; //Sun Baud = 57600 sun.USART->BAUDCTRLB = 0x90; #endif }
/*! \brief Main function. */ int main(void) { uint8_t encoded_message[] = { 0xF2, 0xF5, 0xAB, 0x97, 0x96, 0x8C, 0xDF, 0x96, 0x91, 0xDF, 0x9E, 0x91, 0xDF, 0x9A, 0x91, 0x9C, 0x90, 0x9B, 0x9A, 0x9B, 0xDF, 0x92, 0x9A, 0x8C, 0x8C, 0x9E, 0x98, 0x9A, 0xDF, 0x8C, 0x9A, 0x91, 0x8B, 0xDF, 0x99, 0x8D, 0x90, 0x92, 0xDF, 0xA7, 0xB2, 0xBA, 0xB8, 0xBE, 0xDF, 0xAA, 0xAC, 0xBE, 0xAD, 0xAB, 0xDE }; uint8_t i; /* Initialize the board. * The board-specific conf_board.h file contains the configuration of * the board initialization. */ board_init(); sysclk_init(); pmic_init(); cpu_irq_enable(); sleepmgr_init(); sleepmgr_lock_mode(SLEEPMGR_STDBY); /* USART options. */ static usart_xmegae_rs232_options_t USART_SERIAL_OPTIONS = { .baudrate = USART_SERIAL_EXAMPLE_BAUDRATE, .charlength = USART_SERIAL_CHAR_LENGTH, .paritytype = USART_SERIAL_PARITY, .stopbits = USART_SERIAL_STOP_BIT, .start_frame_detection = false, .one_wire = false, .pec_length = USART_SERIAL_VARIABLE_CHAR_LENGTH, .pec_action = USART_PECACT_PERC01_gc, .encoding_type = USART_DECTYPE_DATA_gc, .encoding_stream = USART_LUTACT_BOTH_gc, }; /* Initialize usart driver in RS232 mode */ usart_xmegae_init_rs232(USART_SERIAL_EXAMPLE, &USART_SERIAL_OPTIONS); usart_set_rx_interrupt_level(USART_SERIAL_EXAMPLE, USART_INT_LVL_LO); xcl_port(USART_SERIAL_XCL_PORT); xcl_lut_type(LUT_2LUT2IN); xcl_lut_in0(USART_SERIAL_LUT_IN_PIN); xcl_lut_in1(LUT_IN_XCL); xcl_lut0_output(LUT0_OUT_DISABLE); xcl_lut_config_delay(DLY11, LUT_DLY_DISABLE, LUT_DLY_DISABLE); xcl_lut0_truth(NOT_IN0); xcl_lut_in2(LUT_IN_XCL); xcl_lut_in3(LUT_IN_XCL); xcl_lut1_truth(NOT_IN3); /* Send "message header" */ for (i = 0; i < sizeof(encoded_message); i++) { usart_putchar(USART_SERIAL_EXAMPLE, encoded_message[i]); while (!usart_tx_is_complete(USART_SERIAL_EXAMPLE)) { } usart_clear_tx_complete(USART_SERIAL_EXAMPLE); } /* Incoming character is process under interrupt * main loop simply enters sleep mode */ while (true) { sleepmgr_enter_sleep(); } }
uint8_t sim900_init() { DEBUG_PRINT_FUNCTION_NAME(VERBOSE,"SIM900_INIT"); #ifdef SIM900_USART_POLLED debug_string_P(NORMAL,PSTR("(sim900_init) code compiled with polled usart\r\n")); #else usart_interruptdriver_initialize(&sim900_usart_data,USART_GPRS,USART_INT_LVL_LO); usart_set_rx_interrupt_level(USART_GPRS,USART_INT_LVL_LO); usart_set_tx_interrupt_level(USART_GPRS,USART_INT_LVL_OFF); debug_string_P(NORMAL,PSTR("(sim900_init) code compiled with interrupt usart\r\n")); #endif init_sim: usart_rx_enable(USART_GPRS); sim900_power_off(); const char * szRET = NULL; int t=2; while(t--) { sim900_power_toggle(); uint8_t i=3; while(i--) { statusled_blink(1); sim900_put_string(sz_AT,PGM_STRING); szRET = sim900_wait_retstring(); if(szRET!=sz_OK) { debug_string_P(NORMAL,PSTR("(SIM900_init) trying to set a serial line speed\r\n")); } else break; } if(szRET==sz_OK) break; debug_string_P(NORMAL,PSTR("(SIM900_init) Not being able to connect to SIM900. Try to power it on again\r\n")); } if (szRET!=sz_OK) { debug_string_P(NORMAL,PSTR("(SIM900_init) failed to synchronize with GPRS UART. The process will end here\r\n")); return -1; } //disabling echo back from the modem LITTLE_DELAY; szRET = sim900_cmd_with_retstring(PSTR("ATE0\r\n"),PGM_STRING); //sim900_wait_retstring(); if(szRET!=sz_OK) { debug_string_P(NORMAL,PSTR("(SIM900_init) this sim900 doesn't support ATE0\r\n")); } else { debug_string_P(VERBOSE,PSTR("(SIM900_init) correctly issued ATE0\r\n")); } LITTLE_DELAY; szRET = sim900_cmd_with_retstring(PSTR("AT+CREG=0\r\n"),PGM_STRING); if(szRET!=sz_OK) { debug_string_P(NORMAL,PSTR("(SIM900_init) this sim900 doesn't support AT+CREG\r\n")); } else { debug_string_P(VERBOSE,PSTR("(SIM900_init) correctly issued AT+CREG=0\r\n")); } //get the IMEI code LITTLE_DELAY; szRET = sim900_cmd_with_retstring(PSTR("AT+GSN=?\r\n"),true); if(szRET!=sz_OK) { debug_string_P(NORMAL,PSTR("(SIM900_init) this sim900 doesn't support AT+GSN\r\n")); } uint8_t i=IMEI_CODE_LEN; LITTLE_DELAY; sim900_cmd_with_read_string(PSTR("AT+GSN\r\n"),PGM_STRING,g_szIMEI,&i); sim900_wait_retstring(); debug_string_P(NORMAL,PSTR("IMEI code is : ")); debug_string(NORMAL,g_szIMEI,RAM_STRING); debug_string_P(NORMAL,szCRLF); if(szRET!=sz_OK) { debug_string_P(NORMAL,PSTR("(SIM900_init) AT+GSN messed up\r\n")); } else { debug_string_P(NORMAL,PSTR("(SIM900_init) AT+GSN GOT OK\r\n")); } LITTLE_DELAY; sim900_put_string(PSTR("AT+CPIN=?\r\n"),PGM_STRING); szRET = sim900_wait_retstring(); if(szRET!=sz_OK) { debug_string_P(NORMAL,PSTR("(SIM900_init) SIM is not present\r\n")); return 1; } LITTLE_DELAY; sim900_put_string(PSTR("AT+CPIN?\r\n"),PGM_STRING); szRET = sim900_wait4dictionary(tbl_CPIN_rets,NUM_OF_CPIN_RETURNS); char szBuf[16]; if(szRET==szCPIN_SIM_PIN) { debug_string_P(NORMAL,PSTR("(SIM900_init) SIM present and is PIN locked\r\n")); cfg_get_sim_pin(szBuf,8); if((szBuf[0]==0xFF) || (szBuf[0]==0x00)) { debug_string_P(NORMAL,PSTR("(SIM900_init) PIN code is not present in memory SIM will remain locked\r\n")); return 3; } else { debug_string_P(NORMAL,PSTR("(SIM900_init) PIN code is present in memory trying to unlock\r\nPIN: ")); debug_string(NORMAL,szBuf,RAM_STRING); debug_string_P(NORMAL,szCRLF); LITTLE_DELAY; sim900_put_string(PSTR("AT+CPIN="),PGM_STRING); sim900_put_string(szBuf,RAM_STRING); sim900_put_string(szCRLF,PGM_STRING); const char * ret = sim900_wait_retstring(); if(ret!=sz_OK) { debug_string_P(NORMAL,PSTR("(sim900_init) WARNING PIN is WRONG\r\n")); return 3; } } } else if (szRET==szCPIN_SIM_PUK) { debug_string_P(NORMAL,PSTR("(SIM900_init) SIM present and is PUK locked\r\n")); debug_string_P(NORMAL,PSTR("(SIM900_init) no PUK code, SIM init will fail here\r\n")); return 4; } for(i=0;i<18;++i) { statusled_blink(1); LITTLE_DELAY; sim900_put_string(PSTR("AT+CREG?\r\n"),PGM_STRING); uint8_t l = 16; memset(szBuf,0,16); sim900_read_string(szBuf,&l); if(strncasecmp_P(szBuf,PSTR("+CREG: 1,1"),5)!=0) { debug_string_P(NORMAL,PSTR("(SIM900_init) device is not answering as it should restarting it\r\n")); goto init_sim; } if(szBuf[9]=='1') { debug_string_P(NORMAL,PSTR("(SIM900_init) device correctly registered in the network\r\n")); break; } debug_string_P(NORMAL,PSTR("(SIM900_init) device answered ")); debug_string(NORMAL,szBuf,RAM_STRING); debug_string_P(NORMAL,PSTR(" seems not registered in the network\r\n")); debug_string_P(NORMAL,PSTR("(SIM900_init) will check again in 5 second\r\n")); delay_ms(5000); } return 0; }
/*! \brief Main function. Execution starts here. */ int main(void) { board_init(); ui_init(); lithneProgrammer.setMainReset(true); lithneProgrammer.setXbeeReset(true); irq_initialize_vectors(); cpu_irq_enable(); sysclk_init(); delay_init(sysclk_get_cpu_hz()); millis_init(); // Start USB stack to authorize VBus monitoring udc_start(); lithneProgrammer.setMainReset(false); lithneProgrammer.setXbeeReset(false); // USART_COMM0 is a directly forwarded serial to the main processor, handled by interrupts. // The port is opened and interrupts are enabled on USB connect on port 0 // USART_COMM1 connects to the main processor for xbee rx/tx forwarding. // USART_XBEE connects to the xbee module // Messages outside the programming scope are forwarded between these ports in the main loop. // When USB port 1 in opened, the main processor is held in reset and USB port 1 is transparently coupled to the XBEE. // Only in that case the RXE interrupt is enabled. // open both COM ports for xbee forward with default settings uart_open(&USART_COMM1); uart_open(&USART_XBEE); usart_set_rx_interrupt_level(&USART_COMM1, USART_INT_LVL_HI); usart_set_rx_interrupt_level(&USART_XBEE, USART_INT_LVL_HI); lithneProgrammer.init(&USART_COMM0, &serialCo2MainSerial); // should not be necessary to pass 2 objects, but a quick hack to make it work near deadline Lithne.setSerial(serialCo2Xbee); while (true) { if(main_cdc_is_open(1)){ // XBEE is directly linked to USB. Skip Lithne forwarding/programming continue; } if (Lithne.available() ){ // Only process messages inside the programming scope if ( Lithne.getScope() == lithneProgrammingScope ){ lithneProgrammer.updateRemoteAddress(); } // The programming function receives all data packets containing the program to be written if ( Lithne.getFunction() == fProgramming ) { lithneProgrammer.processPacket(); } // Check-in Function else if (Lithne.getFunction() == fCheckingIn && !lithneProgrammer.busyProgramming()) { lithneProgrammer.processCheckin(); } // Node Name Functions - Empty messages are a request, Messages with content set the node name else if (Lithne.getFunction() == fNodeName && !lithneProgrammer.busyProgramming()) { lithneProgrammer.processNodeName(); } // LastUpload Functions - Empty messages are a request, Messages with content set the time of last upload else if (Lithne.getFunction() == fLastUpload && !lithneProgrammer.busyProgramming()) { lithneProgrammer.processLastUpload(); } // File Name Functions - Empty messages are a request, Messages with content set the file name else if (Lithne.getFunction() == fFileName && !lithneProgrammer.busyProgramming()) { lithneProgrammer.processFileName(); } // reset the main processor else if (Lithne.getFunction() == fResetMain && !lithneProgrammer.busyProgramming()) { lithneProgrammer.resetMain(); } // Kill the main processor for a longer period of time, or turn it back on again else if (Lithne.functionIs("killMain") && !lithneProgrammer.busyProgramming()) { lithneProgrammer.processKill(); } // If the message is not in the 'programming scope' this is a regular incoming Lithne message for the user (main proc) - Forward all bytes to main processor else { for(int i=0; i < Lithne.getXBeePacketSize(); i++) // send data in XBee packet straight through to the main processor { serialCo2MainXbee.write( Lithne.getXBeePacket()[i] ); } serialCo2MainXbee.flush(); } } // forward communication from main processor to xbee if ( !lithneProgrammer.busyProgramming() && serialCo2MainXbee.available() ) { uint8_t byte_to_pass_on = serialCo2MainXbee.read(); Lithne.sendBytePublic(byte_to_pass_on, false); while (serialCo2MainXbee.available()) { byte_to_pass_on = serialCo2MainXbee.read(); Lithne.sendBytePublic(byte_to_pass_on, true); } } lithneProgrammer.preventHangup(); } }
/*! \brief Main function. */ int main(void) { uint8_t ping_msg[] = "\n\rPing? ->"; uint8_t pong_msg[] = "\n\r Pong! ->"; /* Initialize the board. * The board-specific conf_board.h file contains the configuration of * the board initialization. */ board_init(); sysclk_init(); irq_initialize_vectors(); cpu_irq_enable(); /* USART options. */ static usart_xmegae_rs232_options_t USART_SERIAL_OPTIONS = { .baudrate = USART_SERIAL_EXAMPLE_BAUDRATE, .charlength = USART_SERIAL_CHAR_LENGTH, .paritytype = USART_SERIAL_PARITY, .stopbits = USART_SERIAL_STOP_BIT, .start_frame_detection = false, .one_wire = true, .pec_length = 0, .encoding_type = USART_DECTYPE_DATA_gc, .encoding_stream = USART_LUTACT_OFF_gc, }; /* Initialize usart driver in RS232 mode */ usart_xmegae_init_rs232(USART1_SERIAL_EXAMPLE, &USART_SERIAL_OPTIONS); usart_xmegae_init_rs232(USART2_SERIAL_EXAMPLE, &USART_SERIAL_OPTIONS); usart_set_rx_interrupt_level(USART1_SERIAL_EXAMPLE, USART_INT_LVL_LO); usart_set_rx_interrupt_level(USART2_SERIAL_EXAMPLE, USART_INT_LVL_LO); /* To initiate the first transfer "Ping" */ message2_received = true; /* Configure XCL so it copies the PC2 pin to PC4 (it allows to wire PC4 * to RS232 TXD pin on STK600 to spy the one wire communication */ xcl_enable(XCL_ASYNCHRONOUS); xcl_port(PC); xcl_lut_type(LUT_2LUT2IN); xcl_lut_in0(LUT_IN_PINL); xcl_lut_in1(LUT_IN_XCL); xcl_lut0_output(LUT0_OUT_PIN4); xcl_lut_config_delay(DLY11, LUT_DLY_DISABLE, LUT_DLY_DISABLE); xcl_lut0_truth(IN0); /* Incoming character is process under interrupt * main loop simply enters sleep mode */ while (true) { if (message2_received) { /* When "pong" is received, send "ping" */ gpio_toggle_pin(LED0_GPIO); message2_received = false; send_onewire_message(USART1_SERIAL_EXAMPLE, ping_msg, sizeof(ping_msg)); } if (message1_received) { /* When "ping" is received, send "pong" */ gpio_toggle_pin(LED0_GPIO); message1_received = false; send_onewire_message(USART2_SERIAL_EXAMPLE, pong_msg, sizeof(pong_msg)); } } }