Fix i2c and spi to compile for Netduino Plus 2

This commit is contained in:
Dave Hylands 2014-04-20 00:06:03 -07:00
parent 53775026e7
commit f70630c58f
3 changed files with 35 additions and 27 deletions

View File

@ -17,6 +17,8 @@
#define MICROPY_HW_ENABLE_TIMER (1) #define MICROPY_HW_ENABLE_TIMER (1)
#define MICROPY_HW_ENABLE_SERVO (1) #define MICROPY_HW_ENABLE_SERVO (1)
#define MICROPY_HW_ENABLE_DAC (0) #define MICROPY_HW_ENABLE_DAC (0)
#define MICROPU_HW_ENABLE_I2C1 (0)
#define MICROPU_HW_ENABLE_SPI1 (0)
// USRSW is pulled low. Pressing the button makes the input go high. // USRSW is pulled low. Pressing the button makes the input go high.
#define MICROPY_HW_USRSW_PIN (pin_B11) #define MICROPY_HW_USRSW_PIN (pin_B11)

View File

@ -13,6 +13,10 @@
#include "genhdr/pins.h" #include "genhdr/pins.h"
#include "i2c.h" #include "i2c.h"
#if !defined(MICROPU_HW_ENABLE_I2C1)
#define MICROPY_HW_ENABLE_I2C1 (1)
#endif
I2C_HandleTypeDef I2CHandle1 = {.Instance = NULL}; I2C_HandleTypeDef I2CHandle1 = {.Instance = NULL};
I2C_HandleTypeDef I2CHandle2 = {.Instance = NULL}; I2C_HandleTypeDef I2CHandle2 = {.Instance = NULL};
@ -32,6 +36,7 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines
const pin_obj_t *pins[2]; const pin_obj_t *pins[2];
#if MICROPY_HW_ENABLE_I2C1
if (i2c == &I2CHandle1) { if (i2c == &I2CHandle1) {
// X-skin: X9=PB6=SCL, X10=PB7=SDA // X-skin: X9=PB6=SCL, X10=PB7=SDA
pins[0] = &pin_B6; pins[0] = &pin_B6;
@ -39,7 +44,9 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
GPIO_InitStructure.Alternate = GPIO_AF4_I2C1; GPIO_InitStructure.Alternate = GPIO_AF4_I2C1;
// enable the I2C clock // enable the I2C clock
__I2C1_CLK_ENABLE(); __I2C1_CLK_ENABLE();
} else { } else
#endif
if (i2c == &I2CHandle2) {
// Y-skin: Y9=PB10=SCL, Y10=PB11=SDA // Y-skin: Y9=PB10=SCL, Y10=PB11=SDA
pins[0] = &pin_B10; pins[0] = &pin_B10;
pins[1] = &pin_B11; pins[1] = &pin_B11;
@ -54,13 +61,6 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure); HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
} }
// enable the I2C clock
if (i2c == &I2CHandle1) {
__I2C1_CLK_ENABLE();
} else {
__I2C2_CLK_ENABLE();
}
// init the I2C device // init the I2C device
i2c->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; i2c->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
i2c->Init.ClockSpeed = 400000; i2c->Init.ClockSpeed = 400000;
@ -88,7 +88,10 @@ typedef struct _pyb_i2c_obj_t {
I2C_HandleTypeDef *i2c; I2C_HandleTypeDef *i2c;
} pyb_i2c_obj_t; } pyb_i2c_obj_t;
STATIC const pyb_i2c_obj_t pyb_i2c_obj[PYB_NUM_I2C] = {{{&pyb_i2c_type}, &I2CHandle1}, {{&pyb_i2c_type}, &I2CHandle2}}; STATIC const pyb_i2c_obj_t pyb_i2c_obj[PYB_NUM_I2C] = {
{{&pyb_i2c_type}, &I2CHandle1},
{{&pyb_i2c_type}, &I2CHandle2}
};
STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// check arguments // check arguments

