Category Archives: pyCPU uart access

Using the UART unit of the pyCPU

The pyCPU has a UART unit now.

The Baudrate of the UART unit is set in the Myhdl code. 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:

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 } 

def CPU_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 if something is in the Transmit-Buffer, sending
      # is 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 the current 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
Follow

Get every new post delivered to your Inbox.