mbed library with additional peripherals for ST F401 board
Fork of mbed-src by
This mbed LIB has additional peripherals for ST F401 board
- UART2 : PA_3 rx, PA_2 tx
- UART3 : PC_7 rx, PC_6 tx
- I2C2 : PB_3 SDA, PB_10 SCL
- I2C3 : PB_4 SDA, PA_8 SCL
Revision 54:24d77221bceb, committed 2013-12-07
- Comitter:
- mbed_official
- Date:
- Sat Dec 07 12:45:04 2013 +0000
- Parent:
- 53:ee96c43874d6
- Child:
- 55:3b765ca737a5
- Commit message:
- Synchronized with git revision 2b8e05e0020d3b14d38f2de0b0f00a00b43e571b
Full URL: https://github.com/mbedmicro/mbed/commit/2b8e05e0020d3b14d38f2de0b0f00a00b43e571b/
- the timer data structures were not properly initialized
- changed slave timer mode to external clock 1 (section 15.3.15 of the manual)
- avoid a possible race condition when concatenating the values of the
two 16-bit timers
Changed in this revision
targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/us_ticker.c | Show annotated file Show diff for this revision Revisions of this file |
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/us_ticker.c Tue Dec 03 14:00:05 2013 +0000 +++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/us_ticker.c Sat Dec 07 12:45:04 2013 +0000 @@ -33,6 +33,7 @@ // Time base configuration // TIM1 is used as "master", "TIM4" as "slave". TIM4 is clocked by TIM1. + TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFFFF; TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick TIM_TimeBaseStructure.TIM_ClockDivision = 0; @@ -42,6 +43,7 @@ TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); // Master timer configuration + TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; @@ -51,7 +53,7 @@ TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update); // Slave timer configuration - TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Gated); + TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_External1); TIM_SelectInputTrigger(TIM4, TIM_TS_ITR0); // Enable timers @@ -60,10 +62,21 @@ } uint32_t us_ticker_read() { - uint32_t counter; + uint32_t counter, counter2; if (!us_ticker_inited) us_ticker_init(); - counter = (uint32_t)((uint32_t)TIM_GetCounter(TIM4) << 16) + (uint32_t)TIM_GetCounter(TIM1); - return counter; + // A situation might appear when TIM1 overflows right after TIM4 is read and before the + // new (overflowed) value of TIM1 is read, which would make the code below consider the + // previous (incorrect) value of TIM4 and the new value of TIM1, which would return a + // value in the past. Avoid this by computing consecutive values of the timer until they + // are properly ordered. + counter = counter2 = (uint32_t)((uint32_t)TIM_GetCounter(TIM4) << 16) + (uint32_t)TIM_GetCounter(TIM1); + while (1) { + counter2 = (uint32_t)((uint32_t)TIM_GetCounter(TIM4) << 16) + (uint32_t)TIM_GetCounter(TIM1); + if (counter2 > counter) + break; + counter = counter2; + } + return counter2; } void us_ticker_set_interrupt(unsigned int timestamp) {