usart.c 7.67 KB
Newer Older
1
2
3
#include <stdio.h>
#include <string.h>

4
5
6
#include "stm32f4xx_hal.h"

#include "nlr.h"
7
8
9
10
11
12
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "usart.h"

Dave Hylands's avatar
Dave Hylands committed
13
14
15
16
17
18
struct _pyb_usart_obj_t {
    mp_obj_base_t base;
    pyb_usart_t usart_id;
    bool is_enabled;
    UART_HandleTypeDef handle;
};
19

Dave Hylands's avatar
Dave Hylands committed
20
pyb_usart_obj_t *pyb_usart_global_debug = NULL;
21

22
bool usart_init(pyb_usart_obj_t *usart_obj, uint32_t baudrate) {
23
24
25
26
27
28
    USART_TypeDef *USARTx=NULL;

    uint32_t GPIO_Pin=0;
    uint8_t  GPIO_AF_USARTx=0;
    GPIO_TypeDef* GPIO_Port=NULL;

Dave Hylands's avatar
Dave Hylands committed
29
    switch (usart_obj->usart_id) {
30
        // USART1 is on PA9/PA10, PB6/PB7
31
32
        case PYB_USART_1:
            USARTx = USART1;
33
            GPIO_AF_USARTx = GPIO_AF7_USART1;
34

35
36
37
38
#if defined(PYBV10)
            GPIO_Port = GPIOB;
            GPIO_Pin = GPIO_PIN_6 | GPIO_PIN_7;
#else
39
40
            GPIO_Port = GPIOA;
            GPIO_Pin = GPIO_PIN_9 | GPIO_PIN_10;
41
#endif
42
43
44

            __USART1_CLK_ENABLE();
            break;
45
46
47

#if !defined(PYBV10)
        // USART2 is on PA2/PA3, PD5/PD6
48
49
        case PYB_USART_2:
            USARTx = USART2;
50
            GPIO_AF_USARTx = GPIO_AF7_USART2;
51
52
53
54
55
56

            GPIO_Port = GPIOD;
            GPIO_Pin = GPIO_PIN_5 | GPIO_PIN_6;

            __USART2_CLK_ENABLE();
            break;
57
58
59
#endif

        // USART3 is on PB10/PB11, PC10/PC11, PD8/PD9
60
61
        case PYB_USART_3:
            USARTx = USART3;
62
            GPIO_AF_USARTx = GPIO_AF7_USART3;
63

64
#if defined(PYBV3) || defined(PYBV4) | defined(PYBV10)
65
66
67
68
69
70
71
72
            GPIO_Port = GPIOB;
            GPIO_Pin = GPIO_PIN_10 | GPIO_PIN_11;
#else
            GPIO_Port = GPIOD;
            GPIO_Pin = GPIO_PIN_8 | GPIO_PIN_9;
#endif
            __USART3_CLK_ENABLE();
            break;
73
74
75
76
77
78
79
80
81
82
83
84
85

        // USART4 is on PA0/PA1, PC10/PC11
        case PYB_USART_4:
            USARTx = UART4;
            GPIO_AF_USARTx = GPIO_AF8_UART4;

            GPIO_Port = GPIOA;
            GPIO_Pin = GPIO_PIN_0 | GPIO_PIN_1;

            __UART4_CLK_ENABLE();
            break;

        // USART6 is on PC6/PC7
86
87
        case PYB_USART_6:
            USARTx = USART6;
88
            GPIO_AF_USARTx = GPIO_AF8_USART6;
89
90
91
92
93
94

            GPIO_Port = GPIOC;
            GPIO_Pin = GPIO_PIN_6 | GPIO_PIN_7;

            __USART6_CLK_ENABLE();
            break;
95
96
97

        default:
            return false;
98
99
    }

100
101
    // Initialize USARTx

102
103
104
105
106
107
108
109
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Pin = GPIO_Pin;
    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    GPIO_InitStructure.Alternate = GPIO_AF_USARTx;
    HAL_GPIO_Init(GPIO_Port, &GPIO_InitStructure);

Dave Hylands's avatar
Dave Hylands committed
110
111
112
113
114
115
116
117
118
119
120
    UART_HandleTypeDef *uh = &usart_obj->handle;
    memset(uh, 0, sizeof(*uh));
    uh->Instance = USARTx;
    uh->Init.BaudRate = baudrate;
    uh->Init.WordLength = USART_WORDLENGTH_8B;
    uh->Init.StopBits = USART_STOPBITS_1;
    uh->Init.Parity = USART_PARITY_NONE;
    uh->Init.Mode = USART_MODE_TX_RX;
    uh->Init.HwFlowCtl = UART_HWCONTROL_NONE;
    uh->Init.OverSampling = UART_OVERSAMPLING_16;
    HAL_UART_Init(uh);
121
122

    return true;
123
124
}

