AnsweredAssumed Answered

transfer 8btt data using GPIO 8 ports with CLK 42MHz

Question asked by song.duke on Feb 5, 2015
Latest reply on Feb 5, 2015 by Clive One
Hi

I have 2 board(STM32F427)

1. this is for transferring board.
2. this is for receiving board.

I'm trying to transfer 8bits data using GPIO 8 ports with CLK 42MHz
(transferring board)

-steps-
1. generate PWM signal for CLK(GPIOE pin 15)
2. when PWM is interrupt on time, I write data bit(GPIOE pin 7~14)


and then, I'm receiving data from gpio 8ports
-steps -
1. check CLK signal(PWM) from GPIOE pin 15
2. if CLK is on rising, I get state each GPIOE pin 7~14.
3. save 8bits data.

This is my idea,  but when I tried it, STM32F427 chip cannot deal with this it.

do you know any suggestion?

I'm beginner, so give me any sample code and tutorial.

thanks

here is my challenge code

#define BUFFER_SIZE     188


volatile unsigned int Timer2_Counter = 0;
volatile unsigned int TImer1_counter = 0;
volatile unsigned int TImer3_counter = 0;
volatile unsigned int TImer8_counter = 0;
unsigned char buffer[BUFFER_SIZE];  //MPEG-2 Packet buffer
unsigned char nid_buffer[BUFFER_SIZE] = {
    0x47, 0x40, 0x10, 0x1F, 0x00, 0x40, 0xF3, 0x8D, 0x30, 0x05, 0xCB, 0x00, 0x00, 0xF0, 0x10, 0x40,
    0x0E, 0x43, 0x72, 0x79, 0x73, 0x74, 0x61, 0x6C, 0x20, 0x50, 0x61, 0x6C, 0x61, 0x63, 0x65, 0xF3,
    0x70, 0x10, 0x04, 0x23, 0x3A, 0xF0, 0x50, 0x41, 0x12, 0x10, 0x44, 0x01, 0x10, 0x84, 0x01, 0x10,
    0xFF, 0x01, 0x11, 0x3F, 0x01, 0x11, 0x7F, 0x01, 0x12, 0x3F, 0x01, 0x5A, 0x0B, 0x03, 0x03, 0xD7,
    0x25, 0x1F, 0x42, 0x41, 0xFF, 0xFF, 0xFF, 0xFF, 0x62, 0x0D, 0xFF, 0x04, 0x28, 0xCF, 0x25, 0x04,
    0x1D, 0x1C, 0x5B, 0x03, 0x4D, 0x56, 0x40, 0x5F, 0x04, 0x00, 0x00, 0x23, 0x3A, 0x83, 0x18, 0x10,
    0x44, 0xFC, 0x01, 0x10, 0x84, 0xFC, 0x02, 0x10, 0xFF, 0xFC, 0x07, 0x11, 0x3F, 0xFC, 0x50, 0x11,
    0x7F, 0xFC, 0x69, 0x12, 0x3F, 0xFC, 0x46, 0x20, 0x05, 0x23, 0x3A, 0xF0, 0x7A, 0x41, 0x24, 0x20,
    0x66, 0x01, 0x20, 0x85, 0x01, 0x20, 0xA1, 0x01, 0x20, 0xC0, 0x01, 0x20, 0xFA, 0x01, 0x21, 0x00,
    0x01, 0x21, 0x04, 0x01, 0x21, 0x45, 0x0C, 0x21, 0xC3, 0x0C, 0x22, 0x40, 0x0C, 0x22, 0x44, 0x02,
    0x20, 0x45, 0x01, 0x5A, 0x0B, 0x02, 0xDF, 0x38, 0x25, 0x1F, 0x81, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
    0x62, 0x0D, 0xFF, 0x03, 0xEC, 0x48, 0x5B, 0x04, 0x72, 0x4E, 0x40, 0x02 };
//QUEUE* qPtr;
char bit_array[8] = {0, 0, 0, 0, 0, 0, 0, 0};


void RCC_Configuration(void);
void Timer3_Configuration(void);
void delay_ms(unsigned int del);
void HexToBinary(char hex_number, char* bit_array);
int _write(int file, char *ptr, int len);
void PWM2_Configuration(void);
void initUSART(void);


