Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 13:b44c1f678aff, committed 2017-10-22
- Comitter:
- Cheroukee
- Date:
- Sun Oct 22 20:20:42 2017 +0000
- Parent:
- 12:e21604b50719
- Commit message:
- Needs testing, added CRC calculations and adapted queue code to work with radio
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/crc.cpp Sun Oct 22 20:20:42 2017 +0000
@@ -0,0 +1,43 @@
+
+#include "crc.h"
+
+#define POSSIBLE_VALUES 256
+
+char crc_table[POSSIBLE_VALUES];
+
+void init_crc_module()
+{
+ /* iteration sur toutes la valeurs possible de CRC pour le valeurs de byte possible */
+ for (int i = 0; i < POSSIBLE_VALUES; i++)
+ {
+ char current_byte = (char)i;
+ /* calcul de la valeur de CRC pour le byte actuel a l'aide des operations bit a bit avec XOR */
+ for (char bit = 0; bit < 8; bit++)
+ {
+ /* lorsque la valeur du MSB est 1, on execute le XOR en plus du decalage */
+ if ((current_byte & 0x80) != 0)
+ {
+ current_byte = current_byte << 1;
+ current_byte ^= CRC_GEN_NUM;
+ }
+ else
+ {
+ current_byte = current_byte << 1;
+ }
+ }
+ /* en ajoute cette valeur a la table des valeurs pre calculees */
+ crc_table[i] = current_byte;
+ }
+}
+
+char get_crc_value(char* buffer, int buffer_length)
+{
+ char crc = 0;
+ for (int i = 0; i < buffer_length; i++)
+ {
+ /* get current CRC value = remainder */
+ crc = crc_table[buffer[i] ^ crc];
+ }
+
+ return crc;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crc.h Sun Oct 22 20:20:42 2017 +0000 @@ -0,0 +1,8 @@ + + + +#define CRC_GEN_NUM 0x1D + +void init_crc_module(); + +char get_crc_value(char* buffer, int buffer_length); \ No newline at end of file
--- a/main.cpp Mon Oct 16 01:51:32 2017 +0000
+++ b/main.cpp Sun Oct 22 20:20:42 2017 +0000
@@ -1,7 +1,6 @@
#include "mbed.h"
#include "radio.h"
-#include "quick_queue.h"
// Debug serial output
Serial debug_output(USBTX, USBRX);
@@ -30,12 +29,11 @@
void display_all_messages(){
radio_message_t* message;
- while((message = get_last_in_message()) != NULL)
+ while(get_message(message))
{
// Display messages
debug_output.printf("===========================\n\r");
debug_output.printf("===========START===========\n\r");
- debug_output.printf("OUT = %i :::: IN = %i\n\r", get_input_out_index(), get_input_in_index());
debug_output.printf("Start value = 0x%x\n\r", message->start);
debug_output.printf("Options value = 0x%x\n\r", message->options);
debug_output.printf("Length value = %u\n\r", message->length);
@@ -46,8 +44,6 @@
debug_output.printf("End value = 0x%x\n\r", message->end);
debug_output.printf("===========================\n\r");
//format
-
- last_in_message_read();
message = NULL;
}
}
\ No newline at end of file
--- a/radio.cpp Mon Oct 16 01:51:32 2017 +0000
+++ b/radio.cpp Sun Oct 22 20:20:42 2017 +0000
@@ -8,7 +8,7 @@
#include "mbed.h"
#include "rtos.h"
-
+#include "crc.h"
// Vitesse de sortie des message manchester
#define MANCHESTER_SPEED_OUT 4
@@ -483,6 +483,7 @@
*/
void init_radio_system()
{
+ init_crc_module();
setup_radio_in();
setup_radio_out();
}
@@ -506,7 +507,7 @@
}
// Ajouter calcul du CRC
- message->control = 0xCE;
+ message->control = get_crc_value(message->data, message->length);//0xCE;
message->end = FOOTER_END;
// On avance dans le buffer;
@@ -516,6 +517,32 @@
//////////////////////////////////////////////////////
}
+bool get_message(radio_message_t* message)
+{
+ radio_message_t* received_message;
+ // Si un message a ete recu par la radio
+ if((received_message = get_last_in_message()) != NULL)
+ {
+ // Si la valeur de CRC est valide par rapport a ce qui est calcule
+ if (get_crc_value(received_message->data, received_message->length) == received_message->control)
+ {
+ message->preambule = received_message->preambule;
+ message->start = received_message->start;
+ message->options = received_message->options;
+ message->length = received_message->length;
+ // memcopy
+ memcpy(message->data, received_message->data, received_message->length);
+
+ message->control = received_message->control;
+ message->end = received_message->end;
+
+ last_in_message_read();
+ return true;
+ }
+ }
+ return false;
+}
+
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
--- a/radio.h Mon Oct 16 01:51:32 2017 +0000
+++ b/radio.h Sun Oct 22 20:20:42 2017 +0000
@@ -16,7 +16,7 @@
byte start;
byte options;
byte length;
- byte data[MAX_MESSAGE_LENGTH] = { 0 };
+ byte data[MAX_MESSAGE_LENGTH];
byte control;
byte end;
} radio_message_t;
@@ -25,4 +25,6 @@
// public functions
void init_radio_system();
-bool send_message(char* buffer, int length);
\ No newline at end of file
+bool send_message(char* buffer, int length);
+
+bool get_message(radio_message_t* message);
\ No newline at end of file