Skip to content

Commit 05ac7b8

Browse files
committed
Fix infinite loop on RCO calibration error
1 parent b6cbc40 commit 05ac7b8

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=STM32duino S2-LP
2-
version=1.1.3
2+
version=1.1.4
33
author=SRA
44
maintainer=stm32duino
55
sentence=This library includes drivers for ST S2-LP sub-1GHz transceiver.

src/S2LP.cpp

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -793,33 +793,55 @@ void S2LP::SpiSendRecv(uint8_t *pcHeader, uint8_t *pcBuffer, uint16_t cNbBytes)
793793
/**
794794
* @brief Management of RCO calibration.
795795
* @param None.
796-
* @retval None.
796+
* @retval Error code: 0 on success, -1 on error during calibration of RCO.
797797
*/
798-
void S2LP::S2LPManagementRcoCalibration(void)
798+
int32_t S2LP::S2LPManagementRcoCalibration(void)
799799
{
800-
uint8_t tmp[2],tmp2;
800+
uint8_t tmp[2], tmp2;
801+
uint8_t n_err = 0;
802+
int32_t ret_val = 0;
801803

802804
S2LPSpiReadRegisters(0x6D, 1, &tmp2);
803805
tmp2 |= 0x01;
804806
S2LPSpiWriteRegisters(0x6D, 1, &tmp2);
805807

806-
S2LPSpiCommandStrobes(0x63);
807-
delay(100);
808-
S2LPSpiCommandStrobes(0x62);
809-
810808
do
811809
{
812810
S2LPSpiReadRegisters(0x8D, 1, tmp);
811+
812+
//Check RCO Calibration Error and retry MAX_RCO_ERR times
813+
if ((tmp[0]&0x01) == 1)
814+
{
815+
//Disable TimerCalibrationRco
816+
S2LPSpiReadRegisters(0x6D, 1, &tmp2);
817+
tmp2 &= 0xFE;
818+
S2LPSpiWriteRegisters(0x6D, 1, &tmp2);
819+
820+
//Enable TimerCalibrationRco
821+
S2LPSpiReadRegisters(0x6D, 1, &tmp2);
822+
tmp2 |= 0x01;
823+
S2LPSpiWriteRegisters(0x6D, 1, &tmp2);
824+
n_err++;
825+
}
813826
}
814-
while((tmp[0]&0x10)==0);
827+
while((tmp[0]&0x10) == 0 && n_err <= 3);
815828

816-
S2LPSpiReadRegisters(0x94, 2, tmp);
817-
S2LPSpiReadRegisters(0x6F, 1, &tmp2);
818-
tmp[1]=(tmp[1]&0x80)|(tmp2&0x7F);
829+
if (n_err <= 3)
830+
{
831+
S2LPSpiReadRegisters(0x94, 2, tmp);
832+
S2LPSpiReadRegisters(0x6F, 1, &tmp2);
833+
tmp[1]=(tmp[1]&0x80)|(tmp2&0x7F);
819834

820-
S2LPSpiWriteRegisters(0x6E, 2, tmp);
821-
S2LPSpiReadRegisters(0x6D, 1, &tmp2);
822-
tmp2 &= 0xFE;
835+
S2LPSpiWriteRegisters(0x6E, 2, tmp);
836+
S2LPSpiReadRegisters(0x6D, 1, &tmp2);
837+
tmp2 &= 0xFE;
823838

824-
S2LPSpiWriteRegisters(0x6D, 1, &tmp2);
839+
S2LPSpiWriteRegisters(0x6D, 1, &tmp2);
840+
}
841+
else
842+
{
843+
ret_val = -1;
844+
}
845+
846+
return ret_val;
825847
}

src/S2LP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class S2LP
128128
S2LPStatus S2LPSpiWriteFifo(uint8_t cNbBytes, uint8_t* pcBuffer);
129129
S2LPStatus S2LPSpiReadFifo(uint8_t cNbBytes, uint8_t* pcBuffer);
130130
void SpiSendRecv(uint8_t *pcHeader, uint8_t *pcBuffer, uint16_t cNbBytes);
131-
void S2LPManagementRcoCalibration(void);
131+
int32_t S2LPManagementRcoCalibration(void);
132132
void S2LPCmdStrobeCommand(S2LPCmd xCommandCode);
133133
void S2LPGeneralSetExtRef(ModeExtRef xExtMode);
134134
ModeExtRef S2LPGeneralGetExtRef(void);

0 commit comments

Comments
 (0)