Revision: 1129 Author: [email protected] Date: Tue Jul 28 16:31:21 2009 Log: added pic18f14k50_usb_io/example filea http://code.google.com/p/jallib/source/detail?r=1129
Added: /trunk/project/pic18f14k50_usb_io/examples /trunk/project/pic18f14k50_usb_io/examples/blog_part2_a.jal /trunk/project/pic18f14k50_usb_io/examples/board_18f14k50_usb_af.jal /trunk/project/pic18f14k50_usb_io/examples/usb_i2c_master.jal /trunk/project/pic18f14k50_usb_io/examples/usb_serial_converter.jal Modified: /trunk/include/peripheral/i2c/i2c_hardware_aliases.jal ======================================= --- /dev/null +++ /trunk/project/pic18f14k50_usb_io/examples/blog_part2_a.jal Tue Jul 28 16:31:21 2009 @@ -0,0 +1,58 @@ +-- ------------------------------------------------------ +-- Title: Test board 1a, with Microchip PIC18f14k50 +-- +-- Author: Albert Faber, Copyright (c) 2009, all rights reserved. +-- +-- Adapted-by: +-- +-- Compiler: >=2.4h +-- +-- This file is part of jallib (http://jallib.googlecode.com) +-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php) +-- +-- Description: +-- blog part 2 example(s) +-- Sources: +-- +-- Notes: +-- - for bootloader use following compiler settings. +-- -loader18 -no-fuse +-- ------------------------------------------------------ +-- + +include 18f14k50 -- target PICmicro + +-- +-- This program assumes a 12 MHz resonator or crystal +-- is connected to pins OSC1 and OSC2. +-- Configuration bits may cause a different frequency! +pragma target clock 48_000_000 -- oscillator frequency + +-- include libraries +include usb_serial +include print + +-- initialize the USB serial library +usb_serial_init() + + +-- main loop +forever loop + + var byte ch + + -- Service USB, call on a regular base to keep communcaiton going + usb_serial_flush() + + -- check for input character + if usb_serial_read( ch ) then + -- nothing spectecular, echo ch + 1 + if ch == "?" then + const byte str1[] = "Hi there \r\n" + print_string( usb_serial_data, str1 ) + else + usb_serial_data = ch + end if + end if +end loop +-- ======================================= --- /dev/null +++ /trunk/project/pic18f14k50_usb_io/examples/board_18f14k50_usb_af.jal Tue Jul 28 16:31:21 2009 @@ -0,0 +1,122 @@ +-- ----------------------------------------------------------------------------- +-- Title: USB PICDEM like board description +-- Author: Albert Faber, Copyright (c) 2008..2009, all rights reserved. +-- Adapted-by: - +-- Compiler: >=2.4i +-- +-- This file is part of jallib (http://jallib.googlecode.com) +-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php) +-- +-- Description: This file is the setup for my homebrew, PICDEM like 18f4550 board +-- The board consist of a reset button switch to gnd (tided to pin1, with 4k7 pull up) +-- A program button switch to gnd (tided to pin37, with 4k7 pull up) +-- A 20 MHz external crystal and 15pf caps connected to OSC1/2 +-- pin 18 (Vusb) with a 220 nF capacitor to the ground +-- The connection between PC and the 18F4450 +-- +5v to power the board +-- data- wire to pin 23 (D-) +-- data+ wire to pin 24 (D+) +-- ground wire to the Vss of the PIC +-- +-- In addition, a MAX232 chip connccted to the USART pins, for serial (mainly to debug) +-- communication. +-- The board is loaded with the standard microchip bootloader, therefore the examples +-- are compiled with the following JAL compiler flags: -loader18 -no-fuse +-- +-- +-- +-- Sources: http://www.microchip.org for PICDEM docs +-- +-- Notes: not sure about the compiler version +-- +-- ------------------------------------------------------ +-- ------------------------------------------------------ +-- +-- + + +-- < Version: 1.1 24-02-2009, Albert Faber +-- < 1.0 Orignal release +-- < 1.1 Make RS-232 option configurable +-- ----------------------------------------------------------------------------- +-- ----------------------------------------------------------------------------- + + +-- compiler dependant device definitions +include 18f14k50 + +-- even though the external crystal is 20 MHz, the configuration is such that +-- the CPU clock is derived from the 96 Mhz PLL clock (div2), therefore set +-- target frequency to 48 MHz +pragma target clock 48_000_000 + + +-- fuses +pragma target CPUDIV NO_CPU_SYSTEM_CLOCK_DIVIDE +pragma target USBDIV USB_CLOCK_COMES_DIRECTLY_FROM_THE_OSC1_OSC2_OSCILLATOR_BLOCK__NO_DIVIDE +pragma target OSC HS +pragma target PLLEN OSCILLATOR_MULTIPLIED_BY_4 +pragma target FCMEN DISABLED +pragma target IESO DISABLED +pragma target PWRTE DISABLED -- power up timer +pragma target BROWNOUT DISABLED -- no brownout detection +pragma target VOLTAGE V30 -- brown out voltage +pragma target WDT DISABLED -- no watchdog +pragma target WDTPS P32768 -- watch dog saler setting +pragma target MCLR INTERNAL -- no master reset +pragma target LVP DISABLED -- no low-voltage programming +pragma target XINST ENABLED -- extended instruction set +pragma target BACKBUG DISABLED -- background debugging +pragma target CP_0 DISABLED -- code block 0 not protected +pragma target CP_1 DISABLED -- code block 1 not protected +pragma target CPB DISABLED -- bootblock code not write protected +pragma target WRT_0 DISABLED -- table writeblock 0 not protected +pragma target WRT_1 DISABLED -- table write block 1 not protected +pragma target WRTB DISABLED -- bootblock not write protected +pragma target WRTC DISABLED -- config not write protected +pragma target EBTR_0 DISABLED -- table read block 0 not protected +pragma target EBTR_1 DISABLED -- table read block 1 not protected +pragma target EBTRB DISABLED -- boot block not protected +pragma target HFOFST THE_SYSTEM_CLOCK_IS_HELD_OFF_UNTIL_THE_HFINTOSC_IS_STABLE + + +-- whether or not using the serial port +const HAS_SERIAL_DEBUG_PORT = 1 + +-- include delay functions +include delay + +-- disable comperators +if defined( cmcon ) == true then + cmcon = 0x07 +end if + +if defined( HAS_SERIAL_DEBUG_PORT ) == true then + + const serial_hw_baudrate = 115_200 + + include serial_hardware + include format + include print + + -- intialze serial device + serial_hw_init() + + procedure serial_print_spc() is + serial_hw_write( " " ) + end procedure + + procedure serial_newline() is + serial_hw_write( 10 ) + -- serial_hw_write( 13 ) + end procedure +end if + + +;@jallib section i2c +-- I2C io definition, using hardware I2C +var volatile bit i2c_scl is pin_b6 +var volatile bit i2c_scl_direction is pin_b6_direction +var volatile bit i2c_sda is pin_b4 +var volatile bit i2c_sda_direction is pin_b4_direction + ======================================= --- /dev/null +++ /trunk/project/pic18f14k50_usb_io/examples/usb_i2c_master.jal Tue Jul 28 16:31:21 2009 @@ -0,0 +1,542 @@ +-- Title: USB I2C master exampled +-- Author: Albert Faber, Copyright (c) 2008..2009, all rights reserved. +-- Adapted-by: - +-- Compiler: >=2.4j +-- +-- This file is part of jallib (http://jallib.googlecode.com) +-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php) +-- +-- Description: This application acts a USB controlled I2C master device +-- +-- +-- Sources: http://www.usb.org for USB specifications +-- +-- Notes: compiled with following flags +-- -loader18 -no-fuse -no-variable-reuse +-- +-- ------------------------------------------------------ +-- ------------------------------------------------------ +-- +-- + +include board_18f14k50_usb_af + +include delay +include usb_serial +include print + +-- constants +const byte str_welcome[] = "USB-I2C Master v0.01b\r\n" +const byte str_test_i2c[] = "Start test i2c\r\n" +const byte str_done[] = "done\n" + +var byte target_address = 0x00 + +-- variables +var word i = 0 + + + +-- i2c setup +var word _i2c_bus_speed = 1 ; * 100kHz +const bit _i2c_level = true ; i2c levels (not SMB) + + +; include i2c_software +include i2c_hardware + +i2c_initialize() + +-- --------------- + +var byte i2c_tx_buffer[80] +var byte i2c_rx_buffer[80] at i2c_tx_buffer + +include i2c_level1 + +-- interrupts? No thanks +while INTCON_GIE loop + INTCON_GIE = false +end loop + + +-- setup the USB serial library +usb_serial_init() + +var bit has_shown_welcome_msg = true +var byte ch + +function ascii_nibble_to_hex( byte in data ) return byte is + --pragma inline + var byte return_value = 0x00 + + if data >= "A" then + if data >= "a" then + return_value = data - ( "a" - 10 ) + else + return_value = data - ( "A" - 10 ) + end if + else + return_value = data - "0" + end if + + -- return return_value & 0x0f + return return_value + +end function + +function ascii2_to_hex( byte in nibbles[2]) return byte is + return ( ascii_nibble_to_hex( nibbles[0] ) << 4 ) + ascii_nibble_to_hex( nibbles[1] ) +end function + + +-- this will be the slave address. It looks like: +-- 0b_0101_1100 +-- => 0b_0101_110 : 7-bits address +-- => 0 : 8th bit is to specify read or write operation. +-- Value can be anything, it does not matter while init +const byte SLAVE_ADDRESS = 0xA0 +--i2c_hw_slave_init(SLAVE_ADDRESS) + + +-- simulated memory +var volatile byte memory[32]; +var volatile word address_pointer = 0; + +procedure usage() is + const byte ustr1[] = "Supported comamnds\r\n" + const byte ustr2[] = "=============================================================================\r\n" + const byte ustr3[] = "bAAt0RB : receive_byteaddr; I2C address AA, send t0, receive RB bytes\r\n" + const byte ustr4[] = "wAAt0t1RB : receive_wordaddr; I2C address AA, send t0 & t1, receive RB bytes\r\n" + const byte ustr5[] = "xAATBto..tnRB : send receive; I2C address AA, send TB bytes, data (t0..tn), receive RB bytes\r\n" + const byte ustr6[] = "s : bus scan\r\n" + const byte ustr7[] = "t : eeprom test routine (read first 3 bytes, increment 3rd byte by one\r\n" + const byte ustr8[] = "? : show usage information\r\n" + const byte ustr9[] = "Examples:\r\n" + const byte ustr10[]= "bA0FF. send one byte (FF) to I2C address A0, no bytes received\r\n" + const byte ustr11[]= "wA6000108. send word address (0001) to I2C address A6, receive 08 bytes\r\n" + const byte ustr12[]= "xA404BABEFACE0A. send 4 bytes (BABEFACE) to I2C address A4, receive 10 bytes\r\n" + print_string( usb_serial_data, ustr1 ) + print_string( usb_serial_data, ustr2 ) + print_string( usb_serial_data, ustr3 ) + print_string( usb_serial_data, ustr4 ) + print_string( usb_serial_data, ustr5 ) + print_string( usb_serial_data, ustr6 ) + print_string( usb_serial_data, ustr7 ) + print_string( usb_serial_data, ustr8 ) + print_string( usb_serial_data, ustr9 ) + print_string( usb_serial_data, ustr10 ) + print_string( usb_serial_data, ustr11 ) + print_string( usb_serial_data, ustr12 ) +end procedure + +procedure scan_i2c_bus() is + + const byte str_scan[] = "Scanning i2c bus\r\n" + var byte idx + var bit device_found = false + + print_string( usb_serial_data, str_scan ) + + for 127 using idx loop + + var byte address = idx << 1 + var byte r + var byte w + + w = i2c_send_receive(address, 0, 0) + + address = address + 1 + r = i2c_send_receive(address, 0, 0) + + if ( ( r | w ) == true ) then + device_found = true + print_byte_hex( usb_serial_data, idx << 1 ) + + if ( r == true ) then + const byte str_write[] = " write" + print_string( usb_serial_data, str_write ) + end if + if ( r == true ) then + const byte str_read[] = " read" + print_string( usb_serial_data, str_read ) + end if + print_crlf( usb_serial_data ) + end if + end loop + + if device_found == false then + const byte str_no_device[] = "no device found\r\n" + print_string( usb_serial_data, str_no_device ) + end if + +end procedure + +procedure scan_i2c_bus_1() is + + const byte str_scan[] = "Scanning i2c bus\r\n" + var byte idx + var bit device_found = false + + print_string( usb_serial_data, str_scan ) + + for 230 using idx loop + + var byte address = idx + 10 + var byte w + + const byte str_write[] = " scanning " + + print_string( usb_serial_data, str_write ) + print_byte_hex( usb_serial_data, address ) + + usb_serial_flush() + + w = i2c_send_receive(address, 0, 0) + + usb_serial_data = " " + print_bit_truefalse( usb_serial_data, w ) + print_crlf( usb_serial_data ) + usb_serial_flush() + + delay_1ms( 10 ) + + end loop + +end procedure + +procedure access_A0( bit in read_write) is + + const byte str_scan[] = "READ A0 bus\n" + var byte idx + var bit device_found = false + + print_string( usb_serial_data, str_scan ) + + var byte address = 0xA0 + var byte w + + if (read_write ) then + address = address + 1 + end if + + const byte str_write[] = " scanning " + + print_string( usb_serial_data, str_write ) + print_byte_hex( usb_serial_data, address ) + + usb_serial_flush() + + i2c_start() + w = i2c_transmit_byte( address ) + delay_1ms(1) + i2c_stop() + + print_bit_truefalse( usb_serial_data, w ) + usb_serial_flush() + + +end procedure + + + +procedure test_i2c() is + var bit r + var byte i + + -- same as above + r = i2c_receive_wordaddr(0xA0, 0x0000, 3) + + print_bit_truefalse(usb_serial_data, r) + usb_serial_data = " " + + print_byte_hex(usb_serial_data, i2c_rx_buffer[0]); + usb_serial_data = " " + print_byte_hex(usb_serial_data, i2c_rx_buffer[1]); + usb_serial_data = " " + print_byte_hex(usb_serial_data, i2c_rx_buffer[2]); + usb_serial_data = " " + + -- write part (increment 3rd byte at 0x0002) + i2c_tx_buffer[0] = 0 -- high byte address within i2c eeprom + i2c_tx_buffer[1] = 2 -- low byte address within i2c eeprom + i2c_tx_buffer[2] = i2c_rx_buffer[2] + 1 -- data + r = i2c_send_receive(0xA0, 3, 0) + print_bit_truefalse(usb_serial_data, r) + usb_serial_data = " " + + print_crlf(usb_serial_data) + +end procedure + +var byte msg_state = 0 +var byte hex_nibbles[2] + +const byte ARG_BUFFER_SIZE = 0x40 +var byte arg_buffer[ ARG_BUFFER_SIZE ] +var byte arg_buffer_idx = 0 + +const byte COMMAND_IDLE = "?" +const byte COMMAND_READ = "r" +const byte COMMAND_WRITE = "w" +const byte COMMAND_EXCHANGE = "x" + +var byte command = COMMAND_IDLE +var byte nibble_low +var byte nibble_high +var byte nibble + +-- read command +-- r + +function parse_digit( byte in ch ) return byte is + var byte nibble = 0xFF + + if ch >= "0" then + if ch <= "9" then + nibble = ch - "0" + else + if ch >= "A" then + if ch <= "F" then + nibble = ch - "A" + 10 + else + if ch >= "a" then + if ch <= "f" then + nibble = ch - "a" + 10 + end if + end if + end if + end if + end if + end if + return nibble +end function + +procedure handle_response( bit in is_ok) is + usb_serial_data = "<" + --usb_serial_data = command + if !is_ok then + usb_serial_data = "N" + usb_serial_data = "O" + usb_serial_data = "K" + print_crlf( usb_serial_data ) + else + usb_serial_data = "O" + usb_serial_data = "K" + usb_serial_data = ">" + usb_serial_data = " " + end if + + command = COMMAND_IDLE + msg_state = 0x00 +end procedure + +procedure handle_command() is + const byte str1[] = " Execute " + print_string( usb_serial_data, str1 ) + usb_serial_data = command + usb_serial_data = " " + var byte idx + for arg_buffer_idx using idx loop + print_byte_hex( usb_serial_data, arg_buffer[ idx ] ) + usb_serial_data = "," + end loop + + case command of + COMMAND_READ: + block + var bit r + var byte i + var byte device at arg_buffer[ 0 ] + var byte num_bytes at arg_buffer[ 3 ] + var word word_addr + var byte bt_word_addr[2] at word_addr + bt_word_addr[0] = arg_buffer[ 2 ] + bt_word_addr[1] = arg_buffer[ 1 ] + -- same as above + r = i2c_receive_wordaddr( device, word_addr, num_bytes) + + usb_serial_data = "@" + print_byte_hex( usb_serial_data, num_bytes) + usb_serial_data = "@" + + usb_serial_data = " " + print_bit_truefalse(usb_serial_data, r) + usb_serial_data = "=" + + handle_response( r ) + + for num_bytes using idx loop + print_byte_hex(usb_serial_data, i2c_rx_buffer[idx]) + usb_serial_data = " " + end loop + print_crlf( usb_serial_data ) + + end block + COMMAND_EXCHANGE: + block + var bit r + var byte i + var byte device at arg_buffer[ 0 ] + var byte num_bytes_tx at arg_buffer[ 1 ] + + i = 2 + for num_bytes_tx using idx loop + i2c_rx_buffer[idx] = arg_buffer[ i ] + i = i + 1 + end loop + + var byte num_bytes_rx + num_bytes_rx = arg_buffer[ i ] + i = i + 1 + + -- same as above + r = i2c_send_receive( device, num_bytes_tx, num_bytes_rx ) + + usb_serial_data = " " + print_bit_truefalse(usb_serial_data, r) + usb_serial_data = "=" + + handle_response( r ) + + for num_bytes_rx using idx loop + print_byte_hex(usb_serial_data, i2c_rx_buffer[idx]) + usb_serial_data = " " + end loop + print_crlf( usb_serial_data ) + + end block + otherwise + block + handle_response( false ) + end block + end case + +end procedure + +var byte nibbles + +-- main loop +forever loop + -- poll the usb ISR function on a regular base, in order to + -- serve the USB requests + usb_serial_flush() + + -- check if USB device has been configured by the HOST + if ( usb_cdc_line_status() != 0x00 ) then + if !has_shown_welcome_msg then + has_shown_welcome_msg = true + print_string( usb_serial_data, str_welcome ) + end if + else + has_shown_welcome_msg = false + end if + + -- check for input character + if usb_serial_read( ch ) then + usb_serial_data = ch + case msg_state of + 0: block + case ch of + "U": + block + usb_serial_data = "!" + test_i2c() + -- target_address + + end block + "t": + block + usb_serial_data = "!" + test_i2c() + end block + + "s": + block + scan_i2c_bus_1() + end block + + "?": + block + usage() + end block + + + "S": + block + scan_i2c_bus() + end block + + "u": + block + test_i2c() + end block + + + "a": block + access_A0( 0 ) + end block + "b": block + access_A0( 1 ) + end block + 27: block + + end block + 10,13: block + + end block + + COMMAND_READ, + COMMAND_EXCHANGE, + COMMAND_WRITE: block + nibbles = 0 + msg_state = 1 + arg_buffer_idx = 0 + command = ch + end block + + otherwise + block + const byte str1[] = " Unknown command\r\n" + usb_serial_data = ch + print_string( usb_serial_data, str1 ) + end block + end case + end block + + + 1: block + if ( ( ch == "." ) | ( ch == 10 ) | ( ch == 13 ) ) then + handle_command() + else + nibble_high = parse_digit( ch ) + if nibble_high < 0x10 then + msg_state = 2 + else + handle_response( false ) + end if + end if + end block + + + 2: block + nibble_low = parse_digit( ch ) + if nibble_high < 0x10 then + var byte nibble = ( nibble_high << 4 ) + nibble_low + msg_state = 1 + --if nibbles == 0 then + -- i2c_device = nibble + --else + arg_buffer[ arg_buffer_idx ] = nibble + arg_buffer_idx = arg_buffer_idx + 1 + --end if + nibbles = nibbles + 1 + else + handle_response( false ) + end if + end block + end case + +end if + +end loop ======================================= --- /dev/null +++ /trunk/project/pic18f14k50_usb_io/examples/usb_serial_converter.jal Tue Jul 28 16:31:21 2009 @@ -0,0 +1,201 @@ +-- ----------------------------------------------------------------------------- +-- Title: demo of usb_serial library +-- Author: Albert Faber, Copyright (c) 2009, all rights reserved. +-- Adapted-by: - +-- Compiler: >=2.4j +-- +-- This file is part of jallib (http://jallib.googlecode.com) +-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php) +-- +-- Description: This application demonstratesest USB Serial library, this application +-- is a RS-232 converter for the PIC18f14k50 USB IO Board. Once the USB device has been +-- detected by the host operating system, it will create a virtual COM port. You should +-- be able to open this (virtual) COM port on the host computer, using a standard terminal +-- application. Connect the RS-232 output to a computer with a regular RS-232 port (can be +-- the same computer though) and open also this COM port with a terminal application. +-- When typing character in one terminal it should end up in the other terminal application. +-- -- +-- +-- Sources: http://www.usb.org for USB specifications +-- +-- Notes: compiled with following flags +-- -loader18 -no-fuse ; when the microchip 18f bootloader is present +-- add -no-variable-reuse when debugging flags are set due to a compiler issue +-- - +-- Some instructions for use: +-- - +-- 1. Make sure you logon as administrator, or with administrator privileges, +-- on Windows XP. +-- 2. If needed convert to the correct PIC device (change include file), +-- adapt fuses fuses and speed +-- 3. Create a basic board with USB PIC device +-- 4. Compile the this file and program the hex file into the PIC. +-- 5. After a (short) while you'll get the "new hardware found" message for +-- "JALLIB Serial". Point Windows manually to the driver in the +-- driver directory xp_drivers The drivers can be found at: +-- http://groups.google.com/group/jallib/ (usb_cdc_drivers_winxp32.zip) +-- Ignore the warning that this driver doesn't have a certificate. +-- Wait for the installation to complete. +-- 6. Open the Windows Device Manager and check out the "ports" section. A new +-- communications port will be there, with a COM number. +-- 7. Open a terminal program (for example HyperTerminal) and select the COM +-- port you found in the previous step. Pick any baudrate, Windows will +-- accommodate automatically, and use 8-N-1-no for the other settings. +-- 8. Observe input/output of RS-232 COM port of the USB IO board with another terminal +-- application +-- 9. Have fun, adapt to your needs +-- +-- ------------------------------------------------------ +-- +-- +-- + +;@jallib section chipdef +-- chip setup +include 18f14k50 + +-- even though the external crystal is 20 MHz, the configuration is such that +-- the CPU clock is derived from the 96 Mhz PLL clock (div2), therefore set +-- target frequency to 48 MHz +pragma target clock 48_000_000 + + +-- fuses +-- fuses +pragma target CPUDIV NO_CPU_SYSTEM_CLOCK_DIVIDE +pragma target USBDIV USB_CLOCK_COMES_DIRECTLY_FROM_THE_OSC1_OSC2_OSCILLATOR_BLOCK__NO_DIVIDE +pragma target OSC HS +pragma target PLLEN OSCILLATOR_MULTIPLIED_BY_4 +pragma target FCMEN DISABLED +pragma target IESO DISABLED +pragma target PWRTE DISABLED -- power up timer +pragma target BROWNOUT DISABLED -- no brownout detection +pragma target VOLTAGE V30 -- brown out voltage +pragma target WDT DISABLED -- no watchdog +pragma target WDTPS P32768 -- watch dog saler setting +pragma target MCLR EXTERNAL -- no master reset +pragma target LVP DISABLED -- no low-voltage programming +pragma target XINST ENABLED -- extended instruction set +pragma target BACKBUG DISABLED -- background debugging +pragma target CP_0 DISABLED -- code block 0 not protected +pragma target CP_1 DISABLED -- code block 1 not protected +pragma target CPB DISABLED -- bootblock code not write protected +pragma target WRT_0 DISABLED -- table writeblock 0 not protected +pragma target WRT_1 DISABLED -- table write block 1 not protected +pragma target WRTB DISABLED -- bootblock not write protected +pragma target WRTC DISABLED -- config not write protected +pragma target EBTR_0 DISABLED -- table read block 0 not protected +pragma target EBTR_1 DISABLED -- table read block 1 not protected +pragma target EBTRB DISABLED -- boot block not protected +pragma target HFOFST THE_SYSTEM_CLOCK_IS_HELD_OFF_UNTIL_THE_HFINTOSC_IS_STABLE + + +-- forward procedure declaration +procedure usb_cdc_line_coding_changed_callback() + +include delay +include usb_serial +include print + +procedure _serial_receive_interrupt_handler() is + pragma interrupt + + + if (PIR1_RCIF == TRUE) then -- UART receive interrupt + + if ((RCSTA_OERR == TRUE) | (RCSTA_FERR == TRUE)) then -- frame/overr error + var byte x = RCREG -- flush hardware buffer + while RCSTA_OERR == TRUE loop -- overrun state + RCSTA_CREN = FALSE -- disable UART + RCSTA_CREN = TRUE -- re-enable UART + x = RCREG -- \ flush hardware buffers + x = RCREG -- / + end loop -- until no more overrun + + else -- data without errors + var byte ch = RCREG + -- usb_serial_data = RCREG + usb_cdc_putc( ch ) + end if + end if +end procedure + +procedure change_baudrate( dword in baud_rate) is + + -- compiler issue with -const-detect + if true then + var dword fosc_div4 = dword( target_clock ) / 4 + else + var dword fosc_div4 + fosc_div4 = dword( target_clock ) + fosc_div4 = fosc_div4 / 4 + end if + + var dword div_factor = fosc_div4 / baud_rate - 1 + var word div_wfactor = word( div_factor ) + var byte div_btfactor[2] at div_wfactor + + TXSTA_BRGH = true + BAUDCON_BRG16 = true + + SPBRGH = div_btfactor[1] + SPBRG = div_btfactor[0] + +end procedure + + +-- Initializes the serial port, calculates baudrate registers. +procedure serial_hw_init() is + RCSTA = 0b0000_0000 -- reset + RCSTA_SPEN = enabled -- serial port enable + RCSTA_CREN = enabled -- continuous receive enable + + TXSTA = 0b0000_0100 -- reset (16 bit, asyn) + TXSTA_TXEN = enabled -- UART transmit enabled + -- TXSTA_SYNC = true + TXSTA_BRGH = true + BAUDCON_BRG16 = true + + PIE1_RCIE = enabled -- UART receive int. enable + -- (PIE1_TXIE dynamically) + INTCON_PEIE = enabled -- periferal + INTCON_GIE = enabled -- general +end procedure + + +procedure usb_cdc_line_coding_changed_callback() is + + if ( cdc_line_coding_dte_rate > 115200 ) then + cdc_line_coding_dte_rate = 115200 + end if + change_baudrate( cdc_line_coding_dte_rate ) + serial_hw_init() +end procedure + +-- disable analog +enable_digital_io() + + +-- setup the USB serial library +usb_serial_init() + +var byte ch + +-- main loop +forever loop + -- poll the usb ISR function on a regular base, in order to + -- serve the USB requests + usb_serial_flush() + + -- check if USB device has been configured by the HOST + if ( usb_cdc_line_status() != 0x00 ) then + + -- check for input character + while usb_serial_read( ch ) loop + -- echo input character + TXREG = ch + while ! PIR1_TXIF loop end loop + end loop + + end if +end loop ======================================= --- /trunk/include/peripheral/i2c/i2c_hardware_aliases.jal Mon Jul 27 14:40:40 2009 +++ /trunk/include/peripheral/i2c/i2c_hardware_aliases.jal Tue Jul 28 16:31:21 2009 @@ -99,7 +99,3 @@ -- ----------------------------------------------------------------------------- - - - - --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jallib" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/jallib?hl=en -~----------~----~----~----~------~----~------~--~---
