Support for F412

This commit is contained in:
Hierophect 2019-09-11 13:45:36 -04:00
commit 9e25d9a9be
7 changed files with 54 additions and 56 deletions

View File

@ -1,9 +1,10 @@
USB_VID = 0x483 USB_VID = 0x239A
USB_PID = 0x572B USB_PID = 0x802A
USB_PRODUCT = "STM32F412ZG Discovery Board - CPy" USB_PRODUCT = "STM32F412ZG Discovery Board - CPy"
USB_MANUFACTURER = "STMicroelectronics" USB_MANUFACTURER = "STMicroelectronics"
DISABLE_FILESYSTEM = 1 INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
MCU_SERIES = m4 MCU_SERIES = m4
MCU_VARIANT = stm32f4 MCU_VARIANT = stm32f4

View File

@ -36,7 +36,7 @@
*/ */
#define HAL_MODULE_ENABLED #define HAL_MODULE_ENABLED
/* #define HAL_ADC_MODULE_ENABLED */ #define HAL_ADC_MODULE_ENABLED
/* #define HAL_CRYP_MODULE_ENABLED */ /* #define HAL_CRYP_MODULE_ENABLED */
/* #define HAL_CAN_MODULE_ENABLED */ /* #define HAL_CAN_MODULE_ENABLED */
/* #define HAL_CRC_MODULE_ENABLED */ /* #define HAL_CRC_MODULE_ENABLED */

View File

@ -41,7 +41,7 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
const mcu_pin_obj_t *pin) { const mcu_pin_obj_t *pin) {
//No ADC function on pin //No ADC function on pin
if (pin->adc == 0xff) { if (pin->adc_unit == 0x00) {
mp_raise_ValueError(translate("Pin does not have ADC capabilities")); mp_raise_ValueError(translate("Pin does not have ADC capabilities"));
} }
//TODO: add ADC traits to structure? //TODO: add ADC traits to structure?
@ -60,7 +60,7 @@ void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
if (common_hal_analogio_analogin_deinited(self)) { if (common_hal_analogio_analogin_deinited(self)) {
return; return;
} }
reset_pin_number(self->pin->number); reset_pin_number(self->pin->port,self->pin->number);
self->pin = mp_const_none; self->pin = mp_const_none;
} }
@ -86,7 +86,7 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
AdcHandle.Init.DMAContinuousRequests = ENABLE; AdcHandle.Init.DMAContinuousRequests = ENABLE;
AdcHandle.Init.EOCSelection = DISABLE; AdcHandle.Init.EOCSelection = DISABLE;
sConfig.Channel = stm32_adc_channel(self->pin->adc); sConfig.Channel = self->pin->adc_channel;
sConfig.Rank = 1; sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
sConfig.Offset = 0; sConfig.Offset = 0;

View File

