MCCI Trusted Bootloader
Simple trusted bootloader and tools for small embedded systems
mccibootloaderboard_catenaabz_spi.c
Go to the documentation of this file.
1/*
2
3Module: mccibootloaderboard_catenaabz_spi.c
4
5Function:
6 SPI driver for Catena boards based on Murata ABZ and STM32L0.
7
8Copyright and License:
9 This file copyright (C) 2021 by
10
11 MCCI Corporation
12 3520 Krums Corners Road
13 Ithaca, NY 14850
14
15 See accompanying LICENSE file for copyright and license information.
16
17Author:
18 Terry Moore, MCCI Corporation March 2021
19
20*/
21
23#include "mcci_stm32l0xx.h"
24#include <stdbool.h>
25#include <stdint.h>
26#include <stdlib.h>
27
28/****************************************************************************\
29|
30| Manifest constants & typedefs.
31|
32\****************************************************************************/
33
34
35
36/****************************************************************************\
37|
38| Read-only data.
39|
40\****************************************************************************/
41
42
43
44/****************************************************************************\
45|
46| Variables.
47|
48\****************************************************************************/
49
50
51/*
52
53Name: McciBootloaderBoard_CatenaAbz_spiInit()
54
55Function:
56 Initialize SPI subsystem for bootloader for Catenas based on ABZ module.
57
58Definition:
59 McciBootloaderPlatform_SpiInitFn_t McciBootloaderBoard_CatenaAbz_spiInit;
60
61 void McciBootloaderBoard_CatenaAbz_spiInit(
62 void
63 );
64
65Description:
66 SPI2 is initialized. We assume the following:
67
68 SPI connections:
69 PB12: nss AF0
70 PB13: sclk AF0 pulldown.
71 PB14: miso AF0
72 PB15: mosi AF0
73
74 Clock: 16 MHz (SYSCLK/2).
75 MSB first.
76 clock polarity 0, phase 0
77
78Returns:
79 No explicit result.
80
81Notes:
82
83
84*/
85
86
87void
89 {
90 // set up the storage connections
101 );
102
113 );
114
125 );
126
127 // enable SPI2 at APB1
131 );
132
133 // reset the SPI2 block
137 );
141 );
142
143 // setup SPI_CR1
144 // since we reset above, we don't have to worry about anything
145 // except non-default settings
148 (
149 MCCI_STM32L0_SPI_CR1_BR_2 | // fastest
150 MCCI_STM32L0_SPI_CR1_MSTR // master mode
151 )
152 );
153
154 // setup SPI_CR2
155 // since we reset above, we don't have to worry about anything
156 // except non-default settings
159 MCCI_STM32L0_SPI_CR2_SSOE /* enable the nss output */
160 );
161
162 // setup I2SCFGR_I2SMOD
163 // since we reset above, we don't need to do anything.
164 // McciArm_putRegClear(
165 // MCCI_STM32L0_REG_SPI2 + MCCI_STM32L0_SPI_I2SCFGR,
166 // MCCI_STM32L0_SPI_I2SCFGR_I2SMOD
167 // );
168 }
169
170/*
171
172Name: McciBootloaderBoard_CatenaAbz_spiTransfer()
173
174Function:
175 Implementation of spi_transfer method for CatenaABZ family.
176
177Definition:
178 typedef McciBootloaderPlatform_SpiTransferFn_t
179 McciBootloaderBoard_CatenaAbz_spiTransfer;
180
181 void McciBootloaderBoard_CatenaAbz_spiTransfer(
182 uint8_t *pRx,
183 const uint8_t *pTx,
184 size_t nBytes,
185 bool fContinue
186 );
187
188Description:
189 This API writes and reads data from the SPI bus, optionally leaving the
190 target device selected.
191
192 nBytes bytes of data are transferred from the buffer at pTx to the SPI
193 bus, and received from the SPI bus to pRx.
194
195 The API defines pRx and pTx as optional; if NULL, bytes are discarded
196 or zeroes inserted, respectively.
197
198Returns:
199 No explicit result.
200
201Notes:
202
203
204*/
205
206// do a transfer
207void
209 uint8_t *pRx,
210 const uint8_t *pTx,
211 size_t nBytes,
212 bool fContinue
213 )
214 {
215 uint8_t txdata;
216
220 );
221 txdata = 0;
222 for (; nBytes > 0; --nBytes)
223 {
225 ;
226
227 if (pTx)
228 txdata = *pTx++;
229
231
233 ;
234
236 if (pRx)
237 *pRx++ = (uint8_t)rxData;
238 }
239
240 if (! fContinue)
241 {
245 );
246 }
247 }
248
249/**** end of mccibootloaderboard_catenaabz_spi.c ****/
static uint32_t McciArm_putRegOr(uint32_t reg, uint32_t orVal)
or 32-bit values to a cm0plus register
static uint32_t McciArm_getReg(uint32_t reg)
read a 32-bit value from a cm0plus register
static uint32_t McciArm_putReg(uint32_t reg, uint32_t val)
write a 32-bit value to a cm0plus register
static uint32_t McciArm_putRegClear(uint32_t reg, uint32_t clearVal)
clear out 32-bit values to a cm0plus register
static uint32_t McciArm_putRegMasked(uint32_t reg, uint32_t maskVal, uint32_t modVal)
store to cm0plus register under mask
#define MCCI_BOOTLOADER_FIELD_SET_VALUE(fmask, val)
McciBootloaderPlatform_SpiTransferFn_t McciBootloaderBoard_CatenaAbz_spiTransfer
#define MCCI_STM32L0_REG_RCC_APB1RSTR_SPI2RST
#define MCCI_STM32L0_GPIO_OSPEED_HIGH
#define MCCI_STM32L0_REG_SPI2
Section 30.7.10: SPI register map (1K)
#define MCCI_STM32L0_GPIO_PUPD_P(p)
compute the mask for the mode bits for port bits 0..15
#define MCCI_STM32L0_REG_GPIOB
Section 9.4.12: GPIO register map (1K)
#define MCCI_STM32L0_SPI_CR1_BR_2
PCLK/2.
#define MCCI_STM32L0_GPIO_PUPD_NONE
#define MCCI_STM32L0_SPI_CR1
offset to SPI control register 1
#define MCCI_STM32L0_GPIO_MODER
#define MCCI_STM32L0_SPI_SR
offset to SPI status register
#define MCCI_STM32L0_SPI_SR_RXNE
RX buffer not-empty.
#define MCCI_STM32L0_GPIO_OSPEEDR
#define MCCI_STM32L0_GPIO_OSPEED_P(p)
compute the mask for the mode bits for port bits 0..15
#define MCCI_STM32L0_GPIO_MODE_AF
#define MCCI_STM32L0_REG_RCC_APB1RSTR
APB1 peripheral reset.
#define MCCI_STM32L0_SPI_CR1_MSTR
Master (not slave)
#define MCCI_STM32L0_SPI_DR
offset to SPI data register
#define MCCI_STM32L0_SPI_CR2_SSOE
enable SS in master mode
#define MCCI_STM32L0_GPIO_PUPD_PULLDOWN
#define MCCI_STM32L0_SPI_SR_TXE
TX buffer empty.
#define MCCI_STM32L0_GPIO_PUPDR
#define MCCI_STM32L0_SPI_CR1_SPE
SPI enabled.
#define MCCI_STM32L0_SPI_CR2
offset to SPI control register 2
#define MCCI_STM32L0_GPIO_MODE_P(p)
compute the mask for the mode bits for port bits 0..15
#define MCCI_STM32L0_REG_RCC_APB1ENR_SPI2EN
#define MCCI_STM32L0_REG_RCC_APB1ENR
APB1 peripheral clock enable.
void McciBootloaderBoard_CatenaAbz_spiInit(void)