Страницы

Страницы

четверг, 26 июня 2014 г.

Варианты описания структуры и объявления переменных типа структура в С

  Идентичные структуры могут быть объявлены несколькими немного отличными способами

typedef struct {                                   struct ADCChannel {                          struct {
 unsigned char channel;                        unsigned char channel;                       unsigned char channel;
 unsigned char mode;                           unsigned char mode;                          unsigned char mode;
 float amplifier;                                     float amplifier;                                 float amplifier;
} ADCChannel;                                     };                                                      } ADC0, ADC[16];
...                                                            ...
ADCChannel ADC0;                           struct ADCChannel ADC0, 
ADCChannel ADC[16];                       ADC[16];

среда, 25 июня 2014 г.

printf

Функция printf уже реализована в стандартной библиотеке непосредственному использованию ее мешает, то, что библиотека не знает, куда отправлять получившийся текст, - в USB-CDC, Ethernet, UART .  В нашем случае мы перенаправим ее в  ITM. Для данной задачи необходимо реализовать всего одну функцию: int fputc(int c, FILE *stream). В ней  будем отправлять всё в отладочный порт. Для этого где-то в CMSIS есть объявление и реализация функции ITM_SendChar, которая записывает символ в нулевой порт трассировщика, чтоб передать его в отладчик.

#include <stm32f4xx.h>
#include <stdio.h>

int fputc(int c, FILE *stream)
{
    return ITM_SendChar(c);
}
int main(void)
{    
    while(1)
    {
       printf("Hello world!");
    }

}

вторник, 24 июня 2014 г.

Writing Assembly Code

#include <stdio.h>
int square(int i);
int main(void)
{
  int i;
  for (i=0; i<10; i++)
    {  
       printf("Square of %d is %d\n", i, square(i));
    }
 }
int square(int i)
{
return i*i;

}

C as ARM code

    AREA |.text|, CODE, READONLY
    EXPORT square
    ; int square(int i)
square
    MUL r1, r0, r0    ; r1 = r0 * r0
    MOV r0, r1         ; r0 = r1
    MOV pc, lr          ; return r0

    END

C as Thumb code

    AREA |.text|, CODE, READONLY
    EXPORT square
    ; int square(int i)
square
    MUL r1, r0, r0    ; r1 = r0 * r0
    MOV r0, r1         ; r0 = r1
    BX lr                    ; return r0

    END

пятница, 6 июня 2014 г.

RTC initialisation (инициализация часов) STM32F (STM32F103)

#include "stm32F10x.h"
#include "rtc.h"

//************************** Часы реального времени **************************

//****************************************************************************
//             Инициализация часов                            //
//****************************************************************************  
int  RtcInit  (void)
{
  //разрешить тактирование модулей управления питанием и управлением
  //резервной областью - enable APB1 clocks
  RCC->APB1ENR |= RCC_APB1ENR_PWREN | RCC_APB1ENR_BKPEN;
  // disable backup domain write protection
//разрешить доступ к области резарвных данных
  PWR->CR |= PWR_CR_DBP;
  //если часы выключены - инициализировать их
//Bit 15 RTCEN: RTC clock enable
//Set and cleared by software.
//0: RTC clock disabled
//1: RTC clock enabled
if ((RCC->BDCR & RCC_BDCR_RTCEN) != RCC_BDCR_RTCEN)
{
//выполнить сброс области резервных данных
// reset Backup Domain
//Bit 16 BDRST: Backup domain software reset
//Set and cleared by software.
//0: Reset not activated
//1: Resets the entire Backup domain
RCC->BDCR |= RCC_BDCR_BDRST;
RCC->BDCR &= ~RCC_BDCR_BDRST;
//выбрать источником тактовых импульсов внешний кварц 32768 и подать тактирование
//Bit 15 RTCEN: RTC clock enable
//Set and cleared by software.
//0: RTC clock disabled
//1: RTC clock enabled
//Bits 9:8 RTCSEL[1:0]: RTC clock source selection
//Set by software to select the clock source for the RTC. Once the RTC clock source has been selected,
// it cannot be changed anymore unless the Backup domain is reset. The BDRST bit can be used to reset them.
//00: No clock
//01: LSE oscillator clock used as RTC clock10: LSI oscillator clock used as RTC clock
//11: HSE oscillator clock divided by 128 used as RTC clock
RCC->BDCR |= RCC_BDCR_RTCEN | RCC_BDCR_RTCSEL_LSE;
//установка маски разрешения секундных прерываний
//Bit 0 SECIE: Second interrupt enable
//0: Second interrupt is masked.
//1: Second interrupt is enabled.
RTC->CRH |= RTC_CRH_SECIE; ///!!!!!!!!!!!!SECOND INTERRUPT ENABLE
//Bit 4 CNF: Configuration flag
//This bit must be set by software to enter in configuration mode so as to allow new values to
//be written in the RTC_CNT, RTC_ALR or RTC_PRL registers. The write operation is only
//executed when the CNF bit is reset by software after has been set.
//0: Exit configuration mode (start update of RTC registers).
//1: Enter configuration mode.
RTC->CRL |= RTC_CRL_CNF;
//If the input clock frequency (fRTCCLK) is 32.768 kHz, write 7FFFh in this register to get a
//signal period of 1 second.
RTC->PRLH = 0;
RTC->PRLL = 0x8000;         //тактирование от внешнего кварца

AFIO->EVCR =0xAD; //PC13-calibration clock output enable

BKP->RTCCR |= BKP_RTCCR_CCO; //Setting this bit outputs the
//RTC clock with a frequency divided by 64 on the TAMPER pin
BKP->RTCCR |= BKP_RTCCR_ASOS | BKP_RTCCR_ASOE;
//0: Exit configuration mode (start update of RTC registers).
RTC->CRL &= ~RTC_CRL_CNF;
    //установить бит разрешения работы и дождаться установки бита готовности
RCC->BDCR |= RCC_BDCR_LSEON;
while ((RCC->BDCR & RCC_BDCR_LSEON) != RCC_BDCR_LSEON){}

//while ((RCC->BDCR & RCC_BDCR_LSERDY) != RCC_BDCR_LSERDY); //вариант

//Bit 3 RSF: Registers synchronized flag
//This bit is set by hardware at each time the RTC_CNT and RTC_DIV registers are updated
//and cleared by software. Before any read operation after an APB1 reset or an APB1 clock
//stop, this bit must be cleared by software, and the user application must wait until it is set to
//be sure that the RTC_CNT, RTC_ALR or RTC_PRL registers are synchronized.
//0: Registers not yet synchronized.
//1: Registers synchronized.
RTC->CRL &= (uint16_t)~RTC_CRL_RSF;
while((RTC->CRL & RTC_CRL_RSF) != RTC_CRL_RSF){}
return 0;
}
   return 1;
}