@ -34,7 +34,7 @@
void common_hal_digitalio_digitalinout_never_reset( void common_hal_digitalio_digitalinout_never_reset(
digitalio_digitalinout_obj_t *self) { digitalio_digitalinout_obj_t *self) {
never_reset_pin_number(self->pin->number); never_reset_pin_number(self->pin->port, self->pin->number);
} }
digitalinout_result_t common_hal_digitalio_digitalinout_construct( digitalinout_result_t common_hal_digitalio_digitalinout_construct(
@ -44,7 +44,7 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
self->pin = pin; self->pin = pin;
GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = (pin_mask(self->pin->number)); GPIO_InitStruct.Pin = 1<<self->pin->number;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
@ -62,7 +62,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self
return; return;
} }
reset_pin_number(self->pin->number); reset_pin_number(self->pin->port, self->pin->number);
self->pin = mp_const_none; self->pin = mp_const_none;
} }
@ -70,7 +70,7 @@ void common_hal_digitalio_digitalinout_switch_to_input(
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_mask(self->pin->number); GPIO_InitStruct.Pin = 1<<self->pin->number;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
@ -90,27 +90,27 @@ void common_hal_digitalio_digitalinout_switch_to_output(
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
digitalio_digitalinout_obj_t *self) { digitalio_digitalinout_obj_t *self) {
return (LL_GPIO_GetPinMode(pin_port(self->pin->number), pin_mask(self->pin->number)) return (LL_GPIO_GetPinMode(pin_port(self->pin->number), 1<<self->pin->number)
== LL_GPIO_MODE_INPUT) ? DIRECTION_INPUT : DIRECTION_OUTPUT; == LL_GPIO_MODE_INPUT) ? DIRECTION_INPUT : DIRECTION_OUTPUT;
} }
void common_hal_digitalio_digitalinout_set_value( void common_hal_digitalio_digitalinout_set_value(
digitalio_digitalinout_obj_t *self, bool value) { digitalio_digitalinout_obj_t *self, bool value) {
HAL_GPIO_WritePin(pin_port(self->pin->number), pin_mask(self->pin->number), value); HAL_GPIO_WritePin(pin_port(self->pin->number), 1<<self->pin->number, value);
} }
bool common_hal_digitalio_digitalinout_get_value( bool common_hal_digitalio_digitalinout_get_value(
digitalio_digitalinout_obj_t *self) { digitalio_digitalinout_obj_t *self) {
return (LL_GPIO_GetPinMode(pin_port(self->pin->number), pin_mask(self->pin->number)) == LL_GPIO_MODE_INPUT) return (LL_GPIO_GetPinMode(pin_port(self->pin->number), 1<<self->pin->number) == LL_GPIO_MODE_INPUT)
? HAL_GPIO_ReadPin(pin_port(self->pin->number), pin_mask(self->pin->number)) ? HAL_GPIO_ReadPin(pin_port(self->pin->number), 1<<self->pin->number)
: LL_GPIO_IsOutputPinSet(pin_port(self->pin->number), pin_mask(self->pin->number)); : LL_GPIO_IsOutputPinSet(pin_port(self->pin->number), 1<<self->pin->number);
} }
void common_hal_digitalio_digitalinout_set_drive_mode( void common_hal_digitalio_digitalinout_set_drive_mode(
digitalio_digitalinout_obj_t *self, digitalio_digitalinout_obj_t *self,
digitalio_drive_mode_t drive_mode) { digitalio_drive_mode_t drive_mode) {
GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_mask(self->pin->number); GPIO_InitStruct.Pin = 1<<self->pin->number;
GPIO_InitStruct.Mode = (drive_mode == DRIVE_MODE_OPEN_DRAIN ? GPIO_InitStruct.Mode = (drive_mode == DRIVE_MODE_OPEN_DRAIN ?
GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT_PP); GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT_PP);
GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pull = GPIO_NOPULL;
@ -121,7 +121,7 @@ void common_hal_digitalio_digitalinout_set_drive_mode(
digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode( digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
digitalio_digitalinout_obj_t *self) { digitalio_digitalinout_obj_t *self) {
return LL_GPIO_GetPinOutputType(pin_port(self->pin->number), pin_mask(self->pin->number)) return LL_GPIO_GetPinOutputType(pin_port(self->pin->number), 1<<self->pin->number)
== LL_GPIO_OUTPUT_OPENDRAIN ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL; == LL_GPIO_OUTPUT_OPENDRAIN ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL;
} }
@ -130,13 +130,13 @@ void common_hal_digitalio_digitalinout_set_pull(
switch (pull) { switch (pull) {
case PULL_UP: case PULL_UP:
LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_UP); LL_GPIO_SetPinPull(pin_port(self->pin->number), 1<<self->pin->number,LL_GPIO_PULL_UP);
break; break;
case PULL_DOWN: case PULL_DOWN:
LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_DOWN); LL_GPIO_SetPinPull(pin_port(self->pin->number), 1<<self->pin->number,LL_GPIO_PULL_DOWN);
break; break;
case PULL_NONE: case PULL_NONE:
LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_NO); LL_GPIO_SetPinPull(pin_port(self->pin->number), 1<<self->pin->number,LL_GPIO_PULL_NO);
break; break;
default: default:
break; break;
@ -147,7 +147,7 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
digitalio_digitalinout_obj_t *self) { digitalio_digitalinout_obj_t *self) {
switch (LL_GPIO_GetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number))) { switch (LL_GPIO_GetPinPull(pin_port(self->pin->number), 1<<self->pin->number)) {
case LL_GPIO_PULL_UP: case LL_GPIO_PULL_UP:
return PULL_UP; return PULL_UP;
case LL_GPIO_PULL_DOWN: case LL_GPIO_PULL_DOWN:

View File

@ -53,40 +53,40 @@ void reset_all_pins(void) {
} }
// Mark pin as free and return it to a quiescent state. // Mark pin as free and return it to a quiescent state.
void reset_pin_number(uint8_t pin) { void reset_pin_number(uint8_t pin_port, uint8_t pin_number) {
if (pin == NO_PIN) { if (pin_port == 0x0F) {
return; return;
} }
// Clear claimed bit. // Clear claimed bit.
claimed_pins[gpio_port_num(pin)] &= ~(gpio_mask(pin)); claimed_pins[pin_port] &= ~(1<<pin_number);
// Reset the pin // Reset the pin
HAL_GPIO_DeInit(ports[gpio_port_num(pin)], gpio_mask(pin)); HAL_GPIO_DeInit(ports[pin_port], 1<<pin_number);
} }
void never_reset_pin_number(uint8_t pin) { void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number) {
never_reset_pins[gpio_port_num(pin)] |= gpio_mask(pin); never_reset_pins[pin_port] |= 1<<pin_number;
} }
void claim_pin(const mcu_pin_obj_t* pin) { void claim_pin(const mcu_pin_obj_t* pin) {
// Set bit in claimed_pins bitmask. // Set bit in claimed_pins bitmask.
claimed_pins[gpio_port_num(pin->number)] |= gpio_mask(pin->number); claimed_pins[pin->port] |= 1<<pin->number;
} }
bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number) {
bool pin_number_is_free(uint8_t pin) { return !(claimed_pins[pin_port] & 1<<pin_number);
return !(claimed_pins[gpio_port_num(pin)] & gpio_mask(pin));
} }
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
return pin_number_is_free(pin->number); return pin_number_is_free(pin->port, pin->number);
} }
void* pin_port(uint8_t pin) { GPIO_TypeDef * pin_port(uint8_t pin_port) {
return ports[gpio_port_num(pin)]; return ports[pin_port];
} }
uint16_t pin_mask(uint8_t pin) { //TODO: replace with macro?
return gpio_mask(pin); uint16_t pin_mask(uint8_t pin_number) {
return 1<<pin_number;
} }