Dave Hylands's avatar
Dave Hylands committed
125
126
bool usart_rx_any(pyb_usart_obj_t *usart_obj) {
    return __HAL_UART_GET_FLAG(&usart_obj->handle, USART_FLAG_RXNE);
127
128
}

Dave Hylands's avatar
Dave Hylands committed
129
int usart_rx_char(pyb_usart_obj_t *usart_obj) {
130
    uint8_t ch;
Dave Hylands's avatar
Dave Hylands committed
131
    if (HAL_UART_Receive(&usart_obj->handle, &ch, 1, 0) != HAL_OK) {
132
133
134
135
136
        ch = 0;
    }
    return ch;
}

Dave Hylands's avatar
Dave Hylands committed
137
void usart_tx_char(pyb_usart_obj_t *usart_obj, int c) {
138
    uint8_t ch = c;
Dave Hylands's avatar
Dave Hylands committed
139
    HAL_UART_Transmit(&usart_obj->handle, &ch, 1, 100000);
140
141
}

Dave Hylands's avatar
Dave Hylands committed
142
void usart_tx_str(pyb_usart_obj_t *usart_obj, const char *str) {
143
    for (; *str; str++) {
Dave Hylands's avatar
Dave Hylands committed
144
        usart_tx_char(usart_obj, *str);
145
146
147
    }
}

Dave Hylands's avatar
Dave Hylands committed
148
void usart_tx_strn(pyb_usart_obj_t *usart_obj, const char *str, uint len) {
149
    for (; len > 0; str++, len--) {
Dave Hylands's avatar
Dave Hylands committed
150
        usart_tx_char(usart_obj, *str);
151
152
153
    }
}

Dave Hylands's avatar
Dave Hylands committed
154
void usart_tx_strn_cooked(pyb_usart_obj_t *usart_obj, const char *str, uint len) {
155
156
    for (const char *top = str + len; str < top; str++) {
        if (*str == '\n') {
Dave Hylands's avatar
Dave Hylands committed
157
            usart_tx_char(usart_obj, '\r');
158
        }
Dave Hylands's avatar
Dave Hylands committed
159
        usart_tx_char(usart_obj, *str);
160
161
162
163
164
165
    }
}

/******************************************************************************/
/* Micro Python bindings                                                      */

166
167
168
169
170
171
172
173
STATIC void usart_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
    pyb_usart_obj_t *self = self_in;
    print(env, "<Usart %lu>", self->usart_id);
}

STATIC mp_obj_t usart_obj_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
    // check arguments
    if (!(n_args == 2 && n_kw == 0)) {
174
        nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Usart accepts 2 arguments"));
175
176
    }

177
    // create object
