📄 main_ezlink.c.svn-base
字号:
//read interrupt status registers to release all pending interrupts ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register //check whether preamble is detected if( (ItStatus2 & 0x40) == 0x40 ) {//preamble detected //wait for the synch word interrupt with timeout -- THIS is the proposed SW workaround //start a timer in the MCU and during timeout check whether synch word interrupt happened or not delay = 0; do { delay++; } while((delay < 20000) && (NIRQ == 1)); //check whether the synch word interrupt is detected if( NIRQ == 0) {//synch word detected correctly //read interrupt status registers to release all pending interrupts ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register //Enable one interrupt: // a) one which shows that RX FIFO is almost full: 'irxffafull' SpiWriteRegister(0x05, 0x10); //write 0x10 to the Interrupt Enable 1 register SpiWriteRegister(0x06, 0x00); //write 0x00 to the Interrupt Enable 2 register //read interrupt status registers to release all pending interrupts ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register //wait for the interrupt event while( NIRQ == 1); //read interrupt status registers to release all pending interrupts ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register /*RX FIFO almost full interrupt occured*/ //read 32bytes from the FIFO for(temp8=pointer;temp8<pointer+32;temp8++) { rx_packet[temp8] = SpiReadRegister(0x7F); } //update receive buffer pointer pointer = pointer + 32; //wait for the interrupt event while( NIRQ == 1); //read interrupt status registers to release all pending interrupts ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register /*RX FIFO almost full interrupt occured*/ //read 32bytes from the FIFO for(temp8=pointer;temp8<pointer+32;temp8++) { rx_packet[temp8] = SpiReadRegister(0x7F); } //update receive buffer pointer pointer = pointer + 32; //wait for the interrupt event while( NIRQ == 1); //read interrupt status registers to release all pending interrupts ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register /*RX FIFO almost full interrupt occured*/ //read 32bytes from the FIFO for(temp8=pointer;temp8<pointer+32;temp8++) { rx_packet[temp8] = SpiReadRegister(0x7F); } //update receive buffer pointer pointer = pointer + 32; //Enable two interrupts: // a) one which shows that a valid packet received: 'ipkval' // b) second shows if the packet received with incorrect CRC: 'icrcerror' SpiWriteRegister(0x05, 0x03); //write 0x03 to the Interrupt Enable 1 register SpiWriteRegister(0x06, 0x00); //write 0x00 to the Interrupt Enable 2 register //read interrupt status registers to release all pending interrupts ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register //wait for the interrupt event while(NIRQ == 1); //read interrupt status registers ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register /*CRC Error interrupt occured*/ if( (ItStatus1 & 0x01) == 0x01 ) { //disable the receiver chain SpiWriteRegister(0x07, 0x01); //write 0x01 to the Operating Function Control 1 register //blink all LEDs to show the error TX_LED = 1; RX_LED = 1; for(delay = 0; delay < 10000;delay++); TX_LED = 0; RX_LED = 0; } /*packet received interrupt occured*/ if( (ItStatus1 & 0x02) == 0x02 ) { //disable the receiver chain SpiWriteRegister(0x07, 0x01); //write 0x01 to the Operating Function Control 1 register //Read the length of the received payload length = SpiReadRegister(0x4B); //read the Received Packet Length register //get the remaining 32bytes from the RX FIFO for(temp8=pointer;temp8<pointer+32;temp8++) { rx_packet[temp8] = SpiReadRegister(0x7F); } //check whether the content of the packet is valid if(( length == 128 ) && ( rx_packet[127] == 127 )) { //turn on the LED RX_LED = 1; //wait a bit for showing the LED a bit longer for(delay = 0; delay < 10000;delay++); //turn off the LED RX_LED = 0; } } //clear receive buffer pointer pointer = 0; //Enable two interrupts again: // a) one wich shows that a valid preamble received:'ipreaval' // b) second shows if a sync word received:'iswdet' SpiWriteRegister(0x05, 0x00); //write 0x00 to the Interrupt Enable 1 register SpiWriteRegister(0x06, 0xC0); //write 0xC0 to the Interrupt Enable 2 register //read interrupt status registers to release all pending interrupts ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register //enable the receiver chain again SpiWriteRegister(0x07, 0x05); //write 0x05 to the Operating Function Control 1 register } else {//synch word is not detected within timeout --> reset RX chain to workaround the Si4431-A0 errata //disable receiver chain SpiWriteRegister(0x07, 0x01); //write 0x01 to the Operating Function Control 1 register //read interrupt status registers to release all pending interrupts ItStatus1 = SpiReadRegister(0x03); //read the Interrupt Status1 register ItStatus2 = SpiReadRegister(0x04); //read the Interrupt Status2 register //enable RX chain SpiWriteRegister(0x07, 0x05); //write 0x05 to the Operating Function Control 1 register } } } }}/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + FUNCTION NAME: void MCU_Init(void) + + DESCRIPTION: This function configures the MCU + + INPUT: None + + RETURN: None + + NOTES: None + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/void MCU_Init(void){ //Disable the Watch Dog timer of the MCU PCA0MD &= ~0x40; // Set the clock source of the MCU: 10MHz, using the internal RC osc. CLKSEL = 0x14; // Initialize the the IO ports and the cross bar P0SKIP |= 0x40; // skip P0.6 XBR1 |= 0x40; // Enable SPI1 (3 wire mode) P1MDOUT |= 0x01; // Enable SCK push pull P1MDOUT |= 0x04; // Enable MOSI push pull P1SKIP |= 0x08; // skip NSS P1MDOUT |= 0x08; // Enable NSS push pull P1SKIP |= 0x40; // skip TX_LED P1MDOUT |= 0x40; // Enable TX_LED push pull P2SKIP |= 0x01; // skip RX_LED P2MDOUT |= 0x01; // Enable RX_LED push pull P0SKIP |= 0x02; // skip SDN P0MDOUT |= 0x02; // Enable SDN push pull P0SKIP |= 0x80; // skip PB SFRPAGE = CONFIG_PAGE; P0DRV = 0x12; // TX high current mode P1DRV = 0x4D; // MOSI, SCK, NSS, TX_LED high current mode P2DRV = 0x01; // RX_LED high current mode SFRPAGE = LEGACY_PAGE; XBR2 |= 0x40; // enable Crossbar // For the SPI communication the hardware peripheral of the MCU is used //in 3 wires Single Master Mode. The select pin of the radio is controlled //from software SPI1CFG = 0x40; //Master SPI, CKPHA=0, CKPOL=0 SPI1CN = 0x00; //3-wire Single Master, SPI enabled SPI1CKR = 0x00; SPI1EN = 1; // Enable SPI interrupt NSS = 1; // Turn off the LEDs TX_LED = 0; RX_LED = 0;}/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + FUNCTION NAME: void SpiWriteRegister(U8 reg, U8 value) + + DESCRIPTION: This function writes the registers + + INPUT: U8 reg - register address + U8 value - value write to register + + RETURN: None + + NOTES: Write uses a Double buffered transfer + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/void SpiWriteRegister (U8 reg, U8 value){ // Send SPI data using double buffered write //Select the radio by pulling the nSEL pin to low NSS = 0; //write the address of the register into the SPI buffer of the MCU //(important to set the MSB bit) SPI1DAT = (reg|0x80); //write data into the SPI register //wait until the MCU finishes sending the byte while( SPIF1 == 0); SPIF1 = 0; //write the new value of the radio register into the SPI buffer of the MCU SPI1DAT = value; //write data into the SPI register //wait until the MCU finishes sending the byte while( SPIF1 == 0); //wait for sending the data SPIF1 = 0; //Deselect the radio by pulling high the nSEL pin NSS = 1; }/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + FUNCTION NAME: U8 SpiReadRegister(U8 reg) + + DESCRIPTION: This function reads the registers + + INPUT: U8 reg - register address + + RETURN: SPI1DAT - the register content + + NOTES: none + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/U8 SpiReadRegister (U8 reg){ //Select the radio by pulling the nSEL pin to low NSS = 0; //Write the address of the register into the SPI buffer of the MCU //(important to clear the MSB bit) SPI1DAT = reg; //write data into the SPI register //Wait untill the MCU finishes sending the byte while( SPIF1 == 0); SPIF1 = 0; //Write a dummy data byte into the SPI buffer of the MCU. During sending //this byte the MCU will read the value of the radio register and save it //in its SPI buffer. SPI1DAT = 0xFF; //write dummy data into the SPI register //Wait untill the MCU finishes sending the byte while( SPIF1 == 0); SPIF1 = 0; //Deselect the radio by pulling high the nSEL pin NSS = 1; //Read the received radio register value and return with it return SPI1DAT;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -