Skip to content

Commit d222c64

Browse files
committed
Merge branch 'stm32-spi-oneline' into stm32-i2c-rework
2 parents 90613aa + 3d5528b commit d222c64

File tree

2 files changed

+152
-57
lines changed
  • ports
    • atmel-samd/boards/pewpew_m4
    • stm32f4/common-hal/busio

2 files changed

+152
-57
lines changed

ports/atmel-samd/boards/pewpew_m4/board.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,36 @@
3535

3636
displayio_fourwire_obj_t board_display_obj;
3737

38+
typedef struct {
39+
const uint32_t *config_data;
40+
void *handoverHID;
41+
void *handoverMSC;
42+
const char *info_uf2;
43+
} UF2_BInfo;
44+
45+
#define APP_START_ADDRESS 0x00004000
46+
#define UF2_BINFO ((UF2_BInfo *)(APP_START_ADDRESS - sizeof(UF2_BInfo)))
47+
48+
#define CFG_DISPLAY_CFG0 39
49+
#define CFG_MAGIC0 0x1e9e10f1
50+
3851
#define DELAY 0x80
3952

53+
uint32_t lookupCfg(uint32_t key, uint32_t defl) {
54+
const uint32_t *ptr = UF2_BINFO->config_data;
55+
if (!ptr || (((uint32_t)ptr) & 3) || *ptr != CFG_MAGIC0) {
56+
// no config data!
57+
} else {
58+
ptr += 4;
59+
while (*ptr) {
60+
if (*ptr == key)
61+
return ptr[1];
62+
ptr += 2;
63+
}
64+
}
65+
return defl;
66+
}
67+
4068
uint8_t display_init_sequence[] = {
4169
0x01, 0 | DELAY, 150, // SWRESET
4270
0x11, 0 | DELAY, 255, // SLPOUT
@@ -63,8 +91,6 @@ uint8_t display_init_sequence[] = {
6391
0x2E, 0x2C, 0x29, 0x2D,
6492
0x2E, 0x2E, 0x37, 0x3F,
6593
0x00, 0x00, 0x02, 0x10,
66-
0x2a, 3, 0x02, 0x00, 0x81, // _CASET XSTART = 2, XEND = 129
67-
0x2b, 3, 0x02, 0x00, 0x81, // _RASET XSTART = 2, XEND = 129
6894
0x13, 0 | DELAY, 10, // _NORON
6995
0x29, 0 | DELAY, 100, // _DISPON
7096
};
@@ -83,14 +109,17 @@ void board_init(void) {
83109
&pin_PA17, // TFT_RST Reset
84110
60000000);
85111

112+
uint32_t cfg0 = lookupCfg(CFG_DISPLAY_CFG0, 0x000000);
113+
uint32_t offX = (cfg0 >> 8) & 0xff;
114+
uint32_t offY = (cfg0 >> 16) & 0xff;
86115
displayio_display_obj_t* display = &displays[0].display;
87116
display->base.type = &displayio_display_type;
88117
common_hal_displayio_display_construct(display,
89118
bus,
90119
160, // Width (after rotation)
91120
128, // Height (after rotation)
92-
0, // column start
93-
0, // row start
121+
offX, // column start
122+
offY, // row start
94123
0, // rotation
95124
16, // Color depth
96125
false, // grayscale

ports/stm32f4/common-hal/busio/SPI.c

Lines changed: 119 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
#include "supervisor/shared/translate.h"
3737
#include "common-hal/microcontroller/Pin.h"
3838

39+
// Note that any bugs introduced in this file can cause crashes at startup
40+
// for chips using external SPI flash.
41+
3942
#define MAX_SPI 6 //TODO; replace this as part of periph cleanup
4043
#define ALL_CLOCKS 0xFF
4144

@@ -53,10 +56,10 @@ STATIC void spi_clock_disable(uint8_t mask);
5356
STATIC uint32_t get_busclock(SPI_TypeDef * instance) {
5457
//SPI2 and 3 are on PCLK1, if they exist.
5558
#ifdef SPI2
56-
if(instance == SPI2) return HAL_RCC_GetPCLK1Freq();
59+
if (instance == SPI2) return HAL_RCC_GetPCLK1Freq();
5760
#endif
5861
#ifdef SPI3
59-
if(instance == SPI3) return HAL_RCC_GetPCLK1Freq();
62+
if (instance == SPI3) return HAL_RCC_GetPCLK1Freq();
6063
#endif
6164
return HAL_RCC_GetPCLK2Freq();
6265
}
@@ -113,38 +116,80 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
113116
uint8_t sck_len = sizeof(mcu_spi_sck_list)/sizeof(*mcu_spi_sck_list);
114117
uint8_t mosi_len = sizeof(mcu_spi_mosi_list)/sizeof(*mcu_spi_mosi_list);
115118
uint8_t miso_len = sizeof(mcu_spi_miso_list)/sizeof(*mcu_spi_miso_list);
116-
117119
bool spi_taken = false;
118-
//sck
119-
for(uint i=0; i<sck_len;i++) {
120+
121+
//SCK is not optional. MOSI and MISO are
122+
for (uint i=0; i<sck_len;i++) {
120123
if (mcu_spi_sck_list[i].pin == sck) {
121-
//mosi
122-
for(uint j=0; j<mosi_len;j++) {
123-
if (mcu_spi_mosi_list[j].pin == mosi) {
124-
//miso
125-
for(uint k=0; k<miso_len;k++) {
126-
if ((mcu_spi_miso_list[k].pin == miso) //everything needs the same index
127-
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_mosi_list[j].spi_index)
128-
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_miso_list[k].spi_index)) {
129-
//keep looking if the SPI is taken, edge case
130-
if(reserved_spi[mcu_spi_sck_list[i].spi_index-1]) {
131-
spi_taken = true;
132-
continue;
124+
//if both MOSI and MISO exist, loop search normally
125+
if ((mosi != mp_const_none) && (miso != mp_const_none)) {
126+
//MOSI
127+
for (uint j=0; j<mosi_len;j++) {
128+
if (mcu_spi_mosi_list[j].pin == mosi) {
129+
//MISO
130+
for (uint k=0; k<miso_len;k++) {
131+
if ((mcu_spi_miso_list[k].pin == miso) //everything needs the same index
132+
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_mosi_list[j].spi_index)
133+
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_miso_list[k].spi_index)) {
134+
//keep looking if the SPI is taken, edge case
135+
if (reserved_spi[mcu_spi_sck_list[i].spi_index-1]) {
136+
spi_taken = true;
137+
continue;
138+
}
139+
//store pins if not
140+
self->sck = &mcu_spi_sck_list[i];
141+
self->mosi = &mcu_spi_mosi_list[j];
142+
self->miso = &mcu_spi_miso_list[k];
143+
break;
133144
}
134-
//store pins if not
135-
self->sck = &mcu_spi_sck_list[i];
136-
self->mosi = &mcu_spi_mosi_list[j];
137-
self->miso = &mcu_spi_miso_list[k];
138-
break;
139-
}
140-
}
145+
}
146+
}
141147
}
148+
// if just MISO, reduce search
149+
} else if (miso != mp_const_none) {
150+
for (uint j=0; j<miso_len;j++) {
151+
if ((mcu_spi_miso_list[j].pin == miso) //only SCK and MISO need the same index
152+
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_miso_list[j].spi_index)) {
153+
//keep looking if the SPI is taken, edge case
154+
if (reserved_spi[mcu_spi_sck_list[i].spi_index-1]) {
155+
spi_taken = true;
156+
continue;
157+
}
158+
//store pins if not
159+
self->sck = &mcu_spi_sck_list[i];
160+
self->mosi = NULL;
161+
self->miso = &mcu_spi_miso_list[j];
162+
break;
163+
}
164+
}
165+
// if just MOSI, reduce search
166+
} else if (mosi != mp_const_none) {
167+
for (uint j=0; j<mosi_len;j++) {
168+
if ((mcu_spi_mosi_list[j].pin == mosi) //only SCK and MOSI need the same index
169+
&& (mcu_spi_sck_list[i].spi_index == mcu_spi_mosi_list[j].spi_index)) {
170+
//keep looking if the SPI is taken, edge case
171+
if (reserved_spi[mcu_spi_sck_list[i].spi_index-1]) {
172+
spi_taken = true;
173+
continue;
174+
}
175+
//store pins if not
176+
self->sck = &mcu_spi_sck_list[i];
177+
self->mosi = &mcu_spi_mosi_list[j];
178+
self->miso = NULL;
179+
break;
180+
}
181+
}
182+
} else {
183+
//throw an error immediately
184+
mp_raise_ValueError(translate("Must provide MISO or MOSI pin"));
142185
}
143186
}
144187
}
145188

146189
//handle typedef selection, errors
147-
if(self->sck!=NULL && self->mosi!=NULL && self->miso!=NULL ) {
190+
if ( (self->sck!=NULL && self->mosi!=NULL && self->miso != NULL) ||
191+
(self->sck!=NULL && self->mosi!=NULL && miso == mp_const_none) ||
192+
(self->sck!=NULL && self->miso!=NULL && mosi == mp_const_none)) {
148193
SPIx = mcu_spi_banks[self->sck->spi_index-1];
149194
} else {
150195
if (spi_taken) {
@@ -163,26 +208,31 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
163208
GPIO_InitStruct.Alternate = self->sck->altfn_index;
164209
HAL_GPIO_Init(pin_port(sck->port), &GPIO_InitStruct);
165210

166-
GPIO_InitStruct.Pin = pin_mask(mosi->number);
167-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
168-
GPIO_InitStruct.Pull = GPIO_NOPULL;
169-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
170-
GPIO_InitStruct.Alternate = self->mosi->altfn_index;
171-
HAL_GPIO_Init(pin_port(mosi->port), &GPIO_InitStruct);
211+
if (self->mosi != NULL) {
212+
GPIO_InitStruct.Pin = pin_mask(mosi->number);
213+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
214+
GPIO_InitStruct.Pull = GPIO_NOPULL;
215+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
216+
GPIO_InitStruct.Alternate = self->mosi->altfn_index;
217+
HAL_GPIO_Init(pin_port(mosi->port), &GPIO_InitStruct);
218+
}
172219

173-
GPIO_InitStruct.Pin = pin_mask(miso->number);
174-
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
175-
GPIO_InitStruct.Pull = GPIO_NOPULL;
176-
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
177-
GPIO_InitStruct.Alternate = self->miso->altfn_index;
178-
HAL_GPIO_Init(pin_port(miso->port), &GPIO_InitStruct);
220+
if (self->miso != NULL) {
221+
GPIO_InitStruct.Pin = pin_mask(miso->number);
222+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
223+
GPIO_InitStruct.Pull = GPIO_NOPULL;
224+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
225+
GPIO_InitStruct.Alternate = self->miso->altfn_index;
226+
HAL_GPIO_Init(pin_port(miso->port), &GPIO_InitStruct);
227+
}
179228

180229
spi_clock_enable(1<<(self->sck->spi_index - 1));
181230
reserved_spi[self->sck->spi_index - 1] = true;
182231

183232
self->handle.Instance = SPIx;
184233
self->handle.Init.Mode = SPI_MODE_MASTER;
185-
self->handle.Init.Direction = SPI_DIRECTION_2LINES;
234+
// Direction change only required for RX-only, see RefMan RM0090:884
235+
self->handle.Init.Direction = (self->mosi == NULL) ? SPI_CR1_RXONLY : SPI_DIRECTION_2LINES;
186236
self->handle.Init.DataSize = SPI_DATASIZE_8BIT;
187237
self->handle.Init.CLKPolarity = SPI_POLARITY_LOW;
188238
self->handle.Init.CLKPhase = SPI_PHASE_1EDGE;
@@ -203,17 +253,25 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
203253
self->bits = 8;
204254

205255
claim_pin(sck);
206-
claim_pin(mosi);
207-
claim_pin(miso);
256+
if (self->mosi != NULL) {
257+
claim_pin(mosi);
258+
}
259+
if (self->miso != NULL) {
260+
claim_pin(miso);
261+
}
208262
}
209263

210264
void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
211-
for(size_t i = 0 ; i < MP_ARRAY_SIZE(mcu_spi_banks); i++) {
265+
for (size_t i = 0 ; i < MP_ARRAY_SIZE(mcu_spi_banks); i++) {
212266
if (mcu_spi_banks[i] == self->handle.Instance) {
213267
never_reset_spi[i] = true;
214268
never_reset_pin_number(self->sck->pin->port, self->sck->pin->number);
215-
never_reset_pin_number(self->mosi->pin->port, self->mosi->pin->number);
216-
never_reset_pin_number(self->miso->pin->port, self->miso->pin->number);
269+
if (self->mosi != NULL) {
270+
never_reset_pin_number(self->mosi->pin->port, self->mosi->pin->number);
271+
}
272+
if (self->miso != NULL) {
273+
never_reset_pin_number(self->miso->pin->port, self->miso->pin->number);
274+
}
217275
break;
218276
}
219277
}
@@ -229,8 +287,12 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
229287
never_reset_spi[self->sck->spi_index - 1] = false;
230288

231289
reset_pin_number(self->sck->pin->port,self->sck->pin->number);
232-
reset_pin_number(self->mosi->pin->port,self->mosi->pin->number);
233-
reset_pin_number(self->miso->pin->port,self->miso->pin->number);
290+
if (self->mosi != NULL) {
291+
reset_pin_number(self->mosi->pin->port,self->mosi->pin->number);
292+
}
293+
if (self->miso != NULL) {
294+
reset_pin_number(self->miso->pin->port,self->miso->pin->number);
295+
}
234296
self->sck = mp_const_none;
235297
self->mosi = mp_const_none;
236298
self->miso = mp_const_none;
@@ -240,7 +302,9 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
240302
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
241303
//This resets the SPI, so check before updating it redundantly
242304
if (baudrate == self->baudrate && polarity== self->polarity
243-
&& phase == self->phase && bits == self->bits) return true;
305+
&& phase == self->phase && bits == self->bits) {
306+
return true;
307+
}
244308

245309
//Deinit SPI
246310
HAL_SPI_DeInit(&self->handle);
@@ -251,13 +315,6 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
251315

252316
self->handle.Init.BaudRatePrescaler = stm32_baud_to_spi_div(baudrate, &self->prescaler,
253317
get_busclock(self->handle.Instance));
254-
self->handle.Init.Mode = SPI_MODE_MASTER;
255-
self->handle.Init.Direction = SPI_DIRECTION_2LINES;
256-
self->handle.Init.NSS = SPI_NSS_SOFT;
257-
self->handle.Init.FirstBit = SPI_FIRSTBIT_MSB;
258-
self->handle.Init.TIMode = SPI_TIMODE_DISABLE;
259-
self->handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
260-
self->handle.Init.CRCPolynomial = 10;
261318

262319
if (HAL_SPI_Init(&self->handle) != HAL_OK)
263320
{
@@ -300,18 +357,27 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
300357

301358
bool common_hal_busio_spi_write(busio_spi_obj_t *self,
302359
const uint8_t *data, size_t len) {
360+
if (self->mosi == NULL) {
361+
mp_raise_ValueError(translate("No MOSI Pin"));
362+
}
303363
HAL_StatusTypeDef result = HAL_SPI_Transmit (&self->handle, (uint8_t *)data, (uint16_t)len, HAL_MAX_DELAY);
304364
return result == HAL_OK;
305365
}
306366

307367
bool common_hal_busio_spi_read(busio_spi_obj_t *self,
308368
uint8_t *data, size_t len, uint8_t write_value) {
369+
if (self->miso == NULL) {
370+
mp_raise_ValueError(translate("No MISO Pin"));
371+
}
309372
HAL_StatusTypeDef result = HAL_SPI_Receive (&self->handle, data, (uint16_t)len, HAL_MAX_DELAY);
310373
return result == HAL_OK;
311374
}
312375

313376
bool common_hal_busio_spi_transfer(busio_spi_obj_t *self,
314377
uint8_t *data_out, uint8_t *data_in, size_t len) {
378+
if (self->miso == NULL || self->mosi == NULL) {
379+
mp_raise_ValueError(translate("Missing MISO or MOSI Pin"));
380+
}
315381
HAL_StatusTypeDef result = HAL_SPI_TransmitReceive (&self->handle,
316382
data_out, data_in, (uint16_t)len,HAL_MAX_DELAY);
317383
return result == HAL_OK;

0 commit comments

Comments
 (0)