int main(void)
{


    uint8_t i = 0, j = 0;


    RCC_ClocksTypeDef RCC_ClocksStructure;


    //Configurate RCC
    RCC_Configuration();


    //Update SystemCoreClock
    SystemCoreClockUpdate();


    //Setup and Enable RCC_AHB1PeriphClockCmd, ALL GPIO port(A, B, C, D, E)
    GPIO_ALL_Setup();
    initUSART();
    //PWM1_Configuration();
    PWM2_Configuration();
    //PWM3_Configuration();


    //Timer3_Configuration();


    //Get SystemClock to be set
    RCC_GetClocksFreq(&RCC_ClocksStructure);


    //Print System Information
    printf("STM32F427VI System Start!!\r\n");
    printf("SYSCLK_Frequency : %lu\r\n", RCC_ClocksStructure.SYSCLK_Frequency);
    printf("HCLK_Frequency : %lu\r\n", RCC_ClocksStructure.HCLK_Frequency);
    printf("PCLK1_Frequency : %lu\r\n", RCC_ClocksStructure.PCLK1_Frequency);
    printf("PCLK2_Frequency : %lu\r\n", RCC_ClocksStructure.PCLK2_Frequency);
    printf("Size Information\r\n");
    printf("char : %d, short : %d, int : %d, float : %d, double : %d, long : %d\r\n", sizeof(char), sizeof(short), sizeof(int), sizeof(float), sizeof(double), sizeof(long));


    //GPIO_SetBits(GPIOA, GPIO_Pin_7);    //LED ON
    //GPIO_SetBits(GPIOE, GPIO_Pin_15);   //TUN_CLK


    while(1)
    {
        /*
        buffer[0] = 0x47;
          buffer[1] = rand() % (0xFF - 0x48 + 1) + 0x48;
          buffer[2] = rand() % (0xFF - 0x48 + 1) + 0x48;
          buffer[5] = 0x40;
          for (i = 3; i < BUFFER_SIZE; i++)
          {
               if (i == 5)
               {
                    continue;
               }
               if ((i == 8) || (i == 9))
               {
                    buffer[8] = 0x00;
                    buffer[9] = rand() % (0xFF - 0x00 + 1) + 0x00;
                    continue;
               }
               buffer[i++] = (rand() & (0xFF - 0x48 + 1) + 0x48);
          }
        */
        /*
        if(i == BUFFER_SIZE)
        {
            i = 0;
        }
        char bit_array[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
        HexToBinary(nid_buffer[i], bit_array);




        GPIO_WriteBit(GPIOE, GPIO_Pin_8, bit_array[0]);         //TUN_D0
        GPIO_WriteBit(GPIOE, GPIO_Pin_10, bit_array[1]);        //TUN_D1
        GPIO_WriteBit(GPIOE, GPIO_Pin_9, bit_array[2]);         //TUN_D2
        GPIO_WriteBit(GPIOE, GPIO_Pin_12, bit_array[3]);        //TUN_D3
        GPIO_WriteBit(GPIOE, GPIO_Pin_11, bit_array[4]);        //TUN_D4
        GPIO_WriteBit(GPIOE, GPIO_Pin_14, bit_array[5]);        //TUN_D5
        GPIO_WriteBit(GPIOE, GPIO_Pin_13, bit_array[6]);        //TUN_D6
        GPIO_WriteBit(GPIOE, GPIO_Pin_7, bit_array[7]);         //TUN_D7
        //printf("buffer[%3d] = %d %d %d %d %d %d %d %d\r\n", i, bit_array[0], bit_array[1], bit_array[2],bit_array[3],bit_array[4],bit_array[5],bit_array[6],bit_array[7]);
        GPIO_SetBits(GPIOE, GPIO_Pin_15);                       //TUN_CLK
        //delay_ms(1);


        GPIO_ResetBits(GPIOE, GPIO_Pin_15);                     //TUN_CLK
        //delay_ms(1);
        i++;
        */




    }


    return 0;
}


