Everything working except ticker. Ticker is not working. Please make sure all the ground are connected. INterrupt and CS pin are changed. Please take care of that. Thank You
Fork of October_22_11AM____Worksproperly_till_bbram by
main.cpp@0:cb8104f3ae71, 2015-10-22 (annotated)
- Committer:
- dheerajmpai23
- Date:
- Thu Oct 22 09:43:36 2015 +0000
- Revision:
- 0:cb8104f3ae71
- Child:
- 1:9cd986151b11
Commited ; Problem Occured : problem in CS=0 and CS=1; Timings
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dheerajmpai23 | 0:cb8104f3ae71 | 1 | ///ADF7023 From BIT BUCKET |
dheerajmpai23 | 0:cb8104f3ae71 | 2 | //TO DO SPI and CMD CHeck |
dheerajmpai23 | 0:cb8104f3ae71 | 3 | #include "mbed.h" |
dheerajmpai23 | 0:cb8104f3ae71 | 4 | #include <iostream> |
dheerajmpai23 | 0:cb8104f3ae71 | 5 | #include <stdio.h> |
dheerajmpai23 | 0:cb8104f3ae71 | 6 | using namespace std; |
dheerajmpai23 | 0:cb8104f3ae71 | 7 | #include <bitset> |
dheerajmpai23 | 0:cb8104f3ae71 | 8 | #define ENDL "\r" << endl; |
dheerajmpai23 | 0:cb8104f3ae71 | 9 | |
dheerajmpai23 | 0:cb8104f3ae71 | 10 | #define MISO_PIN PTA17 |
dheerajmpai23 | 0:cb8104f3ae71 | 11 | #define THRS 20 |
dheerajmpai23 | 0:cb8104f3ae71 | 12 | #define STATE_ERR_THRS 20 |
dheerajmpai23 | 0:cb8104f3ae71 | 13 | #define PHY_OFF_EXEC_TIME 300 |
dheerajmpai23 | 0:cb8104f3ae71 | 14 | #define PHY_ON_EXEC_TIME 300 |
dheerajmpai23 | 0:cb8104f3ae71 | 15 | #define PHY_TX_EXEC_TIME 600 |
dheerajmpai23 | 0:cb8104f3ae71 | 16 | /******DEFINING OMMANDS*********/ |
dheerajmpai23 | 0:cb8104f3ae71 | 17 | |
dheerajmpai23 | 0:cb8104f3ae71 | 18 | #define CMD_HW_RESET 0xC8 |
dheerajmpai23 | 0:cb8104f3ae71 | 19 | #define CMD_PHY_ON 0xB1 |
dheerajmpai23 | 0:cb8104f3ae71 | 20 | #define CMD_PHY_OFF 0xB0 |
dheerajmpai23 | 0:cb8104f3ae71 | 21 | #define CMD_PHY_TX 0xB5 |
dheerajmpai23 | 0:cb8104f3ae71 | 22 | #define CMD_CONFIG_DEV 0xBB |
dheerajmpai23 | 0:cb8104f3ae71 | 23 | |
dheerajmpai23 | 0:cb8104f3ae71 | 24 | /*****FUNCTION Declaration********************/ |
dheerajmpai23 | 0:cb8104f3ae71 | 25 | bool assrt_phy_off( int, int, int); |
dheerajmpai23 | 0:cb8104f3ae71 | 26 | bool assrt_phy_on( int,int,int); |
dheerajmpai23 | 0:cb8104f3ae71 | 27 | bool assrt_phy_tx(int,int,int); |
dheerajmpai23 | 0:cb8104f3ae71 | 28 | /****************************/ |
dheerajmpai23 | 0:cb8104f3ae71 | 29 | SPI adf(PTA16, MISO_PIN, PTA15); |
dheerajmpai23 | 0:cb8104f3ae71 | 30 | //MOSI, MISO, CLOCK |
dheerajmpai23 | 0:cb8104f3ae71 | 31 | DigitalOut CS(D9); |
dheerajmpai23 | 0:cb8104f3ae71 | 32 | //DigitalOut CS(PTD4);//D10==PTD4 |
dheerajmpai23 | 0:cb8104f3ae71 | 33 | Serial PC(USBTX, USBRX); |
dheerajmpai23 | 0:cb8104f3ae71 | 34 | DigitalOut ledr(LED1); |
dheerajmpai23 | 0:cb8104f3ae71 | 35 | DigitalOut led2(LED2); |
dheerajmpai23 | 0:cb8104f3ae71 | 36 | InterruptIn IRQ(D8); |
dheerajmpai23 | 0:cb8104f3ae71 | 37 | //InterruptIn IRQ(PTA13);//D8==PTA13 |
dheerajmpai23 | 0:cb8104f3ae71 | 38 | Ticker ticker; |
dheerajmpai23 | 0:cb8104f3ae71 | 39 | //Timer t; |
dheerajmpai23 | 0:cb8104f3ae71 | 40 | //Timer stop; |
dheerajmpai23 | 0:cb8104f3ae71 | 41 | int x=IRQ; |
dheerajmpai23 | 0:cb8104f3ae71 | 42 | int src=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 43 | int irqsrc = 0; |
dheerajmpai23 | 0:cb8104f3ae71 | 44 | int j = 0; |
dheerajmpai23 | 0:cb8104f3ae71 | 45 | int k = 0; |
dheerajmpai23 | 0:cb8104f3ae71 | 46 | int irq1=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 47 | int r = 0; |
dheerajmpai23 | 0:cb8104f3ae71 | 48 | unsigned char data[]={0x65,0xD3,0x06,0x08,0xBB,0xE7,0xCD,0x16,0x65,0xD3,0x06,0x08,0xBB,0xE7,0xCD,0x16,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x20,0xE5,0xEE,0x00,0x10,0x83,0x11,0x24,0x67,0xCE,0x05,0x50,0x01,0x45,0x9D,0x46,0x0D,0x6B,0xC1,0xF2,0xF3,0xE8,0xFE,0x52,0x0A,0x27,0x90,0x0A,0x3F,0x15,0xD1,0x68,0xD0,0x48,0x75,0x06,0x04,0xB7,0xE6,0xA0,0x82,0xF2,0x50,0x20,0x75,0x09,0x65,0x68,0x1A,0x9A,0xDD,0x1C,0x3F,0xC6,0xF5,0xD8,0xBD,0x96,0xB1,0xA0,0x92,0x29,0x44,0x78,0x60,0x0C,0x5D,0xCE,0xED,0x7A,0x85,0xF0,0x70,0x1C,0x7F,0x52,0x7B,0x14,0x43,0x4C,0x76,0xDD,0x41,0x86,0x59,0x6D,0x18,0x7C,0x63,0xF3,0x9F,0x74,0xB8,0x00,0x77,0xC0,0x18,0xDD,0xCE,0xA2,0xD1,0x2A,0xF3,0xFE,0x22,0xB5,0x0E,0x42,0xAF,0xB7,0x89,0x38,0x15,0xB0,0xCD,0xD6,0x27,0xEB,0x3F,0x5C,0x05,0xF7,0x26,0x5A,0x6A,0x4B,0x08,0x9E,0xAA,0x74,0xFF,0x30,0x0A,0x0A,0xBD,0x72,0xA2,0x44,0x20,0xC6,0x92,0x59,0xF3,0x80,0x55,0x00,0x51,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x20,0xE5,0xEE,0x00,0xD4,0x63,0x5A,0xF0,0x1F,0x1C,0xFA,0x3C,0xE5,0x22,0x89,0xE4,0x00,0xAF,0xC5,0x74,0x16,0xB4,0x12,0x1C,0x50,0x61,0x2D,0xF8,0x6A,0x20,0xBC,0x96,0x82,0x1D,0x42,0x5A,0xD6,0x86,0xA6,0xB4,0xD1,0xCF,0xF1,0xBC,0x5D,0xAF,0x65,0xAE,0x9A,0x24,0x8A,0x52,0xC7,0x98,0x03,0x14,0xDC,0xDB,0x5E,0xA0,0x5F,0x1C,0x07,0x1E,0x75,0x3E,0xC5,0x12,0xB4,0xDD,0xB7,0x50,0x18,0x76,0x5B,0x44,0x87,0xD8,0xFC,0xE4,0xF7,0x6E,0x00,0x1E,0xFC,0x06,0x37,0x70,0xEA,0x14,0x4A,0xBE,0xBF,0xE8,0xAD,0x42,0x64,0x0B,0xED,0xE2,0x13,0x85,0x6C,0x30,0xDD,0x69,0xFA,0xCC,0xF5,0xC1,0x7D,0xC8,0x65,0xBA,0x92,0xC0,0x89,0xCA,0x9D,0x3C,0xF3,0x02,0x82,0xAE,0x57,0x08,0x91,0x08,0x0C,0x44,0x96,0x7E,0xB8,0x15,0x40,0x14,0x16,0x75,0x18,0xD4,0xAF,0x07,0xC7,0x3C,0xA3,0xF9,0x48,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x20,0xE5,0xEE,0x00,0x9E,0x40,0x2B,0xF0,0x57,0x45,0xAD,0x04,0x21,0xD4,0x18,0x48,0xDF,0x9A,0x88,0x2C,0xC9,0x40,0x87,0x52,0xA5,0x95,0xA1,0xA8,0x6B,0x74,0x73,0xFC,0x1B,0xD7,0x6B,0xDA,0xDA,0xC6,0x89,0x22,0x25,0x11,0xE6,0x00,0x31,0x77,0x36,0xD6,0x6A,0x17,0xC7,0x02,0xF1,0xFD,0x4F,0xB0,0x51,0x0D,0x37,0x6E,0xF5,0x06,0x1D,0x96,0x34,0x61,0xF6,0x3C,0xCE,0x7D,0xDB,0x80,0x01,0xDF,0x01,0x8C,0x77,0x3A,0x85,0x10,0xAB,0xCF,0xFA,0x2A,0x54,0x39,0x02,0xFA,0x5E,0x24,0xE1,0x58,0xC3,0x37,0x5A,0x7C,0xAC,0xFD,0x70,0x5C,0xDC,0x99,0x6E,0xA6,0xAC,0x22,0x72,0xA6,0x53,0xFC,0xC0,0xA0,0x2A,0xF5,0xC2,0x24,0x10,0x83,0x11,0x24,0x67,0xCE,0x05,0x50,0x01,0x45,0x9D,0x46,0x0D,0x6B,0xC1,0xF2,0xF3,0xE8,0xFE,0x52,0x0A,0x27,0x90,0x0A,0x3F,0x15,0xD1,0x68,0xD0,0x48,0x75,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x20,0xE5,0xEE,0x00,0x04,0xB7,0xE6,0xA0,0x82,0xF2,0x50,0x20,0x75,0x09,0x65,0x68,0x1A,0x9A,0xDD,0x1C,0x3F,0xC6,0xF5,0xD8,0xBD,0x96,0xB1,0xA0,0x92,0x29,0x44,0x78,0x60,0x0C,0x5D,0xCE,0xED,0x7A,0x85,0xF0,0x70,0x1C,0x7F,0x52,0x7B,0x14,0x43,0x4C,0x76,0xDD,0x41,0x86,0x59,0x6D,0x18,0x7C,0x63,0xF3,0x9F,0x74,0xB8,0x00,0x77,0xC0,0x18,0xDD,0xCE,0xA2,0xD1,0x2A,0xF3,0xFE,0x22,0xB5,0x0E,0x42,0xAF,0xB7,0x89,0x38,0x15,0xB0,0xCD,0xD6,0x27,0xEB,0x3F,0x5C,0x05,0xF7,0x26,0x5A,0x6A,0x4B,0x08,0x9E,0xAA,0x74,0xFF,0x30,0x0A,0x0A,0xBD,0x72,0xA2,0x44,0x20,0xC6,0x92,0x59,0xF3,0x80,0x55,0x00,0x51,0x64,0xD4,0x63,0x5A,0xF0,0x1F,0x1C,0xFA,0x3C,0xE5,0x22,0x89,0xE4,0x00,0xAF,0xC5,0x74,0x16,0xB4,0x12,0x1C,0x50,0x61,0x2D,0xF8,0x6A,0x20,0xBC,0x96,0x82,0x1D,0x42,0x5A,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x20,0xE5,0xEE,0x00,0xD6,0x86,0xA6,0xB4,0xD1,0xCF,0xF1,0xBC,0x5D,0xAF,0x65,0xAE,0x9A,0x24,0x8A,0x52,0xC7,0x98,0x03,0x14,0xDC,0xDB,0x5E,0xA0,0x5F,0x1C,0x07,0x1E,0x75,0x3E,0xC5,0x12,0xB4,0xDD,0xB7,0x50,0x18,0x76,0x5B,0x44,0x87,0xD8,0xFC,0xE4,0xF7,0x6E,0x00,0x1E,0xFC,0x06,0x37,0x70,0xEA,0x14,0x4A,0xBE,0xBF,0xE8,0xAD,0x42,0x64,0x0B,0xED,0xE2,0x13,0x85,0x6C,0x30,0xDD,0x69,0xFA,0xCC,0xF5,0xC1,0x7D,0xC8,0x65,0xBA,0x92,0xC0,0x89,0xCA,0x9D,0x3C,0xF3,0x02,0x82,0xAE,0x57,0x08,0x91,0x08,0x0C,0x44,0x96,0x7E,0xB8,0x15,0x40,0x14,0x16,0x75,0x18,0xD4,0xAF,0x07,0xC7,0x3C,0xA3,0xF9,0x48,0xA0,0x9E,0x40,0x2B,0xF0,0x57,0x45,0xAD,0x04,0x21,0xD4,0x18,0x48,0xDF,0x9A,0x88,0x2C,0xC9,0x40,0x87,0x52,0xA5,0x95,0xA1,0xA8,0x6B,0x74,0x73,0xFC,0x1B,0xD7,0x6B,0xDA,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x97,0x4C,0x18,0x22,0xEF,0x9F,0x34,0x59,0x97,0x4C,0x18,0x22,0xEF,0x9F,0x34,0x58,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x97,0x4C,0x18,0x22,0xEF,0x9F,0x34,0x59,0x97,0x4C,0x18,0x22,0xEF,0x9F,0x34,0x58,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
dheerajmpai23 | 0:cb8104f3ae71 | 49 | int a[100]; |
dheerajmpai23 | 0:cb8104f3ae71 | 50 | int b[100]; |
dheerajmpai23 | 0:cb8104f3ae71 | 51 | int c[100]; |
dheerajmpai23 | 0:cb8104f3ae71 | 52 | |
dheerajmpai23 | 0:cb8104f3ae71 | 53 | /*****ERROR CHECKING ALGO*****/ |
dheerajmpai23 | 0:cb8104f3ae71 | 54 | unsigned char status =0; |
dheerajmpai23 | 0:cb8104f3ae71 | 55 | unsigned int cmd_err_cnt=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 56 | unsigned int state_err_cnt=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 57 | unsigned int miso_err_cnt=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 58 | unsigned int hw_reset_err_cnt=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 59 | /*Err_check Flags*/ |
dheerajmpai23 | 0:cb8104f3ae71 | 60 | bool bcn_flag=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 61 | bool bbram_flag=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 62 | /***********/ |
dheerajmpai23 | 0:cb8104f3ae71 | 63 | |
dheerajmpai23 | 0:cb8104f3ae71 | 64 | #define SPI_NOP 0xFF |
dheerajmpai23 | 0:cb8104f3ae71 | 65 | /***********************************************************************************/ |
dheerajmpai23 | 0:cb8104f3ae71 | 66 | void bbram_write() |
dheerajmpai23 | 0:cb8104f3ae71 | 67 | { |
dheerajmpai23 | 0:cb8104f3ae71 | 68 | /* CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 69 | adf.write(0xB0);//PHY_OFF |
dheerajmpai23 | 0:cb8104f3ae71 | 70 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 71 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 72 | *///Commented on 21st October debugging |
dheerajmpai23 | 0:cb8104f3ae71 | 73 | // Write bbram |
dheerajmpai23 | 0:cb8104f3ae71 | 74 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 75 | adf.write(0x19); |
dheerajmpai23 | 0:cb8104f3ae71 | 76 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 77 | adf.write(0x60); |
dheerajmpai23 | 0:cb8104f3ae71 | 78 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 79 | |
dheerajmpai23 | 0:cb8104f3ae71 | 80 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 81 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 82 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 83 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 84 | |
dheerajmpai23 | 0:cb8104f3ae71 | 85 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 86 | adf.write(0x33); |
dheerajmpai23 | 0:cb8104f3ae71 | 87 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 88 | adf.write(0xF4);//Frequency Register F9 = 435.802 MHz F4 = 435.800 MHz |
dheerajmpai23 | 0:cb8104f3ae71 | 89 | |
dheerajmpai23 | 0:cb8104f3ae71 | 90 | adf.write(0xC2); |
dheerajmpai23 | 0:cb8104f3ae71 | 91 | adf.write(0x10); |
dheerajmpai23 | 0:cb8104f3ae71 | 92 | adf.write(0xC0); |
dheerajmpai23 | 0:cb8104f3ae71 | 93 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 94 | |
dheerajmpai23 | 0:cb8104f3ae71 | 95 | adf.write(0x30); |
dheerajmpai23 | 0:cb8104f3ae71 | 96 | adf.write(0x31); |
dheerajmpai23 | 0:cb8104f3ae71 | 97 | adf.write(0x07); |
dheerajmpai23 | 0:cb8104f3ae71 | 98 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 99 | |
dheerajmpai23 | 0:cb8104f3ae71 | 100 | adf.write(0x01); |
dheerajmpai23 | 0:cb8104f3ae71 | 101 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 102 | adf.write(0x7F); |
dheerajmpai23 | 0:cb8104f3ae71 | 103 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 104 | |
dheerajmpai23 | 0:cb8104f3ae71 | 105 | adf.write(0x0B); |
dheerajmpai23 | 0:cb8104f3ae71 | 106 | adf.write(0x37); |
dheerajmpai23 | 0:cb8104f3ae71 | 107 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 108 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 109 | |
dheerajmpai23 | 0:cb8104f3ae71 | 110 | adf.write(0x40); |
dheerajmpai23 | 0:cb8104f3ae71 | 111 | adf.write(0x0C); |
dheerajmpai23 | 0:cb8104f3ae71 | 112 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 113 | adf.write(0x05); |
dheerajmpai23 | 0:cb8104f3ae71 | 114 | |
dheerajmpai23 | 0:cb8104f3ae71 | 115 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 116 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 117 | adf.write(0x18); |
dheerajmpai23 | 0:cb8104f3ae71 | 118 | adf.write(0x12); |
dheerajmpai23 | 0:cb8104f3ae71 | 119 | |
dheerajmpai23 | 0:cb8104f3ae71 | 120 | adf.write(0x34); |
dheerajmpai23 | 0:cb8104f3ae71 | 121 | adf.write(0x56); |
dheerajmpai23 | 0:cb8104f3ae71 | 122 | adf.write(0x10); |
dheerajmpai23 | 0:cb8104f3ae71 | 123 | adf.write(0x10); |
dheerajmpai23 | 0:cb8104f3ae71 | 124 | |
dheerajmpai23 | 0:cb8104f3ae71 | 125 | adf.write(0xC4); // Different |
dheerajmpai23 | 0:cb8104f3ae71 | 126 | adf.write(0x14); |
dheerajmpai23 | 0:cb8104f3ae71 | 127 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 128 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 129 | |
dheerajmpai23 | 0:cb8104f3ae71 | 130 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 131 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 132 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 133 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 134 | |
dheerajmpai23 | 0:cb8104f3ae71 | 135 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 136 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 137 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 138 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 139 | |
dheerajmpai23 | 0:cb8104f3ae71 | 140 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 141 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 142 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 143 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 144 | |
dheerajmpai23 | 0:cb8104f3ae71 | 145 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 146 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 147 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 148 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 149 | |
dheerajmpai23 | 0:cb8104f3ae71 | 150 | adf.write(0x04); |
dheerajmpai23 | 0:cb8104f3ae71 | 151 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 152 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 153 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 154 | |
dheerajmpai23 | 0:cb8104f3ae71 | 155 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 156 | adf.write(0x00); |
dheerajmpai23 | 0:cb8104f3ae71 | 157 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 158 | wait(1); |
dheerajmpai23 | 0:cb8104f3ae71 | 159 | |
dheerajmpai23 | 0:cb8104f3ae71 | 160 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 161 | |
dheerajmpai23 | 0:cb8104f3ae71 | 162 | |
dheerajmpai23 | 0:cb8104f3ae71 | 163 | /******************************************************************************************/ |
dheerajmpai23 | 0:cb8104f3ae71 | 164 | |
dheerajmpai23 | 0:cb8104f3ae71 | 165 | void initiate(void){ |
dheerajmpai23 | 0:cb8104f3ae71 | 166 | |
dheerajmpai23 | 0:cb8104f3ae71 | 167 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 168 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 169 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 170 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 171 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 172 | |
dheerajmpai23 | 0:cb8104f3ae71 | 173 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 174 | adf.write(0x08); // TRANSMIT_DATA LENGTH |
dheerajmpai23 | 0:cb8104f3ae71 | 175 | adf.write(0x14); |
dheerajmpai23 | 0:cb8104f3ae71 | 176 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 177 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 178 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 179 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 180 | adf.write(0x08); // TRANSMIT_DATA LENGTH |
dheerajmpai23 | 0:cb8104f3ae71 | 181 | adf.write(0x15); |
dheerajmpai23 | 0:cb8104f3ae71 | 182 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 183 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 184 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 185 | |
dheerajmpai23 | 0:cb8104f3ae71 | 186 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 187 | adf.write(0x09); |
dheerajmpai23 | 0:cb8104f3ae71 | 188 | adf.write(0x24); // TX_BASE ADDRESS 0x20(starting Tx Byte) |
dheerajmpai23 | 0:cb8104f3ae71 | 189 | adf.write(0x20); |
dheerajmpai23 | 0:cb8104f3ae71 | 190 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 191 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 192 | |
dheerajmpai23 | 0:cb8104f3ae71 | 193 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 194 | adf.write(0x09); |
dheerajmpai23 | 0:cb8104f3ae71 | 195 | adf.write(0x37);// BUFFER SIZE 0xE0=224 Bytes 0x137 is adress of buffer size |
dheerajmpai23 | 0:cb8104f3ae71 | 196 | adf.write(0xE0); |
dheerajmpai23 | 0:cb8104f3ae71 | 197 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 198 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 199 | |
dheerajmpai23 | 0:cb8104f3ae71 | 200 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 201 | adf.write(0x09); |
dheerajmpai23 | 0:cb8104f3ae71 | 202 | adf.write(0x36);//BB_Tx_Buffer Signal when Buffer is half filled |
dheerajmpai23 | 0:cb8104f3ae71 | 203 | adf.write(0x70);//0x70 = 112 >> When Half buffer interrupt is given |
dheerajmpai23 | 0:cb8104f3ae71 | 204 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 205 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 206 | |
dheerajmpai23 | 0:cb8104f3ae71 | 207 | |
dheerajmpai23 | 0:cb8104f3ae71 | 208 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 209 | adf.write(0x09); |
dheerajmpai23 | 0:cb8104f3ae71 | 210 | adf.write(0x39);//BB_Tx_Buffer Signal when Buffer is half filled |
dheerajmpai23 | 0:cb8104f3ae71 | 211 | adf.write(0x10);//0x70 = 112 >> When Half buffer interrupt is given |
dheerajmpai23 | 0:cb8104f3ae71 | 212 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 213 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 214 | |
dheerajmpai23 | 0:cb8104f3ae71 | 215 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 216 | |
dheerajmpai23 | 0:cb8104f3ae71 | 217 | |
dheerajmpai23 | 0:cb8104f3ae71 | 218 | |
dheerajmpai23 | 0:cb8104f3ae71 | 219 | /*int irq_src(void){ |
dheerajmpai23 | 0:cb8104f3ae71 | 220 | |
dheerajmpai23 | 0:cb8104f3ae71 | 221 | adf.write(0x2B); |
dheerajmpai23 | 0:cb8104f3ae71 | 222 | adf.write(0x36); |
dheerajmpai23 | 0:cb8104f3ae71 | 223 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 224 | irqsrc = adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 225 | |
dheerajmpai23 | 0:cb8104f3ae71 | 226 | irqsrc &= 0x60; |
dheerajmpai23 | 0:cb8104f3ae71 | 227 | if (irqsrc == 0x20) |
dheerajmpai23 | 0:cb8104f3ae71 | 228 | return 1;//Buffer is almost full |
dheerajmpai23 | 0:cb8104f3ae71 | 229 | else if (irqsrc == 0x40) |
dheerajmpai23 | 0:cb8104f3ae71 | 230 | return 2;// Buffer is Full |
dheerajmpai23 | 0:cb8104f3ae71 | 231 | else |
dheerajmpai23 | 0:cb8104f3ae71 | 232 | return 0; |
dheerajmpai23 | 0:cb8104f3ae71 | 233 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 234 | */ |
dheerajmpai23 | 0:cb8104f3ae71 | 235 | int p=112; |
dheerajmpai23 | 0:cb8104f3ae71 | 236 | int count = 0; |
dheerajmpai23 | 0:cb8104f3ae71 | 237 | int intcount =0; |
dheerajmpai23 | 0:cb8104f3ae71 | 238 | |
dheerajmpai23 | 0:cb8104f3ae71 | 239 | void write_data(void){ |
dheerajmpai23 | 0:cb8104f3ae71 | 240 | // t.reset(); |
dheerajmpai23 | 0:cb8104f3ae71 | 241 | // count++; |
dheerajmpai23 | 0:cb8104f3ae71 | 242 | // cout<<"3"<<ENDL; |
dheerajmpai23 | 0:cb8104f3ae71 | 243 | // ledr=!ledr; |
dheerajmpai23 | 0:cb8104f3ae71 | 244 | // cout<<"irq detected"<<ENDL; |
dheerajmpai23 | 0:cb8104f3ae71 | 245 | // src = irq_src(); |
dheerajmpai23 | 0:cb8104f3ae71 | 246 | p+=112; |
dheerajmpai23 | 0:cb8104f3ae71 | 247 | |
dheerajmpai23 | 0:cb8104f3ae71 | 248 | //CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 249 | // adf.write(0x0B);//IRQ_SOURCE_Random write //To read CMD = 2B 36 FF FF |
dheerajmpai23 | 0:cb8104f3ae71 | 250 | // adf.write(0x36);//Address : 0x336 |
dheerajmpai23 | 0:cb8104f3ae71 | 251 | // adf.write(0xFF);//Put Low |
dheerajmpai23 | 0:cb8104f3ae71 | 252 | // CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 253 | // wait_us(80); |
dheerajmpai23 | 0:cb8104f3ae71 | 254 | // CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 255 | // k=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 256 | |
dheerajmpai23 | 0:cb8104f3ae71 | 257 | if(!j){ |
dheerajmpai23 | 0:cb8104f3ae71 | 258 | adf.write(0x18); |
dheerajmpai23 | 0:cb8104f3ae71 | 259 | adf.write(0x20); |
dheerajmpai23 | 0:cb8104f3ae71 | 260 | while(k<=223){ |
dheerajmpai23 | 0:cb8104f3ae71 | 261 | |
dheerajmpai23 | 0:cb8104f3ae71 | 262 | adf.write(data[k]); |
dheerajmpai23 | 0:cb8104f3ae71 | 263 | k++; |
dheerajmpai23 | 0:cb8104f3ae71 | 264 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 265 | |
dheerajmpai23 | 0:cb8104f3ae71 | 266 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 267 | |
dheerajmpai23 | 0:cb8104f3ae71 | 268 | else if(k<sizeof(data)){ |
dheerajmpai23 | 0:cb8104f3ae71 | 269 | // if(src == 1){ |
dheerajmpai23 | 0:cb8104f3ae71 | 270 | if(j%2){ //src== Buffer Half Full src == 1 |
dheerajmpai23 | 0:cb8104f3ae71 | 271 | adf.write(0x18); |
dheerajmpai23 | 0:cb8104f3ae71 | 272 | adf.write(0x20); |
dheerajmpai23 | 0:cb8104f3ae71 | 273 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 274 | |
dheerajmpai23 | 0:cb8104f3ae71 | 275 | // else if(src==2){ |
dheerajmpai23 | 0:cb8104f3ae71 | 276 | else{ //src== Buffer is Full src==2 |
dheerajmpai23 | 0:cb8104f3ae71 | 277 | adf.write(0x18); |
dheerajmpai23 | 0:cb8104f3ae71 | 278 | adf.write(0x90); |
dheerajmpai23 | 0:cb8104f3ae71 | 279 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 280 | |
dheerajmpai23 | 0:cb8104f3ae71 | 281 | |
dheerajmpai23 | 0:cb8104f3ae71 | 282 | while(k<p){ |
dheerajmpai23 | 0:cb8104f3ae71 | 283 | adf.write(data[k]); |
dheerajmpai23 | 0:cb8104f3ae71 | 284 | k++; |
dheerajmpai23 | 0:cb8104f3ae71 | 285 | |
dheerajmpai23 | 0:cb8104f3ae71 | 286 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 287 | // if((k)>=sizeof(data)) |
dheerajmpai23 | 0:cb8104f3ae71 | 288 | // { |
dheerajmpai23 | 0:cb8104f3ae71 | 289 | // ticker.detach();//Stop interrupt detection |
dheerajmpai23 | 0:cb8104f3ae71 | 290 | // cout<<src<<ENDL; |
dheerajmpai23 | 0:cb8104f3ae71 | 291 | // } |
dheerajmpai23 | 0:cb8104f3ae71 | 292 | |
dheerajmpai23 | 0:cb8104f3ae71 | 293 | |
dheerajmpai23 | 0:cb8104f3ae71 | 294 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 295 | |
dheerajmpai23 | 0:cb8104f3ae71 | 296 | else if(k>=(sizeof(data)) ){ |
dheerajmpai23 | 0:cb8104f3ae71 | 297 | k=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 298 | j=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 299 | p=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 300 | // cout<<count<<" "<<sizeof(data)/112<<ENDL; |
dheerajmpai23 | 0:cb8104f3ae71 | 301 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 302 | |
dheerajmpai23 | 0:cb8104f3ae71 | 303 | |
dheerajmpai23 | 0:cb8104f3ae71 | 304 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 305 | adf.write(0x0B);//IRQ_SOURCE_Random write //To read CMD = 2B 36 FF FF |
dheerajmpai23 | 0:cb8104f3ae71 | 306 | adf.write(0x36);//Address : 0x336 |
dheerajmpai23 | 0:cb8104f3ae71 | 307 | adf.write(0xFF);//Put Low |
dheerajmpai23 | 0:cb8104f3ae71 | 308 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 309 | wait_us(80); |
dheerajmpai23 | 0:cb8104f3ae71 | 310 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 311 | |
dheerajmpai23 | 0:cb8104f3ae71 | 312 | // a[r]=t.read_us(); |
dheerajmpai23 | 0:cb8104f3ae71 | 313 | j++; |
dheerajmpai23 | 0:cb8104f3ae71 | 314 | // r++; |
dheerajmpai23 | 0:cb8104f3ae71 | 315 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 316 | wait_us(1); |
dheerajmpai23 | 0:cb8104f3ae71 | 317 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 318 | |
dheerajmpai23 | 0:cb8104f3ae71 | 319 | void Interrupt(){ |
dheerajmpai23 | 0:cb8104f3ae71 | 320 | x = IRQ; |
dheerajmpai23 | 0:cb8104f3ae71 | 321 | // intcount++; |
dheerajmpai23 | 0:cb8104f3ae71 | 322 | if(x) |
dheerajmpai23 | 0:cb8104f3ae71 | 323 | { |
dheerajmpai23 | 0:cb8104f3ae71 | 324 | write_data(); |
dheerajmpai23 | 0:cb8104f3ae71 | 325 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 326 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 327 | |
dheerajmpai23 | 0:cb8104f3ae71 | 328 | |
dheerajmpai23 | 0:cb8104f3ae71 | 329 | |
dheerajmpai23 | 0:cb8104f3ae71 | 330 | |
dheerajmpai23 | 0:cb8104f3ae71 | 331 | void send_data(void){ |
dheerajmpai23 | 0:cb8104f3ae71 | 332 | |
dheerajmpai23 | 0:cb8104f3ae71 | 333 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 334 | adf.write(CMD_CONFIG_DEV); |
dheerajmpai23 | 0:cb8104f3ae71 | 335 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 336 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 337 | |
dheerajmpai23 | 0:cb8104f3ae71 | 338 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 339 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 340 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 341 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 342 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 343 | |
dheerajmpai23 | 0:cb8104f3ae71 | 344 | write_data(); |
dheerajmpai23 | 0:cb8104f3ae71 | 345 | |
dheerajmpai23 | 0:cb8104f3ae71 | 346 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 347 | adf.write(0xB1); |
dheerajmpai23 | 0:cb8104f3ae71 | 348 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 349 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 350 | assrt_phy_on(0,0,0); |
dheerajmpai23 | 0:cb8104f3ae71 | 351 | |
dheerajmpai23 | 0:cb8104f3ae71 | 352 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 353 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 354 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 355 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 356 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 357 | |
dheerajmpai23 | 0:cb8104f3ae71 | 358 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 359 | adf.write(CMD_PHY_TX); |
dheerajmpai23 | 0:cb8104f3ae71 | 360 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 361 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 362 | assrt_phy_tx(0,0,0); |
dheerajmpai23 | 0:cb8104f3ae71 | 363 | |
dheerajmpai23 | 0:cb8104f3ae71 | 364 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 365 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 366 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 367 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 368 | wait_ms(2); |
dheerajmpai23 | 0:cb8104f3ae71 | 369 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 370 | /*******ADDITIONAL STUFF****************/ |
dheerajmpai23 | 0:cb8104f3ae71 | 371 | unsigned char check_status(void){ |
dheerajmpai23 | 0:cb8104f3ae71 | 372 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 373 | adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 374 | unsigned char stat = adf.write(0xFF); |
dheerajmpai23 | 0:cb8104f3ae71 | 375 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 376 | return stat; |
dheerajmpai23 | 0:cb8104f3ae71 | 377 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 378 | /*****************************************/ |
dheerajmpai23 | 0:cb8104f3ae71 | 379 | bool assrt_phy_off(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){ |
dheerajmpai23 | 0:cb8104f3ae71 | 380 | status=check_status(); |
dheerajmpai23 | 0:cb8104f3ae71 | 381 | if(status==0xB1){ |
dheerajmpai23 | 0:cb8104f3ae71 | 382 | return 0; |
dheerajmpai23 | 0:cb8104f3ae71 | 383 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 384 | else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){ |
dheerajmpai23 | 0:cb8104f3ae71 | 385 | return 1;//You need to Reset the hardware |
dheerajmpai23 | 0:cb8104f3ae71 | 386 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 387 | else if(state_err_cnt>STATE_ERR_THRS){ |
dheerajmpai23 | 0:cb8104f3ae71 | 388 | return 1;//Again reset the hardware |
dheerajmpai23 | 0:cb8104f3ae71 | 389 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 390 | else if((status&0xA0)==0xA0){ //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready. |
dheerajmpai23 | 0:cb8104f3ae71 | 391 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 392 | adf.write(CMD_PHY_OFF); //CMD_PHY_OFF=0xB0 |
dheerajmpai23 | 0:cb8104f3ae71 | 393 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 394 | wait_us(PHY_OFF_EXEC_TIME);// Typical = 24us We are giving 300us |
dheerajmpai23 | 0:cb8104f3ae71 | 395 | return assrt_phy_off(cmd_err_cnt,spi_err_cnt,state_err_cnt+1); |
dheerajmpai23 | 0:cb8104f3ae71 | 396 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 397 | else if(status&0x80==0x00){ |
dheerajmpai23 | 0:cb8104f3ae71 | 398 | wait_ms(5); |
dheerajmpai23 | 0:cb8104f3ae71 | 399 | //Error: SPI=0 Not ready CMD= Dont care |
dheerajmpai23 | 0:cb8104f3ae71 | 400 | return assrt_phy_off(cmd_err_cnt,spi_err_cnt+1,state_err_cnt); |
dheerajmpai23 | 0:cb8104f3ae71 | 401 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 402 | else {//if(status&0xA0==0x80){ |
dheerajmpai23 | 0:cb8104f3ae71 | 403 | wait_ms(1); |
dheerajmpai23 | 0:cb8104f3ae71 | 404 | //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable |
dheerajmpai23 | 0:cb8104f3ae71 | 405 | return assrt_phy_off(cmd_err_cnt+1,spi_err_cnt,state_err_cnt); |
dheerajmpai23 | 0:cb8104f3ae71 | 406 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 407 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 408 | /*****************************/ |
dheerajmpai23 | 0:cb8104f3ae71 | 409 | bool assrt_phy_on(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){ |
dheerajmpai23 | 0:cb8104f3ae71 | 410 | status=check_status(); |
dheerajmpai23 | 0:cb8104f3ae71 | 411 | if((status&0x1F)==0xB2){ |
dheerajmpai23 | 0:cb8104f3ae71 | 412 | return 0; |
dheerajmpai23 | 0:cb8104f3ae71 | 413 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 414 | else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){ |
dheerajmpai23 | 0:cb8104f3ae71 | 415 | return 1;//You need to Reset the hardware |
dheerajmpai23 | 0:cb8104f3ae71 | 416 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 417 | else if(state_err_cnt>STATE_ERR_THRS){ |
dheerajmpai23 | 0:cb8104f3ae71 | 418 | return 1;//Again reset the hardware |
dheerajmpai23 | 0:cb8104f3ae71 | 419 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 420 | else if((status&0xA0)==0xA0){ //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready. |
dheerajmpai23 | 0:cb8104f3ae71 | 421 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 422 | adf.write(0xB1); //CMD_PHY_OFF |
dheerajmpai23 | 0:cb8104f3ae71 | 423 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 424 | wait_us(PHY_ON_EXEC_TIME);// Typical = 24us We are giving 300us |
dheerajmpai23 | 0:cb8104f3ae71 | 425 | return assrt_phy_on(cmd_err_cnt,spi_err_cnt,state_err_cnt+1); |
dheerajmpai23 | 0:cb8104f3ae71 | 426 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 427 | else if(status&0x80==0x00){ |
dheerajmpai23 | 0:cb8104f3ae71 | 428 | wait_ms(5); |
dheerajmpai23 | 0:cb8104f3ae71 | 429 | //Error: SPI=0 Not ready CMD= Dont care |
dheerajmpai23 | 0:cb8104f3ae71 | 430 | return assrt_phy_on(cmd_err_cnt,spi_err_cnt+1,state_err_cnt); |
dheerajmpai23 | 0:cb8104f3ae71 | 431 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 432 | else if(status&0xA0==0x80){ |
dheerajmpai23 | 0:cb8104f3ae71 | 433 | wait_ms(1); |
dheerajmpai23 | 0:cb8104f3ae71 | 434 | //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable |
dheerajmpai23 | 0:cb8104f3ae71 | 435 | return assrt_phy_on(cmd_err_cnt+1,spi_err_cnt,state_err_cnt); |
dheerajmpai23 | 0:cb8104f3ae71 | 436 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 437 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 438 | /*****************************/ |
dheerajmpai23 | 0:cb8104f3ae71 | 439 | bool assrt_phy_tx(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){ |
dheerajmpai23 | 0:cb8104f3ae71 | 440 | status=check_status(); |
dheerajmpai23 | 0:cb8104f3ae71 | 441 | if((status & 0x1F) == 0xB4){ |
dheerajmpai23 | 0:cb8104f3ae71 | 442 | return 0; |
dheerajmpai23 | 0:cb8104f3ae71 | 443 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 444 | else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){ |
dheerajmpai23 | 0:cb8104f3ae71 | 445 | return 1;//You need to Reset the hardware |
dheerajmpai23 | 0:cb8104f3ae71 | 446 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 447 | else if(state_err_cnt>STATE_ERR_THRS){ |
dheerajmpai23 | 0:cb8104f3ae71 | 448 | return 1;//Again reset the hardware |
dheerajmpai23 | 0:cb8104f3ae71 | 449 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 450 | else if((status&0xA0)==0xA0){ //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready. |
dheerajmpai23 | 0:cb8104f3ae71 | 451 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 452 | adf.write(0xB1); //CMD_PHY_OFF |
dheerajmpai23 | 0:cb8104f3ae71 | 453 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 454 | wait_us(PHY_TX_EXEC_TIME);// Typical = 24us We are giving 300us |
dheerajmpai23 | 0:cb8104f3ae71 | 455 | return assrt_phy_tx(cmd_err_cnt,spi_err_cnt,state_err_cnt+1); |
dheerajmpai23 | 0:cb8104f3ae71 | 456 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 457 | else if(status&0x80==0x00){ |
dheerajmpai23 | 0:cb8104f3ae71 | 458 | wait_ms(1); |
dheerajmpai23 | 0:cb8104f3ae71 | 459 | //Error: SPI=0 Not ready CMD= Dont care |
dheerajmpai23 | 0:cb8104f3ae71 | 460 | return assrt_phy_tx(cmd_err_cnt,spi_err_cnt+1,state_err_cnt); |
dheerajmpai23 | 0:cb8104f3ae71 | 461 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 462 | else if(status&0xA0==0x80){ |
dheerajmpai23 | 0:cb8104f3ae71 | 463 | wait_us(50); |
dheerajmpai23 | 0:cb8104f3ae71 | 464 | //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable |
dheerajmpai23 | 0:cb8104f3ae71 | 465 | return assrt_phy_tx(cmd_err_cnt+1,spi_err_cnt,state_err_cnt); |
dheerajmpai23 | 0:cb8104f3ae71 | 466 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 467 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 468 | |
dheerajmpai23 | 0:cb8104f3ae71 | 469 | /***************************/ |
dheerajmpai23 | 0:cb8104f3ae71 | 470 | bool hardware_reset(int bcn_call){ |
dheerajmpai23 | 0:cb8104f3ae71 | 471 | if (bcn_call>20){//Worst Case 20seconds will be lost ! |
dheerajmpai23 | 0:cb8104f3ae71 | 472 | return 1; |
dheerajmpai23 | 0:cb8104f3ae71 | 473 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 474 | int count=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 475 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 476 | adf.write(CMD_HW_RESET); |
dheerajmpai23 | 0:cb8104f3ae71 | 477 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 478 | wait_ms(2);// Typically 1 ms |
dheerajmpai23 | 0:cb8104f3ae71 | 479 | while(count<1000 && miso_err_cnt<1000){ |
dheerajmpai23 | 0:cb8104f3ae71 | 480 | if(MISO_PIN){ |
dheerajmpai23 | 0:cb8104f3ae71 | 481 | |
dheerajmpai23 | 0:cb8104f3ae71 | 482 | if(!assrt_phy_off(0,0,0)){ |
dheerajmpai23 | 0:cb8104f3ae71 | 483 | break; |
dheerajmpai23 | 0:cb8104f3ae71 | 484 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 485 | count++; |
dheerajmpai23 | 0:cb8104f3ae71 | 486 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 487 | else{ |
dheerajmpai23 | 0:cb8104f3ae71 | 488 | wait_us(50); |
dheerajmpai23 | 0:cb8104f3ae71 | 489 | miso_err_cnt++; |
dheerajmpai23 | 0:cb8104f3ae71 | 490 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 491 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 492 | if(count==1000 ||miso_err_cnt==1000){ |
dheerajmpai23 | 0:cb8104f3ae71 | 493 | return hardware_reset(bcn_call+1); |
dheerajmpai23 | 0:cb8104f3ae71 | 494 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 495 | else |
dheerajmpai23 | 0:cb8104f3ae71 | 496 | return 0; |
dheerajmpai23 | 0:cb8104f3ae71 | 497 | |
dheerajmpai23 | 0:cb8104f3ae71 | 498 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 499 | |
dheerajmpai23 | 0:cb8104f3ae71 | 500 | |
dheerajmpai23 | 0:cb8104f3ae71 | 501 | /*******************************************/ |
dheerajmpai23 | 0:cb8104f3ae71 | 502 | int main() |
dheerajmpai23 | 0:cb8104f3ae71 | 503 | { |
dheerajmpai23 | 0:cb8104f3ae71 | 504 | adf.format(8,0); |
dheerajmpai23 | 0:cb8104f3ae71 | 505 | adf.frequency(1000000); |
dheerajmpai23 | 0:cb8104f3ae71 | 506 | /**ADDED On 21/10/2015 ***/ |
dheerajmpai23 | 0:cb8104f3ae71 | 507 | /******************/ |
dheerajmpai23 | 0:cb8104f3ae71 | 508 | /***********/ |
dheerajmpai23 | 0:cb8104f3ae71 | 509 | bool state_0=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 510 | adf.write(CMD_PHY_OFF); //0xB0 |
dheerajmpai23 | 0:cb8104f3ae71 | 511 | while(hw_reset_err_cnt<2){ |
dheerajmpai23 | 0:cb8104f3ae71 | 512 | |
dheerajmpai23 | 0:cb8104f3ae71 | 513 | if(!assrt_phy_off(0,0,0)){ //assrt_phy_off () returns 0 if state is PHY_OFF , returns 1 if couldn't go to PHY_OFF |
dheerajmpai23 | 0:cb8104f3ae71 | 514 | bbram_write(); |
dheerajmpai23 | 0:cb8104f3ae71 | 515 | bbram_flag=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 516 | break; |
dheerajmpai23 | 0:cb8104f3ae71 | 517 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 518 | else{ |
dheerajmpai23 | 0:cb8104f3ae71 | 519 | hardware_reset(0); // Asserts Hardware for 20sec(20times). PHY_OFF for 20,000 times |
dheerajmpai23 | 0:cb8104f3ae71 | 520 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 521 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 522 | state_0=assrt_phy_off(0,0,0);// We actually do not need this but make sure "we do not need this" |
dheerajmpai23 | 0:cb8104f3ae71 | 523 | if(!bbram_flag){ |
dheerajmpai23 | 0:cb8104f3ae71 | 524 | //Switch to beacon |
dheerajmpai23 | 0:cb8104f3ae71 | 525 | bcn_flag=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 526 | } |
dheerajmpai23 | 0:cb8104f3ae71 | 527 | initiate(); |
dheerajmpai23 | 0:cb8104f3ae71 | 528 | send_data(); |
dheerajmpai23 | 0:cb8104f3ae71 | 529 | cout<<"Work"<<ENDL; |
dheerajmpai23 | 0:cb8104f3ae71 | 530 | CS=0; |
dheerajmpai23 | 0:cb8104f3ae71 | 531 | adf.write(0x0B);//IRQ_SOURCE_Random write //To read CMD = 2B 36 FF FF |
dheerajmpai23 | 0:cb8104f3ae71 | 532 | adf.write(0x36);//Address : 0x336 |
dheerajmpai23 | 0:cb8104f3ae71 | 533 | adf.write(0xFF);//Put Low Clearing the MCR Value of the Source Register |
dheerajmpai23 | 0:cb8104f3ae71 | 534 | CS=1; |
dheerajmpai23 | 0:cb8104f3ae71 | 535 | |
dheerajmpai23 | 0:cb8104f3ae71 | 536 | ticker.attach_us(&Interrupt,32000); |
dheerajmpai23 | 0:cb8104f3ae71 | 537 | } |