Hsieh JenYun
/
446STM32_PC_I2CS
V1
Fork of 446STM32_Print_PC_I2CS by
main.cpp@1:51fb8c086992, 2017-11-06 (annotated)
- Committer:
- Tanakacool
- Date:
- Mon Nov 06 06:05:09 2017 +0000
- Revision:
- 1:51fb8c086992
- Parent:
- 0:8be7c91879df
- Child:
- 2:764981c589b6
I2CS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Tanakacool | 1:51fb8c086992 | 1 | #include <mbed.h> |
Tanakacool | 1:51fb8c086992 | 2 | |
Tanakacool | 0:8be7c91879df | 3 | I2CSlave slave(PC_9, PA_8); |
Tanakacool | 1:51fb8c086992 | 4 | DigitalOut myled(LED1); |
Tanakacool | 1:51fb8c086992 | 5 | Thread thread; |
Tanakacool | 1:51fb8c086992 | 6 | Serial pc(USBTX, USBRX); |
Tanakacool | 1:51fb8c086992 | 7 | |
Tanakacool | 1:51fb8c086992 | 8 | void slave_thread(){ |
Tanakacool | 0:8be7c91879df | 9 | char buffer[128]; |
Tanakacool | 1:51fb8c086992 | 10 | while(true){ |
Tanakacool | 1:51fb8c086992 | 11 | Thread::signal_wait(1); |
Tanakacool | 1:51fb8c086992 | 12 | |
Tanakacool | 0:8be7c91879df | 13 | if(pc.readable()) { |
Tanakacool | 0:8be7c91879df | 14 | slave.stop(); |
Tanakacool | 0:8be7c91879df | 15 | pc.gets(buffer, 128); |
Tanakacool | 0:8be7c91879df | 16 | pc.printf("I got '%s'\n", buffer); |
Tanakacool | 1:51fb8c086992 | 17 | printf("get_state osStatus: %d\n",thread.get_state ()); |
Tanakacool | 0:8be7c91879df | 18 | } |
Tanakacool | 1:51fb8c086992 | 19 | } |
Tanakacool | 1:51fb8c086992 | 20 | } |
Tanakacool | 1:51fb8c086992 | 21 | int main() { |
Tanakacool | 1:51fb8c086992 | 22 | char all_buf[]="i'm here!"; |
Tanakacool | 1:51fb8c086992 | 23 | char buf[20]; |
Tanakacool | 1:51fb8c086992 | 24 | char msg[] = "Hi Master!, this is 0xA0 response"; |
Tanakacool | 1:51fb8c086992 | 25 | slave.address(0xA0); |
Tanakacool | 1:51fb8c086992 | 26 | thread.start(slave_thread); |
Tanakacool | 1:51fb8c086992 | 27 | while (1) { |
Tanakacool | 1:51fb8c086992 | 28 | int i = slave.receive(); |
Tanakacool | 1:51fb8c086992 | 29 | if(i==1||i==2||i==3){ |
Tanakacool | 1:51fb8c086992 | 30 | thread.signal_clr(0); |
Tanakacool | 1:51fb8c086992 | 31 | switch (i) { |
Tanakacool | 0:8be7c91879df | 32 | case I2CSlave::ReadAddressed: |
Tanakacool | 0:8be7c91879df | 33 | if(!slave.write(msg, strlen(msg) + 1)) // Includes null char |
Tanakacool | 0:8be7c91879df | 34 | slave.stop(); |
Tanakacool | 0:8be7c91879df | 35 | printf("ReadAddressed\n"); |
Tanakacool | 0:8be7c91879df | 36 | //slave.stop(); |
Tanakacool | 1:51fb8c086992 | 37 | thread.signal_set(1); |
Tanakacool | 0:8be7c91879df | 38 | break; |
Tanakacool | 0:8be7c91879df | 39 | case I2CSlave::WriteGeneral: |
Tanakacool | 0:8be7c91879df | 40 | if(!slave.write(all_buf, strlen(all_buf) + 1)) // Includes null char |
Tanakacool | 0:8be7c91879df | 41 | slave.stop(); |
Tanakacool | 0:8be7c91879df | 42 | printf("i've received the command...\n"); |
Tanakacool | 1:51fb8c086992 | 43 | thread.signal_set(1); |
Tanakacool | 0:8be7c91879df | 44 | break; |
Tanakacool | 0:8be7c91879df | 45 | case I2CSlave::WriteAddressed: |
Tanakacool | 0:8be7c91879df | 46 | slave.read(buf, 20); |
Tanakacool | 0:8be7c91879df | 47 | printf("master write to A: %s\n", buf); |
Tanakacool | 1:51fb8c086992 | 48 | thread.signal_set(1); |
Tanakacool | 0:8be7c91879df | 49 | break; |
Tanakacool | 0:8be7c91879df | 50 | } |
Tanakacool | 1:51fb8c086992 | 51 | }else{ |
Tanakacool | 1:51fb8c086992 | 52 | //thread.signal_set(1); |
Tanakacool | 1:51fb8c086992 | 53 | } |
Tanakacool | 0:8be7c91879df | 54 | for(int i = 0; i < 10; i++) { |
Tanakacool | 0:8be7c91879df | 55 | buf[i] = 0; // Clear buffer |
Tanakacool | 0:8be7c91879df | 56 | } |
Tanakacool | 1:51fb8c086992 | 57 | } |
Tanakacool | 0:8be7c91879df | 58 | } |