View previous topic :: View next topic |
Author |
Message |
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Sun May 09, 2010 6:47 am Post subject: Help With Slimming Code |
|
|
Hello everyone.
I am working on a project that uses the HopeRF RFM22/23 radio module. I have been porting the original RFM12 project that Dave did a couple of years ago (with his help I must say). Anyway, the RFM22/23 is significantly different in that it has much more features and options.
The initialization parameters of the RFM23 are many many more than the RFM12 and the following code eats up 43% of my ATMEGA88's FLASH.
I am hoping some suggestions to reduce code size might be found in this forum. Although the following is hard coded, it would, in the end, be very special if I was able to us EEPROM for this initilization data so that commands may change it on the fly.
Again, any ideas to reduce the code size would be greatly appreciated!
Code: |
'Init_data:
Sub Init_data
Call Spi_write(&H06 , &H00 , 1 ) '// interrupt all disable
Call Spi_write(&H07 , &H01 , 1 ) '// to ready mode 'reset value = 00000001 (&H01)
Call Spi_write(&H09 , &H7F , 1 ) '// cap = 12.5pf 'not using these initially, but leave it for now
Call Spi_write(&H0a , &H05 , 1 ) '//clk output is 2MHz 'not using these initially, but leave it for now
' Call Spi_write(&H0b , &HF4 , 1) '// GPIO0 is for Rx data output
' Call Spi_write(&H0c , &HEF , 1) '// GPIO1 Tx/Rx data clk output
' Call Spi_write(&H0d , &H00 , 1) '// GPIO2 for MCLK output
' Call Spi_write(&H0e , &H00 , 1) '//GPIO port use default value
Call Spi_write(&H0b , &HD7 , 1 ) '// GPIO0 is for Rx data output
Call Spi_write(&H0c , &HD8 , 1 ) '// GPIO1 Tx/Rx data clk output
Call Spi_write(&H0d , &H00 , 1 ) '// GPIO2 for MCLK output
Call Spi_write(&H0e , &H00 , 1 ) '//GPIO port use default value
Call Spi_write(&H0f , &H70 , 1 ) '// NO ADC used
Call Spi_write(&H10 , &H00 , 1 ) '//no adc used
Call Spi_write(&H12 , &H00 , 1 ) '// no temperature sensor used
Call Spi_write(&H13 , &H00 , 1 ) '// no temperature sensor used
Call Spi_write(&H70 , &H20 , 1 ) '// no mancheset code, no data whiting, data rate < 30Kbps
Call Spi_write(&H1c , &H04 , 1 ) '// IF filter bandwidth
Call Spi_write(&H1d , &H40 , 1 ) '// AFC LOOP
Call Spi_write(&H1e , &H08 , 1 ) '//AFC timing
Call Spi_write(&H20 , &H41 , 1 ) '//clock recovery
Call Spi_write(&H21 , &H60 , 1 ) '//clock recovery
Call Spi_write(&H22 , &H27 , 1 ) '//clock recovery
Call Spi_write(&H23 , &H52 , 1 ) '//clock recovery
Call Spi_write(&H24 , &H00 , 1 ) '//clock recovery timing
Call Spi_write(&H25 , &H06 , 1 ) '//clock recovery timing
Call Spi_write(&H6e , &H27 , 1 ) '// Tx data rate 1
Call Spi_write(&H6f , &H52 , 1 ) '// Tx data rate 0
Call Spi_write(&H30 , &H8C , 1 ) '// data access control
Call Spi_write(&H32 , &HFF , 1 ) '// header control
Call Spi_write(&H33 , &H42 , 1 ) '// header 3, 2, 1,0 used for head length, fixed packet length, synchronize word length 3, 2,
Call Spi_write(&H34 , &H64 , 1 ) '// PREAMBLE 64 nibble = 32byte preamble
Call Spi_write(&H35 , &H20 , 1 ) '//&h35 need to detect 20bit preamble
Call Spi_write(&H36 , &H2D , 1 ) '// synchronize word
Call Spi_write(&H37 , &HD4 , 1 )
Call Spi_write(&H38 , &H00 , 1 )
Call Spi_write(&H39 , &H00 , 1 )
Call Spi_write(&H3a , &H53 , 1 ) '// set tx header "S"
Call Spi_write(&H3b , &H6F , 1 ) ' "o"
Call Spi_write(&H3c , &H6E , 1 ) ' "n"
Call Spi_write(&H3d , &H67 , 1 ) ' "g"
Call Spi_write(&H3e , 17 , 1 ) '// total tx 17 byte
Call Spi_write(&H3f , &H53 , 1 ) '// set rx header' "S"
Call Spi_write(&H40 , &H6F , 1 ) ' "o"
Call Spi_write(&H41 , &H6E , 1 ) ' "n"
Call Spi_write(&H42 , &H67 , 1 ) ' "g"
Call Spi_write(&H43 , &HFF , 1 ) '// all the bit to be checked
Call Spi_write(&H44 , &HFF , 1 ) '// all the bit to be checked
Call Spi_write(&H45 , &HFF , 1 ) '// all the bit to be checked
Call Spi_write(&H46 , &HFF , 1 ) '// all the bit to be checked
Call Spi_write(&H6d , &H07 , 1 ) '// tx power to max
Call Spi_write(&H79 , &H0 , 1 ) '// no frequency hopping
Call Spi_write(&H7a , &H0 , 1 ) '// no frequency hopping
' Call Spi_write(&H70 , &H20 , 1) '// no mancheset code, no data whiting, data rate < 30Kbps
Call Spi_write(&H71 , &H22 , 1 ) '// Gfsk, fd[8] =0, no invert for Tx/Rx data, FIFO mode, txclk -->gpio
'Fc = the center Freq. in MHz
'Fr = the Freq. register value.
'Fc = 430 + Fr * 0.0025 (MHz)
'Fr = (Fc - 430) * 400
'Const Fc = 435.00
'Const Fr =(fc - 430) * 400 '2000
'Const Fcmnd = &HA000 + Fr '42960
Call Spi_write(&H72 , &H48 , 1 ) '// frequency deviation setting to 45k = 72*625
Call Spi_write(&H73 , &H0 , 1 ) '// no frequency offset
Call Spi_write(&H74 , &H0 , 1 ) '// no frequency offset
Call Spi_write(&H75 , &H53 , 1 ) '// frequency set to 434MHz
Call Spi_write(&H76 , &H64 , 1 ) '// frequency set to 434MHz
Call Spi_write(&H77 , &H00 , 1 ) '// frequency set to 434MHz
End Sub
|
The above call this function. But again, the code above is what really eats the FLASH (~43%)!
Code: |
Sub Spi_write(d1 As Byte , D2 As Byte , Wr As Byte )
If Wr = 1 Then D1.7 = 1 'Set the write bit high
Timeout = 1
For I = 1 To Payload_size
Rfm23_rx_wait
If Timeout = 0 Then
' Set Rx_led
' Set Error_led
Exit For
End If
If Wr = 0 Then
D1 = &HB0
D2 = &H00
End If
'Send word (SPI_Tx_wrd) to RFM23.
'Also receive word (SPI_Rx_wrd).
'RFM23 Chip select
' Reset Rfm23_nsel 'Active LOW on the nSelect line high Selects chip USED W/O/ FIFO
'send hi byte.
Spi_rx_h = Spimove(d1)
'send lo byte.
Spi_rx_l = Spimove(d2)
Rxbuffer(i) = Spi_rx_l
If Wr = 1 Then Exit For
Next I
'deselect chip.
' Set Rfm23_nsel 'Active HI on the nSelect line high De-Selects chip USED W/O/ FIFO
' Rssi = 0
' If Sndrssi = 1 Then Rssi = Spi_rx_h .2
End Sub
|
Thank you!
Tim |
|
Back to top |
|
|
for_ro
Joined: 11 Nov 2007 Posts: 260
|
Posted: Sun May 09, 2010 11:05 am Post subject: |
|
|
Hi Tim,
assuming, that init is not time critical I would propose something like this:
Code: | Sub Init_data
Restore Init_daten
For I = 1 To 56
Read P1
Read P2
Read P3
Call Spi_write(p1 , P2 , P3)
Next I
End Sub
$data
Init_daten:
Data &H06 , &H00 , 1 '// interrupt all disable
Data &H07 , &H01 , 1 '// to ready mode 'reset value = 00000001 (&H01)
Data &H09 , &H7F , 1 '// cap = 12.5pf 'not using these initially, but leave it for now
Data &H0A , &H05 , 1 '//clk output is 2MHz 'not using these initially, but leave it for now
Data &H0B , &HD7 , 1 '// GPIO0 is for Rx data output
Data &H0C , &HD8 , 1 '// GPIO1 Tx/Rx data clk output
Data &H0D , &H00 , 1 '// GPIO2 for MCLK output
Data &H0E , &H00 , 1 '//GPIO port use default value
Data &H0F , &H70 , 1 '// NO ADC used
Data &H10 , &H00 , 1 '//no adc used
Data &H12 , &H00 , 1 '// no temperature sensor used
Data &H13 , &H00 , 1 '// no temperature sensor used
Data &H70 , &H20 , 1 '// no mancheset code, no data whiting, data rate < 30Kbps
Data &H1C , &H04 , 1 '// IF filter bandwidth
Data &H1D , &H40 , 1 '// AFC LOOP
Data &H1E , &H08 , 1 '//AFC timing
Data &H20 , &H41 , 1 '//clock recovery
Data &H21 , &H60 , 1 '//clock recovery
Data &H22 , &H27 , 1 '//clock recovery
Data &H23 , &H52 , 1 '//clock recovery
Data &H24 , &H00 , 1 '//clock recovery timing
Data &H25 , &H06 , 1 '//clock recovery timing
Data &H6E , &H27 , 1 '// Tx data rate 1
Data &H6f , &H52 , 1 '// Tx data rate 0
Data &H30 , &H8C , 1 '// data access control
Data &H32 , &HFF , 1 '// header control
Data &H33 , &H42 , 1 '// header 3, 2, 1,0 used for head length, fixed packet length, synchronize word length 3, 2,
Data &H34 , &H64 , 1 '// PREAMBLE 64 nibble = 32byte preamble
Data &H35 , &H20 , 1 '//&h35 need to detect 20bit preamble
Data &H36 , &H2D , 1 '// synchronize word
Data &H37 , &HD4 , 1
Data &H38 , &H00 , 1
Data &H39 , &H00 , 1
Data &H3a , &H53 , 1 '// set tx header "S"
Data &H3b , &H6F , 1 ' "o"
Data &H3c , &H6E , 1 ' "n"
Data &H3d , &H67 , 1 ' "g"
Data &H3e , 17 , 1 '// total tx 17 byte
Data &H3f , &H53 , 1 '// set rx header' "S"
Data &H40 , &H6F , 1 ' "o"
Data &H41 , &H6E , 1 ' "n"
Data &H42 , &H67 , 1 ' "g"
Data &H43 , &HFF , 1 '// all the bit to be checked
Data &H44 , &HFF , 1 '// all the bit to be checked
Data &H45 , &HFF , 1 '// all the bit to be checked
Data &H46 , &HFF , 1 '// all the bit to be checked
Data &H6D , &H07 , 1 '// tx power to max
Data &H79 , &H0 , 1 '// no frequency hopping
Data &H7a , &H0 , 1 '// no frequency hopping
Data &H71 , &H22 , 1 '// Gfsk, fd[8] =0, no invert for Tx/Rx data, FIFO mode, txclk -->gpio
Data &H72 , &H48 , 1 '// frequency deviation setting to 45k = 72*625
Data &H73 , &H0 , 1 '// no frequency offset
Data &H74 , &H0 , 1 '// no frequency offset
Data &H75 , &H53 , 1 '// frequency set to 434MHz
Data &H76 , &H64 , 1 '// frequency set to 434MHz
Data &H77 , &H00 , 1 '// frequency set to 434MHz
|
You need to check, whether this is exactly the same.
Data could be in flash or in EEPROM, whatever fits best.
At least it only consumes 8% in total. |
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
Posted: Sun May 09, 2010 11:12 am Post subject: |
|
|
Hi Tim,
The most init commands are sequential, so making an loop reduces the code size.
Some times your missing an command (like &H10). It's easier for this code to add this command to get an complete sequence.
If this is working it's very easy to store the data line in eeprom.
Code: |
Restore initcode
For B= &H06 to &H7A
Read Value
Call Spi_write(B , Value , 1 )
next B
Initcode:
Data &H01, &H7F , &H05 , &HF4, ........................
|
The other idea is to store both in eeprom.
Code: |
Readeeprom Dummie ,0 'First is dummie byte to set the pointer to the correct address
Do
Readeeprom Command 'Readeeprom incr the address automatic
Readeeprom Value
Call Spi_write(Command , Value , 1 )
loop until Command=&HFF 'Do until the eeprom is empty (FF)
$eeprom
Initcode:
Data &H00,&H06,&H01, &H07, &H7F ,&H08 , &H05, &H09 , &HF4, ........................
$data
|
Both are not tested, just to give some idea's.
Have fun _________________ www.evertdekker.com Bascom code vault |
|
Back to top |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Sun May 09, 2010 6:34 pm Post subject: |
|
|
Thanks Evert!
I will likely go to EEPROM. At this point none is used and it is not likely that any significant part will be used by normal code. Speed is not really important either so reading from EEPROM would definitely work.
Will look into this today - thanks again!
Tim |
|
Back to top |
|
|
holli
Joined: 29 Jul 2010 Posts: 28
|
Posted: Tue Aug 17, 2010 10:31 pm Post subject: |
|
|
Hi,
here are some of my sample codes for the RFM23. 32 Bytes payload are send from one to the other. FIFO is used, one TX version uses the packet handler to generate preamble and sync bytes. This is actually the same level as the RFM12. I already have some more versions using more of the packet handler.
Hope you find it useful. |
|
Back to top |
|
|
TSEYFARTH
Joined: 01 Jul 2006 Posts: 1054
|
Posted: Tue Aug 17, 2010 10:37 pm Post subject: |
|
|
Hi Holli.
Thank you for the samples! Will take a look shortly.
Tim |
|
Back to top |
|
|
UBH65
Joined: 12 Feb 2010 Posts: 23
|
Posted: Wed Aug 25, 2010 8:14 am Post subject: |
|
|
The area is of interest to me also.
Can you post the full code, I hope it will benefit many users.
Ubi |
|
Back to top |
|
|
jagvar
Joined: 15 Jan 2011 Posts: 3
|
Posted: Thu Jan 20, 2011 11:41 am Post subject: |
|
|
holli wrote: | Hi,
here are some of my sample codes for the RFM23. 32 Bytes payload are send from one to the other. FIFO is used, one TX version uses the packet handler to generate preamble and sync bytes. This is actually the same level as the RFM12. I already have some more versions using more of the packet handler.
Hope you find it useful. |
Hi
How can I change your program to be able to send and receive voice?
I want use of ADC & PWM
thank you. |
|
Back to top |
|
|
adrianov
Joined: 09 Mar 2015 Posts: 7
|
Posted: Sat May 23, 2015 3:48 pm Post subject: RFM222/23 help |
|
|
Hi everyone,
I would need your experience to advice me, I have made two Bascom programs to use them in a RF module (RFM22), one program is the emitter and another one is the receiver, the problem is the receiver doesn't receive anything, just random numbers. I was following the TSEYFARTH's programs but I can't solve the problem, maybe you can check my program to see if there is some mistake.
I would appreciate it very much.
Thank you.
Adrià. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|