STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { // parse args mp_arg_val_t vals[PYB_UART_INIT_NUM_ARGS]; mp_arg_parse_all(n_args, args, kw_args, PYB_UART_INIT_NUM_ARGS, pyb_uart_init_args, vals); // set the UART configuration values memset(&self->uart, 0, sizeof(self->uart)); UART_InitTypeDef *init = &self->uart.Init; init->BaudRate = vals[0].u_int; init->WordLength = vals[1].u_int == 8 ? UART_WORDLENGTH_8B : UART_WORDLENGTH_9B; switch (vals[2].u_int) { case 1: init->StopBits = UART_STOPBITS_1; break; default: init->StopBits = UART_STOPBITS_2; break; } if (vals[3].u_obj == mp_const_none) { init->Parity = UART_PARITY_NONE; } else { machine_int_t parity = mp_obj_get_int(vals[3].u_obj); init->Parity = (parity & 1) ? UART_PARITY_ODD : UART_PARITY_EVEN; } init->Mode = UART_MODE_TX_RX; init->HwFlowCtl = UART_HWCONTROL_NONE; init->OverSampling = UART_OVERSAMPLING_16; // init UART (if it fails, it's because the port doesn't exist) if (!uart_init2(self)) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART port %d does not exist", self->uart_id)); } return mp_const_none; }
bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate) { UART_HandleTypeDef *uh = &uart_obj->uart; memset(uh, 0, sizeof(*uh)); uh->Init.BaudRate = baudrate; uh->Init.WordLength = UART_WORDLENGTH_8B; uh->Init.StopBits = UART_STOPBITS_1; uh->Init.Parity = UART_PARITY_NONE; uh->Init.Mode = UART_MODE_TX_RX; uh->Init.HwFlowCtl = UART_HWCONTROL_NONE; uh->Init.OverSampling = UART_OVERSAMPLING_16; return uart_init2(uart_obj); }
STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { bool success; // parse args mp_arg_val_t args[MP_ARRAY_SIZE(pyb_uart_init_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(pyb_uart_init_args), pyb_uart_init_args, args); // set the UART configuration values if (n_args > 1) { self->baudrate = args[0].u_int; switch (args[1].u_int) { case 5: self->config = UART_CONFIG_WLEN_5; break; case 6: self->config = UART_CONFIG_WLEN_6; break; case 7: self->config = UART_CONFIG_WLEN_7; break; case 8: self->config = UART_CONFIG_WLEN_8; break; default: nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); break; } // Parity if (args[2].u_obj == mp_const_none) { self->config |= UART_CONFIG_PAR_NONE; } else { self->config |= ((mp_obj_get_int(args[2].u_obj) & 1) ? UART_CONFIG_PAR_ODD : UART_CONFIG_PAR_EVEN); } // Stop bits self->config |= (args[3].u_int == 1 ? UART_CONFIG_STOP_ONE : UART_CONFIG_STOP_TWO); // Flow control self->flowcontrol = args[4].u_int; success = uart_init2(self); } else { success = uart_init(self, args[0].u_int); } // init UART (if it fails, something weird happened) if (!success) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); } // set timeouts self->timeout = args[5].u_int; self->timeout_char = args[6].u_int; // setup the read buffer m_del(byte, self->read_buf, self->read_buf_len); self->read_buf_head = 0; self->read_buf_tail = 0; if (args[7].u_int <= 0) { // no read buffer self->read_buf_len = 0; self->read_buf = NULL; MAP_UARTIntDisable(self->reg, UART_INT_RX | UART_INT_RT); } else { // read buffer using interrupts self->read_buf_len = args[7].u_int; self->read_buf = m_new(byte, args[7].u_int); } return mp_const_none; }
bool uart_init(pyb_uart_obj_t *self, uint baudrate) { self->baudrate = baudrate; self->config = UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE; self->flowcontrol = UART_FLOWCONTROL_NONE; return uart_init2(self); }
int main(void) { extern int __C30_UART; __C30_UART = 2; unsigned char i; unsigned char in_config = 0; char c; /* generic hw init */ hw_init(); /* real time clock init */ clock_init(); /* adc init */ adc_init(); /* init uart2 */ uart_init2(UART_PORT_TELEMETRY); /* init video driver */ init_video(); /* try to load config from flash */ load_config(); /* video driver config */ video_apply_config(&config.video); /* init widgets */ widgets_init(); /* enable all interrupts */ SRbits.IPL = 0; CORCONbits.IPL3 = 1; /* TODO: rework config entry */ /* check for config entry */ for (i = 0; i < 8; i++) { while (uart_getc2(&c) == 0) { widgets_process(); render_process(); ClrWdt(); } if (c != '!') break; } if (i == 8) { in_config = 1; while (in_config) { in_config = config_osd(); widgets_process(); render_process(); ClrWdt(); } } /* re-build tab list */ build_tab_list(); uart_set_baudrate2(config.baudrate); init_home_process(); init_flight_stats_process(); for (;;) { mavlink_process(); widgets_process(); render_process(); clock_process(); ClrWdt(); } return 0; }