178
179
180
    pyb_usart_obj_t *o = m_new_obj(pyb_usart_obj_t);
    o->base.type = &pyb_usart_type;

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    // work out port
    o->usart_id = 0;
    if (MP_OBJ_IS_STR(args[0])) {
        const char *port = mp_obj_str_get_str(args[0]);
        if (strcmp(port, "XA") == 0) {
            o->usart_id = PYB_USART_XA;
        } else if (strcmp(port, "XB") == 0) {
            o->usart_id = PYB_USART_XB;
        } else if (strcmp(port, "YA") == 0) {
            o->usart_id = PYB_USART_YA;
        } else if (strcmp(port, "YB") == 0) {
            o->usart_id = PYB_USART_YB;
        } else {
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Usart port %s does not exist", port));
        }
    } else if (MP_OBJ_IS_INT(args[0])) {
        o->usart_id = mp_obj_get_int(args[0]);
    }

    // init USART (if it fails, it's because the port doesn't exist)
    if (!usart_init(o, mp_obj_get_int(args[1]))) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Usart port %d does not exist", o->usart_id));
    }

    o->is_enabled = true;
206
207
208
209
210

    return o;
}

STATIC mp_obj_t usart_obj_status(mp_obj_t self_in) {
211
    pyb_usart_obj_t *self = self_in;
Dave Hylands's avatar
Dave Hylands committed
212
    if (usart_rx_any(self)) {
213
214
215
216
217
218
        return mp_const_true;
    } else {
        return mp_const_false;
    }
}

219
STATIC mp_obj_t usart_obj_rx_char(mp_obj_t self_in) {
220
221
222
223
    mp_obj_t ret = mp_const_none;
    pyb_usart_obj_t *self = self_in;

    if (self->is_enabled) {
Dave Hylands's avatar
Dave Hylands committed
224
        ret =  mp_obj_new_int(usart_rx_char(self));
225
226
227
228
    }
    return ret;
}

229
STATIC mp_obj_t usart_obj_tx_char(mp_obj_t self_in, mp_obj_t c) {
230
231
232
233
    pyb_usart_obj_t *self = self_in;
    uint len;
    const char *str = mp_obj_str_get_data(c, &len);
    if (len == 1 && self->is_enabled) {
Dave Hylands's avatar
Dave Hylands committed
234
        usart_tx_char(self, str[0]);
235
236
237
238
    }
    return mp_const_none;
}

239
STATIC mp_obj_t usart_obj_tx_str(mp_obj_t self_in, mp_obj_t s) {
240
241
242
243
244
    pyb_usart_obj_t *self = self_in;
    if (self->is_enabled) {
        if (MP_OBJ_IS_STR(s)) {
            uint len;
            const char *data = mp_obj_str_get_data(s, &len);
Dave Hylands's avatar
Dave Hylands committed
245
            usart_tx_strn(self, data, len);
246
247
248
249
250
        }
    }
    return mp_const_none;
}

251
252
253
254
STATIC MP_DEFINE_CONST_FUN_OBJ_1(usart_obj_status_obj, usart_obj_status);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(usart_obj_rx_char_obj, usart_obj_rx_char);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(usart_obj_tx_char_obj, usart_obj_tx_char);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(usart_obj_tx_str_obj, usart_obj_tx_str);
255

256
257
258
259
260
STATIC const mp_map_elem_t usart_locals_dict_table[] = {
    { MP_OBJ_NEW_QSTR(MP_QSTR_status), (mp_obj_t)&usart_obj_status_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_recv_chr), (mp_obj_t)&usart_obj_rx_char_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_send_chr), (mp_obj_t)&usart_obj_tx_char_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&usart_obj_tx_str_obj },
261
262
};

263
264
STATIC MP_DEFINE_CONST_DICT(usart_locals_dict, usart_locals_dict_table);

265
const mp_obj_type_t pyb_usart_type = {
266
267
268
    { &mp_type_type },
    .name = MP_QSTR_Usart,
    .print = usart_obj_print,
269
    .make_new = usart_obj_make_new,
270
    .locals_dict = (mp_obj_t)&usart_locals_dict,
271
};