MOD-BT and STM32-H152. USART doesn't work.

Started by davidsegovia, November 03, 2014, 04:18:35 PM

Previous topic - Next topic

davidsegovia

Hello everyone. I bought a STM-H152 board and MOD-BT, and I have been for one month trying this to work. I have read all documents about this, I have seen lots of code examples, I have proved lots of configurations,... And nothing works. I need some help.

I use emIDE 2.20 and STM32L1xx Standard Peripheral Library 1.3.0. And this is my simple code for main.c:


#include "main.h"
#include "stm32l1xx_conf.h"

void delay(uint32_t n)
{
while (n--) {
asm volatile ("nop");
}
}

void Leds_Init()
{
    GPIO_InitTypeDef GPIO_Leds;
    GPIO_Leds.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_Leds.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_Leds.GPIO_OType = GPIO_OType_PP;
    GPIO_Leds.GPIO_Speed = GPIO_Speed_40MHz;
    GPIO_Leds.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOE, &GPIO_Leds);
    GPIO_SetBits(GPIOE, GPIO_Pin_10 | GPIO_Pin_11);
}

void Led_Pulse(int led)
{
    if(led==1)
    {
        GPIO_ResetBits(GPIOE, GPIO_Pin_10);
        delay(2000000);
        GPIO_SetBits(GPIOE, GPIO_Pin_10);
        delay(2000000);
    }
    else
    {
        GPIO_ResetBits(GPIOE, GPIO_Pin_11);
        delay(2000000);
        GPIO_SetBits(GPIOE, GPIO_Pin_11);
        delay(2000000);
    }
}

void BT_Init()
{
    /* Configure USART2 Rx & Tx as alternate function */
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    /* Mux out USART2 Rx & Tx */
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2);
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2);
/*
    //RST PB7 Reset (active LOW)
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    GPIO_InitTypeDef GPIO_RST;
    GPIO_RST.GPIO_Pin = GPIO_Pin_7;
    GPIO_RST.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_RST.GPIO_OType = GPIO_OType_PP;
    GPIO_RST.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_RST.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOB, &GPIO_RST);
    GPIO_ResetBits(GPIOB, GPIO_Pin_7);
*/
//USART_DeInit(USART2);

// Configure USART2
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);

// Enable the USART2 Interrupt
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

// Enable USART2 Receive interrupts
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

// Enable the USART2 //
USART_Cmd(USART2, ENABLE);
}

void BT_Send_Byte(unsigned int c)
{
    while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
    {
    }
    USART_SendData(USART2, c);
    //Led_Pulse(1);
}

void BT_Send_Line(char *line)
{
    while(*line) {
BT_Send_Byte((unsigned int) *line);
line++;
}
}

unsigned int BT_Receive_Byte()
{
    unsigned int c = 0;

    if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)
{
        c = USART_ReceiveData(USART2);
    }

return c;
}

void RCC_config()
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

    /* Allow access to the RTC */
    PWR_RTCAccessCmd(ENABLE);

    /* Reset Backup Domain */
    RCC_RTCResetCmd(ENABLE);
    RCC_RTCResetCmd(DISABLE);

    /*!< LSE Enable */
    RCC_LSEConfig(RCC_LSE_ON);

    /*!< Wait till LSE is ready */
    while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
    {}
    /*!< LCD Clock Source Selection */
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
}

int main(void)
{
    //RCC_DeInit();

    /* Set the Vector Table base location at 0x08000000 */
    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);

    //RCC Config
    RCC_config();

    // Enable GPIO Clock
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD | RCC_AHBPeriph_GPIOE, ENABLE);

    // Enable USART2 Clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

    Leds_Init();

    BT_Init();

    while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)
    {
        Led_Pulse(1);
        int c = BT_Receive_Byte();
    }

    Led_Pulse(2);
    BT_Send_Line("+++\r\n");
    Led_Pulse(2);
    BT_Send_Line("+++\r\n");
    Led_Pulse(2);
    BT_Send_Line("+++\r\n");
    Led_Pulse(2);
    BT_Send_Line("ATZ\r\n");
    Led_Pulse(2);
    BT_Send_Line("AT+BTAUT=1,0\r\n");
    Led_Pulse(2);
    BT_Send_Line("AT+BTSRV=1\r\n");

    while(1)
    {
        Led_Pulse(2);
    }
}


First of all, I configure RCC. I copied that code from here: http://fjrg76.wordpress.com/2013/06/10/basic-uart-driver-on-the-stm32l152-discovery-board/
And I configure GPIO and USART, and I enable clocks and interrupt. I don't know if there is something wrong until here.

I added this code to stm32l1xx_it.c and header to stm32l1xx_it.h:

void USART2_IRQHandler(void)
{
    Led_Pulse(1);
    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
unsigned int c = USART_ReceiveData(USART2);
}
}


But it never executes the interruption. Notice that if the interruption has been executed, the red led will turn on and turn off after a delay of a second. But when I load the code to the board, it never turn on.

I think I have written the send byte function correctly. It sends one character, one by one. I send the commands to enable server, but nothing happens. The board doesn't receive any data from MOD-BT. And I guess the board doesn't transmit correctly.

I only use TX and RX pins. I have to configure the rest of pins? CTS, RTS, DTR, DSR, STAT, and RST? How?

When I configure pin RTS (reset) and put it to 0, yellow led from MOD-BT turn on, but nothing works anyway.

Please, would anyone help me? I am blocked on this for too long.

Lurch

You need to read the stuff on the Wiki page of Olimex about the -BT.
I removed the resistors to make it work.
QuoteOlimex sMOD-BT

Pin#   Signal
--------------
1   3.3V
2   GND
3   RXD
4   TXD
5   STAT
6   RST
7   CTS <-
8   RTS ->
9   DSR (DCE) <-
10   DTR (DTE) ->

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Can you check if you have R4, R5, R7, R9, R10 mounted on the board?
You might need to remove those if you aren't aiming for full UART (just RX and TX are needed for data communication with MOD-BT).
We actually don't populate those resistors in the last MOD-BT revision.
Only CTS signal remains (pin 7 of the connector for MOD-BT)!
Be careful with CTS (Clear To Send)!
It might disable the whole BT communication if sending is not enabled by CTS.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

davidsegovia

Hello, Lurch.

Yes, the bluetooth module have those resistors. I connected only the necessary pins (3.3V, GND, RX, TX, and CTS) with wires just like you can see in this photo: https://twitter.com/nitronux/status/526367966615834624

I added this code for configure CTS pin:


        //CTS PE14 Clear To Send
        GPIO_InitTypeDef GPIO_CTS;
GPIO_CTS.GPIO_Pin = GPIO_Pin_14;
GPIO_CTS.GPIO_Mode = GPIO_Mode_OUT;
GPIO_CTS.GPIO_OType = GPIO_OType_PP;
        GPIO_CTS.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_CTS.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init(GPIOE, &GPIO_CTS);
GPIO_SetBits(GPIOE, GPIO_Pin_14);


But still not works.

Thanks for help me.