diff options
Diffstat (limited to 'stmhal')
-rw-r--r--[-rwxr-xr-x] | stmhal/boards/STM32F4DISC/staccel.py | 45 | ||||
-rw-r--r-- | stmhal/hal/src/stm32f4xx_hal.c | 2 | ||||
-rw-r--r-- | stmhal/modpyb.c | 40 | ||||
-rw-r--r-- | stmhal/pybwlan.c | 34 | ||||
-rw-r--r-- | stmhal/qstrdefsport.h | 1 | ||||
-rw-r--r-- | stmhal/stm32f4xx_it.c | 11 | ||||
-rw-r--r-- | stmhal/systick.c | 48 | ||||
-rw-r--r-- | stmhal/systick.h | 1 | ||||
-rw-r--r-- | stmhal/usb.c | 1 |
9 files changed, 148 insertions, 35 deletions
diff --git a/stmhal/boards/STM32F4DISC/staccel.py b/stmhal/boards/STM32F4DISC/staccel.py index 8395df803d..eb36f0343c 100755..100644 --- a/stmhal/boards/STM32F4DISC/staccel.py +++ b/stmhal/boards/STM32F4DISC/staccel.py @@ -21,36 +21,41 @@ from pyb import SPI READWRITE_CMD = const(0x80) MULTIPLEBYTE_CMD = const(0x40) LIS302DL_WHO_AM_I_ADDR = const(0x0f) +LIS302DL_WHO_AM_I_VAL = const(0x3b) LIS302DL_CTRL_REG1_ADDR = const(0x20) LIS302DL_OUT_X = const(0x29) +LIS302DL_OUT_Y = const(0x2b) +LIS302DL_OUT_Z = const(0x2d) # Configuration for 100Hz sampling rate, +-2g range -LIS302DL_CONF = 0b01000111 +LIS302DL_CONF = const(0b01000111) -def signed8(x): +def convert_raw_to_g(x): if x & 0x80: - return x - 256 - else: - return x + x = x - 256 + return x * 18 / 1000 class STAccel: def __init__(self): self.cs_pin = Pin('PE3', Pin.OUT_PP, Pin.PULL_NONE) self.cs_pin.high() self.spi = SPI(1, SPI.MASTER, baudrate=328125, polarity=0, phase=1, bits=8) - self.wr(LIS302DL_CTRL_REG1_ADDR, bytearray([LIS302DL_CONF])) + self.write_bytes(LIS302DL_CTRL_REG1_ADDR, bytearray([LIS302DL_CONF])) + if self.read_id() != LIS302DL_WHO_AM_I_VAL: + raise Exception('LIS302DL accelerometer not present') - def rd(self, addr, nbytes): + def read_bytes(self, addr, nbytes): if nbytes > 1: addr |= READWRITE_CMD | MULTIPLEBYTE_CMD else: addr |= READWRITE_CMD self.cs_pin.low() self.spi.send(addr) - buf = self.spi.send_recv(bytearray(nbytes * [0])) # read data, MSB first + #buf = self.spi.send_recv(bytearray(nbytes * [0])) # read data, MSB first + buf = self.spi.recv(nbytes) self.cs_pin.high() return buf - def wr(self, addr, buf): + def write_bytes(self, addr, buf): if len(buf) > 1: addr |= MULTIPLEBYTE_CMD self.cs_pin.low() @@ -60,11 +65,19 @@ class STAccel: self.cs_pin.high() def read_id(self): - return self.rd(LIS302DL_WHO_AM_I_ADDR, 1) + return self.read_bytes(LIS302DL_WHO_AM_I_ADDR, 1)[0] - def get_xyz(self): - val = self.rd(LIS302DL_OUT_X, 5) - x = signed8(val[0]) * 18.0 / 1000 - y = signed8(val[2]) * 18.0 / 1000 - z = signed8(val[4]) * 18.0 / 1000 - return [x, y, z] + def x(self): + return convert_raw_to_g(self.read_bytes(LIS302DL_OUT_X, 1)[0]) + + def y(self): + return convert_raw_to_g(self.read_bytes(LIS302DL_OUT_Y, 1)[0]) + + def z(self): + return convert_raw_to_g(self.read_bytes(LIS302DL_OUT_Z, 1)[0]) + + def xyz(self): + val = self.read_bytes(LIS302DL_OUT_X, 5) + return [convert_raw_to_g(val[0]), + convert_raw_to_g(val[2]), + convert_raw_to_g(val[4])] diff --git a/stmhal/hal/src/stm32f4xx_hal.c b/stmhal/hal/src/stm32f4xx_hal.c index 6c076c358b..0040658a0c 100644 --- a/stmhal/hal/src/stm32f4xx_hal.c +++ b/stmhal/hal/src/stm32f4xx_hal.c @@ -93,7 +93,7 @@ #define CMPCR_CMP_PD_BB (PERIPH_BB_BASE + (CMPCR_OFFSET * 32) + (CMP_PD_BitNumber * 4))
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
-static __IO uint32_t uwTick;
+__IO uint32_t uwTick;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c index de7eac4024..09a8caf278 100644 --- a/stmhal/modpyb.c +++ b/stmhal/modpyb.c @@ -99,7 +99,7 @@ STATIC mp_obj_t pyb_info(uint n_args, const mp_obj_t *args) { // get and print clock speeds // SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz { - printf("S=%lu\nH=%lu\nP1=%lu\nP2=%lu\n", + printf("S=%lu\nH=%lu\nP1=%lu\nP2=%lu\n", HAL_RCC_GetSysClockFreq(), HAL_RCC_GetHCLKFreq(), HAL_RCC_GetPCLK1Freq(), @@ -187,11 +187,44 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_sync_obj, pyb_sync); /// \function millis() /// Returns the number of milliseconds since the board was last reset. +/// +/// Note that this may return a negative number. This allows you to always +/// do: +/// start = pyb.millis() +/// ...do some operation... +/// elapsed = pyb.millis() - start +/// +/// and as long as the time of your operation is less than 24 days, you'll +/// always get the right answer and not have to worry about whether pyb.millis() +/// wraps around. STATIC mp_obj_t pyb_millis(void) { - return mp_obj_new_int(HAL_GetTick()); + // We want to "cast" the 32 bit unsigned into a small-int. This means + // copying the MSB down 1 bit (extending the sign down), which is + // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro. + return MP_OBJ_NEW_SMALL_INT(HAL_GetTick()); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis); +/// \function micros() +/// Returns the number of microseconds since the board was last reset. +/// +/// Note that this may return a negative number. This allows you to always +/// do: +/// start = pyb.micros() +/// ...do some operation... +/// elapsed = pyb.micros() - start +/// +/// and as long as the time of your operation is less than 35 minutes, you'll +/// always get the right answer and not have to worry about whether pyb.micros() +/// wraps around. +STATIC mp_obj_t pyb_micros(void) { + // We want to "cast" the 32 bit unsigned into a small-int. This means + // copying the MSB down 1 bit (extending the sign down), which is + // equivalent to just using the MP_OBJ_NEW_SMALL_INT macro. + return MP_OBJ_NEW_SMALL_INT(sys_tick_get_microseconds()); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros); + /// \function delay(ms) /// Delay for the given number of milliseconds. STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) { @@ -251,7 +284,7 @@ STATIC mp_obj_t pyb_stop(void) { /* Enter Stop Mode */ PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); - /* Configures system clock after wake-up from STOP: enable HSE, PLL and select + /* Configures system clock after wake-up from STOP: enable HSE, PLL and select * PLL as system clock source (HSE and PLL are disabled in STOP mode) */ SYSCLKConfig_STOP(); @@ -343,6 +376,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_USB_VCP), (mp_obj_t)&pyb_usb_vcp_type }, { MP_OBJ_NEW_QSTR(MP_QSTR_millis), (mp_obj_t)&pyb_millis_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_micros), (mp_obj_t)&pyb_micros_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&pyb_delay_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&pyb_udelay_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&pyb_sync_obj }, diff --git a/stmhal/pybwlan.c b/stmhal/pybwlan.c index 15221091f1..b2f95b93ff 100644 --- a/stmhal/pybwlan.c +++ b/stmhal/pybwlan.c @@ -47,7 +47,7 @@ #include "cc3k/wlan.h" #include "cc3k/nvmem.h" -mp_obj_t pyb_wlan_connect(uint n_args, const mp_obj_t *args) { +STATIC mp_obj_t pyb_wlan_connect(uint n_args, const mp_obj_t *args) { const char *ap; const char *key; if (n_args == 2) { @@ -61,11 +61,13 @@ mp_obj_t pyb_wlan_connect(uint n_args, const mp_obj_t *args) { int ret = wlan_connect(WLAN_SEC_WPA2, ap, strlen(ap), NULL, (byte*)key, strlen(key)); return mp_obj_new_int(ret); } +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_wlan_connect_obj, 0, 2, pyb_wlan_connect); -mp_obj_t pyb_wlan_disconnect(void) { +STATIC mp_obj_t pyb_wlan_disconnect(void) { int ret = wlan_disconnect(); return mp_obj_new_int(ret); } +STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_wlan_disconnect_obj, pyb_wlan_disconnect); mp_obj_t decode_addr(unsigned char *ip, int n_bytes) { char data[64] = ""; @@ -76,7 +78,7 @@ mp_obj_t decode_addr(unsigned char *ip, int n_bytes) { } else if (n_bytes == 32) { snprintf(data, 64, "%s", ip); } - return mp_obj_new_str((byte*)data, strlen(data), false); + return mp_obj_new_str(data, strlen(data), false); } void decode_addr_and_store(mp_obj_t object, qstr q_attr, unsigned char *ip, int n_bytes) { @@ -85,7 +87,7 @@ void decode_addr_and_store(mp_obj_t object, qstr q_attr, unsigned char *ip, int static mp_obj_t net_address_type = MP_OBJ_NULL; -mp_obj_t pyb_wlan_get_ip(void) { +STATIC mp_obj_t pyb_wlan_get_ip(void) { tNetappIpconfigRetArgs ipconfig; netapp_ipconfig(&ipconfig); @@ -113,9 +115,10 @@ mp_obj_t pyb_wlan_get_ip(void) { return net_addr; } +STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_wlan_get_ip_obj, pyb_wlan_get_ip); uint32_t last_ip = 0; // XXX such a hack! -mp_obj_t pyb_wlan_get_host(mp_obj_t host_name) { +STATIC mp_obj_t pyb_wlan_get_host(mp_obj_t host_name) { const char *host = mp_obj_str_get_str(host_name); uint32_t ip; if (gethostbyname(host, strlen(host), &ip) < 0) { @@ -134,8 +137,9 @@ mp_obj_t pyb_wlan_get_host(mp_obj_t host_name) { ip_data[3] = ((ip >> 24) & 0xff); return decode_addr(ip_data, 4); } +STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_wlan_get_host_obj, pyb_wlan_get_host); -mp_obj_t pyb_wlan_http_get(mp_obj_t host_name, mp_obj_t host_path) { +STATIC mp_obj_t pyb_wlan_http_get(mp_obj_t host_name, mp_obj_t host_path) { int port; if (mp_obj_is_integer(host_name)) { last_ip = (192 << 24) | (168 << 16) | (0 << 8) | (mp_obj_get_int(host_name)); @@ -259,7 +263,7 @@ mp_obj_t pyb_wlan_http_get(mp_obj_t host_name, mp_obj_t host_path) { vstr_add_strn(vstr, buf, ret); } - mp_ret = mp_obj_new_str((byte*)vstr->buf, vstr->len, false); + mp_ret = mp_obj_new_str(vstr->buf, vstr->len, false); } closesocket(sd); @@ -267,8 +271,9 @@ mp_obj_t pyb_wlan_http_get(mp_obj_t host_name, mp_obj_t host_path) { return mp_ret; } +STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_wlan_http_get_obj, pyb_wlan_http_get); -mp_obj_t pyb_wlan_serve(void) { +STATIC mp_obj_t pyb_wlan_serve(void) { printf("serve socket\n"); int sd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); printf("serve socket got %d\n", sd); @@ -340,6 +345,7 @@ mp_obj_t pyb_wlan_serve(void) { return mp_const_none; } +STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_wlan_serve_obj, pyb_wlan_serve); //***************************************************************************** // @@ -415,12 +421,12 @@ void pyb_wlan_init(void) { wlan_init(CC3000_UsynchCallback, sendWLFWPatch, sendDriverPatch, sendBootLoaderPatch, ReadWlanInterruptPin, WlanInterruptEnable, WlanInterruptDisable, WriteWlanPin); mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("wlan")); - mp_store_attr(m, QSTR_FROM_STR_STATIC("connect"), mp_make_function_var(0, pyb_wlan_connect)); - mp_store_attr(m, QSTR_FROM_STR_STATIC("disconnect"), mp_make_function_n(0, pyb_wlan_disconnect)); - mp_store_attr(m, QSTR_FROM_STR_STATIC("ip"), mp_make_function_n(0, pyb_wlan_get_ip)); - mp_store_attr(m, QSTR_FROM_STR_STATIC("get_host"), mp_make_function_n(1, pyb_wlan_get_host)); - mp_store_attr(m, QSTR_FROM_STR_STATIC("http_get"), mp_make_function_n(2, pyb_wlan_http_get)); - mp_store_attr(m, QSTR_FROM_STR_STATIC("serve"), mp_make_function_n(0, pyb_wlan_serve)); + mp_store_attr(m, QSTR_FROM_STR_STATIC("connect"), (mp_obj_t)&pyb_wlan_connect_obj); + mp_store_attr(m, QSTR_FROM_STR_STATIC("disconnect"), (mp_obj_t)&pyb_wlan_disconnect_obj); + mp_store_attr(m, QSTR_FROM_STR_STATIC("ip"), (mp_obj_t)&pyb_wlan_get_ip_obj); + mp_store_attr(m, QSTR_FROM_STR_STATIC("get_host"), (mp_obj_t)&pyb_wlan_get_host_obj); + mp_store_attr(m, QSTR_FROM_STR_STATIC("http_get"), (mp_obj_t)&pyb_wlan_http_get_obj); + mp_store_attr(m, QSTR_FROM_STR_STATIC("serve"), (mp_obj_t)&pyb_wlan_serve_obj); mp_store_name(QSTR_FROM_STR_STATIC("wlan"), m); } diff --git a/stmhal/qstrdefsport.h b/stmhal/qstrdefsport.h index 53b385dbfa..04f49276b5 100644 --- a/stmhal/qstrdefsport.h +++ b/stmhal/qstrdefsport.h @@ -67,6 +67,7 @@ Q(/flash/lib) Q(/sd) Q(/sd/lib) Q(millis) +Q(micros) // for file class Q(seek) diff --git a/stmhal/stm32f4xx_it.c b/stmhal/stm32f4xx_it.c index 5fa7f8289d..74fdf53d1e 100644 --- a/stmhal/stm32f4xx_it.c +++ b/stmhal/stm32f4xx_it.c @@ -173,7 +173,16 @@ void PendSV_Handler(void) { * @retval None */ void SysTick_Handler(void) { - HAL_IncTick(); + // Instead of calling HAL_IncTick we do the increment here of the counter. + // This is purely for efficiency, since SysTick is called 1000 times per + // second at the highest interrupt priority. + extern __IO uint32_t uwTick; + uwTick += 1; + + // Read the systick control regster. This has the side effect of clearing + // the COUNTFLAG bit, which makes the logic in sys_tick_get_microseconds + // work properly. + SysTick->CTRL; } /******************************************************************************/ diff --git a/stmhal/systick.c b/stmhal/systick.c index 196f1fbcae..b4dd68ffec 100644 --- a/stmhal/systick.c +++ b/stmhal/systick.c @@ -27,8 +27,24 @@ #include <stm32f4xx_hal.h> #include "mpconfig.h" #include "misc.h" +#include "nlr.h" +#include "qstr.h" +#include "obj.h" +#include "irq.h" #include "systick.h" +// We provide our own version of HAL_Delay that calls __WFI while waiting, in +// order to reduce power consumption. +void HAL_Delay(uint32_t Delay) { + extern __IO uint32_t uwTick; + uint32_t start = uwTick; + // Wraparound of tick is taken care of by 2's complement arithmetic. + while (uwTick - start < Delay) { + // Enter sleep mode, waiting for (at least) the SysTick interrupt. + __WFI(); + } +} + bool sys_tick_has_passed(uint32_t start_tick, uint32_t delay_ms) { return HAL_GetTick() - start_tick >= delay_ms; } @@ -41,3 +57,35 @@ void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) { __WFI(); // enter sleep mode, waiting for interrupt } } + +// The SysTick timer counts down at 168 MHz, so we can use that knowledge +// to grab a microsecond counter. +// +// We assume that HAL_GetTickis returns milliseconds. +uint32_t sys_tick_get_microseconds(void) { + mp_uint_t irq_state = disable_irq(); + uint32_t counter = SysTick->VAL; + uint32_t milliseconds = HAL_GetTick(); + uint32_t status = SysTick->CTRL; + enable_irq(irq_state); + + // It's still possible for the countflag bit to get set if the counter was + // reloaded between reading VAL and reading CTRL. With interrupts disabled + // it definitely takes less than 50 HCLK cycles between reading VAL and + // reading CTRL, so the test (counter > 50) is to cover the case where VAL + // is +ve and very close to zero, and the COUNTFLAG bit is also set. + if ((status & SysTick_CTRL_COUNTFLAG_Msk) && counter > 50) { + // This means that the HW reloaded VAL between the time we read VAL and the + // time we read CTRL, which implies that there is an interrupt pending + // to increment the tick counter. + milliseconds++; + } + uint32_t load = SysTick->LOAD; + counter = load - counter; // Convert from decrementing to incrementing + + // ((load + 1) / 1000) is the number of counts per microsecond. + // + // counter / ((load + 1) / 1000) scales from the systick clock to microseconds + // and is the same thing as (counter * 1000) / (load + 1) + return milliseconds * 1000 + (counter * 1000) / (load + 1); +} diff --git a/stmhal/systick.h b/stmhal/systick.h index 1e7f623355..98045d16c4 100644 --- a/stmhal/systick.h +++ b/stmhal/systick.h @@ -26,3 +26,4 @@ void sys_tick_wait_at_least(uint32_t stc, uint32_t delay_ms); bool sys_tick_has_passed(uint32_t stc, uint32_t delay_ms); +uint32_t sys_tick_get_microseconds(void); diff --git a/stmhal/usb.c b/stmhal/usb.c index 40c474f432..a4369ff4cd 100644 --- a/stmhal/usb.c +++ b/stmhal/usb.c @@ -255,6 +255,7 @@ mp_obj_t pyb_usb_vcp___exit__(uint n_args, const mp_obj_t *args) { STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_usb_vcp___exit___obj, 4, 4, pyb_usb_vcp___exit__); STATIC const mp_map_elem_t pyb_usb_vcp_locals_dict_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR_any), (mp_obj_t)&pyb_usb_vcp_any_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&pyb_usb_vcp_send_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&pyb_usb_vcp_recv_obj }, /// \method read([nbytes]) |