Skip to content

Commit c8ca6c3

Browse files
committed
Merge branch 'master' of https://github.com/mbedmicro/mbed
2 parents 45369fa + 6b044ad commit c8ca6c3

File tree

10 files changed

+169
-64
lines changed

10 files changed

+169
-64
lines changed

libraries/mbed/api/BusIn.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,13 @@ class BusIn {
5151
* An integer with each bit corresponding to the value read from the associated DigitalIn pin
5252
*/
5353
int read();
54-
54+
55+
/** Set the input pin mode
56+
*
57+
* @param mode PullUp, PullDown, PullNone
58+
*/
59+
void mode(PinMode pull);
60+
5561
#ifdef MBED_OPERATORS
5662
/** A shorthand for read()
5763
*/

libraries/mbed/common/BusIn.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ int BusIn::read() {
4949
return v;
5050
}
5151

52+
void BusIn::mode(PinMode pull) {
53+
for (int i=0; i<16; i++) {
54+
if (_pin[i] != 0) {
55+
_pin[i]->mode(pull);
56+
}
57+
}
58+
}
59+
5260
#ifdef MBED_OPERATORS
5361
BusIn::operator int() {
5462
return read();

libraries/mbed/common/gpio.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@
1818
static inline void _gpio_init_in(gpio_t* gpio, PinName pin, PinMode mode)
1919
{
2020
gpio_init(gpio, pin);
21-
gpio_dir(gpio, PIN_INPUT);
22-
gpio_mode(gpio, mode);
21+
if (pin != NC) {
22+
gpio_dir(gpio, PIN_INPUT);
23+
gpio_mode(gpio, mode);
24+
}
2325
}
2426

2527
static inline void _gpio_init_out(gpio_t* gpio, PinName pin, PinMode mode, int value)
2628
{
2729
gpio_init(gpio, pin);
28-
gpio_write(gpio, value);
29-
gpio_dir(gpio, PIN_OUTPUT);
30-
gpio_mode(gpio, mode);
30+
if (pin != NC) {
31+
gpio_write(gpio, value);
32+
gpio_dir(gpio, PIN_OUTPUT);
33+
gpio_mode(gpio, mode);
34+
}
3135
}
3236

3337
void gpio_init_in(gpio_t* gpio, PinName pin) {
@@ -49,7 +53,8 @@ void gpio_init_out_ex(gpio_t* gpio, PinName pin, int value) {
4953
void gpio_init_inout(gpio_t* gpio, PinName pin, PinDirection direction, PinMode mode, int value) {
5054
if (direction == PIN_INPUT) {
5155
_gpio_init_in(gpio, pin, mode);
52-
gpio_write(gpio, value); // we prepare the value in case it is switched later
56+
if (pin != NC)
57+
gpio_write(gpio, value); // we prepare the value in case it is switched later
5358
} else {
5459
_gpio_init_out(gpio, pin, mode, value);
5560
}

libraries/mbed/targets/hal/TARGET_STM/TARGET_DISCO_F407VG/pwmout_api.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static const PinMap PinMap_PWM[] = {
4343
{PA_2, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3
4444
//{PA_2, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH3
4545
//{PA_2, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH1
46-
{PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH3
46+
{PA_3, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH4
4747
//{PA_3, PWM_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM5)}, // TIM5_CH4
4848
//{PA_3, PWM_9, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF3_TIM9)}, // TIM9_CH2
4949
{PA_5, PWM_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_TIM2)}, // TIM2_CH1
@@ -77,7 +77,7 @@ static const PinMap PinMap_PWM[] = {
7777
{PC_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH1
7878
{PC_7, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH2
7979
{PC_8, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3
80-
{PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH3
80+
{PC_9, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF2_TIM3)}, // TIM3_CH4
8181

8282
{NC, NC, 0}
8383
};
@@ -183,13 +183,13 @@ void pwmout_write(pwmout_t* obj, float value) {
183183
break;
184184
// Channels 3
185185
case PA_2:
186-
case PA_3:
186+
//case PA_3:
187187
case PA_10:
188188
//case PB_0:
189189
case PB_8:
190190
case PB_10:
191191
case PC_8:
192-
case PC_9:
192+
//case PC_9:
193193
channel = TIM_CHANNEL_3;
194194
break;
195195
// Channels 3N
@@ -199,10 +199,11 @@ void pwmout_write(pwmout_t* obj, float value) {
199199
complementary_channel = 1;
200200
break;
201201
// Channels 4
202-
//case PA_3:
202+
case PA_3:
203203
case PA_11:
204204
//case PB_1:
205205
case PB_9:
206+
case PC_9:
206207
channel = TIM_CHANNEL_4;
207208
break;
208209
default:

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F302R8/analogout_api.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "pinmap.h"
3434
#include "error.h"
3535

36-
#define RANGE_12BIT (0xFFF)
36+
#define DAC_RANGE (0xFFF) // 12 bits
3737

3838
static const PinMap PinMap_DAC[] = {
3939
{PA_4, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT1
@@ -56,7 +56,7 @@ void analogout_init(dac_t *obj, PinName pin) {
5656
// Configure GPIO
5757
pinmap_pinout(pin, PinMap_DAC);
5858

59-
// Save the channel for the write and read functions
59+
// Save the channel for future use
6060
obj->channel = pin;
6161

6262
// Enable DAC clock
@@ -71,6 +71,12 @@ void analogout_init(dac_t *obj, PinName pin) {
7171
}
7272

7373
void analogout_free(dac_t *obj) {
74+
DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac);
75+
// Disable DAC
76+
DAC_DeInit(dac);
77+
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, DISABLE);
78+
// Configure GPIO
79+
pin_function(obj->channel, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
7480
}
7581

7682
static inline void dac_write(dac_t *obj, uint16_t value) {
@@ -87,23 +93,23 @@ void analogout_write(dac_t *obj, float value) {
8793
if (value < 0.0f) {
8894
dac_write(obj, 0); // Min value
8995
} else if (value > 1.0f) {
90-
dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
96+
dac_write(obj, (uint16_t)DAC_RANGE); // Max value
9197
} else {
92-
dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT));
98+
dac_write(obj, (uint16_t)(value * (float)DAC_RANGE));
9399
}
94100
}
95101

96102
void analogout_write_u16(dac_t *obj, uint16_t value) {
97-
if (value > (uint16_t)RANGE_12BIT) {
98-
dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
103+
if (value > (uint16_t)DAC_RANGE) {
104+
dac_write(obj, (uint16_t)DAC_RANGE); // Max value
99105
} else {
100106
dac_write(obj, value);
101107
}
102108
}
103109

104110
float analogout_read(dac_t *obj) {
105111
uint32_t value = dac_read(obj);
106-
return (float)value * (1.0f / (float)RANGE_12BIT);
112+
return (float)((float)value * (1.0f / (float)DAC_RANGE));
107113
}
108114

109115
uint16_t analogout_read_u16(dac_t *obj) {
Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
#include "mbed.h"
22
#include "MMA8451Q.h"
3-
4-
#define MMA8451_I2C_ADDRESS (0x1d<<1)
3+
#include "test_env.h"
54

65
#ifdef TARGET_KL05Z
7-
#define SDA PTB4
8-
#define SCL PTB3
6+
#define SDA PTB4
7+
#define SCL PTB3
98
#else
10-
#define SDA PTE25
11-
#define SCL PTE24
9+
#define SDA PTE25
10+
#define SCL PTE24
1211
#endif
1312

13+
namespace {
14+
const int MMA8451_I2C_ADDRESS = 0x1D << 1; // I2C bus address
15+
const float MMA8451_DIGITAL_SENSITIVITY = 4096.0; // Counts/g
16+
}
17+
18+
float calc_3d_vector_len(float x, float y, float z) {
19+
return sqrt(x*x + y*y + z*z);
20+
}
21+
22+
#define TEST_ITERATIONS 25
23+
#define TEST_ITERATIONS_SKIP 5
24+
#define MEASURE_DEVIATION_TOLERANCE 0.025 // 2.5%
25+
1426
int main(void) {
1527
DigitalOut led(LED_GREEN);
1628
MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
17-
printf("WHO AM I: 0x%2X\r\n", acc.getWhoAmI());
18-
19-
while (true) {
20-
printf("-----------\r\n");
21-
printf("acc_x: %d\r\n", acc.getAccX());
22-
printf("acc_y: %d\r\n", acc.getAccY());
23-
printf("acc_z: %d\r\n", acc.getAccZ());
29+
bool result = true;
30+
printf("WHO AM I: 0x%2X\r\n\n", acc.getWhoAmI());
2431

25-
wait(1);
32+
for (int i = 0; i < TEST_ITERATIONS; i++) {
33+
if (i < TEST_ITERATIONS_SKIP) {
34+
// Skip first 5 measurements
35+
continue;
36+
}
37+
const float g_vect_len = calc_3d_vector_len(acc.getAccX(), acc.getAccY(), acc.getAccZ()) / MMA8451_DIGITAL_SENSITIVITY;
38+
const float deviation = fabs(g_vect_len - 1.0);
39+
const char *succes_str = deviation <= MEASURE_DEVIATION_TOLERANCE ? "[OK]" : "[FAIL]";
40+
result = result && (deviation <= MEASURE_DEVIATION_TOLERANCE);
41+
printf("X:% 6d Y:% 6d Z:% 5d GF:%0.3fg, dev:%0.3f ... %s\r\n", acc.getAccX(), acc.getAccY(), acc.getAccZ(), g_vect_len, deviation, succes_str);
42+
wait(0.5);
2643
led = !led;
2744
}
45+
notify_completion(result);
2846
}
Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,69 @@
11
#include "mbed.h"
22
#include "EthernetInterface.h"
33

4-
const char* ECHO_SERVER_ADDRESS = "10.2.200.57";
5-
const int ECHO_PORT = 7;
4+
struct s_ip_address
5+
{
6+
int ip_1;
7+
int ip_2;
8+
int ip_3;
9+
int ip_4;
10+
};
11+
12+
#define MAX_ECHO_LOOPS 100
613

714
int main() {
15+
char buffer[256] = {0};
16+
char out_buffer[] = "Hello World\n";
17+
char out_success[] = "{{success}}\n{{end}}\n";
18+
char out_failure[] = "{{failure}}\n{{end}}\n";
19+
s_ip_address ip_addr = {0, 0, 0, 0};
20+
int port = 0;
21+
22+
printf("TCPCllient waiting for server IP and port...\r\n");
23+
scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port);
24+
printf("Address received:%d.%d.%d.%d:%d\r\n", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port);
25+
826
EthernetInterface eth;
927
eth.init(); //Use DHCP
1028
eth.connect();
11-
printf("IP Address is %s\n", eth.getIPAddress());
12-
29+
30+
printf("TCPClient IP Address is %s\r\n", eth.getIPAddress());
31+
sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4);
32+
1333
TCPSocketConnection socket;
14-
15-
while (true) {
16-
while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_PORT) < 0) {
17-
printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_PORT);
18-
wait(1);
19-
}
20-
21-
char hello[] = "Hello World\n";
22-
socket.send_all(hello, sizeof(hello) - 1);
23-
24-
char buf[256];
25-
int n = socket.receive(buf, 256);
26-
buf[n] = '\0';
27-
printf("%s", buf);
28-
29-
socket.close();
34+
while (socket.connect(buffer, port) < 0) {
35+
printf("TCPCllient unable to connect to %s:%d\r\n", buffer, port);
3036
wait(1);
3137
}
32-
38+
39+
// Test loop for multiple client conenctions
40+
bool result = true;
41+
int count_error = 0;
42+
for (int i = 0; i < MAX_ECHO_LOOPS; i++) {
43+
socket.send_all(out_buffer, sizeof(out_buffer) - 1);
44+
45+
int n = socket.receive(buffer, sizeof(buffer));
46+
if (n > 0)
47+
{
48+
buffer[n] = '\0';
49+
printf("%s", buffer);
50+
bool echoed = strncmp(out_buffer, buffer, sizeof(out_buffer) - 1) == 0;
51+
result = result && echoed;
52+
if (echoed == false) {
53+
count_error++; // Count error messages
54+
}
55+
}
56+
}
57+
58+
printf("Loop messages passed: %d/%d\r\n", MAX_ECHO_LOOPS - count_error, MAX_ECHO_LOOPS);
59+
60+
if (result) {
61+
socket.send_all(out_success, sizeof(out_success) - 1);
62+
}
63+
else {
64+
socket.send_all(out_failure, sizeof(out_failure) - 1);
65+
}
66+
socket.close();
3367
eth.disconnect();
68+
return 0;
3469
}

libraries/tests/net/protocols/HTTPClient_HelloWorld/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ int main()
3939
{
4040
bool result = true;
4141
const char *url_httpbin_post = "http://httpbin.org/post";
42-
HTTPMap map;
4342
HTTPText text(http_request_buffer, BUFFER_SIZE);
43+
HTTPMap map;
4444
map.put("Hello", "World");
4545
map.put("test", "1234");
4646
printf("HTTP_POST: Trying to post data to '%s' ...\r\n", url_httpbin_post);
4747
const int ret = http.post(url_httpbin_post, map, &text);
4848
if (ret == 0) {
49-
printf("HTTP_POST: Read %d chars ... [OK]\n", strlen(http_request_buffer));
49+
printf("HTTP_POST: Read %d chars ... [OK]\r\n", strlen(http_request_buffer));
5050
printf("HTTP_POST: %s\r\n", http_request_buffer);
5151
}
5252
else {

0 commit comments

Comments
 (0)