View File

@ -13,11 +13,13 @@
#include "genhdr/pins.h" #include "genhdr/pins.h"
#include "spi.h" #include "spi.h"
#if !defined(MICROPU_HW_ENABLE_SPI1)
#define MICROPY_HW_ENABLE_SPI1 (1)
#endif
SPI_HandleTypeDef SPIHandle1 = {.Instance = NULL}; SPI_HandleTypeDef SPIHandle1 = {.Instance = NULL};
SPI_HandleTypeDef SPIHandle2 = {.Instance = NULL}; SPI_HandleTypeDef SPIHandle2 = {.Instance = NULL};
#if MICROPY_HW_ENABLE_SPI3
SPI_HandleTypeDef SPIHandle3 = {.Instance = NULL}; SPI_HandleTypeDef SPIHandle3 = {.Instance = NULL};
#endif
void spi_init0(void) { void spi_init0(void) {
// reset the SPI handles // reset the SPI handles
@ -25,10 +27,8 @@ void spi_init0(void) {
SPIHandle1.Instance = SPI1; SPIHandle1.Instance = SPI1;
memset(&SPIHandle2, 0, sizeof(SPI_HandleTypeDef)); memset(&SPIHandle2, 0, sizeof(SPI_HandleTypeDef));
SPIHandle2.Instance = SPI2; SPIHandle2.Instance = SPI2;
#if MICROPY_HW_ENABLE_SPI3
memset(&SPIHandle3, 0, sizeof(SPI_HandleTypeDef)); memset(&SPIHandle3, 0, sizeof(SPI_HandleTypeDef));
SPIHandle3.Instance = SPI3; SPIHandle3.Instance = SPI3;
#endif
} }
// TODO allow to take a list of pins to use // TODO allow to take a list of pins to use
@ -40,6 +40,7 @@ void spi_init(SPI_HandleTypeDef *spi) {
GPIO_InitStructure.Pull = GPIO_PULLUP; // ST examples use PULLUP GPIO_InitStructure.Pull = GPIO_PULLUP; // ST examples use PULLUP
const pin_obj_t *pins[4]; const pin_obj_t *pins[4];
#if MICROPY_HW_ENABLE_SPI1
if (spi->Instance == SPI1) { if (spi->Instance == SPI1) {
// X-skin: X5=PA4=SPI1_NSS, X6=PA5=SPI1_SCK, X7=PA6=SPI1_MISO, X8=PA7=SPI1_MOSI // X-skin: X5=PA4=SPI1_NSS, X6=PA5=SPI1_SCK, X7=PA6=SPI1_MISO, X8=PA7=SPI1_MOSI
pins[0] = &pin_A4; pins[0] = &pin_A4;
@ -47,25 +48,28 @@ void spi_init(SPI_HandleTypeDef *spi) {
pins[2] = &pin_A6; pins[2] = &pin_A6;
pins[3] = &pin_A7; pins[3] = &pin_A7;
GPIO_InitStructure.Alternate = GPIO_AF5_SPI1; GPIO_InitStructure.Alternate = GPIO_AF5_SPI1;
} else if (spi->Instance == SPI2) { } else
#endif
if (spi->Instance == SPI2) {
// Y-skin: Y5=PB12=SPI2_NSS, Y6=PB13=SPI2_SCK, Y7=PB14=SPI2_MISO, Y8=PB15=SPI2_MOSI // Y-skin: Y5=PB12=SPI2_NSS, Y6=PB13=SPI2_SCK, Y7=PB14=SPI2_MISO, Y8=PB15=SPI2_MOSI
pins[0] = &pin_B12; pins[0] = &pin_B12;
pins[1] = &pin_B13; pins[1] = &pin_B13;
pins[2] = &pin_B14; pins[2] = &pin_B14;
pins[3] = &pin_B15; pins[3] = &pin_B15;
GPIO_InitStructure.Alternate = GPIO_AF5_SPI2; GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;
} else
#if MICROPY_HW_ENABLE_SPI3 #if MICROPY_HW_ENABLE_SPI3
} else if (spi->Instance == SPI3) { if (spi->Instance == SPI3) {
pins[0] = &pin_A4; pins[0] = &pin_A4;
pins[1] = &pin_B3; pins[1] = &pin_B3;
pins[2] = &pin_B4; pins[2] = &pin_B4;
pins[3] = &pin_B5; pins[3] = &pin_B5;
GPIO_InitStructure.Alternate = GPIO_AF6_SPI3; GPIO_InitStructure.Alternate = GPIO_AF6_SPI3;
} else
#endif #endif
} else { {
// SPI does not exist for this board // SPI does not exist for this board
printf("HardwareError: invalid SPI\n"); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "SPI bus does not exist"));
return;
} }
for (uint i = 0; i < 4; i++) { for (uint i = 0; i < 4; i++) {
@ -78,10 +82,8 @@ void spi_init(SPI_HandleTypeDef *spi) {
__SPI1_CLK_ENABLE(); __SPI1_CLK_ENABLE();
} else if (spi->Instance == SPI2) { } else if (spi->Instance == SPI2) {
__SPI2_CLK_ENABLE(); __SPI2_CLK_ENABLE();
#if MICROPY_HW_ENABLE_SPI3 } else if (spi->Instance == SPI3) {
} else {
__SPI3_CLK_ENABLE(); __SPI3_CLK_ENABLE();
#endif
} }
// init the I2C device // init the I2C device
@ -100,24 +102,25 @@ void spi_deinit(SPI_HandleTypeDef *spi) {
__SPI1_CLK_DISABLE(); __SPI1_CLK_DISABLE();
} else if (spi->Instance == SPI2) { } else if (spi->Instance == SPI2) {
__SPI2_CLK_DISABLE(); __SPI2_CLK_DISABLE();
#if MICROPY_HW_ENABLE_SPI3 } else if (spi->Instance == SPI3) {
} else {
__SPI3_CLK_DISABLE(); __SPI3_CLK_DISABLE();
#endif
} }
} }
/******************************************************************************/ /******************************************************************************/
/* Micro Python bindings */ /* Micro Python bindings */
#define PYB_SPI_NUM (2) #define PYB_NUM_SPI (2)
typedef struct _pyb_spi_obj_t { typedef struct _pyb_spi_obj_t {
mp_obj_base_t base; mp_obj_base_t base;
SPI_HandleTypeDef *spi; SPI_HandleTypeDef *spi;
} pyb_spi_obj_t; } pyb_spi_obj_t;
STATIC const pyb_spi_obj_t pyb_spi_obj[PYB_SPI_NUM] = {{{&pyb_spi_type}, &SPIHandle1}, {{&pyb_spi_type}, &SPIHandle2}}; STATIC const pyb_spi_obj_t pyb_spi_obj[PYB_NUM_SPI] = {
{{&pyb_spi_type}, &SPIHandle1},
{{&pyb_spi_type}, &SPIHandle2}
};
STATIC void pyb_spi_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { STATIC void pyb_spi_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
pyb_spi_obj_t *self = self_in; pyb_spi_obj_t *self = self_in;
@ -220,7 +223,7 @@ STATIC mp_obj_t pyb_spi_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
machine_int_t spi_id = mp_obj_get_int(args[0]) - 1; machine_int_t spi_id = mp_obj_get_int(args[0]) - 1;
// check SPI number // check SPI number
if (!(0 <= spi_id && spi_id < PYB_SPI_NUM)) { if (!(0 <= spi_id && spi_id < PYB_NUM_SPI)) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "SPI bus %d does not exist", spi_id + 1)); nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "SPI bus %d does not exist", spi_id + 1));
} }