views:

33

answers:

1

I see some code which are going to read/write some peripheral registers. I see the write operations are all followed with some delays. It seems to me that SW needs to wait for the value takes effect on HW. Some web page say register will get update every clock cycle. I guess that's the reason for waiting. Well, my questions are:

  1. Does read operation needs such delay as well? If yes, when SW wants to read/write one register, what's the correct sequence to do so? Like

[DELAY]

tmp=some_register //read

some_register=tmp+1 //write

[DELAY]

or

tmp=some_register //read

[DELAY]

some_register=tmp+1 //write

[DELAY]

  1. What if SW don't have such delay after writing operation? I actually see some write operation without that delay can works properly on some slower CPU. But when the code is running at some faster CPU, it fails. I think the delay is reasonable. I just need a explaination why it works for slower CPU.

  2. I'm not very familiar with functionality of register. Any helpful materials or links?

A: 

Do your delay after the write.

  • read
  • write
  • delay

The hardware ; an external serial board will need some time for things to happen. CPU's can write data to IO registers very quickly. Many IO chips do not accept changes that often.

The simplest devices on IO ports are latches connected to a LED. If you write 1, the light goes on. If you write 0 it goes off. If you write 0,1,0,1 to the device faster than it can change state, it won't update. The speed that the device can reliably take data from the computer's data bus is often faster than it can change outputs to the real world. There are bigger voltages, and higher current in IO devices, and they take more time to change than the small signals required to register changes. To keep power consumption down, many devices use clocked output pins. This is because CMOS devices ( CMOS is a common chip fab technique these days) expend power proportional to the rate of change of the bits. Holding all the bits th same uses littel power. Changes use it.

Devices with clocked IO ( and serial ports are the definitive case of this ) will only change/update on the next clock.

When you use a serial port to send serial data, it can take millions of CPU clocks for the data to be transmitted ( if the speed (baud rate) of the serial port is set very low)

The simplest correct delay for a simple serial port is to read the "ready bit" of the "status register", and when it's ready to send another byte, write it.

The advanced solution uses buffers and interrupts. That is a bit advanced for you to worry about yet.

Tim Williscroft