void on_quit_file_activate (GtkMenuItem *menuitem, gpointer user_data) { debug_p("bye world;\n"); close_uart(); gtk_main_quit(); }
void CMainDlg::OnClose() { if(m_BootConnected) OnBnClickedConnect(); close_uart(); CDialog::OnClose(); }
gboolean on_Xgcom_delete_event (GtkWidget *widget, GdkEvent *event, gpointer user_data) { debug_p("bye world;\n"); close_uart(); gtk_main_quit(); return FALSE; }
int main(int argc, char *argv[]){ char test_str[] = "Hello World"; int bytes = strlen(test_str); // get number of bytes in test string char buf[BUF_SIZE]; int ret; // return value int bus; // which bus to use // Parse arguments if(argc!=2){ //argc==2 actually means one argument given print_usage(); return -1; } else bus = atoi(argv[1]); if(!(bus==0||bus==1||bus==2||bus==5)){ print_usage(); return -1; } // Initialization initialize_board(); printf("\ntesting UART bus %d\n\n", bus); if(initialize_uart(bus, BAUDRATE, TIMEOUT_S)){ printf("Failed to initialize_uart%d\n", bus); cleanup_board(); return -1; } // Flush and Write printf("Sending %d bytes: %s \n", bytes, test_str); flush_uart(bus); uart_send_bytes(bus, bytes, &test_str[0]); // Read memset(buf, 0, BUF_SIZE); ret = uart_read_bytes(bus, bytes, &buf[0]); if(ret<0) printf("Error reading bus\n"); else if(ret==0)printf("timeout reached, %d bytes read\n", ret); else printf("Received %d bytes: %s \n", ret, buf); // close up close_uart(bus); cleanup_board(); return 0; }
/// \cond int main(int argc, char **argv) { int fd; char *s; unsigned char ope_code; unsigned char prev_code; unsigned char data_size; unsigned char prev_size; int len; int baud; char ret; printf("bcm2835_for_java ver1.01 start priority=%d\n", nice(1) ); if( argc != 2 ) { printf("bcm2835_for_java needs 1 parameter like as /tmp/shared_mem\n"); exit ( -1 ); } else { fd = open( argv[1], O_RDWR); if( fd == -1 ) { printf("file %s can't open\n",argv[1]); exit(-1); } else { s = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if( s == MAP_FAILED ) { printf("we can't create mapping file\n"); } } } r_buff = (struct ring_buff *)s; w_buff = (struct ring_buff *)(s+sizeof(struct ring_buff) ); bi_send_buff = (char *)( s + 2*sizeof(struct ring_buff) ); bi_rec_buff = (char *)( bi_send_buff + TEMP_BUFF_SIZE ); sync_code = (int *)( bi_rec_buff + TEMP_BUFF_SIZE ); reply_code = (int *)( sync_code + 1 ); init_ring_buff(w_buff,WRITER_INIT); init_ring_buff(r_buff,READER_INIT); bi_status = STATUS_FINE; while( 1 ) { if( calc_data_size( r_buff ) != 0 ) { prev_code = ope_code; prev_size = data_size; get_ope_code(); ope_code = buff[0]; data_size = calc_data_size( r_buff ); switch( ope_code ) { case OPE_INIT: bcm_init(); break; case OPE_CLOSE: bcm_close(); break; case OPE_SET_DEBUG: ope_set_debug(); break; case OPE_PERI_READ: ope_peri_read(); break; case OPE_PERI_READ_NB: ope_peri_read_nb(); break; case OPE_PERI_WRITE: ope_peri_write(); break; case OPE_PERI_WRITE_NB: ope_peri_write_nb(); break; case OPE_PERI_SET_BITS: ope_peri_set_bits(); break; case OPE_GPIO_FSEL: ope_gpio_fsel(); break; case OPE_GPIO_SET: ope_gpio_set(); break; case OPE_GPIO_CLR: ope_gpio_clr(); break; case OPE_GPIO_SET_MULTI: ope_gpio_set_multi(); break; case OPE_GPIO_CLR_MULTI: ope_gpio_clr_multi(); break; case OPE_GPIO_LEV: ope_gpio_lev(); break; case OPE_GPIO_EDS: ope_gpio_eds(); break; case OPE_GPIO_SET_EDS: ope_gpio_set_eds(); break; case OPE_GPIO_REN: ope_gpio_ren(); break; case OPE_GPIO_CLR_REN: ope_gpio_clr_ren(); break; case OPE_GPIO_FEN: ope_gpio_fen(); break; case OPE_GPIO_CLR_FEN: ope_gpio_clr_fen(); break; case OPE_GPIO_HEN: ope_gpio_hen(); break; case OPE_GPIO_CLR_HEN: ope_gpio_clr_hen(); break; case OPE_GPIO_LEN: ope_gpio_len(); break; case OPE_GPIO_CLR_LEN: ope_gpio_clr_len(); break; case OPE_GPIO_AREN: ope_gpio_aren(); break; case OPE_GPIO_CLR_AREN: ope_gpio_clr_aren(); break; case OPE_GPIO_AFEN: ope_gpio_afen(); break; case OPE_GPIO_CLR_AFEN: ope_gpio_clr_afen(); break; case OPE_GPIO_PUD: ope_gpio_pud(); break; case OPE_GPIO_PUDCLK: ope_gpio_pudclk(); break; case OPE_GPIO_WRITE: ope_gpio_write(); break; case OPE_GPIO_PAD: ope_gpio_pad(); break; case OPE_GPIO_SET_PAD: ope_gpio_set_pad(); break; case OPE_DELAY: ope_delay(); break; case OPE_DELAYMICROSECONDS: ope_delaymicroseconds(); break; case OPE_GPIO_WRITE_MULTI: ope_gpio_write_multi(); break; case OPE_GPIO_WRITE_MASK: ope_gpio_write_mask(); break; case OPE_GPIO_SET_PUD: ope_gpio_set_pud(); break; case OPE_SPI_BEGIN: ope_spi_begin(); break; case OPE_SPI_END: ope_spi_end(); break; case OPE_SPI_SETBITORDER: ope_spi_setbitorder(); break; case OPE_SPI_SETCLOCKDIVIDER: ope_spi_setclockdivider(); break; case OPE_SPI_SETDATAMODE: ope_spi_setdatamode(); break; case OPE_SPI_CHIPSELECT: ope_spi_chipselect(); break; case OPE_SPI_SETCHIPSELECTPOLARITY: ope_spi_setchipselectpolarity(); break; case OPE_SPI_TRANSFER: ope_spi_transfer(); break; case OPE_SPI_TRANSFERNB: ope_spi_transfernb(); break; case OPE_SPI_TRANSFERN: ope_spi_transfern(); break; case OPE_SPI_WRITENB: ope_spi_writenb(); break; case OPE_I2C_BEGIN: ope_i2c_begin(); break; case OPE_I2C_END: ope_i2c_end(); break; case OPE_I2C_SETSLAVEADDRESS: ope_i2c_setslaveaddress(); break; case OPE_I2C_SETCLOCKDIVIDER: ope_i2c_setclockdivider(); break; case OPE_I2C_SET_BAUDRATE: ope_i2c_set_baudrate(); break; case OPE_I2C_WRITE: ope_i2c_write(); break; case OPE_I2C_READ: ope_i2c_read(); break; case OPE_I2C_READ_REGISTER_RS: ope_i2c_read_register_rs(); break; case OPE_ST_READ: ope_st_read(); break; case OPE_ST_DELAY: ope_st_delay(); break; case OPE_HELLO: get_int_code(); len = *(int *)(buff+1); set_ope_code( OPE_REPLY ); strcpy( bi_rec_buff, "Nice to meet you." ); set_int_code( strlen(bi_rec_buff) ); put_reply(); mark_sync(); usleep(5000); break; case OPE_SYNC: mark_sync(); break; case OPE_EXIT: goto BREAK_LINE; break; case OPE_OPEN_UART: open_uart(); break; case OPE_CONFIG_UART: configure_uart(); break; case OPE_SEND_UART: send_uart(); break; case OPE_RECEIVE_UART: receive_uart(); break; case OPE_CLOSE_UART: close_uart(); break; default: printf("prev_code %02x \n",prev_code); printf("prev_size %d \n",prev_size); printf("ope_code error %02x \n",ope_code); printf("data_size=%d \n",data_size); break; } } else { usleep(5000); } } BREAK_LINE: printf("Close bcm2835_for_java\n"); exit(0); }
void CMainDlg::OnBnClickedConnect() { CBusy busy; progress_Changed(0); if(!m_BootConnected) { // Unconnected state => try to connect to bootloader char tmpstrCom[512]; char tmpstr[1024]; CString tmpStrFormat; EnableInputs(false); ; if(m_fileS19.GetLBText(m_fileS19.GetCurSel(),tmpstr) < 0) { EnableInputs(true); printf("S19 file must be selected!\n"); return; } if(m_comPorts.GetLBText(m_comPorts.GetCurSel(),tmpstr) < 0) { EnableInputs(true); printf("Serial Com Port must be selected!\n"); return; } if(m_cbComSpeed.GetLBText(m_cbComSpeed.GetCurSel(),tmpstr) < 0) { EnableInputs(true); printf("Speed of serial ComPort must be selected!\n"); return; } // Dual mode !!! if(m_chbSingleWire.GetCheck()) { if(m_chbVerify.GetCheck()) tmpStrFormat = "COM%d!S"; else tmpStrFormat = "COM%d!s"; }else { if(m_chbVerify.GetCheck()) tmpStrFormat = "COM%d!D"; else tmpStrFormat = "COM%d!d"; } sprintf(tmpstrCom, tmpStrFormat, (int)m_comPorts.GetItemData(m_comPorts.GetCurSel())); m_cbComSpeed.GetLBText(m_cbComSpeed.GetCurSel(),tmpstr); // open serial port if(init_uart(tmpstrCom, tmpstr) < 0) { EnableInputs(true); return; } LogResetReplaceLine(); // wait for reset if(hook_reset() < 0) { close_uart(); EnableInputs(true); return; } LogResetReplaceLine(); // calibrate target's oscilator if(calibrate_speed(m_chbShortTrim.GetCheck()) < 0) { close_uart(); EnableInputs(true); return; } LogResetReplaceLine(); // read info if(!read_mcu_info()) { printf("\nBootloader protocol version: 0x%02X (%s, ", bl_version , Version_string(bl_version)); printf("read command "); if (!bl_rcs) printf("NOT "); printf("supported)\n"); printf("CRC protocol "); if (!bl_crcs) printf("NOT "); printf("supported)\n"); printf("Bootloader version string: %s\n", ident.targ_name); if (!bl_rcs) m_chbVerify.SetCheck(0); if (bl_version == BL_HC08) // HC08 { unsigned i; printf("Available flash memory: 0x%04X-0x%04X\n", ident.mem_start[0], ident.mem_end[0]-1); printf("Erase block size: %3d bytes\n", ident.erblk); printf("Write block size: %3d bytes\n", ident.wrblk); printf("Original vector table: 0x%04X\n",ident.int_vect_tbl); printf("Bootloader user table: 0x%04X\n",ident.bl_tbl); printf("Bootloader data (hex): "); for(i=0; i<sizeof(ident.priv_data); i++) printf("%02x ", ident.priv_data[i]); printf("\n"); } else if (bl_version == BL_HCS08) // S08 { unsigned i; printf("System device ID: 0x%03X [%s] rev. %u\n", ident.sdid, chipid ,ident.sdidrev); printf("Number of memory blocks: %u\n", ident.num_blocks); for(i=0; i<ident.num_blocks; i++) printf("Memory block #%u: 0x%04X-0x%04X\n", i+1, ident.mem_start[i], ident.mem_end[i]-1); printf("Erase block size: %3d bytes\n", ident.erblk); printf("Write block size: %3d bytes\n", ident.wrblk); printf("Original vector table: 0x%04X-0xFFFF\n",ident.int_vect_tbl); printf("New vector table: 0x%04X-0x%04X\n",ident.bl_tbl, ident.bl_tbl+(0xFFFF-ident.int_vect_tbl)); } else if (bl_version == BL_HC08_LARGE) // large HC08 { unsigned i; printf("Number of memory blocks: %u\n", ident.num_blocks); for(i=0; i<ident.num_blocks; i++) printf("Memory block #%u: 0x%04X-0x%04X\n", i+1, ident.mem_start[i], ident.mem_end[i]-1); printf("Erase block size: %3d bytes\n", ident.erblk); printf("Write block size: %3d bytes\n", ident.wrblk); printf("Original vector table: 0x%04X\n",ident.int_vect_tbl); printf("Bootloader user table: 0x%04X\n",ident.bl_tbl); } else if ((bl_version == BL_HCS08_LARGE) || (bl_version == BL_HCS08_LONG))// large + long S08 { unsigned i; printf("System device ID: 0x%03X [%s] rev. %u\n", ident.sdid, chipid ,ident.sdidrev); printf("Number of memory blocks: %u\n", ident.num_blocks); for(i=0; i<ident.num_blocks; i++) printf("Memory block #%u: 0x%06X-0x%06X\n", i+1, ident.mem_start[i], ident.mem_end[i]-1); printf("Erase block size: %3d bytes\n", ident.erblk); printf("Write block size: %3d bytes\n", ident.wrblk); printf("Original vector table: 0x%04X\n",ident.int_vect_tbl); printf("Bootloader user table: 0x%04X\n",ident.bl_tbl); } else if (bl_version == BL_CFV1) // CFV1 { unsigned i; printf("System device ID: 0x%03X [%s] rev. %u\n", ident.sdid, chipid ,ident.sdidrev); printf("Number of memory blocks: %u\n", ident.num_blocks); for(i=0; i<ident.num_blocks; i++) printf("Memory block #%u: 0x%08X-0x%08X\n", i+1, ident.mem_start[i], ident.mem_end[i]); printf("Erase block size: %3d bytes\n", ident.erblk); printf("Write block size: %3d bytes\n", ident.wrblk); if (ident.int_vect_tbl != NULL) { printf("Original vector table: 0x%08X-0x%08X\n",NULL, ident.int_vect_tbl-1); printf("New vector table: 0x%08X-0x%08X\n",ident.bl_tbl,ident.bl_tbl+ident.int_vect_tbl-1); } else printf("Vector table not relocated\n"); } else if (bl_version == BL_KINETIS) // Kinetis { unsigned i; printf("System device ID: 0x%03X [%s] rev. %u\n", ident.sdid, chipid ,ident.sdidrev); printf("Kinetis Package: %s .\n", get_package_str(ident.sdid)); printf("Number of memory blocks: %u\n", ident.num_blocks); for(i=0; i<ident.num_blocks; i++) printf("Memory block #%u: 0x%08X-0x%08X\n", i+1, ident.mem_start[i], ident.mem_end[i]); printf("Erase block size: %3d bytes\n", ident.erblk); printf("Write block size: %3d bytes\n", ident.wrblk); printf("Original vector table: 0x%08X-0x%08X\n",ident.int_vect_tbl,ident.int_vect_tbl + 0x3FF); printf("New vector table: 0x%08X-0x%08X\n",ident.bl_tbl, ident.bl_tbl + 0x3FF); } else { fprintf(stderr, "This version [0x%02X] of protocol is not (yet) supported! Check AN2295SW for update!\n", bl_version); close_uart(); EnableInputs(true); return; } } else { fprintf(stderr, "Can't read MCU info. Could be protocol error.\n"); fprintf(stderr, "(Or forgot to set single wire mode?)\n"); close_uart(); EnableInputs(true); return; } printf("\nS19 Image Control.\n"); LogResetReplaceLine(); // read S19 file reset_S19image(&image); m_fileS19.GetLBText(m_fileS19.GetCurSel(),tmpstr); if(read_s19(tmpstr, &image) < 0) { close_uart(); EnableInputs(true); return; } sprintf(tmpstrCom, "0x%04x", S19_GetCRC()); m_edbCheckSum.SetWindowTextA(tmpstrCom); if(S19_GetTotal() > 1024) sprintf(tmpstrCom, "0x%x B, %d KB", S19_GetTotal(), S19_GetTotal()/1024); else sprintf(tmpstrCom, "0x%x B, %d B", S19_GetTotal(), S19_GetTotal()); m_edbImageSize.SetWindowTextA(tmpstrCom); LogResetReplaceLine(); // masquerade vector table if(setup_vect_tbl(ident.int_vect_tbl , ident.bl_tbl) < 0) { close_uart(); EnableInputs(true); return; } LogResetReplaceLine(); // check if code goes to valid memory only - issue warning only, but continue! (r30323) check_image(); LogResetReplaceLine(); printf("\n "); printf("\n "); m_btConnect.SetWindowTextA("Quit/&Run"); m_BootConnected = true; FillUpIdentification(); EnableControlBtn(true); }else { // Connected state => Quit and run MCU application unhook(); LogResetReplaceLine(); close_uart(); m_btConnect.SetWindowTextA("Connect"); EnableInputs(true); m_BootConnected = false; FillUpIdentification(); EnableControlBtn(false); //LogClear(); } }
/******************************************************************************* * int initialize_uart(int bus, int baudrate) * * bus needs to be between MIN_BUS and MAX_BUS which here is 0 & 5. * baudrate must be one of the standard speeds in the UART spec. 115200 and * 57600 are most common. * timeout is in seconds and must be >=0.1 * * returns -1 for failure or 0 for success *******************************************************************************/ int initialize_uart(int bus, int baudrate, float timeout_s){ struct termios config; speed_t speed; //baudrate // sanity checks if(bus<MIN_BUS || bus>MAX_BUS){ printf("ERROR: uart bus must be between %d & %d\n", MIN_BUS, MAX_BUS); return -1; } if(timeout_s<0.1){ printf("ERROR: timeout must be >=0.1 seconds\n"); return -1; } switch(baudrate){ case (230400): speed=B230400; break; case (115200): speed=B115200; break; case (57600): speed=B57600; break; case (38400): speed=B38400; break; case (19200): speed=B19200; break; case (9600): speed=B9600; break; case (4800): speed=B4800; break; case (2400): speed=B2400; break; case (1800): speed=B1800; break; case (1200): speed=B1200; break; case (600): speed=B600; break; case (300): speed=B300; break; case (200): speed=B200; break; case (150): speed=B150; break; case (134): speed=B134; break; case (110): speed=B110; break; case (75): speed=B75; break; case (50): speed=B50; break; default: printf("ERROR: invalid speed. Please use a standard baudrate\n"); return -1; } // close the bus in case it was already open close_uart(bus); // open file descriptor for blocking reads if ((fd[bus] = open(paths[bus], O_RDWR | O_NOCTTY)) < 0) { printf("error opening uart%d in /dev/\n", bus); printf("device tree overlay probably isn't loaded\n"); return -1; } // get current attributes if (tcgetattr(fd[bus],&config)!=0){ close(fd[bus]); printf("Cannot get uart attributes\n"); return -1; } // set up tc config //memset(&config,0,sizeof(config)); // config.c_iflag=0; // config.c_oflag=0; // config.c_lflag = 0; // config.c_cflag = 0; config.c_lflag &= ~ICANON; // turn off canonical read config.c_cflag &= ~PARENB; // no parity config.c_cflag &= ~CSTOPB; // disable 2 stop bits (use just 1) config.c_cflag &= ~CSIZE; // wipe all size masks config.c_cflag |= CS8; // set size to 8 bit characters config.c_cflag |= CREAD; // enable reading config.c_cflag |= CLOCAL; // ignore modem status lines // convert float timeout in seconds to int timeout in tenths of a second config.c_cc[VTIME] = timeout_s*10; //config.c_cc[VTIME]=0; // if VTIME>0 & VMIN>0, read() will return when either the requested number // of bytes are ready or when VMIN bytes are ready, whichever is smaller. // since we set VMIN to the size of the buffer, read() should always return // when the user's requested number of bytes are ready. //config.c_cc[VMIN]=MAX_READ_LEN; // config.c_cc[VMIN] = 0; if(cfsetispeed(&config, speed) < 0) { printf("ERROR: cannot set uart%d baud rate\n", bus); return -1; } if(cfsetospeed(&config, speed) < 0) { printf("ERROR: cannot set uart%d baud rate\n", bus); return -1; } tcflush(fd[bus],TCIOFLUSH); if(tcsetattr(fd[bus], TCSANOW, &config) < 0) { printf("cannot set uart%d attributes\n", bus); close(fd[bus]); return -1; } tcflush(fd[bus],TCIOFLUSH); initialized[bus] = 1; bus_timeout_s[bus]=timeout_s; flush_uart(bus); return 0; }