Uart Access

The pyCPU comes with an embedded RS232 Hardware-Unit:

The unit does a 8 bit transmission with no parity and one Stopbit. The usage of the now tightly  integrated UART unit  is demonstrated in the code below. This main.py file sends 0,1,2,3,4,5,6,7,8,9,….. with the RS232-Unit. Everything that is received from the RS232-Unit shown at the IO-Port: PORTD_OUT.

PYCPU_CONFIGS={ 
  "PREDEFINED_IO_ADDRESSES":{'PORTA_IN':0,'PORTB_IN':1,'PORTC_OUT':2,'PORTD_OUT':3,'RS232Data':4,'RS232_READ_ADDRESS':5,'RS232_RECEIVE_ADDRESS':6,'RS232_WRITEBUFFER_FULL':7}, 
  "Prog_mem_SIZE":1024,          # Spezifies the minimal programm length, the programm memory will have this lenght even if the current programm dont needs it  
  "Var_mem_SIZE":128,            # Size of the Memory for local variables 
  "Globals_mem_SIZE":128,        # Size of Global memory + constants memory + PREDEFINED_IO_ADDRESSES
  "STACK_SIZE":4,                # Minimal Size of one local Stackblock, that the cpu implements, even if the programm needs less stack
  "CPU_BITWIDTH":8,              # Bitwidth of integer values
  "CPU_CLK_FREQ":12e6,           # Needed to for RS232 Baudrate calc
  "CPU_RS232_BAUDRATE":115200,   # Baudrate of the RS232 Unit 
  "NR_FUNCTION_ARGUMENTS":8,     # Limits the maximal number of arguments in a function call
  "NR_NESTED_LOOPS":32           # Limits the maximal number of nested Loops
}
def main():
  global PORTA_IN,PORTB_IN,PORTC_OUT,PORTD_OUT,RS232Data,RS232_READ_ADDRESS,RS232_RECEIVE_ADDRESS,RS232_WRITEBUFFER_FULL
  x=0
  RS232_READ_ADDRESS=0
  while 1:
    ##### RS232 Transmitting ######
    # Only write something to the Transmit buffer if not full
    if not RS232_WRITEBUFFER_FULL:
      # Write the value of x to the RS232 Transmit 
      # buffer, sending the Data is then started automatically
      RS232Data=x    
      x=x+1

    ##### RS232 Receiving ######
    # Wait until a byte is received in the Receive-Buffer
    if RS232_READ_ADDRESS!=RS232_RECEIVE_ADDRESS:  
      # Read data from RS232 receive buffer at the RS232_READ_ADDRESS
      PORTD_OUT=RS232Data    
      # Set the Read address the address where the next data will be received
      RS232_READ_ADDRESS=RS232_RECEIVE_ADDRESS

Advantages:

  • Easier access to RS232 than in most Microcontrollers today
  • Receive-/ and Transmit-Buffer (Buffersize is set in the hardware description)

Disadvantages:

  • No interrupt
  • No dynamic baudrate configuration
  • Only 8 Bit Mode, no parity bit and 1 Stopbit
  • If you want more options you have to upgrade the hardware description

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: