2016-12-21 12:01 AM
Posted on December 21, 2016 at 09:01
Hello. I found something strange in my STM32F100RC controller. I made a clean project in latest CubeMX to show this error.
When I try toswitch my MCU from full JTAG (reset state) to use only SWD pins the programming/debug interface stops working completely. I was forced to reflow two ICs before I found there is the error with init procedure.
The code:
int main(void) {
uint32_t initPauseCounter = 0xFFFFF; // pause that helps us to burn new firmware
while(initPauseCounter--); // before SWD stops working
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
__HAL_AFIO_REMAP_SWJ_ENABLE();
printf('AFIO->MAPR state: 0x%08lX\n', READ_REG(AFIO->MAPR));
printf('AFIO_MAPR_SWJ_CFG_NOJNTRST: 0x%08lX\n', AFIO_MAPR_SWJ_CFG_NOJNTRST);
printf('writing...\n');
__HAL_AFIO_REMAP_SWJ_NONJTRST();
printf('AFIO->MAPR state: 0x%08lX\n\n', READ_REG(AFIO->MAPR));
printf('AFIO_MAPR_SWJ_CFG_JTAGDISABLE: 0x%08lX\n', AFIO_MAPR_SWJ_CFG_JTAGDISABLE);
printf('writing...\n');
__HAL_AFIO_REMAP_SWJ_NOJTAG();
printf('AFIO->MAPR state: 0x%08lX\n\n', READ_REG(AFIO->MAPR));
printf('AFIO_MAPR_SWJ_CFG_DISABLE: 0x%08lX\n', AFIO_MAPR_SWJ_CFG_DISABLE);
printf('writing...\n');
__HAL_AFIO_REMAP_SWJ_DISABLE();
printf('AFIO->MAPR state: 0x%08lX\n\n', READ_REG(AFIO->MAPR));
while (1);
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
It uses only SWD pins, USART1 and clocking from external oscillator. printf() function just redirecting output to USART1 by redefinition fputc() function.
Result from my device with F100RC MCU:
AFIO->MAPR state: 0x00000000
AFIO_MAPR_SWJ_CFG_NOJNTRST: 0x01000000
writing...
AFIO->MAPR state: 0x02000000
AFIO_MAPR_SWJ_CFG_JTAGDISABLE: 0x02000000
writing...
AFIO->MAPR state: 0x06000000
AFIO_MAPR_SWJ_CFG_DISABLE: 0x04000000
writing...
AFIO->MAPR state: 0x0E000000
And from Discovery board with F100RB MCU:
AFIO->MAPR state: 0x00000000
AFIO_MAPR_SWJ_CFG_NOJNTRST: 0x01000000
writing...
AFIO->MAPR state: 0x01000000
AFIO_MAPR_SWJ_CFG_JTAGDISABLE: 0x02000000
writing...
AFIO->MAPR state: 0x03000000
AFIO_MAPR_SWJ_CFG_DISABLE: 0x04000000
writing...
AFIO->MAPR state: 0x07000000
As you can see I read from the AFIO->MAPR register not the data that Iwrote to it before. The values
0x03000000 and
0x07000000 does not match to any JTAG modes.
How can I work this out?
2016-12-21 12:42 AM
Ok, refers to RM0041:
7.4.2 AF remap and debug I/O configuration register (AFIO_MAPR)
...
Bits 26:24 SWJ_CFG[2:0]: Serial wire JTAG configuration These bits are write-only (when read, the value is undefined).
But SWD still not working after
__HAL_AFIO_REMAP_SWJ_NOJTAG();
2016-12-21 01:54 AM
Update.
The code generated by CubeMX calls __HAL_AFIO_REMAP_SWJ_NOJTAG() macro first, in HAL_Init(). But after this there is a calls to MX_SPI1_Init(), MX_I2C1_Init() and other peripherial remapping which uses CLEAR_BIT() and SET_BIT() macros. But these macros works as READ-MODIFY-WRITE to the AFIO->MAPR register. So state of the SWJ_CFG bits always be undefined and different from
HAL_Init() call.
This is obvious code generator bug or HAL architecture bug.
2016-12-21 10:57 AM
Hi
Novikov.Dmitry
I have moved your post to
where product-related questions are asked. If you have any further questions post them here.Thanks
Oli
2016-12-27 03:37 AM
Dear
Novikov.Dmitry
,Thisissue is already reported internally.
Sorry for the inconvenience it may bring andthank you for your understanding.
Best Regards
Imen
2017-11-24 06:41 AM
Hi Imen.
I would like to know are there any official clarifications regarding this issue. It is critical bug and should have been fixed - it makes Cube F1 remap functionality unusable in lots of cases.
I tried to use bit-banding for the purpose of modifying separate bits in AFIO_MAPR register in following manner:
#define BIT_BAND_PERIPH(REG,BIT_POS) (*(volatile uint8_t*)(PERIPH_BB_BASE+32*((uint32_t)(&(REG))-PERIPH_BASE)+4*((uint32_t)BIT_POS)))
#define __HAL_AFIO_REMAP_TIM2_PARTIAL_1() do{ BIT_BAND_PERIPH(AFIO->MAPR, AFIO_MAPR_TIM2_REMAP_PARTIALREMAP2_Pos) = 0; \
BIT_BAND_PERIPH(AFIO->MAPR, AFIO_MAPR_TIM2_REMAP_PARTIALREMAP1_Pos) = 1; \
}while(0U)�?�?�?�?�?�?�?�?�?�?
It works perfect if I place call to this macro before remapping JTAG to SWO pins like this:
However, if I change order of macros invoked, as in case with CubeMX autogenerated code, the error still there:
Modifying macro in following manner didthe trick for me, but it's inflexible solution:
#define __HAL_AFIO_REMAP_TIM2_PARTIAL_1() AFIO->MAPR = 0x02000100;�?
Whencould we expect bugfix or at least some clarifications. Maybe it is worth mentioning in MCU errata?