View File

@ -34,20 +34,11 @@
void reset_all_pins(void); void reset_all_pins(void);
// reset_pin_number takes the pin number instead of the pointer so that objects don't // reset_pin_number takes the pin number instead of the pointer so that objects don't
// need to store a full pointer. // need to store a full pointer.
void reset_pin_number(uint8_t pin); void reset_pin_number(uint8_t pin_port, uint8_t pin_number);
void claim_pin(const mcu_pin_obj_t* pin); void claim_pin(const mcu_pin_obj_t* pin);
bool pin_number_is_free(uint8_t pin); bool pin_number_is_free(uint8_t pin_port, uint8_t pin_number);
void never_reset_pin_number(uint8_t pin); void never_reset_pin_number(uint8_t pin_port, uint8_t pin_number);
void* pin_port(uint8_t pin); GPIO_TypeDef * pin_port(uint8_t pin_port);
uint16_t pin_mask(uint8_t pin); uint16_t pin_mask(uint8_t pin_number);
//helper functions for unpacking the pin object "number" into hal-usable addr/mask
static inline uint8_t gpio_port_num(uint8_t packed_pin) {
return packed_pin >> 4;
}
static inline uint16_t gpio_mask(uint8_t packed_pin) {
return 1 << (0x0F & packed_pin);
}
#endif // MICROPY_INCLUDED_STM34F4_COMMON_HAL_MICROCONTROLLER_PIN_H #endif // MICROPY_INCLUDED_STM34F4_COMMON_HAL_MICROCONTROLLER_PIN_H

View File

@ -38,8 +38,10 @@
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
uint8_t number; uint8_t port:4;
uint8_t adc; //(3)units (5)channel uint8_t number:4;
uint8_t adc_unit:3;
uint8_t adc_channel:5;
} mcu_pin_obj_t; } mcu_pin_obj_t;
//Standard stm32 adc unit combinations //Standard stm32 adc unit combinations
@ -50,11 +52,14 @@ typedef struct {
//STM32 ADC pins can have a combination of 1, 2 or all 3 ADCs on a single pin, //STM32 ADC pins can have a combination of 1, 2 or all 3 ADCs on a single pin,
//but all 3 ADCs will share the same input number per pin. //but all 3 ADCs will share the same input number per pin.
//F4 family has 3 ADC max, 24 channels max.
#define ADC_INPUT(mask, number) \ #define ADC_INPUT(mask, number) \
.adc = (((mask) << 5) | ((number) & 0x1F)), .adc_unit = mask, \
.adc_channel = number,
#define NO_ADC \ #define NO_ADC \
.adc = 0xff, .adc_unit = 0x00, \
.adc_channel = 0x1f
extern const mp_obj_type_t mcu_pin_type; extern const mp_obj_type_t mcu_pin_type;
@ -63,7 +68,8 @@ extern const mp_obj_type_t mcu_pin_type;
#define PIN(p_port, p_number, p_adc) \ #define PIN(p_port, p_number, p_adc) \
{ \ { \
{ &mcu_pin_type }, \ { &mcu_pin_type }, \
.number = (((p_port) << 4) | ((p_number) & 0x0F)), \ .port = p_port, \
.number = p_number, \
p_adc \ p_adc \
} }