# STM32 Experiments ###### tags: `tutorial` `electrical_system` `NTURT` `CAN bus` ##### Author: @solarLiterature ## From Blank to Blink Like many other skills, you can't say you understand STM32 usage without actually doing it. For the first experiment, we will walk through the hello world of microcontrollers, the Blink to get a sense of how to use the STM32CubeIDE. After that, you can try to go further and follow the other tutorials below to learn how to implement interrupts and CAN. We will conclude the tutorial here with a topic about GPIO, EXTI, and CAN, but there are still a lot more functionalities to cover so a bit of preview would help a lot. Besides the ones listed here, there are many other great tutorials out on the internet that each come with they're own ways of explanations and emphasis, so it helps to look for multiple tutorials on the same topics.(Check out the links at the bottom!) [STM32 Guide #2: Registers + HAL (Blink example) - Mitch Davis](https://www.youtube.com/watch?v=Hffw-m9fuxc) [Blink補充](https://www.youtube.com/watch?v=BJlNjMNNXgI) [Blink補充 slides pdf](https://cool.ntu.edu.tw/courses/7119/files/2521795?module_item_id=639346) Note: You will be given a configuration window the first time you initiate the debug tool in a project, but for this time we won't be altering any settings. So, leaving it as default should be ok. ### open-drain and push-pull [open drain / push pull](https://blog.csdn.net/chenpuo/article/details/105876739) Note: We usally wouldn't drive anything that requires high current with an open drain output. If we want to drive with open drain, the pullup resistor would have to be small, and if the pullup resistor is small, it will consume a lot of power when the transistor closes. So, a more reasonable choice is to use push-pull to supply current, and only use open drain with high impedance loads. ## EXTI & interrupts interrupts are essential when we are dealing with anything peripheral related since constantly spending CPU resources polling statuses is really wasteful. EXTI is the simplest of the interrupts as it is simply triggered by a digital voltage swap on a pin. [EXTI外部中斷/事件控制器 - pithreeone](https://ithelp.ithome.com.tw/articles/10273026) [NVIC中斷概要 - pithreeone](https://ithelp.ithome.com.tw/articles/10272548) [Getting started with EXTI - wiki by ST](https://wiki.st.com/stm32mcu/wiki/Getting_started_with_EXTI) [STM32-4 NVIC 外部中斷EXTI - TsaiiiY](https://ithelp.ithome.com.tw/articles/10284265?sc=rss.qu) Note1: there is no user buttons on the blue pill, but we can simply use a line that connects and disconnects from pins. Note2: don't forget to check if you need a pullup or pulldown with EXTI inputs. ## CAN Normally, experimenting with CAN requires at least 2 devices, but STM32 devices provide a method in the CAN peripheral to loopback the CAN signal so you can test CAN out by merging the tranmitter and reciever into one device. Tutorial: [STM32 CAN loopback mode - Controllers Tech](https://www.youtube.com/watch?v=JfWlIY0zAIc&t=1424s) Note: although it is said that you need to wire up the tranciever in the tutorial, the code can work just fine without any additional hardware. For calculating the baud in CAN : [CAN Bit Timing - Kvasar](https://www.kvaser.com/about-can/the-can-protocol/can-bit-timing/) ## STM32 Topic :::success Please document your code using doxygen. ::: :::success Please push your homework onto your github repository in the `stm32` directory. ::: Let's say you have a wire that connects Vcc to one of your GPIO pins, and the wire is extremely critical to your system and its disconnection can lead to the destruction of the Milky Way. ### Requirements 1. Set up a tranmitter that sends a one byte CAN frame containing the number 0 with extended ID 0x1800EEF0 every one second. 2. When the wire disconnects, immediately send a CAN frame with the same ID containg the number 1. 3. Set up a receiver of the frame that filters out everything but the frame described above. 4. The Receiver should toggle the LED everytime it recives a data 1 frame, but leaves the LED lit indefinitely when it receives a data 0 frame. Use CAN loopback mode to make the transmit/receive pair. Note: ExtId works similar to StdId, find how to tweak the settings yourself! ## Common Pitfalls A lot of things can go wrong when experimenting with STM32, and most of the time a thing will go wrong if it can go wrong. Sadly, the symptoms of a problem usually barely tell us which little detail we have missed when setting up a project, and it can be time consuming and painful to find the source of such problems. Below is a short list of a few of such problems that have come up to me before so you don't have to spend days debugging something that can be solved in ten seconds. If you have encountered any problems, try inspecting the symptoms, looking it up below, googling it, or asking about it in the billboard.:heart: 1. EVERY folder in your directory tree have to have English names, otherwise the compiler might freak out. 2. remember to enable your debug peripheral in the CubeMX, otherwise you will not be able to debug or flash any programs when the device is booted from flash. 3. jumpers on the blue pill board determine which memory is chosen when booting up, so make sure it is correctly placed when doing anything. ## Further readings STM32 comes with a lot of hardware functionality that we can leverage, though most of them require additional hardware to experiment properly. Here are some more things that is required when using STM32 in NTUR for those interested. 1. UART (you might need a USB to COM converter) 2. ADC (Analog to Digital Converter) 3. DMA (commonly used with ADC) 4. timers 5. I^2C 6. SPI ### Nice Tutorials The above tutorials are found from these great sources, and they all contain many more great content. It is as worthwhile to check out how different people approach the same topic(say, GPIO) as it is to find more functionalities of STM32. [STM32 基礎入門教學系列 - pithreeone](https://ithelp.ithome.com.tw/users/20141525/ironman/4839) Extensive STM32 content: [Controllers Tech](https://www.youtube.com/c/ControllersTech) Bare metal developement on Linux: [LowLevelLearning](https://www.youtube.com/c/LowLevelLearning) Clear tutorial for beginners: [Mitch Davis](https://www.youtube.com/c/MitchDavis2) STM32 with CubeIDE & FreeRTOS: [Getting Started with STM32 and Nucleo - Digikey](https://www.youtube.com/watch?v=hyZS2p1tW-g&list=PLEBQazB0HUyRYuzfi4clXsKUSgorErmBv)