FreeRTOS Mbed OS RTOS ThreadX

What is Real Time Operating System (RTOS) ?

What is Real Time Operating System(RTOS) ? Operating System must have heard this name earlier but only in relation to Windows, Linux, and MAC OS which are there in Laptops and Computer devices. Most people are aware of only these Operating Systems in general terms which are there in most the General Purpose Computers. But what if I told you that there is an Operating System in most of the electronic devices (Embedded devices) around us like Earphones, Setup Boxes, Cars, speakers, Air Conditioners, Smart Watches, Medical devices and etc? Operating System is basically a software stack that allows the Electronic/Electrical devices to mimic the sensing of multiple inputs, their processing and correspondingly controlling multiple outputs at an instantaneous time. All of these Embedded devices have multiple inputs that need to be sensed, then processed, and correspondingly control the outputs. All of this happens in a real-time and mimic to happen at an instantaneous time. These Embedded devices work in Real-time data on time-critical and small memory space constraints. To put it in a different perspective, imagine a manned spacecraft in low earth orbit traveling about 17,000 miles an hour. Suppose that every 1,000 seconds, it has to fire an orbital maneuvering system rocket for 3.8 seconds to avoid deorbit. A one-second unit of time translates to 25,000 feet of distance covered. Imagine what nightmare would ensue if the spacecraft avionics had to stop, wait for the navigation system to page back in, fire the rockets, and then wait for the next interruption to stop firing the rockets. It could take five seconds or more. In that time the spacecraft will deviate 25 miles from its projected course. You can take any example, let\’s say of speakers. Speakers are connected to our phones for playing music, the moment when we press a song to be played at just a fraction of sec that music gets start playing. But have you ever thought that what all process is behind this? There is Bluetooth signal that has to be send from our phone to speakers, then speakers process and compute the signal that it receives. Then according to signal, speaker base start vibrating to play the music. The connection between phone and speakers should be continuous, so that signal to play the music (lyrics basically) is not interrupted. If in between to this we need to lower the volume or change the BASS of music those signals also have to be processes, computed and executed. Same way out you can try to think the flow of working of electronic devices around you and realize that Operating System is everywhere not just in our desktops. Operating system used in Embedded devices is called Real Time Operating System (RTOS). RTOS is very important topic and concept to be understood to make complex electronics/electrical application projects which are time critical like nuclear monitoring devices, Anti-brake system, automotive ECU\’s, Programmable logic Controllers (PLC\’s), Automated guided vehicle\’s (AGV), Motor drivers which are there in metros/airplanes/vehicles, and many such examples (In some cases, instead of RTOS, Linux OS is also used. That is different topic to be touched).

FreeRTOS RTOS

Starting with RTOS: FreeRTOS

