Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PWM output #12

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions PS_repository/demo/demo_mcu_I2C/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Demo-mcu-I2C

- Using Logic Analyzer to detect I2C data

***

### How to implement
- Burn the program into Flash through Openocd.:
- (gdb) monitor program mbed-os-example-blinky.bin
- Hardware reset:
- (gdb)monitor reset init
- GDB debugging

### The result
- The resulting is as follows
- ![avatar](/I/gitwenjian/readmepicuture/1.png)
25 changes: 25 additions & 0 deletions PS_repository/demo/demo_mcu_I2C/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"
//#include "stats_report.h"

I2C i2c(PTA11 , PTA12);//I2C_SDA=PTA11,I2C_SCL=PTA12
int main()
{
char cmd[2];
while (1)
{
cmd[0] = 0x01;
cmd[1] = 0x00;
// read and write takes the 8-bit version of the address.
// set up configuration register (at 0x01)
i2c.write(0x00, cmd, 2);
wait(0.1);
// read temperature register
cmd[0] = 0x00;
i2c.write(0x00, cmd, 1);
}
}
16 changes: 16 additions & 0 deletions PS_repository/demo/demo_mcu_SPI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Demo-mcu-SPI

- Using Logic Analyzer to detect SPI data

***

### How to implement
- Burn the program into Flash through Openocd.:
- (gdb) monitor program mbed-os-example-blinky.bin
- Hardware reset:
- (gdb)monitor reset init
- GDB debugging

### The result
- The resulting pwm waveform diagram is as follows
- ![avatar](/I:/gitwenjian/readmepicture/2.png)
25 changes: 25 additions & 0 deletions PS_repository/demo/demo_mcu_SPI/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"

SPI spi(PTA16, PTA17, PTA15); // mosi, miso, sclk
DigitalOut cs(PTA14);
int main() {
// Chip must be deselected
cs = 1;
// Setup the spi for 8 bit data, high steady state clock,
// second edge capture, with a 1MHz clock rate
spi.format(8,3);
spi.frequency(1000000);
// Select the device by seting chip select low
cs = 0;
// Send 0x8f, the command to read the WHOAMI register
spi.write(0x8F);
// Send a dummy byte to receive the contents of the WHOAMI register
int whoami = spi.write(0x00);
// Deselect the device
cs = 1;
}
4 changes: 4 additions & 0 deletions PS_repository/demo/demo_mcu_pwm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.build
.mbed
projectfiles
*.py*
16 changes: 16 additions & 0 deletions PS_repository/demo/demo_mcu_pwm/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"
#include "stats_report.h"

PwmOut pwm1(PTB0);
int main()
{
pwm1.period(4.0f);
//pwm1 = 0.5;
pwm1.write(0.5f);
while(1);
}
16 changes: 16 additions & 0 deletions PS_repository/demo/demo_mcu_pwm/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Demo-mcu-pwm

- Pwm output

***

### How to implement
- Burn the program into Flash through Openocd.:
- (gdb) monitor program mbed-os-example-blinky.bin
- Hardware reset:
- (gdb)monitor reset init
- GDB debugging

### The result
- The resulting pwm waveform diagram is as follows
- http://note.youdao.com/noteshare?id=3ee2ea689fe8607ad82c66d5448f6cc9&sub=SVR4F5942405AD3400AB5FA0ACF82FF4F44
132 changes: 132 additions & 0 deletions PS_repository/demo/demo_mcu_pwm/stats_report.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef STATS_REPORT_H
#define STATS_REPORT

#include "mbed.h"

/**
* System Reporting library. Provides runtime information on device:
* - CPU sleep, idle, and wake times
* - Heap and stack usage
* - Thread information
* - Static system information
*/
class SystemReport {
mbed_stats_heap_t heap_stats;
mbed_stats_cpu_t cpu_stats;
mbed_stats_sys_t sys_stats;

mbed_stats_thread_t *thread_stats;
uint8_t thread_count;
uint8_t max_thread_count;
uint32_t sample_time_ms;

public:
/**
* SystemReport - Sample rate in ms is required to handle the CPU percent awake logic
*/
SystemReport(uint32_t sample_rate) : max_thread_count(8), sample_time_ms(sample_rate)
{
thread_stats = new mbed_stats_thread_t[max_thread_count];

// Collect the static system information
mbed_stats_sys_get(&sys_stats);

printf("=============================== SYSTEM INFO ================================\r\n");
printf("Mbed OS Version: %ld \r\n", sys_stats.os_version);
printf("CPU ID: 0x%lx \r\n", sys_stats.cpu_id);
printf("Compiler ID: %d \r\n", sys_stats.compiler_id);
printf("Compiler Version: %ld \r\n", sys_stats.compiler_version);

for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
if (sys_stats.ram_size[i] != 0) {
printf("RAM%d: Start 0x%lx Size: 0x%lx \r\n", i, sys_stats.ram_start[i], sys_stats.ram_size[i]);
}
}
for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
if (sys_stats.rom_size[i] != 0) {
printf("ROM%d: Start 0x%lx Size: 0x%lx \r\n", i, sys_stats.rom_start[i], sys_stats.rom_size[i]);
}
}
}

~SystemReport(void)
{
free(thread_stats);
}

/**
* Report on each Mbed OS Platform stats API
*/
void report_state(void)
{
report_cpu_stats();
report_heap_stats();
report_thread_stats();

// Clear next line to separate subsequent report logs
printf("\r\n");
}

/**
* Report CPU idle and awake time in terms of percentage
*/
void report_cpu_stats(void)
{
static uint64_t prev_idle_time = 0;

printf("================= CPU STATS =================\r\n");

// Collect and print cpu stats
mbed_stats_cpu_get(&cpu_stats);

uint64_t diff = (cpu_stats.idle_time - prev_idle_time);
uint8_t idle = (diff * 100) / (sample_time_ms * 1000); // usec;
uint8_t usage = 100 - ((diff * 100) / (sample_time_ms * 1000)); // usec;;
prev_idle_time = cpu_stats.idle_time;

printf("Idle: %d%% Usage: %d%% \r\n", idle, usage);
}

/**
* Report current heap stats. Current heap refers to the current amount of
* allocated heap. Max heap refers to the highest amount of heap allocated
* since reset.
*/
void report_heap_stats(void)
{
printf("================ HEAP STATS =================\r\n");

// Collect and print heap stats
mbed_stats_heap_get(&heap_stats);

printf("Current heap: %lu\r\n", heap_stats.current_size);
printf("Max heap size: %lu\r\n", heap_stats.max_size);
}

/**
* Report active thread stats
*/
void report_thread_stats(void)
{
printf("================ THREAD STATS ===============\r\n");

// Collect and print running thread stats
int count = mbed_stats_thread_get_each(thread_stats, max_thread_count);

for (int i = 0; i < count; i++) {
printf("ID: 0x%lx \r\n", thread_stats[i].id);
printf("Name: %s \r\n", thread_stats[i].name);
printf("State: %ld \r\n", thread_stats[i].state);
printf("Priority: %ld \r\n", thread_stats[i].priority);
printf("Stack Size: %ld \r\n", thread_stats[i].stack_size);
printf("Stack Space: %ld \r\n", thread_stats[i].stack_space);
}
}
};

#endif // STATS_REPORT_H