void RCC_Configuration(void)
{
    //RCC System reset(for debug purpose
    RCC_DeInit();


    //Enable HSE
    RCC_HSEConfig(RCC_HSE_ON);


    if(RCC_WaitForHSEStartUp() == SUCCESS)
    {
        //Enable Prefetch Buffer
        FLASH_PrefetchBufferCmd(ENABLE);


        //Flash 2 wait state
        FLASH_SetLatency(FLASH_Latency_5);


        //HCLK = SYSCLK
        RCC_HCLKConfig(RCC_SYSCLK_Div1);


        //PCLK1 = HCLK/2
        RCC_PCLK1Config(RCC_HCLK_Div4);


        //PCLK2 = HCLK
        RCC_PCLK2Config(RCC_HCLK_Div2);


        //Set up PLL source = HSE, PLL_M = 8, PLL_N = 336, PLL_P = 2, PLL_Q = 7
        RCC_PLLConfig(RCC_PLLSource_HSE, 8, 336, 2, 7);


        //Enable PLL
        RCC_PLLCmd(ENABLE);


        //Wait still PLL is ready
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);


        //Select PLL as system clock source
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);


        //PLL used as system clock
        while(RCC_GetSYSCLKSource() != 0x08);
    }
}


void PWM2_Configuration(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;


    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);


    TIM_TimeBaseStructure.TIM_Period = 84 - 1;
    TIM_TimeBaseStructure.TIM_Prescaler = 2 - 1;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);


    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);


    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);


    GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM3);
     //because I connected GPIOB pin0 to GPIOE pin15 for using TIM3 CH3


    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 42;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;


    TIM_OC3Init(TIM3, &TIM_OCInitStructure);
    TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
    TIM_ARRPreloadConfig(TIM3, ENABLE);


    TIM_CtrlPWMOutputs(TIM3, ENABLE);


    TIM_Cmd(TIM3, ENABLE);
}




void TIM3_IRQHandler(void)
{
    if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)      //TUN_D5
    {
        if(TImer8_counter == BUFFER_SIZE)
        {
            TImer8_counter = 0;
        }


        HexToBinary(nid_buffer[TImer8_counter], bit_array);


        GPIO_WriteBit(GPIOE, GPIO_Pin_8, bit_array[0]);         //TUN_D0
        GPIO_WriteBit(GPIOE, GPIO_Pin_10, bit_array[1]);        //TUN_D1
        GPIO_WriteBit(GPIOE, GPIO_Pin_9, bit_array[2]);         //TUN_D2
        GPIO_WriteBit(GPIOE, GPIO_Pin_12, bit_array[3]);        //TUN_D3
        GPIO_WriteBit(GPIOE, GPIO_Pin_11, bit_array[4]);        //TUN_D4
        GPIO_WriteBit(GPIOE, GPIO_Pin_14, bit_array[5]);        //TUN_D5
        GPIO_WriteBit(GPIOE, GPIO_Pin_13, bit_array[6]);        //TUN_D6
        GPIO_WriteBit(GPIOE, GPIO_Pin_7, bit_array[7]);         //TUN_D7
        printf("buffer[%d] = %d %d %d %d %d %d %d %d\r\n", TImer8_counter++, bit_array[0], bit_array[1], bit_array[2], bit_array[3], bit_array[4], bit_array[5], bit_array[6], bit_array[7]);


        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    }
}


void delay_ms(unsigned int del)
{
    Timer2_Counter = 0;
    while(Timer2_Counter < del);
}


void TIM2_IRQHandler(void)
{
    if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
        Timer2_Counter++;
    }
}


// HEXADECIMAL TO BINARY
void HexToBinary(char hex_number, char* bit_array)
{
     uint8_t bit_position = 0;
     int i;


     for (i = 7; i >= 0; i--)
     {
          bit_array[i] = (hex_number & (1 << bit_position++)) ? 1 : 0;
     }
}




void initUSART(void)
{
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);


    // The RX and TX pins are now connected their AF so that the USART1 can take over control of the pins
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);


    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_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_Init(USART1, &USART_InitStructure);


    // Enable RXNE/TXE interrupt
    //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);


    USART_Cmd(USART1, ENABLE);


    /*
    //Enable USART1 global interrupt
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);


    NVIC_EnableIRQ(USART1_IRQn);
    */
}


int _write(int file, char *ptr, int len)
{
    int i;


    for(i = 0; i < len; i++)
    {
        while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
        {
        }
          USART_SendData(USART1, ptr[i]);
    }
     return len;
}

Outcomes