Beginning STM32 - Developing with FreeRTOS, libopencm3 and GCC

von: Warren Gay

Apress, 2018

ISBN: 9781484236246 , 418 Seiten

Format: PDF, Online Lesen

Kopierschutz: Wasserzeichen

Mac OSX,Windows PC für alle DRM-fähigen eReader Apple iPad, Android Tablet PC's Online-Lesen für: Mac OSX,Linux,Windows PC

Preis: 56,99 EUR

eBook anfordern eBook anfordern

Mehr zum Inhalt

Beginning STM32 - Developing with FreeRTOS, libopencm3 and GCC


 

Table of Contents

5

About the Author

18

About the Technical Reviewer

19

Chapter 1: Introduction

20

STM32F103C8T6

21

FreeRTOS

24

libopencm3

24

No Arduino

25

No IDE

25

Development Framework

26

Assumptions About You

26

What You Need

27

ST-Link V2 Programming Unit

27

Breadboard

28

DuPont (Jumper) Wires

29

0.1 uF Bypass Capacitors

30

USB TTL Serial Adapter

31

Power Supply

33

Small Stuff

34

Summary

35

Chapter 2: Software Setup

36

Directory Conventions Used

36

Operating Software

36

Book Software

37

libopencm3

37

FreeRTOS

38

38

38

ARM Cross Compiler

39

Build the Software

42

ST-Link Tool

43

Summary

44

Chapter 3: Power Up and Blink

45

Power

45

+3.3V Regulator

47

USB Power/+5V

48

+3.3V Supply

48

One Power Source Rule

49

Ground

50

Reset

50

Showtime

50

ST-Link V2

52

st-flash Utility

54

Read STM32

54

Write Image

55

Erase Flash

56

Summary

56

Bibliography

56

Chapter 4: GPIO

57

Building miniblink

57

Flashing miniblink

58

miniblink.c Source Code

59

GPIO API

62

GPIO Configuration

64

Input Ports

66

Output Ports

67

Ducks in a Row

69

GPIO Inputs

69

Digital Output, Push/Pull

70

Digital Output, Open Drain

71

GPIO Characteristics

71

Input Voltage Thresholds

73

Output-Voltage Thresholds

73

Programmed Delays

74

The Problem with Programmed Delay

75

Summary

76

Chapter 5: FreeRTOS

77

FreeRTOS Facilities

77

Tasking

78

Message Queues

78

Semaphores and Mutexes

79

Timers

79

Event Groups

80

The blinky2 Program

80

Build and Test blinky2

84

Execution

84

FreeRTOSConfig.h

85

FreeRTOS Naming Convention

87

FreeRTOS Macros

88

Summary

89

Chapter 6: USART

91

USART/UART Peripheral

91

Asynchronous Data

92

USB Serial Adapters

92

Hookup

94

Project uart

95

Project

99

Project uart2

103

USART API

108

Include Files

110

Clocks

110

Configuration

110

DMA

111

Interrupts

111

Input/Output/Status

111

Ducks-in-a-Row

111

FreeRTOS

112

Tasks

112

Queues

113

Summary

114

Chapter 7: USB Serial

115

Blue Pill USB Issue

115

Introduction to USB

117

Pipes and Endpoints

117

USB Serial Device

119

Linux USB Serial Device

119

MacOS USB Serial Device

120

Windows USB Serial Device

121

USB GPIO

121

Demo Source Code

122

cdcacm_set_config()

123

cdc_control_request()

124

cdcacm_data_rx_cb()

125

USB Task

126

USB Receiving

128

USB Sending

128

USB Serial Demo

129

Summary

131

Bibliography

132

Chapter 8: SPI Flash

133

Introducing W25QXX

133

Serial Peripheral Interface Bus

133

Chip Select

135

Wiring and Voltages

135

SPI Circuit

136

Hardware /NSS Control

136

STM32 SPI Configuration

138

SPI Clock Rate

142

SPI Clock Modes

143

Endianess and Word Length

145

SPI I/O

146

Read SR1

146

Waiting for Ready

147

Read Manufacturer ID

148

Writing Flash

149

Flash Erase

151

Reading Flash

154

Demonstration

155

Running the Demo

157

Manufacturer ID

162

Power Down

162

Summary

163

Bibliography

163

Chapter 9: Code Overlays

164

The Linker Challenge

164

MEMORY Section

166

Entry

168

Sections

168

PROVIDE

171

Relocation

171

Defining Overlays

172

Overlay Code

174

Overlay Stubs

176

Overlay Manager

176

VMA and Load Addresses

177

Linker Symbols in Code

178

Overlay Manager Function

179

Overlay Stubs

181

Demonstration

182

Extracting Overlays

183

Upload Overlays to W25Q32

184

Overlay Demo Continued

188

Code Change Trap

190

Summary

190

Bibliography

191

Chapter 10: Real-Time Clock (RTC)

192

Demonstration Projects

192

RTC Using One Interrupt

192

RTC Configuration

193

RTC Clock Source

193

Prescaler

194

Starting Counter Value

194

RTC Flags

194

Interrupt and Setup

195

Interrupt Service Routine

196

Servicing Interrupts

198

Task Notification

198

Mutexes

200

Demonstration

201

UART1 Connections

204

Running the Demo

205

rtc_alarm_isr()

207

EXTI Controller

207

Configuring EXT17

208

Summary

209

Chapter 11: I2C

211

