11/*
22 * rf4463.c
33 *
4- * Copyright (C) 2017, Universidade Federal de Santa Catarina.
4+ * Copyright (C) 2017-2019 , Universidade Federal de Santa Catarina.
55 *
6- * This file is part of FloripaSat-Beacon .
6+ * This file is part of FloripaSat-TTC .
77 *
8- * FloripaSat-Beacon is free software: you can redistribute it and/or modify
8+ * FloripaSat-TTC is free software: you can redistribute it and/or modify
99 * it under the terms of the GNU General Public License as published by
1010 * the Free Software Foundation, either version 3 of the License, or
1111 * (at your option) any later version.
1212 *
13- * FloripaSat-Beacon is distributed in the hope that it will be useful,
13+ * FloripaSat-TTCn is distributed in the hope that it will be useful,
1414 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1515 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1616 * GNU General Public License for more details.
1717 *
1818 * You should have received a copy of the GNU General Public License
19- * along with FloripaSat-Beacon . If not, see <http://www.gnu.org/licenses/>.
19+ * along with FloripaSat-TTC . If not, see <http://www.gnu.org/licenses/>.
2020 *
2121 */
2222
2727 *
2828 * \author Gabriel Mariano Marcelino <gabriel.mm8@gmail.com>
2929 *
30- * \version 1.0-dev
30+ * \version 0.1.12
3131 *
3232 * \date 01/06/2017
3333 *
3636 */
3737
3838#include <drivers/driverlib/driverlib.h>
39+ #include <system/debug/debug.h>
3940
4041#include "rf4463.h"
4142#include "rf4463_pinmap.h"
@@ -49,22 +50,24 @@ const uint8_t RF4463_CONFIGURATION_DATA[] = RADIO_CONFIGURATION_DATA_ARRAY;
4950
5051uint8_t rf4463_init ()
5152{
53+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Initializing...\n\r" );
54+
5255 rf4463_gpio_init ();
53-
56+
5457 if (rf4463_spi_init () == STATUS_FAIL )
5558 {
5659 return STATUS_FAIL ;
5760 }
58-
61+
5962 // Reset the RF4463
6063 rf4463_power_on_reset ();
61-
64+
6265 // Registers configuration
6366 rf4463_reg_config ();
64-
67+
6568 // Set max. TX power
6669 rf4463_set_tx_power (127 );
67-
70+
6871 // Check if the RF4463 is working
6972 if (rf4463_check_device ())
7073 {
@@ -78,21 +81,25 @@ uint8_t rf4463_init()
7881
7982static void rf4463_gpio_init ()
8083{
84+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Configuring the GPIO pins...\n\r" );
85+
8186 GPIO_setAsOutputPin (RF4463_POWER_ENABLE_PORT , RF4463_POWER_ENABLE_PIN );
8287 GPIO_setOutputHighOnPin (RF4463_POWER_ENABLE_PORT , RF4463_POWER_ENABLE_PIN ); // Enable RF4463 power
83-
88+
8489 GPIO_setAsOutputPin (RF4463_SDN_PORT , RF4463_SDN_PIN );
8590 GPIO_setAsInputPin (RF4463_nIRQ_PORT , RF4463_nIRQ_PIN );
86-
91+
8792 GPIO_setAsOutputPin (RF4463_GPIO0_PORT , RF4463_GPIO0_PIN );
8893 GPIO_setAsInputPin (RF4463_GPIO1_PORT , RF4463_GPIO1_PIN );
89-
94+
9095 // Set SDN to low
9196 GPIO_setOutputHighOnPin (RF4463_SDN_PORT , RF4463_SDN_PIN );
9297}
9398
9499static void rf4463_reg_config ()
95100{
101+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Loading registers values...\n\r" );
102+
96103 // Set RF parameter like frequency, data rate, etc.
97104 rf4463_set_config (RF4463_CONFIGURATION_DATA , sizeof (RF4463_CONFIGURATION_DATA ));
98105
@@ -110,47 +117,55 @@ static void rf4463_reg_config()
110117
111118void rf4463_power_on_reset ()
112119{
120+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Powering on reset...\n\r" );
121+
113122 uint8_t buffer [8 ] = {RF_POWER_UP };
114-
123+
115124 GPIO_setOutputHighOnPin (RF4463_SDN_PORT , RF4463_SDN_PIN );
116125 rf4463_delay_ms (100 );
117126 GPIO_setOutputLowOnPin (RF4463_SDN_PORT , RF4463_SDN_PIN );
118127 rf4463_delay_ms (20 ); // Wait for RF4463 stabilization
119-
128+
120129 // Send power-up command
121130 GPIO_setOutputLowOnPin (RF4463_NSEL_PORT , RF4463_NSEL_PIN );
122131 rf4463_spi_write (buffer , 7 );
123132 GPIO_setOutputHighOnPin (RF4463_NSEL_PORT , RF4463_NSEL_PIN );
124-
133+
125134 rf4463_delay_ms (200 );
126135}
127136
128137bool rf4463_tx_packet (uint8_t * data , uint8_t len )
129138{
139+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Transmitting a packet...\n\r" );
140+
130141 // Setting packet size
131142 rf4463_set_properties (RF4463_PROPERTY_PKT_FIELD_1_LENGTH_7_0 , & len , 1 );
132-
143+
133144 rf4463_fifo_reset (); // Clear FIFO
134145 rf4463_write_tx_fifo (data , len );
135146 rf4463_clear_interrupts ();
136-
147+
137148 uint16_t tx_timer = RF4463_TX_TIMEOUT ;
138-
149+
139150 rf4463_enter_tx_mode ();
140-
151+
141152 while (tx_timer -- )
142153 {
143154 if (rf4463_wait_nIRQ ()) // Wait packet sent interruption
144155 {
156+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Packet transmitted!\n\r" );
157+
145158 return true;
146159 }
147-
160+
148161 rf4463_delay_us (100 );
149162 }
150-
163+
164+ debug_print_event_from_module (DEBUG_ERROR , RF4463_MODULE_NAME , "Timeout reached during the transmission!\n\r" );
165+
151166 // If the packet tranmission takes longer than expected, resets the radio.
152167 rf4463_init ();
153-
168+
154169 return false;
155170}
156171
@@ -257,17 +272,27 @@ bool rf4463_rx_init()
257272
258273bool rf4463_check_device ()
259274{
275+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Checking the device...\n\r" );
276+
260277 uint8_t buffer [10 ];
261278 uint16_t part_info ;
262-
279+
263280 if (!rf4463_get_cmd (RF4463_CMD_PART_INFO , buffer , 9 ))
264281 {
282+ debug_print_event_from_module (DEBUG_ERROR , RF4463_MODULE_NAME , "Error reading the part info register!\n\r" );
283+
265284 return false;
266285 }
267-
286+
268287 part_info = (buffer [2 ] << 8 ) | buffer [3 ];
269288 if (part_info != RF4463_PART_INFO )
270289 {
290+ debug_print_event_from_module (DEBUG_ERROR , RF4463_MODULE_NAME , "Error checking the device! (read=" );
291+ debug_print_hex (part_info );
292+ debug_print_msg ("expected=" );
293+ debug_print_hex (RF4463_PART_INFO );
294+ debug_print_msg (")\n\r" );
295+
271296 return false;
272297 }
273298 else
@@ -327,15 +352,21 @@ bool rf4463_set_tx_power(uint8_t pwr)
327352{
328353 if (pwr > 127 ) // Max. value is 127
329354 {
355+ debug_print_event_from_module (DEBUG_ERROR , RF4463_MODULE_NAME , "Error configuring the output power! (out-of-range value)\n\r" );
356+
330357 return false;
331358 }
332-
359+
333360 uint8_t buffer [5 ];
334361 buffer [0 ] = 0x08 ;
335362 buffer [1 ] = pwr ;
336363 buffer [2 ] = 0x00 ;
337364 buffer [3 ] = 0x3D ;
338-
365+
366+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Configuring the output power to " );
367+ debug_print_hex (pwr );
368+ debug_print_msg ("...\n\r" );
369+
339370 return rf4463_set_properties (RF4463_PROPERTY_PA_MODE , buffer , 4 );
340371}
341372
@@ -495,9 +526,24 @@ bool rf4463_clear_interrupts()
495526
496527void rf4463_write_tx_fifo (uint8_t * data , uint8_t len )
497528{
529+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Writing data to FIFO: " );
530+
531+ uint16_t i ;
532+ for (i = 0 ; i < len ; i ++ )
533+ {
534+ debug_print_hex (data [i ]);
535+
536+ if (i < len - 1 )
537+ {
538+ debug_print_msg (", " );
539+ }
540+ }
541+
542+ debug_print_msg ("\n\r" );
543+
498544 uint8_t buffer [RF4463_TX_FIFO_LEN ];
499545 memcpy (buffer , data , len );
500-
546+
501547 rf4463_set_cmd (RF4463_CMD_TX_FIFO_WRITE , buffer , len );
502548}
503549
@@ -518,42 +564,50 @@ bool rf4463_read_rx_fifo(uint8_t *data, uint8_t len)
518564
519565void rf4463_fifo_reset ()
520566{
567+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Reseting FIFO...\n\r" );
568+
521569 uint8_t data = 0x03 ;
522-
570+
523571 rf4463_set_cmd (RF4463_CMD_FIFO_INFO , & data , 1 );
524572}
525573
526574void rf4463_enter_tx_mode ()
527575{
576+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Entering TX mode...\n\r" );
577+
528578 uint8_t buffer [5 ];
529-
579+
530580 buffer [0 ] = RF4463_FREQ_CHANNEL ;
531- buffer [1 ] = 0x30 ; // TXCOMPLETE_STATE = Ready State; RETRANSMIT = 0 = No re-transmition; START = 0 = Start TX immediately
532- buffer [2 ] = 0x00 ; // TX packet length MSB (If equal zero, default length)
533- buffer [3 ] = 0x00 ; // TX packet length LSB (If equal zero, default length)
534-
581+ buffer [1 ] = 0x30 ; // TXCOMPLETE_STATE = Ready State; RETRANSMIT = 0 = No re-transmition; START = 0 = Start TX immediately
582+ buffer [2 ] = 0x00 ; // TX packet length MSB (If equal zero, default length)
583+ buffer [3 ] = 0x00 ; // TX packet length LSB (If equal zero, default length)
584+
535585 rf4463_set_cmd (RF4463_CMD_START_TX , buffer , 4 );
536586}
537587
538588void rf4463_enter_rx_mode ()
539589{
590+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Entering RX mode...\n\r" );
591+
540592 uint8_t buffer [8 ];
541-
593+
542594 buffer [0 ] = RF4463_FREQ_CHANNEL ;
543595 buffer [1 ] = 0x00 ;
544596 buffer [2 ] = 0x00 ;
545597 buffer [3 ] = 0x00 ;
546598 buffer [4 ] = 0x00 ;
547599 buffer [5 ] = 0x08 ;
548600 buffer [6 ] = 0x08 ;
549-
601+
550602 rf4463_set_cmd (RF4463_CMD_START_RX , buffer , 7 );
551603}
552604
553605bool rf4463_enter_standby_mode ()
554606{
607+ debug_print_event_from_module (DEBUG_INFO , RF4463_MODULE_NAME , "Entering standby mode...\n\r" );
608+
555609 uint8_t data = 0x01 ;
556-
610+
557611 return rf4463_set_cmd (RF4463_CMD_CHANGE_STATE , & data , 1 );
558612}
559613
0 commit comments