blob: 0faec5a35c63c805a290094f22f5dbbb1177b6a0 [file] [log] [blame]
Armando Montanez1a6727b2020-08-12 13:26:36 -07001// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
Armando Montaneza9ca9992021-01-26 17:06:10 -080014#include "pw_cpu_exception_cortex_m/cpu_state.h"
15#include "pw_cpu_exception_cortex_m_protos/cpu_state.pwpb.h"
Armando Montanez1a6727b2020-08-12 13:26:36 -070016#include "pw_preprocessor/compiler.h"
17#include "pw_protobuf/encoder.h"
18
19namespace pw::cpu_exception {
20
21Status DumpCpuStateProto(protobuf::Encoder& dest,
Armando Montaneza9ca9992021-01-26 17:06:10 -080022 const pw_cpu_exception_State& cpu_state) {
23 cortex_m::ArmV7mCpuState::Encoder state_encoder(&dest);
Armando Montanez1a6727b2020-08-12 13:26:36 -070024
25 // Special and mem-mapped registers.
Armando Montanez1a6727b2020-08-12 13:26:36 -070026 state_encoder.WritePc(cpu_state.base.pc);
27 state_encoder.WriteLr(cpu_state.base.lr);
28 state_encoder.WritePsr(cpu_state.base.psr);
Armando Montanez4159d1e2020-09-10 12:08:49 -070029 state_encoder.WriteMsp(cpu_state.extended.msp);
30 state_encoder.WritePsp(cpu_state.extended.psp);
31 state_encoder.WriteExcReturn(cpu_state.extended.exc_return);
32 state_encoder.WriteCfsr(cpu_state.extended.cfsr);
Armando Montanez1a6727b2020-08-12 13:26:36 -070033 state_encoder.WriteMmfar(cpu_state.extended.mmfar);
34 state_encoder.WriteBfar(cpu_state.extended.bfar);
35 state_encoder.WriteIcsr(cpu_state.extended.icsr);
Armando Montanez4159d1e2020-09-10 12:08:49 -070036 state_encoder.WriteHfsr(cpu_state.extended.hfsr);
37 state_encoder.WriteShcsr(cpu_state.extended.shcsr);
Armando Montanez1a6727b2020-08-12 13:26:36 -070038 state_encoder.WriteControl(cpu_state.extended.control);
39
40 // General purpose registers.
41 state_encoder.WriteR0(cpu_state.base.r0);
42 state_encoder.WriteR1(cpu_state.base.r1);
43 state_encoder.WriteR2(cpu_state.base.r2);
44 state_encoder.WriteR3(cpu_state.base.r3);
45 state_encoder.WriteR4(cpu_state.extended.r4);
46 state_encoder.WriteR5(cpu_state.extended.r5);
47 state_encoder.WriteR6(cpu_state.extended.r6);
48 state_encoder.WriteR7(cpu_state.extended.r7);
49 state_encoder.WriteR8(cpu_state.extended.r8);
50 state_encoder.WriteR9(cpu_state.extended.r9);
51 state_encoder.WriteR10(cpu_state.extended.r10);
52 state_encoder.WriteR11(cpu_state.extended.r11);
53
54 // If the encode buffer was exhausted in an earlier write, it will be
55 // reflected here.
56 Status status = state_encoder.WriteR12(cpu_state.base.r12);
57 if (!status.ok()) {
Wyatt Heplerf276c6b2020-11-11 21:13:38 -080058 return status.IsResourceExhausted() ? Status::ResourceExhausted()
59 : Status::Unknown();
Armando Montanez1a6727b2020-08-12 13:26:36 -070060 }
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080061 return OkStatus();
Armando Montanez1a6727b2020-08-12 13:26:36 -070062}
63
64} // namespace pw::cpu_exception