S12 / MagniV Microcontrollers Knowledge Base

cancel
Showing results for 
Search instead for 
Did you mean: 

S12 / MagniV Microcontrollers Knowledge Base

Labels
  • General 45

Discussions

Sort by:
Hello Community user, This post is intended to show how to recovery from Bus-Off state using the MSCAN and CANPHY modules in the MagniV devices. This example is focused on S12ZVC due to the internal CANPHY so it makes easier to create a quick example. What is Bus-Off condition? The bus-off condition is a catastrophic condition where the node in this state does not transmit anything on the bus. How to enter in Bus-Off state? A node enters in this state if any of the two error counters, Transmit Error Counter (TEC) and Receive Error Counter (REC) raises above 255. How to recovery from Bus-Off? A note is permitted to become error-active (no longer bus-off) when its error counters both set to 0 after 128 occurrences of 11 consecutive recessive bits have been monitored on the bus. The MSCAN module implements two ways to recover from the fault, the automatic and the manual. The automatic bus-off recovery method puts the node into the error-active state after monitoring 128 occurrences of 11 consecutive bits on the bus. It's important to note that the CANPHY also reports errors on the bus so, the proper error flags must be clear to assure the correct functionality of the node if CANPHY is being used. The manual bus-off recovery method (which is used in this example) puts the note into the error-active state after both independent events have become true in any order: 128 occurrences of 11 consecutive recessive bits on the CAN bus have been monitored. (Cannot be consecutive). CANMISC[BOHOLD] bit has been cleared by the user. Implementation The project contains a header file called  "common.h" which contains three defines that are used to set the CAN nodes. Just one define must be active per node. NODE0_TX and NODE1_TX are transmitting messages on the CAN bus and the NODE_RX is receiving the messages. #define NODE0_TX #define NODE1_TX #define NODE_RX‍‍‍ ‍ ‍ ‍ All nodes are blinking the green led indicating the communication is stable and no errors have occurred. At this point, you should generate the bus-off condition. A way to generate a bus-off condition is grounding one of the two CAN lines (CAN_High or CAN_Low) which will cause a bus-off condition to the transmitters nodes. When the node is in the bus-off state, the yellow led is toggled. At this point, you should not be any message being transmitted on the bus due to the transmitter node is in bus-off. The bus-off condition is detected by the application in the bus-off interrupt (refers Events.c file) which triggers the yellow led toggling. void CAN0_OnBusOff ( void ) { /* Get CAN bus status */ CAN0_GetError ( & gCANErrpr ) ; /* If bus error is set, inform to the application*/ if ( gCANErrpr . errName . BusOff ) { gBusOffState = PENDING ; } CAN0RFLG_CSCIF = 1 ; // Clear flag } ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ In the main.c file when the node transmitters are selected, there is a routine asking for a push-button event. This event triggers the bus-off recovery request by setting the CANMISC[BOHOLD]. At that point, the 128 occurrences of 11 consecutive recessive bits on the CAN bus should have passed and the green led will toggle again indicating the communication is stable again and no errors have occurred. At this point, you should see the messages on the CAN bus again. /* Read button to recovery from bus off condition*/ if ( PushButton3_GetVal ( ) ) { CAN0_BusOffRecoveryRequest ( ) ; // Request get out of bus off gBusOffState = NONPENDING ; /* Clear bus off LED indicator */ LED_YEL_OFF ; } ‍‍‍‍‍‍‍‍‍‍‍‍‍‍ ‍‍‍‍‍‍‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ Note: The VLG-MC9S12ZVC was used for this example and the push button that must be pressed is SW12. Best Regards, Atzel Collazo
View full article
Abstract The paper describes basic scaling procedure of a six-step BLDC motor control application with focus on S12ZVM MCU devices.  Abstract Introduction BLDC six-step control Speed scaling Timing Commutation periods Speed calculation Application note links Calculation accuracy Real applications Resources Introduction Based on the various questions regarding the BLDC speed calculation, I've decided to write this document to make the scale calculation clear. Please use it for your reference and comment in case there is something to be explained in more details. BLDC six-step control This document is linked with the six-step control of BLDC motors, known for the trapezoidal back-EMF shaped voltage. The key is to create a torque using DC current in two phases while the third phase is not connected. As the rotor is moving, the phases are switched (commutated) to keep the stator flux ahead the rotor flux. The principles are well described in many application notes, such as AN4718 or AN4704. The instant of the commutations can be driven by Hall sensors or by the back-EMF signal monitoring (so-called "sensorless"). For field-oriented control, please refer to the PMSM control discussed in AN5135 or AN5327. Speed scaling It's more benefitial to describe in details the sensorless algorithm over the hall-sensor-based control. However, the approach is almost the same. Timing The motor speed is calculated based on the zero-cross detection of BEMF voltage on a non-active phase. These zero-cross events times are measured by capturing a timer (TIM0CNT) register at the time the ADC routine detects a zero-cross (or when the Hall sensor commutation event occurs). That means, the time is scaled by the timer TIM0 settings. There is no general guidance how to set up the timer. However, the timer should be set to cover some reasonable number of ticks between two commutations (which is linked to the speed precission at high speeds) and should be able to cover two commutations "far away" from each other at very low speeds, without the timer overflow. A good practice would be: at least 100 timer ticks between two commutations maximum 16bit = 65535 ticks between two commutations The same aproach can be followed using the Hall-sensor events. Commutation periods In the application, zero-cross (or hall-sensor) periods are captured with  periodZC_F_PhA, periodZC_R_PhA , etc. or periodZC[6], defined as tU16 (16 bit unsigned). The periods are captured using a timer, as mentioned above. In electric motor theory, we recognize "electrical" and "mechanical" speed. The "electrical" speed is linked with the rotational field. The mechanical speed is connected directly with the rotor speed. The relation between these two speeds is determined by number of poles or pole-pairs: (1)   mechanical speed = electrical speed / number of pole-pairs = electrical speed / ( number of poles / 2 ) In the following text, 3-phase BLDC motor is discussed. Speed calculation To calculate one "electrical" revolution of a motor = 6 commutations (or 6 zero-crosses), all the 6 commutation time periods shall be summed. For that case, the resulting " period6ZC " is defined, formated as tU32 (or unsigned long) to prevent an overflow if all six 16-bit zero-cross periods are summed. If you sum all the 6 zero-cross periods, you'll get the number of timer ticks per single "electric" revolution. That means, if the motor is 2-pole motor ( = 1 pole-pair motor), it would be the time per single "mechanical" revolution of the rotor. In thsi case: (2)   mechanical speed = electrical speed; You can easily get the time per mechanical revolution for higher-pole motor simply by multipling it by number of pole-pairs. (3)   time per mechanical revolution = period6ZC * number of pole-pairs   The period per 6 zero-crosses  " period6ZC "  is calculated within the control loop, usually in the 1 ms timer interrupt routine. Since the speed (rotor frequency) is just an inverted time period, we can use following code line to calculate the actualSpeed: (4) actualSpeed = SPEED_CALC_NUMERATOR / period6ZC; Now, how to read it's scale and what is the SPEED_CALC_NUMERATOR ? Let's assume the " actualSpeed " is tFrac32 (1.31 formated signed 32bit number). It would mean the maximal fraction number is 1.0, which is represented by its integer value 2^31 = 2,147,483,648. Why to use 32-bit number over 16-bit is obvious - since the period6ZC is 32-bit value, the result should be of the same resolution or width. The task is to find the  right  SPEED_CALC_NUMERATOR . Let's consider the S12ZVM device and the timer TIM0 used to capture the zero-cross events times. The prescaler is set to 16 (TIM0TSCR2_PR = 4) and the bus clock is 12.5MHz, the timer tick is 12.5MHz / 16 = 781.25 kHz, in time scale it is 1.28us. Let's assume the maximum mechanical speed of the motor is 10,000 RPM and the motor has 6-pole-pairs. That would give us (5)   (10,000 RPM) / (60 seconds) = 166.67 revolutions per second, (6)    166.67*(6 pole-pairs) = 1000 electrical revolutions per second, so (7)    1000 * (6 commutations) = 6000 commutations/zero-crosses per second. (8)    That would make 1/6000 = 166.67us per one commutation. (9)   Ticks per one period at max speed = 166.67 us / 1.28us = 130.28 With our 1.28us timer, we can catch upto 130 periods at maximal speed. For maximal speed of the motor 10,000 RPM, we can simply rearrange the " actualSpeed " calulation shown in (4) into (13) and (14), assuming that: (10)    actualSpeed  = FRAC32(1.0) = 2,147,483,648 (11)    period6ZC   = 6 periods * 130 = 780 (rounded down, since the value is still an integer) In case of S12ZVM device, it is more convenient to use 16-bit (tFrac16) calculations than 32-bit. Hence, we can do a trick with the scaling to use 32-bit actualSpeed to prevent an overflow use the same 32-bit number as 16-bit number just by scaling it correctly benefit from the 16bit calculation, which is good enough and fast enough at the same time The trick is hidden in the casting of the   actualSpeed   from Frac32 to Frac16: (12)    speedErr = requiredSpeed - (tFrac16) actualSpeed; That means, only the lower 15 bits are considered the speed (assuming the actualSpeed will never exceed 32767 so the 16th "sign" bit will always be zero). For maximum speed and the SPEED_CALC_NUMERATOR determination, the " actualSpeed " should be 32767 (which is the range of signed tFrac16). Equation (4) can be used to calculate the  SPEED_CALC_NUMERATOR as follows: (13)    32767 = SPEED_CALC_NUMERATOR / 780 (14)    SPEED_CALC_NUMERATOR  = 780 * 32,767 = 25,558,260 Thus  #define SPEED_CALC_NUMERATOR 25558260 Using the equation (4), the speed for the minimum period6ZC would be: (15)    actualSpeed   =   SPEED_CALC_NUMERATOR   /   period6ZC   = 25,558,260 / 780 = 32,767; Masking that value by lower 15 bits and casting it to tFrac16 would give us 1.0. Since the speed scale has been determined by the maximum speed of the motor 10,000 RPM, we can calculate the real speed simply by: (16)   RealScaleSpeedRPM  = (tFrac16) actualSpeed   / 32,767 *   MAX_SPEED   =   (tFrac16) actualSpeed   / 32,767 * 10,000. Application note links In the application note AN4704, there are several macro constants used to control the application. These constants should be calculated using following definitions, with a little help of the SPEED_SCALE - floating point representation of the real scale of the speed. #define SPEED_SCALE 10000.0  // Used for correct calculation of the following: #define REQUIRED_RUN_SPEED FRAC16(2000.0/SPEED_SCALE) // 2000 rpm #define MIN_SPEED FRAC16(500.0/SPEED_SCALE) // 500 rpm minimal speed for Down button control (should be min 10% of nominal motor speed) #define MAX_SPEED FRAC16(5000.0/SPEED_SCALE) // 5 krpm maximum speed for Up button control #define SPEED_STEP FRAC16(100.0/SPEED_SCALE) // 100 rpm, Up/Down step for button controls   If the application uses FreeMASTER to display or control, the scales shall be updated in the FreeMASTER project as well. The new FM scales for AN4704 are: Variable name Variable watch settings requiredSpeed Real type transformation: Linear: a =   SPEED_SCALE   = 10,000; b = 0 actualSpeed Signed int, Size = 4 Show as: REAL Bit fields maks with: word (0xffff) Real type transformation: Linear: a =   SPEED_SCALE   / 32767.0 = 0.3051851; b = 0  Calculation accuracy Now, let's have a look on the speed resolution (or accuracy): At the full speed, if one period changes by 1 tick, then you'll get the   period6ZC   from 780 to 781 at max speed, which makes 12.8041 RPM difference. The higher the   SPEED_SCALE   is, the higher the error is. If the assumption would be a change in all of the 6 periods (thus the   period6ZC   would change from 780 to 786), the resulting change to the speed would be 76.336 RPM. That makes the speed error 10,000 RPM +- 76, which is 0.76%. In terms of accuracy, it makes sense to set the maximum speed (or the SPEED_SCALE) to the maximal speed of the application instead of copying some high number from the datasheet of the motor. Well, the max speed of the application should not exceed the max speed of the motor, but it doesn't help if the speed scale is unnecessarily high. Real applications In real applications, the calculated speed (actualSpeed) reflects all the deviations and changes of the period6ZC. Therefore, it is a good practice to do some kind of filtering of the period6ZC, either by averaging or using a low-pass filter. In case of one or more zero-crosses are lost, there should a mechanism to process and detect if the motor is stalled (mechanical speed loses lock with the commutation) or if it is just a disturbance to the signal. The stall detection or the Hall-signal fault/damage detection is one of the challenges towards high quality application. Resources S12ZVM MCU  - NXP main S12ZVM page AutoMCDevKits - NXP Automotive Motor Control Development Solutions AN4718 - 3-Phase BLDC Hall Sensor Application Using S12ZVM AN4704 -  3-phase Sensorless BLDC Motor Control Kit with the S12 MagniV MC9S12ZVM AN5201 - Integrating the LIN driver with BLDC sensorless motor controller In the S12ZVM128 device AN5330 -   Migration Guide for S12ZVM Devices
View full article
Introduction In many old-fashioned automotive motor control systems, PWM command communication is still preferred over LIN or CAN. Even for in-lab application debugging, it is very convenient to use PWM command communication to control the motor placed in a test-bench or in an acoustic or thermal chamber, especially when the BDM debugger or the SCI/RS232 communication is not available. PWM control is usually specified on the OEM level and it is not provided as a public standard. Nevertheless, adjustable software driver can be created to help the PWM command signal to be detected. The following code has been developed for S12ZVM, but it can be easily adopted to fit in whatever device with a timer input capture feature. Hardware specification may be different for many cases and the implementation is the matter of proper design case-by-case and may be a very easy task for a skilled designer. However, software design can be abstracted and reused for different hardware implementation. In order to help you use the PWM control signal, the following description is provided. The example provided is targeting NXP MagniV S12ZVML devices, but may be used along with any device accordingly. The S12ZVML MCU contains LIN physical layer which will be used as PWM input in our case. PWM Signal Specification Since there is no public standard specification on PWM signal, let's target a general case with adjustable parameters. Since the PWM control signal is being replaced by LIN communication, the specification may be based on 5.5V to 18V voltage range with nominal 12V level. PWM frequency varies from 10 Hz to 1 kHz (or more), but it really depends on the requirements of the application, compatibility, etc. Duty-cycle range of the PWM signal might be from 15% to 90% from stand-by to full speed. Hysteresis at low duty-cycle range (between 10 and 15%) prevents the system from periodic on/off switching at the lowest duty-cycle. Duty-cycles outside of this range may be identified as not valid and appropriate action may be taken (e.g. full speed command to an engine cooling fan). The signal definition should consider some specific values to help with the signal reading. Following values are defined: Label Description noSignalLevel No signal detected below this duty cycle noSignalOutput Output on "no signal" detected lowSignalOutput Output on "low signal" detected hystLowSignalOff Low level of the hysteresis. Smaller duty-cycle means "low signal" state, higher duty-cycle enters the hysteresis range (min speed or stand-by) hystLowSignalOn High level of the hysteresis. Smaller duty-cycle means "hysteresis area: min speed or stand-by", higher duty-cycle means "run"  in the linear signal range, with the speed given by the duty-cycle. linearSignalOutputMin Low level of the linear mode linearSignalLevelMax High level of the linear mode Considering normal operation in stand-by mode, the duty-cycle is between noSignalLevel and hystLowSignalOff. In order to add some distortion-proof feature, the duty-cycle can go up to the hystLowSignalOn and the stand-by mode would be still detected. In order to engage the "run" state, the duty-cycle has to exceed the hystLowSignalOn value and the linear area is reached. Corresponding output is calculated to meet the linearSignalOutputMin at the hystLowSignalOn value and linearSignalOutputMax at the linearSignalLevelMax. System keeps the output at linearSignalOutputMax for duty-cycles higher than linearSignalLevelMax. When slowing down, reaching the hystLowSignalOn sets the output to linearSignalOutputMin and keeps this output until the hystLowSignalOff is reached. This way, the hysteresis feature is provided in order to prevent motors from randomly switch on and off in case the PWM signal is noisy. Lowering the duty-cycle below the hystLowSignalOff, zero output is set, which represents the stand- by mode. Duty-cycles below the noSignalLevel are treated as not valid and noSignalOutput is engaged (which can be set to zero or to max, according to the application requirements). Requirements on Timer In order to correctly detect PWM signal frequency and duty-cycle, hardware timer with input capture feature and rising/falling edges detection shall be used. Timing and chaining of multiple timer channels may be needed to increase the frequency range or duty-cycle resolution. Following example will be simplified to use just one 16-bit timer channel (timer module TIM16B4CV3 used in S12ZVML128). The input of the timer shall be routed to an input pin, on which the PWM signal will be sensed. Integrated LIN physical layer module of S12ZVML128 device can be used for signal conditioning since the signal voltage range is up to 18V. In the example, LINPhy module has to be enabled and the LPRXD0 signal is routed to the timer T0 input capture channel 3 by setting "T0IC3RR1-0" of the MODRR2 register to 0b01. Input capture and timer overflow interrupts are used to detect correct or faulty signal. Requirements on Application Software PWM signal detection is called within the Timer Input Capture interrupt. This way, the duty-cycle is calculated immediately. Signal lost event is detected within the Timer overflow interrupt. It is recommended that the detected duty-cycle is filtered by a low-pass filter (moving average or IIR), which is usually called within some real-time interrupt routine, such as PWM reload or ADC end-of-scan interrupt. Reading of the output value can be done anytime within the code, e.g. within a speed loop to read the demanded speed command. Algorithm Description Frequency and Duty-cycle Detection Frequency of PWM signal is given by the time between two consecutive rising edges or two falling edges. The duty-cycle is a ratio between the time of high-level state over the entire period, as indicated below. The key is to capture the time of the rising edge t R1 , change the detection mode to the falling edge to capture the time of the falling edge t F1 , again change the detection mode to rising edge, etc. This detection can be easily done within the input capture timer interrupt. The time values captured are then used to calculate "High" and "Period" values. Frequency and duty-cycle is calculated based on the following equations. The timer settings should follow requirements on frequency range and duty-cycle resolution. It is recommended that the timer settings allow detecting frequencies slightly beyond the required frequency range. Let's consider an example of: Limit Specified frequency limits Detected frequency Detected period Minimal PWM frequency 20 Hz 19.5 Hz 0.051282 s Maximal PWM frequency 1000 Hz 1010 Hz 0.00099 s Based on these limits, Timer settings can be calculated, considering the MCU clock settings and prescalers: For example, if the f bus = 50 MHz and the TimerPrescaler = 50, MinPeriodTicks for MaxDetectedFreq = 1010 Hz equals 495 ticks of the timer. The duty-cycle resolution at 1010 Hz is then 1/495, which is 0.202%. MaxPeriodTicks is key information for the timer settings since it needs to fit in the 16bit range with no overflow. In this case, the number of ticks is 25641 for 19.5Hz, which is suitable for 16-bit range with no issues. If the detected period (number of timer ticks) is outside of this range, the algorithm should go to the noSignalLevel state with defined noSignalOutput. Finally, if the PWM signal is lost or corrupted, timer overflow event occurs and corresponding interrupt is called. Within the interrupt routine, the entire algorithm should be reset to wait for another rising edge, while turned in the noSignalLevel   state with defined   noSignalOutput . Selecting Detected State Based on the PWM signal condition, frequency and duty-cycle detected, the algorithm state is selected. Following states are defined to help users with state identification: State Description Output  PWM_NoSignalDetect PWM signal is not detected or the PWM period is outside of the range noSignalOutput, usually zero or full command (according to the application requirements) PWM_LowSignalDetect PWM signal is detected, the duty-cycle is within the noSignalLevel and hystLowSignalOn or hystLowSignalOff (depends on the hysteresis) lowSignalOutput, usually zero/stand-by PWM_LinearCtrlDetect PWM signal is detected, the duty-cycle is within the hystLowSignalOn and linearSignalLevelMax output = (dutyCycle * linearSignalSlope + linearSignalOffset) << linearSignalNshift, from linearSignalOutputMin to linearSignalOutputMax PWM_HighSignalDetect PWM signal is detected, the duty-cycle is higher than linearSignalLevelMax linearSignalOutputMax Linear Output Calculation Linear output calculation is an easy way of interpolating two endpoints by a linear equation. In order to calculate the coefficients (slope and offset) and to improve the fixed point range, shifting is implemented as well. Algorithm Integration Interfaces The algorithm is designed as a set of static functions with a structure of settings as a parameter to all the interfaces. This way, the same functionality can be used in multiple instances.  /* PWM Control data structure */ typedef struct { pwmControlStatus_t flags ; //Control status bits pwmControlConfig_t config ; //Configuration unsigned int period ; //Modulation period unsigned int dutyCycle ; //dutyCycle unsigned int hystDutyCycle ; //previous hysteresis of duty cycle #ifndef PWM_CONTROL_SW unsigned int overflowCntr ; //timer overflow counter #endif tFrac16 outputValue ; //Output value unsigned int ControlInputClass ; //Classification of the input signal } pwmControl_t ; ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ The input-output characteristics are defined by a set of parameters as discussed earlier using the following structure: typedef struct { /* No signal definition */ tFrac16 noSignalLevel ; //Level of the duty cycle for NoSignal detection tFrac16 noSignalOutput ; //Output when NoSignal is detected /* Low signal definition */ tFrac16 lowSignalOutput ; //Output when LowSignal is detected /* Linear signal definition */ tFrac16 hystLowSignalOff ; //LowSignal-to-NoSignal hysteresis level tFrac16 hystLowSignalOn ; //NoSignal-to-LowSignal hysteresis level tFrac16 linearSignalLevelMax ; //Level of the duty cycle for LinearSignal saturation tFrac16 linearSignalOutputMin ; //Output at hystLowSignalOn tFrac16 linearSignalOutputMax ; //Output at linearSignalLevelMax tFrac16 linearSignalSlope ; // ((linearSignalOutputMax - linearSignalOutputMin)/(hystLowSignalOn - linearSignalLevelMax)))*2^(-linearSignalNshift) tFrac16 linearSignalOffset ; // (linearSignalOutputMax - (linearSignalSlope * linearSignalLevelMax))*2^(-linearSignalNshift) tU16 linearSignalNshift ; // scaling shift of the linear curve /* timing settings */ unsigned int minPeriod ; //minimal period of the signal to be detected, shorter period leads to PWM_NoSignalDetect unsigned int maxPeriod ; //maximal period of the signal to be detected, longer period leads to PWM_NoSignalDetect } pwmControlConfig_t ; ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ More details on the structures and values are described in the header file in attachment. There is a set of exported functions to be integrated into the application. /* Exported function headers */ extern void PWMControlInit ( pwmControl_t * data ) ; //Clears all the internal accumulators and prepars the algorithm for usage extern unsigned int PWMControlUpdate ( unsigned int pin , pwmControl_t * data ) ; //To be called within the timer input capture interrupt, updates the internal values and calculates the PWM frequency and duty-cycle extern tFrac16 PWMControlGetOutputValue ( pwmControl_t * data ) ; //To be called where necessary by the application to get the output value extern void PWMControlTimerOverflow ( pwmControl_t * data ) ; ‍‍‍‍‍ //To be called within the timer overflow interrupt to indicate that the signal is lost‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ ‍ ‍ ‍ ‍ ‍ Integration The PWM control source and header files should be included in the project to be compiled. The project uses NXP Automotive Math and Motor Control Library Set (AMMCLib version v1.1.17 or higher), which needs to be added to the project as well. For more information, please visit https://www.nxp.com/support/developer-resources/run-time-software/automotive-software-and-tools/automotive-math-and-motor-control-library-set:AUTOMATH_MCL?lang=en&lang_cd=en& The next step is to include the header file in the main application source code: #include "pwm_control.h" ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ ‍ In order to provide necessary settings and to handle the output, the following global variables are recommended: // PWM Control data pwmControl_t pwmControlData ; //PWM input control data structure static tBool pwmControlEnabled = false ; // Off by default‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ ‍ ‍ ‍ Device initialization should include clock settings, timer, and input pin routing settings accordingly. For S12ZVML device, the configuration of TIM, PIM and LIN modules is following: Within the initialization part of the application, the following code should be placed to initialize the PWM control. Please consider the values as an example only.  // PWM control initial settings pwmControlData . config . minPeriod = 495 ; //Period of the max frequency pwmControlData . config . maxPeriod = 25641 ; //Period of the min frequency pwmControlData . config . noSignalLevel = FRAC16 ( 0.05 ) ; //No signal detected below this duty cycle pwmControlData . config . noSignalOutput = FRAC16 ( 0 ) ; //Output on ""no signal"" detected pwmControlData . config . lowSignalOutput = FRAC16 ( 0 ) ; //Output on ""low signal"" detected pwmControlData . config . hystLowSignalOff = FRAC16 ( 0.1 ) ; //Low level of the hysteresis. Smaller duty cycle means ""low signal"", higher duty cycle means ""switch on"" pwmControlData . config . hystLowSignalOn = FRAC16 ( 0.15 ) ; //High level of the hysteresis. Smaller duty cycle means ""min speed or off"", higher duty cycle means ""switch on and linear"" pwmControlData . config . linearSignalLevelMax = FRAC16 ( 0.85 ) ; //High level of the linear mode pwmControlData . config . linearSignalOutputMin = FRAC16 ( 0.116666666666667 ) ; //Output within the hysteresis mode pwmControlData . config . linearSignalOutputMax = FRAC16 ( 0.833333333333333 ) ; //Output at the linearSignalLevelMax pwmControlData . config . linearSignalSlope = FRAC16 ( 0.511904761904762 ) ; //Linear signal slope = (linearSignalOutputMax - linearSignalOutputMin) / (linearSignalLevelMax - hystLowSignalOn) * 2^(-linearSignalNshift) pwmControlData . config . linearSignalOffset = FRAC16 ( - 0.018452380952381 ) ; //Linear signal offset = (linearSignalOutputMin - (linearSignalOutputMax - linearSignalOutputMin) / (linearSignalLevelMax - hystLowSignalOn) * hystLowSignalOn) * 2^(-linearSignalNshift) pwmControlData . config . linearSignalNshift = 1 ; //Linear signal shift pwmControlData . config . f16pwmInputMA . u16NSamples = 5 ; //Moving average filter settings 2^n samples PWMControlInit ( & pwmControlData ) ; ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ The configuration can be generated using an Excel sheet in the attachment. Content of the cell "D34" can be copied-pasted in the code. Name of the structure can be changed in "H1" cell. Orange-colored cells are customizable, while grey-colored cells use formulas to calculate output. Timer input capture interrupt routine should call the PWM control "Update" routine and switch the input capture mode between the rising edge and falling edge detection. In the case of S12ZVML device, the timer interrupt example follows: INTERRUPT void TIM0chan3_ISR ( void ) { // Read PWM input control pin and update status PWMControlUpdate ( TIM0TC3 , & pwmControlData ) ; // Read the PWM Control output pwmControlData . outputValue = PWMControlGetOutputValue ( & pwmControlData ) ; // Toggle edge detection if ( pwmControlData . flags . risingEdge ) { TIM0TCTL4_EDG3A = 1 ; TIM0TCTL4_EDG3B = 0 ; } if ( pwmControlData . flags . fallingEdge ) { TIM0TCTL4_EDG3A = 0 ; TIM0TCTL4_EDG3B = 1 ; } // Clear interrupt flag TIM0TFLG1 = TIM0TFLG1_C3F_MASK ; //TIM0TFLG1_C0F_MASK | TIM0TFLG1_C1F_MASK | TIM0TFLG1_C2F_MASK | TIM0TFLG1_C3F_MASK; } ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ Timer overflow interrupt routine shall implement PWM control "Overflow" handler as follows (again, for S12ZVML device). Output value updater should be called as well to update the output value. INTERRUPT void TIM0overflow_ISR ( void ) { // Detect PWM input control period overflow (ultra low frequencies PWMControlTimerOverflow ( & pwmControlData ) ; pwmControlData . outputValue = PWMControlGetOutputValue ( & pwmControlData ) ; // Clear interrupt flag TIM0TFLG2 = TIM0TFLG2_TOF_MASK ; } ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ Finally, within the application task, the PWM control data can be accessed and used in the application. For example, based on the ControlInputClass, the input signal processing may be enabled or disabled and the output value can be used as the required speed value of an engine cooling fan. [ Application Task ] { // ... // PWM Control // If no signal is detected, enable manual switch (using FreeMASTER) if ( pwmControlData . ControlInputClass == PWM_NoSignalDetect ) { if ( pwmControlEnabled == true ) { cntrState . usrControl . switchAppOnOff = 0 ; pwmControlEnabled = false ; } } else { pwmControlEnabled = true ; } // If PWM input control enabled, update the demanded speed if ( pwmControlEnabled ) { //If the linear or high signal is detected, switch the app on if ( pwmControlData . ControlInputClass >= PWM_LinearCtrlDetect ) { cntrState . usrControl . switchAppOnOff = 1 ; //Store the demanded speed drvFOC . pospeControl . wRotElReq = pwmControlData . outputValue ; } //else switch off the app... else { cntrState . usrControl . switchAppOnOff = 0 ; //...and if a fault is indicated, clear the fault if ( cntrState . state == fault ) { cntrState . usrControl . switchFaultClear = true ; } } } // ... } ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ Example code Example code is provided for S12ZVML-MINIKIT. The code is a modified devkit SW, which is available here (and related AN5327 here). The FreeMASTER project file is included with added PWM Control subblock. If there is no PWM signal detected, the application can be controlled via FreeMASTER on/off button. If the PWM signal is detected (even with a duty-cycle below the minimal value), the application listens and reacts as defined by the signal definition. Conclusion PWM control is still being used as one of the control signals to control auxiliary electric drives in automotive. Even if being replaced by LIN or CAN communication, there are still facilities which can benefit from software drivers enabling such a feature. In this document, a software driver is provided to be integrated within the application. The example is provided for S12ZVML devices, however, it can be easily adopted to any device which meets the hardware requirements necessary for such PWM control signal detection.
View full article
The package contains of AN5327_SW ported to S12ZVML-MINIBRD hardware. Just minor changes are introduced, such as removing a button control, moving the LED light to another pin and using internal oscillator. This is just a working version, it is NOT an official release!!! AN5327_SW_CW11_MINIBRD.ZIP CodeWarrior 11.0 and AMMCLib 1.1.13 or higher is required to run this example. If you are not sure about the AMMCLib version, please download the general AN5327_SW from the www.nxp.com/automcdevkits first, install it and then unzip and use the example above.
View full article
How to get device ID and write program once field without programming application to the S12Z device in the CodeWarrior (Eclipse).   Even the datasheet does not present the device ID it can be read at address 0x1FC000. The device ID is size of 8 bytes from 1F_C000 to 1F_C007. The meaning of the field is confidential information. Of course, other "reserved" fields on higher addresses can also be read but their meaning is also confidential. The ID is not presented in the documentation because it has reason for company only. However, some users require unique identification of the device. It is possible to use this number. In order to read the ID, it is enough to create simple project in the CodeWarrior and connect to the device without programming it and run “mem” command in the debug shell window. If you do not want to remember address ranges, then it is better to prepare command file and execute it.   Of course, we can also simply use memory window.   It is a different story if we want also to see program once field. In this case we have to execute a set of commands to get it. As an example of command file which can be executed to get device ID and program the program once field I present two command files. They can be executed in the debugger window – see and test attached project. They are AAA_Read_Device_ID_And_OTPROM.cmd  and AAA_ProgramOnceField.cmd. The project which also presents reading and writing program once field is not relevant but should be created for the device we want to play with. It is not required to be code (simple code, enough to use what is generated by wizard) loaded into the MCU’s memory. However, necessary is the debugger is connected to a memory. Because of this it is enough to set:   …. And press debug   In the debug enable “Debugger Shell”:   Before continuing please modify flash clock setup in both files to value suitable for programming on the basis of the oscclk you use. In the files search for lines    Write into debugger shell command which runs the AAA_Read_Device_ID_And_OTPROM.cmd: source "c:\D\CODING & SW\ECLIPSE\S12ZVMC-FLASH-WRITEONCEFIELD-CW106\AAA_Read_Device_ID_And_OTPROM.cmd" (it is possible you have different path to the file…please change it)   The command will execute and shows: Device ID And Program field phrase 0. If you want to see another phrase you should modify AAA_Read_Device_ID_And_OTPROM.cmd line set PHRASE 1. Change 1 to the phrase you want to see and run the command presented above “source …..” again. It is enough to press arrow up on the keyboard to see and use previous commands.   In order to program selected field use and run command “source” with the file: AAA_ProgramOnceField.cmd source "c:\D\CODING & SW\ECLIPSE\S12ZVMC-FLASH-WRITEONCEFIELD-CW106\AAA_ProgramOnceField.cmd"   The same note as previosly, the path you have could be different so adjust it. The same line as in previous file should be changed to select the phrase you want to program. As a result, you will see programming algorithm and also read back of the field, example of output: %>source "c:\D\CODING & SW\ECLIPSE\S12ZVMC-FLASH-WRITEONC EFIELD-CW106\AAA_ProgramOnceField .cmd" ############################ Programming Field = 2 ############################ cmdwin::mem 0x0386 %x = 0x30 cmdwin::wait 1000 cmdwin::mem 0x0382 %x = 0x05 cmdwin::mem 0x038C %x = 0x07 cmdwin::mem 0x038D %x = 0x00 cmdwin::mem 0x038E %x = 0x00 cmdwin::mem 0x038F %x = 2 cmdwin::mem 0x0390 %x = 0xA2 cmdwin::mem 0x0391 %x = 0xB2 cmdwin::mem 0x0392 %x = 0xC2 cmdwin::mem 0x0393 %x = 0xD2 cmdwin::mem 0x0394 %x = 0xE2 cmdwin::mem 0x0395 %x = 0xF2 cmdwin::mem 0x0396 %x = 0xA3 cmdwin::mem 0x0397 %x = 0xB3 cmdwin::mem 0x0386 %x = 0x80 cmdwin::wait 1000 ############################ Read back Programmed Field = 2 ############################ cmdwin::mem 0x0386 %x = 0x30 cmdwin::wait 1000 cmdwin::mem 0x0382 %x = 0x01 cmdwin::mem 0x038C %x = 0x04 cmdwin::mem 0x038D %x = 0x00 cmdwin::mem 0x038E %x = 0x00 cmdwin::mem 0x038F %x = 2 cmdwin::mem 0x0386 %x = 0x80 cmdwin::wait 1000 cmdwin::mem 0x0390 8      390  $a2 $b2 $c2 $d2 $e2 $f2  $a3 $b3    . . . . . . . .    Now the question, where to find commands to be able to prepare such a command files, can be asked. 1) The first information source is the CodeWarrior help. For example, search for keyword DW and it could find a command list. 2) Tcl Reference Manual   3) http://www.tcl.tk/     Finally, in the attached project can be seen also approach how to write program once field by SW. Of course, mentioned cmd files are also part of the project.
View full article
This application note was already officially released. Please, download it from: AN12086 Simple Serial Bootloader for S12Z AN12086SW software package    1.  Introduction This application note covers the operation and use of a simple serial Bootloader for the S12Z microcontroller families. The Bootloader can be a convenient way to support programming during production or “in-system”, where support for the dedicated HCS12 Background Debug interface (BDM) may not be available. Users must pre-program the S12Z MCU with the Bootloader during production or at a programming vendor. The Bootloader should reside in the MCU for further use. This Bootloader implementation allows User Application software to be downloaded into the MCU flash and EEPROM memory using the SCI serial interface. The Bootloader may be also configured for self-update similar way as a User Application software update. This Bootloader is originally based on AN4258 Serial Bootloader for S12(X) Microcontrollers Based on 180 nm Technology with several modifications and improvements. The Bootloader described in this document is only an example and comes with no guarantees and no direct support.   2. Bootloader Overview The software architecture consists of two independent software projects: Bootloader Project User Application Project For avoiding compatibility issues between these projects, the minimum interface points must be kept. The switching between Bootloader and User Application codes is managed through MCU reset. So, the Bootloader and application are not limited by write-once registers. The Bootloader Project itself contain small user-defined dispatcher code which is executed directly after MCU reset and decide whether Bootloader or User Application will be executed as next. Figure 1 Software Architecture 2.1.     Hardware Compatibility The Bootloader is written for S12Z derivatives (S12ZVM, S12ZVMB, S12ZVMA, S12ZVC, S12ZVL, S12ZVH, S12ZVHY and S12ZVFP). The Bootloader for S12Z microcontrollers is not optimized for the smallest derivatives. Therefore, S12ZVL8 derivative (only 512B RAM) is not supported and only some of derivatives (with 4kB or more RAM) supports Bootloader self-update feature (rewritable).   2.2.     Dispatcher code The Dispatcher code is executed right after MCU reset. It just temporary initializes stack (write stack pointer) and directly executes user code for testing conditions whether Bootloader code should be executed or not. The most typical testing conditions for Bootloader presents: pushed button some specific value in the EEPROM memory waiting loop for command via the communication interface If User Application code is already loaded and Bootloader was not selected, the User Application is automatically started.   2.3.     Serial interface All S12Z microcontrollers include an on-chip serial communication interface. Notice that the RS-232 or USB to TTL level shifter is necessary to communicate with PC. Optionally, the virtual COM port at OSBDM interface may be used. By default, the serial communication is set to format: 8-data bit one start bit one stop bit no parity Xon / Xoff flow control Default baud rate is configured in Config.h file. The baud rate can be changed via the Bootloader’s menu to 38400, 57600, or 115200 bps. The Bootloader implementation currently assumes using just one of SCI channels. However, this may be changed by Bootloader code modification. The same approach may be used for a different type of channel implementation like CAN or LIN.   2.4.     PC software interface A general terminal emulation program like Microsoft HyperTerminal, Tera Term, RealTerm or similar software can be used on a PC to communicate with a microcontroller. A terminal emulation program must support communications over serial COM ports, Xon / Xoff flow control, and it must be able to send a text file.   3.  Functional Description The operation of the Bootloader is straightforward. This section describes only the most important and specific points. Figure 2 Memory map 3.1.     Bootloader Project Folders The C language codes and appropriate header files are located in Sources and Project_Header folders. The exception is Dispatcher.c and start12z.c files located in Project_Settings\Startup_Code folder. The linker file is located in Project_Settings\Linker_Files folder. The FLASH folder contain generated files like elf, map or s19 files. The SRecCvt folder contains a SRecCvt.exe tool for re-formating generated S-record file. The content of Documentation folder should help you in guide how to use this Bootloader. Figure 3 Bootloader Project Folders 3.2.     Bootloader Configuration The most of the Bootloader configuration settings are collected in Config.h file.   If the modified Bootloader will use any derivative specific registers, the mc9s12zvl128.h and mc9s12zvl128.c files should be replaced with appropriate files from CodeWarrior folders: "c:\Freescale\CW MCU v10.7\MCU\lib\wizard_data\S12Z\DataBase\device\include\.." and "c:\Freescale\CW MCU v10.7\MCU\lib\wizard_data\S12Z\DataBase\device\src\.."  And followed by modification of derivative.h file.   The Config.h contains settings for: Bootloader feature configuration Clock settings SCI module settings Dispatcher definitions MCU derivative configuration and addresses Flash Configuration Field configuration   The Bootloader-persistent.prm and Bootloader-rewritable.prm linker files contain definitions of Flash and RAM ranges for Bootloader. The appropriate linker file is selected by Build Configuration. Figure 4 Build Configuration   3.2.1.     Bootloader features SECURE_MCU: The MCU may be secured after Bootloader loading (default 0 = MCU unsecured) USE_BACKDOOR_KEY: The Flash Configuration Field may already contain Backdoor Key for temporary unsecure of MCU (default 0 = backdoor key 0xFFFF FFFF FFFF FFFF). WRITE_EEPROM: When enabled, the Bootloader menu offers an option for erasing EEPROM and program option will be able to program both Flash and EEPROM. BOOTLODER_REWRITABLE: When enabled, the Bootloader code is automatically loaded into RAM and allow unprotect, erase and program Bootloader Flash area. This variable is already defined differently in used Build Configurations. That allows us to keep BOOTLODER_REWRITABLE value and selected linker file synchronized.   3.2.2.     Serial Channel The current S12Z MCU derivatives offer up to 2 SCI channels. The SCI_MODULE defines whether SCI0 or SCI1 will be used for Bootloader communication (default SCI1). The DEFAULT_BAUDRATE specifies default SCI baud rate for initial Bootloader menu display (default Baud9600: 9600bps). The baud rate can be changed via the Bootloader’s menu to 38400, 57600, or 115200 bps. The S12Z MCU derivatives offer routing options for SCI0, SCI1, and other peripherals channels. The BL_MODRR0 value defines MODRR0 value loaded by Bootloader code after a reset.    Note: The MODRR0 values may be written just once in normal mode and cannot be changed until next MCU reset.   3.2.3.     Configure Dispatcher code The default Dispatcher condition is based on the testing value of pin with push button. The PUSH_BOTTON defines tested port and pin (default PTP_PTP1) while PB_ACTIVE_LEVEL defines active logic level – when the push button is pressed (default 0 = push button to GND with pull-up). The BL_REQUEST_ADD defines tested address (default 0x100000) while BL_REQUEST_VALUE defines active value for bootloader request key. When the memory content fits to the BL_REQUEST_VALUE, Bootloader code is executed (default "BOOT" in ASCII = 0x424F4F54).   3.2.4.     Configure Derivative and addresses The FLASH_START_ADD, EEPROM_START_ADD, EEPROM_END_ADD defines MCU derivative memory ranges. The BOOTLOADER_START_ADD value is automatically obtained from linker file (ROM segment start). The Bootloader automatically move User Application reset vector to the APPLICATION_RESET_VEC_ADD address (default BOOTLOADER_START_ADD-4 = placed to the end of User Application flash area)   3.2.5.     Configure Flash Configuration Field The bytes from BACKDOOR_KEY_0 to BACKDOOR_KEY_7 defines Backdoor Comparison Key (default "UNSECURE" = 0x554E534543555245 in ASCII). The bytes PROTECTION_OVERRIDE_KEY_H and PROTECTION_OVERRIDE_KEY_L defines Protection Override Comparison Key (default "AB" = 0x4142 in ASCII). The Bootloader code should be protected against unwanted erase/rewrite operations. Trying to alter data in any protected area in the P-Flash memory will result in a protection violation error and the FPVIOL bit will be set in the FSTAT register. Flash protection scheme allows protecting 2, 4, 8 or 16KB from upper range of Flash memory. The BL_FPROT defines FPROT value automatically loaded from Flash Configuration Field after reset (default 0xD7 = upper 8kB of P-Flash protected.). The BL_DFPROT defines DFPROT value automatically loaded from Flash Configuration Field after reset (default 0xFF = EEPROM unprotected). The initial configuration of COP watchdog and GDU (S12ZVM derivatives) is given by FOPT register value. The BL_FOPT defines FPROT value automatically loaded from Flash Configuration Field after reset (default 0xFF = COP is not active after reset).   3.2.6.     Bootloader linker file Since the preprocessor functionality is no longer supported directly in prm linker file, the Bootloader use two versions of linker file: Bootloader-persistent.prm Bootloader-rewritable.prm The linker file selection is managed by two Build Configurations (Figure 4 Build Configuration).   3.3     Operation The Bootloader handles reset vector. After reset, the Bootloader’s Dispatcher routine is called. As a first step, the Dispatcher initialize stack and reads the push button pin status. If the value of the pin is equal to the PB_ACTIVE_LEVEL, the Bootloader starts its operation. If the value of this pin is not equal to the PB_ACTIVE_LEVEL, the User Application’s reset vector is tested. If the reset vector of the User Application is not available (long word at address APPLICATION_RESET_VEC_ADD is in erased state = 0xFFFFFFFF) then the Bootloader is called anyway. The User Application is called only when User Application reset vector is already programmed and the condition for Bootloader enter is not true. The user can rewrite this code to start the Bootloader or User Application upon another condition. Note: Since this code is executed prior Bootloader/application startup code, the dispatcher code shouldn’t use any uninitialized global variables.   3.4.     Interrupt vector In the case when the User Application using interrupts, it is necessary to relocate the interrupt vector table using the IVBR register. The Bootloader resides in the upper 4/8/16kB fixed block at the address defined by linker prm file. This area is had to be protected (configured in Config.h), so the User Application cannot rewrite a default location of the interrupt vector table at 0xFFFE10-0xFFFFFF. The advantage of this solution is that the Bootloader cannot be affected by a power failure that could occur when rewriting User Application interrupt and reset vectors. The Bootloader automatically moves User Application’s reset vector from address 0xFFFFFD to the  APPLICATION_RESET_VEC_ADD address = typically the end of User Application’s flash area. Since this moving is managed by the Bootloader, the whole phrase (by default from (BOOTLOADER_START_ADD-8) to BOOTLOADER_START_ADD) must be excluded from User Application’s linker usage and the same sector (512B) shouldn’t be used for User Application’s interrupt vector table.   4.  User Guide This section describes the step-by-step procedure to use the Bootloader.   4.1.     Setup procedure for the Bootloader Open the Simple_Serial_Bootloader_for_S12Z project in CodeWarrior Development Studio for MCU. The Bootloader was tested in current versions v10.7. Select the Build Configuration as shown in Figure 4 Build Configuration. The Persistent Bootloader code option allows just update a User Application. The Bootloader Rewritable code option linker allows updating both User Application and Bootloader itself. Optionally edit appropriate linker file Optionally edit the Config.h header file per your target environment (required features, clocks, channels, derivative addresses, backdoor, and protection override keys). Optionally edit the c file with conditions for select User Application/Bootloader code execution.   4.2.     Bootloader Guide   1. Compile the project and download the Bootloader to the MCU via BDM device. Note: The correct derivative should be selected in debug configuration for successful complete MCU Flash erase and programming (Figure 5 Select derivative for Bootloader download). Figure 5 Select derivative for Bootloader download     2. Open the Microsoft HyperTerminal or any other serial terminal utility (e.g. Realterm, Tera Term,…). Set the baud rate 9600 bps, 1 start bit, 8 data bits, 1 stop bit, and flow control Xon / Xoff.   3. Make sure there is a serial (USB to Serial) cable connection between the PC and the board.   4. Hold the PUSH_BOTTON pin low and reset the MCU.   5. The Bootloader is started and the following response is received in HyperTerminal. See Figure 6 Initial screen.   Figure 6 Initial screen   6. Type “a” to erase the flash memory. This is not necessary the first time because the flash has been already erased by the BDM device.   7. Type “b” to program the flash.   8. Now send the desired S-record as a text file — See Figure 7 Send Text File. Browse for S-record which is downloaded to the MCU. For testing purposes, use demo S-records that are attached to this application note. The S-records must have a specified format. Refer to chapter 3 How to write a User Application and to chapter 4.4 How to convert S-Record that will be downloaded by the Bootloader. Figure 7 Send Text File     9. Confirm the dialog and the S-record are downloaded to the MCU. One printed star (*) means that one line of S-record has been programmed. See Figure 8 Download User Application. Figure 8 Download User Application   Note: If User Application contains any EEPROM data, please erase EEPROM by typing “h” prior the step 7. Note: If you want to reprogram Bootloader code, please use subsequently options “e” (it will ask you for entering protection override key), step “f” and step “g” instead steps 6 and 7.   4.3.     How to write a User Application You must ensure that the User Application does not interfere with Bootloader area defined by Bootloader linker file.   1. Create a new User Application project in CodeWarrior Development Studio for MCU.   2. Open the User Application project.prm file   3. Trim the high address of the segment ROM from the original to the (BOOTLOADER_START_ADD-8). For example, to the address 0xFFDFF7. This is because the area 0xFFDFF8–0xFFFFFF is occupied by the Bootloader and the area at address 0xFFDFF8–0xFFDFFF will be used for the User Application reset vector. If interrupts are used:   4. Trim the low address of the segment ROM from the original to (the original address + 0x200). This area will be used for the relocated interrupt vector table. Note: You may use any other Flash sector in User Application memory for that purpose except the last one (already used for the User Application reset vector). Note: The generated CodeWarrior project doesn’t show warning about memory overlapping between linker and user object placement. This warning is disabled by default (Linker option -WmsgSd1912).      5. Create a vector table as shown in the attached demo applications and set the IVBR register accordingly. The IVBR sets the interrupt vector table base address. It contains 15 most significant bits from 24bit vector table address.   4.4.     How to convert S-Record that will be downloaded by the Bootloader The default S12Z MCU project generated by CW10 does not generate a file with s-records automatically.   1. Check option for generating S-Record file in User Application project settings per Figure 9 Generate S-Record file The <YourProjectName>.sx file will be generated when you will build your project. Figure 9 Generate S-Record file For the simplifying, the Bootloader parser, only s-records with specific format are accepted. All records must be aligned to 32 bytes and the length of records must be 32 bytes.   2. Copy SRecCvt directory with SRecCvt.exe tool from Bootloader/Application Example project to your User Application project   3. Exter command “ ${ProjDirPath}/SRecCvt/sreccvt.exe -hc12 -m 100000 FFFFFF 32 -o ..\FLASH\\${BuildArtifactFileBaseName}.s19 ..\FLASH\\${BuildArtifactFileBaseName}.sx ” as post build step into User Application project settings per Figure 10 Adding Post-build step . Figure 10 Adding Post-build step This command will load S-Records from your <YourProjectName>.sx file and create new <YourProjectName>.s19 file with reformatted S-Records when you build your project. Note: The complete SRecCvt utility may be downloaded from NXP web pages. Unfortunately, the graphical user interface doesn’t support directly S12Z derivatives. Therefore we use this tool in command line mode.   4.5.     How to merge the User Application and Bootloader The User Application can be developed independently, that is without the Bootloader. The User Application can be loaded into the microcontroller and can be debugged directly via the BDM device. However, for production purposes, it is worth merging the User Application and Bootloader together, so it can be downloaded into the microcontroller all at once as a single S-Record file. This is a recommended procedure:   1. Open the User Application that was created as per chapter 3, "How to write a User Application" in the CodeWarrior Development Studio for MCU.   2. Replace a User Application reset vector in Project.prm file from default address to the address (BOOTLOADER_START_ADD-3), so the Bootloader can use this vector for start User Application. For example: //VECTOR 0 _Startup VECTOR ADDRESS 0xFFDFFD _Startup   3. Copy the ready-mades19 file to ..\user_application_project\FLASH directory. Note: the s-record file can be renamed for example to the bootloader.s19.   4. Link this file to the User Application be using command at the beginning of Project.prm: HEXFILE Simple_Serial_Bootloader_for_S12Z.s19   5. Build the project. The final S-Record is ready to be downloaded into the microcontroller by the BDM interface.   5.  Testing The Bootloader and example application were tested on evaluation boards: DEVKIT-ZVL128 S12ZVMC256EVB S12ZVM32EVB S12ZVM-MINIBRD   6.  Content of Zip file All mentioned projects and utilities can be found in a zip file associated with this application note: Simple_Serial_Bootloader_for_S12Z — Bootloader project for S12Z microcontrollers. Application_Example_for_SSBZ — Projects that show how to write User Applications. Converted_srecords — S-Records was taken from Bootloader and Application Example that has been converted by the SRecCvt utility (*.s19 file extension). These S-Record files can be downloaded into the MCU by the Bootloader. SRecCvt — Utility for S-Record files converting.   7.  Reference AN4258 AN4258SW: Serial Bootloader for S12(X) Microcontrollers Based on 180 nm Technology Rev 0. February 2011. S12Z Protection Override S12Z constant, variable, code allocation in CodeWarrior S12Z Interrupt catcher for unexpected interrupts S12Z Flash example code S12Z EEPROM example code   I hope it help you. Please let me know when you find any error or if something missing in this document.   Radek Sestak
View full article
Officially we offer two S12ZVM motor control kits: MTRCKTSBNZVM128: 3-phase Sensorless BLDC Development Kit with S12 MagniV S12ZVM MTRCKTSPNZVM128: 3-phase Sensorless PMSM Development Kit with S12 MagniV® MC9S12ZVML128 MCU   In fact, the kit contents are the same - S12ZVML12EVBLIN board and LINIX 45ZVN24-90 motor. The LINIX motor is BLDC type, but their characteristics are rather close to the PMSM type. So, the difference is just in used software and jumper settings.   Since the current web pages are slightly confused, I would like to place here links for the main 4 types of software for MTRCKTSBNZVM128 and MTRCKTSPNZVM128 kits.   3-Phase Sensorless BLDC S12ZVM Application – six step driving http://www.nxp.com/docs/en/user-guide/MTRCKTSBNZVM128QSG.pdf http://www.nxp.com/docs/en/application-note/AN4704.pdf https://www.nxp.com/webapp/Download?colCode=MTRCKTSBNZVM128_SW&appType=license  3-Phase Hall Sensor BLDC S12ZVM Application – six step driving http://www.nxp.com/docs/en/user-guide/MTRCKTSBNZVMHQSG.pdf http://www.nxp.com/docs/en/application-note/AN4718.pdf http://www.nxp.com/docs/en/application-note-software/AN4718SW.zip 3-phase Sensorless Dual-Shunt PMSM S12ZVM Application – FOC driving http://www.nxp.com/docs/en/user-guide/MTRCKTSPNZVM128QSG.pdf http://www.nxp.com/docs/en/application-note/AN5135.pdf https://www.nxp.com/webapp/Download?colCode=MTRCKTSPNZVM128-SW&appType=license  3-phase Sensorless Single-Shunt PMSM S12ZVM Application – FOC driving http://www.nxp.com/assets/documents/data/en/user-guides/AN5327-QSG.pdf Note: please be aware that there is still typo error in this document (already reported) – the jumpers J57 and J60 should be in position 2-3. http://www.nxp.com/docs/en/application-note/AN5327.pdf http://www.nxp.com/docs/en/application-note-software/AN5327SW.zip EDIT: Additionally, we may use a cost-effective S12ZVM Mini Board or Mini Kit evaluation platform. The software refers to the above mentioned, but with small modification for S12ZVML-MINBRD board.  I hope it helps you.
View full article
This example: 1. write unprotected P-Flash area 2. protect target P-Flash area 3. tries to write protected P-Flash area 4. call Protection Override command for temporary modify P-Flash protection 5. write into temporary unprotected P-Flash area 6. restore original P-Flash protection scheme The part of P-Flash memory used in this example for writing tests (0xFF8000..0xFF8FFFUL) is excluded from linker using. Please look at prm linker file for more details.   The flash_array constant with Protection Override Comparison Key is not directly referenced in the code, therefore we must enter the constant name into ENTRIES section in prm linker file for avoiding stripping it out as unused. Please look at P-Flash Protection Register (FPROT) chapter in the reference manual for more details about P-Flash protection. The key for Protection Override should be rather received from external source and used as PFLASH_Protection_Override() parameter. The correct Protection Override key is here hard-coded (err = PFLASH_Protection_Override(0xCAFE, 0xF...) for example code simplifying.   I hope it helps you. Radek
View full article
This example simulates ECC issue by cumulative write into the same EEPROM area without an erasing.   The EEPROM erase operation set all bits into log1. The EEPROM program operation may keep bit cells in log1 state or change it into log0 state, but not in opposite way.   The S12Z MCU EEPROM is protected by 22-Bit ECC Scheme. Every word (16bits) is protected by additional 6 bits with ECC checksum. The ECC values are not accessible for users. Every EEPROM reading triggers also ECC check by internal logic. The single bit error in user data may be corrected by ECC checksum. The double bit error cannot be corrected.   The ECC protection is implemented also at flash controller commands and results are signalling by MGSTAT bits.   The first case simulates Single-bit ECC error during reading. The MGSTAT bits after the second write are 0b10 due to fact that just 1 bit is different during write verification (correctable error)   The second case simulates Single-bit ECC error during reading. The MGSTAT bits after the second write are 0b11 due to fact that more than 1 bits are different during write verification (non-correctable error)   The third case simulates Double-bit ECC error during reading. The MGSTAT bits after the second write are 0b11 due to fact that more than 1 bits are different during write verification (non-correctable error)       The EEPROM patterns are selected for highlighted described behavior and they don't have any real meaning.   The cumulative write is not allowed for normal operation!!! The code from this example should be used only for design testing - not in production!!!   Please, see the prm file. Address range 0x100000-0x100001 is excluded from default EEPROM and is used as user EEPROM memory The size of EEPROM sectors is 2 bytes. The EEPROM_Program() function may program up to 4 words in single flash command.   Note: The EEPROM_Program() function was updated - erase verification is skipped     I hope it helps you. Radek
View full article
For debug purposes, it is possible to read and write the user data and the ECC value directly from/to the SRAM memory. For these debug accesses a register interface is available.   By the debug access and writing incorrect data + ECC values into the system memory,  the single and double bit ECC errors may be simulated for checking the software error handling.   The debug registers may be modified only in a special mode.   The tested address 0x3000 is excluded from linker use - see prm linker file.   I hope it helps you. Radek
View full article
This example simulates ECC issue by cumulative write into the same Flash area without an erasing.   The Flash erase operation set all bits into log1. The Flash program operation may keep bit cells in log1 state or change it into log0 state, but not in opposite way.   The S12Z MCU Flash is protected by 39-Bit ECC Scheme. Every aligned 32bits is protected by additional 7 bits with ECC checksum. The ECC values are not accessible for users. Every Flash reading triggers also ECC check by internal logic. The single bit error in user data may be corrected by ECC checksum. The double bit error cannot be corrected.   The ECC protection is implemented also at flash controller commands and results are signaling by MGSTAT bits.     The first case simulates Single-bit ECC error during reading. The MGSTAT bits after the second write are 0b10 due to fact that just 1 bit is different during write verification (correctable error)   The second case simulates Single-bit ECC error during reading. The MGSTAT bits after the second write are 0b11 due to fact that more than 1 bits are different during write verification (non-correctable error)   The third case simulates Double bit ECC error during reading. The MGSTAT bits after the second write are 0b11 due to fact that more than 1 bits are different during write verification (non-correctable error)     The flash patterns are selected for highlighted described behavior and they don't have any real meaning.   The cumulative write is not allowed for normal operation!!! The code from this example should be used only for design testing - not in production!!!   Please, see the prm file. Address range 0xFF0000-0xFF01FF is excluded from default ROM and is used as user flash memory The size of sectors is 512 bytes = 64 phrases.   Note: The PFLASH_Program() function was updated - erase verification is skipped     I hope it helps you. Radek
View full article
Here is a SW example demonstrating measurement of the bandgap voltage featured on a microcontroller. The bandgap voltage is routed to an internal ATD channel. ATD converter measures the input voltage and stores it to a variable located in Flash memory. The SW example project is made in CodeWarrior v5.1 for MC9S12XEP100 device.
View full article
Attached zip file contains three Code Warrior project for MagniV devices (S12ZVM, S12ZVL and S12ZVC). Each of them demonstrates usage of High Voltage Input feature. Find detailed description in the header of main.c file of the CW project.
View full article
The example code shows how to invoke single or double bit RAM ECC error at S12Z devices.   Some basic overview about S12Z ECC codes may be found in thread AM/FLASH ECC Error handling .
View full article
Here is the example code demonstrating erase/write/read of internal EEPROM module integrated into MagniV devices such as S12ZVM, S12ZVC, S12ZVL etc.   Find detailed description in the project's main.c file.   The example project was created in CodeWarrior IDE v10.6.4 and tested on TRK-S12ZVL board.
View full article
Here is the example code demonstrating erase/write/read of internal Flash module integrated into MagniV devices such as S12ZVM, S12ZVC, S12ZVL etc.   Find detailed description in the project's main.c file.   The example project was created in CodeWarrior IDE v10.6.4 and tested on TRK-S12ZVL board.
View full article
A project presents control of the HD44780 driven display where it is assumed that RW pin of the LCD is permanently connected to “Write” level (GND). The SW contains mirror(s) of the LCD display as array of characters stored within RAM. Both read and write function can be used because writing is performed to the display and also to the array. Read functions are directed to the character array stored within RAM.
View full article
 *******************************************************************************  * File              main.c  * Owner             LaMa-TIC-RPR  * Version           1.1  * Date              Jun/17/2016  * Classification    General Business Information  * Brief             S12Z Frequency Measurement at PT0 (IOC0_0) by TIM module  *                   in input capture mode  *******************************************************************************  * Detailed Description:  * The code measures frequency at PT0  * It demonstrates  * - It measures frequency at PT0 by means of TIM module IOC0_0.  * - The measurement is sensitive on rising edges so duty cycle has no affect  *   to the measurement  * - Constants:  *           BUSCLOCK  *           MAX_OVERFLOWS  *           TIMER_PRESCALLER  *      must be defined  * - MAX_OVERFLOWS determines minimum measurable frequency.  *   It is max. allowed number of overflows. When overflows counter reaches  *   this value it the flag FREQUENCY_TOO_LOW is set. The message  *   PERIOD BETWEEN TWO EDGES IS TO WIDE is then written to a variable "str"  *   This value gives time interval:  *     T = TIMER_PRESCALER * MAX_OVERFLOWS * 65536 / BUSCLOCK      *     For example:  *      - TIMER_PRESCALER = 1  *      - MAX_OVERFLOWS   = 1000  *      - BUSCLOCK        = 16,000,000Hz  *      T = 1 * 1000 * 65536 / 16,000,000 = 4,096s  => f=0,244140625Hz  * - TIMER_PRESCALER = { 1,2,4,8,16,32,64,128} - smaller values give more  *   precise values but it generates timer overflow more frequently  * - the physical representation of the measured frequency is stored in the  *   string "str"  *  * - The measurement precision is df which can be expressed as:  *  *    df = BUSCLK / ((n^2 + n) * PRESCALER); n = measured number of TCNT periods  *               *   For example: BUSCLK = 16MHz, n = 399, PRESCALER = 1  *     *     *             df = 16,000,000 / ((399^2 + 399) * 1) = 100.25Hz  *               *             so, count 399 means f=BUSCL/399=16,000,000/399 = 40,100.25Hz  *             so, count 400 means f=BUSCL/400=16,000,000/399 = 40,000.00Hz  *                   * So this method is not suitable for large frequencies as can be seen in  * following table (percentual error we can get is df/f):  * n   f[Hz]      df[Hz]                        n   f[Hz] df[Hz]  * 1   8000000   5333333,3           101 1553,0 30,1  * 2   2666666,6 1333333,3           102 1522,9 29,2  * 3   1333333,3  533333,3           103 1493,6 28,4  * 4    800000    266666,6             104 1465,2 27,6  * 5    533333,3  152380,9            105 1437,5 26,8  * 6    380952,3   95238,0             106 1410,6 26,1  * 7    285714,2   63492,0             107 1384,5 25,4  * 8    222222,2   44444,4             108 1359,1 24,7  * 9    177777,7   32323,2             109 1334,4 24,0  * 10   145454,5   24242,4            110 1310,4 23,4  * 11   121212,1   18648,0            111 1287,0 22,7  * 12   102564,1   14652,0            112 1264,2 22,1  * 13    87912,0   11721,6             113 1242,0 21,6  * 14    76190,4    9523,8             114 1220,4 21,0  * 15    66666,6    7843,1             115 1199,40,502  * 16    58823,5    6535,9             116 1178,8 19,9  * 17    52287,5    5503,9             117 1158,9 19,4  * 18    46783,6    4678,3             118 1139,4 18,9  * 19    42105,2    4010,0             119 1120,4 18,5  * 20    38095,2    3463,2             120 1101,9 18,0  * 21    34632,0    3011,4             121 1083,8 17,6  * 22    31620,5    2635,0             122 1066,2 17,1  * 23    28985,5    2318,8             123 1049,0 16,7  * 24    26666,6    2051,2             124 1032,2 16,3  * 25    24615,3    1823,3             125 1015,8 15,9  *  * - PCB setup:  *   - J35 is disconnected because measurement is done on PT0  *   - pulse generator is connected to the J35 pin 1. (PT0)  *    * - Reference to documentation: MC9S12ZVLMR1.pdf Rev. 1.02  * - Tested on TRK-S12ZVL  * - MCU MC9S12ZVL32 0N22G  * - OSCCLK = 4MHz  * - BUSCLK = 16MHz (set by PLL)  *  * The info about frequency and count is transmitted over the SCI0 Tx which is  * routed (MODRR) to PS1 pin  *  *******************************************************************************  Revision History:  Version Date          Author            Description of Changes  1.0     Jun/17/2016   LaMa-TIC-RPR      Initial version  ******************************************************************************/
View full article
The ADC measurement is always relative – relative to your voltage reference. The most of the applications don't allow to use accurate and expensive voltage reference. In that case, VDDA is used as the reference for ADC measurement. Since the operational VSUP range starts from 3.5V and accuracy of the voltage regulator is limited, we should use internal bandgap reference for compensating ADC voltage results. The bandgap voltage VBG has a narrow variation over temperature and external voltage supply. The example shows how to compensate ADC results by additional bandgap voltage measurement.
View full article
In the ZIP file you can find two CodeWarrior projects, one for S12ZVM and other for S12ZVL device, demonstrating the trimming of Autonomous Clock (ACLK - trimmable internal RC-Oscillator) which can be selected as clock source for some CPMU features. Also the Autonomous Periodical Interrupt feature using the trimmed ACLK as a clock source is demonstrated too in this SW example.
View full article