static void configure_console(void) { sysclk_enable_peripheral_clock(PRINTF_USART_ID); //const usart_serial_options_t uart_serial_options = { .baudrate = CONF_UART_BAUDRATE, .paritytype = CONF_UART_PARITY, }; const usart_serial_options_t uart_serial_options = { .baudrate = USART_BAUDRATE, .charlength = USART_CHAR_LENGTH, .paritytype = USART_PARITY, .stopbits = false //US_MR_CHMODE_NORMAL }; usart_serial_init(PRINTF_USART, &uart_serial_options); stdio_serial_init(PRINTF_USART, &uart_serial_options); usart_enable_tx(PRINTF_USART); usart_enable_rx(PRINTF_USART); } int main(void) { sysclk_init(); board_init(); configure_console(); printf("CPH BaseStation v%d\r\n", 1); printf("create_uart_cli_task\r\n"); create_uart_cli_task(CONSOLE_UART, mainUART_CLI_TASK_STACK_SIZE, mainUART_CLI_TASK_PRIORITY); // printf("create_dialer_task\r\n"); // create_dialer_task(mainDIALER_TASK_STACK_SIZE, mainDIALER_TASK_PRIORITY); printf("create_comm_task\r\n"); create_comm_task(mainCOMM_TASK_STACK_SIZE, mainCOMM_TASK_PRIORITY); printf("create_apptask_task\r\n"); create_app_task(mainAPPTASK_TASK_STACK_SIZE, mainAPPTASK_TASK_PRIORITY); printf("create_led_task\r\n"); create_led_task(); printf("starting task scheduler\r\n"); /* Start the scheduler. */ vTaskStartScheduler(); for (;;) { } /* Will only get here if there was insufficient memory to create the idle task. */ return 0; }
/* * Initialise UART. */ void stdio_uart_init(void) { static const usart_serial_options_t uart_options = { .baudrate = CONF_UART_BAUDRATE, #ifdef CONF_UART_CHAR_LENGTH .charlength = CONF_UART_CHAR_LENGTH, #endif .paritytype = CONF_UART_PARITY, #ifdef CONF_UART_STOP_BITS .stopbits = CONF_UART_STOP_BITS, #endif }; /* Configure USART. */ usart_serial_init(CONF_UART, (usart_serial_options_t *) &uart_options); }
void serial_baud(serial_t *obj, int baudrate) { /* Sanity check arguments */ MBED_ASSERT(obj); MBED_ASSERT((baudrate == 110) || (baudrate == 150) || (baudrate == 300) || (baudrate == 1200) || (baudrate == 2400) || (baudrate == 4800) || (baudrate == 9600) || (baudrate == 19200) || (baudrate == 38400) || (baudrate == 57600) || (baudrate == 115200) || (baudrate == 230400) || (baudrate == 460800) || (baudrate == 921600) ); uint32_t clockid = 0; clockid = get_usart_clock_id(pUSART_S(obj)); if (clockid != (uint32_t)NC) { sysclk_disable_peripheral_clock(clockid); } pSERIAL_S(obj)->uart_serial_options.baudrate = baudrate; usart_serial_init(_USART(obj), &(pSERIAL_S(obj)->uart_serial_options)); sysclk_enable_peripheral_clock(clockid); }
/** * 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 }
void bluetooth_init(void) { ioport_set_pin_dir(BT_TX,IOPORT_DIR_OUTPUT); static usart_serial_options_t usart_options = { .baudrate = 19200, .charlength = USART_CHSIZE_8BIT_gc, .paritytype = USART_PMODE_DISABLED_gc, .stopbits = false }; usart_serial_init(BLUETOOTH, &usart_options); } void bluetooth_init_interrupt(void) { pmic_init(); pmic_set_scheduling(PMIC_SCH_ROUND_ROBIN); USARTD0.CTRLA = USART_RXCINTLVL_HI_gc; cpu_irq_enable(); }
int wifi_init(void){ uint32_t BUF_SIZE = MD_BUF_SIZE; uint8_t tmp; //dummy var for flushing UART int r; //return value from wifi_send_cmd (length of resp) char *buf; char *tx_buf; //allocate static buffers if they are not NULL if(resp_buf==NULL){ resp_buf=core_malloc(RESP_BUF_SIZE); } if(resp_complete_buf==NULL){ resp_complete_buf=core_malloc(RESP_COMPLETE_BUF_SIZE); } if(wifi_rx_buf==NULL){ wifi_rx_buf = core_malloc(WIFI_RX_BUF_SIZE); } //if we are standalone, don't do anything if(wemo_config.standalone){ printf("warning: wifi_init called in standalone mode\n"); return 0; } //initialize the memory buf = core_malloc(BUF_SIZE); tx_buf = core_malloc(BUF_SIZE); //set up the UART static usart_serial_options_t usart_options = { .baudrate = WIFI_UART_BAUDRATE, .charlength = WIFI_UART_CHAR_LENGTH, .paritytype = WIFI_UART_PARITY, .stopbits = WIFI_UART_STOP_BITS }; gpio_configure_pin(PIO_PA9_IDX, (PIO_PERIPH_A | PIO_DEFAULT)); gpio_configure_pin(PIO_PA10_IDX, (PIO_PERIPH_A | PIO_DEFAULT)); pmc_enable_periph_clk(ID_WIFI_UART); sysclk_enable_peripheral_clock(ID_WIFI_UART); usart_serial_init(WIFI_UART,&usart_options); //flush any existing data while(usart_serial_is_rx_ready(WIFI_UART)){ usart_serial_getchar(WIFI_UART,&tmp); } // Trigger from timer 0 pmc_enable_periph_clk(ID_TC0); tc_init(TC0, 0, // channel 0 TC_CMR_TCCLKS_TIMER_CLOCK5 // source clock (CLOCK5 = Slow Clock) | TC_CMR_CPCTRG // up mode with automatic reset on RC match | TC_CMR_WAVE // waveform mode | TC_CMR_ACPA_CLEAR // RA compare effect: clear | TC_CMR_ACPC_SET // RC compare effect: set ); TC0->TC_CHANNEL[0].TC_RA = 0; // doesn't matter TC0->TC_CHANNEL[0].TC_RC = 64000; // sets frequency: 32kHz/32000 = 1 Hz NVIC_ClearPendingIRQ(TC0_IRQn); NVIC_SetPriority(TC0_IRQn,1);//high priority NVIC_EnableIRQ(TC0_IRQn); tc_enable_interrupt(TC0, 0, TC_IER_CPCS); //reset the module if(wifi_send_cmd("AT+RST","ready",buf,BUF_SIZE,8)==0){ printf("Error reseting ESP8266\n"); //free memory core_free(buf); core_free(tx_buf); return -1; } //set to mode STA if(wifi_send_cmd("AT+CWMODE=1","OK",buf,BUF_SIZE,8)==0){ printf("Error setting ESP8266 mode\n"); core_free(buf); core_free(tx_buf); return 0; } //try to join the specified network snprintf(tx_buf,BUF_SIZE,"AT+CWJAP=\"%s\",\"%s\"", wemo_config.wifi_ssid,wemo_config.wifi_pwd); if((r=wifi_send_cmd(tx_buf,"OK",buf,BUF_SIZE,10))==0){ printf("no response to CWJAP\n"); //free memory core_free(buf); core_free(tx_buf); return -1; } //check for errors if( (r<1) || (strcmp(&buf[r-1],"OK")!=0)){ snprintf(tx_buf,BUF_SIZE,"failed to join network [%s]: [%s]\n",wemo_config.wifi_ssid, buf); printf(tx_buf); core_log(tx_buf); //free memory core_free(buf); core_free(tx_buf); return -1; } //see if we have an IP address wifi_send_cmd("AT+CIFSR","OK",buf,BUF_SIZE,5); if(strstr(buf,"ERROR")==buf){ printf("error getting IP address\n"); //free the memory core_free(tx_buf); core_free(buf); return -1; } //try to parse the response into an IP address //expect 4 octets but *not* 0.0.0.0 int a1,a2,a3,a4; if(!(sscanf(buf,"+CIFSR:STAIP,\"%d.%d.%d.%d\"",&a1,&a2,&a3,&a4)==4 && a1!=0)){ printf("error, bad address: %s\n",buf); //free the memory core_free(tx_buf); core_free(buf); return -1; } //save the IP to our config snprintf(buf,BUF_SIZE,"%d.%d.%d.%d",a1,a2,a3,a4); memset(wemo_config.ip_addr,0x0,MAX_CONFIG_LEN); strcpy(wemo_config.ip_addr,buf); //set the mode to multiple connection wifi_send_cmd("AT+CIPMUX=1","OK",buf,BUF_SIZE,2); //start a server on port 1336 wifi_send_cmd("AT+CIPSERVER=1,1336","OK",buf,BUF_SIZE,2); //if we know the NILM IP address, send it our IP if(strlen(wemo_config.nilm_ip_addr)!=0){ if(wifi_send_ip()==TX_ERR_MODULE_RESET){ return TX_ERR_MODULE_RESET; } } else { //get the NILM IP address from the manager //once we know the NILM address we send it ours core_get_nilm_ip_addr(); } //log the event snprintf(buf,BUF_SIZE,"Joined [%s] with IP [%s]", wemo_config.wifi_ssid,wemo_config.ip_addr); printf("\n%s\n",buf); core_log(buf); //free the memory core_free(tx_buf); core_free(buf); return 0; }
/****************************************************************************** rn42_init *//** @brief Initializes RN42 driver. @brief usart Pointer to the USART hardware structure the RN42 is on. @brief irq IRQ number to process interrupts on. @brief gpio_pin Pin connected to the RN42's status output. ******************************************************************************/ void rn42_init( volatile avr32_usart_t *usart, uint32_t irq, uint32_t gpio_pin) { static usart_options_t usart_options = { .baudrate = 115200, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = USART_NORMAL_CHMODE }; static pdca_channel_options_t pdca_rx_options = { .pid = AVR32_PDCA_PID_USART3_RX, .addr = NULL, .size = 0, .r_addr = NULL, .r_size = 0, .transfer_size = PDCA_TRANSFER_SIZE_BYTE }; static pdca_channel_options_t pdca_tx_options = { .pid = AVR32_PDCA_PID_USART3_TX, .addr = NULL, .size = 0, .r_addr = NULL, .r_size = 0, .transfer_size = PDCA_TRANSFER_SIZE_BYTE }; /* Initialize the USART to use hardware handshaking. Without the handshaking, the RN42 has trouble keeping up when continuosly sending fairly small amounts of data. Note that when hardware handshaking is enabled, the PDCA must be used for receiving data. */ rn42_usart = usart; usart_serial_init(rn42_usart, &usart_options); rn42_usart->mr = (rn42_usart->mr & ~0xf) | 0x02; /* Register the ISR for USART's DMA completion IRQs. */ pdca_init_channel(PDCA_CHANNEL_RX, &pdca_rx_options); pdca_init_channel(PDCA_CHANNEL_TX, &pdca_tx_options); irq_register_handler(usart_isr, PDCA_IRQ, 0); setup_usart_rx_dma(bt_buf, sizeof(command_t)); usart_irq_enable(); pdca_enable(PDCA_CHANNEL_RX); pdca_enable(PDCA_CHANNEL_TX); /* Configure RN42 status GPIO. */ gpio_configure_pin(gpio_pin, GPIO_DIR_INPUT | GPIO_PULL_DOWN); gpio_disable_pin_pull_up(gpio_pin); rn42_status_pin = gpio_pin; } /****************************************************************************** rn42_connected *//** @brief Returns whether the RN42 is connected by examining the status GPIO. The RN42's status line appears to bounce, so this function takes care of debouncing it. ******************************************************************************/ static bool rn42_connected(void) { bool connected1; bool connected2; do { connected1 = gpio_pin_is_high(rn42_status_pin); delay_ms(50); connected2 = gpio_pin_is_high(rn42_status_pin); } while (connected1 != connected2); return connected1 & connected2; } /****************************************************************************** rn42_send_data *//** @brief Transmits data to a Bluetooth peer over the RN42 module. @param[in] buf Pointer to the data that should be sent. @param[in] size Number of bytes to send. ******************************************************************************/ void rn42_send_data(uint8_t *buf, uint32_t size) { uint32_t status; setup_usart_tx_dma(buf, size); while (true) { status = pdca_get_transfer_status(PDCA_CHANNEL_TX); if (status & PDCA_TRANSFER_COMPLETE) { break; } } } /****************************************************************************** send_response *//** @brief Sends a response packet to the host. @param[in] cmd_id The command ID we're responding to. @param[in] status The status code. @param[in] data Pointer to data to send to the PC. @param[in] data_length Number of bytes to send. ******************************************************************************/ static void send_response( uint8_t cmd_id, uint8_t status, uint8_t *data, uint32_t data_length) { response_t response = { {'C', 'C', 'M', 'D'}, cmd_id, status, }; storeBigU32(&response.data_length, data_length); rn42_send_data((uint8_t*) &response, sizeof(response)); rn42_send_data(data, data_length); }