6.2.1
ADC - Analog-to-Digital Converter
The ADC implementation consists of four parts. ADC1, ADC2, ADC3 and the common functions part named ADC.
ADC has functions to enable, disable, enable external triggering and cal- ibrate the three ADCs. A single conversion can be sampled by calling the
6.2. MICROCONTROLLER : DRIVERS 93
Type Rule Example
Variables and
Objects
thisIsAVariableOrStructure uartRxQueue
Function Filename_FunctionName() TIM_SetCounterMode()
Defines and Enumerations BIG_LETTERS_ AND_UNDERSCORES FLASH_BASE_ADDRESS Enumeration and Structure types typeName_t ADC_t
Boolean variable isSomethingEnabled isAdc3Done
Table 6.4: Naming conventions used in the microcontroller C code in this thesis.
single conversion function. In addition, a function to disable the internal connection to the internal reference voltage and temperature sensor is avail- able.
ADC1 has functions for initializing measurement of the internal reference voltage, 4fs measurement mode and normal lock-in mode.
ADC2 has a function to initialize the ADC2 as a slave for use in dual mode (synchronous mode) with ADC1. This is used for the normal lock-in mode.
ADC3 has an initialize function to ready the ADC3 to measure the exci- tation voltage on the analog front-end.
6.2.2
DMA - Direct Memory Access
The direct memory access has been implemented for ADC1 and ADC3. Two different implementations of the ADC1 exist. One that transfers the con- verted ADC1 data to a buffer and one that transfers synchronously sampled ADC1 and ADC2 data to a buffer. The DMA for ADC3 is used to trans- fer the sampled ADC3 data. Two independent DMA controllers exist and DMA2 is used for moving ADC3 data and DMA1 for the two others.
A structure containing the address of the sampling buffer, the number of samples to move and a status flag indicating that the move is done has been made. Get and set functions is available to set and get the values of the structure. Each of the three DMA implementations has its own instance of the structure and an initializer function.
6.2.3
DDS - Direct Digital Synthesizer
The DDS has a serial communication interface and a 40-bit register as ex-
plained in chapter 5.2.
The DDS can be initialized by calling the configuration function, which sets the frequency, phase offset and if the DDS is to be powered on or off. The current frequency, phase offset and power status of the DDS is obtainable by their respective functions. A structure holds all of the information related to the DDS.
6.2.4
EXTI - External Interrupt
All functions for the whole application initializing GPIO as external inter- rupts are collected in this module.
The ZCD interrupt line is initialized in this module.
6.2.5
FLASH - Flash Memory
The flash is used to store data that can be kept even when the system is not powered. On system startup, the stored data is read and determines the system state and settings. This means that if the watchdog resets the system, it might be able to recover without the user noticing.
The initialize function erases all the flash pages and readies them for writing. There are own functions to check if a page is used, convert page to the start address of that page and convert an address to the page number. In addition, a function for erasing all or one page and reading and writing 16-bit, 32-bit, float and double data exists.
A test function testing this module is also available.
6.2.6
GPIO - General Purpose Input Output
All GPIO initialization functions for the whole application are collected in this module.
This module also have functions for initializing, setting, clearing and tog- gling port B pins that are not used elsewhere for debugging purposes. For example to be used with a logic analyzer.
6.2.7
IWDG - Independent Watchdog
The independent watchdog (IWDG) is a timer that must be reset within a set time and if it is not, the system will do a software reset. This is done to
6.2. MICROCONTROLLER : DRIVERS 95
make sure the system does not freeze. The watchdog timer uses the LSI as reference and the calibration is necessary to ensure the timeout set is close to the actual timeout. If the timeout is set with an expected LSI clock frequency of 40 kHz when it is in fact 60 kHz, it will timeout two thirds faster than expected and the system might continuously reset itself. An alternative is to set a much higher timeout ensuring that the variations in the LSI clock frequency has less or no effect.
The watchdog can be set up calling the initialize function and then has 200 milliseconds timeout by default. There is a function to set the timeout and start the independent watchdog. The update function is used to keep the system from resetting and a function can be called on system startup to check if the system was reset due to the independent watchdog timeout.
A special function to halt the independent watchdog when debugging is
provided and explained why and how to use in chapter4.3.1.
6.2.8
LED - Light Emitting Diode
The light emitting diodes (LED) has been used for simple debugging pur- poses. A function initializes the LEDs and a different function can turn a specific LED on or off or toggle it.
6.2.9
LSI - Low Speed Internal Oscillator
The low speed internal (LSI) RC oscillator has a frequency of 40 kHz. This oscillator is inaccurate and may vary from 30 kHz to 60 kHz. To further en- hance its accuracy it can be calibrated. This is done by internally connecting the LSI oscillator to a timer (TIM5 on channel 4). The timer counts with the high speed internal (HSI) oscillator as its reference and is used to accurately calculate the actual frequency of the LSI with the resolution of the HSI. The LSI oscillator is used by other parts of the microcontroller, for example the independent watchdog.
The LSI calibration is done by calling the calibrate function. The fre- quency is set by TIM5 ISR when calling the calibrate function and can be read by calling the get function afterwards.
6.2.10
NVIC - Nested Vector Interrupt Controller
As explained in chapter 4.1.3the NVIC controls the interrupt priorities.
All NVIC initialization functions for the whole application are collected in this module.
Also a function to be called before using any of the initialize functions is provided. This function sets how many bits to be used for pre-emption priority and sub-priority. Two bits are used for each, which means four levels for each.
6.2.11
RCC - Reset and Clock Control
The RCC module controls all the peripherals clocks. Most of the peripherals clock has to be enabled before it can be used. It is done this way to decrease unnecessary power consumption.
All RCC initialization functions for the whole application are collected in this module.
6.2.12
SYSTICK - System Tick
The SysTick timer is a 24-bit down counter timer that generates an interrupt when it reaches zero. When zero the SysTick timer is automatically reloaded with the value in the auto-reload register and repeats this process. The Systick timer is a standard timer for all Cortex microcontrollers and can be set up using CMSIS alone. The SysTick is mainly used for providing periodic ticks to a real-time operating system (RTOS) for time keeping or executing periodic tasks.
Calling the initialize function configures the system tick to happen once every millisecond. The delay function can be called to wait for a number of milliseconds in a busy-waiting loop.
6.2.13
TIM - Timer
TIM is the timer module. The TIM implementation consists of four parts. TIM1, TIM2, TIM8 and the common functions part named TIM.
TIM has functions to enable and disable timers. A structure containing the timer prescaler, counting mode, clock divider and counting period are found in this module. There are also function to set these values.
TIM1 has an instance of the structure in TIM and an initialize function to configure the TIM1 with the set parameters. In addition, a function to get the TIM1 instance exist.
TIM2 has a function to configure it as a precision hardware timer similar to SYSTICK and a function to get the current tick.
TIM8 has an instance of the structure in TIM and an initialize function to configure the TIM8 with the set parameters. In addition, a function to get the TIM8 instance exist.