Skip to content

Commit 8fe8413

Browse files
committed
stm: servos on PA0-3; MMA filtering; timer for Python REPL.
1 parent 11809ee commit 8fe8413

File tree

1 file changed

+65
-15
lines changed

1 file changed

+65
-15
lines changed

stm/main.c

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
#include "timer.h"
2525
#include "audio.h"
2626

27-
static FATFS fatfs0;
28-
2927
extern uint32_t _heap_start;
3028

29+
static FATFS fatfs0;
30+
3131
void flash_error(int n) {
3232
for (int i = 0; i < n; i++) {
3333
led_state(PYB_LED_R1, 1);
@@ -376,9 +376,12 @@ void do_repl(void) {
376376
py_obj_t module_fun = rt_make_function_from_id(1);
377377
if (module_fun != py_const_none) {
378378
nlr_buf_t nlr;
379+
uint32_t start = sys_tick_counter;
379380
if (nlr_push(&nlr) == 0) {
380381
rt_call_function_0(module_fun);
381382
nlr_pop();
383+
uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly
384+
printf("(took %lu ms)\n", ticks);
382385
} else {
383386
// uncaught exception
384387
py_obj_print((py_obj_t)nlr.ret_val);
@@ -468,7 +471,9 @@ void servo_init(void) {
468471
// TIM2 clock enable
469472
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
470473

471-
// GPIOC Configuration: TIM2_CH3 (PB10)
474+
// for PB10
475+
/*
476+
// GPIOB Configuration: TIM2_CH3 (PB10)
472477
GPIO_InitTypeDef GPIO_InitStructure;
473478
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
474479
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
@@ -479,6 +484,25 @@ void servo_init(void) {
479484
480485
// Connect TIM2 pins to AF1
481486
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_TIM2);
487+
*/
488+
489+
// for PA0, PA1, PA2, PA3
490+
{
491+
// GPIOA Configuration: TIM2_CH0, TIM2_CH1 (PA0, PA1)
492+
GPIO_InitTypeDef GPIO_InitStructure;
493+
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
494+
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
495+
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
496+
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
497+
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
498+
GPIO_Init(GPIOA, &GPIO_InitStructure);
499+
500+
// Connect TIM2 pins to AF1
501+
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);
502+
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);
503+
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM2);
504+
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM2);
505+
}
482506

483507
// Compute the prescaler value so TIM2 runs at 100kHz
484508
uint16_t PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 100000) - 1;
@@ -491,16 +515,22 @@ void servo_init(void) {
491515
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
492516
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
493517

494-
// PWM1 Mode configuration: Channel1
518+
// PWM Mode configuration
495519
TIM_OCInitTypeDef TIM_OCInitStructure;
496520
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
497521
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
498522
TIM_OCInitStructure.TIM_Pulse = 150; // units of 10us
499523
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
500-
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
524+
TIM_OC1Init(TIM2, &TIM_OCInitStructure); // channel 1
525+
TIM_OC2Init(TIM2, &TIM_OCInitStructure); // channel 2
526+
TIM_OC3Init(TIM2, &TIM_OCInitStructure); // channel 3
527+
TIM_OC4Init(TIM2, &TIM_OCInitStructure); // channel 4
501528

502529
// ?
503-
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
530+
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); // channel 1
531+
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); // channel 2
532+
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); // channel 3
533+
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); // channel 4
504534

505535
// ?
506536
TIM_ARRPreloadConfig(TIM2, ENABLE);
@@ -509,11 +539,17 @@ void servo_init(void) {
509539
TIM_Cmd(TIM2, ENABLE);
510540
}
511541

512-
py_obj_t pyb_servo_set(py_obj_t value) {
542+
py_obj_t pyb_servo_set(py_obj_t port, py_obj_t value) {
543+
int p = py_obj_get_int(port);
513544
int v = py_obj_get_int(value);
514545
if (v < 100) { v = 100; }
515546
if (v > 200) { v = 200; }
516-
TIM2->CCR3 = v;
547+
switch (p) {
548+
case 1: TIM2->CCR1 = v; break;
549+
case 2: TIM2->CCR2 = v; break;
550+
case 3: TIM2->CCR3 = v; break;
551+
case 4: TIM2->CCR4 = v; break;
552+
}
517553
return py_const_none;
518554
}
519555

@@ -527,19 +563,33 @@ py_obj_t pyb_pwm_set(py_obj_t period, py_obj_t pulse) {
527563

528564
#define MMA_ADDR (0x4c)
529565

566+
int mma_buf[12];
567+
530568
py_obj_t pyb_mma_read(void) {
569+
for (int i = 0; i <= 6; i += 3) {
570+
mma_buf[0 + i] = mma_buf[0 + i + 3];
571+
mma_buf[1 + i] = mma_buf[1 + i + 3];
572+
mma_buf[2 + i] = mma_buf[2 + i + 3];
573+
}
574+
531575
mma_start(MMA_ADDR, 1);
532576
mma_send_byte(0);
533577
mma_restart(MMA_ADDR, 0);
534-
py_obj_t data[4];
535-
for (int i = 3; i >= 1; i--) {
578+
for (int i = 0; i <= 2; i++) {
536579
int v = mma_read_ack() & 0x3f;
537580
if (v & 0x20) {
538581
v |= ~0x1f;
539582
}
540-
data[i] = py_obj_new_int(v);
583+
mma_buf[9 + i] = v;
541584
}
542-
data[0] = py_obj_new_int(mma_read_nack());
585+
int jolt_info = mma_read_nack();
586+
587+
py_obj_t data[4];
588+
data[0] = py_obj_new_int(jolt_info);
589+
data[1] = py_obj_new_int(mma_buf[2] + mma_buf[5] + mma_buf[8] + mma_buf[11]);
590+
data[2] = py_obj_new_int(mma_buf[1] + mma_buf[4] + mma_buf[7] + mma_buf[10]);
591+
data[3] = py_obj_new_int(mma_buf[0] + mma_buf[3] + mma_buf[6] + mma_buf[9]);
592+
543593
return rt_build_tuple(4, data); // items in reverse order in data
544594
}
545595

@@ -742,7 +792,7 @@ int main(void) {
742792
rt_init();
743793

744794
// LCD init
745-
lcd_init();
795+
//lcd_init(); disabled while servos on PA0 PA1
746796

747797
// servo
748798
servo_init();
@@ -770,7 +820,7 @@ int main(void) {
770820
rt_store_attr(m, qstr_from_str_static("delay"), rt_make_function_1(pyb_delay));
771821
rt_store_attr(m, qstr_from_str_static("led"), rt_make_function_1(pyb_led));
772822
rt_store_attr(m, qstr_from_str_static("sw"), rt_make_function_0(pyb_sw));
773-
rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_1(pyb_servo_set));
823+
rt_store_attr(m, qstr_from_str_static("servo"), rt_make_function_2(pyb_servo_set));
774824
rt_store_attr(m, qstr_from_str_static("pwm"), rt_make_function_2(pyb_pwm_set));
775825
rt_store_attr(m, qstr_from_str_static("mma"), rt_make_function_0(pyb_mma_read));
776826
rt_store_attr(m, qstr_from_str_static("hid"), rt_make_function_1(pyb_hid_send_report));
@@ -1126,7 +1176,7 @@ int main(void) {
11261176
// SD card testing
11271177
if (1) {
11281178
extern void sdio_init(void);
1129-
sdio_init();
1179+
//sdio_init();
11301180
}
11311181

11321182
printf("PYB: sync filesystems\n");

0 commit comments

Comments
 (0)