Table of Contents Intro From this blog, we will be starting with RTOS(FREERTOS). In RTOS there are many concepts & things related to kernel, tasks management, task scheduling, IPC, and many other terminologies. In the tutorial series, we are not going into a theoretical understanding of the RTOS topics, we would be mainly focusing on developing different applications in RTOS. Like how to use mutexes, and semaphores in different applications, we will get into the RTOS kernel and understand its different components like scheduler, task management and etc., compare different scheduling algorithms in terms of time and how to create multiple tasks and pass information between them, how to use Software timers, queues and etc. while writing firmware for industrial projects. As there are many resources & tutorials to understand these theoretical concepts and how to use RTOSes APIs to build different kinds of applications is not much there. So, we would be focusing on that thing. RTOS is used in applications like that AGV(Automated Guidance Vehicles), Automotive ECU, and industrial automation products (Like metering instruments, industrial smart sensors and etc.). I have a personal interest and passion for these, so I would be making projects using RTOS related to these by interfacing sensors and modules used in these applications. Ultimately making the different tasks run together to utilize the power of RTOS to make an efficient project. To Know about what RTOS is, refer to this blog What is Real Time Operating System (RTOS) | gettobyte Now to start with RTOS, there are many open source RTOSes available like FreeRTOS, embed OS, Zypher OS, ThreadX OS and etc. But starting with FreeRTOS would be my choice that too with STM32 because the integration of STM32CubeIDE with FREERTOS is pretty easy & gives an easy starting point for beginners. Subsequently, we will migrate to NXP controllers with FreeRTOS. Once getting hands-on with FreeRTOS via STM32. Requites: Bare metal coding On MCU’s (Register level programming, working of MCU’s, SoC/processor architecture, boot process: linker script, startup code, GNU GCC, Debugging and etc.). Knowledge of peripheral of UART, I2C, SPI, ADC, Interrupts, DMA and etcC/CPP programming, etc). Device driver development to integrate different sensors & Modules with MCU. (Datasheet reading, communication protocols, sensors and modules working/Cpp programming). Knowledge of C/CPP programming. Software Development Kit, IDE, HAL, RTD drivers and etc. FreeRTOS What is FreeRTOS? FreeRTOS is Real Time Operating System, which is highly used in industry. It is also best recommended for beginners to start with if one wants to learn RTOS, because of its wide number of resources available, documentation, and community support. FreeRTOS is a market-leading embedded System RTOS, which is available for many processor architectures, it supports more than 40 processor architectures including ARM cortex processors. Also portable to a wide number of compilers and hardware platforms. FreeRTOS is supported by a wide number of silicon vendor chips like NXP Semiconductors, STMicroelectronics, Texas Instruments, and many others. You can find a full list of that here: Microcontrollers and compiler toolchains supported by FreeRTOS. Features of RTOS Highly configurable kernel. FreeRTOS has Microkernel architecture. Pre-emptive or co-operative operation. Inter Processor Communication techniques like Mutex and Semaphore for synchronization. Support for message queues. Software Timers. Event groups. Memory management to maintain heap memory. Also, advanced features for debugging the tasks in Real Time. FreeRTOS Kernel The kernel is the most important thing and core of the RTOS. For the understanding you can have analogy that Human body is RTOS, and Human Brain is RTOS kernel. As without our brain we can’t do any work and loose our capabilities. Same without the kernel, RTOS can’t work. It is kernel which makes electronic devices, a multitasking system. It is kernelling whose responsibility is to run different tasks and have communication between them. Kernel act as the interface between system resources and user application. Kernel has different services for making multitasking system. Just like human brain has different biological parts to make us work. ( In the upcoming blogs and in this also, i would be using analogy of Human brain and Human body for understanding). The basic services of RTOS kernel are:  Task/Process Managment (Creation and deletion of tasks) Task/Process Scheduling (Scheduler) task/Process synchronization (Inter Process Communication) Memory Managment (heap & static) Interrupt Handling Error/Exception handling Coming to the FreeRTOS, it has highly configurable kernel. We can configure how to use the FreeRTOs kernel and which features/services of it. FreeRTOS is configured by FreeRTOSConfig.h header file. This file contains application specific definitions to configure FreeRTOS kernel. Every RTOS has one such configuration file which has macros, which can be enabled or disabled. Like we can configure which scheduling algorithm to use, how to create the tasks (static or dynamic), which IPCs to use for communication between tasks, enabling of run-time trace and debug features. When you will download the FreeRTOS kernel zip file, you will find many numbers of files. But core FreeRTOS is runned by just 2 files tasks.c and list.c. They are located in FreeRTOS/Source directory. In addition to these files, imp files whose Api’s we would be using are queue.c, timers.c, event_groups.c, croutines.c. Examples of RTOS Kernel: Mach QNX VXworks FreeRTOS Chibios ThreadX mbedOS FreeRTOS Libraries In addition to FreeRTOS kernel, there are add on libraries to extend the RTOS functionality. FreeRTOS+: To add support of TCP/IP stack and command line interpreter. FreeRTOS Core: To add support of cloud connectivity with HTTP, MQTT, JSON, Cellular interface. FreeRTOS AWS: To add support of AWS services. How To Start with RTOS? So, as of now, I have told you many things about RTOS and FreeRTOS. Most of you must have come up with that now how to start with FreeRTOS to learn RTOS. When we go to the FreeRTOS website for starting with RTOS, there are many things on the website, lots of documentation, articles, and resources on the website that might become overwhelming and confusing for beginners to start with it. At least for me, it does become like that, so I

Embedded Basics

Interview at Texas Instruments

I was in my final year of my graduation (Btech:EEE) and our last semester is getting over. It was 26th of May 2022, I was in my college lab with my friends and Hod teacher discussing our Farewell venue and date, at that moment I just checked my phone and I saw a notification on LinkedIn, got a LinkedIn HR mail from one of the Re recruiters of Texas Instruments. I got very excited and happy at that moment. I got so anxious at that  moment  that I immediately  told my  best friend and gf, after reading the Job Description. My resume got selected for 2 positions in Texas Instruments,  one is of Embedded Application Developer and other is of Field Application Engineer. For the position of Embedded Application Developer, I got the LinkedIn HR mail and from there my profile was selected. For the position of Field Application Engineer, I got a referral from the Alumni of my college, who is Field Application Manager at Texas Instruments. And this happens in a span of 1 week, at first my resume is selected for Embedded Application Developer. The very next day I talked to one of the teachers of my college regarding my selection and then she gave me the reference of Alumni of our college who have been at Texas Instruments for 10 years. On talking to the alumni, he told me that there are positions open in his team for Field Application Engineer. He likes my profile and resume both for that position and he referred to my profile for that position. Though I have called him with an intention to talk about an Embedded Application Developer position, he has referred me for one of the positions open in his team. So my interview process was going on for 2 positions parallel at Texas Instruments. Field Application Engineer Now at first let\’s  start with the interview process and experience of a Field Application Developer position, for which I ultimately get selected too and offered with a final offer letter of 25 LPA CTC. Field Application Engineer is a kind of technical sales job, in which the hired person has to provide technical assistance, development and strategies of Company products to customers. So the hired person should have knowledge related to company products. Should also be good in communication skills and interpersonal skills to make long lasting professional relations with company customers. So in this case, Texas Instruments is the company and its products are mainly concentric to Its Wide number of MCU, MPU, IoT modules and ASIC chips. So FAE should have good understanding and knowledge of microcontrollers, system level knowledge of embedded products, IoT protocols, use cases of different peripherals of the MCU. A person should have knowledge of different sub components that are used in the design of Embedded products, but is not necessary to have in depth knowledge as FAE would not be involved in development but much more acts as interface between company and the customer using the company products. 4 rounds were taken, out of which 3 are in online mode and one(last one) is offline mode. For the position of FAE, interview questions are different from what we all normally used to prepare ourselves. The questions are much more to test one\’s personality, communication skills, conceptual questions related to technology. My first round was for about 45 mins, in which the interviewer asked questions related to my journey so far, how I have learnt things on my own, why I chose to get into the technical field and some basic technical questions related to Embedded. Also about 15-20 mins of my first round went into discussion of the Field Application Engineer job role, in which the interviewer told me what exactly would be my roles and responsibilities. I expected that there would be coding questions or questions related to in depth hardware but nothing much like that. And i analyzed that the interviewer was concentric to my communication skills, how well can i address my self, how i well i can interact with people to 