The I2C Bus

211

Master and Slave

212

Start and Stop

212

Data Bits

213

I2C Address

214

I2C Transactions

215

PCF8574 GPIO Extender

216

I2C Circuit

218

The PCF8574 Line

219

PCF8574 Configuration

219

PCF8574 GPIO Drive

221

Wave Shaping

222

Demo Circuit

222

EXTI Interrupt

223

I2C Software

225

Testing I2C Ready

226

Start I2C

227

I2C Write

229

I2C Read

229

I2C Restart

230

Demo Program

231

Demo Session

234

Summary

236

Chapter 12: OLED

238

OLED Display

238

Configuration

239

Display Connections

241

Display Features

241

Demo Schematic

242

AFIO

243

Graphics

245

The Pixmap

247

Pixmap Writing

248

The Meter Software

249

meter_init()

249

meter_set_value()

250

meter_redraw()

250

meter_update()

250

Main Module

251

oled_command()

251

oled_data()

252

oled_reset()

252

oled_init()

252

Demonstration

253

Summary

255

Chapter 13: OLED Using DMA

256

Challenges

256

Circuit

257

DMA Operation

257

DMA Execution

257

DMA Request Signals

258

SPI1_TX Request

262

The Demonstration

262

Initializing DMA

264

Launching DMA

265

OLED SPI/DMA Management Task

266

DMA ISR Routine

269

Restarting DMA Transfers

270

Executing the Demo

271

Further Challenges

273

Summary

274

Chapter 14: Analog-to-Digital Conversion

275

STM32F103C8T6 Resources

275

Demonstration

276

Analog Inputs PA0 and PA1

277

ADC Peripheral Configuration

277

ADC Prescaler

278

ADC Modes

278

Sample Time

278

Readying the ADC

280

Demonstration Run

280

Reading ADC

281

Computing Temperature

282

Voltage Reference

284

Analog Voltages

284

Summary

285

Bibliography

286

Chapter 15: Clock Tree

287

In the Beginning

287

RC Oscillators

288

Crystal Oscillators

289

Oscillator Power

290

Real-time Clock

290

Watchdog Clock

290

System Clock (SYSCLK)

291

SYSCLK and USB

293

AHB Bus

294

rcc_clock_setup_in_hse_8mhz_out_72mhz()

295

APB1 Peripherals

299

APB2 Peripherals

299

Timers

299

rcc_set_mco()

300

HSI Demo

300

HSE Demo

302

PLL ÷ 2 Demo

303

Summary

304

Bibliography

304

Chapter 16: PWM with Timer 2

306

PWM Signals

306

Timer 2

307

PWM Loop

311

Calculating Timer Prescale

311

30 Hz Cycle

312

Servo Hookup

313

Running the Demo

314

PWM on PB3

314

Other Timers

315

More PWM Channels

316

Summary

317

Bibliography

317

Chapter 17: PWM Input with Timer 4

318

The Servo Signal

318

Signal Voltage

319

Demo Project

319

GPIO Configuration

319

Timer 4 Configuration

319

Task1 Loop

321

ISR Routine

322

Demonstration Run

323

Session Output

325

Timer Inputs

326

Summary

328

Chapter 18: CAN Bus

329

The CAN Bus

329

Differential Signals

331

Dominant/Recessive

332

Bus Arbitration

333

Synchronization

334

Message Format

335

STM32 Limitation

336

Demonstration

337

Software Build

337

UART Interface

338

MCU Flashing

338

Demo Bus

339

Session Run

340

CAN Messages

342

Synchronicity

343

Summary

343

Bibliography

343

Chapter 19: CAN Bus Software

344

Initialization

344

can_init()

347

CAN Receive Filters

349

CAN Receive Interrupts

350

Application Receiving

354

Processing the Message

354

Sending CAN Messages

356

Summary

357

Chapter 20: New Projects

358

Project Creation

358

Makefile

359

Macro BINARY

360

Macro SRCFILES

360

Macro LDSCRIPT

361

Macro DEPS

361

Macro CLOBBER

361

Included Makefiles

362

Header Dependencies

362

Compile Options

362

Flashing 128k

363

FreeRTOS

364

rtos/opencm3.c

364

rtos/heap_4.c

365

Required Modules

365

FreeRTOSConfig.h

366

User Libraries

368

Rookie Mistakes

369

Summary

369

Bibliography

369

Chapter 21: Troubleshooting

371

Gnu GDB

371

GDB Server

371

Remote GDB

373

GDB Text User Interface

376

Peripheral GPIO Trouble

377

Alternate Function Fail

378

Peripheral Fail

379

ISR FreeRTOS Crash

379

Stack Overflow

380

Estimating Stack Size

381

When a Debugger Doesn’t Help

381

Push/Pull or Open Drain

382

Peripheral Defects

382

Resources

382

libopencm3

383

FreeRTOS Task Priorities

385

Scheduling Within libopencm3

386

Summary

387

AppendixA: Answers to Exercises

388

Chapter 4

388

Chapter 5

390

Chapter 6

391

Chapter 7

392

Chapter 8

393

Chapter 9

394

Chapter 10

395

Chapter 11

395

Chapter 12

396

Chapter 13

397

Chapter 14

398

Chapter 15

399

Chapter 16

399

Chapter 17

400

Chapter 19

400

Chapter 20

401

Appendix B: STM32F103C8T6 GPIO Pins

402

Index

410