| 1 |
5 |
sinclairrf |
; Copyright 2012, Sinclair R.F., Inc.
|
| 2 |
|
|
;
|
| 3 |
|
|
; I2C EEPROM design example:
|
| 4 |
|
|
; Write a 16-byte string to the EEPROM and then read and display it.
|
| 5 |
|
|
|
| 6 |
|
|
.constant C_I2C_EEPROM_ADDR ${0xa*16+0x4*2}
|
| 7 |
|
|
|
| 8 |
|
|
.include ../../lib_i2c.s
|
| 9 |
|
|
|
| 10 |
|
|
.memory RAM ram
|
| 11 |
|
|
.variable ram__msg 0*16
|
| 12 |
|
|
|
| 13 |
|
|
.memory ROM messages
|
| 14 |
|
|
.variable msg__bad_device_number N"Bad Device Number"
|
| 15 |
|
|
.variable msg__write_address_rejected N"Write Address Rejected"
|
| 16 |
|
|
.variable msg__rejected_data N"Data Rejected"
|
| 17 |
|
|
.variable msg__read_address_rejected N"Read Address Rejected"
|
| 18 |
|
|
.variable msg__read_state_rejected N"Read State Rejected"
|
| 19 |
|
|
|
| 20 |
|
|
.main
|
| 21 |
|
|
|
| 22 |
|
|
; Write a 16-byte, null-terminated string to the EEPROM.
|
| 23 |
|
|
C"Hello World!!\r\n\0"
|
| 24 |
|
|
.call(i2c_send_start)
|
| 25 |
|
|
.call(i2c_send_byte,${C_I2C_EEPROM_ADDR|0}) .jumpc(error__bad_device_number)
|
| 26 |
|
|
.call(i2c_send_byte,0) .jumpc(error__write_address_rejected)
|
| 27 |
|
|
:write_loop
|
| 28 |
|
|
1- .call(i2c_send_byte,swap) .jumpc(error__rejected_data)
|
| 29 |
|
|
.jumpc(write_loop,nop) drop
|
| 30 |
|
|
.call(i2c_send_stop)
|
| 31 |
|
|
|
| 32 |
|
|
;
|
| 33 |
|
|
; Read the null-terminated string from the EEPROM (after the write cycle finishes)
|
| 34 |
|
|
;
|
| 35 |
|
|
|
| 36 |
|
|
; Put the address on the bus until the EEPROM acknowledges it.
|
| 37 |
|
|
:write_wait
|
| 38 |
|
|
.call(i2c_send_start)
|
| 39 |
|
|
${C_I2C_EEPROM_ADDR|0} .call(i2c_send_byte) 0= .jumpc(write_wait_done)
|
| 40 |
|
|
.call(i2c_send_stop)
|
| 41 |
|
|
.jump(write_wait)
|
| 42 |
|
|
:write_wait_done
|
| 43 |
|
|
|
| 44 |
|
|
; Send the start address for the reads followed by a start (with no stop)
|
| 45 |
|
|
|
| 46 |
|
|
.call(i2c_send_restart)
|
| 47 |
|
|
|
| 48 |
|
|
; Put the EEPROM into the read state
|
| 49 |
|
|
${C_I2C_EEPROM_ADDR|1} .call(i2c_send_byte) .jumpc(error__read_state_rejected)
|
| 50 |
|
|
|
| 51 |
|
|
; Read the EEPROM and write each byte to memory until the null terminator is
|
| 52 |
|
|
; encountered. Add the CRLF pair
|
| 53 |
|
|
ram__msg >r
|
| 54 |
|
|
:read_loop
|
| 55 |
|
|
.call(i2c_read_byte,0)
|
| 56 |
|
|
dup r> .store+(ram) >r .jumpc(read_loop)
|
| 57 |
|
|
r> drop
|
| 58 |
|
|
|
| 59 |
|
|
; Send the string copied from the EEPROM to the UART.
|
| 60 |
|
|
ram__msg
|
| 61 |
|
|
:uart_loop .fetch+(ram) over 0= .jumpc(uart_done)
|
| 62 |
|
|
swap .outport(O_UART_TX)
|
| 63 |
|
|
:uart_wait .inport(I_UART_TX) .jumpc(uart_wait)
|
| 64 |
|
|
.jump(uart_loop)
|
| 65 |
|
|
:uart_done
|
| 66 |
|
|
drop drop .jump(infinite)
|
| 67 |
|
|
|
| 68 |
|
|
:error__bad_device_number
|
| 69 |
|
|
.jump(error_with_clear,msg__bad_device_number)
|
| 70 |
|
|
:error__write_address_rejected
|
| 71 |
|
|
.jump(error_with_clear,msg__write_address_rejected)
|
| 72 |
|
|
:error__rejected_data
|
| 73 |
|
|
.jump(error_with_clear,msg__rejected_data)
|
| 74 |
|
|
:error__read_address_rejected
|
| 75 |
|
|
.jump(error_print_done,msg__read_address_rejected)
|
| 76 |
|
|
:error__read_state_rejected
|
| 77 |
|
|
.jump(error_print_done,msg__read_state_rejected)
|
| 78 |
|
|
; Print the error message and then wait forever.
|
| 79 |
|
|
:error_with_clear
|
| 80 |
|
|
>r
|
| 81 |
|
|
; clear the count-encoded string from the data stack
|
| 82 |
|
|
:clear 1- .jumpc(clear,nip) drop
|
| 83 |
|
|
; read and display the error message
|
| 84 |
|
|
r>
|
| 85 |
|
|
:error_print_loop
|
| 86 |
|
|
.fetch+(messages) over 0= .jumpc(error_print_done)
|
| 87 |
|
|
swap .outport(O_UART_TX)
|
| 88 |
|
|
:error_print_wait .inport(I_UART_TX) .jumpc(error_print_wait)
|
| 89 |
|
|
.jump(error_print_loop)
|
| 90 |
|
|
:error_print_done
|
| 91 |
|
|
drop drop
|
| 92 |
|
|
|
| 93 |
|
|
:infinite
|
| 94 |
|
|
.jump(infinite)
|
| 95 |
|
|
|