Embedded Basics

Getting Started with Firmware Development

See around yourself, we all are surrounded by electronic gadgets around ourselves. The mouse which you are using to navigate through desktop, the Earphones via we all used to listen to music and show off our styles while traveling!!!, the doorbell we ring in homes and see who is outside through video, wireless car keys through which we open our cars, high volume speakers and decks we enjoy dancing on playing the music of our choice when we go to pubs/discos or parties, automation in cars which we see in the forms of lights, automatic systems, high graphics infotainment along with cameras and wireless systems, setup boxes, medical devices like oximeter, sugar, and blood pressure machines. The list will not stop but the size of this blog will get par. All the above examples which I have listed can be termed as Embedded products. Embedded Products are like a silent servants of the modern technological society, as most of the time people are not aware of it but they keep on doing their work Silently and efficiently. One thing in common in all types of Embedded products is the presence of Microcontrollers or Microprocessors. Microcontrollers or Microprocessors are like the brain of embedded products.  Microcontrollers or Microprocessors are digital systems as they only understand binary language that is 0 or 1’s. Now the thing that makes these microcontrollers and microprocessors different from the human brain is that we can program these digital systems. Here comes the Embedded Software/Firmware Development. What is Embedded Software/Firmware Development? Basically, the programming that is done into microcontrollers/microprocessors is termed Embedded Software Development. The term Embedded makes this different from traditional Software Development ( Web/App Development).By the term embedded, it means that their is s Embedded products have a constraint of memory, so programming language has to be there that is of less size and easily be converted to binary language of 0 & 1’s. Programming languages that are widely used: are C and CPP language. Microcontrollers/Microprocessors are physical hardware that can be felt, thus Embedded Software Developers do need some hardware knowledge( like that of Electronics and Computers).  Now the difference in Embedded Software and Embedded Firmware is not much. Both of these are used interchangeably in embedded domain. Embedded Firmware is used when we are developing programs very close to hardware like: Device driver development, Peripheral Interfacing, Sensors and Modules integration. Embedded Software is much more inclined towards Hardware Abstraction Layers(HAL), RTOSes, Wireless protocol stacks, Code portability on different platforms. Need of Embedded Firmware/Software Development. Embedded Software/Firmware development is niche R&D skill in modern world technology whose scope and demand will only increase with growing technologies. All modern world technologies which we see around ourselves:  AR-VR gadgets, Industrial PLC-HMI’s, Next Generation Automotive cars, Internet of Things, Aircrafts, Avionics and many more can only be build by the help of Embedded Software/Firmware Developers. Because it is role of Embedded Software Developers to interact different hardware subsystems interact with each other( through programming and coding) and made the Embedded product work. How to get Started with Embedded Firmware Development. I hope by now you have get bit of clarity and idea about Embedded Firmware Development and its need. Also the kind of industries one can work on after getting this skill. These are very important things to know before actually start learning any skill. As learning a skill is long process and one need good and strong motivation down the line to stay dedicated to learn that skill. Essential things to learn for Embedded Firmware Development. As if now I have talked much about the high end technologies and big flashy words. But now lets get down to basics and actually see how to learn this skill. Essential things to learn for Embedded Software/Firmware Development. C/CPP language–> This is de-facto and most important. As 95 percent of time programming language which is used by embedded developers would be C/CPP language. Both of the languages are more and less same apart from some concepts. Concept of pointers, bit shifting/bit manipulation, functions, types of data types, strings and arrays are the some concepts that should be gone through. Below are some of the books which one can refer for learning basics of C/CPP. Understanding and Using C Pointers: Core Techniques for Memory Management Object-Oriented Programming in Microsoft C++ Pointers on C by Kenneth Reek  Computer Architecture and Organization –> Okay so done with programming part, now hardware knowledge plays equally important role. As Embedded Software/Firmware developers will be programming on Microcontrollers (MCU)/Microprocessors (MPU), so knowledge of them is essential. What exactly is Microcontroller? How do they work? Different kind of architectures in MCU/MPU, difference between MPU&MCU, Boot Process in MPU/MCU, getting familiar with terminologies related to MPU/MCU world are some of the thoughts you can start learning about these. Below are some of the books which one can refer for learning about Computer Architecture and Organization. Computer Organization and Architecture: Designing for Performance Computer system architecture by morris mano One of the best ways to learn these languages is by reading already built in codes. 3. Basic Electronics and Electrical knowledge: Okay so this is the part because of which majority of people after starting initially with Embedded Development left it further in college days. As there is role of hardware also, so alongside with programming language having knowledge of electronics and electrical is required sometimes and can be beneficial. In Electronics knowledge of Resistors, Capacitors, Inductors, Basic Ohm’s law (their use cases in real life application: Current sense amplifiers), Opamps, ADC’s would be good as this would make you understand the Embedded product in better way. In Electrical knowledge of AC-DC voltages, power supplies, Power electronics, basic principles of electricity generation and etc is though not always necessary but would be good to have to make your interactive and interesting DIY projects. Below are the books which one can refer for learning about Basic Electronics and Electricals: Get started with Embedded Firmware Development with Gettobyte (To all

Motor Driver Sensors and Modules TIMER Modules

HLW8012 AC Energy metering IC: Part 2

Table of Contents In the last blog of HLW8012 energy metering IC, we have got an overview and understanding of HLW8012 IC. So now in continuation to that blog, in this blog, we are going to develop the Application driver for HLW8012. But before preceding with the Application Driver, we will see the hardware setup. At first, we are going to set up the hardware and connections that has to be made for reading the AC readings. Hardware Setup and connections To be honest this is one of the challenging parts which I faced when dealing with energy metering ICs. As being mainly into Firmware and software part, I was not well aware of the electrical and electronics-related terms, concepts and instruments. Though I am from the EEE branch interest since my graduation was more aligned with Firmware. But I also want to learn and understand the electronics and electrical part, in continuation to my Firmware skills. Thus, I thought of using the Energy Metering IC and make the Energy Meter project, as by that have to learn and understand the Electronics/electrical concepts. Energy Metering IC\’s senses the AC voltage and Current, so input to these IC\’s are AC electrical signals. In comparison to other sensors/modules like temperature, Flash memory\’s, gyroscope sensors, display screens etc., in which input is either our environment or values given by us. But in Energy metering or current sensor ICs, for input we need AC load which will be powered by AC power supply. In addition to these we need multimeters, connecting wires and cables for electrical measurement and connections. Hardware Setup AC load Their are 3 types of AC load: Resistive load, Capacitive load, and Inductive load.  There are electrical programmable AC loads, which are actually used in industry for testing and development of Energy Meters. These programmable AC loads can be configured for any type of load. But those are very expensive, to be afforded. So, I started looking for cheap alternatives for all 3 types of AC load. We are going to use resistive load, which will be an AC bulb filament type. AC Source For powering the AC load, we need an AC power supply that will power it. Now while developing the Energy meter and driver for Energy metering IC, I need a variable AC power supply so that I can check the different readings of AC electrical parameters across my AC load. This is one of the first challenges that I faced, which electrical instrument to use, to have a regulated AC power supply. Because AC-regulated power supplies are also quite expensive in comparison to DC-regulated power supplies, which are not affordable to students.  Then I came to know about the Autotransformers that regulate AC power. One can take it analogous to potentiometers, on rotating the knob of them we get variable resistance. Same way in Autotransformers, there is a knob that maps from 0-270V. By rotating the knob, we can get variable AC voltage. Also, the AC signal from Autotransformer is quite stabilized and accurate, so these work great for such small and hobbyist projects. Multimeters  Multimeters are electrical instruments that are used to measure electrical/electronics parameters. Multimeters can measure both AC/DC readings. In this project multimeters would be used for measurement of AC voltage and AC current across our load. 2 multimeters would be used. One is the generic multimeter, which would be connected across the Autotransformer output pins to measure the voltage. Another is the Clamp meter, which would be connected across one of the wires of our AC load to measure the AC Current. The readings which we will get from here would be used to cross verify and check the readings that would be decoded from HLW8012. HOST MCU In our case, I have used the STM32f103-Bluepill as HOST MCU. Logic Analyzer I will also be using the Logic analyzer, for debugging and seeing the PWM digital signals, that would be outputted by HLW8012. STLINKV2 Debugger STLINKV2 will be used as a debugger to program and flash the firmware into our HOST MCU. Hardware Connections Now coming to the connection parts, we can divide HLW8012 into 2 parts, one is the electronics connection that has to be connected with the HOST MCU. Another is an electrical connection that has to be made with AC load and AC supply. At first, we will interface HLW8012 with our host MCU( which in this case is STM32F103-Blue pill). Viewers can refer to this part to understand how HLW8012 and host MCU communicate with each other. HLW8012 has mainly 3 pins CF(Blue), CF1(Green), and SEL(Yellow) pins that have to be connected with HOST MCU. These pins can be connected to any GPIO pins, just have to make sure that those GPIO pins can be configured in TIMER INPUT CAPTURED MODE. The pins at which CF &CF1 pins will be connected are configured as INPUT and the pins at which the SEL pin would be connected are configured as OUTPUT. In my case I have connected : CF1 pin of hLW8012 –> PA8 CF pin of HLW8012 –> PB6 SEL pin of HLW8012 –> PB12 VCC pin of HLW8012 –> +5V pin of MCU GND pin of HLW8012 –> GND pin of MCU I have also connected the Logic analyzer for seeing the PWM signals digitally and the debugger STLINKv2 for flashing and debugging the code with my HOST MCU. Coming to the electrical connection part, the HLW8012 module has 2 couple of green connectors on it. One couple of connectors is connected to Power Supply, in our case power supply is coming from Autotransformer. So it will be connected to the Output terminals of Autotransformer. Generic Multimeter would be. Another couple of connector is connected to the AC load, in our case, the AC load is AC bulb Filament type. Clamp multimeter would be connected across one of the wire of this connection to measure AC current. That is all connection that has to be made and

Current/Power Sensors Sensors and Modules TIMER Modules

HLW8012 AC Energy metering IC: Part 1

Table of Contents So hello guys, welcome back to Gettobyte Once again. Todays blog is going to be on interfacing the Energy metering IC : HLW8012 which is manufactured by HLW Technology.On previous blogs we have developed the application driver   for W25Q SPI based flash memory’s, is time we are going to develop the Application driver for HLW8012. So first question that must come in your mind is that what is Energy Metering IC. Lets first Understand this term. What are Energy Metering Integrated Chips ? Before Understanding Energy metering IC’s their is one term that i would like to get familiar my viewers that is ASIC’s. ASIC’s are Application Specific Integrated Chips i.e. chips which are designed, developed and fabricated for particular applications.  Energy Metering IC’s are one of the ASIC’s. In this case the specific application is that of Electrical Energy Measurement. By the use and emergence of Energy Metering IC’s we can calculate the Electrical Energy parameters like RMS/Instantaneous Voltage, RMS/Instantaneous Current, Reactive/Active/Apparent Power, Energy Consumed, Power loss/dissipation, Power factor, Frequency digitally. Using this electrical Energy Measurements only our monthly electrical energy bill is calculated. These electrical energy parameters play very important role for monitoring and analyzing of Power consumption in our homes, offices, factories, city’s and all such infrastructures where their is use of electricity. These Ic’s are widely used in Energy Meters, Multifunction Meters, Smart Energy meters, Solar Energy control panels, Factory Automation Panels and etc. Energy metering IC’s are designed using the knowledge of ADC, Opamps, Low pass/High pass filters, use of Analog Electronics with Power Electronics, DSP(Digital Signal processing), Signal Conditioning circuits. All these circuits are designed inside the Energy Metering IC’s using VLSI/VHDL. That is where the concept of Application Specific comes in. So when reading the datasheet of Energy Metering IC’s some overview about these terms and terminologies would be beneficial for understanding the functioning of these IC’s. Voltage and current drive the world in every sense. Using these 2 parameters we can calculate all other electrical energy parameters be it reactive power, active power, energy, frequency, power factor and etc. Energy Metering IC’s primarily measures/senses only Voltage and Current by the use of Signal conditioning circuits, ADC, Opamps, Analog Electronics. Further these IC’s calculates all other Electrical energy parameters using DSP, Low pass/High pass filters and etc. All of these things are designed inside the Energy metering IC’s. Input voltage and current to these IC’s are fed depending upon the principal which we are using for sensing Voltage and current. After feeding the Input to these IC’s via one of the above principals, comes the role of above mentioned circuits/units which are designed inside the chips for their efficient functioning and digital accurate readings. Energy metering IC’s which are widely used are of The above listed metering IC’s and their development boards are very expensive, so for my use case i have found out some cheap/alternative Energy Metering IC’s, but their manufacturer are Chinese companies: STMicroelectronics Metering ICSTMP32/33/34 seriesClick HereTexas Instruments Metering ICsClick HereAnalog DevicesADE seriesClick HereNXP semiconductorsClick Here Previous Next HLW8012( the one we are using this blog) HLW8032 BLO937 PZEM-004T module  CS5460 bi-directional energy meter module This is the First string, Second string, Third string of the sentence. General Description of HLW8012 Energy Metering IC HLW8012 is a single phase energy metering IC, which is based on shunt resistor based principal. The HLW8012 module has on board 1 milli-ohm copper-manganese shunt resistor, which acts as a shunt resistor and used to sense the voltage and current. One of the biggest problem with IC, is its datasheet. The datasheet of this IC is in Chinese language, so likely it would be difficult to understand from it. The IC can measure the RMS Voltage, RMS Current, Active power and Outputs these reading digitally in the form of PWM signals. HLW8012 is a good choice for making small and hobbyist kind of projects which requires the measurement of AC electrical parameters. One such project/product that I found, is made on HLW8012. HLW8012 is inexpensive and good alternative to be used in comparison to  other expensive metering IC’s. The module of HLW8012 is very handy and easy to use. It has 4 input terminals: 2 terminals are connected with AC Load and other 2 terminals are connected with AC Power supply. HLW8012 Outputs the Readings of Voltage, Current and power via PWM signals of 50 percent duty cycle, Their are two pins in HLW8012 CF and CF1 which outputs the voltage, current and power readings in a digital PWM square signal, which host MCU can capture using Timer Input Capture mode. The readings are directly proportional to the Frequency of the PWM signal. Every modern MCU has Timer peripheral, which we can configure in Timer Input capture mode. In this mode Host MCU can capture the PWM signals from external world (here our external world is HLW8012). That’s how HLW8012 will communicate with the Host MCU. Features of HLW8012 Energy Metering IC Based on shunt resistor based principal. Can measure current upto 20Amps and Voltage upto 300 Volts Easy interface to Host MCU sends voltage, current and power readings digitally via PWM square signals. Needs +5 volt to operate the IC. Block Diagram of HLW8012 Metering IC PINOUT of the HLW8012 Energy metering IC Pin Number 1 Pin number 1 is VCC (Chip Select), which is used to operate the power the HLW8012 IC. Connect this pin to +5 Volts of the host MCU. Pin Number 2 & 3 Pin number 2 &3 are V1P/V1N, which are Current differential input pins. These pins are connected in parallel to the shunt resistor(See the application circuit below). This connection is already made on the Module of HLW8012. The maximum voltage that can be input to these pins are 43.75mV. Pin Number 4 Pin number 4 is V2P, which is Voltage differential input pin, this pin is connected to  input AC voltage via resistor network of 47k ohms Pin Number 5 Pin

Memory Modules Sensors and Modules SPI Modules

W25Q128JV SPI Flash Memory: Part3

Table of Contents So continuing with the blog series of, W25Q128 SPI based flash memory\’s , in the previous blogs W25Q128JV SPI Flash Memory: Part1 | gettobyte  W25Q128JV SPI Flash Memory: Part2 | gettobyte we have gone through the introduction and overview for W25Q128JV  flash memory\’s. From this blog we are going to start with the Application and Device driver development of W25Q128JV IC. The Driver which i am going to develop in this blog will be generic can be used with any MCU, by just replacing the SPI API\’s. This application driver will be generic and simple one which will be having API\’s to perform basic Operation on this chip. We will be creating the 2 files, header file and source file(.h &.c) for W25Q128JV Application driver. Header file(.h) will be having all the Macros, Typedefs, Enums, Structures  and function declarations. Source file(.c) will be having all the function definitions and local variables to be used in the driver. Header file (W25Q128JV.h) First thing that we are going to do is define the Object like Macro\’s for all the registers of W25Q128JV in the header file(.h) of W25Q128JV. Macros are widely used in Embedded Programming for referring the registers address with the acronym of the Register names, so that it is easy for developer/user to understand the code or using the API.  Like, above if we want to read the JEDECID of the chip, instead of writing 0x9F in the Application code we can pass the Macro JEDECID. (Though we are not going to use all the registers of W25Q128, as in this blog we are just going to make the driver for following features. The Application driver will be having API\’s for reading-writing the data, erasing the data, reading-writing of Status registers, reading JEDEC ID , chip erase and chip initialise.) /* * w25q128jv.h * * Created on: 15-Apr-2021 * Author: kunal */ #define WriteEnable 0x06 #define WriteDisable 0x04 #define Dummybyte 0xA5 #define ReadSR1 0x05 #define WriteSR1 0x01 #define ReadSR2 0x35 //0x35: 00110101 #define WriteSR2 0x31 #define ReadSR3 0x15 #define WriteSR3 0x11 #define Write_Enab_for_Vol_status_regist 0x50 #define ReadData 0x03 #define WriteData 0x02 #define ReadDataFast 0x0B #define JEDECID 0x9F #define UinqueID 0x4B #define SectErase4KB 0x20 #define SectErase32KB 0x52 #define SectErase64KB 0xD8 #define chiperase 0xC7 #define reset1 0x66 #define reset2 0x99 #define read_addr1 0x020000 #define read_addr2 0x030000 #define read_addr3 0x040000 #define BUSY_BIT 0x01 #define WRITE_ENABLE_LATCH 0x02 Next thing in Header file will be the function definitions that would be used for interacting with the W25Q128JV flash memory\’s. void W25_Reset (void); void WriteEnable_flash(); void W25_Read_Data(uint32_t addr, char block[], uint32_t sz); void W25_Write_Data(uint32_t addr, char block[], uint32_t sz); uint32_t W25_Read_ID(void); void W25_Ini(void); void erase_sector4KB(uint32_t addr); void erase_sector32KB(uint32_t addr); void erase_sector64KB(uint32_t addr); void chip_erase(); void Uinque_ID(uint8_t uinque[]); void WriteSR(uint8_t SR_address, uint8_t SR_data); uint8_t ReadSR(uint8_t SR_address); void WaitForWriteEnd(void); Apart from Object like Macro\’s and Function definition\’s their would be 2 additional function like Macro\’s. //For STM32 CUBEMX #define cs_set() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET) #define cs_reset() HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET) //For STM32 BareMetal #define cs_set() GPIOA->ODR |= GPIO_ODR_ODR4; #define cs_reset() GPIOA->ODR &= ~GPIO_ODR_ODR4; As we are going to interface the W25Q128JV via SPI peripheral to our MCU\’s, in which MCU would be the Master device and W25Q128JV would be slave device. And in SPI -> Chip Select/Chip Enable pin is used for selecting the slave. Thus these 2 Macro\’s would be used for selecting the slave before the SPI instructions are send ( by using the cs_set()) and then deselecting the slave after the SPI instructions( by using the cs_reset()). Source file(W25q128JV.c) This file would be having all the function declarations of the functions which are defined in (W25Q128JV.h). The 2 most important API\’s which will Send and Receive the SPI commands are: void SPI1_Send (uint8_t *dt, uint16_t cnt) { HAL_SPI_Transmit (&hspi1, dt, cnt, 5000); } void SPI1_Recv (uint8_t *dt, uint16_t cnt) { HAL_SPI_Receive (&hspi1, dt, cnt, 5000); } API\’s Explained for Device Driver of W25Q128JV: void SPI1_Send () This function is wrapper for transmitting the data via SPI. not be used directly in Application driver, but it will always be called by Other API\’s of the driver to send the command to W25Q via SPI. It has 2 parameters: 1) uint8_t *dt –>pointer to store the data that will be transmitted from the Host MCU to W25Q128JV. 2) uint16_t cnt –> Variable that will be storing the size of data that has to be transmitted from MCU to W25Q128JV. void SPI1_Send (uint8_t *dt, uint16_t cnt) { HAL_SPI_Transmit (&hspi1, dt, cnt, 5000); } void SPI1_Recv() This function is wrapper for receiving the data via SPI. This API is also not used directly by the Application Driver, but will be used by the other API\’s of the driver for receivng the data from W25Q via SPI. It also has 2 parameters: uint8_t *dt –> pointer to store the data that will be received from the W25Q128JV. uint16_t cnt –> variable that will be storing the size of data that has to be received. void SPI1_Recv (uint8_t *dt, uint16_t cnt) { HAL_SPI_Receive (&hspi1, dt, cnt, 5000); } void W25_Reset(): W25Q SPI flash Ic\’s come in small package and they have limited number of the pins. Thus W25Q provides the software reset instruction feature. User/Developer can reset the W25Q by sending the specified instructions to W25Q  via SPI. After reset the device will come to its default state and loose all volatile content. Enable reset – 0x66( reset 1 macro) and Reset – 0x99( reset 2 macro)are the instructions that has to be send for generating the software reset. These 2 instructions has to be send in sequence, as any other command after the Enable reset command( 0x66) apart from Reset(0x99) will disable the reset procedure. Once the reset command is accepted it woulfd take approx 30us to reset the W25Q IC. void W25_Reset (void) { cs_reset(); tx_buf[0] = reset1; tx_buf[1] = reset2; SPI1_Send(tx_buf, 2); cs_set(); } void WriteEnable_flash(): In W25Q, before writing to any Page, Erasing any sector/block or performing full chip erase. We have to send the Write enable Instruction via SPI.

Embedded Basics

What are Device driver, Application Codes and HAL in Embedded Softwares

What are Peripherals in MCU? What is peripheral driver in MCU? What is sensor/module interfacing in MCU. What is device driver and RTD? What is HAL/BSP? What is Application Software? When people start with Embedded Software development or Firmware development, there are couple of terms which they come across and found them very confusing to understand. Terms which i am targeting in this blog are like Device driver/low level drivers, Application codes, and HAL (Hardware Abstraction layer). To understand these terms, one has to understand that in Embedded systems there are hardware and Embedded Software is a software (take it as a programming) that is done to interact with different hardware devices. Now these hardware devices can be categorized broadly into input devices, processing devices and output devices. Input devices can be out and main These hardware devices would be like can be a complex software and s Device Driver –> First let’s breaks down the are the drivers that interface the 2 hardware’s. Real Time Drivers SDK HAL Application Codes BSP Abstraction layers

Memory Modules Sensors and Modules SPI Modules

W25Q128JV SPI Flash Memory: Part2

Table of Contents So guys this is the continued blog on my interfacing W25Q128JV SPI flash memory with STM32 and AVR MCU. So in the last blog we have started with W25Q128 overview, its features and pin descriptions. Now in this blog we are going to talk about following topic: SPI standard instructions. Status and Configuration Registers of W25Q128JV SPI Serial Flash memory. Write protection features. About its block diagram of memory mapping & management.  and then going to understand Status and Configuration Registers. Then in next blog we are going to start with its driver implementation on STM32 and AVR MCU. SPI Standard Instructions So as I have mentioned that module which we are going to use has standard SPI pins only on the breakout module( one can buy this module from robu). Remember the pinout of W25Q128 from last blog??? If not kindly refer to that W25Q128JV SPI Flash Memory: Part1 once before going further in this topic. W25Q128JV IO2 and IO1 pins are not available in the modules which we are going to use and buy. We can operate the SPI at mode 0(0,0) or mode 3(1,1),  that is SPI CPHA or CPOL bits would be either 0,0 or 1,1. W25Q128JV would be used as a slave and host MCU would be used as a master. In standard SPI we can run this IC at frequency of 133MHZ for read-write operations. But in our sample codes i would be using the IC at 1 MHZ. Most Significant bit(MSB) is sent first during the SPI communication. Chip select pin(CS) would be used for selecting the slave. When CS is set as low, the slave is selected and when CS is set as HIGH, the slave would not be selected. Serial Data Input( DI) is the MOSI pin and Serial Data Output(DO) is the MISO pin. Serial Clock Input (CLK) pin is used as Serial Clock for SPI communication. During configuring of SPI pins for our host MCU, CS pin of the host MCU would be configured as Output pin. MOSI pin of the host MCU would be configured as OUTPUT pin. SCLK pin of the host MCU would be configured as INPUT pin. MISO pin of the host MCU would be configured as INPUT pin. Dual and Quad SPI are not of our concern, so we are not going to dig deep into those in this blog. though we are going to discuss standard SPI only if anybody has any things to ask related to Dual and Quad SPI they can reach out to me via gettobyte community. Status and Configuration Registers These are very important registers, plays an important role in configuring and using these memory chips. Their are 3 Status registers, SR1,SR2,SR3. Status register provide the status on the availability of the flash memory array, whether the device is write enabled or disabled, the state of the write protection, QUAD SPI settings, Security register lock status, and Erase/Program suspend status, output driver strength, and power up status. Also, status registers are used to configure the device write protection failures, QUAD SPI settings, Security register OTP locks and output driver strength. Each Status register can be read and write by specific commands. For reading the status register we have to issue the Read status register instructions for reading the corresponding Status register. One can read the Status registers of the memory chip when we want to know the status on the availability of the flash memory array, whether the device is write enabled or disabled, the state of the write protection, QUAD SPI settings, Security register lock status, Erase/Program suspend status, output driver strength, and power up status. For writing the status register we have to issue the write status register instructions for the corresponding Status register. One can write on the status registers when we want to configure the chip for the device write protection failures, QUAD SPI settings, Security register OTP locks and output driver strength. Status Register 1 S0: BUSY bit –> BUSY is a read-only bit in the status register (S0) that is set to a 1 state when the device is executing a Page Program(02h), Quad Page Program(32h), Sector Erase(20h), Block Erase(52h), Chip Erase(60h), Write Status Register(01h,31h,11h) or Erase/Program Security Register instruction(44h/42h). During this time the device will ignore further instructions except for the Read Status Register. So in short we can use this bit inside the while loop or if loop to check whether the device is ready for further instructions or not. erase_sector4KB(read_addr1); // device is executing a erase sector instruction if((ReadSR(ReadSR1) & BUSY_BIT) == 0x01) // Busy bit is set when erase sector instruction is send, so checking that { erase_sector4KB(read_addr1); } S1:WEL –> Write enable latch is also a read-only bit that is set to 1 after executing a Write enable instruction and making the chip in write enabled mode. Prior to every Page Program(02h), Quad Page Program(32h), Sector Erase(20h), Block Erase(52h), Chip Erase(60h), Write Status Register(01h,31h,11h) or Erase/Program Security Register instruction(44h/42h) we have to send the Write enable instruction. So after sending the write enable instruction we can read this bit to check whether Write enable Instruction is executed or not.  It is cleared to 0 when the device is written disabled. Write disabled state occurs after the Page Program(02h), Quad Page Program(32h), Sector Erase(20h), Block Erase(52h), Chip Erase(60h), Write Status Register(01h,31h,11h) or Erase/Program Security Register instruction(44h/42h). So in short we can say that the WEL bit is used to check whether Write enable Instruction is executed or not. S2-S3-S4: Block Protect Bits(BP2, BP1, BP0) –> are read/write bits that can be used to protect the memory array from Program or erase instructions. One can protect ALL, NONE, or PORTION of the memory, corresponding configurations can be done for BP2, BP1, and BP0 bits according to the below tables. Also, see the TB and SEC bits for Write protection configurations S5: Top/Bottom BLOCK Protect(TB)–> This bit controls whether the memory protection has to be performed from TOP

Stay Updated With Us

Error: Contact form not found.