Revision: 1805
Author: wattymage
Date: Mon Mar 15 10:56:21 2010
Log: initial alpha sample
http://code.google.com/p/jallib/source/detail?r=1805

Added:
 /trunk/catpad/serial_hardwaremw.jal

=======================================
--- /dev/null
+++ /trunk/catpad/serial_hardwaremw.jal Mon Mar 15 10:56:21 2010
@@ -0,0 +1,188 @@
+-- Title: USART hardware control
+-- Author: Stef Mientki, Copyright (c) 2002..2006, all rights reserved.
+-- Adapted-by: Sebastien Lelong.
+-- Compiler: >=2.4g
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the ZLIB license (http://www.opensource.org/licenses/zlib-license.html)
+--
+-- Description: USART hardware control.
+-- Routines for sending and receiving through the PIC-usart,
+-- both asynchrone and synchrone are supported.
+-- Baudrate can simply be set through a human constant,
+-- because the baudrate depending registers are calculated by this unit.
+-- Baudrate is calculated, starting at the high baudrate flag,
+-- which will ensure the highest possible accuracy.
+--
+-- TODO: should this be configurable ?
+-- Transmission parameters are 8 databits, 1 stopbit, no parity, no handshake.
+--
+
+include usart_common
+
+-- Initializes the serial port, calculates baudrate registers.
+procedure serial_hw_init() is
+   -- Calculate and Load baudrate generator
+   _calculate_and_set_baudrate()
+
+   -- disable all USART interrupts
+   PIE1_RCIE = false
+   PIE1_TXIE = false
+
+   -- Enable transmitter : TXSTA_TXEN=1 (preserve TXSTA_BRGH)
+   TXSTA_TXEN = true
+
+   -- Enable serial port : RCSTA_SPEN=1
+   -- Enable receiving   : RCSTA_CREN=1
+   RCSTA = 0x90
+
+end procedure
+
+
+-- Disables USART so ports can be used (temporary) for other purposes.
+-- USART can be enabled again by calling serial_hw_enable()
+procedure serial_hw_disable() is
+   -- wait till running transmissions are finished
+   while !TXSTA_TRMT loop end loop
+   -- Disable Serial port:
+   RCSTA_SPEN = false
+end procedure
+
+
+-- Enables USART
+procedure serial_hw_enable() is
+   -- Enable Serial port
+   RCSTA_SPEN = true
+end procedure
+
+procedure serial_twostop_bits ( bit in twoBits) is
+   if twoBits then
+       RCSTA_RX9 = on
+       RCSTA_RX9D = on
+       TXSTA_TX9 = on
+       RCSTA_RX9D = on
+   else
+       RCSTA_RX9 = off
+       RCSTA_RX9D = off
+       TXSTA_TX9 = off
+       RCSTA_RX9D = off
+   end if
+end procedure
+
+-- -----------------------------------------------------------------------
+-- serial_hw_write - write char to serial port, blocking
+-- -----------------------------------------------------------------------
+-- Asynchronuous serial send routine, using the TX pin
+-- Sends byte X (8 bit with no parity) to the serial port
+-- First checks (and waits if necessary) if transmit buffer is empty
+-- -----------------------------------------------------------------------
+procedure serial_hw_write(byte in data) is
+   -- wait until TXREG is empty
+   while ! PIR1_TXIF loop end loop
+   -- then put new byte in TXREG (prepare for transmission)
+   TXREG = data
+end procedure
+
+
+-- like Serial_H_write, but then with a word as input
+-- The MSB is outputed first
+procedure serial_hw_write_word(word in data) is
+   var byte DX[2] at data
+   -- can be blocked by debugging
+   while ! PIR1_TXIF loop end loop
+   TXREG = DX[1]
+   asm nop  -- this is necessary for damned good optimized compilers
+            -- loading of the TXREG doesn't immediatly set PIR1_TXIF !!!!
+   while ! PIR1_TXIF loop end loop
+   TXREG = DX[0]
+end procedure
+
+
+-- -----------------------------------------------------------------------
+-- _serial_hw_read - internal use only!
+-- -----------------------------------------------------------------------
+-- (using this inline function for serial_hw_data'get saves a stack level)
+-- Returns true if a character was received, otherwise returns false.
+-- Overrun error flag is cleared.
+-- -----------------------------------------------------------------------
+function _serial_hw_read(byte out data) return bit is
+   pragma inline
+
+   -- test if byte available, and if so,
+   -- get byte and transport to outer world
+   if PIR1_RCIF then
+      data = RCREG
+      PIR1_RCIF   = false       -- [email protected] 12-sept-08
+   else
+      return false  ;result = false
+   end if
+
+   if RCSTA_OERR then
+      RCSTA_CREN = false
+      RCSTA_CREN =true
+   end if
+
+   return true
+end function
+
+
+-- -----------------------------------------------------------------------
+-- serial_hw_read - read char if available (non-blocking)
+-- -----------------------------------------------------------------------
+-- Returns true if a character was received, otherwise returns false.
+-- Overrun error flag is cleared.
+-- -----------------------------------------------------------------------
+function serial_hw_read(byte out data) return bit is
+   return _serial_hw_read(data)
+end function
+
+
+-- Here Serial read and write are definied as pseudo variables
+-- so you use them as normal vars, like
+--  * wait for character being received,
+--  * then echo the inverted character
+-- {{{
+-- serial_hw_data = ! serial_hw_data
+-- }}}
+--
+-- these procedures will wait till they can perform their action
+-- therefore it's better to use to following construct
+-- {{{
+-- -- if charater received, echo the inverted character
+-- if  serial_hw_data_available then
+--     serial_hw_data = ! serial_hw_data
+-- end if
+-- -- do other things
+-- }}}
+--
+procedure serial_hw_data'put(byte in data) is
+   serial_hw_write(data)
+end procedure
+
+
+function serial_hw_data'get() return byte is
+   var byte data
+   while ! _serial_hw_read(data) loop end loop
+   return data
+end function
+
+
+-- -----------------------------------------------------------------------
+-- raw interface
+-- -----------------------------------------------------------------------
+
+-- some variables made available under a general (pic-independent) name
+alias serial_hw_data_available is PIR1_RCIF
+alias serial_hw_data_ready is PIR1_TXIF
+
+-- These are real raw procedures, declared as pseudo variables
+-- the user is totally responsible for testing the transmit/receive
+-- flag before using these functions
+procedure serial_hw_data_raw'put(byte in data) is
+   TXREG = data
+end procedure
+
+function serial_hw_data_raw'get() return byte is
+   return RCREG
+end function
+

--
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.

Reply via email to