void init_GPS() { usart_send(1,"$PGCMD,33,0*6D\r\n"); usart_send(1,"$PMTK220,500*2B\r\n"); usart_send(1,"$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n"); return; }
// Data Register Empty Interrupt handler void serial_txint(void) { uint32_t tail = serial_tx_buffer_tail; // Temporary serial_tx_buffer_tail (to optimize for volatile) #ifdef ENABLE_XONXOFF if (flow_ctrl == SEND_XOFF) { usart_send(USART2, XOFF_CHAR); flow_ctrl = XOFF_SENT; } else if (flow_ctrl == SEND_XON) { usart_send(USART2, XON_CHAR); flow_ctrl = XON_SENT; } else #endif { if (tail != serial_tx_buffer_head) { // Send a byte from the buffer usart_send(USART2, serial_tx_buffer[tail]); // Update tail position tail++; if (tail == TX_BUFFER_SIZE) { tail = 0; } serial_tx_buffer_tail = tail; } } // Turn off Data Register Empty Interrupt to stop tx-streaming if this concludes the transfer //if (tail == serial_tx_buffer_head) { UCSR0B &= ~(1 << UDRIE0); } if (tail == serial_tx_buffer_head) usart_disable_tx_interrupt(USART2); return; }
void logger_ip(uint8_t *addr) { logger_number(addr[0]); usart_send(0x2E); // . logger_number(addr[1]); usart_send(0x2E); // . logger_number(addr[2]); usart_send(0x2E); // . logger_number(addr[3]); }
void logic_reverse(uint8_t state) { if (state) { /* reverse switch on */ lamp_on(LAMP_REVERSE); usart_send(PACK_WORDS(MSG_LAMP_REVERSE, ON)); } else { /* reverse switch off */ lamp_off(LAMP_REVERSE); usart_send(PACK_WORDS(MSG_LAMP_REVERSE, OFF)); } }
void logic_highbeam(uint8_t state) { if (state) { /* highbeam switch on */ lamp_on(LAMP_HIGHBEAM); usart_send(PACK_WORDS(MSG_LAMP_HIGHBEAM, ON)); } else { /* highbeam switch off */ lamp_off(LAMP_HIGHBEAM); usart_send(PACK_WORDS(MSG_LAMP_HIGHBEAM, OFF)); } }
void logic_brake(uint8_t state) { if (state) { /* brake switch on */ lamp_on(LAMP_BRAKE); usart_send(PACK_WORDS(MSG_LAMP_BRAKE, ON)); } else { /* brake switch off */ lamp_off(LAMP_BRAKE); usart_send(PACK_WORDS(MSG_LAMP_BRAKE, OFF)); } }
void SerialWrite(char s[]){ int i = 0; while(s[i] != 0x00){ usart_send(s[i]); i++; } if (s[i] == 0x00){ // if it's the last element // send \n usart_send('\n'); } }
void logger_mac(uint8_t *addr) { logger_number_as_hex(addr[0]); usart_send(0x3A); // : logger_number_as_hex(addr[1]); usart_send(0x3A); // : logger_number_as_hex(addr[2]); usart_send(0x3A); // : logger_number_as_hex(addr[3]); usart_send(0x3A); // : logger_number_as_hex(addr[4]); usart_send(0x3A); // : logger_number_as_hex(addr[5]); }
void logic_headlamp(uint8_t state) { if (state) { /* headlamp switch on */ lamp_on(LAMP_HEADLAMP); lamp_on(LAMP_TAILLAMP); usart_send(PACK_WORDS(MSG_LAMP_HEADLAMP, ON)); } else { /* headlamp switch off */ lamp_off(LAMP_HEADLAMP); lamp_off(LAMP_TAILLAMP); usart_send(PACK_WORDS(MSG_LAMP_HEADLAMP, OFF)); } }
void logic_rturn(uint8_t state) { if (state) { /* right-turn switch on */ blinker_on(LAMP_RTURN); lamp_on(LAMP_RTURN); usart_send(PACK_WORDS(MSG_LAMP_RTURN, ON)); } else { /* right-turn switch off */ blinker_off(LAMP_RTURN); lamp_off(LAMP_RTURN); usart_send(PACK_WORDS(MSG_LAMP_RTURN, OFF)); } }
void print_int32_to_serial(uint32_t v) { char str[64] = ""; sprintf(str, "%" PRId32, v); usart_send(str); }
void print_int_to_serial(uint8_t v) { char str[8] = ""; sprintf(str, "%" PRIo8, v); usart_send(str); }
static void my_usart_print_string(uint32_t usart, char *s) { while (*s != 0) { usart_send(usart, *s); s++; } }
void usart3_isr(void) { long xHigherPriorityTaskWoken = pdFALSE; char cChar; if (usart_get_flag(USART3, USART_SR_TXE) == true) { /* The interrupt was caused by the THR becoming empty. Are there any more characters to transmit? */ if (xQueueReceiveFromISR(xCharsForTx[2], &cChar, &xHigherPriorityTaskWoken)) { gpio_set(GPIO_BANK_USART3_RTS, GPIO_USART3_RTS); // set RTS /* A character was retrieved from the buffer so can be sent to the THR now. */ usart_send(USART3, (uint8_t) cChar); } else { // gpio_clear(GPIO_BANK_USART3_RTS, GPIO_USART3_RTS); // clear RTS } } if (usart_get_flag(USART3, USART_SR_RXNE) == true) { cChar = (char) usart_recv(USART3); xQueueSendFromISR(xRxedChars[2], &cChar, &xHigherPriorityTaskWoken); } // ----- transmission complete: if (usart_get_flag(USART3, USART_SR_TC) == true) { gpio_clear(GPIO_BANK_USART3_RTS, GPIO_USART3_RTS); // clear RTS USART_SR(USART3) &= ~USART_SR_TC; // reset flag TC usart_disable_tx_interrupt(USART3); } portEND_SWITCHING_ISR(xHigherPriorityTaskWoken); }
void usart_print_int32_hex(uint32_t v){ RETURN_IF_AVRSIM; char str[64] = ""; sprintf(str, "%" PRIX32, v); usart_send(str); }
void usart1_isr(void) { if (usart_get_flag(USART1, USART_SR_RXNE)) { char c = usart_recv(USART1); if (c == '\n') { if (rx_head > 0) { rx_buf[rx_head] = 0; usart_on_line_recv(rx_buf, rx_head); } rx_head = 0; } else { rx_buf[rx_head] = c; rx_head = (rx_head + 1) % sizeof(rx_buf); } } if (usart_get_flag(USART1, USART_SR_TXE)) { if (waiting_tx_bytes() > 0) { usart_send(USART1, tx_buf[tx_tail]); tx_tail = (tx_tail + 1) % sizeof(tx_buf); } else { usart_disable_tx_interrupt(USART1); } } }
void usart_dostuff() { if (flag_command_ready) handle_command(); if (flag_read_flash) { bool_t verified = false; while (!verified) { flash_read(flash_addr); verified = flash_verify(flash_addr); } while (!verified) { _delay_ms(25); flash_read(flash_addr); verified = flash_verify(flash_addr); } flash_addr++; flag_read_flash = false; } if (flag_xmodem_next_packet) { prepare_packet(); usart_send(); } }
/** * USART interrupt handler. */ void usart1_isr(void) { //TOGGLE(GREEN); /* input (RX) handler */ if ((USART_SR(USART1) & USART_SR_RXNE) != 0) { data_buf = usart_recv(USART1); if (gpc_handle_byte((u8)data_buf) != 0) { //LED_GREEN_TOGGLE(); } else { //LED_RED_ON(); } } /* output (TX) handler */ if ((USART_SR(USART1) & USART_SR_TXE) != 0) { if ((data_buf = gpc_pickup_byte()) >= 0) { usart_send(USART1, (uint16_t)data_buf); //LED_GREEN_TOGGLE(); } else { usart_disable_send(); } } }
void usart2_isr(void) { static u8 data = 'A'; /* Check if we were called because of RXNE. */ if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART2) & USART_SR_RXNE) != 0)) { /* Indicate that we got data. */ gpio_toggle(GPIOD, GPIO12); /* Retrieve the data from the peripheral. */ data = usart_recv(USART2); /* Enable transmit interrupt so it sends back the data. */ usart_enable_tx_interrupt(USART2); } /* Check if we were called because of TXE. */ if (((USART_CR1(USART2) & USART_CR1_TXEIE) != 0) && ((USART_SR(USART2) & USART_SR_TXE) != 0)) { /* Put data into the transmit register. */ usart_send(USART2, data); /* Disable the TXE interrupt as we don't need it anymore. */ usart_disable_tx_interrupt(USART2); } }
void my_usart_print_string(u32 usart, char * s) { while (*s != 0) { usart_send(usart, *s); s++; } }
int main(void) { // Init GPIO: all leds on DDRA = 0xFF; PORTA = 0xFF; // init USART (9600 baud, 8N1) usart_init(); // make sure the USART works // (and PC can check that it has the right test) usart_send_str("async-n-semaphore test\r\n"); // wait for the PC while (usart_recv() != 's') usart_send('?'); // enable USART receive interrupts UCSRxB |= (1<<RXCIE0); // Init Os StartOS(); //NOTE: Since OS is used, program will never get here! while(1); }
void usart2_isr(void) { /* Check if we were called because of RXNE. */ if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART2) & USART_SR_RXNE) != 0)) { /* Indicate that we got data. */ gpio_toggle(GPIOA, GPIO8); /* Retrieve the data from the peripheral. */ ring_write_ch(&output_ring, usart_recv(USART2)); /* Enable transmit interrupt so it sends back the data. */ USART_CR1(USART2) |= USART_CR1_TXEIE; } /* Check if we were called because of TXE. */ if (((USART_CR1(USART2) & USART_CR1_TXEIE) != 0) && ((USART_SR(USART2) & USART_SR_TXE) != 0)) { int32_t data; data = ring_read_ch(&output_ring, NULL); if (data == -1) { /* Disable the TXE interrupt, it's no longer needed. */ USART_CR1(USART2) &= ~USART_CR1_TXEIE; } else { /* Put data into the transmit register. */ usart_send(USART2, data); } } }
void DBG_USART_ISR(void) { /* Check if we were called because of RXNE. */ if (((USART_CR1(DBG_USART) & USART_CR1_RXNEIE) != 0) && ((USART_SR(DBG_USART) & USART_SR_RXNE) != 0)) { usart_data = usart_recv(DBG_USART); if( !dbg_fifo_write_byte( &usart_rx_buf, usart_data ) ) { usart_disable_rx_interrupt(DBG_USART); } } /* Check if we were called because of TXE. */ if (((USART_CR1(DBG_USART) & USART_CR1_TXEIE) != 0) && ((USART_SR(DBG_USART) & USART_SR_TXE) != 0)) { /* Put data into the transmit register. */ if( dbg_fifo_read_byte( &usart_tx_buf, &usart_data ) ) { usart_send(DBG_USART, usart_data); } else { /* Disable the TXE interrupt as we don't need it anymore. */ usart_disable_tx_interrupt(DBG_USART); } } }
void usart1_isr(void) { static uint8_t data = 'A'; /* Check if we were called because of RXNE. */ if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART1) & USART_SR_RXNE) != 0)) { /* Indicate that we got data. */ gpio_toggle(GPIOA, GPIO6); /* Retrieve the data from the peripheral. */ data = usart_recv(USART1); /* Enable transmit interrupt so it sends back the data. */ USART_CR1(USART1) |= USART_CR1_TXEIE; } /* Check if we were called because of TXE. */ if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) && ((USART_SR(USART1) & USART_SR_TXE) != 0)) { /* Indicate that we are sending out data. */ gpio_toggle(GPIOA, GPIO7); /* Put data into the transmit register. */ usart_send(USART1, data); /* Disable the TXE interrupt as we don't need it anymore. */ USART_CR1(USART1) &= ~USART_CR1_TXEIE; } }
/** * Output string @a arg */ void usart_puts(const char *arg) { while (*arg != '\0') { usart_wait_send_ready(USART6); usart_send(USART6, *arg++); } }
void usart1_isr(void) { u8 ch; //if Receive interrupt if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART1) & USART_SR_RXNE) != 0)) { ch=usart_recv(USART1); buffer_put(&u1rx, ch); } if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) && ((USART_SR(USART1) & USART_SR_TXE) != 0)) { if (buffer_get(&u1tx, &ch) == SUCCESS) { //if char read from buffer usart_send(USART1, ch); } else //if buffer empty { //disable Transmit Data Register empty interrupt usart_disable_tx_interrupt(USART1); } } }
int main(void) { u8 channel_array[16]; u16 temperature; rcc_clock_setup_in_hse_16mhz_out_72mhz(); gpio_setup(); usart_setup(); adc_setup(); gpio_clear(GPIOB, GPIO7); /* LED1 on */ gpio_set(GPIOB, GPIO6); /* LED2 off */ /* Send a message on USART1. */ usart_send(USART1, 's'); usart_send(USART1, 't'); usart_send(USART1, 'm'); usart_send(USART1, '\r'); usart_send(USART1, '\n'); /* Select the channel we want to convert. 16=temperature_sensor. */ channel_array[0] = 16; adc_set_regular_sequence(ADC1, 1, channel_array); /* * If the ADC_CR2_ON bit is already set -> setting it another time * starts the conversion. */ adc_on(ADC1); /* Wait for end of conversion. */ while (!(ADC_SR(ADC1) & ADC_SR_EOC)); temperature = ADC_DR(ADC1); /* * That's actually not the real temperature - you have to compute it * as described in the datasheet. */ my_usart_print_int(USART1, temperature); gpio_clear(GPIOB, GPIO6); /* LED2 on */ while(1); /* Halt. */ return 0; }
void logic_state_run(uint32_t msg) { switch (HIGH_WORD(msg)) { case MSG_SWITCH_BRAKE: logic_brake(LOW_BYTE(LOW_WORD(msg))); break; case MSG_SWITCH_LTURN: logic_lturn(LOW_BYTE(LOW_WORD(msg))); break; case MSG_SWITCH_RTURN: logic_rturn(LOW_BYTE(LOW_WORD(msg))); break; case MSG_SWITCH_HEADLAMP: logic_headlamp(LOW_BYTE(LOW_WORD(msg))); break; case MSG_SWITCH_HIGHBEAM: logic_highbeam(LOW_BYTE(LOW_WORD(msg))); break; case MSG_SWITCH_REVERSE: logic_reverse(LOW_BYTE(LOW_WORD(msg))); break; case MSG_BLINKER_BLINK: if (blinker_is_on(LAMP_LTURN)) { lamp_toggle(LAMP_LTURN); usart_send(PACK_WORDS(MSG_LAMP_LTURN, lamp_is_on(LAMP_LTURN))); } if (blinker_is_on(LAMP_RTURN)) { lamp_toggle(LAMP_RTURN); usart_send(PACK_WORDS(MSG_LAMP_RTURN, lamp_is_on(LAMP_RTURN))); } break; case MSG_SWITCH_START: if (!LOW_WORD(msg)) { blinker_disable(); lamp_disable(); usart_send(PACK_WORDS(MSG_STANDBY, 0)); state_set(LOGIC_STANDBY); /* TODO: control display module power */ /* display_power_set(OFF); */ } break; case MSG_DISPLAY_READY: logic_sync_lamps(); break; default: console_log("event ignored"); } }
void print_int32_to_serial(uint32_t v){ char str[64] = ""; sprintf(str, "value is %" PRId32 "\n", v); usart_send(str); usart_psend(PSTR("hello\n")); }
int main(void) { uint8_t channel_array[16]; uint16_t temperature; rcc_clock_setup_in_hse_16mhz_out_72mhz(); gpio_setup(); usart_setup(); adc_setup(); gpio_clear(GPIOB, GPIO7); /* LED1 on */ gpio_set(GPIOB, GPIO6); /* LED2 off */ /* Send a message on USART1. */ usart_send(USART1, 's'); usart_send(USART1, 't'); usart_send(USART1, 'm'); usart_send(USART1, '\r'); usart_send(USART1, '\n'); /* Select the channel we want to convert. 16=temperature_sensor. */ channel_array[0] = 16; adc_set_regular_sequence(ADC1, 1, channel_array); /* * Start the conversion directly (not trigger mode). */ adc_start_conversion_direct(ADC1); /* Wait for end of conversion. */ while (!(ADC_SR(ADC1) & ADC_SR_EOC)); temperature = ADC_DR(ADC1); /* * That's actually not the real temperature - you have to compute it * as described in the datasheet. */ my_usart_print_int(USART1, temperature); gpio_clear(GPIOB, GPIO6); /* LED2 on */ while(1); /* Halt. */ return 0; }