Last active
December 30, 2017 21:23
-
-
Save wero1414/dafd5ead9448c9e00f646453ede7e0bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mcc_generated_files/mcc.h" | |
//#include "AES-128_V10.h" | |
#include "mcc_generated_files/LoRaWAN/lorawan_aes.h" | |
#include "mcc_generated_files/LoRaWAN/AES.h" | |
void RFM_Write(uint8_t, uint8_t); | |
uint8_t RFM_Read(uint8_t); | |
void RFM_Init(void); | |
void RFM_Continuous_Receive(void); | |
static void RFM_Reset(void); | |
static void Encrypt_Payload_s(unsigned char *, unsigned char , unsigned int , unsigned char ); | |
void AESEncodeLoRa_s(unsigned char* , unsigned char* ); | |
void AESEncode(unsigned char* , unsigned char* ); | |
unsigned char AppSkey[16] = {0x6e,0x9b,0x51,0x67,0x09,0x92,0xc2,0x40,0xac,0x1c,0xf4,0xe2,0x38,0x9c,0x8d,0xf2}; | |
unsigned char DevAddr[4] = {0x01, 0x00, 0x00, 0x07}; | |
unsigned char FCount[2]; //= {0x00, 0x4c}; | |
unsigned char Dir = 0x01; | |
unsigned char FRMPayload[2]; // = {0xDC, 0x54}; | |
bool send=false; | |
bool trans=false; | |
uint16_t init_counter=0; | |
uint8_t Message = 0; | |
uint8_t counterTmr3=0; | |
uint8_t data[255]=0; | |
void INT_Message(void){ | |
Message=1; | |
} | |
void RADIO_Reset(void){ | |
HALResetPinMakeOutput(); | |
HALResetPinOutputValue(0); | |
uint8_t ms=1; | |
while (ms > 0){ | |
__delay_ms(1); | |
ms--; | |
} | |
HALResetPinMakeInput(); | |
ms=10; | |
while (ms > 0){ | |
__delay_ms(1); | |
ms--; | |
} | |
//Added these two lines to make sure this pin is not left in floating state during sleep | |
HALResetPinOutputValue(1); | |
HALResetPinMakeOutput(); | |
} | |
void handle2sInterrupt(){ | |
printf("Registro op: %x \n\r",RADIO_RegisterRead(0x01)); | |
printf("Registro stat: %x \n\r",RADIO_RegisterRead(0x18)); | |
counterTmr3++; | |
} | |
void handle1msInterrupt(void){ | |
init_counter++; //each number equals 1mS | |
} | |
//PUERTO A: 7-SEG 1 A0 A1 A2 A3 | |
//PUERTOS E Y D: 7-SEG 2 E0 E1 D2 E2 | |
//Funcion para enmascarar y mandar cada bit al puerto correspondiente | |
void bcd(uint8_t dato){ | |
IO_RE0_PORT=dato&0x01; | |
IO_RE1_PORT=(dato&0x02)>>1; | |
IO_RD2_PORT=(dato&0x04)>>2; | |
IO_RE2_PORT=(dato&0x08)>>3; | |
} | |
void RxData(uint8_t* pData, uint8_t dataLength, OpStatus_t status) | |
{ | |
if(dataLength>0){ | |
printf("[RX]Dato recibido: \n\r"); | |
printf("[Rx]Puerto: %d\n\r",pData[0]); | |
for(int i=1;i<dataLength;i++){ | |
printf("[Rx]Posicion %d: %d \n",i, pData[i]-48); | |
} | |
uint8_t data_1= pData[1]-48; | |
uint8_t data_2= pData[2]-48; | |
if((data_1==0)&&(data_2==0)){ | |
trans=false; | |
printf("[INFO] Transistor off\n\r"); | |
} | |
else{ | |
trans=true; | |
printf("[INFO] Transistor on\n\r"); | |
} | |
PORTA=data_1; | |
bcd(data_2); | |
IO_RD5_SetLow(); | |
} | |
} | |
void main(void) | |
{ | |
SYSTEM_Initialize(); | |
EUSART1_Initialize(); | |
INTERRUPT_GlobalInterruptEnable(); | |
INTERRUPT_PeripheralInterruptEnable(); | |
EXT_INT_Initialize(); | |
INT1_SetInterruptHandler(INT_Message); | |
TMR3_SetInterruptHandler(handle2sInterrupt); | |
TMR5_SetInterruptHandler(handle1msInterrupt); | |
TMR5_StopTimer(); | |
TRISA=0x00; | |
PORTA=0x00; | |
srand(RADIO_ReadRandom()); | |
uint32_t devAddr = 0x0000029; | |
printf("\n[INFO]Bienvenido\n\r"); | |
printf("[INFO]Version 1.0r\n\r"); | |
printf("[INFO]DevAdr: 0x%08x",devAddr); | |
//srand(RADIO_ReadRandom()); | |
bcd(0x00); | |
TRISA=0x00; | |
PORTA=0x00; | |
RFM_Init(); | |
RFM_Continuous_Receive(); | |
int i=0; | |
while (1){ | |
if(Message){ | |
TMR3_StopTimer(); | |
printf("Rx:\n\r"); | |
uint8_t RFM_Package_Location = 0x00; | |
uint8_t RFM_Rx_Length = 0x00; | |
if((RFM_Read(0x12) & 0x20) != 0x20){ | |
printf("CRC Ok\n\r"); | |
RADIO_SW_POW_SetDigitalOutput(); | |
RADIO_SW_POW_SetLow(); | |
} | |
else{ | |
printf("CRC No Ok\n\r"); | |
} | |
RFM_Package_Location = RFM_Read(0x10); //Read start position of received package | |
RFM_Rx_Length = RFM_Read(0x13); //Read length of received package | |
printf("Package location: %d \n\rLength: %d",RFM_Package_Location,RFM_Rx_Length); | |
RFM_Write(0x0D,RFM_Package_Location); //Set SPI pointer to start of package | |
for (uint8_t i = 0; i < RFM_Rx_Length; i++){ | |
data[i] = RFM_Read(0x00); | |
printf("Dato[%d] = 0x%02X\n\r",i,data[i]); | |
} | |
FCount[0]=data[7]; | |
FCount[1]=data[6]; | |
FRMPayload[0]=data[9]; | |
FRMPayload[1]=data[10]; | |
printf("FRMPayload= %X"); | |
Encrypt_Payload_s(&FRMPayload[0], sizeof(FRMPayload), FCount, Dir); | |
for(int k = 0; k < 2; k++){ | |
char datos = FRMPayload[k]; | |
printf("FRMPayload %d\n\r",FRMPayload[k]); | |
printf("Dato desenencriptado[%d]= %d\n\r",k,datos-48); | |
} | |
Message=0; | |
RFM_Write(0x12, 0xFF); //Clear interrupts | |
uint8_t data_1= FRMPayload[0]-48; | |
uint8_t data_2= FRMPayload[1]-48; | |
PORTA=data_1; | |
bcd(data_2); | |
} | |
TMR3_StartTimer(); | |
} | |
} | |
void RFM_Init(void) | |
{ | |
RADIO_Reset(); | |
RADIO_SW_POW_SetDigitalInput(); | |
RADIO_SW_POW_SetPullup(); | |
INTCON2bits.nRBPU = 0; | |
for (char cnt=0; !(cnt&0x80); cnt++) | |
if (RADIO_SW_POW_GetValue()) | |
{ | |
RADIO_SW_POW_SetDigitalOutput(); | |
break; | |
} | |
RADIO_SW_POW_ResetPullup(); | |
INTCON2bits.nRBPU = 1; | |
uint8_t ver = RFM_Read(0x42); | |
printf("Version: %d\n\r",ver); | |
if(ver==18){ | |
printf("SX1276 or RFM95 detected \n\r"); | |
} | |
else{ | |
printf("Error detecting RFM\n\r"); | |
printf("Check your connections\n\r"); | |
while(true); | |
} | |
RFM_Write(0x01,0x00); //Switch RFM to sleep | |
RFM_Write(0x01,0x80); //Set RFM in LoRa mode | |
RFM_Write(0x01,0x81); //Set RFM in Standby mode wait on mode ready | |
//while (IO_RB0_GetValue() == 0) { | |
// printf("."); | |
// } | |
__delay_ms(10); | |
//Set carrair frequency | |
// 868.100 MHz / 61.035 Hz = 14222987 = 0xD9068B | |
// 904.400 Mhz / 61.035 Hz = 14866880 = 0xE2D9C0 | |
// 912.500 Mhz / 61.035 Hz = 14950438 = 0xE42026 | |
// 902.300 Mhz / 61.035 Hz = 14783321 = 0xE19359 | |
// 923.300 Mhz / 61.035 Hz = 15127386 = 0xE6D35A . | |
RFM_Write(0x06,0xE6); | |
RFM_Write(0x07,0xD3); | |
RFM_Write(0x08,0x5A); | |
//PA pin (minimal power) | |
RFM_Write(0x09,0xFF); | |
//Switch LNA boost on | |
//RFM_Write(0x0C,0x23); | |
RFM_Write(0x1E,0xB4); //SF7 CRC On | |
RFM_Write(0x1D,0x72); //125 kHz 4/5 coding rate explicit header mode | |
RFM_Write(0x26,0x0C); //Low datarate optimization off AGC auto on | |
//Rx Timeout set to 37 symbols | |
RFM_Write(0x1F,0x25); | |
//Preamble length set to 8 symbols | |
//0x0008 + 4 = 12 | |
RFM_Write(0x20,0x00); | |
RFM_Write(0x21,0x08); | |
//Set LoRa sync word | |
RFM_Write(0x39,0x34); | |
//Set IQ to normal values | |
RFM_Write(0x33,0x27); | |
RFM_Write(0x3B,0x1D); | |
//Set FIFO pointers | |
//TX base adress | |
RFM_Write(0x0E,0x80); | |
//Rx base adress | |
RFM_Write(0x0F,0x00); | |
//Switch RFM to sleep | |
RFM_Write(0x01,0x80); | |
printf("Configured\n\r"); | |
} | |
/* | |
***************************************************************************************** | |
* Description : Function to switch RFM to continuous receive mode, used for Class C | |
***************************************************************************************** | |
*/ | |
void RFM_Continuous_Receive(void) | |
{ | |
EXT_INT1_InterruptEnable(); | |
//Clear all interrupts | |
//RFM_Write(0x12, 0xFF); | |
//Change DIO 0 back to RxDone | |
RFM_Write(0x40,0x00); | |
//Invert IQ Back | |
RFM_Write(0x33,0x67); | |
RFM_Write(0x3B,0x19); | |
//Change Datarate | |
RFM_Write(0x1E,0xA4); //SF10 CRC On | |
RFM_Write(0x1D,0x72); //125 kHz 4/5 coding rate explicit header mode | |
RFM_Write(0x26,0x04); //Low datarate optimization off AGC auto on | |
//Set Hop frequency Period to 0 | |
RFM_Write(0x24,0x00); | |
//Set carrair frequency | |
// 904.400 Mhz / 61.035 Hz = 14866880 = 0xE2D9C0 | |
// 912.500 Mhz / 61.035 Hz = 14950438 = 0xE42026 | |
// 902.300 Mhz / 61.035 Hz = 14783321 = 0xE19359 | |
// 923.300 Mhz / 61.035 Hz = 15127386 = 0xE6D35A . | |
RFM_Write(0x06,0xE6); | |
RFM_Write(0x07,0xD3); | |
RFM_Write(0x08,0x5A); | |
//Switch to continuous receive | |
//Set operation mode to Rx continuous | |
RFM_Write(0x01,0x85); | |
//while (IO_RB0_GetValue() == 0){} | |
__delay_ms(10); | |
printf("OpMode: %02X \n\r",RFM_Read(0x01)); | |
printf("Class C mode\n\r"); | |
} | |
/* | |
***************************************************************************************** | |
* Description : Function that writes a register from the RFM | |
* | |
* Arguments : RFM_Address Address of register to be written | |
* RFM_Data Data to be written | |
***************************************************************************************** | |
*/ | |
void RFM_Write(uint8_t reg, uint8_t value) | |
{ | |
HALSPICSAssert(); | |
SPI2_Exchange8bit(0x80 | reg); | |
SPI2_Exchange8bit(value); | |
HALSPICSDeassert(); | |
} | |
/* | |
***************************************************************************************** | |
* Description : Funtion that reads a register from the RFM | |
* | |
* Arguments : RFM_Address Address of register to be read | |
***************************************************************************************** | |
*/ | |
uint8_t RFM_Read(uint8_t addr) { | |
HALSPICSAssert(); | |
SPI2_Exchange8bit(addr & 0x7F); | |
uint8_t val = SPI2_Exchange8bit(0x00); | |
HALSPICSDeassert(); | |
return val; | |
} | |
static void Encrypt_Payload_s(unsigned char *Data, unsigned char Data_Length, unsigned int Frame_Counter, unsigned char Direction){ | |
unsigned char i = 0x00; | |
unsigned char j; | |
unsigned char Number_of_Blocks = 0x00; | |
unsigned char Incomplete_Block_Size = 0x00; | |
//uint8_t *buffer | |
unsigned char Block_A[16]; | |
printf("\nData encriptado"); | |
printf(Data[0]); | |
printf(Data[1]); | |
//Serial.println(Data_Length); | |
//Calculate number of blocks | |
Number_of_Blocks = Data_Length / 16; | |
Incomplete_Block_Size = Data_Length % 16; | |
if(Incomplete_Block_Size != 0) | |
{ | |
Number_of_Blocks++; | |
} | |
//Serial.println(Number_of_Blocks); | |
for(i = 1; i <= Number_of_Blocks; i++) | |
{ | |
Block_A[0] = 0x01; | |
Block_A[1] = 0x00; | |
Block_A[2] = 0x00; | |
Block_A[3] = 0x00; | |
Block_A[4] = 0x00; | |
Block_A[5] = Dir; | |
Block_A[6] = DevAddr[3]; | |
Block_A[7] = DevAddr[2]; | |
Block_A[8] = DevAddr[1]; | |
Block_A[9] = DevAddr[0]; | |
Block_A[10] = FCount[1]; | |
Block_A[11] = FCount[0]; | |
Block_A[12] = 0x00; //Frame counter upper Bytes | |
Block_A[13] = 0x00; | |
Block_A[14] = 0x00; | |
Block_A[15] = i; | |
/* | |
Serial.println("Block: " ); | |
for(j = 0; j < 16; j++) | |
{ | |
Serial.print(Block_A[j],HEX); | |
} | |
*/ | |
//Calculate S | |
AESEncodeLoRa_s(Block_A,AppSkey); | |
//AESEncode(&Block_A[0],AppSkey); | |
//AES_Encrypt(&Block_A[0],AppSkey); | |
//Check for last block | |
if(i != (Number_of_Blocks-1)) | |
{ | |
for(j = 0; j < 16; j++) | |
{ | |
*Data = *Data ^ Block_A[j]; | |
Data++; | |
} | |
} | |
else | |
{ | |
if(Incomplete_Block_Size == 0) | |
{ | |
Incomplete_Block_Size = 16; | |
} | |
for(j = 0; j < Incomplete_Block_Size; j++) | |
{ | |
*Data = *Data ^ Block_A[j]; | |
Data++; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment