AnsweredAssumed Answered

Problems porting mbed project to SPL

Question asked by hebeler.jochen on Oct 27, 2016
Latest reply on Oct 27, 2016 by hebeler.jochen
Hello,


I ported the microchip MiWi stack to STM32(NucleoL152RE) initaly using the mbed hardware librarys. I do not use the mbed compiler, but rather imported it into EmBitz and use the arm-none-eabi-gcc/g++ compilers. The MRF24J40 RF-module is connected to the SPI connections on the Nucleo board. This worked.


But as the mbed-os is too locked down and the underlying HAL has virtually no documentation I switched to the SPL, therefore created a new project in EmBitz. The MiWi stack only has very little interface to the hardware, I just adapted the SPI communication, pin interrupt and a symbol timer. But the implementation with the SPL does not work, as in there is no communication to other Nodes of the network.
So far I checked various thing, compiler and linker settings, linker scripts, #defines, diffed some files, but the problem persists. 
So, where is my problem? This is the code to initalise the hardware, SPI on GPIOA 5-7, interrupt on GPIOC 7, CS at GPIOB6 and Reset at GPIO9

01.GPIO_InitTypeDef   GPIO_InitStructure;
02.EXTI_InitTypeDef   EXTI_InitStructure;
03.NVIC_InitTypeDef   NVIC_InitStructure;
04.SPI_InitTypeDef    SPI_InitStructure;
05.RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC, ENABLE);
06.RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
07.RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
08. 
09. 
10.GPIO_StructInit(&GPIO_InitStructure);
11. 
12. 
13.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
14.GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
15.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_5;
16.GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
17.GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
18.GPIO_Init(GPIOA, &GPIO_InitStructure);
19. 
20. 
21.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
22.GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
23.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
24.GPIO_Init(GPIOA, &GPIO_InitStructure);
25. 
26. 
27.GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
28.GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
29.GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
30. 
31. 
32.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
33.GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
34.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
35.GPIO_Init(GPIOA, &GPIO_InitStructure);
36.GPIO_ResetBits(GPIOA, GPIO_Pin_9);    //Set Wake high
37. 
38. 
39. 
40. 
41.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
42.GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
43.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_9;
44.GPIO_Init(GPIOB, &GPIO_InitStructure);
45. 
46. 
47.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
48.GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
49.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
50.GPIO_Init(GPIOC, &GPIO_InitStructure);
51. 
52. 
53.GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
54.GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
55.GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
56.GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
57.GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
58.GPIO_Init(GPIOB, &GPIO_InitStructure);
59.GPIO_ToggleBits(GPIOB, GPIO_Pin_5);
60. 
61. 
62.SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource7);
63. 
64. 
65.EXTI_InitStructure.EXTI_Line = EXTI_Line7;
66.EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
67.EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
68.EXTI_InitStructure.EXTI_LineCmd = ENABLE;
69.EXTI_Init(&EXTI_InitStructure);
70. 
71. 
72.NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
73.NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x10;
74.NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
75.NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
76.NVIC_Init(&NVIC_InitStructure);
77. 
78. 
79.//EXTI_GenerateSWInterrupt(EXTI_Line7);
80. 
81. 
82.SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
83.SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
84.SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
85.SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
86.SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
87.SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
88.SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
89.SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
90.SPI_Init(SPI1, &SPI_InitStructure);
91.SPI_CalculateCRC(SPI1, DISABLE);
92.SPI_Cmd(SPI1, ENABLE);


The code to read and write SPI data:
1.uint8_t SendSPIData(uint8_t data)
2. {
3.    //Wait for the data register to be empty
4.    while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
5.    SPI_I2S_SendData(SPI1,data);
6.    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
7.    return SPI_I2S_ReceiveData(SPI1);
8. }



Init Symbol Timer, 16µs per Tick


01.MIWI_TICK ticks;
02. 
03.void InitSymbolTimer(void)
04.{
05.    ticks.Val = 0;
06. 
07.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
08.    TIM_TimeBaseInitTypeDef TIM_InitStructure;
09.    TIM_TimeBaseStructInit(&TIM_InitStructure);
10.    TIM_DeInit(TIM5);
11.    TIM_InitStructure.TIM_Prescaler = 512;//16 * (SystemCoreClock / 1000000);
12.    TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
13.    TIM_InitStructure.TIM_Period = 0xffffffff;
14.    TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
15.    TIM_TimeBaseInit(TIM5, &TIM_InitStructure);
16.    TIM_Cmd(TIM5, ENABLE);
17.}
Within the code I execute the EXTI interrupt 
1.EXTI_GenerateSWInterrupt(EXTI_Line7);



To communicate with the module:


01.uint8_t  MRF24J40_Handler::PHYGetLongRAMAddr(INPUT uint16_t address)
02.{
03.    uint8_t toReturn;
04.    EXTI->IMR &= ~EXTI_Line7;
05.    GPIO_ResetBits(GPIOB, GPIO_Pin_6);
06.    wait_us(1);
07.    SendSPIData(((address >> 3)&0x7F) | 0x80);  // MCC_SPI
08.    SendSPIData(((address << 5)&0xE0));    // MCC_SPI
09.    toReturn = ReadSPIData(0x00);    // MCC_SPI
10.    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == 1);
11.    wait_us(1);
12.    GPIO_SetBits(GPIOB, GPIO_Pin_6);
13.    EXTI->IMR |= EXTI_Line7;
14.    return toReturn;
15.}

Outcomes